au88x0_pcm.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. */
  4. /*
  5. * Vortex PCM ALSA driver.
  6. *
  7. * Supports ADB and WT DMA. Unfortunately, WT channels do not run yet.
  8. * It remains stuck,and DMA transfers do not happen.
  9. */
  10. #include <sound/asoundef.h>
  11. #include <linux/time.h>
  12. #include <sound/core.h>
  13. #include <sound/pcm.h>
  14. #include <sound/pcm_params.h>
  15. #include "au88x0.h"
  16. #define VORTEX_PCM_TYPE(x) (x->name[40])
  17. /* hardware definition */
  18. static const struct snd_pcm_hardware snd_vortex_playback_hw_adb = {
  19. .info =
  20. (SNDRV_PCM_INFO_MMAP | /* SNDRV_PCM_INFO_RESUME | */
  21. SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_INTERLEAVED |
  22. SNDRV_PCM_INFO_MMAP_VALID),
  23. .formats =
  24. SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U8 |
  25. SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW,
  26. .rates = SNDRV_PCM_RATE_CONTINUOUS,
  27. .rate_min = 5000,
  28. .rate_max = 48000,
  29. .channels_min = 1,
  30. .channels_max = 2,
  31. .buffer_bytes_max = 0x10000,
  32. .period_bytes_min = 0x20,
  33. .period_bytes_max = 0x1000,
  34. .periods_min = 2,
  35. .periods_max = 1024,
  36. };
  37. #ifndef CHIP_AU8820
  38. static const struct snd_pcm_hardware snd_vortex_playback_hw_a3d = {
  39. .info =
  40. (SNDRV_PCM_INFO_MMAP | /* SNDRV_PCM_INFO_RESUME | */
  41. SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_INTERLEAVED |
  42. SNDRV_PCM_INFO_MMAP_VALID),
  43. .formats =
  44. SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U8 |
  45. SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW,
  46. .rates = SNDRV_PCM_RATE_CONTINUOUS,
  47. .rate_min = 5000,
  48. .rate_max = 48000,
  49. .channels_min = 1,
  50. .channels_max = 1,
  51. .buffer_bytes_max = 0x10000,
  52. .period_bytes_min = 0x100,
  53. .period_bytes_max = 0x1000,
  54. .periods_min = 2,
  55. .periods_max = 64,
  56. };
  57. #endif
  58. static const struct snd_pcm_hardware snd_vortex_playback_hw_spdif = {
  59. .info =
  60. (SNDRV_PCM_INFO_MMAP | /* SNDRV_PCM_INFO_RESUME | */
  61. SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_INTERLEAVED |
  62. SNDRV_PCM_INFO_MMAP_VALID),
  63. .formats =
  64. SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U8 |
  65. SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE | SNDRV_PCM_FMTBIT_MU_LAW |
  66. SNDRV_PCM_FMTBIT_A_LAW,
  67. .rates =
  68. SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
  69. .rate_min = 32000,
  70. .rate_max = 48000,
  71. .channels_min = 1,
  72. .channels_max = 2,
  73. .buffer_bytes_max = 0x10000,
  74. .period_bytes_min = 0x100,
  75. .period_bytes_max = 0x1000,
  76. .periods_min = 2,
  77. .periods_max = 64,
  78. };
  79. #ifndef CHIP_AU8810
  80. static const struct snd_pcm_hardware snd_vortex_playback_hw_wt = {
  81. .info = (SNDRV_PCM_INFO_MMAP |
  82. SNDRV_PCM_INFO_INTERLEAVED |
  83. SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_MMAP_VALID),
  84. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  85. .rates = SNDRV_PCM_RATE_8000_48000 | SNDRV_PCM_RATE_CONTINUOUS, // SNDRV_PCM_RATE_48000,
  86. .rate_min = 8000,
  87. .rate_max = 48000,
  88. .channels_min = 1,
  89. .channels_max = 2,
  90. .buffer_bytes_max = 0x10000,
  91. .period_bytes_min = 0x0400,
  92. .period_bytes_max = 0x1000,
  93. .periods_min = 2,
  94. .periods_max = 64,
  95. };
  96. #endif
  97. #ifdef CHIP_AU8830
  98. static const unsigned int au8830_channels[3] = {
  99. 1, 2, 4,
  100. };
  101. static const struct snd_pcm_hw_constraint_list hw_constraints_au8830_channels = {
  102. .count = ARRAY_SIZE(au8830_channels),
  103. .list = au8830_channels,
  104. .mask = 0,
  105. };
  106. #endif
  107. static void vortex_notify_pcm_vol_change(struct snd_card *card,
  108. struct snd_kcontrol *kctl, int activate)
  109. {
  110. if (activate)
  111. kctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  112. else
  113. kctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  114. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE |
  115. SNDRV_CTL_EVENT_MASK_INFO, &(kctl->id));
  116. }
  117. /* open callback */
  118. static int snd_vortex_pcm_open(struct snd_pcm_substream *substream)
  119. {
  120. vortex_t *vortex = snd_pcm_substream_chip(substream);
  121. struct snd_pcm_runtime *runtime = substream->runtime;
  122. int err;
  123. /* Force equal size periods */
  124. err = snd_pcm_hw_constraint_integer(runtime,
  125. SNDRV_PCM_HW_PARAM_PERIODS);
  126. if (err < 0)
  127. return err;
  128. /* Avoid PAGE_SIZE boundary to fall inside of a period. */
  129. err = snd_pcm_hw_constraint_pow2(runtime, 0,
  130. SNDRV_PCM_HW_PARAM_PERIOD_BYTES);
  131. if (err < 0)
  132. return err;
  133. snd_pcm_hw_constraint_step(runtime, 0,
  134. SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 64);
  135. if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_WT) {
  136. #ifndef CHIP_AU8820
  137. if (VORTEX_PCM_TYPE(substream->pcm) == VORTEX_PCM_A3D) {
  138. runtime->hw = snd_vortex_playback_hw_a3d;
  139. }
  140. #endif
  141. if (VORTEX_PCM_TYPE(substream->pcm) == VORTEX_PCM_SPDIF) {
  142. runtime->hw = snd_vortex_playback_hw_spdif;
  143. switch (vortex->spdif_sr) {
  144. case 32000:
  145. runtime->hw.rates = SNDRV_PCM_RATE_32000;
  146. break;
  147. case 44100:
  148. runtime->hw.rates = SNDRV_PCM_RATE_44100;
  149. break;
  150. case 48000:
  151. runtime->hw.rates = SNDRV_PCM_RATE_48000;
  152. break;
  153. }
  154. }
  155. if (VORTEX_PCM_TYPE(substream->pcm) == VORTEX_PCM_ADB
  156. || VORTEX_PCM_TYPE(substream->pcm) == VORTEX_PCM_I2S)
  157. runtime->hw = snd_vortex_playback_hw_adb;
  158. #ifdef CHIP_AU8830
  159. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
  160. VORTEX_IS_QUAD(vortex) &&
  161. VORTEX_PCM_TYPE(substream->pcm) == VORTEX_PCM_ADB) {
  162. runtime->hw.channels_max = 4;
  163. snd_pcm_hw_constraint_list(runtime, 0,
  164. SNDRV_PCM_HW_PARAM_CHANNELS,
  165. &hw_constraints_au8830_channels);
  166. }
  167. #endif
  168. substream->runtime->private_data = NULL;
  169. }
  170. #ifndef CHIP_AU8810
  171. else {
  172. runtime->hw = snd_vortex_playback_hw_wt;
  173. substream->runtime->private_data = NULL;
  174. }
  175. #endif
  176. return 0;
  177. }
  178. /* close callback */
  179. static int snd_vortex_pcm_close(struct snd_pcm_substream *substream)
  180. {
  181. //vortex_t *chip = snd_pcm_substream_chip(substream);
  182. stream_t *stream = (stream_t *) substream->runtime->private_data;
  183. // the hardware-specific codes will be here
  184. if (stream != NULL) {
  185. stream->substream = NULL;
  186. stream->nr_ch = 0;
  187. }
  188. substream->runtime->private_data = NULL;
  189. return 0;
  190. }
  191. /* hw_params callback */
  192. static int
  193. snd_vortex_pcm_hw_params(struct snd_pcm_substream *substream,
  194. struct snd_pcm_hw_params *hw_params)
  195. {
  196. vortex_t *chip = snd_pcm_substream_chip(substream);
  197. stream_t *stream = (stream_t *) (substream->runtime->private_data);
  198. /*
  199. pr_info( "Vortex: periods %d, period_bytes %d, channels = %d\n", params_periods(hw_params),
  200. params_period_bytes(hw_params), params_channels(hw_params));
  201. */
  202. spin_lock_irq(&chip->lock);
  203. // Make audio routes and config buffer DMA.
  204. if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_WT) {
  205. int dma, type = VORTEX_PCM_TYPE(substream->pcm);
  206. /* Dealloc any routes. */
  207. if (stream != NULL)
  208. vortex_adb_allocroute(chip, stream->dma,
  209. stream->nr_ch, stream->dir,
  210. stream->type,
  211. substream->number);
  212. /* Alloc routes. */
  213. dma =
  214. vortex_adb_allocroute(chip, -1,
  215. params_channels(hw_params),
  216. substream->stream, type,
  217. substream->number);
  218. if (dma < 0) {
  219. spin_unlock_irq(&chip->lock);
  220. return dma;
  221. }
  222. stream = substream->runtime->private_data = &chip->dma_adb[dma];
  223. stream->substream = substream;
  224. /* Setup Buffers. */
  225. vortex_adbdma_setbuffers(chip, dma,
  226. params_period_bytes(hw_params),
  227. params_periods(hw_params));
  228. if (VORTEX_PCM_TYPE(substream->pcm) == VORTEX_PCM_ADB) {
  229. chip->pcm_vol[substream->number].active = 1;
  230. vortex_notify_pcm_vol_change(chip->card,
  231. chip->pcm_vol[substream->number].kctl, 1);
  232. }
  233. }
  234. #ifndef CHIP_AU8810
  235. else {
  236. /* if (stream != NULL)
  237. vortex_wt_allocroute(chip, substream->number, 0); */
  238. vortex_wt_allocroute(chip, substream->number,
  239. params_channels(hw_params));
  240. stream = substream->runtime->private_data =
  241. &chip->dma_wt[substream->number];
  242. stream->dma = substream->number;
  243. stream->substream = substream;
  244. vortex_wtdma_setbuffers(chip, substream->number,
  245. params_period_bytes(hw_params),
  246. params_periods(hw_params));
  247. }
  248. #endif
  249. spin_unlock_irq(&chip->lock);
  250. return 0;
  251. }
  252. /* hw_free callback */
  253. static int snd_vortex_pcm_hw_free(struct snd_pcm_substream *substream)
  254. {
  255. vortex_t *chip = snd_pcm_substream_chip(substream);
  256. stream_t *stream = (stream_t *) (substream->runtime->private_data);
  257. spin_lock_irq(&chip->lock);
  258. // Delete audio routes.
  259. if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_WT) {
  260. if (stream != NULL) {
  261. if (VORTEX_PCM_TYPE(substream->pcm) == VORTEX_PCM_ADB) {
  262. chip->pcm_vol[substream->number].active = 0;
  263. vortex_notify_pcm_vol_change(chip->card,
  264. chip->pcm_vol[substream->number].kctl,
  265. 0);
  266. }
  267. vortex_adb_allocroute(chip, stream->dma,
  268. stream->nr_ch, stream->dir,
  269. stream->type,
  270. substream->number);
  271. }
  272. }
  273. #ifndef CHIP_AU8810
  274. else {
  275. if (stream != NULL)
  276. vortex_wt_allocroute(chip, stream->dma, 0);
  277. }
  278. #endif
  279. substream->runtime->private_data = NULL;
  280. spin_unlock_irq(&chip->lock);
  281. return 0;
  282. }
  283. /* prepare callback */
  284. static int snd_vortex_pcm_prepare(struct snd_pcm_substream *substream)
  285. {
  286. vortex_t *chip = snd_pcm_substream_chip(substream);
  287. struct snd_pcm_runtime *runtime = substream->runtime;
  288. stream_t *stream = (stream_t *) substream->runtime->private_data;
  289. int dma = stream->dma, fmt, dir;
  290. // set up the hardware with the current configuration.
  291. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  292. dir = 1;
  293. else
  294. dir = 0;
  295. fmt = vortex_alsafmt_aspfmt(runtime->format, chip);
  296. spin_lock_irq(&chip->lock);
  297. if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_WT) {
  298. vortex_adbdma_setmode(chip, dma, 1, dir, fmt,
  299. runtime->channels == 1 ? 0 : 1, 0);
  300. vortex_adbdma_setstartbuffer(chip, dma, 0);
  301. if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_SPDIF)
  302. vortex_adb_setsrc(chip, dma, runtime->rate, dir);
  303. }
  304. #ifndef CHIP_AU8810
  305. else {
  306. vortex_wtdma_setmode(chip, dma, 1, fmt, 0, 0);
  307. // FIXME: Set rate (i guess using vortex_wt_writereg() somehow).
  308. vortex_wtdma_setstartbuffer(chip, dma, 0);
  309. }
  310. #endif
  311. spin_unlock_irq(&chip->lock);
  312. return 0;
  313. }
  314. /* trigger callback */
  315. static int snd_vortex_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  316. {
  317. vortex_t *chip = snd_pcm_substream_chip(substream);
  318. stream_t *stream = (stream_t *) substream->runtime->private_data;
  319. int dma = stream->dma;
  320. spin_lock(&chip->lock);
  321. switch (cmd) {
  322. case SNDRV_PCM_TRIGGER_START:
  323. // do something to start the PCM engine
  324. //printk(KERN_INFO "vortex: start %d\n", dma);
  325. stream->fifo_enabled = 1;
  326. if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_WT) {
  327. vortex_adbdma_resetup(chip, dma);
  328. vortex_adbdma_startfifo(chip, dma);
  329. }
  330. #ifndef CHIP_AU8810
  331. else {
  332. dev_info(chip->card->dev, "wt start %d\n", dma);
  333. vortex_wtdma_startfifo(chip, dma);
  334. }
  335. #endif
  336. break;
  337. case SNDRV_PCM_TRIGGER_STOP:
  338. // do something to stop the PCM engine
  339. //printk(KERN_INFO "vortex: stop %d\n", dma);
  340. stream->fifo_enabled = 0;
  341. if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_WT)
  342. vortex_adbdma_stopfifo(chip, dma);
  343. #ifndef CHIP_AU8810
  344. else {
  345. dev_info(chip->card->dev, "wt stop %d\n", dma);
  346. vortex_wtdma_stopfifo(chip, dma);
  347. }
  348. #endif
  349. break;
  350. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  351. //printk(KERN_INFO "vortex: pause %d\n", dma);
  352. if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_WT)
  353. vortex_adbdma_pausefifo(chip, dma);
  354. #ifndef CHIP_AU8810
  355. else
  356. vortex_wtdma_pausefifo(chip, dma);
  357. #endif
  358. break;
  359. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  360. //printk(KERN_INFO "vortex: resume %d\n", dma);
  361. if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_WT)
  362. vortex_adbdma_resumefifo(chip, dma);
  363. #ifndef CHIP_AU8810
  364. else
  365. vortex_wtdma_resumefifo(chip, dma);
  366. #endif
  367. break;
  368. default:
  369. spin_unlock(&chip->lock);
  370. return -EINVAL;
  371. }
  372. spin_unlock(&chip->lock);
  373. return 0;
  374. }
  375. /* pointer callback */
  376. static snd_pcm_uframes_t snd_vortex_pcm_pointer(struct snd_pcm_substream *substream)
  377. {
  378. vortex_t *chip = snd_pcm_substream_chip(substream);
  379. stream_t *stream = (stream_t *) substream->runtime->private_data;
  380. int dma = stream->dma;
  381. snd_pcm_uframes_t current_ptr = 0;
  382. spin_lock(&chip->lock);
  383. if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_WT)
  384. current_ptr = vortex_adbdma_getlinearpos(chip, dma);
  385. #ifndef CHIP_AU8810
  386. else
  387. current_ptr = vortex_wtdma_getlinearpos(chip, dma);
  388. #endif
  389. //printk(KERN_INFO "vortex: pointer = 0x%x\n", current_ptr);
  390. spin_unlock(&chip->lock);
  391. current_ptr = bytes_to_frames(substream->runtime, current_ptr);
  392. if (current_ptr >= substream->runtime->buffer_size)
  393. current_ptr = 0;
  394. return current_ptr;
  395. }
  396. /* operators */
  397. static const struct snd_pcm_ops snd_vortex_playback_ops = {
  398. .open = snd_vortex_pcm_open,
  399. .close = snd_vortex_pcm_close,
  400. .hw_params = snd_vortex_pcm_hw_params,
  401. .hw_free = snd_vortex_pcm_hw_free,
  402. .prepare = snd_vortex_pcm_prepare,
  403. .trigger = snd_vortex_pcm_trigger,
  404. .pointer = snd_vortex_pcm_pointer,
  405. };
  406. /*
  407. * definitions of capture are omitted here...
  408. */
  409. static const char * const vortex_pcm_prettyname[VORTEX_PCM_LAST] = {
  410. CARD_NAME " ADB",
  411. CARD_NAME " SPDIF",
  412. CARD_NAME " A3D",
  413. CARD_NAME " WT",
  414. CARD_NAME " I2S",
  415. };
  416. static const char * const vortex_pcm_name[VORTEX_PCM_LAST] = {
  417. "adb",
  418. "spdif",
  419. "a3d",
  420. "wt",
  421. "i2s",
  422. };
  423. /* SPDIF kcontrol */
  424. static int snd_vortex_spdif_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  425. {
  426. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  427. uinfo->count = 1;
  428. return 0;
  429. }
  430. static int snd_vortex_spdif_mask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  431. {
  432. ucontrol->value.iec958.status[0] = 0xff;
  433. ucontrol->value.iec958.status[1] = 0xff;
  434. ucontrol->value.iec958.status[2] = 0xff;
  435. ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS;
  436. return 0;
  437. }
  438. static int snd_vortex_spdif_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  439. {
  440. vortex_t *vortex = snd_kcontrol_chip(kcontrol);
  441. ucontrol->value.iec958.status[0] = 0x00;
  442. ucontrol->value.iec958.status[1] = IEC958_AES1_CON_ORIGINAL|IEC958_AES1_CON_DIGDIGCONV_ID;
  443. ucontrol->value.iec958.status[2] = 0x00;
  444. switch (vortex->spdif_sr) {
  445. case 32000: ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS_32000; break;
  446. case 44100: ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS_44100; break;
  447. case 48000: ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS_48000; break;
  448. }
  449. return 0;
  450. }
  451. static int snd_vortex_spdif_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  452. {
  453. vortex_t *vortex = snd_kcontrol_chip(kcontrol);
  454. int spdif_sr = 48000;
  455. switch (ucontrol->value.iec958.status[3] & IEC958_AES3_CON_FS) {
  456. case IEC958_AES3_CON_FS_32000: spdif_sr = 32000; break;
  457. case IEC958_AES3_CON_FS_44100: spdif_sr = 44100; break;
  458. case IEC958_AES3_CON_FS_48000: spdif_sr = 48000; break;
  459. }
  460. if (spdif_sr == vortex->spdif_sr)
  461. return 0;
  462. vortex->spdif_sr = spdif_sr;
  463. vortex_spdif_init(vortex, vortex->spdif_sr, 1);
  464. return 1;
  465. }
  466. /* spdif controls */
  467. static const struct snd_kcontrol_new snd_vortex_mixer_spdif[] = {
  468. {
  469. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  470. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
  471. .info = snd_vortex_spdif_info,
  472. .get = snd_vortex_spdif_get,
  473. .put = snd_vortex_spdif_put,
  474. },
  475. {
  476. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  477. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  478. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
  479. .info = snd_vortex_spdif_info,
  480. .get = snd_vortex_spdif_mask_get
  481. },
  482. };
  483. /* subdevice PCM Volume control */
  484. static int snd_vortex_pcm_vol_info(struct snd_kcontrol *kcontrol,
  485. struct snd_ctl_elem_info *uinfo)
  486. {
  487. vortex_t *vortex = snd_kcontrol_chip(kcontrol);
  488. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  489. uinfo->count = (VORTEX_IS_QUAD(vortex) ? 4 : 2);
  490. uinfo->value.integer.min = -128;
  491. uinfo->value.integer.max = 32;
  492. return 0;
  493. }
  494. static int snd_vortex_pcm_vol_get(struct snd_kcontrol *kcontrol,
  495. struct snd_ctl_elem_value *ucontrol)
  496. {
  497. int i;
  498. vortex_t *vortex = snd_kcontrol_chip(kcontrol);
  499. int subdev = kcontrol->id.subdevice;
  500. struct pcm_vol *p = &vortex->pcm_vol[subdev];
  501. int max_chn = (VORTEX_IS_QUAD(vortex) ? 4 : 2);
  502. for (i = 0; i < max_chn; i++)
  503. ucontrol->value.integer.value[i] = p->vol[i];
  504. return 0;
  505. }
  506. static int snd_vortex_pcm_vol_put(struct snd_kcontrol *kcontrol,
  507. struct snd_ctl_elem_value *ucontrol)
  508. {
  509. int i;
  510. int changed = 0;
  511. int mixin;
  512. unsigned char vol;
  513. vortex_t *vortex = snd_kcontrol_chip(kcontrol);
  514. int subdev = kcontrol->id.subdevice;
  515. struct pcm_vol *p = &vortex->pcm_vol[subdev];
  516. int max_chn = (VORTEX_IS_QUAD(vortex) ? 4 : 2);
  517. for (i = 0; i < max_chn; i++) {
  518. if (p->vol[i] != ucontrol->value.integer.value[i]) {
  519. p->vol[i] = ucontrol->value.integer.value[i];
  520. if (p->active) {
  521. switch (vortex->dma_adb[p->dma].nr_ch) {
  522. case 1:
  523. mixin = p->mixin[0];
  524. break;
  525. case 2:
  526. default:
  527. mixin = p->mixin[(i < 2) ? i : (i - 2)];
  528. break;
  529. case 4:
  530. mixin = p->mixin[i];
  531. break;
  532. }
  533. vol = p->vol[i];
  534. vortex_mix_setinputvolumebyte(vortex,
  535. vortex->mixplayb[i], mixin, vol);
  536. }
  537. changed = 1;
  538. }
  539. }
  540. return changed;
  541. }
  542. static const DECLARE_TLV_DB_MINMAX(vortex_pcm_vol_db_scale, -9600, 2400);
  543. static const struct snd_kcontrol_new snd_vortex_pcm_vol = {
  544. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  545. .name = "PCM Playback Volume",
  546. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
  547. SNDRV_CTL_ELEM_ACCESS_TLV_READ |
  548. SNDRV_CTL_ELEM_ACCESS_INACTIVE,
  549. .info = snd_vortex_pcm_vol_info,
  550. .get = snd_vortex_pcm_vol_get,
  551. .put = snd_vortex_pcm_vol_put,
  552. .tlv = { .p = vortex_pcm_vol_db_scale },
  553. };
  554. /* create a pcm device */
  555. static int snd_vortex_new_pcm(vortex_t *chip, int idx, int nr)
  556. {
  557. struct snd_pcm *pcm;
  558. struct snd_kcontrol *kctl;
  559. int i;
  560. int err, nr_capt;
  561. if (!chip || idx < 0 || idx >= VORTEX_PCM_LAST)
  562. return -ENODEV;
  563. /* idx indicates which kind of PCM device. ADB, SPDIF, I2S and A3D share the
  564. * same dma engine. WT uses it own separate dma engine which can't capture. */
  565. if (idx == VORTEX_PCM_ADB)
  566. nr_capt = nr;
  567. else
  568. nr_capt = 0;
  569. err = snd_pcm_new(chip->card, vortex_pcm_prettyname[idx], idx, nr,
  570. nr_capt, &pcm);
  571. if (err < 0)
  572. return err;
  573. snprintf(pcm->name, sizeof(pcm->name),
  574. "%s %s", CARD_NAME_SHORT, vortex_pcm_name[idx]);
  575. chip->pcm[idx] = pcm;
  576. // This is an evil hack, but it saves a lot of duplicated code.
  577. VORTEX_PCM_TYPE(pcm) = idx;
  578. pcm->private_data = chip;
  579. /* set operators */
  580. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  581. &snd_vortex_playback_ops);
  582. if (idx == VORTEX_PCM_ADB)
  583. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
  584. &snd_vortex_playback_ops);
  585. /* pre-allocation of Scatter-Gather buffers */
  586. snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
  587. &chip->pci_dev->dev, 0x10000, 0x10000);
  588. switch (VORTEX_PCM_TYPE(pcm)) {
  589. case VORTEX_PCM_ADB:
  590. err = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  591. snd_pcm_std_chmaps,
  592. VORTEX_IS_QUAD(chip) ? 4 : 2,
  593. 0, NULL);
  594. if (err < 0)
  595. return err;
  596. err = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_CAPTURE,
  597. snd_pcm_std_chmaps, 2, 0, NULL);
  598. if (err < 0)
  599. return err;
  600. break;
  601. #ifdef CHIP_AU8830
  602. case VORTEX_PCM_A3D:
  603. err = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  604. snd_pcm_std_chmaps, 1, 0, NULL);
  605. if (err < 0)
  606. return err;
  607. break;
  608. #endif
  609. }
  610. if (VORTEX_PCM_TYPE(pcm) == VORTEX_PCM_SPDIF) {
  611. for (i = 0; i < ARRAY_SIZE(snd_vortex_mixer_spdif); i++) {
  612. kctl = snd_ctl_new1(&snd_vortex_mixer_spdif[i], chip);
  613. if (!kctl)
  614. return -ENOMEM;
  615. err = snd_ctl_add(chip->card, kctl);
  616. if (err < 0)
  617. return err;
  618. }
  619. }
  620. if (VORTEX_PCM_TYPE(pcm) == VORTEX_PCM_ADB) {
  621. for (i = 0; i < NR_PCM; i++) {
  622. chip->pcm_vol[i].active = 0;
  623. chip->pcm_vol[i].dma = -1;
  624. kctl = snd_ctl_new1(&snd_vortex_pcm_vol, chip);
  625. if (!kctl)
  626. return -ENOMEM;
  627. chip->pcm_vol[i].kctl = kctl;
  628. kctl->id.device = 0;
  629. kctl->id.subdevice = i;
  630. err = snd_ctl_add(chip->card, kctl);
  631. if (err < 0)
  632. return err;
  633. }
  634. }
  635. return 0;
  636. }