saa7134-alsa.c 25 KB

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