ca0106_main.c 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970
  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 initially 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/module.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 bool 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 = 0x4021 } ,
  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 = 0x4021 } ,
  237. /* Sound Blaster 5.1vx
  238. * Tested: Playback on front, rear, center/lfe speakers
  239. * Not-Tested: Capture
  240. */
  241. { .serial = 0x10041102,
  242. .name = "Sound Blaster 5.1vx [SB1070]",
  243. .gpio_type = 1,
  244. .i2c_adc = 0,
  245. .spi_dac = 0x0124
  246. } ,
  247. /* MSI K8N Diamond Motherboard with onboard SB Live 24bit without AC97 */
  248. /* SB0438
  249. * CTRL:CA0106-DAT
  250. * ADC: WM8775SEDS
  251. * DAC: CS4382-KQZ
  252. */
  253. { .serial = 0x10091462,
  254. .name = "MSI K8N Diamond MB [SB0438]",
  255. .gpio_type = 2,
  256. .i2c_adc = 1 } ,
  257. /* MSI K8N Diamond PLUS MB */
  258. { .serial = 0x10091102,
  259. .name = "MSI K8N Diamond MB",
  260. .gpio_type = 2,
  261. .i2c_adc = 1,
  262. .spi_dac = 0x4021 } ,
  263. /* Giga-byte GA-G1975X mobo
  264. * Novell bnc#395807
  265. */
  266. /* FIXME: the GPIO and I2C setting aren't tested well */
  267. { .serial = 0x1458a006,
  268. .name = "Giga-byte GA-G1975X",
  269. .gpio_type = 1,
  270. .i2c_adc = 1 },
  271. /* Shuttle XPC SD31P which has an onboard Creative Labs
  272. * Sound Blaster Live! 24-bit EAX
  273. * high-definition 7.1 audio processor".
  274. * Added using info from andrewvegan in alsa bug #1298
  275. */
  276. { .serial = 0x30381297,
  277. .name = "Shuttle XPC SD31P [SD31P]",
  278. .gpio_type = 1,
  279. .i2c_adc = 1 } ,
  280. /* Shuttle XPC SD11G5 which has an onboard Creative Labs
  281. * Sound Blaster Live! 24-bit EAX
  282. * high-definition 7.1 audio processor".
  283. * Fixes ALSA bug#1600
  284. */
  285. { .serial = 0x30411297,
  286. .name = "Shuttle XPC SD11G5 [SD11G5]",
  287. .gpio_type = 1,
  288. .i2c_adc = 1 } ,
  289. { .serial = 0,
  290. .name = "AudigyLS [Unknown]" }
  291. };
  292. /* hardware definition */
  293. static const struct snd_pcm_hardware snd_ca0106_playback_hw = {
  294. .info = SNDRV_PCM_INFO_MMAP |
  295. SNDRV_PCM_INFO_INTERLEAVED |
  296. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  297. SNDRV_PCM_INFO_MMAP_VALID |
  298. SNDRV_PCM_INFO_SYNC_START,
  299. .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE,
  300. .rates = (SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_96000 |
  301. SNDRV_PCM_RATE_192000),
  302. .rate_min = 48000,
  303. .rate_max = 192000,
  304. .channels_min = 2, //1,
  305. .channels_max = 2, //6,
  306. .buffer_bytes_max = ((65536 - 64) * 8),
  307. .period_bytes_min = 64,
  308. .period_bytes_max = (65536 - 64),
  309. .periods_min = 2,
  310. .periods_max = 8,
  311. .fifo_size = 0,
  312. };
  313. static const struct snd_pcm_hardware snd_ca0106_capture_hw = {
  314. .info = (SNDRV_PCM_INFO_MMAP |
  315. SNDRV_PCM_INFO_INTERLEAVED |
  316. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  317. SNDRV_PCM_INFO_MMAP_VALID),
  318. .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE,
  319. #if 0 /* FIXME: looks like 44.1kHz capture causes noisy output on 48kHz */
  320. .rates = (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
  321. SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_192000),
  322. .rate_min = 44100,
  323. #else
  324. .rates = (SNDRV_PCM_RATE_48000 |
  325. SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_192000),
  326. .rate_min = 48000,
  327. #endif /* FIXME */
  328. .rate_max = 192000,
  329. .channels_min = 2,
  330. .channels_max = 2,
  331. .buffer_bytes_max = 65536 - 128,
  332. .period_bytes_min = 64,
  333. .period_bytes_max = 32768 - 64,
  334. .periods_min = 2,
  335. .periods_max = 2,
  336. .fifo_size = 0,
  337. };
  338. unsigned int snd_ca0106_ptr_read(struct snd_ca0106 * emu,
  339. unsigned int reg,
  340. unsigned int chn)
  341. {
  342. unsigned long flags;
  343. unsigned int regptr, val;
  344. regptr = (reg << 16) | chn;
  345. spin_lock_irqsave(&emu->emu_lock, flags);
  346. outl(regptr, emu->port + PTR);
  347. val = inl(emu->port + DATA);
  348. spin_unlock_irqrestore(&emu->emu_lock, flags);
  349. return val;
  350. }
  351. void snd_ca0106_ptr_write(struct snd_ca0106 *emu,
  352. unsigned int reg,
  353. unsigned int chn,
  354. unsigned int data)
  355. {
  356. unsigned int regptr;
  357. unsigned long flags;
  358. regptr = (reg << 16) | chn;
  359. spin_lock_irqsave(&emu->emu_lock, flags);
  360. outl(regptr, emu->port + PTR);
  361. outl(data, emu->port + DATA);
  362. spin_unlock_irqrestore(&emu->emu_lock, flags);
  363. }
  364. int snd_ca0106_spi_write(struct snd_ca0106 * emu,
  365. unsigned int data)
  366. {
  367. unsigned int reset, set;
  368. unsigned int reg, tmp;
  369. int n, result;
  370. reg = SPI;
  371. if (data > 0xffff) /* Only 16bit values allowed */
  372. return 1;
  373. tmp = snd_ca0106_ptr_read(emu, reg, 0);
  374. reset = (tmp & ~0x3ffff) | 0x20000; /* Set xxx20000 */
  375. set = reset | 0x10000; /* Set xxx1xxxx */
  376. snd_ca0106_ptr_write(emu, reg, 0, reset | data);
  377. tmp = snd_ca0106_ptr_read(emu, reg, 0); /* write post */
  378. snd_ca0106_ptr_write(emu, reg, 0, set | data);
  379. result = 1;
  380. /* Wait for status bit to return to 0 */
  381. for (n = 0; n < 100; n++) {
  382. udelay(10);
  383. tmp = snd_ca0106_ptr_read(emu, reg, 0);
  384. if (!(tmp & 0x10000)) {
  385. result = 0;
  386. break;
  387. }
  388. }
  389. if (result) /* Timed out */
  390. return 1;
  391. snd_ca0106_ptr_write(emu, reg, 0, reset | data);
  392. tmp = snd_ca0106_ptr_read(emu, reg, 0); /* Write post */
  393. return 0;
  394. }
  395. /* The ADC does not support i2c read, so only write is implemented */
  396. int snd_ca0106_i2c_write(struct snd_ca0106 *emu,
  397. u32 reg,
  398. u32 value)
  399. {
  400. u32 tmp;
  401. int timeout = 0;
  402. int status;
  403. int retry;
  404. if ((reg > 0x7f) || (value > 0x1ff)) {
  405. dev_err(emu->card->dev, "i2c_write: invalid values.\n");
  406. return -EINVAL;
  407. }
  408. tmp = reg << 25 | value << 16;
  409. /*
  410. dev_dbg(emu->card->dev, "I2C-write:reg=0x%x, value=0x%x\n", reg, value);
  411. */
  412. /* Not sure what this I2C channel controls. */
  413. /* snd_ca0106_ptr_write(emu, I2C_D0, 0, tmp); */
  414. /* This controls the I2C connected to the WM8775 ADC Codec */
  415. snd_ca0106_ptr_write(emu, I2C_D1, 0, tmp);
  416. for (retry = 0; retry < 10; retry++) {
  417. /* Send the data to i2c */
  418. //tmp = snd_ca0106_ptr_read(emu, I2C_A, 0);
  419. //tmp = tmp & ~(I2C_A_ADC_READ|I2C_A_ADC_LAST|I2C_A_ADC_START|I2C_A_ADC_ADD_MASK);
  420. tmp = 0;
  421. tmp = tmp | (I2C_A_ADC_LAST|I2C_A_ADC_START|I2C_A_ADC_ADD);
  422. snd_ca0106_ptr_write(emu, I2C_A, 0, tmp);
  423. /* Wait till the transaction ends */
  424. while (1) {
  425. status = snd_ca0106_ptr_read(emu, I2C_A, 0);
  426. /*dev_dbg(emu->card->dev, "I2C:status=0x%x\n", status);*/
  427. timeout++;
  428. if ((status & I2C_A_ADC_START) == 0)
  429. break;
  430. if (timeout > 1000)
  431. break;
  432. }
  433. //Read back and see if the transaction is successful
  434. if ((status & I2C_A_ADC_ABORT) == 0)
  435. break;
  436. }
  437. if (retry == 10) {
  438. dev_err(emu->card->dev, "Writing to ADC failed!\n");
  439. return -EINVAL;
  440. }
  441. return 0;
  442. }
  443. static void snd_ca0106_intr_enable(struct snd_ca0106 *emu, unsigned int intrenb)
  444. {
  445. unsigned long flags;
  446. unsigned int intr_enable;
  447. spin_lock_irqsave(&emu->emu_lock, flags);
  448. intr_enable = inl(emu->port + INTE) | intrenb;
  449. outl(intr_enable, emu->port + INTE);
  450. spin_unlock_irqrestore(&emu->emu_lock, flags);
  451. }
  452. static void snd_ca0106_intr_disable(struct snd_ca0106 *emu, unsigned int intrenb)
  453. {
  454. unsigned long flags;
  455. unsigned int intr_enable;
  456. spin_lock_irqsave(&emu->emu_lock, flags);
  457. intr_enable = inl(emu->port + INTE) & ~intrenb;
  458. outl(intr_enable, emu->port + INTE);
  459. spin_unlock_irqrestore(&emu->emu_lock, flags);
  460. }
  461. static void snd_ca0106_pcm_free_substream(struct snd_pcm_runtime *runtime)
  462. {
  463. kfree(runtime->private_data);
  464. }
  465. static const int spi_dacd_reg[] = {
  466. SPI_DACD0_REG,
  467. SPI_DACD1_REG,
  468. SPI_DACD2_REG,
  469. 0,
  470. SPI_DACD4_REG,
  471. };
  472. static const int spi_dacd_bit[] = {
  473. SPI_DACD0_BIT,
  474. SPI_DACD1_BIT,
  475. SPI_DACD2_BIT,
  476. 0,
  477. SPI_DACD4_BIT,
  478. };
  479. static void restore_spdif_bits(struct snd_ca0106 *chip, int idx)
  480. {
  481. if (chip->spdif_str_bits[idx] != chip->spdif_bits[idx]) {
  482. chip->spdif_str_bits[idx] = chip->spdif_bits[idx];
  483. snd_ca0106_ptr_write(chip, SPCS0 + idx, 0,
  484. chip->spdif_str_bits[idx]);
  485. }
  486. }
  487. static int snd_ca0106_channel_dac(struct snd_ca0106 *chip,
  488. struct snd_ca0106_details *details,
  489. int channel_id)
  490. {
  491. switch (channel_id) {
  492. case PCM_FRONT_CHANNEL:
  493. return (details->spi_dac & 0xf000) >> (4 * 3);
  494. case PCM_REAR_CHANNEL:
  495. return (details->spi_dac & 0x0f00) >> (4 * 2);
  496. case PCM_CENTER_LFE_CHANNEL:
  497. return (details->spi_dac & 0x00f0) >> (4 * 1);
  498. case PCM_UNKNOWN_CHANNEL:
  499. return (details->spi_dac & 0x000f) >> (4 * 0);
  500. default:
  501. dev_dbg(chip->card->dev, "ca0106: unknown channel_id %d\n",
  502. channel_id);
  503. }
  504. return 0;
  505. }
  506. static int snd_ca0106_pcm_power_dac(struct snd_ca0106 *chip, int channel_id,
  507. int power)
  508. {
  509. if (chip->details->spi_dac) {
  510. const int dac = snd_ca0106_channel_dac(chip, chip->details,
  511. channel_id);
  512. const int reg = spi_dacd_reg[dac];
  513. const int bit = spi_dacd_bit[dac];
  514. if (power)
  515. /* Power up */
  516. chip->spi_dac_reg[reg] &= ~bit;
  517. else
  518. /* Power down */
  519. chip->spi_dac_reg[reg] |= bit;
  520. return snd_ca0106_spi_write(chip, chip->spi_dac_reg[reg]);
  521. }
  522. return 0;
  523. }
  524. /* open_playback callback */
  525. static int snd_ca0106_pcm_open_playback_channel(struct snd_pcm_substream *substream,
  526. int channel_id)
  527. {
  528. struct snd_ca0106 *chip = snd_pcm_substream_chip(substream);
  529. struct snd_ca0106_channel *channel = &(chip->playback_channels[channel_id]);
  530. struct snd_ca0106_pcm *epcm;
  531. struct snd_pcm_runtime *runtime = substream->runtime;
  532. int err;
  533. epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
  534. if (epcm == NULL)
  535. return -ENOMEM;
  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_playback_hw;
  542. channel->emu = chip;
  543. channel->number = channel_id;
  544. channel->use = 1;
  545. /*
  546. dev_dbg(chip->card->dev, "open:channel_id=%d, chip=%p, channel=%p\n",
  547. channel_id, chip, channel);
  548. */
  549. //channel->interrupt = snd_ca0106_pcm_channel_interrupt;
  550. channel->epcm = epcm;
  551. if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
  552. return err;
  553. if ((err = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64)) < 0)
  554. return err;
  555. snd_pcm_set_sync(substream);
  556. /* Front channel dac should already be on */
  557. if (channel_id != PCM_FRONT_CHANNEL) {
  558. err = snd_ca0106_pcm_power_dac(chip, channel_id, 1);
  559. if (err < 0)
  560. return err;
  561. }
  562. restore_spdif_bits(chip, channel_id);
  563. return 0;
  564. }
  565. /* close callback */
  566. static int snd_ca0106_pcm_close_playback(struct snd_pcm_substream *substream)
  567. {
  568. struct snd_ca0106 *chip = snd_pcm_substream_chip(substream);
  569. struct snd_pcm_runtime *runtime = substream->runtime;
  570. struct snd_ca0106_pcm *epcm = runtime->private_data;
  571. chip->playback_channels[epcm->channel_id].use = 0;
  572. restore_spdif_bits(chip, epcm->channel_id);
  573. /* Front channel dac should stay on */
  574. if (epcm->channel_id != PCM_FRONT_CHANNEL) {
  575. int err;
  576. err = snd_ca0106_pcm_power_dac(chip, epcm->channel_id, 0);
  577. if (err < 0)
  578. return err;
  579. }
  580. /* FIXME: maybe zero others */
  581. return 0;
  582. }
  583. static int snd_ca0106_pcm_open_playback_front(struct snd_pcm_substream *substream)
  584. {
  585. return snd_ca0106_pcm_open_playback_channel(substream, PCM_FRONT_CHANNEL);
  586. }
  587. static int snd_ca0106_pcm_open_playback_center_lfe(struct snd_pcm_substream *substream)
  588. {
  589. return snd_ca0106_pcm_open_playback_channel(substream, PCM_CENTER_LFE_CHANNEL);
  590. }
  591. static int snd_ca0106_pcm_open_playback_unknown(struct snd_pcm_substream *substream)
  592. {
  593. return snd_ca0106_pcm_open_playback_channel(substream, PCM_UNKNOWN_CHANNEL);
  594. }
  595. static int snd_ca0106_pcm_open_playback_rear(struct snd_pcm_substream *substream)
  596. {
  597. return snd_ca0106_pcm_open_playback_channel(substream, PCM_REAR_CHANNEL);
  598. }
  599. /* open_capture callback */
  600. static int snd_ca0106_pcm_open_capture_channel(struct snd_pcm_substream *substream,
  601. int channel_id)
  602. {
  603. struct snd_ca0106 *chip = snd_pcm_substream_chip(substream);
  604. struct snd_ca0106_channel *channel = &(chip->capture_channels[channel_id]);
  605. struct snd_ca0106_pcm *epcm;
  606. struct snd_pcm_runtime *runtime = substream->runtime;
  607. int err;
  608. epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
  609. if (!epcm)
  610. return -ENOMEM;
  611. epcm->emu = chip;
  612. epcm->substream = substream;
  613. epcm->channel_id=channel_id;
  614. runtime->private_data = epcm;
  615. runtime->private_free = snd_ca0106_pcm_free_substream;
  616. runtime->hw = snd_ca0106_capture_hw;
  617. channel->emu = chip;
  618. channel->number = channel_id;
  619. channel->use = 1;
  620. /*
  621. dev_dbg(chip->card->dev, "open:channel_id=%d, chip=%p, channel=%p\n",
  622. channel_id, chip, channel);
  623. */
  624. //channel->interrupt = snd_ca0106_pcm_channel_interrupt;
  625. channel->epcm = epcm;
  626. if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
  627. return err;
  628. //snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, &hw_constraints_capture_period_sizes);
  629. if ((err = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64)) < 0)
  630. return err;
  631. return 0;
  632. }
  633. /* close callback */
  634. static int snd_ca0106_pcm_close_capture(struct snd_pcm_substream *substream)
  635. {
  636. struct snd_ca0106 *chip = snd_pcm_substream_chip(substream);
  637. struct snd_pcm_runtime *runtime = substream->runtime;
  638. struct snd_ca0106_pcm *epcm = runtime->private_data;
  639. chip->capture_channels[epcm->channel_id].use = 0;
  640. /* FIXME: maybe zero others */
  641. return 0;
  642. }
  643. static int snd_ca0106_pcm_open_0_capture(struct snd_pcm_substream *substream)
  644. {
  645. return snd_ca0106_pcm_open_capture_channel(substream, 0);
  646. }
  647. static int snd_ca0106_pcm_open_1_capture(struct snd_pcm_substream *substream)
  648. {
  649. return snd_ca0106_pcm_open_capture_channel(substream, 1);
  650. }
  651. static int snd_ca0106_pcm_open_2_capture(struct snd_pcm_substream *substream)
  652. {
  653. return snd_ca0106_pcm_open_capture_channel(substream, 2);
  654. }
  655. static int snd_ca0106_pcm_open_3_capture(struct snd_pcm_substream *substream)
  656. {
  657. return snd_ca0106_pcm_open_capture_channel(substream, 3);
  658. }
  659. /* hw_params callback */
  660. static int snd_ca0106_pcm_hw_params_playback(struct snd_pcm_substream *substream,
  661. struct snd_pcm_hw_params *hw_params)
  662. {
  663. return snd_pcm_lib_malloc_pages(substream,
  664. params_buffer_bytes(hw_params));
  665. }
  666. /* hw_free callback */
  667. static int snd_ca0106_pcm_hw_free_playback(struct snd_pcm_substream *substream)
  668. {
  669. return snd_pcm_lib_free_pages(substream);
  670. }
  671. /* hw_params callback */
  672. static int snd_ca0106_pcm_hw_params_capture(struct snd_pcm_substream *substream,
  673. struct snd_pcm_hw_params *hw_params)
  674. {
  675. return snd_pcm_lib_malloc_pages(substream,
  676. params_buffer_bytes(hw_params));
  677. }
  678. /* hw_free callback */
  679. static int snd_ca0106_pcm_hw_free_capture(struct snd_pcm_substream *substream)
  680. {
  681. return snd_pcm_lib_free_pages(substream);
  682. }
  683. /* prepare playback callback */
  684. static int snd_ca0106_pcm_prepare_playback(struct snd_pcm_substream *substream)
  685. {
  686. struct snd_ca0106 *emu = snd_pcm_substream_chip(substream);
  687. struct snd_pcm_runtime *runtime = substream->runtime;
  688. struct snd_ca0106_pcm *epcm = runtime->private_data;
  689. int channel = epcm->channel_id;
  690. u32 *table_base = (u32 *)(emu->buffer.area+(8*16*channel));
  691. u32 period_size_bytes = frames_to_bytes(runtime, runtime->period_size);
  692. u32 hcfg_mask = HCFG_PLAYBACK_S32_LE;
  693. u32 hcfg_set = 0x00000000;
  694. u32 hcfg;
  695. u32 reg40_mask = 0x30000 << (channel<<1);
  696. u32 reg40_set = 0;
  697. u32 reg40;
  698. /* FIXME: Depending on mixer selection of SPDIF out or not, select the spdif rate or the DAC rate. */
  699. u32 reg71_mask = 0x03030000 ; /* Global. Set SPDIF rate. We only support 44100 to spdif, not to DAC. */
  700. u32 reg71_set = 0;
  701. u32 reg71;
  702. int i;
  703. #if 0 /* debug */
  704. dev_dbg(emu->card->dev,
  705. "prepare:channel_number=%d, rate=%d, format=0x%x, "
  706. "channels=%d, buffer_size=%ld, period_size=%ld, "
  707. "periods=%u, frames_to_bytes=%d\n",
  708. channel, runtime->rate, runtime->format,
  709. runtime->channels, runtime->buffer_size,
  710. runtime->period_size, runtime->periods,
  711. frames_to_bytes(runtime, 1));
  712. dev_dbg(emu->card->dev,
  713. "dma_addr=%x, dma_area=%p, table_base=%p\n",
  714. runtime->dma_addr, runtime->dma_area, table_base);
  715. dev_dbg(emu->card->dev,
  716. "dma_addr=%x, dma_area=%p, dma_bytes(size)=%x\n",
  717. emu->buffer.addr, emu->buffer.area, emu->buffer.bytes);
  718. #endif /* debug */
  719. /* Rate can be set per channel. */
  720. /* reg40 control host to fifo */
  721. /* reg71 controls DAC rate. */
  722. switch (runtime->rate) {
  723. case 44100:
  724. reg40_set = 0x10000 << (channel<<1);
  725. reg71_set = 0x01010000;
  726. break;
  727. case 48000:
  728. reg40_set = 0;
  729. reg71_set = 0;
  730. break;
  731. case 96000:
  732. reg40_set = 0x20000 << (channel<<1);
  733. reg71_set = 0x02020000;
  734. break;
  735. case 192000:
  736. reg40_set = 0x30000 << (channel<<1);
  737. reg71_set = 0x03030000;
  738. break;
  739. default:
  740. reg40_set = 0;
  741. reg71_set = 0;
  742. break;
  743. }
  744. /* Format is a global setting */
  745. /* FIXME: Only let the first channel accessed set this. */
  746. switch (runtime->format) {
  747. case SNDRV_PCM_FORMAT_S16_LE:
  748. hcfg_set = 0;
  749. break;
  750. case SNDRV_PCM_FORMAT_S32_LE:
  751. hcfg_set = HCFG_PLAYBACK_S32_LE;
  752. break;
  753. default:
  754. hcfg_set = 0;
  755. break;
  756. }
  757. hcfg = inl(emu->port + HCFG) ;
  758. hcfg = (hcfg & ~hcfg_mask) | hcfg_set;
  759. outl(hcfg, emu->port + HCFG);
  760. reg40 = snd_ca0106_ptr_read(emu, 0x40, 0);
  761. reg40 = (reg40 & ~reg40_mask) | reg40_set;
  762. snd_ca0106_ptr_write(emu, 0x40, 0, reg40);
  763. reg71 = snd_ca0106_ptr_read(emu, 0x71, 0);
  764. reg71 = (reg71 & ~reg71_mask) | reg71_set;
  765. snd_ca0106_ptr_write(emu, 0x71, 0, reg71);
  766. /* FIXME: Check emu->buffer.size before actually writing to it. */
  767. for(i=0; i < runtime->periods; i++) {
  768. table_base[i*2] = runtime->dma_addr + (i * period_size_bytes);
  769. table_base[i*2+1] = period_size_bytes << 16;
  770. }
  771. snd_ca0106_ptr_write(emu, PLAYBACK_LIST_ADDR, channel, emu->buffer.addr+(8*16*channel));
  772. snd_ca0106_ptr_write(emu, PLAYBACK_LIST_SIZE, channel, (runtime->periods - 1) << 19);
  773. snd_ca0106_ptr_write(emu, PLAYBACK_LIST_PTR, channel, 0);
  774. snd_ca0106_ptr_write(emu, PLAYBACK_DMA_ADDR, channel, runtime->dma_addr);
  775. snd_ca0106_ptr_write(emu, PLAYBACK_PERIOD_SIZE, channel, frames_to_bytes(runtime, runtime->period_size)<<16); // buffer size in bytes
  776. /* FIXME test what 0 bytes does. */
  777. snd_ca0106_ptr_write(emu, PLAYBACK_PERIOD_SIZE, channel, 0); // buffer size in bytes
  778. snd_ca0106_ptr_write(emu, PLAYBACK_POINTER, channel, 0);
  779. snd_ca0106_ptr_write(emu, 0x07, channel, 0x0);
  780. snd_ca0106_ptr_write(emu, 0x08, channel, 0);
  781. snd_ca0106_ptr_write(emu, PLAYBACK_MUTE, 0x0, 0x0); /* Unmute output */
  782. #if 0
  783. snd_ca0106_ptr_write(emu, SPCS0, 0,
  784. SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
  785. SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
  786. SPCS_GENERATIONSTATUS | 0x00001200 |
  787. 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT );
  788. #endif
  789. return 0;
  790. }
  791. /* prepare capture callback */
  792. static int snd_ca0106_pcm_prepare_capture(struct snd_pcm_substream *substream)
  793. {
  794. struct snd_ca0106 *emu = snd_pcm_substream_chip(substream);
  795. struct snd_pcm_runtime *runtime = substream->runtime;
  796. struct snd_ca0106_pcm *epcm = runtime->private_data;
  797. int channel = epcm->channel_id;
  798. u32 hcfg_mask = HCFG_CAPTURE_S32_LE;
  799. u32 hcfg_set = 0x00000000;
  800. u32 hcfg;
  801. u32 over_sampling=0x2;
  802. u32 reg71_mask = 0x0000c000 ; /* Global. Set ADC rate. */
  803. u32 reg71_set = 0;
  804. u32 reg71;
  805. #if 0 /* debug */
  806. dev_dbg(emu->card->dev,
  807. "prepare:channel_number=%d, rate=%d, format=0x%x, "
  808. "channels=%d, buffer_size=%ld, period_size=%ld, "
  809. "periods=%u, frames_to_bytes=%d\n",
  810. channel, runtime->rate, runtime->format,
  811. runtime->channels, runtime->buffer_size,
  812. runtime->period_size, runtime->periods,
  813. frames_to_bytes(runtime, 1));
  814. dev_dbg(emu->card->dev,
  815. "dma_addr=%x, dma_area=%p, table_base=%p\n",
  816. runtime->dma_addr, runtime->dma_area, table_base);
  817. dev_dbg(emu->card->dev,
  818. "dma_addr=%x, dma_area=%p, dma_bytes(size)=%x\n",
  819. emu->buffer.addr, emu->buffer.area, emu->buffer.bytes);
  820. #endif /* debug */
  821. /* reg71 controls ADC rate. */
  822. switch (runtime->rate) {
  823. case 44100:
  824. reg71_set = 0x00004000;
  825. break;
  826. case 48000:
  827. reg71_set = 0;
  828. break;
  829. case 96000:
  830. reg71_set = 0x00008000;
  831. over_sampling=0xa;
  832. break;
  833. case 192000:
  834. reg71_set = 0x0000c000;
  835. over_sampling=0xa;
  836. break;
  837. default:
  838. reg71_set = 0;
  839. break;
  840. }
  841. /* Format is a global setting */
  842. /* FIXME: Only let the first channel accessed set this. */
  843. switch (runtime->format) {
  844. case SNDRV_PCM_FORMAT_S16_LE:
  845. hcfg_set = 0;
  846. break;
  847. case SNDRV_PCM_FORMAT_S32_LE:
  848. hcfg_set = HCFG_CAPTURE_S32_LE;
  849. break;
  850. default:
  851. hcfg_set = 0;
  852. break;
  853. }
  854. hcfg = inl(emu->port + HCFG) ;
  855. hcfg = (hcfg & ~hcfg_mask) | hcfg_set;
  856. outl(hcfg, emu->port + HCFG);
  857. reg71 = snd_ca0106_ptr_read(emu, 0x71, 0);
  858. reg71 = (reg71 & ~reg71_mask) | reg71_set;
  859. snd_ca0106_ptr_write(emu, 0x71, 0, reg71);
  860. if (emu->details->i2c_adc == 1) { /* The SB0410 and SB0413 use I2C to control ADC. */
  861. snd_ca0106_i2c_write(emu, ADC_MASTER, over_sampling); /* Adjust the over sampler to better suit the capture rate. */
  862. }
  863. /*
  864. dev_dbg(emu->card->dev,
  865. "prepare:channel_number=%d, rate=%d, format=0x%x, channels=%d, "
  866. "buffer_size=%ld, period_size=%ld, frames_to_bytes=%d\n",
  867. channel, runtime->rate, runtime->format, runtime->channels,
  868. runtime->buffer_size, runtime->period_size,
  869. frames_to_bytes(runtime, 1));
  870. */
  871. snd_ca0106_ptr_write(emu, 0x13, channel, 0);
  872. snd_ca0106_ptr_write(emu, CAPTURE_DMA_ADDR, channel, runtime->dma_addr);
  873. snd_ca0106_ptr_write(emu, CAPTURE_BUFFER_SIZE, channel, frames_to_bytes(runtime, runtime->buffer_size)<<16); // buffer size in bytes
  874. snd_ca0106_ptr_write(emu, CAPTURE_POINTER, channel, 0);
  875. return 0;
  876. }
  877. /* trigger_playback callback */
  878. static int snd_ca0106_pcm_trigger_playback(struct snd_pcm_substream *substream,
  879. int cmd)
  880. {
  881. struct snd_ca0106 *emu = snd_pcm_substream_chip(substream);
  882. struct snd_pcm_runtime *runtime;
  883. struct snd_ca0106_pcm *epcm;
  884. int channel;
  885. int result = 0;
  886. struct snd_pcm_substream *s;
  887. u32 basic = 0;
  888. u32 extended = 0;
  889. u32 bits;
  890. int running = 0;
  891. switch (cmd) {
  892. case SNDRV_PCM_TRIGGER_START:
  893. case SNDRV_PCM_TRIGGER_RESUME:
  894. running = 1;
  895. break;
  896. case SNDRV_PCM_TRIGGER_STOP:
  897. case SNDRV_PCM_TRIGGER_SUSPEND:
  898. default:
  899. running = 0;
  900. break;
  901. }
  902. snd_pcm_group_for_each_entry(s, substream) {
  903. if (snd_pcm_substream_chip(s) != emu ||
  904. s->stream != SNDRV_PCM_STREAM_PLAYBACK)
  905. continue;
  906. runtime = s->runtime;
  907. epcm = runtime->private_data;
  908. channel = epcm->channel_id;
  909. /* dev_dbg(emu->card->dev, "channel=%d\n", channel); */
  910. epcm->running = running;
  911. basic |= (0x1 << channel);
  912. extended |= (0x10 << channel);
  913. snd_pcm_trigger_done(s, substream);
  914. }
  915. /* dev_dbg(emu->card->dev, "basic=0x%x, extended=0x%x\n",basic, extended); */
  916. switch (cmd) {
  917. case SNDRV_PCM_TRIGGER_START:
  918. case SNDRV_PCM_TRIGGER_RESUME:
  919. bits = snd_ca0106_ptr_read(emu, EXTENDED_INT_MASK, 0);
  920. bits |= extended;
  921. snd_ca0106_ptr_write(emu, EXTENDED_INT_MASK, 0, bits);
  922. bits = snd_ca0106_ptr_read(emu, BASIC_INTERRUPT, 0);
  923. bits |= basic;
  924. snd_ca0106_ptr_write(emu, BASIC_INTERRUPT, 0, bits);
  925. break;
  926. case SNDRV_PCM_TRIGGER_STOP:
  927. case SNDRV_PCM_TRIGGER_SUSPEND:
  928. bits = snd_ca0106_ptr_read(emu, BASIC_INTERRUPT, 0);
  929. bits &= ~basic;
  930. snd_ca0106_ptr_write(emu, BASIC_INTERRUPT, 0, bits);
  931. bits = snd_ca0106_ptr_read(emu, EXTENDED_INT_MASK, 0);
  932. bits &= ~extended;
  933. snd_ca0106_ptr_write(emu, EXTENDED_INT_MASK, 0, bits);
  934. break;
  935. default:
  936. result = -EINVAL;
  937. break;
  938. }
  939. return result;
  940. }
  941. /* trigger_capture callback */
  942. static int snd_ca0106_pcm_trigger_capture(struct snd_pcm_substream *substream,
  943. int cmd)
  944. {
  945. struct snd_ca0106 *emu = snd_pcm_substream_chip(substream);
  946. struct snd_pcm_runtime *runtime = substream->runtime;
  947. struct snd_ca0106_pcm *epcm = runtime->private_data;
  948. int channel = epcm->channel_id;
  949. int result = 0;
  950. switch (cmd) {
  951. case SNDRV_PCM_TRIGGER_START:
  952. snd_ca0106_ptr_write(emu, EXTENDED_INT_MASK, 0, snd_ca0106_ptr_read(emu, EXTENDED_INT_MASK, 0) | (0x110000<<channel));
  953. snd_ca0106_ptr_write(emu, BASIC_INTERRUPT, 0, snd_ca0106_ptr_read(emu, BASIC_INTERRUPT, 0)|(0x100<<channel));
  954. epcm->running = 1;
  955. break;
  956. case SNDRV_PCM_TRIGGER_STOP:
  957. snd_ca0106_ptr_write(emu, BASIC_INTERRUPT, 0, snd_ca0106_ptr_read(emu, BASIC_INTERRUPT, 0) & ~(0x100<<channel));
  958. snd_ca0106_ptr_write(emu, EXTENDED_INT_MASK, 0, snd_ca0106_ptr_read(emu, EXTENDED_INT_MASK, 0) & ~(0x110000<<channel));
  959. epcm->running = 0;
  960. break;
  961. default:
  962. result = -EINVAL;
  963. break;
  964. }
  965. return result;
  966. }
  967. /* pointer_playback callback */
  968. static snd_pcm_uframes_t
  969. snd_ca0106_pcm_pointer_playback(struct snd_pcm_substream *substream)
  970. {
  971. struct snd_ca0106 *emu = snd_pcm_substream_chip(substream);
  972. struct snd_pcm_runtime *runtime = substream->runtime;
  973. struct snd_ca0106_pcm *epcm = runtime->private_data;
  974. unsigned int ptr, prev_ptr;
  975. int channel = epcm->channel_id;
  976. int timeout = 10;
  977. if (!epcm->running)
  978. return 0;
  979. prev_ptr = -1;
  980. do {
  981. ptr = snd_ca0106_ptr_read(emu, PLAYBACK_LIST_PTR, channel);
  982. ptr = (ptr >> 3) * runtime->period_size;
  983. ptr += bytes_to_frames(runtime,
  984. snd_ca0106_ptr_read(emu, PLAYBACK_POINTER, channel));
  985. if (ptr >= runtime->buffer_size)
  986. ptr -= runtime->buffer_size;
  987. if (prev_ptr == ptr)
  988. return ptr;
  989. prev_ptr = ptr;
  990. } while (--timeout);
  991. dev_warn(emu->card->dev, "ca0106: unstable DMA pointer!\n");
  992. return 0;
  993. }
  994. /* pointer_capture callback */
  995. static snd_pcm_uframes_t
  996. snd_ca0106_pcm_pointer_capture(struct snd_pcm_substream *substream)
  997. {
  998. struct snd_ca0106 *emu = snd_pcm_substream_chip(substream);
  999. struct snd_pcm_runtime *runtime = substream->runtime;
  1000. struct snd_ca0106_pcm *epcm = runtime->private_data;
  1001. snd_pcm_uframes_t ptr, ptr1, ptr2 = 0;
  1002. int channel = epcm->channel_id;
  1003. if (!epcm->running)
  1004. return 0;
  1005. ptr1 = snd_ca0106_ptr_read(emu, CAPTURE_POINTER, channel);
  1006. ptr2 = bytes_to_frames(runtime, ptr1);
  1007. ptr=ptr2;
  1008. if (ptr >= runtime->buffer_size)
  1009. ptr -= runtime->buffer_size;
  1010. /*
  1011. dev_dbg(emu->card->dev, "ptr1 = 0x%lx, ptr2=0x%lx, ptr=0x%lx, "
  1012. "buffer_size = 0x%x, period_size = 0x%x, bits=%d, rate=%d\n",
  1013. ptr1, ptr2, ptr, (int)runtime->buffer_size,
  1014. (int)runtime->period_size, (int)runtime->frame_bits,
  1015. (int)runtime->rate);
  1016. */
  1017. return ptr;
  1018. }
  1019. /* operators */
  1020. static const struct snd_pcm_ops snd_ca0106_playback_front_ops = {
  1021. .open = snd_ca0106_pcm_open_playback_front,
  1022. .close = snd_ca0106_pcm_close_playback,
  1023. .ioctl = snd_pcm_lib_ioctl,
  1024. .hw_params = snd_ca0106_pcm_hw_params_playback,
  1025. .hw_free = snd_ca0106_pcm_hw_free_playback,
  1026. .prepare = snd_ca0106_pcm_prepare_playback,
  1027. .trigger = snd_ca0106_pcm_trigger_playback,
  1028. .pointer = snd_ca0106_pcm_pointer_playback,
  1029. };
  1030. static const struct snd_pcm_ops snd_ca0106_capture_0_ops = {
  1031. .open = snd_ca0106_pcm_open_0_capture,
  1032. .close = snd_ca0106_pcm_close_capture,
  1033. .ioctl = snd_pcm_lib_ioctl,
  1034. .hw_params = snd_ca0106_pcm_hw_params_capture,
  1035. .hw_free = snd_ca0106_pcm_hw_free_capture,
  1036. .prepare = snd_ca0106_pcm_prepare_capture,
  1037. .trigger = snd_ca0106_pcm_trigger_capture,
  1038. .pointer = snd_ca0106_pcm_pointer_capture,
  1039. };
  1040. static const struct snd_pcm_ops snd_ca0106_capture_1_ops = {
  1041. .open = snd_ca0106_pcm_open_1_capture,
  1042. .close = snd_ca0106_pcm_close_capture,
  1043. .ioctl = snd_pcm_lib_ioctl,
  1044. .hw_params = snd_ca0106_pcm_hw_params_capture,
  1045. .hw_free = snd_ca0106_pcm_hw_free_capture,
  1046. .prepare = snd_ca0106_pcm_prepare_capture,
  1047. .trigger = snd_ca0106_pcm_trigger_capture,
  1048. .pointer = snd_ca0106_pcm_pointer_capture,
  1049. };
  1050. static const struct snd_pcm_ops snd_ca0106_capture_2_ops = {
  1051. .open = snd_ca0106_pcm_open_2_capture,
  1052. .close = snd_ca0106_pcm_close_capture,
  1053. .ioctl = snd_pcm_lib_ioctl,
  1054. .hw_params = snd_ca0106_pcm_hw_params_capture,
  1055. .hw_free = snd_ca0106_pcm_hw_free_capture,
  1056. .prepare = snd_ca0106_pcm_prepare_capture,
  1057. .trigger = snd_ca0106_pcm_trigger_capture,
  1058. .pointer = snd_ca0106_pcm_pointer_capture,
  1059. };
  1060. static const struct snd_pcm_ops snd_ca0106_capture_3_ops = {
  1061. .open = snd_ca0106_pcm_open_3_capture,
  1062. .close = snd_ca0106_pcm_close_capture,
  1063. .ioctl = snd_pcm_lib_ioctl,
  1064. .hw_params = snd_ca0106_pcm_hw_params_capture,
  1065. .hw_free = snd_ca0106_pcm_hw_free_capture,
  1066. .prepare = snd_ca0106_pcm_prepare_capture,
  1067. .trigger = snd_ca0106_pcm_trigger_capture,
  1068. .pointer = snd_ca0106_pcm_pointer_capture,
  1069. };
  1070. static const struct snd_pcm_ops snd_ca0106_playback_center_lfe_ops = {
  1071. .open = snd_ca0106_pcm_open_playback_center_lfe,
  1072. .close = snd_ca0106_pcm_close_playback,
  1073. .ioctl = snd_pcm_lib_ioctl,
  1074. .hw_params = snd_ca0106_pcm_hw_params_playback,
  1075. .hw_free = snd_ca0106_pcm_hw_free_playback,
  1076. .prepare = snd_ca0106_pcm_prepare_playback,
  1077. .trigger = snd_ca0106_pcm_trigger_playback,
  1078. .pointer = snd_ca0106_pcm_pointer_playback,
  1079. };
  1080. static const struct snd_pcm_ops snd_ca0106_playback_unknown_ops = {
  1081. .open = snd_ca0106_pcm_open_playback_unknown,
  1082. .close = snd_ca0106_pcm_close_playback,
  1083. .ioctl = snd_pcm_lib_ioctl,
  1084. .hw_params = snd_ca0106_pcm_hw_params_playback,
  1085. .hw_free = snd_ca0106_pcm_hw_free_playback,
  1086. .prepare = snd_ca0106_pcm_prepare_playback,
  1087. .trigger = snd_ca0106_pcm_trigger_playback,
  1088. .pointer = snd_ca0106_pcm_pointer_playback,
  1089. };
  1090. static const struct snd_pcm_ops snd_ca0106_playback_rear_ops = {
  1091. .open = snd_ca0106_pcm_open_playback_rear,
  1092. .close = snd_ca0106_pcm_close_playback,
  1093. .ioctl = snd_pcm_lib_ioctl,
  1094. .hw_params = snd_ca0106_pcm_hw_params_playback,
  1095. .hw_free = snd_ca0106_pcm_hw_free_playback,
  1096. .prepare = snd_ca0106_pcm_prepare_playback,
  1097. .trigger = snd_ca0106_pcm_trigger_playback,
  1098. .pointer = snd_ca0106_pcm_pointer_playback,
  1099. };
  1100. static unsigned short snd_ca0106_ac97_read(struct snd_ac97 *ac97,
  1101. unsigned short reg)
  1102. {
  1103. struct snd_ca0106 *emu = ac97->private_data;
  1104. unsigned long flags;
  1105. unsigned short val;
  1106. spin_lock_irqsave(&emu->emu_lock, flags);
  1107. outb(reg, emu->port + AC97ADDRESS);
  1108. val = inw(emu->port + AC97DATA);
  1109. spin_unlock_irqrestore(&emu->emu_lock, flags);
  1110. return val;
  1111. }
  1112. static void snd_ca0106_ac97_write(struct snd_ac97 *ac97,
  1113. unsigned short reg, unsigned short val)
  1114. {
  1115. struct snd_ca0106 *emu = ac97->private_data;
  1116. unsigned long flags;
  1117. spin_lock_irqsave(&emu->emu_lock, flags);
  1118. outb(reg, emu->port + AC97ADDRESS);
  1119. outw(val, emu->port + AC97DATA);
  1120. spin_unlock_irqrestore(&emu->emu_lock, flags);
  1121. }
  1122. static int snd_ca0106_ac97(struct snd_ca0106 *chip)
  1123. {
  1124. struct snd_ac97_bus *pbus;
  1125. struct snd_ac97_template ac97;
  1126. int err;
  1127. static struct snd_ac97_bus_ops ops = {
  1128. .write = snd_ca0106_ac97_write,
  1129. .read = snd_ca0106_ac97_read,
  1130. };
  1131. if ((err = snd_ac97_bus(chip->card, 0, &ops, NULL, &pbus)) < 0)
  1132. return err;
  1133. pbus->no_vra = 1; /* we don't need VRA */
  1134. memset(&ac97, 0, sizeof(ac97));
  1135. ac97.private_data = chip;
  1136. ac97.scaps = AC97_SCAP_NO_SPDIF;
  1137. return snd_ac97_mixer(pbus, &ac97, &chip->ac97);
  1138. }
  1139. static void ca0106_stop_chip(struct snd_ca0106 *chip);
  1140. static int snd_ca0106_free(struct snd_ca0106 *chip)
  1141. {
  1142. if (chip->res_port != NULL) {
  1143. /* avoid access to already used hardware */
  1144. ca0106_stop_chip(chip);
  1145. }
  1146. if (chip->irq >= 0)
  1147. free_irq(chip->irq, chip);
  1148. // release the data
  1149. #if 1
  1150. if (chip->buffer.area)
  1151. snd_dma_free_pages(&chip->buffer);
  1152. #endif
  1153. // release the i/o port
  1154. release_and_free_resource(chip->res_port);
  1155. pci_disable_device(chip->pci);
  1156. kfree(chip);
  1157. return 0;
  1158. }
  1159. static int snd_ca0106_dev_free(struct snd_device *device)
  1160. {
  1161. struct snd_ca0106 *chip = device->device_data;
  1162. return snd_ca0106_free(chip);
  1163. }
  1164. static irqreturn_t snd_ca0106_interrupt(int irq, void *dev_id)
  1165. {
  1166. unsigned int status;
  1167. struct snd_ca0106 *chip = dev_id;
  1168. int i;
  1169. int mask;
  1170. unsigned int stat76;
  1171. struct snd_ca0106_channel *pchannel;
  1172. status = inl(chip->port + IPR);
  1173. if (! status)
  1174. return IRQ_NONE;
  1175. stat76 = snd_ca0106_ptr_read(chip, EXTENDED_INT, 0);
  1176. /*
  1177. dev_dbg(emu->card->dev, "interrupt status = 0x%08x, stat76=0x%08x\n",
  1178. status, stat76);
  1179. dev_dbg(emu->card->dev, "ptr=0x%08x\n",
  1180. snd_ca0106_ptr_read(chip, PLAYBACK_POINTER, 0));
  1181. */
  1182. mask = 0x11; /* 0x1 for one half, 0x10 for the other half period. */
  1183. for(i = 0; i < 4; i++) {
  1184. pchannel = &(chip->playback_channels[i]);
  1185. if (stat76 & mask) {
  1186. /* FIXME: Select the correct substream for period elapsed */
  1187. if(pchannel->use) {
  1188. snd_pcm_period_elapsed(pchannel->epcm->substream);
  1189. /* dev_dbg(emu->card->dev, "interrupt [%d] used\n", i); */
  1190. }
  1191. }
  1192. /*
  1193. dev_dbg(emu->card->dev, "channel=%p\n", pchannel);
  1194. dev_dbg(emu->card->dev, "interrupt stat76[%d] = %08x, use=%d, channel=%d\n", i, stat76, pchannel->use, pchannel->number);
  1195. */
  1196. mask <<= 1;
  1197. }
  1198. mask = 0x110000; /* 0x1 for one half, 0x10 for the other half period. */
  1199. for(i = 0; i < 4; i++) {
  1200. pchannel = &(chip->capture_channels[i]);
  1201. if (stat76 & mask) {
  1202. /* FIXME: Select the correct substream for period elapsed */
  1203. if(pchannel->use) {
  1204. snd_pcm_period_elapsed(pchannel->epcm->substream);
  1205. /* dev_dbg(emu->card->dev, "interrupt [%d] used\n", i); */
  1206. }
  1207. }
  1208. /*
  1209. dev_dbg(emu->card->dev, "channel=%p\n", pchannel);
  1210. dev_dbg(emu->card->dev, "interrupt stat76[%d] = %08x, use=%d, channel=%d\n", i, stat76, pchannel->use, pchannel->number);
  1211. */
  1212. mask <<= 1;
  1213. }
  1214. snd_ca0106_ptr_write(chip, EXTENDED_INT, 0, stat76);
  1215. if (chip->midi.dev_id &&
  1216. (status & (chip->midi.ipr_tx|chip->midi.ipr_rx))) {
  1217. if (chip->midi.interrupt)
  1218. chip->midi.interrupt(&chip->midi, status);
  1219. else
  1220. chip->midi.interrupt_disable(&chip->midi, chip->midi.tx_enable | chip->midi.rx_enable);
  1221. }
  1222. // acknowledge the interrupt if necessary
  1223. outl(status, chip->port+IPR);
  1224. return IRQ_HANDLED;
  1225. }
  1226. static const struct snd_pcm_chmap_elem surround_map[] = {
  1227. { .channels = 2,
  1228. .map = { SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
  1229. { }
  1230. };
  1231. static const struct snd_pcm_chmap_elem clfe_map[] = {
  1232. { .channels = 2,
  1233. .map = { SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE } },
  1234. { }
  1235. };
  1236. static const struct snd_pcm_chmap_elem side_map[] = {
  1237. { .channels = 2,
  1238. .map = { SNDRV_CHMAP_SL, SNDRV_CHMAP_SR } },
  1239. { }
  1240. };
  1241. static int snd_ca0106_pcm(struct snd_ca0106 *emu, int device)
  1242. {
  1243. struct snd_pcm *pcm;
  1244. struct snd_pcm_substream *substream;
  1245. const struct snd_pcm_chmap_elem *map = NULL;
  1246. int err;
  1247. err = snd_pcm_new(emu->card, "ca0106", device, 1, 1, &pcm);
  1248. if (err < 0)
  1249. return err;
  1250. pcm->private_data = emu;
  1251. switch (device) {
  1252. case 0:
  1253. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ca0106_playback_front_ops);
  1254. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ca0106_capture_0_ops);
  1255. map = snd_pcm_std_chmaps;
  1256. break;
  1257. case 1:
  1258. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ca0106_playback_rear_ops);
  1259. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ca0106_capture_1_ops);
  1260. map = surround_map;
  1261. break;
  1262. case 2:
  1263. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ca0106_playback_center_lfe_ops);
  1264. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ca0106_capture_2_ops);
  1265. map = clfe_map;
  1266. break;
  1267. case 3:
  1268. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ca0106_playback_unknown_ops);
  1269. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ca0106_capture_3_ops);
  1270. map = side_map;
  1271. break;
  1272. }
  1273. pcm->info_flags = 0;
  1274. strcpy(pcm->name, "CA0106");
  1275. for(substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
  1276. substream;
  1277. substream = substream->next) {
  1278. if ((err = snd_pcm_lib_preallocate_pages(substream,
  1279. SNDRV_DMA_TYPE_DEV,
  1280. snd_dma_pci_data(emu->pci),
  1281. 64*1024, 64*1024)) < 0) /* FIXME: 32*1024 for sound buffer, between 32and64 for Periods table. */
  1282. return err;
  1283. }
  1284. for (substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
  1285. substream;
  1286. substream = substream->next) {
  1287. if ((err = snd_pcm_lib_preallocate_pages(substream,
  1288. SNDRV_DMA_TYPE_DEV,
  1289. snd_dma_pci_data(emu->pci),
  1290. 64*1024, 64*1024)) < 0)
  1291. return err;
  1292. }
  1293. err = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK, map, 2,
  1294. 1 << 2, NULL);
  1295. if (err < 0)
  1296. return err;
  1297. emu->pcm[device] = pcm;
  1298. return 0;
  1299. }
  1300. #define SPI_REG(reg, value) (((reg) << SPI_REG_SHIFT) | (value))
  1301. static unsigned int spi_dac_init[] = {
  1302. SPI_REG(SPI_LDA1_REG, SPI_DA_BIT_0dB), /* 0dB dig. attenuation */
  1303. SPI_REG(SPI_RDA1_REG, SPI_DA_BIT_0dB),
  1304. SPI_REG(SPI_PL_REG, SPI_PL_BIT_L_L | SPI_PL_BIT_R_R | SPI_IZD_BIT),
  1305. SPI_REG(SPI_FMT_REG, SPI_FMT_BIT_I2S | SPI_IWL_BIT_24),
  1306. SPI_REG(SPI_LDA2_REG, SPI_DA_BIT_0dB),
  1307. SPI_REG(SPI_RDA2_REG, SPI_DA_BIT_0dB),
  1308. SPI_REG(SPI_LDA3_REG, SPI_DA_BIT_0dB),
  1309. SPI_REG(SPI_RDA3_REG, SPI_DA_BIT_0dB),
  1310. SPI_REG(SPI_MASTDA_REG, SPI_DA_BIT_0dB),
  1311. SPI_REG(9, 0x00),
  1312. SPI_REG(SPI_MS_REG, SPI_DACD0_BIT | SPI_DACD1_BIT | SPI_DACD2_BIT),
  1313. SPI_REG(12, 0x00),
  1314. SPI_REG(SPI_LDA4_REG, SPI_DA_BIT_0dB),
  1315. SPI_REG(SPI_RDA4_REG, SPI_DA_BIT_0dB | SPI_DA_BIT_UPDATE),
  1316. SPI_REG(SPI_DACD4_REG, SPI_DACD4_BIT),
  1317. };
  1318. static unsigned int i2c_adc_init[][2] = {
  1319. { 0x17, 0x00 }, /* Reset */
  1320. { 0x07, 0x00 }, /* Timeout */
  1321. { 0x0b, 0x22 }, /* Interface control */
  1322. { 0x0c, 0x22 }, /* Master mode control */
  1323. { 0x0d, 0x08 }, /* Powerdown control */
  1324. { 0x0e, 0xcf }, /* Attenuation Left 0x01 = -103dB, 0xff = 24dB */
  1325. { 0x0f, 0xcf }, /* Attenuation Right 0.5dB steps */
  1326. { 0x10, 0x7b }, /* ALC Control 1 */
  1327. { 0x11, 0x00 }, /* ALC Control 2 */
  1328. { 0x12, 0x32 }, /* ALC Control 3 */
  1329. { 0x13, 0x00 }, /* Noise gate control */
  1330. { 0x14, 0xa6 }, /* Limiter control */
  1331. { 0x15, ADC_MUX_LINEIN }, /* ADC Mixer control */
  1332. };
  1333. static void ca0106_init_chip(struct snd_ca0106 *chip, int resume)
  1334. {
  1335. int ch;
  1336. unsigned int def_bits;
  1337. outl(0, chip->port + INTE);
  1338. /*
  1339. * Init to 0x02109204 :
  1340. * Clock accuracy = 0 (1000ppm)
  1341. * Sample Rate = 2 (48kHz)
  1342. * Audio Channel = 1 (Left of 2)
  1343. * Source Number = 0 (Unspecified)
  1344. * Generation Status = 1 (Original for Cat Code 12)
  1345. * Cat Code = 12 (Digital Signal Mixer)
  1346. * Mode = 0 (Mode 0)
  1347. * Emphasis = 0 (None)
  1348. * CP = 1 (Copyright unasserted)
  1349. * AN = 0 (Audio data)
  1350. * P = 0 (Consumer)
  1351. */
  1352. def_bits =
  1353. SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
  1354. SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
  1355. SPCS_GENERATIONSTATUS | 0x00001200 |
  1356. 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT;
  1357. if (!resume) {
  1358. chip->spdif_str_bits[0] = chip->spdif_bits[0] = def_bits;
  1359. chip->spdif_str_bits[1] = chip->spdif_bits[1] = def_bits;
  1360. chip->spdif_str_bits[2] = chip->spdif_bits[2] = def_bits;
  1361. chip->spdif_str_bits[3] = chip->spdif_bits[3] = def_bits;
  1362. }
  1363. /* Only SPCS1 has been tested */
  1364. snd_ca0106_ptr_write(chip, SPCS1, 0, chip->spdif_str_bits[1]);
  1365. snd_ca0106_ptr_write(chip, SPCS0, 0, chip->spdif_str_bits[0]);
  1366. snd_ca0106_ptr_write(chip, SPCS2, 0, chip->spdif_str_bits[2]);
  1367. snd_ca0106_ptr_write(chip, SPCS3, 0, chip->spdif_str_bits[3]);
  1368. snd_ca0106_ptr_write(chip, PLAYBACK_MUTE, 0, 0x00fc0000);
  1369. snd_ca0106_ptr_write(chip, CAPTURE_MUTE, 0, 0x00fc0000);
  1370. /* Write 0x8000 to AC97_REC_GAIN to mute it. */
  1371. outb(AC97_REC_GAIN, chip->port + AC97ADDRESS);
  1372. outw(0x8000, chip->port + AC97DATA);
  1373. #if 0 /* FIXME: what are these? */
  1374. snd_ca0106_ptr_write(chip, SPCS0, 0, 0x2108006);
  1375. snd_ca0106_ptr_write(chip, 0x42, 0, 0x2108006);
  1376. snd_ca0106_ptr_write(chip, 0x43, 0, 0x2108006);
  1377. snd_ca0106_ptr_write(chip, 0x44, 0, 0x2108006);
  1378. #endif
  1379. /* OSS drivers set this. */
  1380. /* snd_ca0106_ptr_write(chip, SPDIF_SELECT2, 0, 0xf0f003f); */
  1381. /* Analog or Digital output */
  1382. snd_ca0106_ptr_write(chip, SPDIF_SELECT1, 0, 0xf);
  1383. /* 0x0b000000 for digital, 0x000b0000 for analog, from win2000 drivers.
  1384. * Use 0x000f0000 for surround71
  1385. */
  1386. snd_ca0106_ptr_write(chip, SPDIF_SELECT2, 0, 0x000f0000);
  1387. chip->spdif_enable = 0; /* Set digital SPDIF output off */
  1388. /*snd_ca0106_ptr_write(chip, 0x45, 0, 0);*/ /* Analogue out */
  1389. /*snd_ca0106_ptr_write(chip, 0x45, 0, 0xf00);*/ /* Digital out */
  1390. /* goes to 0x40c80000 when doing SPDIF IN/OUT */
  1391. snd_ca0106_ptr_write(chip, CAPTURE_CONTROL, 0, 0x40c81000);
  1392. /* (Mute) CAPTURE feedback into PLAYBACK volume.
  1393. * Only lower 16 bits matter.
  1394. */
  1395. snd_ca0106_ptr_write(chip, CAPTURE_CONTROL, 1, 0xffffffff);
  1396. /* SPDIF IN Volume */
  1397. snd_ca0106_ptr_write(chip, CAPTURE_CONTROL, 2, 0x30300000);
  1398. /* SPDIF IN Volume, 0x70 = (vol & 0x3f) | 0x40 */
  1399. snd_ca0106_ptr_write(chip, CAPTURE_CONTROL, 3, 0x00700000);
  1400. snd_ca0106_ptr_write(chip, PLAYBACK_ROUTING1, 0, 0x32765410);
  1401. snd_ca0106_ptr_write(chip, PLAYBACK_ROUTING2, 0, 0x76767676);
  1402. snd_ca0106_ptr_write(chip, CAPTURE_ROUTING1, 0, 0x32765410);
  1403. snd_ca0106_ptr_write(chip, CAPTURE_ROUTING2, 0, 0x76767676);
  1404. for (ch = 0; ch < 4; ch++) {
  1405. /* Only high 16 bits matter */
  1406. snd_ca0106_ptr_write(chip, CAPTURE_VOLUME1, ch, 0x30303030);
  1407. snd_ca0106_ptr_write(chip, CAPTURE_VOLUME2, ch, 0x30303030);
  1408. #if 0 /* Mute */
  1409. snd_ca0106_ptr_write(chip, PLAYBACK_VOLUME1, ch, 0x40404040);
  1410. snd_ca0106_ptr_write(chip, PLAYBACK_VOLUME2, ch, 0x40404040);
  1411. snd_ca0106_ptr_write(chip, PLAYBACK_VOLUME1, ch, 0xffffffff);
  1412. snd_ca0106_ptr_write(chip, PLAYBACK_VOLUME2, ch, 0xffffffff);
  1413. #endif
  1414. }
  1415. if (chip->details->i2c_adc == 1) {
  1416. /* Select MIC, Line in, TAD in, AUX in */
  1417. snd_ca0106_ptr_write(chip, CAPTURE_SOURCE, 0x0, 0x333300e4);
  1418. /* Default to CAPTURE_SOURCE to i2s in */
  1419. if (!resume)
  1420. chip->capture_source = 3;
  1421. } else if (chip->details->ac97 == 1) {
  1422. /* Default to AC97 in */
  1423. snd_ca0106_ptr_write(chip, CAPTURE_SOURCE, 0x0, 0x444400e4);
  1424. /* Default to CAPTURE_SOURCE to AC97 in */
  1425. if (!resume)
  1426. chip->capture_source = 4;
  1427. } else {
  1428. /* Select MIC, Line in, TAD in, AUX in */
  1429. snd_ca0106_ptr_write(chip, CAPTURE_SOURCE, 0x0, 0x333300e4);
  1430. /* Default to Set CAPTURE_SOURCE to i2s in */
  1431. if (!resume)
  1432. chip->capture_source = 3;
  1433. }
  1434. if (chip->details->gpio_type == 2) {
  1435. /* The SB0438 use GPIO differently. */
  1436. /* FIXME: Still need to find out what the other GPIO bits do.
  1437. * E.g. For digital spdif out.
  1438. */
  1439. outl(0x0, chip->port+GPIO);
  1440. /* outl(0x00f0e000, chip->port+GPIO); */ /* Analog */
  1441. outl(0x005f5301, chip->port+GPIO); /* Analog */
  1442. } else if (chip->details->gpio_type == 1) {
  1443. /* The SB0410 and SB0413 use GPIO differently. */
  1444. /* FIXME: Still need to find out what the other GPIO bits do.
  1445. * E.g. For digital spdif out.
  1446. */
  1447. outl(0x0, chip->port+GPIO);
  1448. /* outl(0x00f0e000, chip->port+GPIO); */ /* Analog */
  1449. outl(0x005f5301, chip->port+GPIO); /* Analog */
  1450. } else {
  1451. outl(0x0, chip->port+GPIO);
  1452. outl(0x005f03a3, chip->port+GPIO); /* Analog */
  1453. /* outl(0x005f02a2, chip->port+GPIO); */ /* SPDIF */
  1454. }
  1455. snd_ca0106_intr_enable(chip, 0x105); /* Win2000 uses 0x1e0 */
  1456. /* outl(HCFG_LOCKSOUNDCACHE|HCFG_AUDIOENABLE, chip->port+HCFG); */
  1457. /* 0x1000 causes AC3 to fails. Maybe it effects 24 bit output. */
  1458. /* outl(0x00001409, chip->port+HCFG); */
  1459. /* outl(0x00000009, chip->port+HCFG); */
  1460. /* AC97 2.0, Enable outputs. */
  1461. outl(HCFG_AC97 | HCFG_AUDIOENABLE, chip->port+HCFG);
  1462. if (chip->details->i2c_adc == 1) {
  1463. /* The SB0410 and SB0413 use I2C to control ADC. */
  1464. int size, n;
  1465. size = ARRAY_SIZE(i2c_adc_init);
  1466. /* dev_dbg(emu->card->dev, "I2C:array size=0x%x\n", size); */
  1467. for (n = 0; n < size; n++)
  1468. snd_ca0106_i2c_write(chip, i2c_adc_init[n][0],
  1469. i2c_adc_init[n][1]);
  1470. for (n = 0; n < 4; n++) {
  1471. chip->i2c_capture_volume[n][0] = 0xcf;
  1472. chip->i2c_capture_volume[n][1] = 0xcf;
  1473. }
  1474. chip->i2c_capture_source = 2; /* Line in */
  1475. /* Enable Line-in capture. MIC in currently untested. */
  1476. /* snd_ca0106_i2c_write(chip, ADC_MUX, ADC_MUX_LINEIN); */
  1477. }
  1478. if (chip->details->spi_dac) {
  1479. /* The SB0570 use SPI to control DAC. */
  1480. int size, n;
  1481. size = ARRAY_SIZE(spi_dac_init);
  1482. for (n = 0; n < size; n++) {
  1483. int reg = spi_dac_init[n] >> SPI_REG_SHIFT;
  1484. snd_ca0106_spi_write(chip, spi_dac_init[n]);
  1485. if (reg < ARRAY_SIZE(chip->spi_dac_reg))
  1486. chip->spi_dac_reg[reg] = spi_dac_init[n];
  1487. }
  1488. /* Enable front dac only */
  1489. snd_ca0106_pcm_power_dac(chip, PCM_FRONT_CHANNEL, 1);
  1490. }
  1491. }
  1492. static void ca0106_stop_chip(struct snd_ca0106 *chip)
  1493. {
  1494. /* disable interrupts */
  1495. snd_ca0106_ptr_write(chip, BASIC_INTERRUPT, 0, 0);
  1496. outl(0, chip->port + INTE);
  1497. snd_ca0106_ptr_write(chip, EXTENDED_INT_MASK, 0, 0);
  1498. udelay(1000);
  1499. /* disable audio */
  1500. /* outl(HCFG_LOCKSOUNDCACHE, chip->port + HCFG); */
  1501. outl(0, chip->port + HCFG);
  1502. /* FIXME: We need to stop and DMA transfers here.
  1503. * But as I am not sure how yet, we cannot from the dma pages.
  1504. * So we can fix: snd-malloc: Memory leak? pages not freed = 8
  1505. */
  1506. }
  1507. static int snd_ca0106_create(int dev, struct snd_card *card,
  1508. struct pci_dev *pci,
  1509. struct snd_ca0106 **rchip)
  1510. {
  1511. struct snd_ca0106 *chip;
  1512. struct snd_ca0106_details *c;
  1513. int err;
  1514. static struct snd_device_ops ops = {
  1515. .dev_free = snd_ca0106_dev_free,
  1516. };
  1517. *rchip = NULL;
  1518. err = pci_enable_device(pci);
  1519. if (err < 0)
  1520. return err;
  1521. if (dma_set_mask(&pci->dev, DMA_BIT_MASK(32)) < 0 ||
  1522. dma_set_coherent_mask(&pci->dev, DMA_BIT_MASK(32)) < 0) {
  1523. dev_err(card->dev, "error to set 32bit mask DMA\n");
  1524. pci_disable_device(pci);
  1525. return -ENXIO;
  1526. }
  1527. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  1528. if (chip == NULL) {
  1529. pci_disable_device(pci);
  1530. return -ENOMEM;
  1531. }
  1532. chip->card = card;
  1533. chip->pci = pci;
  1534. chip->irq = -1;
  1535. spin_lock_init(&chip->emu_lock);
  1536. chip->port = pci_resource_start(pci, 0);
  1537. chip->res_port = request_region(chip->port, 0x20, "snd_ca0106");
  1538. if (!chip->res_port) {
  1539. snd_ca0106_free(chip);
  1540. dev_err(card->dev, "cannot allocate the port\n");
  1541. return -EBUSY;
  1542. }
  1543. if (request_irq(pci->irq, snd_ca0106_interrupt,
  1544. IRQF_SHARED, KBUILD_MODNAME, chip)) {
  1545. snd_ca0106_free(chip);
  1546. dev_err(card->dev, "cannot grab irq\n");
  1547. return -EBUSY;
  1548. }
  1549. chip->irq = pci->irq;
  1550. /* This stores the periods table. */
  1551. if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
  1552. 1024, &chip->buffer) < 0) {
  1553. snd_ca0106_free(chip);
  1554. return -ENOMEM;
  1555. }
  1556. pci_set_master(pci);
  1557. /* read serial */
  1558. pci_read_config_dword(pci, PCI_SUBSYSTEM_VENDOR_ID, &chip->serial);
  1559. pci_read_config_word(pci, PCI_SUBSYSTEM_ID, &chip->model);
  1560. dev_info(card->dev, "Model %04x Rev %08x Serial %08x\n",
  1561. chip->model, pci->revision, chip->serial);
  1562. strcpy(card->driver, "CA0106");
  1563. strcpy(card->shortname, "CA0106");
  1564. for (c = ca0106_chip_details; c->serial; c++) {
  1565. if (subsystem[dev]) {
  1566. if (c->serial == subsystem[dev])
  1567. break;
  1568. } else if (c->serial == chip->serial)
  1569. break;
  1570. }
  1571. chip->details = c;
  1572. if (subsystem[dev]) {
  1573. dev_info(card->dev, "Sound card name=%s, "
  1574. "subsystem=0x%x. Forced to subsystem=0x%x\n",
  1575. c->name, chip->serial, subsystem[dev]);
  1576. }
  1577. sprintf(card->longname, "%s at 0x%lx irq %i",
  1578. c->name, chip->port, chip->irq);
  1579. ca0106_init_chip(chip, 0);
  1580. err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
  1581. if (err < 0) {
  1582. snd_ca0106_free(chip);
  1583. return err;
  1584. }
  1585. *rchip = chip;
  1586. return 0;
  1587. }
  1588. static void ca0106_midi_interrupt_enable(struct snd_ca_midi *midi, int intr)
  1589. {
  1590. snd_ca0106_intr_enable((struct snd_ca0106 *)(midi->dev_id), intr);
  1591. }
  1592. static void ca0106_midi_interrupt_disable(struct snd_ca_midi *midi, int intr)
  1593. {
  1594. snd_ca0106_intr_disable((struct snd_ca0106 *)(midi->dev_id), intr);
  1595. }
  1596. static unsigned char ca0106_midi_read(struct snd_ca_midi *midi, int idx)
  1597. {
  1598. return (unsigned char)snd_ca0106_ptr_read((struct snd_ca0106 *)(midi->dev_id),
  1599. midi->port + idx, 0);
  1600. }
  1601. static void ca0106_midi_write(struct snd_ca_midi *midi, int data, int idx)
  1602. {
  1603. snd_ca0106_ptr_write((struct snd_ca0106 *)(midi->dev_id), midi->port + idx, 0, data);
  1604. }
  1605. static struct snd_card *ca0106_dev_id_card(void *dev_id)
  1606. {
  1607. return ((struct snd_ca0106 *)dev_id)->card;
  1608. }
  1609. static int ca0106_dev_id_port(void *dev_id)
  1610. {
  1611. return ((struct snd_ca0106 *)dev_id)->port;
  1612. }
  1613. static int snd_ca0106_midi(struct snd_ca0106 *chip, unsigned int channel)
  1614. {
  1615. struct snd_ca_midi *midi;
  1616. char *name;
  1617. int err;
  1618. if (channel == CA0106_MIDI_CHAN_B) {
  1619. name = "CA0106 MPU-401 (UART) B";
  1620. midi = &chip->midi2;
  1621. midi->tx_enable = INTE_MIDI_TX_B;
  1622. midi->rx_enable = INTE_MIDI_RX_B;
  1623. midi->ipr_tx = IPR_MIDI_TX_B;
  1624. midi->ipr_rx = IPR_MIDI_RX_B;
  1625. midi->port = MIDI_UART_B_DATA;
  1626. } else {
  1627. name = "CA0106 MPU-401 (UART)";
  1628. midi = &chip->midi;
  1629. midi->tx_enable = INTE_MIDI_TX_A;
  1630. midi->rx_enable = INTE_MIDI_TX_B;
  1631. midi->ipr_tx = IPR_MIDI_TX_A;
  1632. midi->ipr_rx = IPR_MIDI_RX_A;
  1633. midi->port = MIDI_UART_A_DATA;
  1634. }
  1635. midi->reset = CA0106_MPU401_RESET;
  1636. midi->enter_uart = CA0106_MPU401_ENTER_UART;
  1637. midi->ack = CA0106_MPU401_ACK;
  1638. midi->input_avail = CA0106_MIDI_INPUT_AVAIL;
  1639. midi->output_ready = CA0106_MIDI_OUTPUT_READY;
  1640. midi->channel = channel;
  1641. midi->interrupt_enable = ca0106_midi_interrupt_enable;
  1642. midi->interrupt_disable = ca0106_midi_interrupt_disable;
  1643. midi->read = ca0106_midi_read;
  1644. midi->write = ca0106_midi_write;
  1645. midi->get_dev_id_card = ca0106_dev_id_card;
  1646. midi->get_dev_id_port = ca0106_dev_id_port;
  1647. midi->dev_id = chip;
  1648. if ((err = ca_midi_init(chip, midi, 0, name)) < 0)
  1649. return err;
  1650. return 0;
  1651. }
  1652. static int snd_ca0106_probe(struct pci_dev *pci,
  1653. const struct pci_device_id *pci_id)
  1654. {
  1655. static int dev;
  1656. struct snd_card *card;
  1657. struct snd_ca0106 *chip;
  1658. int i, err;
  1659. if (dev >= SNDRV_CARDS)
  1660. return -ENODEV;
  1661. if (!enable[dev]) {
  1662. dev++;
  1663. return -ENOENT;
  1664. }
  1665. err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
  1666. 0, &card);
  1667. if (err < 0)
  1668. return err;
  1669. err = snd_ca0106_create(dev, card, pci, &chip);
  1670. if (err < 0)
  1671. goto error;
  1672. card->private_data = chip;
  1673. for (i = 0; i < 4; i++) {
  1674. err = snd_ca0106_pcm(chip, i);
  1675. if (err < 0)
  1676. goto error;
  1677. }
  1678. if (chip->details->ac97 == 1) {
  1679. /* The SB0410 and SB0413 do not have an AC97 chip. */
  1680. err = snd_ca0106_ac97(chip);
  1681. if (err < 0)
  1682. goto error;
  1683. }
  1684. err = snd_ca0106_mixer(chip);
  1685. if (err < 0)
  1686. goto error;
  1687. dev_dbg(card->dev, "probe for MIDI channel A ...");
  1688. err = snd_ca0106_midi(chip, CA0106_MIDI_CHAN_A);
  1689. if (err < 0)
  1690. goto error;
  1691. dev_dbg(card->dev, " done.\n");
  1692. #ifdef CONFIG_SND_PROC_FS
  1693. snd_ca0106_proc_init(chip);
  1694. #endif
  1695. err = snd_card_register(card);
  1696. if (err < 0)
  1697. goto error;
  1698. pci_set_drvdata(pci, card);
  1699. dev++;
  1700. return 0;
  1701. error:
  1702. snd_card_free(card);
  1703. return err;
  1704. }
  1705. static void snd_ca0106_remove(struct pci_dev *pci)
  1706. {
  1707. snd_card_free(pci_get_drvdata(pci));
  1708. }
  1709. #ifdef CONFIG_PM_SLEEP
  1710. static int snd_ca0106_suspend(struct device *dev)
  1711. {
  1712. struct snd_card *card = dev_get_drvdata(dev);
  1713. struct snd_ca0106 *chip = card->private_data;
  1714. int i;
  1715. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  1716. for (i = 0; i < 4; i++)
  1717. snd_pcm_suspend_all(chip->pcm[i]);
  1718. if (chip->details->ac97)
  1719. snd_ac97_suspend(chip->ac97);
  1720. snd_ca0106_mixer_suspend(chip);
  1721. ca0106_stop_chip(chip);
  1722. return 0;
  1723. }
  1724. static int snd_ca0106_resume(struct device *dev)
  1725. {
  1726. struct snd_card *card = dev_get_drvdata(dev);
  1727. struct snd_ca0106 *chip = card->private_data;
  1728. int i;
  1729. ca0106_init_chip(chip, 1);
  1730. if (chip->details->ac97)
  1731. snd_ac97_resume(chip->ac97);
  1732. snd_ca0106_mixer_resume(chip);
  1733. if (chip->details->spi_dac) {
  1734. for (i = 0; i < ARRAY_SIZE(chip->spi_dac_reg); i++)
  1735. snd_ca0106_spi_write(chip, chip->spi_dac_reg[i]);
  1736. }
  1737. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  1738. return 0;
  1739. }
  1740. static SIMPLE_DEV_PM_OPS(snd_ca0106_pm, snd_ca0106_suspend, snd_ca0106_resume);
  1741. #define SND_CA0106_PM_OPS &snd_ca0106_pm
  1742. #else
  1743. #define SND_CA0106_PM_OPS NULL
  1744. #endif
  1745. // PCI IDs
  1746. static const struct pci_device_id snd_ca0106_ids[] = {
  1747. { PCI_VDEVICE(CREATIVE, 0x0007), 0 }, /* Audigy LS or Live 24bit */
  1748. { 0, }
  1749. };
  1750. MODULE_DEVICE_TABLE(pci, snd_ca0106_ids);
  1751. // pci_driver definition
  1752. static struct pci_driver ca0106_driver = {
  1753. .name = KBUILD_MODNAME,
  1754. .id_table = snd_ca0106_ids,
  1755. .probe = snd_ca0106_probe,
  1756. .remove = snd_ca0106_remove,
  1757. .driver = {
  1758. .pm = SND_CA0106_PM_OPS,
  1759. },
  1760. };
  1761. module_pci_driver(ca0106_driver);