saa7134-alsa.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048
  1. /*
  2. * SAA713x ALSA support for V4L
  3. *
  4. *
  5. * Caveats:
  6. * - Volume doesn't work (it's always at max)
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, version 2
  11. *
  12. * This program 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 this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. */
  22. #include <sound/driver.h>
  23. #include <linux/init.h>
  24. #include <linux/slab.h>
  25. #include <linux/time.h>
  26. #include <linux/wait.h>
  27. #include <linux/moduleparam.h>
  28. #include <linux/module.h>
  29. #include <sound/core.h>
  30. #include <sound/control.h>
  31. #include <sound/pcm.h>
  32. #include <sound/initval.h>
  33. #include <linux/interrupt.h>
  34. #include "saa7134.h"
  35. #include "saa7134-reg.h"
  36. static unsigned int debug = 0;
  37. module_param(debug, int, 0644);
  38. MODULE_PARM_DESC(debug,"enable debug messages [alsa]");
  39. /*
  40. * Configuration macros
  41. */
  42. /* defaults */
  43. #define MIXER_ADDR_TVTUNER 0
  44. #define MIXER_ADDR_LINE1 1
  45. #define MIXER_ADDR_LINE2 2
  46. #define MIXER_ADDR_LAST 2
  47. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
  48. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  49. static int enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 0};
  50. module_param_array(index, int, NULL, 0444);
  51. MODULE_PARM_DESC(index, "Index value for SAA7134 capture interface(s).");
  52. int position;
  53. #define dprintk(fmt, arg...) if (debug) \
  54. printk(KERN_DEBUG "%s/alsa: " fmt, dev->name , ## arg)
  55. /*
  56. * Main chip structure
  57. */
  58. typedef struct snd_card_saa7134 {
  59. snd_card_t *card;
  60. spinlock_t mixer_lock;
  61. int mixer_volume[MIXER_ADDR_LAST+1][2];
  62. int capture_source[MIXER_ADDR_LAST+1][2];
  63. struct pci_dev *pci;
  64. struct saa7134_dev *saadev;
  65. unsigned long iobase;
  66. int irq;
  67. spinlock_t lock;
  68. } snd_card_saa7134_t;
  69. /*
  70. * PCM structure
  71. */
  72. typedef struct snd_card_saa7134_pcm {
  73. struct saa7134_dev *saadev;
  74. spinlock_t lock;
  75. unsigned int pcm_size; /* buffer size */
  76. unsigned int pcm_count; /* bytes per period */
  77. unsigned int pcm_bps; /* bytes per second */
  78. snd_pcm_substream_t *substream;
  79. } snd_card_saa7134_pcm_t;
  80. static snd_card_t *snd_saa7134_cards[SNDRV_CARDS];
  81. /*
  82. * saa7134 DMA audio stop
  83. *
  84. * Called when the capture device is released or the buffer overflows
  85. *
  86. * - Copied verbatim from saa7134-oss's dsp_dma_stop.
  87. *
  88. */
  89. static void saa7134_dma_stop(struct saa7134_dev *dev)
  90. {
  91. dev->dmasound.dma_blk = -1;
  92. dev->dmasound.dma_running = 0;
  93. saa7134_set_dmabits(dev);
  94. }
  95. /*
  96. * saa7134 DMA audio start
  97. *
  98. * Called when preparing the capture device for use
  99. *
  100. * - Copied verbatim from saa7134-oss's dsp_dma_start.
  101. *
  102. */
  103. static void saa7134_dma_start(struct saa7134_dev *dev)
  104. {
  105. dev->dmasound.dma_blk = 0;
  106. dev->dmasound.dma_running = 1;
  107. saa7134_set_dmabits(dev);
  108. }
  109. /*
  110. * saa7134 audio DMA IRQ handler
  111. *
  112. * Called whenever we get an SAA7134_IRQ_REPORT_DONE_RA3 interrupt
  113. * Handles shifting between the 2 buffers, manages the read counters,
  114. * and notifies ALSA when periods elapse
  115. *
  116. * - Mostly copied from saa7134-oss's saa7134_irq_oss_done.
  117. *
  118. */
  119. void saa7134_irq_alsa_done(struct saa7134_dev *dev, unsigned long status)
  120. {
  121. int next_blk, reg = 0;
  122. spin_lock(&dev->slock);
  123. if (UNSET == dev->dmasound.dma_blk) {
  124. dprintk("irq: recording stopped\n");
  125. goto done;
  126. }
  127. if (0 != (status & 0x0f000000))
  128. dprintk("irq: lost %ld\n", (status >> 24) & 0x0f);
  129. if (0 == (status & 0x10000000)) {
  130. /* odd */
  131. if (0 == (dev->dmasound.dma_blk & 0x01))
  132. reg = SAA7134_RS_BA1(6);
  133. } else {
  134. /* even */
  135. if (1 == (dev->dmasound.dma_blk & 0x01))
  136. reg = SAA7134_RS_BA2(6);
  137. }
  138. if (0 == reg) {
  139. dprintk("irq: field oops [%s]\n",
  140. (status & 0x10000000) ? "even" : "odd");
  141. goto done;
  142. }
  143. if (dev->dmasound.read_count >= dev->dmasound.blksize * (dev->dmasound.blocks-2)) {
  144. dprintk("irq: overrun [full=%d/%d] - Blocks in %d\n",dev->dmasound.read_count,
  145. dev->dmasound.bufsize, dev->dmasound.blocks);
  146. snd_pcm_stop(dev->dmasound.substream,SNDRV_PCM_STATE_XRUN);
  147. goto done;
  148. }
  149. /* next block addr */
  150. next_blk = (dev->dmasound.dma_blk + 2) % dev->dmasound.blocks;
  151. saa_writel(reg,next_blk * dev->dmasound.blksize);
  152. if (debug > 2)
  153. dprintk("irq: ok, %s, next_blk=%d, addr=%x, blocks=%u, size=%u, read=%u\n",
  154. (status & 0x10000000) ? "even" : "odd ", next_blk,
  155. next_blk * dev->dmasound.blksize, dev->dmasound.blocks, dev->dmasound.blksize, dev->dmasound.read_count);
  156. /* update status & wake waiting readers */
  157. dev->dmasound.dma_blk = (dev->dmasound.dma_blk + 1) % dev->dmasound.blocks;
  158. dev->dmasound.read_count += dev->dmasound.blksize;
  159. dev->dmasound.recording_on = reg;
  160. if (dev->dmasound.read_count >= snd_pcm_lib_period_bytes(dev->dmasound.substream)) {
  161. spin_unlock(&dev->slock);
  162. snd_pcm_period_elapsed(dev->dmasound.substream);
  163. spin_lock(&dev->slock);
  164. }
  165. done:
  166. spin_unlock(&dev->slock);
  167. }
  168. /*
  169. * IRQ request handler
  170. *
  171. * Runs along with saa7134's IRQ handler, discards anything that isn't
  172. * DMA sound
  173. *
  174. */
  175. static irqreturn_t saa7134_alsa_irq(int irq, void *dev_id, struct pt_regs *regs)
  176. {
  177. snd_card_saa7134_t *saa7134 = dev_id;
  178. struct saa7134_dev *dev = saa7134->saadev;
  179. unsigned long report, status;
  180. int loop, handled = 0;
  181. for (loop = 0; loop < 10; loop++) {
  182. report = saa_readl(SAA7134_IRQ_REPORT);
  183. status = saa_readl(SAA7134_IRQ_STATUS);
  184. if (report & SAA7134_IRQ_REPORT_DONE_RA3) {
  185. handled = 1;
  186. saa_writel(SAA7134_IRQ_REPORT,report);
  187. saa7134_irq_alsa_done(dev, status);
  188. } else {
  189. goto out;
  190. }
  191. }
  192. if (loop == 10) {
  193. dprintk("error! looping IRQ!");
  194. }
  195. out:
  196. return IRQ_RETVAL(handled);
  197. }
  198. /*
  199. * ALSA capture trigger
  200. *
  201. * - One of the ALSA capture callbacks.
  202. *
  203. * Called whenever a capture is started or stopped. Must be defined,
  204. * but there's nothing we want to do here
  205. *
  206. */
  207. static int snd_card_saa7134_capture_trigger(snd_pcm_substream_t * substream,
  208. int cmd)
  209. {
  210. snd_pcm_runtime_t *runtime = substream->runtime;
  211. snd_card_saa7134_pcm_t *saapcm = runtime->private_data;
  212. struct saa7134_dev *dev=saapcm->saadev;
  213. int err = 0;
  214. spin_lock_irq(&dev->slock);
  215. if (cmd == SNDRV_PCM_TRIGGER_START) {
  216. /* start dma */
  217. saa7134_dma_start(dev);
  218. } else if (cmd == SNDRV_PCM_TRIGGER_STOP) {
  219. /* stop dma */
  220. saa7134_dma_stop(dev);
  221. } else {
  222. err = -EINVAL;
  223. }
  224. spin_unlock_irq(&dev->slock);
  225. return err;
  226. }
  227. /*
  228. * DMA buffer config
  229. *
  230. * Sets the values that will later be used as the size of the buffer,
  231. * size of the fragments, and total number of fragments.
  232. * Must be called during the preparation stage, before memory is
  233. * allocated
  234. *
  235. * - Copied verbatim from saa7134-oss.
  236. *
  237. */
  238. static int dsp_buffer_conf(struct saa7134_dev *dev, int blksize, int blocks)
  239. {
  240. if (blksize < 0x100)
  241. blksize = 0x100;
  242. if (blksize > 0x10000)
  243. blksize = 0x10000;
  244. if (blocks < 2)
  245. blocks = 2;
  246. if ((blksize * blocks) > 1024*1024)
  247. blocks = 1024*1024 / blksize;
  248. dev->dmasound.blocks = blocks;
  249. dev->dmasound.blksize = blksize;
  250. dev->dmasound.bufsize = blksize * blocks;
  251. dprintk("buffer config: %d blocks / %d bytes, %d kB total\n",
  252. blocks,blksize,blksize * blocks / 1024);
  253. return 0;
  254. }
  255. /*
  256. * DMA buffer initialization
  257. *
  258. * Uses V4L functions to initialize the DMA. Shouldn't be necessary in
  259. * ALSA, but I was unable to use ALSA's own DMA, and had to force the
  260. * usage of V4L's
  261. *
  262. * - Copied verbatim from saa7134-oss.
  263. *
  264. */
  265. static int dsp_buffer_init(struct saa7134_dev *dev)
  266. {
  267. int err;
  268. if (!dev->dmasound.bufsize)
  269. BUG();
  270. videobuf_dma_init(&dev->dmasound.dma);
  271. err = videobuf_dma_init_kernel(&dev->dmasound.dma, PCI_DMA_FROMDEVICE,
  272. (dev->dmasound.bufsize + PAGE_SIZE) >> PAGE_SHIFT);
  273. if (0 != err)
  274. return err;
  275. return 0;
  276. }
  277. /*
  278. * ALSA PCM preparation
  279. *
  280. * - One of the ALSA capture callbacks.
  281. *
  282. * Called right after the capture device is opened, this function configures
  283. * the buffer using the previously defined functions, allocates the memory,
  284. * sets up the hardware registers, and then starts the DMA. When this function
  285. * returns, the audio should be flowing.
  286. *
  287. */
  288. static int snd_card_saa7134_capture_prepare(snd_pcm_substream_t * substream)
  289. {
  290. snd_pcm_runtime_t *runtime = substream->runtime;
  291. int err, bswap, sign;
  292. u32 fmt, control;
  293. snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
  294. struct saa7134_dev *dev;
  295. snd_card_saa7134_pcm_t *saapcm = runtime->private_data;
  296. unsigned int bps;
  297. unsigned long size;
  298. unsigned count;
  299. size = snd_pcm_lib_buffer_bytes(substream);
  300. count = snd_pcm_lib_period_bytes(substream);
  301. saapcm->saadev->dmasound.substream = substream;
  302. bps = runtime->rate * runtime->channels;
  303. bps *= snd_pcm_format_width(runtime->format);
  304. bps /= 8;
  305. if (bps <= 0)
  306. return -EINVAL;
  307. saapcm->pcm_bps = bps;
  308. saapcm->pcm_size = snd_pcm_lib_buffer_bytes(substream);
  309. saapcm->pcm_count = snd_pcm_lib_period_bytes(substream);
  310. dev=saa7134->saadev;
  311. dsp_buffer_conf(dev,saapcm->pcm_count,(saapcm->pcm_size/saapcm->pcm_count));
  312. err = dsp_buffer_init(dev);
  313. if (0 != err)
  314. return err;
  315. /* prepare buffer */
  316. if (0 != (err = videobuf_dma_pci_map(dev->pci,&dev->dmasound.dma)))
  317. return err;
  318. if (0 != (err = saa7134_pgtable_alloc(dev->pci,&dev->dmasound.pt)))
  319. goto fail1;
  320. if (0 != (err = saa7134_pgtable_build(dev->pci,&dev->dmasound.pt,
  321. dev->dmasound.dma.sglist,
  322. dev->dmasound.dma.sglen,
  323. 0)))
  324. goto fail2;
  325. switch (runtime->format) {
  326. case SNDRV_PCM_FORMAT_U8:
  327. case SNDRV_PCM_FORMAT_S8:
  328. fmt = 0x00;
  329. break;
  330. case SNDRV_PCM_FORMAT_U16_LE:
  331. case SNDRV_PCM_FORMAT_U16_BE:
  332. case SNDRV_PCM_FORMAT_S16_LE:
  333. case SNDRV_PCM_FORMAT_S16_BE:
  334. fmt = 0x01;
  335. break;
  336. default:
  337. err = -EINVAL;
  338. return 1;
  339. }
  340. switch (runtime->format) {
  341. case SNDRV_PCM_FORMAT_S8:
  342. case SNDRV_PCM_FORMAT_S16_LE:
  343. case SNDRV_PCM_FORMAT_S16_BE:
  344. sign = 1;
  345. break;
  346. default:
  347. sign = 0;
  348. break;
  349. }
  350. switch (runtime->format) {
  351. case SNDRV_PCM_FORMAT_U16_BE:
  352. case SNDRV_PCM_FORMAT_S16_BE:
  353. bswap = 1; break;
  354. default:
  355. bswap = 0; break;
  356. }
  357. switch (dev->pci->device) {
  358. case PCI_DEVICE_ID_PHILIPS_SAA7134:
  359. if (1 == runtime->channels)
  360. fmt |= (1 << 3);
  361. if (2 == runtime->channels)
  362. fmt |= (3 << 3);
  363. if (sign)
  364. fmt |= 0x04;
  365. fmt |= (MIXER_ADDR_TVTUNER == dev->dmasound.input) ? 0xc0 : 0x80;
  366. saa_writeb(SAA7134_NUM_SAMPLES0, ((dev->dmasound.blksize - 1) & 0x0000ff));
  367. saa_writeb(SAA7134_NUM_SAMPLES1, ((dev->dmasound.blksize - 1) & 0x00ff00) >> 8);
  368. saa_writeb(SAA7134_NUM_SAMPLES2, ((dev->dmasound.blksize - 1) & 0xff0000) >> 16);
  369. saa_writeb(SAA7134_AUDIO_FORMAT_CTRL, fmt);
  370. break;
  371. case PCI_DEVICE_ID_PHILIPS_SAA7133:
  372. case PCI_DEVICE_ID_PHILIPS_SAA7135:
  373. if (1 == runtime->channels)
  374. fmt |= (1 << 4);
  375. if (2 == runtime->channels)
  376. fmt |= (2 << 4);
  377. if (!sign)
  378. fmt |= 0x04;
  379. saa_writel(SAA7133_NUM_SAMPLES, dev->dmasound.blksize -1);
  380. saa_writel(SAA7133_AUDIO_CHANNEL, 0x543210 | (fmt << 24));
  381. //saa_writel(SAA7133_AUDIO_CHANNEL, 0x543210);
  382. break;
  383. }
  384. dprintk("rec_start: afmt=%d ch=%d => fmt=0x%x swap=%c\n",
  385. runtime->format, runtime->channels, fmt,
  386. bswap ? 'b' : '-');
  387. /* dma: setup channel 6 (= AUDIO) */
  388. control = SAA7134_RS_CONTROL_BURST_16 |
  389. SAA7134_RS_CONTROL_ME |
  390. (dev->dmasound.pt.dma >> 12);
  391. if (bswap)
  392. control |= SAA7134_RS_CONTROL_BSWAP;
  393. /* I should be able to use runtime->dma_addr in the control
  394. byte, but it doesn't work. So I allocate the DMA using the
  395. V4L functions, and force ALSA to use that as the DMA area */
  396. runtime->dma_area = dev->dmasound.dma.vmalloc;
  397. saa_writel(SAA7134_RS_BA1(6),0);
  398. saa_writel(SAA7134_RS_BA2(6),dev->dmasound.blksize);
  399. saa_writel(SAA7134_RS_PITCH(6),0);
  400. saa_writel(SAA7134_RS_CONTROL(6),control);
  401. dev->dmasound.rate = runtime->rate;
  402. return 0;
  403. fail2:
  404. saa7134_pgtable_free(dev->pci,&dev->dmasound.pt);
  405. fail1:
  406. videobuf_dma_pci_unmap(dev->pci,&dev->dmasound.dma);
  407. return err;
  408. }
  409. /*
  410. * ALSA pointer fetching
  411. *
  412. * - One of the ALSA capture callbacks.
  413. *
  414. * Called whenever a period elapses, it must return the current hardware
  415. * position of the buffer.
  416. * Also resets the read counter used to prevent overruns
  417. *
  418. */
  419. static snd_pcm_uframes_t snd_card_saa7134_capture_pointer(snd_pcm_substream_t * substream)
  420. {
  421. snd_pcm_runtime_t *runtime = substream->runtime;
  422. snd_card_saa7134_pcm_t *saapcm = runtime->private_data;
  423. struct saa7134_dev *dev=saapcm->saadev;
  424. if (dev->dmasound.read_count) {
  425. dev->dmasound.read_count -= snd_pcm_lib_period_bytes(substream);
  426. dev->dmasound.read_offset += snd_pcm_lib_period_bytes(substream);
  427. if (dev->dmasound.read_offset == dev->dmasound.bufsize)
  428. dev->dmasound.read_offset = 0;
  429. }
  430. return bytes_to_frames(runtime, dev->dmasound.read_offset);
  431. }
  432. /*
  433. * ALSA hardware capabilities definition
  434. */
  435. static snd_pcm_hardware_t snd_card_saa7134_capture =
  436. {
  437. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  438. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  439. SNDRV_PCM_INFO_MMAP_VALID),
  440. .formats = SNDRV_PCM_FMTBIT_S16_LE | \
  441. SNDRV_PCM_FMTBIT_S16_BE | \
  442. SNDRV_PCM_FMTBIT_S8 | \
  443. SNDRV_PCM_FMTBIT_U8 | \
  444. SNDRV_PCM_FMTBIT_U16_LE | \
  445. SNDRV_PCM_FMTBIT_U16_BE,
  446. .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_48000,
  447. .rate_min = 32000,
  448. .rate_max = 48000,
  449. .channels_min = 1,
  450. .channels_max = 2,
  451. .buffer_bytes_max = (256*1024),
  452. .period_bytes_min = 64,
  453. .period_bytes_max = (256*1024),
  454. .periods_min = 2,
  455. .periods_max = 1024,
  456. };
  457. static void snd_card_saa7134_runtime_free(snd_pcm_runtime_t *runtime)
  458. {
  459. snd_card_saa7134_pcm_t *saapcm = runtime->private_data;
  460. kfree(saapcm);
  461. }
  462. /*
  463. * ALSA hardware params
  464. *
  465. * - One of the ALSA capture callbacks.
  466. *
  467. * Called on initialization, right before the PCM preparation
  468. * Usually used in ALSA to allocate the DMA, but since we don't use the
  469. * ALSA DMA it does nothing
  470. *
  471. */
  472. static int snd_card_saa7134_hw_params(snd_pcm_substream_t * substream,
  473. snd_pcm_hw_params_t * hw_params)
  474. {
  475. return 0;
  476. }
  477. /*
  478. * ALSA hardware release
  479. *
  480. * - One of the ALSA capture callbacks.
  481. *
  482. * Called after closing the device, but before snd_card_saa7134_capture_close
  483. * Usually used in ALSA to free the DMA, but since we don't use the
  484. * ALSA DMA I'm almost sure this isn't necessary.
  485. *
  486. */
  487. static int snd_card_saa7134_hw_free(snd_pcm_substream_t * substream)
  488. {
  489. return 0;
  490. }
  491. /*
  492. * DMA buffer release
  493. *
  494. * Called after closing the device, during snd_card_saa7134_capture_close
  495. *
  496. */
  497. static int dsp_buffer_free(struct saa7134_dev *dev)
  498. {
  499. if (!dev->dmasound.blksize)
  500. BUG();
  501. videobuf_dma_free(&dev->dmasound.dma);
  502. dev->dmasound.blocks = 0;
  503. dev->dmasound.blksize = 0;
  504. dev->dmasound.bufsize = 0;
  505. return 0;
  506. }
  507. /*
  508. * ALSA capture finish
  509. *
  510. * - One of the ALSA capture callbacks.
  511. *
  512. * Called after closing the device. It stops the DMA audio and releases
  513. * the buffers
  514. *
  515. */
  516. static int snd_card_saa7134_capture_close(snd_pcm_substream_t * substream)
  517. {
  518. snd_card_saa7134_t *chip = snd_pcm_substream_chip(substream);
  519. struct saa7134_dev *dev = chip->saadev;
  520. /* unlock buffer */
  521. saa7134_pgtable_free(dev->pci,&dev->dmasound.pt);
  522. videobuf_dma_pci_unmap(dev->pci,&dev->dmasound.dma);
  523. dsp_buffer_free(dev);
  524. return 0;
  525. }
  526. /*
  527. * ALSA capture start
  528. *
  529. * - One of the ALSA capture callbacks.
  530. *
  531. * Called when opening the device. It creates and populates the PCM
  532. * structure
  533. *
  534. */
  535. static int snd_card_saa7134_capture_open(snd_pcm_substream_t * substream)
  536. {
  537. snd_pcm_runtime_t *runtime = substream->runtime;
  538. snd_card_saa7134_pcm_t *saapcm;
  539. snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
  540. struct saa7134_dev *dev = saa7134->saadev;
  541. int err;
  542. down(&dev->dmasound.lock);
  543. dev->dmasound.afmt = SNDRV_PCM_FORMAT_U8;
  544. dev->dmasound.channels = 2;
  545. dev->dmasound.read_count = 0;
  546. dev->dmasound.read_offset = 0;
  547. up(&dev->dmasound.lock);
  548. saapcm = kzalloc(sizeof(*saapcm), GFP_KERNEL);
  549. if (saapcm == NULL)
  550. return -ENOMEM;
  551. saapcm->saadev=saa7134->saadev;
  552. spin_lock_init(&saapcm->lock);
  553. saapcm->substream = substream;
  554. runtime->private_data = saapcm;
  555. runtime->private_free = snd_card_saa7134_runtime_free;
  556. runtime->hw = snd_card_saa7134_capture;
  557. if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
  558. return err;
  559. return 0;
  560. }
  561. /*
  562. * ALSA capture callbacks definition
  563. */
  564. static snd_pcm_ops_t snd_card_saa7134_capture_ops = {
  565. .open = snd_card_saa7134_capture_open,
  566. .close = snd_card_saa7134_capture_close,
  567. .ioctl = snd_pcm_lib_ioctl,
  568. .hw_params = snd_card_saa7134_hw_params,
  569. .hw_free = snd_card_saa7134_hw_free,
  570. .prepare = snd_card_saa7134_capture_prepare,
  571. .trigger = snd_card_saa7134_capture_trigger,
  572. .pointer = snd_card_saa7134_capture_pointer,
  573. };
  574. /*
  575. * ALSA PCM setup
  576. *
  577. * Called when initializing the board. Sets up the name and hooks up
  578. * the callbacks
  579. *
  580. */
  581. static int snd_card_saa7134_pcm(snd_card_saa7134_t *saa7134, int device)
  582. {
  583. snd_pcm_t *pcm;
  584. int err;
  585. if ((err = snd_pcm_new(saa7134->card, "SAA7134 PCM", device, 0, 1, &pcm)) < 0)
  586. return err;
  587. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_card_saa7134_capture_ops);
  588. pcm->private_data = saa7134;
  589. pcm->info_flags = 0;
  590. strcpy(pcm->name, "SAA7134 PCM");
  591. return 0;
  592. }
  593. #define SAA713x_VOLUME(xname, xindex, addr) \
  594. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
  595. .info = snd_saa7134_volume_info, \
  596. .get = snd_saa7134_volume_get, .put = snd_saa7134_volume_put, \
  597. .private_value = addr }
  598. static int snd_saa7134_volume_info(snd_kcontrol_t * kcontrol, snd_ctl_elem_info_t * uinfo)
  599. {
  600. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  601. uinfo->count = 2;
  602. uinfo->value.integer.min = 0;
  603. uinfo->value.integer.max = 20;
  604. return 0;
  605. }
  606. static int snd_saa7134_volume_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
  607. {
  608. snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
  609. int addr = kcontrol->private_value;
  610. ucontrol->value.integer.value[0] = chip->mixer_volume[addr][0];
  611. ucontrol->value.integer.value[1] = chip->mixer_volume[addr][1];
  612. return 0;
  613. }
  614. static int snd_saa7134_volume_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
  615. {
  616. snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
  617. unsigned long flags;
  618. int change, addr = kcontrol->private_value;
  619. int left, right;
  620. left = ucontrol->value.integer.value[0];
  621. if (left < 0)
  622. left = 0;
  623. if (left > 20)
  624. left = 20;
  625. right = ucontrol->value.integer.value[1];
  626. if (right < 0)
  627. right = 0;
  628. if (right > 20)
  629. right = 20;
  630. spin_lock_irqsave(&chip->mixer_lock, flags);
  631. change = chip->mixer_volume[addr][0] != left ||
  632. chip->mixer_volume[addr][1] != right;
  633. chip->mixer_volume[addr][0] = left;
  634. chip->mixer_volume[addr][1] = right;
  635. spin_unlock_irqrestore(&chip->mixer_lock, flags);
  636. return change;
  637. }
  638. #define SAA713x_CAPSRC(xname, xindex, addr) \
  639. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
  640. .info = snd_saa7134_capsrc_info, \
  641. .get = snd_saa7134_capsrc_get, .put = snd_saa7134_capsrc_put, \
  642. .private_value = addr }
  643. static int snd_saa7134_capsrc_info(snd_kcontrol_t * kcontrol, snd_ctl_elem_info_t * uinfo)
  644. {
  645. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  646. uinfo->count = 2;
  647. uinfo->value.integer.min = 0;
  648. uinfo->value.integer.max = 1;
  649. return 0;
  650. }
  651. static int snd_saa7134_capsrc_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
  652. {
  653. snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
  654. unsigned long flags;
  655. int addr = kcontrol->private_value;
  656. spin_lock_irqsave(&chip->mixer_lock, flags);
  657. ucontrol->value.integer.value[0] = chip->capture_source[addr][0];
  658. ucontrol->value.integer.value[1] = chip->capture_source[addr][1];
  659. spin_unlock_irqrestore(&chip->mixer_lock, flags);
  660. return 0;
  661. }
  662. static int snd_saa7134_capsrc_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
  663. {
  664. snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
  665. int change, addr = kcontrol->private_value;
  666. int left, right;
  667. u32 anabar, xbarin;
  668. int analog_io, rate;
  669. struct saa7134_dev *dev;
  670. dev = chip->saadev;
  671. left = ucontrol->value.integer.value[0] & 1;
  672. right = ucontrol->value.integer.value[1] & 1;
  673. spin_lock_irq(&chip->mixer_lock);
  674. change = chip->capture_source[addr][0] != left ||
  675. chip->capture_source[addr][1] != right;
  676. chip->capture_source[addr][0] = left;
  677. chip->capture_source[addr][1] = right;
  678. dev->dmasound.input=addr;
  679. spin_unlock_irq(&chip->mixer_lock);
  680. if (change) {
  681. switch (dev->pci->device) {
  682. case PCI_DEVICE_ID_PHILIPS_SAA7134:
  683. switch (addr) {
  684. case MIXER_ADDR_TVTUNER:
  685. saa_andorb(SAA7134_AUDIO_FORMAT_CTRL, 0xc0, 0xc0);
  686. saa_andorb(SAA7134_SIF_SAMPLE_FREQ, 0x03, 0x00);
  687. break;
  688. case MIXER_ADDR_LINE1:
  689. case MIXER_ADDR_LINE2:
  690. analog_io = (MIXER_ADDR_LINE1 == addr) ? 0x00 : 0x08;
  691. rate = (32000 == dev->dmasound.rate) ? 0x01 : 0x03;
  692. saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x08, analog_io);
  693. saa_andorb(SAA7134_AUDIO_FORMAT_CTRL, 0xc0, 0x80);
  694. saa_andorb(SAA7134_SIF_SAMPLE_FREQ, 0x03, rate);
  695. break;
  696. }
  697. break;
  698. case PCI_DEVICE_ID_PHILIPS_SAA7133:
  699. case PCI_DEVICE_ID_PHILIPS_SAA7135:
  700. xbarin = 0x03; // adc
  701. anabar = 0;
  702. switch (addr) {
  703. case MIXER_ADDR_TVTUNER:
  704. xbarin = 0; // Demodulator
  705. anabar = 2; // DACs
  706. break;
  707. case MIXER_ADDR_LINE1:
  708. anabar = 0; // aux1, aux1
  709. break;
  710. case MIXER_ADDR_LINE2:
  711. anabar = 9; // aux2, aux2
  712. break;
  713. }
  714. /* output xbar always main channel */
  715. saa_dsp_writel(dev, SAA7133_DIGITAL_OUTPUT_SEL1, 0xbbbb10);
  716. if (left || right) { // We've got data, turn the input on
  717. saa_dsp_writel(dev, SAA7133_DIGITAL_INPUT_XBAR1, xbarin);
  718. saa_writel(SAA7133_ANALOG_IO_SELECT, anabar);
  719. } else {
  720. saa_dsp_writel(dev, SAA7133_DIGITAL_INPUT_XBAR1, 0);
  721. saa_writel(SAA7133_ANALOG_IO_SELECT, 0);
  722. }
  723. break;
  724. }
  725. }
  726. return change;
  727. }
  728. static snd_kcontrol_new_t snd_saa7134_controls[] = {
  729. SAA713x_VOLUME("Video Volume", 0, MIXER_ADDR_TVTUNER),
  730. SAA713x_CAPSRC("Video Capture Switch", 0, MIXER_ADDR_TVTUNER),
  731. SAA713x_VOLUME("Line Volume", 1, MIXER_ADDR_LINE1),
  732. SAA713x_CAPSRC("Line Capture Switch", 1, MIXER_ADDR_LINE1),
  733. SAA713x_VOLUME("Line Volume", 2, MIXER_ADDR_LINE2),
  734. SAA713x_CAPSRC("Line Capture Switch", 2, MIXER_ADDR_LINE2),
  735. };
  736. /*
  737. * ALSA mixer setup
  738. *
  739. * Called when initializing the board. Sets up the name and hooks up
  740. * the callbacks
  741. *
  742. */
  743. static int snd_card_saa7134_new_mixer(snd_card_saa7134_t * chip)
  744. {
  745. snd_card_t *card = chip->card;
  746. unsigned int idx;
  747. int err;
  748. snd_assert(chip != NULL, return -EINVAL);
  749. strcpy(card->mixername, "SAA7134 Mixer");
  750. for (idx = 0; idx < ARRAY_SIZE(snd_saa7134_controls); idx++) {
  751. if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_saa7134_controls[idx], chip))) < 0)
  752. return err;
  753. }
  754. return 0;
  755. }
  756. static void snd_saa7134_free(snd_card_t * card)
  757. {
  758. return;
  759. }
  760. static int snd_saa7134_dev_free(snd_device_t *device)
  761. {
  762. snd_card_saa7134_t *chip = device->device_data;
  763. if (chip->irq >= 0) {
  764. synchronize_irq(chip->irq);
  765. free_irq(chip->irq, (void *) chip);
  766. }
  767. return 0;
  768. }
  769. /*
  770. * ALSA initialization
  771. *
  772. * Called by the init routine, once for each saa7134 device present,
  773. * it creates the basic structures and registers the ALSA devices
  774. *
  775. */
  776. int alsa_card_saa7134_create(struct saa7134_dev *saadev, int dev)
  777. {
  778. snd_card_t *card;
  779. snd_card_saa7134_t *chip;
  780. int err;
  781. static snd_device_ops_t ops = {
  782. .dev_free = snd_saa7134_dev_free,
  783. };
  784. if (dev >= SNDRV_CARDS)
  785. return -ENODEV;
  786. if (!enable[dev])
  787. return -ENODEV;
  788. card = snd_card_new(index[dev], id[dev], THIS_MODULE, sizeof(snd_card_saa7134_t));
  789. if (card == NULL)
  790. return -ENOMEM;
  791. strcpy(card->driver, "SAA7134");
  792. /* Card "creation" */
  793. card->private_free = snd_saa7134_free;
  794. chip = (snd_card_saa7134_t *) card->private_data;
  795. spin_lock_init(&chip->lock);
  796. spin_lock_init(&chip->mixer_lock);
  797. chip->saadev = saadev;
  798. chip->card = card;
  799. chip->pci = saadev->pci;
  800. chip->irq = saadev->pci->irq;
  801. chip->iobase = pci_resource_start(saadev->pci, 0);
  802. err = request_irq(saadev->pci->irq, saa7134_alsa_irq,
  803. SA_SHIRQ | SA_INTERRUPT, saadev->name, (void *)chip);
  804. if (err < 0) {
  805. printk(KERN_ERR "%s: can't get IRQ %d for ALSA\n",
  806. saadev->name, saadev->pci->irq);
  807. goto __nodev;
  808. }
  809. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
  810. goto __nodev;
  811. }
  812. if ((err = snd_card_saa7134_new_mixer(chip)) < 0)
  813. goto __nodev;
  814. if ((err = snd_card_saa7134_pcm(chip, 0)) < 0)
  815. goto __nodev;
  816. snd_card_set_dev(card, &chip->pci->dev);
  817. /* End of "creation" */
  818. strcpy(card->shortname, "SAA7134");
  819. sprintf(card->longname, "%s at 0x%lx irq %d",
  820. chip->saadev->name, chip->iobase, chip->irq);
  821. if ((err = snd_card_register(card)) == 0) {
  822. snd_saa7134_cards[dev] = card;
  823. return 0;
  824. }
  825. __nodev:
  826. snd_card_free(card);
  827. return err;
  828. }
  829. /*
  830. * Module initializer
  831. *
  832. * Loops through present saa7134 cards, and assigns an ALSA device
  833. * to each one
  834. *
  835. */
  836. static int saa7134_alsa_init(void)
  837. {
  838. struct saa7134_dev *saadev = NULL;
  839. struct list_head *list;
  840. position = 0;
  841. printk(KERN_INFO "saa7134 ALSA driver for DMA sound loaded\n");
  842. list_for_each(list,&saa7134_devlist) {
  843. saadev = list_entry(list, struct saa7134_dev, devlist);
  844. alsa_card_saa7134_create(saadev,position);
  845. position++;
  846. }
  847. if (saadev == NULL)
  848. printk(KERN_INFO "saa7134 ALSA: no saa7134 cards found\n");
  849. return 0;
  850. }
  851. /*
  852. * Module destructor
  853. */
  854. void saa7134_alsa_exit(void)
  855. {
  856. int idx;
  857. for (idx = 0; idx < SNDRV_CARDS; idx++) {
  858. snd_card_free(snd_saa7134_cards[idx]);
  859. }
  860. printk(KERN_INFO "saa7134 ALSA driver for DMA sound unloaded\n");
  861. return;
  862. }
  863. module_init(saa7134_alsa_init);
  864. module_exit(saa7134_alsa_exit);
  865. MODULE_LICENSE("GPL");
  866. MODULE_AUTHOR("Ricardo Cerqueira");