ca0106_main.c 56 KB

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