ca0106_main.c 59 KB

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