patch_hdmi.c 99 KB

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