saa7134-alsa.c 27 KB

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