hdac_ext_stream.c 12 KB

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