patch_hdmi.c 100 KB

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