hdac_ext_controller.c 8.0 KB

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