aw2-alsa.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  1. /*****************************************************************************
  2. *
  3. * Copyright (C) 2008 Cedric Bregardis <cedric.bregardis@free.fr> and
  4. * Jean-Christian Hassler <jhassler@free.fr>
  5. *
  6. * This file is part of the Audiowerk2 ALSA driver
  7. *
  8. * The Audiowerk2 ALSA driver is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; version 2.
  11. *
  12. * The Audiowerk2 ALSA driver is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with the Audiowerk2 ALSA driver; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  20. * USA.
  21. *
  22. *****************************************************************************/
  23. #include <linux/init.h>
  24. #include <linux/pci.h>
  25. #include <linux/dma-mapping.h>
  26. #include <linux/slab.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/delay.h>
  29. #include <linux/io.h>
  30. #include <linux/module.h>
  31. #include <sound/core.h>
  32. #include <sound/initval.h>
  33. #include <sound/pcm.h>
  34. #include <sound/pcm_params.h>
  35. #include <sound/control.h>
  36. #include "saa7146.h"
  37. #include "aw2-saa7146.h"
  38. MODULE_AUTHOR("Cedric Bregardis <cedric.bregardis@free.fr>, "
  39. "Jean-Christian Hassler <jhassler@free.fr>");
  40. MODULE_DESCRIPTION("Emagic Audiowerk 2 sound driver");
  41. MODULE_LICENSE("GPL");
  42. /*********************************
  43. * DEFINES
  44. ********************************/
  45. #define CTL_ROUTE_ANALOG 0
  46. #define CTL_ROUTE_DIGITAL 1
  47. /*********************************
  48. * TYPEDEFS
  49. ********************************/
  50. /* hardware definition */
  51. static struct snd_pcm_hardware snd_aw2_playback_hw = {
  52. .info = (SNDRV_PCM_INFO_MMAP |
  53. SNDRV_PCM_INFO_INTERLEAVED |
  54. SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_MMAP_VALID),
  55. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  56. .rates = SNDRV_PCM_RATE_44100,
  57. .rate_min = 44100,
  58. .rate_max = 44100,
  59. .channels_min = 2,
  60. .channels_max = 4,
  61. .buffer_bytes_max = 32768,
  62. .period_bytes_min = 4096,
  63. .period_bytes_max = 32768,
  64. .periods_min = 1,
  65. .periods_max = 1024,
  66. };
  67. static struct snd_pcm_hardware snd_aw2_capture_hw = {
  68. .info = (SNDRV_PCM_INFO_MMAP |
  69. SNDRV_PCM_INFO_INTERLEAVED |
  70. SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_MMAP_VALID),
  71. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  72. .rates = SNDRV_PCM_RATE_44100,
  73. .rate_min = 44100,
  74. .rate_max = 44100,
  75. .channels_min = 2,
  76. .channels_max = 2,
  77. .buffer_bytes_max = 32768,
  78. .period_bytes_min = 4096,
  79. .period_bytes_max = 32768,
  80. .periods_min = 1,
  81. .periods_max = 1024,
  82. };
  83. struct aw2_pcm_device {
  84. struct snd_pcm *pcm;
  85. unsigned int stream_number;
  86. struct aw2 *chip;
  87. };
  88. struct aw2 {
  89. struct snd_aw2_saa7146 saa7146;
  90. struct pci_dev *pci;
  91. int irq;
  92. spinlock_t reg_lock;
  93. struct mutex mtx;
  94. unsigned long iobase_phys;
  95. void __iomem *iobase_virt;
  96. struct snd_card *card;
  97. struct aw2_pcm_device device_playback[NB_STREAM_PLAYBACK];
  98. struct aw2_pcm_device device_capture[NB_STREAM_CAPTURE];
  99. };
  100. /*********************************
  101. * FUNCTION DECLARATIONS
  102. ********************************/
  103. static int snd_aw2_dev_free(struct snd_device *device);
  104. static int snd_aw2_create(struct snd_card *card,
  105. struct pci_dev *pci, struct aw2 **rchip);
  106. static int snd_aw2_probe(struct pci_dev *pci,
  107. const struct pci_device_id *pci_id);
  108. static void snd_aw2_remove(struct pci_dev *pci);
  109. static int snd_aw2_pcm_playback_open(struct snd_pcm_substream *substream);
  110. static int snd_aw2_pcm_playback_close(struct snd_pcm_substream *substream);
  111. static int snd_aw2_pcm_capture_open(struct snd_pcm_substream *substream);
  112. static int snd_aw2_pcm_capture_close(struct snd_pcm_substream *substream);
  113. static int snd_aw2_pcm_hw_params(struct snd_pcm_substream *substream,
  114. struct snd_pcm_hw_params *hw_params);
  115. static int snd_aw2_pcm_hw_free(struct snd_pcm_substream *substream);
  116. static int snd_aw2_pcm_prepare_playback(struct snd_pcm_substream *substream);
  117. static int snd_aw2_pcm_prepare_capture(struct snd_pcm_substream *substream);
  118. static int snd_aw2_pcm_trigger_playback(struct snd_pcm_substream *substream,
  119. int cmd);
  120. static int snd_aw2_pcm_trigger_capture(struct snd_pcm_substream *substream,
  121. int cmd);
  122. static snd_pcm_uframes_t snd_aw2_pcm_pointer_playback(struct snd_pcm_substream
  123. *substream);
  124. static snd_pcm_uframes_t snd_aw2_pcm_pointer_capture(struct snd_pcm_substream
  125. *substream);
  126. static int snd_aw2_new_pcm(struct aw2 *chip);
  127. static int snd_aw2_control_switch_capture_info(struct snd_kcontrol *kcontrol,
  128. struct snd_ctl_elem_info *uinfo);
  129. static int snd_aw2_control_switch_capture_get(struct snd_kcontrol *kcontrol,
  130. struct snd_ctl_elem_value
  131. *ucontrol);
  132. static int snd_aw2_control_switch_capture_put(struct snd_kcontrol *kcontrol,
  133. struct snd_ctl_elem_value
  134. *ucontrol);
  135. /*********************************
  136. * VARIABLES
  137. ********************************/
  138. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
  139. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
  140. static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
  141. module_param_array(index, int, NULL, 0444);
  142. MODULE_PARM_DESC(index, "Index value for Audiowerk2 soundcard.");
  143. module_param_array(id, charp, NULL, 0444);
  144. MODULE_PARM_DESC(id, "ID string for the Audiowerk2 soundcard.");
  145. module_param_array(enable, bool, NULL, 0444);
  146. MODULE_PARM_DESC(enable, "Enable Audiowerk2 soundcard.");
  147. static const struct pci_device_id snd_aw2_ids[] = {
  148. {PCI_VENDOR_ID_PHILIPS, PCI_DEVICE_ID_PHILIPS_SAA7146, 0, 0,
  149. 0, 0, 0},
  150. {0}
  151. };
  152. MODULE_DEVICE_TABLE(pci, snd_aw2_ids);
  153. /* pci_driver definition */
  154. static struct pci_driver aw2_driver = {
  155. .name = KBUILD_MODNAME,
  156. .id_table = snd_aw2_ids,
  157. .probe = snd_aw2_probe,
  158. .remove = snd_aw2_remove,
  159. };
  160. module_pci_driver(aw2_driver);
  161. /* operators for playback PCM alsa interface */
  162. static const struct snd_pcm_ops snd_aw2_playback_ops = {
  163. .open = snd_aw2_pcm_playback_open,
  164. .close = snd_aw2_pcm_playback_close,
  165. .ioctl = snd_pcm_lib_ioctl,
  166. .hw_params = snd_aw2_pcm_hw_params,
  167. .hw_free = snd_aw2_pcm_hw_free,
  168. .prepare = snd_aw2_pcm_prepare_playback,
  169. .trigger = snd_aw2_pcm_trigger_playback,
  170. .pointer = snd_aw2_pcm_pointer_playback,
  171. };
  172. /* operators for capture PCM alsa interface */
  173. static const struct snd_pcm_ops snd_aw2_capture_ops = {
  174. .open = snd_aw2_pcm_capture_open,
  175. .close = snd_aw2_pcm_capture_close,
  176. .ioctl = snd_pcm_lib_ioctl,
  177. .hw_params = snd_aw2_pcm_hw_params,
  178. .hw_free = snd_aw2_pcm_hw_free,
  179. .prepare = snd_aw2_pcm_prepare_capture,
  180. .trigger = snd_aw2_pcm_trigger_capture,
  181. .pointer = snd_aw2_pcm_pointer_capture,
  182. };
  183. static struct snd_kcontrol_new aw2_control = {
  184. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  185. .name = "PCM Capture Route",
  186. .index = 0,
  187. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  188. .private_value = 0xffff,
  189. .info = snd_aw2_control_switch_capture_info,
  190. .get = snd_aw2_control_switch_capture_get,
  191. .put = snd_aw2_control_switch_capture_put
  192. };
  193. /*********************************
  194. * FUNCTION IMPLEMENTATIONS
  195. ********************************/
  196. /* component-destructor */
  197. static int snd_aw2_dev_free(struct snd_device *device)
  198. {
  199. struct aw2 *chip = device->device_data;
  200. /* Free hardware */
  201. snd_aw2_saa7146_free(&chip->saa7146);
  202. /* release the irq */
  203. if (chip->irq >= 0)
  204. free_irq(chip->irq, (void *)chip);
  205. /* release the i/o ports & memory */
  206. iounmap(chip->iobase_virt);
  207. pci_release_regions(chip->pci);
  208. /* disable the PCI entry */
  209. pci_disable_device(chip->pci);
  210. /* release the data */
  211. kfree(chip);
  212. return 0;
  213. }
  214. /* chip-specific constructor */
  215. static int snd_aw2_create(struct snd_card *card,
  216. struct pci_dev *pci, struct aw2 **rchip)
  217. {
  218. struct aw2 *chip;
  219. int err;
  220. static struct snd_device_ops ops = {
  221. .dev_free = snd_aw2_dev_free,
  222. };
  223. *rchip = NULL;
  224. /* initialize the PCI entry */
  225. err = pci_enable_device(pci);
  226. if (err < 0)
  227. return err;
  228. pci_set_master(pci);
  229. /* check PCI availability (32bit DMA) */
  230. if ((dma_set_mask(&pci->dev, DMA_BIT_MASK(32)) < 0) ||
  231. (dma_set_coherent_mask(&pci->dev, DMA_BIT_MASK(32)) < 0)) {
  232. dev_err(card->dev, "Impossible to set 32bit mask DMA\n");
  233. pci_disable_device(pci);
  234. return -ENXIO;
  235. }
  236. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  237. if (chip == NULL) {
  238. pci_disable_device(pci);
  239. return -ENOMEM;
  240. }
  241. /* initialize the stuff */
  242. chip->card = card;
  243. chip->pci = pci;
  244. chip->irq = -1;
  245. /* (1) PCI resource allocation */
  246. err = pci_request_regions(pci, "Audiowerk2");
  247. if (err < 0) {
  248. pci_disable_device(pci);
  249. kfree(chip);
  250. return err;
  251. }
  252. chip->iobase_phys = pci_resource_start(pci, 0);
  253. chip->iobase_virt =
  254. ioremap_nocache(chip->iobase_phys,
  255. pci_resource_len(pci, 0));
  256. if (chip->iobase_virt == NULL) {
  257. dev_err(card->dev, "unable to remap memory region");
  258. pci_release_regions(pci);
  259. pci_disable_device(pci);
  260. kfree(chip);
  261. return -ENOMEM;
  262. }
  263. /* (2) initialization of the chip hardware */
  264. snd_aw2_saa7146_setup(&chip->saa7146, chip->iobase_virt);
  265. if (request_irq(pci->irq, snd_aw2_saa7146_interrupt,
  266. IRQF_SHARED, KBUILD_MODNAME, chip)) {
  267. dev_err(card->dev, "Cannot grab irq %d\n", pci->irq);
  268. iounmap(chip->iobase_virt);
  269. pci_release_regions(chip->pci);
  270. pci_disable_device(chip->pci);
  271. kfree(chip);
  272. return -EBUSY;
  273. }
  274. chip->irq = pci->irq;
  275. err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
  276. if (err < 0) {
  277. free_irq(chip->irq, (void *)chip);
  278. iounmap(chip->iobase_virt);
  279. pci_release_regions(chip->pci);
  280. pci_disable_device(chip->pci);
  281. kfree(chip);
  282. return err;
  283. }
  284. *rchip = chip;
  285. dev_info(card->dev,
  286. "Audiowerk 2 sound card (saa7146 chipset) detected and managed\n");
  287. return 0;
  288. }
  289. /* constructor */
  290. static int snd_aw2_probe(struct pci_dev *pci,
  291. const struct pci_device_id *pci_id)
  292. {
  293. static int dev;
  294. struct snd_card *card;
  295. struct aw2 *chip;
  296. int err;
  297. /* (1) Continue if device is not enabled, else inc dev */
  298. if (dev >= SNDRV_CARDS)
  299. return -ENODEV;
  300. if (!enable[dev]) {
  301. dev++;
  302. return -ENOENT;
  303. }
  304. /* (2) Create card instance */
  305. err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
  306. 0, &card);
  307. if (err < 0)
  308. return err;
  309. /* (3) Create main component */
  310. err = snd_aw2_create(card, pci, &chip);
  311. if (err < 0) {
  312. snd_card_free(card);
  313. return err;
  314. }
  315. /* initialize mutex */
  316. mutex_init(&chip->mtx);
  317. /* init spinlock */
  318. spin_lock_init(&chip->reg_lock);
  319. /* (4) Define driver ID and name string */
  320. strcpy(card->driver, "aw2");
  321. strcpy(card->shortname, "Audiowerk2");
  322. sprintf(card->longname, "%s with SAA7146 irq %i",
  323. card->shortname, chip->irq);
  324. /* (5) Create other components */
  325. snd_aw2_new_pcm(chip);
  326. /* (6) Register card instance */
  327. err = snd_card_register(card);
  328. if (err < 0) {
  329. snd_card_free(card);
  330. return err;
  331. }
  332. /* (7) Set PCI driver data */
  333. pci_set_drvdata(pci, card);
  334. dev++;
  335. return 0;
  336. }
  337. /* destructor */
  338. static void snd_aw2_remove(struct pci_dev *pci)
  339. {
  340. snd_card_free(pci_get_drvdata(pci));
  341. }
  342. /* open callback */
  343. static int snd_aw2_pcm_playback_open(struct snd_pcm_substream *substream)
  344. {
  345. struct snd_pcm_runtime *runtime = substream->runtime;
  346. dev_dbg(substream->pcm->card->dev, "Playback_open\n");
  347. runtime->hw = snd_aw2_playback_hw;
  348. return 0;
  349. }
  350. /* close callback */
  351. static int snd_aw2_pcm_playback_close(struct snd_pcm_substream *substream)
  352. {
  353. return 0;
  354. }
  355. static int snd_aw2_pcm_capture_open(struct snd_pcm_substream *substream)
  356. {
  357. struct snd_pcm_runtime *runtime = substream->runtime;
  358. dev_dbg(substream->pcm->card->dev, "Capture_open\n");
  359. runtime->hw = snd_aw2_capture_hw;
  360. return 0;
  361. }
  362. /* close callback */
  363. static int snd_aw2_pcm_capture_close(struct snd_pcm_substream *substream)
  364. {
  365. /* TODO: something to do ? */
  366. return 0;
  367. }
  368. /* hw_params callback */
  369. static int snd_aw2_pcm_hw_params(struct snd_pcm_substream *substream,
  370. struct snd_pcm_hw_params *hw_params)
  371. {
  372. return snd_pcm_lib_malloc_pages(substream,
  373. params_buffer_bytes(hw_params));
  374. }
  375. /* hw_free callback */
  376. static int snd_aw2_pcm_hw_free(struct snd_pcm_substream *substream)
  377. {
  378. return snd_pcm_lib_free_pages(substream);
  379. }
  380. /* prepare callback for playback */
  381. static int snd_aw2_pcm_prepare_playback(struct snd_pcm_substream *substream)
  382. {
  383. struct aw2_pcm_device *pcm_device = snd_pcm_substream_chip(substream);
  384. struct aw2 *chip = pcm_device->chip;
  385. struct snd_pcm_runtime *runtime = substream->runtime;
  386. unsigned long period_size, buffer_size;
  387. mutex_lock(&chip->mtx);
  388. period_size = snd_pcm_lib_period_bytes(substream);
  389. buffer_size = snd_pcm_lib_buffer_bytes(substream);
  390. snd_aw2_saa7146_pcm_init_playback(&chip->saa7146,
  391. pcm_device->stream_number,
  392. runtime->dma_addr, period_size,
  393. buffer_size);
  394. /* Define Interrupt callback */
  395. snd_aw2_saa7146_define_it_playback_callback(pcm_device->stream_number,
  396. (snd_aw2_saa7146_it_cb)
  397. snd_pcm_period_elapsed,
  398. (void *)substream);
  399. mutex_unlock(&chip->mtx);
  400. return 0;
  401. }
  402. /* prepare callback for capture */
  403. static int snd_aw2_pcm_prepare_capture(struct snd_pcm_substream *substream)
  404. {
  405. struct aw2_pcm_device *pcm_device = snd_pcm_substream_chip(substream);
  406. struct aw2 *chip = pcm_device->chip;
  407. struct snd_pcm_runtime *runtime = substream->runtime;
  408. unsigned long period_size, buffer_size;
  409. mutex_lock(&chip->mtx);
  410. period_size = snd_pcm_lib_period_bytes(substream);
  411. buffer_size = snd_pcm_lib_buffer_bytes(substream);
  412. snd_aw2_saa7146_pcm_init_capture(&chip->saa7146,
  413. pcm_device->stream_number,
  414. runtime->dma_addr, period_size,
  415. buffer_size);
  416. /* Define Interrupt callback */
  417. snd_aw2_saa7146_define_it_capture_callback(pcm_device->stream_number,
  418. (snd_aw2_saa7146_it_cb)
  419. snd_pcm_period_elapsed,
  420. (void *)substream);
  421. mutex_unlock(&chip->mtx);
  422. return 0;
  423. }
  424. /* playback trigger callback */
  425. static int snd_aw2_pcm_trigger_playback(struct snd_pcm_substream *substream,
  426. int cmd)
  427. {
  428. int status = 0;
  429. struct aw2_pcm_device *pcm_device = snd_pcm_substream_chip(substream);
  430. struct aw2 *chip = pcm_device->chip;
  431. spin_lock(&chip->reg_lock);
  432. switch (cmd) {
  433. case SNDRV_PCM_TRIGGER_START:
  434. snd_aw2_saa7146_pcm_trigger_start_playback(&chip->saa7146,
  435. pcm_device->
  436. stream_number);
  437. break;
  438. case SNDRV_PCM_TRIGGER_STOP:
  439. snd_aw2_saa7146_pcm_trigger_stop_playback(&chip->saa7146,
  440. pcm_device->
  441. stream_number);
  442. break;
  443. default:
  444. status = -EINVAL;
  445. }
  446. spin_unlock(&chip->reg_lock);
  447. return status;
  448. }
  449. /* capture trigger callback */
  450. static int snd_aw2_pcm_trigger_capture(struct snd_pcm_substream *substream,
  451. int cmd)
  452. {
  453. int status = 0;
  454. struct aw2_pcm_device *pcm_device = snd_pcm_substream_chip(substream);
  455. struct aw2 *chip = pcm_device->chip;
  456. spin_lock(&chip->reg_lock);
  457. switch (cmd) {
  458. case SNDRV_PCM_TRIGGER_START:
  459. snd_aw2_saa7146_pcm_trigger_start_capture(&chip->saa7146,
  460. pcm_device->
  461. stream_number);
  462. break;
  463. case SNDRV_PCM_TRIGGER_STOP:
  464. snd_aw2_saa7146_pcm_trigger_stop_capture(&chip->saa7146,
  465. pcm_device->
  466. stream_number);
  467. break;
  468. default:
  469. status = -EINVAL;
  470. }
  471. spin_unlock(&chip->reg_lock);
  472. return status;
  473. }
  474. /* playback pointer callback */
  475. static snd_pcm_uframes_t snd_aw2_pcm_pointer_playback(struct snd_pcm_substream
  476. *substream)
  477. {
  478. struct aw2_pcm_device *pcm_device = snd_pcm_substream_chip(substream);
  479. struct aw2 *chip = pcm_device->chip;
  480. unsigned int current_ptr;
  481. /* get the current hardware pointer */
  482. struct snd_pcm_runtime *runtime = substream->runtime;
  483. current_ptr =
  484. snd_aw2_saa7146_get_hw_ptr_playback(&chip->saa7146,
  485. pcm_device->stream_number,
  486. runtime->dma_area,
  487. runtime->buffer_size);
  488. return bytes_to_frames(substream->runtime, current_ptr);
  489. }
  490. /* capture pointer callback */
  491. static snd_pcm_uframes_t snd_aw2_pcm_pointer_capture(struct snd_pcm_substream
  492. *substream)
  493. {
  494. struct aw2_pcm_device *pcm_device = snd_pcm_substream_chip(substream);
  495. struct aw2 *chip = pcm_device->chip;
  496. unsigned int current_ptr;
  497. /* get the current hardware pointer */
  498. struct snd_pcm_runtime *runtime = substream->runtime;
  499. current_ptr =
  500. snd_aw2_saa7146_get_hw_ptr_capture(&chip->saa7146,
  501. pcm_device->stream_number,
  502. runtime->dma_area,
  503. runtime->buffer_size);
  504. return bytes_to_frames(substream->runtime, current_ptr);
  505. }
  506. /* create a pcm device */
  507. static int snd_aw2_new_pcm(struct aw2 *chip)
  508. {
  509. struct snd_pcm *pcm_playback_ana;
  510. struct snd_pcm *pcm_playback_num;
  511. struct snd_pcm *pcm_capture;
  512. struct aw2_pcm_device *pcm_device;
  513. int err = 0;
  514. /* Create new Alsa PCM device */
  515. err = snd_pcm_new(chip->card, "Audiowerk2 analog playback", 0, 1, 0,
  516. &pcm_playback_ana);
  517. if (err < 0) {
  518. dev_err(chip->card->dev, "snd_pcm_new error (0x%X)\n", err);
  519. return err;
  520. }
  521. /* Creation ok */
  522. pcm_device = &chip->device_playback[NUM_STREAM_PLAYBACK_ANA];
  523. /* Set PCM device name */
  524. strcpy(pcm_playback_ana->name, "Analog playback");
  525. /* Associate private data to PCM device */
  526. pcm_playback_ana->private_data = pcm_device;
  527. /* set operators of PCM device */
  528. snd_pcm_set_ops(pcm_playback_ana, SNDRV_PCM_STREAM_PLAYBACK,
  529. &snd_aw2_playback_ops);
  530. /* store PCM device */
  531. pcm_device->pcm = pcm_playback_ana;
  532. /* give base chip pointer to our internal pcm device
  533. structure */
  534. pcm_device->chip = chip;
  535. /* Give stream number to PCM device */
  536. pcm_device->stream_number = NUM_STREAM_PLAYBACK_ANA;
  537. /* pre-allocation of buffers */
  538. /* Preallocate continuous pages. */
  539. err = snd_pcm_lib_preallocate_pages_for_all(pcm_playback_ana,
  540. SNDRV_DMA_TYPE_DEV,
  541. snd_dma_pci_data
  542. (chip->pci),
  543. 64 * 1024, 64 * 1024);
  544. if (err)
  545. dev_err(chip->card->dev,
  546. "snd_pcm_lib_preallocate_pages_for_all error (0x%X)\n",
  547. err);
  548. err = snd_pcm_new(chip->card, "Audiowerk2 digital playback", 1, 1, 0,
  549. &pcm_playback_num);
  550. if (err < 0) {
  551. dev_err(chip->card->dev, "snd_pcm_new error (0x%X)\n", err);
  552. return err;
  553. }
  554. /* Creation ok */
  555. pcm_device = &chip->device_playback[NUM_STREAM_PLAYBACK_DIG];
  556. /* Set PCM device name */
  557. strcpy(pcm_playback_num->name, "Digital playback");
  558. /* Associate private data to PCM device */
  559. pcm_playback_num->private_data = pcm_device;
  560. /* set operators of PCM device */
  561. snd_pcm_set_ops(pcm_playback_num, SNDRV_PCM_STREAM_PLAYBACK,
  562. &snd_aw2_playback_ops);
  563. /* store PCM device */
  564. pcm_device->pcm = pcm_playback_num;
  565. /* give base chip pointer to our internal pcm device
  566. structure */
  567. pcm_device->chip = chip;
  568. /* Give stream number to PCM device */
  569. pcm_device->stream_number = NUM_STREAM_PLAYBACK_DIG;
  570. /* pre-allocation of buffers */
  571. /* Preallocate continuous pages. */
  572. err = snd_pcm_lib_preallocate_pages_for_all(pcm_playback_num,
  573. SNDRV_DMA_TYPE_DEV,
  574. snd_dma_pci_data
  575. (chip->pci),
  576. 64 * 1024, 64 * 1024);
  577. if (err)
  578. dev_err(chip->card->dev,
  579. "snd_pcm_lib_preallocate_pages_for_all error (0x%X)\n",
  580. err);
  581. err = snd_pcm_new(chip->card, "Audiowerk2 capture", 2, 0, 1,
  582. &pcm_capture);
  583. if (err < 0) {
  584. dev_err(chip->card->dev, "snd_pcm_new error (0x%X)\n", err);
  585. return err;
  586. }
  587. /* Creation ok */
  588. pcm_device = &chip->device_capture[NUM_STREAM_CAPTURE_ANA];
  589. /* Set PCM device name */
  590. strcpy(pcm_capture->name, "Capture");
  591. /* Associate private data to PCM device */
  592. pcm_capture->private_data = pcm_device;
  593. /* set operators of PCM device */
  594. snd_pcm_set_ops(pcm_capture, SNDRV_PCM_STREAM_CAPTURE,
  595. &snd_aw2_capture_ops);
  596. /* store PCM device */
  597. pcm_device->pcm = pcm_capture;
  598. /* give base chip pointer to our internal pcm device
  599. structure */
  600. pcm_device->chip = chip;
  601. /* Give stream number to PCM device */
  602. pcm_device->stream_number = NUM_STREAM_CAPTURE_ANA;
  603. /* pre-allocation of buffers */
  604. /* Preallocate continuous pages. */
  605. err = snd_pcm_lib_preallocate_pages_for_all(pcm_capture,
  606. SNDRV_DMA_TYPE_DEV,
  607. snd_dma_pci_data
  608. (chip->pci),
  609. 64 * 1024, 64 * 1024);
  610. if (err)
  611. dev_err(chip->card->dev,
  612. "snd_pcm_lib_preallocate_pages_for_all error (0x%X)\n",
  613. err);
  614. /* Create control */
  615. err = snd_ctl_add(chip->card, snd_ctl_new1(&aw2_control, chip));
  616. if (err < 0) {
  617. dev_err(chip->card->dev, "snd_ctl_add error (0x%X)\n", err);
  618. return err;
  619. }
  620. return 0;
  621. }
  622. static int snd_aw2_control_switch_capture_info(struct snd_kcontrol *kcontrol,
  623. struct snd_ctl_elem_info *uinfo)
  624. {
  625. static const char * const texts[2] = {
  626. "Analog", "Digital"
  627. };
  628. return snd_ctl_enum_info(uinfo, 1, 2, texts);
  629. }
  630. static int snd_aw2_control_switch_capture_get(struct snd_kcontrol *kcontrol,
  631. struct snd_ctl_elem_value
  632. *ucontrol)
  633. {
  634. struct aw2 *chip = snd_kcontrol_chip(kcontrol);
  635. if (snd_aw2_saa7146_is_using_digital_input(&chip->saa7146))
  636. ucontrol->value.enumerated.item[0] = CTL_ROUTE_DIGITAL;
  637. else
  638. ucontrol->value.enumerated.item[0] = CTL_ROUTE_ANALOG;
  639. return 0;
  640. }
  641. static int snd_aw2_control_switch_capture_put(struct snd_kcontrol *kcontrol,
  642. struct snd_ctl_elem_value
  643. *ucontrol)
  644. {
  645. struct aw2 *chip = snd_kcontrol_chip(kcontrol);
  646. int changed = 0;
  647. int is_disgital =
  648. snd_aw2_saa7146_is_using_digital_input(&chip->saa7146);
  649. if (((ucontrol->value.integer.value[0] == CTL_ROUTE_DIGITAL)
  650. && !is_disgital)
  651. || ((ucontrol->value.integer.value[0] == CTL_ROUTE_ANALOG)
  652. && is_disgital)) {
  653. snd_aw2_saa7146_use_digital_input(&chip->saa7146, !is_disgital);
  654. changed = 1;
  655. }
  656. return changed;
  657. }