hdac_ext_stream.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  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 (bus->ppcap) {
  41. stream->pphc_addr = bus->ppcap + AZX_PPHC_BASE +
  42. AZX_PPHC_INTERVAL * idx;
  43. stream->pplc_addr = bus->ppcap + AZX_PPLC_BASE +
  44. AZX_PPLC_MULTI * ebus->num_streams +
  45. AZX_PPLC_INTERVAL * idx;
  46. }
  47. if (bus->spbcap) {
  48. stream->spib_addr = bus->spbcap + AZX_SPB_BASE +
  49. AZX_SPB_INTERVAL * idx +
  50. AZX_SPB_SPIB;
  51. stream->fifo_addr = bus->spbcap + AZX_SPB_BASE +
  52. AZX_SPB_INTERVAL * idx +
  53. AZX_SPB_MAXFIFO;
  54. }
  55. if (bus->drsmcap)
  56. stream->dpibr_addr = bus->drsmcap + AZX_DRSM_BASE +
  57. AZX_DRSM_INTERVAL * idx;
  58. stream->decoupled = false;
  59. snd_hdac_stream_init(bus, &stream->hstream, idx, direction, tag);
  60. }
  61. EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_init);
  62. /**
  63. * snd_hdac_ext_stream_init_all - create and initialize the stream objects
  64. * for an extended hda bus
  65. * @ebus: HD-audio ext core bus
  66. * @start_idx: start index for streams
  67. * @num_stream: number of streams to initialize
  68. * @dir: direction of streams
  69. */
  70. int snd_hdac_ext_stream_init_all(struct hdac_ext_bus *ebus, int start_idx,
  71. int num_stream, int dir)
  72. {
  73. int stream_tag = 0;
  74. int i, tag, idx = start_idx;
  75. for (i = 0; i < num_stream; i++) {
  76. struct hdac_ext_stream *stream =
  77. kzalloc(sizeof(*stream), GFP_KERNEL);
  78. if (!stream)
  79. return -ENOMEM;
  80. tag = ++stream_tag;
  81. snd_hdac_ext_stream_init(ebus, stream, idx, dir, tag);
  82. idx++;
  83. }
  84. return 0;
  85. }
  86. EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_init_all);
  87. /**
  88. * snd_hdac_stream_free_all - free hdac extended stream objects
  89. *
  90. * @ebus: HD-audio ext core bus
  91. */
  92. void snd_hdac_stream_free_all(struct hdac_ext_bus *ebus)
  93. {
  94. struct hdac_stream *s, *_s;
  95. struct hdac_ext_stream *stream;
  96. struct hdac_bus *bus = ebus_to_hbus(ebus);
  97. list_for_each_entry_safe(s, _s, &bus->stream_list, list) {
  98. stream = stream_to_hdac_ext_stream(s);
  99. snd_hdac_ext_stream_decouple(ebus, stream, false);
  100. list_del(&s->list);
  101. kfree(stream);
  102. }
  103. }
  104. EXPORT_SYMBOL_GPL(snd_hdac_stream_free_all);
  105. /**
  106. * snd_hdac_ext_stream_decouple - decouple the hdac stream
  107. * @ebus: HD-audio ext core bus
  108. * @stream: HD-audio ext core stream object to initialize
  109. * @decouple: flag to decouple
  110. */
  111. void snd_hdac_ext_stream_decouple(struct hdac_ext_bus *ebus,
  112. struct hdac_ext_stream *stream, bool decouple)
  113. {
  114. struct hdac_stream *hstream = &stream->hstream;
  115. struct hdac_bus *bus = &ebus->bus;
  116. spin_lock_irq(&bus->reg_lock);
  117. if (decouple)
  118. snd_hdac_updatel(bus->ppcap, AZX_REG_PP_PPCTL, 0,
  119. AZX_PPCTL_PROCEN(hstream->index));
  120. else
  121. snd_hdac_updatel(bus->ppcap, AZX_REG_PP_PPCTL,
  122. AZX_PPCTL_PROCEN(hstream->index), 0);
  123. stream->decoupled = decouple;
  124. spin_unlock_irq(&bus->reg_lock);
  125. }
  126. EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_decouple);
  127. /**
  128. * snd_hdac_ext_linkstream_start - start a stream
  129. * @stream: HD-audio ext core stream to start
  130. */
  131. void snd_hdac_ext_link_stream_start(struct hdac_ext_stream *stream)
  132. {
  133. snd_hdac_updatel(stream->pplc_addr, AZX_REG_PPLCCTL, 0, AZX_PPLCCTL_RUN);
  134. }
  135. EXPORT_SYMBOL_GPL(snd_hdac_ext_link_stream_start);
  136. /**
  137. * snd_hdac_ext_link_stream_clear - stop a stream DMA
  138. * @stream: HD-audio ext core stream to stop
  139. */
  140. void snd_hdac_ext_link_stream_clear(struct hdac_ext_stream *stream)
  141. {
  142. snd_hdac_updatel(stream->pplc_addr, AZX_REG_PPLCCTL, AZX_PPLCCTL_RUN, 0);
  143. }
  144. EXPORT_SYMBOL_GPL(snd_hdac_ext_link_stream_clear);
  145. /**
  146. * snd_hdac_ext_link_stream_reset - reset a stream
  147. * @stream: HD-audio ext core stream to reset
  148. */
  149. void snd_hdac_ext_link_stream_reset(struct hdac_ext_stream *stream)
  150. {
  151. unsigned char val;
  152. int timeout;
  153. snd_hdac_ext_link_stream_clear(stream);
  154. snd_hdac_updatel(stream->pplc_addr, AZX_REG_PPLCCTL, 0, 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, 0, (1 << stream));
  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_ext_bus *ebus,
  222. struct snd_pcm_substream *substream)
  223. {
  224. struct hdac_ext_stream *res = NULL;
  225. struct hdac_stream *stream = NULL;
  226. struct hdac_bus *hbus = &ebus->bus;
  227. if (!hbus->ppcap) {
  228. dev_err(hbus->dev, "stream type not supported\n");
  229. return NULL;
  230. }
  231. list_for_each_entry(stream, &hbus->stream_list, list) {
  232. struct hdac_ext_stream *hstream = container_of(stream,
  233. struct hdac_ext_stream,
  234. hstream);
  235. if (stream->direction != substream->stream)
  236. continue;
  237. /* check if decoupled stream and not in use is available */
  238. if (hstream->decoupled && !hstream->link_locked) {
  239. res = hstream;
  240. break;
  241. }
  242. if (!hstream->link_locked) {
  243. snd_hdac_ext_stream_decouple(ebus, hstream, true);
  244. res = hstream;
  245. break;
  246. }
  247. }
  248. if (res) {
  249. spin_lock_irq(&hbus->reg_lock);
  250. res->link_locked = 1;
  251. res->link_substream = substream;
  252. spin_unlock_irq(&hbus->reg_lock);
  253. }
  254. return res;
  255. }
  256. static struct hdac_ext_stream *
  257. hdac_ext_host_stream_assign(struct hdac_ext_bus *ebus,
  258. struct snd_pcm_substream *substream)
  259. {
  260. struct hdac_ext_stream *res = NULL;
  261. struct hdac_stream *stream = NULL;
  262. struct hdac_bus *hbus = &ebus->bus;
  263. if (!hbus->ppcap) {
  264. dev_err(hbus->dev, "stream type not supported\n");
  265. return NULL;
  266. }
  267. list_for_each_entry(stream, &hbus->stream_list, list) {
  268. struct hdac_ext_stream *hstream = container_of(stream,
  269. struct hdac_ext_stream,
  270. hstream);
  271. if (stream->direction != substream->stream)
  272. continue;
  273. if (!stream->opened) {
  274. if (!hstream->decoupled)
  275. snd_hdac_ext_stream_decouple(ebus, hstream, true);
  276. res = hstream;
  277. break;
  278. }
  279. }
  280. if (res) {
  281. spin_lock_irq(&hbus->reg_lock);
  282. res->hstream.opened = 1;
  283. res->hstream.running = 0;
  284. res->hstream.substream = substream;
  285. spin_unlock_irq(&hbus->reg_lock);
  286. }
  287. return res;
  288. }
  289. /**
  290. * snd_hdac_ext_stream_assign - assign a stream for the PCM
  291. * @ebus: HD-audio ext core bus
  292. * @substream: PCM substream to assign
  293. * @type: type of stream (coupled, host or link stream)
  294. *
  295. * This assigns the stream based on the type (coupled/host/link), for the
  296. * given PCM substream, assigns it and returns the stream object
  297. *
  298. * coupled: Looks for an unused stream
  299. * host: Looks for an unused decoupled host stream
  300. * link: Looks for an unused decoupled link stream
  301. *
  302. * If no stream is free, returns NULL. The function tries to keep using
  303. * the same stream object when it's used beforehand. when a stream is
  304. * decoupled, it becomes a host stream and link stream.
  305. */
  306. struct hdac_ext_stream *snd_hdac_ext_stream_assign(struct hdac_ext_bus *ebus,
  307. struct snd_pcm_substream *substream,
  308. int type)
  309. {
  310. struct hdac_ext_stream *hstream = NULL;
  311. struct hdac_stream *stream = NULL;
  312. struct hdac_bus *hbus = &ebus->bus;
  313. switch (type) {
  314. case HDAC_EXT_STREAM_TYPE_COUPLED:
  315. stream = snd_hdac_stream_assign(hbus, substream);
  316. if (stream)
  317. hstream = container_of(stream,
  318. struct hdac_ext_stream, hstream);
  319. return hstream;
  320. case HDAC_EXT_STREAM_TYPE_HOST:
  321. return hdac_ext_host_stream_assign(ebus, substream);
  322. case HDAC_EXT_STREAM_TYPE_LINK:
  323. return hdac_ext_link_stream_assign(ebus, substream);
  324. default:
  325. return NULL;
  326. }
  327. }
  328. EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_assign);
  329. /**
  330. * snd_hdac_ext_stream_release - release the assigned stream
  331. * @stream: HD-audio ext core stream to release
  332. * @type: type of stream (coupled, host or link stream)
  333. *
  334. * Release the stream that has been assigned by snd_hdac_ext_stream_assign().
  335. */
  336. void snd_hdac_ext_stream_release(struct hdac_ext_stream *stream, int type)
  337. {
  338. struct hdac_bus *bus = stream->hstream.bus;
  339. struct hdac_ext_bus *ebus = hbus_to_ebus(bus);
  340. switch (type) {
  341. case HDAC_EXT_STREAM_TYPE_COUPLED:
  342. snd_hdac_stream_release(&stream->hstream);
  343. break;
  344. case HDAC_EXT_STREAM_TYPE_HOST:
  345. if (stream->decoupled && !stream->link_locked)
  346. snd_hdac_ext_stream_decouple(ebus, stream, false);
  347. snd_hdac_stream_release(&stream->hstream);
  348. break;
  349. case HDAC_EXT_STREAM_TYPE_LINK:
  350. if (stream->decoupled && !stream->hstream.opened)
  351. snd_hdac_ext_stream_decouple(ebus, stream, false);
  352. spin_lock_irq(&bus->reg_lock);
  353. stream->link_locked = 0;
  354. 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_stream_spbcap_enable - enable SPIB for a stream
  364. * @ebus: HD-audio ext core bus
  365. * @enable: flag to enable/disable SPIB
  366. * @index: stream index for which SPIB need to be enabled
  367. */
  368. void snd_hdac_ext_stream_spbcap_enable(struct hdac_ext_bus *ebus,
  369. bool enable, int index)
  370. {
  371. u32 mask = 0;
  372. u32 register_mask = 0;
  373. struct hdac_bus *bus = &ebus->bus;
  374. if (!bus->spbcap) {
  375. dev_err(bus->dev, "Address of SPB capability is NULL\n");
  376. return;
  377. }
  378. mask |= (1 << index);
  379. register_mask = readl(bus->spbcap + AZX_REG_SPB_SPBFCCTL);
  380. mask |= register_mask;
  381. if (enable)
  382. snd_hdac_updatel(bus->spbcap, AZX_REG_SPB_SPBFCCTL, 0, mask);
  383. else
  384. snd_hdac_updatel(bus->spbcap, AZX_REG_SPB_SPBFCCTL, mask, 0);
  385. }
  386. EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_spbcap_enable);
  387. /**
  388. * snd_hdac_ext_stream_set_spib - sets the spib value of a stream
  389. * @ebus: HD-audio ext core bus
  390. * @stream: hdac_ext_stream
  391. * @value: spib value to set
  392. */
  393. int snd_hdac_ext_stream_set_spib(struct hdac_ext_bus *ebus,
  394. struct hdac_ext_stream *stream, u32 value)
  395. {
  396. struct hdac_bus *bus = &ebus->bus;
  397. if (!bus->spbcap) {
  398. dev_err(bus->dev, "Address of SPB capability is NULL\n");
  399. return -EINVAL;
  400. }
  401. writel(value, stream->spib_addr);
  402. return 0;
  403. }
  404. EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_set_spib);
  405. /**
  406. * snd_hdac_ext_stream_get_spbmaxfifo - gets the spib value of a stream
  407. * @ebus: HD-audio ext core bus
  408. * @stream: hdac_ext_stream
  409. *
  410. * Return maxfifo for the stream
  411. */
  412. int snd_hdac_ext_stream_get_spbmaxfifo(struct hdac_ext_bus *ebus,
  413. struct hdac_ext_stream *stream)
  414. {
  415. struct hdac_bus *bus = &ebus->bus;
  416. if (!bus->spbcap) {
  417. dev_err(bus->dev, "Address of SPB capability is NULL\n");
  418. return -EINVAL;
  419. }
  420. return readl(stream->fifo_addr);
  421. }
  422. EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_get_spbmaxfifo);
  423. /**
  424. * snd_hdac_ext_stop_streams - stop all stream if running
  425. * @ebus: HD-audio ext core bus
  426. */
  427. void snd_hdac_ext_stop_streams(struct hdac_ext_bus *ebus)
  428. {
  429. struct hdac_bus *bus = ebus_to_hbus(ebus);
  430. struct hdac_stream *stream;
  431. if (bus->chip_init) {
  432. list_for_each_entry(stream, &bus->stream_list, list)
  433. snd_hdac_stream_stop(stream);
  434. snd_hdac_bus_stop_chip(bus);
  435. }
  436. }
  437. EXPORT_SYMBOL_GPL(snd_hdac_ext_stop_streams);
  438. /**
  439. * snd_hdac_ext_stream_drsm_enable - enable DMA resume for a stream
  440. * @ebus: HD-audio ext core bus
  441. * @enable: flag to enable/disable DRSM
  442. * @index: stream index for which DRSM need to be enabled
  443. */
  444. void snd_hdac_ext_stream_drsm_enable(struct hdac_ext_bus *ebus,
  445. bool enable, int index)
  446. {
  447. u32 mask = 0;
  448. u32 register_mask = 0;
  449. struct hdac_bus *bus = &ebus->bus;
  450. if (!bus->drsmcap) {
  451. dev_err(bus->dev, "Address of DRSM capability is NULL\n");
  452. return;
  453. }
  454. mask |= (1 << index);
  455. register_mask = readl(bus->drsmcap + AZX_REG_SPB_SPBFCCTL);
  456. mask |= register_mask;
  457. if (enable)
  458. snd_hdac_updatel(bus->drsmcap, AZX_REG_DRSM_CTL, 0, mask);
  459. else
  460. snd_hdac_updatel(bus->drsmcap, AZX_REG_DRSM_CTL, mask, 0);
  461. }
  462. EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_drsm_enable);
  463. /**
  464. * snd_hdac_ext_stream_set_dpibr - sets the dpibr value of a stream
  465. * @ebus: HD-audio ext core bus
  466. * @stream: hdac_ext_stream
  467. * @value: dpib value to set
  468. */
  469. int snd_hdac_ext_stream_set_dpibr(struct hdac_ext_bus *ebus,
  470. struct hdac_ext_stream *stream, u32 value)
  471. {
  472. struct hdac_bus *bus = &ebus->bus;
  473. if (!bus->drsmcap) {
  474. dev_err(bus->dev, "Address of DRSM capability is NULL\n");
  475. return -EINVAL;
  476. }
  477. writel(value, stream->dpibr_addr);
  478. return 0;
  479. }
  480. EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_set_dpibr);
  481. /**
  482. * snd_hdac_ext_stream_set_lpib - sets the lpib value of a stream
  483. * @ebus: HD-audio ext core bus
  484. * @stream: hdac_ext_stream
  485. * @value: lpib value to set
  486. */
  487. int snd_hdac_ext_stream_set_lpib(struct hdac_ext_stream *stream, u32 value)
  488. {
  489. snd_hdac_stream_writel(&stream->hstream, SD_LPIB, value);
  490. return 0;
  491. }
  492. EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_set_lpib);