saa7134-alsa.c 27 KB

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