hdac_stream.c 19 KB

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