lola_pcm.c 18 KB

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