p16v.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  1. /*
  2. * Copyright (c) by James Courtier-Dutton <James@superbug.demon.co.uk>
  3. * Driver p16v chips
  4. * Version: 0.25
  5. *
  6. * FEATURES currently supported:
  7. * Output fixed at S32_LE, 2 channel to hw:0,0
  8. * Rates: 44.1, 48, 96, 192.
  9. *
  10. * Changelog:
  11. * 0.8
  12. * Use separate card based buffer for periods table.
  13. * 0.9
  14. * Use 2 channel output streams instead of 8 channel.
  15. * (8 channel output streams might be good for ASIO type output)
  16. * Corrected speaker output, so Front -> Front etc.
  17. * 0.10
  18. * Fixed missed interrupts.
  19. * 0.11
  20. * Add Sound card model number and names.
  21. * Add Analog volume controls.
  22. * 0.12
  23. * Corrected playback interrupts. Now interrupt per period, instead of half period.
  24. * 0.13
  25. * Use single trigger for multichannel.
  26. * 0.14
  27. * Mic capture now works at fixed: S32_LE, 96000Hz, Stereo.
  28. * 0.15
  29. * Force buffer_size / period_size == INTEGER.
  30. * 0.16
  31. * Update p16v.c to work with changed alsa api.
  32. * 0.17
  33. * Update p16v.c to work with changed alsa api. Removed boot_devs.
  34. * 0.18
  35. * Merging with snd-emu10k1 driver.
  36. * 0.19
  37. * One stereo channel at 24bit now works.
  38. * 0.20
  39. * Added better register defines.
  40. * 0.21
  41. * Integrated with snd-emu10k1 driver.
  42. * 0.22
  43. * Removed #if 0 ... #endif
  44. * 0.23
  45. * Implement different capture rates.
  46. * 0.24
  47. * Implement different capture source channels.
  48. * e.g. When HD Capture source is set to SPDIF,
  49. * setting HD Capture channel to 0 captures from CDROM digital input.
  50. * setting HD Capture channel to 1 captures from SPDIF in.
  51. * 0.25
  52. * Include capture buffer sizes.
  53. *
  54. * BUGS:
  55. * Some stability problems when unloading the snd-p16v kernel module.
  56. * --
  57. *
  58. * TODO:
  59. * SPDIF out.
  60. * Find out how to change capture sample rates. E.g. To record SPDIF at 48000Hz.
  61. * Currently capture fixed at 48000Hz.
  62. *
  63. * --
  64. * GENERAL INFO:
  65. * Model: SB0240
  66. * P16V Chip: CA0151-DBS
  67. * Audigy 2 Chip: CA0102-IAT
  68. * AC97 Codec: STAC 9721
  69. * ADC: Philips 1361T (Stereo 24bit)
  70. * DAC: CS4382-K (8-channel, 24bit, 192Khz)
  71. *
  72. * This code was initally based on code from ALSA's emu10k1x.c which is:
  73. * Copyright (c) by Francisco Moraes <fmoraes@nc.rr.com>
  74. *
  75. * This program is free software; you can redistribute it and/or modify
  76. * it under the terms of the GNU General Public License as published by
  77. * the Free Software Foundation; either version 2 of the License, or
  78. * (at your option) any later version.
  79. *
  80. * This program is distributed in the hope that it will be useful,
  81. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  82. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  83. * GNU General Public License for more details.
  84. *
  85. * You should have received a copy of the GNU General Public License
  86. * along with this program; if not, write to the Free Software
  87. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  88. *
  89. */
  90. #include <sound/driver.h>
  91. #include <linux/delay.h>
  92. #include <linux/init.h>
  93. #include <linux/interrupt.h>
  94. #include <linux/pci.h>
  95. #include <linux/slab.h>
  96. #include <linux/vmalloc.h>
  97. #include <linux/moduleparam.h>
  98. #include <sound/core.h>
  99. #include <sound/initval.h>
  100. #include <sound/pcm.h>
  101. #include <sound/ac97_codec.h>
  102. #include <sound/info.h>
  103. #include <sound/emu10k1.h>
  104. #include "p16v.h"
  105. #define SET_CHANNEL 0 /* Testing channel outputs 0=Front, 1=Center/LFE, 2=Unknown, 3=Rear */
  106. #define PCM_FRONT_CHANNEL 0
  107. #define PCM_REAR_CHANNEL 1
  108. #define PCM_CENTER_LFE_CHANNEL 2
  109. #define PCM_SIDE_CHANNEL 3
  110. #define CONTROL_FRONT_CHANNEL 0
  111. #define CONTROL_REAR_CHANNEL 3
  112. #define CONTROL_CENTER_LFE_CHANNEL 1
  113. #define CONTROL_SIDE_CHANNEL 2
  114. /* Card IDs:
  115. * Class 0401: 1102:0004 (rev 04) Subsystem: 1102:2002 -> Audigy2 ZS 7.1 Model:SB0350
  116. * Class 0401: 1102:0004 (rev 04) Subsystem: 1102:1007 -> Audigy2 6.1 Model:SB0240
  117. * Class 0401: 1102:0004 (rev 04) Subsystem: 1102:1002 -> Audigy2 Platinum Model:SB msb0240230009266
  118. * Class 0401: 1102:0004 (rev 04) Subsystem: 1102:2007 -> Audigy4 Pro Model:SB0380 M1SB0380472001901E
  119. *
  120. */
  121. /* hardware definition */
  122. static struct snd_pcm_hardware snd_p16v_playback_hw = {
  123. .info = (SNDRV_PCM_INFO_MMAP |
  124. SNDRV_PCM_INFO_INTERLEAVED |
  125. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  126. SNDRV_PCM_INFO_RESUME |
  127. SNDRV_PCM_INFO_MMAP_VALID),
  128. .formats = SNDRV_PCM_FMTBIT_S32_LE, /* Only supports 24-bit samples padded to 32 bits. */
  129. .rates = SNDRV_PCM_RATE_192000 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_44100,
  130. .rate_min = 44100,
  131. .rate_max = 192000,
  132. .channels_min = 8,
  133. .channels_max = 8,
  134. .buffer_bytes_max = ((65536 - 64) * 8),
  135. .period_bytes_min = 64,
  136. .period_bytes_max = (65536 - 64),
  137. .periods_min = 2,
  138. .periods_max = 8,
  139. .fifo_size = 0,
  140. };
  141. static struct snd_pcm_hardware snd_p16v_capture_hw = {
  142. .info = (SNDRV_PCM_INFO_MMAP |
  143. SNDRV_PCM_INFO_INTERLEAVED |
  144. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  145. SNDRV_PCM_INFO_RESUME |
  146. SNDRV_PCM_INFO_MMAP_VALID),
  147. .formats = SNDRV_PCM_FMTBIT_S32_LE,
  148. .rates = SNDRV_PCM_RATE_192000 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_44100,
  149. .rate_min = 44100,
  150. .rate_max = 192000,
  151. .channels_min = 2,
  152. .channels_max = 2,
  153. .buffer_bytes_max = (65536 - 64),
  154. .period_bytes_min = 64,
  155. .period_bytes_max = (65536 - 128) >> 1, /* size has to be N*64 bytes */
  156. .periods_min = 2,
  157. .periods_max = 2,
  158. .fifo_size = 0,
  159. };
  160. static void snd_p16v_pcm_free_substream(struct snd_pcm_runtime *runtime)
  161. {
  162. struct snd_emu10k1_pcm *epcm = runtime->private_data;
  163. if (epcm) {
  164. //snd_printk("epcm free: %p\n", epcm);
  165. kfree(epcm);
  166. }
  167. }
  168. /* open_playback callback */
  169. static int snd_p16v_pcm_open_playback_channel(struct snd_pcm_substream *substream, int channel_id)
  170. {
  171. struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
  172. struct snd_emu10k1_voice *channel = &(emu->p16v_voices[channel_id]);
  173. struct snd_emu10k1_pcm *epcm;
  174. struct snd_pcm_runtime *runtime = substream->runtime;
  175. int err;
  176. epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
  177. //snd_printk("epcm kcalloc: %p\n", epcm);
  178. if (epcm == NULL)
  179. return -ENOMEM;
  180. epcm->emu = emu;
  181. epcm->substream = substream;
  182. //snd_printk("epcm device=%d, channel_id=%d\n", substream->pcm->device, channel_id);
  183. runtime->private_data = epcm;
  184. runtime->private_free = snd_p16v_pcm_free_substream;
  185. runtime->hw = snd_p16v_playback_hw;
  186. channel->emu = emu;
  187. channel->number = channel_id;
  188. channel->use=1;
  189. //snd_printk("p16v: open channel_id=%d, channel=%p, use=0x%x\n", channel_id, channel, channel->use);
  190. //printk("open:channel_id=%d, chip=%p, channel=%p\n",channel_id, chip, channel);
  191. //channel->interrupt = snd_p16v_pcm_channel_interrupt;
  192. channel->epcm=epcm;
  193. if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
  194. return err;
  195. return 0;
  196. }
  197. /* open_capture callback */
  198. static int snd_p16v_pcm_open_capture_channel(struct snd_pcm_substream *substream, int channel_id)
  199. {
  200. struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
  201. struct snd_emu10k1_voice *channel = &(emu->p16v_capture_voice);
  202. struct snd_emu10k1_pcm *epcm;
  203. struct snd_pcm_runtime *runtime = substream->runtime;
  204. int err;
  205. epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
  206. //snd_printk("epcm kcalloc: %p\n", epcm);
  207. if (epcm == NULL)
  208. return -ENOMEM;
  209. epcm->emu = emu;
  210. epcm->substream = substream;
  211. //snd_printk("epcm device=%d, channel_id=%d\n", substream->pcm->device, channel_id);
  212. runtime->private_data = epcm;
  213. runtime->private_free = snd_p16v_pcm_free_substream;
  214. runtime->hw = snd_p16v_capture_hw;
  215. channel->emu = emu;
  216. channel->number = channel_id;
  217. channel->use=1;
  218. //snd_printk("p16v: open channel_id=%d, channel=%p, use=0x%x\n", channel_id, channel, channel->use);
  219. //printk("open:channel_id=%d, chip=%p, channel=%p\n",channel_id, chip, channel);
  220. //channel->interrupt = snd_p16v_pcm_channel_interrupt;
  221. channel->epcm=epcm;
  222. if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
  223. return err;
  224. return 0;
  225. }
  226. /* close callback */
  227. static int snd_p16v_pcm_close_playback(struct snd_pcm_substream *substream)
  228. {
  229. struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
  230. //struct snd_pcm_runtime *runtime = substream->runtime;
  231. //struct snd_emu10k1_pcm *epcm = runtime->private_data;
  232. emu->p16v_voices[substream->pcm->device - emu->p16v_device_offset].use=0;
  233. /* FIXME: maybe zero others */
  234. return 0;
  235. }
  236. /* close callback */
  237. static int snd_p16v_pcm_close_capture(struct snd_pcm_substream *substream)
  238. {
  239. struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
  240. //struct snd_pcm_runtime *runtime = substream->runtime;
  241. //struct snd_emu10k1_pcm *epcm = runtime->private_data;
  242. emu->p16v_capture_voice.use=0;
  243. /* FIXME: maybe zero others */
  244. return 0;
  245. }
  246. static int snd_p16v_pcm_open_playback_front(struct snd_pcm_substream *substream)
  247. {
  248. return snd_p16v_pcm_open_playback_channel(substream, PCM_FRONT_CHANNEL);
  249. }
  250. static int snd_p16v_pcm_open_capture(struct snd_pcm_substream *substream)
  251. {
  252. // Only using channel 0 for now, but the card has 2 channels.
  253. return snd_p16v_pcm_open_capture_channel(substream, 0);
  254. }
  255. /* hw_params callback */
  256. static int snd_p16v_pcm_hw_params_playback(struct snd_pcm_substream *substream,
  257. struct snd_pcm_hw_params *hw_params)
  258. {
  259. int result;
  260. result = snd_pcm_lib_malloc_pages(substream,
  261. params_buffer_bytes(hw_params));
  262. return result;
  263. }
  264. /* hw_params callback */
  265. static int snd_p16v_pcm_hw_params_capture(struct snd_pcm_substream *substream,
  266. struct snd_pcm_hw_params *hw_params)
  267. {
  268. int result;
  269. result = snd_pcm_lib_malloc_pages(substream,
  270. params_buffer_bytes(hw_params));
  271. return result;
  272. }
  273. /* hw_free callback */
  274. static int snd_p16v_pcm_hw_free_playback(struct snd_pcm_substream *substream)
  275. {
  276. int result;
  277. result = snd_pcm_lib_free_pages(substream);
  278. return result;
  279. }
  280. /* hw_free callback */
  281. static int snd_p16v_pcm_hw_free_capture(struct snd_pcm_substream *substream)
  282. {
  283. int result;
  284. result = snd_pcm_lib_free_pages(substream);
  285. return result;
  286. }
  287. /* prepare playback callback */
  288. static int snd_p16v_pcm_prepare_playback(struct snd_pcm_substream *substream)
  289. {
  290. struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
  291. struct snd_pcm_runtime *runtime = substream->runtime;
  292. int channel = substream->pcm->device - emu->p16v_device_offset;
  293. u32 *table_base = (u32 *)(emu->p16v_buffer.area+(8*16*channel));
  294. u32 period_size_bytes = frames_to_bytes(runtime, runtime->period_size);
  295. int i;
  296. u32 tmp;
  297. //snd_printk("prepare:channel_number=%d, rate=%d, format=0x%x, channels=%d, buffer_size=%ld, period_size=%ld, periods=%u, frames_to_bytes=%d\n",channel, runtime->rate, runtime->format, runtime->channels, runtime->buffer_size, runtime->period_size, runtime->periods, frames_to_bytes(runtime, 1));
  298. //snd_printk("dma_addr=%x, dma_area=%p, table_base=%p\n",runtime->dma_addr, runtime->dma_area, table_base);
  299. //snd_printk("dma_addr=%x, dma_area=%p, dma_bytes(size)=%x\n",emu->p16v_buffer.addr, emu->p16v_buffer.area, emu->p16v_buffer.bytes);
  300. tmp = snd_emu10k1_ptr_read(emu, A_SPDIF_SAMPLERATE, channel);
  301. switch (runtime->rate) {
  302. case 44100:
  303. snd_emu10k1_ptr_write(emu, A_SPDIF_SAMPLERATE, channel, (tmp & ~0xe0e0) | 0x8080);
  304. break;
  305. case 96000:
  306. snd_emu10k1_ptr_write(emu, A_SPDIF_SAMPLERATE, channel, (tmp & ~0xe0e0) | 0x4040);
  307. break;
  308. case 192000:
  309. snd_emu10k1_ptr_write(emu, A_SPDIF_SAMPLERATE, channel, (tmp & ~0xe0e0) | 0x2020);
  310. break;
  311. case 48000:
  312. default:
  313. snd_emu10k1_ptr_write(emu, A_SPDIF_SAMPLERATE, channel, (tmp & ~0xe0e0) | 0x0000);
  314. break;
  315. }
  316. /* FIXME: Check emu->buffer.size before actually writing to it. */
  317. for(i=0; i < runtime->periods; i++) {
  318. table_base[i*2]=runtime->dma_addr+(i*period_size_bytes);
  319. table_base[(i*2)+1]=period_size_bytes<<16;
  320. }
  321. snd_emu10k1_ptr20_write(emu, PLAYBACK_LIST_ADDR, channel, emu->p16v_buffer.addr+(8*16*channel));
  322. snd_emu10k1_ptr20_write(emu, PLAYBACK_LIST_SIZE, channel, (runtime->periods - 1) << 19);
  323. snd_emu10k1_ptr20_write(emu, PLAYBACK_LIST_PTR, channel, 0);
  324. snd_emu10k1_ptr20_write(emu, PLAYBACK_DMA_ADDR, channel, runtime->dma_addr);
  325. //snd_emu10k1_ptr20_write(emu, PLAYBACK_PERIOD_SIZE, channel, frames_to_bytes(runtime, runtime->period_size)<<16); // buffer size in bytes
  326. snd_emu10k1_ptr20_write(emu, PLAYBACK_PERIOD_SIZE, channel, 0); // buffer size in bytes
  327. snd_emu10k1_ptr20_write(emu, PLAYBACK_POINTER, channel, 0);
  328. snd_emu10k1_ptr20_write(emu, 0x07, channel, 0x0);
  329. snd_emu10k1_ptr20_write(emu, 0x08, channel, 0);
  330. return 0;
  331. }
  332. /* prepare capture callback */
  333. static int snd_p16v_pcm_prepare_capture(struct snd_pcm_substream *substream)
  334. {
  335. struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
  336. struct snd_pcm_runtime *runtime = substream->runtime;
  337. int channel = substream->pcm->device - emu->p16v_device_offset;
  338. u32 tmp;
  339. //printk("prepare capture:channel_number=%d, rate=%d, format=0x%x, channels=%d, buffer_size=%ld, period_size=%ld, frames_to_bytes=%d\n",channel, runtime->rate, runtime->format, runtime->channels, runtime->buffer_size, runtime->period_size, frames_to_bytes(runtime, 1));
  340. tmp = snd_emu10k1_ptr_read(emu, A_SPDIF_SAMPLERATE, channel);
  341. switch (runtime->rate) {
  342. case 44100:
  343. snd_emu10k1_ptr_write(emu, A_SPDIF_SAMPLERATE, channel, (tmp & ~0x0e00) | 0x0800);
  344. break;
  345. case 96000:
  346. snd_emu10k1_ptr_write(emu, A_SPDIF_SAMPLERATE, channel, (tmp & ~0x0e00) | 0x0400);
  347. break;
  348. case 192000:
  349. snd_emu10k1_ptr_write(emu, A_SPDIF_SAMPLERATE, channel, (tmp & ~0x0e00) | 0x0200);
  350. break;
  351. case 48000:
  352. default:
  353. snd_emu10k1_ptr_write(emu, A_SPDIF_SAMPLERATE, channel, (tmp & ~0x0e00) | 0x0000);
  354. break;
  355. }
  356. /* FIXME: Check emu->buffer.size before actually writing to it. */
  357. snd_emu10k1_ptr20_write(emu, 0x13, channel, 0);
  358. snd_emu10k1_ptr20_write(emu, CAPTURE_DMA_ADDR, channel, runtime->dma_addr);
  359. snd_emu10k1_ptr20_write(emu, CAPTURE_BUFFER_SIZE, channel, frames_to_bytes(runtime, runtime->buffer_size)<<16); // buffer size in bytes
  360. snd_emu10k1_ptr20_write(emu, CAPTURE_POINTER, channel, 0);
  361. //snd_emu10k1_ptr20_write(emu, CAPTURE_SOURCE, 0x0, 0x333300e4); /* Select MIC or Line in */
  362. //snd_emu10k1_ptr20_write(emu, EXTENDED_INT_MASK, 0, snd_emu10k1_ptr20_read(emu, EXTENDED_INT_MASK, 0) | (0x110000<<channel));
  363. return 0;
  364. }
  365. static void snd_p16v_intr_enable(struct snd_emu10k1 *emu, unsigned int intrenb)
  366. {
  367. unsigned long flags;
  368. unsigned int enable;
  369. spin_lock_irqsave(&emu->emu_lock, flags);
  370. enable = inl(emu->port + INTE2) | intrenb;
  371. outl(enable, emu->port + INTE2);
  372. spin_unlock_irqrestore(&emu->emu_lock, flags);
  373. }
  374. static void snd_p16v_intr_disable(struct snd_emu10k1 *emu, unsigned int intrenb)
  375. {
  376. unsigned long flags;
  377. unsigned int disable;
  378. spin_lock_irqsave(&emu->emu_lock, flags);
  379. disable = inl(emu->port + INTE2) & (~intrenb);
  380. outl(disable, emu->port + INTE2);
  381. spin_unlock_irqrestore(&emu->emu_lock, flags);
  382. }
  383. /* trigger_playback callback */
  384. static int snd_p16v_pcm_trigger_playback(struct snd_pcm_substream *substream,
  385. int cmd)
  386. {
  387. struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
  388. struct snd_pcm_runtime *runtime;
  389. struct snd_emu10k1_pcm *epcm;
  390. int channel;
  391. int result = 0;
  392. struct list_head *pos;
  393. struct snd_pcm_substream *s;
  394. u32 basic = 0;
  395. u32 inte = 0;
  396. int running=0;
  397. switch (cmd) {
  398. case SNDRV_PCM_TRIGGER_START:
  399. running=1;
  400. break;
  401. case SNDRV_PCM_TRIGGER_STOP:
  402. default:
  403. running=0;
  404. break;
  405. }
  406. snd_pcm_group_for_each(pos, substream) {
  407. s = snd_pcm_group_substream_entry(pos);
  408. runtime = s->runtime;
  409. epcm = runtime->private_data;
  410. channel = substream->pcm->device-emu->p16v_device_offset;
  411. //snd_printk("p16v channel=%d\n",channel);
  412. epcm->running = running;
  413. basic |= (0x1<<channel);
  414. inte |= (INTE2_PLAYBACK_CH_0_LOOP<<channel);
  415. snd_pcm_trigger_done(s, substream);
  416. }
  417. //snd_printk("basic=0x%x, inte=0x%x\n",basic, inte);
  418. switch (cmd) {
  419. case SNDRV_PCM_TRIGGER_START:
  420. snd_p16v_intr_enable(emu, inte);
  421. snd_emu10k1_ptr20_write(emu, BASIC_INTERRUPT, 0, snd_emu10k1_ptr20_read(emu, BASIC_INTERRUPT, 0)| (basic));
  422. break;
  423. case SNDRV_PCM_TRIGGER_STOP:
  424. snd_emu10k1_ptr20_write(emu, BASIC_INTERRUPT, 0, snd_emu10k1_ptr20_read(emu, BASIC_INTERRUPT, 0) & ~(basic));
  425. snd_p16v_intr_disable(emu, inte);
  426. break;
  427. default:
  428. result = -EINVAL;
  429. break;
  430. }
  431. return result;
  432. }
  433. /* trigger_capture callback */
  434. static int snd_p16v_pcm_trigger_capture(struct snd_pcm_substream *substream,
  435. int cmd)
  436. {
  437. struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
  438. struct snd_pcm_runtime *runtime = substream->runtime;
  439. struct snd_emu10k1_pcm *epcm = runtime->private_data;
  440. int channel = 0;
  441. int result = 0;
  442. u32 inte = INTE2_CAPTURE_CH_0_LOOP | INTE2_CAPTURE_CH_0_HALF_LOOP;
  443. switch (cmd) {
  444. case SNDRV_PCM_TRIGGER_START:
  445. snd_p16v_intr_enable(emu, inte);
  446. snd_emu10k1_ptr20_write(emu, BASIC_INTERRUPT, 0, snd_emu10k1_ptr20_read(emu, BASIC_INTERRUPT, 0)|(0x100<<channel));
  447. epcm->running = 1;
  448. break;
  449. case SNDRV_PCM_TRIGGER_STOP:
  450. snd_emu10k1_ptr20_write(emu, BASIC_INTERRUPT, 0, snd_emu10k1_ptr20_read(emu, BASIC_INTERRUPT, 0) & ~(0x100<<channel));
  451. snd_p16v_intr_disable(emu, inte);
  452. //snd_emu10k1_ptr20_write(emu, EXTENDED_INT_MASK, 0, snd_emu10k1_ptr20_read(emu, EXTENDED_INT_MASK, 0) & ~(0x110000<<channel));
  453. epcm->running = 0;
  454. break;
  455. default:
  456. result = -EINVAL;
  457. break;
  458. }
  459. return result;
  460. }
  461. /* pointer_playback callback */
  462. static snd_pcm_uframes_t
  463. snd_p16v_pcm_pointer_playback(struct snd_pcm_substream *substream)
  464. {
  465. struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
  466. struct snd_pcm_runtime *runtime = substream->runtime;
  467. struct snd_emu10k1_pcm *epcm = runtime->private_data;
  468. snd_pcm_uframes_t ptr, ptr1, ptr2,ptr3,ptr4 = 0;
  469. int channel = substream->pcm->device - emu->p16v_device_offset;
  470. if (!epcm->running)
  471. return 0;
  472. ptr3 = snd_emu10k1_ptr20_read(emu, PLAYBACK_LIST_PTR, channel);
  473. ptr1 = snd_emu10k1_ptr20_read(emu, PLAYBACK_POINTER, channel);
  474. ptr4 = snd_emu10k1_ptr20_read(emu, PLAYBACK_LIST_PTR, channel);
  475. if (ptr3 != ptr4) ptr1 = snd_emu10k1_ptr20_read(emu, PLAYBACK_POINTER, channel);
  476. ptr2 = bytes_to_frames(runtime, ptr1);
  477. ptr2+= (ptr4 >> 3) * runtime->period_size;
  478. ptr=ptr2;
  479. if (ptr >= runtime->buffer_size)
  480. ptr -= runtime->buffer_size;
  481. return ptr;
  482. }
  483. /* pointer_capture callback */
  484. static snd_pcm_uframes_t
  485. snd_p16v_pcm_pointer_capture(struct snd_pcm_substream *substream)
  486. {
  487. struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
  488. struct snd_pcm_runtime *runtime = substream->runtime;
  489. struct snd_emu10k1_pcm *epcm = runtime->private_data;
  490. snd_pcm_uframes_t ptr, ptr1, ptr2 = 0;
  491. int channel = 0;
  492. if (!epcm->running)
  493. return 0;
  494. ptr1 = snd_emu10k1_ptr20_read(emu, CAPTURE_POINTER, channel);
  495. ptr2 = bytes_to_frames(runtime, ptr1);
  496. ptr=ptr2;
  497. if (ptr >= runtime->buffer_size) {
  498. ptr -= runtime->buffer_size;
  499. printk(KERN_WARNING "buffer capture limited!\n");
  500. }
  501. //printk("ptr1 = 0x%lx, ptr2=0x%lx, ptr=0x%lx, buffer_size = 0x%x, period_size = 0x%x, bits=%d, rate=%d\n", ptr1, ptr2, ptr, (int)runtime->buffer_size, (int)runtime->period_size, (int)runtime->frame_bits, (int)runtime->rate);
  502. return ptr;
  503. }
  504. /* operators */
  505. static struct snd_pcm_ops snd_p16v_playback_front_ops = {
  506. .open = snd_p16v_pcm_open_playback_front,
  507. .close = snd_p16v_pcm_close_playback,
  508. .ioctl = snd_pcm_lib_ioctl,
  509. .hw_params = snd_p16v_pcm_hw_params_playback,
  510. .hw_free = snd_p16v_pcm_hw_free_playback,
  511. .prepare = snd_p16v_pcm_prepare_playback,
  512. .trigger = snd_p16v_pcm_trigger_playback,
  513. .pointer = snd_p16v_pcm_pointer_playback,
  514. };
  515. static struct snd_pcm_ops snd_p16v_capture_ops = {
  516. .open = snd_p16v_pcm_open_capture,
  517. .close = snd_p16v_pcm_close_capture,
  518. .ioctl = snd_pcm_lib_ioctl,
  519. .hw_params = snd_p16v_pcm_hw_params_capture,
  520. .hw_free = snd_p16v_pcm_hw_free_capture,
  521. .prepare = snd_p16v_pcm_prepare_capture,
  522. .trigger = snd_p16v_pcm_trigger_capture,
  523. .pointer = snd_p16v_pcm_pointer_capture,
  524. };
  525. int snd_p16v_free(struct snd_emu10k1 *chip)
  526. {
  527. // release the data
  528. if (chip->p16v_buffer.area) {
  529. snd_dma_free_pages(&chip->p16v_buffer);
  530. //snd_printk("period lables free: %p\n", &chip->p16v_buffer);
  531. }
  532. return 0;
  533. }
  534. int __devinit snd_p16v_pcm(struct snd_emu10k1 *emu, int device, struct snd_pcm **rpcm)
  535. {
  536. struct snd_pcm *pcm;
  537. struct snd_pcm_substream *substream;
  538. int err;
  539. int capture=1;
  540. //snd_printk("snd_p16v_pcm called. device=%d\n", device);
  541. emu->p16v_device_offset = device;
  542. if (rpcm)
  543. *rpcm = NULL;
  544. if ((err = snd_pcm_new(emu->card, "p16v", device, 1, capture, &pcm)) < 0)
  545. return err;
  546. pcm->private_data = emu;
  547. // Single playback 8 channel device.
  548. // Single capture 2 channel device.
  549. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_p16v_playback_front_ops);
  550. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_p16v_capture_ops);
  551. pcm->info_flags = 0;
  552. pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
  553. strcpy(pcm->name, "p16v");
  554. emu->pcm_p16v = pcm;
  555. for(substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
  556. substream;
  557. substream = substream->next) {
  558. if ((err = snd_pcm_lib_preallocate_pages(substream,
  559. SNDRV_DMA_TYPE_DEV,
  560. snd_dma_pci_data(emu->pci),
  561. ((65536 - 64) * 8), ((65536 - 64) * 8))) < 0)
  562. return err;
  563. //snd_printk("preallocate playback substream: err=%d\n", err);
  564. }
  565. for (substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
  566. substream;
  567. substream = substream->next) {
  568. if ((err = snd_pcm_lib_preallocate_pages(substream,
  569. SNDRV_DMA_TYPE_DEV,
  570. snd_dma_pci_data(emu->pci),
  571. 65536 - 64, 65536 - 64)) < 0)
  572. return err;
  573. //snd_printk("preallocate capture substream: err=%d\n", err);
  574. }
  575. if (rpcm)
  576. *rpcm = pcm;
  577. return 0;
  578. }
  579. static int snd_p16v_volume_info(struct snd_kcontrol *kcontrol,
  580. struct snd_ctl_elem_info *uinfo)
  581. {
  582. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  583. uinfo->count = 2;
  584. uinfo->value.integer.min = 0;
  585. uinfo->value.integer.max = 255;
  586. return 0;
  587. }
  588. static int snd_p16v_volume_get(struct snd_kcontrol *kcontrol,
  589. struct snd_ctl_elem_value *ucontrol)
  590. {
  591. struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
  592. int high_low = (kcontrol->private_value >> 8) & 0xff;
  593. int reg = kcontrol->private_value & 0xff;
  594. u32 value;
  595. value = snd_emu10k1_ptr20_read(emu, reg, high_low);
  596. if (high_low) {
  597. ucontrol->value.integer.value[0] = 0xff - ((value >> 24) & 0xff); /* Left */
  598. ucontrol->value.integer.value[1] = 0xff - ((value >> 16) & 0xff); /* Right */
  599. } else {
  600. ucontrol->value.integer.value[0] = 0xff - ((value >> 8) & 0xff); /* Left */
  601. ucontrol->value.integer.value[1] = 0xff - ((value >> 0) & 0xff); /* Right */
  602. }
  603. return 0;
  604. }
  605. static int snd_p16v_volume_put(struct snd_kcontrol *kcontrol,
  606. struct snd_ctl_elem_value *ucontrol)
  607. {
  608. struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
  609. int high_low = (kcontrol->private_value >> 8) & 0xff;
  610. int reg = kcontrol->private_value & 0xff;
  611. u32 value, oval;
  612. oval = value = snd_emu10k1_ptr20_read(emu, reg, 0);
  613. if (high_low == 1) {
  614. value &= 0xffff;
  615. value |= ((0xff - ucontrol->value.integer.value[0]) << 24) |
  616. ((0xff - ucontrol->value.integer.value[1]) << 16);
  617. } else {
  618. value &= 0xffff0000;
  619. value |= ((0xff - ucontrol->value.integer.value[0]) << 8) |
  620. ((0xff - ucontrol->value.integer.value[1]) );
  621. }
  622. if (value != oval) {
  623. snd_emu10k1_ptr20_write(emu, reg, 0, value);
  624. return 1;
  625. }
  626. return 0;
  627. }
  628. static int snd_p16v_capture_source_info(struct snd_kcontrol *kcontrol,
  629. struct snd_ctl_elem_info *uinfo)
  630. {
  631. static char *texts[8] = {
  632. "SPDIF", "I2S", "SRC48", "SRCMulti_SPDIF", "SRCMulti_I2S",
  633. "CDIF", "FX", "AC97"
  634. };
  635. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  636. uinfo->count = 1;
  637. uinfo->value.enumerated.items = 8;
  638. if (uinfo->value.enumerated.item > 7)
  639. uinfo->value.enumerated.item = 7;
  640. strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
  641. return 0;
  642. }
  643. static int snd_p16v_capture_source_get(struct snd_kcontrol *kcontrol,
  644. struct snd_ctl_elem_value *ucontrol)
  645. {
  646. struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
  647. ucontrol->value.enumerated.item[0] = emu->p16v_capture_source;
  648. return 0;
  649. }
  650. static int snd_p16v_capture_source_put(struct snd_kcontrol *kcontrol,
  651. struct snd_ctl_elem_value *ucontrol)
  652. {
  653. struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
  654. unsigned int val;
  655. int change = 0;
  656. u32 mask;
  657. u32 source;
  658. val = ucontrol->value.enumerated.item[0] ;
  659. change = (emu->p16v_capture_source != val);
  660. if (change) {
  661. emu->p16v_capture_source = val;
  662. source = (val << 28) | (val << 24) | (val << 20) | (val << 16);
  663. mask = snd_emu10k1_ptr20_read(emu, BASIC_INTERRUPT, 0) & 0xffff;
  664. snd_emu10k1_ptr20_write(emu, BASIC_INTERRUPT, 0, source | mask);
  665. }
  666. return change;
  667. }
  668. static int snd_p16v_capture_channel_info(struct snd_kcontrol *kcontrol,
  669. struct snd_ctl_elem_info *uinfo)
  670. {
  671. static char *texts[4] = { "0", "1", "2", "3", };
  672. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  673. uinfo->count = 1;
  674. uinfo->value.enumerated.items = 4;
  675. if (uinfo->value.enumerated.item > 3)
  676. uinfo->value.enumerated.item = 3;
  677. strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
  678. return 0;
  679. }
  680. static int snd_p16v_capture_channel_get(struct snd_kcontrol *kcontrol,
  681. struct snd_ctl_elem_value *ucontrol)
  682. {
  683. struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
  684. ucontrol->value.enumerated.item[0] = emu->p16v_capture_channel;
  685. return 0;
  686. }
  687. static int snd_p16v_capture_channel_put(struct snd_kcontrol *kcontrol,
  688. struct snd_ctl_elem_value *ucontrol)
  689. {
  690. struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
  691. unsigned int val;
  692. int change = 0;
  693. u32 tmp;
  694. val = ucontrol->value.enumerated.item[0] ;
  695. change = (emu->p16v_capture_channel != val);
  696. if (change) {
  697. emu->p16v_capture_channel = val;
  698. tmp = snd_emu10k1_ptr20_read(emu, CAPTURE_P16V_SOURCE, 0) & 0xfffc;
  699. snd_emu10k1_ptr20_write(emu, CAPTURE_P16V_SOURCE, 0, tmp | val);
  700. }
  701. return change;
  702. }
  703. #define P16V_VOL(xname,xreg,xhl) { \
  704. .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
  705. .info = snd_p16v_volume_info, \
  706. .get = snd_p16v_volume_get, \
  707. .put = snd_p16v_volume_put, \
  708. .private_value = ((xreg) | ((xhl) << 8)) \
  709. }
  710. static struct snd_kcontrol_new p16v_mixer_controls[] __devinitdata = {
  711. P16V_VOL("HD Analog Front Playback Volume", PLAYBACK_VOLUME_MIXER9, 0),
  712. P16V_VOL("HD Analog Rear Playback Volume", PLAYBACK_VOLUME_MIXER10, 1),
  713. P16V_VOL("HD Analog Center/LFE Playback Volume", PLAYBACK_VOLUME_MIXER9, 1),
  714. P16V_VOL("HD Analog Side Playback Volume", PLAYBACK_VOLUME_MIXER10, 0),
  715. P16V_VOL("HD SPDIF Front Playback Volume", PLAYBACK_VOLUME_MIXER7, 0),
  716. P16V_VOL("HD SPDIF Rear Playback Volume", PLAYBACK_VOLUME_MIXER8, 1),
  717. P16V_VOL("HD SPDIF Center/LFE Playback Volume", PLAYBACK_VOLUME_MIXER7, 1),
  718. P16V_VOL("HD SPDIF Side Playback Volume", PLAYBACK_VOLUME_MIXER8, 0),
  719. {
  720. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  721. .name = "HD source Capture",
  722. .info = snd_p16v_capture_source_info,
  723. .get = snd_p16v_capture_source_get,
  724. .put = snd_p16v_capture_source_put
  725. },
  726. {
  727. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  728. .name = "HD channel Capture",
  729. .info = snd_p16v_capture_channel_info,
  730. .get = snd_p16v_capture_channel_get,
  731. .put = snd_p16v_capture_channel_put
  732. },
  733. };
  734. int __devinit snd_p16v_mixer(struct snd_emu10k1 *emu)
  735. {
  736. int i, err;
  737. struct snd_card *card = emu->card;
  738. for (i = 0; i < ARRAY_SIZE(p16v_mixer_controls); i++) {
  739. if ((err = snd_ctl_add(card, snd_ctl_new1(&p16v_mixer_controls[i],
  740. emu))) < 0)
  741. return err;
  742. }
  743. return 0;
  744. }
  745. #ifdef CONFIG_PM
  746. #define NUM_CHS 1 /* up to 4, but only first channel is used */
  747. int __devinit snd_p16v_alloc_pm_buffer(struct snd_emu10k1 *emu)
  748. {
  749. emu->p16v_saved = vmalloc(NUM_CHS * 4 * 0x80);
  750. if (! emu->p16v_saved)
  751. return -ENOMEM;
  752. return 0;
  753. }
  754. void snd_p16v_free_pm_buffer(struct snd_emu10k1 *emu)
  755. {
  756. vfree(emu->p16v_saved);
  757. }
  758. void snd_p16v_suspend(struct snd_emu10k1 *emu)
  759. {
  760. int i, ch;
  761. unsigned int *val;
  762. val = emu->p16v_saved;
  763. for (ch = 0; ch < NUM_CHS; ch++)
  764. for (i = 0; i < 0x80; i++, val++)
  765. *val = snd_emu10k1_ptr20_read(emu, i, ch);
  766. }
  767. void snd_p16v_resume(struct snd_emu10k1 *emu)
  768. {
  769. int i, ch;
  770. unsigned int *val;
  771. val = emu->p16v_saved;
  772. for (ch = 0; ch < NUM_CHS; ch++)
  773. for (i = 0; i < 0x80; i++, val++)
  774. snd_emu10k1_ptr20_write(emu, i, ch, *val);
  775. }
  776. #endif