ca0106_main.c 59 KB

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