patch_hdmi.c 94 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494
  1. /*
  2. *
  3. * patch_hdmi.c - routines for HDMI/DisplayPort codecs
  4. *
  5. * Copyright(c) 2008-2010 Intel Corporation. All rights reserved.
  6. * Copyright (c) 2006 ATI Technologies Inc.
  7. * Copyright (c) 2008 NVIDIA Corp. All rights reserved.
  8. * Copyright (c) 2008 Wei Ni <wni@nvidia.com>
  9. * Copyright (c) 2013 Anssi Hannula <anssi.hannula@iki.fi>
  10. *
  11. * Authors:
  12. * Wu Fengguang <wfg@linux.intel.com>
  13. *
  14. * Maintained by:
  15. * Wu Fengguang <wfg@linux.intel.com>
  16. *
  17. * This program is free software; you can redistribute it and/or modify it
  18. * under the terms of the GNU General Public License as published by the Free
  19. * Software Foundation; either version 2 of the License, or (at your option)
  20. * any later version.
  21. *
  22. * This program is distributed in the hope that it will be useful, but
  23. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  24. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  25. * for more details.
  26. *
  27. * You should have received a copy of the GNU General Public License
  28. * along with this program; if not, write to the Free Software Foundation,
  29. * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  30. */
  31. #include <linux/init.h>
  32. #include <linux/delay.h>
  33. #include <linux/slab.h>
  34. #include <linux/module.h>
  35. #include <sound/core.h>
  36. #include <sound/jack.h>
  37. #include <sound/asoundef.h>
  38. #include <sound/tlv.h>
  39. #include <sound/hdaudio.h>
  40. #include <sound/hda_i915.h>
  41. #include <sound/hda_chmap.h>
  42. #include "hda_codec.h"
  43. #include "hda_local.h"
  44. #include "hda_jack.h"
  45. static bool static_hdmi_pcm;
  46. module_param(static_hdmi_pcm, bool, 0644);
  47. MODULE_PARM_DESC(static_hdmi_pcm, "Don't restrict PCM parameters per ELD info");
  48. #define is_haswell(codec) ((codec)->core.vendor_id == 0x80862807)
  49. #define is_broadwell(codec) ((codec)->core.vendor_id == 0x80862808)
  50. #define is_skylake(codec) ((codec)->core.vendor_id == 0x80862809)
  51. #define is_broxton(codec) ((codec)->core.vendor_id == 0x8086280a)
  52. #define is_kabylake(codec) ((codec)->core.vendor_id == 0x8086280b)
  53. #define is_haswell_plus(codec) (is_haswell(codec) || is_broadwell(codec) \
  54. || is_skylake(codec) || is_broxton(codec) \
  55. || is_kabylake(codec))
  56. #define is_valleyview(codec) ((codec)->core.vendor_id == 0x80862882)
  57. #define is_cherryview(codec) ((codec)->core.vendor_id == 0x80862883)
  58. #define is_valleyview_plus(codec) (is_valleyview(codec) || is_cherryview(codec))
  59. struct hdmi_spec_per_cvt {
  60. hda_nid_t cvt_nid;
  61. int assigned;
  62. unsigned int channels_min;
  63. unsigned int channels_max;
  64. u32 rates;
  65. u64 formats;
  66. unsigned int maxbps;
  67. };
  68. /* max. connections to a widget */
  69. #define HDA_MAX_CONNECTIONS 32
  70. struct hdmi_spec_per_pin {
  71. hda_nid_t pin_nid;
  72. /* pin idx, different device entries on the same pin use the same idx */
  73. int pin_nid_idx;
  74. int num_mux_nids;
  75. hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
  76. int mux_idx;
  77. hda_nid_t cvt_nid;
  78. struct hda_codec *codec;
  79. struct hdmi_eld sink_eld;
  80. struct mutex lock;
  81. struct delayed_work work;
  82. struct hdmi_pcm *pcm; /* pointer to spec->pcm_rec[n] dynamically*/
  83. int pcm_idx; /* which pcm is attached. -1 means no pcm is attached */
  84. int repoll_count;
  85. bool setup; /* the stream has been set up by prepare callback */
  86. int channels; /* current number of channels */
  87. bool non_pcm;
  88. bool chmap_set; /* channel-map override by ALSA API? */
  89. unsigned char chmap[8]; /* ALSA API channel-map */
  90. #ifdef CONFIG_SND_PROC_FS
  91. struct snd_info_entry *proc_entry;
  92. #endif
  93. };
  94. /* operations used by generic code that can be overridden by patches */
  95. struct hdmi_ops {
  96. int (*pin_get_eld)(struct hda_codec *codec, hda_nid_t pin_nid,
  97. unsigned char *buf, int *eld_size);
  98. void (*pin_setup_infoframe)(struct hda_codec *codec, hda_nid_t pin_nid,
  99. int ca, int active_channels, int conn_type);
  100. /* enable/disable HBR (HD passthrough) */
  101. int (*pin_hbr_setup)(struct hda_codec *codec, hda_nid_t pin_nid, bool hbr);
  102. int (*setup_stream)(struct hda_codec *codec, hda_nid_t cvt_nid,
  103. hda_nid_t pin_nid, u32 stream_tag, int format);
  104. };
  105. struct hdmi_pcm {
  106. struct hda_pcm *pcm;
  107. struct snd_jack *jack;
  108. struct snd_kcontrol *eld_ctl;
  109. };
  110. struct hdmi_spec {
  111. int num_cvts;
  112. struct snd_array cvts; /* struct hdmi_spec_per_cvt */
  113. hda_nid_t cvt_nids[4]; /* only for haswell fix */
  114. int num_pins;
  115. struct snd_array pins; /* struct hdmi_spec_per_pin */
  116. struct hdmi_pcm pcm_rec[16];
  117. struct mutex pcm_lock;
  118. /* pcm_bitmap means which pcms have been assigned to pins*/
  119. unsigned long pcm_bitmap;
  120. int pcm_used; /* counter of pcm_rec[] */
  121. /* bitmap shows whether the pcm is opened in user space
  122. * bit 0 means the first playback PCM (PCM3);
  123. * bit 1 means the second playback PCM, and so on.
  124. */
  125. unsigned long pcm_in_use;
  126. struct hdmi_eld temp_eld;
  127. struct hdmi_ops ops;
  128. bool dyn_pin_out;
  129. bool dyn_pcm_assign;
  130. /*
  131. * Non-generic VIA/NVIDIA specific
  132. */
  133. struct hda_multi_out multiout;
  134. struct hda_pcm_stream pcm_playback;
  135. /* i915/powerwell (Haswell+/Valleyview+) specific */
  136. struct i915_audio_component_audio_ops i915_audio_ops;
  137. bool i915_bound; /* was i915 bound in this driver? */
  138. struct hdac_chmap chmap;
  139. };
  140. #ifdef CONFIG_SND_HDA_I915
  141. #define codec_has_acomp(codec) \
  142. ((codec)->bus->core.audio_component != NULL)
  143. #else
  144. #define codec_has_acomp(codec) false
  145. #endif
  146. struct hdmi_audio_infoframe {
  147. u8 type; /* 0x84 */
  148. u8 ver; /* 0x01 */
  149. u8 len; /* 0x0a */
  150. u8 checksum;
  151. u8 CC02_CT47; /* CC in bits 0:2, CT in 4:7 */
  152. u8 SS01_SF24;
  153. u8 CXT04;
  154. u8 CA;
  155. u8 LFEPBL01_LSV36_DM_INH7;
  156. };
  157. struct dp_audio_infoframe {
  158. u8 type; /* 0x84 */
  159. u8 len; /* 0x1b */
  160. u8 ver; /* 0x11 << 2 */
  161. u8 CC02_CT47; /* match with HDMI infoframe from this on */
  162. u8 SS01_SF24;
  163. u8 CXT04;
  164. u8 CA;
  165. u8 LFEPBL01_LSV36_DM_INH7;
  166. };
  167. union audio_infoframe {
  168. struct hdmi_audio_infoframe hdmi;
  169. struct dp_audio_infoframe dp;
  170. u8 bytes[0];
  171. };
  172. /*
  173. * HDMI routines
  174. */
  175. #define get_pin(spec, idx) \
  176. ((struct hdmi_spec_per_pin *)snd_array_elem(&spec->pins, idx))
  177. #define get_cvt(spec, idx) \
  178. ((struct hdmi_spec_per_cvt *)snd_array_elem(&spec->cvts, idx))
  179. /* obtain hdmi_pcm object assigned to idx */
  180. #define get_hdmi_pcm(spec, idx) (&(spec)->pcm_rec[idx])
  181. /* obtain hda_pcm object assigned to idx */
  182. #define get_pcm_rec(spec, idx) (get_hdmi_pcm(spec, idx)->pcm)
  183. static int pin_nid_to_pin_index(struct hda_codec *codec, hda_nid_t pin_nid)
  184. {
  185. struct hdmi_spec *spec = codec->spec;
  186. int pin_idx;
  187. for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++)
  188. if (get_pin(spec, pin_idx)->pin_nid == pin_nid)
  189. return pin_idx;
  190. codec_warn(codec, "HDMI: pin nid %d not registered\n", pin_nid);
  191. return -EINVAL;
  192. }
  193. static int hinfo_to_pcm_index(struct hda_codec *codec,
  194. struct hda_pcm_stream *hinfo)
  195. {
  196. struct hdmi_spec *spec = codec->spec;
  197. int pcm_idx;
  198. for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++)
  199. if (get_pcm_rec(spec, pcm_idx)->stream == hinfo)
  200. return pcm_idx;
  201. codec_warn(codec, "HDMI: hinfo %p not registered\n", hinfo);
  202. return -EINVAL;
  203. }
  204. static int hinfo_to_pin_index(struct hda_codec *codec,
  205. struct hda_pcm_stream *hinfo)
  206. {
  207. struct hdmi_spec *spec = codec->spec;
  208. struct hdmi_spec_per_pin *per_pin;
  209. int pin_idx;
  210. for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
  211. per_pin = get_pin(spec, pin_idx);
  212. if (per_pin->pcm &&
  213. per_pin->pcm->pcm->stream == hinfo)
  214. return pin_idx;
  215. }
  216. codec_dbg(codec, "HDMI: hinfo %p not registered\n", hinfo);
  217. return -EINVAL;
  218. }
  219. static struct hdmi_spec_per_pin *pcm_idx_to_pin(struct hdmi_spec *spec,
  220. int pcm_idx)
  221. {
  222. int i;
  223. struct hdmi_spec_per_pin *per_pin;
  224. for (i = 0; i < spec->num_pins; i++) {
  225. per_pin = get_pin(spec, i);
  226. if (per_pin->pcm_idx == pcm_idx)
  227. return per_pin;
  228. }
  229. return NULL;
  230. }
  231. static int cvt_nid_to_cvt_index(struct hda_codec *codec, hda_nid_t cvt_nid)
  232. {
  233. struct hdmi_spec *spec = codec->spec;
  234. int cvt_idx;
  235. for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++)
  236. if (get_cvt(spec, cvt_idx)->cvt_nid == cvt_nid)
  237. return cvt_idx;
  238. codec_warn(codec, "HDMI: cvt nid %d not registered\n", cvt_nid);
  239. return -EINVAL;
  240. }
  241. static int hdmi_eld_ctl_info(struct snd_kcontrol *kcontrol,
  242. struct snd_ctl_elem_info *uinfo)
  243. {
  244. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  245. struct hdmi_spec *spec = codec->spec;
  246. struct hdmi_spec_per_pin *per_pin;
  247. struct hdmi_eld *eld;
  248. int pcm_idx;
  249. uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
  250. pcm_idx = kcontrol->private_value;
  251. mutex_lock(&spec->pcm_lock);
  252. per_pin = pcm_idx_to_pin(spec, pcm_idx);
  253. if (!per_pin) {
  254. /* no pin is bound to the pcm */
  255. uinfo->count = 0;
  256. mutex_unlock(&spec->pcm_lock);
  257. return 0;
  258. }
  259. eld = &per_pin->sink_eld;
  260. uinfo->count = eld->eld_valid ? eld->eld_size : 0;
  261. mutex_unlock(&spec->pcm_lock);
  262. return 0;
  263. }
  264. static int hdmi_eld_ctl_get(struct snd_kcontrol *kcontrol,
  265. struct snd_ctl_elem_value *ucontrol)
  266. {
  267. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  268. struct hdmi_spec *spec = codec->spec;
  269. struct hdmi_spec_per_pin *per_pin;
  270. struct hdmi_eld *eld;
  271. int pcm_idx;
  272. pcm_idx = kcontrol->private_value;
  273. mutex_lock(&spec->pcm_lock);
  274. per_pin = pcm_idx_to_pin(spec, pcm_idx);
  275. if (!per_pin) {
  276. /* no pin is bound to the pcm */
  277. memset(ucontrol->value.bytes.data, 0,
  278. ARRAY_SIZE(ucontrol->value.bytes.data));
  279. mutex_unlock(&spec->pcm_lock);
  280. return 0;
  281. }
  282. eld = &per_pin->sink_eld;
  283. if (eld->eld_size > ARRAY_SIZE(ucontrol->value.bytes.data) ||
  284. eld->eld_size > ELD_MAX_SIZE) {
  285. mutex_unlock(&spec->pcm_lock);
  286. snd_BUG();
  287. return -EINVAL;
  288. }
  289. memset(ucontrol->value.bytes.data, 0,
  290. ARRAY_SIZE(ucontrol->value.bytes.data));
  291. if (eld->eld_valid)
  292. memcpy(ucontrol->value.bytes.data, eld->eld_buffer,
  293. eld->eld_size);
  294. mutex_unlock(&spec->pcm_lock);
  295. return 0;
  296. }
  297. static struct snd_kcontrol_new eld_bytes_ctl = {
  298. .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
  299. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  300. .name = "ELD",
  301. .info = hdmi_eld_ctl_info,
  302. .get = hdmi_eld_ctl_get,
  303. };
  304. static int hdmi_create_eld_ctl(struct hda_codec *codec, int pcm_idx,
  305. int device)
  306. {
  307. struct snd_kcontrol *kctl;
  308. struct hdmi_spec *spec = codec->spec;
  309. int err;
  310. kctl = snd_ctl_new1(&eld_bytes_ctl, codec);
  311. if (!kctl)
  312. return -ENOMEM;
  313. kctl->private_value = pcm_idx;
  314. kctl->id.device = device;
  315. /* no pin nid is associated with the kctl now
  316. * tbd: associate pin nid to eld ctl later
  317. */
  318. err = snd_hda_ctl_add(codec, 0, kctl);
  319. if (err < 0)
  320. return err;
  321. get_hdmi_pcm(spec, pcm_idx)->eld_ctl = kctl;
  322. return 0;
  323. }
  324. #ifdef BE_PARANOID
  325. static void hdmi_get_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
  326. int *packet_index, int *byte_index)
  327. {
  328. int val;
  329. val = snd_hda_codec_read(codec, pin_nid, 0,
  330. AC_VERB_GET_HDMI_DIP_INDEX, 0);
  331. *packet_index = val >> 5;
  332. *byte_index = val & 0x1f;
  333. }
  334. #endif
  335. static void hdmi_set_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
  336. int packet_index, int byte_index)
  337. {
  338. int val;
  339. val = (packet_index << 5) | (byte_index & 0x1f);
  340. snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val);
  341. }
  342. static void hdmi_write_dip_byte(struct hda_codec *codec, hda_nid_t pin_nid,
  343. unsigned char val)
  344. {
  345. snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_DATA, val);
  346. }
  347. static void hdmi_init_pin(struct hda_codec *codec, hda_nid_t pin_nid)
  348. {
  349. struct hdmi_spec *spec = codec->spec;
  350. int pin_out;
  351. /* Unmute */
  352. if (get_wcaps(codec, pin_nid) & AC_WCAP_OUT_AMP)
  353. snd_hda_codec_write(codec, pin_nid, 0,
  354. AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
  355. if (spec->dyn_pin_out)
  356. /* Disable pin out until stream is active */
  357. pin_out = 0;
  358. else
  359. /* Enable pin out: some machines with GM965 gets broken output
  360. * when the pin is disabled or changed while using with HDMI
  361. */
  362. pin_out = PIN_OUT;
  363. snd_hda_codec_write(codec, pin_nid, 0,
  364. AC_VERB_SET_PIN_WIDGET_CONTROL, pin_out);
  365. }
  366. /*
  367. * ELD proc files
  368. */
  369. #ifdef CONFIG_SND_PROC_FS
  370. static void print_eld_info(struct snd_info_entry *entry,
  371. struct snd_info_buffer *buffer)
  372. {
  373. struct hdmi_spec_per_pin *per_pin = entry->private_data;
  374. mutex_lock(&per_pin->lock);
  375. snd_hdmi_print_eld_info(&per_pin->sink_eld, buffer);
  376. mutex_unlock(&per_pin->lock);
  377. }
  378. static void write_eld_info(struct snd_info_entry *entry,
  379. struct snd_info_buffer *buffer)
  380. {
  381. struct hdmi_spec_per_pin *per_pin = entry->private_data;
  382. mutex_lock(&per_pin->lock);
  383. snd_hdmi_write_eld_info(&per_pin->sink_eld, buffer);
  384. mutex_unlock(&per_pin->lock);
  385. }
  386. static int eld_proc_new(struct hdmi_spec_per_pin *per_pin, int index)
  387. {
  388. char name[32];
  389. struct hda_codec *codec = per_pin->codec;
  390. struct snd_info_entry *entry;
  391. int err;
  392. snprintf(name, sizeof(name), "eld#%d.%d", codec->addr, index);
  393. err = snd_card_proc_new(codec->card, name, &entry);
  394. if (err < 0)
  395. return err;
  396. snd_info_set_text_ops(entry, per_pin, print_eld_info);
  397. entry->c.text.write = write_eld_info;
  398. entry->mode |= S_IWUSR;
  399. per_pin->proc_entry = entry;
  400. return 0;
  401. }
  402. static void eld_proc_free(struct hdmi_spec_per_pin *per_pin)
  403. {
  404. if (!per_pin->codec->bus->shutdown) {
  405. snd_info_free_entry(per_pin->proc_entry);
  406. per_pin->proc_entry = NULL;
  407. }
  408. }
  409. #else
  410. static inline int eld_proc_new(struct hdmi_spec_per_pin *per_pin,
  411. int index)
  412. {
  413. return 0;
  414. }
  415. static inline void eld_proc_free(struct hdmi_spec_per_pin *per_pin)
  416. {
  417. }
  418. #endif
  419. /*
  420. * Audio InfoFrame routines
  421. */
  422. /*
  423. * Enable Audio InfoFrame Transmission
  424. */
  425. static void hdmi_start_infoframe_trans(struct hda_codec *codec,
  426. hda_nid_t pin_nid)
  427. {
  428. hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
  429. snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
  430. AC_DIPXMIT_BEST);
  431. }
  432. /*
  433. * Disable Audio InfoFrame Transmission
  434. */
  435. static void hdmi_stop_infoframe_trans(struct hda_codec *codec,
  436. hda_nid_t pin_nid)
  437. {
  438. hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
  439. snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
  440. AC_DIPXMIT_DISABLE);
  441. }
  442. static void hdmi_debug_dip_size(struct hda_codec *codec, hda_nid_t pin_nid)
  443. {
  444. #ifdef CONFIG_SND_DEBUG_VERBOSE
  445. int i;
  446. int size;
  447. size = snd_hdmi_get_eld_size(codec, pin_nid);
  448. codec_dbg(codec, "HDMI: ELD buf size is %d\n", size);
  449. for (i = 0; i < 8; i++) {
  450. size = snd_hda_codec_read(codec, pin_nid, 0,
  451. AC_VERB_GET_HDMI_DIP_SIZE, i);
  452. codec_dbg(codec, "HDMI: DIP GP[%d] buf size is %d\n", i, size);
  453. }
  454. #endif
  455. }
  456. static void hdmi_clear_dip_buffers(struct hda_codec *codec, hda_nid_t pin_nid)
  457. {
  458. #ifdef BE_PARANOID
  459. int i, j;
  460. int size;
  461. int pi, bi;
  462. for (i = 0; i < 8; i++) {
  463. size = snd_hda_codec_read(codec, pin_nid, 0,
  464. AC_VERB_GET_HDMI_DIP_SIZE, i);
  465. if (size == 0)
  466. continue;
  467. hdmi_set_dip_index(codec, pin_nid, i, 0x0);
  468. for (j = 1; j < 1000; j++) {
  469. hdmi_write_dip_byte(codec, pin_nid, 0x0);
  470. hdmi_get_dip_index(codec, pin_nid, &pi, &bi);
  471. if (pi != i)
  472. codec_dbg(codec, "dip index %d: %d != %d\n",
  473. bi, pi, i);
  474. if (bi == 0) /* byte index wrapped around */
  475. break;
  476. }
  477. codec_dbg(codec,
  478. "HDMI: DIP GP[%d] buf reported size=%d, written=%d\n",
  479. i, size, j);
  480. }
  481. #endif
  482. }
  483. static void hdmi_checksum_audio_infoframe(struct hdmi_audio_infoframe *hdmi_ai)
  484. {
  485. u8 *bytes = (u8 *)hdmi_ai;
  486. u8 sum = 0;
  487. int i;
  488. hdmi_ai->checksum = 0;
  489. for (i = 0; i < sizeof(*hdmi_ai); i++)
  490. sum += bytes[i];
  491. hdmi_ai->checksum = -sum;
  492. }
  493. static void hdmi_fill_audio_infoframe(struct hda_codec *codec,
  494. hda_nid_t pin_nid,
  495. u8 *dip, int size)
  496. {
  497. int i;
  498. hdmi_debug_dip_size(codec, pin_nid);
  499. hdmi_clear_dip_buffers(codec, pin_nid); /* be paranoid */
  500. hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
  501. for (i = 0; i < size; i++)
  502. hdmi_write_dip_byte(codec, pin_nid, dip[i]);
  503. }
  504. static bool hdmi_infoframe_uptodate(struct hda_codec *codec, hda_nid_t pin_nid,
  505. u8 *dip, int size)
  506. {
  507. u8 val;
  508. int i;
  509. if (snd_hda_codec_read(codec, pin_nid, 0, AC_VERB_GET_HDMI_DIP_XMIT, 0)
  510. != AC_DIPXMIT_BEST)
  511. return false;
  512. hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
  513. for (i = 0; i < size; i++) {
  514. val = snd_hda_codec_read(codec, pin_nid, 0,
  515. AC_VERB_GET_HDMI_DIP_DATA, 0);
  516. if (val != dip[i])
  517. return false;
  518. }
  519. return true;
  520. }
  521. static void hdmi_pin_setup_infoframe(struct hda_codec *codec,
  522. hda_nid_t pin_nid,
  523. int ca, int active_channels,
  524. int conn_type)
  525. {
  526. union audio_infoframe ai;
  527. memset(&ai, 0, sizeof(ai));
  528. if (conn_type == 0) { /* HDMI */
  529. struct hdmi_audio_infoframe *hdmi_ai = &ai.hdmi;
  530. hdmi_ai->type = 0x84;
  531. hdmi_ai->ver = 0x01;
  532. hdmi_ai->len = 0x0a;
  533. hdmi_ai->CC02_CT47 = active_channels - 1;
  534. hdmi_ai->CA = ca;
  535. hdmi_checksum_audio_infoframe(hdmi_ai);
  536. } else if (conn_type == 1) { /* DisplayPort */
  537. struct dp_audio_infoframe *dp_ai = &ai.dp;
  538. dp_ai->type = 0x84;
  539. dp_ai->len = 0x1b;
  540. dp_ai->ver = 0x11 << 2;
  541. dp_ai->CC02_CT47 = active_channels - 1;
  542. dp_ai->CA = ca;
  543. } else {
  544. codec_dbg(codec, "HDMI: unknown connection type at pin %d\n",
  545. pin_nid);
  546. return;
  547. }
  548. /*
  549. * sizeof(ai) is used instead of sizeof(*hdmi_ai) or
  550. * sizeof(*dp_ai) to avoid partial match/update problems when
  551. * the user switches between HDMI/DP monitors.
  552. */
  553. if (!hdmi_infoframe_uptodate(codec, pin_nid, ai.bytes,
  554. sizeof(ai))) {
  555. codec_dbg(codec,
  556. "hdmi_pin_setup_infoframe: pin=%d channels=%d ca=0x%02x\n",
  557. pin_nid,
  558. active_channels, ca);
  559. hdmi_stop_infoframe_trans(codec, pin_nid);
  560. hdmi_fill_audio_infoframe(codec, pin_nid,
  561. ai.bytes, sizeof(ai));
  562. hdmi_start_infoframe_trans(codec, pin_nid);
  563. }
  564. }
  565. static void hdmi_setup_audio_infoframe(struct hda_codec *codec,
  566. struct hdmi_spec_per_pin *per_pin,
  567. bool non_pcm)
  568. {
  569. struct hdmi_spec *spec = codec->spec;
  570. struct hdac_chmap *chmap = &spec->chmap;
  571. hda_nid_t pin_nid = per_pin->pin_nid;
  572. int channels = per_pin->channels;
  573. int active_channels;
  574. struct hdmi_eld *eld;
  575. int ca;
  576. if (!channels)
  577. return;
  578. if (is_haswell_plus(codec))
  579. snd_hda_codec_write(codec, pin_nid, 0,
  580. AC_VERB_SET_AMP_GAIN_MUTE,
  581. AMP_OUT_UNMUTE);
  582. eld = &per_pin->sink_eld;
  583. ca = hdmi_channel_allocation(&codec->core,
  584. eld->info.spk_alloc, channels,
  585. per_pin->chmap_set, non_pcm, per_pin->chmap);
  586. active_channels = hdmi_get_active_channels(ca);
  587. chmap->ops.set_channel_count(&codec->core, per_pin->cvt_nid,
  588. active_channels);
  589. /*
  590. * always configure channel mapping, it may have been changed by the
  591. * user in the meantime
  592. */
  593. hdmi_setup_channel_mapping(&spec->chmap,
  594. pin_nid, non_pcm, ca, channels,
  595. per_pin->chmap, per_pin->chmap_set);
  596. spec->ops.pin_setup_infoframe(codec, pin_nid, ca, active_channels,
  597. eld->info.conn_type);
  598. per_pin->non_pcm = non_pcm;
  599. }
  600. /*
  601. * Unsolicited events
  602. */
  603. static bool hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll);
  604. static void check_presence_and_report(struct hda_codec *codec, hda_nid_t nid)
  605. {
  606. struct hdmi_spec *spec = codec->spec;
  607. int pin_idx = pin_nid_to_pin_index(codec, nid);
  608. if (pin_idx < 0)
  609. return;
  610. if (hdmi_present_sense(get_pin(spec, pin_idx), 1))
  611. snd_hda_jack_report_sync(codec);
  612. }
  613. static void jack_callback(struct hda_codec *codec,
  614. struct hda_jack_callback *jack)
  615. {
  616. check_presence_and_report(codec, jack->nid);
  617. }
  618. static void hdmi_intrinsic_event(struct hda_codec *codec, unsigned int res)
  619. {
  620. int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
  621. struct hda_jack_tbl *jack;
  622. int dev_entry = (res & AC_UNSOL_RES_DE) >> AC_UNSOL_RES_DE_SHIFT;
  623. jack = snd_hda_jack_tbl_get_from_tag(codec, tag);
  624. if (!jack)
  625. return;
  626. jack->jack_dirty = 1;
  627. codec_dbg(codec,
  628. "HDMI hot plug event: Codec=%d Pin=%d Device=%d Inactive=%d Presence_Detect=%d ELD_Valid=%d\n",
  629. codec->addr, jack->nid, dev_entry, !!(res & AC_UNSOL_RES_IA),
  630. !!(res & AC_UNSOL_RES_PD), !!(res & AC_UNSOL_RES_ELDV));
  631. check_presence_and_report(codec, jack->nid);
  632. }
  633. static void hdmi_non_intrinsic_event(struct hda_codec *codec, unsigned int res)
  634. {
  635. int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
  636. int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
  637. int cp_state = !!(res & AC_UNSOL_RES_CP_STATE);
  638. int cp_ready = !!(res & AC_UNSOL_RES_CP_READY);
  639. codec_info(codec,
  640. "HDMI CP event: CODEC=%d TAG=%d SUBTAG=0x%x CP_STATE=%d CP_READY=%d\n",
  641. codec->addr,
  642. tag,
  643. subtag,
  644. cp_state,
  645. cp_ready);
  646. /* TODO */
  647. if (cp_state)
  648. ;
  649. if (cp_ready)
  650. ;
  651. }
  652. static void hdmi_unsol_event(struct hda_codec *codec, unsigned int res)
  653. {
  654. int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
  655. int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
  656. if (!snd_hda_jack_tbl_get_from_tag(codec, tag)) {
  657. codec_dbg(codec, "Unexpected HDMI event tag 0x%x\n", tag);
  658. return;
  659. }
  660. if (subtag == 0)
  661. hdmi_intrinsic_event(codec, res);
  662. else
  663. hdmi_non_intrinsic_event(codec, res);
  664. }
  665. static void haswell_verify_D0(struct hda_codec *codec,
  666. hda_nid_t cvt_nid, hda_nid_t nid)
  667. {
  668. int pwr;
  669. /* For Haswell, the converter 1/2 may keep in D3 state after bootup,
  670. * thus pins could only choose converter 0 for use. Make sure the
  671. * converters are in correct power state */
  672. if (!snd_hda_check_power_state(codec, cvt_nid, AC_PWRST_D0))
  673. snd_hda_codec_write(codec, cvt_nid, 0, AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
  674. if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D0)) {
  675. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
  676. AC_PWRST_D0);
  677. msleep(40);
  678. pwr = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_POWER_STATE, 0);
  679. pwr = (pwr & AC_PWRST_ACTUAL) >> AC_PWRST_ACTUAL_SHIFT;
  680. codec_dbg(codec, "Haswell HDMI audio: Power for pin 0x%x is now D%d\n", nid, pwr);
  681. }
  682. }
  683. /*
  684. * Callbacks
  685. */
  686. /* HBR should be Non-PCM, 8 channels */
  687. #define is_hbr_format(format) \
  688. ((format & AC_FMT_TYPE_NON_PCM) && (format & AC_FMT_CHAN_MASK) == 7)
  689. static int hdmi_pin_hbr_setup(struct hda_codec *codec, hda_nid_t pin_nid,
  690. bool hbr)
  691. {
  692. int pinctl, new_pinctl;
  693. if (snd_hda_query_pin_caps(codec, pin_nid) & AC_PINCAP_HBR) {
  694. pinctl = snd_hda_codec_read(codec, pin_nid, 0,
  695. AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
  696. if (pinctl < 0)
  697. return hbr ? -EINVAL : 0;
  698. new_pinctl = pinctl & ~AC_PINCTL_EPT;
  699. if (hbr)
  700. new_pinctl |= AC_PINCTL_EPT_HBR;
  701. else
  702. new_pinctl |= AC_PINCTL_EPT_NATIVE;
  703. codec_dbg(codec,
  704. "hdmi_pin_hbr_setup: NID=0x%x, %spinctl=0x%x\n",
  705. pin_nid,
  706. pinctl == new_pinctl ? "" : "new-",
  707. new_pinctl);
  708. if (pinctl != new_pinctl)
  709. snd_hda_codec_write(codec, pin_nid, 0,
  710. AC_VERB_SET_PIN_WIDGET_CONTROL,
  711. new_pinctl);
  712. } else if (hbr)
  713. return -EINVAL;
  714. return 0;
  715. }
  716. static int hdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
  717. hda_nid_t pin_nid, u32 stream_tag, int format)
  718. {
  719. struct hdmi_spec *spec = codec->spec;
  720. int err;
  721. if (is_haswell_plus(codec))
  722. haswell_verify_D0(codec, cvt_nid, pin_nid);
  723. err = spec->ops.pin_hbr_setup(codec, pin_nid, is_hbr_format(format));
  724. if (err) {
  725. codec_dbg(codec, "hdmi_setup_stream: HBR is not supported\n");
  726. return err;
  727. }
  728. snd_hda_codec_setup_stream(codec, cvt_nid, stream_tag, 0, format);
  729. return 0;
  730. }
  731. /* Try to find an available converter
  732. * If pin_idx is less then zero, just try to find an available converter.
  733. * Otherwise, try to find an available converter and get the cvt mux index
  734. * of the pin.
  735. */
  736. static int hdmi_choose_cvt(struct hda_codec *codec,
  737. int pin_idx, int *cvt_id, int *mux_id)
  738. {
  739. struct hdmi_spec *spec = codec->spec;
  740. struct hdmi_spec_per_pin *per_pin;
  741. struct hdmi_spec_per_cvt *per_cvt = NULL;
  742. int cvt_idx, mux_idx = 0;
  743. /* pin_idx < 0 means no pin will be bound to the converter */
  744. if (pin_idx < 0)
  745. per_pin = NULL;
  746. else
  747. per_pin = get_pin(spec, pin_idx);
  748. /* Dynamically assign converter to stream */
  749. for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
  750. per_cvt = get_cvt(spec, cvt_idx);
  751. /* Must not already be assigned */
  752. if (per_cvt->assigned)
  753. continue;
  754. if (per_pin == NULL)
  755. break;
  756. /* Must be in pin's mux's list of converters */
  757. for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++)
  758. if (per_pin->mux_nids[mux_idx] == per_cvt->cvt_nid)
  759. break;
  760. /* Not in mux list */
  761. if (mux_idx == per_pin->num_mux_nids)
  762. continue;
  763. break;
  764. }
  765. /* No free converters */
  766. if (cvt_idx == spec->num_cvts)
  767. return -EBUSY;
  768. if (per_pin != NULL)
  769. per_pin->mux_idx = mux_idx;
  770. if (cvt_id)
  771. *cvt_id = cvt_idx;
  772. if (mux_id)
  773. *mux_id = mux_idx;
  774. return 0;
  775. }
  776. /* Assure the pin select the right convetor */
  777. static void intel_verify_pin_cvt_connect(struct hda_codec *codec,
  778. struct hdmi_spec_per_pin *per_pin)
  779. {
  780. hda_nid_t pin_nid = per_pin->pin_nid;
  781. int mux_idx, curr;
  782. mux_idx = per_pin->mux_idx;
  783. curr = snd_hda_codec_read(codec, pin_nid, 0,
  784. AC_VERB_GET_CONNECT_SEL, 0);
  785. if (curr != mux_idx)
  786. snd_hda_codec_write_cache(codec, pin_nid, 0,
  787. AC_VERB_SET_CONNECT_SEL,
  788. mux_idx);
  789. }
  790. /* get the mux index for the converter of the pins
  791. * converter's mux index is the same for all pins on Intel platform
  792. */
  793. static int intel_cvt_id_to_mux_idx(struct hdmi_spec *spec,
  794. hda_nid_t cvt_nid)
  795. {
  796. int i;
  797. for (i = 0; i < spec->num_cvts; i++)
  798. if (spec->cvt_nids[i] == cvt_nid)
  799. return i;
  800. return -EINVAL;
  801. }
  802. /* Intel HDMI workaround to fix audio routing issue:
  803. * For some Intel display codecs, pins share the same connection list.
  804. * So a conveter can be selected by multiple pins and playback on any of these
  805. * pins will generate sound on the external display, because audio flows from
  806. * the same converter to the display pipeline. Also muting one pin may make
  807. * other pins have no sound output.
  808. * So this function assures that an assigned converter for a pin is not selected
  809. * by any other pins.
  810. */
  811. static void intel_not_share_assigned_cvt(struct hda_codec *codec,
  812. hda_nid_t pin_nid, int mux_idx)
  813. {
  814. struct hdmi_spec *spec = codec->spec;
  815. hda_nid_t nid;
  816. int cvt_idx, curr;
  817. struct hdmi_spec_per_cvt *per_cvt;
  818. /* configure all pins, including "no physical connection" ones */
  819. for_each_hda_codec_node(nid, codec) {
  820. unsigned int wid_caps = get_wcaps(codec, nid);
  821. unsigned int wid_type = get_wcaps_type(wid_caps);
  822. if (wid_type != AC_WID_PIN)
  823. continue;
  824. if (nid == pin_nid)
  825. continue;
  826. curr = snd_hda_codec_read(codec, nid, 0,
  827. AC_VERB_GET_CONNECT_SEL, 0);
  828. if (curr != mux_idx)
  829. continue;
  830. /* choose an unassigned converter. The conveters in the
  831. * connection list are in the same order as in the codec.
  832. */
  833. for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
  834. per_cvt = get_cvt(spec, cvt_idx);
  835. if (!per_cvt->assigned) {
  836. codec_dbg(codec,
  837. "choose cvt %d for pin nid %d\n",
  838. cvt_idx, nid);
  839. snd_hda_codec_write_cache(codec, nid, 0,
  840. AC_VERB_SET_CONNECT_SEL,
  841. cvt_idx);
  842. break;
  843. }
  844. }
  845. }
  846. }
  847. /* A wrapper of intel_not_share_asigned_cvt() */
  848. static void intel_not_share_assigned_cvt_nid(struct hda_codec *codec,
  849. hda_nid_t pin_nid, hda_nid_t cvt_nid)
  850. {
  851. int mux_idx;
  852. struct hdmi_spec *spec = codec->spec;
  853. if (!is_haswell_plus(codec) && !is_valleyview_plus(codec))
  854. return;
  855. /* On Intel platform, the mapping of converter nid to
  856. * mux index of the pins are always the same.
  857. * The pin nid may be 0, this means all pins will not
  858. * share the converter.
  859. */
  860. mux_idx = intel_cvt_id_to_mux_idx(spec, cvt_nid);
  861. if (mux_idx >= 0)
  862. intel_not_share_assigned_cvt(codec, pin_nid, mux_idx);
  863. }
  864. /* called in hdmi_pcm_open when no pin is assigned to the PCM
  865. * in dyn_pcm_assign mode.
  866. */
  867. static int hdmi_pcm_open_no_pin(struct hda_pcm_stream *hinfo,
  868. struct hda_codec *codec,
  869. struct snd_pcm_substream *substream)
  870. {
  871. struct hdmi_spec *spec = codec->spec;
  872. struct snd_pcm_runtime *runtime = substream->runtime;
  873. int cvt_idx, pcm_idx;
  874. struct hdmi_spec_per_cvt *per_cvt = NULL;
  875. int err;
  876. pcm_idx = hinfo_to_pcm_index(codec, hinfo);
  877. if (pcm_idx < 0)
  878. return -EINVAL;
  879. err = hdmi_choose_cvt(codec, -1, &cvt_idx, NULL);
  880. if (err)
  881. return err;
  882. per_cvt = get_cvt(spec, cvt_idx);
  883. per_cvt->assigned = 1;
  884. hinfo->nid = per_cvt->cvt_nid;
  885. intel_not_share_assigned_cvt_nid(codec, 0, per_cvt->cvt_nid);
  886. set_bit(pcm_idx, &spec->pcm_in_use);
  887. /* todo: setup spdif ctls assign */
  888. /* Initially set the converter's capabilities */
  889. hinfo->channels_min = per_cvt->channels_min;
  890. hinfo->channels_max = per_cvt->channels_max;
  891. hinfo->rates = per_cvt->rates;
  892. hinfo->formats = per_cvt->formats;
  893. hinfo->maxbps = per_cvt->maxbps;
  894. /* Store the updated parameters */
  895. runtime->hw.channels_min = hinfo->channels_min;
  896. runtime->hw.channels_max = hinfo->channels_max;
  897. runtime->hw.formats = hinfo->formats;
  898. runtime->hw.rates = hinfo->rates;
  899. snd_pcm_hw_constraint_step(substream->runtime, 0,
  900. SNDRV_PCM_HW_PARAM_CHANNELS, 2);
  901. return 0;
  902. }
  903. /*
  904. * HDA PCM callbacks
  905. */
  906. static int hdmi_pcm_open(struct hda_pcm_stream *hinfo,
  907. struct hda_codec *codec,
  908. struct snd_pcm_substream *substream)
  909. {
  910. struct hdmi_spec *spec = codec->spec;
  911. struct snd_pcm_runtime *runtime = substream->runtime;
  912. int pin_idx, cvt_idx, pcm_idx, mux_idx = 0;
  913. struct hdmi_spec_per_pin *per_pin;
  914. struct hdmi_eld *eld;
  915. struct hdmi_spec_per_cvt *per_cvt = NULL;
  916. int err;
  917. /* Validate hinfo */
  918. pcm_idx = hinfo_to_pcm_index(codec, hinfo);
  919. if (pcm_idx < 0)
  920. return -EINVAL;
  921. mutex_lock(&spec->pcm_lock);
  922. pin_idx = hinfo_to_pin_index(codec, hinfo);
  923. if (!spec->dyn_pcm_assign) {
  924. if (snd_BUG_ON(pin_idx < 0)) {
  925. mutex_unlock(&spec->pcm_lock);
  926. return -EINVAL;
  927. }
  928. } else {
  929. /* no pin is assigned to the PCM
  930. * PA need pcm open successfully when probe
  931. */
  932. if (pin_idx < 0) {
  933. err = hdmi_pcm_open_no_pin(hinfo, codec, substream);
  934. mutex_unlock(&spec->pcm_lock);
  935. return err;
  936. }
  937. }
  938. err = hdmi_choose_cvt(codec, pin_idx, &cvt_idx, &mux_idx);
  939. if (err < 0) {
  940. mutex_unlock(&spec->pcm_lock);
  941. return err;
  942. }
  943. per_cvt = get_cvt(spec, cvt_idx);
  944. /* Claim converter */
  945. per_cvt->assigned = 1;
  946. set_bit(pcm_idx, &spec->pcm_in_use);
  947. per_pin = get_pin(spec, pin_idx);
  948. per_pin->cvt_nid = per_cvt->cvt_nid;
  949. hinfo->nid = per_cvt->cvt_nid;
  950. snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0,
  951. AC_VERB_SET_CONNECT_SEL,
  952. mux_idx);
  953. /* configure unused pins to choose other converters */
  954. if (is_haswell_plus(codec) || is_valleyview_plus(codec))
  955. intel_not_share_assigned_cvt(codec, per_pin->pin_nid, mux_idx);
  956. snd_hda_spdif_ctls_assign(codec, pcm_idx, per_cvt->cvt_nid);
  957. /* Initially set the converter's capabilities */
  958. hinfo->channels_min = per_cvt->channels_min;
  959. hinfo->channels_max = per_cvt->channels_max;
  960. hinfo->rates = per_cvt->rates;
  961. hinfo->formats = per_cvt->formats;
  962. hinfo->maxbps = per_cvt->maxbps;
  963. eld = &per_pin->sink_eld;
  964. /* Restrict capabilities by ELD if this isn't disabled */
  965. if (!static_hdmi_pcm && eld->eld_valid) {
  966. snd_hdmi_eld_update_pcm_info(&eld->info, hinfo);
  967. if (hinfo->channels_min > hinfo->channels_max ||
  968. !hinfo->rates || !hinfo->formats) {
  969. per_cvt->assigned = 0;
  970. hinfo->nid = 0;
  971. snd_hda_spdif_ctls_unassign(codec, pcm_idx);
  972. mutex_unlock(&spec->pcm_lock);
  973. return -ENODEV;
  974. }
  975. }
  976. mutex_unlock(&spec->pcm_lock);
  977. /* Store the updated parameters */
  978. runtime->hw.channels_min = hinfo->channels_min;
  979. runtime->hw.channels_max = hinfo->channels_max;
  980. runtime->hw.formats = hinfo->formats;
  981. runtime->hw.rates = hinfo->rates;
  982. snd_pcm_hw_constraint_step(substream->runtime, 0,
  983. SNDRV_PCM_HW_PARAM_CHANNELS, 2);
  984. return 0;
  985. }
  986. /*
  987. * HDA/HDMI auto parsing
  988. */
  989. static int hdmi_read_pin_conn(struct hda_codec *codec, int pin_idx)
  990. {
  991. struct hdmi_spec *spec = codec->spec;
  992. struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
  993. hda_nid_t pin_nid = per_pin->pin_nid;
  994. if (!(get_wcaps(codec, pin_nid) & AC_WCAP_CONN_LIST)) {
  995. codec_warn(codec,
  996. "HDMI: pin %d wcaps %#x does not support connection list\n",
  997. pin_nid, get_wcaps(codec, pin_nid));
  998. return -EINVAL;
  999. }
  1000. per_pin->num_mux_nids = snd_hda_get_connections(codec, pin_nid,
  1001. per_pin->mux_nids,
  1002. HDA_MAX_CONNECTIONS);
  1003. return 0;
  1004. }
  1005. static int hdmi_find_pcm_slot(struct hdmi_spec *spec,
  1006. struct hdmi_spec_per_pin *per_pin)
  1007. {
  1008. int i;
  1009. /* try the prefer PCM */
  1010. if (!test_bit(per_pin->pin_nid_idx, &spec->pcm_bitmap))
  1011. return per_pin->pin_nid_idx;
  1012. /* have a second try; check the "reserved area" over num_pins */
  1013. for (i = spec->num_pins; i < spec->pcm_used; i++) {
  1014. if (!test_bit(i, &spec->pcm_bitmap))
  1015. return i;
  1016. }
  1017. /* the last try; check the empty slots in pins */
  1018. for (i = 0; i < spec->num_pins; i++) {
  1019. if (!test_bit(i, &spec->pcm_bitmap))
  1020. return i;
  1021. }
  1022. return -EBUSY;
  1023. }
  1024. static void hdmi_attach_hda_pcm(struct hdmi_spec *spec,
  1025. struct hdmi_spec_per_pin *per_pin)
  1026. {
  1027. int idx;
  1028. /* pcm already be attached to the pin */
  1029. if (per_pin->pcm)
  1030. return;
  1031. idx = hdmi_find_pcm_slot(spec, per_pin);
  1032. if (idx == -EBUSY)
  1033. return;
  1034. per_pin->pcm_idx = idx;
  1035. per_pin->pcm = get_hdmi_pcm(spec, idx);
  1036. set_bit(idx, &spec->pcm_bitmap);
  1037. }
  1038. static void hdmi_detach_hda_pcm(struct hdmi_spec *spec,
  1039. struct hdmi_spec_per_pin *per_pin)
  1040. {
  1041. int idx;
  1042. /* pcm already be detached from the pin */
  1043. if (!per_pin->pcm)
  1044. return;
  1045. idx = per_pin->pcm_idx;
  1046. per_pin->pcm_idx = -1;
  1047. per_pin->pcm = NULL;
  1048. if (idx >= 0 && idx < spec->pcm_used)
  1049. clear_bit(idx, &spec->pcm_bitmap);
  1050. }
  1051. static int hdmi_get_pin_cvt_mux(struct hdmi_spec *spec,
  1052. struct hdmi_spec_per_pin *per_pin, hda_nid_t cvt_nid)
  1053. {
  1054. int mux_idx;
  1055. for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++)
  1056. if (per_pin->mux_nids[mux_idx] == cvt_nid)
  1057. break;
  1058. return mux_idx;
  1059. }
  1060. static bool check_non_pcm_per_cvt(struct hda_codec *codec, hda_nid_t cvt_nid);
  1061. static void hdmi_pcm_setup_pin(struct hdmi_spec *spec,
  1062. struct hdmi_spec_per_pin *per_pin)
  1063. {
  1064. struct hda_codec *codec = per_pin->codec;
  1065. struct hda_pcm *pcm;
  1066. struct hda_pcm_stream *hinfo;
  1067. struct snd_pcm_substream *substream;
  1068. int mux_idx;
  1069. bool non_pcm;
  1070. if (per_pin->pcm_idx >= 0 && per_pin->pcm_idx < spec->pcm_used)
  1071. pcm = get_pcm_rec(spec, per_pin->pcm_idx);
  1072. else
  1073. return;
  1074. if (!test_bit(per_pin->pcm_idx, &spec->pcm_in_use))
  1075. return;
  1076. /* hdmi audio only uses playback and one substream */
  1077. hinfo = pcm->stream;
  1078. substream = pcm->pcm->streams[0].substream;
  1079. per_pin->cvt_nid = hinfo->nid;
  1080. mux_idx = hdmi_get_pin_cvt_mux(spec, per_pin, hinfo->nid);
  1081. if (mux_idx < per_pin->num_mux_nids)
  1082. snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0,
  1083. AC_VERB_SET_CONNECT_SEL,
  1084. mux_idx);
  1085. snd_hda_spdif_ctls_assign(codec, per_pin->pcm_idx, hinfo->nid);
  1086. non_pcm = check_non_pcm_per_cvt(codec, hinfo->nid);
  1087. if (substream->runtime)
  1088. per_pin->channels = substream->runtime->channels;
  1089. per_pin->setup = true;
  1090. per_pin->mux_idx = mux_idx;
  1091. hdmi_setup_audio_infoframe(codec, per_pin, non_pcm);
  1092. }
  1093. static void hdmi_pcm_reset_pin(struct hdmi_spec *spec,
  1094. struct hdmi_spec_per_pin *per_pin)
  1095. {
  1096. if (per_pin->pcm_idx >= 0 && per_pin->pcm_idx < spec->pcm_used)
  1097. snd_hda_spdif_ctls_unassign(per_pin->codec, per_pin->pcm_idx);
  1098. per_pin->chmap_set = false;
  1099. memset(per_pin->chmap, 0, sizeof(per_pin->chmap));
  1100. per_pin->setup = false;
  1101. per_pin->channels = 0;
  1102. }
  1103. /* update per_pin ELD from the given new ELD;
  1104. * setup info frame and notification accordingly
  1105. */
  1106. static void update_eld(struct hda_codec *codec,
  1107. struct hdmi_spec_per_pin *per_pin,
  1108. struct hdmi_eld *eld)
  1109. {
  1110. struct hdmi_eld *pin_eld = &per_pin->sink_eld;
  1111. struct hdmi_spec *spec = codec->spec;
  1112. bool old_eld_valid = pin_eld->eld_valid;
  1113. bool eld_changed;
  1114. int pcm_idx = -1;
  1115. /* for monitor disconnection, save pcm_idx firstly */
  1116. pcm_idx = per_pin->pcm_idx;
  1117. if (spec->dyn_pcm_assign) {
  1118. if (eld->eld_valid) {
  1119. hdmi_attach_hda_pcm(spec, per_pin);
  1120. hdmi_pcm_setup_pin(spec, per_pin);
  1121. } else {
  1122. hdmi_pcm_reset_pin(spec, per_pin);
  1123. hdmi_detach_hda_pcm(spec, per_pin);
  1124. }
  1125. }
  1126. /* if pcm_idx == -1, it means this is in monitor connection event
  1127. * we can get the correct pcm_idx now.
  1128. */
  1129. if (pcm_idx == -1)
  1130. pcm_idx = per_pin->pcm_idx;
  1131. if (eld->eld_valid)
  1132. snd_hdmi_show_eld(codec, &eld->info);
  1133. eld_changed = (pin_eld->eld_valid != eld->eld_valid);
  1134. if (eld->eld_valid && pin_eld->eld_valid)
  1135. if (pin_eld->eld_size != eld->eld_size ||
  1136. memcmp(pin_eld->eld_buffer, eld->eld_buffer,
  1137. eld->eld_size) != 0)
  1138. eld_changed = true;
  1139. pin_eld->eld_valid = eld->eld_valid;
  1140. pin_eld->eld_size = eld->eld_size;
  1141. if (eld->eld_valid)
  1142. memcpy(pin_eld->eld_buffer, eld->eld_buffer, eld->eld_size);
  1143. pin_eld->info = eld->info;
  1144. /*
  1145. * Re-setup pin and infoframe. This is needed e.g. when
  1146. * - sink is first plugged-in
  1147. * - transcoder can change during stream playback on Haswell
  1148. * and this can make HW reset converter selection on a pin.
  1149. */
  1150. if (eld->eld_valid && !old_eld_valid && per_pin->setup) {
  1151. if (is_haswell_plus(codec) || is_valleyview_plus(codec)) {
  1152. intel_verify_pin_cvt_connect(codec, per_pin);
  1153. intel_not_share_assigned_cvt(codec, per_pin->pin_nid,
  1154. per_pin->mux_idx);
  1155. }
  1156. hdmi_setup_audio_infoframe(codec, per_pin, per_pin->non_pcm);
  1157. }
  1158. if (eld_changed && pcm_idx >= 0)
  1159. snd_ctl_notify(codec->card,
  1160. SNDRV_CTL_EVENT_MASK_VALUE |
  1161. SNDRV_CTL_EVENT_MASK_INFO,
  1162. &get_hdmi_pcm(spec, pcm_idx)->eld_ctl->id);
  1163. }
  1164. /* update ELD and jack state via HD-audio verbs */
  1165. static bool hdmi_present_sense_via_verbs(struct hdmi_spec_per_pin *per_pin,
  1166. int repoll)
  1167. {
  1168. struct hda_jack_tbl *jack;
  1169. struct hda_codec *codec = per_pin->codec;
  1170. struct hdmi_spec *spec = codec->spec;
  1171. struct hdmi_eld *eld = &spec->temp_eld;
  1172. struct hdmi_eld *pin_eld = &per_pin->sink_eld;
  1173. hda_nid_t pin_nid = per_pin->pin_nid;
  1174. /*
  1175. * Always execute a GetPinSense verb here, even when called from
  1176. * hdmi_intrinsic_event; for some NVIDIA HW, the unsolicited
  1177. * response's PD bit is not the real PD value, but indicates that
  1178. * the real PD value changed. An older version of the HD-audio
  1179. * specification worked this way. Hence, we just ignore the data in
  1180. * the unsolicited response to avoid custom WARs.
  1181. */
  1182. int present;
  1183. bool ret;
  1184. bool do_repoll = false;
  1185. snd_hda_power_up_pm(codec);
  1186. present = snd_hda_pin_sense(codec, pin_nid);
  1187. mutex_lock(&per_pin->lock);
  1188. pin_eld->monitor_present = !!(present & AC_PINSENSE_PRESENCE);
  1189. if (pin_eld->monitor_present)
  1190. eld->eld_valid = !!(present & AC_PINSENSE_ELDV);
  1191. else
  1192. eld->eld_valid = false;
  1193. codec_dbg(codec,
  1194. "HDMI status: Codec=%d Pin=%d Presence_Detect=%d ELD_Valid=%d\n",
  1195. codec->addr, pin_nid, pin_eld->monitor_present, eld->eld_valid);
  1196. if (eld->eld_valid) {
  1197. if (spec->ops.pin_get_eld(codec, pin_nid, eld->eld_buffer,
  1198. &eld->eld_size) < 0)
  1199. eld->eld_valid = false;
  1200. else {
  1201. if (snd_hdmi_parse_eld(codec, &eld->info, eld->eld_buffer,
  1202. eld->eld_size) < 0)
  1203. eld->eld_valid = false;
  1204. }
  1205. if (!eld->eld_valid && repoll)
  1206. do_repoll = true;
  1207. }
  1208. if (do_repoll)
  1209. schedule_delayed_work(&per_pin->work, msecs_to_jiffies(300));
  1210. else
  1211. update_eld(codec, per_pin, eld);
  1212. ret = !repoll || !pin_eld->monitor_present || pin_eld->eld_valid;
  1213. jack = snd_hda_jack_tbl_get(codec, pin_nid);
  1214. if (jack)
  1215. jack->block_report = !ret;
  1216. mutex_unlock(&per_pin->lock);
  1217. snd_hda_power_down_pm(codec);
  1218. return ret;
  1219. }
  1220. static struct snd_jack *pin_idx_to_jack(struct hda_codec *codec,
  1221. struct hdmi_spec_per_pin *per_pin)
  1222. {
  1223. struct hdmi_spec *spec = codec->spec;
  1224. struct snd_jack *jack = NULL;
  1225. struct hda_jack_tbl *jack_tbl;
  1226. /* if !dyn_pcm_assign, get jack from hda_jack_tbl
  1227. * in !dyn_pcm_assign case, spec->pcm_rec[].jack is not
  1228. * NULL even after snd_hda_jack_tbl_clear() is called to
  1229. * free snd_jack. This may cause access invalid memory
  1230. * when calling snd_jack_report
  1231. */
  1232. if (per_pin->pcm_idx >= 0 && spec->dyn_pcm_assign)
  1233. jack = spec->pcm_rec[per_pin->pcm_idx].jack;
  1234. else if (!spec->dyn_pcm_assign) {
  1235. jack_tbl = snd_hda_jack_tbl_get(codec, per_pin->pin_nid);
  1236. if (jack_tbl)
  1237. jack = jack_tbl->jack;
  1238. }
  1239. return jack;
  1240. }
  1241. /* update ELD and jack state via audio component */
  1242. static void sync_eld_via_acomp(struct hda_codec *codec,
  1243. struct hdmi_spec_per_pin *per_pin)
  1244. {
  1245. struct hdmi_spec *spec = codec->spec;
  1246. struct hdmi_eld *eld = &spec->temp_eld;
  1247. struct snd_jack *jack = NULL;
  1248. int size;
  1249. mutex_lock(&per_pin->lock);
  1250. size = snd_hdac_acomp_get_eld(&codec->bus->core, per_pin->pin_nid,
  1251. &eld->monitor_present, eld->eld_buffer,
  1252. ELD_MAX_SIZE);
  1253. if (size < 0)
  1254. goto unlock;
  1255. if (size > 0) {
  1256. size = min(size, ELD_MAX_SIZE);
  1257. if (snd_hdmi_parse_eld(codec, &eld->info,
  1258. eld->eld_buffer, size) < 0)
  1259. size = -EINVAL;
  1260. }
  1261. if (size > 0) {
  1262. eld->eld_valid = true;
  1263. eld->eld_size = size;
  1264. } else {
  1265. eld->eld_valid = false;
  1266. eld->eld_size = 0;
  1267. }
  1268. /* pcm_idx >=0 before update_eld() means it is in monitor
  1269. * disconnected event. Jack must be fetched before update_eld()
  1270. */
  1271. jack = pin_idx_to_jack(codec, per_pin);
  1272. update_eld(codec, per_pin, eld);
  1273. if (jack == NULL)
  1274. jack = pin_idx_to_jack(codec, per_pin);
  1275. if (jack == NULL)
  1276. goto unlock;
  1277. snd_jack_report(jack,
  1278. eld->monitor_present ? SND_JACK_AVOUT : 0);
  1279. unlock:
  1280. mutex_unlock(&per_pin->lock);
  1281. }
  1282. static bool hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll)
  1283. {
  1284. struct hda_codec *codec = per_pin->codec;
  1285. struct hdmi_spec *spec = codec->spec;
  1286. int ret;
  1287. mutex_lock(&spec->pcm_lock);
  1288. if (codec_has_acomp(codec)) {
  1289. sync_eld_via_acomp(codec, per_pin);
  1290. ret = false; /* don't call snd_hda_jack_report_sync() */
  1291. } else {
  1292. ret = hdmi_present_sense_via_verbs(per_pin, repoll);
  1293. }
  1294. mutex_unlock(&spec->pcm_lock);
  1295. return ret;
  1296. }
  1297. static void hdmi_repoll_eld(struct work_struct *work)
  1298. {
  1299. struct hdmi_spec_per_pin *per_pin =
  1300. container_of(to_delayed_work(work), struct hdmi_spec_per_pin, work);
  1301. if (per_pin->repoll_count++ > 6)
  1302. per_pin->repoll_count = 0;
  1303. if (hdmi_present_sense(per_pin, per_pin->repoll_count))
  1304. snd_hda_jack_report_sync(per_pin->codec);
  1305. }
  1306. static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
  1307. hda_nid_t nid);
  1308. static int hdmi_add_pin(struct hda_codec *codec, hda_nid_t pin_nid)
  1309. {
  1310. struct hdmi_spec *spec = codec->spec;
  1311. unsigned int caps, config;
  1312. int pin_idx;
  1313. struct hdmi_spec_per_pin *per_pin;
  1314. int err;
  1315. caps = snd_hda_query_pin_caps(codec, pin_nid);
  1316. if (!(caps & (AC_PINCAP_HDMI | AC_PINCAP_DP)))
  1317. return 0;
  1318. config = snd_hda_codec_get_pincfg(codec, pin_nid);
  1319. if (get_defcfg_connect(config) == AC_JACK_PORT_NONE)
  1320. return 0;
  1321. if (is_haswell_plus(codec))
  1322. intel_haswell_fixup_connect_list(codec, pin_nid);
  1323. pin_idx = spec->num_pins;
  1324. per_pin = snd_array_new(&spec->pins);
  1325. if (!per_pin)
  1326. return -ENOMEM;
  1327. per_pin->pin_nid = pin_nid;
  1328. per_pin->non_pcm = false;
  1329. if (spec->dyn_pcm_assign)
  1330. per_pin->pcm_idx = -1;
  1331. else {
  1332. per_pin->pcm = get_hdmi_pcm(spec, pin_idx);
  1333. per_pin->pcm_idx = pin_idx;
  1334. }
  1335. per_pin->pin_nid_idx = pin_idx;
  1336. err = hdmi_read_pin_conn(codec, pin_idx);
  1337. if (err < 0)
  1338. return err;
  1339. spec->num_pins++;
  1340. return 0;
  1341. }
  1342. static int hdmi_add_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
  1343. {
  1344. struct hdmi_spec *spec = codec->spec;
  1345. struct hdmi_spec_per_cvt *per_cvt;
  1346. unsigned int chans;
  1347. int err;
  1348. chans = get_wcaps(codec, cvt_nid);
  1349. chans = get_wcaps_channels(chans);
  1350. per_cvt = snd_array_new(&spec->cvts);
  1351. if (!per_cvt)
  1352. return -ENOMEM;
  1353. per_cvt->cvt_nid = cvt_nid;
  1354. per_cvt->channels_min = 2;
  1355. if (chans <= 16) {
  1356. per_cvt->channels_max = chans;
  1357. if (chans > spec->chmap.channels_max)
  1358. spec->chmap.channels_max = chans;
  1359. }
  1360. err = snd_hda_query_supported_pcm(codec, cvt_nid,
  1361. &per_cvt->rates,
  1362. &per_cvt->formats,
  1363. &per_cvt->maxbps);
  1364. if (err < 0)
  1365. return err;
  1366. if (spec->num_cvts < ARRAY_SIZE(spec->cvt_nids))
  1367. spec->cvt_nids[spec->num_cvts] = cvt_nid;
  1368. spec->num_cvts++;
  1369. return 0;
  1370. }
  1371. static int hdmi_parse_codec(struct hda_codec *codec)
  1372. {
  1373. hda_nid_t nid;
  1374. int i, nodes;
  1375. nodes = snd_hda_get_sub_nodes(codec, codec->core.afg, &nid);
  1376. if (!nid || nodes < 0) {
  1377. codec_warn(codec, "HDMI: failed to get afg sub nodes\n");
  1378. return -EINVAL;
  1379. }
  1380. for (i = 0; i < nodes; i++, nid++) {
  1381. unsigned int caps;
  1382. unsigned int type;
  1383. caps = get_wcaps(codec, nid);
  1384. type = get_wcaps_type(caps);
  1385. if (!(caps & AC_WCAP_DIGITAL))
  1386. continue;
  1387. switch (type) {
  1388. case AC_WID_AUD_OUT:
  1389. hdmi_add_cvt(codec, nid);
  1390. break;
  1391. case AC_WID_PIN:
  1392. hdmi_add_pin(codec, nid);
  1393. break;
  1394. }
  1395. }
  1396. return 0;
  1397. }
  1398. /*
  1399. */
  1400. static bool check_non_pcm_per_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
  1401. {
  1402. struct hda_spdif_out *spdif;
  1403. bool non_pcm;
  1404. mutex_lock(&codec->spdif_mutex);
  1405. spdif = snd_hda_spdif_out_of_nid(codec, cvt_nid);
  1406. non_pcm = !!(spdif->status & IEC958_AES0_NONAUDIO);
  1407. mutex_unlock(&codec->spdif_mutex);
  1408. return non_pcm;
  1409. }
  1410. /*
  1411. * HDMI callbacks
  1412. */
  1413. static int generic_hdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
  1414. struct hda_codec *codec,
  1415. unsigned int stream_tag,
  1416. unsigned int format,
  1417. struct snd_pcm_substream *substream)
  1418. {
  1419. hda_nid_t cvt_nid = hinfo->nid;
  1420. struct hdmi_spec *spec = codec->spec;
  1421. int pin_idx;
  1422. struct hdmi_spec_per_pin *per_pin;
  1423. hda_nid_t pin_nid;
  1424. struct snd_pcm_runtime *runtime = substream->runtime;
  1425. bool non_pcm;
  1426. int pinctl;
  1427. int err;
  1428. mutex_lock(&spec->pcm_lock);
  1429. pin_idx = hinfo_to_pin_index(codec, hinfo);
  1430. if (spec->dyn_pcm_assign && pin_idx < 0) {
  1431. /* when dyn_pcm_assign and pcm is not bound to a pin
  1432. * skip pin setup and return 0 to make audio playback
  1433. * be ongoing
  1434. */
  1435. intel_not_share_assigned_cvt_nid(codec, 0, cvt_nid);
  1436. snd_hda_codec_setup_stream(codec, cvt_nid,
  1437. stream_tag, 0, format);
  1438. mutex_unlock(&spec->pcm_lock);
  1439. return 0;
  1440. }
  1441. if (snd_BUG_ON(pin_idx < 0)) {
  1442. mutex_unlock(&spec->pcm_lock);
  1443. return -EINVAL;
  1444. }
  1445. per_pin = get_pin(spec, pin_idx);
  1446. pin_nid = per_pin->pin_nid;
  1447. if (is_haswell_plus(codec) || is_valleyview_plus(codec)) {
  1448. /* Verify pin:cvt selections to avoid silent audio after S3.
  1449. * After S3, the audio driver restores pin:cvt selections
  1450. * but this can happen before gfx is ready and such selection
  1451. * is overlooked by HW. Thus multiple pins can share a same
  1452. * default convertor and mute control will affect each other,
  1453. * which can cause a resumed audio playback become silent
  1454. * after S3.
  1455. */
  1456. intel_verify_pin_cvt_connect(codec, per_pin);
  1457. intel_not_share_assigned_cvt(codec, pin_nid, per_pin->mux_idx);
  1458. }
  1459. /* Call sync_audio_rate to set the N/CTS/M manually if necessary */
  1460. /* Todo: add DP1.2 MST audio support later */
  1461. snd_hdac_sync_audio_rate(&codec->bus->core, pin_nid, runtime->rate);
  1462. non_pcm = check_non_pcm_per_cvt(codec, cvt_nid);
  1463. mutex_lock(&per_pin->lock);
  1464. per_pin->channels = substream->runtime->channels;
  1465. per_pin->setup = true;
  1466. hdmi_setup_audio_infoframe(codec, per_pin, non_pcm);
  1467. mutex_unlock(&per_pin->lock);
  1468. if (spec->dyn_pin_out) {
  1469. pinctl = snd_hda_codec_read(codec, pin_nid, 0,
  1470. AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
  1471. snd_hda_codec_write(codec, pin_nid, 0,
  1472. AC_VERB_SET_PIN_WIDGET_CONTROL,
  1473. pinctl | PIN_OUT);
  1474. }
  1475. err = spec->ops.setup_stream(codec, cvt_nid, pin_nid,
  1476. stream_tag, format);
  1477. mutex_unlock(&spec->pcm_lock);
  1478. return err;
  1479. }
  1480. static int generic_hdmi_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
  1481. struct hda_codec *codec,
  1482. struct snd_pcm_substream *substream)
  1483. {
  1484. snd_hda_codec_cleanup_stream(codec, hinfo->nid);
  1485. return 0;
  1486. }
  1487. static int hdmi_pcm_close(struct hda_pcm_stream *hinfo,
  1488. struct hda_codec *codec,
  1489. struct snd_pcm_substream *substream)
  1490. {
  1491. struct hdmi_spec *spec = codec->spec;
  1492. int cvt_idx, pin_idx, pcm_idx;
  1493. struct hdmi_spec_per_cvt *per_cvt;
  1494. struct hdmi_spec_per_pin *per_pin;
  1495. int pinctl;
  1496. if (hinfo->nid) {
  1497. pcm_idx = hinfo_to_pcm_index(codec, hinfo);
  1498. if (snd_BUG_ON(pcm_idx < 0))
  1499. return -EINVAL;
  1500. cvt_idx = cvt_nid_to_cvt_index(codec, hinfo->nid);
  1501. if (snd_BUG_ON(cvt_idx < 0))
  1502. return -EINVAL;
  1503. per_cvt = get_cvt(spec, cvt_idx);
  1504. snd_BUG_ON(!per_cvt->assigned);
  1505. per_cvt->assigned = 0;
  1506. hinfo->nid = 0;
  1507. mutex_lock(&spec->pcm_lock);
  1508. snd_hda_spdif_ctls_unassign(codec, pcm_idx);
  1509. clear_bit(pcm_idx, &spec->pcm_in_use);
  1510. pin_idx = hinfo_to_pin_index(codec, hinfo);
  1511. if (spec->dyn_pcm_assign && pin_idx < 0) {
  1512. mutex_unlock(&spec->pcm_lock);
  1513. return 0;
  1514. }
  1515. if (snd_BUG_ON(pin_idx < 0)) {
  1516. mutex_unlock(&spec->pcm_lock);
  1517. return -EINVAL;
  1518. }
  1519. per_pin = get_pin(spec, pin_idx);
  1520. if (spec->dyn_pin_out) {
  1521. pinctl = snd_hda_codec_read(codec, per_pin->pin_nid, 0,
  1522. AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
  1523. snd_hda_codec_write(codec, per_pin->pin_nid, 0,
  1524. AC_VERB_SET_PIN_WIDGET_CONTROL,
  1525. pinctl & ~PIN_OUT);
  1526. }
  1527. mutex_lock(&per_pin->lock);
  1528. per_pin->chmap_set = false;
  1529. memset(per_pin->chmap, 0, sizeof(per_pin->chmap));
  1530. per_pin->setup = false;
  1531. per_pin->channels = 0;
  1532. mutex_unlock(&per_pin->lock);
  1533. mutex_unlock(&spec->pcm_lock);
  1534. }
  1535. return 0;
  1536. }
  1537. static const struct hda_pcm_ops generic_ops = {
  1538. .open = hdmi_pcm_open,
  1539. .close = hdmi_pcm_close,
  1540. .prepare = generic_hdmi_playback_pcm_prepare,
  1541. .cleanup = generic_hdmi_playback_pcm_cleanup,
  1542. };
  1543. static void hdmi_get_chmap(struct hdac_device *hdac, int pcm_idx,
  1544. unsigned char *chmap)
  1545. {
  1546. struct hda_codec *codec = container_of(hdac, struct hda_codec, core);
  1547. struct hdmi_spec *spec = codec->spec;
  1548. struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
  1549. /* chmap is already set to 0 in caller */
  1550. if (!per_pin)
  1551. return;
  1552. memcpy(chmap, per_pin->chmap, ARRAY_SIZE(per_pin->chmap));
  1553. }
  1554. static void hdmi_set_chmap(struct hdac_device *hdac, int pcm_idx,
  1555. unsigned char *chmap, int prepared)
  1556. {
  1557. struct hda_codec *codec = container_of(hdac, struct hda_codec, core);
  1558. struct hdmi_spec *spec = codec->spec;
  1559. struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
  1560. mutex_lock(&per_pin->lock);
  1561. per_pin->chmap_set = true;
  1562. memcpy(per_pin->chmap, chmap, ARRAY_SIZE(per_pin->chmap));
  1563. if (prepared)
  1564. hdmi_setup_audio_infoframe(codec, per_pin, per_pin->non_pcm);
  1565. mutex_unlock(&per_pin->lock);
  1566. }
  1567. static bool is_hdmi_pcm_attached(struct hdac_device *hdac, int pcm_idx)
  1568. {
  1569. struct hda_codec *codec = container_of(hdac, struct hda_codec, core);
  1570. struct hdmi_spec *spec = codec->spec;
  1571. struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
  1572. return per_pin ? true:false;
  1573. }
  1574. static int generic_hdmi_build_pcms(struct hda_codec *codec)
  1575. {
  1576. struct hdmi_spec *spec = codec->spec;
  1577. int pin_idx;
  1578. for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
  1579. struct hda_pcm *info;
  1580. struct hda_pcm_stream *pstr;
  1581. info = snd_hda_codec_pcm_new(codec, "HDMI %d", pin_idx);
  1582. if (!info)
  1583. return -ENOMEM;
  1584. spec->pcm_rec[pin_idx].pcm = info;
  1585. spec->pcm_used++;
  1586. info->pcm_type = HDA_PCM_TYPE_HDMI;
  1587. info->own_chmap = true;
  1588. pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
  1589. pstr->substreams = 1;
  1590. pstr->ops = generic_ops;
  1591. /* other pstr fields are set in open */
  1592. }
  1593. return 0;
  1594. }
  1595. static void free_hdmi_jack_priv(struct snd_jack *jack)
  1596. {
  1597. struct hdmi_pcm *pcm = jack->private_data;
  1598. pcm->jack = NULL;
  1599. }
  1600. static int add_hdmi_jack_kctl(struct hda_codec *codec,
  1601. struct hdmi_spec *spec,
  1602. int pcm_idx,
  1603. const char *name)
  1604. {
  1605. struct snd_jack *jack;
  1606. int err;
  1607. err = snd_jack_new(codec->card, name, SND_JACK_AVOUT, &jack,
  1608. true, false);
  1609. if (err < 0)
  1610. return err;
  1611. spec->pcm_rec[pcm_idx].jack = jack;
  1612. jack->private_data = &spec->pcm_rec[pcm_idx];
  1613. jack->private_free = free_hdmi_jack_priv;
  1614. return 0;
  1615. }
  1616. static int generic_hdmi_build_jack(struct hda_codec *codec, int pcm_idx)
  1617. {
  1618. char hdmi_str[32] = "HDMI/DP";
  1619. struct hdmi_spec *spec = codec->spec;
  1620. struct hdmi_spec_per_pin *per_pin;
  1621. struct hda_jack_tbl *jack;
  1622. int pcmdev = get_pcm_rec(spec, pcm_idx)->device;
  1623. bool phantom_jack;
  1624. int ret;
  1625. if (pcmdev > 0)
  1626. sprintf(hdmi_str + strlen(hdmi_str), ",pcm=%d", pcmdev);
  1627. if (spec->dyn_pcm_assign)
  1628. return add_hdmi_jack_kctl(codec, spec, pcm_idx, hdmi_str);
  1629. /* for !dyn_pcm_assign, we still use hda_jack for compatibility */
  1630. /* if !dyn_pcm_assign, it must be non-MST mode.
  1631. * This means pcms and pins are statically mapped.
  1632. * And pcm_idx is pin_idx.
  1633. */
  1634. per_pin = get_pin(spec, pcm_idx);
  1635. phantom_jack = !is_jack_detectable(codec, per_pin->pin_nid);
  1636. if (phantom_jack)
  1637. strncat(hdmi_str, " Phantom",
  1638. sizeof(hdmi_str) - strlen(hdmi_str) - 1);
  1639. ret = snd_hda_jack_add_kctl(codec, per_pin->pin_nid, hdmi_str,
  1640. phantom_jack);
  1641. if (ret < 0)
  1642. return ret;
  1643. jack = snd_hda_jack_tbl_get(codec, per_pin->pin_nid);
  1644. if (jack == NULL)
  1645. return 0;
  1646. /* assign jack->jack to pcm_rec[].jack to
  1647. * align with dyn_pcm_assign mode
  1648. */
  1649. spec->pcm_rec[pcm_idx].jack = jack->jack;
  1650. return 0;
  1651. }
  1652. static int generic_hdmi_build_controls(struct hda_codec *codec)
  1653. {
  1654. struct hdmi_spec *spec = codec->spec;
  1655. int err;
  1656. int pin_idx, pcm_idx;
  1657. for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) {
  1658. err = generic_hdmi_build_jack(codec, pcm_idx);
  1659. if (err < 0)
  1660. return err;
  1661. /* create the spdif for each pcm
  1662. * pin will be bound when monitor is connected
  1663. */
  1664. if (spec->dyn_pcm_assign)
  1665. err = snd_hda_create_dig_out_ctls(codec,
  1666. 0, spec->cvt_nids[0],
  1667. HDA_PCM_TYPE_HDMI);
  1668. else {
  1669. struct hdmi_spec_per_pin *per_pin =
  1670. get_pin(spec, pcm_idx);
  1671. err = snd_hda_create_dig_out_ctls(codec,
  1672. per_pin->pin_nid,
  1673. per_pin->mux_nids[0],
  1674. HDA_PCM_TYPE_HDMI);
  1675. }
  1676. if (err < 0)
  1677. return err;
  1678. snd_hda_spdif_ctls_unassign(codec, pcm_idx);
  1679. /* add control for ELD Bytes */
  1680. err = hdmi_create_eld_ctl(codec, pcm_idx,
  1681. get_pcm_rec(spec, pcm_idx)->device);
  1682. if (err < 0)
  1683. return err;
  1684. }
  1685. for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
  1686. struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
  1687. hdmi_present_sense(per_pin, 0);
  1688. }
  1689. /* add channel maps */
  1690. for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) {
  1691. struct hda_pcm *pcm;
  1692. pcm = get_pcm_rec(spec, pcm_idx);
  1693. if (!pcm || !pcm->pcm)
  1694. break;
  1695. err = snd_hdac_add_chmap_ctls(pcm->pcm, pcm_idx, &spec->chmap);
  1696. if (err < 0)
  1697. return err;
  1698. }
  1699. return 0;
  1700. }
  1701. static int generic_hdmi_init_per_pins(struct hda_codec *codec)
  1702. {
  1703. struct hdmi_spec *spec = codec->spec;
  1704. int pin_idx;
  1705. for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
  1706. struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
  1707. per_pin->codec = codec;
  1708. mutex_init(&per_pin->lock);
  1709. INIT_DELAYED_WORK(&per_pin->work, hdmi_repoll_eld);
  1710. eld_proc_new(per_pin, pin_idx);
  1711. }
  1712. return 0;
  1713. }
  1714. static int generic_hdmi_init(struct hda_codec *codec)
  1715. {
  1716. struct hdmi_spec *spec = codec->spec;
  1717. int pin_idx;
  1718. for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
  1719. struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
  1720. hda_nid_t pin_nid = per_pin->pin_nid;
  1721. hdmi_init_pin(codec, pin_nid);
  1722. if (!codec_has_acomp(codec))
  1723. snd_hda_jack_detect_enable_callback(codec, pin_nid,
  1724. codec->jackpoll_interval > 0 ?
  1725. jack_callback : NULL);
  1726. }
  1727. return 0;
  1728. }
  1729. static void hdmi_array_init(struct hdmi_spec *spec, int nums)
  1730. {
  1731. snd_array_init(&spec->pins, sizeof(struct hdmi_spec_per_pin), nums);
  1732. snd_array_init(&spec->cvts, sizeof(struct hdmi_spec_per_cvt), nums);
  1733. }
  1734. static void hdmi_array_free(struct hdmi_spec *spec)
  1735. {
  1736. snd_array_free(&spec->pins);
  1737. snd_array_free(&spec->cvts);
  1738. }
  1739. static void generic_hdmi_free(struct hda_codec *codec)
  1740. {
  1741. struct hdmi_spec *spec = codec->spec;
  1742. int pin_idx, pcm_idx;
  1743. if (codec_has_acomp(codec))
  1744. snd_hdac_i915_register_notifier(NULL);
  1745. for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
  1746. struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
  1747. cancel_delayed_work_sync(&per_pin->work);
  1748. eld_proc_free(per_pin);
  1749. }
  1750. for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) {
  1751. if (spec->pcm_rec[pcm_idx].jack == NULL)
  1752. continue;
  1753. if (spec->dyn_pcm_assign)
  1754. snd_device_free(codec->card,
  1755. spec->pcm_rec[pcm_idx].jack);
  1756. else
  1757. spec->pcm_rec[pcm_idx].jack = NULL;
  1758. }
  1759. if (spec->i915_bound)
  1760. snd_hdac_i915_exit(&codec->bus->core);
  1761. hdmi_array_free(spec);
  1762. kfree(spec);
  1763. }
  1764. #ifdef CONFIG_PM
  1765. static int generic_hdmi_resume(struct hda_codec *codec)
  1766. {
  1767. struct hdmi_spec *spec = codec->spec;
  1768. int pin_idx;
  1769. codec->patch_ops.init(codec);
  1770. regcache_sync(codec->core.regmap);
  1771. for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
  1772. struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
  1773. hdmi_present_sense(per_pin, 1);
  1774. }
  1775. return 0;
  1776. }
  1777. #endif
  1778. static const struct hda_codec_ops generic_hdmi_patch_ops = {
  1779. .init = generic_hdmi_init,
  1780. .free = generic_hdmi_free,
  1781. .build_pcms = generic_hdmi_build_pcms,
  1782. .build_controls = generic_hdmi_build_controls,
  1783. .unsol_event = hdmi_unsol_event,
  1784. #ifdef CONFIG_PM
  1785. .resume = generic_hdmi_resume,
  1786. #endif
  1787. };
  1788. static const struct hdmi_ops generic_standard_hdmi_ops = {
  1789. .pin_get_eld = snd_hdmi_get_eld,
  1790. .pin_setup_infoframe = hdmi_pin_setup_infoframe,
  1791. .pin_hbr_setup = hdmi_pin_hbr_setup,
  1792. .setup_stream = hdmi_setup_stream,
  1793. };
  1794. static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
  1795. hda_nid_t nid)
  1796. {
  1797. struct hdmi_spec *spec = codec->spec;
  1798. hda_nid_t conns[4];
  1799. int nconns;
  1800. nconns = snd_hda_get_connections(codec, nid, conns, ARRAY_SIZE(conns));
  1801. if (nconns == spec->num_cvts &&
  1802. !memcmp(conns, spec->cvt_nids, spec->num_cvts * sizeof(hda_nid_t)))
  1803. return;
  1804. /* override pins connection list */
  1805. codec_dbg(codec, "hdmi: haswell: override pin connection 0x%x\n", nid);
  1806. snd_hda_override_conn_list(codec, nid, spec->num_cvts, spec->cvt_nids);
  1807. }
  1808. #define INTEL_VENDOR_NID 0x08
  1809. #define INTEL_GET_VENDOR_VERB 0xf81
  1810. #define INTEL_SET_VENDOR_VERB 0x781
  1811. #define INTEL_EN_DP12 0x02 /* enable DP 1.2 features */
  1812. #define INTEL_EN_ALL_PIN_CVTS 0x01 /* enable 2nd & 3rd pins and convertors */
  1813. static void intel_haswell_enable_all_pins(struct hda_codec *codec,
  1814. bool update_tree)
  1815. {
  1816. unsigned int vendor_param;
  1817. vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
  1818. INTEL_GET_VENDOR_VERB, 0);
  1819. if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
  1820. return;
  1821. vendor_param |= INTEL_EN_ALL_PIN_CVTS;
  1822. vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
  1823. INTEL_SET_VENDOR_VERB, vendor_param);
  1824. if (vendor_param == -1)
  1825. return;
  1826. if (update_tree)
  1827. snd_hda_codec_update_widgets(codec);
  1828. }
  1829. static void intel_haswell_fixup_enable_dp12(struct hda_codec *codec)
  1830. {
  1831. unsigned int vendor_param;
  1832. vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
  1833. INTEL_GET_VENDOR_VERB, 0);
  1834. if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
  1835. return;
  1836. /* enable DP1.2 mode */
  1837. vendor_param |= INTEL_EN_DP12;
  1838. snd_hdac_regmap_add_vendor_verb(&codec->core, INTEL_SET_VENDOR_VERB);
  1839. snd_hda_codec_write_cache(codec, INTEL_VENDOR_NID, 0,
  1840. INTEL_SET_VENDOR_VERB, vendor_param);
  1841. }
  1842. /* Haswell needs to re-issue the vendor-specific verbs before turning to D0.
  1843. * Otherwise you may get severe h/w communication errors.
  1844. */
  1845. static void haswell_set_power_state(struct hda_codec *codec, hda_nid_t fg,
  1846. unsigned int power_state)
  1847. {
  1848. if (power_state == AC_PWRST_D0) {
  1849. intel_haswell_enable_all_pins(codec, false);
  1850. intel_haswell_fixup_enable_dp12(codec);
  1851. }
  1852. snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE, power_state);
  1853. snd_hda_codec_set_power_to_all(codec, fg, power_state);
  1854. }
  1855. static void intel_pin_eld_notify(void *audio_ptr, int port)
  1856. {
  1857. struct hda_codec *codec = audio_ptr;
  1858. int pin_nid = port + 0x04;
  1859. /* skip notification during system suspend (but not in runtime PM);
  1860. * the state will be updated at resume
  1861. */
  1862. if (snd_power_get_state(codec->card) != SNDRV_CTL_POWER_D0)
  1863. return;
  1864. /* ditto during suspend/resume process itself */
  1865. if (atomic_read(&(codec)->core.in_pm))
  1866. return;
  1867. check_presence_and_report(codec, pin_nid);
  1868. }
  1869. static int patch_generic_hdmi(struct hda_codec *codec)
  1870. {
  1871. struct hdmi_spec *spec;
  1872. spec = kzalloc(sizeof(*spec), GFP_KERNEL);
  1873. if (spec == NULL)
  1874. return -ENOMEM;
  1875. spec->ops = generic_standard_hdmi_ops;
  1876. mutex_init(&spec->pcm_lock);
  1877. snd_hdac_register_chmap_ops(&codec->core, &spec->chmap);
  1878. spec->chmap.ops.get_chmap = hdmi_get_chmap;
  1879. spec->chmap.ops.set_chmap = hdmi_set_chmap;
  1880. spec->chmap.ops.is_pcm_attached = is_hdmi_pcm_attached;
  1881. codec->spec = spec;
  1882. hdmi_array_init(spec, 4);
  1883. /* Try to bind with i915 for any Intel codecs (if not done yet) */
  1884. if (!codec_has_acomp(codec) &&
  1885. (codec->core.vendor_id >> 16) == 0x8086)
  1886. if (!snd_hdac_i915_init(&codec->bus->core))
  1887. spec->i915_bound = true;
  1888. if (is_haswell_plus(codec)) {
  1889. intel_haswell_enable_all_pins(codec, true);
  1890. intel_haswell_fixup_enable_dp12(codec);
  1891. }
  1892. /* For Valleyview/Cherryview, only the display codec is in the display
  1893. * power well and can use link_power ops to request/release the power.
  1894. * For Haswell/Broadwell, the controller is also in the power well and
  1895. * can cover the codec power request, and so need not set this flag.
  1896. * For previous platforms, there is no such power well feature.
  1897. */
  1898. if (is_valleyview_plus(codec) || is_skylake(codec) ||
  1899. is_broxton(codec))
  1900. codec->core.link_power_control = 1;
  1901. if (hdmi_parse_codec(codec) < 0) {
  1902. if (spec->i915_bound)
  1903. snd_hdac_i915_exit(&codec->bus->core);
  1904. codec->spec = NULL;
  1905. kfree(spec);
  1906. return -EINVAL;
  1907. }
  1908. codec->patch_ops = generic_hdmi_patch_ops;
  1909. if (is_haswell_plus(codec)) {
  1910. codec->patch_ops.set_power_state = haswell_set_power_state;
  1911. codec->dp_mst = true;
  1912. }
  1913. /* Enable runtime pm for HDMI audio codec of HSW/BDW/SKL/BYT/BSW */
  1914. if (is_haswell_plus(codec) || is_valleyview_plus(codec))
  1915. codec->auto_runtime_pm = 1;
  1916. generic_hdmi_init_per_pins(codec);
  1917. if (codec_has_acomp(codec)) {
  1918. codec->depop_delay = 0;
  1919. spec->i915_audio_ops.audio_ptr = codec;
  1920. /* intel_audio_codec_enable() or intel_audio_codec_disable()
  1921. * will call pin_eld_notify with using audio_ptr pointer
  1922. * We need make sure audio_ptr is really setup
  1923. */
  1924. wmb();
  1925. spec->i915_audio_ops.pin_eld_notify = intel_pin_eld_notify;
  1926. snd_hdac_i915_register_notifier(&spec->i915_audio_ops);
  1927. }
  1928. WARN_ON(spec->dyn_pcm_assign && !codec_has_acomp(codec));
  1929. return 0;
  1930. }
  1931. /*
  1932. * Shared non-generic implementations
  1933. */
  1934. static int simple_playback_build_pcms(struct hda_codec *codec)
  1935. {
  1936. struct hdmi_spec *spec = codec->spec;
  1937. struct hda_pcm *info;
  1938. unsigned int chans;
  1939. struct hda_pcm_stream *pstr;
  1940. struct hdmi_spec_per_cvt *per_cvt;
  1941. per_cvt = get_cvt(spec, 0);
  1942. chans = get_wcaps(codec, per_cvt->cvt_nid);
  1943. chans = get_wcaps_channels(chans);
  1944. info = snd_hda_codec_pcm_new(codec, "HDMI 0");
  1945. if (!info)
  1946. return -ENOMEM;
  1947. spec->pcm_rec[0].pcm = info;
  1948. info->pcm_type = HDA_PCM_TYPE_HDMI;
  1949. pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
  1950. *pstr = spec->pcm_playback;
  1951. pstr->nid = per_cvt->cvt_nid;
  1952. if (pstr->channels_max <= 2 && chans && chans <= 16)
  1953. pstr->channels_max = chans;
  1954. return 0;
  1955. }
  1956. /* unsolicited event for jack sensing */
  1957. static void simple_hdmi_unsol_event(struct hda_codec *codec,
  1958. unsigned int res)
  1959. {
  1960. snd_hda_jack_set_dirty_all(codec);
  1961. snd_hda_jack_report_sync(codec);
  1962. }
  1963. /* generic_hdmi_build_jack can be used for simple_hdmi, too,
  1964. * as long as spec->pins[] is set correctly
  1965. */
  1966. #define simple_hdmi_build_jack generic_hdmi_build_jack
  1967. static int simple_playback_build_controls(struct hda_codec *codec)
  1968. {
  1969. struct hdmi_spec *spec = codec->spec;
  1970. struct hdmi_spec_per_cvt *per_cvt;
  1971. int err;
  1972. per_cvt = get_cvt(spec, 0);
  1973. err = snd_hda_create_dig_out_ctls(codec, per_cvt->cvt_nid,
  1974. per_cvt->cvt_nid,
  1975. HDA_PCM_TYPE_HDMI);
  1976. if (err < 0)
  1977. return err;
  1978. return simple_hdmi_build_jack(codec, 0);
  1979. }
  1980. static int simple_playback_init(struct hda_codec *codec)
  1981. {
  1982. struct hdmi_spec *spec = codec->spec;
  1983. struct hdmi_spec_per_pin *per_pin = get_pin(spec, 0);
  1984. hda_nid_t pin = per_pin->pin_nid;
  1985. snd_hda_codec_write(codec, pin, 0,
  1986. AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
  1987. /* some codecs require to unmute the pin */
  1988. if (get_wcaps(codec, pin) & AC_WCAP_OUT_AMP)
  1989. snd_hda_codec_write(codec, pin, 0, AC_VERB_SET_AMP_GAIN_MUTE,
  1990. AMP_OUT_UNMUTE);
  1991. snd_hda_jack_detect_enable(codec, pin);
  1992. return 0;
  1993. }
  1994. static void simple_playback_free(struct hda_codec *codec)
  1995. {
  1996. struct hdmi_spec *spec = codec->spec;
  1997. hdmi_array_free(spec);
  1998. kfree(spec);
  1999. }
  2000. /*
  2001. * Nvidia specific implementations
  2002. */
  2003. #define Nv_VERB_SET_Channel_Allocation 0xF79
  2004. #define Nv_VERB_SET_Info_Frame_Checksum 0xF7A
  2005. #define Nv_VERB_SET_Audio_Protection_On 0xF98
  2006. #define Nv_VERB_SET_Audio_Protection_Off 0xF99
  2007. #define nvhdmi_master_con_nid_7x 0x04
  2008. #define nvhdmi_master_pin_nid_7x 0x05
  2009. static const hda_nid_t nvhdmi_con_nids_7x[4] = {
  2010. /*front, rear, clfe, rear_surr */
  2011. 0x6, 0x8, 0xa, 0xc,
  2012. };
  2013. static const struct hda_verb nvhdmi_basic_init_7x_2ch[] = {
  2014. /* set audio protect on */
  2015. { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
  2016. /* enable digital output on pin widget */
  2017. { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
  2018. {} /* terminator */
  2019. };
  2020. static const struct hda_verb nvhdmi_basic_init_7x_8ch[] = {
  2021. /* set audio protect on */
  2022. { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
  2023. /* enable digital output on pin widget */
  2024. { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
  2025. { 0x7, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
  2026. { 0x9, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
  2027. { 0xb, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
  2028. { 0xd, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
  2029. {} /* terminator */
  2030. };
  2031. #ifdef LIMITED_RATE_FMT_SUPPORT
  2032. /* support only the safe format and rate */
  2033. #define SUPPORTED_RATES SNDRV_PCM_RATE_48000
  2034. #define SUPPORTED_MAXBPS 16
  2035. #define SUPPORTED_FORMATS SNDRV_PCM_FMTBIT_S16_LE
  2036. #else
  2037. /* support all rates and formats */
  2038. #define SUPPORTED_RATES \
  2039. (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
  2040. SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |\
  2041. SNDRV_PCM_RATE_192000)
  2042. #define SUPPORTED_MAXBPS 24
  2043. #define SUPPORTED_FORMATS \
  2044. (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE)
  2045. #endif
  2046. static int nvhdmi_7x_init_2ch(struct hda_codec *codec)
  2047. {
  2048. snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_2ch);
  2049. return 0;
  2050. }
  2051. static int nvhdmi_7x_init_8ch(struct hda_codec *codec)
  2052. {
  2053. snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_8ch);
  2054. return 0;
  2055. }
  2056. static unsigned int channels_2_6_8[] = {
  2057. 2, 6, 8
  2058. };
  2059. static unsigned int channels_2_8[] = {
  2060. 2, 8
  2061. };
  2062. static struct snd_pcm_hw_constraint_list hw_constraints_2_6_8_channels = {
  2063. .count = ARRAY_SIZE(channels_2_6_8),
  2064. .list = channels_2_6_8,
  2065. .mask = 0,
  2066. };
  2067. static struct snd_pcm_hw_constraint_list hw_constraints_2_8_channels = {
  2068. .count = ARRAY_SIZE(channels_2_8),
  2069. .list = channels_2_8,
  2070. .mask = 0,
  2071. };
  2072. static int simple_playback_pcm_open(struct hda_pcm_stream *hinfo,
  2073. struct hda_codec *codec,
  2074. struct snd_pcm_substream *substream)
  2075. {
  2076. struct hdmi_spec *spec = codec->spec;
  2077. struct snd_pcm_hw_constraint_list *hw_constraints_channels = NULL;
  2078. switch (codec->preset->vendor_id) {
  2079. case 0x10de0002:
  2080. case 0x10de0003:
  2081. case 0x10de0005:
  2082. case 0x10de0006:
  2083. hw_constraints_channels = &hw_constraints_2_8_channels;
  2084. break;
  2085. case 0x10de0007:
  2086. hw_constraints_channels = &hw_constraints_2_6_8_channels;
  2087. break;
  2088. default:
  2089. break;
  2090. }
  2091. if (hw_constraints_channels != NULL) {
  2092. snd_pcm_hw_constraint_list(substream->runtime, 0,
  2093. SNDRV_PCM_HW_PARAM_CHANNELS,
  2094. hw_constraints_channels);
  2095. } else {
  2096. snd_pcm_hw_constraint_step(substream->runtime, 0,
  2097. SNDRV_PCM_HW_PARAM_CHANNELS, 2);
  2098. }
  2099. return snd_hda_multi_out_dig_open(codec, &spec->multiout);
  2100. }
  2101. static int simple_playback_pcm_close(struct hda_pcm_stream *hinfo,
  2102. struct hda_codec *codec,
  2103. struct snd_pcm_substream *substream)
  2104. {
  2105. struct hdmi_spec *spec = codec->spec;
  2106. return snd_hda_multi_out_dig_close(codec, &spec->multiout);
  2107. }
  2108. static int simple_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
  2109. struct hda_codec *codec,
  2110. unsigned int stream_tag,
  2111. unsigned int format,
  2112. struct snd_pcm_substream *substream)
  2113. {
  2114. struct hdmi_spec *spec = codec->spec;
  2115. return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
  2116. stream_tag, format, substream);
  2117. }
  2118. static const struct hda_pcm_stream simple_pcm_playback = {
  2119. .substreams = 1,
  2120. .channels_min = 2,
  2121. .channels_max = 2,
  2122. .ops = {
  2123. .open = simple_playback_pcm_open,
  2124. .close = simple_playback_pcm_close,
  2125. .prepare = simple_playback_pcm_prepare
  2126. },
  2127. };
  2128. static const struct hda_codec_ops simple_hdmi_patch_ops = {
  2129. .build_controls = simple_playback_build_controls,
  2130. .build_pcms = simple_playback_build_pcms,
  2131. .init = simple_playback_init,
  2132. .free = simple_playback_free,
  2133. .unsol_event = simple_hdmi_unsol_event,
  2134. };
  2135. static int patch_simple_hdmi(struct hda_codec *codec,
  2136. hda_nid_t cvt_nid, hda_nid_t pin_nid)
  2137. {
  2138. struct hdmi_spec *spec;
  2139. struct hdmi_spec_per_cvt *per_cvt;
  2140. struct hdmi_spec_per_pin *per_pin;
  2141. spec = kzalloc(sizeof(*spec), GFP_KERNEL);
  2142. if (!spec)
  2143. return -ENOMEM;
  2144. codec->spec = spec;
  2145. hdmi_array_init(spec, 1);
  2146. spec->multiout.num_dacs = 0; /* no analog */
  2147. spec->multiout.max_channels = 2;
  2148. spec->multiout.dig_out_nid = cvt_nid;
  2149. spec->num_cvts = 1;
  2150. spec->num_pins = 1;
  2151. per_pin = snd_array_new(&spec->pins);
  2152. per_cvt = snd_array_new(&spec->cvts);
  2153. if (!per_pin || !per_cvt) {
  2154. simple_playback_free(codec);
  2155. return -ENOMEM;
  2156. }
  2157. per_cvt->cvt_nid = cvt_nid;
  2158. per_pin->pin_nid = pin_nid;
  2159. spec->pcm_playback = simple_pcm_playback;
  2160. codec->patch_ops = simple_hdmi_patch_ops;
  2161. return 0;
  2162. }
  2163. static void nvhdmi_8ch_7x_set_info_frame_parameters(struct hda_codec *codec,
  2164. int channels)
  2165. {
  2166. unsigned int chanmask;
  2167. int chan = channels ? (channels - 1) : 1;
  2168. switch (channels) {
  2169. default:
  2170. case 0:
  2171. case 2:
  2172. chanmask = 0x00;
  2173. break;
  2174. case 4:
  2175. chanmask = 0x08;
  2176. break;
  2177. case 6:
  2178. chanmask = 0x0b;
  2179. break;
  2180. case 8:
  2181. chanmask = 0x13;
  2182. break;
  2183. }
  2184. /* Set the audio infoframe channel allocation and checksum fields. The
  2185. * channel count is computed implicitly by the hardware. */
  2186. snd_hda_codec_write(codec, 0x1, 0,
  2187. Nv_VERB_SET_Channel_Allocation, chanmask);
  2188. snd_hda_codec_write(codec, 0x1, 0,
  2189. Nv_VERB_SET_Info_Frame_Checksum,
  2190. (0x71 - chan - chanmask));
  2191. }
  2192. static int nvhdmi_8ch_7x_pcm_close(struct hda_pcm_stream *hinfo,
  2193. struct hda_codec *codec,
  2194. struct snd_pcm_substream *substream)
  2195. {
  2196. struct hdmi_spec *spec = codec->spec;
  2197. int i;
  2198. snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x,
  2199. 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
  2200. for (i = 0; i < 4; i++) {
  2201. /* set the stream id */
  2202. snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
  2203. AC_VERB_SET_CHANNEL_STREAMID, 0);
  2204. /* set the stream format */
  2205. snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
  2206. AC_VERB_SET_STREAM_FORMAT, 0);
  2207. }
  2208. /* The audio hardware sends a channel count of 0x7 (8ch) when all the
  2209. * streams are disabled. */
  2210. nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
  2211. return snd_hda_multi_out_dig_close(codec, &spec->multiout);
  2212. }
  2213. static int nvhdmi_8ch_7x_pcm_prepare(struct hda_pcm_stream *hinfo,
  2214. struct hda_codec *codec,
  2215. unsigned int stream_tag,
  2216. unsigned int format,
  2217. struct snd_pcm_substream *substream)
  2218. {
  2219. int chs;
  2220. unsigned int dataDCC2, channel_id;
  2221. int i;
  2222. struct hdmi_spec *spec = codec->spec;
  2223. struct hda_spdif_out *spdif;
  2224. struct hdmi_spec_per_cvt *per_cvt;
  2225. mutex_lock(&codec->spdif_mutex);
  2226. per_cvt = get_cvt(spec, 0);
  2227. spdif = snd_hda_spdif_out_of_nid(codec, per_cvt->cvt_nid);
  2228. chs = substream->runtime->channels;
  2229. dataDCC2 = 0x2;
  2230. /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
  2231. if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
  2232. snd_hda_codec_write(codec,
  2233. nvhdmi_master_con_nid_7x,
  2234. 0,
  2235. AC_VERB_SET_DIGI_CONVERT_1,
  2236. spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
  2237. /* set the stream id */
  2238. snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
  2239. AC_VERB_SET_CHANNEL_STREAMID, (stream_tag << 4) | 0x0);
  2240. /* set the stream format */
  2241. snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
  2242. AC_VERB_SET_STREAM_FORMAT, format);
  2243. /* turn on again (if needed) */
  2244. /* enable and set the channel status audio/data flag */
  2245. if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE)) {
  2246. snd_hda_codec_write(codec,
  2247. nvhdmi_master_con_nid_7x,
  2248. 0,
  2249. AC_VERB_SET_DIGI_CONVERT_1,
  2250. spdif->ctls & 0xff);
  2251. snd_hda_codec_write(codec,
  2252. nvhdmi_master_con_nid_7x,
  2253. 0,
  2254. AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
  2255. }
  2256. for (i = 0; i < 4; i++) {
  2257. if (chs == 2)
  2258. channel_id = 0;
  2259. else
  2260. channel_id = i * 2;
  2261. /* turn off SPDIF once;
  2262. *otherwise the IEC958 bits won't be updated
  2263. */
  2264. if (codec->spdif_status_reset &&
  2265. (spdif->ctls & AC_DIG1_ENABLE))
  2266. snd_hda_codec_write(codec,
  2267. nvhdmi_con_nids_7x[i],
  2268. 0,
  2269. AC_VERB_SET_DIGI_CONVERT_1,
  2270. spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
  2271. /* set the stream id */
  2272. snd_hda_codec_write(codec,
  2273. nvhdmi_con_nids_7x[i],
  2274. 0,
  2275. AC_VERB_SET_CHANNEL_STREAMID,
  2276. (stream_tag << 4) | channel_id);
  2277. /* set the stream format */
  2278. snd_hda_codec_write(codec,
  2279. nvhdmi_con_nids_7x[i],
  2280. 0,
  2281. AC_VERB_SET_STREAM_FORMAT,
  2282. format);
  2283. /* turn on again (if needed) */
  2284. /* enable and set the channel status audio/data flag */
  2285. if (codec->spdif_status_reset &&
  2286. (spdif->ctls & AC_DIG1_ENABLE)) {
  2287. snd_hda_codec_write(codec,
  2288. nvhdmi_con_nids_7x[i],
  2289. 0,
  2290. AC_VERB_SET_DIGI_CONVERT_1,
  2291. spdif->ctls & 0xff);
  2292. snd_hda_codec_write(codec,
  2293. nvhdmi_con_nids_7x[i],
  2294. 0,
  2295. AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
  2296. }
  2297. }
  2298. nvhdmi_8ch_7x_set_info_frame_parameters(codec, chs);
  2299. mutex_unlock(&codec->spdif_mutex);
  2300. return 0;
  2301. }
  2302. static const struct hda_pcm_stream nvhdmi_pcm_playback_8ch_7x = {
  2303. .substreams = 1,
  2304. .channels_min = 2,
  2305. .channels_max = 8,
  2306. .nid = nvhdmi_master_con_nid_7x,
  2307. .rates = SUPPORTED_RATES,
  2308. .maxbps = SUPPORTED_MAXBPS,
  2309. .formats = SUPPORTED_FORMATS,
  2310. .ops = {
  2311. .open = simple_playback_pcm_open,
  2312. .close = nvhdmi_8ch_7x_pcm_close,
  2313. .prepare = nvhdmi_8ch_7x_pcm_prepare
  2314. },
  2315. };
  2316. static int patch_nvhdmi_2ch(struct hda_codec *codec)
  2317. {
  2318. struct hdmi_spec *spec;
  2319. int err = patch_simple_hdmi(codec, nvhdmi_master_con_nid_7x,
  2320. nvhdmi_master_pin_nid_7x);
  2321. if (err < 0)
  2322. return err;
  2323. codec->patch_ops.init = nvhdmi_7x_init_2ch;
  2324. /* override the PCM rates, etc, as the codec doesn't give full list */
  2325. spec = codec->spec;
  2326. spec->pcm_playback.rates = SUPPORTED_RATES;
  2327. spec->pcm_playback.maxbps = SUPPORTED_MAXBPS;
  2328. spec->pcm_playback.formats = SUPPORTED_FORMATS;
  2329. return 0;
  2330. }
  2331. static int nvhdmi_7x_8ch_build_pcms(struct hda_codec *codec)
  2332. {
  2333. struct hdmi_spec *spec = codec->spec;
  2334. int err = simple_playback_build_pcms(codec);
  2335. if (!err) {
  2336. struct hda_pcm *info = get_pcm_rec(spec, 0);
  2337. info->own_chmap = true;
  2338. }
  2339. return err;
  2340. }
  2341. static int nvhdmi_7x_8ch_build_controls(struct hda_codec *codec)
  2342. {
  2343. struct hdmi_spec *spec = codec->spec;
  2344. struct hda_pcm *info;
  2345. struct snd_pcm_chmap *chmap;
  2346. int err;
  2347. err = simple_playback_build_controls(codec);
  2348. if (err < 0)
  2349. return err;
  2350. /* add channel maps */
  2351. info = get_pcm_rec(spec, 0);
  2352. err = snd_pcm_add_chmap_ctls(info->pcm,
  2353. SNDRV_PCM_STREAM_PLAYBACK,
  2354. snd_pcm_alt_chmaps, 8, 0, &chmap);
  2355. if (err < 0)
  2356. return err;
  2357. switch (codec->preset->vendor_id) {
  2358. case 0x10de0002:
  2359. case 0x10de0003:
  2360. case 0x10de0005:
  2361. case 0x10de0006:
  2362. chmap->channel_mask = (1U << 2) | (1U << 8);
  2363. break;
  2364. case 0x10de0007:
  2365. chmap->channel_mask = (1U << 2) | (1U << 6) | (1U << 8);
  2366. }
  2367. return 0;
  2368. }
  2369. static int patch_nvhdmi_8ch_7x(struct hda_codec *codec)
  2370. {
  2371. struct hdmi_spec *spec;
  2372. int err = patch_nvhdmi_2ch(codec);
  2373. if (err < 0)
  2374. return err;
  2375. spec = codec->spec;
  2376. spec->multiout.max_channels = 8;
  2377. spec->pcm_playback = nvhdmi_pcm_playback_8ch_7x;
  2378. codec->patch_ops.init = nvhdmi_7x_init_8ch;
  2379. codec->patch_ops.build_pcms = nvhdmi_7x_8ch_build_pcms;
  2380. codec->patch_ops.build_controls = nvhdmi_7x_8ch_build_controls;
  2381. /* Initialize the audio infoframe channel mask and checksum to something
  2382. * valid */
  2383. nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
  2384. return 0;
  2385. }
  2386. /*
  2387. * NVIDIA codecs ignore ASP mapping for 2ch - confirmed on:
  2388. * - 0x10de0015
  2389. * - 0x10de0040
  2390. */
  2391. static int nvhdmi_chmap_cea_alloc_validate_get_type(struct hdac_chmap *chmap,
  2392. struct hdac_cea_channel_speaker_allocation *cap, int channels)
  2393. {
  2394. if (cap->ca_index == 0x00 && channels == 2)
  2395. return SNDRV_CTL_TLVT_CHMAP_FIXED;
  2396. return chmap->ops.chmap_cea_alloc_validate_get_type(
  2397. chmap, cap, channels);
  2398. }
  2399. static int nvhdmi_chmap_validate(struct hdac_chmap *chmap,
  2400. int ca, int chs, unsigned char *map)
  2401. {
  2402. if (ca == 0x00 && (map[0] != SNDRV_CHMAP_FL || map[1] != SNDRV_CHMAP_FR))
  2403. return -EINVAL;
  2404. return 0;
  2405. }
  2406. static int patch_nvhdmi(struct hda_codec *codec)
  2407. {
  2408. struct hdmi_spec *spec;
  2409. int err;
  2410. err = patch_generic_hdmi(codec);
  2411. if (err)
  2412. return err;
  2413. spec = codec->spec;
  2414. spec->dyn_pin_out = true;
  2415. spec->chmap.ops.chmap_cea_alloc_validate_get_type =
  2416. nvhdmi_chmap_cea_alloc_validate_get_type;
  2417. spec->chmap.ops.chmap_validate = nvhdmi_chmap_validate;
  2418. return 0;
  2419. }
  2420. /*
  2421. * The HDA codec on NVIDIA Tegra contains two scratch registers that are
  2422. * accessed using vendor-defined verbs. These registers can be used for
  2423. * interoperability between the HDA and HDMI drivers.
  2424. */
  2425. /* Audio Function Group node */
  2426. #define NVIDIA_AFG_NID 0x01
  2427. /*
  2428. * The SCRATCH0 register is used to notify the HDMI codec of changes in audio
  2429. * format. On Tegra, bit 31 is used as a trigger that causes an interrupt to
  2430. * be raised in the HDMI codec. The remainder of the bits is arbitrary. This
  2431. * implementation stores the HDA format (see AC_FMT_*) in bits [15:0] and an
  2432. * additional bit (at position 30) to signal the validity of the format.
  2433. *
  2434. * | 31 | 30 | 29 16 | 15 0 |
  2435. * +---------+-------+--------+--------+
  2436. * | TRIGGER | VALID | UNUSED | FORMAT |
  2437. * +-----------------------------------|
  2438. *
  2439. * Note that for the trigger bit to take effect it needs to change value
  2440. * (i.e. it needs to be toggled).
  2441. */
  2442. #define NVIDIA_GET_SCRATCH0 0xfa6
  2443. #define NVIDIA_SET_SCRATCH0_BYTE0 0xfa7
  2444. #define NVIDIA_SET_SCRATCH0_BYTE1 0xfa8
  2445. #define NVIDIA_SET_SCRATCH0_BYTE2 0xfa9
  2446. #define NVIDIA_SET_SCRATCH0_BYTE3 0xfaa
  2447. #define NVIDIA_SCRATCH_TRIGGER (1 << 7)
  2448. #define NVIDIA_SCRATCH_VALID (1 << 6)
  2449. #define NVIDIA_GET_SCRATCH1 0xfab
  2450. #define NVIDIA_SET_SCRATCH1_BYTE0 0xfac
  2451. #define NVIDIA_SET_SCRATCH1_BYTE1 0xfad
  2452. #define NVIDIA_SET_SCRATCH1_BYTE2 0xfae
  2453. #define NVIDIA_SET_SCRATCH1_BYTE3 0xfaf
  2454. /*
  2455. * The format parameter is the HDA audio format (see AC_FMT_*). If set to 0,
  2456. * the format is invalidated so that the HDMI codec can be disabled.
  2457. */
  2458. static void tegra_hdmi_set_format(struct hda_codec *codec, unsigned int format)
  2459. {
  2460. unsigned int value;
  2461. /* bits [31:30] contain the trigger and valid bits */
  2462. value = snd_hda_codec_read(codec, NVIDIA_AFG_NID, 0,
  2463. NVIDIA_GET_SCRATCH0, 0);
  2464. value = (value >> 24) & 0xff;
  2465. /* bits [15:0] are used to store the HDA format */
  2466. snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
  2467. NVIDIA_SET_SCRATCH0_BYTE0,
  2468. (format >> 0) & 0xff);
  2469. snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
  2470. NVIDIA_SET_SCRATCH0_BYTE1,
  2471. (format >> 8) & 0xff);
  2472. /* bits [16:24] are unused */
  2473. snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
  2474. NVIDIA_SET_SCRATCH0_BYTE2, 0);
  2475. /*
  2476. * Bit 30 signals that the data is valid and hence that HDMI audio can
  2477. * be enabled.
  2478. */
  2479. if (format == 0)
  2480. value &= ~NVIDIA_SCRATCH_VALID;
  2481. else
  2482. value |= NVIDIA_SCRATCH_VALID;
  2483. /*
  2484. * Whenever the trigger bit is toggled, an interrupt is raised in the
  2485. * HDMI codec. The HDMI driver will use that as trigger to update its
  2486. * configuration.
  2487. */
  2488. value ^= NVIDIA_SCRATCH_TRIGGER;
  2489. snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
  2490. NVIDIA_SET_SCRATCH0_BYTE3, value);
  2491. }
  2492. static int tegra_hdmi_pcm_prepare(struct hda_pcm_stream *hinfo,
  2493. struct hda_codec *codec,
  2494. unsigned int stream_tag,
  2495. unsigned int format,
  2496. struct snd_pcm_substream *substream)
  2497. {
  2498. int err;
  2499. err = generic_hdmi_playback_pcm_prepare(hinfo, codec, stream_tag,
  2500. format, substream);
  2501. if (err < 0)
  2502. return err;
  2503. /* notify the HDMI codec of the format change */
  2504. tegra_hdmi_set_format(codec, format);
  2505. return 0;
  2506. }
  2507. static int tegra_hdmi_pcm_cleanup(struct hda_pcm_stream *hinfo,
  2508. struct hda_codec *codec,
  2509. struct snd_pcm_substream *substream)
  2510. {
  2511. /* invalidate the format in the HDMI codec */
  2512. tegra_hdmi_set_format(codec, 0);
  2513. return generic_hdmi_playback_pcm_cleanup(hinfo, codec, substream);
  2514. }
  2515. static struct hda_pcm *hda_find_pcm_by_type(struct hda_codec *codec, int type)
  2516. {
  2517. struct hdmi_spec *spec = codec->spec;
  2518. unsigned int i;
  2519. for (i = 0; i < spec->num_pins; i++) {
  2520. struct hda_pcm *pcm = get_pcm_rec(spec, i);
  2521. if (pcm->pcm_type == type)
  2522. return pcm;
  2523. }
  2524. return NULL;
  2525. }
  2526. static int tegra_hdmi_build_pcms(struct hda_codec *codec)
  2527. {
  2528. struct hda_pcm_stream *stream;
  2529. struct hda_pcm *pcm;
  2530. int err;
  2531. err = generic_hdmi_build_pcms(codec);
  2532. if (err < 0)
  2533. return err;
  2534. pcm = hda_find_pcm_by_type(codec, HDA_PCM_TYPE_HDMI);
  2535. if (!pcm)
  2536. return -ENODEV;
  2537. /*
  2538. * Override ->prepare() and ->cleanup() operations to notify the HDMI
  2539. * codec about format changes.
  2540. */
  2541. stream = &pcm->stream[SNDRV_PCM_STREAM_PLAYBACK];
  2542. stream->ops.prepare = tegra_hdmi_pcm_prepare;
  2543. stream->ops.cleanup = tegra_hdmi_pcm_cleanup;
  2544. return 0;
  2545. }
  2546. static int patch_tegra_hdmi(struct hda_codec *codec)
  2547. {
  2548. int err;
  2549. err = patch_generic_hdmi(codec);
  2550. if (err)
  2551. return err;
  2552. codec->patch_ops.build_pcms = tegra_hdmi_build_pcms;
  2553. return 0;
  2554. }
  2555. /*
  2556. * ATI/AMD-specific implementations
  2557. */
  2558. #define is_amdhdmi_rev3_or_later(codec) \
  2559. ((codec)->core.vendor_id == 0x1002aa01 && \
  2560. ((codec)->core.revision_id & 0xff00) >= 0x0300)
  2561. #define has_amd_full_remap_support(codec) is_amdhdmi_rev3_or_later(codec)
  2562. /* ATI/AMD specific HDA pin verbs, see the AMD HDA Verbs specification */
  2563. #define ATI_VERB_SET_CHANNEL_ALLOCATION 0x771
  2564. #define ATI_VERB_SET_DOWNMIX_INFO 0x772
  2565. #define ATI_VERB_SET_MULTICHANNEL_01 0x777
  2566. #define ATI_VERB_SET_MULTICHANNEL_23 0x778
  2567. #define ATI_VERB_SET_MULTICHANNEL_45 0x779
  2568. #define ATI_VERB_SET_MULTICHANNEL_67 0x77a
  2569. #define ATI_VERB_SET_HBR_CONTROL 0x77c
  2570. #define ATI_VERB_SET_MULTICHANNEL_1 0x785
  2571. #define ATI_VERB_SET_MULTICHANNEL_3 0x786
  2572. #define ATI_VERB_SET_MULTICHANNEL_5 0x787
  2573. #define ATI_VERB_SET_MULTICHANNEL_7 0x788
  2574. #define ATI_VERB_SET_MULTICHANNEL_MODE 0x789
  2575. #define ATI_VERB_GET_CHANNEL_ALLOCATION 0xf71
  2576. #define ATI_VERB_GET_DOWNMIX_INFO 0xf72
  2577. #define ATI_VERB_GET_MULTICHANNEL_01 0xf77
  2578. #define ATI_VERB_GET_MULTICHANNEL_23 0xf78
  2579. #define ATI_VERB_GET_MULTICHANNEL_45 0xf79
  2580. #define ATI_VERB_GET_MULTICHANNEL_67 0xf7a
  2581. #define ATI_VERB_GET_HBR_CONTROL 0xf7c
  2582. #define ATI_VERB_GET_MULTICHANNEL_1 0xf85
  2583. #define ATI_VERB_GET_MULTICHANNEL_3 0xf86
  2584. #define ATI_VERB_GET_MULTICHANNEL_5 0xf87
  2585. #define ATI_VERB_GET_MULTICHANNEL_7 0xf88
  2586. #define ATI_VERB_GET_MULTICHANNEL_MODE 0xf89
  2587. /* AMD specific HDA cvt verbs */
  2588. #define ATI_VERB_SET_RAMP_RATE 0x770
  2589. #define ATI_VERB_GET_RAMP_RATE 0xf70
  2590. #define ATI_OUT_ENABLE 0x1
  2591. #define ATI_MULTICHANNEL_MODE_PAIRED 0
  2592. #define ATI_MULTICHANNEL_MODE_SINGLE 1
  2593. #define ATI_HBR_CAPABLE 0x01
  2594. #define ATI_HBR_ENABLE 0x10
  2595. static int atihdmi_pin_get_eld(struct hda_codec *codec, hda_nid_t nid,
  2596. unsigned char *buf, int *eld_size)
  2597. {
  2598. /* call hda_eld.c ATI/AMD-specific function */
  2599. return snd_hdmi_get_eld_ati(codec, nid, buf, eld_size,
  2600. is_amdhdmi_rev3_or_later(codec));
  2601. }
  2602. static void atihdmi_pin_setup_infoframe(struct hda_codec *codec, hda_nid_t pin_nid, int ca,
  2603. int active_channels, int conn_type)
  2604. {
  2605. snd_hda_codec_write(codec, pin_nid, 0, ATI_VERB_SET_CHANNEL_ALLOCATION, ca);
  2606. }
  2607. static int atihdmi_paired_swap_fc_lfe(int pos)
  2608. {
  2609. /*
  2610. * ATI/AMD have automatic FC/LFE swap built-in
  2611. * when in pairwise mapping mode.
  2612. */
  2613. switch (pos) {
  2614. /* see channel_allocations[].speakers[] */
  2615. case 2: return 3;
  2616. case 3: return 2;
  2617. default: break;
  2618. }
  2619. return pos;
  2620. }
  2621. static int atihdmi_paired_chmap_validate(struct hdac_chmap *chmap,
  2622. int ca, int chs, unsigned char *map)
  2623. {
  2624. struct hdac_cea_channel_speaker_allocation *cap;
  2625. int i, j;
  2626. /* check that only channel pairs need to be remapped on old pre-rev3 ATI/AMD */
  2627. cap = hdmi_get_ch_alloc_from_ca(ca);
  2628. for (i = 0; i < chs; ++i) {
  2629. int mask = to_spk_mask(map[i]);
  2630. bool ok = false;
  2631. bool companion_ok = false;
  2632. if (!mask)
  2633. continue;
  2634. for (j = 0 + i % 2; j < 8; j += 2) {
  2635. int chan_idx = 7 - atihdmi_paired_swap_fc_lfe(j);
  2636. if (cap->speakers[chan_idx] == mask) {
  2637. /* channel is in a supported position */
  2638. ok = true;
  2639. if (i % 2 == 0 && i + 1 < chs) {
  2640. /* even channel, check the odd companion */
  2641. int comp_chan_idx = 7 - atihdmi_paired_swap_fc_lfe(j + 1);
  2642. int comp_mask_req = to_spk_mask(map[i+1]);
  2643. int comp_mask_act = cap->speakers[comp_chan_idx];
  2644. if (comp_mask_req == comp_mask_act)
  2645. companion_ok = true;
  2646. else
  2647. return -EINVAL;
  2648. }
  2649. break;
  2650. }
  2651. }
  2652. if (!ok)
  2653. return -EINVAL;
  2654. if (companion_ok)
  2655. i++; /* companion channel already checked */
  2656. }
  2657. return 0;
  2658. }
  2659. static int atihdmi_pin_set_slot_channel(struct hdac_device *hdac,
  2660. hda_nid_t pin_nid, int hdmi_slot, int stream_channel)
  2661. {
  2662. struct hda_codec *codec = container_of(hdac, struct hda_codec, core);
  2663. int verb;
  2664. int ati_channel_setup = 0;
  2665. if (hdmi_slot > 7)
  2666. return -EINVAL;
  2667. if (!has_amd_full_remap_support(codec)) {
  2668. hdmi_slot = atihdmi_paired_swap_fc_lfe(hdmi_slot);
  2669. /* In case this is an odd slot but without stream channel, do not
  2670. * disable the slot since the corresponding even slot could have a
  2671. * channel. In case neither have a channel, the slot pair will be
  2672. * disabled when this function is called for the even slot. */
  2673. if (hdmi_slot % 2 != 0 && stream_channel == 0xf)
  2674. return 0;
  2675. hdmi_slot -= hdmi_slot % 2;
  2676. if (stream_channel != 0xf)
  2677. stream_channel -= stream_channel % 2;
  2678. }
  2679. verb = ATI_VERB_SET_MULTICHANNEL_01 + hdmi_slot/2 + (hdmi_slot % 2) * 0x00e;
  2680. /* ati_channel_setup format: [7..4] = stream_channel_id, [1] = mute, [0] = enable */
  2681. if (stream_channel != 0xf)
  2682. ati_channel_setup = (stream_channel << 4) | ATI_OUT_ENABLE;
  2683. return snd_hda_codec_write(codec, pin_nid, 0, verb, ati_channel_setup);
  2684. }
  2685. static int atihdmi_pin_get_slot_channel(struct hdac_device *hdac,
  2686. hda_nid_t pin_nid, int asp_slot)
  2687. {
  2688. struct hda_codec *codec = container_of(hdac, struct hda_codec, core);
  2689. bool was_odd = false;
  2690. int ati_asp_slot = asp_slot;
  2691. int verb;
  2692. int ati_channel_setup;
  2693. if (asp_slot > 7)
  2694. return -EINVAL;
  2695. if (!has_amd_full_remap_support(codec)) {
  2696. ati_asp_slot = atihdmi_paired_swap_fc_lfe(asp_slot);
  2697. if (ati_asp_slot % 2 != 0) {
  2698. ati_asp_slot -= 1;
  2699. was_odd = true;
  2700. }
  2701. }
  2702. verb = ATI_VERB_GET_MULTICHANNEL_01 + ati_asp_slot/2 + (ati_asp_slot % 2) * 0x00e;
  2703. ati_channel_setup = snd_hda_codec_read(codec, pin_nid, 0, verb, 0);
  2704. if (!(ati_channel_setup & ATI_OUT_ENABLE))
  2705. return 0xf;
  2706. return ((ati_channel_setup & 0xf0) >> 4) + !!was_odd;
  2707. }
  2708. static int atihdmi_paired_chmap_cea_alloc_validate_get_type(
  2709. struct hdac_chmap *chmap,
  2710. struct hdac_cea_channel_speaker_allocation *cap,
  2711. int channels)
  2712. {
  2713. int c;
  2714. /*
  2715. * Pre-rev3 ATI/AMD codecs operate in a paired channel mode, so
  2716. * we need to take that into account (a single channel may take 2
  2717. * channel slots if we need to carry a silent channel next to it).
  2718. * On Rev3+ AMD codecs this function is not used.
  2719. */
  2720. int chanpairs = 0;
  2721. /* We only produce even-numbered channel count TLVs */
  2722. if ((channels % 2) != 0)
  2723. return -1;
  2724. for (c = 0; c < 7; c += 2) {
  2725. if (cap->speakers[c] || cap->speakers[c+1])
  2726. chanpairs++;
  2727. }
  2728. if (chanpairs * 2 != channels)
  2729. return -1;
  2730. return SNDRV_CTL_TLVT_CHMAP_PAIRED;
  2731. }
  2732. static void atihdmi_paired_cea_alloc_to_tlv_chmap(struct hdac_chmap *hchmap,
  2733. struct hdac_cea_channel_speaker_allocation *cap,
  2734. unsigned int *chmap, int channels)
  2735. {
  2736. /* produce paired maps for pre-rev3 ATI/AMD codecs */
  2737. int count = 0;
  2738. int c;
  2739. for (c = 7; c >= 0; c--) {
  2740. int chan = 7 - atihdmi_paired_swap_fc_lfe(7 - c);
  2741. int spk = cap->speakers[chan];
  2742. if (!spk) {
  2743. /* add N/A channel if the companion channel is occupied */
  2744. if (cap->speakers[chan + (chan % 2 ? -1 : 1)])
  2745. chmap[count++] = SNDRV_CHMAP_NA;
  2746. continue;
  2747. }
  2748. chmap[count++] = spk_to_chmap(spk);
  2749. }
  2750. WARN_ON(count != channels);
  2751. }
  2752. static int atihdmi_pin_hbr_setup(struct hda_codec *codec, hda_nid_t pin_nid,
  2753. bool hbr)
  2754. {
  2755. int hbr_ctl, hbr_ctl_new;
  2756. hbr_ctl = snd_hda_codec_read(codec, pin_nid, 0, ATI_VERB_GET_HBR_CONTROL, 0);
  2757. if (hbr_ctl >= 0 && (hbr_ctl & ATI_HBR_CAPABLE)) {
  2758. if (hbr)
  2759. hbr_ctl_new = hbr_ctl | ATI_HBR_ENABLE;
  2760. else
  2761. hbr_ctl_new = hbr_ctl & ~ATI_HBR_ENABLE;
  2762. codec_dbg(codec,
  2763. "atihdmi_pin_hbr_setup: NID=0x%x, %shbr-ctl=0x%x\n",
  2764. pin_nid,
  2765. hbr_ctl == hbr_ctl_new ? "" : "new-",
  2766. hbr_ctl_new);
  2767. if (hbr_ctl != hbr_ctl_new)
  2768. snd_hda_codec_write(codec, pin_nid, 0,
  2769. ATI_VERB_SET_HBR_CONTROL,
  2770. hbr_ctl_new);
  2771. } else if (hbr)
  2772. return -EINVAL;
  2773. return 0;
  2774. }
  2775. static int atihdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
  2776. hda_nid_t pin_nid, u32 stream_tag, int format)
  2777. {
  2778. if (is_amdhdmi_rev3_or_later(codec)) {
  2779. int ramp_rate = 180; /* default as per AMD spec */
  2780. /* disable ramp-up/down for non-pcm as per AMD spec */
  2781. if (format & AC_FMT_TYPE_NON_PCM)
  2782. ramp_rate = 0;
  2783. snd_hda_codec_write(codec, cvt_nid, 0, ATI_VERB_SET_RAMP_RATE, ramp_rate);
  2784. }
  2785. return hdmi_setup_stream(codec, cvt_nid, pin_nid, stream_tag, format);
  2786. }
  2787. static int atihdmi_init(struct hda_codec *codec)
  2788. {
  2789. struct hdmi_spec *spec = codec->spec;
  2790. int pin_idx, err;
  2791. err = generic_hdmi_init(codec);
  2792. if (err)
  2793. return err;
  2794. for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
  2795. struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
  2796. /* make sure downmix information in infoframe is zero */
  2797. snd_hda_codec_write(codec, per_pin->pin_nid, 0, ATI_VERB_SET_DOWNMIX_INFO, 0);
  2798. /* enable channel-wise remap mode if supported */
  2799. if (has_amd_full_remap_support(codec))
  2800. snd_hda_codec_write(codec, per_pin->pin_nid, 0,
  2801. ATI_VERB_SET_MULTICHANNEL_MODE,
  2802. ATI_MULTICHANNEL_MODE_SINGLE);
  2803. }
  2804. return 0;
  2805. }
  2806. static int patch_atihdmi(struct hda_codec *codec)
  2807. {
  2808. struct hdmi_spec *spec;
  2809. struct hdmi_spec_per_cvt *per_cvt;
  2810. int err, cvt_idx;
  2811. err = patch_generic_hdmi(codec);
  2812. if (err)
  2813. return err;
  2814. codec->patch_ops.init = atihdmi_init;
  2815. spec = codec->spec;
  2816. spec->ops.pin_get_eld = atihdmi_pin_get_eld;
  2817. spec->ops.pin_setup_infoframe = atihdmi_pin_setup_infoframe;
  2818. spec->ops.pin_hbr_setup = atihdmi_pin_hbr_setup;
  2819. spec->ops.setup_stream = atihdmi_setup_stream;
  2820. if (!has_amd_full_remap_support(codec)) {
  2821. /* override to ATI/AMD-specific versions with pairwise mapping */
  2822. spec->chmap.ops.chmap_cea_alloc_validate_get_type =
  2823. atihdmi_paired_chmap_cea_alloc_validate_get_type;
  2824. spec->chmap.ops.cea_alloc_to_tlv_chmap =
  2825. atihdmi_paired_cea_alloc_to_tlv_chmap;
  2826. spec->chmap.ops.chmap_validate = atihdmi_paired_chmap_validate;
  2827. spec->chmap.ops.pin_get_slot_channel =
  2828. atihdmi_pin_get_slot_channel;
  2829. spec->chmap.ops.pin_set_slot_channel =
  2830. atihdmi_pin_set_slot_channel;
  2831. }
  2832. /* ATI/AMD converters do not advertise all of their capabilities */
  2833. for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
  2834. per_cvt = get_cvt(spec, cvt_idx);
  2835. per_cvt->channels_max = max(per_cvt->channels_max, 8u);
  2836. per_cvt->rates |= SUPPORTED_RATES;
  2837. per_cvt->formats |= SUPPORTED_FORMATS;
  2838. per_cvt->maxbps = max(per_cvt->maxbps, 24u);
  2839. }
  2840. spec->chmap.channels_max = max(spec->chmap.channels_max, 8u);
  2841. return 0;
  2842. }
  2843. /* VIA HDMI Implementation */
  2844. #define VIAHDMI_CVT_NID 0x02 /* audio converter1 */
  2845. #define VIAHDMI_PIN_NID 0x03 /* HDMI output pin1 */
  2846. static int patch_via_hdmi(struct hda_codec *codec)
  2847. {
  2848. return patch_simple_hdmi(codec, VIAHDMI_CVT_NID, VIAHDMI_PIN_NID);
  2849. }
  2850. /*
  2851. * patch entries
  2852. */
  2853. static const struct hda_device_id snd_hda_id_hdmi[] = {
  2854. HDA_CODEC_ENTRY(0x1002793c, "RS600 HDMI", patch_atihdmi),
  2855. HDA_CODEC_ENTRY(0x10027919, "RS600 HDMI", patch_atihdmi),
  2856. HDA_CODEC_ENTRY(0x1002791a, "RS690/780 HDMI", patch_atihdmi),
  2857. HDA_CODEC_ENTRY(0x1002aa01, "R6xx HDMI", patch_atihdmi),
  2858. HDA_CODEC_ENTRY(0x10951390, "SiI1390 HDMI", patch_generic_hdmi),
  2859. HDA_CODEC_ENTRY(0x10951392, "SiI1392 HDMI", patch_generic_hdmi),
  2860. HDA_CODEC_ENTRY(0x17e80047, "Chrontel HDMI", patch_generic_hdmi),
  2861. HDA_CODEC_ENTRY(0x10de0002, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
  2862. HDA_CODEC_ENTRY(0x10de0003, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
  2863. HDA_CODEC_ENTRY(0x10de0005, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
  2864. HDA_CODEC_ENTRY(0x10de0006, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
  2865. HDA_CODEC_ENTRY(0x10de0007, "MCP79/7A HDMI", patch_nvhdmi_8ch_7x),
  2866. HDA_CODEC_ENTRY(0x10de000a, "GPU 0a HDMI/DP", patch_nvhdmi),
  2867. HDA_CODEC_ENTRY(0x10de000b, "GPU 0b HDMI/DP", patch_nvhdmi),
  2868. HDA_CODEC_ENTRY(0x10de000c, "MCP89 HDMI", patch_nvhdmi),
  2869. HDA_CODEC_ENTRY(0x10de000d, "GPU 0d HDMI/DP", patch_nvhdmi),
  2870. HDA_CODEC_ENTRY(0x10de0010, "GPU 10 HDMI/DP", patch_nvhdmi),
  2871. HDA_CODEC_ENTRY(0x10de0011, "GPU 11 HDMI/DP", patch_nvhdmi),
  2872. HDA_CODEC_ENTRY(0x10de0012, "GPU 12 HDMI/DP", patch_nvhdmi),
  2873. HDA_CODEC_ENTRY(0x10de0013, "GPU 13 HDMI/DP", patch_nvhdmi),
  2874. HDA_CODEC_ENTRY(0x10de0014, "GPU 14 HDMI/DP", patch_nvhdmi),
  2875. HDA_CODEC_ENTRY(0x10de0015, "GPU 15 HDMI/DP", patch_nvhdmi),
  2876. HDA_CODEC_ENTRY(0x10de0016, "GPU 16 HDMI/DP", patch_nvhdmi),
  2877. /* 17 is known to be absent */
  2878. HDA_CODEC_ENTRY(0x10de0018, "GPU 18 HDMI/DP", patch_nvhdmi),
  2879. HDA_CODEC_ENTRY(0x10de0019, "GPU 19 HDMI/DP", patch_nvhdmi),
  2880. HDA_CODEC_ENTRY(0x10de001a, "GPU 1a HDMI/DP", patch_nvhdmi),
  2881. HDA_CODEC_ENTRY(0x10de001b, "GPU 1b HDMI/DP", patch_nvhdmi),
  2882. HDA_CODEC_ENTRY(0x10de001c, "GPU 1c HDMI/DP", patch_nvhdmi),
  2883. HDA_CODEC_ENTRY(0x10de0020, "Tegra30 HDMI", patch_tegra_hdmi),
  2884. HDA_CODEC_ENTRY(0x10de0022, "Tegra114 HDMI", patch_tegra_hdmi),
  2885. HDA_CODEC_ENTRY(0x10de0028, "Tegra124 HDMI", patch_tegra_hdmi),
  2886. HDA_CODEC_ENTRY(0x10de0029, "Tegra210 HDMI/DP", patch_tegra_hdmi),
  2887. HDA_CODEC_ENTRY(0x10de0040, "GPU 40 HDMI/DP", patch_nvhdmi),
  2888. HDA_CODEC_ENTRY(0x10de0041, "GPU 41 HDMI/DP", patch_nvhdmi),
  2889. HDA_CODEC_ENTRY(0x10de0042, "GPU 42 HDMI/DP", patch_nvhdmi),
  2890. HDA_CODEC_ENTRY(0x10de0043, "GPU 43 HDMI/DP", patch_nvhdmi),
  2891. HDA_CODEC_ENTRY(0x10de0044, "GPU 44 HDMI/DP", patch_nvhdmi),
  2892. HDA_CODEC_ENTRY(0x10de0051, "GPU 51 HDMI/DP", patch_nvhdmi),
  2893. HDA_CODEC_ENTRY(0x10de0060, "GPU 60 HDMI/DP", patch_nvhdmi),
  2894. HDA_CODEC_ENTRY(0x10de0067, "MCP67 HDMI", patch_nvhdmi_2ch),
  2895. HDA_CODEC_ENTRY(0x10de0070, "GPU 70 HDMI/DP", patch_nvhdmi),
  2896. HDA_CODEC_ENTRY(0x10de0071, "GPU 71 HDMI/DP", patch_nvhdmi),
  2897. HDA_CODEC_ENTRY(0x10de0072, "GPU 72 HDMI/DP", patch_nvhdmi),
  2898. HDA_CODEC_ENTRY(0x10de007d, "GPU 7d HDMI/DP", patch_nvhdmi),
  2899. HDA_CODEC_ENTRY(0x10de0083, "GPU 83 HDMI/DP", patch_nvhdmi),
  2900. HDA_CODEC_ENTRY(0x10de8001, "MCP73 HDMI", patch_nvhdmi_2ch),
  2901. HDA_CODEC_ENTRY(0x11069f80, "VX900 HDMI/DP", patch_via_hdmi),
  2902. HDA_CODEC_ENTRY(0x11069f81, "VX900 HDMI/DP", patch_via_hdmi),
  2903. HDA_CODEC_ENTRY(0x11069f84, "VX11 HDMI/DP", patch_generic_hdmi),
  2904. HDA_CODEC_ENTRY(0x11069f85, "VX11 HDMI/DP", patch_generic_hdmi),
  2905. HDA_CODEC_ENTRY(0x80860054, "IbexPeak HDMI", patch_generic_hdmi),
  2906. HDA_CODEC_ENTRY(0x80862801, "Bearlake HDMI", patch_generic_hdmi),
  2907. HDA_CODEC_ENTRY(0x80862802, "Cantiga HDMI", patch_generic_hdmi),
  2908. HDA_CODEC_ENTRY(0x80862803, "Eaglelake HDMI", patch_generic_hdmi),
  2909. HDA_CODEC_ENTRY(0x80862804, "IbexPeak HDMI", patch_generic_hdmi),
  2910. HDA_CODEC_ENTRY(0x80862805, "CougarPoint HDMI", patch_generic_hdmi),
  2911. HDA_CODEC_ENTRY(0x80862806, "PantherPoint HDMI", patch_generic_hdmi),
  2912. HDA_CODEC_ENTRY(0x80862807, "Haswell HDMI", patch_generic_hdmi),
  2913. HDA_CODEC_ENTRY(0x80862808, "Broadwell HDMI", patch_generic_hdmi),
  2914. HDA_CODEC_ENTRY(0x80862809, "Skylake HDMI", patch_generic_hdmi),
  2915. HDA_CODEC_ENTRY(0x8086280a, "Broxton HDMI", patch_generic_hdmi),
  2916. HDA_CODEC_ENTRY(0x8086280b, "Kabylake HDMI", patch_generic_hdmi),
  2917. HDA_CODEC_ENTRY(0x80862880, "CedarTrail HDMI", patch_generic_hdmi),
  2918. HDA_CODEC_ENTRY(0x80862882, "Valleyview2 HDMI", patch_generic_hdmi),
  2919. HDA_CODEC_ENTRY(0x80862883, "Braswell HDMI", patch_generic_hdmi),
  2920. HDA_CODEC_ENTRY(0x808629fb, "Crestline HDMI", patch_generic_hdmi),
  2921. /* special ID for generic HDMI */
  2922. HDA_CODEC_ENTRY(HDA_CODEC_ID_GENERIC_HDMI, "Generic HDMI", patch_generic_hdmi),
  2923. {} /* terminator */
  2924. };
  2925. MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_hdmi);
  2926. MODULE_LICENSE("GPL");
  2927. MODULE_DESCRIPTION("HDMI HD-audio codec");
  2928. MODULE_ALIAS("snd-hda-codec-intelhdmi");
  2929. MODULE_ALIAS("snd-hda-codec-nvhdmi");
  2930. MODULE_ALIAS("snd-hda-codec-atihdmi");
  2931. static struct hda_codec_driver hdmi_driver = {
  2932. .id = snd_hda_id_hdmi,
  2933. };
  2934. module_hda_codec_driver(hdmi_driver);