saa7134-alsa.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125
  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/driver.h>
  24. #include <sound/core.h>
  25. #include <sound/control.h>
  26. #include <sound/pcm.h>
  27. #include <sound/pcm_params.h>
  28. #include <sound/initval.h>
  29. #include <linux/interrupt.h>
  30. #include "saa7134.h"
  31. #include "saa7134-reg.h"
  32. static unsigned int debug = 0;
  33. module_param(debug, int, 0644);
  34. MODULE_PARM_DESC(debug,"enable debug messages [alsa]");
  35. /*
  36. * Configuration macros
  37. */
  38. /* defaults */
  39. #define MIXER_ADDR_TVTUNER 0
  40. #define MIXER_ADDR_LINE1 1
  41. #define MIXER_ADDR_LINE2 2
  42. #define MIXER_ADDR_LAST 2
  43. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
  44. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  45. static int enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 1};
  46. module_param_array(index, int, NULL, 0444);
  47. module_param_array(enable, int, NULL, 0444);
  48. MODULE_PARM_DESC(index, "Index value for SAA7134 capture interface(s).");
  49. MODULE_PARM_DESC(enable, "Enable (or not) the SAA7134 capture interface(s).");
  50. #define dprintk(fmt, arg...) if (debug) \
  51. printk(KERN_DEBUG "%s/alsa: " fmt, dev->name , ##arg)
  52. /*
  53. * Main chip structure
  54. */
  55. typedef struct snd_card_saa7134 {
  56. struct snd_card *card;
  57. spinlock_t mixer_lock;
  58. int mixer_volume[MIXER_ADDR_LAST+1][2];
  59. int capture_source[MIXER_ADDR_LAST+1][2];
  60. struct pci_dev *pci;
  61. struct saa7134_dev *dev;
  62. unsigned long iobase;
  63. s16 irq;
  64. u16 mute_was_on;
  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 *dev;
  72. spinlock_t lock;
  73. struct snd_pcm_substream *substream;
  74. } snd_card_saa7134_pcm_t;
  75. static struct snd_card *snd_saa7134_cards[SNDRV_CARDS];
  76. /*
  77. * saa7134 DMA audio stop
  78. *
  79. * Called when the capture device is released or the buffer overflows
  80. *
  81. * - Copied verbatim from saa7134-oss's dsp_dma_stop.
  82. *
  83. */
  84. static void saa7134_dma_stop(struct saa7134_dev *dev)
  85. {
  86. dev->dmasound.dma_blk = -1;
  87. dev->dmasound.dma_running = 0;
  88. saa7134_set_dmabits(dev);
  89. }
  90. /*
  91. * saa7134 DMA audio start
  92. *
  93. * Called when preparing the capture device for use
  94. *
  95. * - Copied verbatim from saa7134-oss's dsp_dma_start.
  96. *
  97. */
  98. static void saa7134_dma_start(struct saa7134_dev *dev)
  99. {
  100. dev->dmasound.dma_blk = 0;
  101. dev->dmasound.dma_running = 1;
  102. saa7134_set_dmabits(dev);
  103. }
  104. /*
  105. * saa7134 audio DMA IRQ handler
  106. *
  107. * Called whenever we get an SAA7134_IRQ_REPORT_DONE_RA3 interrupt
  108. * Handles shifting between the 2 buffers, manages the read counters,
  109. * and notifies ALSA when periods elapse
  110. *
  111. * - Mostly copied from saa7134-oss's saa7134_irq_oss_done.
  112. *
  113. */
  114. static void saa7134_irq_alsa_done(struct saa7134_dev *dev,
  115. unsigned long status)
  116. {
  117. int next_blk, reg = 0;
  118. spin_lock(&dev->slock);
  119. if (UNSET == dev->dmasound.dma_blk) {
  120. dprintk("irq: recording stopped\n");
  121. goto done;
  122. }
  123. if (0 != (status & 0x0f000000))
  124. dprintk("irq: lost %ld\n", (status >> 24) & 0x0f);
  125. if (0 == (status & 0x10000000)) {
  126. /* odd */
  127. if (0 == (dev->dmasound.dma_blk & 0x01))
  128. reg = SAA7134_RS_BA1(6);
  129. } else {
  130. /* even */
  131. if (1 == (dev->dmasound.dma_blk & 0x01))
  132. reg = SAA7134_RS_BA2(6);
  133. }
  134. if (0 == reg) {
  135. dprintk("irq: field oops [%s]\n",
  136. (status & 0x10000000) ? "even" : "odd");
  137. goto done;
  138. }
  139. if (dev->dmasound.read_count >= dev->dmasound.blksize * (dev->dmasound.blocks-2)) {
  140. dprintk("irq: overrun [full=%d/%d] - Blocks in %d\n",dev->dmasound.read_count,
  141. dev->dmasound.bufsize, dev->dmasound.blocks);
  142. spin_unlock(&dev->slock);
  143. snd_pcm_stop(dev->dmasound.substream,SNDRV_PCM_STATE_XRUN);
  144. return;
  145. }
  146. /* next block addr */
  147. next_blk = (dev->dmasound.dma_blk + 2) % dev->dmasound.blocks;
  148. saa_writel(reg,next_blk * dev->dmasound.blksize);
  149. if (debug > 2)
  150. dprintk("irq: ok, %s, next_blk=%d, addr=%x, blocks=%u, size=%u, read=%u\n",
  151. (status & 0x10000000) ? "even" : "odd ", next_blk,
  152. next_blk * dev->dmasound.blksize, dev->dmasound.blocks, dev->dmasound.blksize, dev->dmasound.read_count);
  153. /* update status & wake waiting readers */
  154. dev->dmasound.dma_blk = (dev->dmasound.dma_blk + 1) % dev->dmasound.blocks;
  155. dev->dmasound.read_count += dev->dmasound.blksize;
  156. dev->dmasound.recording_on = reg;
  157. if (dev->dmasound.read_count >= snd_pcm_lib_period_bytes(dev->dmasound.substream)) {
  158. spin_unlock(&dev->slock);
  159. snd_pcm_period_elapsed(dev->dmasound.substream);
  160. spin_lock(&dev->slock);
  161. }
  162. done:
  163. spin_unlock(&dev->slock);
  164. }
  165. /*
  166. * IRQ request handler
  167. *
  168. * Runs along with saa7134's IRQ handler, discards anything that isn't
  169. * DMA sound
  170. *
  171. */
  172. static irqreturn_t saa7134_alsa_irq(int irq, void *dev_id)
  173. {
  174. struct saa7134_dmasound *dmasound = dev_id;
  175. struct saa7134_dev *dev = dmasound->priv_data;
  176. unsigned long report, status;
  177. int loop, handled = 0;
  178. for (loop = 0; loop < 10; loop++) {
  179. report = saa_readl(SAA7134_IRQ_REPORT);
  180. status = saa_readl(SAA7134_IRQ_STATUS);
  181. if (report & SAA7134_IRQ_REPORT_DONE_RA3) {
  182. handled = 1;
  183. saa_writel(SAA7134_IRQ_REPORT,report);
  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 = 2,
  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 >= 2, 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_pci_dma_unmap(dev->pci, &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_pci_dma_map(dev->pci, &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_pci_dma_unmap(dev->pci, &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_pci_dma_unmap(dev->pci, &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_pci_dma_unmap(dev->pci, &dev->dmasound.dma);
  474. dsp_buffer_free(dev);
  475. substream->runtime->dma_area = NULL;
  476. }
  477. return 0;
  478. }
  479. /*
  480. * ALSA capture finish
  481. *
  482. * - One of the ALSA capture callbacks.
  483. *
  484. * Called after closing the device.
  485. *
  486. */
  487. static int snd_card_saa7134_capture_close(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. if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
  536. return err;
  537. return 0;
  538. }
  539. /*
  540. * page callback (needed for mmap)
  541. */
  542. static struct page *snd_card_saa7134_page(struct snd_pcm_substream *substream,
  543. unsigned long offset)
  544. {
  545. void *pageptr = substream->runtime->dma_area + offset;
  546. return vmalloc_to_page(pageptr);
  547. }
  548. /*
  549. * ALSA capture callbacks definition
  550. */
  551. static struct snd_pcm_ops snd_card_saa7134_capture_ops = {
  552. .open = snd_card_saa7134_capture_open,
  553. .close = snd_card_saa7134_capture_close,
  554. .ioctl = snd_pcm_lib_ioctl,
  555. .hw_params = snd_card_saa7134_hw_params,
  556. .hw_free = snd_card_saa7134_hw_free,
  557. .prepare = snd_card_saa7134_capture_prepare,
  558. .trigger = snd_card_saa7134_capture_trigger,
  559. .pointer = snd_card_saa7134_capture_pointer,
  560. .page = snd_card_saa7134_page,
  561. };
  562. /*
  563. * ALSA PCM setup
  564. *
  565. * Called when initializing the board. Sets up the name and hooks up
  566. * the callbacks
  567. *
  568. */
  569. static int snd_card_saa7134_pcm(snd_card_saa7134_t *saa7134, int device)
  570. {
  571. struct snd_pcm *pcm;
  572. int err;
  573. if ((err = snd_pcm_new(saa7134->card, "SAA7134 PCM", device, 0, 1, &pcm)) < 0)
  574. return err;
  575. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_card_saa7134_capture_ops);
  576. pcm->private_data = saa7134;
  577. pcm->info_flags = 0;
  578. strcpy(pcm->name, "SAA7134 PCM");
  579. return 0;
  580. }
  581. #define SAA713x_VOLUME(xname, xindex, addr) \
  582. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
  583. .info = snd_saa7134_volume_info, \
  584. .get = snd_saa7134_volume_get, .put = snd_saa7134_volume_put, \
  585. .private_value = addr }
  586. static int snd_saa7134_volume_info(struct snd_kcontrol * kcontrol,
  587. struct snd_ctl_elem_info * uinfo)
  588. {
  589. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  590. uinfo->count = 2;
  591. uinfo->value.integer.min = 0;
  592. uinfo->value.integer.max = 20;
  593. return 0;
  594. }
  595. static int snd_saa7134_volume_get(struct snd_kcontrol * kcontrol,
  596. struct snd_ctl_elem_value * ucontrol)
  597. {
  598. snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
  599. int addr = kcontrol->private_value;
  600. ucontrol->value.integer.value[0] = chip->mixer_volume[addr][0];
  601. ucontrol->value.integer.value[1] = chip->mixer_volume[addr][1];
  602. return 0;
  603. }
  604. static int snd_saa7134_volume_put(struct snd_kcontrol * kcontrol,
  605. struct snd_ctl_elem_value * ucontrol)
  606. {
  607. snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
  608. struct saa7134_dev *dev = chip->dev;
  609. int change, addr = kcontrol->private_value;
  610. int left, right;
  611. left = ucontrol->value.integer.value[0];
  612. if (left < 0)
  613. left = 0;
  614. if (left > 20)
  615. left = 20;
  616. right = ucontrol->value.integer.value[1];
  617. if (right < 0)
  618. right = 0;
  619. if (right > 20)
  620. right = 20;
  621. spin_lock_irq(&chip->mixer_lock);
  622. change = 0;
  623. if (chip->mixer_volume[addr][0] != left) {
  624. change = 1;
  625. right = left;
  626. }
  627. if (chip->mixer_volume[addr][1] != right) {
  628. change = 1;
  629. left = right;
  630. }
  631. if (change) {
  632. switch (dev->pci->device) {
  633. case PCI_DEVICE_ID_PHILIPS_SAA7134:
  634. switch (addr) {
  635. case MIXER_ADDR_TVTUNER:
  636. left = 20;
  637. break;
  638. case MIXER_ADDR_LINE1:
  639. saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x10,
  640. (left > 10) ? 0x00 : 0x10);
  641. break;
  642. case MIXER_ADDR_LINE2:
  643. saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x20,
  644. (left > 10) ? 0x00 : 0x20);
  645. break;
  646. }
  647. break;
  648. case PCI_DEVICE_ID_PHILIPS_SAA7133:
  649. case PCI_DEVICE_ID_PHILIPS_SAA7135:
  650. switch (addr) {
  651. case MIXER_ADDR_TVTUNER:
  652. left = 20;
  653. break;
  654. case MIXER_ADDR_LINE1:
  655. saa_andorb(0x0594, 0x10,
  656. (left > 10) ? 0x00 : 0x10);
  657. break;
  658. case MIXER_ADDR_LINE2:
  659. saa_andorb(0x0594, 0x20,
  660. (left > 10) ? 0x00 : 0x20);
  661. break;
  662. }
  663. break;
  664. }
  665. chip->mixer_volume[addr][0] = left;
  666. chip->mixer_volume[addr][1] = right;
  667. }
  668. spin_unlock_irq(&chip->mixer_lock);
  669. return change;
  670. }
  671. #define SAA713x_CAPSRC(xname, xindex, addr) \
  672. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
  673. .info = snd_saa7134_capsrc_info, \
  674. .get = snd_saa7134_capsrc_get, .put = snd_saa7134_capsrc_put, \
  675. .private_value = addr }
  676. static int snd_saa7134_capsrc_info(struct snd_kcontrol * kcontrol,
  677. struct snd_ctl_elem_info * uinfo)
  678. {
  679. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  680. uinfo->count = 2;
  681. uinfo->value.integer.min = 0;
  682. uinfo->value.integer.max = 1;
  683. return 0;
  684. }
  685. static int snd_saa7134_capsrc_get(struct snd_kcontrol * kcontrol,
  686. struct snd_ctl_elem_value * ucontrol)
  687. {
  688. snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
  689. int addr = kcontrol->private_value;
  690. spin_lock_irq(&chip->mixer_lock);
  691. ucontrol->value.integer.value[0] = chip->capture_source[addr][0];
  692. ucontrol->value.integer.value[1] = chip->capture_source[addr][1];
  693. spin_unlock_irq(&chip->mixer_lock);
  694. return 0;
  695. }
  696. static int snd_saa7134_capsrc_put(struct snd_kcontrol * kcontrol,
  697. struct snd_ctl_elem_value * ucontrol)
  698. {
  699. snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
  700. int change, addr = kcontrol->private_value;
  701. int left, right;
  702. u32 anabar, xbarin;
  703. int analog_io, rate;
  704. struct saa7134_dev *dev;
  705. dev = chip->dev;
  706. left = ucontrol->value.integer.value[0] & 1;
  707. right = ucontrol->value.integer.value[1] & 1;
  708. spin_lock_irq(&chip->mixer_lock);
  709. change = chip->capture_source[addr][0] != left ||
  710. chip->capture_source[addr][1] != right;
  711. chip->capture_source[addr][0] = left;
  712. chip->capture_source[addr][1] = right;
  713. dev->dmasound.input=addr;
  714. spin_unlock_irq(&chip->mixer_lock);
  715. if (change) {
  716. switch (dev->pci->device) {
  717. case PCI_DEVICE_ID_PHILIPS_SAA7134:
  718. switch (addr) {
  719. case MIXER_ADDR_TVTUNER:
  720. saa_andorb(SAA7134_AUDIO_FORMAT_CTRL, 0xc0, 0xc0);
  721. saa_andorb(SAA7134_SIF_SAMPLE_FREQ, 0x03, 0x00);
  722. break;
  723. case MIXER_ADDR_LINE1:
  724. case MIXER_ADDR_LINE2:
  725. analog_io = (MIXER_ADDR_LINE1 == addr) ? 0x00 : 0x08;
  726. rate = (32000 == dev->dmasound.rate) ? 0x01 : 0x03;
  727. saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x08, analog_io);
  728. saa_andorb(SAA7134_AUDIO_FORMAT_CTRL, 0xc0, 0x80);
  729. saa_andorb(SAA7134_SIF_SAMPLE_FREQ, 0x03, rate);
  730. break;
  731. }
  732. break;
  733. case PCI_DEVICE_ID_PHILIPS_SAA7133:
  734. case PCI_DEVICE_ID_PHILIPS_SAA7135:
  735. xbarin = 0x03; // adc
  736. anabar = 0;
  737. switch (addr) {
  738. case MIXER_ADDR_TVTUNER:
  739. xbarin = 0; // Demodulator
  740. anabar = 2; // DACs
  741. break;
  742. case MIXER_ADDR_LINE1:
  743. anabar = 0; // aux1, aux1
  744. break;
  745. case MIXER_ADDR_LINE2:
  746. anabar = 9; // aux2, aux2
  747. break;
  748. }
  749. /* output xbar always main channel */
  750. saa_dsp_writel(dev, SAA7133_DIGITAL_OUTPUT_SEL1, 0xbbbb10);
  751. if (left || right) { // We've got data, turn the input on
  752. saa_dsp_writel(dev, SAA7133_DIGITAL_INPUT_XBAR1, xbarin);
  753. saa_writel(SAA7133_ANALOG_IO_SELECT, anabar);
  754. } else {
  755. saa_dsp_writel(dev, SAA7133_DIGITAL_INPUT_XBAR1, 0);
  756. saa_writel(SAA7133_ANALOG_IO_SELECT, 0);
  757. }
  758. break;
  759. }
  760. }
  761. return change;
  762. }
  763. static struct snd_kcontrol_new snd_saa7134_controls[] = {
  764. SAA713x_VOLUME("Video Volume", 0, MIXER_ADDR_TVTUNER),
  765. SAA713x_CAPSRC("Video Capture Switch", 0, MIXER_ADDR_TVTUNER),
  766. SAA713x_VOLUME("Line Volume", 1, MIXER_ADDR_LINE1),
  767. SAA713x_CAPSRC("Line Capture Switch", 1, MIXER_ADDR_LINE1),
  768. SAA713x_VOLUME("Line Volume", 2, MIXER_ADDR_LINE2),
  769. SAA713x_CAPSRC("Line Capture Switch", 2, MIXER_ADDR_LINE2),
  770. };
  771. /*
  772. * ALSA mixer setup
  773. *
  774. * Called when initializing the board. Sets up the name and hooks up
  775. * the callbacks
  776. *
  777. */
  778. static int snd_card_saa7134_new_mixer(snd_card_saa7134_t * chip)
  779. {
  780. struct snd_card *card = chip->card;
  781. unsigned int idx;
  782. int err;
  783. snd_assert(chip != NULL, return -EINVAL);
  784. strcpy(card->mixername, "SAA7134 Mixer");
  785. for (idx = 0; idx < ARRAY_SIZE(snd_saa7134_controls); idx++) {
  786. if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_saa7134_controls[idx], chip))) < 0)
  787. return err;
  788. }
  789. return 0;
  790. }
  791. static void snd_saa7134_free(struct snd_card * card)
  792. {
  793. snd_card_saa7134_t *chip = card->private_data;
  794. if (chip->dev->dmasound.priv_data == NULL)
  795. return;
  796. if (chip->irq >= 0) {
  797. synchronize_irq(chip->irq);
  798. free_irq(chip->irq, &chip->dev->dmasound);
  799. }
  800. chip->dev->dmasound.priv_data = NULL;
  801. }
  802. /*
  803. * ALSA initialization
  804. *
  805. * Called by the init routine, once for each saa7134 device present,
  806. * it creates the basic structures and registers the ALSA devices
  807. *
  808. */
  809. static int alsa_card_saa7134_create(struct saa7134_dev *dev, int devnum)
  810. {
  811. struct snd_card *card;
  812. snd_card_saa7134_t *chip;
  813. int err;
  814. if (devnum >= SNDRV_CARDS)
  815. return -ENODEV;
  816. if (!enable[devnum])
  817. return -ENODEV;
  818. card = snd_card_new(index[devnum], id[devnum], THIS_MODULE, sizeof(snd_card_saa7134_t));
  819. if (card == NULL)
  820. return -ENOMEM;
  821. strcpy(card->driver, "SAA7134");
  822. /* Card "creation" */
  823. card->private_free = snd_saa7134_free;
  824. chip = (snd_card_saa7134_t *) card->private_data;
  825. spin_lock_init(&chip->lock);
  826. spin_lock_init(&chip->mixer_lock);
  827. chip->dev = dev;
  828. chip->card = card;
  829. chip->pci = dev->pci;
  830. chip->iobase = pci_resource_start(dev->pci, 0);
  831. err = request_irq(dev->pci->irq, saa7134_alsa_irq,
  832. IRQF_SHARED | IRQF_DISABLED, dev->name,
  833. (void*) &dev->dmasound);
  834. if (err < 0) {
  835. printk(KERN_ERR "%s: can't get IRQ %d for ALSA\n",
  836. dev->name, dev->pci->irq);
  837. goto __nodev;
  838. }
  839. chip->irq = dev->pci->irq;
  840. mutex_init(&dev->dmasound.lock);
  841. if ((err = snd_card_saa7134_new_mixer(chip)) < 0)
  842. goto __nodev;
  843. if ((err = snd_card_saa7134_pcm(chip, 0)) < 0)
  844. goto __nodev;
  845. snd_card_set_dev(card, &chip->pci->dev);
  846. /* End of "creation" */
  847. strcpy(card->shortname, "SAA7134");
  848. sprintf(card->longname, "%s at 0x%lx irq %d",
  849. chip->dev->name, chip->iobase, chip->irq);
  850. printk(KERN_INFO "%s/alsa: %s registered as card %d\n",dev->name,card->longname,index[devnum]);
  851. if ((err = snd_card_register(card)) == 0) {
  852. snd_saa7134_cards[devnum] = card;
  853. return 0;
  854. }
  855. __nodev:
  856. snd_card_free(card);
  857. return err;
  858. }
  859. static int alsa_device_init(struct saa7134_dev *dev)
  860. {
  861. dev->dmasound.priv_data = dev;
  862. alsa_card_saa7134_create(dev,dev->nr);
  863. return 1;
  864. }
  865. static int alsa_device_exit(struct saa7134_dev *dev)
  866. {
  867. snd_card_free(snd_saa7134_cards[dev->nr]);
  868. snd_saa7134_cards[dev->nr] = NULL;
  869. return 1;
  870. }
  871. /*
  872. * Module initializer
  873. *
  874. * Loops through present saa7134 cards, and assigns an ALSA device
  875. * to each one
  876. *
  877. */
  878. static int saa7134_alsa_init(void)
  879. {
  880. struct saa7134_dev *dev = NULL;
  881. struct list_head *list;
  882. if (!saa7134_dmasound_init && !saa7134_dmasound_exit) {
  883. saa7134_dmasound_init = alsa_device_init;
  884. saa7134_dmasound_exit = alsa_device_exit;
  885. } else {
  886. printk(KERN_WARNING "saa7134 ALSA: can't load, DMA sound handler already assigned (probably to OSS)\n");
  887. return -EBUSY;
  888. }
  889. printk(KERN_INFO "saa7134 ALSA driver for DMA sound loaded\n");
  890. list_for_each(list,&saa7134_devlist) {
  891. dev = list_entry(list, struct saa7134_dev, devlist);
  892. if (dev->dmasound.priv_data == NULL) {
  893. alsa_device_init(dev);
  894. } else {
  895. printk(KERN_ERR "saa7134 ALSA: DMA sound is being handled by OSS. ignoring %s\n",dev->name);
  896. return -EBUSY;
  897. }
  898. }
  899. if (dev == NULL)
  900. printk(KERN_INFO "saa7134 ALSA: no saa7134 cards found\n");
  901. return 0;
  902. }
  903. /*
  904. * Module destructor
  905. */
  906. static void saa7134_alsa_exit(void)
  907. {
  908. int idx;
  909. for (idx = 0; idx < SNDRV_CARDS; idx++) {
  910. snd_card_free(snd_saa7134_cards[idx]);
  911. }
  912. saa7134_dmasound_init = NULL;
  913. saa7134_dmasound_exit = NULL;
  914. printk(KERN_INFO "saa7134 ALSA driver for DMA sound unloaded\n");
  915. return;
  916. }
  917. /* We initialize this late, to make sure the sound system is up and running */
  918. late_initcall(saa7134_alsa_init);
  919. module_exit(saa7134_alsa_exit);
  920. MODULE_LICENSE("GPL");
  921. MODULE_AUTHOR("Ricardo Cerqueira");