patch_hdmi.c 100 KB

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