hdac_ext_stream.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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. * @bus: HD-audio 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_bus *bus,
  36. struct hdac_ext_stream *stream,
  37. int idx, int direction, int tag)
  38. {
  39. if (bus->ppcap) {
  40. stream->pphc_addr = bus->ppcap + AZX_PPHC_BASE +
  41. AZX_PPHC_INTERVAL * idx;
  42. stream->pplc_addr = bus->ppcap + AZX_PPLC_BASE +
  43. AZX_PPLC_MULTI * bus->num_streams +
  44. AZX_PPLC_INTERVAL * idx;
  45. }
  46. if (bus->spbcap) {
  47. stream->spib_addr = bus->spbcap + AZX_SPB_BASE +
  48. AZX_SPB_INTERVAL * idx +
  49. AZX_SPB_SPIB;
  50. stream->fifo_addr = bus->spbcap + AZX_SPB_BASE +
  51. AZX_SPB_INTERVAL * idx +
  52. AZX_SPB_MAXFIFO;
  53. }
  54. if (bus->drsmcap)
  55. stream->dpibr_addr = bus->drsmcap + AZX_DRSM_BASE +
  56. AZX_DRSM_INTERVAL * idx;
  57. stream->decoupled = false;
  58. snd_hdac_stream_init(bus, &stream->hstream, idx, direction, tag);
  59. }
  60. EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_init);
  61. /**
  62. * snd_hdac_ext_stream_init_all - create and initialize the stream objects
  63. * for an extended hda bus
  64. * @bus: HD-audio core bus
  65. * @start_idx: start index for streams
  66. * @num_stream: number of streams to initialize
  67. * @dir: direction of streams
  68. */
  69. int snd_hdac_ext_stream_init_all(struct hdac_bus *bus, int start_idx,
  70. int num_stream, int dir)
  71. {
  72. int stream_tag = 0;
  73. int i, tag, idx = start_idx;
  74. for (i = 0; i < num_stream; i++) {
  75. struct hdac_ext_stream *stream =
  76. kzalloc(sizeof(*stream), GFP_KERNEL);
  77. if (!stream)
  78. return -ENOMEM;
  79. tag = ++stream_tag;
  80. snd_hdac_ext_stream_init(bus, stream, idx, dir, tag);
  81. idx++;
  82. }
  83. return 0;
  84. }
  85. EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_init_all);
  86. /**
  87. * snd_hdac_stream_free_all - free hdac extended stream objects
  88. *
  89. * @bus: HD-audio core bus
  90. */
  91. void snd_hdac_stream_free_all(struct hdac_bus *bus)
  92. {
  93. struct hdac_stream *s, *_s;
  94. struct hdac_ext_stream *stream;
  95. list_for_each_entry_safe(s, _s, &bus->stream_list, list) {
  96. stream = stream_to_hdac_ext_stream(s);
  97. snd_hdac_ext_stream_decouple(bus, stream, false);
  98. list_del(&s->list);
  99. kfree(stream);
  100. }
  101. }
  102. EXPORT_SYMBOL_GPL(snd_hdac_stream_free_all);
  103. /**
  104. * snd_hdac_ext_stream_decouple - decouple the hdac stream
  105. * @bus: HD-audio core bus
  106. * @stream: HD-audio ext core stream object to initialize
  107. * @decouple: flag to decouple
  108. */
  109. void snd_hdac_ext_stream_decouple(struct hdac_bus *bus,
  110. struct hdac_ext_stream *stream, bool decouple)
  111. {
  112. struct hdac_stream *hstream = &stream->hstream;
  113. u32 val;
  114. int mask = AZX_PPCTL_PROCEN(hstream->index);
  115. spin_lock_irq(&bus->reg_lock);
  116. val = readw(bus->ppcap + AZX_REG_PP_PPCTL) & mask;
  117. if (decouple && !val)
  118. snd_hdac_updatel(bus->ppcap, AZX_REG_PP_PPCTL, mask, mask);
  119. else if (!decouple && val)
  120. snd_hdac_updatel(bus->ppcap, AZX_REG_PP_PPCTL, mask, 0);
  121. stream->decoupled = decouple;
  122. spin_unlock_irq(&bus->reg_lock);
  123. }
  124. EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_decouple);
  125. /**
  126. * snd_hdac_ext_linkstream_start - start a stream
  127. * @stream: HD-audio ext core stream to start
  128. */
  129. void snd_hdac_ext_link_stream_start(struct hdac_ext_stream *stream)
  130. {
  131. snd_hdac_updatel(stream->pplc_addr, AZX_REG_PPLCCTL,
  132. AZX_PPLCCTL_RUN, AZX_PPLCCTL_RUN);
  133. }
  134. EXPORT_SYMBOL_GPL(snd_hdac_ext_link_stream_start);
  135. /**
  136. * snd_hdac_ext_link_stream_clear - stop a stream DMA
  137. * @stream: HD-audio ext core stream to stop
  138. */
  139. void snd_hdac_ext_link_stream_clear(struct hdac_ext_stream *stream)
  140. {
  141. snd_hdac_updatel(stream->pplc_addr, AZX_REG_PPLCCTL, AZX_PPLCCTL_RUN, 0);
  142. }
  143. EXPORT_SYMBOL_GPL(snd_hdac_ext_link_stream_clear);
  144. /**
  145. * snd_hdac_ext_link_stream_reset - reset a stream
  146. * @stream: HD-audio ext core stream to reset
  147. */
  148. void snd_hdac_ext_link_stream_reset(struct hdac_ext_stream *stream)
  149. {
  150. unsigned char val;
  151. int timeout;
  152. snd_hdac_ext_link_stream_clear(stream);
  153. snd_hdac_updatel(stream->pplc_addr, AZX_REG_PPLCCTL,
  154. AZX_PPLCCTL_STRST, AZX_PPLCCTL_STRST);
  155. udelay(3);
  156. timeout = 50;
  157. do {
  158. val = readl(stream->pplc_addr + AZX_REG_PPLCCTL) &
  159. AZX_PPLCCTL_STRST;
  160. if (val)
  161. break;
  162. udelay(3);
  163. } while (--timeout);
  164. val &= ~AZX_PPLCCTL_STRST;
  165. writel(val, stream->pplc_addr + AZX_REG_PPLCCTL);
  166. udelay(3);
  167. timeout = 50;
  168. /* waiting for hardware to report that the stream is out of reset */
  169. do {
  170. val = readl(stream->pplc_addr + AZX_REG_PPLCCTL) & AZX_PPLCCTL_STRST;
  171. if (!val)
  172. break;
  173. udelay(3);
  174. } while (--timeout);
  175. }
  176. EXPORT_SYMBOL_GPL(snd_hdac_ext_link_stream_reset);
  177. /**
  178. * snd_hdac_ext_link_stream_setup - set up the SD for streaming
  179. * @stream: HD-audio ext core stream to set up
  180. * @fmt: stream format
  181. */
  182. int snd_hdac_ext_link_stream_setup(struct hdac_ext_stream *stream, int fmt)
  183. {
  184. struct hdac_stream *hstream = &stream->hstream;
  185. unsigned int val;
  186. /* make sure the run bit is zero for SD */
  187. snd_hdac_ext_link_stream_clear(stream);
  188. /* program the stream_tag */
  189. val = readl(stream->pplc_addr + AZX_REG_PPLCCTL);
  190. val = (val & ~AZX_PPLCCTL_STRM_MASK) |
  191. (hstream->stream_tag << AZX_PPLCCTL_STRM_SHIFT);
  192. writel(val, stream->pplc_addr + AZX_REG_PPLCCTL);
  193. /* program the stream format */
  194. writew(fmt, stream->pplc_addr + AZX_REG_PPLCFMT);
  195. return 0;
  196. }
  197. EXPORT_SYMBOL_GPL(snd_hdac_ext_link_stream_setup);
  198. /**
  199. * snd_hdac_ext_link_set_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_set_stream_id(struct hdac_ext_link *link,
  204. int stream)
  205. {
  206. snd_hdac_updatew(link->ml_addr, AZX_REG_ML_LOSIDV, (1 << stream), 1 << stream);
  207. }
  208. EXPORT_SYMBOL_GPL(snd_hdac_ext_link_set_stream_id);
  209. /**
  210. * snd_hdac_ext_link_clear_stream_id - maps stream id to link output
  211. * @link: HD-audio ext link to set up
  212. * @stream: stream id
  213. */
  214. void snd_hdac_ext_link_clear_stream_id(struct hdac_ext_link *link,
  215. int stream)
  216. {
  217. snd_hdac_updatew(link->ml_addr, AZX_REG_ML_LOSIDV, (1 << stream), 0);
  218. }
  219. EXPORT_SYMBOL_GPL(snd_hdac_ext_link_clear_stream_id);
  220. static struct hdac_ext_stream *
  221. hdac_ext_link_stream_assign(struct hdac_bus *bus,
  222. struct snd_pcm_substream *substream)
  223. {
  224. struct hdac_ext_stream *res = NULL;
  225. struct hdac_stream *stream = NULL;
  226. if (!bus->ppcap) {
  227. dev_err(bus->dev, "stream type not supported\n");
  228. return NULL;
  229. }
  230. list_for_each_entry(stream, &bus->stream_list, list) {
  231. struct hdac_ext_stream *hstream = container_of(stream,
  232. struct hdac_ext_stream,
  233. hstream);
  234. if (stream->direction != substream->stream)
  235. continue;
  236. /* check if decoupled stream and not in use is available */
  237. if (hstream->decoupled && !hstream->link_locked) {
  238. res = hstream;
  239. break;
  240. }
  241. if (!hstream->link_locked) {
  242. snd_hdac_ext_stream_decouple(bus, hstream, true);
  243. res = hstream;
  244. break;
  245. }
  246. }
  247. if (res) {
  248. spin_lock_irq(&bus->reg_lock);
  249. res->link_locked = 1;
  250. res->link_substream = substream;
  251. spin_unlock_irq(&bus->reg_lock);
  252. }
  253. return res;
  254. }
  255. static struct hdac_ext_stream *
  256. hdac_ext_host_stream_assign(struct hdac_bus *bus,
  257. struct snd_pcm_substream *substream)
  258. {
  259. struct hdac_ext_stream *res = NULL;
  260. struct hdac_stream *stream = NULL;
  261. if (!bus->ppcap) {
  262. dev_err(bus->dev, "stream type not supported\n");
  263. return NULL;
  264. }
  265. list_for_each_entry(stream, &bus->stream_list, list) {
  266. struct hdac_ext_stream *hstream = container_of(stream,
  267. struct hdac_ext_stream,
  268. hstream);
  269. if (stream->direction != substream->stream)
  270. continue;
  271. if (!stream->opened) {
  272. if (!hstream->decoupled)
  273. snd_hdac_ext_stream_decouple(bus, hstream, true);
  274. res = hstream;
  275. break;
  276. }
  277. }
  278. if (res) {
  279. spin_lock_irq(&bus->reg_lock);
  280. res->hstream.opened = 1;
  281. res->hstream.running = 0;
  282. res->hstream.substream = substream;
  283. spin_unlock_irq(&bus->reg_lock);
  284. }
  285. return res;
  286. }
  287. /**
  288. * snd_hdac_ext_stream_assign - assign a stream for the PCM
  289. * @bus: HD-audio core bus
  290. * @substream: PCM substream to assign
  291. * @type: type of stream (coupled, host or link stream)
  292. *
  293. * This assigns the stream based on the type (coupled/host/link), for the
  294. * given PCM substream, assigns it and returns the stream object
  295. *
  296. * coupled: Looks for an unused stream
  297. * host: Looks for an unused decoupled host stream
  298. * link: Looks for an unused decoupled link stream
  299. *
  300. * If no stream is free, returns NULL. The function tries to keep using
  301. * the same stream object when it's used beforehand. when a stream is
  302. * decoupled, it becomes a host stream and link stream.
  303. */
  304. struct hdac_ext_stream *snd_hdac_ext_stream_assign(struct hdac_bus *bus,
  305. struct snd_pcm_substream *substream,
  306. int type)
  307. {
  308. struct hdac_ext_stream *hstream = NULL;
  309. struct hdac_stream *stream = NULL;
  310. switch (type) {
  311. case HDAC_EXT_STREAM_TYPE_COUPLED:
  312. stream = snd_hdac_stream_assign(bus, 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(bus, substream);
  319. case HDAC_EXT_STREAM_TYPE_LINK:
  320. return hdac_ext_link_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. * @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. switch (type) {
  337. case HDAC_EXT_STREAM_TYPE_COUPLED:
  338. snd_hdac_stream_release(&stream->hstream);
  339. break;
  340. case HDAC_EXT_STREAM_TYPE_HOST:
  341. if (stream->decoupled && !stream->link_locked)
  342. snd_hdac_ext_stream_decouple(bus, stream, false);
  343. snd_hdac_stream_release(&stream->hstream);
  344. break;
  345. case HDAC_EXT_STREAM_TYPE_LINK:
  346. if (stream->decoupled && !stream->hstream.opened)
  347. snd_hdac_ext_stream_decouple(bus, stream, false);
  348. spin_lock_irq(&bus->reg_lock);
  349. stream->link_locked = 0;
  350. stream->link_substream = NULL;
  351. spin_unlock_irq(&bus->reg_lock);
  352. break;
  353. default:
  354. dev_dbg(bus->dev, "Invalid type %d\n", type);
  355. }
  356. }
  357. EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_release);
  358. /**
  359. * snd_hdac_ext_stream_spbcap_enable - enable SPIB for a stream
  360. * @bus: HD-audio core bus
  361. * @enable: flag to enable/disable SPIB
  362. * @index: stream index for which SPIB need to be enabled
  363. */
  364. void snd_hdac_ext_stream_spbcap_enable(struct hdac_bus *bus,
  365. bool enable, int index)
  366. {
  367. u32 mask = 0;
  368. if (!bus->spbcap) {
  369. dev_err(bus->dev, "Address of SPB capability is NULL\n");
  370. return;
  371. }
  372. mask |= (1 << index);
  373. if (enable)
  374. snd_hdac_updatel(bus->spbcap, AZX_REG_SPB_SPBFCCTL, mask, mask);
  375. else
  376. snd_hdac_updatel(bus->spbcap, AZX_REG_SPB_SPBFCCTL, mask, 0);
  377. }
  378. EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_spbcap_enable);
  379. /**
  380. * snd_hdac_ext_stream_set_spib - sets the spib value of a stream
  381. * @bus: HD-audio core bus
  382. * @stream: hdac_ext_stream
  383. * @value: spib value to set
  384. */
  385. int snd_hdac_ext_stream_set_spib(struct hdac_bus *bus,
  386. struct hdac_ext_stream *stream, u32 value)
  387. {
  388. if (!bus->spbcap) {
  389. dev_err(bus->dev, "Address of SPB capability is NULL\n");
  390. return -EINVAL;
  391. }
  392. writel(value, stream->spib_addr);
  393. return 0;
  394. }
  395. EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_set_spib);
  396. /**
  397. * snd_hdac_ext_stream_get_spbmaxfifo - gets the spib value of a stream
  398. * @bus: HD-audio core bus
  399. * @stream: hdac_ext_stream
  400. *
  401. * Return maxfifo for the stream
  402. */
  403. int snd_hdac_ext_stream_get_spbmaxfifo(struct hdac_bus *bus,
  404. struct hdac_ext_stream *stream)
  405. {
  406. if (!bus->spbcap) {
  407. dev_err(bus->dev, "Address of SPB capability is NULL\n");
  408. return -EINVAL;
  409. }
  410. return readl(stream->fifo_addr);
  411. }
  412. EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_get_spbmaxfifo);
  413. /**
  414. * snd_hdac_ext_stop_streams - stop all stream if running
  415. * @bus: HD-audio core bus
  416. */
  417. void snd_hdac_ext_stop_streams(struct hdac_bus *bus)
  418. {
  419. struct hdac_stream *stream;
  420. if (bus->chip_init) {
  421. list_for_each_entry(stream, &bus->stream_list, list)
  422. snd_hdac_stream_stop(stream);
  423. snd_hdac_bus_stop_chip(bus);
  424. }
  425. }
  426. EXPORT_SYMBOL_GPL(snd_hdac_ext_stop_streams);
  427. /**
  428. * snd_hdac_ext_stream_drsm_enable - enable DMA resume for a stream
  429. * @bus: HD-audio core bus
  430. * @enable: flag to enable/disable DRSM
  431. * @index: stream index for which DRSM need to be enabled
  432. */
  433. void snd_hdac_ext_stream_drsm_enable(struct hdac_bus *bus,
  434. bool enable, int index)
  435. {
  436. u32 mask = 0;
  437. if (!bus->drsmcap) {
  438. dev_err(bus->dev, "Address of DRSM capability is NULL\n");
  439. return;
  440. }
  441. mask |= (1 << index);
  442. if (enable)
  443. snd_hdac_updatel(bus->drsmcap, AZX_REG_DRSM_CTL, mask, mask);
  444. else
  445. snd_hdac_updatel(bus->drsmcap, AZX_REG_DRSM_CTL, mask, 0);
  446. }
  447. EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_drsm_enable);
  448. /**
  449. * snd_hdac_ext_stream_set_dpibr - sets the dpibr value of a stream
  450. * @bus: HD-audio core bus
  451. * @stream: hdac_ext_stream
  452. * @value: dpib value to set
  453. */
  454. int snd_hdac_ext_stream_set_dpibr(struct hdac_bus *bus,
  455. struct hdac_ext_stream *stream, u32 value)
  456. {
  457. if (!bus->drsmcap) {
  458. dev_err(bus->dev, "Address of DRSM capability is NULL\n");
  459. return -EINVAL;
  460. }
  461. writel(value, stream->dpibr_addr);
  462. return 0;
  463. }
  464. EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_set_dpibr);
  465. /**
  466. * snd_hdac_ext_stream_set_lpib - sets the lpib value of a stream
  467. * @bus: HD-audio core bus
  468. * @stream: hdac_ext_stream
  469. * @value: lpib value to set
  470. */
  471. int snd_hdac_ext_stream_set_lpib(struct hdac_ext_stream *stream, u32 value)
  472. {
  473. snd_hdac_stream_writel(&stream->hstream, SD_LPIB, value);
  474. return 0;
  475. }
  476. EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_set_lpib);