ca0106_main.c 56 KB

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