saa7134-alsa.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212
  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_stream_lock(dev->dmasound.substream);
  146. snd_pcm_stop(dev->dmasound.substream,SNDRV_PCM_STATE_XRUN);
  147. snd_pcm_stream_unlock(dev->dmasound.substream);
  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)
  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,
  188. SAA7134_IRQ_REPORT_DONE_RA3);
  189. saa7134_irq_alsa_done(dev, status);
  190. } else {
  191. goto out;
  192. }
  193. }
  194. if (loop == 10) {
  195. dprintk("error! looping IRQ!");
  196. }
  197. out:
  198. return IRQ_RETVAL(handled);
  199. }
  200. /*
  201. * ALSA capture trigger
  202. *
  203. * - One of the ALSA capture callbacks.
  204. *
  205. * Called whenever a capture is started or stopped. Must be defined,
  206. * but there's nothing we want to do here
  207. *
  208. */
  209. static int snd_card_saa7134_capture_trigger(struct snd_pcm_substream * substream,
  210. int cmd)
  211. {
  212. struct snd_pcm_runtime *runtime = substream->runtime;
  213. snd_card_saa7134_pcm_t *pcm = runtime->private_data;
  214. struct saa7134_dev *dev=pcm->dev;
  215. int err = 0;
  216. spin_lock(&dev->slock);
  217. if (cmd == SNDRV_PCM_TRIGGER_START) {
  218. /* start dma */
  219. saa7134_dma_start(dev);
  220. } else if (cmd == SNDRV_PCM_TRIGGER_STOP) {
  221. /* stop dma */
  222. saa7134_dma_stop(dev);
  223. } else {
  224. err = -EINVAL;
  225. }
  226. spin_unlock(&dev->slock);
  227. return err;
  228. }
  229. /*
  230. * DMA buffer initialization
  231. *
  232. * Uses V4L functions to initialize the DMA. Shouldn't be necessary in
  233. * ALSA, but I was unable to use ALSA's own DMA, and had to force the
  234. * usage of V4L's
  235. *
  236. * - Copied verbatim from saa7134-oss.
  237. *
  238. */
  239. static int dsp_buffer_init(struct saa7134_dev *dev)
  240. {
  241. int err;
  242. BUG_ON(!dev->dmasound.bufsize);
  243. videobuf_dma_init(&dev->dmasound.dma);
  244. err = videobuf_dma_init_kernel(&dev->dmasound.dma, PCI_DMA_FROMDEVICE,
  245. (dev->dmasound.bufsize + PAGE_SIZE) >> PAGE_SHIFT);
  246. if (0 != err)
  247. return err;
  248. return 0;
  249. }
  250. /*
  251. * DMA buffer release
  252. *
  253. * Called after closing the device, during snd_card_saa7134_capture_close
  254. *
  255. */
  256. static int dsp_buffer_free(struct saa7134_dev *dev)
  257. {
  258. BUG_ON(!dev->dmasound.blksize);
  259. videobuf_dma_free(&dev->dmasound.dma);
  260. dev->dmasound.blocks = 0;
  261. dev->dmasound.blksize = 0;
  262. dev->dmasound.bufsize = 0;
  263. return 0;
  264. }
  265. /*
  266. * Setting the capture source and updating the ALSA controls
  267. */
  268. static int snd_saa7134_capsrc_set(struct snd_kcontrol *kcontrol,
  269. int left, int right, bool force_notify)
  270. {
  271. snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
  272. int change = 0, addr = kcontrol->private_value;
  273. int active, old_addr;
  274. u32 anabar, xbarin;
  275. int analog_io, rate;
  276. struct saa7134_dev *dev;
  277. dev = chip->dev;
  278. spin_lock_irq(&chip->mixer_lock);
  279. active = left != 0 || right != 0;
  280. old_addr = chip->capture_source_addr;
  281. /* The active capture source cannot be deactivated */
  282. if (active) {
  283. change = old_addr != addr ||
  284. chip->capture_source[0] != left ||
  285. chip->capture_source[1] != right;
  286. chip->capture_source[0] = left;
  287. chip->capture_source[1] = right;
  288. chip->capture_source_addr = addr;
  289. dev->dmasound.input = addr;
  290. }
  291. spin_unlock_irq(&chip->mixer_lock);
  292. if (change) {
  293. switch (dev->pci->device) {
  294. case PCI_DEVICE_ID_PHILIPS_SAA7134:
  295. switch (addr) {
  296. case MIXER_ADDR_TVTUNER:
  297. saa_andorb(SAA7134_AUDIO_FORMAT_CTRL,
  298. 0xc0, 0xc0);
  299. saa_andorb(SAA7134_SIF_SAMPLE_FREQ,
  300. 0x03, 0x00);
  301. break;
  302. case MIXER_ADDR_LINE1:
  303. case MIXER_ADDR_LINE2:
  304. analog_io = (MIXER_ADDR_LINE1 == addr) ?
  305. 0x00 : 0x08;
  306. rate = (32000 == dev->dmasound.rate) ?
  307. 0x01 : 0x03;
  308. saa_andorb(SAA7134_ANALOG_IO_SELECT,
  309. 0x08, analog_io);
  310. saa_andorb(SAA7134_AUDIO_FORMAT_CTRL,
  311. 0xc0, 0x80);
  312. saa_andorb(SAA7134_SIF_SAMPLE_FREQ,
  313. 0x03, rate);
  314. break;
  315. }
  316. break;
  317. case PCI_DEVICE_ID_PHILIPS_SAA7133:
  318. case PCI_DEVICE_ID_PHILIPS_SAA7135:
  319. xbarin = 0x03; /* adc */
  320. anabar = 0;
  321. switch (addr) {
  322. case MIXER_ADDR_TVTUNER:
  323. xbarin = 0; /* Demodulator */
  324. anabar = 2; /* DACs */
  325. break;
  326. case MIXER_ADDR_LINE1:
  327. anabar = 0; /* aux1, aux1 */
  328. break;
  329. case MIXER_ADDR_LINE2:
  330. anabar = 9; /* aux2, aux2 */
  331. break;
  332. }
  333. /* output xbar always main channel */
  334. saa_dsp_writel(dev, SAA7133_DIGITAL_OUTPUT_SEL1,
  335. 0xbbbb10);
  336. if (left || right) {
  337. /* We've got data, turn the input on */
  338. saa_dsp_writel(dev, SAA7133_DIGITAL_INPUT_XBAR1,
  339. xbarin);
  340. saa_writel(SAA7133_ANALOG_IO_SELECT, anabar);
  341. } else {
  342. saa_dsp_writel(dev, SAA7133_DIGITAL_INPUT_XBAR1,
  343. 0);
  344. saa_writel(SAA7133_ANALOG_IO_SELECT, 0);
  345. }
  346. break;
  347. }
  348. }
  349. if (change) {
  350. if (force_notify)
  351. snd_ctl_notify(chip->card,
  352. SNDRV_CTL_EVENT_MASK_VALUE,
  353. &chip->capture_ctl[addr]->id);
  354. if (old_addr != MIXER_ADDR_UNSELECTED && old_addr != addr)
  355. snd_ctl_notify(chip->card,
  356. SNDRV_CTL_EVENT_MASK_VALUE,
  357. &chip->capture_ctl[old_addr]->id);
  358. }
  359. return change;
  360. }
  361. /*
  362. * ALSA PCM preparation
  363. *
  364. * - One of the ALSA capture callbacks.
  365. *
  366. * Called right after the capture device is opened, this function configures
  367. * the buffer using the previously defined functions, allocates the memory,
  368. * sets up the hardware registers, and then starts the DMA. When this function
  369. * returns, the audio should be flowing.
  370. *
  371. */
  372. static int snd_card_saa7134_capture_prepare(struct snd_pcm_substream * substream)
  373. {
  374. struct snd_pcm_runtime *runtime = substream->runtime;
  375. int bswap, sign;
  376. u32 fmt, control;
  377. snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
  378. struct saa7134_dev *dev;
  379. snd_card_saa7134_pcm_t *pcm = runtime->private_data;
  380. pcm->dev->dmasound.substream = substream;
  381. dev = saa7134->dev;
  382. if (snd_pcm_format_width(runtime->format) == 8)
  383. fmt = 0x00;
  384. else
  385. fmt = 0x01;
  386. if (snd_pcm_format_signed(runtime->format))
  387. sign = 1;
  388. else
  389. sign = 0;
  390. if (snd_pcm_format_big_endian(runtime->format))
  391. bswap = 1;
  392. else
  393. bswap = 0;
  394. switch (dev->pci->device) {
  395. case PCI_DEVICE_ID_PHILIPS_SAA7134:
  396. if (1 == runtime->channels)
  397. fmt |= (1 << 3);
  398. if (2 == runtime->channels)
  399. fmt |= (3 << 3);
  400. if (sign)
  401. fmt |= 0x04;
  402. fmt |= (MIXER_ADDR_TVTUNER == dev->dmasound.input) ? 0xc0 : 0x80;
  403. saa_writeb(SAA7134_NUM_SAMPLES0, ((dev->dmasound.blksize - 1) & 0x0000ff));
  404. saa_writeb(SAA7134_NUM_SAMPLES1, ((dev->dmasound.blksize - 1) & 0x00ff00) >> 8);
  405. saa_writeb(SAA7134_NUM_SAMPLES2, ((dev->dmasound.blksize - 1) & 0xff0000) >> 16);
  406. saa_writeb(SAA7134_AUDIO_FORMAT_CTRL, fmt);
  407. break;
  408. case PCI_DEVICE_ID_PHILIPS_SAA7133:
  409. case PCI_DEVICE_ID_PHILIPS_SAA7135:
  410. if (1 == runtime->channels)
  411. fmt |= (1 << 4);
  412. if (2 == runtime->channels)
  413. fmt |= (2 << 4);
  414. if (!sign)
  415. fmt |= 0x04;
  416. saa_writel(SAA7133_NUM_SAMPLES, dev->dmasound.blksize -1);
  417. saa_writel(SAA7133_AUDIO_CHANNEL, 0x543210 | (fmt << 24));
  418. break;
  419. }
  420. dprintk("rec_start: afmt=%d ch=%d => fmt=0x%x swap=%c\n",
  421. runtime->format, runtime->channels, fmt,
  422. bswap ? 'b' : '-');
  423. /* dma: setup channel 6 (= AUDIO) */
  424. control = SAA7134_RS_CONTROL_BURST_16 |
  425. SAA7134_RS_CONTROL_ME |
  426. (dev->dmasound.pt.dma >> 12);
  427. if (bswap)
  428. control |= SAA7134_RS_CONTROL_BSWAP;
  429. saa_writel(SAA7134_RS_BA1(6),0);
  430. saa_writel(SAA7134_RS_BA2(6),dev->dmasound.blksize);
  431. saa_writel(SAA7134_RS_PITCH(6),0);
  432. saa_writel(SAA7134_RS_CONTROL(6),control);
  433. dev->dmasound.rate = runtime->rate;
  434. /* Setup and update the card/ALSA controls */
  435. snd_saa7134_capsrc_set(saa7134->capture_ctl[dev->dmasound.input], 1, 1,
  436. true);
  437. return 0;
  438. }
  439. /*
  440. * ALSA pointer fetching
  441. *
  442. * - One of the ALSA capture callbacks.
  443. *
  444. * Called whenever a period elapses, it must return the current hardware
  445. * position of the buffer.
  446. * Also resets the read counter used to prevent overruns
  447. *
  448. */
  449. static snd_pcm_uframes_t
  450. snd_card_saa7134_capture_pointer(struct snd_pcm_substream * substream)
  451. {
  452. struct snd_pcm_runtime *runtime = substream->runtime;
  453. snd_card_saa7134_pcm_t *pcm = runtime->private_data;
  454. struct saa7134_dev *dev=pcm->dev;
  455. if (dev->dmasound.read_count) {
  456. dev->dmasound.read_count -= snd_pcm_lib_period_bytes(substream);
  457. dev->dmasound.read_offset += snd_pcm_lib_period_bytes(substream);
  458. if (dev->dmasound.read_offset == dev->dmasound.bufsize)
  459. dev->dmasound.read_offset = 0;
  460. }
  461. return bytes_to_frames(runtime, dev->dmasound.read_offset);
  462. }
  463. /*
  464. * ALSA hardware capabilities definition
  465. *
  466. * Report only 32kHz for ALSA:
  467. *
  468. * - SAA7133/35 uses DDEP (DemDec Easy Programming mode), which works in 32kHz
  469. * only
  470. * - SAA7134 for TV mode uses DemDec mode (32kHz)
  471. * - Radio works in 32kHz only
  472. * - When recording 48kHz from Line1/Line2, switching of capture source to TV
  473. * means
  474. * switching to 32kHz without any frequency translation
  475. */
  476. static struct snd_pcm_hardware snd_card_saa7134_capture =
  477. {
  478. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  479. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  480. SNDRV_PCM_INFO_MMAP_VALID),
  481. .formats = SNDRV_PCM_FMTBIT_S16_LE | \
  482. SNDRV_PCM_FMTBIT_S16_BE | \
  483. SNDRV_PCM_FMTBIT_S8 | \
  484. SNDRV_PCM_FMTBIT_U8 | \
  485. SNDRV_PCM_FMTBIT_U16_LE | \
  486. SNDRV_PCM_FMTBIT_U16_BE,
  487. .rates = SNDRV_PCM_RATE_32000,
  488. .rate_min = 32000,
  489. .rate_max = 32000,
  490. .channels_min = 1,
  491. .channels_max = 2,
  492. .buffer_bytes_max = (256*1024),
  493. .period_bytes_min = 64,
  494. .period_bytes_max = (256*1024),
  495. .periods_min = 4,
  496. .periods_max = 1024,
  497. };
  498. static void snd_card_saa7134_runtime_free(struct snd_pcm_runtime *runtime)
  499. {
  500. snd_card_saa7134_pcm_t *pcm = runtime->private_data;
  501. kfree(pcm);
  502. }
  503. /*
  504. * ALSA hardware params
  505. *
  506. * - One of the ALSA capture callbacks.
  507. *
  508. * Called on initialization, right before the PCM preparation
  509. *
  510. */
  511. static int snd_card_saa7134_hw_params(struct snd_pcm_substream * substream,
  512. struct snd_pcm_hw_params * hw_params)
  513. {
  514. snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
  515. struct saa7134_dev *dev;
  516. unsigned int period_size, periods;
  517. int err;
  518. period_size = params_period_bytes(hw_params);
  519. periods = params_periods(hw_params);
  520. if (period_size < 0x100 || period_size > 0x10000)
  521. return -EINVAL;
  522. if (periods < 4)
  523. return -EINVAL;
  524. if (period_size * periods > 1024 * 1024)
  525. return -EINVAL;
  526. dev = saa7134->dev;
  527. if (dev->dmasound.blocks == periods &&
  528. dev->dmasound.blksize == period_size)
  529. return 0;
  530. /* release the old buffer */
  531. if (substream->runtime->dma_area) {
  532. saa7134_pgtable_free(dev->pci, &dev->dmasound.pt);
  533. videobuf_dma_unmap(&dev->pci->dev, &dev->dmasound.dma);
  534. dsp_buffer_free(dev);
  535. substream->runtime->dma_area = NULL;
  536. }
  537. dev->dmasound.blocks = periods;
  538. dev->dmasound.blksize = period_size;
  539. dev->dmasound.bufsize = period_size * periods;
  540. err = dsp_buffer_init(dev);
  541. if (0 != err) {
  542. dev->dmasound.blocks = 0;
  543. dev->dmasound.blksize = 0;
  544. dev->dmasound.bufsize = 0;
  545. return err;
  546. }
  547. if (0 != (err = videobuf_dma_map(&dev->pci->dev, &dev->dmasound.dma))) {
  548. dsp_buffer_free(dev);
  549. return err;
  550. }
  551. if (0 != (err = saa7134_pgtable_alloc(dev->pci,&dev->dmasound.pt))) {
  552. videobuf_dma_unmap(&dev->pci->dev, &dev->dmasound.dma);
  553. dsp_buffer_free(dev);
  554. return err;
  555. }
  556. if (0 != (err = saa7134_pgtable_build(dev->pci,&dev->dmasound.pt,
  557. dev->dmasound.dma.sglist,
  558. dev->dmasound.dma.sglen,
  559. 0))) {
  560. saa7134_pgtable_free(dev->pci, &dev->dmasound.pt);
  561. videobuf_dma_unmap(&dev->pci->dev, &dev->dmasound.dma);
  562. dsp_buffer_free(dev);
  563. return err;
  564. }
  565. /* I should be able to use runtime->dma_addr in the control
  566. byte, but it doesn't work. So I allocate the DMA using the
  567. V4L functions, and force ALSA to use that as the DMA area */
  568. substream->runtime->dma_area = dev->dmasound.dma.vaddr;
  569. substream->runtime->dma_bytes = dev->dmasound.bufsize;
  570. substream->runtime->dma_addr = 0;
  571. return 0;
  572. }
  573. /*
  574. * ALSA hardware release
  575. *
  576. * - One of the ALSA capture callbacks.
  577. *
  578. * Called after closing the device, but before snd_card_saa7134_capture_close
  579. * It stops the DMA audio and releases the buffers.
  580. *
  581. */
  582. static int snd_card_saa7134_hw_free(struct snd_pcm_substream * substream)
  583. {
  584. snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
  585. struct saa7134_dev *dev;
  586. dev = saa7134->dev;
  587. if (substream->runtime->dma_area) {
  588. saa7134_pgtable_free(dev->pci, &dev->dmasound.pt);
  589. videobuf_dma_unmap(&dev->pci->dev, &dev->dmasound.dma);
  590. dsp_buffer_free(dev);
  591. substream->runtime->dma_area = NULL;
  592. }
  593. return 0;
  594. }
  595. /*
  596. * ALSA capture finish
  597. *
  598. * - One of the ALSA capture callbacks.
  599. *
  600. * Called after closing the device.
  601. *
  602. */
  603. static int snd_card_saa7134_capture_close(struct snd_pcm_substream * substream)
  604. {
  605. snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
  606. struct saa7134_dev *dev = saa7134->dev;
  607. if (saa7134->mute_was_on) {
  608. dev->ctl_mute = 1;
  609. saa7134_tvaudio_setmute(dev);
  610. }
  611. return 0;
  612. }
  613. /*
  614. * ALSA capture start
  615. *
  616. * - One of the ALSA capture callbacks.
  617. *
  618. * Called when opening the device. It creates and populates the PCM
  619. * structure
  620. *
  621. */
  622. static int snd_card_saa7134_capture_open(struct snd_pcm_substream * substream)
  623. {
  624. struct snd_pcm_runtime *runtime = substream->runtime;
  625. snd_card_saa7134_pcm_t *pcm;
  626. snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
  627. struct saa7134_dev *dev;
  628. int amux, err;
  629. if (!saa7134) {
  630. printk(KERN_ERR "BUG: saa7134 can't find device struct."
  631. " Can't proceed with open\n");
  632. return -ENODEV;
  633. }
  634. dev = saa7134->dev;
  635. mutex_lock(&dev->dmasound.lock);
  636. dev->dmasound.read_count = 0;
  637. dev->dmasound.read_offset = 0;
  638. amux = dev->input->amux;
  639. if ((amux < 1) || (amux > 3))
  640. amux = 1;
  641. dev->dmasound.input = amux - 1;
  642. mutex_unlock(&dev->dmasound.lock);
  643. pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
  644. if (pcm == NULL)
  645. return -ENOMEM;
  646. pcm->dev=saa7134->dev;
  647. spin_lock_init(&pcm->lock);
  648. pcm->substream = substream;
  649. runtime->private_data = pcm;
  650. runtime->private_free = snd_card_saa7134_runtime_free;
  651. runtime->hw = snd_card_saa7134_capture;
  652. if (dev->ctl_mute != 0) {
  653. saa7134->mute_was_on = 1;
  654. dev->ctl_mute = 0;
  655. saa7134_tvaudio_setmute(dev);
  656. }
  657. err = snd_pcm_hw_constraint_integer(runtime,
  658. SNDRV_PCM_HW_PARAM_PERIODS);
  659. if (err < 0)
  660. return err;
  661. err = snd_pcm_hw_constraint_step(runtime, 0,
  662. SNDRV_PCM_HW_PARAM_PERIODS, 2);
  663. if (err < 0)
  664. return err;
  665. return 0;
  666. }
  667. /*
  668. * page callback (needed for mmap)
  669. */
  670. static struct page *snd_card_saa7134_page(struct snd_pcm_substream *substream,
  671. unsigned long offset)
  672. {
  673. void *pageptr = substream->runtime->dma_area + offset;
  674. return vmalloc_to_page(pageptr);
  675. }
  676. /*
  677. * ALSA capture callbacks definition
  678. */
  679. static struct snd_pcm_ops snd_card_saa7134_capture_ops = {
  680. .open = snd_card_saa7134_capture_open,
  681. .close = snd_card_saa7134_capture_close,
  682. .ioctl = snd_pcm_lib_ioctl,
  683. .hw_params = snd_card_saa7134_hw_params,
  684. .hw_free = snd_card_saa7134_hw_free,
  685. .prepare = snd_card_saa7134_capture_prepare,
  686. .trigger = snd_card_saa7134_capture_trigger,
  687. .pointer = snd_card_saa7134_capture_pointer,
  688. .page = snd_card_saa7134_page,
  689. };
  690. /*
  691. * ALSA PCM setup
  692. *
  693. * Called when initializing the board. Sets up the name and hooks up
  694. * the callbacks
  695. *
  696. */
  697. static int snd_card_saa7134_pcm(snd_card_saa7134_t *saa7134, int device)
  698. {
  699. struct snd_pcm *pcm;
  700. int err;
  701. if ((err = snd_pcm_new(saa7134->card, "SAA7134 PCM", device, 0, 1, &pcm)) < 0)
  702. return err;
  703. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_card_saa7134_capture_ops);
  704. pcm->private_data = saa7134;
  705. pcm->info_flags = 0;
  706. strcpy(pcm->name, "SAA7134 PCM");
  707. return 0;
  708. }
  709. #define SAA713x_VOLUME(xname, xindex, addr) \
  710. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
  711. .info = snd_saa7134_volume_info, \
  712. .get = snd_saa7134_volume_get, .put = snd_saa7134_volume_put, \
  713. .private_value = addr }
  714. static int snd_saa7134_volume_info(struct snd_kcontrol * kcontrol,
  715. struct snd_ctl_elem_info * uinfo)
  716. {
  717. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  718. uinfo->count = 2;
  719. uinfo->value.integer.min = 0;
  720. uinfo->value.integer.max = 20;
  721. return 0;
  722. }
  723. static int snd_saa7134_volume_get(struct snd_kcontrol * kcontrol,
  724. struct snd_ctl_elem_value * ucontrol)
  725. {
  726. snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
  727. int addr = kcontrol->private_value;
  728. ucontrol->value.integer.value[0] = chip->mixer_volume[addr][0];
  729. ucontrol->value.integer.value[1] = chip->mixer_volume[addr][1];
  730. return 0;
  731. }
  732. static int snd_saa7134_volume_put(struct snd_kcontrol * kcontrol,
  733. struct snd_ctl_elem_value * ucontrol)
  734. {
  735. snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
  736. struct saa7134_dev *dev = chip->dev;
  737. int change, addr = kcontrol->private_value;
  738. int left, right;
  739. left = ucontrol->value.integer.value[0];
  740. if (left < 0)
  741. left = 0;
  742. if (left > 20)
  743. left = 20;
  744. right = ucontrol->value.integer.value[1];
  745. if (right < 0)
  746. right = 0;
  747. if (right > 20)
  748. right = 20;
  749. spin_lock_irq(&chip->mixer_lock);
  750. change = 0;
  751. if (chip->mixer_volume[addr][0] != left) {
  752. change = 1;
  753. right = left;
  754. }
  755. if (chip->mixer_volume[addr][1] != right) {
  756. change = 1;
  757. left = right;
  758. }
  759. if (change) {
  760. switch (dev->pci->device) {
  761. case PCI_DEVICE_ID_PHILIPS_SAA7134:
  762. switch (addr) {
  763. case MIXER_ADDR_TVTUNER:
  764. left = 20;
  765. break;
  766. case MIXER_ADDR_LINE1:
  767. saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x10,
  768. (left > 10) ? 0x00 : 0x10);
  769. break;
  770. case MIXER_ADDR_LINE2:
  771. saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x20,
  772. (left > 10) ? 0x00 : 0x20);
  773. break;
  774. }
  775. break;
  776. case PCI_DEVICE_ID_PHILIPS_SAA7133:
  777. case PCI_DEVICE_ID_PHILIPS_SAA7135:
  778. switch (addr) {
  779. case MIXER_ADDR_TVTUNER:
  780. left = 20;
  781. break;
  782. case MIXER_ADDR_LINE1:
  783. saa_andorb(0x0594, 0x10,
  784. (left > 10) ? 0x00 : 0x10);
  785. break;
  786. case MIXER_ADDR_LINE2:
  787. saa_andorb(0x0594, 0x20,
  788. (left > 10) ? 0x00 : 0x20);
  789. break;
  790. }
  791. break;
  792. }
  793. chip->mixer_volume[addr][0] = left;
  794. chip->mixer_volume[addr][1] = right;
  795. }
  796. spin_unlock_irq(&chip->mixer_lock);
  797. return change;
  798. }
  799. #define SAA713x_CAPSRC(xname, xindex, addr) \
  800. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
  801. .info = snd_saa7134_capsrc_info, \
  802. .get = snd_saa7134_capsrc_get, .put = snd_saa7134_capsrc_put, \
  803. .private_value = addr }
  804. static int snd_saa7134_capsrc_info(struct snd_kcontrol * kcontrol,
  805. struct snd_ctl_elem_info * uinfo)
  806. {
  807. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  808. uinfo->count = 2;
  809. uinfo->value.integer.min = 0;
  810. uinfo->value.integer.max = 1;
  811. return 0;
  812. }
  813. static int snd_saa7134_capsrc_get(struct snd_kcontrol * kcontrol,
  814. struct snd_ctl_elem_value * ucontrol)
  815. {
  816. snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
  817. int addr = kcontrol->private_value;
  818. spin_lock_irq(&chip->mixer_lock);
  819. if (chip->capture_source_addr == addr) {
  820. ucontrol->value.integer.value[0] = chip->capture_source[0];
  821. ucontrol->value.integer.value[1] = chip->capture_source[1];
  822. } else {
  823. ucontrol->value.integer.value[0] = 0;
  824. ucontrol->value.integer.value[1] = 0;
  825. }
  826. spin_unlock_irq(&chip->mixer_lock);
  827. return 0;
  828. }
  829. static int snd_saa7134_capsrc_put(struct snd_kcontrol * kcontrol,
  830. struct snd_ctl_elem_value * ucontrol)
  831. {
  832. int left, right;
  833. left = ucontrol->value.integer.value[0] & 1;
  834. right = ucontrol->value.integer.value[1] & 1;
  835. return snd_saa7134_capsrc_set(kcontrol, left, right, false);
  836. }
  837. static struct snd_kcontrol_new snd_saa7134_volume_controls[] = {
  838. SAA713x_VOLUME("Video Volume", 0, MIXER_ADDR_TVTUNER),
  839. SAA713x_VOLUME("Line Volume", 1, MIXER_ADDR_LINE1),
  840. SAA713x_VOLUME("Line Volume", 2, MIXER_ADDR_LINE2),
  841. };
  842. static struct snd_kcontrol_new snd_saa7134_capture_controls[] = {
  843. SAA713x_CAPSRC("Video Capture Switch", 0, MIXER_ADDR_TVTUNER),
  844. SAA713x_CAPSRC("Line Capture Switch", 1, MIXER_ADDR_LINE1),
  845. SAA713x_CAPSRC("Line Capture Switch", 2, MIXER_ADDR_LINE2),
  846. };
  847. /*
  848. * ALSA mixer setup
  849. *
  850. * Called when initializing the board. Sets up the name and hooks up
  851. * the callbacks
  852. *
  853. */
  854. static int snd_card_saa7134_new_mixer(snd_card_saa7134_t * chip)
  855. {
  856. struct snd_card *card = chip->card;
  857. struct snd_kcontrol *kcontrol;
  858. unsigned int idx;
  859. int err, addr;
  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 = 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");