hdac_stream.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  1. /*
  2. * HD-audio stream operations
  3. */
  4. #include <linux/kernel.h>
  5. #include <linux/delay.h>
  6. #include <linux/export.h>
  7. #include <linux/clocksource.h>
  8. #include <sound/core.h>
  9. #include <sound/pcm.h>
  10. #include <sound/hdaudio.h>
  11. #include <sound/hda_register.h>
  12. #include "trace.h"
  13. /**
  14. * snd_hdac_stream_init - initialize each stream (aka device)
  15. * @bus: HD-audio core bus
  16. * @azx_dev: HD-audio core stream object to initialize
  17. * @idx: stream index number
  18. * @direction: stream direction (SNDRV_PCM_STREAM_PLAYBACK or SNDRV_PCM_STREAM_CAPTURE)
  19. * @tag: the tag id to assign
  20. *
  21. * Assign the starting bdl address to each stream (device) and initialize.
  22. */
  23. void snd_hdac_stream_init(struct hdac_bus *bus, struct hdac_stream *azx_dev,
  24. int idx, int direction, int tag)
  25. {
  26. azx_dev->bus = bus;
  27. /* offset: SDI0=0x80, SDI1=0xa0, ... SDO3=0x160 */
  28. azx_dev->sd_addr = bus->remap_addr + (0x20 * idx + 0x80);
  29. /* int mask: SDI0=0x01, SDI1=0x02, ... SDO3=0x80 */
  30. azx_dev->sd_int_sta_mask = 1 << idx;
  31. azx_dev->index = idx;
  32. azx_dev->direction = direction;
  33. azx_dev->stream_tag = tag;
  34. snd_hdac_dsp_lock_init(azx_dev);
  35. list_add_tail(&azx_dev->list, &bus->stream_list);
  36. }
  37. EXPORT_SYMBOL_GPL(snd_hdac_stream_init);
  38. /**
  39. * snd_hdac_stream_start - start a stream
  40. * @azx_dev: HD-audio core stream to start
  41. * @fresh_start: false = wallclock timestamp relative to period wallclock
  42. *
  43. * Start a stream, set start_wallclk and set the running flag.
  44. */
  45. void snd_hdac_stream_start(struct hdac_stream *azx_dev, bool fresh_start)
  46. {
  47. struct hdac_bus *bus = azx_dev->bus;
  48. trace_snd_hdac_stream_start(bus, azx_dev);
  49. azx_dev->start_wallclk = snd_hdac_chip_readl(bus, WALLCLK);
  50. if (!fresh_start)
  51. azx_dev->start_wallclk -= azx_dev->period_wallclk;
  52. /* enable SIE */
  53. snd_hdac_chip_updatel(bus, INTCTL, 0, 1 << azx_dev->index);
  54. /* set DMA start and interrupt mask */
  55. snd_hdac_stream_updateb(azx_dev, SD_CTL,
  56. 0, SD_CTL_DMA_START | SD_INT_MASK);
  57. azx_dev->running = true;
  58. }
  59. EXPORT_SYMBOL_GPL(snd_hdac_stream_start);
  60. /**
  61. * snd_hdac_stream_clear - stop a stream DMA
  62. * @azx_dev: HD-audio core stream to stop
  63. */
  64. void snd_hdac_stream_clear(struct hdac_stream *azx_dev)
  65. {
  66. snd_hdac_stream_updateb(azx_dev, SD_CTL,
  67. SD_CTL_DMA_START | SD_INT_MASK, 0);
  68. snd_hdac_stream_writeb(azx_dev, SD_STS, SD_INT_MASK); /* to be sure */
  69. azx_dev->running = false;
  70. }
  71. EXPORT_SYMBOL_GPL(snd_hdac_stream_clear);
  72. /**
  73. * snd_hdac_stream_stop - stop a stream
  74. * @azx_dev: HD-audio core stream to stop
  75. *
  76. * Stop a stream DMA and disable stream interrupt
  77. */
  78. void snd_hdac_stream_stop(struct hdac_stream *azx_dev)
  79. {
  80. trace_snd_hdac_stream_stop(azx_dev->bus, azx_dev);
  81. snd_hdac_stream_clear(azx_dev);
  82. /* disable SIE */
  83. snd_hdac_chip_updatel(azx_dev->bus, INTCTL, 1 << azx_dev->index, 0);
  84. }
  85. EXPORT_SYMBOL_GPL(snd_hdac_stream_stop);
  86. /**
  87. * snd_hdac_stream_reset - reset a stream
  88. * @azx_dev: HD-audio core stream to reset
  89. */
  90. void snd_hdac_stream_reset(struct hdac_stream *azx_dev)
  91. {
  92. unsigned char val;
  93. int timeout;
  94. snd_hdac_stream_clear(azx_dev);
  95. snd_hdac_stream_updateb(azx_dev, SD_CTL, 0, SD_CTL_STREAM_RESET);
  96. udelay(3);
  97. timeout = 300;
  98. do {
  99. val = snd_hdac_stream_readb(azx_dev, SD_CTL) &
  100. SD_CTL_STREAM_RESET;
  101. if (val)
  102. break;
  103. } while (--timeout);
  104. val &= ~SD_CTL_STREAM_RESET;
  105. snd_hdac_stream_writeb(azx_dev, SD_CTL, val);
  106. udelay(3);
  107. timeout = 300;
  108. /* waiting for hardware to report that the stream is out of reset */
  109. do {
  110. val = snd_hdac_stream_readb(azx_dev, SD_CTL) &
  111. SD_CTL_STREAM_RESET;
  112. if (!val)
  113. break;
  114. } while (--timeout);
  115. /* reset first position - may not be synced with hw at this time */
  116. if (azx_dev->posbuf)
  117. *azx_dev->posbuf = 0;
  118. }
  119. EXPORT_SYMBOL_GPL(snd_hdac_stream_reset);
  120. /**
  121. * snd_hdac_stream_setup - set up the SD for streaming
  122. * @azx_dev: HD-audio core stream to set up
  123. */
  124. int snd_hdac_stream_setup(struct hdac_stream *azx_dev)
  125. {
  126. struct hdac_bus *bus = azx_dev->bus;
  127. struct snd_pcm_runtime *runtime;
  128. unsigned int val;
  129. if (azx_dev->substream)
  130. runtime = azx_dev->substream->runtime;
  131. else
  132. runtime = NULL;
  133. /* make sure the run bit is zero for SD */
  134. snd_hdac_stream_clear(azx_dev);
  135. /* program the stream_tag */
  136. val = snd_hdac_stream_readl(azx_dev, SD_CTL);
  137. val = (val & ~SD_CTL_STREAM_TAG_MASK) |
  138. (azx_dev->stream_tag << SD_CTL_STREAM_TAG_SHIFT);
  139. if (!bus->snoop)
  140. val |= SD_CTL_TRAFFIC_PRIO;
  141. snd_hdac_stream_writel(azx_dev, SD_CTL, val);
  142. /* program the length of samples in cyclic buffer */
  143. snd_hdac_stream_writel(azx_dev, SD_CBL, azx_dev->bufsize);
  144. /* program the stream format */
  145. /* this value needs to be the same as the one programmed */
  146. snd_hdac_stream_writew(azx_dev, SD_FORMAT, azx_dev->format_val);
  147. /* program the stream LVI (last valid index) of the BDL */
  148. snd_hdac_stream_writew(azx_dev, SD_LVI, azx_dev->frags - 1);
  149. /* program the BDL address */
  150. /* lower BDL address */
  151. snd_hdac_stream_writel(azx_dev, SD_BDLPL, (u32)azx_dev->bdl.addr);
  152. /* upper BDL address */
  153. snd_hdac_stream_writel(azx_dev, SD_BDLPU,
  154. upper_32_bits(azx_dev->bdl.addr));
  155. /* enable the position buffer */
  156. if (bus->use_posbuf && bus->posbuf.addr) {
  157. if (!(snd_hdac_chip_readl(bus, DPLBASE) & AZX_DPLBASE_ENABLE))
  158. snd_hdac_chip_writel(bus, DPLBASE,
  159. (u32)bus->posbuf.addr | AZX_DPLBASE_ENABLE);
  160. }
  161. /* set the interrupt enable bits in the descriptor control register */
  162. snd_hdac_stream_updatel(azx_dev, SD_CTL, 0, SD_INT_MASK);
  163. if (azx_dev->direction == SNDRV_PCM_STREAM_PLAYBACK)
  164. azx_dev->fifo_size =
  165. snd_hdac_stream_readw(azx_dev, SD_FIFOSIZE) + 1;
  166. else
  167. azx_dev->fifo_size = 0;
  168. /* when LPIB delay correction gives a small negative value,
  169. * we ignore it; currently set the threshold statically to
  170. * 64 frames
  171. */
  172. if (runtime && runtime->period_size > 64)
  173. azx_dev->delay_negative_threshold =
  174. -frames_to_bytes(runtime, 64);
  175. else
  176. azx_dev->delay_negative_threshold = 0;
  177. /* wallclk has 24Mhz clock source */
  178. if (runtime)
  179. azx_dev->period_wallclk = (((runtime->period_size * 24000) /
  180. runtime->rate) * 1000);
  181. return 0;
  182. }
  183. EXPORT_SYMBOL_GPL(snd_hdac_stream_setup);
  184. /**
  185. * snd_hdac_stream_cleanup - cleanup a stream
  186. * @azx_dev: HD-audio core stream to clean up
  187. */
  188. void snd_hdac_stream_cleanup(struct hdac_stream *azx_dev)
  189. {
  190. snd_hdac_stream_writel(azx_dev, SD_BDLPL, 0);
  191. snd_hdac_stream_writel(azx_dev, SD_BDLPU, 0);
  192. snd_hdac_stream_writel(azx_dev, SD_CTL, 0);
  193. azx_dev->bufsize = 0;
  194. azx_dev->period_bytes = 0;
  195. azx_dev->format_val = 0;
  196. }
  197. EXPORT_SYMBOL_GPL(snd_hdac_stream_cleanup);
  198. /**
  199. * snd_hdac_stream_assign - assign a stream for the PCM
  200. * @bus: HD-audio core bus
  201. * @substream: PCM substream to assign
  202. *
  203. * Look for an unused stream for the given PCM substream, assign it
  204. * and return the stream object. If no stream is free, returns NULL.
  205. * The function tries to keep using the same stream object when it's used
  206. * beforehand. Also, when bus->reverse_assign flag is set, the last free
  207. * or matching entry is returned. This is needed for some strange codecs.
  208. */
  209. struct hdac_stream *snd_hdac_stream_assign(struct hdac_bus *bus,
  210. struct snd_pcm_substream *substream)
  211. {
  212. struct hdac_stream *azx_dev;
  213. struct hdac_stream *res = NULL;
  214. /* make a non-zero unique key for the substream */
  215. int key = (substream->pcm->device << 16) | (substream->number << 2) |
  216. (substream->stream + 1);
  217. list_for_each_entry(azx_dev, &bus->stream_list, list) {
  218. if (azx_dev->direction != substream->stream)
  219. continue;
  220. if (azx_dev->opened)
  221. continue;
  222. if (azx_dev->assigned_key == key) {
  223. res = azx_dev;
  224. break;
  225. }
  226. if (!res || bus->reverse_assign)
  227. res = azx_dev;
  228. }
  229. if (res) {
  230. spin_lock_irq(&bus->reg_lock);
  231. res->opened = 1;
  232. res->running = 0;
  233. res->assigned_key = key;
  234. res->substream = substream;
  235. spin_unlock_irq(&bus->reg_lock);
  236. }
  237. return res;
  238. }
  239. EXPORT_SYMBOL_GPL(snd_hdac_stream_assign);
  240. /**
  241. * snd_hdac_stream_release - release the assigned stream
  242. * @azx_dev: HD-audio core stream to release
  243. *
  244. * Release the stream that has been assigned by snd_hdac_stream_assign().
  245. */
  246. void snd_hdac_stream_release(struct hdac_stream *azx_dev)
  247. {
  248. struct hdac_bus *bus = azx_dev->bus;
  249. spin_lock_irq(&bus->reg_lock);
  250. azx_dev->opened = 0;
  251. azx_dev->running = 0;
  252. azx_dev->substream = NULL;
  253. spin_unlock_irq(&bus->reg_lock);
  254. }
  255. EXPORT_SYMBOL_GPL(snd_hdac_stream_release);
  256. /*
  257. * set up a BDL entry
  258. */
  259. static int setup_bdle(struct hdac_bus *bus,
  260. struct snd_dma_buffer *dmab,
  261. struct hdac_stream *azx_dev, __le32 **bdlp,
  262. int ofs, int size, int with_ioc)
  263. {
  264. __le32 *bdl = *bdlp;
  265. while (size > 0) {
  266. dma_addr_t addr;
  267. int chunk;
  268. if (azx_dev->frags >= AZX_MAX_BDL_ENTRIES)
  269. return -EINVAL;
  270. addr = snd_sgbuf_get_addr(dmab, ofs);
  271. /* program the address field of the BDL entry */
  272. bdl[0] = cpu_to_le32((u32)addr);
  273. bdl[1] = cpu_to_le32(upper_32_bits(addr));
  274. /* program the size field of the BDL entry */
  275. chunk = snd_sgbuf_get_chunk_size(dmab, ofs, size);
  276. /* one BDLE cannot cross 4K boundary on CTHDA chips */
  277. if (bus->align_bdle_4k) {
  278. u32 remain = 0x1000 - (ofs & 0xfff);
  279. if (chunk > remain)
  280. chunk = remain;
  281. }
  282. bdl[2] = cpu_to_le32(chunk);
  283. /* program the IOC to enable interrupt
  284. * only when the whole fragment is processed
  285. */
  286. size -= chunk;
  287. bdl[3] = (size || !with_ioc) ? 0 : cpu_to_le32(0x01);
  288. bdl += 4;
  289. azx_dev->frags++;
  290. ofs += chunk;
  291. }
  292. *bdlp = bdl;
  293. return ofs;
  294. }
  295. /**
  296. * snd_hdac_stream_setup_periods - set up BDL entries
  297. * @azx_dev: HD-audio core stream to set up
  298. *
  299. * Set up the buffer descriptor table of the given stream based on the
  300. * period and buffer sizes of the assigned PCM substream.
  301. */
  302. int snd_hdac_stream_setup_periods(struct hdac_stream *azx_dev)
  303. {
  304. struct hdac_bus *bus = azx_dev->bus;
  305. struct snd_pcm_substream *substream = azx_dev->substream;
  306. struct snd_pcm_runtime *runtime = substream->runtime;
  307. __le32 *bdl;
  308. int i, ofs, periods, period_bytes;
  309. int pos_adj, pos_align;
  310. /* reset BDL address */
  311. snd_hdac_stream_writel(azx_dev, SD_BDLPL, 0);
  312. snd_hdac_stream_writel(azx_dev, SD_BDLPU, 0);
  313. period_bytes = azx_dev->period_bytes;
  314. periods = azx_dev->bufsize / period_bytes;
  315. /* program the initial BDL entries */
  316. bdl = (__le32 *)azx_dev->bdl.area;
  317. ofs = 0;
  318. azx_dev->frags = 0;
  319. pos_adj = bus->bdl_pos_adj;
  320. if (!azx_dev->no_period_wakeup && pos_adj > 0) {
  321. pos_align = pos_adj;
  322. pos_adj = (pos_adj * runtime->rate + 47999) / 48000;
  323. if (!pos_adj)
  324. pos_adj = pos_align;
  325. else
  326. pos_adj = ((pos_adj + pos_align - 1) / pos_align) *
  327. pos_align;
  328. pos_adj = frames_to_bytes(runtime, pos_adj);
  329. if (pos_adj >= period_bytes) {
  330. dev_warn(bus->dev, "Too big adjustment %d\n",
  331. pos_adj);
  332. pos_adj = 0;
  333. } else {
  334. ofs = setup_bdle(bus, snd_pcm_get_dma_buf(substream),
  335. azx_dev,
  336. &bdl, ofs, pos_adj, true);
  337. if (ofs < 0)
  338. goto error;
  339. }
  340. } else
  341. pos_adj = 0;
  342. for (i = 0; i < periods; i++) {
  343. if (i == periods - 1 && pos_adj)
  344. ofs = setup_bdle(bus, snd_pcm_get_dma_buf(substream),
  345. azx_dev, &bdl, ofs,
  346. period_bytes - pos_adj, 0);
  347. else
  348. ofs = setup_bdle(bus, snd_pcm_get_dma_buf(substream),
  349. azx_dev, &bdl, ofs,
  350. period_bytes,
  351. !azx_dev->no_period_wakeup);
  352. if (ofs < 0)
  353. goto error;
  354. }
  355. return 0;
  356. error:
  357. dev_err(bus->dev, "Too many BDL entries: buffer=%d, period=%d\n",
  358. azx_dev->bufsize, period_bytes);
  359. return -EINVAL;
  360. }
  361. EXPORT_SYMBOL_GPL(snd_hdac_stream_setup_periods);
  362. /* snd_hdac_stream_set_params - set stream parameters
  363. * @azx_dev: HD-audio core stream for which parameters are to be set
  364. * @format_val: format value parameter
  365. *
  366. * Setup the HD-audio core stream parameters from substream of the stream
  367. * and passed format value
  368. */
  369. int snd_hdac_stream_set_params(struct hdac_stream *azx_dev,
  370. unsigned int format_val)
  371. {
  372. unsigned int bufsize, period_bytes;
  373. struct snd_pcm_substream *substream = azx_dev->substream;
  374. struct snd_pcm_runtime *runtime;
  375. int err;
  376. if (!substream)
  377. return -EINVAL;
  378. runtime = substream->runtime;
  379. bufsize = snd_pcm_lib_buffer_bytes(substream);
  380. period_bytes = snd_pcm_lib_period_bytes(substream);
  381. if (bufsize != azx_dev->bufsize ||
  382. period_bytes != azx_dev->period_bytes ||
  383. format_val != azx_dev->format_val ||
  384. runtime->no_period_wakeup != azx_dev->no_period_wakeup) {
  385. azx_dev->bufsize = bufsize;
  386. azx_dev->period_bytes = period_bytes;
  387. azx_dev->format_val = format_val;
  388. azx_dev->no_period_wakeup = runtime->no_period_wakeup;
  389. err = snd_hdac_stream_setup_periods(azx_dev);
  390. if (err < 0)
  391. return err;
  392. }
  393. return 0;
  394. }
  395. EXPORT_SYMBOL_GPL(snd_hdac_stream_set_params);
  396. static cycle_t azx_cc_read(const struct cyclecounter *cc)
  397. {
  398. struct hdac_stream *azx_dev = container_of(cc, struct hdac_stream, cc);
  399. return snd_hdac_chip_readl(azx_dev->bus, WALLCLK);
  400. }
  401. static void azx_timecounter_init(struct hdac_stream *azx_dev,
  402. bool force, cycle_t last)
  403. {
  404. struct timecounter *tc = &azx_dev->tc;
  405. struct cyclecounter *cc = &azx_dev->cc;
  406. u64 nsec;
  407. cc->read = azx_cc_read;
  408. cc->mask = CLOCKSOURCE_MASK(32);
  409. /*
  410. * Converting from 24 MHz to ns means applying a 125/3 factor.
  411. * To avoid any saturation issues in intermediate operations,
  412. * the 125 factor is applied first. The division is applied
  413. * last after reading the timecounter value.
  414. * Applying the 1/3 factor as part of the multiplication
  415. * requires at least 20 bits for a decent precision, however
  416. * overflows occur after about 4 hours or less, not a option.
  417. */
  418. cc->mult = 125; /* saturation after 195 years */
  419. cc->shift = 0;
  420. nsec = 0; /* audio time is elapsed time since trigger */
  421. timecounter_init(tc, cc, nsec);
  422. if (force) {
  423. /*
  424. * force timecounter to use predefined value,
  425. * used for synchronized starts
  426. */
  427. tc->cycle_last = last;
  428. }
  429. }
  430. /**
  431. * snd_hdac_stream_timecounter_init - initialize time counter
  432. * @azx_dev: HD-audio core stream (master stream)
  433. * @streams: bit flags of streams to set up
  434. *
  435. * Initializes the time counter of streams marked by the bit flags (each
  436. * bit corresponds to the stream index).
  437. * The trigger timestamp of PCM substream assigned to the given stream is
  438. * updated accordingly, too.
  439. */
  440. void snd_hdac_stream_timecounter_init(struct hdac_stream *azx_dev,
  441. unsigned int streams)
  442. {
  443. struct hdac_bus *bus = azx_dev->bus;
  444. struct snd_pcm_runtime *runtime = azx_dev->substream->runtime;
  445. struct hdac_stream *s;
  446. bool inited = false;
  447. cycle_t cycle_last = 0;
  448. int i = 0;
  449. list_for_each_entry(s, &bus->stream_list, list) {
  450. if (streams & (1 << i)) {
  451. azx_timecounter_init(s, inited, cycle_last);
  452. if (!inited) {
  453. inited = true;
  454. cycle_last = s->tc.cycle_last;
  455. }
  456. }
  457. i++;
  458. }
  459. snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
  460. runtime->trigger_tstamp_latched = true;
  461. }
  462. EXPORT_SYMBOL_GPL(snd_hdac_stream_timecounter_init);
  463. /**
  464. * snd_hdac_stream_sync_trigger - turn on/off stream sync register
  465. * @azx_dev: HD-audio core stream (master stream)
  466. * @streams: bit flags of streams to sync
  467. */
  468. void snd_hdac_stream_sync_trigger(struct hdac_stream *azx_dev, bool set,
  469. unsigned int streams, unsigned int reg)
  470. {
  471. struct hdac_bus *bus = azx_dev->bus;
  472. unsigned int val;
  473. if (!reg)
  474. reg = AZX_REG_SSYNC;
  475. val = _snd_hdac_chip_read(l, bus, reg);
  476. if (set)
  477. val |= streams;
  478. else
  479. val &= ~streams;
  480. _snd_hdac_chip_write(l, bus, reg, val);
  481. }
  482. EXPORT_SYMBOL_GPL(snd_hdac_stream_sync_trigger);
  483. /**
  484. * snd_hdac_stream_sync - sync with start/strop trigger operation
  485. * @azx_dev: HD-audio core stream (master stream)
  486. * @start: true = start, false = stop
  487. * @streams: bit flags of streams to sync
  488. *
  489. * For @start = true, wait until all FIFOs get ready.
  490. * For @start = false, wait until all RUN bits are cleared.
  491. */
  492. void snd_hdac_stream_sync(struct hdac_stream *azx_dev, bool start,
  493. unsigned int streams)
  494. {
  495. struct hdac_bus *bus = azx_dev->bus;
  496. int i, nwait, timeout;
  497. struct hdac_stream *s;
  498. for (timeout = 5000; timeout; timeout--) {
  499. nwait = 0;
  500. i = 0;
  501. list_for_each_entry(s, &bus->stream_list, list) {
  502. if (streams & (1 << i)) {
  503. if (start) {
  504. /* check FIFO gets ready */
  505. if (!(snd_hdac_stream_readb(s, SD_STS) &
  506. SD_STS_FIFO_READY))
  507. nwait++;
  508. } else {
  509. /* check RUN bit is cleared */
  510. if (snd_hdac_stream_readb(s, SD_CTL) &
  511. SD_CTL_DMA_START)
  512. nwait++;
  513. }
  514. }
  515. i++;
  516. }
  517. if (!nwait)
  518. break;
  519. cpu_relax();
  520. }
  521. }
  522. EXPORT_SYMBOL_GPL(snd_hdac_stream_sync);
  523. #ifdef CONFIG_SND_HDA_DSP_LOADER
  524. /**
  525. * snd_hdac_dsp_prepare - prepare for DSP loading
  526. * @azx_dev: HD-audio core stream used for DSP loading
  527. * @format: HD-audio stream format
  528. * @byte_size: data chunk byte size
  529. * @bufp: allocated buffer
  530. *
  531. * Allocate the buffer for the given size and set up the given stream for
  532. * DSP loading. Returns the stream tag (>= 0), or a negative error code.
  533. */
  534. int snd_hdac_dsp_prepare(struct hdac_stream *azx_dev, unsigned int format,
  535. unsigned int byte_size, struct snd_dma_buffer *bufp)
  536. {
  537. struct hdac_bus *bus = azx_dev->bus;
  538. u32 *bdl;
  539. int err;
  540. snd_hdac_dsp_lock(azx_dev);
  541. spin_lock_irq(&bus->reg_lock);
  542. if (azx_dev->running || azx_dev->locked) {
  543. spin_unlock_irq(&bus->reg_lock);
  544. err = -EBUSY;
  545. goto unlock;
  546. }
  547. azx_dev->locked = true;
  548. spin_unlock_irq(&bus->reg_lock);
  549. err = bus->io_ops->dma_alloc_pages(bus, SNDRV_DMA_TYPE_DEV_SG,
  550. byte_size, bufp);
  551. if (err < 0)
  552. goto err_alloc;
  553. azx_dev->substream = NULL;
  554. azx_dev->bufsize = byte_size;
  555. azx_dev->period_bytes = byte_size;
  556. azx_dev->format_val = format;
  557. snd_hdac_stream_reset(azx_dev);
  558. /* reset BDL address */
  559. snd_hdac_stream_writel(azx_dev, SD_BDLPL, 0);
  560. snd_hdac_stream_writel(azx_dev, SD_BDLPU, 0);
  561. azx_dev->frags = 0;
  562. bdl = (u32 *)azx_dev->bdl.area;
  563. err = setup_bdle(bus, bufp, azx_dev, &bdl, 0, byte_size, 0);
  564. if (err < 0)
  565. goto error;
  566. snd_hdac_stream_setup(azx_dev);
  567. snd_hdac_dsp_unlock(azx_dev);
  568. return azx_dev->stream_tag;
  569. error:
  570. bus->io_ops->dma_free_pages(bus, bufp);
  571. err_alloc:
  572. spin_lock_irq(&bus->reg_lock);
  573. azx_dev->locked = false;
  574. spin_unlock_irq(&bus->reg_lock);
  575. unlock:
  576. snd_hdac_dsp_unlock(azx_dev);
  577. return err;
  578. }
  579. EXPORT_SYMBOL_GPL(snd_hdac_dsp_prepare);
  580. /**
  581. * snd_hdac_dsp_trigger - start / stop DSP loading
  582. * @azx_dev: HD-audio core stream used for DSP loading
  583. * @start: trigger start or stop
  584. */
  585. void snd_hdac_dsp_trigger(struct hdac_stream *azx_dev, bool start)
  586. {
  587. if (start)
  588. snd_hdac_stream_start(azx_dev, true);
  589. else
  590. snd_hdac_stream_stop(azx_dev);
  591. }
  592. EXPORT_SYMBOL_GPL(snd_hdac_dsp_trigger);
  593. /**
  594. * snd_hdac_dsp_cleanup - clean up the stream from DSP loading to normal
  595. * @azx_dev: HD-audio core stream used for DSP loading
  596. * @dmab: buffer used by DSP loading
  597. */
  598. void snd_hdac_dsp_cleanup(struct hdac_stream *azx_dev,
  599. struct snd_dma_buffer *dmab)
  600. {
  601. struct hdac_bus *bus = azx_dev->bus;
  602. if (!dmab->area || !azx_dev->locked)
  603. return;
  604. snd_hdac_dsp_lock(azx_dev);
  605. /* reset BDL address */
  606. snd_hdac_stream_writel(azx_dev, SD_BDLPL, 0);
  607. snd_hdac_stream_writel(azx_dev, SD_BDLPU, 0);
  608. snd_hdac_stream_writel(azx_dev, SD_CTL, 0);
  609. azx_dev->bufsize = 0;
  610. azx_dev->period_bytes = 0;
  611. azx_dev->format_val = 0;
  612. bus->io_ops->dma_free_pages(bus, dmab);
  613. dmab->area = NULL;
  614. spin_lock_irq(&bus->reg_lock);
  615. azx_dev->locked = false;
  616. spin_unlock_irq(&bus->reg_lock);
  617. snd_hdac_dsp_unlock(azx_dev);
  618. }
  619. EXPORT_SYMBOL_GPL(snd_hdac_dsp_cleanup);
  620. #endif /* CONFIG_SND_HDA_DSP_LOADER */