hdac_stream.c 22 KB

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