hdac_ext_controller.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. * processing pipe helpers - these helpers are useful for dealing with HDA
  31. * new capability of processing pipelines
  32. */
  33. /**
  34. * snd_hdac_ext_bus_ppcap_enable - enable/disable processing pipe capability
  35. * @ebus: HD-audio extended core bus
  36. * @enable: flag to turn on/off the capability
  37. */
  38. void snd_hdac_ext_bus_ppcap_enable(struct hdac_ext_bus *ebus, bool enable)
  39. {
  40. struct hdac_bus *bus = &ebus->bus;
  41. if (!bus->ppcap) {
  42. dev_err(bus->dev, "Address of PP capability is NULL");
  43. return;
  44. }
  45. if (enable)
  46. snd_hdac_updatel(bus->ppcap, AZX_REG_PP_PPCTL, 0, AZX_PPCTL_GPROCEN);
  47. else
  48. snd_hdac_updatel(bus->ppcap, AZX_REG_PP_PPCTL, AZX_PPCTL_GPROCEN, 0);
  49. }
  50. EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_ppcap_enable);
  51. /**
  52. * snd_hdac_ext_bus_ppcap_int_enable - ppcap interrupt enable/disable
  53. * @ebus: HD-audio extended core bus
  54. * @enable: flag to enable/disable interrupt
  55. */
  56. void snd_hdac_ext_bus_ppcap_int_enable(struct hdac_ext_bus *ebus, bool enable)
  57. {
  58. struct hdac_bus *bus = &ebus->bus;
  59. if (!bus->ppcap) {
  60. dev_err(bus->dev, "Address of PP capability is NULL\n");
  61. return;
  62. }
  63. if (enable)
  64. snd_hdac_updatel(bus->ppcap, AZX_REG_PP_PPCTL, 0, AZX_PPCTL_PIE);
  65. else
  66. snd_hdac_updatel(bus->ppcap, AZX_REG_PP_PPCTL, AZX_PPCTL_PIE, 0);
  67. }
  68. EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_ppcap_int_enable);
  69. /*
  70. * Multilink helpers - these helpers are useful for dealing with HDA
  71. * new multilink capability
  72. */
  73. /**
  74. * snd_hdac_ext_bus_get_ml_capabilities - get multilink capability
  75. * @ebus: HD-audio extended core bus
  76. *
  77. * This will parse all links and read the mlink capabilities and add them
  78. * in hlink_list of extended hdac bus
  79. * Note: this will be freed on bus exit by driver
  80. */
  81. int snd_hdac_ext_bus_get_ml_capabilities(struct hdac_ext_bus *ebus)
  82. {
  83. int idx;
  84. u32 link_count;
  85. struct hdac_ext_link *hlink;
  86. struct hdac_bus *bus = &ebus->bus;
  87. link_count = readl(bus->mlcap + AZX_REG_ML_MLCD) + 1;
  88. dev_dbg(bus->dev, "In %s Link count: %d\n", __func__, link_count);
  89. for (idx = 0; idx < link_count; idx++) {
  90. hlink = kzalloc(sizeof(*hlink), GFP_KERNEL);
  91. if (!hlink)
  92. return -ENOMEM;
  93. hlink->index = idx;
  94. hlink->bus = bus;
  95. hlink->ml_addr = bus->mlcap + AZX_ML_BASE +
  96. (AZX_ML_INTERVAL * idx);
  97. hlink->lcaps = readl(hlink->ml_addr + AZX_REG_ML_LCAP);
  98. hlink->lsdiid = readw(hlink->ml_addr + AZX_REG_ML_LSDIID);
  99. /* since link in On, update the ref */
  100. hlink->ref_count = 1;
  101. list_add_tail(&hlink->list, &ebus->hlink_list);
  102. }
  103. return 0;
  104. }
  105. EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_get_ml_capabilities);
  106. /**
  107. * snd_hdac_link_free_all- free hdac extended link objects
  108. *
  109. * @ebus: HD-audio ext core bus
  110. */
  111. void snd_hdac_link_free_all(struct hdac_ext_bus *ebus)
  112. {
  113. struct hdac_ext_link *l;
  114. while (!list_empty(&ebus->hlink_list)) {
  115. l = list_first_entry(&ebus->hlink_list, struct hdac_ext_link, list);
  116. list_del(&l->list);
  117. kfree(l);
  118. }
  119. }
  120. EXPORT_SYMBOL_GPL(snd_hdac_link_free_all);
  121. /**
  122. * snd_hdac_ext_bus_get_link_index - get link based on codec name
  123. * @ebus: HD-audio extended core bus
  124. * @codec_name: codec name
  125. */
  126. struct hdac_ext_link *snd_hdac_ext_bus_get_link(struct hdac_ext_bus *ebus,
  127. const char *codec_name)
  128. {
  129. int i;
  130. struct hdac_ext_link *hlink = NULL;
  131. int bus_idx, addr;
  132. if (sscanf(codec_name, "ehdaudio%dD%d", &bus_idx, &addr) != 2)
  133. return NULL;
  134. if (ebus->idx != bus_idx)
  135. return NULL;
  136. list_for_each_entry(hlink, &ebus->hlink_list, list) {
  137. for (i = 0; i < HDA_MAX_CODECS; i++) {
  138. if (hlink->lsdiid & (0x1 << addr))
  139. return hlink;
  140. }
  141. }
  142. return NULL;
  143. }
  144. EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_get_link);
  145. static int check_hdac_link_power_active(struct hdac_ext_link *link, bool enable)
  146. {
  147. int timeout;
  148. u32 val;
  149. int mask = (1 << AZX_MLCTL_CPA);
  150. udelay(3);
  151. timeout = 150;
  152. do {
  153. val = readl(link->ml_addr + AZX_REG_ML_LCTL);
  154. if (enable) {
  155. if (((val & mask) >> AZX_MLCTL_CPA))
  156. return 0;
  157. } else {
  158. if (!((val & mask) >> AZX_MLCTL_CPA))
  159. return 0;
  160. }
  161. udelay(3);
  162. } while (--timeout);
  163. return -EIO;
  164. }
  165. /**
  166. * snd_hdac_ext_bus_link_power_up -power up hda link
  167. * @link: HD-audio extended link
  168. */
  169. int snd_hdac_ext_bus_link_power_up(struct hdac_ext_link *link)
  170. {
  171. snd_hdac_updatel(link->ml_addr, AZX_REG_ML_LCTL, 0, AZX_MLCTL_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_MLCTL_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. * @ebus: HD-audio extended bus
  188. */
  189. int snd_hdac_ext_bus_link_power_up_all(struct hdac_ext_bus *ebus)
  190. {
  191. struct hdac_ext_link *hlink = NULL;
  192. int ret;
  193. list_for_each_entry(hlink, &ebus->hlink_list, list) {
  194. snd_hdac_updatel(hlink->ml_addr,
  195. AZX_REG_ML_LCTL, 0, AZX_MLCTL_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. * @ebus: HD-audio extended bus
  206. */
  207. int snd_hdac_ext_bus_link_power_down_all(struct hdac_ext_bus *ebus)
  208. {
  209. struct hdac_ext_link *hlink = NULL;
  210. int ret;
  211. list_for_each_entry(hlink, &ebus->hlink_list, list) {
  212. snd_hdac_updatel(hlink->ml_addr, AZX_REG_ML_LCTL, AZX_MLCTL_SPA, 0);
  213. ret = check_hdac_link_power_active(hlink, false);
  214. if (ret < 0)
  215. return ret;
  216. }
  217. return 0;
  218. }
  219. EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_power_down_all);
  220. int snd_hdac_ext_bus_link_get(struct hdac_ext_bus *ebus,
  221. struct hdac_ext_link *link)
  222. {
  223. int ret = 0;
  224. mutex_lock(&ebus->lock);
  225. /*
  226. * if we move from 0 to 1, count will be 1 so power up this link
  227. * as well, also check the dma status and trigger that
  228. */
  229. if (++link->ref_count == 1) {
  230. if (!ebus->cmd_dma_state) {
  231. snd_hdac_bus_init_cmd_io(&ebus->bus);
  232. ebus->cmd_dma_state = true;
  233. }
  234. ret = snd_hdac_ext_bus_link_power_up(link);
  235. }
  236. mutex_unlock(&ebus->lock);
  237. return ret;
  238. }
  239. EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_get);
  240. int snd_hdac_ext_bus_link_put(struct hdac_ext_bus *ebus,
  241. struct hdac_ext_link *link)
  242. {
  243. int ret = 0;
  244. struct hdac_ext_link *hlink;
  245. bool link_up = false;
  246. mutex_lock(&ebus->lock);
  247. /*
  248. * if we move from 1 to 0, count will be 0
  249. * so power down this link as well
  250. */
  251. if (--link->ref_count == 0) {
  252. ret = snd_hdac_ext_bus_link_power_down(link);
  253. /*
  254. * now check if all links are off, if so turn off
  255. * cmd dma as well
  256. */
  257. list_for_each_entry(hlink, &ebus->hlink_list, list) {
  258. if (hlink->ref_count) {
  259. link_up = true;
  260. break;
  261. }
  262. }
  263. if (!link_up) {
  264. snd_hdac_bus_stop_cmd_io(&ebus->bus);
  265. ebus->cmd_dma_state = false;
  266. }
  267. }
  268. mutex_unlock(&ebus->lock);
  269. return ret;
  270. }
  271. EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_put);