lola_pcm.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  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. dev_warn(chip->card->dev, "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. dev_warn(chip->card->dev, "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. dev_warn(chip->card->dev, "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. dev_err(chip->card->dev, "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. dev_err(chip->card->dev, "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. dev_err(chip->card->dev,
  392. "Cannot set stream channel %d\n", i);
  393. return err;
  394. }
  395. }
  396. return 0;
  397. }
  398. /*
  399. * set up the SD for streaming
  400. */
  401. static int lola_setup_controller(struct lola *chip, struct lola_pcm *pcm,
  402. struct lola_stream *str)
  403. {
  404. dma_addr_t bdl;
  405. if (str->prepared)
  406. return -EINVAL;
  407. /* set up BDL */
  408. bdl = pcm->bdl.addr + LOLA_BDL_ENTRY_SIZE * str->index;
  409. lola_dsd_write(chip, str->dsd, BDPL, (u32)bdl);
  410. lola_dsd_write(chip, str->dsd, BDPU, upper_32_bits(bdl));
  411. /* program the stream LVI (last valid index) of the BDL */
  412. lola_dsd_write(chip, str->dsd, LVI, str->frags - 1);
  413. lola_stream_clear_pending_irq(chip, str);
  414. lola_dsd_write(chip, str->dsd, CTL,
  415. LOLA_DSD_CTL_IOCE | LOLA_DSD_CTL_DEIE | LOLA_DSD_CTL_SRUN);
  416. str->prepared = 1;
  417. return lola_stream_wait_for_fifo(chip, str, true);
  418. }
  419. static int lola_pcm_prepare(struct snd_pcm_substream *substream)
  420. {
  421. struct lola *chip = snd_pcm_substream_chip(substream);
  422. struct lola_pcm *pcm = lola_get_pcm(substream);
  423. struct lola_stream *str = lola_get_stream(substream);
  424. struct snd_pcm_runtime *runtime = substream->runtime;
  425. unsigned int bufsize, period_bytes, format_verb;
  426. int i, err;
  427. mutex_lock(&chip->open_mutex);
  428. lola_stream_reset(chip, str);
  429. lola_cleanup_slave_streams(pcm, str);
  430. if (str->index + runtime->channels > pcm->num_streams) {
  431. mutex_unlock(&chip->open_mutex);
  432. return -EINVAL;
  433. }
  434. for (i = 1; i < runtime->channels; i++) {
  435. str[i].master = str;
  436. str[i].opened = 1;
  437. }
  438. mutex_unlock(&chip->open_mutex);
  439. bufsize = snd_pcm_lib_buffer_bytes(substream);
  440. period_bytes = snd_pcm_lib_period_bytes(substream);
  441. format_verb = lola_get_format_verb(substream);
  442. str->bufsize = bufsize;
  443. str->period_bytes = period_bytes;
  444. str->format_verb = format_verb;
  445. err = lola_setup_periods(chip, pcm, substream, str);
  446. if (err < 0)
  447. return err;
  448. err = lola_set_sample_rate(chip, runtime->rate);
  449. if (err < 0)
  450. return err;
  451. chip->sample_rate = runtime->rate; /* sample rate gets locked */
  452. err = lola_set_stream_config(chip, str, runtime->channels);
  453. if (err < 0)
  454. return err;
  455. err = lola_setup_controller(chip, pcm, str);
  456. if (err < 0) {
  457. lola_stream_reset(chip, str);
  458. return err;
  459. }
  460. return 0;
  461. }
  462. static int lola_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  463. {
  464. struct lola *chip = snd_pcm_substream_chip(substream);
  465. struct lola_stream *str;
  466. struct snd_pcm_substream *s;
  467. unsigned int start;
  468. unsigned int tstamp;
  469. bool sync_streams;
  470. switch (cmd) {
  471. case SNDRV_PCM_TRIGGER_START:
  472. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  473. case SNDRV_PCM_TRIGGER_RESUME:
  474. start = 1;
  475. break;
  476. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  477. case SNDRV_PCM_TRIGGER_SUSPEND:
  478. case SNDRV_PCM_TRIGGER_STOP:
  479. start = 0;
  480. break;
  481. default:
  482. return -EINVAL;
  483. }
  484. /*
  485. * sample correct synchronization is only needed starting several
  486. * streams. On stop or if only one stream do as quick as possible
  487. */
  488. sync_streams = (start && snd_pcm_stream_linked(substream));
  489. tstamp = lola_get_tstamp(chip, !sync_streams);
  490. spin_lock(&chip->reg_lock);
  491. snd_pcm_group_for_each_entry(s, substream) {
  492. if (s->pcm->card != substream->pcm->card)
  493. continue;
  494. str = lola_get_stream(s);
  495. if (start)
  496. lola_stream_start(chip, str, tstamp);
  497. else
  498. lola_stream_stop(chip, str, tstamp);
  499. str->running = start;
  500. str->paused = !start;
  501. snd_pcm_trigger_done(s, substream);
  502. }
  503. spin_unlock(&chip->reg_lock);
  504. return 0;
  505. }
  506. static snd_pcm_uframes_t lola_pcm_pointer(struct snd_pcm_substream *substream)
  507. {
  508. struct lola *chip = snd_pcm_substream_chip(substream);
  509. struct lola_stream *str = lola_get_stream(substream);
  510. unsigned int pos = lola_dsd_read(chip, str->dsd, LPIB);
  511. if (pos >= str->bufsize)
  512. pos = 0;
  513. return bytes_to_frames(substream->runtime, pos);
  514. }
  515. void lola_pcm_update(struct lola *chip, struct lola_pcm *pcm, unsigned int bits)
  516. {
  517. int i;
  518. for (i = 0; bits && i < pcm->num_streams; i++) {
  519. if (bits & (1 << i)) {
  520. struct lola_stream *str = &pcm->streams[i];
  521. if (str->substream && str->running)
  522. snd_pcm_period_elapsed(str->substream);
  523. bits &= ~(1 << i);
  524. }
  525. }
  526. }
  527. static const struct snd_pcm_ops lola_pcm_ops = {
  528. .open = lola_pcm_open,
  529. .close = lola_pcm_close,
  530. .ioctl = snd_pcm_lib_ioctl,
  531. .hw_params = lola_pcm_hw_params,
  532. .hw_free = lola_pcm_hw_free,
  533. .prepare = lola_pcm_prepare,
  534. .trigger = lola_pcm_trigger,
  535. .pointer = lola_pcm_pointer,
  536. .page = snd_pcm_sgbuf_ops_page,
  537. };
  538. int lola_create_pcm(struct lola *chip)
  539. {
  540. struct snd_pcm *pcm;
  541. int i, err;
  542. for (i = 0; i < 2; i++) {
  543. err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV,
  544. snd_dma_pci_data(chip->pci),
  545. PAGE_SIZE, &chip->pcm[i].bdl);
  546. if (err < 0)
  547. return err;
  548. }
  549. err = snd_pcm_new(chip->card, "Digigram Lola", 0,
  550. chip->pcm[SNDRV_PCM_STREAM_PLAYBACK].num_streams,
  551. chip->pcm[SNDRV_PCM_STREAM_CAPTURE].num_streams,
  552. &pcm);
  553. if (err < 0)
  554. return err;
  555. strlcpy(pcm->name, "Digigram Lola", sizeof(pcm->name));
  556. pcm->private_data = chip;
  557. for (i = 0; i < 2; i++) {
  558. if (chip->pcm[i].num_streams)
  559. snd_pcm_set_ops(pcm, i, &lola_pcm_ops);
  560. }
  561. /* buffer pre-allocation */
  562. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
  563. snd_dma_pci_data(chip->pci),
  564. 1024 * 64, 32 * 1024 * 1024);
  565. return 0;
  566. }
  567. void lola_free_pcm(struct lola *chip)
  568. {
  569. snd_dma_free_pages(&chip->pcm[0].bdl);
  570. snd_dma_free_pages(&chip->pcm[1].bdl);
  571. }
  572. /*
  573. */
  574. static int lola_init_stream(struct lola *chip, struct lola_stream *str,
  575. int idx, int nid, int dir)
  576. {
  577. unsigned int val;
  578. int err;
  579. str->nid = nid;
  580. str->index = idx;
  581. str->dsd = idx;
  582. if (dir == PLAY)
  583. str->dsd += MAX_STREAM_IN_COUNT;
  584. err = lola_read_param(chip, nid, LOLA_PAR_AUDIO_WIDGET_CAP, &val);
  585. if (err < 0) {
  586. dev_err(chip->card->dev, "Can't read wcaps for 0x%x\n", nid);
  587. return err;
  588. }
  589. if (dir == PLAY) {
  590. /* test TYPE and bits 0..11 (no test bit9 : Digital = 0/1) */
  591. if ((val & 0x00f00dff) != 0x00000010) {
  592. dev_err(chip->card->dev,
  593. "Invalid wcaps 0x%x for 0x%x\n",
  594. val, nid);
  595. return -EINVAL;
  596. }
  597. } else {
  598. /* test TYPE and bits 0..11 (no test bit9 : Digital = 0/1)
  599. * (bug : ignore bit8: Conn list = 0/1)
  600. */
  601. if ((val & 0x00f00cff) != 0x00100010) {
  602. dev_err(chip->card->dev,
  603. "Invalid wcaps 0x%x for 0x%x\n",
  604. val, nid);
  605. return -EINVAL;
  606. }
  607. /* test bit9:DIGITAL and bit12:SRC_PRESENT*/
  608. if ((val & 0x00001200) == 0x00001200)
  609. chip->input_src_caps_mask |= (1 << idx);
  610. }
  611. err = lola_read_param(chip, nid, LOLA_PAR_STREAM_FORMATS, &val);
  612. if (err < 0) {
  613. dev_err(chip->card->dev, "Can't read FORMATS 0x%x\n", nid);
  614. return err;
  615. }
  616. val &= 3;
  617. if (val == 3)
  618. str->can_float = true;
  619. if (!(val & 1)) {
  620. dev_err(chip->card->dev,
  621. "Invalid formats 0x%x for 0x%x", val, nid);
  622. return -EINVAL;
  623. }
  624. return 0;
  625. }
  626. int lola_init_pcm(struct lola *chip, int dir, int *nidp)
  627. {
  628. struct lola_pcm *pcm = &chip->pcm[dir];
  629. int i, nid, err;
  630. nid = *nidp;
  631. for (i = 0; i < pcm->num_streams; i++, nid++) {
  632. err = lola_init_stream(chip, &pcm->streams[i], i, nid, dir);
  633. if (err < 0)
  634. return err;
  635. }
  636. *nidp = nid;
  637. return 0;
  638. }