saa7134-alsa.c 27 KB

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