hdmi.c 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. *
  4. * hdmi.c - routines for HDMI/DisplayPort codecs
  5. *
  6. * Copyright(c) 2008-2010 Intel Corporation
  7. * Copyright (c) 2006 ATI Technologies Inc.
  8. * Copyright (c) 2008 NVIDIA Corp. All rights reserved.
  9. * Copyright (c) 2008 Wei Ni <wni@nvidia.com>
  10. * Copyright (c) 2013 Anssi Hannula <anssi.hannula@iki.fi>
  11. *
  12. * Authors:
  13. * Wu Fengguang <wfg@linux.intel.com>
  14. *
  15. * Maintained by:
  16. * Wu Fengguang <wfg@linux.intel.com>
  17. */
  18. #include <linux/init.h>
  19. #include <linux/delay.h>
  20. #include <linux/pci.h>
  21. #include <linux/slab.h>
  22. #include <linux/module.h>
  23. #include <linux/pm_runtime.h>
  24. #include <sound/core.h>
  25. #include <sound/jack.h>
  26. #include <sound/asoundef.h>
  27. #include <sound/tlv.h>
  28. #include <sound/hdaudio.h>
  29. #include <sound/hda_i915.h>
  30. #include <sound/hda_chmap.h>
  31. #include <sound/hda_codec.h>
  32. #include "hda_local.h"
  33. #include "hda_jack.h"
  34. #include "hda_controller.h"
  35. #include "hdmi_local.h"
  36. static bool static_hdmi_pcm;
  37. module_param(static_hdmi_pcm, bool, 0644);
  38. MODULE_PARM_DESC(static_hdmi_pcm, "Don't restrict PCM parameters per ELD info");
  39. static bool enable_acomp = true;
  40. module_param(enable_acomp, bool, 0444);
  41. MODULE_PARM_DESC(enable_acomp, "Enable audio component binding (default=yes)");
  42. static bool enable_all_pins;
  43. module_param(enable_all_pins, bool, 0444);
  44. MODULE_PARM_DESC(enable_all_pins, "Forcibly enable all pins");
  45. int snd_hda_hdmi_pin_id_to_pin_index(struct hda_codec *codec,
  46. hda_nid_t pin_nid, int dev_id)
  47. {
  48. struct hdmi_spec *spec = codec->spec;
  49. int pin_idx;
  50. struct hdmi_spec_per_pin *per_pin;
  51. /*
  52. * (dev_id == -1) means it is NON-MST pin
  53. * return the first virtual pin on this port
  54. */
  55. if (dev_id == -1)
  56. dev_id = 0;
  57. for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
  58. per_pin = get_pin(spec, pin_idx);
  59. if ((per_pin->pin_nid == pin_nid) &&
  60. (per_pin->dev_id == dev_id))
  61. return pin_idx;
  62. }
  63. codec_warn(codec, "HDMI: pin NID 0x%x not registered\n", pin_nid);
  64. return -EINVAL;
  65. }
  66. EXPORT_SYMBOL_NS_GPL(snd_hda_hdmi_pin_id_to_pin_index, "SND_HDA_CODEC_HDMI");
  67. static int hinfo_to_pcm_index(struct hda_codec *codec,
  68. struct hda_pcm_stream *hinfo)
  69. {
  70. struct hdmi_spec *spec = codec->spec;
  71. int pcm_idx;
  72. for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++)
  73. if (get_pcm_rec(spec, pcm_idx)->stream == hinfo)
  74. return pcm_idx;
  75. codec_warn(codec, "HDMI: hinfo %p not tied to a PCM\n", hinfo);
  76. return -EINVAL;
  77. }
  78. static int hinfo_to_pin_index(struct hda_codec *codec,
  79. struct hda_pcm_stream *hinfo)
  80. {
  81. struct hdmi_spec *spec = codec->spec;
  82. struct hdmi_spec_per_pin *per_pin;
  83. int pin_idx;
  84. for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
  85. per_pin = get_pin(spec, pin_idx);
  86. if (per_pin->pcm &&
  87. per_pin->pcm->pcm->stream == hinfo)
  88. return pin_idx;
  89. }
  90. codec_dbg(codec, "HDMI: hinfo %p (pcm %d) not registered\n", hinfo,
  91. hinfo_to_pcm_index(codec, hinfo));
  92. return -EINVAL;
  93. }
  94. static struct hdmi_spec_per_pin *pcm_idx_to_pin(struct hdmi_spec *spec,
  95. int pcm_idx)
  96. {
  97. int i;
  98. struct hdmi_spec_per_pin *per_pin;
  99. for (i = 0; i < spec->num_pins; i++) {
  100. per_pin = get_pin(spec, i);
  101. if (per_pin->pcm_idx == pcm_idx)
  102. return per_pin;
  103. }
  104. return NULL;
  105. }
  106. static int cvt_nid_to_cvt_index(struct hda_codec *codec, hda_nid_t cvt_nid)
  107. {
  108. struct hdmi_spec *spec = codec->spec;
  109. int cvt_idx;
  110. for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++)
  111. if (get_cvt(spec, cvt_idx)->cvt_nid == cvt_nid)
  112. return cvt_idx;
  113. codec_warn(codec, "HDMI: cvt NID 0x%x not registered\n", cvt_nid);
  114. return -EINVAL;
  115. }
  116. static int hdmi_eld_ctl_info(struct snd_kcontrol *kcontrol,
  117. struct snd_ctl_elem_info *uinfo)
  118. {
  119. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  120. struct hdmi_spec *spec = codec->spec;
  121. struct hdmi_spec_per_pin *per_pin;
  122. struct hdmi_eld *eld;
  123. int pcm_idx;
  124. uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
  125. pcm_idx = kcontrol->private_value;
  126. guard(mutex)(&spec->pcm_lock);
  127. per_pin = pcm_idx_to_pin(spec, pcm_idx);
  128. if (!per_pin) {
  129. /* no pin is bound to the pcm */
  130. uinfo->count = 0;
  131. return 0;
  132. }
  133. eld = &per_pin->sink_eld;
  134. uinfo->count = eld->eld_valid ? eld->eld_size : 0;
  135. return 0;
  136. }
  137. static int hdmi_eld_ctl_get(struct snd_kcontrol *kcontrol,
  138. struct snd_ctl_elem_value *ucontrol)
  139. {
  140. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  141. struct hdmi_spec *spec = codec->spec;
  142. struct hdmi_spec_per_pin *per_pin;
  143. struct hdmi_eld *eld;
  144. int pcm_idx;
  145. pcm_idx = kcontrol->private_value;
  146. guard(mutex)(&spec->pcm_lock);
  147. per_pin = pcm_idx_to_pin(spec, pcm_idx);
  148. if (!per_pin) {
  149. /* no pin is bound to the pcm */
  150. memset(ucontrol->value.bytes.data, 0,
  151. ARRAY_SIZE(ucontrol->value.bytes.data));
  152. return 0;
  153. }
  154. eld = &per_pin->sink_eld;
  155. if (eld->eld_size > ARRAY_SIZE(ucontrol->value.bytes.data) ||
  156. eld->eld_size > ELD_MAX_SIZE) {
  157. snd_BUG();
  158. return -EINVAL;
  159. }
  160. memset(ucontrol->value.bytes.data, 0,
  161. ARRAY_SIZE(ucontrol->value.bytes.data));
  162. if (eld->eld_valid)
  163. memcpy(ucontrol->value.bytes.data, eld->eld_buffer,
  164. eld->eld_size);
  165. return 0;
  166. }
  167. static const struct snd_kcontrol_new eld_bytes_ctl = {
  168. .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE |
  169. SNDRV_CTL_ELEM_ACCESS_SKIP_CHECK,
  170. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  171. .name = "ELD",
  172. .info = hdmi_eld_ctl_info,
  173. .get = hdmi_eld_ctl_get,
  174. };
  175. static int hdmi_create_eld_ctl(struct hda_codec *codec, int pcm_idx,
  176. int device)
  177. {
  178. struct snd_kcontrol *kctl;
  179. struct hdmi_spec *spec = codec->spec;
  180. int err;
  181. kctl = snd_ctl_new1(&eld_bytes_ctl, codec);
  182. if (!kctl)
  183. return -ENOMEM;
  184. kctl->private_value = pcm_idx;
  185. kctl->id.device = device;
  186. /* no pin nid is associated with the kctl now
  187. * tbd: associate pin nid to eld ctl later
  188. */
  189. err = snd_hda_ctl_add(codec, 0, kctl);
  190. if (err < 0)
  191. return err;
  192. get_hdmi_pcm(spec, pcm_idx)->eld_ctl = kctl;
  193. return 0;
  194. }
  195. #ifdef BE_PARANOID
  196. static void hdmi_get_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
  197. int *packet_index, int *byte_index)
  198. {
  199. int val;
  200. val = snd_hda_codec_read(codec, pin_nid, 0,
  201. AC_VERB_GET_HDMI_DIP_INDEX, 0);
  202. *packet_index = val >> 5;
  203. *byte_index = val & 0x1f;
  204. }
  205. #endif
  206. static void hdmi_set_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
  207. int packet_index, int byte_index)
  208. {
  209. int val;
  210. val = (packet_index << 5) | (byte_index & 0x1f);
  211. snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val);
  212. }
  213. static void hdmi_write_dip_byte(struct hda_codec *codec, hda_nid_t pin_nid,
  214. unsigned char val)
  215. {
  216. snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_DATA, val);
  217. }
  218. static void hdmi_init_pin(struct hda_codec *codec, hda_nid_t pin_nid)
  219. {
  220. struct hdmi_spec *spec = codec->spec;
  221. int pin_out;
  222. /* Unmute */
  223. if (get_wcaps(codec, pin_nid) & AC_WCAP_OUT_AMP)
  224. snd_hda_codec_write(codec, pin_nid, 0,
  225. AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
  226. if (spec->dyn_pin_out)
  227. /* Disable pin out until stream is active */
  228. pin_out = 0;
  229. else
  230. /* Enable pin out: some machines with GM965 gets broken output
  231. * when the pin is disabled or changed while using with HDMI
  232. */
  233. pin_out = PIN_OUT;
  234. snd_hda_codec_write(codec, pin_nid, 0,
  235. AC_VERB_SET_PIN_WIDGET_CONTROL, pin_out);
  236. }
  237. /*
  238. * ELD proc files
  239. */
  240. #ifdef CONFIG_SND_PROC_FS
  241. static void print_eld_info(struct snd_info_entry *entry,
  242. struct snd_info_buffer *buffer)
  243. {
  244. struct hdmi_spec_per_pin *per_pin = entry->private_data;
  245. guard(mutex)(&per_pin->lock);
  246. snd_hdmi_print_eld_info(&per_pin->sink_eld, buffer, per_pin->pin_nid,
  247. per_pin->dev_id, per_pin->cvt_nid);
  248. }
  249. static void write_eld_info(struct snd_info_entry *entry,
  250. struct snd_info_buffer *buffer)
  251. {
  252. struct hdmi_spec_per_pin *per_pin = entry->private_data;
  253. guard(mutex)(&per_pin->lock);
  254. snd_hdmi_write_eld_info(&per_pin->sink_eld, buffer);
  255. }
  256. static int eld_proc_new(struct hdmi_spec_per_pin *per_pin, int index)
  257. {
  258. char name[32];
  259. struct hda_codec *codec = per_pin->codec;
  260. struct snd_info_entry *entry;
  261. int err;
  262. snprintf(name, sizeof(name), "eld#%d.%d", codec->addr, index);
  263. err = snd_card_proc_new(codec->card, name, &entry);
  264. if (err < 0)
  265. return err;
  266. snd_info_set_text_ops(entry, per_pin, print_eld_info);
  267. entry->c.text.write = write_eld_info;
  268. entry->mode |= 0200;
  269. per_pin->proc_entry = entry;
  270. return 0;
  271. }
  272. static void eld_proc_free(struct hdmi_spec_per_pin *per_pin)
  273. {
  274. if (!per_pin->codec->bus->shutdown) {
  275. snd_info_free_entry(per_pin->proc_entry);
  276. per_pin->proc_entry = NULL;
  277. }
  278. }
  279. #else
  280. static inline int eld_proc_new(struct hdmi_spec_per_pin *per_pin,
  281. int index)
  282. {
  283. return 0;
  284. }
  285. static inline void eld_proc_free(struct hdmi_spec_per_pin *per_pin)
  286. {
  287. }
  288. #endif
  289. /*
  290. * Audio InfoFrame routines
  291. */
  292. /*
  293. * Enable Audio InfoFrame Transmission
  294. */
  295. static void hdmi_start_infoframe_trans(struct hda_codec *codec,
  296. hda_nid_t pin_nid)
  297. {
  298. hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
  299. snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
  300. AC_DIPXMIT_BEST);
  301. }
  302. /*
  303. * Disable Audio InfoFrame Transmission
  304. */
  305. static void hdmi_stop_infoframe_trans(struct hda_codec *codec,
  306. hda_nid_t pin_nid)
  307. {
  308. hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
  309. snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
  310. AC_DIPXMIT_DISABLE);
  311. }
  312. static void hdmi_debug_dip_size(struct hda_codec *codec, hda_nid_t pin_nid)
  313. {
  314. #ifdef CONFIG_SND_DEBUG_VERBOSE
  315. int i;
  316. int size;
  317. size = snd_hdmi_get_eld_size(codec, pin_nid);
  318. codec_dbg(codec, "HDMI: ELD buf size is %d\n", size);
  319. for (i = 0; i < 8; i++) {
  320. size = snd_hda_codec_read(codec, pin_nid, 0,
  321. AC_VERB_GET_HDMI_DIP_SIZE, i);
  322. codec_dbg(codec, "HDMI: DIP GP[%d] buf size is %d\n", i, size);
  323. }
  324. #endif
  325. }
  326. static void hdmi_clear_dip_buffers(struct hda_codec *codec, hda_nid_t pin_nid)
  327. {
  328. #ifdef BE_PARANOID
  329. int i, j;
  330. int size;
  331. int pi, bi;
  332. for (i = 0; i < 8; i++) {
  333. size = snd_hda_codec_read(codec, pin_nid, 0,
  334. AC_VERB_GET_HDMI_DIP_SIZE, i);
  335. if (size == 0)
  336. continue;
  337. hdmi_set_dip_index(codec, pin_nid, i, 0x0);
  338. for (j = 1; j < 1000; j++) {
  339. hdmi_write_dip_byte(codec, pin_nid, 0x0);
  340. hdmi_get_dip_index(codec, pin_nid, &pi, &bi);
  341. if (pi != i)
  342. codec_dbg(codec, "dip index %d: %d != %d\n",
  343. bi, pi, i);
  344. if (bi == 0) /* byte index wrapped around */
  345. break;
  346. }
  347. codec_dbg(codec,
  348. "HDMI: DIP GP[%d] buf reported size=%d, written=%d\n",
  349. i, size, j);
  350. }
  351. #endif
  352. }
  353. static void hdmi_checksum_audio_infoframe(struct hdmi_audio_infoframe *hdmi_ai)
  354. {
  355. u8 *bytes = (u8 *)hdmi_ai;
  356. u8 sum = 0;
  357. int i;
  358. hdmi_ai->checksum = 0;
  359. for (i = 0; i < sizeof(*hdmi_ai); i++)
  360. sum += bytes[i];
  361. hdmi_ai->checksum = -sum;
  362. }
  363. static void hdmi_fill_audio_infoframe(struct hda_codec *codec,
  364. hda_nid_t pin_nid,
  365. u8 *dip, int size)
  366. {
  367. int i;
  368. hdmi_debug_dip_size(codec, pin_nid);
  369. hdmi_clear_dip_buffers(codec, pin_nid); /* be paranoid */
  370. hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
  371. for (i = 0; i < size; i++)
  372. hdmi_write_dip_byte(codec, pin_nid, dip[i]);
  373. }
  374. static bool hdmi_infoframe_uptodate(struct hda_codec *codec, hda_nid_t pin_nid,
  375. u8 *dip, int size)
  376. {
  377. u8 val;
  378. int i;
  379. hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
  380. if (snd_hda_codec_read(codec, pin_nid, 0, AC_VERB_GET_HDMI_DIP_XMIT, 0)
  381. != AC_DIPXMIT_BEST)
  382. return false;
  383. for (i = 0; i < size; i++) {
  384. val = snd_hda_codec_read(codec, pin_nid, 0,
  385. AC_VERB_GET_HDMI_DIP_DATA, 0);
  386. if (val != dip[i])
  387. return false;
  388. }
  389. return true;
  390. }
  391. static int hdmi_pin_get_eld(struct hda_codec *codec, hda_nid_t nid,
  392. int dev_id, unsigned char *buf, int *eld_size)
  393. {
  394. snd_hda_set_dev_select(codec, nid, dev_id);
  395. return snd_hdmi_get_eld(codec, nid, buf, eld_size);
  396. }
  397. static void hdmi_pin_setup_infoframe(struct hda_codec *codec,
  398. hda_nid_t pin_nid, int dev_id,
  399. int ca, int active_channels,
  400. int conn_type)
  401. {
  402. struct hdmi_spec *spec = codec->spec;
  403. union audio_infoframe ai;
  404. memset(&ai, 0, sizeof(ai));
  405. if ((conn_type == 0) || /* HDMI */
  406. /* Nvidia DisplayPort: Nvidia HW expects same layout as HDMI */
  407. (conn_type == 1 && spec->nv_dp_workaround)) {
  408. struct hdmi_audio_infoframe *hdmi_ai = &ai.hdmi;
  409. if (conn_type == 0) { /* HDMI */
  410. hdmi_ai->type = 0x84;
  411. hdmi_ai->ver = 0x01;
  412. hdmi_ai->len = 0x0a;
  413. } else {/* Nvidia DP */
  414. hdmi_ai->type = 0x84;
  415. hdmi_ai->ver = 0x1b;
  416. hdmi_ai->len = 0x11 << 2;
  417. }
  418. hdmi_ai->CC02_CT47 = active_channels - 1;
  419. hdmi_ai->CA = ca;
  420. hdmi_checksum_audio_infoframe(hdmi_ai);
  421. } else if (conn_type == 1) { /* DisplayPort */
  422. struct dp_audio_infoframe *dp_ai = &ai.dp;
  423. dp_ai->type = 0x84;
  424. dp_ai->len = 0x1b;
  425. dp_ai->ver = 0x11 << 2;
  426. dp_ai->CC02_CT47 = active_channels - 1;
  427. dp_ai->CA = ca;
  428. } else {
  429. codec_dbg(codec, "HDMI: unknown connection type at pin NID 0x%x\n", pin_nid);
  430. return;
  431. }
  432. snd_hda_set_dev_select(codec, pin_nid, dev_id);
  433. /*
  434. * sizeof(ai) is used instead of sizeof(*hdmi_ai) or
  435. * sizeof(*dp_ai) to avoid partial match/update problems when
  436. * the user switches between HDMI/DP monitors.
  437. */
  438. if (!hdmi_infoframe_uptodate(codec, pin_nid, ai.bytes,
  439. sizeof(ai))) {
  440. codec_dbg(codec, "%s: pin NID=0x%x channels=%d ca=0x%02x\n",
  441. __func__, pin_nid, active_channels, ca);
  442. hdmi_stop_infoframe_trans(codec, pin_nid);
  443. hdmi_fill_audio_infoframe(codec, pin_nid,
  444. ai.bytes, sizeof(ai));
  445. hdmi_start_infoframe_trans(codec, pin_nid);
  446. }
  447. }
  448. void snd_hda_hdmi_setup_audio_infoframe(struct hda_codec *codec,
  449. struct hdmi_spec_per_pin *per_pin,
  450. bool non_pcm)
  451. {
  452. struct hdmi_spec *spec = codec->spec;
  453. struct hdac_chmap *chmap = &spec->chmap;
  454. hda_nid_t pin_nid = per_pin->pin_nid;
  455. int dev_id = per_pin->dev_id;
  456. int channels = per_pin->channels;
  457. int active_channels;
  458. struct hdmi_eld *eld;
  459. int ca;
  460. if (!channels)
  461. return;
  462. snd_hda_set_dev_select(codec, pin_nid, dev_id);
  463. /* some HW (e.g. HSW+) needs reprogramming the amp at each time */
  464. if (get_wcaps(codec, pin_nid) & AC_WCAP_OUT_AMP)
  465. snd_hda_codec_write(codec, pin_nid, 0,
  466. AC_VERB_SET_AMP_GAIN_MUTE,
  467. AMP_OUT_UNMUTE);
  468. eld = &per_pin->sink_eld;
  469. ca = snd_hdac_channel_allocation(&codec->core,
  470. eld->info.spk_alloc, channels,
  471. per_pin->chmap_set, non_pcm, per_pin->chmap);
  472. active_channels = snd_hdac_get_active_channels(ca);
  473. chmap->ops.set_channel_count(&codec->core, per_pin->cvt_nid,
  474. active_channels);
  475. /*
  476. * always configure channel mapping, it may have been changed by the
  477. * user in the meantime
  478. */
  479. snd_hdac_setup_channel_mapping(&spec->chmap,
  480. pin_nid, non_pcm, ca, channels,
  481. per_pin->chmap, per_pin->chmap_set);
  482. spec->ops.pin_setup_infoframe(codec, pin_nid, dev_id,
  483. ca, active_channels, eld->info.conn_type);
  484. per_pin->non_pcm = non_pcm;
  485. }
  486. EXPORT_SYMBOL_NS_GPL(snd_hda_hdmi_setup_audio_infoframe, "SND_HDA_CODEC_HDMI");
  487. /*
  488. * Unsolicited events
  489. */
  490. static void hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll);
  491. void snd_hda_hdmi_check_presence_and_report(struct hda_codec *codec,
  492. hda_nid_t nid, int dev_id)
  493. {
  494. struct hdmi_spec *spec = codec->spec;
  495. int pin_idx = pin_id_to_pin_index(codec, nid, dev_id);
  496. if (pin_idx < 0)
  497. return;
  498. guard(mutex)(&spec->pcm_lock);
  499. hdmi_present_sense(get_pin(spec, pin_idx), 1);
  500. }
  501. EXPORT_SYMBOL_NS_GPL(snd_hda_hdmi_check_presence_and_report,
  502. "SND_HDA_CODEC_HDMI");
  503. static void jack_callback(struct hda_codec *codec,
  504. struct hda_jack_callback *jack)
  505. {
  506. /* stop polling when notification is enabled */
  507. if (codec_has_acomp(codec))
  508. return;
  509. snd_hda_hdmi_check_presence_and_report(codec, jack->nid, jack->dev_id);
  510. }
  511. static void hdmi_intrinsic_event(struct hda_codec *codec, unsigned int res,
  512. struct hda_jack_tbl *jack)
  513. {
  514. jack->jack_dirty = 1;
  515. codec_dbg(codec,
  516. "HDMI hot plug event: Codec=%d NID=0x%x Device=%d Inactive=%d Presence_Detect=%d ELD_Valid=%d\n",
  517. codec->addr, jack->nid, jack->dev_id, !!(res & AC_UNSOL_RES_IA),
  518. !!(res & AC_UNSOL_RES_PD), !!(res & AC_UNSOL_RES_ELDV));
  519. snd_hda_hdmi_check_presence_and_report(codec, jack->nid, jack->dev_id);
  520. }
  521. static void hdmi_non_intrinsic_event(struct hda_codec *codec, unsigned int res)
  522. {
  523. int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
  524. int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
  525. int cp_state = !!(res & AC_UNSOL_RES_CP_STATE);
  526. int cp_ready = !!(res & AC_UNSOL_RES_CP_READY);
  527. codec_info(codec,
  528. "HDMI CP event: CODEC=%d TAG=%d SUBTAG=0x%x CP_STATE=%d CP_READY=%d\n",
  529. codec->addr,
  530. tag,
  531. subtag,
  532. cp_state,
  533. cp_ready);
  534. /* TODO */
  535. if (cp_state) {
  536. ;
  537. }
  538. if (cp_ready) {
  539. ;
  540. }
  541. }
  542. void snd_hda_hdmi_generic_unsol_event(struct hda_codec *codec, unsigned int res)
  543. {
  544. int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
  545. int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
  546. struct hda_jack_tbl *jack;
  547. if (codec_has_acomp(codec))
  548. return;
  549. if (codec->dp_mst) {
  550. int dev_entry =
  551. (res & AC_UNSOL_RES_DE) >> AC_UNSOL_RES_DE_SHIFT;
  552. jack = snd_hda_jack_tbl_get_from_tag(codec, tag, dev_entry);
  553. } else {
  554. jack = snd_hda_jack_tbl_get_from_tag(codec, tag, 0);
  555. }
  556. if (!jack) {
  557. codec_dbg(codec, "Unexpected HDMI event tag 0x%x\n", tag);
  558. return;
  559. }
  560. if (subtag == 0)
  561. hdmi_intrinsic_event(codec, res, jack);
  562. else
  563. hdmi_non_intrinsic_event(codec, res);
  564. }
  565. EXPORT_SYMBOL_NS_GPL(snd_hda_hdmi_generic_unsol_event, "SND_HDA_CODEC_HDMI");
  566. /*
  567. * Callbacks
  568. */
  569. /* HBR should be Non-PCM, 8 channels */
  570. #define is_hbr_format(format) \
  571. ((format & AC_FMT_TYPE_NON_PCM) && (format & AC_FMT_CHAN_MASK) == 7)
  572. static int hdmi_pin_hbr_setup(struct hda_codec *codec, hda_nid_t pin_nid,
  573. int dev_id, bool hbr)
  574. {
  575. int pinctl, new_pinctl;
  576. if (snd_hda_query_pin_caps(codec, pin_nid) & AC_PINCAP_HBR) {
  577. snd_hda_set_dev_select(codec, pin_nid, dev_id);
  578. pinctl = snd_hda_codec_read(codec, pin_nid, 0,
  579. AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
  580. if (pinctl < 0)
  581. return hbr ? -EINVAL : 0;
  582. new_pinctl = pinctl & ~AC_PINCTL_EPT;
  583. if (hbr)
  584. new_pinctl |= AC_PINCTL_EPT_HBR;
  585. else
  586. new_pinctl |= AC_PINCTL_EPT_NATIVE;
  587. codec_dbg(codec,
  588. "hdmi_pin_hbr_setup: NID=0x%x, %spinctl=0x%x\n",
  589. pin_nid,
  590. pinctl == new_pinctl ? "" : "new-",
  591. new_pinctl);
  592. if (pinctl != new_pinctl)
  593. snd_hda_codec_write(codec, pin_nid, 0,
  594. AC_VERB_SET_PIN_WIDGET_CONTROL,
  595. new_pinctl);
  596. } else if (hbr)
  597. return -EINVAL;
  598. return 0;
  599. }
  600. int snd_hda_hdmi_setup_stream(struct hda_codec *codec,
  601. hda_nid_t cvt_nid,
  602. hda_nid_t pin_nid, int dev_id,
  603. u32 stream_tag, int format)
  604. {
  605. struct hdmi_spec *spec = codec->spec;
  606. unsigned int param;
  607. int err;
  608. err = spec->ops.pin_hbr_setup(codec, pin_nid, dev_id,
  609. is_hbr_format(format));
  610. if (err) {
  611. codec_dbg(codec, "hdmi_setup_stream: HBR is not supported\n");
  612. return err;
  613. }
  614. if (spec->intel_hsw_fixup) {
  615. /*
  616. * on recent platforms IEC Coding Type is required for HBR
  617. * support, read current Digital Converter settings and set
  618. * ICT bitfield if needed.
  619. */
  620. param = snd_hda_codec_read(codec, cvt_nid, 0,
  621. AC_VERB_GET_DIGI_CONVERT_1, 0);
  622. param = (param >> 16) & ~(AC_DIG3_ICT);
  623. /* on recent platforms ICT mode is required for HBR support */
  624. if (is_hbr_format(format))
  625. param |= 0x1;
  626. snd_hda_codec_write(codec, cvt_nid, 0,
  627. AC_VERB_SET_DIGI_CONVERT_3, param);
  628. }
  629. snd_hda_codec_setup_stream(codec, cvt_nid, stream_tag, 0, format);
  630. return 0;
  631. }
  632. EXPORT_SYMBOL_NS_GPL(snd_hda_hdmi_setup_stream, "SND_HDA_CODEC_HDMI");
  633. /* Try to find an available converter
  634. * If pin_idx is less then zero, just try to find an available converter.
  635. * Otherwise, try to find an available converter and get the cvt mux index
  636. * of the pin.
  637. */
  638. static int hdmi_choose_cvt(struct hda_codec *codec,
  639. int pin_idx, int *cvt_id,
  640. bool silent)
  641. {
  642. struct hdmi_spec *spec = codec->spec;
  643. struct hdmi_spec_per_pin *per_pin;
  644. struct hdmi_spec_per_cvt *per_cvt = NULL;
  645. int cvt_idx, mux_idx = 0;
  646. /* pin_idx < 0 means no pin will be bound to the converter */
  647. if (pin_idx < 0)
  648. per_pin = NULL;
  649. else
  650. per_pin = get_pin(spec, pin_idx);
  651. if (per_pin && per_pin->silent_stream) {
  652. cvt_idx = cvt_nid_to_cvt_index(codec, per_pin->cvt_nid);
  653. per_cvt = get_cvt(spec, cvt_idx);
  654. if (per_cvt->assigned && !silent)
  655. return -EBUSY;
  656. if (cvt_id)
  657. *cvt_id = cvt_idx;
  658. return 0;
  659. }
  660. /* Dynamically assign converter to stream */
  661. for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
  662. per_cvt = get_cvt(spec, cvt_idx);
  663. /* Must not already be assigned */
  664. if (per_cvt->assigned || per_cvt->silent_stream)
  665. continue;
  666. if (per_pin == NULL)
  667. break;
  668. /* Must be in pin's mux's list of converters */
  669. for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++)
  670. if (per_pin->mux_nids[mux_idx] == per_cvt->cvt_nid)
  671. break;
  672. /* Not in mux list */
  673. if (mux_idx == per_pin->num_mux_nids)
  674. continue;
  675. break;
  676. }
  677. /* No free converters */
  678. if (cvt_idx == spec->num_cvts)
  679. return -EBUSY;
  680. if (per_pin != NULL)
  681. per_pin->mux_idx = mux_idx;
  682. if (cvt_id)
  683. *cvt_id = cvt_idx;
  684. return 0;
  685. }
  686. /* skeleton caller of pin_cvt_fixup ops */
  687. static void pin_cvt_fixup(struct hda_codec *codec,
  688. struct hdmi_spec_per_pin *per_pin,
  689. hda_nid_t cvt_nid)
  690. {
  691. struct hdmi_spec *spec = codec->spec;
  692. if (spec->ops.pin_cvt_fixup)
  693. spec->ops.pin_cvt_fixup(codec, per_pin, cvt_nid);
  694. }
  695. /* called in hdmi_pcm_open when no pin is assigned to the PCM */
  696. static int hdmi_pcm_open_no_pin(struct hda_pcm_stream *hinfo,
  697. struct hda_codec *codec,
  698. struct snd_pcm_substream *substream)
  699. {
  700. struct hdmi_spec *spec = codec->spec;
  701. struct snd_pcm_runtime *runtime = substream->runtime;
  702. int cvt_idx, pcm_idx;
  703. struct hdmi_spec_per_cvt *per_cvt = NULL;
  704. int err;
  705. pcm_idx = hinfo_to_pcm_index(codec, hinfo);
  706. if (pcm_idx < 0)
  707. return -EINVAL;
  708. err = hdmi_choose_cvt(codec, -1, &cvt_idx, false);
  709. if (err)
  710. return err;
  711. per_cvt = get_cvt(spec, cvt_idx);
  712. per_cvt->assigned = true;
  713. hinfo->nid = per_cvt->cvt_nid;
  714. pin_cvt_fixup(codec, NULL, per_cvt->cvt_nid);
  715. set_bit(pcm_idx, &spec->pcm_in_use);
  716. /* todo: setup spdif ctls assign */
  717. /* Initially set the converter's capabilities */
  718. hinfo->channels_min = per_cvt->channels_min;
  719. hinfo->channels_max = per_cvt->channels_max;
  720. hinfo->rates = per_cvt->rates;
  721. hinfo->formats = per_cvt->formats;
  722. hinfo->maxbps = per_cvt->maxbps;
  723. /* Store the updated parameters */
  724. runtime->hw.channels_min = hinfo->channels_min;
  725. runtime->hw.channels_max = hinfo->channels_max;
  726. runtime->hw.formats = hinfo->formats;
  727. runtime->hw.rates = hinfo->rates;
  728. snd_pcm_hw_constraint_step(substream->runtime, 0,
  729. SNDRV_PCM_HW_PARAM_CHANNELS, 2);
  730. return 0;
  731. }
  732. /*
  733. * HDA PCM callbacks
  734. */
  735. static int hdmi_pcm_open(struct hda_pcm_stream *hinfo,
  736. struct hda_codec *codec,
  737. struct snd_pcm_substream *substream)
  738. {
  739. struct hdmi_spec *spec = codec->spec;
  740. struct snd_pcm_runtime *runtime = substream->runtime;
  741. int pin_idx, cvt_idx, pcm_idx;
  742. struct hdmi_spec_per_pin *per_pin;
  743. struct hdmi_eld *eld;
  744. struct hdmi_spec_per_cvt *per_cvt = NULL;
  745. int err;
  746. /* Validate hinfo */
  747. pcm_idx = hinfo_to_pcm_index(codec, hinfo);
  748. if (pcm_idx < 0)
  749. return -EINVAL;
  750. guard(mutex)(&spec->pcm_lock);
  751. pin_idx = hinfo_to_pin_index(codec, hinfo);
  752. /* no pin is assigned to the PCM
  753. * PA need pcm open successfully when probe
  754. */
  755. if (pin_idx < 0)
  756. return hdmi_pcm_open_no_pin(hinfo, codec, substream);
  757. err = hdmi_choose_cvt(codec, pin_idx, &cvt_idx, false);
  758. if (err < 0)
  759. return err;
  760. per_cvt = get_cvt(spec, cvt_idx);
  761. /* Claim converter */
  762. per_cvt->assigned = true;
  763. set_bit(pcm_idx, &spec->pcm_in_use);
  764. per_pin = get_pin(spec, pin_idx);
  765. per_pin->cvt_nid = per_cvt->cvt_nid;
  766. hinfo->nid = per_cvt->cvt_nid;
  767. /* flip stripe flag for the assigned stream if supported */
  768. if (get_wcaps(codec, per_cvt->cvt_nid) & AC_WCAP_STRIPE)
  769. azx_stream(get_azx_dev(substream))->stripe = 1;
  770. snd_hda_set_dev_select(codec, per_pin->pin_nid, per_pin->dev_id);
  771. snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0,
  772. AC_VERB_SET_CONNECT_SEL,
  773. per_pin->mux_idx);
  774. /* configure unused pins to choose other converters */
  775. pin_cvt_fixup(codec, per_pin, 0);
  776. snd_hda_spdif_ctls_assign(codec, pcm_idx, per_cvt->cvt_nid);
  777. /* Initially set the converter's capabilities */
  778. hinfo->channels_min = per_cvt->channels_min;
  779. hinfo->channels_max = per_cvt->channels_max;
  780. hinfo->rates = per_cvt->rates;
  781. hinfo->formats = per_cvt->formats;
  782. hinfo->maxbps = per_cvt->maxbps;
  783. eld = &per_pin->sink_eld;
  784. /* Restrict capabilities by ELD if this isn't disabled */
  785. if (!static_hdmi_pcm && eld->eld_valid) {
  786. snd_hdmi_eld_update_pcm_info(&eld->info, hinfo);
  787. if (hinfo->channels_min > hinfo->channels_max ||
  788. !hinfo->rates || !hinfo->formats) {
  789. per_cvt->assigned = false;
  790. hinfo->nid = 0;
  791. snd_hda_spdif_ctls_unassign(codec, pcm_idx);
  792. return -ENODEV;
  793. }
  794. }
  795. /* Store the updated parameters */
  796. runtime->hw.channels_min = hinfo->channels_min;
  797. runtime->hw.channels_max = hinfo->channels_max;
  798. runtime->hw.formats = hinfo->formats;
  799. runtime->hw.rates = hinfo->rates;
  800. snd_pcm_hw_constraint_step(substream->runtime, 0,
  801. SNDRV_PCM_HW_PARAM_CHANNELS, 2);
  802. return 0;
  803. }
  804. /*
  805. * HDA/HDMI auto parsing
  806. */
  807. static int hdmi_read_pin_conn(struct hda_codec *codec, int pin_idx)
  808. {
  809. struct hdmi_spec *spec = codec->spec;
  810. struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
  811. hda_nid_t pin_nid = per_pin->pin_nid;
  812. int dev_id = per_pin->dev_id;
  813. int conns;
  814. if (!(get_wcaps(codec, pin_nid) & AC_WCAP_CONN_LIST)) {
  815. codec_warn(codec,
  816. "HDMI: pin NID 0x%x wcaps %#x does not support connection list\n",
  817. pin_nid, get_wcaps(codec, pin_nid));
  818. return -EINVAL;
  819. }
  820. snd_hda_set_dev_select(codec, pin_nid, dev_id);
  821. if (spec->intel_hsw_fixup) {
  822. conns = spec->num_cvts;
  823. memcpy(per_pin->mux_nids, spec->cvt_nids,
  824. sizeof(hda_nid_t) * conns);
  825. } else {
  826. conns = snd_hda_get_raw_connections(codec, pin_nid,
  827. per_pin->mux_nids,
  828. HDA_MAX_CONNECTIONS);
  829. }
  830. /* all the device entries on the same pin have the same conn list */
  831. per_pin->num_mux_nids = conns;
  832. return 0;
  833. }
  834. static int hdmi_find_pcm_slot(struct hdmi_spec *spec,
  835. struct hdmi_spec_per_pin *per_pin)
  836. {
  837. int i;
  838. for (i = 0; i < spec->pcm_used; i++) {
  839. if (!test_bit(i, &spec->pcm_bitmap))
  840. return i;
  841. }
  842. return -EBUSY;
  843. }
  844. static void hdmi_attach_hda_pcm(struct hdmi_spec *spec,
  845. struct hdmi_spec_per_pin *per_pin)
  846. {
  847. int idx;
  848. /* pcm already be attached to the pin */
  849. if (per_pin->pcm)
  850. return;
  851. /* try the previously used slot at first */
  852. idx = per_pin->prev_pcm_idx;
  853. if (idx >= 0) {
  854. if (!test_bit(idx, &spec->pcm_bitmap))
  855. goto found;
  856. per_pin->prev_pcm_idx = -1; /* no longer valid, clear it */
  857. }
  858. idx = hdmi_find_pcm_slot(spec, per_pin);
  859. if (idx == -EBUSY)
  860. return;
  861. found:
  862. per_pin->pcm_idx = idx;
  863. per_pin->pcm = get_hdmi_pcm(spec, idx);
  864. set_bit(idx, &spec->pcm_bitmap);
  865. }
  866. static void hdmi_detach_hda_pcm(struct hdmi_spec *spec,
  867. struct hdmi_spec_per_pin *per_pin)
  868. {
  869. int idx;
  870. /* pcm already be detached from the pin */
  871. if (!per_pin->pcm)
  872. return;
  873. idx = per_pin->pcm_idx;
  874. per_pin->pcm_idx = -1;
  875. per_pin->prev_pcm_idx = idx; /* remember the previous index */
  876. per_pin->pcm = NULL;
  877. if (idx >= 0 && idx < spec->pcm_used)
  878. clear_bit(idx, &spec->pcm_bitmap);
  879. }
  880. static int hdmi_get_pin_cvt_mux(struct hdmi_spec *spec,
  881. struct hdmi_spec_per_pin *per_pin, hda_nid_t cvt_nid)
  882. {
  883. int mux_idx;
  884. for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++)
  885. if (per_pin->mux_nids[mux_idx] == cvt_nid)
  886. break;
  887. return mux_idx;
  888. }
  889. static bool check_non_pcm_per_cvt(struct hda_codec *codec, hda_nid_t cvt_nid);
  890. static void hdmi_pcm_setup_pin(struct hdmi_spec *spec,
  891. struct hdmi_spec_per_pin *per_pin)
  892. {
  893. struct hda_codec *codec = per_pin->codec;
  894. struct hda_pcm *pcm;
  895. struct hda_pcm_stream *hinfo;
  896. struct snd_pcm_substream *substream;
  897. int mux_idx;
  898. bool non_pcm;
  899. if (per_pin->pcm_idx < 0 || per_pin->pcm_idx >= spec->pcm_used)
  900. return;
  901. pcm = get_pcm_rec(spec, per_pin->pcm_idx);
  902. if (!pcm->pcm)
  903. return;
  904. if (!test_bit(per_pin->pcm_idx, &spec->pcm_in_use))
  905. return;
  906. /* hdmi audio only uses playback and one substream */
  907. hinfo = pcm->stream;
  908. substream = pcm->pcm->streams[0].substream;
  909. per_pin->cvt_nid = hinfo->nid;
  910. mux_idx = hdmi_get_pin_cvt_mux(spec, per_pin, hinfo->nid);
  911. if (mux_idx < per_pin->num_mux_nids) {
  912. snd_hda_set_dev_select(codec, per_pin->pin_nid,
  913. per_pin->dev_id);
  914. snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0,
  915. AC_VERB_SET_CONNECT_SEL,
  916. mux_idx);
  917. }
  918. snd_hda_spdif_ctls_assign(codec, per_pin->pcm_idx, hinfo->nid);
  919. non_pcm = check_non_pcm_per_cvt(codec, hinfo->nid);
  920. if (substream->runtime)
  921. per_pin->channels = substream->runtime->channels;
  922. per_pin->setup = true;
  923. per_pin->mux_idx = mux_idx;
  924. snd_hda_hdmi_setup_audio_infoframe(codec, per_pin, non_pcm);
  925. }
  926. static void hdmi_pcm_reset_pin(struct hdmi_spec *spec,
  927. struct hdmi_spec_per_pin *per_pin)
  928. {
  929. if (per_pin->pcm_idx >= 0 && per_pin->pcm_idx < spec->pcm_used)
  930. snd_hda_spdif_ctls_unassign(per_pin->codec, per_pin->pcm_idx);
  931. per_pin->chmap_set = false;
  932. memset(per_pin->chmap, 0, sizeof(per_pin->chmap));
  933. per_pin->setup = false;
  934. per_pin->channels = 0;
  935. }
  936. static struct snd_jack *pin_idx_to_pcm_jack(struct hda_codec *codec,
  937. struct hdmi_spec_per_pin *per_pin)
  938. {
  939. struct hdmi_spec *spec = codec->spec;
  940. if (per_pin->pcm_idx >= 0)
  941. return spec->pcm_rec[per_pin->pcm_idx].jack;
  942. else
  943. return NULL;
  944. }
  945. /* update per_pin ELD from the given new ELD;
  946. * setup info frame and notification accordingly
  947. * also notify ELD kctl and report jack status changes
  948. */
  949. static void update_eld(struct hda_codec *codec,
  950. struct hdmi_spec_per_pin *per_pin,
  951. struct hdmi_eld *eld,
  952. int repoll)
  953. {
  954. struct hdmi_eld *pin_eld = &per_pin->sink_eld;
  955. struct hdmi_spec *spec = codec->spec;
  956. struct snd_jack *pcm_jack;
  957. bool old_eld_valid = pin_eld->eld_valid;
  958. bool eld_changed;
  959. int pcm_idx;
  960. if (eld->eld_valid) {
  961. if (eld->eld_size <= 0 ||
  962. snd_parse_eld(hda_codec_dev(codec), &eld->info,
  963. eld->eld_buffer, eld->eld_size) < 0) {
  964. eld->eld_valid = false;
  965. if (repoll) {
  966. schedule_delayed_work(&per_pin->work,
  967. msecs_to_jiffies(300));
  968. return;
  969. }
  970. }
  971. }
  972. if (!eld->eld_valid || eld->eld_size <= 0 || eld->info.sad_count <= 0) {
  973. eld->eld_valid = false;
  974. eld->eld_size = 0;
  975. }
  976. /* for monitor disconnection, save pcm_idx firstly */
  977. pcm_idx = per_pin->pcm_idx;
  978. /*
  979. * pcm_idx >=0 before update_eld() means it is in monitor
  980. * disconnected event. Jack must be fetched before update_eld().
  981. */
  982. pcm_jack = pin_idx_to_pcm_jack(codec, per_pin);
  983. if (!spec->static_pcm_mapping) {
  984. if (eld->eld_valid) {
  985. hdmi_attach_hda_pcm(spec, per_pin);
  986. hdmi_pcm_setup_pin(spec, per_pin);
  987. } else {
  988. hdmi_pcm_reset_pin(spec, per_pin);
  989. hdmi_detach_hda_pcm(spec, per_pin);
  990. }
  991. }
  992. /* if pcm_idx == -1, it means this is in monitor connection event
  993. * we can get the correct pcm_idx now.
  994. */
  995. if (pcm_idx == -1)
  996. pcm_idx = per_pin->pcm_idx;
  997. if (!pcm_jack)
  998. pcm_jack = pin_idx_to_pcm_jack(codec, per_pin);
  999. if (eld->eld_valid)
  1000. snd_show_eld(hda_codec_dev(codec), &eld->info);
  1001. eld_changed = (pin_eld->eld_valid != eld->eld_valid);
  1002. eld_changed |= (pin_eld->monitor_present != eld->monitor_present);
  1003. if (!eld_changed && eld->eld_valid && pin_eld->eld_valid)
  1004. if (pin_eld->eld_size != eld->eld_size ||
  1005. memcmp(pin_eld->eld_buffer, eld->eld_buffer,
  1006. eld->eld_size) != 0)
  1007. eld_changed = true;
  1008. if (eld_changed) {
  1009. pin_eld->monitor_present = eld->monitor_present;
  1010. pin_eld->eld_valid = eld->eld_valid;
  1011. pin_eld->eld_size = eld->eld_size;
  1012. if (eld->eld_valid)
  1013. memcpy(pin_eld->eld_buffer, eld->eld_buffer,
  1014. eld->eld_size);
  1015. pin_eld->info = eld->info;
  1016. }
  1017. /*
  1018. * Re-setup pin and infoframe. This is needed e.g. when
  1019. * - sink is first plugged-in
  1020. * - transcoder can change during stream playback on Haswell
  1021. * and this can make HW reset converter selection on a pin.
  1022. */
  1023. if (eld->eld_valid && !old_eld_valid && per_pin->setup) {
  1024. pin_cvt_fixup(codec, per_pin, 0);
  1025. snd_hda_hdmi_setup_audio_infoframe(codec, per_pin, per_pin->non_pcm);
  1026. }
  1027. if (eld_changed && pcm_idx >= 0)
  1028. snd_ctl_notify(codec->card,
  1029. SNDRV_CTL_EVENT_MASK_VALUE |
  1030. SNDRV_CTL_EVENT_MASK_INFO,
  1031. &get_hdmi_pcm(spec, pcm_idx)->eld_ctl->id);
  1032. if (eld_changed && pcm_jack)
  1033. snd_jack_report(pcm_jack,
  1034. (eld->monitor_present && eld->eld_valid) ?
  1035. SND_JACK_AVOUT : 0);
  1036. }
  1037. /* update ELD and jack state via HD-audio verbs */
  1038. static void hdmi_present_sense_via_verbs(struct hdmi_spec_per_pin *per_pin,
  1039. int repoll)
  1040. {
  1041. struct hda_codec *codec = per_pin->codec;
  1042. struct hdmi_spec *spec = codec->spec;
  1043. struct hdmi_eld *eld = &spec->temp_eld;
  1044. struct device *dev = hda_codec_dev(codec);
  1045. hda_nid_t pin_nid = per_pin->pin_nid;
  1046. int dev_id = per_pin->dev_id;
  1047. /*
  1048. * Always execute a GetPinSense verb here, even when called from
  1049. * hdmi_intrinsic_event; for some NVIDIA HW, the unsolicited
  1050. * response's PD bit is not the real PD value, but indicates that
  1051. * the real PD value changed. An older version of the HD-audio
  1052. * specification worked this way. Hence, we just ignore the data in
  1053. * the unsolicited response to avoid custom WARs.
  1054. */
  1055. int present;
  1056. #ifdef CONFIG_PM
  1057. if (dev->power.runtime_status == RPM_SUSPENDING)
  1058. return;
  1059. #endif
  1060. CLASS(snd_hda_power_pm, pm)(codec);
  1061. if (pm.err < 0 && pm_runtime_suspended(dev))
  1062. return;
  1063. present = snd_hda_jack_pin_sense(codec, pin_nid, dev_id);
  1064. guard(mutex)(&per_pin->lock);
  1065. eld->monitor_present = !!(present & AC_PINSENSE_PRESENCE);
  1066. if (eld->monitor_present)
  1067. eld->eld_valid = !!(present & AC_PINSENSE_ELDV);
  1068. else
  1069. eld->eld_valid = false;
  1070. codec_dbg(codec,
  1071. "HDMI status: Codec=%d NID=0x%x Presence_Detect=%d ELD_Valid=%d\n",
  1072. codec->addr, pin_nid, eld->monitor_present, eld->eld_valid);
  1073. if (eld->eld_valid) {
  1074. if (spec->ops.pin_get_eld(codec, pin_nid, dev_id,
  1075. eld->eld_buffer, &eld->eld_size) < 0)
  1076. eld->eld_valid = false;
  1077. }
  1078. update_eld(codec, per_pin, eld, repoll);
  1079. }
  1080. static void silent_stream_enable(struct hda_codec *codec,
  1081. struct hdmi_spec_per_pin *per_pin)
  1082. {
  1083. struct hdmi_spec *spec = codec->spec;
  1084. struct hdmi_spec_per_cvt *per_cvt;
  1085. int cvt_idx, pin_idx, err;
  1086. /*
  1087. * Power-up will call hdmi_present_sense, so the PM calls
  1088. * have to be done without mutex held.
  1089. */
  1090. CLASS(snd_hda_power_pm, pm)(codec);
  1091. if (pm.err < 0 && pm.err != -EACCES) {
  1092. codec_err(codec,
  1093. "Failed to power up codec for silent stream enable ret=[%d]\n", pm.err);
  1094. return;
  1095. }
  1096. guard(mutex)(&per_pin->lock);
  1097. if (per_pin->setup) {
  1098. codec_dbg(codec, "hdmi: PCM already open, no silent stream\n");
  1099. return;
  1100. }
  1101. pin_idx = pin_id_to_pin_index(codec, per_pin->pin_nid, per_pin->dev_id);
  1102. err = hdmi_choose_cvt(codec, pin_idx, &cvt_idx, true);
  1103. if (err) {
  1104. codec_err(codec, "hdmi: no free converter to enable silent mode\n");
  1105. return;
  1106. }
  1107. per_cvt = get_cvt(spec, cvt_idx);
  1108. per_cvt->silent_stream = true;
  1109. per_pin->cvt_nid = per_cvt->cvt_nid;
  1110. per_pin->silent_stream = true;
  1111. codec_dbg(codec, "hdmi: enabling silent stream pin-NID=0x%x cvt-NID=0x%x\n",
  1112. per_pin->pin_nid, per_cvt->cvt_nid);
  1113. snd_hda_set_dev_select(codec, per_pin->pin_nid, per_pin->dev_id);
  1114. snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0,
  1115. AC_VERB_SET_CONNECT_SEL,
  1116. per_pin->mux_idx);
  1117. /* configure unused pins to choose other converters */
  1118. pin_cvt_fixup(codec, per_pin, 0);
  1119. spec->ops.silent_stream(codec, per_pin, true);
  1120. }
  1121. static void silent_stream_disable(struct hda_codec *codec,
  1122. struct hdmi_spec_per_pin *per_pin)
  1123. {
  1124. struct hdmi_spec *spec = codec->spec;
  1125. struct hdmi_spec_per_cvt *per_cvt;
  1126. int cvt_idx;
  1127. CLASS(snd_hda_power_pm, pm)(codec);
  1128. if (pm.err < 0 && pm.err != -EACCES) {
  1129. codec_err(codec,
  1130. "Failed to power up codec for silent stream disable ret=[%d]\n",
  1131. pm.err);
  1132. return;
  1133. }
  1134. guard(mutex)(&per_pin->lock);
  1135. if (!per_pin->silent_stream)
  1136. return;
  1137. codec_dbg(codec, "HDMI: disable silent stream on pin-NID=0x%x cvt-NID=0x%x\n",
  1138. per_pin->pin_nid, per_pin->cvt_nid);
  1139. cvt_idx = cvt_nid_to_cvt_index(codec, per_pin->cvt_nid);
  1140. if (cvt_idx >= 0 && cvt_idx < spec->num_cvts) {
  1141. per_cvt = get_cvt(spec, cvt_idx);
  1142. per_cvt->silent_stream = false;
  1143. }
  1144. spec->ops.silent_stream(codec, per_pin, false);
  1145. per_pin->cvt_nid = 0;
  1146. per_pin->silent_stream = false;
  1147. }
  1148. /* update ELD and jack state via audio component */
  1149. static void sync_eld_via_acomp(struct hda_codec *codec,
  1150. struct hdmi_spec_per_pin *per_pin)
  1151. {
  1152. struct hdmi_spec *spec = codec->spec;
  1153. struct hdmi_eld *eld = &spec->temp_eld;
  1154. bool monitor_prev, monitor_next;
  1155. scoped_guard(mutex, &per_pin->lock) {
  1156. eld->monitor_present = false;
  1157. monitor_prev = per_pin->sink_eld.monitor_present;
  1158. eld->eld_size = snd_hdac_acomp_get_eld(&codec->core, per_pin->pin_nid,
  1159. per_pin->dev_id, &eld->monitor_present,
  1160. eld->eld_buffer, ELD_MAX_SIZE);
  1161. eld->eld_valid = (eld->eld_size > 0);
  1162. update_eld(codec, per_pin, eld, 0);
  1163. monitor_next = per_pin->sink_eld.monitor_present;
  1164. }
  1165. if (spec->silent_stream_type) {
  1166. if (!monitor_prev && monitor_next)
  1167. silent_stream_enable(codec, per_pin);
  1168. else if (monitor_prev && !monitor_next)
  1169. silent_stream_disable(codec, per_pin);
  1170. }
  1171. }
  1172. static void hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll)
  1173. {
  1174. struct hda_codec *codec = per_pin->codec;
  1175. if (!codec_has_acomp(codec))
  1176. hdmi_present_sense_via_verbs(per_pin, repoll);
  1177. else
  1178. sync_eld_via_acomp(codec, per_pin);
  1179. }
  1180. static void hdmi_repoll_eld(struct work_struct *work)
  1181. {
  1182. struct hdmi_spec_per_pin *per_pin =
  1183. container_of(to_delayed_work(work), struct hdmi_spec_per_pin, work);
  1184. struct hda_codec *codec = per_pin->codec;
  1185. struct hdmi_spec *spec = codec->spec;
  1186. struct hda_jack_tbl *jack;
  1187. jack = snd_hda_jack_tbl_get_mst(codec, per_pin->pin_nid,
  1188. per_pin->dev_id);
  1189. if (jack)
  1190. jack->jack_dirty = 1;
  1191. if (per_pin->repoll_count++ > 6)
  1192. per_pin->repoll_count = 0;
  1193. guard(mutex)(&spec->pcm_lock);
  1194. hdmi_present_sense(per_pin, per_pin->repoll_count);
  1195. }
  1196. static int hdmi_add_pin(struct hda_codec *codec, hda_nid_t pin_nid)
  1197. {
  1198. struct hdmi_spec *spec = codec->spec;
  1199. unsigned int caps, config;
  1200. int pin_idx;
  1201. struct hdmi_spec_per_pin *per_pin;
  1202. int err;
  1203. int dev_num, i;
  1204. caps = snd_hda_query_pin_caps(codec, pin_nid);
  1205. if (!(caps & (AC_PINCAP_HDMI | AC_PINCAP_DP)))
  1206. return 0;
  1207. /*
  1208. * For DP MST audio, Configuration Default is the same for
  1209. * all device entries on the same pin
  1210. */
  1211. config = snd_hda_codec_get_pincfg(codec, pin_nid);
  1212. if (get_defcfg_connect(config) == AC_JACK_PORT_NONE &&
  1213. !spec->force_connect)
  1214. return 0;
  1215. /*
  1216. * To simplify the implementation, malloc all
  1217. * the virtual pins in the initialization statically
  1218. */
  1219. if (spec->intel_hsw_fixup) {
  1220. /*
  1221. * On Intel platforms, device entries count returned
  1222. * by AC_PAR_DEVLIST_LEN is dynamic, and depends on
  1223. * the type of receiver that is connected. Allocate pin
  1224. * structures based on worst case.
  1225. */
  1226. dev_num = spec->dev_num;
  1227. } else if (codec->dp_mst) {
  1228. dev_num = snd_hda_get_num_devices(codec, pin_nid) + 1;
  1229. /*
  1230. * spec->dev_num is the maxinum number of device entries
  1231. * among all the pins
  1232. */
  1233. spec->dev_num = (spec->dev_num > dev_num) ?
  1234. spec->dev_num : dev_num;
  1235. } else {
  1236. /*
  1237. * If the platform doesn't support DP MST,
  1238. * manually set dev_num to 1. This means
  1239. * the pin has only one device entry.
  1240. */
  1241. dev_num = 1;
  1242. spec->dev_num = 1;
  1243. }
  1244. for (i = 0; i < dev_num; i++) {
  1245. pin_idx = spec->num_pins;
  1246. per_pin = snd_array_new(&spec->pins);
  1247. if (!per_pin)
  1248. return -ENOMEM;
  1249. per_pin->pcm = NULL;
  1250. per_pin->pcm_idx = -1;
  1251. per_pin->prev_pcm_idx = -1;
  1252. per_pin->pin_nid = pin_nid;
  1253. per_pin->pin_nid_idx = spec->num_nids;
  1254. per_pin->dev_id = i;
  1255. per_pin->non_pcm = false;
  1256. snd_hda_set_dev_select(codec, pin_nid, i);
  1257. err = hdmi_read_pin_conn(codec, pin_idx);
  1258. if (err < 0)
  1259. return err;
  1260. if (!is_jack_detectable(codec, pin_nid))
  1261. codec_warn(codec, "HDMI: pin NID 0x%x - jack not detectable\n", pin_nid);
  1262. spec->num_pins++;
  1263. }
  1264. spec->num_nids++;
  1265. return 0;
  1266. }
  1267. static int hdmi_add_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
  1268. {
  1269. struct hdmi_spec *spec = codec->spec;
  1270. struct hdmi_spec_per_cvt *per_cvt;
  1271. unsigned int chans;
  1272. int err;
  1273. chans = get_wcaps(codec, cvt_nid);
  1274. chans = get_wcaps_channels(chans);
  1275. per_cvt = snd_array_new(&spec->cvts);
  1276. if (!per_cvt)
  1277. return -ENOMEM;
  1278. per_cvt->cvt_nid = cvt_nid;
  1279. per_cvt->channels_min = 2;
  1280. if (chans <= 16) {
  1281. per_cvt->channels_max = chans;
  1282. if (chans > spec->chmap.channels_max)
  1283. spec->chmap.channels_max = chans;
  1284. }
  1285. err = snd_hda_query_supported_pcm(codec, cvt_nid,
  1286. &per_cvt->rates,
  1287. &per_cvt->formats,
  1288. NULL,
  1289. &per_cvt->maxbps);
  1290. if (err < 0)
  1291. return err;
  1292. if (spec->num_cvts < ARRAY_SIZE(spec->cvt_nids))
  1293. spec->cvt_nids[spec->num_cvts] = cvt_nid;
  1294. spec->num_cvts++;
  1295. return 0;
  1296. }
  1297. static const struct snd_pci_quirk force_connect_list[] = {
  1298. SND_PCI_QUIRK(0x103c, 0x83e2, "HP EliteDesk 800 G4", 1),
  1299. SND_PCI_QUIRK(0x103c, 0x83ef, "HP MP9 G4 Retail System AMS", 1),
  1300. SND_PCI_QUIRK(0x103c, 0x845a, "HP EliteDesk 800 G4 DM 65W", 1),
  1301. SND_PCI_QUIRK(0x103c, 0x83f3, "HP ProDesk 400", 1),
  1302. SND_PCI_QUIRK(0x103c, 0x870f, "HP", 1),
  1303. SND_PCI_QUIRK(0x103c, 0x871a, "HP", 1),
  1304. SND_PCI_QUIRK(0x103c, 0x8711, "HP", 1),
  1305. SND_PCI_QUIRK(0x103c, 0x8715, "HP", 1),
  1306. SND_PCI_QUIRK(0x1043, 0x86ae, "ASUS", 1), /* Z170 PRO */
  1307. SND_PCI_QUIRK(0x1043, 0x86c7, "ASUS", 1), /* Z170M PLUS */
  1308. SND_PCI_QUIRK(0x1462, 0xec94, "MS-7C94", 1),
  1309. SND_PCI_QUIRK(0x1558, 0x14a1, "TUXEDO InfinityBook S 14 Gen6", 1),
  1310. SND_PCI_QUIRK(0x8086, 0x2060, "Intel NUC5CPYB", 1),
  1311. SND_PCI_QUIRK(0x8086, 0x2081, "Intel NUC 10", 1),
  1312. {}
  1313. };
  1314. int snd_hda_hdmi_parse_codec(struct hda_codec *codec)
  1315. {
  1316. struct hdmi_spec *spec = codec->spec;
  1317. hda_nid_t start_nid;
  1318. unsigned int caps;
  1319. int i, nodes;
  1320. const struct snd_pci_quirk *q;
  1321. nodes = snd_hda_get_sub_nodes(codec, codec->core.afg, &start_nid);
  1322. if (!start_nid || nodes < 0) {
  1323. codec_warn(codec, "HDMI: failed to get afg sub nodes\n");
  1324. return -EINVAL;
  1325. }
  1326. if (enable_all_pins)
  1327. spec->force_connect = true;
  1328. q = snd_pci_quirk_lookup(codec->bus->pci, force_connect_list);
  1329. if (q && q->value)
  1330. spec->force_connect = true;
  1331. /*
  1332. * hdmi_add_pin() assumes total amount of converters to
  1333. * be known, so first discover all converters
  1334. */
  1335. for (i = 0; i < nodes; i++) {
  1336. hda_nid_t nid = start_nid + i;
  1337. caps = get_wcaps(codec, nid);
  1338. if (!(caps & AC_WCAP_DIGITAL))
  1339. continue;
  1340. if (get_wcaps_type(caps) == AC_WID_AUD_OUT)
  1341. hdmi_add_cvt(codec, nid);
  1342. }
  1343. /* discover audio pins */
  1344. for (i = 0; i < nodes; i++) {
  1345. hda_nid_t nid = start_nid + i;
  1346. caps = get_wcaps(codec, nid);
  1347. if (!(caps & AC_WCAP_DIGITAL))
  1348. continue;
  1349. if (get_wcaps_type(caps) == AC_WID_PIN)
  1350. hdmi_add_pin(codec, nid);
  1351. }
  1352. return 0;
  1353. }
  1354. EXPORT_SYMBOL_NS_GPL(snd_hda_hdmi_parse_codec, "SND_HDA_CODEC_HDMI");
  1355. /*
  1356. */
  1357. static bool check_non_pcm_per_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
  1358. {
  1359. struct hda_spdif_out *spdif;
  1360. guard(mutex)(&codec->spdif_mutex);
  1361. spdif = snd_hda_spdif_out_of_nid(codec, cvt_nid);
  1362. /* Add sanity check to pass klockwork check.
  1363. * This should never happen.
  1364. */
  1365. if (WARN_ON(spdif == NULL))
  1366. return true;
  1367. return !!(spdif->status & IEC958_AES0_NONAUDIO);
  1368. }
  1369. /*
  1370. * HDMI callbacks
  1371. */
  1372. int snd_hda_hdmi_generic_pcm_prepare(struct hda_pcm_stream *hinfo,
  1373. struct hda_codec *codec,
  1374. unsigned int stream_tag,
  1375. unsigned int format,
  1376. struct snd_pcm_substream *substream)
  1377. {
  1378. hda_nid_t cvt_nid = hinfo->nid;
  1379. struct hdmi_spec *spec = codec->spec;
  1380. int pin_idx;
  1381. struct hdmi_spec_per_pin *per_pin;
  1382. struct snd_pcm_runtime *runtime = substream->runtime;
  1383. bool non_pcm;
  1384. int pinctl, stripe;
  1385. guard(mutex)(&spec->pcm_lock);
  1386. pin_idx = hinfo_to_pin_index(codec, hinfo);
  1387. if (pin_idx < 0) {
  1388. /* when pcm is not bound to a pin skip pin setup and return 0
  1389. * to make audio playback be ongoing
  1390. */
  1391. pin_cvt_fixup(codec, NULL, cvt_nid);
  1392. snd_hda_codec_setup_stream(codec, cvt_nid,
  1393. stream_tag, 0, format);
  1394. return 0;
  1395. }
  1396. per_pin = get_pin(spec, pin_idx);
  1397. /* Verify pin:cvt selections to avoid silent audio after S3.
  1398. * After S3, the audio driver restores pin:cvt selections
  1399. * but this can happen before gfx is ready and such selection
  1400. * is overlooked by HW. Thus multiple pins can share a same
  1401. * default convertor and mute control will affect each other,
  1402. * which can cause a resumed audio playback become silent
  1403. * after S3.
  1404. */
  1405. pin_cvt_fixup(codec, per_pin, 0);
  1406. /* Call sync_audio_rate to set the N/CTS/M manually if necessary */
  1407. /* Todo: add DP1.2 MST audio support later */
  1408. if (codec_has_acomp(codec))
  1409. snd_hdac_sync_audio_rate(&codec->core, per_pin->pin_nid,
  1410. per_pin->dev_id, runtime->rate);
  1411. non_pcm = check_non_pcm_per_cvt(codec, cvt_nid);
  1412. scoped_guard(mutex, &per_pin->lock) {
  1413. per_pin->channels = substream->runtime->channels;
  1414. per_pin->setup = true;
  1415. if (get_wcaps(codec, cvt_nid) & AC_WCAP_STRIPE) {
  1416. stripe = snd_hdac_get_stream_stripe_ctl(&codec->bus->core,
  1417. substream);
  1418. snd_hda_codec_write(codec, cvt_nid, 0,
  1419. AC_VERB_SET_STRIPE_CONTROL,
  1420. stripe);
  1421. }
  1422. snd_hda_hdmi_setup_audio_infoframe(codec, per_pin, non_pcm);
  1423. }
  1424. if (spec->dyn_pin_out) {
  1425. snd_hda_set_dev_select(codec, per_pin->pin_nid,
  1426. per_pin->dev_id);
  1427. pinctl = snd_hda_codec_read(codec, per_pin->pin_nid, 0,
  1428. AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
  1429. snd_hda_codec_write(codec, per_pin->pin_nid, 0,
  1430. AC_VERB_SET_PIN_WIDGET_CONTROL,
  1431. pinctl | PIN_OUT);
  1432. }
  1433. /* snd_hda_set_dev_select() has been called before */
  1434. return spec->ops.setup_stream(codec, cvt_nid, per_pin->pin_nid,
  1435. per_pin->dev_id, stream_tag, format);
  1436. }
  1437. EXPORT_SYMBOL_NS_GPL(snd_hda_hdmi_generic_pcm_prepare, "SND_HDA_CODEC_HDMI");
  1438. int snd_hda_hdmi_generic_pcm_cleanup(struct hda_pcm_stream *hinfo,
  1439. struct hda_codec *codec,
  1440. struct snd_pcm_substream *substream)
  1441. {
  1442. snd_hda_codec_cleanup_stream(codec, hinfo->nid);
  1443. return 0;
  1444. }
  1445. EXPORT_SYMBOL_NS_GPL(snd_hda_hdmi_generic_pcm_cleanup, "SND_HDA_CODEC_HDMI");
  1446. static int hdmi_pcm_close(struct hda_pcm_stream *hinfo,
  1447. struct hda_codec *codec,
  1448. struct snd_pcm_substream *substream)
  1449. {
  1450. struct hdmi_spec *spec = codec->spec;
  1451. int cvt_idx, pin_idx, pcm_idx;
  1452. struct hdmi_spec_per_cvt *per_cvt;
  1453. struct hdmi_spec_per_pin *per_pin;
  1454. int pinctl;
  1455. guard(mutex)(&spec->pcm_lock);
  1456. if (hinfo->nid) {
  1457. pcm_idx = hinfo_to_pcm_index(codec, hinfo);
  1458. if (snd_BUG_ON(pcm_idx < 0))
  1459. return -EINVAL;
  1460. cvt_idx = cvt_nid_to_cvt_index(codec, hinfo->nid);
  1461. if (snd_BUG_ON(cvt_idx < 0))
  1462. return -EINVAL;
  1463. per_cvt = get_cvt(spec, cvt_idx);
  1464. per_cvt->assigned = false;
  1465. hinfo->nid = 0;
  1466. azx_stream(get_azx_dev(substream))->stripe = 0;
  1467. snd_hda_spdif_ctls_unassign(codec, pcm_idx);
  1468. clear_bit(pcm_idx, &spec->pcm_in_use);
  1469. pin_idx = hinfo_to_pin_index(codec, hinfo);
  1470. /*
  1471. * In such a case, return 0 to match the behavior in
  1472. * hdmi_pcm_open()
  1473. */
  1474. if (pin_idx < 0)
  1475. return 0;
  1476. per_pin = get_pin(spec, pin_idx);
  1477. if (spec->dyn_pin_out) {
  1478. snd_hda_set_dev_select(codec, per_pin->pin_nid,
  1479. per_pin->dev_id);
  1480. pinctl = snd_hda_codec_read(codec, per_pin->pin_nid, 0,
  1481. AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
  1482. snd_hda_codec_write(codec, per_pin->pin_nid, 0,
  1483. AC_VERB_SET_PIN_WIDGET_CONTROL,
  1484. pinctl & ~PIN_OUT);
  1485. }
  1486. guard(mutex)(&per_pin->lock);
  1487. per_pin->chmap_set = false;
  1488. memset(per_pin->chmap, 0, sizeof(per_pin->chmap));
  1489. per_pin->setup = false;
  1490. per_pin->channels = 0;
  1491. }
  1492. return 0;
  1493. }
  1494. static const struct hda_pcm_ops generic_ops = {
  1495. .open = hdmi_pcm_open,
  1496. .close = hdmi_pcm_close,
  1497. .prepare = snd_hda_hdmi_generic_pcm_prepare,
  1498. .cleanup = snd_hda_hdmi_generic_pcm_cleanup,
  1499. };
  1500. static int hdmi_get_spk_alloc(struct hdac_device *hdac, int pcm_idx)
  1501. {
  1502. struct hda_codec *codec = hdac_to_hda_codec(hdac);
  1503. struct hdmi_spec *spec = codec->spec;
  1504. struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
  1505. if (!per_pin)
  1506. return 0;
  1507. return per_pin->sink_eld.info.spk_alloc;
  1508. }
  1509. static void hdmi_get_chmap(struct hdac_device *hdac, int pcm_idx,
  1510. unsigned char *chmap)
  1511. {
  1512. struct hda_codec *codec = hdac_to_hda_codec(hdac);
  1513. struct hdmi_spec *spec = codec->spec;
  1514. struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
  1515. /* chmap is already set to 0 in caller */
  1516. if (!per_pin)
  1517. return;
  1518. memcpy(chmap, per_pin->chmap, ARRAY_SIZE(per_pin->chmap));
  1519. }
  1520. static void hdmi_set_chmap(struct hdac_device *hdac, int pcm_idx,
  1521. unsigned char *chmap, int prepared)
  1522. {
  1523. struct hda_codec *codec = hdac_to_hda_codec(hdac);
  1524. struct hdmi_spec *spec = codec->spec;
  1525. struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
  1526. if (!per_pin)
  1527. return;
  1528. guard(mutex)(&per_pin->lock);
  1529. per_pin->chmap_set = true;
  1530. memcpy(per_pin->chmap, chmap, ARRAY_SIZE(per_pin->chmap));
  1531. if (prepared)
  1532. snd_hda_hdmi_setup_audio_infoframe(codec, per_pin, per_pin->non_pcm);
  1533. }
  1534. static bool is_hdmi_pcm_attached(struct hdac_device *hdac, int pcm_idx)
  1535. {
  1536. struct hda_codec *codec = hdac_to_hda_codec(hdac);
  1537. struct hdmi_spec *spec = codec->spec;
  1538. struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
  1539. return per_pin ? true:false;
  1540. }
  1541. int snd_hda_hdmi_generic_build_pcms(struct hda_codec *codec)
  1542. {
  1543. struct hdmi_spec *spec = codec->spec;
  1544. int idx, pcm_num;
  1545. /* limit the PCM devices to the codec converters or available PINs */
  1546. pcm_num = min(spec->num_cvts, spec->num_pins);
  1547. codec_dbg(codec, "hdmi: pcm_num set to %d\n", pcm_num);
  1548. for (idx = 0; idx < pcm_num; idx++) {
  1549. struct hdmi_spec_per_cvt *per_cvt;
  1550. struct hda_pcm *info;
  1551. struct hda_pcm_stream *pstr;
  1552. info = snd_hda_codec_pcm_new(codec, "HDMI %d", idx);
  1553. if (!info)
  1554. return -ENOMEM;
  1555. spec->pcm_rec[idx].pcm = info;
  1556. spec->pcm_used++;
  1557. info->pcm_type = HDA_PCM_TYPE_HDMI;
  1558. info->own_chmap = true;
  1559. pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
  1560. pstr->substreams = 1;
  1561. pstr->ops = generic_ops;
  1562. per_cvt = get_cvt(spec, 0);
  1563. pstr->channels_min = per_cvt->channels_min;
  1564. pstr->channels_max = per_cvt->channels_max;
  1565. /* pcm number is less than pcm_rec array size */
  1566. if (spec->pcm_used >= ARRAY_SIZE(spec->pcm_rec))
  1567. break;
  1568. /* other pstr fields are set in open */
  1569. }
  1570. return 0;
  1571. }
  1572. EXPORT_SYMBOL_NS_GPL(snd_hda_hdmi_generic_build_pcms, "SND_HDA_CODEC_HDMI");
  1573. static void free_hdmi_jack_priv(struct snd_jack *jack)
  1574. {
  1575. struct hdmi_pcm *pcm = jack->private_data;
  1576. pcm->jack = NULL;
  1577. }
  1578. static int generic_hdmi_build_jack(struct hda_codec *codec, int pcm_idx)
  1579. {
  1580. char hdmi_str[32] = "HDMI/DP";
  1581. struct hdmi_spec *spec = codec->spec;
  1582. struct snd_jack *jack;
  1583. int pcmdev = get_pcm_rec(spec, pcm_idx)->device;
  1584. int err;
  1585. if (pcmdev > 0)
  1586. sprintf(hdmi_str + strlen(hdmi_str), ",pcm=%d", pcmdev);
  1587. err = snd_jack_new(codec->card, hdmi_str, SND_JACK_AVOUT, &jack,
  1588. true, false);
  1589. if (err < 0)
  1590. return err;
  1591. spec->pcm_rec[pcm_idx].jack = jack;
  1592. jack->private_data = &spec->pcm_rec[pcm_idx];
  1593. jack->private_free = free_hdmi_jack_priv;
  1594. return 0;
  1595. }
  1596. int snd_hda_hdmi_generic_build_controls(struct hda_codec *codec)
  1597. {
  1598. struct hdmi_spec *spec = codec->spec;
  1599. int dev, err;
  1600. int pin_idx, pcm_idx;
  1601. for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) {
  1602. if (!get_pcm_rec(spec, pcm_idx)->pcm) {
  1603. /* no PCM: mark this for skipping permanently */
  1604. set_bit(pcm_idx, &spec->pcm_bitmap);
  1605. continue;
  1606. }
  1607. err = generic_hdmi_build_jack(codec, pcm_idx);
  1608. if (err < 0)
  1609. return err;
  1610. /* create the spdif for each pcm
  1611. * pin will be bound when monitor is connected
  1612. */
  1613. err = snd_hda_create_dig_out_ctls(codec,
  1614. 0, spec->cvt_nids[0],
  1615. HDA_PCM_TYPE_HDMI);
  1616. if (err < 0)
  1617. return err;
  1618. snd_hda_spdif_ctls_unassign(codec, pcm_idx);
  1619. dev = get_pcm_rec(spec, pcm_idx)->device;
  1620. if (dev != SNDRV_PCM_INVALID_DEVICE) {
  1621. /* add control for ELD Bytes */
  1622. err = hdmi_create_eld_ctl(codec, pcm_idx, dev);
  1623. if (err < 0)
  1624. return err;
  1625. }
  1626. }
  1627. for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
  1628. struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
  1629. struct hdmi_eld *pin_eld = &per_pin->sink_eld;
  1630. if (spec->static_pcm_mapping) {
  1631. hdmi_attach_hda_pcm(spec, per_pin);
  1632. hdmi_pcm_setup_pin(spec, per_pin);
  1633. }
  1634. pin_eld->eld_valid = false;
  1635. hdmi_present_sense(per_pin, 0);
  1636. }
  1637. /* add channel maps */
  1638. for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) {
  1639. struct hda_pcm *pcm;
  1640. pcm = get_pcm_rec(spec, pcm_idx);
  1641. if (!pcm || !pcm->pcm)
  1642. break;
  1643. err = snd_hdac_add_chmap_ctls(pcm->pcm, pcm_idx, &spec->chmap);
  1644. if (err < 0)
  1645. return err;
  1646. }
  1647. return 0;
  1648. }
  1649. EXPORT_SYMBOL_NS_GPL(snd_hda_hdmi_generic_build_controls, "SND_HDA_CODEC_HDMI");
  1650. int snd_hda_hdmi_generic_init_per_pins(struct hda_codec *codec)
  1651. {
  1652. struct hdmi_spec *spec = codec->spec;
  1653. int pin_idx;
  1654. for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
  1655. struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
  1656. per_pin->codec = codec;
  1657. mutex_init(&per_pin->lock);
  1658. INIT_DELAYED_WORK(&per_pin->work, hdmi_repoll_eld);
  1659. eld_proc_new(per_pin, pin_idx);
  1660. }
  1661. return 0;
  1662. }
  1663. EXPORT_SYMBOL_NS_GPL(snd_hda_hdmi_generic_init_per_pins, "SND_HDA_CODEC_HDMI");
  1664. int snd_hda_hdmi_generic_init(struct hda_codec *codec)
  1665. {
  1666. struct hdmi_spec *spec = codec->spec;
  1667. int pin_idx;
  1668. guard(mutex)(&spec->bind_lock);
  1669. for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
  1670. struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
  1671. hda_nid_t pin_nid = per_pin->pin_nid;
  1672. int dev_id = per_pin->dev_id;
  1673. snd_hda_set_dev_select(codec, pin_nid, dev_id);
  1674. hdmi_init_pin(codec, pin_nid);
  1675. if (codec_has_acomp(codec))
  1676. continue;
  1677. snd_hda_jack_detect_enable_callback_mst(codec, pin_nid, dev_id,
  1678. jack_callback);
  1679. }
  1680. return 0;
  1681. }
  1682. EXPORT_SYMBOL_NS_GPL(snd_hda_hdmi_generic_init, "SND_HDA_CODEC_HDMI");
  1683. static void hdmi_array_init(struct hdmi_spec *spec, int nums)
  1684. {
  1685. snd_array_init(&spec->pins, sizeof(struct hdmi_spec_per_pin), nums);
  1686. snd_array_init(&spec->cvts, sizeof(struct hdmi_spec_per_cvt), nums);
  1687. }
  1688. static void hdmi_array_free(struct hdmi_spec *spec)
  1689. {
  1690. snd_array_free(&spec->pins);
  1691. snd_array_free(&spec->cvts);
  1692. }
  1693. void snd_hda_hdmi_generic_spec_free(struct hda_codec *codec)
  1694. {
  1695. struct hdmi_spec *spec = codec->spec;
  1696. if (spec) {
  1697. hdmi_array_free(spec);
  1698. kfree(spec);
  1699. codec->spec = NULL;
  1700. }
  1701. codec->dp_mst = false;
  1702. }
  1703. EXPORT_SYMBOL_NS_GPL(snd_hda_hdmi_generic_spec_free, "SND_HDA_CODEC_HDMI");
  1704. void snd_hda_hdmi_generic_remove(struct hda_codec *codec)
  1705. {
  1706. struct hdmi_spec *spec = codec->spec;
  1707. int pin_idx, pcm_idx;
  1708. if (spec->acomp_registered) {
  1709. snd_hdac_acomp_exit(&codec->bus->core);
  1710. } else if (codec_has_acomp(codec)) {
  1711. snd_hdac_acomp_register_notifier(&codec->bus->core, NULL);
  1712. }
  1713. codec->relaxed_resume = 0;
  1714. for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
  1715. struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
  1716. cancel_delayed_work_sync(&per_pin->work);
  1717. eld_proc_free(per_pin);
  1718. }
  1719. for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) {
  1720. if (spec->pcm_rec[pcm_idx].jack == NULL)
  1721. continue;
  1722. snd_device_free(codec->card, spec->pcm_rec[pcm_idx].jack);
  1723. }
  1724. snd_hda_hdmi_generic_spec_free(codec);
  1725. }
  1726. EXPORT_SYMBOL_NS_GPL(snd_hda_hdmi_generic_remove, "SND_HDA_CODEC_HDMI");
  1727. int snd_hda_hdmi_generic_suspend(struct hda_codec *codec)
  1728. {
  1729. struct hdmi_spec *spec = codec->spec;
  1730. int pin_idx;
  1731. for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
  1732. struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
  1733. cancel_delayed_work_sync(&per_pin->work);
  1734. }
  1735. return 0;
  1736. }
  1737. EXPORT_SYMBOL_NS_GPL(snd_hda_hdmi_generic_suspend, "SND_HDA_CODEC_HDMI");
  1738. int snd_hda_hdmi_generic_resume(struct hda_codec *codec)
  1739. {
  1740. struct hdmi_spec *spec = codec->spec;
  1741. int pin_idx;
  1742. snd_hda_codec_init(codec);
  1743. snd_hda_regmap_sync(codec);
  1744. for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
  1745. struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
  1746. hdmi_present_sense(per_pin, 1);
  1747. }
  1748. return 0;
  1749. }
  1750. EXPORT_SYMBOL_NS_GPL(snd_hda_hdmi_generic_resume, "SND_HDA_CODEC_HDMI");
  1751. static const struct hdmi_ops generic_standard_hdmi_ops = {
  1752. .pin_get_eld = hdmi_pin_get_eld,
  1753. .pin_setup_infoframe = hdmi_pin_setup_infoframe,
  1754. .pin_hbr_setup = hdmi_pin_hbr_setup,
  1755. .setup_stream = snd_hda_hdmi_setup_stream,
  1756. };
  1757. /* allocate codec->spec and assign/initialize generic parser ops */
  1758. int snd_hda_hdmi_generic_alloc(struct hda_codec *codec)
  1759. {
  1760. struct hdmi_spec *spec;
  1761. spec = kzalloc_obj(*spec);
  1762. if (!spec)
  1763. return -ENOMEM;
  1764. spec->codec = codec;
  1765. spec->ops = generic_standard_hdmi_ops;
  1766. spec->dev_num = 1; /* initialize to 1 */
  1767. mutex_init(&spec->pcm_lock);
  1768. mutex_init(&spec->bind_lock);
  1769. snd_hdac_register_chmap_ops(&codec->core, &spec->chmap);
  1770. spec->chmap.ops.get_chmap = hdmi_get_chmap;
  1771. spec->chmap.ops.set_chmap = hdmi_set_chmap;
  1772. spec->chmap.ops.is_pcm_attached = is_hdmi_pcm_attached;
  1773. spec->chmap.ops.get_spk_alloc = hdmi_get_spk_alloc;
  1774. codec->spec = spec;
  1775. hdmi_array_init(spec, 4);
  1776. return 0;
  1777. }
  1778. EXPORT_SYMBOL_NS_GPL(snd_hda_hdmi_generic_alloc, "SND_HDA_CODEC_HDMI");
  1779. /* generic HDMI parser */
  1780. int snd_hda_hdmi_generic_probe(struct hda_codec *codec)
  1781. {
  1782. int err;
  1783. err = snd_hda_hdmi_generic_alloc(codec);
  1784. if (err < 0)
  1785. return err;
  1786. err = snd_hda_hdmi_parse_codec(codec);
  1787. if (err < 0) {
  1788. snd_hda_hdmi_generic_spec_free(codec);
  1789. return err;
  1790. }
  1791. snd_hda_hdmi_generic_init_per_pins(codec);
  1792. return 0;
  1793. }
  1794. EXPORT_SYMBOL_NS_GPL(snd_hda_hdmi_generic_probe, "SND_HDA_CODEC_HDMI");
  1795. /*
  1796. * generic audio component binding
  1797. */
  1798. /* turn on / off the unsol event jack detection dynamically */
  1799. static void reprogram_jack_detect(struct hda_codec *codec, hda_nid_t nid,
  1800. int dev_id, bool use_acomp)
  1801. {
  1802. struct hda_jack_tbl *tbl;
  1803. tbl = snd_hda_jack_tbl_get_mst(codec, nid, dev_id);
  1804. if (tbl) {
  1805. /* clear unsol even if component notifier is used, or re-enable
  1806. * if notifier is cleared
  1807. */
  1808. unsigned int val = use_acomp ? 0 : (AC_USRSP_EN | tbl->tag);
  1809. snd_hda_codec_write_cache(codec, nid, 0,
  1810. AC_VERB_SET_UNSOLICITED_ENABLE, val);
  1811. }
  1812. }
  1813. /* set up / clear component notifier dynamically */
  1814. static void generic_acomp_notifier_set(struct drm_audio_component *acomp,
  1815. bool use_acomp)
  1816. {
  1817. struct hdmi_spec *spec;
  1818. int i;
  1819. spec = container_of(acomp->audio_ops, struct hdmi_spec, drm_audio_ops);
  1820. guard(mutex)(&spec->bind_lock);
  1821. spec->use_acomp_notifier = use_acomp;
  1822. spec->codec->relaxed_resume = use_acomp;
  1823. spec->codec->bus->keep_power = 0;
  1824. /* reprogram each jack detection logic depending on the notifier */
  1825. for (i = 0; i < spec->num_pins; i++)
  1826. reprogram_jack_detect(spec->codec,
  1827. get_pin(spec, i)->pin_nid,
  1828. get_pin(spec, i)->dev_id,
  1829. use_acomp);
  1830. }
  1831. /* enable / disable the notifier via master bind / unbind */
  1832. int snd_hda_hdmi_acomp_master_bind(struct device *dev,
  1833. struct drm_audio_component *acomp)
  1834. {
  1835. generic_acomp_notifier_set(acomp, true);
  1836. return 0;
  1837. }
  1838. EXPORT_SYMBOL_NS_GPL(snd_hda_hdmi_acomp_master_bind, "SND_HDA_CODEC_HDMI");
  1839. void snd_hda_hdmi_acomp_master_unbind(struct device *dev,
  1840. struct drm_audio_component *acomp)
  1841. {
  1842. generic_acomp_notifier_set(acomp, false);
  1843. }
  1844. EXPORT_SYMBOL_NS_GPL(snd_hda_hdmi_acomp_master_unbind, "SND_HDA_CODEC_HDMI");
  1845. /* check whether both HD-audio and DRM PCI devices belong to the same bus */
  1846. static int match_bound_vga(struct device *dev, int subtype, void *data)
  1847. {
  1848. struct hdac_bus *bus = data;
  1849. struct pci_dev *pci, *master;
  1850. if (!dev_is_pci(dev) || !dev_is_pci(bus->dev))
  1851. return 0;
  1852. master = to_pci_dev(bus->dev);
  1853. pci = to_pci_dev(dev);
  1854. return master->bus == pci->bus;
  1855. }
  1856. /* audio component notifier for AMD/Nvidia HDMI codecs */
  1857. void snd_hda_hdmi_acomp_pin_eld_notify(void *audio_ptr, int port, int dev_id)
  1858. {
  1859. struct hda_codec *codec = audio_ptr;
  1860. struct hdmi_spec *spec = codec->spec;
  1861. hda_nid_t pin_nid = spec->port2pin(codec, port);
  1862. if (!pin_nid)
  1863. return;
  1864. if (get_wcaps_type(get_wcaps(codec, pin_nid)) != AC_WID_PIN)
  1865. return;
  1866. /* skip notification during system suspend (but not in runtime PM);
  1867. * the state will be updated at resume
  1868. */
  1869. if (codec->core.dev.power.power_state.event == PM_EVENT_SUSPEND)
  1870. return;
  1871. snd_hda_hdmi_check_presence_and_report(codec, pin_nid, dev_id);
  1872. }
  1873. EXPORT_SYMBOL_NS_GPL(snd_hda_hdmi_acomp_pin_eld_notify, "SND_HDA_CODEC_HDMI");
  1874. /* set up the private drm_audio_ops from the template */
  1875. void snd_hda_hdmi_setup_drm_audio_ops(struct hda_codec *codec,
  1876. const struct drm_audio_component_audio_ops *ops)
  1877. {
  1878. struct hdmi_spec *spec = codec->spec;
  1879. spec->drm_audio_ops.audio_ptr = codec;
  1880. /* intel_audio_codec_enable() or intel_audio_codec_disable()
  1881. * will call pin_eld_notify with using audio_ptr pointer
  1882. * We need make sure audio_ptr is really setup
  1883. */
  1884. wmb();
  1885. spec->drm_audio_ops.pin2port = ops->pin2port;
  1886. spec->drm_audio_ops.pin_eld_notify = ops->pin_eld_notify;
  1887. spec->drm_audio_ops.master_bind = ops->master_bind;
  1888. spec->drm_audio_ops.master_unbind = ops->master_unbind;
  1889. }
  1890. EXPORT_SYMBOL_NS_GPL(snd_hda_hdmi_setup_drm_audio_ops, "SND_HDA_CODEC_HDMI");
  1891. /* initialize the generic HDMI audio component */
  1892. void snd_hda_hdmi_acomp_init(struct hda_codec *codec,
  1893. const struct drm_audio_component_audio_ops *ops,
  1894. int (*port2pin)(struct hda_codec *, int))
  1895. {
  1896. struct hdmi_spec *spec = codec->spec;
  1897. if (!enable_acomp) {
  1898. codec_info(codec, "audio component disabled by module option\n");
  1899. return;
  1900. }
  1901. spec->port2pin = port2pin;
  1902. snd_hda_hdmi_setup_drm_audio_ops(codec, ops);
  1903. if (!snd_hdac_acomp_init(&codec->bus->core, &spec->drm_audio_ops,
  1904. match_bound_vga, 0)) {
  1905. spec->acomp_registered = true;
  1906. }
  1907. }
  1908. EXPORT_SYMBOL_NS_GPL(snd_hda_hdmi_acomp_init, "SND_HDA_CODEC_HDMI");
  1909. /*
  1910. */
  1911. enum {
  1912. MODEL_GENERIC,
  1913. MODEL_GF,
  1914. };
  1915. static int generichdmi_probe(struct hda_codec *codec,
  1916. const struct hda_device_id *id)
  1917. {
  1918. int err;
  1919. err = snd_hda_hdmi_generic_probe(codec);
  1920. if (err < 0)
  1921. return err;
  1922. /*
  1923. * Glenfly GPUs have two codecs, stream switches from one codec to
  1924. * another, need to do actual clean-ups in codec_cleanup_stream
  1925. */
  1926. if (id->driver_data == MODEL_GF)
  1927. codec->no_sticky_stream = 1;
  1928. return 0;
  1929. }
  1930. static const struct hda_codec_ops generichdmi_codec_ops = {
  1931. .probe = generichdmi_probe,
  1932. .remove = snd_hda_hdmi_generic_remove,
  1933. .init = snd_hda_hdmi_generic_init,
  1934. .build_pcms = snd_hda_hdmi_generic_build_pcms,
  1935. .build_controls = snd_hda_hdmi_generic_build_controls,
  1936. .unsol_event = snd_hda_hdmi_generic_unsol_event,
  1937. .suspend = snd_hda_hdmi_generic_suspend,
  1938. .resume = snd_hda_hdmi_generic_resume,
  1939. };
  1940. /*
  1941. */
  1942. static const struct hda_device_id snd_hda_id_generichdmi[] = {
  1943. HDA_CODEC_ID_MODEL(0x00147a47, "Loongson HDMI", MODEL_GENERIC),
  1944. HDA_CODEC_ID_MODEL(0x10951390, "SiI1390 HDMI", MODEL_GENERIC),
  1945. HDA_CODEC_ID_MODEL(0x10951392, "SiI1392 HDMI", MODEL_GENERIC),
  1946. HDA_CODEC_ID_MODEL(0x11069f84, "VX11 HDMI/DP", MODEL_GENERIC),
  1947. HDA_CODEC_ID_MODEL(0x11069f85, "VX11 HDMI/DP", MODEL_GENERIC),
  1948. HDA_CODEC_ID_MODEL(0x17e80047, "Chrontel HDMI", MODEL_GENERIC),
  1949. HDA_CODEC_ID_MODEL(0x1d179f86, "ZX-100S HDMI/DP", MODEL_GF),
  1950. HDA_CODEC_ID_MODEL(0x1d179f87, "ZX-100S HDMI/DP", MODEL_GF),
  1951. HDA_CODEC_ID_MODEL(0x1d179f88, "KX-5000 HDMI/DP", MODEL_GF),
  1952. HDA_CODEC_ID_MODEL(0x1d179f89, "KX-5000 HDMI/DP", MODEL_GF),
  1953. HDA_CODEC_ID_MODEL(0x1d179f8a, "KX-6000 HDMI/DP", MODEL_GF),
  1954. HDA_CODEC_ID_MODEL(0x1d179f8b, "KX-6000 HDMI/DP", MODEL_GF),
  1955. HDA_CODEC_ID_MODEL(0x1d179f8c, "KX-6000G HDMI/DP", MODEL_GF),
  1956. HDA_CODEC_ID_MODEL(0x1d179f8d, "KX-6000G HDMI/DP", MODEL_GF),
  1957. HDA_CODEC_ID_MODEL(0x1d179f8e, "KX-7000 HDMI/DP", MODEL_GF),
  1958. HDA_CODEC_ID_MODEL(0x1d179f8f, "KX-7000 HDMI/DP", MODEL_GF),
  1959. HDA_CODEC_ID_MODEL(0x1d179f90, "KX-7000 HDMI/DP", MODEL_GF),
  1960. HDA_CODEC_ID_MODEL(0x67663d82, "Arise 82 HDMI/DP", MODEL_GF),
  1961. HDA_CODEC_ID_MODEL(0x67663d83, "Arise 83 HDMI/DP", MODEL_GF),
  1962. HDA_CODEC_ID_MODEL(0x67663d84, "Arise 84 HDMI/DP", MODEL_GF),
  1963. HDA_CODEC_ID_MODEL(0x67663d85, "Arise 85 HDMI/DP", MODEL_GF),
  1964. HDA_CODEC_ID_MODEL(0x67663d86, "Arise 86 HDMI/DP", MODEL_GF),
  1965. HDA_CODEC_ID_MODEL(0x67663d87, "Arise 87 HDMI/DP", MODEL_GF),
  1966. HDA_CODEC_ID_MODEL(0x80862801, "Bearlake HDMI", MODEL_GENERIC),
  1967. HDA_CODEC_ID_MODEL(0x80862802, "Cantiga HDMI", MODEL_GENERIC),
  1968. HDA_CODEC_ID_MODEL(0x80862803, "Eaglelake HDMI", MODEL_GENERIC),
  1969. HDA_CODEC_ID_MODEL(0x80862880, "CedarTrail HDMI", MODEL_GENERIC),
  1970. HDA_CODEC_ID_MODEL(0x808629fb, "Crestline HDMI", MODEL_GENERIC),
  1971. /* special ID for generic HDMI */
  1972. HDA_CODEC_ID_MODEL(HDA_CODEC_ID_GENERIC_HDMI, "Generic HDMI", MODEL_GENERIC),
  1973. {} /* terminator */
  1974. };
  1975. MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_generichdmi);
  1976. MODULE_LICENSE("GPL");
  1977. MODULE_DESCRIPTION("Generic HDMI HD-audio codec");
  1978. static struct hda_codec_driver generichdmi_driver = {
  1979. .id = snd_hda_id_generichdmi,
  1980. .ops = &generichdmi_codec_ops,
  1981. };
  1982. module_hda_codec_driver(generichdmi_driver);