hdac_ext_controller.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. if (addr < 0 || addr > 31)
  138. return NULL;
  139. list_for_each_entry(hlink, &bus->hlink_list, list) {
  140. for (i = 0; i < HDA_MAX_CODECS; i++) {
  141. if (hlink->lsdiid & (0x1 << addr))
  142. return hlink;
  143. }
  144. }
  145. return NULL;
  146. }
  147. EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_get_link);
  148. static int check_hdac_link_power_active(struct hdac_ext_link *link, bool enable)
  149. {
  150. int timeout;
  151. u32 val;
  152. int mask = (1 << AZX_MLCTL_CPA_SHIFT);
  153. udelay(3);
  154. timeout = 150;
  155. do {
  156. val = readl(link->ml_addr + AZX_REG_ML_LCTL);
  157. if (enable) {
  158. if (((val & mask) >> AZX_MLCTL_CPA_SHIFT))
  159. return 0;
  160. } else {
  161. if (!((val & mask) >> AZX_MLCTL_CPA_SHIFT))
  162. return 0;
  163. }
  164. udelay(3);
  165. } while (--timeout);
  166. return -EIO;
  167. }
  168. /**
  169. * snd_hdac_ext_bus_link_power_up -power up hda link
  170. * @link: HD-audio extended link
  171. */
  172. int snd_hdac_ext_bus_link_power_up(struct hdac_ext_link *link)
  173. {
  174. snd_hdac_updatel(link->ml_addr, AZX_REG_ML_LCTL,
  175. AZX_MLCTL_SPA, AZX_MLCTL_SPA);
  176. return check_hdac_link_power_active(link, true);
  177. }
  178. EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_power_up);
  179. /**
  180. * snd_hdac_ext_bus_link_power_down -power down hda link
  181. * @link: HD-audio extended link
  182. */
  183. int snd_hdac_ext_bus_link_power_down(struct hdac_ext_link *link)
  184. {
  185. snd_hdac_updatel(link->ml_addr, AZX_REG_ML_LCTL, AZX_MLCTL_SPA, 0);
  186. return check_hdac_link_power_active(link, false);
  187. }
  188. EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_power_down);
  189. /**
  190. * snd_hdac_ext_bus_link_power_up_all -power up all hda link
  191. * @ebus: HD-audio extended bus
  192. */
  193. int snd_hdac_ext_bus_link_power_up_all(struct hdac_bus *bus)
  194. {
  195. struct hdac_ext_link *hlink = NULL;
  196. int ret;
  197. list_for_each_entry(hlink, &bus->hlink_list, list) {
  198. snd_hdac_updatel(hlink->ml_addr, AZX_REG_ML_LCTL,
  199. AZX_MLCTL_SPA, AZX_MLCTL_SPA);
  200. ret = check_hdac_link_power_active(hlink, true);
  201. if (ret < 0)
  202. return ret;
  203. }
  204. return 0;
  205. }
  206. EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_power_up_all);
  207. /**
  208. * snd_hdac_ext_bus_link_power_down_all -power down all hda link
  209. * @ebus: HD-audio extended bus
  210. */
  211. int snd_hdac_ext_bus_link_power_down_all(struct hdac_bus *bus)
  212. {
  213. struct hdac_ext_link *hlink = NULL;
  214. int ret;
  215. list_for_each_entry(hlink, &bus->hlink_list, list) {
  216. snd_hdac_updatel(hlink->ml_addr, AZX_REG_ML_LCTL,
  217. AZX_MLCTL_SPA, 0);
  218. ret = check_hdac_link_power_active(hlink, false);
  219. if (ret < 0)
  220. return ret;
  221. }
  222. return 0;
  223. }
  224. EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_power_down_all);
  225. int snd_hdac_ext_bus_link_get(struct hdac_bus *bus,
  226. struct hdac_ext_link *link)
  227. {
  228. unsigned long codec_mask;
  229. int ret = 0;
  230. mutex_lock(&bus->lock);
  231. /*
  232. * if we move from 0 to 1, count will be 1 so power up this link
  233. * as well, also check the dma status and trigger that
  234. */
  235. if (++link->ref_count == 1) {
  236. if (!bus->cmd_dma_state) {
  237. snd_hdac_bus_init_cmd_io(bus);
  238. bus->cmd_dma_state = true;
  239. }
  240. ret = snd_hdac_ext_bus_link_power_up(link);
  241. /*
  242. * wait for 521usec for codec to report status
  243. * HDA spec section 4.3 - Codec Discovery
  244. */
  245. udelay(521);
  246. codec_mask = snd_hdac_chip_readw(bus, STATESTS);
  247. dev_dbg(bus->dev, "codec_mask = 0x%lx\n", codec_mask);
  248. snd_hdac_chip_writew(bus, STATESTS, codec_mask);
  249. if (!bus->codec_mask)
  250. bus->codec_mask = codec_mask;
  251. }
  252. mutex_unlock(&bus->lock);
  253. return ret;
  254. }
  255. EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_get);
  256. int snd_hdac_ext_bus_link_put(struct hdac_bus *bus,
  257. struct hdac_ext_link *link)
  258. {
  259. int ret = 0;
  260. struct hdac_ext_link *hlink;
  261. bool link_up = false;
  262. mutex_lock(&bus->lock);
  263. /*
  264. * if we move from 1 to 0, count will be 0
  265. * so power down this link as well
  266. */
  267. if (--link->ref_count == 0) {
  268. ret = snd_hdac_ext_bus_link_power_down(link);
  269. /*
  270. * now check if all links are off, if so turn off
  271. * cmd dma as well
  272. */
  273. list_for_each_entry(hlink, &bus->hlink_list, list) {
  274. if (hlink->ref_count) {
  275. link_up = true;
  276. break;
  277. }
  278. }
  279. if (!link_up) {
  280. snd_hdac_bus_stop_cmd_io(bus);
  281. bus->cmd_dma_state = false;
  282. }
  283. }
  284. mutex_unlock(&bus->lock);
  285. return ret;
  286. }
  287. EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_put);