saa7134-alsa.c 25 KB

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