hdac_ext_controller.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * hdac-ext-controller.c - HD-audio extended controller functions.
  4. *
  5. * Copyright (C) 2014-2015 Intel Corp
  6. * Author: Jeeja KP <jeeja.kp@intel.com>
  7. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  8. *
  9. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  10. */
  11. #include <linux/delay.h>
  12. #include <linux/slab.h>
  13. #include <sound/hda_register.h>
  14. #include <sound/hdaudio_ext.h>
  15. /*
  16. * processing pipe helpers - these helpers are useful for dealing with HDA
  17. * new capability of processing pipelines
  18. */
  19. /**
  20. * snd_hdac_ext_bus_ppcap_enable - enable/disable processing pipe capability
  21. * @bus: the pointer to HDAC bus object
  22. * @enable: flag to turn on/off the capability
  23. */
  24. void snd_hdac_ext_bus_ppcap_enable(struct hdac_bus *bus, bool enable)
  25. {
  26. if (!bus->ppcap) {
  27. dev_err(bus->dev, "Address of PP capability is NULL");
  28. return;
  29. }
  30. if (enable)
  31. snd_hdac_updatel(bus->ppcap, AZX_REG_PP_PPCTL,
  32. AZX_PPCTL_GPROCEN, AZX_PPCTL_GPROCEN);
  33. else
  34. snd_hdac_updatel(bus->ppcap, AZX_REG_PP_PPCTL,
  35. AZX_PPCTL_GPROCEN, 0);
  36. }
  37. EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_ppcap_enable);
  38. /**
  39. * snd_hdac_ext_bus_ppcap_int_enable - ppcap interrupt enable/disable
  40. * @bus: the pointer to HDAC bus object
  41. * @enable: flag to enable/disable interrupt
  42. */
  43. void snd_hdac_ext_bus_ppcap_int_enable(struct hdac_bus *bus, bool enable)
  44. {
  45. if (!bus->ppcap) {
  46. dev_err(bus->dev, "Address of PP capability is NULL\n");
  47. return;
  48. }
  49. if (enable)
  50. snd_hdac_updatel(bus->ppcap, AZX_REG_PP_PPCTL,
  51. AZX_PPCTL_PIE, AZX_PPCTL_PIE);
  52. else
  53. snd_hdac_updatel(bus->ppcap, AZX_REG_PP_PPCTL,
  54. AZX_PPCTL_PIE, 0);
  55. }
  56. EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_ppcap_int_enable);
  57. /*
  58. * Multilink helpers - these helpers are useful for dealing with HDA
  59. * new multilink capability
  60. */
  61. /**
  62. * snd_hdac_ext_bus_get_ml_capabilities - get multilink capability
  63. * @bus: the pointer to HDAC bus object
  64. *
  65. * This will parse all links and read the mlink capabilities and add them
  66. * in hlink_list of extended hdac bus
  67. * Note: this will be freed on bus exit by driver
  68. */
  69. int snd_hdac_ext_bus_get_ml_capabilities(struct hdac_bus *bus)
  70. {
  71. int idx;
  72. u32 link_count;
  73. struct hdac_ext_link *hlink;
  74. link_count = readl(bus->mlcap + AZX_REG_ML_MLCD) + 1;
  75. dev_dbg(bus->dev, "In %s Link count: %d\n", __func__, link_count);
  76. for (idx = 0; idx < link_count; idx++) {
  77. hlink = kzalloc(sizeof(*hlink), GFP_KERNEL);
  78. if (!hlink)
  79. return -ENOMEM;
  80. hlink->index = idx;
  81. hlink->bus = bus;
  82. hlink->ml_addr = bus->mlcap + AZX_ML_BASE +
  83. (AZX_ML_INTERVAL * idx);
  84. hlink->lcaps = readl(hlink->ml_addr + AZX_REG_ML_LCAP);
  85. hlink->lsdiid = readw(hlink->ml_addr + AZX_REG_ML_LSDIID);
  86. /* since link in On, update the ref */
  87. hlink->ref_count = 1;
  88. list_add_tail(&hlink->list, &bus->hlink_list);
  89. }
  90. return 0;
  91. }
  92. EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_get_ml_capabilities);
  93. /**
  94. * snd_hdac_ext_link_free_all- free hdac extended link objects
  95. *
  96. * @bus: the pointer to HDAC bus object
  97. */
  98. void snd_hdac_ext_link_free_all(struct hdac_bus *bus)
  99. {
  100. struct hdac_ext_link *hlink;
  101. while (!list_empty(&bus->hlink_list)) {
  102. hlink = list_first_entry(&bus->hlink_list, struct hdac_ext_link, list);
  103. list_del(&hlink->list);
  104. kfree(hlink);
  105. }
  106. }
  107. EXPORT_SYMBOL_GPL(snd_hdac_ext_link_free_all);
  108. /**
  109. * snd_hdac_ext_bus_get_hlink_by_addr - get hlink at specified address
  110. * @bus: hlink's parent bus device
  111. * @addr: codec device address
  112. *
  113. * Returns hlink object or NULL if matching hlink is not found.
  114. */
  115. struct hdac_ext_link *snd_hdac_ext_bus_get_hlink_by_addr(struct hdac_bus *bus, int addr)
  116. {
  117. struct hdac_ext_link *hlink;
  118. list_for_each_entry(hlink, &bus->hlink_list, list)
  119. if (hlink->lsdiid & (0x1 << addr))
  120. return hlink;
  121. return NULL;
  122. }
  123. EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_get_hlink_by_addr);
  124. /**
  125. * snd_hdac_ext_bus_get_hlink_by_name - get hlink based on codec name
  126. * @bus: the pointer to HDAC bus object
  127. * @codec_name: codec name
  128. */
  129. struct hdac_ext_link *snd_hdac_ext_bus_get_hlink_by_name(struct hdac_bus *bus,
  130. const char *codec_name)
  131. {
  132. int bus_idx, addr;
  133. if (sscanf(codec_name, "ehdaudio%dD%d", &bus_idx, &addr) != 2)
  134. return NULL;
  135. if (bus->idx != bus_idx)
  136. return NULL;
  137. if (addr < 0 || addr > 31)
  138. return NULL;
  139. return snd_hdac_ext_bus_get_hlink_by_addr(bus, addr);
  140. }
  141. EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_get_hlink_by_name);
  142. static int check_hdac_link_power_active(struct hdac_ext_link *hlink, bool enable)
  143. {
  144. int timeout;
  145. u32 val;
  146. int mask = (1 << AZX_ML_LCTL_CPA_SHIFT);
  147. udelay(3);
  148. timeout = 150;
  149. do {
  150. val = readl(hlink->ml_addr + AZX_REG_ML_LCTL);
  151. if (enable) {
  152. if (((val & mask) >> AZX_ML_LCTL_CPA_SHIFT))
  153. return 0;
  154. } else {
  155. if (!((val & mask) >> AZX_ML_LCTL_CPA_SHIFT))
  156. return 0;
  157. }
  158. udelay(3);
  159. } while (--timeout);
  160. return -EIO;
  161. }
  162. /**
  163. * snd_hdac_ext_bus_link_power_up -power up hda link
  164. * @hlink: HD-audio extended link
  165. */
  166. int snd_hdac_ext_bus_link_power_up(struct hdac_ext_link *hlink)
  167. {
  168. snd_hdac_updatel(hlink->ml_addr, AZX_REG_ML_LCTL,
  169. AZX_ML_LCTL_SPA, AZX_ML_LCTL_SPA);
  170. return check_hdac_link_power_active(hlink, true);
  171. }
  172. EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_power_up);
  173. /**
  174. * snd_hdac_ext_bus_link_power_down -power down hda link
  175. * @hlink: HD-audio extended link
  176. */
  177. int snd_hdac_ext_bus_link_power_down(struct hdac_ext_link *hlink)
  178. {
  179. snd_hdac_updatel(hlink->ml_addr, AZX_REG_ML_LCTL, AZX_ML_LCTL_SPA, 0);
  180. return check_hdac_link_power_active(hlink, false);
  181. }
  182. EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_power_down);
  183. /**
  184. * snd_hdac_ext_bus_link_power_up_all -power up all hda link
  185. * @bus: the pointer to HDAC bus object
  186. */
  187. int snd_hdac_ext_bus_link_power_up_all(struct hdac_bus *bus)
  188. {
  189. struct hdac_ext_link *hlink = NULL;
  190. int ret;
  191. list_for_each_entry(hlink, &bus->hlink_list, list) {
  192. ret = snd_hdac_ext_bus_link_power_up(hlink);
  193. if (ret < 0)
  194. return ret;
  195. }
  196. return 0;
  197. }
  198. EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_power_up_all);
  199. /**
  200. * snd_hdac_ext_bus_link_power_down_all -power down all hda link
  201. * @bus: the pointer to HDAC bus object
  202. */
  203. int snd_hdac_ext_bus_link_power_down_all(struct hdac_bus *bus)
  204. {
  205. struct hdac_ext_link *hlink = NULL;
  206. int ret;
  207. list_for_each_entry(hlink, &bus->hlink_list, list) {
  208. ret = snd_hdac_ext_bus_link_power_down(hlink);
  209. if (ret < 0)
  210. return ret;
  211. }
  212. return 0;
  213. }
  214. EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_power_down_all);
  215. /**
  216. * snd_hdac_ext_bus_link_set_stream_id - maps stream id to link output
  217. * @link: HD-audio ext link to set up
  218. * @stream: stream id
  219. */
  220. void snd_hdac_ext_bus_link_set_stream_id(struct hdac_ext_link *link,
  221. int stream)
  222. {
  223. snd_hdac_updatew(link->ml_addr, AZX_REG_ML_LOSIDV, (1 << stream), 1 << stream);
  224. }
  225. EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_set_stream_id);
  226. /**
  227. * snd_hdac_ext_bus_link_clear_stream_id - maps stream id to link output
  228. * @link: HD-audio ext link to set up
  229. * @stream: stream id
  230. */
  231. void snd_hdac_ext_bus_link_clear_stream_id(struct hdac_ext_link *link,
  232. int stream)
  233. {
  234. snd_hdac_updatew(link->ml_addr, AZX_REG_ML_LOSIDV, (1 << stream), 0);
  235. }
  236. EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_clear_stream_id);
  237. int snd_hdac_ext_bus_link_get(struct hdac_bus *bus,
  238. struct hdac_ext_link *hlink)
  239. {
  240. unsigned long codec_mask;
  241. int ret = 0;
  242. mutex_lock(&bus->lock);
  243. /*
  244. * if we move from 0 to 1, count will be 1 so power up this link
  245. * as well, also check the dma status and trigger that
  246. */
  247. if (++hlink->ref_count == 1) {
  248. if (!bus->cmd_dma_state) {
  249. snd_hdac_bus_init_cmd_io(bus);
  250. bus->cmd_dma_state = true;
  251. }
  252. ret = snd_hdac_ext_bus_link_power_up(hlink);
  253. /*
  254. * clear the register to invalidate all the output streams
  255. */
  256. snd_hdac_updatew(hlink->ml_addr, AZX_REG_ML_LOSIDV,
  257. AZX_ML_LOSIDV_STREAM_MASK, 0);
  258. /*
  259. * wait for 521usec for codec to report status
  260. * HDA spec section 4.3 - Codec Discovery
  261. */
  262. udelay(521);
  263. codec_mask = snd_hdac_chip_readw(bus, STATESTS);
  264. dev_dbg(bus->dev, "codec_mask = 0x%lx\n", codec_mask);
  265. snd_hdac_chip_writew(bus, STATESTS, codec_mask);
  266. if (!bus->codec_mask)
  267. bus->codec_mask = codec_mask;
  268. }
  269. mutex_unlock(&bus->lock);
  270. return ret;
  271. }
  272. EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_get);
  273. int snd_hdac_ext_bus_link_put(struct hdac_bus *bus,
  274. struct hdac_ext_link *hlink)
  275. {
  276. int ret = 0;
  277. struct hdac_ext_link *hlink_tmp;
  278. bool link_up = false;
  279. mutex_lock(&bus->lock);
  280. /*
  281. * if we move from 1 to 0, count will be 0
  282. * so power down this link as well
  283. */
  284. if (--hlink->ref_count == 0) {
  285. ret = snd_hdac_ext_bus_link_power_down(hlink);
  286. /*
  287. * now check if all links are off, if so turn off
  288. * cmd dma as well
  289. */
  290. list_for_each_entry(hlink_tmp, &bus->hlink_list, list) {
  291. if (hlink_tmp->ref_count) {
  292. link_up = true;
  293. break;
  294. }
  295. }
  296. if (!link_up) {
  297. snd_hdac_bus_stop_cmd_io(bus);
  298. bus->cmd_dma_state = false;
  299. }
  300. }
  301. mutex_unlock(&bus->lock);
  302. return ret;
  303. }
  304. EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_put);
  305. static void hdac_ext_codec_link_up(struct hdac_device *codec)
  306. {
  307. const char *devname = dev_name(&codec->dev);
  308. struct hdac_ext_link *hlink =
  309. snd_hdac_ext_bus_get_hlink_by_name(codec->bus, devname);
  310. if (hlink)
  311. snd_hdac_ext_bus_link_get(codec->bus, hlink);
  312. }
  313. static void hdac_ext_codec_link_down(struct hdac_device *codec)
  314. {
  315. const char *devname = dev_name(&codec->dev);
  316. struct hdac_ext_link *hlink =
  317. snd_hdac_ext_bus_get_hlink_by_name(codec->bus, devname);
  318. if (hlink)
  319. snd_hdac_ext_bus_link_put(codec->bus, hlink);
  320. }
  321. void snd_hdac_ext_bus_link_power(struct hdac_device *codec, bool enable)
  322. {
  323. struct hdac_bus *bus = codec->bus;
  324. bool oldstate = test_bit(codec->addr, &bus->codec_powered);
  325. if (enable == oldstate)
  326. return;
  327. snd_hdac_bus_link_power(codec, enable);
  328. if (enable)
  329. hdac_ext_codec_link_up(codec);
  330. else
  331. hdac_ext_codec_link_down(codec);
  332. }
  333. EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_power);