saa7134-alsa.c 25 KB

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