saa7134-alsa.c 29 KB

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