saa7134-alsa.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061
  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)
  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. snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
  492. struct saa7134_dev *dev = saa7134->dev;
  493. dev->ctl_mute = 1;
  494. saa7134_tvaudio_setmute(dev);
  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 err;
  513. mutex_lock(&dev->dmasound.lock);
  514. dev->dmasound.read_count = 0;
  515. dev->dmasound.read_offset = 0;
  516. mutex_unlock(&dev->dmasound.lock);
  517. pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
  518. if (pcm == NULL)
  519. return -ENOMEM;
  520. pcm->dev=saa7134->dev;
  521. spin_lock_init(&pcm->lock);
  522. pcm->substream = substream;
  523. runtime->private_data = pcm;
  524. runtime->private_free = snd_card_saa7134_runtime_free;
  525. runtime->hw = snd_card_saa7134_capture;
  526. dev->ctl_mute = 0;
  527. saa7134_tvaudio_setmute(dev);
  528. if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
  529. return err;
  530. return 0;
  531. }
  532. /*
  533. * ALSA capture callbacks definition
  534. */
  535. static struct snd_pcm_ops snd_card_saa7134_capture_ops = {
  536. .open = snd_card_saa7134_capture_open,
  537. .close = snd_card_saa7134_capture_close,
  538. .ioctl = snd_pcm_lib_ioctl,
  539. .hw_params = snd_card_saa7134_hw_params,
  540. .hw_free = snd_card_saa7134_hw_free,
  541. .prepare = snd_card_saa7134_capture_prepare,
  542. .trigger = snd_card_saa7134_capture_trigger,
  543. .pointer = snd_card_saa7134_capture_pointer,
  544. };
  545. /*
  546. * ALSA PCM setup
  547. *
  548. * Called when initializing the board. Sets up the name and hooks up
  549. * the callbacks
  550. *
  551. */
  552. static int snd_card_saa7134_pcm(snd_card_saa7134_t *saa7134, int device)
  553. {
  554. struct snd_pcm *pcm;
  555. int err;
  556. if ((err = snd_pcm_new(saa7134->card, "SAA7134 PCM", device, 0, 1, &pcm)) < 0)
  557. return err;
  558. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_card_saa7134_capture_ops);
  559. pcm->private_data = saa7134;
  560. pcm->info_flags = 0;
  561. strcpy(pcm->name, "SAA7134 PCM");
  562. return 0;
  563. }
  564. #define SAA713x_VOLUME(xname, xindex, addr) \
  565. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
  566. .info = snd_saa7134_volume_info, \
  567. .get = snd_saa7134_volume_get, .put = snd_saa7134_volume_put, \
  568. .private_value = addr }
  569. static int snd_saa7134_volume_info(struct snd_kcontrol * kcontrol,
  570. struct snd_ctl_elem_info * uinfo)
  571. {
  572. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  573. uinfo->count = 2;
  574. uinfo->value.integer.min = 0;
  575. uinfo->value.integer.max = 20;
  576. return 0;
  577. }
  578. static int snd_saa7134_volume_get(struct snd_kcontrol * kcontrol,
  579. struct snd_ctl_elem_value * ucontrol)
  580. {
  581. snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
  582. int addr = kcontrol->private_value;
  583. ucontrol->value.integer.value[0] = chip->mixer_volume[addr][0];
  584. ucontrol->value.integer.value[1] = chip->mixer_volume[addr][1];
  585. return 0;
  586. }
  587. static int snd_saa7134_volume_put(struct snd_kcontrol * kcontrol,
  588. struct snd_ctl_elem_value * ucontrol)
  589. {
  590. snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
  591. int change, addr = kcontrol->private_value;
  592. int left, right;
  593. left = ucontrol->value.integer.value[0];
  594. if (left < 0)
  595. left = 0;
  596. if (left > 20)
  597. left = 20;
  598. right = ucontrol->value.integer.value[1];
  599. if (right < 0)
  600. right = 0;
  601. if (right > 20)
  602. right = 20;
  603. spin_lock_irq(&chip->mixer_lock);
  604. change = chip->mixer_volume[addr][0] != left ||
  605. chip->mixer_volume[addr][1] != right;
  606. chip->mixer_volume[addr][0] = left;
  607. chip->mixer_volume[addr][1] = right;
  608. spin_unlock_irq(&chip->mixer_lock);
  609. return change;
  610. }
  611. #define SAA713x_CAPSRC(xname, xindex, addr) \
  612. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
  613. .info = snd_saa7134_capsrc_info, \
  614. .get = snd_saa7134_capsrc_get, .put = snd_saa7134_capsrc_put, \
  615. .private_value = addr }
  616. static int snd_saa7134_capsrc_info(struct snd_kcontrol * kcontrol,
  617. struct snd_ctl_elem_info * uinfo)
  618. {
  619. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  620. uinfo->count = 2;
  621. uinfo->value.integer.min = 0;
  622. uinfo->value.integer.max = 1;
  623. return 0;
  624. }
  625. static int snd_saa7134_capsrc_get(struct snd_kcontrol * kcontrol,
  626. struct snd_ctl_elem_value * ucontrol)
  627. {
  628. snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
  629. int addr = kcontrol->private_value;
  630. spin_lock_irq(&chip->mixer_lock);
  631. ucontrol->value.integer.value[0] = chip->capture_source[addr][0];
  632. ucontrol->value.integer.value[1] = chip->capture_source[addr][1];
  633. spin_unlock_irq(&chip->mixer_lock);
  634. return 0;
  635. }
  636. static int snd_saa7134_capsrc_put(struct snd_kcontrol * kcontrol,
  637. struct snd_ctl_elem_value * ucontrol)
  638. {
  639. snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
  640. int change, addr = kcontrol->private_value;
  641. int left, right;
  642. u32 anabar, xbarin;
  643. int analog_io, rate;
  644. struct saa7134_dev *dev;
  645. dev = chip->dev;
  646. left = ucontrol->value.integer.value[0] & 1;
  647. right = ucontrol->value.integer.value[1] & 1;
  648. spin_lock_irq(&chip->mixer_lock);
  649. change = chip->capture_source[addr][0] != left ||
  650. chip->capture_source[addr][1] != right;
  651. chip->capture_source[addr][0] = left;
  652. chip->capture_source[addr][1] = right;
  653. dev->dmasound.input=addr;
  654. spin_unlock_irq(&chip->mixer_lock);
  655. if (change) {
  656. switch (dev->pci->device) {
  657. case PCI_DEVICE_ID_PHILIPS_SAA7134:
  658. switch (addr) {
  659. case MIXER_ADDR_TVTUNER:
  660. saa_andorb(SAA7134_AUDIO_FORMAT_CTRL, 0xc0, 0xc0);
  661. saa_andorb(SAA7134_SIF_SAMPLE_FREQ, 0x03, 0x00);
  662. break;
  663. case MIXER_ADDR_LINE1:
  664. case MIXER_ADDR_LINE2:
  665. analog_io = (MIXER_ADDR_LINE1 == addr) ? 0x00 : 0x08;
  666. rate = (32000 == dev->dmasound.rate) ? 0x01 : 0x03;
  667. saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x08, analog_io);
  668. saa_andorb(SAA7134_AUDIO_FORMAT_CTRL, 0xc0, 0x80);
  669. saa_andorb(SAA7134_SIF_SAMPLE_FREQ, 0x03, rate);
  670. break;
  671. }
  672. break;
  673. case PCI_DEVICE_ID_PHILIPS_SAA7133:
  674. case PCI_DEVICE_ID_PHILIPS_SAA7135:
  675. xbarin = 0x03; // adc
  676. anabar = 0;
  677. switch (addr) {
  678. case MIXER_ADDR_TVTUNER:
  679. xbarin = 0; // Demodulator
  680. anabar = 2; // DACs
  681. break;
  682. case MIXER_ADDR_LINE1:
  683. anabar = 0; // aux1, aux1
  684. break;
  685. case MIXER_ADDR_LINE2:
  686. anabar = 9; // aux2, aux2
  687. break;
  688. }
  689. /* output xbar always main channel */
  690. saa_dsp_writel(dev, SAA7133_DIGITAL_OUTPUT_SEL1, 0xbbbb10);
  691. if (left || right) { // We've got data, turn the input on
  692. saa_dsp_writel(dev, SAA7133_DIGITAL_INPUT_XBAR1, xbarin);
  693. saa_writel(SAA7133_ANALOG_IO_SELECT, anabar);
  694. } else {
  695. saa_dsp_writel(dev, SAA7133_DIGITAL_INPUT_XBAR1, 0);
  696. saa_writel(SAA7133_ANALOG_IO_SELECT, 0);
  697. }
  698. break;
  699. }
  700. }
  701. return change;
  702. }
  703. static struct snd_kcontrol_new snd_saa7134_controls[] = {
  704. SAA713x_VOLUME("Video Volume", 0, MIXER_ADDR_TVTUNER),
  705. SAA713x_CAPSRC("Video Capture Switch", 0, MIXER_ADDR_TVTUNER),
  706. SAA713x_VOLUME("Line Volume", 1, MIXER_ADDR_LINE1),
  707. SAA713x_CAPSRC("Line Capture Switch", 1, MIXER_ADDR_LINE1),
  708. SAA713x_VOLUME("Line Volume", 2, MIXER_ADDR_LINE2),
  709. SAA713x_CAPSRC("Line Capture Switch", 2, MIXER_ADDR_LINE2),
  710. };
  711. /*
  712. * ALSA mixer setup
  713. *
  714. * Called when initializing the board. Sets up the name and hooks up
  715. * the callbacks
  716. *
  717. */
  718. static int snd_card_saa7134_new_mixer(snd_card_saa7134_t * chip)
  719. {
  720. struct snd_card *card = chip->card;
  721. unsigned int idx;
  722. int err;
  723. snd_assert(chip != NULL, return -EINVAL);
  724. strcpy(card->mixername, "SAA7134 Mixer");
  725. for (idx = 0; idx < ARRAY_SIZE(snd_saa7134_controls); idx++) {
  726. if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_saa7134_controls[idx], chip))) < 0)
  727. return err;
  728. }
  729. return 0;
  730. }
  731. static void snd_saa7134_free(struct snd_card * card)
  732. {
  733. snd_card_saa7134_t *chip = card->private_data;
  734. if (chip->dev->dmasound.priv_data == NULL)
  735. return;
  736. if (chip->irq >= 0) {
  737. synchronize_irq(chip->irq);
  738. free_irq(chip->irq, &chip->dev->dmasound);
  739. }
  740. chip->dev->dmasound.priv_data = NULL;
  741. }
  742. /*
  743. * ALSA initialization
  744. *
  745. * Called by the init routine, once for each saa7134 device present,
  746. * it creates the basic structures and registers the ALSA devices
  747. *
  748. */
  749. static int alsa_card_saa7134_create(struct saa7134_dev *dev, int devnum)
  750. {
  751. struct snd_card *card;
  752. snd_card_saa7134_t *chip;
  753. int err;
  754. if (devnum >= SNDRV_CARDS)
  755. return -ENODEV;
  756. if (!enable[devnum])
  757. return -ENODEV;
  758. card = snd_card_new(index[devnum], id[devnum], THIS_MODULE, sizeof(snd_card_saa7134_t));
  759. if (card == NULL)
  760. return -ENOMEM;
  761. strcpy(card->driver, "SAA7134");
  762. /* Card "creation" */
  763. card->private_free = snd_saa7134_free;
  764. chip = (snd_card_saa7134_t *) card->private_data;
  765. spin_lock_init(&chip->lock);
  766. spin_lock_init(&chip->mixer_lock);
  767. chip->dev = dev;
  768. chip->card = card;
  769. chip->pci = dev->pci;
  770. chip->iobase = pci_resource_start(dev->pci, 0);
  771. err = request_irq(dev->pci->irq, saa7134_alsa_irq,
  772. IRQF_SHARED | IRQF_DISABLED, dev->name,
  773. (void*) &dev->dmasound);
  774. if (err < 0) {
  775. printk(KERN_ERR "%s: can't get IRQ %d for ALSA\n",
  776. dev->name, dev->pci->irq);
  777. goto __nodev;
  778. }
  779. chip->irq = dev->pci->irq;
  780. mutex_init(&dev->dmasound.lock);
  781. if ((err = snd_card_saa7134_new_mixer(chip)) < 0)
  782. goto __nodev;
  783. if ((err = snd_card_saa7134_pcm(chip, 0)) < 0)
  784. goto __nodev;
  785. snd_card_set_dev(card, &chip->pci->dev);
  786. /* End of "creation" */
  787. strcpy(card->shortname, "SAA7134");
  788. sprintf(card->longname, "%s at 0x%lx irq %d",
  789. chip->dev->name, chip->iobase, chip->irq);
  790. printk(KERN_INFO "%s/alsa: %s registered as card %d\n",dev->name,card->longname,index[devnum]);
  791. if ((err = snd_card_register(card)) == 0) {
  792. snd_saa7134_cards[devnum] = card;
  793. return 0;
  794. }
  795. __nodev:
  796. snd_card_free(card);
  797. return err;
  798. }
  799. static int alsa_device_init(struct saa7134_dev *dev)
  800. {
  801. dev->dmasound.priv_data = dev;
  802. alsa_card_saa7134_create(dev,dev->nr);
  803. return 1;
  804. }
  805. static int alsa_device_exit(struct saa7134_dev *dev)
  806. {
  807. snd_card_free(snd_saa7134_cards[dev->nr]);
  808. snd_saa7134_cards[dev->nr] = NULL;
  809. return 1;
  810. }
  811. /*
  812. * Module initializer
  813. *
  814. * Loops through present saa7134 cards, and assigns an ALSA device
  815. * to each one
  816. *
  817. */
  818. static int saa7134_alsa_init(void)
  819. {
  820. struct saa7134_dev *dev = NULL;
  821. struct list_head *list;
  822. if (!saa7134_dmasound_init && !saa7134_dmasound_exit) {
  823. saa7134_dmasound_init = alsa_device_init;
  824. saa7134_dmasound_exit = alsa_device_exit;
  825. } else {
  826. printk(KERN_WARNING "saa7134 ALSA: can't load, DMA sound handler already assigned (probably to OSS)\n");
  827. return -EBUSY;
  828. }
  829. printk(KERN_INFO "saa7134 ALSA driver for DMA sound loaded\n");
  830. list_for_each(list,&saa7134_devlist) {
  831. dev = list_entry(list, struct saa7134_dev, devlist);
  832. if (dev->dmasound.priv_data == NULL) {
  833. alsa_device_init(dev);
  834. } else {
  835. printk(KERN_ERR "saa7134 ALSA: DMA sound is being handled by OSS. ignoring %s\n",dev->name);
  836. return -EBUSY;
  837. }
  838. }
  839. if (dev == NULL)
  840. printk(KERN_INFO "saa7134 ALSA: no saa7134 cards found\n");
  841. return 0;
  842. }
  843. /*
  844. * Module destructor
  845. */
  846. static void saa7134_alsa_exit(void)
  847. {
  848. int idx;
  849. for (idx = 0; idx < SNDRV_CARDS; idx++) {
  850. snd_card_free(snd_saa7134_cards[idx]);
  851. }
  852. saa7134_dmasound_init = NULL;
  853. saa7134_dmasound_exit = NULL;
  854. printk(KERN_INFO "saa7134 ALSA driver for DMA sound unloaded\n");
  855. return;
  856. }
  857. /* We initialize this late, to make sure the sound system is up and running */
  858. late_initcall(saa7134_alsa_init);
  859. module_exit(saa7134_alsa_exit);
  860. MODULE_LICENSE("GPL");
  861. MODULE_AUTHOR("Ricardo Cerqueira");