saa7134-alsa.c 25 KB

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