hdac_ext_stream.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * hdac-ext-stream.c - HD-audio extended stream operations.
  4. *
  5. * Copyright (C) 2015 Intel Corp
  6. * Author: Jeeja KP <jeeja.kp@intel.com>
  7. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  8. *
  9. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  10. */
  11. #include <linux/delay.h>
  12. #include <linux/pci.h>
  13. #include <linux/pci_ids.h>
  14. #include <linux/slab.h>
  15. #include <sound/pcm.h>
  16. #include <sound/hda_register.h>
  17. #include <sound/hdaudio_ext.h>
  18. #include <sound/compress_driver.h>
  19. /**
  20. * snd_hdac_ext_host_stream_setup - Setup a HOST stream.
  21. * @hext_stream: HDAudio stream to set up.
  22. * @code_loading: Whether the stream is for PCM or code-loading.
  23. *
  24. * Return: Zero on success or negative error code.
  25. */
  26. int snd_hdac_ext_host_stream_setup(struct hdac_ext_stream *hext_stream, bool code_loading)
  27. {
  28. return hext_stream->host_setup(hdac_stream(hext_stream), code_loading);
  29. }
  30. EXPORT_SYMBOL_GPL(snd_hdac_ext_host_stream_setup);
  31. /**
  32. * snd_hdac_apl_host_stream_setup - Setup a HOST stream following procedure
  33. * recommended for ApolloLake devices.
  34. * @hstream: HDAudio stream to set up.
  35. * @code_loading: Whether the stream is for PCM or code-loading.
  36. *
  37. * Return: Zero on success or negative error code.
  38. */
  39. static int snd_hdac_apl_host_stream_setup(struct hdac_stream *hstream, bool code_loading)
  40. {
  41. struct hdac_ext_stream *hext_stream = stream_to_hdac_ext_stream(hstream);
  42. int ret;
  43. snd_hdac_ext_stream_decouple(hstream->bus, hext_stream, false);
  44. ret = snd_hdac_stream_setup(hstream, code_loading);
  45. snd_hdac_ext_stream_decouple(hstream->bus, hext_stream, true);
  46. return ret;
  47. }
  48. /**
  49. * snd_hdac_ext_stream_init - initialize each stream (aka device)
  50. * @bus: HD-audio core bus
  51. * @hext_stream: HD-audio ext core stream object to initialize
  52. * @idx: stream index number
  53. * @direction: stream direction (SNDRV_PCM_STREAM_PLAYBACK or SNDRV_PCM_STREAM_CAPTURE)
  54. * @tag: the tag id to assign
  55. *
  56. * initialize the stream, if ppcap is enabled then init those and then
  57. * invoke hdac stream initialization routine
  58. */
  59. static void snd_hdac_ext_stream_init(struct hdac_bus *bus,
  60. struct hdac_ext_stream *hext_stream,
  61. int idx, int direction, int tag)
  62. {
  63. if (bus->ppcap) {
  64. hext_stream->pphc_addr = bus->ppcap + AZX_PPHC_BASE +
  65. AZX_PPHC_INTERVAL * idx;
  66. hext_stream->pplc_addr = bus->ppcap + AZX_PPLC_BASE +
  67. AZX_PPLC_MULTI * bus->num_streams +
  68. AZX_PPLC_INTERVAL * idx;
  69. }
  70. hext_stream->decoupled = false;
  71. snd_hdac_stream_init(bus, &hext_stream->hstream, idx, direction, tag);
  72. }
  73. /**
  74. * snd_hdac_ext_stream_init_all - create and initialize the stream objects
  75. * for an extended hda bus
  76. * @bus: HD-audio core bus
  77. * @start_idx: start index for streams
  78. * @num_stream: number of streams to initialize
  79. * @dir: direction of streams
  80. */
  81. int snd_hdac_ext_stream_init_all(struct hdac_bus *bus, int start_idx,
  82. int num_stream, int dir)
  83. {
  84. struct pci_dev *pci = to_pci_dev(bus->dev);
  85. int (*setup_op)(struct hdac_stream *, bool);
  86. int stream_tag = 0;
  87. int i, tag, idx = start_idx;
  88. if (pci->device == PCI_DEVICE_ID_INTEL_HDA_APL)
  89. setup_op = snd_hdac_apl_host_stream_setup;
  90. else
  91. setup_op = snd_hdac_stream_setup;
  92. for (i = 0; i < num_stream; i++) {
  93. struct hdac_ext_stream *hext_stream =
  94. kzalloc(sizeof(*hext_stream), GFP_KERNEL);
  95. if (!hext_stream)
  96. return -ENOMEM;
  97. tag = ++stream_tag;
  98. snd_hdac_ext_stream_init(bus, hext_stream, idx, dir, tag);
  99. idx++;
  100. hext_stream->host_setup = setup_op;
  101. }
  102. return 0;
  103. }
  104. EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_init_all);
  105. /**
  106. * snd_hdac_ext_stream_free_all - free hdac extended stream objects
  107. *
  108. * @bus: HD-audio core bus
  109. */
  110. void snd_hdac_ext_stream_free_all(struct hdac_bus *bus)
  111. {
  112. struct hdac_stream *s, *_s;
  113. struct hdac_ext_stream *hext_stream;
  114. list_for_each_entry_safe(s, _s, &bus->stream_list, list) {
  115. hext_stream = stream_to_hdac_ext_stream(s);
  116. snd_hdac_ext_stream_decouple(bus, hext_stream, false);
  117. list_del(&s->list);
  118. kfree(hext_stream);
  119. }
  120. }
  121. EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_free_all);
  122. void snd_hdac_ext_stream_decouple_locked(struct hdac_bus *bus,
  123. struct hdac_ext_stream *hext_stream,
  124. bool decouple)
  125. {
  126. struct hdac_stream *hstream = &hext_stream->hstream;
  127. u32 val;
  128. int mask = AZX_PPCTL_PROCEN(hstream->index);
  129. val = readw(bus->ppcap + AZX_REG_PP_PPCTL) & mask;
  130. if (decouple && !val)
  131. snd_hdac_updatel(bus->ppcap, AZX_REG_PP_PPCTL, mask, mask);
  132. else if (!decouple && val)
  133. snd_hdac_updatel(bus->ppcap, AZX_REG_PP_PPCTL, mask, 0);
  134. hext_stream->decoupled = decouple;
  135. }
  136. EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_decouple_locked);
  137. /**
  138. * snd_hdac_ext_stream_decouple - decouple the hdac stream
  139. * @bus: HD-audio core bus
  140. * @hext_stream: HD-audio ext core stream object to initialize
  141. * @decouple: flag to decouple
  142. */
  143. void snd_hdac_ext_stream_decouple(struct hdac_bus *bus,
  144. struct hdac_ext_stream *hext_stream, bool decouple)
  145. {
  146. spin_lock_irq(&bus->reg_lock);
  147. snd_hdac_ext_stream_decouple_locked(bus, hext_stream, decouple);
  148. spin_unlock_irq(&bus->reg_lock);
  149. }
  150. EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_decouple);
  151. /**
  152. * snd_hdac_ext_stream_start - start a stream
  153. * @hext_stream: HD-audio ext core stream to start
  154. */
  155. void snd_hdac_ext_stream_start(struct hdac_ext_stream *hext_stream)
  156. {
  157. snd_hdac_updatel(hext_stream->pplc_addr, AZX_REG_PPLCCTL,
  158. AZX_PPLCCTL_RUN, AZX_PPLCCTL_RUN);
  159. }
  160. EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_start);
  161. /**
  162. * snd_hdac_ext_stream_clear - stop a stream DMA
  163. * @hext_stream: HD-audio ext core stream to stop
  164. */
  165. void snd_hdac_ext_stream_clear(struct hdac_ext_stream *hext_stream)
  166. {
  167. snd_hdac_updatel(hext_stream->pplc_addr, AZX_REG_PPLCCTL, AZX_PPLCCTL_RUN, 0);
  168. }
  169. EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_clear);
  170. /**
  171. * snd_hdac_ext_stream_reset - reset a stream
  172. * @hext_stream: HD-audio ext core stream to reset
  173. */
  174. void snd_hdac_ext_stream_reset(struct hdac_ext_stream *hext_stream)
  175. {
  176. unsigned char val;
  177. int timeout;
  178. snd_hdac_ext_stream_clear(hext_stream);
  179. snd_hdac_updatel(hext_stream->pplc_addr, AZX_REG_PPLCCTL,
  180. AZX_PPLCCTL_STRST, AZX_PPLCCTL_STRST);
  181. udelay(3);
  182. timeout = 50;
  183. do {
  184. val = readl(hext_stream->pplc_addr + AZX_REG_PPLCCTL) &
  185. AZX_PPLCCTL_STRST;
  186. if (val)
  187. break;
  188. udelay(3);
  189. } while (--timeout);
  190. val &= ~AZX_PPLCCTL_STRST;
  191. writel(val, hext_stream->pplc_addr + AZX_REG_PPLCCTL);
  192. udelay(3);
  193. timeout = 50;
  194. /* waiting for hardware to report that the stream is out of reset */
  195. do {
  196. val = readl(hext_stream->pplc_addr + AZX_REG_PPLCCTL) & AZX_PPLCCTL_STRST;
  197. if (!val)
  198. break;
  199. udelay(3);
  200. } while (--timeout);
  201. }
  202. EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_reset);
  203. /**
  204. * snd_hdac_ext_stream_setup - set up the SD for streaming
  205. * @hext_stream: HD-audio ext core stream to set up
  206. * @fmt: stream format
  207. */
  208. int snd_hdac_ext_stream_setup(struct hdac_ext_stream *hext_stream, int fmt)
  209. {
  210. struct hdac_stream *hstream = &hext_stream->hstream;
  211. unsigned int val;
  212. /* make sure the run bit is zero for SD */
  213. snd_hdac_ext_stream_clear(hext_stream);
  214. /* program the stream_tag */
  215. val = readl(hext_stream->pplc_addr + AZX_REG_PPLCCTL);
  216. val = (val & ~AZX_PPLCCTL_STRM_MASK) |
  217. (hstream->stream_tag << AZX_PPLCCTL_STRM_SHIFT);
  218. writel(val, hext_stream->pplc_addr + AZX_REG_PPLCCTL);
  219. /* program the stream format */
  220. writew(fmt, hext_stream->pplc_addr + AZX_REG_PPLCFMT);
  221. return 0;
  222. }
  223. EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_setup);
  224. static struct hdac_ext_stream *
  225. hdac_ext_link_dma_stream_assign(struct hdac_bus *bus,
  226. struct snd_pcm_substream *substream)
  227. {
  228. struct hdac_ext_stream *res = NULL;
  229. struct hdac_stream *hstream = NULL;
  230. if (!bus->ppcap) {
  231. dev_err(bus->dev, "stream type not supported\n");
  232. return NULL;
  233. }
  234. spin_lock_irq(&bus->reg_lock);
  235. list_for_each_entry(hstream, &bus->stream_list, list) {
  236. struct hdac_ext_stream *hext_stream = container_of(hstream,
  237. struct hdac_ext_stream,
  238. hstream);
  239. if (hstream->direction != substream->stream)
  240. continue;
  241. /* check if link stream is available */
  242. if (!hext_stream->link_locked) {
  243. res = hext_stream;
  244. break;
  245. }
  246. }
  247. if (res) {
  248. snd_hdac_ext_stream_decouple_locked(bus, res, true);
  249. res->link_locked = 1;
  250. res->link_substream = substream;
  251. }
  252. spin_unlock_irq(&bus->reg_lock);
  253. return res;
  254. }
  255. static struct hdac_ext_stream *
  256. hdac_ext_host_dma_stream_assign(struct hdac_bus *bus,
  257. struct snd_pcm_substream *substream)
  258. {
  259. struct hdac_ext_stream *res = NULL;
  260. struct hdac_stream *hstream = NULL;
  261. if (!bus->ppcap) {
  262. dev_err(bus->dev, "stream type not supported\n");
  263. return NULL;
  264. }
  265. spin_lock_irq(&bus->reg_lock);
  266. list_for_each_entry(hstream, &bus->stream_list, list) {
  267. struct hdac_ext_stream *hext_stream = container_of(hstream,
  268. struct hdac_ext_stream,
  269. hstream);
  270. if (hstream->direction != substream->stream)
  271. continue;
  272. if (!hstream->opened) {
  273. res = hext_stream;
  274. break;
  275. }
  276. }
  277. if (res) {
  278. snd_hdac_ext_stream_decouple_locked(bus, res, true);
  279. res->hstream.opened = 1;
  280. res->hstream.running = 0;
  281. res->hstream.substream = substream;
  282. }
  283. spin_unlock_irq(&bus->reg_lock);
  284. return res;
  285. }
  286. /**
  287. * snd_hdac_ext_stream_assign - assign a stream for the PCM
  288. * @bus: HD-audio core bus
  289. * @substream: PCM substream to assign
  290. * @type: type of stream (coupled, host or link stream)
  291. *
  292. * This assigns the stream based on the type (coupled/host/link), for the
  293. * given PCM substream, assigns it and returns the stream object
  294. *
  295. * coupled: Looks for an unused stream
  296. * host: Looks for an unused decoupled host stream
  297. * link: Looks for an unused decoupled link stream
  298. *
  299. * If no stream is free, returns NULL. The function tries to keep using
  300. * the same stream object when it's used beforehand. when a stream is
  301. * decoupled, it becomes a host stream and link stream.
  302. */
  303. struct hdac_ext_stream *snd_hdac_ext_stream_assign(struct hdac_bus *bus,
  304. struct snd_pcm_substream *substream,
  305. int type)
  306. {
  307. struct hdac_ext_stream *hext_stream = NULL;
  308. struct hdac_stream *hstream = NULL;
  309. switch (type) {
  310. case HDAC_EXT_STREAM_TYPE_COUPLED:
  311. hstream = snd_hdac_stream_assign(bus, substream);
  312. if (hstream)
  313. hext_stream = container_of(hstream,
  314. struct hdac_ext_stream,
  315. hstream);
  316. return hext_stream;
  317. case HDAC_EXT_STREAM_TYPE_HOST:
  318. return hdac_ext_host_dma_stream_assign(bus, substream);
  319. case HDAC_EXT_STREAM_TYPE_LINK:
  320. return hdac_ext_link_dma_stream_assign(bus, substream);
  321. default:
  322. return NULL;
  323. }
  324. }
  325. EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_assign);
  326. /**
  327. * snd_hdac_ext_stream_release - release the assigned stream
  328. * @hext_stream: HD-audio ext core stream to release
  329. * @type: type of stream (coupled, host or link stream)
  330. *
  331. * Release the stream that has been assigned by snd_hdac_ext_stream_assign().
  332. */
  333. void snd_hdac_ext_stream_release(struct hdac_ext_stream *hext_stream, int type)
  334. {
  335. struct hdac_bus *bus = hext_stream->hstream.bus;
  336. switch (type) {
  337. case HDAC_EXT_STREAM_TYPE_COUPLED:
  338. snd_hdac_stream_release(&hext_stream->hstream);
  339. break;
  340. case HDAC_EXT_STREAM_TYPE_HOST:
  341. spin_lock_irq(&bus->reg_lock);
  342. /* couple link only if not in use */
  343. if (!hext_stream->link_locked)
  344. snd_hdac_ext_stream_decouple_locked(bus, hext_stream, false);
  345. snd_hdac_stream_release_locked(&hext_stream->hstream);
  346. spin_unlock_irq(&bus->reg_lock);
  347. break;
  348. case HDAC_EXT_STREAM_TYPE_LINK:
  349. spin_lock_irq(&bus->reg_lock);
  350. /* couple host only if not in use */
  351. if (!hext_stream->hstream.opened)
  352. snd_hdac_ext_stream_decouple_locked(bus, hext_stream, false);
  353. hext_stream->link_locked = 0;
  354. hext_stream->link_substream = NULL;
  355. spin_unlock_irq(&bus->reg_lock);
  356. break;
  357. default:
  358. dev_dbg(bus->dev, "Invalid type %d\n", type);
  359. }
  360. }
  361. EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_release);
  362. /**
  363. * snd_hdac_ext_cstream_assign - assign a host stream for compress
  364. * @bus: HD-audio core bus
  365. * @cstream: Compress stream to assign
  366. *
  367. * Assign an unused host stream for the given compress stream.
  368. * If no stream is free, NULL is returned. Stream is decoupled
  369. * before assignment.
  370. */
  371. struct hdac_ext_stream *snd_hdac_ext_cstream_assign(struct hdac_bus *bus,
  372. struct snd_compr_stream *cstream)
  373. {
  374. struct hdac_ext_stream *res = NULL;
  375. struct hdac_stream *hstream;
  376. spin_lock_irq(&bus->reg_lock);
  377. list_for_each_entry(hstream, &bus->stream_list, list) {
  378. struct hdac_ext_stream *hext_stream = stream_to_hdac_ext_stream(hstream);
  379. if (hstream->direction != cstream->direction)
  380. continue;
  381. if (!hstream->opened) {
  382. res = hext_stream;
  383. break;
  384. }
  385. }
  386. if (res) {
  387. snd_hdac_ext_stream_decouple_locked(bus, res, true);
  388. res->hstream.opened = 1;
  389. res->hstream.running = 0;
  390. res->hstream.cstream = cstream;
  391. }
  392. spin_unlock_irq(&bus->reg_lock);
  393. return res;
  394. }
  395. EXPORT_SYMBOL_GPL(snd_hdac_ext_cstream_assign);