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