hdac_ext_controller.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /*
  2. * hdac-ext-controller.c - HD-audio extended controller functions.
  3. *
  4. * Copyright (C) 2014-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/hda_register.h>
  22. #include <sound/hdaudio_ext.h>
  23. /*
  24. * maximum HDAC capablities we should parse to avoid endless looping:
  25. * currently we have 4 extended caps, so this is future proof for now.
  26. * extend when this limit is seen meeting in real HW
  27. */
  28. #define HDAC_MAX_CAPS 10
  29. /**
  30. * snd_hdac_ext_bus_parse_capabilities - parse capablity structure
  31. * @ebus: the pointer to extended bus object
  32. *
  33. * Returns 0 if successful, or a negative error code.
  34. */
  35. int snd_hdac_ext_bus_parse_capabilities(struct hdac_ext_bus *ebus)
  36. {
  37. unsigned int cur_cap;
  38. unsigned int offset;
  39. struct hdac_bus *bus = &ebus->bus;
  40. unsigned int counter = 0;
  41. offset = snd_hdac_chip_readl(bus, LLCH);
  42. /* Lets walk the linked capabilities list */
  43. do {
  44. cur_cap = _snd_hdac_chip_read(l, bus, offset);
  45. dev_dbg(bus->dev, "Capability version: 0x%x\n",
  46. ((cur_cap & AZX_CAP_HDR_VER_MASK) >> AZX_CAP_HDR_VER_OFF));
  47. dev_dbg(bus->dev, "HDA capability ID: 0x%x\n",
  48. (cur_cap & AZX_CAP_HDR_ID_MASK) >> AZX_CAP_HDR_ID_OFF);
  49. switch ((cur_cap & AZX_CAP_HDR_ID_MASK) >> AZX_CAP_HDR_ID_OFF) {
  50. case AZX_ML_CAP_ID:
  51. dev_dbg(bus->dev, "Found ML capability\n");
  52. ebus->mlcap = bus->remap_addr + offset;
  53. break;
  54. case AZX_GTS_CAP_ID:
  55. dev_dbg(bus->dev, "Found GTS capability offset=%x\n", offset);
  56. ebus->gtscap = bus->remap_addr + offset;
  57. break;
  58. case AZX_PP_CAP_ID:
  59. /* PP capability found, the Audio DSP is present */
  60. dev_dbg(bus->dev, "Found PP capability offset=%x\n", offset);
  61. ebus->ppcap = bus->remap_addr + offset;
  62. break;
  63. case AZX_SPB_CAP_ID:
  64. /* SPIB capability found, handler function */
  65. dev_dbg(bus->dev, "Found SPB capability\n");
  66. ebus->spbcap = bus->remap_addr + offset;
  67. break;
  68. case AZX_DRSM_CAP_ID:
  69. /* DMA resume capability found, handler function */
  70. dev_dbg(bus->dev, "Found DRSM capability\n");
  71. ebus->drsmcap = bus->remap_addr + offset;
  72. break;
  73. default:
  74. dev_dbg(bus->dev, "Unknown capability %d\n", cur_cap);
  75. break;
  76. }
  77. counter++;
  78. if (counter > HDAC_MAX_CAPS) {
  79. dev_err(bus->dev, "We exceeded HDAC Ext capablities!!!\n");
  80. break;
  81. }
  82. /* read the offset of next capabiity */
  83. offset = cur_cap & AZX_CAP_HDR_NXT_PTR_MASK;
  84. } while (offset);
  85. return 0;
  86. }
  87. EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_parse_capabilities);
  88. /*
  89. * processing pipe helpers - these helpers are useful for dealing with HDA
  90. * new capability of processing pipelines
  91. */
  92. /**
  93. * snd_hdac_ext_bus_ppcap_enable - enable/disable processing pipe capability
  94. * @ebus: HD-audio extended core bus
  95. * @enable: flag to turn on/off the capability
  96. */
  97. void snd_hdac_ext_bus_ppcap_enable(struct hdac_ext_bus *ebus, bool enable)
  98. {
  99. struct hdac_bus *bus = &ebus->bus;
  100. if (!ebus->ppcap) {
  101. dev_err(bus->dev, "Address of PP capability is NULL");
  102. return;
  103. }
  104. if (enable)
  105. snd_hdac_updatel(ebus->ppcap, AZX_REG_PP_PPCTL, 0, AZX_PPCTL_GPROCEN);
  106. else
  107. snd_hdac_updatel(ebus->ppcap, AZX_REG_PP_PPCTL, AZX_PPCTL_GPROCEN, 0);
  108. }
  109. EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_ppcap_enable);
  110. /**
  111. * snd_hdac_ext_bus_ppcap_int_enable - ppcap interrupt enable/disable
  112. * @ebus: HD-audio extended core bus
  113. * @enable: flag to enable/disable interrupt
  114. */
  115. void snd_hdac_ext_bus_ppcap_int_enable(struct hdac_ext_bus *ebus, bool enable)
  116. {
  117. struct hdac_bus *bus = &ebus->bus;
  118. if (!ebus->ppcap) {
  119. dev_err(bus->dev, "Address of PP capability is NULL\n");
  120. return;
  121. }
  122. if (enable)
  123. snd_hdac_updatel(ebus->ppcap, AZX_REG_PP_PPCTL, 0, AZX_PPCTL_PIE);
  124. else
  125. snd_hdac_updatel(ebus->ppcap, AZX_REG_PP_PPCTL, AZX_PPCTL_PIE, 0);
  126. }
  127. EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_ppcap_int_enable);
  128. /*
  129. * Multilink helpers - these helpers are useful for dealing with HDA
  130. * new multilink capability
  131. */
  132. /**
  133. * snd_hdac_ext_bus_get_ml_capabilities - get multilink capability
  134. * @ebus: HD-audio extended core bus
  135. *
  136. * This will parse all links and read the mlink capabilities and add them
  137. * in hlink_list of extended hdac bus
  138. * Note: this will be freed on bus exit by driver
  139. */
  140. int snd_hdac_ext_bus_get_ml_capabilities(struct hdac_ext_bus *ebus)
  141. {
  142. int idx;
  143. u32 link_count;
  144. struct hdac_ext_link *hlink;
  145. struct hdac_bus *bus = &ebus->bus;
  146. link_count = readl(ebus->mlcap + AZX_REG_ML_MLCD) + 1;
  147. dev_dbg(bus->dev, "In %s Link count: %d\n", __func__, link_count);
  148. for (idx = 0; idx < link_count; idx++) {
  149. hlink = kzalloc(sizeof(*hlink), GFP_KERNEL);
  150. if (!hlink)
  151. return -ENOMEM;
  152. hlink->index = idx;
  153. hlink->bus = bus;
  154. hlink->ml_addr = ebus->mlcap + AZX_ML_BASE +
  155. (AZX_ML_INTERVAL * idx);
  156. hlink->lcaps = readl(hlink->ml_addr + AZX_REG_ML_LCAP);
  157. hlink->lsdiid = readw(hlink->ml_addr + AZX_REG_ML_LSDIID);
  158. list_add_tail(&hlink->list, &ebus->hlink_list);
  159. }
  160. return 0;
  161. }
  162. EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_get_ml_capabilities);
  163. /**
  164. * snd_hdac_link_free_all- free hdac extended link objects
  165. *
  166. * @ebus: HD-audio ext core bus
  167. */
  168. void snd_hdac_link_free_all(struct hdac_ext_bus *ebus)
  169. {
  170. struct hdac_ext_link *l;
  171. while (!list_empty(&ebus->hlink_list)) {
  172. l = list_first_entry(&ebus->hlink_list, struct hdac_ext_link, list);
  173. list_del(&l->list);
  174. kfree(l);
  175. }
  176. }
  177. EXPORT_SYMBOL_GPL(snd_hdac_link_free_all);
  178. /**
  179. * snd_hdac_ext_bus_get_link_index - get link based on codec name
  180. * @ebus: HD-audio extended core bus
  181. * @codec_name: codec name
  182. */
  183. struct hdac_ext_link *snd_hdac_ext_bus_get_link(struct hdac_ext_bus *ebus,
  184. const char *codec_name)
  185. {
  186. int i;
  187. struct hdac_ext_link *hlink = NULL;
  188. int bus_idx, addr;
  189. if (sscanf(codec_name, "ehdaudio%dD%d", &bus_idx, &addr) != 2)
  190. return NULL;
  191. if (ebus->idx != bus_idx)
  192. return NULL;
  193. list_for_each_entry(hlink, &ebus->hlink_list, list) {
  194. for (i = 0; i < HDA_MAX_CODECS; i++) {
  195. if (hlink->lsdiid & (0x1 << addr))
  196. return hlink;
  197. }
  198. }
  199. return NULL;
  200. }
  201. EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_get_link);
  202. static int check_hdac_link_power_active(struct hdac_ext_link *link, bool enable)
  203. {
  204. int timeout;
  205. u32 val;
  206. int mask = (1 << AZX_MLCTL_CPA);
  207. udelay(3);
  208. timeout = 150;
  209. do {
  210. val = readl(link->ml_addr + AZX_REG_ML_LCTL);
  211. if (enable) {
  212. if (((val & mask) >> AZX_MLCTL_CPA))
  213. return 0;
  214. } else {
  215. if (!((val & mask) >> AZX_MLCTL_CPA))
  216. return 0;
  217. }
  218. udelay(3);
  219. } while (--timeout);
  220. return -EIO;
  221. }
  222. /**
  223. * snd_hdac_ext_bus_link_power_up -power up hda link
  224. * @link: HD-audio extended link
  225. */
  226. int snd_hdac_ext_bus_link_power_up(struct hdac_ext_link *link)
  227. {
  228. snd_hdac_updatel(link->ml_addr, AZX_REG_ML_LCTL, 0, AZX_MLCTL_SPA);
  229. return check_hdac_link_power_active(link, true);
  230. }
  231. EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_power_up);
  232. /**
  233. * snd_hdac_ext_bus_link_power_down -power down hda link
  234. * @link: HD-audio extended link
  235. */
  236. int snd_hdac_ext_bus_link_power_down(struct hdac_ext_link *link)
  237. {
  238. snd_hdac_updatel(link->ml_addr, AZX_REG_ML_LCTL, AZX_MLCTL_SPA, 0);
  239. return check_hdac_link_power_active(link, false);
  240. }
  241. EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_power_down);
  242. /**
  243. * snd_hdac_ext_bus_link_power_up_all -power up all hda link
  244. * @ebus: HD-audio extended bus
  245. */
  246. int snd_hdac_ext_bus_link_power_up_all(struct hdac_ext_bus *ebus)
  247. {
  248. struct hdac_ext_link *hlink = NULL;
  249. int ret;
  250. list_for_each_entry(hlink, &ebus->hlink_list, list) {
  251. snd_hdac_updatel(hlink->ml_addr,
  252. AZX_REG_ML_LCTL, 0, AZX_MLCTL_SPA);
  253. ret = check_hdac_link_power_active(hlink, true);
  254. if (ret < 0)
  255. return ret;
  256. }
  257. return 0;
  258. }
  259. EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_power_up_all);
  260. /**
  261. * snd_hdac_ext_bus_link_power_down_all -power down all hda link
  262. * @ebus: HD-audio extended bus
  263. */
  264. int snd_hdac_ext_bus_link_power_down_all(struct hdac_ext_bus *ebus)
  265. {
  266. struct hdac_ext_link *hlink = NULL;
  267. int ret;
  268. list_for_each_entry(hlink, &ebus->hlink_list, list) {
  269. snd_hdac_updatel(hlink->ml_addr, AZX_REG_ML_LCTL, AZX_MLCTL_SPA, 0);
  270. ret = check_hdac_link_power_active(hlink, false);
  271. if (ret < 0)
  272. return ret;
  273. }
  274. return 0;
  275. }
  276. EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_power_down_all);