ca0106_main.c 54 KB

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