hdac_ext_controller.c 8.1 KB

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