ca0106_main.c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833
  1. /*
  2. * Copyright (c) 2004 James Courtier-Dutton <James@superbug.demon.co.uk>
  3. * Driver CA0106 chips. e.g. Sound Blaster Audigy LS and Live 24bit
  4. * Version: 0.0.25
  5. *
  6. * FEATURES currently supported:
  7. * Front, Rear and Center/LFE.
  8. * Surround40 and Surround51.
  9. * Capture from MIC an LINE IN input.
  10. * SPDIF digital playback of PCM stereo and AC3/DTS works.
  11. * (One can use a standard mono mini-jack to one RCA plugs cable.
  12. * or one can use a standard stereo mini-jack to two RCA plugs cable.
  13. * Plug one of the RCA plugs into the Coax input of the external decoder/receiver.)
  14. * ( In theory one could output 3 different AC3 streams at once, to 3 different SPDIF outputs. )
  15. * Notes on how to capture sound:
  16. * The AC97 is used in the PLAYBACK direction.
  17. * The output from the AC97 chip, instead of reaching the speakers, is fed into the Philips 1361T ADC.
  18. * So, to record from the MIC, set the MIC Playback volume to max,
  19. * unmute the MIC and turn up the MASTER Playback volume.
  20. * So, to prevent feedback when capturing, minimise the "Capture feedback into Playback" volume.
  21. *
  22. * The only playback controls that currently do anything are: -
  23. * Analog Front
  24. * Analog Rear
  25. * Analog Center/LFE
  26. * SPDIF Front
  27. * SPDIF Rear
  28. * SPDIF Center/LFE
  29. *
  30. * For capture from Mic in or Line in.
  31. * Digital/Analog ( switch must be in Analog mode for CAPTURE. )
  32. *
  33. * CAPTURE feedback into PLAYBACK
  34. *
  35. * Changelog:
  36. * Support interrupts per period.
  37. * Removed noise from Center/LFE channel when in Analog mode.
  38. * Rename and remove mixer controls.
  39. * 0.0.6
  40. * Use separate card based DMA buffer for periods table list.
  41. * 0.0.7
  42. * Change remove and rename ctrls into lists.
  43. * 0.0.8
  44. * Try to fix capture sources.
  45. * 0.0.9
  46. * Fix AC3 output.
  47. * Enable S32_LE format support.
  48. * 0.0.10
  49. * Enable playback 48000 and 96000 rates. (Rates other that these do not work, even with "plug:front".)
  50. * 0.0.11
  51. * Add Model name recognition.
  52. * 0.0.12
  53. * Correct interrupt timing. interrupt at end of period, instead of in the middle of a playback period.
  54. * Remove redundent "voice" handling.
  55. * 0.0.13
  56. * Single trigger call for multi channels.
  57. * 0.0.14
  58. * Set limits based on what the sound card hardware can do.
  59. * playback periods_min=2, periods_max=8
  60. * capture hw constraints require period_size = n * 64 bytes.
  61. * playback hw constraints require period_size = n * 64 bytes.
  62. * 0.0.15
  63. * Minor updates.
  64. * 0.0.16
  65. * Implement 192000 sample rate.
  66. * 0.0.17
  67. * Add support for SB0410 and SB0413.
  68. * 0.0.18
  69. * Modified Copyright message.
  70. * 0.0.19
  71. * Finally fix support for SB Live 24 bit. SB0410 and SB0413.
  72. * The output codec needs resetting, otherwise all output is muted.
  73. * 0.0.20
  74. * Merge "pci_disable_device(pci);" fixes.
  75. * 0.0.21
  76. * Add 4 capture channels. (SPDIF only comes in on channel 0. )
  77. * Add SPDIF capture using optional digital I/O module for SB Live 24bit. (Analog capture does not yet work.)
  78. * 0.0.22
  79. * Add support for MSI K8N Diamond Motherboard with onboard SB Live 24bit without AC97. From kiksen, bug #901
  80. * 0.0.23
  81. * Implement support for Line-in capture on SB Live 24bit.
  82. * 0.0.24
  83. * Add support for mute control on SB Live 24bit (cards w/ SPI DAC)
  84. * 0.0.25
  85. * Powerdown SPI DAC channels when not in use
  86. *
  87. * BUGS:
  88. * Some stability problems when unloading the snd-ca0106 kernel module.
  89. * --
  90. *
  91. * TODO:
  92. * 4 Capture channels, only one implemented so far.
  93. * Other capture rates apart from 48khz not implemented.
  94. * MIDI
  95. * --
  96. * GENERAL INFO:
  97. * Model: SB0310
  98. * P17 Chip: CA0106-DAT
  99. * AC97 Codec: STAC 9721
  100. * ADC: Philips 1361T (Stereo 24bit)
  101. * DAC: WM8746EDS (6-channel, 24bit, 192Khz)
  102. *
  103. * GENERAL INFO:
  104. * Model: SB0410
  105. * P17 Chip: CA0106-DAT
  106. * AC97 Codec: None
  107. * ADC: WM8775EDS (4 Channel)
  108. * DAC: CS4382 (114 dB, 24-Bit, 192 kHz, 8-Channel D/A Converter with DSD Support)
  109. * SPDIF Out control switches between Mic in and SPDIF out.
  110. * No sound out or mic input working yet.
  111. *
  112. * GENERAL INFO:
  113. * Model: SB0413
  114. * P17 Chip: CA0106-DAT
  115. * AC97 Codec: None.
  116. * ADC: Unknown
  117. * DAC: Unknown
  118. * Trying to handle it like the SB0410.
  119. *
  120. * This code was initally based on code from ALSA's emu10k1x.c which is:
  121. * Copyright (c) by Francisco Moraes <fmoraes@nc.rr.com>
  122. *
  123. * This program is free software; you can redistribute it and/or modify
  124. * it under the terms of the GNU General Public License as published by
  125. * the Free Software Foundation; either version 2 of the License, or
  126. * (at your option) any later version.
  127. *
  128. * This program is distributed in the hope that it will be useful,
  129. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  130. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  131. * GNU General Public License for more details.
  132. *
  133. * You should have received a copy of the GNU General Public License
  134. * along with this program; if not, write to the Free Software
  135. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  136. *
  137. */
  138. #include <linux/delay.h>
  139. #include <linux/init.h>
  140. #include <linux/interrupt.h>
  141. #include <linux/pci.h>
  142. #include <linux/slab.h>
  143. #include <linux/moduleparam.h>
  144. #include <linux/dma-mapping.h>
  145. #include <sound/core.h>
  146. #include <sound/initval.h>
  147. #include <sound/pcm.h>
  148. #include <sound/ac97_codec.h>
  149. #include <sound/info.h>
  150. MODULE_AUTHOR("James Courtier-Dutton <James@superbug.demon.co.uk>");
  151. MODULE_DESCRIPTION("CA0106");
  152. MODULE_LICENSE("GPL");
  153. MODULE_SUPPORTED_DEVICE("{{Creative,SB CA0106 chip}}");
  154. // module parameters (see "Module Parameters")
  155. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
  156. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
  157. static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
  158. static uint subsystem[SNDRV_CARDS]; /* Force card subsystem model */
  159. module_param_array(index, int, NULL, 0444);
  160. MODULE_PARM_DESC(index, "Index value for the CA0106 soundcard.");
  161. module_param_array(id, charp, NULL, 0444);
  162. MODULE_PARM_DESC(id, "ID string for the CA0106 soundcard.");
  163. module_param_array(enable, bool, NULL, 0444);
  164. MODULE_PARM_DESC(enable, "Enable the CA0106 soundcard.");
  165. module_param_array(subsystem, uint, NULL, 0444);
  166. MODULE_PARM_DESC(subsystem, "Force card subsystem model.");
  167. #include "ca0106.h"
  168. static struct snd_ca0106_details ca0106_chip_details[] = {
  169. /* Sound Blaster X-Fi Extreme Audio. This does not have an AC97. 53SB079000000 */
  170. /* It is really just a normal SB Live 24bit. */
  171. /* Tested:
  172. * See ALSA bug#3251
  173. */
  174. { .serial = 0x10131102,
  175. .name = "X-Fi Extreme Audio [SBxxxx]",
  176. .gpio_type = 1,
  177. .i2c_adc = 1 } ,
  178. /* Sound Blaster X-Fi Extreme Audio. This does not have an AC97. 53SB079000000 */
  179. /* It is really just a normal SB Live 24bit. */
  180. /*
  181. * CTRL:CA0111-WTLF
  182. * ADC: WM8775SEDS
  183. * DAC: CS4382-KQZ
  184. */
  185. /* Tested:
  186. * Playback on front, rear, center/lfe speakers
  187. * Capture from Mic in.
  188. * Not-Tested:
  189. * Capture from Line in.
  190. * Playback to digital out.
  191. */
  192. { .serial = 0x10121102,
  193. .name = "X-Fi Extreme Audio [SB0790]",
  194. .gpio_type = 1,
  195. .i2c_adc = 1 } ,
  196. /* New Dell Sound Blaster Live! 7.1 24bit. This does not have an AC97. */
  197. /* AudigyLS[SB0310] */
  198. { .serial = 0x10021102,
  199. .name = "AudigyLS [SB0310]",
  200. .ac97 = 1 } ,
  201. /* Unknown AudigyLS that also says SB0310 on it */
  202. { .serial = 0x10051102,
  203. .name = "AudigyLS [SB0310b]",
  204. .ac97 = 1 } ,
  205. /* New Sound Blaster Live! 7.1 24bit. This does not have an AC97. 53SB041000001 */
  206. { .serial = 0x10061102,
  207. .name = "Live! 7.1 24bit [SB0410]",
  208. .gpio_type = 1,
  209. .i2c_adc = 1 } ,
  210. /* New Dell Sound Blaster Live! 7.1 24bit. This does not have an AC97. */
  211. { .serial = 0x10071102,
  212. .name = "Live! 7.1 24bit [SB0413]",
  213. .gpio_type = 1,
  214. .i2c_adc = 1 } ,
  215. /* New Audigy SE. Has a different DAC. */
  216. /* SB0570:
  217. * CTRL:CA0106-DAT
  218. * ADC: WM8775EDS
  219. * DAC: WM8768GEDS
  220. */
  221. { .serial = 0x100a1102,
  222. .name = "Audigy SE [SB0570]",
  223. .gpio_type = 1,
  224. .i2c_adc = 1,
  225. .spi_dac = 1 } ,
  226. /* New Audigy LS. Has a different DAC. */
  227. /* SB0570:
  228. * CTRL:CA0106-DAT
  229. * ADC: WM8775EDS
  230. * DAC: WM8768GEDS
  231. */
  232. { .serial = 0x10111102,
  233. .name = "Audigy SE OEM [SB0570a]",
  234. .gpio_type = 1,
  235. .i2c_adc = 1,
  236. .spi_dac = 1 } ,
  237. /* MSI K8N Diamond Motherboard with onboard SB Live 24bit without AC97 */
  238. /* SB0438
  239. * CTRL:CA0106-DAT
  240. * ADC: WM8775SEDS
  241. * DAC: CS4382-KQZ
  242. */
  243. { .serial = 0x10091462,
  244. .name = "MSI K8N Diamond MB [SB0438]",
  245. .gpio_type = 2,
  246. .i2c_adc = 1 } ,
  247. /* MSI K8N Diamond PLUS MB */
  248. { .serial = 0x10091102,
  249. .name = "MSI K8N Diamond MB",
  250. .gpio_type = 2,
  251. .i2c_adc = 1,
  252. .spi_dac = 2 } ,
  253. /* Shuttle XPC SD31P which has an onboard Creative Labs
  254. * Sound Blaster Live! 24-bit EAX
  255. * high-definition 7.1 audio processor".
  256. * Added using info from andrewvegan in alsa bug #1298
  257. */
  258. { .serial = 0x30381297,
  259. .name = "Shuttle XPC SD31P [SD31P]",
  260. .gpio_type = 1,
  261. .i2c_adc = 1 } ,
  262. /* Shuttle XPC SD11G5 which has an onboard Creative Labs
  263. * Sound Blaster Live! 24-bit EAX
  264. * high-definition 7.1 audio processor".
  265. * Fixes ALSA bug#1600
  266. */
  267. { .serial = 0x30411297,
  268. .name = "Shuttle XPC SD11G5 [SD11G5]",
  269. .gpio_type = 1,
  270. .i2c_adc = 1 } ,
  271. { .serial = 0,
  272. .name = "AudigyLS [Unknown]" }
  273. };
  274. /* hardware definition */
  275. static struct snd_pcm_hardware snd_ca0106_playback_hw = {
  276. .info = SNDRV_PCM_INFO_MMAP |
  277. SNDRV_PCM_INFO_INTERLEAVED |
  278. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  279. SNDRV_PCM_INFO_MMAP_VALID |
  280. SNDRV_PCM_INFO_SYNC_START,
  281. .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE,
  282. .rates = (SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_96000 |
  283. SNDRV_PCM_RATE_192000),
  284. .rate_min = 48000,
  285. .rate_max = 192000,
  286. .channels_min = 2, //1,
  287. .channels_max = 2, //6,
  288. .buffer_bytes_max = ((65536 - 64) * 8),
  289. .period_bytes_min = 64,
  290. .period_bytes_max = (65536 - 64),
  291. .periods_min = 2,
  292. .periods_max = 8,
  293. .fifo_size = 0,
  294. };
  295. static struct snd_pcm_hardware snd_ca0106_capture_hw = {
  296. .info = (SNDRV_PCM_INFO_MMAP |
  297. SNDRV_PCM_INFO_INTERLEAVED |
  298. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  299. SNDRV_PCM_INFO_MMAP_VALID),
  300. .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE,
  301. .rates = (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
  302. SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_192000),
  303. .rate_min = 44100,
  304. .rate_max = 192000,
  305. .channels_min = 2,
  306. .channels_max = 2,
  307. .buffer_bytes_max = ((65536 - 64) * 8),
  308. .period_bytes_min = 64,
  309. .period_bytes_max = (65536 - 64),
  310. .periods_min = 2,
  311. .periods_max = 2,
  312. .fifo_size = 0,
  313. };
  314. unsigned int snd_ca0106_ptr_read(struct snd_ca0106 * emu,
  315. unsigned int reg,
  316. unsigned int chn)
  317. {
  318. unsigned long flags;
  319. unsigned int regptr, val;
  320. regptr = (reg << 16) | chn;
  321. spin_lock_irqsave(&emu->emu_lock, flags);
  322. outl(regptr, emu->port + PTR);
  323. val = inl(emu->port + DATA);
  324. spin_unlock_irqrestore(&emu->emu_lock, flags);
  325. return val;
  326. }
  327. void snd_ca0106_ptr_write(struct snd_ca0106 *emu,
  328. unsigned int reg,
  329. unsigned int chn,
  330. unsigned int data)
  331. {
  332. unsigned int regptr;
  333. unsigned long flags;
  334. regptr = (reg << 16) | chn;
  335. spin_lock_irqsave(&emu->emu_lock, flags);
  336. outl(regptr, emu->port + PTR);
  337. outl(data, emu->port + DATA);
  338. spin_unlock_irqrestore(&emu->emu_lock, flags);
  339. }
  340. int snd_ca0106_spi_write(struct snd_ca0106 * emu,
  341. unsigned int data)
  342. {
  343. unsigned int reset, set;
  344. unsigned int reg, tmp;
  345. int n, result;
  346. reg = SPI;
  347. if (data > 0xffff) /* Only 16bit values allowed */
  348. return 1;
  349. tmp = snd_ca0106_ptr_read(emu, reg, 0);
  350. reset = (tmp & ~0x3ffff) | 0x20000; /* Set xxx20000 */
  351. set = reset | 0x10000; /* Set xxx1xxxx */
  352. snd_ca0106_ptr_write(emu, reg, 0, reset | data);
  353. tmp = snd_ca0106_ptr_read(emu, reg, 0); /* write post */
  354. snd_ca0106_ptr_write(emu, reg, 0, set | data);
  355. result = 1;
  356. /* Wait for status bit to return to 0 */
  357. for (n = 0; n < 100; n++) {
  358. udelay(10);
  359. tmp = snd_ca0106_ptr_read(emu, reg, 0);
  360. if (!(tmp & 0x10000)) {
  361. result = 0;
  362. break;
  363. }
  364. }
  365. if (result) /* Timed out */
  366. return 1;
  367. snd_ca0106_ptr_write(emu, reg, 0, reset | data);
  368. tmp = snd_ca0106_ptr_read(emu, reg, 0); /* Write post */
  369. return 0;
  370. }
  371. /* The ADC does not support i2c read, so only write is implemented */
  372. int snd_ca0106_i2c_write(struct snd_ca0106 *emu,
  373. u32 reg,
  374. u32 value)
  375. {
  376. u32 tmp;
  377. int timeout = 0;
  378. int status;
  379. int retry;
  380. if ((reg > 0x7f) || (value > 0x1ff)) {
  381. snd_printk(KERN_ERR "i2c_write: invalid values.\n");
  382. return -EINVAL;
  383. }
  384. tmp = reg << 25 | value << 16;
  385. // snd_printk("I2C-write:reg=0x%x, value=0x%x\n", reg, value);
  386. /* Not sure what this I2C channel controls. */
  387. /* snd_ca0106_ptr_write(emu, I2C_D0, 0, tmp); */
  388. /* This controls the I2C connected to the WM8775 ADC Codec */
  389. snd_ca0106_ptr_write(emu, I2C_D1, 0, tmp);
  390. for (retry = 0; retry < 10; retry++) {
  391. /* Send the data to i2c */
  392. //tmp = snd_ca0106_ptr_read(emu, I2C_A, 0);
  393. //tmp = tmp & ~(I2C_A_ADC_READ|I2C_A_ADC_LAST|I2C_A_ADC_START|I2C_A_ADC_ADD_MASK);
  394. tmp = 0;
  395. tmp = tmp | (I2C_A_ADC_LAST|I2C_A_ADC_START|I2C_A_ADC_ADD);
  396. snd_ca0106_ptr_write(emu, I2C_A, 0, tmp);
  397. /* Wait till the transaction ends */
  398. while (1) {
  399. status = snd_ca0106_ptr_read(emu, I2C_A, 0);
  400. //snd_printk("I2C:status=0x%x\n", status);
  401. timeout++;
  402. if ((status & I2C_A_ADC_START) == 0)
  403. break;
  404. if (timeout > 1000)
  405. break;
  406. }
  407. //Read back and see if the transaction is successful
  408. if ((status & I2C_A_ADC_ABORT) == 0)
  409. break;
  410. }
  411. if (retry == 10) {
  412. snd_printk(KERN_ERR "Writing to ADC failed!\n");
  413. return -EINVAL;
  414. }
  415. return 0;
  416. }
  417. static void snd_ca0106_intr_enable(struct snd_ca0106 *emu, unsigned int intrenb)
  418. {
  419. unsigned long flags;
  420. unsigned int intr_enable;
  421. spin_lock_irqsave(&emu->emu_lock, flags);
  422. intr_enable = inl(emu->port + INTE) | intrenb;
  423. outl(intr_enable, emu->port + INTE);
  424. spin_unlock_irqrestore(&emu->emu_lock, flags);
  425. }
  426. static void snd_ca0106_intr_disable(struct snd_ca0106 *emu, unsigned int intrenb)
  427. {
  428. unsigned long flags;
  429. unsigned int intr_enable;
  430. spin_lock_irqsave(&emu->emu_lock, flags);
  431. intr_enable = inl(emu->port + INTE) & ~intrenb;
  432. outl(intr_enable, emu->port + INTE);
  433. spin_unlock_irqrestore(&emu->emu_lock, flags);
  434. }
  435. static void snd_ca0106_pcm_free_substream(struct snd_pcm_runtime *runtime)
  436. {
  437. kfree(runtime->private_data);
  438. }
  439. static const int spi_dacd_reg[] = {
  440. [PCM_FRONT_CHANNEL] = SPI_DACD4_REG,
  441. [PCM_REAR_CHANNEL] = SPI_DACD0_REG,
  442. [PCM_CENTER_LFE_CHANNEL]= SPI_DACD2_REG,
  443. [PCM_UNKNOWN_CHANNEL] = SPI_DACD1_REG,
  444. };
  445. static const int spi_dacd_bit[] = {
  446. [PCM_FRONT_CHANNEL] = SPI_DACD4_BIT,
  447. [PCM_REAR_CHANNEL] = SPI_DACD0_BIT,
  448. [PCM_CENTER_LFE_CHANNEL]= SPI_DACD2_BIT,
  449. [PCM_UNKNOWN_CHANNEL] = SPI_DACD1_BIT,
  450. };
  451. /* open_playback callback */
  452. static int snd_ca0106_pcm_open_playback_channel(struct snd_pcm_substream *substream,
  453. int channel_id)
  454. {
  455. struct snd_ca0106 *chip = snd_pcm_substream_chip(substream);
  456. struct snd_ca0106_channel *channel = &(chip->playback_channels[channel_id]);
  457. struct snd_ca0106_pcm *epcm;
  458. struct snd_pcm_runtime *runtime = substream->runtime;
  459. int err;
  460. epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
  461. if (epcm == NULL)
  462. return -ENOMEM;
  463. epcm->emu = chip;
  464. epcm->substream = substream;
  465. epcm->channel_id=channel_id;
  466. runtime->private_data = epcm;
  467. runtime->private_free = snd_ca0106_pcm_free_substream;
  468. runtime->hw = snd_ca0106_playback_hw;
  469. channel->emu = chip;
  470. channel->number = channel_id;
  471. channel->use = 1;
  472. //printk("open:channel_id=%d, chip=%p, channel=%p\n",channel_id, chip, channel);
  473. //channel->interrupt = snd_ca0106_pcm_channel_interrupt;
  474. channel->epcm = epcm;
  475. if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
  476. return err;
  477. if ((err = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64)) < 0)
  478. return err;
  479. snd_pcm_set_sync(substream);
  480. if (chip->details->spi_dac && channel_id != PCM_FRONT_CHANNEL) {
  481. const int reg = spi_dacd_reg[channel_id];
  482. /* Power up dac */
  483. chip->spi_dac_reg[reg] &= ~spi_dacd_bit[channel_id];
  484. err = snd_ca0106_spi_write(chip, chip->spi_dac_reg[reg]);
  485. if (err < 0)
  486. return err;
  487. }
  488. return 0;
  489. }
  490. /* close callback */
  491. static int snd_ca0106_pcm_close_playback(struct snd_pcm_substream *substream)
  492. {
  493. struct snd_ca0106 *chip = snd_pcm_substream_chip(substream);
  494. struct snd_pcm_runtime *runtime = substream->runtime;
  495. struct snd_ca0106_pcm *epcm = runtime->private_data;
  496. chip->playback_channels[epcm->channel_id].use = 0;
  497. if (chip->details->spi_dac && epcm->channel_id != PCM_FRONT_CHANNEL) {
  498. const int reg = spi_dacd_reg[epcm->channel_id];
  499. /* Power down DAC */
  500. chip->spi_dac_reg[reg] |= spi_dacd_bit[epcm->channel_id];
  501. snd_ca0106_spi_write(chip, chip->spi_dac_reg[reg]);
  502. }
  503. /* FIXME: maybe zero others */
  504. return 0;
  505. }
  506. static int snd_ca0106_pcm_open_playback_front(struct snd_pcm_substream *substream)
  507. {
  508. return snd_ca0106_pcm_open_playback_channel(substream, PCM_FRONT_CHANNEL);
  509. }
  510. static int snd_ca0106_pcm_open_playback_center_lfe(struct snd_pcm_substream *substream)
  511. {
  512. return snd_ca0106_pcm_open_playback_channel(substream, PCM_CENTER_LFE_CHANNEL);
  513. }
  514. static int snd_ca0106_pcm_open_playback_unknown(struct snd_pcm_substream *substream)
  515. {
  516. return snd_ca0106_pcm_open_playback_channel(substream, PCM_UNKNOWN_CHANNEL);
  517. }
  518. static int snd_ca0106_pcm_open_playback_rear(struct snd_pcm_substream *substream)
  519. {
  520. return snd_ca0106_pcm_open_playback_channel(substream, PCM_REAR_CHANNEL);
  521. }
  522. /* open_capture callback */
  523. static int snd_ca0106_pcm_open_capture_channel(struct snd_pcm_substream *substream,
  524. int channel_id)
  525. {
  526. struct snd_ca0106 *chip = snd_pcm_substream_chip(substream);
  527. struct snd_ca0106_channel *channel = &(chip->capture_channels[channel_id]);
  528. struct snd_ca0106_pcm *epcm;
  529. struct snd_pcm_runtime *runtime = substream->runtime;
  530. int err;
  531. epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
  532. if (epcm == NULL) {
  533. snd_printk(KERN_ERR "open_capture_channel: failed epcm alloc\n");
  534. return -ENOMEM;
  535. }
  536. epcm->emu = chip;
  537. epcm->substream = substream;
  538. epcm->channel_id=channel_id;
  539. runtime->private_data = epcm;
  540. runtime->private_free = snd_ca0106_pcm_free_substream;
  541. runtime->hw = snd_ca0106_capture_hw;
  542. channel->emu = chip;
  543. channel->number = channel_id;
  544. channel->use = 1;
  545. //printk("open:channel_id=%d, chip=%p, channel=%p\n",channel_id, chip, channel);
  546. //channel->interrupt = snd_ca0106_pcm_channel_interrupt;
  547. channel->epcm = epcm;
  548. if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
  549. return err;
  550. //snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, &hw_constraints_capture_period_sizes);
  551. if ((err = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64)) < 0)
  552. return err;
  553. return 0;
  554. }
  555. /* close callback */
  556. static int snd_ca0106_pcm_close_capture(struct snd_pcm_substream *substream)
  557. {
  558. struct snd_ca0106 *chip = snd_pcm_substream_chip(substream);
  559. struct snd_pcm_runtime *runtime = substream->runtime;
  560. struct snd_ca0106_pcm *epcm = runtime->private_data;
  561. chip->capture_channels[epcm->channel_id].use = 0;
  562. /* FIXME: maybe zero others */
  563. return 0;
  564. }
  565. static int snd_ca0106_pcm_open_0_capture(struct snd_pcm_substream *substream)
  566. {
  567. return snd_ca0106_pcm_open_capture_channel(substream, 0);
  568. }
  569. static int snd_ca0106_pcm_open_1_capture(struct snd_pcm_substream *substream)
  570. {
  571. return snd_ca0106_pcm_open_capture_channel(substream, 1);
  572. }
  573. static int snd_ca0106_pcm_open_2_capture(struct snd_pcm_substream *substream)
  574. {
  575. return snd_ca0106_pcm_open_capture_channel(substream, 2);
  576. }
  577. static int snd_ca0106_pcm_open_3_capture(struct snd_pcm_substream *substream)
  578. {
  579. return snd_ca0106_pcm_open_capture_channel(substream, 3);
  580. }
  581. /* hw_params callback */
  582. static int snd_ca0106_pcm_hw_params_playback(struct snd_pcm_substream *substream,
  583. struct snd_pcm_hw_params *hw_params)
  584. {
  585. return snd_pcm_lib_malloc_pages(substream,
  586. params_buffer_bytes(hw_params));
  587. }
  588. /* hw_free callback */
  589. static int snd_ca0106_pcm_hw_free_playback(struct snd_pcm_substream *substream)
  590. {
  591. return snd_pcm_lib_free_pages(substream);
  592. }
  593. /* hw_params callback */
  594. static int snd_ca0106_pcm_hw_params_capture(struct snd_pcm_substream *substream,
  595. struct snd_pcm_hw_params *hw_params)
  596. {
  597. return snd_pcm_lib_malloc_pages(substream,
  598. params_buffer_bytes(hw_params));
  599. }
  600. /* hw_free callback */
  601. static int snd_ca0106_pcm_hw_free_capture(struct snd_pcm_substream *substream)
  602. {
  603. return snd_pcm_lib_free_pages(substream);
  604. }
  605. /* prepare playback callback */
  606. static int snd_ca0106_pcm_prepare_playback(struct snd_pcm_substream *substream)
  607. {
  608. struct snd_ca0106 *emu = snd_pcm_substream_chip(substream);
  609. struct snd_pcm_runtime *runtime = substream->runtime;
  610. struct snd_ca0106_pcm *epcm = runtime->private_data;
  611. int channel = epcm->channel_id;
  612. u32 *table_base = (u32 *)(emu->buffer.area+(8*16*channel));
  613. u32 period_size_bytes = frames_to_bytes(runtime, runtime->period_size);
  614. u32 hcfg_mask = HCFG_PLAYBACK_S32_LE;
  615. u32 hcfg_set = 0x00000000;
  616. u32 hcfg;
  617. u32 reg40_mask = 0x30000 << (channel<<1);
  618. u32 reg40_set = 0;
  619. u32 reg40;
  620. /* FIXME: Depending on mixer selection of SPDIF out or not, select the spdif rate or the DAC rate. */
  621. u32 reg71_mask = 0x03030000 ; /* Global. Set SPDIF rate. We only support 44100 to spdif, not to DAC. */
  622. u32 reg71_set = 0;
  623. u32 reg71;
  624. int i;
  625. //snd_printk("prepare:channel_number=%d, rate=%d, format=0x%x, channels=%d, buffer_size=%ld, period_size=%ld, periods=%u, frames_to_bytes=%d\n",channel, runtime->rate, runtime->format, runtime->channels, runtime->buffer_size, runtime->period_size, runtime->periods, frames_to_bytes(runtime, 1));
  626. //snd_printk("dma_addr=%x, dma_area=%p, table_base=%p\n",runtime->dma_addr, runtime->dma_area, table_base);
  627. //snd_printk("dma_addr=%x, dma_area=%p, dma_bytes(size)=%x\n",emu->buffer.addr, emu->buffer.area, emu->buffer.bytes);
  628. /* Rate can be set per channel. */
  629. /* reg40 control host to fifo */
  630. /* reg71 controls DAC rate. */
  631. switch (runtime->rate) {
  632. case 44100:
  633. reg40_set = 0x10000 << (channel<<1);
  634. reg71_set = 0x01010000;
  635. break;
  636. case 48000:
  637. reg40_set = 0;
  638. reg71_set = 0;
  639. break;
  640. case 96000:
  641. reg40_set = 0x20000 << (channel<<1);
  642. reg71_set = 0x02020000;
  643. break;
  644. case 192000:
  645. reg40_set = 0x30000 << (channel<<1);
  646. reg71_set = 0x03030000;
  647. break;
  648. default:
  649. reg40_set = 0;
  650. reg71_set = 0;
  651. break;
  652. }
  653. /* Format is a global setting */
  654. /* FIXME: Only let the first channel accessed set this. */
  655. switch (runtime->format) {
  656. case SNDRV_PCM_FORMAT_S16_LE:
  657. hcfg_set = 0;
  658. break;
  659. case SNDRV_PCM_FORMAT_S32_LE:
  660. hcfg_set = HCFG_PLAYBACK_S32_LE;
  661. break;
  662. default:
  663. hcfg_set = 0;
  664. break;
  665. }
  666. hcfg = inl(emu->port + HCFG) ;
  667. hcfg = (hcfg & ~hcfg_mask) | hcfg_set;
  668. outl(hcfg, emu->port + HCFG);
  669. reg40 = snd_ca0106_ptr_read(emu, 0x40, 0);
  670. reg40 = (reg40 & ~reg40_mask) | reg40_set;
  671. snd_ca0106_ptr_write(emu, 0x40, 0, reg40);
  672. reg71 = snd_ca0106_ptr_read(emu, 0x71, 0);
  673. reg71 = (reg71 & ~reg71_mask) | reg71_set;
  674. snd_ca0106_ptr_write(emu, 0x71, 0, reg71);
  675. /* FIXME: Check emu->buffer.size before actually writing to it. */
  676. for(i=0; i < runtime->periods; i++) {
  677. table_base[i*2] = runtime->dma_addr + (i * period_size_bytes);
  678. table_base[i*2+1] = period_size_bytes << 16;
  679. }
  680. snd_ca0106_ptr_write(emu, PLAYBACK_LIST_ADDR, channel, emu->buffer.addr+(8*16*channel));
  681. snd_ca0106_ptr_write(emu, PLAYBACK_LIST_SIZE, channel, (runtime->periods - 1) << 19);
  682. snd_ca0106_ptr_write(emu, PLAYBACK_LIST_PTR, channel, 0);
  683. snd_ca0106_ptr_write(emu, PLAYBACK_DMA_ADDR, channel, runtime->dma_addr);
  684. snd_ca0106_ptr_write(emu, PLAYBACK_PERIOD_SIZE, channel, frames_to_bytes(runtime, runtime->period_size)<<16); // buffer size in bytes
  685. /* FIXME test what 0 bytes does. */
  686. snd_ca0106_ptr_write(emu, PLAYBACK_PERIOD_SIZE, channel, 0); // buffer size in bytes
  687. snd_ca0106_ptr_write(emu, PLAYBACK_POINTER, channel, 0);
  688. snd_ca0106_ptr_write(emu, 0x07, channel, 0x0);
  689. snd_ca0106_ptr_write(emu, 0x08, channel, 0);
  690. snd_ca0106_ptr_write(emu, PLAYBACK_MUTE, 0x0, 0x0); /* Unmute output */
  691. #if 0
  692. snd_ca0106_ptr_write(emu, SPCS0, 0,
  693. SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
  694. SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
  695. SPCS_GENERATIONSTATUS | 0x00001200 |
  696. 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT );
  697. #endif
  698. return 0;
  699. }
  700. /* prepare capture callback */
  701. static int snd_ca0106_pcm_prepare_capture(struct snd_pcm_substream *substream)
  702. {
  703. struct snd_ca0106 *emu = snd_pcm_substream_chip(substream);
  704. struct snd_pcm_runtime *runtime = substream->runtime;
  705. struct snd_ca0106_pcm *epcm = runtime->private_data;
  706. int channel = epcm->channel_id;
  707. u32 hcfg_mask = HCFG_CAPTURE_S32_LE;
  708. u32 hcfg_set = 0x00000000;
  709. u32 hcfg;
  710. u32 over_sampling=0x2;
  711. u32 reg71_mask = 0x0000c000 ; /* Global. Set ADC rate. */
  712. u32 reg71_set = 0;
  713. u32 reg71;
  714. //snd_printk("prepare:channel_number=%d, rate=%d, format=0x%x, channels=%d, buffer_size=%ld, period_size=%ld, periods=%u, frames_to_bytes=%d\n",channel, runtime->rate, runtime->format, runtime->channels, runtime->buffer_size, runtime->period_size, runtime->periods, frames_to_bytes(runtime, 1));
  715. //snd_printk("dma_addr=%x, dma_area=%p, table_base=%p\n",runtime->dma_addr, runtime->dma_area, table_base);
  716. //snd_printk("dma_addr=%x, dma_area=%p, dma_bytes(size)=%x\n",emu->buffer.addr, emu->buffer.area, emu->buffer.bytes);
  717. /* reg71 controls ADC rate. */
  718. switch (runtime->rate) {
  719. case 44100:
  720. reg71_set = 0x00004000;
  721. break;
  722. case 48000:
  723. reg71_set = 0;
  724. break;
  725. case 96000:
  726. reg71_set = 0x00008000;
  727. over_sampling=0xa;
  728. break;
  729. case 192000:
  730. reg71_set = 0x0000c000;
  731. over_sampling=0xa;
  732. break;
  733. default:
  734. reg71_set = 0;
  735. break;
  736. }
  737. /* Format is a global setting */
  738. /* FIXME: Only let the first channel accessed set this. */
  739. switch (runtime->format) {
  740. case SNDRV_PCM_FORMAT_S16_LE:
  741. hcfg_set = 0;
  742. break;
  743. case SNDRV_PCM_FORMAT_S32_LE:
  744. hcfg_set = HCFG_CAPTURE_S32_LE;
  745. break;
  746. default:
  747. hcfg_set = 0;
  748. break;
  749. }
  750. hcfg = inl(emu->port + HCFG) ;
  751. hcfg = (hcfg & ~hcfg_mask) | hcfg_set;
  752. outl(hcfg, emu->port + HCFG);
  753. reg71 = snd_ca0106_ptr_read(emu, 0x71, 0);
  754. reg71 = (reg71 & ~reg71_mask) | reg71_set;
  755. snd_ca0106_ptr_write(emu, 0x71, 0, reg71);
  756. if (emu->details->i2c_adc == 1) { /* The SB0410 and SB0413 use I2C to control ADC. */
  757. snd_ca0106_i2c_write(emu, ADC_MASTER, over_sampling); /* Adjust the over sampler to better suit the capture rate. */
  758. }
  759. //printk("prepare:channel_number=%d, rate=%d, format=0x%x, channels=%d, buffer_size=%ld, period_size=%ld, frames_to_bytes=%d\n",channel, runtime->rate, runtime->format, runtime->channels, runtime->buffer_size, runtime->period_size, frames_to_bytes(runtime, 1));
  760. snd_ca0106_ptr_write(emu, 0x13, channel, 0);
  761. snd_ca0106_ptr_write(emu, CAPTURE_DMA_ADDR, channel, runtime->dma_addr);
  762. snd_ca0106_ptr_write(emu, CAPTURE_BUFFER_SIZE, channel, frames_to_bytes(runtime, runtime->buffer_size)<<16); // buffer size in bytes
  763. snd_ca0106_ptr_write(emu, CAPTURE_POINTER, channel, 0);
  764. return 0;
  765. }
  766. /* trigger_playback callback */
  767. static int snd_ca0106_pcm_trigger_playback(struct snd_pcm_substream *substream,
  768. int cmd)
  769. {
  770. struct snd_ca0106 *emu = snd_pcm_substream_chip(substream);
  771. struct snd_pcm_runtime *runtime;
  772. struct snd_ca0106_pcm *epcm;
  773. int channel;
  774. int result = 0;
  775. struct snd_pcm_substream *s;
  776. u32 basic = 0;
  777. u32 extended = 0;
  778. u32 bits;
  779. int running = 0;
  780. switch (cmd) {
  781. case SNDRV_PCM_TRIGGER_START:
  782. case SNDRV_PCM_TRIGGER_RESUME:
  783. running = 1;
  784. break;
  785. case SNDRV_PCM_TRIGGER_STOP:
  786. case SNDRV_PCM_TRIGGER_SUSPEND:
  787. default:
  788. running = 0;
  789. break;
  790. }
  791. snd_pcm_group_for_each_entry(s, substream) {
  792. if (snd_pcm_substream_chip(s) != emu ||
  793. s->stream != SNDRV_PCM_STREAM_PLAYBACK)
  794. continue;
  795. runtime = s->runtime;
  796. epcm = runtime->private_data;
  797. channel = epcm->channel_id;
  798. /* snd_printk("channel=%d\n",channel); */
  799. epcm->running = running;
  800. basic |= (0x1 << channel);
  801. extended |= (0x10 << channel);
  802. snd_pcm_trigger_done(s, substream);
  803. }
  804. /* snd_printk("basic=0x%x, extended=0x%x\n",basic, extended); */
  805. switch (cmd) {
  806. case SNDRV_PCM_TRIGGER_START:
  807. case SNDRV_PCM_TRIGGER_RESUME:
  808. bits = snd_ca0106_ptr_read(emu, EXTENDED_INT_MASK, 0);
  809. bits |= extended;
  810. snd_ca0106_ptr_write(emu, EXTENDED_INT_MASK, 0, bits);
  811. bits = snd_ca0106_ptr_read(emu, BASIC_INTERRUPT, 0);
  812. bits |= basic;
  813. snd_ca0106_ptr_write(emu, BASIC_INTERRUPT, 0, bits);
  814. break;
  815. case SNDRV_PCM_TRIGGER_STOP:
  816. case SNDRV_PCM_TRIGGER_SUSPEND:
  817. bits = snd_ca0106_ptr_read(emu, BASIC_INTERRUPT, 0);
  818. bits &= ~basic;
  819. snd_ca0106_ptr_write(emu, BASIC_INTERRUPT, 0, bits);
  820. bits = snd_ca0106_ptr_read(emu, EXTENDED_INT_MASK, 0);
  821. bits &= ~extended;
  822. snd_ca0106_ptr_write(emu, EXTENDED_INT_MASK, 0, bits);
  823. break;
  824. default:
  825. result = -EINVAL;
  826. break;
  827. }
  828. return result;
  829. }
  830. /* trigger_capture callback */
  831. static int snd_ca0106_pcm_trigger_capture(struct snd_pcm_substream *substream,
  832. int cmd)
  833. {
  834. struct snd_ca0106 *emu = snd_pcm_substream_chip(substream);
  835. struct snd_pcm_runtime *runtime = substream->runtime;
  836. struct snd_ca0106_pcm *epcm = runtime->private_data;
  837. int channel = epcm->channel_id;
  838. int result = 0;
  839. switch (cmd) {
  840. case SNDRV_PCM_TRIGGER_START:
  841. snd_ca0106_ptr_write(emu, EXTENDED_INT_MASK, 0, snd_ca0106_ptr_read(emu, EXTENDED_INT_MASK, 0) | (0x110000<<channel));
  842. snd_ca0106_ptr_write(emu, BASIC_INTERRUPT, 0, snd_ca0106_ptr_read(emu, BASIC_INTERRUPT, 0)|(0x100<<channel));
  843. epcm->running = 1;
  844. break;
  845. case SNDRV_PCM_TRIGGER_STOP:
  846. snd_ca0106_ptr_write(emu, BASIC_INTERRUPT, 0, snd_ca0106_ptr_read(emu, BASIC_INTERRUPT, 0) & ~(0x100<<channel));
  847. snd_ca0106_ptr_write(emu, EXTENDED_INT_MASK, 0, snd_ca0106_ptr_read(emu, EXTENDED_INT_MASK, 0) & ~(0x110000<<channel));
  848. epcm->running = 0;
  849. break;
  850. default:
  851. result = -EINVAL;
  852. break;
  853. }
  854. return result;
  855. }
  856. /* pointer_playback callback */
  857. static snd_pcm_uframes_t
  858. snd_ca0106_pcm_pointer_playback(struct snd_pcm_substream *substream)
  859. {
  860. struct snd_ca0106 *emu = snd_pcm_substream_chip(substream);
  861. struct snd_pcm_runtime *runtime = substream->runtime;
  862. struct snd_ca0106_pcm *epcm = runtime->private_data;
  863. snd_pcm_uframes_t ptr, ptr1, ptr2,ptr3,ptr4 = 0;
  864. int channel = epcm->channel_id;
  865. if (!epcm->running)
  866. return 0;
  867. ptr3 = snd_ca0106_ptr_read(emu, PLAYBACK_LIST_PTR, channel);
  868. ptr1 = snd_ca0106_ptr_read(emu, PLAYBACK_POINTER, channel);
  869. ptr4 = snd_ca0106_ptr_read(emu, PLAYBACK_LIST_PTR, channel);
  870. if (ptr3 != ptr4) ptr1 = snd_ca0106_ptr_read(emu, PLAYBACK_POINTER, channel);
  871. ptr2 = bytes_to_frames(runtime, ptr1);
  872. ptr2+= (ptr4 >> 3) * runtime->period_size;
  873. ptr=ptr2;
  874. if (ptr >= runtime->buffer_size)
  875. ptr -= runtime->buffer_size;
  876. //printk("ptr1 = 0x%lx, ptr2=0x%lx, ptr=0x%lx, buffer_size = 0x%x, period_size = 0x%x, bits=%d, rate=%d\n", ptr1, ptr2, ptr, (int)runtime->buffer_size, (int)runtime->period_size, (int)runtime->frame_bits, (int)runtime->rate);
  877. return ptr;
  878. }
  879. /* pointer_capture callback */
  880. static snd_pcm_uframes_t
  881. snd_ca0106_pcm_pointer_capture(struct snd_pcm_substream *substream)
  882. {
  883. struct snd_ca0106 *emu = snd_pcm_substream_chip(substream);
  884. struct snd_pcm_runtime *runtime = substream->runtime;
  885. struct snd_ca0106_pcm *epcm = runtime->private_data;
  886. snd_pcm_uframes_t ptr, ptr1, ptr2 = 0;
  887. int channel = channel=epcm->channel_id;
  888. if (!epcm->running)
  889. return 0;
  890. ptr1 = snd_ca0106_ptr_read(emu, CAPTURE_POINTER, channel);
  891. ptr2 = bytes_to_frames(runtime, ptr1);
  892. ptr=ptr2;
  893. if (ptr >= runtime->buffer_size)
  894. ptr -= runtime->buffer_size;
  895. //printk("ptr1 = 0x%lx, ptr2=0x%lx, ptr=0x%lx, buffer_size = 0x%x, period_size = 0x%x, bits=%d, rate=%d\n", ptr1, ptr2, ptr, (int)runtime->buffer_size, (int)runtime->period_size, (int)runtime->frame_bits, (int)runtime->rate);
  896. return ptr;
  897. }
  898. /* operators */
  899. static struct snd_pcm_ops snd_ca0106_playback_front_ops = {
  900. .open = snd_ca0106_pcm_open_playback_front,
  901. .close = snd_ca0106_pcm_close_playback,
  902. .ioctl = snd_pcm_lib_ioctl,
  903. .hw_params = snd_ca0106_pcm_hw_params_playback,
  904. .hw_free = snd_ca0106_pcm_hw_free_playback,
  905. .prepare = snd_ca0106_pcm_prepare_playback,
  906. .trigger = snd_ca0106_pcm_trigger_playback,
  907. .pointer = snd_ca0106_pcm_pointer_playback,
  908. };
  909. static struct snd_pcm_ops snd_ca0106_capture_0_ops = {
  910. .open = snd_ca0106_pcm_open_0_capture,
  911. .close = snd_ca0106_pcm_close_capture,
  912. .ioctl = snd_pcm_lib_ioctl,
  913. .hw_params = snd_ca0106_pcm_hw_params_capture,
  914. .hw_free = snd_ca0106_pcm_hw_free_capture,
  915. .prepare = snd_ca0106_pcm_prepare_capture,
  916. .trigger = snd_ca0106_pcm_trigger_capture,
  917. .pointer = snd_ca0106_pcm_pointer_capture,
  918. };
  919. static struct snd_pcm_ops snd_ca0106_capture_1_ops = {
  920. .open = snd_ca0106_pcm_open_1_capture,
  921. .close = snd_ca0106_pcm_close_capture,
  922. .ioctl = snd_pcm_lib_ioctl,
  923. .hw_params = snd_ca0106_pcm_hw_params_capture,
  924. .hw_free = snd_ca0106_pcm_hw_free_capture,
  925. .prepare = snd_ca0106_pcm_prepare_capture,
  926. .trigger = snd_ca0106_pcm_trigger_capture,
  927. .pointer = snd_ca0106_pcm_pointer_capture,
  928. };
  929. static struct snd_pcm_ops snd_ca0106_capture_2_ops = {
  930. .open = snd_ca0106_pcm_open_2_capture,
  931. .close = snd_ca0106_pcm_close_capture,
  932. .ioctl = snd_pcm_lib_ioctl,
  933. .hw_params = snd_ca0106_pcm_hw_params_capture,
  934. .hw_free = snd_ca0106_pcm_hw_free_capture,
  935. .prepare = snd_ca0106_pcm_prepare_capture,
  936. .trigger = snd_ca0106_pcm_trigger_capture,
  937. .pointer = snd_ca0106_pcm_pointer_capture,
  938. };
  939. static struct snd_pcm_ops snd_ca0106_capture_3_ops = {
  940. .open = snd_ca0106_pcm_open_3_capture,
  941. .close = snd_ca0106_pcm_close_capture,
  942. .ioctl = snd_pcm_lib_ioctl,
  943. .hw_params = snd_ca0106_pcm_hw_params_capture,
  944. .hw_free = snd_ca0106_pcm_hw_free_capture,
  945. .prepare = snd_ca0106_pcm_prepare_capture,
  946. .trigger = snd_ca0106_pcm_trigger_capture,
  947. .pointer = snd_ca0106_pcm_pointer_capture,
  948. };
  949. static struct snd_pcm_ops snd_ca0106_playback_center_lfe_ops = {
  950. .open = snd_ca0106_pcm_open_playback_center_lfe,
  951. .close = snd_ca0106_pcm_close_playback,
  952. .ioctl = snd_pcm_lib_ioctl,
  953. .hw_params = snd_ca0106_pcm_hw_params_playback,
  954. .hw_free = snd_ca0106_pcm_hw_free_playback,
  955. .prepare = snd_ca0106_pcm_prepare_playback,
  956. .trigger = snd_ca0106_pcm_trigger_playback,
  957. .pointer = snd_ca0106_pcm_pointer_playback,
  958. };
  959. static struct snd_pcm_ops snd_ca0106_playback_unknown_ops = {
  960. .open = snd_ca0106_pcm_open_playback_unknown,
  961. .close = snd_ca0106_pcm_close_playback,
  962. .ioctl = snd_pcm_lib_ioctl,
  963. .hw_params = snd_ca0106_pcm_hw_params_playback,
  964. .hw_free = snd_ca0106_pcm_hw_free_playback,
  965. .prepare = snd_ca0106_pcm_prepare_playback,
  966. .trigger = snd_ca0106_pcm_trigger_playback,
  967. .pointer = snd_ca0106_pcm_pointer_playback,
  968. };
  969. static struct snd_pcm_ops snd_ca0106_playback_rear_ops = {
  970. .open = snd_ca0106_pcm_open_playback_rear,
  971. .close = snd_ca0106_pcm_close_playback,
  972. .ioctl = snd_pcm_lib_ioctl,
  973. .hw_params = snd_ca0106_pcm_hw_params_playback,
  974. .hw_free = snd_ca0106_pcm_hw_free_playback,
  975. .prepare = snd_ca0106_pcm_prepare_playback,
  976. .trigger = snd_ca0106_pcm_trigger_playback,
  977. .pointer = snd_ca0106_pcm_pointer_playback,
  978. };
  979. static unsigned short snd_ca0106_ac97_read(struct snd_ac97 *ac97,
  980. unsigned short reg)
  981. {
  982. struct snd_ca0106 *emu = ac97->private_data;
  983. unsigned long flags;
  984. unsigned short val;
  985. spin_lock_irqsave(&emu->emu_lock, flags);
  986. outb(reg, emu->port + AC97ADDRESS);
  987. val = inw(emu->port + AC97DATA);
  988. spin_unlock_irqrestore(&emu->emu_lock, flags);
  989. return val;
  990. }
  991. static void snd_ca0106_ac97_write(struct snd_ac97 *ac97,
  992. unsigned short reg, unsigned short val)
  993. {
  994. struct snd_ca0106 *emu = ac97->private_data;
  995. unsigned long flags;
  996. spin_lock_irqsave(&emu->emu_lock, flags);
  997. outb(reg, emu->port + AC97ADDRESS);
  998. outw(val, emu->port + AC97DATA);
  999. spin_unlock_irqrestore(&emu->emu_lock, flags);
  1000. }
  1001. static int snd_ca0106_ac97(struct snd_ca0106 *chip)
  1002. {
  1003. struct snd_ac97_bus *pbus;
  1004. struct snd_ac97_template ac97;
  1005. int err;
  1006. static struct snd_ac97_bus_ops ops = {
  1007. .write = snd_ca0106_ac97_write,
  1008. .read = snd_ca0106_ac97_read,
  1009. };
  1010. if ((err = snd_ac97_bus(chip->card, 0, &ops, NULL, &pbus)) < 0)
  1011. return err;
  1012. pbus->no_vra = 1; /* we don't need VRA */
  1013. memset(&ac97, 0, sizeof(ac97));
  1014. ac97.private_data = chip;
  1015. ac97.scaps = AC97_SCAP_NO_SPDIF;
  1016. return snd_ac97_mixer(pbus, &ac97, &chip->ac97);
  1017. }
  1018. static void ca0106_stop_chip(struct snd_ca0106 *chip);
  1019. static int snd_ca0106_free(struct snd_ca0106 *chip)
  1020. {
  1021. if (chip->res_port != NULL) {
  1022. /* avoid access to already used hardware */
  1023. ca0106_stop_chip(chip);
  1024. }
  1025. if (chip->irq >= 0)
  1026. free_irq(chip->irq, chip);
  1027. // release the data
  1028. #if 1
  1029. if (chip->buffer.area)
  1030. snd_dma_free_pages(&chip->buffer);
  1031. #endif
  1032. // release the i/o port
  1033. release_and_free_resource(chip->res_port);
  1034. pci_disable_device(chip->pci);
  1035. kfree(chip);
  1036. return 0;
  1037. }
  1038. static int snd_ca0106_dev_free(struct snd_device *device)
  1039. {
  1040. struct snd_ca0106 *chip = device->device_data;
  1041. return snd_ca0106_free(chip);
  1042. }
  1043. static irqreturn_t snd_ca0106_interrupt(int irq, void *dev_id)
  1044. {
  1045. unsigned int status;
  1046. struct snd_ca0106 *chip = dev_id;
  1047. int i;
  1048. int mask;
  1049. unsigned int stat76;
  1050. struct snd_ca0106_channel *pchannel;
  1051. status = inl(chip->port + IPR);
  1052. if (! status)
  1053. return IRQ_NONE;
  1054. stat76 = snd_ca0106_ptr_read(chip, EXTENDED_INT, 0);
  1055. //snd_printk("interrupt status = 0x%08x, stat76=0x%08x\n", status, stat76);
  1056. //snd_printk("ptr=0x%08x\n",snd_ca0106_ptr_read(chip, PLAYBACK_POINTER, 0));
  1057. mask = 0x11; /* 0x1 for one half, 0x10 for the other half period. */
  1058. for(i = 0; i < 4; i++) {
  1059. pchannel = &(chip->playback_channels[i]);
  1060. if (stat76 & mask) {
  1061. /* FIXME: Select the correct substream for period elapsed */
  1062. if(pchannel->use) {
  1063. snd_pcm_period_elapsed(pchannel->epcm->substream);
  1064. //printk(KERN_INFO "interrupt [%d] used\n", i);
  1065. }
  1066. }
  1067. //printk(KERN_INFO "channel=%p\n",pchannel);
  1068. //printk(KERN_INFO "interrupt stat76[%d] = %08x, use=%d, channel=%d\n", i, stat76, pchannel->use, pchannel->number);
  1069. mask <<= 1;
  1070. }
  1071. mask = 0x110000; /* 0x1 for one half, 0x10 for the other half period. */
  1072. for(i = 0; i < 4; i++) {
  1073. pchannel = &(chip->capture_channels[i]);
  1074. if (stat76 & mask) {
  1075. /* FIXME: Select the correct substream for period elapsed */
  1076. if(pchannel->use) {
  1077. snd_pcm_period_elapsed(pchannel->epcm->substream);
  1078. //printk(KERN_INFO "interrupt [%d] used\n", i);
  1079. }
  1080. }
  1081. //printk(KERN_INFO "channel=%p\n",pchannel);
  1082. //printk(KERN_INFO "interrupt stat76[%d] = %08x, use=%d, channel=%d\n", i, stat76, pchannel->use, pchannel->number);
  1083. mask <<= 1;
  1084. }
  1085. snd_ca0106_ptr_write(chip, EXTENDED_INT, 0, stat76);
  1086. if (chip->midi.dev_id &&
  1087. (status & (chip->midi.ipr_tx|chip->midi.ipr_rx))) {
  1088. if (chip->midi.interrupt)
  1089. chip->midi.interrupt(&chip->midi, status);
  1090. else
  1091. chip->midi.interrupt_disable(&chip->midi, chip->midi.tx_enable | chip->midi.rx_enable);
  1092. }
  1093. // acknowledge the interrupt if necessary
  1094. outl(status, chip->port+IPR);
  1095. return IRQ_HANDLED;
  1096. }
  1097. static int __devinit snd_ca0106_pcm(struct snd_ca0106 *emu, int device)
  1098. {
  1099. struct snd_pcm *pcm;
  1100. struct snd_pcm_substream *substream;
  1101. int err;
  1102. err = snd_pcm_new(emu->card, "ca0106", device, 1, 1, &pcm);
  1103. if (err < 0)
  1104. return err;
  1105. pcm->private_data = emu;
  1106. switch (device) {
  1107. case 0:
  1108. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ca0106_playback_front_ops);
  1109. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ca0106_capture_0_ops);
  1110. break;
  1111. case 1:
  1112. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ca0106_playback_rear_ops);
  1113. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ca0106_capture_1_ops);
  1114. break;
  1115. case 2:
  1116. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ca0106_playback_center_lfe_ops);
  1117. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ca0106_capture_2_ops);
  1118. break;
  1119. case 3:
  1120. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ca0106_playback_unknown_ops);
  1121. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ca0106_capture_3_ops);
  1122. break;
  1123. }
  1124. pcm->info_flags = 0;
  1125. pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
  1126. strcpy(pcm->name, "CA0106");
  1127. for(substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
  1128. substream;
  1129. substream = substream->next) {
  1130. if ((err = snd_pcm_lib_preallocate_pages(substream,
  1131. SNDRV_DMA_TYPE_DEV,
  1132. snd_dma_pci_data(emu->pci),
  1133. 64*1024, 64*1024)) < 0) /* FIXME: 32*1024 for sound buffer, between 32and64 for Periods table. */
  1134. return err;
  1135. }
  1136. for (substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
  1137. substream;
  1138. substream = substream->next) {
  1139. if ((err = snd_pcm_lib_preallocate_pages(substream,
  1140. SNDRV_DMA_TYPE_DEV,
  1141. snd_dma_pci_data(emu->pci),
  1142. 64*1024, 64*1024)) < 0)
  1143. return err;
  1144. }
  1145. emu->pcm[device] = pcm;
  1146. return 0;
  1147. }
  1148. #define SPI_REG(reg, value) (((reg) << SPI_REG_SHIFT) | (value))
  1149. static unsigned int spi_dac_init[] = {
  1150. SPI_REG(SPI_LDA1_REG, SPI_DA_BIT_0dB), /* 0dB dig. attenuation */
  1151. SPI_REG(SPI_RDA1_REG, SPI_DA_BIT_0dB),
  1152. SPI_REG(SPI_PL_REG, SPI_PL_BIT_L_L | SPI_PL_BIT_R_R | SPI_IZD_BIT),
  1153. SPI_REG(SPI_FMT_REG, SPI_FMT_BIT_I2S | SPI_IWL_BIT_24),
  1154. SPI_REG(SPI_LDA2_REG, SPI_DA_BIT_0dB),
  1155. SPI_REG(SPI_RDA2_REG, SPI_DA_BIT_0dB),
  1156. SPI_REG(SPI_LDA3_REG, SPI_DA_BIT_0dB),
  1157. SPI_REG(SPI_RDA3_REG, SPI_DA_BIT_0dB),
  1158. SPI_REG(SPI_MASTDA_REG, SPI_DA_BIT_0dB),
  1159. SPI_REG(9, 0x00),
  1160. SPI_REG(SPI_MS_REG, SPI_DACD0_BIT | SPI_DACD1_BIT | SPI_DACD2_BIT),
  1161. SPI_REG(12, 0x00),
  1162. SPI_REG(SPI_LDA4_REG, SPI_DA_BIT_0dB),
  1163. SPI_REG(SPI_RDA4_REG, SPI_DA_BIT_0dB | SPI_DA_BIT_UPDATE),
  1164. SPI_REG(SPI_DACD4_REG, 0x00),
  1165. };
  1166. static unsigned int i2c_adc_init[][2] = {
  1167. { 0x17, 0x00 }, /* Reset */
  1168. { 0x07, 0x00 }, /* Timeout */
  1169. { 0x0b, 0x22 }, /* Interface control */
  1170. { 0x0c, 0x22 }, /* Master mode control */
  1171. { 0x0d, 0x08 }, /* Powerdown control */
  1172. { 0x0e, 0xcf }, /* Attenuation Left 0x01 = -103dB, 0xff = 24dB */
  1173. { 0x0f, 0xcf }, /* Attenuation Right 0.5dB steps */
  1174. { 0x10, 0x7b }, /* ALC Control 1 */
  1175. { 0x11, 0x00 }, /* ALC Control 2 */
  1176. { 0x12, 0x32 }, /* ALC Control 3 */
  1177. { 0x13, 0x00 }, /* Noise gate control */
  1178. { 0x14, 0xa6 }, /* Limiter control */
  1179. { 0x15, ADC_MUX_LINEIN }, /* ADC Mixer control */
  1180. };
  1181. static void ca0106_init_chip(struct snd_ca0106 *chip)
  1182. {
  1183. int ch;
  1184. outl(0, chip->port + INTE);
  1185. /*
  1186. * Init to 0x02109204 :
  1187. * Clock accuracy = 0 (1000ppm)
  1188. * Sample Rate = 2 (48kHz)
  1189. * Audio Channel = 1 (Left of 2)
  1190. * Source Number = 0 (Unspecified)
  1191. * Generation Status = 1 (Original for Cat Code 12)
  1192. * Cat Code = 12 (Digital Signal Mixer)
  1193. * Mode = 0 (Mode 0)
  1194. * Emphasis = 0 (None)
  1195. * CP = 1 (Copyright unasserted)
  1196. * AN = 0 (Audio data)
  1197. * P = 0 (Consumer)
  1198. */
  1199. chip->spdif_bits[0] =
  1200. SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
  1201. SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
  1202. SPCS_GENERATIONSTATUS | 0x00001200 |
  1203. 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT;
  1204. snd_ca0106_ptr_write(chip, SPCS0, 0, chip->spdif_bits[0]);
  1205. /* Only SPCS1 has been tested */
  1206. chip->spdif_bits[1] =
  1207. SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
  1208. SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
  1209. SPCS_GENERATIONSTATUS | 0x00001200 |
  1210. 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT;
  1211. snd_ca0106_ptr_write(chip, SPCS1, 0, chip->spdif_bits[1]);
  1212. chip->spdif_bits[2] =
  1213. SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
  1214. SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
  1215. SPCS_GENERATIONSTATUS | 0x00001200 |
  1216. 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT;
  1217. snd_ca0106_ptr_write(chip, SPCS2, 0, chip->spdif_bits[2]);
  1218. chip->spdif_bits[3] =
  1219. SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
  1220. SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
  1221. SPCS_GENERATIONSTATUS | 0x00001200 |
  1222. 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT;
  1223. snd_ca0106_ptr_write(chip, SPCS3, 0, chip->spdif_bits[3]);
  1224. snd_ca0106_ptr_write(chip, PLAYBACK_MUTE, 0, 0x00fc0000);
  1225. snd_ca0106_ptr_write(chip, CAPTURE_MUTE, 0, 0x00fc0000);
  1226. /* Write 0x8000 to AC97_REC_GAIN to mute it. */
  1227. outb(AC97_REC_GAIN, chip->port + AC97ADDRESS);
  1228. outw(0x8000, chip->port + AC97DATA);
  1229. #if 0 /* FIXME: what are these? */
  1230. snd_ca0106_ptr_write(chip, SPCS0, 0, 0x2108006);
  1231. snd_ca0106_ptr_write(chip, 0x42, 0, 0x2108006);
  1232. snd_ca0106_ptr_write(chip, 0x43, 0, 0x2108006);
  1233. snd_ca0106_ptr_write(chip, 0x44, 0, 0x2108006);
  1234. #endif
  1235. /* OSS drivers set this. */
  1236. /* snd_ca0106_ptr_write(chip, SPDIF_SELECT2, 0, 0xf0f003f); */
  1237. /* Analog or Digital output */
  1238. snd_ca0106_ptr_write(chip, SPDIF_SELECT1, 0, 0xf);
  1239. /* 0x0b000000 for digital, 0x000b0000 for analog, from win2000 drivers.
  1240. * Use 0x000f0000 for surround71
  1241. */
  1242. snd_ca0106_ptr_write(chip, SPDIF_SELECT2, 0, 0x000f0000);
  1243. chip->spdif_enable = 0; /* Set digital SPDIF output off */
  1244. /*snd_ca0106_ptr_write(chip, 0x45, 0, 0);*/ /* Analogue out */
  1245. /*snd_ca0106_ptr_write(chip, 0x45, 0, 0xf00);*/ /* Digital out */
  1246. /* goes to 0x40c80000 when doing SPDIF IN/OUT */
  1247. snd_ca0106_ptr_write(chip, CAPTURE_CONTROL, 0, 0x40c81000);
  1248. /* (Mute) CAPTURE feedback into PLAYBACK volume.
  1249. * Only lower 16 bits matter.
  1250. */
  1251. snd_ca0106_ptr_write(chip, CAPTURE_CONTROL, 1, 0xffffffff);
  1252. /* SPDIF IN Volume */
  1253. snd_ca0106_ptr_write(chip, CAPTURE_CONTROL, 2, 0x30300000);
  1254. /* SPDIF IN Volume, 0x70 = (vol & 0x3f) | 0x40 */
  1255. snd_ca0106_ptr_write(chip, CAPTURE_CONTROL, 3, 0x00700000);
  1256. snd_ca0106_ptr_write(chip, PLAYBACK_ROUTING1, 0, 0x32765410);
  1257. snd_ca0106_ptr_write(chip, PLAYBACK_ROUTING2, 0, 0x76767676);
  1258. snd_ca0106_ptr_write(chip, CAPTURE_ROUTING1, 0, 0x32765410);
  1259. snd_ca0106_ptr_write(chip, CAPTURE_ROUTING2, 0, 0x76767676);
  1260. for (ch = 0; ch < 4; ch++) {
  1261. /* Only high 16 bits matter */
  1262. snd_ca0106_ptr_write(chip, CAPTURE_VOLUME1, ch, 0x30303030);
  1263. snd_ca0106_ptr_write(chip, CAPTURE_VOLUME2, ch, 0x30303030);
  1264. #if 0 /* Mute */
  1265. snd_ca0106_ptr_write(chip, PLAYBACK_VOLUME1, ch, 0x40404040);
  1266. snd_ca0106_ptr_write(chip, PLAYBACK_VOLUME2, ch, 0x40404040);
  1267. snd_ca0106_ptr_write(chip, PLAYBACK_VOLUME1, ch, 0xffffffff);
  1268. snd_ca0106_ptr_write(chip, PLAYBACK_VOLUME2, ch, 0xffffffff);
  1269. #endif
  1270. }
  1271. if (chip->details->i2c_adc == 1) {
  1272. /* Select MIC, Line in, TAD in, AUX in */
  1273. snd_ca0106_ptr_write(chip, CAPTURE_SOURCE, 0x0, 0x333300e4);
  1274. /* Default to CAPTURE_SOURCE to i2s in */
  1275. chip->capture_source = 3;
  1276. } else if (chip->details->ac97 == 1) {
  1277. /* Default to AC97 in */
  1278. snd_ca0106_ptr_write(chip, CAPTURE_SOURCE, 0x0, 0x444400e4);
  1279. /* Default to CAPTURE_SOURCE to AC97 in */
  1280. chip->capture_source = 4;
  1281. } else {
  1282. /* Select MIC, Line in, TAD in, AUX in */
  1283. snd_ca0106_ptr_write(chip, CAPTURE_SOURCE, 0x0, 0x333300e4);
  1284. /* Default to Set CAPTURE_SOURCE to i2s in */
  1285. chip->capture_source = 3;
  1286. }
  1287. if (chip->details->gpio_type == 2) {
  1288. /* The SB0438 use GPIO differently. */
  1289. /* FIXME: Still need to find out what the other GPIO bits do.
  1290. * E.g. For digital spdif out.
  1291. */
  1292. outl(0x0, chip->port+GPIO);
  1293. /* outl(0x00f0e000, chip->port+GPIO); */ /* Analog */
  1294. outl(0x005f5301, chip->port+GPIO); /* Analog */
  1295. } else if (chip->details->gpio_type == 1) {
  1296. /* The SB0410 and SB0413 use GPIO differently. */
  1297. /* FIXME: Still need to find out what the other GPIO bits do.
  1298. * E.g. For digital spdif out.
  1299. */
  1300. outl(0x0, chip->port+GPIO);
  1301. /* outl(0x00f0e000, chip->port+GPIO); */ /* Analog */
  1302. outl(0x005f5301, chip->port+GPIO); /* Analog */
  1303. } else {
  1304. outl(0x0, chip->port+GPIO);
  1305. outl(0x005f03a3, chip->port+GPIO); /* Analog */
  1306. /* outl(0x005f02a2, chip->port+GPIO); */ /* SPDIF */
  1307. }
  1308. snd_ca0106_intr_enable(chip, 0x105); /* Win2000 uses 0x1e0 */
  1309. /* outl(HCFG_LOCKSOUNDCACHE|HCFG_AUDIOENABLE, chip->port+HCFG); */
  1310. /* 0x1000 causes AC3 to fails. Maybe it effects 24 bit output. */
  1311. /* outl(0x00001409, chip->port+HCFG); */
  1312. /* outl(0x00000009, chip->port+HCFG); */
  1313. /* AC97 2.0, Enable outputs. */
  1314. outl(HCFG_AC97 | HCFG_AUDIOENABLE, chip->port+HCFG);
  1315. if (chip->details->i2c_adc == 1) {
  1316. /* The SB0410 and SB0413 use I2C to control ADC. */
  1317. int size, n;
  1318. size = ARRAY_SIZE(i2c_adc_init);
  1319. /* snd_printk("I2C:array size=0x%x\n", size); */
  1320. for (n = 0; n < size; n++)
  1321. snd_ca0106_i2c_write(chip, i2c_adc_init[n][0],
  1322. i2c_adc_init[n][1]);
  1323. for (n = 0; n < 4; n++) {
  1324. chip->i2c_capture_volume[n][0] = 0xcf;
  1325. chip->i2c_capture_volume[n][1] = 0xcf;
  1326. }
  1327. chip->i2c_capture_source = 2; /* Line in */
  1328. /* Enable Line-in capture. MIC in currently untested. */
  1329. /* snd_ca0106_i2c_write(chip, ADC_MUX, ADC_MUX_LINEIN); */
  1330. }
  1331. if (chip->details->spi_dac == 1) {
  1332. /* The SB0570 use SPI to control DAC. */
  1333. int size, n;
  1334. size = ARRAY_SIZE(spi_dac_init);
  1335. for (n = 0; n < size; n++) {
  1336. int reg = spi_dac_init[n] >> SPI_REG_SHIFT;
  1337. snd_ca0106_spi_write(chip, spi_dac_init[n]);
  1338. if (reg < ARRAY_SIZE(chip->spi_dac_reg))
  1339. chip->spi_dac_reg[reg] = spi_dac_init[n];
  1340. }
  1341. }
  1342. }
  1343. static void ca0106_stop_chip(struct snd_ca0106 *chip)
  1344. {
  1345. /* disable interrupts */
  1346. snd_ca0106_ptr_write(chip, BASIC_INTERRUPT, 0, 0);
  1347. outl(0, chip->port + INTE);
  1348. snd_ca0106_ptr_write(chip, EXTENDED_INT_MASK, 0, 0);
  1349. udelay(1000);
  1350. /* disable audio */
  1351. /* outl(HCFG_LOCKSOUNDCACHE, chip->port + HCFG); */
  1352. outl(0, chip->port + HCFG);
  1353. /* FIXME: We need to stop and DMA transfers here.
  1354. * But as I am not sure how yet, we cannot from the dma pages.
  1355. * So we can fix: snd-malloc: Memory leak? pages not freed = 8
  1356. */
  1357. }
  1358. static int __devinit snd_ca0106_create(int dev, struct snd_card *card,
  1359. struct pci_dev *pci,
  1360. struct snd_ca0106 **rchip)
  1361. {
  1362. struct snd_ca0106 *chip;
  1363. struct snd_ca0106_details *c;
  1364. int err;
  1365. static struct snd_device_ops ops = {
  1366. .dev_free = snd_ca0106_dev_free,
  1367. };
  1368. *rchip = NULL;
  1369. err = pci_enable_device(pci);
  1370. if (err < 0)
  1371. return err;
  1372. if (pci_set_dma_mask(pci, DMA_32BIT_MASK) < 0 ||
  1373. pci_set_consistent_dma_mask(pci, DMA_32BIT_MASK) < 0) {
  1374. printk(KERN_ERR "error to set 32bit mask DMA\n");
  1375. pci_disable_device(pci);
  1376. return -ENXIO;
  1377. }
  1378. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  1379. if (chip == NULL) {
  1380. pci_disable_device(pci);
  1381. return -ENOMEM;
  1382. }
  1383. chip->card = card;
  1384. chip->pci = pci;
  1385. chip->irq = -1;
  1386. spin_lock_init(&chip->emu_lock);
  1387. chip->port = pci_resource_start(pci, 0);
  1388. chip->res_port = request_region(chip->port, 0x20, "snd_ca0106");
  1389. if (!chip->res_port) {
  1390. snd_ca0106_free(chip);
  1391. printk(KERN_ERR "cannot allocate the port\n");
  1392. return -EBUSY;
  1393. }
  1394. if (request_irq(pci->irq, snd_ca0106_interrupt,
  1395. IRQF_SHARED, "snd_ca0106", chip)) {
  1396. snd_ca0106_free(chip);
  1397. printk(KERN_ERR "cannot grab irq\n");
  1398. return -EBUSY;
  1399. }
  1400. chip->irq = pci->irq;
  1401. /* This stores the periods table. */
  1402. if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
  1403. 1024, &chip->buffer) < 0) {
  1404. snd_ca0106_free(chip);
  1405. return -ENOMEM;
  1406. }
  1407. pci_set_master(pci);
  1408. /* read serial */
  1409. pci_read_config_dword(pci, PCI_SUBSYSTEM_VENDOR_ID, &chip->serial);
  1410. pci_read_config_word(pci, PCI_SUBSYSTEM_ID, &chip->model);
  1411. printk(KERN_INFO "snd-ca0106: Model %04x Rev %08x Serial %08x\n",
  1412. chip->model, pci->revision, chip->serial);
  1413. strcpy(card->driver, "CA0106");
  1414. strcpy(card->shortname, "CA0106");
  1415. for (c = ca0106_chip_details; c->serial; c++) {
  1416. if (subsystem[dev]) {
  1417. if (c->serial == subsystem[dev])
  1418. break;
  1419. } else if (c->serial == chip->serial)
  1420. break;
  1421. }
  1422. chip->details = c;
  1423. if (subsystem[dev]) {
  1424. printk(KERN_INFO "snd-ca0106: Sound card name=%s, "
  1425. "subsystem=0x%x. Forced to subsystem=0x%x\n",
  1426. c->name, chip->serial, subsystem[dev]);
  1427. }
  1428. sprintf(card->longname, "%s at 0x%lx irq %i",
  1429. c->name, chip->port, chip->irq);
  1430. ca0106_init_chip(chip);
  1431. err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
  1432. if (err < 0) {
  1433. snd_ca0106_free(chip);
  1434. return err;
  1435. }
  1436. *rchip = chip;
  1437. return 0;
  1438. }
  1439. static void ca0106_midi_interrupt_enable(struct snd_ca_midi *midi, int intr)
  1440. {
  1441. snd_ca0106_intr_enable((struct snd_ca0106 *)(midi->dev_id), intr);
  1442. }
  1443. static void ca0106_midi_interrupt_disable(struct snd_ca_midi *midi, int intr)
  1444. {
  1445. snd_ca0106_intr_disable((struct snd_ca0106 *)(midi->dev_id), intr);
  1446. }
  1447. static unsigned char ca0106_midi_read(struct snd_ca_midi *midi, int idx)
  1448. {
  1449. return (unsigned char)snd_ca0106_ptr_read((struct snd_ca0106 *)(midi->dev_id),
  1450. midi->port + idx, 0);
  1451. }
  1452. static void ca0106_midi_write(struct snd_ca_midi *midi, int data, int idx)
  1453. {
  1454. snd_ca0106_ptr_write((struct snd_ca0106 *)(midi->dev_id), midi->port + idx, 0, data);
  1455. }
  1456. static struct snd_card *ca0106_dev_id_card(void *dev_id)
  1457. {
  1458. return ((struct snd_ca0106 *)dev_id)->card;
  1459. }
  1460. static int ca0106_dev_id_port(void *dev_id)
  1461. {
  1462. return ((struct snd_ca0106 *)dev_id)->port;
  1463. }
  1464. static int __devinit snd_ca0106_midi(struct snd_ca0106 *chip, unsigned int channel)
  1465. {
  1466. struct snd_ca_midi *midi;
  1467. char *name;
  1468. int err;
  1469. if (channel == CA0106_MIDI_CHAN_B) {
  1470. name = "CA0106 MPU-401 (UART) B";
  1471. midi = &chip->midi2;
  1472. midi->tx_enable = INTE_MIDI_TX_B;
  1473. midi->rx_enable = INTE_MIDI_RX_B;
  1474. midi->ipr_tx = IPR_MIDI_TX_B;
  1475. midi->ipr_rx = IPR_MIDI_RX_B;
  1476. midi->port = MIDI_UART_B_DATA;
  1477. } else {
  1478. name = "CA0106 MPU-401 (UART)";
  1479. midi = &chip->midi;
  1480. midi->tx_enable = INTE_MIDI_TX_A;
  1481. midi->rx_enable = INTE_MIDI_TX_B;
  1482. midi->ipr_tx = IPR_MIDI_TX_A;
  1483. midi->ipr_rx = IPR_MIDI_RX_A;
  1484. midi->port = MIDI_UART_A_DATA;
  1485. }
  1486. midi->reset = CA0106_MPU401_RESET;
  1487. midi->enter_uart = CA0106_MPU401_ENTER_UART;
  1488. midi->ack = CA0106_MPU401_ACK;
  1489. midi->input_avail = CA0106_MIDI_INPUT_AVAIL;
  1490. midi->output_ready = CA0106_MIDI_OUTPUT_READY;
  1491. midi->channel = channel;
  1492. midi->interrupt_enable = ca0106_midi_interrupt_enable;
  1493. midi->interrupt_disable = ca0106_midi_interrupt_disable;
  1494. midi->read = ca0106_midi_read;
  1495. midi->write = ca0106_midi_write;
  1496. midi->get_dev_id_card = ca0106_dev_id_card;
  1497. midi->get_dev_id_port = ca0106_dev_id_port;
  1498. midi->dev_id = chip;
  1499. if ((err = ca_midi_init(chip, midi, 0, name)) < 0)
  1500. return err;
  1501. return 0;
  1502. }
  1503. static int __devinit snd_ca0106_probe(struct pci_dev *pci,
  1504. const struct pci_device_id *pci_id)
  1505. {
  1506. static int dev;
  1507. struct snd_card *card;
  1508. struct snd_ca0106 *chip;
  1509. int i, err;
  1510. if (dev >= SNDRV_CARDS)
  1511. return -ENODEV;
  1512. if (!enable[dev]) {
  1513. dev++;
  1514. return -ENOENT;
  1515. }
  1516. card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
  1517. if (card == NULL)
  1518. return -ENOMEM;
  1519. err = snd_ca0106_create(dev, card, pci, &chip);
  1520. if (err < 0)
  1521. goto error;
  1522. for (i = 0; i < 4; i++) {
  1523. err = snd_ca0106_pcm(chip, i);
  1524. if (err < 0)
  1525. goto error;
  1526. }
  1527. if (chip->details->ac97 == 1) {
  1528. /* The SB0410 and SB0413 do not have an AC97 chip. */
  1529. err = snd_ca0106_ac97(chip);
  1530. if (err < 0)
  1531. goto error;
  1532. }
  1533. err = snd_ca0106_mixer(chip);
  1534. if (err < 0)
  1535. goto error;
  1536. snd_printdd("ca0106: probe for MIDI channel A ...");
  1537. err = snd_ca0106_midi(chip, CA0106_MIDI_CHAN_A);
  1538. if (err < 0)
  1539. goto error;
  1540. snd_printdd(" done.\n");
  1541. #ifdef CONFIG_PROC_FS
  1542. snd_ca0106_proc_init(chip);
  1543. #endif
  1544. snd_card_set_dev(card, &pci->dev);
  1545. err = snd_card_register(card);
  1546. if (err < 0)
  1547. goto error;
  1548. pci_set_drvdata(pci, card);
  1549. dev++;
  1550. return 0;
  1551. error:
  1552. snd_card_free(card);
  1553. return err;
  1554. }
  1555. static void __devexit snd_ca0106_remove(struct pci_dev *pci)
  1556. {
  1557. snd_card_free(pci_get_drvdata(pci));
  1558. pci_set_drvdata(pci, NULL);
  1559. }
  1560. #ifdef CONFIG_PM
  1561. static int snd_ca0106_suspend(struct pci_dev *pci, pm_message_t state)
  1562. {
  1563. struct snd_card *card = pci_get_drvdata(pci);
  1564. struct snd_ca0106 *chip = card->private_data;
  1565. int i;
  1566. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  1567. for (i = 0; i < 4; i++)
  1568. snd_pcm_suspend_all(chip->pcm[i]);
  1569. snd_ac97_suspend(chip->ac97);
  1570. snd_ca0106_mixer_suspend(chip);
  1571. ca0106_stop_chip(chip);
  1572. pci_disable_device(pci);
  1573. pci_save_state(pci);
  1574. pci_set_power_state(pci, pci_choose_state(pci, state));
  1575. return 0;
  1576. }
  1577. static int snd_ca0106_resume(struct pci_dev *pci)
  1578. {
  1579. struct snd_card *card = pci_get_drvdata(pci);
  1580. struct snd_ca0106 *chip = card->private_data;
  1581. int i;
  1582. pci_set_power_state(pci, PCI_D0);
  1583. pci_restore_state(pci);
  1584. if (pci_enable_device(pci) < 0) {
  1585. snd_card_disconnect(card);
  1586. return -EIO;
  1587. }
  1588. pci_set_master(pci);
  1589. ca0106_init_chip(chip);
  1590. snd_ac97_resume(chip->ac97);
  1591. snd_ca0106_mixer_resume(chip);
  1592. if (chip->details->spi_dac) {
  1593. for (i = 0; i < ARRAY_SIZE(chip->spi_dac_reg); i++)
  1594. snd_ca0106_spi_write(chip, chip->spi_dac_reg[i]);
  1595. }
  1596. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  1597. return 0;
  1598. }
  1599. #endif
  1600. // PCI IDs
  1601. static struct pci_device_id snd_ca0106_ids[] = {
  1602. { 0x1102, 0x0007, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 }, /* Audigy LS or Live 24bit */
  1603. { 0, }
  1604. };
  1605. MODULE_DEVICE_TABLE(pci, snd_ca0106_ids);
  1606. // pci_driver definition
  1607. static struct pci_driver driver = {
  1608. .name = "CA0106",
  1609. .id_table = snd_ca0106_ids,
  1610. .probe = snd_ca0106_probe,
  1611. .remove = __devexit_p(snd_ca0106_remove),
  1612. #ifdef CONFIG_PM
  1613. .suspend = snd_ca0106_suspend,
  1614. .resume = snd_ca0106_resume,
  1615. #endif
  1616. };
  1617. // initialization of the module
  1618. static int __init alsa_card_ca0106_init(void)
  1619. {
  1620. return pci_register_driver(&driver);
  1621. }
  1622. // clean up the module
  1623. static void __exit alsa_card_ca0106_exit(void)
  1624. {
  1625. pci_unregister_driver(&driver);
  1626. }
  1627. module_init(alsa_card_ca0106_init)
  1628. module_exit(alsa_card_ca0106_exit)