hdac_stream.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  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. * snd_hdac_get_stream - return hdac_stream based on stream_tag and
  258. * direction
  259. *
  260. * @bus: HD-audio core bus
  261. * @dir: direction for the stream to be found
  262. * @stream_tag: stream tag for stream to be found
  263. */
  264. struct hdac_stream *snd_hdac_get_stream(struct hdac_bus *bus,
  265. int dir, int stream_tag)
  266. {
  267. struct hdac_stream *s;
  268. list_for_each_entry(s, &bus->stream_list, list) {
  269. if (s->direction == dir && s->stream_tag == stream_tag)
  270. return s;
  271. }
  272. return NULL;
  273. }
  274. EXPORT_SYMBOL_GPL(snd_hdac_get_stream);
  275. /*
  276. * set up a BDL entry
  277. */
  278. static int setup_bdle(struct hdac_bus *bus,
  279. struct snd_dma_buffer *dmab,
  280. struct hdac_stream *azx_dev, __le32 **bdlp,
  281. int ofs, int size, int with_ioc)
  282. {
  283. __le32 *bdl = *bdlp;
  284. while (size > 0) {
  285. dma_addr_t addr;
  286. int chunk;
  287. if (azx_dev->frags >= AZX_MAX_BDL_ENTRIES)
  288. return -EINVAL;
  289. addr = snd_sgbuf_get_addr(dmab, ofs);
  290. /* program the address field of the BDL entry */
  291. bdl[0] = cpu_to_le32((u32)addr);
  292. bdl[1] = cpu_to_le32(upper_32_bits(addr));
  293. /* program the size field of the BDL entry */
  294. chunk = snd_sgbuf_get_chunk_size(dmab, ofs, size);
  295. /* one BDLE cannot cross 4K boundary on CTHDA chips */
  296. if (bus->align_bdle_4k) {
  297. u32 remain = 0x1000 - (ofs & 0xfff);
  298. if (chunk > remain)
  299. chunk = remain;
  300. }
  301. bdl[2] = cpu_to_le32(chunk);
  302. /* program the IOC to enable interrupt
  303. * only when the whole fragment is processed
  304. */
  305. size -= chunk;
  306. bdl[3] = (size || !with_ioc) ? 0 : cpu_to_le32(0x01);
  307. bdl += 4;
  308. azx_dev->frags++;
  309. ofs += chunk;
  310. }
  311. *bdlp = bdl;
  312. return ofs;
  313. }
  314. /**
  315. * snd_hdac_stream_setup_periods - set up BDL entries
  316. * @azx_dev: HD-audio core stream to set up
  317. *
  318. * Set up the buffer descriptor table of the given stream based on the
  319. * period and buffer sizes of the assigned PCM substream.
  320. */
  321. int snd_hdac_stream_setup_periods(struct hdac_stream *azx_dev)
  322. {
  323. struct hdac_bus *bus = azx_dev->bus;
  324. struct snd_pcm_substream *substream = azx_dev->substream;
  325. struct snd_pcm_runtime *runtime = substream->runtime;
  326. __le32 *bdl;
  327. int i, ofs, periods, period_bytes;
  328. int pos_adj, pos_align;
  329. /* reset BDL address */
  330. snd_hdac_stream_writel(azx_dev, SD_BDLPL, 0);
  331. snd_hdac_stream_writel(azx_dev, SD_BDLPU, 0);
  332. period_bytes = azx_dev->period_bytes;
  333. periods = azx_dev->bufsize / period_bytes;
  334. /* program the initial BDL entries */
  335. bdl = (__le32 *)azx_dev->bdl.area;
  336. ofs = 0;
  337. azx_dev->frags = 0;
  338. pos_adj = bus->bdl_pos_adj;
  339. if (!azx_dev->no_period_wakeup && pos_adj > 0) {
  340. pos_align = pos_adj;
  341. pos_adj = (pos_adj * runtime->rate + 47999) / 48000;
  342. if (!pos_adj)
  343. pos_adj = pos_align;
  344. else
  345. pos_adj = ((pos_adj + pos_align - 1) / pos_align) *
  346. pos_align;
  347. pos_adj = frames_to_bytes(runtime, pos_adj);
  348. if (pos_adj >= period_bytes) {
  349. dev_warn(bus->dev, "Too big adjustment %d\n",
  350. pos_adj);
  351. pos_adj = 0;
  352. } else {
  353. ofs = setup_bdle(bus, snd_pcm_get_dma_buf(substream),
  354. azx_dev,
  355. &bdl, ofs, pos_adj, true);
  356. if (ofs < 0)
  357. goto error;
  358. }
  359. } else
  360. pos_adj = 0;
  361. for (i = 0; i < periods; i++) {
  362. if (i == periods - 1 && pos_adj)
  363. ofs = setup_bdle(bus, snd_pcm_get_dma_buf(substream),
  364. azx_dev, &bdl, ofs,
  365. period_bytes - pos_adj, 0);
  366. else
  367. ofs = setup_bdle(bus, snd_pcm_get_dma_buf(substream),
  368. azx_dev, &bdl, ofs,
  369. period_bytes,
  370. !azx_dev->no_period_wakeup);
  371. if (ofs < 0)
  372. goto error;
  373. }
  374. return 0;
  375. error:
  376. dev_err(bus->dev, "Too many BDL entries: buffer=%d, period=%d\n",
  377. azx_dev->bufsize, period_bytes);
  378. return -EINVAL;
  379. }
  380. EXPORT_SYMBOL_GPL(snd_hdac_stream_setup_periods);
  381. /**
  382. * snd_hdac_stream_set_params - set stream parameters
  383. * @azx_dev: HD-audio core stream for which parameters are to be set
  384. * @format_val: format value parameter
  385. *
  386. * Setup the HD-audio core stream parameters from substream of the stream
  387. * and passed format value
  388. */
  389. int snd_hdac_stream_set_params(struct hdac_stream *azx_dev,
  390. unsigned int format_val)
  391. {
  392. unsigned int bufsize, period_bytes;
  393. struct snd_pcm_substream *substream = azx_dev->substream;
  394. struct snd_pcm_runtime *runtime;
  395. int err;
  396. if (!substream)
  397. return -EINVAL;
  398. runtime = substream->runtime;
  399. bufsize = snd_pcm_lib_buffer_bytes(substream);
  400. period_bytes = snd_pcm_lib_period_bytes(substream);
  401. if (bufsize != azx_dev->bufsize ||
  402. period_bytes != azx_dev->period_bytes ||
  403. format_val != azx_dev->format_val ||
  404. runtime->no_period_wakeup != azx_dev->no_period_wakeup) {
  405. azx_dev->bufsize = bufsize;
  406. azx_dev->period_bytes = period_bytes;
  407. azx_dev->format_val = format_val;
  408. azx_dev->no_period_wakeup = runtime->no_period_wakeup;
  409. err = snd_hdac_stream_setup_periods(azx_dev);
  410. if (err < 0)
  411. return err;
  412. }
  413. return 0;
  414. }
  415. EXPORT_SYMBOL_GPL(snd_hdac_stream_set_params);
  416. static cycle_t azx_cc_read(const struct cyclecounter *cc)
  417. {
  418. struct hdac_stream *azx_dev = container_of(cc, struct hdac_stream, cc);
  419. return snd_hdac_chip_readl(azx_dev->bus, WALLCLK);
  420. }
  421. static void azx_timecounter_init(struct hdac_stream *azx_dev,
  422. bool force, cycle_t last)
  423. {
  424. struct timecounter *tc = &azx_dev->tc;
  425. struct cyclecounter *cc = &azx_dev->cc;
  426. u64 nsec;
  427. cc->read = azx_cc_read;
  428. cc->mask = CLOCKSOURCE_MASK(32);
  429. /*
  430. * Converting from 24 MHz to ns means applying a 125/3 factor.
  431. * To avoid any saturation issues in intermediate operations,
  432. * the 125 factor is applied first. The division is applied
  433. * last after reading the timecounter value.
  434. * Applying the 1/3 factor as part of the multiplication
  435. * requires at least 20 bits for a decent precision, however
  436. * overflows occur after about 4 hours or less, not a option.
  437. */
  438. cc->mult = 125; /* saturation after 195 years */
  439. cc->shift = 0;
  440. nsec = 0; /* audio time is elapsed time since trigger */
  441. timecounter_init(tc, cc, nsec);
  442. if (force) {
  443. /*
  444. * force timecounter to use predefined value,
  445. * used for synchronized starts
  446. */
  447. tc->cycle_last = last;
  448. }
  449. }
  450. /**
  451. * snd_hdac_stream_timecounter_init - initialize time counter
  452. * @azx_dev: HD-audio core stream (master stream)
  453. * @streams: bit flags of streams to set up
  454. *
  455. * Initializes the time counter of streams marked by the bit flags (each
  456. * bit corresponds to the stream index).
  457. * The trigger timestamp of PCM substream assigned to the given stream is
  458. * updated accordingly, too.
  459. */
  460. void snd_hdac_stream_timecounter_init(struct hdac_stream *azx_dev,
  461. unsigned int streams)
  462. {
  463. struct hdac_bus *bus = azx_dev->bus;
  464. struct snd_pcm_runtime *runtime = azx_dev->substream->runtime;
  465. struct hdac_stream *s;
  466. bool inited = false;
  467. cycle_t cycle_last = 0;
  468. int i = 0;
  469. list_for_each_entry(s, &bus->stream_list, list) {
  470. if (streams & (1 << i)) {
  471. azx_timecounter_init(s, inited, cycle_last);
  472. if (!inited) {
  473. inited = true;
  474. cycle_last = s->tc.cycle_last;
  475. }
  476. }
  477. i++;
  478. }
  479. snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
  480. runtime->trigger_tstamp_latched = true;
  481. }
  482. EXPORT_SYMBOL_GPL(snd_hdac_stream_timecounter_init);
  483. /**
  484. * snd_hdac_stream_sync_trigger - turn on/off stream sync register
  485. * @azx_dev: HD-audio core stream (master stream)
  486. * @streams: bit flags of streams to sync
  487. */
  488. void snd_hdac_stream_sync_trigger(struct hdac_stream *azx_dev, bool set,
  489. unsigned int streams, unsigned int reg)
  490. {
  491. struct hdac_bus *bus = azx_dev->bus;
  492. unsigned int val;
  493. if (!reg)
  494. reg = AZX_REG_SSYNC;
  495. val = _snd_hdac_chip_read(l, bus, reg);
  496. if (set)
  497. val |= streams;
  498. else
  499. val &= ~streams;
  500. _snd_hdac_chip_write(l, bus, reg, val);
  501. }
  502. EXPORT_SYMBOL_GPL(snd_hdac_stream_sync_trigger);
  503. /**
  504. * snd_hdac_stream_sync - sync with start/strop trigger operation
  505. * @azx_dev: HD-audio core stream (master stream)
  506. * @start: true = start, false = stop
  507. * @streams: bit flags of streams to sync
  508. *
  509. * For @start = true, wait until all FIFOs get ready.
  510. * For @start = false, wait until all RUN bits are cleared.
  511. */
  512. void snd_hdac_stream_sync(struct hdac_stream *azx_dev, bool start,
  513. unsigned int streams)
  514. {
  515. struct hdac_bus *bus = azx_dev->bus;
  516. int i, nwait, timeout;
  517. struct hdac_stream *s;
  518. for (timeout = 5000; timeout; timeout--) {
  519. nwait = 0;
  520. i = 0;
  521. list_for_each_entry(s, &bus->stream_list, list) {
  522. if (streams & (1 << i)) {
  523. if (start) {
  524. /* check FIFO gets ready */
  525. if (!(snd_hdac_stream_readb(s, SD_STS) &
  526. SD_STS_FIFO_READY))
  527. nwait++;
  528. } else {
  529. /* check RUN bit is cleared */
  530. if (snd_hdac_stream_readb(s, SD_CTL) &
  531. SD_CTL_DMA_START)
  532. nwait++;
  533. }
  534. }
  535. i++;
  536. }
  537. if (!nwait)
  538. break;
  539. cpu_relax();
  540. }
  541. }
  542. EXPORT_SYMBOL_GPL(snd_hdac_stream_sync);
  543. #ifdef CONFIG_SND_HDA_DSP_LOADER
  544. /**
  545. * snd_hdac_dsp_prepare - prepare for DSP loading
  546. * @azx_dev: HD-audio core stream used for DSP loading
  547. * @format: HD-audio stream format
  548. * @byte_size: data chunk byte size
  549. * @bufp: allocated buffer
  550. *
  551. * Allocate the buffer for the given size and set up the given stream for
  552. * DSP loading. Returns the stream tag (>= 0), or a negative error code.
  553. */
  554. int snd_hdac_dsp_prepare(struct hdac_stream *azx_dev, unsigned int format,
  555. unsigned int byte_size, struct snd_dma_buffer *bufp)
  556. {
  557. struct hdac_bus *bus = azx_dev->bus;
  558. u32 *bdl;
  559. int err;
  560. snd_hdac_dsp_lock(azx_dev);
  561. spin_lock_irq(&bus->reg_lock);
  562. if (azx_dev->running || azx_dev->locked) {
  563. spin_unlock_irq(&bus->reg_lock);
  564. err = -EBUSY;
  565. goto unlock;
  566. }
  567. azx_dev->locked = true;
  568. spin_unlock_irq(&bus->reg_lock);
  569. err = bus->io_ops->dma_alloc_pages(bus, SNDRV_DMA_TYPE_DEV_SG,
  570. byte_size, bufp);
  571. if (err < 0)
  572. goto err_alloc;
  573. azx_dev->substream = NULL;
  574. azx_dev->bufsize = byte_size;
  575. azx_dev->period_bytes = byte_size;
  576. azx_dev->format_val = format;
  577. snd_hdac_stream_reset(azx_dev);
  578. /* reset BDL address */
  579. snd_hdac_stream_writel(azx_dev, SD_BDLPL, 0);
  580. snd_hdac_stream_writel(azx_dev, SD_BDLPU, 0);
  581. azx_dev->frags = 0;
  582. bdl = (u32 *)azx_dev->bdl.area;
  583. err = setup_bdle(bus, bufp, azx_dev, &bdl, 0, byte_size, 0);
  584. if (err < 0)
  585. goto error;
  586. snd_hdac_stream_setup(azx_dev);
  587. snd_hdac_dsp_unlock(azx_dev);
  588. return azx_dev->stream_tag;
  589. error:
  590. bus->io_ops->dma_free_pages(bus, bufp);
  591. err_alloc:
  592. spin_lock_irq(&bus->reg_lock);
  593. azx_dev->locked = false;
  594. spin_unlock_irq(&bus->reg_lock);
  595. unlock:
  596. snd_hdac_dsp_unlock(azx_dev);
  597. return err;
  598. }
  599. EXPORT_SYMBOL_GPL(snd_hdac_dsp_prepare);
  600. /**
  601. * snd_hdac_dsp_trigger - start / stop DSP loading
  602. * @azx_dev: HD-audio core stream used for DSP loading
  603. * @start: trigger start or stop
  604. */
  605. void snd_hdac_dsp_trigger(struct hdac_stream *azx_dev, bool start)
  606. {
  607. if (start)
  608. snd_hdac_stream_start(azx_dev, true);
  609. else
  610. snd_hdac_stream_stop(azx_dev);
  611. }
  612. EXPORT_SYMBOL_GPL(snd_hdac_dsp_trigger);
  613. /**
  614. * snd_hdac_dsp_cleanup - clean up the stream from DSP loading to normal
  615. * @azx_dev: HD-audio core stream used for DSP loading
  616. * @dmab: buffer used by DSP loading
  617. */
  618. void snd_hdac_dsp_cleanup(struct hdac_stream *azx_dev,
  619. struct snd_dma_buffer *dmab)
  620. {
  621. struct hdac_bus *bus = azx_dev->bus;
  622. if (!dmab->area || !azx_dev->locked)
  623. return;
  624. snd_hdac_dsp_lock(azx_dev);
  625. /* reset BDL address */
  626. snd_hdac_stream_writel(azx_dev, SD_BDLPL, 0);
  627. snd_hdac_stream_writel(azx_dev, SD_BDLPU, 0);
  628. snd_hdac_stream_writel(azx_dev, SD_CTL, 0);
  629. azx_dev->bufsize = 0;
  630. azx_dev->period_bytes = 0;
  631. azx_dev->format_val = 0;
  632. bus->io_ops->dma_free_pages(bus, dmab);
  633. dmab->area = NULL;
  634. spin_lock_irq(&bus->reg_lock);
  635. azx_dev->locked = false;
  636. spin_unlock_irq(&bus->reg_lock);
  637. snd_hdac_dsp_unlock(azx_dev);
  638. }
  639. EXPORT_SYMBOL_GPL(snd_hdac_dsp_cleanup);
  640. #endif /* CONFIG_SND_HDA_DSP_LOADER */