hdac_ext_controller.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. /* since link in On, update the ref */
  159. hlink->ref_count = 1;
  160. list_add_tail(&hlink->list, &ebus->hlink_list);
  161. }
  162. return 0;
  163. }
  164. EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_get_ml_capabilities);
  165. /**
  166. * snd_hdac_link_free_all- free hdac extended link objects
  167. *
  168. * @ebus: HD-audio ext core bus
  169. */
  170. void snd_hdac_link_free_all(struct hdac_ext_bus *ebus)
  171. {
  172. struct hdac_ext_link *l;
  173. while (!list_empty(&ebus->hlink_list)) {
  174. l = list_first_entry(&ebus->hlink_list, struct hdac_ext_link, list);
  175. list_del(&l->list);
  176. kfree(l);
  177. }
  178. }
  179. EXPORT_SYMBOL_GPL(snd_hdac_link_free_all);
  180. /**
  181. * snd_hdac_ext_bus_get_link_index - get link based on codec name
  182. * @ebus: HD-audio extended core bus
  183. * @codec_name: codec name
  184. */
  185. struct hdac_ext_link *snd_hdac_ext_bus_get_link(struct hdac_ext_bus *ebus,
  186. const char *codec_name)
  187. {
  188. int i;
  189. struct hdac_ext_link *hlink = NULL;
  190. int bus_idx, addr;
  191. if (sscanf(codec_name, "ehdaudio%dD%d", &bus_idx, &addr) != 2)
  192. return NULL;
  193. if (ebus->idx != bus_idx)
  194. return NULL;
  195. list_for_each_entry(hlink, &ebus->hlink_list, list) {
  196. for (i = 0; i < HDA_MAX_CODECS; i++) {
  197. if (hlink->lsdiid & (0x1 << addr))
  198. return hlink;
  199. }
  200. }
  201. return NULL;
  202. }
  203. EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_get_link);
  204. static int check_hdac_link_power_active(struct hdac_ext_link *link, bool enable)
  205. {
  206. int timeout;
  207. u32 val;
  208. int mask = (1 << AZX_MLCTL_CPA);
  209. udelay(3);
  210. timeout = 150;
  211. do {
  212. val = readl(link->ml_addr + AZX_REG_ML_LCTL);
  213. if (enable) {
  214. if (((val & mask) >> AZX_MLCTL_CPA))
  215. return 0;
  216. } else {
  217. if (!((val & mask) >> AZX_MLCTL_CPA))
  218. return 0;
  219. }
  220. udelay(3);
  221. } while (--timeout);
  222. return -EIO;
  223. }
  224. /**
  225. * snd_hdac_ext_bus_link_power_up -power up hda link
  226. * @link: HD-audio extended link
  227. */
  228. int snd_hdac_ext_bus_link_power_up(struct hdac_ext_link *link)
  229. {
  230. snd_hdac_updatel(link->ml_addr, AZX_REG_ML_LCTL, 0, AZX_MLCTL_SPA);
  231. return check_hdac_link_power_active(link, true);
  232. }
  233. EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_power_up);
  234. /**
  235. * snd_hdac_ext_bus_link_power_down -power down hda link
  236. * @link: HD-audio extended link
  237. */
  238. int snd_hdac_ext_bus_link_power_down(struct hdac_ext_link *link)
  239. {
  240. snd_hdac_updatel(link->ml_addr, AZX_REG_ML_LCTL, AZX_MLCTL_SPA, 0);
  241. return check_hdac_link_power_active(link, false);
  242. }
  243. EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_power_down);
  244. /**
  245. * snd_hdac_ext_bus_link_power_up_all -power up all hda link
  246. * @ebus: HD-audio extended bus
  247. */
  248. int snd_hdac_ext_bus_link_power_up_all(struct hdac_ext_bus *ebus)
  249. {
  250. struct hdac_ext_link *hlink = NULL;
  251. int ret;
  252. list_for_each_entry(hlink, &ebus->hlink_list, list) {
  253. snd_hdac_updatel(hlink->ml_addr,
  254. AZX_REG_ML_LCTL, 0, AZX_MLCTL_SPA);
  255. ret = check_hdac_link_power_active(hlink, true);
  256. if (ret < 0)
  257. return ret;
  258. }
  259. return 0;
  260. }
  261. EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_power_up_all);
  262. /**
  263. * snd_hdac_ext_bus_link_power_down_all -power down all hda link
  264. * @ebus: HD-audio extended bus
  265. */
  266. int snd_hdac_ext_bus_link_power_down_all(struct hdac_ext_bus *ebus)
  267. {
  268. struct hdac_ext_link *hlink = NULL;
  269. int ret;
  270. list_for_each_entry(hlink, &ebus->hlink_list, list) {
  271. snd_hdac_updatel(hlink->ml_addr, AZX_REG_ML_LCTL, AZX_MLCTL_SPA, 0);
  272. ret = check_hdac_link_power_active(hlink, false);
  273. if (ret < 0)
  274. return ret;
  275. }
  276. return 0;
  277. }
  278. EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_power_down_all);
  279. int snd_hdac_ext_bus_link_get(struct hdac_ext_bus *ebus,
  280. struct hdac_ext_link *link)
  281. {
  282. int ret = 0;
  283. mutex_lock(&ebus->lock);
  284. /*
  285. * if we move from 0 to 1, count will be 1 so power up this link
  286. * as well, also check the dma status and trigger that
  287. */
  288. if (++link->ref_count == 1) {
  289. if (!ebus->cmd_dma_state) {
  290. snd_hdac_bus_init_cmd_io(&ebus->bus);
  291. ebus->cmd_dma_state = true;
  292. }
  293. ret = snd_hdac_ext_bus_link_power_up(link);
  294. }
  295. mutex_unlock(&ebus->lock);
  296. return ret;
  297. }
  298. EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_get);
  299. int snd_hdac_ext_bus_link_put(struct hdac_ext_bus *ebus,
  300. struct hdac_ext_link *link)
  301. {
  302. int ret = 0;
  303. struct hdac_ext_link *hlink;
  304. bool link_up = false;
  305. mutex_lock(&ebus->lock);
  306. /*
  307. * if we move from 1 to 0, count will be 0
  308. * so power down this link as well
  309. */
  310. if (--link->ref_count == 0) {
  311. ret = snd_hdac_ext_bus_link_power_down(link);
  312. /*
  313. * now check if all links are off, if so turn off
  314. * cmd dma as well
  315. */
  316. list_for_each_entry(hlink, &ebus->hlink_list, list) {
  317. if (hlink->ref_count) {
  318. link_up = true;
  319. break;
  320. }
  321. }
  322. if (!link_up) {
  323. snd_hdac_bus_stop_cmd_io(&ebus->bus);
  324. ebus->cmd_dma_state = false;
  325. }
  326. }
  327. mutex_unlock(&ebus->lock);
  328. return ret;
  329. }
  330. EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_put);