hdac_ext_controller.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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 <[email protected]>
  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_link_free_all- free hdac extended link objects
  95. *
  96. * @bus: the pointer to HDAC bus object
  97. */
  98. void snd_hdac_link_free_all(struct hdac_bus *bus)
  99. {
  100. struct hdac_ext_link *l;
  101. while (!list_empty(&bus->hlink_list)) {
  102. l = list_first_entry(&bus->hlink_list, struct hdac_ext_link, list);
  103. list_del(&l->list);
  104. kfree(l);
  105. }
  106. }
  107. EXPORT_SYMBOL_GPL(snd_hdac_link_free_all);
  108. /**
  109. * snd_hdac_ext_bus_link_at - get link at specified address
  110. * @bus: link's parent bus device
  111. * @addr: codec device address
  112. *
  113. * Returns link object or NULL if matching link is not found.
  114. */
  115. struct hdac_ext_link *snd_hdac_ext_bus_link_at(struct hdac_bus *bus, int addr)
  116. {
  117. struct hdac_ext_link *hlink;
  118. int i;
  119. list_for_each_entry(hlink, &bus->hlink_list, list)
  120. for (i = 0; i < HDA_MAX_CODECS; i++)
  121. if (hlink->lsdiid & (0x1 << addr))
  122. return hlink;
  123. return NULL;
  124. }
  125. EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_at);
  126. /**
  127. * snd_hdac_ext_bus_get_link - get link based on codec name
  128. * @bus: the pointer to HDAC bus object
  129. * @codec_name: codec name
  130. */
  131. struct hdac_ext_link *snd_hdac_ext_bus_get_link(struct hdac_bus *bus,
  132. const char *codec_name)
  133. {
  134. int bus_idx, addr;
  135. if (sscanf(codec_name, "ehdaudio%dD%d", &bus_idx, &addr) != 2)
  136. return NULL;
  137. if (bus->idx != bus_idx)
  138. return NULL;
  139. if (addr < 0 || addr > 31)
  140. return NULL;
  141. return snd_hdac_ext_bus_link_at(bus, addr);
  142. }
  143. EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_get_link);
  144. static int check_hdac_link_power_active(struct hdac_ext_link *link, bool enable)
  145. {
  146. int timeout;
  147. u32 val;
  148. int mask = (1 << AZX_ML_LCTL_CPA_SHIFT);
  149. udelay(3);
  150. timeout = 150;
  151. do {
  152. val = readl(link->ml_addr + AZX_REG_ML_LCTL);
  153. if (enable) {
  154. if (((val & mask) >> AZX_ML_LCTL_CPA_SHIFT))
  155. return 0;
  156. } else {
  157. if (!((val & mask) >> AZX_ML_LCTL_CPA_SHIFT))
  158. return 0;
  159. }
  160. udelay(3);
  161. } while (--timeout);
  162. return -EIO;
  163. }
  164. /**
  165. * snd_hdac_ext_bus_link_power_up -power up hda link
  166. * @link: HD-audio extended link
  167. */
  168. int snd_hdac_ext_bus_link_power_up(struct hdac_ext_link *link)
  169. {
  170. snd_hdac_updatel(link->ml_addr, AZX_REG_ML_LCTL,
  171. AZX_ML_LCTL_SPA, AZX_ML_LCTL_SPA);
  172. return check_hdac_link_power_active(link, true);
  173. }
  174. EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_power_up);
  175. /**
  176. * snd_hdac_ext_bus_link_power_down -power down hda link
  177. * @link: HD-audio extended link
  178. */
  179. int snd_hdac_ext_bus_link_power_down(struct hdac_ext_link *link)
  180. {
  181. snd_hdac_updatel(link->ml_addr, AZX_REG_ML_LCTL, AZX_ML_LCTL_SPA, 0);
  182. return check_hdac_link_power_active(link, false);
  183. }
  184. EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_power_down);
  185. /**
  186. * snd_hdac_ext_bus_link_power_up_all -power up all hda link
  187. * @bus: the pointer to HDAC bus object
  188. */
  189. int snd_hdac_ext_bus_link_power_up_all(struct hdac_bus *bus)
  190. {
  191. struct hdac_ext_link *hlink = NULL;
  192. int ret;
  193. list_for_each_entry(hlink, &bus->hlink_list, list) {
  194. snd_hdac_updatel(hlink->ml_addr, AZX_REG_ML_LCTL,
  195. AZX_ML_LCTL_SPA, AZX_ML_LCTL_SPA);
  196. ret = check_hdac_link_power_active(hlink, true);
  197. if (ret < 0)
  198. return ret;
  199. }
  200. return 0;
  201. }
  202. EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_power_up_all);
  203. /**
  204. * snd_hdac_ext_bus_link_power_down_all -power down all hda link
  205. * @bus: the pointer to HDAC bus object
  206. */
  207. int snd_hdac_ext_bus_link_power_down_all(struct hdac_bus *bus)
  208. {
  209. struct hdac_ext_link *hlink = NULL;
  210. int ret;
  211. list_for_each_entry(hlink, &bus->hlink_list, list) {
  212. snd_hdac_updatel(hlink->ml_addr, AZX_REG_ML_LCTL,
  213. AZX_ML_LCTL_SPA, 0);
  214. ret = check_hdac_link_power_active(hlink, false);
  215. if (ret < 0)
  216. return ret;
  217. }
  218. return 0;
  219. }
  220. EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_power_down_all);
  221. int snd_hdac_ext_bus_link_get(struct hdac_bus *bus,
  222. struct hdac_ext_link *link)
  223. {
  224. unsigned long codec_mask;
  225. int ret = 0;
  226. mutex_lock(&bus->lock);
  227. /*
  228. * if we move from 0 to 1, count will be 1 so power up this link
  229. * as well, also check the dma status and trigger that
  230. */
  231. if (++link->ref_count == 1) {
  232. if (!bus->cmd_dma_state) {
  233. snd_hdac_bus_init_cmd_io(bus);
  234. bus->cmd_dma_state = true;
  235. }
  236. ret = snd_hdac_ext_bus_link_power_up(link);
  237. /*
  238. * clear the register to invalidate all the output streams
  239. */
  240. snd_hdac_updatew(link->ml_addr, AZX_REG_ML_LOSIDV,
  241. AZX_ML_LOSIDV_STREAM_MASK, 0);
  242. /*
  243. * wait for 521usec for codec to report status
  244. * HDA spec section 4.3 - Codec Discovery
  245. */
  246. udelay(521);
  247. codec_mask = snd_hdac_chip_readw(bus, STATESTS);
  248. dev_dbg(bus->dev, "codec_mask = 0x%lx\n", codec_mask);
  249. snd_hdac_chip_writew(bus, STATESTS, codec_mask);
  250. if (!bus->codec_mask)
  251. bus->codec_mask = codec_mask;
  252. }
  253. mutex_unlock(&bus->lock);
  254. return ret;
  255. }
  256. EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_get);
  257. int snd_hdac_ext_bus_link_put(struct hdac_bus *bus,
  258. struct hdac_ext_link *link)
  259. {
  260. int ret = 0;
  261. struct hdac_ext_link *hlink;
  262. bool link_up = false;
  263. mutex_lock(&bus->lock);
  264. /*
  265. * if we move from 1 to 0, count will be 0
  266. * so power down this link as well
  267. */
  268. if (--link->ref_count == 0) {
  269. ret = snd_hdac_ext_bus_link_power_down(link);
  270. /*
  271. * now check if all links are off, if so turn off
  272. * cmd dma as well
  273. */
  274. list_for_each_entry(hlink, &bus->hlink_list, list) {
  275. if (hlink->ref_count) {
  276. link_up = true;
  277. break;
  278. }
  279. }
  280. if (!link_up) {
  281. snd_hdac_bus_stop_cmd_io(bus);
  282. bus->cmd_dma_state = false;
  283. }
  284. }
  285. mutex_unlock(&bus->lock);
  286. return ret;
  287. }
  288. EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_put);
  289. static void hdac_ext_codec_link_up(struct hdac_device *codec)
  290. {
  291. const char *devname = dev_name(&codec->dev);
  292. struct hdac_ext_link *hlink =
  293. snd_hdac_ext_bus_get_link(codec->bus, devname);
  294. if (hlink)
  295. snd_hdac_ext_bus_link_get(codec->bus, hlink);
  296. }
  297. static void hdac_ext_codec_link_down(struct hdac_device *codec)
  298. {
  299. const char *devname = dev_name(&codec->dev);
  300. struct hdac_ext_link *hlink =
  301. snd_hdac_ext_bus_get_link(codec->bus, devname);
  302. if (hlink)
  303. snd_hdac_ext_bus_link_put(codec->bus, hlink);
  304. }
  305. void snd_hdac_ext_bus_link_power(struct hdac_device *codec, bool enable)
  306. {
  307. struct hdac_bus *bus = codec->bus;
  308. bool oldstate = test_bit(codec->addr, &bus->codec_powered);
  309. if (enable == oldstate)
  310. return;
  311. snd_hdac_bus_link_power(codec, enable);
  312. if (enable)
  313. hdac_ext_codec_link_up(codec);
  314. else
  315. hdac_ext_codec_link_down(codec);
  316. }
  317. EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_power);