ca0106_main.c 53 KB

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