hdac_ext_stream.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  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. u32 val;
  117. int mask = AZX_PPCTL_PROCEN(hstream->index);
  118. spin_lock_irq(&bus->reg_lock);
  119. val = readw(bus->ppcap + AZX_REG_PP_PPCTL) & mask;
  120. if (decouple && !val)
  121. snd_hdac_updatel(bus->ppcap, AZX_REG_PP_PPCTL, mask, mask);
  122. else if (!decouple && val)
  123. snd_hdac_updatel(bus->ppcap, AZX_REG_PP_PPCTL, mask, 0);
  124. stream->decoupled = decouple;
  125. spin_unlock_irq(&bus->reg_lock);
  126. }
  127. EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_decouple);
  128. /**
  129. * snd_hdac_ext_linkstream_start - start a stream
  130. * @stream: HD-audio ext core stream to start
  131. */
  132. void snd_hdac_ext_link_stream_start(struct hdac_ext_stream *stream)
  133. {
  134. snd_hdac_updatel(stream->pplc_addr, AZX_REG_PPLCCTL, 0, AZX_PPLCCTL_RUN);
  135. }
  136. EXPORT_SYMBOL_GPL(snd_hdac_ext_link_stream_start);
  137. /**
  138. * snd_hdac_ext_link_stream_clear - stop a stream DMA
  139. * @stream: HD-audio ext core stream to stop
  140. */
  141. void snd_hdac_ext_link_stream_clear(struct hdac_ext_stream *stream)
  142. {
  143. snd_hdac_updatel(stream->pplc_addr, AZX_REG_PPLCCTL, AZX_PPLCCTL_RUN, 0);
  144. }
  145. EXPORT_SYMBOL_GPL(snd_hdac_ext_link_stream_clear);
  146. /**
  147. * snd_hdac_ext_link_stream_reset - reset a stream
  148. * @stream: HD-audio ext core stream to reset
  149. */
  150. void snd_hdac_ext_link_stream_reset(struct hdac_ext_stream *stream)
  151. {
  152. unsigned char val;
  153. int timeout;
  154. snd_hdac_ext_link_stream_clear(stream);
  155. snd_hdac_updatel(stream->pplc_addr, AZX_REG_PPLCCTL, 0, AZX_PPLCCTL_STRST);
  156. udelay(3);
  157. timeout = 50;
  158. do {
  159. val = readl(stream->pplc_addr + AZX_REG_PPLCCTL) &
  160. AZX_PPLCCTL_STRST;
  161. if (val)
  162. break;
  163. udelay(3);
  164. } while (--timeout);
  165. val &= ~AZX_PPLCCTL_STRST;
  166. writel(val, stream->pplc_addr + AZX_REG_PPLCCTL);
  167. udelay(3);
  168. timeout = 50;
  169. /* waiting for hardware to report that the stream is out of reset */
  170. do {
  171. val = readl(stream->pplc_addr + AZX_REG_PPLCCTL) & AZX_PPLCCTL_STRST;
  172. if (!val)
  173. break;
  174. udelay(3);
  175. } while (--timeout);
  176. }
  177. EXPORT_SYMBOL_GPL(snd_hdac_ext_link_stream_reset);
  178. /**
  179. * snd_hdac_ext_link_stream_setup - set up the SD for streaming
  180. * @stream: HD-audio ext core stream to set up
  181. * @fmt: stream format
  182. */
  183. int snd_hdac_ext_link_stream_setup(struct hdac_ext_stream *stream, int fmt)
  184. {
  185. struct hdac_stream *hstream = &stream->hstream;
  186. unsigned int val;
  187. /* make sure the run bit is zero for SD */
  188. snd_hdac_ext_link_stream_clear(stream);
  189. /* program the stream_tag */
  190. val = readl(stream->pplc_addr + AZX_REG_PPLCCTL);
  191. val = (val & ~AZX_PPLCCTL_STRM_MASK) |
  192. (hstream->stream_tag << AZX_PPLCCTL_STRM_SHIFT);
  193. writel(val, stream->pplc_addr + AZX_REG_PPLCCTL);
  194. /* program the stream format */
  195. writew(fmt, stream->pplc_addr + AZX_REG_PPLCFMT);
  196. return 0;
  197. }
  198. EXPORT_SYMBOL_GPL(snd_hdac_ext_link_stream_setup);
  199. /**
  200. * snd_hdac_ext_link_set_stream_id - maps stream id to link output
  201. * @link: HD-audio ext link to set up
  202. * @stream: stream id
  203. */
  204. void snd_hdac_ext_link_set_stream_id(struct hdac_ext_link *link,
  205. int stream)
  206. {
  207. snd_hdac_updatew(link->ml_addr, AZX_REG_ML_LOSIDV, (1 << stream), 1 << stream);
  208. }
  209. EXPORT_SYMBOL_GPL(snd_hdac_ext_link_set_stream_id);
  210. /**
  211. * snd_hdac_ext_link_clear_stream_id - maps stream id to link output
  212. * @link: HD-audio ext link to set up
  213. * @stream: stream id
  214. */
  215. void snd_hdac_ext_link_clear_stream_id(struct hdac_ext_link *link,
  216. int stream)
  217. {
  218. snd_hdac_updatew(link->ml_addr, AZX_REG_ML_LOSIDV, 0, (1 << stream));
  219. }
  220. EXPORT_SYMBOL_GPL(snd_hdac_ext_link_clear_stream_id);
  221. static struct hdac_ext_stream *
  222. hdac_ext_link_stream_assign(struct hdac_ext_bus *ebus,
  223. struct snd_pcm_substream *substream)
  224. {
  225. struct hdac_ext_stream *res = NULL;
  226. struct hdac_stream *stream = NULL;
  227. struct hdac_bus *hbus = &ebus->bus;
  228. if (!hbus->ppcap) {
  229. dev_err(hbus->dev, "stream type not supported\n");
  230. return NULL;
  231. }
  232. list_for_each_entry(stream, &hbus->stream_list, list) {
  233. struct hdac_ext_stream *hstream = container_of(stream,
  234. struct hdac_ext_stream,
  235. hstream);
  236. if (stream->direction != substream->stream)
  237. continue;
  238. /* check if decoupled stream and not in use is available */
  239. if (hstream->decoupled && !hstream->link_locked) {
  240. res = hstream;
  241. break;
  242. }
  243. if (!hstream->link_locked) {
  244. snd_hdac_ext_stream_decouple(ebus, hstream, true);
  245. res = hstream;
  246. break;
  247. }
  248. }
  249. if (res) {
  250. spin_lock_irq(&hbus->reg_lock);
  251. res->link_locked = 1;
  252. res->link_substream = substream;
  253. spin_unlock_irq(&hbus->reg_lock);
  254. }
  255. return res;
  256. }
  257. static struct hdac_ext_stream *
  258. hdac_ext_host_stream_assign(struct hdac_ext_bus *ebus,
  259. struct snd_pcm_substream *substream)
  260. {
  261. struct hdac_ext_stream *res = NULL;
  262. struct hdac_stream *stream = NULL;
  263. struct hdac_bus *hbus = &ebus->bus;
  264. if (!hbus->ppcap) {
  265. dev_err(hbus->dev, "stream type not supported\n");
  266. return NULL;
  267. }
  268. list_for_each_entry(stream, &hbus->stream_list, list) {
  269. struct hdac_ext_stream *hstream = container_of(stream,
  270. struct hdac_ext_stream,
  271. hstream);
  272. if (stream->direction != substream->stream)
  273. continue;
  274. if (!stream->opened) {
  275. if (!hstream->decoupled)
  276. snd_hdac_ext_stream_decouple(ebus, hstream, true);
  277. res = hstream;
  278. break;
  279. }
  280. }
  281. if (res) {
  282. spin_lock_irq(&hbus->reg_lock);
  283. res->hstream.opened = 1;
  284. res->hstream.running = 0;
  285. res->hstream.substream = substream;
  286. spin_unlock_irq(&hbus->reg_lock);
  287. }
  288. return res;
  289. }
  290. /**
  291. * snd_hdac_ext_stream_assign - assign a stream for the PCM
  292. * @ebus: HD-audio ext core bus
  293. * @substream: PCM substream to assign
  294. * @type: type of stream (coupled, host or link stream)
  295. *
  296. * This assigns the stream based on the type (coupled/host/link), for the
  297. * given PCM substream, assigns it and returns the stream object
  298. *
  299. * coupled: Looks for an unused stream
  300. * host: Looks for an unused decoupled host stream
  301. * link: Looks for an unused decoupled link stream
  302. *
  303. * If no stream is free, returns NULL. The function tries to keep using
  304. * the same stream object when it's used beforehand. when a stream is
  305. * decoupled, it becomes a host stream and link stream.
  306. */
  307. struct hdac_ext_stream *snd_hdac_ext_stream_assign(struct hdac_ext_bus *ebus,
  308. struct snd_pcm_substream *substream,
  309. int type)
  310. {
  311. struct hdac_ext_stream *hstream = NULL;
  312. struct hdac_stream *stream = NULL;
  313. struct hdac_bus *hbus = &ebus->bus;
  314. switch (type) {
  315. case HDAC_EXT_STREAM_TYPE_COUPLED:
  316. stream = snd_hdac_stream_assign(hbus, substream);
  317. if (stream)
  318. hstream = container_of(stream,
  319. struct hdac_ext_stream, hstream);
  320. return hstream;
  321. case HDAC_EXT_STREAM_TYPE_HOST:
  322. return hdac_ext_host_stream_assign(ebus, substream);
  323. case HDAC_EXT_STREAM_TYPE_LINK:
  324. return hdac_ext_link_stream_assign(ebus, substream);
  325. default:
  326. return NULL;
  327. }
  328. }
  329. EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_assign);
  330. /**
  331. * snd_hdac_ext_stream_release - release the assigned stream
  332. * @stream: HD-audio ext core stream to release
  333. * @type: type of stream (coupled, host or link stream)
  334. *
  335. * Release the stream that has been assigned by snd_hdac_ext_stream_assign().
  336. */
  337. void snd_hdac_ext_stream_release(struct hdac_ext_stream *stream, int type)
  338. {
  339. struct hdac_bus *bus = stream->hstream.bus;
  340. struct hdac_ext_bus *ebus = hbus_to_ebus(bus);
  341. switch (type) {
  342. case HDAC_EXT_STREAM_TYPE_COUPLED:
  343. snd_hdac_stream_release(&stream->hstream);
  344. break;
  345. case HDAC_EXT_STREAM_TYPE_HOST:
  346. if (stream->decoupled && !stream->link_locked)
  347. snd_hdac_ext_stream_decouple(ebus, stream, false);
  348. snd_hdac_stream_release(&stream->hstream);
  349. break;
  350. case HDAC_EXT_STREAM_TYPE_LINK:
  351. if (stream->decoupled && !stream->hstream.opened)
  352. snd_hdac_ext_stream_decouple(ebus, stream, false);
  353. spin_lock_irq(&bus->reg_lock);
  354. stream->link_locked = 0;
  355. stream->link_substream = NULL;
  356. spin_unlock_irq(&bus->reg_lock);
  357. break;
  358. default:
  359. dev_dbg(bus->dev, "Invalid type %d\n", type);
  360. }
  361. }
  362. EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_release);
  363. /**
  364. * snd_hdac_ext_stream_spbcap_enable - enable SPIB for a stream
  365. * @ebus: HD-audio ext core bus
  366. * @enable: flag to enable/disable SPIB
  367. * @index: stream index for which SPIB need to be enabled
  368. */
  369. void snd_hdac_ext_stream_spbcap_enable(struct hdac_ext_bus *ebus,
  370. bool enable, int index)
  371. {
  372. u32 mask = 0;
  373. u32 register_mask = 0;
  374. struct hdac_bus *bus = &ebus->bus;
  375. if (!bus->spbcap) {
  376. dev_err(bus->dev, "Address of SPB capability is NULL\n");
  377. return;
  378. }
  379. mask |= (1 << index);
  380. register_mask = readl(bus->spbcap + AZX_REG_SPB_SPBFCCTL);
  381. mask |= register_mask;
  382. if (enable)
  383. snd_hdac_updatel(bus->spbcap, AZX_REG_SPB_SPBFCCTL, 0, mask);
  384. else
  385. snd_hdac_updatel(bus->spbcap, AZX_REG_SPB_SPBFCCTL, mask, 0);
  386. }
  387. EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_spbcap_enable);
  388. /**
  389. * snd_hdac_ext_stream_set_spib - sets the spib value of a stream
  390. * @ebus: HD-audio ext core bus
  391. * @stream: hdac_ext_stream
  392. * @value: spib value to set
  393. */
  394. int snd_hdac_ext_stream_set_spib(struct hdac_ext_bus *ebus,
  395. struct hdac_ext_stream *stream, u32 value)
  396. {
  397. struct hdac_bus *bus = &ebus->bus;
  398. if (!bus->spbcap) {
  399. dev_err(bus->dev, "Address of SPB capability is NULL\n");
  400. return -EINVAL;
  401. }
  402. writel(value, stream->spib_addr);
  403. return 0;
  404. }
  405. EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_set_spib);
  406. /**
  407. * snd_hdac_ext_stream_get_spbmaxfifo - gets the spib value of a stream
  408. * @ebus: HD-audio ext core bus
  409. * @stream: hdac_ext_stream
  410. *
  411. * Return maxfifo for the stream
  412. */
  413. int snd_hdac_ext_stream_get_spbmaxfifo(struct hdac_ext_bus *ebus,
  414. struct hdac_ext_stream *stream)
  415. {
  416. struct hdac_bus *bus = &ebus->bus;
  417. if (!bus->spbcap) {
  418. dev_err(bus->dev, "Address of SPB capability is NULL\n");
  419. return -EINVAL;
  420. }
  421. return readl(stream->fifo_addr);
  422. }
  423. EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_get_spbmaxfifo);
  424. /**
  425. * snd_hdac_ext_stop_streams - stop all stream if running
  426. * @ebus: HD-audio ext core bus
  427. */
  428. void snd_hdac_ext_stop_streams(struct hdac_ext_bus *ebus)
  429. {
  430. struct hdac_bus *bus = ebus_to_hbus(ebus);
  431. struct hdac_stream *stream;
  432. if (bus->chip_init) {
  433. list_for_each_entry(stream, &bus->stream_list, list)
  434. snd_hdac_stream_stop(stream);
  435. snd_hdac_bus_stop_chip(bus);
  436. }
  437. }
  438. EXPORT_SYMBOL_GPL(snd_hdac_ext_stop_streams);
  439. /**
  440. * snd_hdac_ext_stream_drsm_enable - enable DMA resume for a stream
  441. * @ebus: HD-audio ext core bus
  442. * @enable: flag to enable/disable DRSM
  443. * @index: stream index for which DRSM need to be enabled
  444. */
  445. void snd_hdac_ext_stream_drsm_enable(struct hdac_ext_bus *ebus,
  446. bool enable, int index)
  447. {
  448. u32 mask = 0;
  449. u32 register_mask = 0;
  450. struct hdac_bus *bus = &ebus->bus;
  451. if (!bus->drsmcap) {
  452. dev_err(bus->dev, "Address of DRSM capability is NULL\n");
  453. return;
  454. }
  455. mask |= (1 << index);
  456. register_mask = readl(bus->drsmcap + AZX_REG_SPB_SPBFCCTL);
  457. mask |= register_mask;
  458. if (enable)
  459. snd_hdac_updatel(bus->drsmcap, AZX_REG_DRSM_CTL, 0, mask);
  460. else
  461. snd_hdac_updatel(bus->drsmcap, AZX_REG_DRSM_CTL, mask, 0);
  462. }
  463. EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_drsm_enable);
  464. /**
  465. * snd_hdac_ext_stream_set_dpibr - sets the dpibr value of a stream
  466. * @ebus: HD-audio ext core bus
  467. * @stream: hdac_ext_stream
  468. * @value: dpib value to set
  469. */
  470. int snd_hdac_ext_stream_set_dpibr(struct hdac_ext_bus *ebus,
  471. struct hdac_ext_stream *stream, u32 value)
  472. {
  473. struct hdac_bus *bus = &ebus->bus;
  474. if (!bus->drsmcap) {
  475. dev_err(bus->dev, "Address of DRSM capability is NULL\n");
  476. return -EINVAL;
  477. }
  478. writel(value, stream->dpibr_addr);
  479. return 0;
  480. }
  481. EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_set_dpibr);
  482. /**
  483. * snd_hdac_ext_stream_set_lpib - sets the lpib value of a stream
  484. * @ebus: HD-audio ext core bus
  485. * @stream: hdac_ext_stream
  486. * @value: lpib value to set
  487. */
  488. int snd_hdac_ext_stream_set_lpib(struct hdac_ext_stream *stream, u32 value)
  489. {
  490. snd_hdac_stream_writel(&stream->hstream, SD_LPIB, value);
  491. return 0;
  492. }
  493. EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_set_lpib);