ca0106_main.c 52 KB

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