saa7134-alsa.c 30 KB

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