aw2-alsa.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*****************************************************************************
  3. *
  4. * Copyright (C) 2008 Cedric Bregardis <[email protected]> and
  5. * Jean-Christian Hassler <[email protected]>
  6. *
  7. * This file is part of the Audiowerk2 ALSA driver
  8. *
  9. *****************************************************************************/
  10. #include <linux/init.h>
  11. #include <linux/pci.h>
  12. #include <linux/dma-mapping.h>
  13. #include <linux/slab.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/delay.h>
  16. #include <linux/io.h>
  17. #include <linux/module.h>
  18. #include <sound/core.h>
  19. #include <sound/initval.h>
  20. #include <sound/pcm.h>
  21. #include <sound/pcm_params.h>
  22. #include <sound/control.h>
  23. #include "saa7146.h"
  24. #include "aw2-saa7146.h"
  25. MODULE_AUTHOR("Cedric Bregardis <[email protected]>, "
  26. "Jean-Christian Hassler <[email protected]>");
  27. MODULE_DESCRIPTION("Emagic Audiowerk 2 sound driver");
  28. MODULE_LICENSE("GPL");
  29. /*********************************
  30. * DEFINES
  31. ********************************/
  32. #define CTL_ROUTE_ANALOG 0
  33. #define CTL_ROUTE_DIGITAL 1
  34. /*********************************
  35. * TYPEDEFS
  36. ********************************/
  37. /* hardware definition */
  38. static const struct snd_pcm_hardware snd_aw2_playback_hw = {
  39. .info = (SNDRV_PCM_INFO_MMAP |
  40. SNDRV_PCM_INFO_INTERLEAVED |
  41. SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_MMAP_VALID),
  42. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  43. .rates = SNDRV_PCM_RATE_44100,
  44. .rate_min = 44100,
  45. .rate_max = 44100,
  46. .channels_min = 2,
  47. .channels_max = 4,
  48. .buffer_bytes_max = 32768,
  49. .period_bytes_min = 4096,
  50. .period_bytes_max = 32768,
  51. .periods_min = 1,
  52. .periods_max = 1024,
  53. };
  54. static const struct snd_pcm_hardware snd_aw2_capture_hw = {
  55. .info = (SNDRV_PCM_INFO_MMAP |
  56. SNDRV_PCM_INFO_INTERLEAVED |
  57. SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_MMAP_VALID),
  58. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  59. .rates = SNDRV_PCM_RATE_44100,
  60. .rate_min = 44100,
  61. .rate_max = 44100,
  62. .channels_min = 2,
  63. .channels_max = 2,
  64. .buffer_bytes_max = 32768,
  65. .period_bytes_min = 4096,
  66. .period_bytes_max = 32768,
  67. .periods_min = 1,
  68. .periods_max = 1024,
  69. };
  70. struct aw2_pcm_device {
  71. struct snd_pcm *pcm;
  72. unsigned int stream_number;
  73. struct aw2 *chip;
  74. };
  75. struct aw2 {
  76. struct snd_aw2_saa7146 saa7146;
  77. struct pci_dev *pci;
  78. int irq;
  79. spinlock_t reg_lock;
  80. struct mutex mtx;
  81. unsigned long iobase_phys;
  82. void __iomem *iobase_virt;
  83. struct snd_card *card;
  84. struct aw2_pcm_device device_playback[NB_STREAM_PLAYBACK];
  85. struct aw2_pcm_device device_capture[NB_STREAM_CAPTURE];
  86. };
  87. /*********************************
  88. * FUNCTION DECLARATIONS
  89. ********************************/
  90. static int snd_aw2_create(struct snd_card *card, struct pci_dev *pci);
  91. static int snd_aw2_probe(struct pci_dev *pci,
  92. const struct pci_device_id *pci_id);
  93. static int snd_aw2_pcm_playback_open(struct snd_pcm_substream *substream);
  94. static int snd_aw2_pcm_playback_close(struct snd_pcm_substream *substream);
  95. static int snd_aw2_pcm_capture_open(struct snd_pcm_substream *substream);
  96. static int snd_aw2_pcm_capture_close(struct snd_pcm_substream *substream);
  97. static int snd_aw2_pcm_prepare_playback(struct snd_pcm_substream *substream);
  98. static int snd_aw2_pcm_prepare_capture(struct snd_pcm_substream *substream);
  99. static int snd_aw2_pcm_trigger_playback(struct snd_pcm_substream *substream,
  100. int cmd);
  101. static int snd_aw2_pcm_trigger_capture(struct snd_pcm_substream *substream,
  102. int cmd);
  103. static snd_pcm_uframes_t snd_aw2_pcm_pointer_playback(struct snd_pcm_substream
  104. *substream);
  105. static snd_pcm_uframes_t snd_aw2_pcm_pointer_capture(struct snd_pcm_substream
  106. *substream);
  107. static int snd_aw2_new_pcm(struct aw2 *chip);
  108. static int snd_aw2_control_switch_capture_info(struct snd_kcontrol *kcontrol,
  109. struct snd_ctl_elem_info *uinfo);
  110. static int snd_aw2_control_switch_capture_get(struct snd_kcontrol *kcontrol,
  111. struct snd_ctl_elem_value
  112. *ucontrol);
  113. static int snd_aw2_control_switch_capture_put(struct snd_kcontrol *kcontrol,
  114. struct snd_ctl_elem_value
  115. *ucontrol);
  116. /*********************************
  117. * VARIABLES
  118. ********************************/
  119. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
  120. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
  121. static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
  122. module_param_array(index, int, NULL, 0444);
  123. MODULE_PARM_DESC(index, "Index value for Audiowerk2 soundcard.");
  124. module_param_array(id, charp, NULL, 0444);
  125. MODULE_PARM_DESC(id, "ID string for the Audiowerk2 soundcard.");
  126. module_param_array(enable, bool, NULL, 0444);
  127. MODULE_PARM_DESC(enable, "Enable Audiowerk2 soundcard.");
  128. static const struct pci_device_id snd_aw2_ids[] = {
  129. {PCI_VENDOR_ID_PHILIPS, PCI_DEVICE_ID_PHILIPS_SAA7146, 0, 0,
  130. 0, 0, 0},
  131. {0}
  132. };
  133. MODULE_DEVICE_TABLE(pci, snd_aw2_ids);
  134. /* pci_driver definition */
  135. static struct pci_driver aw2_driver = {
  136. .name = KBUILD_MODNAME,
  137. .id_table = snd_aw2_ids,
  138. .probe = snd_aw2_probe,
  139. };
  140. module_pci_driver(aw2_driver);
  141. /* operators for playback PCM alsa interface */
  142. static const struct snd_pcm_ops snd_aw2_playback_ops = {
  143. .open = snd_aw2_pcm_playback_open,
  144. .close = snd_aw2_pcm_playback_close,
  145. .prepare = snd_aw2_pcm_prepare_playback,
  146. .trigger = snd_aw2_pcm_trigger_playback,
  147. .pointer = snd_aw2_pcm_pointer_playback,
  148. };
  149. /* operators for capture PCM alsa interface */
  150. static const struct snd_pcm_ops snd_aw2_capture_ops = {
  151. .open = snd_aw2_pcm_capture_open,
  152. .close = snd_aw2_pcm_capture_close,
  153. .prepare = snd_aw2_pcm_prepare_capture,
  154. .trigger = snd_aw2_pcm_trigger_capture,
  155. .pointer = snd_aw2_pcm_pointer_capture,
  156. };
  157. static const struct snd_kcontrol_new aw2_control = {
  158. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  159. .name = "PCM Capture Route",
  160. .index = 0,
  161. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  162. .private_value = 0xffff,
  163. .info = snd_aw2_control_switch_capture_info,
  164. .get = snd_aw2_control_switch_capture_get,
  165. .put = snd_aw2_control_switch_capture_put
  166. };
  167. /*********************************
  168. * FUNCTION IMPLEMENTATIONS
  169. ********************************/
  170. /* component-destructor */
  171. static void snd_aw2_free(struct snd_card *card)
  172. {
  173. struct aw2 *chip = card->private_data;
  174. /* Free hardware */
  175. snd_aw2_saa7146_free(&chip->saa7146);
  176. }
  177. /* chip-specific constructor */
  178. static int snd_aw2_create(struct snd_card *card,
  179. struct pci_dev *pci)
  180. {
  181. struct aw2 *chip = card->private_data;
  182. int err;
  183. /* initialize the PCI entry */
  184. err = pcim_enable_device(pci);
  185. if (err < 0)
  186. return err;
  187. pci_set_master(pci);
  188. /* check PCI availability (32bit DMA) */
  189. if (dma_set_mask_and_coherent(&pci->dev, DMA_BIT_MASK(32))) {
  190. dev_err(card->dev, "Impossible to set 32bit mask DMA\n");
  191. return -ENXIO;
  192. }
  193. /* initialize the stuff */
  194. chip->card = card;
  195. chip->pci = pci;
  196. chip->irq = -1;
  197. /* (1) PCI resource allocation */
  198. err = pcim_iomap_regions(pci, 1 << 0, "Audiowerk2");
  199. if (err < 0)
  200. return err;
  201. chip->iobase_phys = pci_resource_start(pci, 0);
  202. chip->iobase_virt = pcim_iomap_table(pci)[0];
  203. /* (2) initialization of the chip hardware */
  204. snd_aw2_saa7146_setup(&chip->saa7146, chip->iobase_virt);
  205. if (devm_request_irq(&pci->dev, pci->irq, snd_aw2_saa7146_interrupt,
  206. IRQF_SHARED, KBUILD_MODNAME, chip)) {
  207. dev_err(card->dev, "Cannot grab irq %d\n", pci->irq);
  208. return -EBUSY;
  209. }
  210. chip->irq = pci->irq;
  211. card->sync_irq = chip->irq;
  212. card->private_free = snd_aw2_free;
  213. dev_info(card->dev,
  214. "Audiowerk 2 sound card (saa7146 chipset) detected and managed\n");
  215. return 0;
  216. }
  217. /* constructor */
  218. static int snd_aw2_probe(struct pci_dev *pci,
  219. const struct pci_device_id *pci_id)
  220. {
  221. static int dev;
  222. struct snd_card *card;
  223. struct aw2 *chip;
  224. int err;
  225. /* (1) Continue if device is not enabled, else inc dev */
  226. if (dev >= SNDRV_CARDS)
  227. return -ENODEV;
  228. if (!enable[dev]) {
  229. dev++;
  230. return -ENOENT;
  231. }
  232. /* (2) Create card instance */
  233. err = snd_devm_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
  234. sizeof(*chip), &card);
  235. if (err < 0)
  236. return err;
  237. chip = card->private_data;
  238. /* (3) Create main component */
  239. err = snd_aw2_create(card, pci);
  240. if (err < 0)
  241. goto error;
  242. /* initialize mutex */
  243. mutex_init(&chip->mtx);
  244. /* init spinlock */
  245. spin_lock_init(&chip->reg_lock);
  246. /* (4) Define driver ID and name string */
  247. strcpy(card->driver, "aw2");
  248. strcpy(card->shortname, "Audiowerk2");
  249. sprintf(card->longname, "%s with SAA7146 irq %i",
  250. card->shortname, chip->irq);
  251. /* (5) Create other components */
  252. snd_aw2_new_pcm(chip);
  253. /* (6) Register card instance */
  254. err = snd_card_register(card);
  255. if (err < 0)
  256. goto error;
  257. /* (7) Set PCI driver data */
  258. pci_set_drvdata(pci, card);
  259. dev++;
  260. return 0;
  261. error:
  262. snd_card_free(card);
  263. return err;
  264. }
  265. /* open callback */
  266. static int snd_aw2_pcm_playback_open(struct snd_pcm_substream *substream)
  267. {
  268. struct snd_pcm_runtime *runtime = substream->runtime;
  269. dev_dbg(substream->pcm->card->dev, "Playback_open\n");
  270. runtime->hw = snd_aw2_playback_hw;
  271. return 0;
  272. }
  273. /* close callback */
  274. static int snd_aw2_pcm_playback_close(struct snd_pcm_substream *substream)
  275. {
  276. return 0;
  277. }
  278. static int snd_aw2_pcm_capture_open(struct snd_pcm_substream *substream)
  279. {
  280. struct snd_pcm_runtime *runtime = substream->runtime;
  281. dev_dbg(substream->pcm->card->dev, "Capture_open\n");
  282. runtime->hw = snd_aw2_capture_hw;
  283. return 0;
  284. }
  285. /* close callback */
  286. static int snd_aw2_pcm_capture_close(struct snd_pcm_substream *substream)
  287. {
  288. /* TODO: something to do ? */
  289. return 0;
  290. }
  291. /* prepare callback for playback */
  292. static int snd_aw2_pcm_prepare_playback(struct snd_pcm_substream *substream)
  293. {
  294. struct aw2_pcm_device *pcm_device = snd_pcm_substream_chip(substream);
  295. struct aw2 *chip = pcm_device->chip;
  296. struct snd_pcm_runtime *runtime = substream->runtime;
  297. unsigned long period_size, buffer_size;
  298. mutex_lock(&chip->mtx);
  299. period_size = snd_pcm_lib_period_bytes(substream);
  300. buffer_size = snd_pcm_lib_buffer_bytes(substream);
  301. snd_aw2_saa7146_pcm_init_playback(&chip->saa7146,
  302. pcm_device->stream_number,
  303. runtime->dma_addr, period_size,
  304. buffer_size);
  305. /* Define Interrupt callback */
  306. snd_aw2_saa7146_define_it_playback_callback(pcm_device->stream_number,
  307. (snd_aw2_saa7146_it_cb)
  308. snd_pcm_period_elapsed,
  309. (void *)substream);
  310. mutex_unlock(&chip->mtx);
  311. return 0;
  312. }
  313. /* prepare callback for capture */
  314. static int snd_aw2_pcm_prepare_capture(struct snd_pcm_substream *substream)
  315. {
  316. struct aw2_pcm_device *pcm_device = snd_pcm_substream_chip(substream);
  317. struct aw2 *chip = pcm_device->chip;
  318. struct snd_pcm_runtime *runtime = substream->runtime;
  319. unsigned long period_size, buffer_size;
  320. mutex_lock(&chip->mtx);
  321. period_size = snd_pcm_lib_period_bytes(substream);
  322. buffer_size = snd_pcm_lib_buffer_bytes(substream);
  323. snd_aw2_saa7146_pcm_init_capture(&chip->saa7146,
  324. pcm_device->stream_number,
  325. runtime->dma_addr, period_size,
  326. buffer_size);
  327. /* Define Interrupt callback */
  328. snd_aw2_saa7146_define_it_capture_callback(pcm_device->stream_number,
  329. (snd_aw2_saa7146_it_cb)
  330. snd_pcm_period_elapsed,
  331. (void *)substream);
  332. mutex_unlock(&chip->mtx);
  333. return 0;
  334. }
  335. /* playback trigger callback */
  336. static int snd_aw2_pcm_trigger_playback(struct snd_pcm_substream *substream,
  337. int cmd)
  338. {
  339. int status = 0;
  340. struct aw2_pcm_device *pcm_device = snd_pcm_substream_chip(substream);
  341. struct aw2 *chip = pcm_device->chip;
  342. spin_lock(&chip->reg_lock);
  343. switch (cmd) {
  344. case SNDRV_PCM_TRIGGER_START:
  345. snd_aw2_saa7146_pcm_trigger_start_playback(&chip->saa7146,
  346. pcm_device->
  347. stream_number);
  348. break;
  349. case SNDRV_PCM_TRIGGER_STOP:
  350. snd_aw2_saa7146_pcm_trigger_stop_playback(&chip->saa7146,
  351. pcm_device->
  352. stream_number);
  353. break;
  354. default:
  355. status = -EINVAL;
  356. }
  357. spin_unlock(&chip->reg_lock);
  358. return status;
  359. }
  360. /* capture trigger callback */
  361. static int snd_aw2_pcm_trigger_capture(struct snd_pcm_substream *substream,
  362. int cmd)
  363. {
  364. int status = 0;
  365. struct aw2_pcm_device *pcm_device = snd_pcm_substream_chip(substream);
  366. struct aw2 *chip = pcm_device->chip;
  367. spin_lock(&chip->reg_lock);
  368. switch (cmd) {
  369. case SNDRV_PCM_TRIGGER_START:
  370. snd_aw2_saa7146_pcm_trigger_start_capture(&chip->saa7146,
  371. pcm_device->
  372. stream_number);
  373. break;
  374. case SNDRV_PCM_TRIGGER_STOP:
  375. snd_aw2_saa7146_pcm_trigger_stop_capture(&chip->saa7146,
  376. pcm_device->
  377. stream_number);
  378. break;
  379. default:
  380. status = -EINVAL;
  381. }
  382. spin_unlock(&chip->reg_lock);
  383. return status;
  384. }
  385. /* playback pointer callback */
  386. static snd_pcm_uframes_t snd_aw2_pcm_pointer_playback(struct snd_pcm_substream
  387. *substream)
  388. {
  389. struct aw2_pcm_device *pcm_device = snd_pcm_substream_chip(substream);
  390. struct aw2 *chip = pcm_device->chip;
  391. unsigned int current_ptr;
  392. /* get the current hardware pointer */
  393. struct snd_pcm_runtime *runtime = substream->runtime;
  394. current_ptr =
  395. snd_aw2_saa7146_get_hw_ptr_playback(&chip->saa7146,
  396. pcm_device->stream_number,
  397. runtime->dma_area,
  398. runtime->buffer_size);
  399. return bytes_to_frames(substream->runtime, current_ptr);
  400. }
  401. /* capture pointer callback */
  402. static snd_pcm_uframes_t snd_aw2_pcm_pointer_capture(struct snd_pcm_substream
  403. *substream)
  404. {
  405. struct aw2_pcm_device *pcm_device = snd_pcm_substream_chip(substream);
  406. struct aw2 *chip = pcm_device->chip;
  407. unsigned int current_ptr;
  408. /* get the current hardware pointer */
  409. struct snd_pcm_runtime *runtime = substream->runtime;
  410. current_ptr =
  411. snd_aw2_saa7146_get_hw_ptr_capture(&chip->saa7146,
  412. pcm_device->stream_number,
  413. runtime->dma_area,
  414. runtime->buffer_size);
  415. return bytes_to_frames(substream->runtime, current_ptr);
  416. }
  417. /* create a pcm device */
  418. static int snd_aw2_new_pcm(struct aw2 *chip)
  419. {
  420. struct snd_pcm *pcm_playback_ana;
  421. struct snd_pcm *pcm_playback_num;
  422. struct snd_pcm *pcm_capture;
  423. struct aw2_pcm_device *pcm_device;
  424. int err = 0;
  425. /* Create new Alsa PCM device */
  426. err = snd_pcm_new(chip->card, "Audiowerk2 analog playback", 0, 1, 0,
  427. &pcm_playback_ana);
  428. if (err < 0) {
  429. dev_err(chip->card->dev, "snd_pcm_new error (0x%X)\n", err);
  430. return err;
  431. }
  432. /* Creation ok */
  433. pcm_device = &chip->device_playback[NUM_STREAM_PLAYBACK_ANA];
  434. /* Set PCM device name */
  435. strcpy(pcm_playback_ana->name, "Analog playback");
  436. /* Associate private data to PCM device */
  437. pcm_playback_ana->private_data = pcm_device;
  438. /* set operators of PCM device */
  439. snd_pcm_set_ops(pcm_playback_ana, SNDRV_PCM_STREAM_PLAYBACK,
  440. &snd_aw2_playback_ops);
  441. /* store PCM device */
  442. pcm_device->pcm = pcm_playback_ana;
  443. /* give base chip pointer to our internal pcm device
  444. structure */
  445. pcm_device->chip = chip;
  446. /* Give stream number to PCM device */
  447. pcm_device->stream_number = NUM_STREAM_PLAYBACK_ANA;
  448. /* pre-allocation of buffers */
  449. /* Preallocate continuous pages. */
  450. snd_pcm_set_managed_buffer_all(pcm_playback_ana,
  451. SNDRV_DMA_TYPE_DEV,
  452. &chip->pci->dev,
  453. 64 * 1024, 64 * 1024);
  454. err = snd_pcm_new(chip->card, "Audiowerk2 digital playback", 1, 1, 0,
  455. &pcm_playback_num);
  456. if (err < 0) {
  457. dev_err(chip->card->dev, "snd_pcm_new error (0x%X)\n", err);
  458. return err;
  459. }
  460. /* Creation ok */
  461. pcm_device = &chip->device_playback[NUM_STREAM_PLAYBACK_DIG];
  462. /* Set PCM device name */
  463. strcpy(pcm_playback_num->name, "Digital playback");
  464. /* Associate private data to PCM device */
  465. pcm_playback_num->private_data = pcm_device;
  466. /* set operators of PCM device */
  467. snd_pcm_set_ops(pcm_playback_num, SNDRV_PCM_STREAM_PLAYBACK,
  468. &snd_aw2_playback_ops);
  469. /* store PCM device */
  470. pcm_device->pcm = pcm_playback_num;
  471. /* give base chip pointer to our internal pcm device
  472. structure */
  473. pcm_device->chip = chip;
  474. /* Give stream number to PCM device */
  475. pcm_device->stream_number = NUM_STREAM_PLAYBACK_DIG;
  476. /* pre-allocation of buffers */
  477. /* Preallocate continuous pages. */
  478. snd_pcm_set_managed_buffer_all(pcm_playback_num,
  479. SNDRV_DMA_TYPE_DEV,
  480. &chip->pci->dev,
  481. 64 * 1024, 64 * 1024);
  482. err = snd_pcm_new(chip->card, "Audiowerk2 capture", 2, 0, 1,
  483. &pcm_capture);
  484. if (err < 0) {
  485. dev_err(chip->card->dev, "snd_pcm_new error (0x%X)\n", err);
  486. return err;
  487. }
  488. /* Creation ok */
  489. pcm_device = &chip->device_capture[NUM_STREAM_CAPTURE_ANA];
  490. /* Set PCM device name */
  491. strcpy(pcm_capture->name, "Capture");
  492. /* Associate private data to PCM device */
  493. pcm_capture->private_data = pcm_device;
  494. /* set operators of PCM device */
  495. snd_pcm_set_ops(pcm_capture, SNDRV_PCM_STREAM_CAPTURE,
  496. &snd_aw2_capture_ops);
  497. /* store PCM device */
  498. pcm_device->pcm = pcm_capture;
  499. /* give base chip pointer to our internal pcm device
  500. structure */
  501. pcm_device->chip = chip;
  502. /* Give stream number to PCM device */
  503. pcm_device->stream_number = NUM_STREAM_CAPTURE_ANA;
  504. /* pre-allocation of buffers */
  505. /* Preallocate continuous pages. */
  506. snd_pcm_set_managed_buffer_all(pcm_capture,
  507. SNDRV_DMA_TYPE_DEV,
  508. &chip->pci->dev,
  509. 64 * 1024, 64 * 1024);
  510. /* Create control */
  511. err = snd_ctl_add(chip->card, snd_ctl_new1(&aw2_control, chip));
  512. if (err < 0) {
  513. dev_err(chip->card->dev, "snd_ctl_add error (0x%X)\n", err);
  514. return err;
  515. }
  516. return 0;
  517. }
  518. static int snd_aw2_control_switch_capture_info(struct snd_kcontrol *kcontrol,
  519. struct snd_ctl_elem_info *uinfo)
  520. {
  521. static const char * const texts[2] = {
  522. "Analog", "Digital"
  523. };
  524. return snd_ctl_enum_info(uinfo, 1, 2, texts);
  525. }
  526. static int snd_aw2_control_switch_capture_get(struct snd_kcontrol *kcontrol,
  527. struct snd_ctl_elem_value
  528. *ucontrol)
  529. {
  530. struct aw2 *chip = snd_kcontrol_chip(kcontrol);
  531. if (snd_aw2_saa7146_is_using_digital_input(&chip->saa7146))
  532. ucontrol->value.enumerated.item[0] = CTL_ROUTE_DIGITAL;
  533. else
  534. ucontrol->value.enumerated.item[0] = CTL_ROUTE_ANALOG;
  535. return 0;
  536. }
  537. static int snd_aw2_control_switch_capture_put(struct snd_kcontrol *kcontrol,
  538. struct snd_ctl_elem_value
  539. *ucontrol)
  540. {
  541. struct aw2 *chip = snd_kcontrol_chip(kcontrol);
  542. int changed = 0;
  543. int is_disgital =
  544. snd_aw2_saa7146_is_using_digital_input(&chip->saa7146);
  545. if (((ucontrol->value.integer.value[0] == CTL_ROUTE_DIGITAL)
  546. && !is_disgital)
  547. || ((ucontrol->value.integer.value[0] == CTL_ROUTE_ANALOG)
  548. && is_disgital)) {
  549. snd_aw2_saa7146_use_digital_input(&chip->saa7146, !is_disgital);
  550. changed = 1;
  551. }
  552. return changed;
  553. }