saa7134-alsa.c 27 KB

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