ca0106_main.c 56 KB

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