ca0106_main.c 59 KB

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