saa7134-alsa.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133
  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/module.h>
  23. #include <sound/driver.h>
  24. #include <sound/core.h>
  25. #include <sound/control.h>
  26. #include <sound/pcm.h>
  27. #include <sound/pcm_params.h>
  28. #include <sound/initval.h>
  29. #include <linux/interrupt.h>
  30. #include "saa7134.h"
  31. #include "saa7134-reg.h"
  32. static unsigned int debug = 0;
  33. module_param(debug, int, 0644);
  34. MODULE_PARM_DESC(debug,"enable debug messages [alsa]");
  35. /*
  36. * Configuration macros
  37. */
  38. /* defaults */
  39. #define MIXER_ADDR_TVTUNER 0
  40. #define MIXER_ADDR_LINE1 1
  41. #define MIXER_ADDR_LINE2 2
  42. #define MIXER_ADDR_LAST 2
  43. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
  44. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  45. static int enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 1};
  46. module_param_array(index, int, NULL, 0444);
  47. module_param_array(enable, int, NULL, 0444);
  48. MODULE_PARM_DESC(index, "Index value for SAA7134 capture interface(s).");
  49. MODULE_PARM_DESC(enable, "Enable (or not) the SAA7134 capture interface(s).");
  50. #define dprintk(fmt, arg...) if (debug) \
  51. printk(KERN_DEBUG "%s/alsa: " fmt, dev->name , ##arg)
  52. /*
  53. * Main chip structure
  54. */
  55. typedef struct snd_card_saa7134 {
  56. struct snd_card *card;
  57. spinlock_t mixer_lock;
  58. int mixer_volume[MIXER_ADDR_LAST+1][2];
  59. int capture_source[MIXER_ADDR_LAST+1][2];
  60. struct pci_dev *pci;
  61. struct saa7134_dev *dev;
  62. unsigned long iobase;
  63. s16 irq;
  64. u16 mute_was_on;
  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,
  184. SAA7134_IRQ_REPORT_DONE_RA3);
  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 = 4,
  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 >= 4, 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. substream->runtime->dma_bytes = dev->dmasound.bufsize;
  455. substream->runtime->dma_addr = 0;
  456. return 0;
  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(struct snd_pcm_substream * 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_pci_dma_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(struct snd_pcm_substream * substream)
  489. {
  490. snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
  491. struct saa7134_dev *dev = saa7134->dev;
  492. if (saa7134->mute_was_on) {
  493. dev->ctl_mute = 1;
  494. saa7134_tvaudio_setmute(dev);
  495. }
  496. return 0;
  497. }
  498. /*
  499. * ALSA capture start
  500. *
  501. * - One of the ALSA capture callbacks.
  502. *
  503. * Called when opening the device. It creates and populates the PCM
  504. * structure
  505. *
  506. */
  507. static int snd_card_saa7134_capture_open(struct snd_pcm_substream * substream)
  508. {
  509. struct snd_pcm_runtime *runtime = substream->runtime;
  510. snd_card_saa7134_pcm_t *pcm;
  511. snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
  512. struct saa7134_dev *dev = saa7134->dev;
  513. int amux, err;
  514. mutex_lock(&dev->dmasound.lock);
  515. dev->dmasound.read_count = 0;
  516. dev->dmasound.read_offset = 0;
  517. amux = dev->input->amux;
  518. if ((amux < 1) || (amux > 3))
  519. amux = 1;
  520. dev->dmasound.input = amux - 1;
  521. mutex_unlock(&dev->dmasound.lock);
  522. pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
  523. if (pcm == NULL)
  524. return -ENOMEM;
  525. pcm->dev=saa7134->dev;
  526. spin_lock_init(&pcm->lock);
  527. pcm->substream = substream;
  528. runtime->private_data = pcm;
  529. runtime->private_free = snd_card_saa7134_runtime_free;
  530. runtime->hw = snd_card_saa7134_capture;
  531. if (dev->ctl_mute != 0) {
  532. saa7134->mute_was_on = 1;
  533. dev->ctl_mute = 0;
  534. saa7134_tvaudio_setmute(dev);
  535. }
  536. err = snd_pcm_hw_constraint_integer(runtime,
  537. SNDRV_PCM_HW_PARAM_PERIODS);
  538. if (err < 0)
  539. return err;
  540. err = snd_pcm_hw_constraint_step(runtime, 0,
  541. SNDRV_PCM_HW_PARAM_PERIODS, 2);
  542. if (err < 0)
  543. return err;
  544. return 0;
  545. }
  546. /*
  547. * page callback (needed for mmap)
  548. */
  549. static struct page *snd_card_saa7134_page(struct snd_pcm_substream *substream,
  550. unsigned long offset)
  551. {
  552. void *pageptr = substream->runtime->dma_area + offset;
  553. return vmalloc_to_page(pageptr);
  554. }
  555. /*
  556. * ALSA capture callbacks definition
  557. */
  558. static struct snd_pcm_ops snd_card_saa7134_capture_ops = {
  559. .open = snd_card_saa7134_capture_open,
  560. .close = snd_card_saa7134_capture_close,
  561. .ioctl = snd_pcm_lib_ioctl,
  562. .hw_params = snd_card_saa7134_hw_params,
  563. .hw_free = snd_card_saa7134_hw_free,
  564. .prepare = snd_card_saa7134_capture_prepare,
  565. .trigger = snd_card_saa7134_capture_trigger,
  566. .pointer = snd_card_saa7134_capture_pointer,
  567. .page = snd_card_saa7134_page,
  568. };
  569. /*
  570. * ALSA PCM setup
  571. *
  572. * Called when initializing the board. Sets up the name and hooks up
  573. * the callbacks
  574. *
  575. */
  576. static int snd_card_saa7134_pcm(snd_card_saa7134_t *saa7134, int device)
  577. {
  578. struct snd_pcm *pcm;
  579. int err;
  580. if ((err = snd_pcm_new(saa7134->card, "SAA7134 PCM", device, 0, 1, &pcm)) < 0)
  581. return err;
  582. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_card_saa7134_capture_ops);
  583. pcm->private_data = saa7134;
  584. pcm->info_flags = 0;
  585. strcpy(pcm->name, "SAA7134 PCM");
  586. return 0;
  587. }
  588. #define SAA713x_VOLUME(xname, xindex, addr) \
  589. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
  590. .info = snd_saa7134_volume_info, \
  591. .get = snd_saa7134_volume_get, .put = snd_saa7134_volume_put, \
  592. .private_value = addr }
  593. static int snd_saa7134_volume_info(struct snd_kcontrol * kcontrol,
  594. struct snd_ctl_elem_info * uinfo)
  595. {
  596. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  597. uinfo->count = 2;
  598. uinfo->value.integer.min = 0;
  599. uinfo->value.integer.max = 20;
  600. return 0;
  601. }
  602. static int snd_saa7134_volume_get(struct snd_kcontrol * kcontrol,
  603. struct snd_ctl_elem_value * ucontrol)
  604. {
  605. snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
  606. int addr = kcontrol->private_value;
  607. ucontrol->value.integer.value[0] = chip->mixer_volume[addr][0];
  608. ucontrol->value.integer.value[1] = chip->mixer_volume[addr][1];
  609. return 0;
  610. }
  611. static int snd_saa7134_volume_put(struct snd_kcontrol * kcontrol,
  612. struct snd_ctl_elem_value * ucontrol)
  613. {
  614. snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
  615. struct saa7134_dev *dev = chip->dev;
  616. int change, addr = kcontrol->private_value;
  617. int left, right;
  618. left = ucontrol->value.integer.value[0];
  619. if (left < 0)
  620. left = 0;
  621. if (left > 20)
  622. left = 20;
  623. right = ucontrol->value.integer.value[1];
  624. if (right < 0)
  625. right = 0;
  626. if (right > 20)
  627. right = 20;
  628. spin_lock_irq(&chip->mixer_lock);
  629. change = 0;
  630. if (chip->mixer_volume[addr][0] != left) {
  631. change = 1;
  632. right = left;
  633. }
  634. if (chip->mixer_volume[addr][1] != right) {
  635. change = 1;
  636. left = right;
  637. }
  638. if (change) {
  639. switch (dev->pci->device) {
  640. case PCI_DEVICE_ID_PHILIPS_SAA7134:
  641. switch (addr) {
  642. case MIXER_ADDR_TVTUNER:
  643. left = 20;
  644. break;
  645. case MIXER_ADDR_LINE1:
  646. saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x10,
  647. (left > 10) ? 0x00 : 0x10);
  648. break;
  649. case MIXER_ADDR_LINE2:
  650. saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x20,
  651. (left > 10) ? 0x00 : 0x20);
  652. break;
  653. }
  654. break;
  655. case PCI_DEVICE_ID_PHILIPS_SAA7133:
  656. case PCI_DEVICE_ID_PHILIPS_SAA7135:
  657. switch (addr) {
  658. case MIXER_ADDR_TVTUNER:
  659. left = 20;
  660. break;
  661. case MIXER_ADDR_LINE1:
  662. saa_andorb(0x0594, 0x10,
  663. (left > 10) ? 0x00 : 0x10);
  664. break;
  665. case MIXER_ADDR_LINE2:
  666. saa_andorb(0x0594, 0x20,
  667. (left > 10) ? 0x00 : 0x20);
  668. break;
  669. }
  670. break;
  671. }
  672. chip->mixer_volume[addr][0] = left;
  673. chip->mixer_volume[addr][1] = right;
  674. }
  675. spin_unlock_irq(&chip->mixer_lock);
  676. return change;
  677. }
  678. #define SAA713x_CAPSRC(xname, xindex, addr) \
  679. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
  680. .info = snd_saa7134_capsrc_info, \
  681. .get = snd_saa7134_capsrc_get, .put = snd_saa7134_capsrc_put, \
  682. .private_value = addr }
  683. static int snd_saa7134_capsrc_info(struct snd_kcontrol * kcontrol,
  684. struct snd_ctl_elem_info * uinfo)
  685. {
  686. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  687. uinfo->count = 2;
  688. uinfo->value.integer.min = 0;
  689. uinfo->value.integer.max = 1;
  690. return 0;
  691. }
  692. static int snd_saa7134_capsrc_get(struct snd_kcontrol * kcontrol,
  693. struct snd_ctl_elem_value * ucontrol)
  694. {
  695. snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
  696. int addr = kcontrol->private_value;
  697. spin_lock_irq(&chip->mixer_lock);
  698. ucontrol->value.integer.value[0] = chip->capture_source[addr][0];
  699. ucontrol->value.integer.value[1] = chip->capture_source[addr][1];
  700. spin_unlock_irq(&chip->mixer_lock);
  701. return 0;
  702. }
  703. static int snd_saa7134_capsrc_put(struct snd_kcontrol * kcontrol,
  704. struct snd_ctl_elem_value * ucontrol)
  705. {
  706. snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
  707. int change, addr = kcontrol->private_value;
  708. int left, right;
  709. u32 anabar, xbarin;
  710. int analog_io, rate;
  711. struct saa7134_dev *dev;
  712. dev = chip->dev;
  713. left = ucontrol->value.integer.value[0] & 1;
  714. right = ucontrol->value.integer.value[1] & 1;
  715. spin_lock_irq(&chip->mixer_lock);
  716. change = chip->capture_source[addr][0] != left ||
  717. chip->capture_source[addr][1] != right;
  718. chip->capture_source[addr][0] = left;
  719. chip->capture_source[addr][1] = right;
  720. dev->dmasound.input=addr;
  721. spin_unlock_irq(&chip->mixer_lock);
  722. if (change) {
  723. switch (dev->pci->device) {
  724. case PCI_DEVICE_ID_PHILIPS_SAA7134:
  725. switch (addr) {
  726. case MIXER_ADDR_TVTUNER:
  727. saa_andorb(SAA7134_AUDIO_FORMAT_CTRL, 0xc0, 0xc0);
  728. saa_andorb(SAA7134_SIF_SAMPLE_FREQ, 0x03, 0x00);
  729. break;
  730. case MIXER_ADDR_LINE1:
  731. case MIXER_ADDR_LINE2:
  732. analog_io = (MIXER_ADDR_LINE1 == addr) ? 0x00 : 0x08;
  733. rate = (32000 == dev->dmasound.rate) ? 0x01 : 0x03;
  734. saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x08, analog_io);
  735. saa_andorb(SAA7134_AUDIO_FORMAT_CTRL, 0xc0, 0x80);
  736. saa_andorb(SAA7134_SIF_SAMPLE_FREQ, 0x03, rate);
  737. break;
  738. }
  739. break;
  740. case PCI_DEVICE_ID_PHILIPS_SAA7133:
  741. case PCI_DEVICE_ID_PHILIPS_SAA7135:
  742. xbarin = 0x03; // adc
  743. anabar = 0;
  744. switch (addr) {
  745. case MIXER_ADDR_TVTUNER:
  746. xbarin = 0; // Demodulator
  747. anabar = 2; // DACs
  748. break;
  749. case MIXER_ADDR_LINE1:
  750. anabar = 0; // aux1, aux1
  751. break;
  752. case MIXER_ADDR_LINE2:
  753. anabar = 9; // aux2, aux2
  754. break;
  755. }
  756. /* output xbar always main channel */
  757. saa_dsp_writel(dev, SAA7133_DIGITAL_OUTPUT_SEL1, 0xbbbb10);
  758. if (left || right) { // We've got data, turn the input on
  759. saa_dsp_writel(dev, SAA7133_DIGITAL_INPUT_XBAR1, xbarin);
  760. saa_writel(SAA7133_ANALOG_IO_SELECT, anabar);
  761. } else {
  762. saa_dsp_writel(dev, SAA7133_DIGITAL_INPUT_XBAR1, 0);
  763. saa_writel(SAA7133_ANALOG_IO_SELECT, 0);
  764. }
  765. break;
  766. }
  767. }
  768. return change;
  769. }
  770. static struct snd_kcontrol_new snd_saa7134_controls[] = {
  771. SAA713x_VOLUME("Video Volume", 0, MIXER_ADDR_TVTUNER),
  772. SAA713x_CAPSRC("Video Capture Switch", 0, MIXER_ADDR_TVTUNER),
  773. SAA713x_VOLUME("Line Volume", 1, MIXER_ADDR_LINE1),
  774. SAA713x_CAPSRC("Line Capture Switch", 1, MIXER_ADDR_LINE1),
  775. SAA713x_VOLUME("Line Volume", 2, MIXER_ADDR_LINE2),
  776. SAA713x_CAPSRC("Line Capture Switch", 2, MIXER_ADDR_LINE2),
  777. };
  778. /*
  779. * ALSA mixer setup
  780. *
  781. * Called when initializing the board. Sets up the name and hooks up
  782. * the callbacks
  783. *
  784. */
  785. static int snd_card_saa7134_new_mixer(snd_card_saa7134_t * chip)
  786. {
  787. struct snd_card *card = chip->card;
  788. unsigned int idx;
  789. int err;
  790. snd_assert(chip != NULL, return -EINVAL);
  791. strcpy(card->mixername, "SAA7134 Mixer");
  792. for (idx = 0; idx < ARRAY_SIZE(snd_saa7134_controls); idx++) {
  793. if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_saa7134_controls[idx], chip))) < 0)
  794. return err;
  795. }
  796. return 0;
  797. }
  798. static void snd_saa7134_free(struct snd_card * card)
  799. {
  800. snd_card_saa7134_t *chip = card->private_data;
  801. if (chip->dev->dmasound.priv_data == NULL)
  802. return;
  803. if (chip->irq >= 0) {
  804. synchronize_irq(chip->irq);
  805. free_irq(chip->irq, &chip->dev->dmasound);
  806. }
  807. chip->dev->dmasound.priv_data = NULL;
  808. }
  809. /*
  810. * ALSA initialization
  811. *
  812. * Called by the init routine, once for each saa7134 device present,
  813. * it creates the basic structures and registers the ALSA devices
  814. *
  815. */
  816. static int alsa_card_saa7134_create(struct saa7134_dev *dev, int devnum)
  817. {
  818. struct snd_card *card;
  819. snd_card_saa7134_t *chip;
  820. int err;
  821. if (devnum >= SNDRV_CARDS)
  822. return -ENODEV;
  823. if (!enable[devnum])
  824. return -ENODEV;
  825. card = snd_card_new(index[devnum], id[devnum], THIS_MODULE, sizeof(snd_card_saa7134_t));
  826. if (card == NULL)
  827. return -ENOMEM;
  828. strcpy(card->driver, "SAA7134");
  829. /* Card "creation" */
  830. card->private_free = snd_saa7134_free;
  831. chip = (snd_card_saa7134_t *) card->private_data;
  832. spin_lock_init(&chip->lock);
  833. spin_lock_init(&chip->mixer_lock);
  834. chip->dev = dev;
  835. chip->card = card;
  836. chip->pci = dev->pci;
  837. chip->iobase = pci_resource_start(dev->pci, 0);
  838. err = request_irq(dev->pci->irq, saa7134_alsa_irq,
  839. IRQF_SHARED | IRQF_DISABLED, dev->name,
  840. (void*) &dev->dmasound);
  841. if (err < 0) {
  842. printk(KERN_ERR "%s: can't get IRQ %d for ALSA\n",
  843. dev->name, dev->pci->irq);
  844. goto __nodev;
  845. }
  846. chip->irq = dev->pci->irq;
  847. mutex_init(&dev->dmasound.lock);
  848. if ((err = snd_card_saa7134_new_mixer(chip)) < 0)
  849. goto __nodev;
  850. if ((err = snd_card_saa7134_pcm(chip, 0)) < 0)
  851. goto __nodev;
  852. snd_card_set_dev(card, &chip->pci->dev);
  853. /* End of "creation" */
  854. strcpy(card->shortname, "SAA7134");
  855. sprintf(card->longname, "%s at 0x%lx irq %d",
  856. chip->dev->name, chip->iobase, chip->irq);
  857. printk(KERN_INFO "%s/alsa: %s registered as card %d\n",dev->name,card->longname,index[devnum]);
  858. if ((err = snd_card_register(card)) == 0) {
  859. snd_saa7134_cards[devnum] = card;
  860. return 0;
  861. }
  862. __nodev:
  863. snd_card_free(card);
  864. return err;
  865. }
  866. static int alsa_device_init(struct saa7134_dev *dev)
  867. {
  868. dev->dmasound.priv_data = dev;
  869. alsa_card_saa7134_create(dev,dev->nr);
  870. return 1;
  871. }
  872. static int alsa_device_exit(struct saa7134_dev *dev)
  873. {
  874. snd_card_free(snd_saa7134_cards[dev->nr]);
  875. snd_saa7134_cards[dev->nr] = NULL;
  876. return 1;
  877. }
  878. /*
  879. * Module initializer
  880. *
  881. * Loops through present saa7134 cards, and assigns an ALSA device
  882. * to each one
  883. *
  884. */
  885. static int saa7134_alsa_init(void)
  886. {
  887. struct saa7134_dev *dev = NULL;
  888. struct list_head *list;
  889. if (!saa7134_dmasound_init && !saa7134_dmasound_exit) {
  890. saa7134_dmasound_init = alsa_device_init;
  891. saa7134_dmasound_exit = alsa_device_exit;
  892. } else {
  893. printk(KERN_WARNING "saa7134 ALSA: can't load, DMA sound handler already assigned (probably to OSS)\n");
  894. return -EBUSY;
  895. }
  896. printk(KERN_INFO "saa7134 ALSA driver for DMA sound loaded\n");
  897. list_for_each(list,&saa7134_devlist) {
  898. dev = list_entry(list, struct saa7134_dev, devlist);
  899. if (dev->dmasound.priv_data == NULL) {
  900. alsa_device_init(dev);
  901. } else {
  902. printk(KERN_ERR "saa7134 ALSA: DMA sound is being handled by OSS. ignoring %s\n",dev->name);
  903. return -EBUSY;
  904. }
  905. }
  906. if (dev == NULL)
  907. printk(KERN_INFO "saa7134 ALSA: no saa7134 cards found\n");
  908. return 0;
  909. }
  910. /*
  911. * Module destructor
  912. */
  913. static void saa7134_alsa_exit(void)
  914. {
  915. int idx;
  916. for (idx = 0; idx < SNDRV_CARDS; idx++) {
  917. snd_card_free(snd_saa7134_cards[idx]);
  918. }
  919. saa7134_dmasound_init = NULL;
  920. saa7134_dmasound_exit = NULL;
  921. printk(KERN_INFO "saa7134 ALSA driver for DMA sound unloaded\n");
  922. return;
  923. }
  924. /* We initialize this late, to make sure the sound system is up and running */
  925. late_initcall(saa7134_alsa_init);
  926. module_exit(saa7134_alsa_exit);
  927. MODULE_LICENSE("GPL");
  928. MODULE_AUTHOR("Ricardo Cerqueira");