lola_pcm.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  1. /*
  2. * Support for Digigram Lola PCI-e boards
  3. *
  4. * Copyright (c) 2011 Takashi Iwai <tiwai@suse.de>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation; either version 2 of the License, or (at your option)
  9. * any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along with
  17. * this program; if not, write to the Free Software Foundation, Inc., 59
  18. * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/init.h>
  22. #include <linux/dma-mapping.h>
  23. #include <linux/pci.h>
  24. #include <sound/core.h>
  25. #include <sound/pcm.h>
  26. #include "lola.h"
  27. #define LOLA_MAX_BDL_ENTRIES 8
  28. #define LOLA_MAX_BUF_SIZE (1024*1024*1024)
  29. #define LOLA_BDL_ENTRY_SIZE (16 * 16)
  30. static struct lola_pcm *lola_get_pcm(struct snd_pcm_substream *substream)
  31. {
  32. struct lola *chip = snd_pcm_substream_chip(substream);
  33. return &chip->pcm[substream->stream];
  34. }
  35. static struct lola_stream *lola_get_stream(struct snd_pcm_substream *substream)
  36. {
  37. struct lola_pcm *pcm = lola_get_pcm(substream);
  38. unsigned int idx = substream->number;
  39. return &pcm->streams[idx];
  40. }
  41. static unsigned int lola_get_lrc(struct lola *chip)
  42. {
  43. return lola_readl(chip, BAR1, LRC);
  44. }
  45. static unsigned int lola_get_tstamp(struct lola *chip, bool quick_no_sync)
  46. {
  47. unsigned int tstamp = lola_get_lrc(chip) >> 8;
  48. if (chip->granularity) {
  49. unsigned int wait_banks = quick_no_sync ? 0 : 8;
  50. tstamp += (wait_banks + 1) * chip->granularity - 1;
  51. tstamp -= tstamp % chip->granularity;
  52. }
  53. return tstamp << 8;
  54. }
  55. /* clear any pending interrupt status */
  56. static void lola_stream_clear_pending_irq(struct lola *chip,
  57. struct lola_stream *str)
  58. {
  59. unsigned int val = lola_dsd_read(chip, str->dsd, STS);
  60. val &= LOLA_DSD_STS_DESE | LOLA_DSD_STS_BCIS;
  61. if (val)
  62. lola_dsd_write(chip, str->dsd, STS, val);
  63. }
  64. static void lola_stream_start(struct lola *chip, struct lola_stream *str,
  65. unsigned int tstamp)
  66. {
  67. lola_stream_clear_pending_irq(chip, str);
  68. lola_dsd_write(chip, str->dsd, CTL,
  69. LOLA_DSD_CTL_SRUN |
  70. LOLA_DSD_CTL_IOCE |
  71. LOLA_DSD_CTL_DEIE |
  72. LOLA_DSD_CTL_VLRCV |
  73. tstamp);
  74. }
  75. static void lola_stream_stop(struct lola *chip, struct lola_stream *str,
  76. unsigned int tstamp)
  77. {
  78. lola_dsd_write(chip, str->dsd, CTL,
  79. LOLA_DSD_CTL_IOCE |
  80. LOLA_DSD_CTL_DEIE |
  81. LOLA_DSD_CTL_VLRCV |
  82. tstamp);
  83. lola_stream_clear_pending_irq(chip, str);
  84. }
  85. static void wait_for_srst_clear(struct lola *chip, struct lola_stream *str)
  86. {
  87. unsigned long end_time = jiffies + msecs_to_jiffies(200);
  88. while (time_before(jiffies, end_time)) {
  89. unsigned int val;
  90. val = lola_dsd_read(chip, str->dsd, CTL);
  91. if (!(val & LOLA_DSD_CTL_SRST))
  92. return;
  93. msleep(1);
  94. }
  95. printk(KERN_WARNING SFX "SRST not clear (stream %d)\n", str->dsd);
  96. }
  97. static int lola_stream_wait_for_fifo(struct lola *chip,
  98. struct lola_stream *str,
  99. bool ready)
  100. {
  101. unsigned int val = ready ? LOLA_DSD_STS_FIFORDY : 0;
  102. unsigned long end_time = jiffies + msecs_to_jiffies(200);
  103. while (time_before(jiffies, end_time)) {
  104. unsigned int reg = lola_dsd_read(chip, str->dsd, STS);
  105. if ((reg & LOLA_DSD_STS_FIFORDY) == val)
  106. return 0;
  107. msleep(1);
  108. }
  109. printk(KERN_WARNING SFX "FIFO not ready (stream %d)\n", str->dsd);
  110. return -EIO;
  111. }
  112. static void lola_stream_reset(struct lola *chip, struct lola_stream *str)
  113. {
  114. if (str->prepared) {
  115. str->prepared = 0;
  116. if (str->paused) {
  117. /* finish pause - prepare for a new resume
  118. * move this code later to trigger function,
  119. * as this is also needed when resuming from pause
  120. */
  121. str->paused = 0;
  122. /* implement later loop for all streams */
  123. lola_stream_wait_for_fifo(chip, str, false);
  124. lola_dsd_write(chip, str->dsd, CTL, LOLA_DSD_CTL_SRUN |
  125. LOLA_DSD_CTL_IOCE | LOLA_DSD_CTL_DEIE);
  126. /* end loop */
  127. /* implement later once more loop for all streams */
  128. lola_stream_wait_for_fifo(chip, str, true);
  129. /* end loop */
  130. /* end finish pause */
  131. }
  132. lola_dsd_write(chip, str->dsd, CTL,
  133. LOLA_DSD_CTL_IOCE | LOLA_DSD_CTL_DEIE);
  134. lola_stream_wait_for_fifo(chip, str, false);
  135. lola_stream_clear_pending_irq(chip, str);
  136. lola_dsd_write(chip, str->dsd, CTL, LOLA_DSD_CTL_SRST);
  137. lola_dsd_write(chip, str->dsd, LVI, 0);
  138. lola_dsd_write(chip, str->dsd, BDPU, 0);
  139. lola_dsd_write(chip, str->dsd, BDPL, 0);
  140. wait_for_srst_clear(chip, str);
  141. }
  142. }
  143. static struct snd_pcm_hardware lola_pcm_hw = {
  144. .info = (SNDRV_PCM_INFO_MMAP |
  145. SNDRV_PCM_INFO_INTERLEAVED |
  146. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  147. SNDRV_PCM_INFO_MMAP_VALID |
  148. SNDRV_PCM_INFO_PAUSE),
  149. .formats = (SNDRV_PCM_FMTBIT_S16_LE |
  150. SNDRV_PCM_FMTBIT_S24_LE |
  151. SNDRV_PCM_FMTBIT_S32_LE |
  152. SNDRV_PCM_FMTBIT_FLOAT_LE),
  153. .rates = SNDRV_PCM_RATE_8000_192000,
  154. .rate_min = 8000,
  155. .rate_max = 192000,
  156. .channels_min = 1,
  157. .channels_max = 2,
  158. .buffer_bytes_max = LOLA_MAX_BUF_SIZE,
  159. .period_bytes_min = 128,
  160. .period_bytes_max = LOLA_MAX_BUF_SIZE / 2,
  161. .periods_min = 2,
  162. .periods_max = LOLA_MAX_BDL_ENTRIES,
  163. .fifo_size = 0,
  164. };
  165. static int lola_pcm_open(struct snd_pcm_substream *substream)
  166. {
  167. struct lola *chip = snd_pcm_substream_chip(substream);
  168. struct lola_pcm *pcm = lola_get_pcm(substream);
  169. struct lola_stream *str = lola_get_stream(substream);
  170. struct snd_pcm_runtime *runtime = substream->runtime;
  171. mutex_lock(&chip->open_mutex);
  172. if (str->opened) {
  173. mutex_unlock(&chip->open_mutex);
  174. return -EBUSY;
  175. }
  176. str->substream = substream;
  177. str->master = NULL;
  178. str->opened = 1;
  179. runtime->hw = lola_pcm_hw;
  180. runtime->hw.channels_max = pcm->num_streams - str->index;
  181. if (chip->sample_rate) {
  182. /* sample rate is locked */
  183. runtime->hw.rate_min = chip->sample_rate;
  184. runtime->hw.rate_max = chip->sample_rate;
  185. } else {
  186. runtime->hw.rate_min = chip->sample_rate_min;
  187. runtime->hw.rate_max = chip->sample_rate_max;
  188. }
  189. chip->ref_count_rate++;
  190. snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
  191. /* period size = multiple of chip->granularity (8, 16 or 32 frames)*/
  192. snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
  193. chip->granularity);
  194. snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
  195. chip->granularity);
  196. mutex_unlock(&chip->open_mutex);
  197. return 0;
  198. }
  199. static void lola_cleanup_slave_streams(struct lola_pcm *pcm,
  200. struct lola_stream *str)
  201. {
  202. int i;
  203. for (i = str->index + 1; i < pcm->num_streams; i++) {
  204. struct lola_stream *s = &pcm->streams[i];
  205. if (s->master != str)
  206. break;
  207. s->master = NULL;
  208. s->opened = 0;
  209. }
  210. }
  211. static int lola_pcm_close(struct snd_pcm_substream *substream)
  212. {
  213. struct lola *chip = snd_pcm_substream_chip(substream);
  214. struct lola_stream *str = lola_get_stream(substream);
  215. mutex_lock(&chip->open_mutex);
  216. if (str->substream == substream) {
  217. str->substream = NULL;
  218. str->opened = 0;
  219. }
  220. if (--chip->ref_count_rate == 0) {
  221. /* release sample rate */
  222. chip->sample_rate = 0;
  223. }
  224. mutex_unlock(&chip->open_mutex);
  225. return 0;
  226. }
  227. static int lola_pcm_hw_params(struct snd_pcm_substream *substream,
  228. struct snd_pcm_hw_params *hw_params)
  229. {
  230. struct lola_stream *str = lola_get_stream(substream);
  231. str->bufsize = 0;
  232. str->period_bytes = 0;
  233. str->format_verb = 0;
  234. return snd_pcm_lib_malloc_pages(substream,
  235. params_buffer_bytes(hw_params));
  236. }
  237. static int lola_pcm_hw_free(struct snd_pcm_substream *substream)
  238. {
  239. struct lola *chip = snd_pcm_substream_chip(substream);
  240. struct lola_pcm *pcm = lola_get_pcm(substream);
  241. struct lola_stream *str = lola_get_stream(substream);
  242. mutex_lock(&chip->open_mutex);
  243. lola_stream_reset(chip, str);
  244. lola_cleanup_slave_streams(pcm, str);
  245. mutex_unlock(&chip->open_mutex);
  246. return snd_pcm_lib_free_pages(substream);
  247. }
  248. /*
  249. * set up a BDL entry
  250. */
  251. static int setup_bdle(struct snd_pcm_substream *substream,
  252. struct lola_stream *str, u32 **bdlp,
  253. int ofs, int size)
  254. {
  255. u32 *bdl = *bdlp;
  256. while (size > 0) {
  257. dma_addr_t addr;
  258. int chunk;
  259. if (str->frags >= LOLA_MAX_BDL_ENTRIES)
  260. return -EINVAL;
  261. addr = snd_pcm_sgbuf_get_addr(substream, ofs);
  262. /* program the address field of the BDL entry */
  263. bdl[0] = cpu_to_le32((u32)addr);
  264. bdl[1] = cpu_to_le32(upper_32_bits(addr));
  265. /* program the size field of the BDL entry */
  266. chunk = snd_pcm_sgbuf_get_chunk_size(substream, ofs, size);
  267. bdl[2] = cpu_to_le32(chunk);
  268. /* program the IOC to enable interrupt
  269. * only when the whole fragment is processed
  270. */
  271. size -= chunk;
  272. bdl[3] = size ? 0 : cpu_to_le32(0x01);
  273. bdl += 4;
  274. str->frags++;
  275. ofs += chunk;
  276. }
  277. *bdlp = bdl;
  278. return ofs;
  279. }
  280. /*
  281. * set up BDL entries
  282. */
  283. static int lola_setup_periods(struct lola *chip, struct lola_pcm *pcm,
  284. struct snd_pcm_substream *substream,
  285. struct lola_stream *str)
  286. {
  287. u32 *bdl;
  288. int i, ofs, periods, period_bytes;
  289. period_bytes = str->period_bytes;
  290. periods = str->bufsize / period_bytes;
  291. /* program the initial BDL entries */
  292. bdl = (u32 *)(pcm->bdl.area + LOLA_BDL_ENTRY_SIZE * str->index);
  293. ofs = 0;
  294. str->frags = 0;
  295. for (i = 0; i < periods; i++) {
  296. ofs = setup_bdle(substream, str, &bdl, ofs, period_bytes);
  297. if (ofs < 0)
  298. goto error;
  299. }
  300. return 0;
  301. error:
  302. snd_printk(KERN_ERR SFX "Too many BDL entries: buffer=%d, period=%d\n",
  303. str->bufsize, period_bytes);
  304. return -EINVAL;
  305. }
  306. static unsigned int lola_get_format_verb(struct snd_pcm_substream *substream)
  307. {
  308. unsigned int verb;
  309. switch (substream->runtime->format) {
  310. case SNDRV_PCM_FORMAT_S16_LE:
  311. verb = 0x00000000;
  312. break;
  313. case SNDRV_PCM_FORMAT_S24_LE:
  314. verb = 0x00000200;
  315. break;
  316. case SNDRV_PCM_FORMAT_S32_LE:
  317. verb = 0x00000300;
  318. break;
  319. case SNDRV_PCM_FORMAT_FLOAT_LE:
  320. verb = 0x00001300;
  321. break;
  322. default:
  323. return 0;
  324. }
  325. verb |= substream->runtime->channels;
  326. return verb;
  327. }
  328. static int lola_set_stream_config(struct lola *chip,
  329. struct lola_stream *str,
  330. int channels)
  331. {
  332. int i, err;
  333. unsigned int verb, val;
  334. /* set format info for all channels
  335. * (with only one command for the first channel)
  336. */
  337. err = lola_codec_read(chip, str->nid, LOLA_VERB_SET_STREAM_FORMAT,
  338. str->format_verb, 0, &val, NULL);
  339. if (err < 0) {
  340. printk(KERN_ERR SFX "Cannot set stream format 0x%x\n",
  341. str->format_verb);
  342. return err;
  343. }
  344. /* update stream - channel config */
  345. for (i = 0; i < channels; i++) {
  346. verb = (str->index << 6) | i;
  347. err = lola_codec_read(chip, str[i].nid,
  348. LOLA_VERB_SET_CHANNEL_STREAMID, 0, verb,
  349. &val, NULL);
  350. if (err < 0) {
  351. printk(KERN_ERR SFX "Cannot set stream channel %d\n", i);
  352. return err;
  353. }
  354. }
  355. return 0;
  356. }
  357. /*
  358. * set up the SD for streaming
  359. */
  360. static int lola_setup_controller(struct lola *chip, struct lola_pcm *pcm,
  361. struct lola_stream *str)
  362. {
  363. dma_addr_t bdl;
  364. if (str->prepared)
  365. return -EINVAL;
  366. /* set up BDL */
  367. bdl = pcm->bdl.addr + LOLA_BDL_ENTRY_SIZE * str->index;
  368. lola_dsd_write(chip, str->dsd, BDPL, (u32)bdl);
  369. lola_dsd_write(chip, str->dsd, BDPU, upper_32_bits(bdl));
  370. /* program the stream LVI (last valid index) of the BDL */
  371. lola_dsd_write(chip, str->dsd, LVI, str->frags - 1);
  372. lola_stream_clear_pending_irq(chip, str);
  373. lola_dsd_write(chip, str->dsd, CTL,
  374. LOLA_DSD_CTL_IOCE | LOLA_DSD_CTL_DEIE | LOLA_DSD_CTL_SRUN);
  375. str->prepared = 1;
  376. return lola_stream_wait_for_fifo(chip, str, true);
  377. }
  378. static int lola_pcm_prepare(struct snd_pcm_substream *substream)
  379. {
  380. struct lola *chip = snd_pcm_substream_chip(substream);
  381. struct lola_pcm *pcm = lola_get_pcm(substream);
  382. struct lola_stream *str = lola_get_stream(substream);
  383. struct snd_pcm_runtime *runtime = substream->runtime;
  384. unsigned int bufsize, period_bytes, format_verb;
  385. int i, err;
  386. mutex_lock(&chip->open_mutex);
  387. lola_stream_reset(chip, str);
  388. lola_cleanup_slave_streams(pcm, str);
  389. if (str->index + runtime->channels >= pcm->num_streams) {
  390. mutex_unlock(&chip->open_mutex);
  391. return -EINVAL;
  392. }
  393. for (i = 1; i < runtime->channels; i++) {
  394. str[i].master = str;
  395. str[i].opened = 1;
  396. }
  397. mutex_unlock(&chip->open_mutex);
  398. bufsize = snd_pcm_lib_buffer_bytes(substream);
  399. period_bytes = snd_pcm_lib_period_bytes(substream);
  400. format_verb = lola_get_format_verb(substream);
  401. str->bufsize = bufsize;
  402. str->period_bytes = period_bytes;
  403. str->format_verb = format_verb;
  404. err = lola_setup_periods(chip, pcm, substream, str);
  405. if (err < 0)
  406. return err;
  407. err = lola_set_sample_rate(chip, runtime->rate);
  408. if (err < 0)
  409. return err;
  410. chip->sample_rate = runtime->rate; /* sample rate gets locked */
  411. err = lola_set_stream_config(chip, str, runtime->channels);
  412. if (err < 0)
  413. return err;
  414. err = lola_setup_controller(chip, pcm, str);
  415. if (err < 0) {
  416. lola_stream_reset(chip, str);
  417. return err;
  418. }
  419. return 0;
  420. }
  421. static int lola_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  422. {
  423. struct lola *chip = snd_pcm_substream_chip(substream);
  424. struct lola_stream *str;
  425. struct snd_pcm_substream *s;
  426. unsigned int start;
  427. unsigned int tstamp;
  428. bool sync_streams;
  429. switch (cmd) {
  430. case SNDRV_PCM_TRIGGER_START:
  431. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  432. case SNDRV_PCM_TRIGGER_RESUME:
  433. start = 1;
  434. break;
  435. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  436. case SNDRV_PCM_TRIGGER_SUSPEND:
  437. case SNDRV_PCM_TRIGGER_STOP:
  438. start = 0;
  439. break;
  440. default:
  441. return -EINVAL;
  442. }
  443. /*
  444. * sample correct synchronization is only needed starting several
  445. * streams on stop or if only one stream do as quick as possible
  446. */
  447. sync_streams = (start && snd_pcm_stream_linked(substream));
  448. tstamp = lola_get_tstamp(chip, !sync_streams);
  449. spin_lock(&chip->reg_lock);
  450. snd_pcm_group_for_each_entry(s, substream) {
  451. if (s->pcm->card != substream->pcm->card)
  452. continue;
  453. str = lola_get_stream(s);
  454. if (start)
  455. lola_stream_start(chip, str, tstamp);
  456. else
  457. lola_stream_stop(chip, str, tstamp);
  458. str->running = start;
  459. str->paused = !start;
  460. snd_pcm_trigger_done(s, substream);
  461. }
  462. spin_unlock(&chip->reg_lock);
  463. return 0;
  464. }
  465. static snd_pcm_uframes_t lola_pcm_pointer(struct snd_pcm_substream *substream)
  466. {
  467. struct lola *chip = snd_pcm_substream_chip(substream);
  468. struct lola_stream *str = lola_get_stream(substream);
  469. unsigned int pos = lola_dsd_read(chip, str->dsd, LPIB);
  470. if (pos >= str->bufsize)
  471. pos = 0;
  472. return bytes_to_frames(substream->runtime, pos);
  473. }
  474. void lola_pcm_update(struct lola *chip, struct lola_pcm *pcm, unsigned int bits)
  475. {
  476. int i;
  477. for (i = 0; bits && i < pcm->num_streams; i++) {
  478. if (bits & (1 << i)) {
  479. struct lola_stream *str = &pcm->streams[i];
  480. if (str->substream && str->running)
  481. snd_pcm_period_elapsed(str->substream);
  482. bits &= ~(1 << i);
  483. }
  484. }
  485. }
  486. static struct snd_pcm_ops lola_pcm_ops = {
  487. .open = lola_pcm_open,
  488. .close = lola_pcm_close,
  489. .ioctl = snd_pcm_lib_ioctl,
  490. .hw_params = lola_pcm_hw_params,
  491. .hw_free = lola_pcm_hw_free,
  492. .prepare = lola_pcm_prepare,
  493. .trigger = lola_pcm_trigger,
  494. .pointer = lola_pcm_pointer,
  495. .page = snd_pcm_sgbuf_ops_page,
  496. };
  497. int __devinit lola_create_pcm(struct lola *chip)
  498. {
  499. struct snd_pcm *pcm;
  500. int i, err;
  501. for (i = 0; i < 2; i++) {
  502. err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV,
  503. snd_dma_pci_data(chip->pci),
  504. PAGE_SIZE, &chip->pcm[i].bdl);
  505. if (err < 0)
  506. return err;
  507. }
  508. err = snd_pcm_new(chip->card, "Digigram Lola", 0,
  509. chip->pcm[SNDRV_PCM_STREAM_PLAYBACK].num_streams,
  510. chip->pcm[SNDRV_PCM_STREAM_CAPTURE].num_streams,
  511. &pcm);
  512. if (err < 0)
  513. return err;
  514. strlcpy(pcm->name, "Digigram Lola", sizeof(pcm->name));
  515. pcm->private_data = chip;
  516. for (i = 0; i < 2; i++) {
  517. if (chip->pcm[i].num_streams)
  518. snd_pcm_set_ops(pcm, i, &lola_pcm_ops);
  519. }
  520. /* buffer pre-allocation */
  521. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
  522. snd_dma_pci_data(chip->pci),
  523. 1024 * 64, 32 * 1024 * 1024);
  524. return 0;
  525. }
  526. void lola_free_pcm(struct lola *chip)
  527. {
  528. snd_dma_free_pages(&chip->pcm[0].bdl);
  529. snd_dma_free_pages(&chip->pcm[1].bdl);
  530. }
  531. /*
  532. */
  533. static int lola_init_stream(struct lola *chip, struct lola_stream *str,
  534. int idx, int nid, int dir)
  535. {
  536. unsigned int val;
  537. int err;
  538. str->nid = nid;
  539. str->index = idx;
  540. str->dsd = idx;
  541. if (dir == PLAY)
  542. str->dsd += MAX_STREAM_IN_COUNT;
  543. err = lola_read_param(chip, nid, LOLA_PAR_AUDIO_WIDGET_CAP, &val);
  544. if (err < 0) {
  545. printk(KERN_ERR SFX "Can't read wcaps for 0x%x\n", nid);
  546. return err;
  547. }
  548. if (dir == PLAY) {
  549. /* test TYPE and bits 0..11 (no test bit9 : Digital = 0/1) */
  550. if ((val & 0x00f00dff) != 0x00000010) {
  551. printk(KERN_ERR SFX "Invalid wcaps 0x%x for 0x%x\n",
  552. val, nid);
  553. return -EINVAL;
  554. }
  555. } else {
  556. /* test TYPE and bits 0..11 (no test bit9 : Digital = 0/1)
  557. * (bug : ignore bit8: Conn list = 0/1)
  558. */
  559. if ((val & 0x00f00cff) != 0x00100010) {
  560. printk(KERN_ERR SFX "Invalid wcaps 0x%x for 0x%x\n",
  561. val, nid);
  562. return -EINVAL;
  563. }
  564. /* test bit9:DIGITAL and bit12:SRC_PRESENT*/
  565. if ((val & 0x00001200) == 0x00001200)
  566. chip->input_src_caps_mask |= (1 << idx);
  567. }
  568. err = lola_read_param(chip, nid, LOLA_PAR_STREAM_FORMATS, &val);
  569. if (err < 0) {
  570. printk(KERN_ERR SFX "Can't read FORMATS 0x%x\n", nid);
  571. return err;
  572. }
  573. val &= 3;
  574. if (val == 3)
  575. str->can_float = true;
  576. if (!(val & 1)) {
  577. printk(KERN_ERR SFX "Invalid formats 0x%x for 0x%x", val, nid);
  578. return -EINVAL;
  579. }
  580. return 0;
  581. }
  582. int __devinit lola_init_pcm(struct lola *chip, int dir, int *nidp)
  583. {
  584. struct lola_pcm *pcm = &chip->pcm[dir];
  585. int i, nid, err;
  586. nid = *nidp;
  587. for (i = 0; i < pcm->num_streams; i++, nid++) {
  588. err = lola_init_stream(chip, &pcm->streams[i], i, nid, dir);
  589. if (err < 0)
  590. return err;
  591. }
  592. *nidp = nid;
  593. return 0;
  594. }