p16v.c 26 KB

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