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 0x%08lx, size=%d\n",
  231. (unsigned long)dma->vaddr,
  232. nr_pages << PAGE_SHIFT);
  233. memset(dma->vaddr, 0, nr_pages << PAGE_SHIFT);
  234. dma->nr_pages = nr_pages;
  235. dma->sglist = vzalloc(dma->nr_pages * sizeof(*dma->sglist));
  236. if (NULL == dma->sglist)
  237. goto vzalloc_err;
  238. sg_init_table(dma->sglist, dma->nr_pages);
  239. for (i = 0; i < dma->nr_pages; i++) {
  240. pg = vmalloc_to_page(dma->vaddr + i * PAGE_SIZE);
  241. if (NULL == pg)
  242. goto vmalloc_to_page_err;
  243. sg_set_page(&dma->sglist[i], pg, PAGE_SIZE, 0);
  244. }
  245. return 0;
  246. vmalloc_to_page_err:
  247. vfree(dma->sglist);
  248. dma->sglist = NULL;
  249. vzalloc_err:
  250. vfree(dma->vaddr);
  251. dma->vaddr = NULL;
  252. return -ENOMEM;
  253. }
  254. static int saa7134_alsa_dma_map(struct saa7134_dev *dev)
  255. {
  256. struct saa7134_dmasound *dma = &dev->dmasound;
  257. dma->sglen = dma_map_sg(&dev->pci->dev, dma->sglist,
  258. dma->nr_pages, PCI_DMA_FROMDEVICE);
  259. if (0 == dma->sglen) {
  260. pr_warn("%s: saa7134_alsa_map_sg failed\n", __func__);
  261. return -ENOMEM;
  262. }
  263. return 0;
  264. }
  265. static int saa7134_alsa_dma_unmap(struct saa7134_dev *dev)
  266. {
  267. struct saa7134_dmasound *dma = &dev->dmasound;
  268. if (!dma->sglen)
  269. return 0;
  270. dma_unmap_sg(&dev->pci->dev, dma->sglist, dma->sglen, PCI_DMA_FROMDEVICE);
  271. dma->sglen = 0;
  272. return 0;
  273. }
  274. static int saa7134_alsa_dma_free(struct saa7134_dmasound *dma)
  275. {
  276. vfree(dma->sglist);
  277. dma->sglist = NULL;
  278. vfree(dma->vaddr);
  279. dma->vaddr = NULL;
  280. return 0;
  281. }
  282. /*
  283. * DMA buffer initialization
  284. *
  285. * Uses V4L functions to initialize the DMA. Shouldn't be necessary in
  286. * ALSA, but I was unable to use ALSA's own DMA, and had to force the
  287. * usage of V4L's
  288. *
  289. * - Copied verbatim from saa7134-oss.
  290. *
  291. */
  292. static int dsp_buffer_init(struct saa7134_dev *dev)
  293. {
  294. int err;
  295. BUG_ON(!dev->dmasound.bufsize);
  296. err = saa7134_alsa_dma_init(dev,
  297. (dev->dmasound.bufsize + PAGE_SIZE) >> PAGE_SHIFT);
  298. if (0 != err)
  299. return err;
  300. return 0;
  301. }
  302. /*
  303. * DMA buffer release
  304. *
  305. * Called after closing the device, during snd_card_saa7134_capture_close
  306. *
  307. */
  308. static int dsp_buffer_free(struct saa7134_dev *dev)
  309. {
  310. BUG_ON(!dev->dmasound.blksize);
  311. saa7134_alsa_dma_free(&dev->dmasound);
  312. dev->dmasound.blocks = 0;
  313. dev->dmasound.blksize = 0;
  314. dev->dmasound.bufsize = 0;
  315. return 0;
  316. }
  317. /*
  318. * Setting the capture source and updating the ALSA controls
  319. */
  320. static int snd_saa7134_capsrc_set(struct snd_kcontrol *kcontrol,
  321. int left, int right, bool force_notify)
  322. {
  323. snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
  324. int change = 0, addr = kcontrol->private_value;
  325. int active, old_addr;
  326. u32 anabar, xbarin;
  327. int analog_io, rate;
  328. struct saa7134_dev *dev;
  329. dev = chip->dev;
  330. spin_lock_irq(&chip->mixer_lock);
  331. active = left != 0 || right != 0;
  332. old_addr = chip->capture_source_addr;
  333. /* The active capture source cannot be deactivated */
  334. if (active) {
  335. change = old_addr != addr ||
  336. chip->capture_source[0] != left ||
  337. chip->capture_source[1] != right;
  338. chip->capture_source[0] = left;
  339. chip->capture_source[1] = right;
  340. chip->capture_source_addr = addr;
  341. dev->dmasound.input = addr;
  342. }
  343. spin_unlock_irq(&chip->mixer_lock);
  344. if (change) {
  345. switch (dev->pci->device) {
  346. case PCI_DEVICE_ID_PHILIPS_SAA7134:
  347. switch (addr) {
  348. case MIXER_ADDR_TVTUNER:
  349. saa_andorb(SAA7134_AUDIO_FORMAT_CTRL,
  350. 0xc0, 0xc0);
  351. saa_andorb(SAA7134_SIF_SAMPLE_FREQ,
  352. 0x03, 0x00);
  353. break;
  354. case MIXER_ADDR_LINE1:
  355. case MIXER_ADDR_LINE2:
  356. analog_io = (MIXER_ADDR_LINE1 == addr) ?
  357. 0x00 : 0x08;
  358. rate = (32000 == dev->dmasound.rate) ?
  359. 0x01 : 0x03;
  360. saa_andorb(SAA7134_ANALOG_IO_SELECT,
  361. 0x08, analog_io);
  362. saa_andorb(SAA7134_AUDIO_FORMAT_CTRL,
  363. 0xc0, 0x80);
  364. saa_andorb(SAA7134_SIF_SAMPLE_FREQ,
  365. 0x03, rate);
  366. break;
  367. }
  368. break;
  369. case PCI_DEVICE_ID_PHILIPS_SAA7133:
  370. case PCI_DEVICE_ID_PHILIPS_SAA7135:
  371. xbarin = 0x03; /* adc */
  372. anabar = 0;
  373. switch (addr) {
  374. case MIXER_ADDR_TVTUNER:
  375. xbarin = 0; /* Demodulator */
  376. anabar = 2; /* DACs */
  377. break;
  378. case MIXER_ADDR_LINE1:
  379. anabar = 0; /* aux1, aux1 */
  380. break;
  381. case MIXER_ADDR_LINE2:
  382. anabar = 9; /* aux2, aux2 */
  383. break;
  384. }
  385. /* output xbar always main channel */
  386. saa_dsp_writel(dev, SAA7133_DIGITAL_OUTPUT_SEL1,
  387. 0xbbbb10);
  388. if (left || right) {
  389. /* We've got data, turn the input on */
  390. saa_dsp_writel(dev, SAA7133_DIGITAL_INPUT_XBAR1,
  391. xbarin);
  392. saa_writel(SAA7133_ANALOG_IO_SELECT, anabar);
  393. } else {
  394. saa_dsp_writel(dev, SAA7133_DIGITAL_INPUT_XBAR1,
  395. 0);
  396. saa_writel(SAA7133_ANALOG_IO_SELECT, 0);
  397. }
  398. break;
  399. }
  400. }
  401. if (change) {
  402. if (force_notify)
  403. snd_ctl_notify(chip->card,
  404. SNDRV_CTL_EVENT_MASK_VALUE,
  405. &chip->capture_ctl[addr]->id);
  406. if (old_addr != MIXER_ADDR_UNSELECTED && old_addr != addr)
  407. snd_ctl_notify(chip->card,
  408. SNDRV_CTL_EVENT_MASK_VALUE,
  409. &chip->capture_ctl[old_addr]->id);
  410. }
  411. return change;
  412. }
  413. /*
  414. * ALSA PCM preparation
  415. *
  416. * - One of the ALSA capture callbacks.
  417. *
  418. * Called right after the capture device is opened, this function configures
  419. * the buffer using the previously defined functions, allocates the memory,
  420. * sets up the hardware registers, and then starts the DMA. When this function
  421. * returns, the audio should be flowing.
  422. *
  423. */
  424. static int snd_card_saa7134_capture_prepare(struct snd_pcm_substream * substream)
  425. {
  426. struct snd_pcm_runtime *runtime = substream->runtime;
  427. int bswap, sign;
  428. u32 fmt, control;
  429. snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
  430. struct saa7134_dev *dev;
  431. snd_card_saa7134_pcm_t *pcm = runtime->private_data;
  432. pcm->dev->dmasound.substream = substream;
  433. dev = saa7134->dev;
  434. if (snd_pcm_format_width(runtime->format) == 8)
  435. fmt = 0x00;
  436. else
  437. fmt = 0x01;
  438. if (snd_pcm_format_signed(runtime->format))
  439. sign = 1;
  440. else
  441. sign = 0;
  442. if (snd_pcm_format_big_endian(runtime->format))
  443. bswap = 1;
  444. else
  445. bswap = 0;
  446. switch (dev->pci->device) {
  447. case PCI_DEVICE_ID_PHILIPS_SAA7134:
  448. if (1 == runtime->channels)
  449. fmt |= (1 << 3);
  450. if (2 == runtime->channels)
  451. fmt |= (3 << 3);
  452. if (sign)
  453. fmt |= 0x04;
  454. fmt |= (MIXER_ADDR_TVTUNER == dev->dmasound.input) ? 0xc0 : 0x80;
  455. saa_writeb(SAA7134_NUM_SAMPLES0, ((dev->dmasound.blksize - 1) & 0x0000ff));
  456. saa_writeb(SAA7134_NUM_SAMPLES1, ((dev->dmasound.blksize - 1) & 0x00ff00) >> 8);
  457. saa_writeb(SAA7134_NUM_SAMPLES2, ((dev->dmasound.blksize - 1) & 0xff0000) >> 16);
  458. saa_writeb(SAA7134_AUDIO_FORMAT_CTRL, fmt);
  459. break;
  460. case PCI_DEVICE_ID_PHILIPS_SAA7133:
  461. case PCI_DEVICE_ID_PHILIPS_SAA7135:
  462. if (1 == runtime->channels)
  463. fmt |= (1 << 4);
  464. if (2 == runtime->channels)
  465. fmt |= (2 << 4);
  466. if (!sign)
  467. fmt |= 0x04;
  468. saa_writel(SAA7133_NUM_SAMPLES, dev->dmasound.blksize -1);
  469. saa_writel(SAA7133_AUDIO_CHANNEL, 0x543210 | (fmt << 24));
  470. break;
  471. }
  472. pr_debug("rec_start: afmt=%d ch=%d => fmt=0x%x swap=%c\n",
  473. runtime->format, runtime->channels, fmt,
  474. bswap ? 'b' : '-');
  475. /* dma: setup channel 6 (= AUDIO) */
  476. control = SAA7134_RS_CONTROL_BURST_16 |
  477. SAA7134_RS_CONTROL_ME |
  478. (dev->dmasound.pt.dma >> 12);
  479. if (bswap)
  480. control |= SAA7134_RS_CONTROL_BSWAP;
  481. saa_writel(SAA7134_RS_BA1(6),0);
  482. saa_writel(SAA7134_RS_BA2(6),dev->dmasound.blksize);
  483. saa_writel(SAA7134_RS_PITCH(6),0);
  484. saa_writel(SAA7134_RS_CONTROL(6),control);
  485. dev->dmasound.rate = runtime->rate;
  486. /* Setup and update the card/ALSA controls */
  487. snd_saa7134_capsrc_set(saa7134->capture_ctl[dev->dmasound.input], 1, 1,
  488. true);
  489. return 0;
  490. }
  491. /*
  492. * ALSA pointer fetching
  493. *
  494. * - One of the ALSA capture callbacks.
  495. *
  496. * Called whenever a period elapses, it must return the current hardware
  497. * position of the buffer.
  498. * Also resets the read counter used to prevent overruns
  499. *
  500. */
  501. static snd_pcm_uframes_t
  502. snd_card_saa7134_capture_pointer(struct snd_pcm_substream * substream)
  503. {
  504. struct snd_pcm_runtime *runtime = substream->runtime;
  505. snd_card_saa7134_pcm_t *pcm = runtime->private_data;
  506. struct saa7134_dev *dev=pcm->dev;
  507. if (dev->dmasound.read_count) {
  508. dev->dmasound.read_count -= snd_pcm_lib_period_bytes(substream);
  509. dev->dmasound.read_offset += snd_pcm_lib_period_bytes(substream);
  510. if (dev->dmasound.read_offset == dev->dmasound.bufsize)
  511. dev->dmasound.read_offset = 0;
  512. }
  513. return bytes_to_frames(runtime, dev->dmasound.read_offset);
  514. }
  515. /*
  516. * ALSA hardware capabilities definition
  517. *
  518. * Report only 32kHz for ALSA:
  519. *
  520. * - SAA7133/35 uses DDEP (DemDec Easy Programming mode), which works in 32kHz
  521. * only
  522. * - SAA7134 for TV mode uses DemDec mode (32kHz)
  523. * - Radio works in 32kHz only
  524. * - When recording 48kHz from Line1/Line2, switching of capture source to TV
  525. * means
  526. * switching to 32kHz without any frequency translation
  527. */
  528. static struct snd_pcm_hardware snd_card_saa7134_capture =
  529. {
  530. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  531. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  532. SNDRV_PCM_INFO_MMAP_VALID),
  533. .formats = SNDRV_PCM_FMTBIT_S16_LE | \
  534. SNDRV_PCM_FMTBIT_S16_BE | \
  535. SNDRV_PCM_FMTBIT_S8 | \
  536. SNDRV_PCM_FMTBIT_U8 | \
  537. SNDRV_PCM_FMTBIT_U16_LE | \
  538. SNDRV_PCM_FMTBIT_U16_BE,
  539. .rates = SNDRV_PCM_RATE_32000,
  540. .rate_min = 32000,
  541. .rate_max = 32000,
  542. .channels_min = 1,
  543. .channels_max = 2,
  544. .buffer_bytes_max = (256*1024),
  545. .period_bytes_min = 64,
  546. .period_bytes_max = (256*1024),
  547. .periods_min = 4,
  548. .periods_max = 1024,
  549. };
  550. static void snd_card_saa7134_runtime_free(struct snd_pcm_runtime *runtime)
  551. {
  552. snd_card_saa7134_pcm_t *pcm = runtime->private_data;
  553. kfree(pcm);
  554. }
  555. /*
  556. * ALSA hardware params
  557. *
  558. * - One of the ALSA capture callbacks.
  559. *
  560. * Called on initialization, right before the PCM preparation
  561. *
  562. */
  563. static int snd_card_saa7134_hw_params(struct snd_pcm_substream * substream,
  564. struct snd_pcm_hw_params * hw_params)
  565. {
  566. snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
  567. struct saa7134_dev *dev;
  568. unsigned int period_size, periods;
  569. int err;
  570. period_size = params_period_bytes(hw_params);
  571. periods = params_periods(hw_params);
  572. if (period_size < 0x100 || period_size > 0x10000)
  573. return -EINVAL;
  574. if (periods < 4)
  575. return -EINVAL;
  576. if (period_size * periods > 1024 * 1024)
  577. return -EINVAL;
  578. dev = saa7134->dev;
  579. if (dev->dmasound.blocks == periods &&
  580. dev->dmasound.blksize == period_size)
  581. return 0;
  582. /* release the old buffer */
  583. if (substream->runtime->dma_area) {
  584. saa7134_pgtable_free(dev->pci, &dev->dmasound.pt);
  585. saa7134_alsa_dma_unmap(dev);
  586. dsp_buffer_free(dev);
  587. substream->runtime->dma_area = NULL;
  588. }
  589. dev->dmasound.blocks = periods;
  590. dev->dmasound.blksize = period_size;
  591. dev->dmasound.bufsize = period_size * periods;
  592. err = dsp_buffer_init(dev);
  593. if (0 != err) {
  594. dev->dmasound.blocks = 0;
  595. dev->dmasound.blksize = 0;
  596. dev->dmasound.bufsize = 0;
  597. return err;
  598. }
  599. err = saa7134_alsa_dma_map(dev);
  600. if (err) {
  601. dsp_buffer_free(dev);
  602. return err;
  603. }
  604. err = saa7134_pgtable_alloc(dev->pci, &dev->dmasound.pt);
  605. if (err) {
  606. saa7134_alsa_dma_unmap(dev);
  607. dsp_buffer_free(dev);
  608. return err;
  609. }
  610. err = saa7134_pgtable_build(dev->pci, &dev->dmasound.pt,
  611. dev->dmasound.sglist, dev->dmasound.sglen, 0);
  612. if (err) {
  613. saa7134_pgtable_free(dev->pci, &dev->dmasound.pt);
  614. saa7134_alsa_dma_unmap(dev);
  615. dsp_buffer_free(dev);
  616. return err;
  617. }
  618. /* I should be able to use runtime->dma_addr in the control
  619. byte, but it doesn't work. So I allocate the DMA using the
  620. V4L functions, and force ALSA to use that as the DMA area */
  621. substream->runtime->dma_area = dev->dmasound.vaddr;
  622. substream->runtime->dma_bytes = dev->dmasound.bufsize;
  623. substream->runtime->dma_addr = 0;
  624. return 0;
  625. }
  626. /*
  627. * ALSA hardware release
  628. *
  629. * - One of the ALSA capture callbacks.
  630. *
  631. * Called after closing the device, but before snd_card_saa7134_capture_close
  632. * It stops the DMA audio and releases the buffers.
  633. *
  634. */
  635. static int snd_card_saa7134_hw_free(struct snd_pcm_substream * substream)
  636. {
  637. snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
  638. struct saa7134_dev *dev;
  639. dev = saa7134->dev;
  640. if (substream->runtime->dma_area) {
  641. saa7134_pgtable_free(dev->pci, &dev->dmasound.pt);
  642. saa7134_alsa_dma_unmap(dev);
  643. dsp_buffer_free(dev);
  644. substream->runtime->dma_area = NULL;
  645. }
  646. return 0;
  647. }
  648. /*
  649. * ALSA capture finish
  650. *
  651. * - One of the ALSA capture callbacks.
  652. *
  653. * Called after closing the device.
  654. *
  655. */
  656. static int snd_card_saa7134_capture_close(struct snd_pcm_substream * substream)
  657. {
  658. snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
  659. struct saa7134_dev *dev = saa7134->dev;
  660. if (saa7134->mute_was_on) {
  661. dev->ctl_mute = 1;
  662. saa7134_tvaudio_setmute(dev);
  663. }
  664. return 0;
  665. }
  666. /*
  667. * ALSA capture start
  668. *
  669. * - One of the ALSA capture callbacks.
  670. *
  671. * Called when opening the device. It creates and populates the PCM
  672. * structure
  673. *
  674. */
  675. static int snd_card_saa7134_capture_open(struct snd_pcm_substream * substream)
  676. {
  677. struct snd_pcm_runtime *runtime = substream->runtime;
  678. snd_card_saa7134_pcm_t *pcm;
  679. snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
  680. struct saa7134_dev *dev;
  681. int amux, err;
  682. if (!saa7134) {
  683. pr_err("BUG: saa7134 can't find device struct. Can't proceed with open\n");
  684. return -ENODEV;
  685. }
  686. dev = saa7134->dev;
  687. mutex_lock(&dev->dmasound.lock);
  688. dev->dmasound.read_count = 0;
  689. dev->dmasound.read_offset = 0;
  690. amux = dev->input->amux;
  691. if ((amux < 1) || (amux > 3))
  692. amux = 1;
  693. dev->dmasound.input = amux - 1;
  694. mutex_unlock(&dev->dmasound.lock);
  695. pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
  696. if (pcm == NULL)
  697. return -ENOMEM;
  698. pcm->dev=saa7134->dev;
  699. spin_lock_init(&pcm->lock);
  700. pcm->substream = substream;
  701. runtime->private_data = pcm;
  702. runtime->private_free = snd_card_saa7134_runtime_free;
  703. runtime->hw = snd_card_saa7134_capture;
  704. if (dev->ctl_mute != 0) {
  705. saa7134->mute_was_on = 1;
  706. dev->ctl_mute = 0;
  707. saa7134_tvaudio_setmute(dev);
  708. }
  709. err = snd_pcm_hw_constraint_integer(runtime,
  710. SNDRV_PCM_HW_PARAM_PERIODS);
  711. if (err < 0)
  712. return err;
  713. err = snd_pcm_hw_constraint_step(runtime, 0,
  714. SNDRV_PCM_HW_PARAM_PERIODS, 2);
  715. if (err < 0)
  716. return err;
  717. return 0;
  718. }
  719. /*
  720. * page callback (needed for mmap)
  721. */
  722. static struct page *snd_card_saa7134_page(struct snd_pcm_substream *substream,
  723. unsigned long offset)
  724. {
  725. void *pageptr = substream->runtime->dma_area + offset;
  726. return vmalloc_to_page(pageptr);
  727. }
  728. /*
  729. * ALSA capture callbacks definition
  730. */
  731. static const struct snd_pcm_ops snd_card_saa7134_capture_ops = {
  732. .open = snd_card_saa7134_capture_open,
  733. .close = snd_card_saa7134_capture_close,
  734. .ioctl = snd_pcm_lib_ioctl,
  735. .hw_params = snd_card_saa7134_hw_params,
  736. .hw_free = snd_card_saa7134_hw_free,
  737. .prepare = snd_card_saa7134_capture_prepare,
  738. .trigger = snd_card_saa7134_capture_trigger,
  739. .pointer = snd_card_saa7134_capture_pointer,
  740. .page = snd_card_saa7134_page,
  741. };
  742. /*
  743. * ALSA PCM setup
  744. *
  745. * Called when initializing the board. Sets up the name and hooks up
  746. * the callbacks
  747. *
  748. */
  749. static int snd_card_saa7134_pcm(snd_card_saa7134_t *saa7134, int device)
  750. {
  751. struct snd_pcm *pcm;
  752. int err;
  753. if ((err = snd_pcm_new(saa7134->card, "SAA7134 PCM", device, 0, 1, &pcm)) < 0)
  754. return err;
  755. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_card_saa7134_capture_ops);
  756. pcm->private_data = saa7134;
  757. pcm->info_flags = 0;
  758. strcpy(pcm->name, "SAA7134 PCM");
  759. return 0;
  760. }
  761. #define SAA713x_VOLUME(xname, xindex, addr) \
  762. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
  763. .info = snd_saa7134_volume_info, \
  764. .get = snd_saa7134_volume_get, .put = snd_saa7134_volume_put, \
  765. .private_value = addr }
  766. static int snd_saa7134_volume_info(struct snd_kcontrol * kcontrol,
  767. struct snd_ctl_elem_info * uinfo)
  768. {
  769. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  770. uinfo->count = 2;
  771. uinfo->value.integer.min = 0;
  772. uinfo->value.integer.max = 20;
  773. return 0;
  774. }
  775. static int snd_saa7134_volume_get(struct snd_kcontrol * kcontrol,
  776. struct snd_ctl_elem_value * ucontrol)
  777. {
  778. snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
  779. int addr = kcontrol->private_value;
  780. ucontrol->value.integer.value[0] = chip->mixer_volume[addr][0];
  781. ucontrol->value.integer.value[1] = chip->mixer_volume[addr][1];
  782. return 0;
  783. }
  784. static int snd_saa7134_volume_put(struct snd_kcontrol * kcontrol,
  785. struct snd_ctl_elem_value * ucontrol)
  786. {
  787. snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
  788. struct saa7134_dev *dev = chip->dev;
  789. int change, addr = kcontrol->private_value;
  790. int left, right;
  791. left = ucontrol->value.integer.value[0];
  792. if (left < 0)
  793. left = 0;
  794. if (left > 20)
  795. left = 20;
  796. right = ucontrol->value.integer.value[1];
  797. if (right < 0)
  798. right = 0;
  799. if (right > 20)
  800. right = 20;
  801. spin_lock_irq(&chip->mixer_lock);
  802. change = 0;
  803. if (chip->mixer_volume[addr][0] != left) {
  804. change = 1;
  805. right = left;
  806. }
  807. if (chip->mixer_volume[addr][1] != right) {
  808. change = 1;
  809. left = right;
  810. }
  811. if (change) {
  812. switch (dev->pci->device) {
  813. case PCI_DEVICE_ID_PHILIPS_SAA7134:
  814. switch (addr) {
  815. case MIXER_ADDR_TVTUNER:
  816. left = 20;
  817. break;
  818. case MIXER_ADDR_LINE1:
  819. saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x10,
  820. (left > 10) ? 0x00 : 0x10);
  821. break;
  822. case MIXER_ADDR_LINE2:
  823. saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x20,
  824. (left > 10) ? 0x00 : 0x20);
  825. break;
  826. }
  827. break;
  828. case PCI_DEVICE_ID_PHILIPS_SAA7133:
  829. case PCI_DEVICE_ID_PHILIPS_SAA7135:
  830. switch (addr) {
  831. case MIXER_ADDR_TVTUNER:
  832. left = 20;
  833. break;
  834. case MIXER_ADDR_LINE1:
  835. saa_andorb(0x0594, 0x10,
  836. (left > 10) ? 0x00 : 0x10);
  837. break;
  838. case MIXER_ADDR_LINE2:
  839. saa_andorb(0x0594, 0x20,
  840. (left > 10) ? 0x00 : 0x20);
  841. break;
  842. }
  843. break;
  844. }
  845. chip->mixer_volume[addr][0] = left;
  846. chip->mixer_volume[addr][1] = right;
  847. }
  848. spin_unlock_irq(&chip->mixer_lock);
  849. return change;
  850. }
  851. #define SAA713x_CAPSRC(xname, xindex, addr) \
  852. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
  853. .info = snd_saa7134_capsrc_info, \
  854. .get = snd_saa7134_capsrc_get, .put = snd_saa7134_capsrc_put, \
  855. .private_value = addr }
  856. static int snd_saa7134_capsrc_info(struct snd_kcontrol * kcontrol,
  857. struct snd_ctl_elem_info * uinfo)
  858. {
  859. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  860. uinfo->count = 2;
  861. uinfo->value.integer.min = 0;
  862. uinfo->value.integer.max = 1;
  863. return 0;
  864. }
  865. static int snd_saa7134_capsrc_get(struct snd_kcontrol * kcontrol,
  866. struct snd_ctl_elem_value * ucontrol)
  867. {
  868. snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
  869. int addr = kcontrol->private_value;
  870. spin_lock_irq(&chip->mixer_lock);
  871. if (chip->capture_source_addr == addr) {
  872. ucontrol->value.integer.value[0] = chip->capture_source[0];
  873. ucontrol->value.integer.value[1] = chip->capture_source[1];
  874. } else {
  875. ucontrol->value.integer.value[0] = 0;
  876. ucontrol->value.integer.value[1] = 0;
  877. }
  878. spin_unlock_irq(&chip->mixer_lock);
  879. return 0;
  880. }
  881. static int snd_saa7134_capsrc_put(struct snd_kcontrol * kcontrol,
  882. struct snd_ctl_elem_value * ucontrol)
  883. {
  884. int left, right;
  885. left = ucontrol->value.integer.value[0] & 1;
  886. right = ucontrol->value.integer.value[1] & 1;
  887. return snd_saa7134_capsrc_set(kcontrol, left, right, false);
  888. }
  889. static struct snd_kcontrol_new snd_saa7134_volume_controls[] = {
  890. SAA713x_VOLUME("Video Volume", 0, MIXER_ADDR_TVTUNER),
  891. SAA713x_VOLUME("Line Volume", 1, MIXER_ADDR_LINE1),
  892. SAA713x_VOLUME("Line Volume", 2, MIXER_ADDR_LINE2),
  893. };
  894. static struct snd_kcontrol_new snd_saa7134_capture_controls[] = {
  895. SAA713x_CAPSRC("Video Capture Switch", 0, MIXER_ADDR_TVTUNER),
  896. SAA713x_CAPSRC("Line Capture Switch", 1, MIXER_ADDR_LINE1),
  897. SAA713x_CAPSRC("Line Capture Switch", 2, MIXER_ADDR_LINE2),
  898. };
  899. /*
  900. * ALSA mixer setup
  901. *
  902. * Called when initializing the board. Sets up the name and hooks up
  903. * the callbacks
  904. *
  905. */
  906. static int snd_card_saa7134_new_mixer(snd_card_saa7134_t * chip)
  907. {
  908. struct snd_card *card = chip->card;
  909. struct snd_kcontrol *kcontrol;
  910. unsigned int idx;
  911. int err, addr;
  912. strcpy(card->mixername, "SAA7134 Mixer");
  913. for (idx = 0; idx < ARRAY_SIZE(snd_saa7134_volume_controls); idx++) {
  914. kcontrol = snd_ctl_new1(&snd_saa7134_volume_controls[idx],
  915. chip);
  916. err = snd_ctl_add(card, kcontrol);
  917. if (err < 0)
  918. return err;
  919. }
  920. for (idx = 0; idx < ARRAY_SIZE(snd_saa7134_capture_controls); idx++) {
  921. kcontrol = snd_ctl_new1(&snd_saa7134_capture_controls[idx],
  922. chip);
  923. addr = snd_saa7134_capture_controls[idx].private_value;
  924. chip->capture_ctl[addr] = kcontrol;
  925. err = snd_ctl_add(card, kcontrol);
  926. if (err < 0)
  927. return err;
  928. }
  929. chip->capture_source_addr = MIXER_ADDR_UNSELECTED;
  930. return 0;
  931. }
  932. static void snd_saa7134_free(struct snd_card * card)
  933. {
  934. snd_card_saa7134_t *chip = card->private_data;
  935. if (chip->dev->dmasound.priv_data == NULL)
  936. return;
  937. if (chip->irq >= 0)
  938. free_irq(chip->irq, &chip->dev->dmasound);
  939. chip->dev->dmasound.priv_data = NULL;
  940. }
  941. /*
  942. * ALSA initialization
  943. *
  944. * Called by the init routine, once for each saa7134 device present,
  945. * it creates the basic structures and registers the ALSA devices
  946. *
  947. */
  948. static int alsa_card_saa7134_create(struct saa7134_dev *dev, int devnum)
  949. {
  950. struct snd_card *card;
  951. snd_card_saa7134_t *chip;
  952. int err;
  953. if (devnum >= SNDRV_CARDS)
  954. return -ENODEV;
  955. if (!enable[devnum])
  956. return -ENODEV;
  957. err = snd_card_new(&dev->pci->dev, index[devnum], id[devnum],
  958. THIS_MODULE, sizeof(snd_card_saa7134_t), &card);
  959. if (err < 0)
  960. return err;
  961. strcpy(card->driver, "SAA7134");
  962. /* Card "creation" */
  963. card->private_free = snd_saa7134_free;
  964. chip = card->private_data;
  965. spin_lock_init(&chip->lock);
  966. spin_lock_init(&chip->mixer_lock);
  967. chip->dev = dev;
  968. chip->card = card;
  969. chip->pci = dev->pci;
  970. chip->iobase = pci_resource_start(dev->pci, 0);
  971. err = request_irq(dev->pci->irq, saa7134_alsa_irq,
  972. IRQF_SHARED, dev->name,
  973. (void*) &dev->dmasound);
  974. if (err < 0) {
  975. pr_err("%s: can't get IRQ %d for ALSA\n",
  976. dev->name, dev->pci->irq);
  977. goto __nodev;
  978. }
  979. chip->irq = dev->pci->irq;
  980. mutex_init(&dev->dmasound.lock);
  981. if ((err = snd_card_saa7134_new_mixer(chip)) < 0)
  982. goto __nodev;
  983. if ((err = snd_card_saa7134_pcm(chip, 0)) < 0)
  984. goto __nodev;
  985. /* End of "creation" */
  986. strcpy(card->shortname, "SAA7134");
  987. sprintf(card->longname, "%s at 0x%lx irq %d",
  988. chip->dev->name, chip->iobase, chip->irq);
  989. pr_info("%s/alsa: %s registered as card %d\n",
  990. dev->name, card->longname, index[devnum]);
  991. if ((err = snd_card_register(card)) == 0) {
  992. snd_saa7134_cards[devnum] = card;
  993. return 0;
  994. }
  995. __nodev:
  996. snd_card_free(card);
  997. return err;
  998. }
  999. static int alsa_device_init(struct saa7134_dev *dev)
  1000. {
  1001. dev->dmasound.priv_data = dev;
  1002. alsa_card_saa7134_create(dev,dev->nr);
  1003. return 1;
  1004. }
  1005. static int alsa_device_exit(struct saa7134_dev *dev)
  1006. {
  1007. if (!snd_saa7134_cards[dev->nr])
  1008. return 1;
  1009. snd_card_free(snd_saa7134_cards[dev->nr]);
  1010. snd_saa7134_cards[dev->nr] = NULL;
  1011. return 1;
  1012. }
  1013. /*
  1014. * Module initializer
  1015. *
  1016. * Loops through present saa7134 cards, and assigns an ALSA device
  1017. * to each one
  1018. *
  1019. */
  1020. static int saa7134_alsa_init(void)
  1021. {
  1022. struct saa7134_dev *dev = NULL;
  1023. struct list_head *list;
  1024. saa7134_dmasound_init = alsa_device_init;
  1025. saa7134_dmasound_exit = alsa_device_exit;
  1026. pr_info("saa7134 ALSA driver for DMA sound loaded\n");
  1027. list_for_each(list,&saa7134_devlist) {
  1028. dev = list_entry(list, struct saa7134_dev, devlist);
  1029. if (dev->pci->device == PCI_DEVICE_ID_PHILIPS_SAA7130)
  1030. pr_info("%s/alsa: %s doesn't support digital audio\n",
  1031. dev->name, saa7134_boards[dev->board].name);
  1032. else
  1033. alsa_device_init(dev);
  1034. }
  1035. if (dev == NULL)
  1036. pr_info("saa7134 ALSA: no saa7134 cards found\n");
  1037. return 0;
  1038. }
  1039. /*
  1040. * Module destructor
  1041. */
  1042. static void saa7134_alsa_exit(void)
  1043. {
  1044. int idx;
  1045. for (idx = 0; idx < SNDRV_CARDS; idx++) {
  1046. if (snd_saa7134_cards[idx])
  1047. snd_card_free(snd_saa7134_cards[idx]);
  1048. }
  1049. saa7134_dmasound_init = NULL;
  1050. saa7134_dmasound_exit = NULL;
  1051. pr_info("saa7134 ALSA driver for DMA sound unloaded\n");
  1052. return;
  1053. }
  1054. /* We initialize this late, to make sure the sound system is up and running */
  1055. late_initcall(saa7134_alsa_init);
  1056. module_exit(saa7134_alsa_exit);
  1057. MODULE_LICENSE("GPL");
  1058. MODULE_AUTHOR("Ricardo Cerqueira");