saa7134-alsa.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275
  1. /*
  2. * SAA713x ALSA support for V4L
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, version 2
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. */
  14. #include "saa7134.h"
  15. #include "saa7134-reg.h"
  16. #include <linux/init.h>
  17. #include <linux/slab.h>
  18. #include <linux/time.h>
  19. #include <linux/wait.h>
  20. #include <linux/module.h>
  21. #include <sound/core.h>
  22. #include <sound/control.h>
  23. #include <sound/pcm.h>
  24. #include <sound/pcm_params.h>
  25. #include <sound/initval.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/vmalloc.h>
  28. /*
  29. * Configuration macros
  30. */
  31. /* defaults */
  32. #define MIXER_ADDR_UNSELECTED -1
  33. #define MIXER_ADDR_TVTUNER 0
  34. #define MIXER_ADDR_LINE1 1
  35. #define MIXER_ADDR_LINE2 2
  36. #define MIXER_ADDR_LAST 2
  37. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
  38. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  39. static int enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 1};
  40. module_param_array(index, int, NULL, 0444);
  41. module_param_array(enable, int, NULL, 0444);
  42. MODULE_PARM_DESC(index, "Index value for SAA7134 capture interface(s).");
  43. MODULE_PARM_DESC(enable, "Enable (or not) the SAA7134 capture interface(s).");
  44. /*
  45. * Main chip structure
  46. */
  47. typedef struct snd_card_saa7134 {
  48. struct snd_card *card;
  49. spinlock_t mixer_lock;
  50. int mixer_volume[MIXER_ADDR_LAST+1][2];
  51. int capture_source_addr;
  52. int capture_source[2];
  53. struct snd_kcontrol *capture_ctl[MIXER_ADDR_LAST+1];
  54. struct pci_dev *pci;
  55. struct saa7134_dev *dev;
  56. unsigned long iobase;
  57. s16 irq;
  58. u16 mute_was_on;
  59. spinlock_t lock;
  60. } snd_card_saa7134_t;
  61. /*
  62. * PCM structure
  63. */
  64. typedef struct snd_card_saa7134_pcm {
  65. struct saa7134_dev *dev;
  66. spinlock_t lock;
  67. struct snd_pcm_substream *substream;
  68. } snd_card_saa7134_pcm_t;
  69. static struct snd_card *snd_saa7134_cards[SNDRV_CARDS];
  70. /*
  71. * saa7134 DMA audio stop
  72. *
  73. * Called when the capture device is released or the buffer overflows
  74. *
  75. * - Copied verbatim from saa7134-oss's dsp_dma_stop.
  76. *
  77. */
  78. static void saa7134_dma_stop(struct saa7134_dev *dev)
  79. {
  80. dev->dmasound.dma_blk = -1;
  81. dev->dmasound.dma_running = 0;
  82. saa7134_set_dmabits(dev);
  83. }
  84. /*
  85. * saa7134 DMA audio start
  86. *
  87. * Called when preparing the capture device for use
  88. *
  89. * - Copied verbatim from saa7134-oss's dsp_dma_start.
  90. *
  91. */
  92. static void saa7134_dma_start(struct saa7134_dev *dev)
  93. {
  94. dev->dmasound.dma_blk = 0;
  95. dev->dmasound.dma_running = 1;
  96. saa7134_set_dmabits(dev);
  97. }
  98. /*
  99. * saa7134 audio DMA IRQ handler
  100. *
  101. * Called whenever we get an SAA7134_IRQ_REPORT_DONE_RA3 interrupt
  102. * Handles shifting between the 2 buffers, manages the read counters,
  103. * and notifies ALSA when periods elapse
  104. *
  105. * - Mostly copied from saa7134-oss's saa7134_irq_oss_done.
  106. *
  107. */
  108. static void saa7134_irq_alsa_done(struct saa7134_dev *dev,
  109. unsigned long status)
  110. {
  111. int next_blk, reg = 0;
  112. spin_lock(&dev->slock);
  113. if (UNSET == dev->dmasound.dma_blk) {
  114. pr_debug("irq: recording stopped\n");
  115. goto done;
  116. }
  117. if (0 != (status & 0x0f000000))
  118. pr_debug("irq: lost %ld\n", (status >> 24) & 0x0f);
  119. if (0 == (status & 0x10000000)) {
  120. /* odd */
  121. if (0 == (dev->dmasound.dma_blk & 0x01))
  122. reg = SAA7134_RS_BA1(6);
  123. } else {
  124. /* even */
  125. if (1 == (dev->dmasound.dma_blk & 0x01))
  126. reg = SAA7134_RS_BA2(6);
  127. }
  128. if (0 == reg) {
  129. pr_debug("irq: field oops [%s]\n",
  130. (status & 0x10000000) ? "even" : "odd");
  131. goto done;
  132. }
  133. if (dev->dmasound.read_count >= dev->dmasound.blksize * (dev->dmasound.blocks-2)) {
  134. pr_debug("irq: overrun [full=%d/%d] - Blocks in %d\n",
  135. dev->dmasound.read_count,
  136. dev->dmasound.bufsize, dev->dmasound.blocks);
  137. spin_unlock(&dev->slock);
  138. snd_pcm_stop_xrun(dev->dmasound.substream);
  139. return;
  140. }
  141. /* next block addr */
  142. next_blk = (dev->dmasound.dma_blk + 2) % dev->dmasound.blocks;
  143. saa_writel(reg,next_blk * dev->dmasound.blksize);
  144. pr_debug("irq: ok, %s, next_blk=%d, addr=%x, blocks=%u, size=%u, read=%u\n",
  145. (status & 0x10000000) ? "even" : "odd ", next_blk,
  146. next_blk * dev->dmasound.blksize, dev->dmasound.blocks,
  147. dev->dmasound.blksize, dev->dmasound.read_count);
  148. /* update status & wake waiting readers */
  149. dev->dmasound.dma_blk = (dev->dmasound.dma_blk + 1) % dev->dmasound.blocks;
  150. dev->dmasound.read_count += dev->dmasound.blksize;
  151. dev->dmasound.recording_on = reg;
  152. if (dev->dmasound.read_count >= snd_pcm_lib_period_bytes(dev->dmasound.substream)) {
  153. spin_unlock(&dev->slock);
  154. snd_pcm_period_elapsed(dev->dmasound.substream);
  155. spin_lock(&dev->slock);
  156. }
  157. done:
  158. spin_unlock(&dev->slock);
  159. }
  160. /*
  161. * IRQ request handler
  162. *
  163. * Runs along with saa7134's IRQ handler, discards anything that isn't
  164. * DMA sound
  165. *
  166. */
  167. static irqreturn_t saa7134_alsa_irq(int irq, void *dev_id)
  168. {
  169. struct saa7134_dmasound *dmasound = dev_id;
  170. struct saa7134_dev *dev = dmasound->priv_data;
  171. unsigned long report, status;
  172. int loop, handled = 0;
  173. for (loop = 0; loop < 10; loop++) {
  174. report = saa_readl(SAA7134_IRQ_REPORT);
  175. status = saa_readl(SAA7134_IRQ_STATUS);
  176. if (report & SAA7134_IRQ_REPORT_DONE_RA3) {
  177. handled = 1;
  178. saa_writel(SAA7134_IRQ_REPORT,
  179. SAA7134_IRQ_REPORT_DONE_RA3);
  180. saa7134_irq_alsa_done(dev, status);
  181. } else {
  182. goto out;
  183. }
  184. }
  185. if (loop == 10) {
  186. pr_debug("error! looping IRQ!");
  187. }
  188. out:
  189. return IRQ_RETVAL(handled);
  190. }
  191. /*
  192. * ALSA capture trigger
  193. *
  194. * - One of the ALSA capture callbacks.
  195. *
  196. * Called whenever a capture is started or stopped. Must be defined,
  197. * but there's nothing we want to do here
  198. *
  199. */
  200. static int snd_card_saa7134_capture_trigger(struct snd_pcm_substream * substream,
  201. int cmd)
  202. {
  203. struct snd_pcm_runtime *runtime = substream->runtime;
  204. snd_card_saa7134_pcm_t *pcm = runtime->private_data;
  205. struct saa7134_dev *dev=pcm->dev;
  206. int err = 0;
  207. spin_lock(&dev->slock);
  208. if (cmd == SNDRV_PCM_TRIGGER_START) {
  209. /* start dma */
  210. saa7134_dma_start(dev);
  211. } else if (cmd == SNDRV_PCM_TRIGGER_STOP) {
  212. /* stop dma */
  213. saa7134_dma_stop(dev);
  214. } else {
  215. err = -EINVAL;
  216. }
  217. spin_unlock(&dev->slock);
  218. return err;
  219. }
  220. static int saa7134_alsa_dma_init(struct saa7134_dev *dev, int nr_pages)
  221. {
  222. struct saa7134_dmasound *dma = &dev->dmasound;
  223. struct page *pg;
  224. int i;
  225. dma->vaddr = vmalloc_32(nr_pages << PAGE_SHIFT);
  226. if (NULL == dma->vaddr) {
  227. pr_debug("vmalloc_32(%d pages) failed\n", nr_pages);
  228. return -ENOMEM;
  229. }
  230. pr_debug("vmalloc is at addr %p, size=%d\n",
  231. dma->vaddr, nr_pages << PAGE_SHIFT);
  232. memset(dma->vaddr, 0, nr_pages << PAGE_SHIFT);
  233. dma->nr_pages = nr_pages;
  234. dma->sglist = vzalloc(array_size(sizeof(*dma->sglist), dma->nr_pages));
  235. if (NULL == dma->sglist)
  236. goto vzalloc_err;
  237. sg_init_table(dma->sglist, dma->nr_pages);
  238. for (i = 0; i < dma->nr_pages; i++) {
  239. pg = vmalloc_to_page(dma->vaddr + i * PAGE_SIZE);
  240. if (NULL == pg)
  241. goto vmalloc_to_page_err;
  242. sg_set_page(&dma->sglist[i], pg, PAGE_SIZE, 0);
  243. }
  244. return 0;
  245. vmalloc_to_page_err:
  246. vfree(dma->sglist);
  247. dma->sglist = NULL;
  248. vzalloc_err:
  249. vfree(dma->vaddr);
  250. dma->vaddr = NULL;
  251. return -ENOMEM;
  252. }
  253. static int saa7134_alsa_dma_map(struct saa7134_dev *dev)
  254. {
  255. struct saa7134_dmasound *dma = &dev->dmasound;
  256. dma->sglen = dma_map_sg(&dev->pci->dev, dma->sglist,
  257. dma->nr_pages, PCI_DMA_FROMDEVICE);
  258. if (0 == dma->sglen) {
  259. pr_warn("%s: saa7134_alsa_map_sg failed\n", __func__);
  260. return -ENOMEM;
  261. }
  262. return 0;
  263. }
  264. static int saa7134_alsa_dma_unmap(struct saa7134_dev *dev)
  265. {
  266. struct saa7134_dmasound *dma = &dev->dmasound;
  267. if (!dma->sglen)
  268. return 0;
  269. dma_unmap_sg(&dev->pci->dev, dma->sglist, dma->sglen, PCI_DMA_FROMDEVICE);
  270. dma->sglen = 0;
  271. return 0;
  272. }
  273. static int saa7134_alsa_dma_free(struct saa7134_dmasound *dma)
  274. {
  275. vfree(dma->sglist);
  276. dma->sglist = NULL;
  277. vfree(dma->vaddr);
  278. dma->vaddr = NULL;
  279. return 0;
  280. }
  281. /*
  282. * DMA buffer initialization
  283. *
  284. * Uses V4L functions to initialize the DMA. Shouldn't be necessary in
  285. * ALSA, but I was unable to use ALSA's own DMA, and had to force the
  286. * usage of V4L's
  287. *
  288. * - Copied verbatim from saa7134-oss.
  289. *
  290. */
  291. static int dsp_buffer_init(struct saa7134_dev *dev)
  292. {
  293. int err;
  294. BUG_ON(!dev->dmasound.bufsize);
  295. err = saa7134_alsa_dma_init(dev,
  296. (dev->dmasound.bufsize + PAGE_SIZE) >> PAGE_SHIFT);
  297. if (0 != err)
  298. return err;
  299. return 0;
  300. }
  301. /*
  302. * DMA buffer release
  303. *
  304. * Called after closing the device, during snd_card_saa7134_capture_close
  305. *
  306. */
  307. static int dsp_buffer_free(struct saa7134_dev *dev)
  308. {
  309. BUG_ON(!dev->dmasound.blksize);
  310. saa7134_alsa_dma_free(&dev->dmasound);
  311. dev->dmasound.blocks = 0;
  312. dev->dmasound.blksize = 0;
  313. dev->dmasound.bufsize = 0;
  314. return 0;
  315. }
  316. /*
  317. * Setting the capture source and updating the ALSA controls
  318. */
  319. static int snd_saa7134_capsrc_set(struct snd_kcontrol *kcontrol,
  320. int left, int right, bool force_notify)
  321. {
  322. snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
  323. int change = 0, addr = kcontrol->private_value;
  324. int active, old_addr;
  325. u32 anabar, xbarin;
  326. int analog_io, rate;
  327. struct saa7134_dev *dev;
  328. dev = chip->dev;
  329. spin_lock_irq(&chip->mixer_lock);
  330. active = left != 0 || right != 0;
  331. old_addr = chip->capture_source_addr;
  332. /* The active capture source cannot be deactivated */
  333. if (active) {
  334. change = old_addr != addr ||
  335. chip->capture_source[0] != left ||
  336. chip->capture_source[1] != right;
  337. chip->capture_source[0] = left;
  338. chip->capture_source[1] = right;
  339. chip->capture_source_addr = addr;
  340. dev->dmasound.input = addr;
  341. }
  342. spin_unlock_irq(&chip->mixer_lock);
  343. if (change) {
  344. switch (dev->pci->device) {
  345. case PCI_DEVICE_ID_PHILIPS_SAA7134:
  346. switch (addr) {
  347. case MIXER_ADDR_TVTUNER:
  348. saa_andorb(SAA7134_AUDIO_FORMAT_CTRL,
  349. 0xc0, 0xc0);
  350. saa_andorb(SAA7134_SIF_SAMPLE_FREQ,
  351. 0x03, 0x00);
  352. break;
  353. case MIXER_ADDR_LINE1:
  354. case MIXER_ADDR_LINE2:
  355. analog_io = (MIXER_ADDR_LINE1 == addr) ?
  356. 0x00 : 0x08;
  357. rate = (32000 == dev->dmasound.rate) ?
  358. 0x01 : 0x03;
  359. saa_andorb(SAA7134_ANALOG_IO_SELECT,
  360. 0x08, analog_io);
  361. saa_andorb(SAA7134_AUDIO_FORMAT_CTRL,
  362. 0xc0, 0x80);
  363. saa_andorb(SAA7134_SIF_SAMPLE_FREQ,
  364. 0x03, rate);
  365. break;
  366. }
  367. break;
  368. case PCI_DEVICE_ID_PHILIPS_SAA7133:
  369. case PCI_DEVICE_ID_PHILIPS_SAA7135:
  370. xbarin = 0x03; /* adc */
  371. anabar = 0;
  372. switch (addr) {
  373. case MIXER_ADDR_TVTUNER:
  374. xbarin = 0; /* Demodulator */
  375. anabar = 2; /* DACs */
  376. break;
  377. case MIXER_ADDR_LINE1:
  378. anabar = 0; /* aux1, aux1 */
  379. break;
  380. case MIXER_ADDR_LINE2:
  381. anabar = 9; /* aux2, aux2 */
  382. break;
  383. }
  384. /* output xbar always main channel */
  385. saa_dsp_writel(dev, SAA7133_DIGITAL_OUTPUT_SEL1,
  386. 0xbbbb10);
  387. if (left || right) {
  388. /* We've got data, turn the input on */
  389. saa_dsp_writel(dev, SAA7133_DIGITAL_INPUT_XBAR1,
  390. xbarin);
  391. saa_writel(SAA7133_ANALOG_IO_SELECT, anabar);
  392. } else {
  393. saa_dsp_writel(dev, SAA7133_DIGITAL_INPUT_XBAR1,
  394. 0);
  395. saa_writel(SAA7133_ANALOG_IO_SELECT, 0);
  396. }
  397. break;
  398. }
  399. }
  400. if (change) {
  401. if (force_notify)
  402. snd_ctl_notify(chip->card,
  403. SNDRV_CTL_EVENT_MASK_VALUE,
  404. &chip->capture_ctl[addr]->id);
  405. if (old_addr != MIXER_ADDR_UNSELECTED && old_addr != addr)
  406. snd_ctl_notify(chip->card,
  407. SNDRV_CTL_EVENT_MASK_VALUE,
  408. &chip->capture_ctl[old_addr]->id);
  409. }
  410. return change;
  411. }
  412. /*
  413. * ALSA PCM preparation
  414. *
  415. * - One of the ALSA capture callbacks.
  416. *
  417. * Called right after the capture device is opened, this function configures
  418. * the buffer using the previously defined functions, allocates the memory,
  419. * sets up the hardware registers, and then starts the DMA. When this function
  420. * returns, the audio should be flowing.
  421. *
  422. */
  423. static int snd_card_saa7134_capture_prepare(struct snd_pcm_substream * substream)
  424. {
  425. struct snd_pcm_runtime *runtime = substream->runtime;
  426. int bswap, sign;
  427. u32 fmt, control;
  428. snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
  429. struct saa7134_dev *dev;
  430. snd_card_saa7134_pcm_t *pcm = runtime->private_data;
  431. pcm->dev->dmasound.substream = substream;
  432. dev = saa7134->dev;
  433. if (snd_pcm_format_width(runtime->format) == 8)
  434. fmt = 0x00;
  435. else
  436. fmt = 0x01;
  437. if (snd_pcm_format_signed(runtime->format))
  438. sign = 1;
  439. else
  440. sign = 0;
  441. if (snd_pcm_format_big_endian(runtime->format))
  442. bswap = 1;
  443. else
  444. bswap = 0;
  445. switch (dev->pci->device) {
  446. case PCI_DEVICE_ID_PHILIPS_SAA7134:
  447. if (1 == runtime->channels)
  448. fmt |= (1 << 3);
  449. if (2 == runtime->channels)
  450. fmt |= (3 << 3);
  451. if (sign)
  452. fmt |= 0x04;
  453. fmt |= (MIXER_ADDR_TVTUNER == dev->dmasound.input) ? 0xc0 : 0x80;
  454. saa_writeb(SAA7134_NUM_SAMPLES0, ((dev->dmasound.blksize - 1) & 0x0000ff));
  455. saa_writeb(SAA7134_NUM_SAMPLES1, ((dev->dmasound.blksize - 1) & 0x00ff00) >> 8);
  456. saa_writeb(SAA7134_NUM_SAMPLES2, ((dev->dmasound.blksize - 1) & 0xff0000) >> 16);
  457. saa_writeb(SAA7134_AUDIO_FORMAT_CTRL, fmt);
  458. break;
  459. case PCI_DEVICE_ID_PHILIPS_SAA7133:
  460. case PCI_DEVICE_ID_PHILIPS_SAA7135:
  461. if (1 == runtime->channels)
  462. fmt |= (1 << 4);
  463. if (2 == runtime->channels)
  464. fmt |= (2 << 4);
  465. if (!sign)
  466. fmt |= 0x04;
  467. saa_writel(SAA7133_NUM_SAMPLES, dev->dmasound.blksize -1);
  468. saa_writel(SAA7133_AUDIO_CHANNEL, 0x543210 | (fmt << 24));
  469. break;
  470. }
  471. pr_debug("rec_start: afmt=%d ch=%d => fmt=0x%x swap=%c\n",
  472. runtime->format, runtime->channels, fmt,
  473. bswap ? 'b' : '-');
  474. /* dma: setup channel 6 (= AUDIO) */
  475. control = SAA7134_RS_CONTROL_BURST_16 |
  476. SAA7134_RS_CONTROL_ME |
  477. (dev->dmasound.pt.dma >> 12);
  478. if (bswap)
  479. control |= SAA7134_RS_CONTROL_BSWAP;
  480. saa_writel(SAA7134_RS_BA1(6),0);
  481. saa_writel(SAA7134_RS_BA2(6),dev->dmasound.blksize);
  482. saa_writel(SAA7134_RS_PITCH(6),0);
  483. saa_writel(SAA7134_RS_CONTROL(6),control);
  484. dev->dmasound.rate = runtime->rate;
  485. /* Setup and update the card/ALSA controls */
  486. snd_saa7134_capsrc_set(saa7134->capture_ctl[dev->dmasound.input], 1, 1,
  487. true);
  488. return 0;
  489. }
  490. /*
  491. * ALSA pointer fetching
  492. *
  493. * - One of the ALSA capture callbacks.
  494. *
  495. * Called whenever a period elapses, it must return the current hardware
  496. * position of the buffer.
  497. * Also resets the read counter used to prevent overruns
  498. *
  499. */
  500. static snd_pcm_uframes_t
  501. snd_card_saa7134_capture_pointer(struct snd_pcm_substream * substream)
  502. {
  503. struct snd_pcm_runtime *runtime = substream->runtime;
  504. snd_card_saa7134_pcm_t *pcm = runtime->private_data;
  505. struct saa7134_dev *dev=pcm->dev;
  506. if (dev->dmasound.read_count) {
  507. dev->dmasound.read_count -= snd_pcm_lib_period_bytes(substream);
  508. dev->dmasound.read_offset += snd_pcm_lib_period_bytes(substream);
  509. if (dev->dmasound.read_offset == dev->dmasound.bufsize)
  510. dev->dmasound.read_offset = 0;
  511. }
  512. return bytes_to_frames(runtime, dev->dmasound.read_offset);
  513. }
  514. /*
  515. * ALSA hardware capabilities definition
  516. *
  517. * Report only 32kHz for ALSA:
  518. *
  519. * - SAA7133/35 uses DDEP (DemDec Easy Programming mode), which works in 32kHz
  520. * only
  521. * - SAA7134 for TV mode uses DemDec mode (32kHz)
  522. * - Radio works in 32kHz only
  523. * - When recording 48kHz from Line1/Line2, switching of capture source to TV
  524. * means
  525. * switching to 32kHz without any frequency translation
  526. */
  527. static const struct snd_pcm_hardware snd_card_saa7134_capture =
  528. {
  529. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  530. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  531. SNDRV_PCM_INFO_MMAP_VALID),
  532. .formats = SNDRV_PCM_FMTBIT_S16_LE | \
  533. SNDRV_PCM_FMTBIT_S16_BE | \
  534. SNDRV_PCM_FMTBIT_S8 | \
  535. SNDRV_PCM_FMTBIT_U8 | \
  536. SNDRV_PCM_FMTBIT_U16_LE | \
  537. SNDRV_PCM_FMTBIT_U16_BE,
  538. .rates = SNDRV_PCM_RATE_32000,
  539. .rate_min = 32000,
  540. .rate_max = 32000,
  541. .channels_min = 1,
  542. .channels_max = 2,
  543. .buffer_bytes_max = (256*1024),
  544. .period_bytes_min = 64,
  545. .period_bytes_max = (256*1024),
  546. .periods_min = 4,
  547. .periods_max = 1024,
  548. };
  549. static void snd_card_saa7134_runtime_free(struct snd_pcm_runtime *runtime)
  550. {
  551. snd_card_saa7134_pcm_t *pcm = runtime->private_data;
  552. kfree(pcm);
  553. }
  554. /*
  555. * ALSA hardware params
  556. *
  557. * - One of the ALSA capture callbacks.
  558. *
  559. * Called on initialization, right before the PCM preparation
  560. *
  561. */
  562. static int snd_card_saa7134_hw_params(struct snd_pcm_substream * substream,
  563. struct snd_pcm_hw_params * hw_params)
  564. {
  565. snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
  566. struct saa7134_dev *dev;
  567. unsigned int period_size, periods;
  568. int err;
  569. period_size = params_period_bytes(hw_params);
  570. periods = params_periods(hw_params);
  571. if (period_size < 0x100 || period_size > 0x10000)
  572. return -EINVAL;
  573. if (periods < 4)
  574. return -EINVAL;
  575. if (period_size * periods > 1024 * 1024)
  576. return -EINVAL;
  577. dev = saa7134->dev;
  578. if (dev->dmasound.blocks == periods &&
  579. dev->dmasound.blksize == period_size)
  580. return 0;
  581. /* release the old buffer */
  582. if (substream->runtime->dma_area) {
  583. saa7134_pgtable_free(dev->pci, &dev->dmasound.pt);
  584. saa7134_alsa_dma_unmap(dev);
  585. dsp_buffer_free(dev);
  586. substream->runtime->dma_area = NULL;
  587. }
  588. dev->dmasound.blocks = periods;
  589. dev->dmasound.blksize = period_size;
  590. dev->dmasound.bufsize = period_size * periods;
  591. err = dsp_buffer_init(dev);
  592. if (0 != err) {
  593. dev->dmasound.blocks = 0;
  594. dev->dmasound.blksize = 0;
  595. dev->dmasound.bufsize = 0;
  596. return err;
  597. }
  598. err = saa7134_alsa_dma_map(dev);
  599. if (err) {
  600. dsp_buffer_free(dev);
  601. return err;
  602. }
  603. err = saa7134_pgtable_alloc(dev->pci, &dev->dmasound.pt);
  604. if (err) {
  605. saa7134_alsa_dma_unmap(dev);
  606. dsp_buffer_free(dev);
  607. return err;
  608. }
  609. err = saa7134_pgtable_build(dev->pci, &dev->dmasound.pt,
  610. dev->dmasound.sglist, dev->dmasound.sglen, 0);
  611. if (err) {
  612. saa7134_pgtable_free(dev->pci, &dev->dmasound.pt);
  613. saa7134_alsa_dma_unmap(dev);
  614. dsp_buffer_free(dev);
  615. return err;
  616. }
  617. /* I should be able to use runtime->dma_addr in the control
  618. byte, but it doesn't work. So I allocate the DMA using the
  619. V4L functions, and force ALSA to use that as the DMA area */
  620. substream->runtime->dma_area = dev->dmasound.vaddr;
  621. substream->runtime->dma_bytes = dev->dmasound.bufsize;
  622. substream->runtime->dma_addr = 0;
  623. return 0;
  624. }
  625. /*
  626. * ALSA hardware release
  627. *
  628. * - One of the ALSA capture callbacks.
  629. *
  630. * Called after closing the device, but before snd_card_saa7134_capture_close
  631. * It stops the DMA audio and releases the buffers.
  632. *
  633. */
  634. static int snd_card_saa7134_hw_free(struct snd_pcm_substream * substream)
  635. {
  636. snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
  637. struct saa7134_dev *dev;
  638. dev = saa7134->dev;
  639. if (substream->runtime->dma_area) {
  640. saa7134_pgtable_free(dev->pci, &dev->dmasound.pt);
  641. saa7134_alsa_dma_unmap(dev);
  642. dsp_buffer_free(dev);
  643. substream->runtime->dma_area = NULL;
  644. }
  645. return 0;
  646. }
  647. /*
  648. * ALSA capture finish
  649. *
  650. * - One of the ALSA capture callbacks.
  651. *
  652. * Called after closing the device.
  653. *
  654. */
  655. static int snd_card_saa7134_capture_close(struct snd_pcm_substream * substream)
  656. {
  657. snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
  658. struct saa7134_dev *dev = saa7134->dev;
  659. if (saa7134->mute_was_on) {
  660. dev->ctl_mute = 1;
  661. saa7134_tvaudio_setmute(dev);
  662. }
  663. return 0;
  664. }
  665. /*
  666. * ALSA capture start
  667. *
  668. * - One of the ALSA capture callbacks.
  669. *
  670. * Called when opening the device. It creates and populates the PCM
  671. * structure
  672. *
  673. */
  674. static int snd_card_saa7134_capture_open(struct snd_pcm_substream * substream)
  675. {
  676. struct snd_pcm_runtime *runtime = substream->runtime;
  677. snd_card_saa7134_pcm_t *pcm;
  678. snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
  679. struct saa7134_dev *dev;
  680. int amux, err;
  681. if (!saa7134) {
  682. pr_err("BUG: saa7134 can't find device struct. Can't proceed with open\n");
  683. return -ENODEV;
  684. }
  685. dev = saa7134->dev;
  686. mutex_lock(&dev->dmasound.lock);
  687. dev->dmasound.read_count = 0;
  688. dev->dmasound.read_offset = 0;
  689. amux = dev->input->amux;
  690. if ((amux < 1) || (amux > 3))
  691. amux = 1;
  692. dev->dmasound.input = amux - 1;
  693. mutex_unlock(&dev->dmasound.lock);
  694. pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
  695. if (pcm == NULL)
  696. return -ENOMEM;
  697. pcm->dev=saa7134->dev;
  698. spin_lock_init(&pcm->lock);
  699. pcm->substream = substream;
  700. runtime->private_data = pcm;
  701. runtime->private_free = snd_card_saa7134_runtime_free;
  702. runtime->hw = snd_card_saa7134_capture;
  703. if (dev->ctl_mute != 0) {
  704. saa7134->mute_was_on = 1;
  705. dev->ctl_mute = 0;
  706. saa7134_tvaudio_setmute(dev);
  707. }
  708. err = snd_pcm_hw_constraint_integer(runtime,
  709. SNDRV_PCM_HW_PARAM_PERIODS);
  710. if (err < 0)
  711. return err;
  712. err = snd_pcm_hw_constraint_step(runtime, 0,
  713. SNDRV_PCM_HW_PARAM_PERIODS, 2);
  714. if (err < 0)
  715. return err;
  716. return 0;
  717. }
  718. /*
  719. * page callback (needed for mmap)
  720. */
  721. static struct page *snd_card_saa7134_page(struct snd_pcm_substream *substream,
  722. unsigned long offset)
  723. {
  724. void *pageptr = substream->runtime->dma_area + offset;
  725. return vmalloc_to_page(pageptr);
  726. }
  727. /*
  728. * ALSA capture callbacks definition
  729. */
  730. static const struct snd_pcm_ops snd_card_saa7134_capture_ops = {
  731. .open = snd_card_saa7134_capture_open,
  732. .close = snd_card_saa7134_capture_close,
  733. .ioctl = snd_pcm_lib_ioctl,
  734. .hw_params = snd_card_saa7134_hw_params,
  735. .hw_free = snd_card_saa7134_hw_free,
  736. .prepare = snd_card_saa7134_capture_prepare,
  737. .trigger = snd_card_saa7134_capture_trigger,
  738. .pointer = snd_card_saa7134_capture_pointer,
  739. .page = snd_card_saa7134_page,
  740. };
  741. /*
  742. * ALSA PCM setup
  743. *
  744. * Called when initializing the board. Sets up the name and hooks up
  745. * the callbacks
  746. *
  747. */
  748. static int snd_card_saa7134_pcm(snd_card_saa7134_t *saa7134, int device)
  749. {
  750. struct snd_pcm *pcm;
  751. int err;
  752. if ((err = snd_pcm_new(saa7134->card, "SAA7134 PCM", device, 0, 1, &pcm)) < 0)
  753. return err;
  754. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_card_saa7134_capture_ops);
  755. pcm->private_data = saa7134;
  756. pcm->info_flags = 0;
  757. strcpy(pcm->name, "SAA7134 PCM");
  758. return 0;
  759. }
  760. #define SAA713x_VOLUME(xname, xindex, addr) \
  761. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
  762. .info = snd_saa7134_volume_info, \
  763. .get = snd_saa7134_volume_get, .put = snd_saa7134_volume_put, \
  764. .private_value = addr }
  765. static int snd_saa7134_volume_info(struct snd_kcontrol * kcontrol,
  766. struct snd_ctl_elem_info * uinfo)
  767. {
  768. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  769. uinfo->count = 2;
  770. uinfo->value.integer.min = 0;
  771. uinfo->value.integer.max = 20;
  772. return 0;
  773. }
  774. static int snd_saa7134_volume_get(struct snd_kcontrol * kcontrol,
  775. struct snd_ctl_elem_value * ucontrol)
  776. {
  777. snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
  778. int addr = kcontrol->private_value;
  779. ucontrol->value.integer.value[0] = chip->mixer_volume[addr][0];
  780. ucontrol->value.integer.value[1] = chip->mixer_volume[addr][1];
  781. return 0;
  782. }
  783. static int snd_saa7134_volume_put(struct snd_kcontrol * kcontrol,
  784. struct snd_ctl_elem_value * ucontrol)
  785. {
  786. snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
  787. struct saa7134_dev *dev = chip->dev;
  788. int change, addr = kcontrol->private_value;
  789. int left, right;
  790. left = ucontrol->value.integer.value[0];
  791. if (left < 0)
  792. left = 0;
  793. if (left > 20)
  794. left = 20;
  795. right = ucontrol->value.integer.value[1];
  796. if (right < 0)
  797. right = 0;
  798. if (right > 20)
  799. right = 20;
  800. spin_lock_irq(&chip->mixer_lock);
  801. change = 0;
  802. if (chip->mixer_volume[addr][0] != left) {
  803. change = 1;
  804. right = left;
  805. }
  806. if (chip->mixer_volume[addr][1] != right) {
  807. change = 1;
  808. left = right;
  809. }
  810. if (change) {
  811. switch (dev->pci->device) {
  812. case PCI_DEVICE_ID_PHILIPS_SAA7134:
  813. switch (addr) {
  814. case MIXER_ADDR_TVTUNER:
  815. left = 20;
  816. break;
  817. case MIXER_ADDR_LINE1:
  818. saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x10,
  819. (left > 10) ? 0x00 : 0x10);
  820. break;
  821. case MIXER_ADDR_LINE2:
  822. saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x20,
  823. (left > 10) ? 0x00 : 0x20);
  824. break;
  825. }
  826. break;
  827. case PCI_DEVICE_ID_PHILIPS_SAA7133:
  828. case PCI_DEVICE_ID_PHILIPS_SAA7135:
  829. switch (addr) {
  830. case MIXER_ADDR_TVTUNER:
  831. left = 20;
  832. break;
  833. case MIXER_ADDR_LINE1:
  834. saa_andorb(0x0594, 0x10,
  835. (left > 10) ? 0x00 : 0x10);
  836. break;
  837. case MIXER_ADDR_LINE2:
  838. saa_andorb(0x0594, 0x20,
  839. (left > 10) ? 0x00 : 0x20);
  840. break;
  841. }
  842. break;
  843. }
  844. chip->mixer_volume[addr][0] = left;
  845. chip->mixer_volume[addr][1] = right;
  846. }
  847. spin_unlock_irq(&chip->mixer_lock);
  848. return change;
  849. }
  850. #define SAA713x_CAPSRC(xname, xindex, addr) \
  851. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
  852. .info = snd_saa7134_capsrc_info, \
  853. .get = snd_saa7134_capsrc_get, .put = snd_saa7134_capsrc_put, \
  854. .private_value = addr }
  855. static int snd_saa7134_capsrc_info(struct snd_kcontrol * kcontrol,
  856. struct snd_ctl_elem_info * uinfo)
  857. {
  858. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  859. uinfo->count = 2;
  860. uinfo->value.integer.min = 0;
  861. uinfo->value.integer.max = 1;
  862. return 0;
  863. }
  864. static int snd_saa7134_capsrc_get(struct snd_kcontrol * kcontrol,
  865. struct snd_ctl_elem_value * ucontrol)
  866. {
  867. snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
  868. int addr = kcontrol->private_value;
  869. spin_lock_irq(&chip->mixer_lock);
  870. if (chip->capture_source_addr == addr) {
  871. ucontrol->value.integer.value[0] = chip->capture_source[0];
  872. ucontrol->value.integer.value[1] = chip->capture_source[1];
  873. } else {
  874. ucontrol->value.integer.value[0] = 0;
  875. ucontrol->value.integer.value[1] = 0;
  876. }
  877. spin_unlock_irq(&chip->mixer_lock);
  878. return 0;
  879. }
  880. static int snd_saa7134_capsrc_put(struct snd_kcontrol * kcontrol,
  881. struct snd_ctl_elem_value * ucontrol)
  882. {
  883. int left, right;
  884. left = ucontrol->value.integer.value[0] & 1;
  885. right = ucontrol->value.integer.value[1] & 1;
  886. return snd_saa7134_capsrc_set(kcontrol, left, right, false);
  887. }
  888. static struct snd_kcontrol_new snd_saa7134_volume_controls[] = {
  889. SAA713x_VOLUME("Video Volume", 0, MIXER_ADDR_TVTUNER),
  890. SAA713x_VOLUME("Line Volume", 1, MIXER_ADDR_LINE1),
  891. SAA713x_VOLUME("Line Volume", 2, MIXER_ADDR_LINE2),
  892. };
  893. static struct snd_kcontrol_new snd_saa7134_capture_controls[] = {
  894. SAA713x_CAPSRC("Video Capture Switch", 0, MIXER_ADDR_TVTUNER),
  895. SAA713x_CAPSRC("Line Capture Switch", 1, MIXER_ADDR_LINE1),
  896. SAA713x_CAPSRC("Line Capture Switch", 2, MIXER_ADDR_LINE2),
  897. };
  898. /*
  899. * ALSA mixer setup
  900. *
  901. * Called when initializing the board. Sets up the name and hooks up
  902. * the callbacks
  903. *
  904. */
  905. static int snd_card_saa7134_new_mixer(snd_card_saa7134_t * chip)
  906. {
  907. struct snd_card *card = chip->card;
  908. struct snd_kcontrol *kcontrol;
  909. unsigned int idx;
  910. int err, addr;
  911. strcpy(card->mixername, "SAA7134 Mixer");
  912. for (idx = 0; idx < ARRAY_SIZE(snd_saa7134_volume_controls); idx++) {
  913. kcontrol = snd_ctl_new1(&snd_saa7134_volume_controls[idx],
  914. chip);
  915. err = snd_ctl_add(card, kcontrol);
  916. if (err < 0)
  917. return err;
  918. }
  919. for (idx = 0; idx < ARRAY_SIZE(snd_saa7134_capture_controls); idx++) {
  920. kcontrol = snd_ctl_new1(&snd_saa7134_capture_controls[idx],
  921. chip);
  922. addr = snd_saa7134_capture_controls[idx].private_value;
  923. chip->capture_ctl[addr] = kcontrol;
  924. err = snd_ctl_add(card, kcontrol);
  925. if (err < 0)
  926. return err;
  927. }
  928. chip->capture_source_addr = MIXER_ADDR_UNSELECTED;
  929. return 0;
  930. }
  931. static void snd_saa7134_free(struct snd_card * card)
  932. {
  933. snd_card_saa7134_t *chip = card->private_data;
  934. if (chip->dev->dmasound.priv_data == NULL)
  935. return;
  936. if (chip->irq >= 0)
  937. free_irq(chip->irq, &chip->dev->dmasound);
  938. chip->dev->dmasound.priv_data = NULL;
  939. }
  940. /*
  941. * ALSA initialization
  942. *
  943. * Called by the init routine, once for each saa7134 device present,
  944. * it creates the basic structures and registers the ALSA devices
  945. *
  946. */
  947. static int alsa_card_saa7134_create(struct saa7134_dev *dev, int devnum)
  948. {
  949. struct snd_card *card;
  950. snd_card_saa7134_t *chip;
  951. int err;
  952. if (devnum >= SNDRV_CARDS)
  953. return -ENODEV;
  954. if (!enable[devnum])
  955. return -ENODEV;
  956. err = snd_card_new(&dev->pci->dev, index[devnum], id[devnum],
  957. THIS_MODULE, sizeof(snd_card_saa7134_t), &card);
  958. if (err < 0)
  959. return err;
  960. strcpy(card->driver, "SAA7134");
  961. /* Card "creation" */
  962. card->private_free = snd_saa7134_free;
  963. chip = card->private_data;
  964. spin_lock_init(&chip->lock);
  965. spin_lock_init(&chip->mixer_lock);
  966. chip->dev = dev;
  967. chip->card = card;
  968. chip->pci = dev->pci;
  969. chip->iobase = pci_resource_start(dev->pci, 0);
  970. err = request_irq(dev->pci->irq, saa7134_alsa_irq,
  971. IRQF_SHARED, dev->name,
  972. (void*) &dev->dmasound);
  973. if (err < 0) {
  974. pr_err("%s: can't get IRQ %d for ALSA\n",
  975. dev->name, dev->pci->irq);
  976. goto __nodev;
  977. }
  978. chip->irq = dev->pci->irq;
  979. mutex_init(&dev->dmasound.lock);
  980. if ((err = snd_card_saa7134_new_mixer(chip)) < 0)
  981. goto __nodev;
  982. if ((err = snd_card_saa7134_pcm(chip, 0)) < 0)
  983. goto __nodev;
  984. /* End of "creation" */
  985. strcpy(card->shortname, "SAA7134");
  986. sprintf(card->longname, "%s at 0x%lx irq %d",
  987. chip->dev->name, chip->iobase, chip->irq);
  988. pr_info("%s/alsa: %s registered as card %d\n",
  989. dev->name, card->longname, index[devnum]);
  990. if ((err = snd_card_register(card)) == 0) {
  991. snd_saa7134_cards[devnum] = card;
  992. return 0;
  993. }
  994. __nodev:
  995. snd_card_free(card);
  996. return err;
  997. }
  998. static int alsa_device_init(struct saa7134_dev *dev)
  999. {
  1000. dev->dmasound.priv_data = dev;
  1001. alsa_card_saa7134_create(dev,dev->nr);
  1002. return 1;
  1003. }
  1004. static int alsa_device_exit(struct saa7134_dev *dev)
  1005. {
  1006. if (!snd_saa7134_cards[dev->nr])
  1007. return 1;
  1008. snd_card_free(snd_saa7134_cards[dev->nr]);
  1009. snd_saa7134_cards[dev->nr] = NULL;
  1010. return 1;
  1011. }
  1012. /*
  1013. * Module initializer
  1014. *
  1015. * Loops through present saa7134 cards, and assigns an ALSA device
  1016. * to each one
  1017. *
  1018. */
  1019. static int saa7134_alsa_init(void)
  1020. {
  1021. struct saa7134_dev *dev = NULL;
  1022. struct list_head *list;
  1023. saa7134_dmasound_init = alsa_device_init;
  1024. saa7134_dmasound_exit = alsa_device_exit;
  1025. pr_info("saa7134 ALSA driver for DMA sound loaded\n");
  1026. list_for_each(list,&saa7134_devlist) {
  1027. dev = list_entry(list, struct saa7134_dev, devlist);
  1028. if (dev->pci->device == PCI_DEVICE_ID_PHILIPS_SAA7130)
  1029. pr_info("%s/alsa: %s doesn't support digital audio\n",
  1030. dev->name, saa7134_boards[dev->board].name);
  1031. else
  1032. alsa_device_init(dev);
  1033. }
  1034. if (dev == NULL)
  1035. pr_info("saa7134 ALSA: no saa7134 cards found\n");
  1036. return 0;
  1037. }
  1038. /*
  1039. * Module destructor
  1040. */
  1041. static void saa7134_alsa_exit(void)
  1042. {
  1043. int idx;
  1044. for (idx = 0; idx < SNDRV_CARDS; idx++) {
  1045. if (snd_saa7134_cards[idx])
  1046. snd_card_free(snd_saa7134_cards[idx]);
  1047. }
  1048. saa7134_dmasound_init = NULL;
  1049. saa7134_dmasound_exit = NULL;
  1050. pr_info("saa7134 ALSA driver for DMA sound unloaded\n");
  1051. return;
  1052. }
  1053. /* We initialize this late, to make sure the sound system is up and running */
  1054. late_initcall(saa7134_alsa_init);
  1055. module_exit(saa7134_alsa_exit);
  1056. MODULE_LICENSE("GPL");
  1057. MODULE_AUTHOR("Ricardo Cerqueira");