patch_hdmi.c 102 KB

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