patch_hdmi.c 102 KB

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