saa7134-alsa.c 25 KB

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