ca0106_main.c 53 KB

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