lola_pcm.c 18 KB

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