saa7134-alsa.c 30 KB

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