hdac_ext_stream.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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. if (ebus->spbcap) {
  48. stream->spib_addr = ebus->spbcap + AZX_SPB_BASE +
  49. AZX_SPB_INTERVAL * idx +
  50. AZX_SPB_SPIB;
  51. stream->fifo_addr = ebus->spbcap + AZX_SPB_BASE +
  52. AZX_SPB_INTERVAL * idx +
  53. AZX_SPB_MAXFIFO;
  54. }
  55. stream->decoupled = false;
  56. snd_hdac_stream_init(bus, &stream->hstream, idx, direction, tag);
  57. }
  58. EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_init);
  59. /**
  60. * snd_hdac_ext_stream_init_all - create and initialize the stream objects
  61. * for an extended hda bus
  62. * @ebus: HD-audio ext core bus
  63. * @start_idx: start index for streams
  64. * @num_stream: number of streams to initialize
  65. * @dir: direction of streams
  66. */
  67. int snd_hdac_ext_stream_init_all(struct hdac_ext_bus *ebus, int start_idx,
  68. int num_stream, int dir)
  69. {
  70. int stream_tag = 0;
  71. int i, tag, idx = start_idx;
  72. for (i = 0; i < num_stream; i++) {
  73. struct hdac_ext_stream *stream =
  74. kzalloc(sizeof(*stream), GFP_KERNEL);
  75. if (!stream)
  76. return -ENOMEM;
  77. tag = ++stream_tag;
  78. snd_hdac_ext_stream_init(ebus, stream, idx, dir, tag);
  79. idx++;
  80. }
  81. return 0;
  82. }
  83. EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_init_all);
  84. /**
  85. * snd_hdac_stream_free_all - free hdac extended stream objects
  86. *
  87. * @ebus: HD-audio ext core bus
  88. */
  89. void snd_hdac_stream_free_all(struct hdac_ext_bus *ebus)
  90. {
  91. struct hdac_stream *s;
  92. struct hdac_ext_stream *stream;
  93. struct hdac_bus *bus = ebus_to_hbus(ebus);
  94. while (!list_empty(&bus->stream_list)) {
  95. s = list_first_entry(&bus->stream_list, struct hdac_stream, list);
  96. stream = stream_to_hdac_ext_stream(s);
  97. list_del(&s->list);
  98. kfree(stream);
  99. }
  100. }
  101. EXPORT_SYMBOL_GPL(snd_hdac_stream_free_all);
  102. /**
  103. * snd_hdac_ext_stream_decouple - decouple the hdac stream
  104. * @ebus: HD-audio ext core bus
  105. * @stream: HD-audio ext core stream object to initialize
  106. * @decouple: flag to decouple
  107. */
  108. void snd_hdac_ext_stream_decouple(struct hdac_ext_bus *ebus,
  109. struct hdac_ext_stream *stream, bool decouple)
  110. {
  111. struct hdac_stream *hstream = &stream->hstream;
  112. struct hdac_bus *bus = &ebus->bus;
  113. spin_lock_irq(&bus->reg_lock);
  114. if (decouple)
  115. snd_hdac_updatel(ebus->ppcap, AZX_REG_PP_PPCTL, 0,
  116. AZX_PPCTL_PROCEN(hstream->index));
  117. else
  118. snd_hdac_updatel(ebus->ppcap, AZX_REG_PP_PPCTL,
  119. AZX_PPCTL_PROCEN(hstream->index), 0);
  120. stream->decoupled = decouple;
  121. spin_unlock_irq(&bus->reg_lock);
  122. }
  123. EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_decouple);
  124. /**
  125. * snd_hdac_ext_linkstream_start - start a stream
  126. * @stream: HD-audio ext core stream to start
  127. */
  128. void snd_hdac_ext_link_stream_start(struct hdac_ext_stream *stream)
  129. {
  130. snd_hdac_updatel(stream->pplc_addr, AZX_REG_PPLCCTL, 0, AZX_PPLCCTL_RUN);
  131. }
  132. EXPORT_SYMBOL_GPL(snd_hdac_ext_link_stream_start);
  133. /**
  134. * snd_hdac_ext_link_stream_clear - stop a stream DMA
  135. * @stream: HD-audio ext core stream to stop
  136. */
  137. void snd_hdac_ext_link_stream_clear(struct hdac_ext_stream *stream)
  138. {
  139. snd_hdac_updatel(stream->pplc_addr, AZX_REG_PPLCCTL, AZX_PPLCCTL_RUN, 0);
  140. }
  141. EXPORT_SYMBOL_GPL(snd_hdac_ext_link_stream_clear);
  142. /**
  143. * snd_hdac_ext_link_stream_reset - reset a stream
  144. * @stream: HD-audio ext core stream to reset
  145. */
  146. void snd_hdac_ext_link_stream_reset(struct hdac_ext_stream *stream)
  147. {
  148. unsigned char val;
  149. int timeout;
  150. snd_hdac_ext_link_stream_clear(stream);
  151. snd_hdac_updatel(stream->pplc_addr, AZX_REG_PPLCCTL, 0, AZX_PPLCCTL_STRST);
  152. udelay(3);
  153. timeout = 50;
  154. do {
  155. val = readl(stream->pplc_addr + AZX_REG_PPLCCTL) &
  156. AZX_PPLCCTL_STRST;
  157. if (val)
  158. break;
  159. udelay(3);
  160. } while (--timeout);
  161. val &= ~AZX_PPLCCTL_STRST;
  162. writel(val, stream->pplc_addr + AZX_REG_PPLCCTL);
  163. udelay(3);
  164. timeout = 50;
  165. /* waiting for hardware to report that the stream is out of reset */
  166. do {
  167. val = readl(stream->pplc_addr + AZX_REG_PPLCCTL) & AZX_PPLCCTL_STRST;
  168. if (!val)
  169. break;
  170. udelay(3);
  171. } while (--timeout);
  172. }
  173. EXPORT_SYMBOL_GPL(snd_hdac_ext_link_stream_reset);
  174. /**
  175. * snd_hdac_ext_link_stream_setup - set up the SD for streaming
  176. * @stream: HD-audio ext core stream to set up
  177. * @fmt: stream format
  178. */
  179. int snd_hdac_ext_link_stream_setup(struct hdac_ext_stream *stream, int fmt)
  180. {
  181. struct hdac_stream *hstream = &stream->hstream;
  182. unsigned int val;
  183. /* make sure the run bit is zero for SD */
  184. snd_hdac_ext_link_stream_clear(stream);
  185. /* program the stream_tag */
  186. val = readl(stream->pplc_addr + AZX_REG_PPLCCTL);
  187. val = (val & ~AZX_PPLCCTL_STRM_MASK) |
  188. (hstream->stream_tag << AZX_PPLCCTL_STRM_SHIFT);
  189. writel(val, stream->pplc_addr + AZX_REG_PPLCCTL);
  190. /* program the stream format */
  191. writew(fmt, stream->pplc_addr + AZX_REG_PPLCFMT);
  192. return 0;
  193. }
  194. EXPORT_SYMBOL_GPL(snd_hdac_ext_link_stream_setup);
  195. /**
  196. * snd_hdac_ext_link_set_stream_id - maps stream id to link output
  197. * @link: HD-audio ext link to set up
  198. * @stream: stream id
  199. */
  200. void snd_hdac_ext_link_set_stream_id(struct hdac_ext_link *link,
  201. int stream)
  202. {
  203. snd_hdac_updatew(link->ml_addr, AZX_REG_ML_LOSIDV, (1 << stream), 1 << stream);
  204. }
  205. EXPORT_SYMBOL_GPL(snd_hdac_ext_link_set_stream_id);
  206. /**
  207. * snd_hdac_ext_link_clear_stream_id - maps stream id to link output
  208. * @link: HD-audio ext link to set up
  209. * @stream: stream id
  210. */
  211. void snd_hdac_ext_link_clear_stream_id(struct hdac_ext_link *link,
  212. int stream)
  213. {
  214. snd_hdac_updatew(link->ml_addr, AZX_REG_ML_LOSIDV, 0, (1 << stream));
  215. }
  216. EXPORT_SYMBOL_GPL(snd_hdac_ext_link_clear_stream_id);
  217. static struct hdac_ext_stream *
  218. hdac_ext_link_stream_assign(struct hdac_ext_bus *ebus,
  219. struct snd_pcm_substream *substream)
  220. {
  221. struct hdac_ext_stream *res = NULL;
  222. struct hdac_stream *stream = NULL;
  223. struct hdac_bus *hbus = &ebus->bus;
  224. if (!ebus->ppcap) {
  225. dev_err(hbus->dev, "stream type not supported\n");
  226. return NULL;
  227. }
  228. list_for_each_entry(stream, &hbus->stream_list, list) {
  229. struct hdac_ext_stream *hstream = container_of(stream,
  230. struct hdac_ext_stream,
  231. hstream);
  232. if (stream->direction != substream->stream)
  233. continue;
  234. /* check if decoupled stream and not in use is available */
  235. if (hstream->decoupled && !hstream->link_locked) {
  236. res = hstream;
  237. break;
  238. }
  239. if (!hstream->link_locked) {
  240. snd_hdac_ext_stream_decouple(ebus, hstream, true);
  241. res = hstream;
  242. break;
  243. }
  244. }
  245. if (res) {
  246. spin_lock_irq(&hbus->reg_lock);
  247. res->link_locked = 1;
  248. res->link_substream = substream;
  249. spin_unlock_irq(&hbus->reg_lock);
  250. }
  251. return res;
  252. }
  253. static struct hdac_ext_stream *
  254. hdac_ext_host_stream_assign(struct hdac_ext_bus *ebus,
  255. struct snd_pcm_substream *substream)
  256. {
  257. struct hdac_ext_stream *res = NULL;
  258. struct hdac_stream *stream = NULL;
  259. struct hdac_bus *hbus = &ebus->bus;
  260. if (!ebus->ppcap) {
  261. dev_err(hbus->dev, "stream type not supported\n");
  262. return NULL;
  263. }
  264. list_for_each_entry(stream, &hbus->stream_list, list) {
  265. struct hdac_ext_stream *hstream = container_of(stream,
  266. struct hdac_ext_stream,
  267. hstream);
  268. if (stream->direction != substream->stream)
  269. continue;
  270. if (!stream->opened) {
  271. if (!hstream->decoupled)
  272. snd_hdac_ext_stream_decouple(ebus, hstream, true);
  273. res = hstream;
  274. break;
  275. }
  276. }
  277. if (res) {
  278. spin_lock_irq(&hbus->reg_lock);
  279. res->hstream.opened = 1;
  280. res->hstream.running = 0;
  281. res->hstream.substream = substream;
  282. spin_unlock_irq(&hbus->reg_lock);
  283. }
  284. return res;
  285. }
  286. /**
  287. * snd_hdac_ext_stream_assign - assign a stream for the PCM
  288. * @ebus: HD-audio ext 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_ext_bus *ebus,
  304. struct snd_pcm_substream *substream,
  305. int type)
  306. {
  307. struct hdac_ext_stream *hstream = NULL;
  308. struct hdac_stream *stream = NULL;
  309. struct hdac_bus *hbus = &ebus->bus;
  310. switch (type) {
  311. case HDAC_EXT_STREAM_TYPE_COUPLED:
  312. stream = snd_hdac_stream_assign(hbus, substream);
  313. if (stream)
  314. hstream = container_of(stream,
  315. struct hdac_ext_stream, hstream);
  316. return hstream;
  317. case HDAC_EXT_STREAM_TYPE_HOST:
  318. return hdac_ext_host_stream_assign(ebus, substream);
  319. case HDAC_EXT_STREAM_TYPE_LINK:
  320. return hdac_ext_link_stream_assign(ebus, 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. * @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 *stream, int type)
  334. {
  335. struct hdac_bus *bus = stream->hstream.bus;
  336. struct hdac_ext_bus *ebus = hbus_to_ebus(bus);
  337. switch (type) {
  338. case HDAC_EXT_STREAM_TYPE_COUPLED:
  339. snd_hdac_stream_release(&stream->hstream);
  340. break;
  341. case HDAC_EXT_STREAM_TYPE_HOST:
  342. if (stream->decoupled && !stream->link_locked)
  343. snd_hdac_ext_stream_decouple(ebus, stream, false);
  344. snd_hdac_stream_release(&stream->hstream);
  345. break;
  346. case HDAC_EXT_STREAM_TYPE_LINK:
  347. if (stream->decoupled && !stream->hstream.opened)
  348. snd_hdac_ext_stream_decouple(ebus, stream, false);
  349. spin_lock_irq(&bus->reg_lock);
  350. stream->link_locked = 0;
  351. stream->link_substream = NULL;
  352. spin_unlock_irq(&bus->reg_lock);
  353. break;
  354. default:
  355. dev_dbg(bus->dev, "Invalid type %d\n", type);
  356. }
  357. }
  358. EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_release);
  359. /**
  360. * snd_hdac_ext_stream_spbcap_enable - enable SPIB for a stream
  361. * @ebus: HD-audio ext core bus
  362. * @enable: flag to enable/disable SPIB
  363. * @index: stream index for which SPIB need to be enabled
  364. */
  365. void snd_hdac_ext_stream_spbcap_enable(struct hdac_ext_bus *ebus,
  366. bool enable, int index)
  367. {
  368. u32 mask = 0;
  369. u32 register_mask = 0;
  370. struct hdac_bus *bus = &ebus->bus;
  371. if (!ebus->spbcap) {
  372. dev_err(bus->dev, "Address of SPB capability is NULL");
  373. return;
  374. }
  375. mask |= (1 << index);
  376. register_mask = readl(ebus->spbcap + AZX_REG_SPB_SPBFCCTL);
  377. mask |= register_mask;
  378. if (enable)
  379. snd_hdac_updatel(ebus->spbcap, AZX_REG_SPB_SPBFCCTL, 0, mask);
  380. else
  381. snd_hdac_updatel(ebus->spbcap, AZX_REG_SPB_SPBFCCTL, mask, 0);
  382. }
  383. EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_spbcap_enable);
  384. /**
  385. * snd_hdac_ext_stream_set_spib - sets the spib value of a stream
  386. * @ebus: HD-audio ext core bus
  387. * @stream: hdac_ext_stream
  388. * @value: spib value to set
  389. */
  390. int snd_hdac_ext_stream_set_spib(struct hdac_ext_bus *ebus,
  391. struct hdac_ext_stream *stream, u32 value)
  392. {
  393. struct hdac_bus *bus = &ebus->bus;
  394. if (!ebus->spbcap) {
  395. dev_err(bus->dev, "Address of SPB capability is NULL");
  396. return -EINVAL;
  397. }
  398. writel(value, stream->spib_addr);
  399. return 0;
  400. }
  401. EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_set_spib);
  402. /**
  403. * snd_hdac_ext_stream_get_spbmaxfifo - gets the spib value of a stream
  404. * @ebus: HD-audio ext core bus
  405. * @stream: hdac_ext_stream
  406. *
  407. * Return maxfifo for the stream
  408. */
  409. int snd_hdac_ext_stream_get_spbmaxfifo(struct hdac_ext_bus *ebus,
  410. struct hdac_ext_stream *stream)
  411. {
  412. struct hdac_bus *bus = &ebus->bus;
  413. if (!ebus->spbcap) {
  414. dev_err(bus->dev, "Address of SPB capability is NULL");
  415. return -EINVAL;
  416. }
  417. return readl(stream->fifo_addr);
  418. }
  419. EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_get_spbmaxfifo);
  420. /**
  421. * snd_hdac_ext_stop_streams - stop all stream if running
  422. * @ebus: HD-audio ext core bus
  423. */
  424. void snd_hdac_ext_stop_streams(struct hdac_ext_bus *ebus)
  425. {
  426. struct hdac_bus *bus = ebus_to_hbus(ebus);
  427. struct hdac_stream *stream;
  428. if (bus->chip_init) {
  429. list_for_each_entry(stream, &bus->stream_list, list)
  430. snd_hdac_stream_stop(stream);
  431. snd_hdac_bus_stop_chip(bus);
  432. }
  433. }
  434. EXPORT_SYMBOL_GPL(snd_hdac_ext_stop_streams);