lola_pcm.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  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. /* sync for FIFO ready/empty for all linked streams;
  113. * clear paused flag when FIFO gets ready again
  114. */
  115. static int lola_sync_wait_for_fifo(struct lola *chip,
  116. struct snd_pcm_substream *substream,
  117. bool ready)
  118. {
  119. unsigned int val = ready ? LOLA_DSD_STS_FIFORDY : 0;
  120. unsigned long end_time = jiffies + msecs_to_jiffies(200);
  121. struct snd_pcm_substream *s;
  122. int pending = 0;
  123. while (time_before(jiffies, end_time)) {
  124. pending = 0;
  125. snd_pcm_group_for_each_entry(s, substream) {
  126. struct lola_stream *str;
  127. if (s->pcm->card != substream->pcm->card)
  128. continue;
  129. str = lola_get_stream(s);
  130. if (str->prepared && str->paused) {
  131. unsigned int reg;
  132. reg = lola_dsd_read(chip, str->dsd, STS);
  133. if ((reg & LOLA_DSD_STS_FIFORDY) != val) {
  134. pending = str->dsd + 1;
  135. break;
  136. }
  137. if (ready)
  138. str->paused = 0;
  139. }
  140. }
  141. if (!pending)
  142. return 0;
  143. msleep(1);
  144. }
  145. printk(KERN_WARNING SFX "FIFO not ready (pending %d)\n", pending - 1);
  146. return -EIO;
  147. }
  148. /* finish pause - prepare for a new resume */
  149. static void lola_sync_pause(struct lola *chip,
  150. struct snd_pcm_substream *substream)
  151. {
  152. struct snd_pcm_substream *s;
  153. lola_sync_wait_for_fifo(chip, substream, false);
  154. snd_pcm_group_for_each_entry(s, substream) {
  155. struct lola_stream *str;
  156. if (s->pcm->card != substream->pcm->card)
  157. continue;
  158. str = lola_get_stream(s);
  159. if (str->paused && str->prepared)
  160. lola_dsd_write(chip, str->dsd, CTL, LOLA_DSD_CTL_SRUN |
  161. LOLA_DSD_CTL_IOCE | LOLA_DSD_CTL_DEIE);
  162. }
  163. lola_sync_wait_for_fifo(chip, substream, true);
  164. }
  165. static void lola_stream_reset(struct lola *chip, struct lola_stream *str)
  166. {
  167. if (str->prepared) {
  168. if (str->paused)
  169. lola_sync_pause(chip, str->substream);
  170. str->prepared = 0;
  171. lola_dsd_write(chip, str->dsd, CTL,
  172. LOLA_DSD_CTL_IOCE | LOLA_DSD_CTL_DEIE);
  173. lola_stream_wait_for_fifo(chip, str, false);
  174. lola_stream_clear_pending_irq(chip, str);
  175. lola_dsd_write(chip, str->dsd, CTL, LOLA_DSD_CTL_SRST);
  176. lola_dsd_write(chip, str->dsd, LVI, 0);
  177. lola_dsd_write(chip, str->dsd, BDPU, 0);
  178. lola_dsd_write(chip, str->dsd, BDPL, 0);
  179. wait_for_srst_clear(chip, str);
  180. }
  181. }
  182. static struct snd_pcm_hardware lola_pcm_hw = {
  183. .info = (SNDRV_PCM_INFO_MMAP |
  184. SNDRV_PCM_INFO_INTERLEAVED |
  185. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  186. SNDRV_PCM_INFO_MMAP_VALID |
  187. SNDRV_PCM_INFO_PAUSE),
  188. .formats = (SNDRV_PCM_FMTBIT_S16_LE |
  189. SNDRV_PCM_FMTBIT_S24_LE |
  190. SNDRV_PCM_FMTBIT_S32_LE |
  191. SNDRV_PCM_FMTBIT_FLOAT_LE),
  192. .rates = SNDRV_PCM_RATE_8000_192000,
  193. .rate_min = 8000,
  194. .rate_max = 192000,
  195. .channels_min = 1,
  196. .channels_max = 2,
  197. .buffer_bytes_max = LOLA_MAX_BUF_SIZE,
  198. .period_bytes_min = 128,
  199. .period_bytes_max = LOLA_MAX_BUF_SIZE / 2,
  200. .periods_min = 2,
  201. .periods_max = LOLA_MAX_BDL_ENTRIES,
  202. .fifo_size = 0,
  203. };
  204. static int lola_pcm_open(struct snd_pcm_substream *substream)
  205. {
  206. struct lola *chip = snd_pcm_substream_chip(substream);
  207. struct lola_pcm *pcm = lola_get_pcm(substream);
  208. struct lola_stream *str = lola_get_stream(substream);
  209. struct snd_pcm_runtime *runtime = substream->runtime;
  210. mutex_lock(&chip->open_mutex);
  211. if (str->opened) {
  212. mutex_unlock(&chip->open_mutex);
  213. return -EBUSY;
  214. }
  215. str->substream = substream;
  216. str->master = NULL;
  217. str->opened = 1;
  218. runtime->hw = lola_pcm_hw;
  219. runtime->hw.channels_max = pcm->num_streams - str->index;
  220. if (chip->sample_rate) {
  221. /* sample rate is locked */
  222. runtime->hw.rate_min = chip->sample_rate;
  223. runtime->hw.rate_max = chip->sample_rate;
  224. } else {
  225. runtime->hw.rate_min = chip->sample_rate_min;
  226. runtime->hw.rate_max = chip->sample_rate_max;
  227. }
  228. chip->ref_count_rate++;
  229. snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
  230. /* period size = multiple of chip->granularity (8, 16 or 32 frames)*/
  231. snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
  232. chip->granularity);
  233. snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
  234. chip->granularity);
  235. mutex_unlock(&chip->open_mutex);
  236. return 0;
  237. }
  238. static void lola_cleanup_slave_streams(struct lola_pcm *pcm,
  239. struct lola_stream *str)
  240. {
  241. int i;
  242. for (i = str->index + 1; i < pcm->num_streams; i++) {
  243. struct lola_stream *s = &pcm->streams[i];
  244. if (s->master != str)
  245. break;
  246. s->master = NULL;
  247. s->opened = 0;
  248. }
  249. }
  250. static int lola_pcm_close(struct snd_pcm_substream *substream)
  251. {
  252. struct lola *chip = snd_pcm_substream_chip(substream);
  253. struct lola_stream *str = lola_get_stream(substream);
  254. mutex_lock(&chip->open_mutex);
  255. if (str->substream == substream) {
  256. str->substream = NULL;
  257. str->opened = 0;
  258. }
  259. if (--chip->ref_count_rate == 0) {
  260. /* release sample rate */
  261. chip->sample_rate = 0;
  262. }
  263. mutex_unlock(&chip->open_mutex);
  264. return 0;
  265. }
  266. static int lola_pcm_hw_params(struct snd_pcm_substream *substream,
  267. struct snd_pcm_hw_params *hw_params)
  268. {
  269. struct lola_stream *str = lola_get_stream(substream);
  270. str->bufsize = 0;
  271. str->period_bytes = 0;
  272. str->format_verb = 0;
  273. return snd_pcm_lib_malloc_pages(substream,
  274. params_buffer_bytes(hw_params));
  275. }
  276. static int lola_pcm_hw_free(struct snd_pcm_substream *substream)
  277. {
  278. struct lola *chip = snd_pcm_substream_chip(substream);
  279. struct lola_pcm *pcm = lola_get_pcm(substream);
  280. struct lola_stream *str = lola_get_stream(substream);
  281. mutex_lock(&chip->open_mutex);
  282. lola_stream_reset(chip, str);
  283. lola_cleanup_slave_streams(pcm, str);
  284. mutex_unlock(&chip->open_mutex);
  285. return snd_pcm_lib_free_pages(substream);
  286. }
  287. /*
  288. * set up a BDL entry
  289. */
  290. static int setup_bdle(struct snd_pcm_substream *substream,
  291. struct lola_stream *str, u32 **bdlp,
  292. int ofs, int size)
  293. {
  294. u32 *bdl = *bdlp;
  295. while (size > 0) {
  296. dma_addr_t addr;
  297. int chunk;
  298. if (str->frags >= LOLA_MAX_BDL_ENTRIES)
  299. return -EINVAL;
  300. addr = snd_pcm_sgbuf_get_addr(substream, ofs);
  301. /* program the address field of the BDL entry */
  302. bdl[0] = cpu_to_le32((u32)addr);
  303. bdl[1] = cpu_to_le32(upper_32_bits(addr));
  304. /* program the size field of the BDL entry */
  305. chunk = snd_pcm_sgbuf_get_chunk_size(substream, ofs, size);
  306. bdl[2] = cpu_to_le32(chunk);
  307. /* program the IOC to enable interrupt
  308. * only when the whole fragment is processed
  309. */
  310. size -= chunk;
  311. bdl[3] = size ? 0 : cpu_to_le32(0x01);
  312. bdl += 4;
  313. str->frags++;
  314. ofs += chunk;
  315. }
  316. *bdlp = bdl;
  317. return ofs;
  318. }
  319. /*
  320. * set up BDL entries
  321. */
  322. static int lola_setup_periods(struct lola *chip, struct lola_pcm *pcm,
  323. struct snd_pcm_substream *substream,
  324. struct lola_stream *str)
  325. {
  326. u32 *bdl;
  327. int i, ofs, periods, period_bytes;
  328. period_bytes = str->period_bytes;
  329. periods = str->bufsize / period_bytes;
  330. /* program the initial BDL entries */
  331. bdl = (u32 *)(pcm->bdl.area + LOLA_BDL_ENTRY_SIZE * str->index);
  332. ofs = 0;
  333. str->frags = 0;
  334. for (i = 0; i < periods; i++) {
  335. ofs = setup_bdle(substream, str, &bdl, ofs, period_bytes);
  336. if (ofs < 0)
  337. goto error;
  338. }
  339. return 0;
  340. error:
  341. snd_printk(KERN_ERR SFX "Too many BDL entries: buffer=%d, period=%d\n",
  342. str->bufsize, period_bytes);
  343. return -EINVAL;
  344. }
  345. static unsigned int lola_get_format_verb(struct snd_pcm_substream *substream)
  346. {
  347. unsigned int verb;
  348. switch (substream->runtime->format) {
  349. case SNDRV_PCM_FORMAT_S16_LE:
  350. verb = 0x00000000;
  351. break;
  352. case SNDRV_PCM_FORMAT_S24_LE:
  353. verb = 0x00000200;
  354. break;
  355. case SNDRV_PCM_FORMAT_S32_LE:
  356. verb = 0x00000300;
  357. break;
  358. case SNDRV_PCM_FORMAT_FLOAT_LE:
  359. verb = 0x00001300;
  360. break;
  361. default:
  362. return 0;
  363. }
  364. verb |= substream->runtime->channels;
  365. return verb;
  366. }
  367. static int lola_set_stream_config(struct lola *chip,
  368. struct lola_stream *str,
  369. int channels)
  370. {
  371. int i, err;
  372. unsigned int verb, val;
  373. /* set format info for all channels
  374. * (with only one command for the first channel)
  375. */
  376. err = lola_codec_read(chip, str->nid, LOLA_VERB_SET_STREAM_FORMAT,
  377. str->format_verb, 0, &val, NULL);
  378. if (err < 0) {
  379. printk(KERN_ERR SFX "Cannot set stream format 0x%x\n",
  380. str->format_verb);
  381. return err;
  382. }
  383. /* update stream - channel config */
  384. for (i = 0; i < channels; i++) {
  385. verb = (str->index << 6) | i;
  386. err = lola_codec_read(chip, str[i].nid,
  387. LOLA_VERB_SET_CHANNEL_STREAMID, 0, verb,
  388. &val, NULL);
  389. if (err < 0) {
  390. printk(KERN_ERR SFX "Cannot set stream channel %d\n", i);
  391. return err;
  392. }
  393. }
  394. return 0;
  395. }
  396. /*
  397. * set up the SD for streaming
  398. */
  399. static int lola_setup_controller(struct lola *chip, struct lola_pcm *pcm,
  400. struct lola_stream *str)
  401. {
  402. dma_addr_t bdl;
  403. if (str->prepared)
  404. return -EINVAL;
  405. /* set up BDL */
  406. bdl = pcm->bdl.addr + LOLA_BDL_ENTRY_SIZE * str->index;
  407. lola_dsd_write(chip, str->dsd, BDPL, (u32)bdl);
  408. lola_dsd_write(chip, str->dsd, BDPU, upper_32_bits(bdl));
  409. /* program the stream LVI (last valid index) of the BDL */
  410. lola_dsd_write(chip, str->dsd, LVI, str->frags - 1);
  411. lola_stream_clear_pending_irq(chip, str);
  412. lola_dsd_write(chip, str->dsd, CTL,
  413. LOLA_DSD_CTL_IOCE | LOLA_DSD_CTL_DEIE | LOLA_DSD_CTL_SRUN);
  414. str->prepared = 1;
  415. return lola_stream_wait_for_fifo(chip, str, true);
  416. }
  417. static int lola_pcm_prepare(struct snd_pcm_substream *substream)
  418. {
  419. struct lola *chip = snd_pcm_substream_chip(substream);
  420. struct lola_pcm *pcm = lola_get_pcm(substream);
  421. struct lola_stream *str = lola_get_stream(substream);
  422. struct snd_pcm_runtime *runtime = substream->runtime;
  423. unsigned int bufsize, period_bytes, format_verb;
  424. int i, err;
  425. mutex_lock(&chip->open_mutex);
  426. lola_stream_reset(chip, str);
  427. lola_cleanup_slave_streams(pcm, str);
  428. if (str->index + runtime->channels >= pcm->num_streams) {
  429. mutex_unlock(&chip->open_mutex);
  430. return -EINVAL;
  431. }
  432. for (i = 1; i < runtime->channels; i++) {
  433. str[i].master = str;
  434. str[i].opened = 1;
  435. }
  436. mutex_unlock(&chip->open_mutex);
  437. bufsize = snd_pcm_lib_buffer_bytes(substream);
  438. period_bytes = snd_pcm_lib_period_bytes(substream);
  439. format_verb = lola_get_format_verb(substream);
  440. str->bufsize = bufsize;
  441. str->period_bytes = period_bytes;
  442. str->format_verb = format_verb;
  443. err = lola_setup_periods(chip, pcm, substream, str);
  444. if (err < 0)
  445. return err;
  446. err = lola_set_sample_rate(chip, runtime->rate);
  447. if (err < 0)
  448. return err;
  449. chip->sample_rate = runtime->rate; /* sample rate gets locked */
  450. err = lola_set_stream_config(chip, str, runtime->channels);
  451. if (err < 0)
  452. return err;
  453. err = lola_setup_controller(chip, pcm, str);
  454. if (err < 0) {
  455. lola_stream_reset(chip, str);
  456. return err;
  457. }
  458. return 0;
  459. }
  460. static int lola_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  461. {
  462. struct lola *chip = snd_pcm_substream_chip(substream);
  463. struct lola_stream *str;
  464. struct snd_pcm_substream *s;
  465. unsigned int start;
  466. unsigned int tstamp;
  467. bool sync_streams;
  468. switch (cmd) {
  469. case SNDRV_PCM_TRIGGER_START:
  470. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  471. case SNDRV_PCM_TRIGGER_RESUME:
  472. start = 1;
  473. break;
  474. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  475. case SNDRV_PCM_TRIGGER_SUSPEND:
  476. case SNDRV_PCM_TRIGGER_STOP:
  477. start = 0;
  478. break;
  479. default:
  480. return -EINVAL;
  481. }
  482. /*
  483. * sample correct synchronization is only needed starting several
  484. * streams on stop or if only one stream do as quick as possible
  485. */
  486. sync_streams = (start && snd_pcm_stream_linked(substream));
  487. tstamp = lola_get_tstamp(chip, !sync_streams);
  488. spin_lock(&chip->reg_lock);
  489. snd_pcm_group_for_each_entry(s, substream) {
  490. if (s->pcm->card != substream->pcm->card)
  491. continue;
  492. str = lola_get_stream(s);
  493. if (start)
  494. lola_stream_start(chip, str, tstamp);
  495. else
  496. lola_stream_stop(chip, str, tstamp);
  497. str->running = start;
  498. str->paused = !start;
  499. snd_pcm_trigger_done(s, substream);
  500. }
  501. spin_unlock(&chip->reg_lock);
  502. return 0;
  503. }
  504. static snd_pcm_uframes_t lola_pcm_pointer(struct snd_pcm_substream *substream)
  505. {
  506. struct lola *chip = snd_pcm_substream_chip(substream);
  507. struct lola_stream *str = lola_get_stream(substream);
  508. unsigned int pos = lola_dsd_read(chip, str->dsd, LPIB);
  509. if (pos >= str->bufsize)
  510. pos = 0;
  511. return bytes_to_frames(substream->runtime, pos);
  512. }
  513. void lola_pcm_update(struct lola *chip, struct lola_pcm *pcm, unsigned int bits)
  514. {
  515. int i;
  516. for (i = 0; bits && i < pcm->num_streams; i++) {
  517. if (bits & (1 << i)) {
  518. struct lola_stream *str = &pcm->streams[i];
  519. if (str->substream && str->running)
  520. snd_pcm_period_elapsed(str->substream);
  521. bits &= ~(1 << i);
  522. }
  523. }
  524. }
  525. static struct snd_pcm_ops lola_pcm_ops = {
  526. .open = lola_pcm_open,
  527. .close = lola_pcm_close,
  528. .ioctl = snd_pcm_lib_ioctl,
  529. .hw_params = lola_pcm_hw_params,
  530. .hw_free = lola_pcm_hw_free,
  531. .prepare = lola_pcm_prepare,
  532. .trigger = lola_pcm_trigger,
  533. .pointer = lola_pcm_pointer,
  534. .page = snd_pcm_sgbuf_ops_page,
  535. };
  536. int __devinit lola_create_pcm(struct lola *chip)
  537. {
  538. struct snd_pcm *pcm;
  539. int i, err;
  540. for (i = 0; i < 2; i++) {
  541. err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV,
  542. snd_dma_pci_data(chip->pci),
  543. PAGE_SIZE, &chip->pcm[i].bdl);
  544. if (err < 0)
  545. return err;
  546. }
  547. err = snd_pcm_new(chip->card, "Digigram Lola", 0,
  548. chip->pcm[SNDRV_PCM_STREAM_PLAYBACK].num_streams,
  549. chip->pcm[SNDRV_PCM_STREAM_CAPTURE].num_streams,
  550. &pcm);
  551. if (err < 0)
  552. return err;
  553. strlcpy(pcm->name, "Digigram Lola", sizeof(pcm->name));
  554. pcm->private_data = chip;
  555. for (i = 0; i < 2; i++) {
  556. if (chip->pcm[i].num_streams)
  557. snd_pcm_set_ops(pcm, i, &lola_pcm_ops);
  558. }
  559. /* buffer pre-allocation */
  560. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
  561. snd_dma_pci_data(chip->pci),
  562. 1024 * 64, 32 * 1024 * 1024);
  563. return 0;
  564. }
  565. void lola_free_pcm(struct lola *chip)
  566. {
  567. snd_dma_free_pages(&chip->pcm[0].bdl);
  568. snd_dma_free_pages(&chip->pcm[1].bdl);
  569. }
  570. /*
  571. */
  572. static int lola_init_stream(struct lola *chip, struct lola_stream *str,
  573. int idx, int nid, int dir)
  574. {
  575. unsigned int val;
  576. int err;
  577. str->nid = nid;
  578. str->index = idx;
  579. str->dsd = idx;
  580. if (dir == PLAY)
  581. str->dsd += MAX_STREAM_IN_COUNT;
  582. err = lola_read_param(chip, nid, LOLA_PAR_AUDIO_WIDGET_CAP, &val);
  583. if (err < 0) {
  584. printk(KERN_ERR SFX "Can't read wcaps for 0x%x\n", nid);
  585. return err;
  586. }
  587. if (dir == PLAY) {
  588. /* test TYPE and bits 0..11 (no test bit9 : Digital = 0/1) */
  589. if ((val & 0x00f00dff) != 0x00000010) {
  590. printk(KERN_ERR SFX "Invalid wcaps 0x%x for 0x%x\n",
  591. val, nid);
  592. return -EINVAL;
  593. }
  594. } else {
  595. /* test TYPE and bits 0..11 (no test bit9 : Digital = 0/1)
  596. * (bug : ignore bit8: Conn list = 0/1)
  597. */
  598. if ((val & 0x00f00cff) != 0x00100010) {
  599. printk(KERN_ERR SFX "Invalid wcaps 0x%x for 0x%x\n",
  600. val, nid);
  601. return -EINVAL;
  602. }
  603. /* test bit9:DIGITAL and bit12:SRC_PRESENT*/
  604. if ((val & 0x00001200) == 0x00001200)
  605. chip->input_src_caps_mask |= (1 << idx);
  606. }
  607. err = lola_read_param(chip, nid, LOLA_PAR_STREAM_FORMATS, &val);
  608. if (err < 0) {
  609. printk(KERN_ERR SFX "Can't read FORMATS 0x%x\n", nid);
  610. return err;
  611. }
  612. val &= 3;
  613. if (val == 3)
  614. str->can_float = true;
  615. if (!(val & 1)) {
  616. printk(KERN_ERR SFX "Invalid formats 0x%x for 0x%x", val, nid);
  617. return -EINVAL;
  618. }
  619. return 0;
  620. }
  621. int __devinit lola_init_pcm(struct lola *chip, int dir, int *nidp)
  622. {
  623. struct lola_pcm *pcm = &chip->pcm[dir];
  624. int i, nid, err;
  625. nid = *nidp;
  626. for (i = 0; i < pcm->num_streams; i++, nid++) {
  627. err = lola_init_stream(chip, &pcm->streams[i], i, nid, dir);
  628. if (err < 0)
  629. return err;
  630. }
  631. *nidp = nid;
  632. return 0;
  633. }