patch_hdmi.c 96 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457
  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)->vendor_id == 0x80862807)
  46. #define is_broadwell(codec) ((codec)->vendor_id == 0x80862808)
  47. #define is_haswell_plus(codec) (is_haswell(codec) || is_broadwell(codec))
  48. #define is_valleyview(codec) ((codec)->vendor_id == 0x80862882)
  49. #define is_cherryview(codec) ((codec)->vendor_id == 0x80862883)
  50. #define is_valleyview_plus(codec) (is_valleyview(codec) || is_cherryview(codec))
  51. struct hdmi_spec_per_cvt {
  52. hda_nid_t cvt_nid;
  53. int assigned;
  54. unsigned int channels_min;
  55. unsigned int channels_max;
  56. u32 rates;
  57. u64 formats;
  58. unsigned int maxbps;
  59. };
  60. /* max. connections to a widget */
  61. #define HDA_MAX_CONNECTIONS 32
  62. struct hdmi_spec_per_pin {
  63. hda_nid_t pin_nid;
  64. int num_mux_nids;
  65. hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
  66. int mux_idx;
  67. hda_nid_t cvt_nid;
  68. struct hda_codec *codec;
  69. struct hdmi_eld sink_eld;
  70. struct mutex lock;
  71. struct delayed_work work;
  72. struct snd_kcontrol *eld_ctl;
  73. int repoll_count;
  74. bool setup; /* the stream has been set up by prepare callback */
  75. int channels; /* current number of channels */
  76. bool non_pcm;
  77. bool chmap_set; /* channel-map override by ALSA API? */
  78. unsigned char chmap[8]; /* ALSA API channel-map */
  79. char pcm_name[8]; /* filled in build_pcm callbacks */
  80. #ifdef CONFIG_PROC_FS
  81. struct snd_info_entry *proc_entry;
  82. #endif
  83. };
  84. struct cea_channel_speaker_allocation;
  85. /* operations used by generic code that can be overridden by patches */
  86. struct hdmi_ops {
  87. int (*pin_get_eld)(struct hda_codec *codec, hda_nid_t pin_nid,
  88. unsigned char *buf, int *eld_size);
  89. /* get and set channel assigned to each HDMI ASP (audio sample packet) slot */
  90. int (*pin_get_slot_channel)(struct hda_codec *codec, hda_nid_t pin_nid,
  91. int asp_slot);
  92. int (*pin_set_slot_channel)(struct hda_codec *codec, hda_nid_t pin_nid,
  93. int asp_slot, int channel);
  94. void (*pin_setup_infoframe)(struct hda_codec *codec, hda_nid_t pin_nid,
  95. int ca, int active_channels, int conn_type);
  96. /* enable/disable HBR (HD passthrough) */
  97. int (*pin_hbr_setup)(struct hda_codec *codec, hda_nid_t pin_nid, bool hbr);
  98. int (*setup_stream)(struct hda_codec *codec, hda_nid_t cvt_nid,
  99. hda_nid_t pin_nid, u32 stream_tag, int format);
  100. /* Helpers for producing the channel map TLVs. These can be overridden
  101. * for devices that have non-standard mapping requirements. */
  102. int (*chmap_cea_alloc_validate_get_type)(struct cea_channel_speaker_allocation *cap,
  103. int channels);
  104. void (*cea_alloc_to_tlv_chmap)(struct cea_channel_speaker_allocation *cap,
  105. unsigned int *chmap, int channels);
  106. /* check that the user-given chmap is supported */
  107. int (*chmap_validate)(int ca, int channels, unsigned char *chmap);
  108. };
  109. struct hdmi_spec {
  110. int num_cvts;
  111. struct snd_array cvts; /* struct hdmi_spec_per_cvt */
  112. hda_nid_t cvt_nids[4]; /* only for haswell fix */
  113. int num_pins;
  114. struct snd_array pins; /* struct hdmi_spec_per_pin */
  115. struct snd_array pcm_rec; /* struct hda_pcm */
  116. unsigned int channels_max; /* max over all cvts */
  117. struct hdmi_eld temp_eld;
  118. struct hdmi_ops ops;
  119. bool dyn_pin_out;
  120. /*
  121. * Non-generic VIA/NVIDIA specific
  122. */
  123. struct hda_multi_out multiout;
  124. struct hda_pcm_stream pcm_playback;
  125. };
  126. struct hdmi_audio_infoframe {
  127. u8 type; /* 0x84 */
  128. u8 ver; /* 0x01 */
  129. u8 len; /* 0x0a */
  130. u8 checksum;
  131. u8 CC02_CT47; /* CC in bits 0:2, CT in 4:7 */
  132. u8 SS01_SF24;
  133. u8 CXT04;
  134. u8 CA;
  135. u8 LFEPBL01_LSV36_DM_INH7;
  136. };
  137. struct dp_audio_infoframe {
  138. u8 type; /* 0x84 */
  139. u8 len; /* 0x1b */
  140. u8 ver; /* 0x11 << 2 */
  141. u8 CC02_CT47; /* match with HDMI infoframe from this on */
  142. u8 SS01_SF24;
  143. u8 CXT04;
  144. u8 CA;
  145. u8 LFEPBL01_LSV36_DM_INH7;
  146. };
  147. union audio_infoframe {
  148. struct hdmi_audio_infoframe hdmi;
  149. struct dp_audio_infoframe dp;
  150. u8 bytes[0];
  151. };
  152. /*
  153. * CEA speaker placement:
  154. *
  155. * FLH FCH FRH
  156. * FLW FL FLC FC FRC FR FRW
  157. *
  158. * LFE
  159. * TC
  160. *
  161. * RL RLC RC RRC RR
  162. *
  163. * The Left/Right Surround channel _notions_ LS/RS in SMPTE 320M corresponds to
  164. * CEA RL/RR; The SMPTE channel _assignment_ C/LFE is swapped to CEA LFE/FC.
  165. */
  166. enum cea_speaker_placement {
  167. FL = (1 << 0), /* Front Left */
  168. FC = (1 << 1), /* Front Center */
  169. FR = (1 << 2), /* Front Right */
  170. FLC = (1 << 3), /* Front Left Center */
  171. FRC = (1 << 4), /* Front Right Center */
  172. RL = (1 << 5), /* Rear Left */
  173. RC = (1 << 6), /* Rear Center */
  174. RR = (1 << 7), /* Rear Right */
  175. RLC = (1 << 8), /* Rear Left Center */
  176. RRC = (1 << 9), /* Rear Right Center */
  177. LFE = (1 << 10), /* Low Frequency Effect */
  178. FLW = (1 << 11), /* Front Left Wide */
  179. FRW = (1 << 12), /* Front Right Wide */
  180. FLH = (1 << 13), /* Front Left High */
  181. FCH = (1 << 14), /* Front Center High */
  182. FRH = (1 << 15), /* Front Right High */
  183. TC = (1 << 16), /* Top Center */
  184. };
  185. /*
  186. * ELD SA bits in the CEA Speaker Allocation data block
  187. */
  188. static int eld_speaker_allocation_bits[] = {
  189. [0] = FL | FR,
  190. [1] = LFE,
  191. [2] = FC,
  192. [3] = RL | RR,
  193. [4] = RC,
  194. [5] = FLC | FRC,
  195. [6] = RLC | RRC,
  196. /* the following are not defined in ELD yet */
  197. [7] = FLW | FRW,
  198. [8] = FLH | FRH,
  199. [9] = TC,
  200. [10] = FCH,
  201. };
  202. struct cea_channel_speaker_allocation {
  203. int ca_index;
  204. int speakers[8];
  205. /* derived values, just for convenience */
  206. int channels;
  207. int spk_mask;
  208. };
  209. /*
  210. * ALSA sequence is:
  211. *
  212. * surround40 surround41 surround50 surround51 surround71
  213. * ch0 front left = = = =
  214. * ch1 front right = = = =
  215. * ch2 rear left = = = =
  216. * ch3 rear right = = = =
  217. * ch4 LFE center center center
  218. * ch5 LFE LFE
  219. * ch6 side left
  220. * ch7 side right
  221. *
  222. * surround71 = {FL, FR, RLC, RRC, FC, LFE, RL, RR}
  223. */
  224. static int hdmi_channel_mapping[0x32][8] = {
  225. /* stereo */
  226. [0x00] = { 0x00, 0x11, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7 },
  227. /* 2.1 */
  228. [0x01] = { 0x00, 0x11, 0x22, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7 },
  229. /* Dolby Surround */
  230. [0x02] = { 0x00, 0x11, 0x23, 0xf2, 0xf4, 0xf5, 0xf6, 0xf7 },
  231. /* surround40 */
  232. [0x08] = { 0x00, 0x11, 0x24, 0x35, 0xf3, 0xf2, 0xf6, 0xf7 },
  233. /* 4ch */
  234. [0x03] = { 0x00, 0x11, 0x23, 0x32, 0x44, 0xf5, 0xf6, 0xf7 },
  235. /* surround41 */
  236. [0x09] = { 0x00, 0x11, 0x24, 0x35, 0x42, 0xf3, 0xf6, 0xf7 },
  237. /* surround50 */
  238. [0x0a] = { 0x00, 0x11, 0x24, 0x35, 0x43, 0xf2, 0xf6, 0xf7 },
  239. /* surround51 */
  240. [0x0b] = { 0x00, 0x11, 0x24, 0x35, 0x43, 0x52, 0xf6, 0xf7 },
  241. /* 7.1 */
  242. [0x13] = { 0x00, 0x11, 0x26, 0x37, 0x43, 0x52, 0x64, 0x75 },
  243. };
  244. /*
  245. * This is an ordered list!
  246. *
  247. * The preceding ones have better chances to be selected by
  248. * hdmi_channel_allocation().
  249. */
  250. static struct cea_channel_speaker_allocation channel_allocations[] = {
  251. /* channel: 7 6 5 4 3 2 1 0 */
  252. { .ca_index = 0x00, .speakers = { 0, 0, 0, 0, 0, 0, FR, FL } },
  253. /* 2.1 */
  254. { .ca_index = 0x01, .speakers = { 0, 0, 0, 0, 0, LFE, FR, FL } },
  255. /* Dolby Surround */
  256. { .ca_index = 0x02, .speakers = { 0, 0, 0, 0, FC, 0, FR, FL } },
  257. /* surround40 */
  258. { .ca_index = 0x08, .speakers = { 0, 0, RR, RL, 0, 0, FR, FL } },
  259. /* surround41 */
  260. { .ca_index = 0x09, .speakers = { 0, 0, RR, RL, 0, LFE, FR, FL } },
  261. /* surround50 */
  262. { .ca_index = 0x0a, .speakers = { 0, 0, RR, RL, FC, 0, FR, FL } },
  263. /* surround51 */
  264. { .ca_index = 0x0b, .speakers = { 0, 0, RR, RL, FC, LFE, FR, FL } },
  265. /* 6.1 */
  266. { .ca_index = 0x0f, .speakers = { 0, RC, RR, RL, FC, LFE, FR, FL } },
  267. /* surround71 */
  268. { .ca_index = 0x13, .speakers = { RRC, RLC, RR, RL, FC, LFE, FR, FL } },
  269. { .ca_index = 0x03, .speakers = { 0, 0, 0, 0, FC, LFE, FR, FL } },
  270. { .ca_index = 0x04, .speakers = { 0, 0, 0, RC, 0, 0, FR, FL } },
  271. { .ca_index = 0x05, .speakers = { 0, 0, 0, RC, 0, LFE, FR, FL } },
  272. { .ca_index = 0x06, .speakers = { 0, 0, 0, RC, FC, 0, FR, FL } },
  273. { .ca_index = 0x07, .speakers = { 0, 0, 0, RC, FC, LFE, FR, FL } },
  274. { .ca_index = 0x0c, .speakers = { 0, RC, RR, RL, 0, 0, FR, FL } },
  275. { .ca_index = 0x0d, .speakers = { 0, RC, RR, RL, 0, LFE, FR, FL } },
  276. { .ca_index = 0x0e, .speakers = { 0, RC, RR, RL, FC, 0, FR, FL } },
  277. { .ca_index = 0x10, .speakers = { RRC, RLC, RR, RL, 0, 0, FR, FL } },
  278. { .ca_index = 0x11, .speakers = { RRC, RLC, RR, RL, 0, LFE, FR, FL } },
  279. { .ca_index = 0x12, .speakers = { RRC, RLC, RR, RL, FC, 0, FR, FL } },
  280. { .ca_index = 0x14, .speakers = { FRC, FLC, 0, 0, 0, 0, FR, FL } },
  281. { .ca_index = 0x15, .speakers = { FRC, FLC, 0, 0, 0, LFE, FR, FL } },
  282. { .ca_index = 0x16, .speakers = { FRC, FLC, 0, 0, FC, 0, FR, FL } },
  283. { .ca_index = 0x17, .speakers = { FRC, FLC, 0, 0, FC, LFE, FR, FL } },
  284. { .ca_index = 0x18, .speakers = { FRC, FLC, 0, RC, 0, 0, FR, FL } },
  285. { .ca_index = 0x19, .speakers = { FRC, FLC, 0, RC, 0, LFE, FR, FL } },
  286. { .ca_index = 0x1a, .speakers = { FRC, FLC, 0, RC, FC, 0, FR, FL } },
  287. { .ca_index = 0x1b, .speakers = { FRC, FLC, 0, RC, FC, LFE, FR, FL } },
  288. { .ca_index = 0x1c, .speakers = { FRC, FLC, RR, RL, 0, 0, FR, FL } },
  289. { .ca_index = 0x1d, .speakers = { FRC, FLC, RR, RL, 0, LFE, FR, FL } },
  290. { .ca_index = 0x1e, .speakers = { FRC, FLC, RR, RL, FC, 0, FR, FL } },
  291. { .ca_index = 0x1f, .speakers = { FRC, FLC, RR, RL, FC, LFE, FR, FL } },
  292. { .ca_index = 0x20, .speakers = { 0, FCH, RR, RL, FC, 0, FR, FL } },
  293. { .ca_index = 0x21, .speakers = { 0, FCH, RR, RL, FC, LFE, FR, FL } },
  294. { .ca_index = 0x22, .speakers = { TC, 0, RR, RL, FC, 0, FR, FL } },
  295. { .ca_index = 0x23, .speakers = { TC, 0, RR, RL, FC, LFE, FR, FL } },
  296. { .ca_index = 0x24, .speakers = { FRH, FLH, RR, RL, 0, 0, FR, FL } },
  297. { .ca_index = 0x25, .speakers = { FRH, FLH, RR, RL, 0, LFE, FR, FL } },
  298. { .ca_index = 0x26, .speakers = { FRW, FLW, RR, RL, 0, 0, FR, FL } },
  299. { .ca_index = 0x27, .speakers = { FRW, FLW, RR, RL, 0, LFE, FR, FL } },
  300. { .ca_index = 0x28, .speakers = { TC, RC, RR, RL, FC, 0, FR, FL } },
  301. { .ca_index = 0x29, .speakers = { TC, RC, RR, RL, FC, LFE, FR, FL } },
  302. { .ca_index = 0x2a, .speakers = { FCH, RC, RR, RL, FC, 0, FR, FL } },
  303. { .ca_index = 0x2b, .speakers = { FCH, RC, RR, RL, FC, LFE, FR, FL } },
  304. { .ca_index = 0x2c, .speakers = { TC, FCH, RR, RL, FC, 0, FR, FL } },
  305. { .ca_index = 0x2d, .speakers = { TC, FCH, RR, RL, FC, LFE, FR, FL } },
  306. { .ca_index = 0x2e, .speakers = { FRH, FLH, RR, RL, FC, 0, FR, FL } },
  307. { .ca_index = 0x2f, .speakers = { FRH, FLH, RR, RL, FC, LFE, FR, FL } },
  308. { .ca_index = 0x30, .speakers = { FRW, FLW, RR, RL, FC, 0, FR, FL } },
  309. { .ca_index = 0x31, .speakers = { FRW, FLW, RR, RL, FC, LFE, FR, FL } },
  310. };
  311. /*
  312. * HDMI routines
  313. */
  314. #define get_pin(spec, idx) \
  315. ((struct hdmi_spec_per_pin *)snd_array_elem(&spec->pins, idx))
  316. #define get_cvt(spec, idx) \
  317. ((struct hdmi_spec_per_cvt *)snd_array_elem(&spec->cvts, idx))
  318. #define get_pcm_rec(spec, idx) \
  319. ((struct hda_pcm *)snd_array_elem(&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_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->bus->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 && per_pin->proc_entry) {
  510. snd_device_free(per_pin->codec->bus->card, 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, end_nid;
  1188. int cvt_idx, curr;
  1189. struct hdmi_spec_per_cvt *per_cvt;
  1190. /* configure all pins, including "no physical connection" ones */
  1191. end_nid = codec->start_nid + codec->num_nodes;
  1192. for (nid = codec->start_nid; nid < end_nid; nid++) {
  1193. unsigned int wid_caps = get_wcaps(codec, nid);
  1194. unsigned int wid_type = get_wcaps_type(wid_caps);
  1195. if (wid_type != AC_WID_PIN)
  1196. continue;
  1197. if (nid == pin_nid)
  1198. continue;
  1199. curr = snd_hda_codec_read(codec, nid, 0,
  1200. AC_VERB_GET_CONNECT_SEL, 0);
  1201. if (curr != mux_idx)
  1202. continue;
  1203. /* choose an unassigned converter. The conveters in the
  1204. * connection list are in the same order as in the codec.
  1205. */
  1206. for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
  1207. per_cvt = get_cvt(spec, cvt_idx);
  1208. if (!per_cvt->assigned) {
  1209. codec_dbg(codec,
  1210. "choose cvt %d for pin nid %d\n",
  1211. cvt_idx, nid);
  1212. snd_hda_codec_write_cache(codec, nid, 0,
  1213. AC_VERB_SET_CONNECT_SEL,
  1214. cvt_idx);
  1215. break;
  1216. }
  1217. }
  1218. }
  1219. }
  1220. /*
  1221. * HDA PCM callbacks
  1222. */
  1223. static int hdmi_pcm_open(struct hda_pcm_stream *hinfo,
  1224. struct hda_codec *codec,
  1225. struct snd_pcm_substream *substream)
  1226. {
  1227. struct hdmi_spec *spec = codec->spec;
  1228. struct snd_pcm_runtime *runtime = substream->runtime;
  1229. int pin_idx, cvt_idx, mux_idx = 0;
  1230. struct hdmi_spec_per_pin *per_pin;
  1231. struct hdmi_eld *eld;
  1232. struct hdmi_spec_per_cvt *per_cvt = NULL;
  1233. int err;
  1234. /* Validate hinfo */
  1235. pin_idx = hinfo_to_pin_index(codec, hinfo);
  1236. if (snd_BUG_ON(pin_idx < 0))
  1237. return -EINVAL;
  1238. per_pin = get_pin(spec, pin_idx);
  1239. eld = &per_pin->sink_eld;
  1240. err = hdmi_choose_cvt(codec, pin_idx, &cvt_idx, &mux_idx);
  1241. if (err < 0)
  1242. return err;
  1243. per_cvt = get_cvt(spec, cvt_idx);
  1244. /* Claim converter */
  1245. per_cvt->assigned = 1;
  1246. per_pin->cvt_nid = per_cvt->cvt_nid;
  1247. hinfo->nid = per_cvt->cvt_nid;
  1248. snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0,
  1249. AC_VERB_SET_CONNECT_SEL,
  1250. mux_idx);
  1251. /* configure unused pins to choose other converters */
  1252. if (is_haswell_plus(codec) || is_valleyview_plus(codec))
  1253. intel_not_share_assigned_cvt(codec, per_pin->pin_nid, mux_idx);
  1254. snd_hda_spdif_ctls_assign(codec, pin_idx, per_cvt->cvt_nid);
  1255. /* Initially set the converter's capabilities */
  1256. hinfo->channels_min = per_cvt->channels_min;
  1257. hinfo->channels_max = per_cvt->channels_max;
  1258. hinfo->rates = per_cvt->rates;
  1259. hinfo->formats = per_cvt->formats;
  1260. hinfo->maxbps = per_cvt->maxbps;
  1261. /* Restrict capabilities by ELD if this isn't disabled */
  1262. if (!static_hdmi_pcm && eld->eld_valid) {
  1263. snd_hdmi_eld_update_pcm_info(&eld->info, hinfo);
  1264. if (hinfo->channels_min > hinfo->channels_max ||
  1265. !hinfo->rates || !hinfo->formats) {
  1266. per_cvt->assigned = 0;
  1267. hinfo->nid = 0;
  1268. snd_hda_spdif_ctls_unassign(codec, pin_idx);
  1269. return -ENODEV;
  1270. }
  1271. }
  1272. /* Store the updated parameters */
  1273. runtime->hw.channels_min = hinfo->channels_min;
  1274. runtime->hw.channels_max = hinfo->channels_max;
  1275. runtime->hw.formats = hinfo->formats;
  1276. runtime->hw.rates = hinfo->rates;
  1277. snd_pcm_hw_constraint_step(substream->runtime, 0,
  1278. SNDRV_PCM_HW_PARAM_CHANNELS, 2);
  1279. return 0;
  1280. }
  1281. /*
  1282. * HDA/HDMI auto parsing
  1283. */
  1284. static int hdmi_read_pin_conn(struct hda_codec *codec, int pin_idx)
  1285. {
  1286. struct hdmi_spec *spec = codec->spec;
  1287. struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
  1288. hda_nid_t pin_nid = per_pin->pin_nid;
  1289. if (!(get_wcaps(codec, pin_nid) & AC_WCAP_CONN_LIST)) {
  1290. codec_warn(codec,
  1291. "HDMI: pin %d wcaps %#x does not support connection list\n",
  1292. pin_nid, get_wcaps(codec, pin_nid));
  1293. return -EINVAL;
  1294. }
  1295. per_pin->num_mux_nids = snd_hda_get_connections(codec, pin_nid,
  1296. per_pin->mux_nids,
  1297. HDA_MAX_CONNECTIONS);
  1298. return 0;
  1299. }
  1300. static bool hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll)
  1301. {
  1302. struct hda_jack_tbl *jack;
  1303. struct hda_codec *codec = per_pin->codec;
  1304. struct hdmi_spec *spec = codec->spec;
  1305. struct hdmi_eld *eld = &spec->temp_eld;
  1306. struct hdmi_eld *pin_eld = &per_pin->sink_eld;
  1307. hda_nid_t pin_nid = per_pin->pin_nid;
  1308. /*
  1309. * Always execute a GetPinSense verb here, even when called from
  1310. * hdmi_intrinsic_event; for some NVIDIA HW, the unsolicited
  1311. * response's PD bit is not the real PD value, but indicates that
  1312. * the real PD value changed. An older version of the HD-audio
  1313. * specification worked this way. Hence, we just ignore the data in
  1314. * the unsolicited response to avoid custom WARs.
  1315. */
  1316. int present;
  1317. bool update_eld = false;
  1318. bool eld_changed = false;
  1319. bool ret;
  1320. snd_hda_power_up(codec);
  1321. present = snd_hda_pin_sense(codec, pin_nid);
  1322. mutex_lock(&per_pin->lock);
  1323. pin_eld->monitor_present = !!(present & AC_PINSENSE_PRESENCE);
  1324. if (pin_eld->monitor_present)
  1325. eld->eld_valid = !!(present & AC_PINSENSE_ELDV);
  1326. else
  1327. eld->eld_valid = false;
  1328. codec_dbg(codec,
  1329. "HDMI status: Codec=%d Pin=%d Presence_Detect=%d ELD_Valid=%d\n",
  1330. codec->addr, pin_nid, pin_eld->monitor_present, eld->eld_valid);
  1331. if (eld->eld_valid) {
  1332. if (spec->ops.pin_get_eld(codec, pin_nid, eld->eld_buffer,
  1333. &eld->eld_size) < 0)
  1334. eld->eld_valid = false;
  1335. else {
  1336. memset(&eld->info, 0, sizeof(struct parsed_hdmi_eld));
  1337. if (snd_hdmi_parse_eld(codec, &eld->info, eld->eld_buffer,
  1338. eld->eld_size) < 0)
  1339. eld->eld_valid = false;
  1340. }
  1341. if (eld->eld_valid) {
  1342. snd_hdmi_show_eld(codec, &eld->info);
  1343. update_eld = true;
  1344. }
  1345. else if (repoll) {
  1346. queue_delayed_work(codec->bus->workq,
  1347. &per_pin->work,
  1348. msecs_to_jiffies(300));
  1349. goto unlock;
  1350. }
  1351. }
  1352. if (pin_eld->eld_valid != eld->eld_valid)
  1353. eld_changed = true;
  1354. if (pin_eld->eld_valid && !eld->eld_valid)
  1355. update_eld = true;
  1356. if (update_eld) {
  1357. bool old_eld_valid = pin_eld->eld_valid;
  1358. pin_eld->eld_valid = eld->eld_valid;
  1359. if (pin_eld->eld_size != eld->eld_size ||
  1360. memcmp(pin_eld->eld_buffer, eld->eld_buffer,
  1361. eld->eld_size) != 0) {
  1362. memcpy(pin_eld->eld_buffer, eld->eld_buffer,
  1363. eld->eld_size);
  1364. eld_changed = true;
  1365. }
  1366. pin_eld->eld_size = eld->eld_size;
  1367. pin_eld->info = eld->info;
  1368. /*
  1369. * Re-setup pin and infoframe. This is needed e.g. when
  1370. * - sink is first plugged-in (infoframe is not set up if !monitor_present)
  1371. * - transcoder can change during stream playback on Haswell
  1372. * and this can make HW reset converter selection on a pin.
  1373. */
  1374. if (eld->eld_valid && !old_eld_valid && per_pin->setup) {
  1375. if (is_haswell_plus(codec) ||
  1376. is_valleyview_plus(codec)) {
  1377. intel_verify_pin_cvt_connect(codec, per_pin);
  1378. intel_not_share_assigned_cvt(codec, pin_nid,
  1379. per_pin->mux_idx);
  1380. }
  1381. hdmi_setup_audio_infoframe(codec, per_pin,
  1382. per_pin->non_pcm);
  1383. }
  1384. }
  1385. if (eld_changed)
  1386. snd_ctl_notify(codec->bus->card,
  1387. SNDRV_CTL_EVENT_MASK_VALUE | SNDRV_CTL_EVENT_MASK_INFO,
  1388. &per_pin->eld_ctl->id);
  1389. unlock:
  1390. ret = !repoll || !pin_eld->monitor_present || pin_eld->eld_valid;
  1391. jack = snd_hda_jack_tbl_get(codec, pin_nid);
  1392. if (jack)
  1393. jack->block_report = !ret;
  1394. mutex_unlock(&per_pin->lock);
  1395. snd_hda_power_down(codec);
  1396. return ret;
  1397. }
  1398. static void hdmi_repoll_eld(struct work_struct *work)
  1399. {
  1400. struct hdmi_spec_per_pin *per_pin =
  1401. container_of(to_delayed_work(work), struct hdmi_spec_per_pin, work);
  1402. if (per_pin->repoll_count++ > 6)
  1403. per_pin->repoll_count = 0;
  1404. if (hdmi_present_sense(per_pin, per_pin->repoll_count))
  1405. snd_hda_jack_report_sync(per_pin->codec);
  1406. }
  1407. static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
  1408. hda_nid_t nid);
  1409. static int hdmi_add_pin(struct hda_codec *codec, hda_nid_t pin_nid)
  1410. {
  1411. struct hdmi_spec *spec = codec->spec;
  1412. unsigned int caps, config;
  1413. int pin_idx;
  1414. struct hdmi_spec_per_pin *per_pin;
  1415. int err;
  1416. caps = snd_hda_query_pin_caps(codec, pin_nid);
  1417. if (!(caps & (AC_PINCAP_HDMI | AC_PINCAP_DP)))
  1418. return 0;
  1419. config = snd_hda_codec_get_pincfg(codec, pin_nid);
  1420. if (get_defcfg_connect(config) == AC_JACK_PORT_NONE)
  1421. return 0;
  1422. if (is_haswell_plus(codec))
  1423. intel_haswell_fixup_connect_list(codec, pin_nid);
  1424. pin_idx = spec->num_pins;
  1425. per_pin = snd_array_new(&spec->pins);
  1426. if (!per_pin)
  1427. return -ENOMEM;
  1428. per_pin->pin_nid = pin_nid;
  1429. per_pin->non_pcm = false;
  1430. err = hdmi_read_pin_conn(codec, pin_idx);
  1431. if (err < 0)
  1432. return err;
  1433. spec->num_pins++;
  1434. return 0;
  1435. }
  1436. static int hdmi_add_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
  1437. {
  1438. struct hdmi_spec *spec = codec->spec;
  1439. struct hdmi_spec_per_cvt *per_cvt;
  1440. unsigned int chans;
  1441. int err;
  1442. chans = get_wcaps(codec, cvt_nid);
  1443. chans = get_wcaps_channels(chans);
  1444. per_cvt = snd_array_new(&spec->cvts);
  1445. if (!per_cvt)
  1446. return -ENOMEM;
  1447. per_cvt->cvt_nid = cvt_nid;
  1448. per_cvt->channels_min = 2;
  1449. if (chans <= 16) {
  1450. per_cvt->channels_max = chans;
  1451. if (chans > spec->channels_max)
  1452. spec->channels_max = chans;
  1453. }
  1454. err = snd_hda_query_supported_pcm(codec, cvt_nid,
  1455. &per_cvt->rates,
  1456. &per_cvt->formats,
  1457. &per_cvt->maxbps);
  1458. if (err < 0)
  1459. return err;
  1460. if (spec->num_cvts < ARRAY_SIZE(spec->cvt_nids))
  1461. spec->cvt_nids[spec->num_cvts] = cvt_nid;
  1462. spec->num_cvts++;
  1463. return 0;
  1464. }
  1465. static int hdmi_parse_codec(struct hda_codec *codec)
  1466. {
  1467. hda_nid_t nid;
  1468. int i, nodes;
  1469. nodes = snd_hda_get_sub_nodes(codec, codec->afg, &nid);
  1470. if (!nid || nodes < 0) {
  1471. codec_warn(codec, "HDMI: failed to get afg sub nodes\n");
  1472. return -EINVAL;
  1473. }
  1474. for (i = 0; i < nodes; i++, nid++) {
  1475. unsigned int caps;
  1476. unsigned int type;
  1477. caps = get_wcaps(codec, nid);
  1478. type = get_wcaps_type(caps);
  1479. if (!(caps & AC_WCAP_DIGITAL))
  1480. continue;
  1481. switch (type) {
  1482. case AC_WID_AUD_OUT:
  1483. hdmi_add_cvt(codec, nid);
  1484. break;
  1485. case AC_WID_PIN:
  1486. hdmi_add_pin(codec, nid);
  1487. break;
  1488. }
  1489. }
  1490. return 0;
  1491. }
  1492. /*
  1493. */
  1494. static bool check_non_pcm_per_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
  1495. {
  1496. struct hda_spdif_out *spdif;
  1497. bool non_pcm;
  1498. mutex_lock(&codec->spdif_mutex);
  1499. spdif = snd_hda_spdif_out_of_nid(codec, cvt_nid);
  1500. non_pcm = !!(spdif->status & IEC958_AES0_NONAUDIO);
  1501. mutex_unlock(&codec->spdif_mutex);
  1502. return non_pcm;
  1503. }
  1504. /*
  1505. * HDMI callbacks
  1506. */
  1507. static int generic_hdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
  1508. struct hda_codec *codec,
  1509. unsigned int stream_tag,
  1510. unsigned int format,
  1511. struct snd_pcm_substream *substream)
  1512. {
  1513. hda_nid_t cvt_nid = hinfo->nid;
  1514. struct hdmi_spec *spec = codec->spec;
  1515. int pin_idx = hinfo_to_pin_index(codec, hinfo);
  1516. struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
  1517. hda_nid_t pin_nid = per_pin->pin_nid;
  1518. bool non_pcm;
  1519. int pinctl;
  1520. if (is_haswell_plus(codec) || is_valleyview_plus(codec)) {
  1521. /* Verify pin:cvt selections to avoid silent audio after S3.
  1522. * After S3, the audio driver restores pin:cvt selections
  1523. * but this can happen before gfx is ready and such selection
  1524. * is overlooked by HW. Thus multiple pins can share a same
  1525. * default convertor and mute control will affect each other,
  1526. * which can cause a resumed audio playback become silent
  1527. * after S3.
  1528. */
  1529. intel_verify_pin_cvt_connect(codec, per_pin);
  1530. intel_not_share_assigned_cvt(codec, pin_nid, per_pin->mux_idx);
  1531. }
  1532. non_pcm = check_non_pcm_per_cvt(codec, cvt_nid);
  1533. mutex_lock(&per_pin->lock);
  1534. per_pin->channels = substream->runtime->channels;
  1535. per_pin->setup = true;
  1536. hdmi_setup_audio_infoframe(codec, per_pin, non_pcm);
  1537. mutex_unlock(&per_pin->lock);
  1538. if (spec->dyn_pin_out) {
  1539. pinctl = snd_hda_codec_read(codec, pin_nid, 0,
  1540. AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
  1541. snd_hda_codec_write(codec, pin_nid, 0,
  1542. AC_VERB_SET_PIN_WIDGET_CONTROL,
  1543. pinctl | PIN_OUT);
  1544. }
  1545. return spec->ops.setup_stream(codec, cvt_nid, pin_nid, stream_tag, format);
  1546. }
  1547. static int generic_hdmi_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
  1548. struct hda_codec *codec,
  1549. struct snd_pcm_substream *substream)
  1550. {
  1551. snd_hda_codec_cleanup_stream(codec, hinfo->nid);
  1552. return 0;
  1553. }
  1554. static int hdmi_pcm_close(struct hda_pcm_stream *hinfo,
  1555. struct hda_codec *codec,
  1556. struct snd_pcm_substream *substream)
  1557. {
  1558. struct hdmi_spec *spec = codec->spec;
  1559. int cvt_idx, pin_idx;
  1560. struct hdmi_spec_per_cvt *per_cvt;
  1561. struct hdmi_spec_per_pin *per_pin;
  1562. int pinctl;
  1563. if (hinfo->nid) {
  1564. cvt_idx = cvt_nid_to_cvt_index(codec, hinfo->nid);
  1565. if (snd_BUG_ON(cvt_idx < 0))
  1566. return -EINVAL;
  1567. per_cvt = get_cvt(spec, cvt_idx);
  1568. snd_BUG_ON(!per_cvt->assigned);
  1569. per_cvt->assigned = 0;
  1570. hinfo->nid = 0;
  1571. pin_idx = hinfo_to_pin_index(codec, hinfo);
  1572. if (snd_BUG_ON(pin_idx < 0))
  1573. return -EINVAL;
  1574. per_pin = get_pin(spec, pin_idx);
  1575. if (spec->dyn_pin_out) {
  1576. pinctl = snd_hda_codec_read(codec, per_pin->pin_nid, 0,
  1577. AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
  1578. snd_hda_codec_write(codec, per_pin->pin_nid, 0,
  1579. AC_VERB_SET_PIN_WIDGET_CONTROL,
  1580. pinctl & ~PIN_OUT);
  1581. }
  1582. snd_hda_spdif_ctls_unassign(codec, pin_idx);
  1583. mutex_lock(&per_pin->lock);
  1584. per_pin->chmap_set = false;
  1585. memset(per_pin->chmap, 0, sizeof(per_pin->chmap));
  1586. per_pin->setup = false;
  1587. per_pin->channels = 0;
  1588. mutex_unlock(&per_pin->lock);
  1589. }
  1590. return 0;
  1591. }
  1592. static const struct hda_pcm_ops generic_ops = {
  1593. .open = hdmi_pcm_open,
  1594. .close = hdmi_pcm_close,
  1595. .prepare = generic_hdmi_playback_pcm_prepare,
  1596. .cleanup = generic_hdmi_playback_pcm_cleanup,
  1597. };
  1598. /*
  1599. * ALSA API channel-map control callbacks
  1600. */
  1601. static int hdmi_chmap_ctl_info(struct snd_kcontrol *kcontrol,
  1602. struct snd_ctl_elem_info *uinfo)
  1603. {
  1604. struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
  1605. struct hda_codec *codec = info->private_data;
  1606. struct hdmi_spec *spec = codec->spec;
  1607. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1608. uinfo->count = spec->channels_max;
  1609. uinfo->value.integer.min = 0;
  1610. uinfo->value.integer.max = SNDRV_CHMAP_LAST;
  1611. return 0;
  1612. }
  1613. static int hdmi_chmap_cea_alloc_validate_get_type(struct cea_channel_speaker_allocation *cap,
  1614. int channels)
  1615. {
  1616. /* If the speaker allocation matches the channel count, it is OK.*/
  1617. if (cap->channels != channels)
  1618. return -1;
  1619. /* all channels are remappable freely */
  1620. return SNDRV_CTL_TLVT_CHMAP_VAR;
  1621. }
  1622. static void hdmi_cea_alloc_to_tlv_chmap(struct cea_channel_speaker_allocation *cap,
  1623. unsigned int *chmap, int channels)
  1624. {
  1625. int count = 0;
  1626. int c;
  1627. for (c = 7; c >= 0; c--) {
  1628. int spk = cap->speakers[c];
  1629. if (!spk)
  1630. continue;
  1631. chmap[count++] = spk_to_chmap(spk);
  1632. }
  1633. WARN_ON(count != channels);
  1634. }
  1635. static int hdmi_chmap_ctl_tlv(struct snd_kcontrol *kcontrol, int op_flag,
  1636. unsigned int size, unsigned int __user *tlv)
  1637. {
  1638. struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
  1639. struct hda_codec *codec = info->private_data;
  1640. struct hdmi_spec *spec = codec->spec;
  1641. unsigned int __user *dst;
  1642. int chs, count = 0;
  1643. if (size < 8)
  1644. return -ENOMEM;
  1645. if (put_user(SNDRV_CTL_TLVT_CONTAINER, tlv))
  1646. return -EFAULT;
  1647. size -= 8;
  1648. dst = tlv + 2;
  1649. for (chs = 2; chs <= spec->channels_max; chs++) {
  1650. int i;
  1651. struct cea_channel_speaker_allocation *cap;
  1652. cap = channel_allocations;
  1653. for (i = 0; i < ARRAY_SIZE(channel_allocations); i++, cap++) {
  1654. int chs_bytes = chs * 4;
  1655. int type = spec->ops.chmap_cea_alloc_validate_get_type(cap, chs);
  1656. unsigned int tlv_chmap[8];
  1657. if (type < 0)
  1658. continue;
  1659. if (size < 8)
  1660. return -ENOMEM;
  1661. if (put_user(type, dst) ||
  1662. put_user(chs_bytes, dst + 1))
  1663. return -EFAULT;
  1664. dst += 2;
  1665. size -= 8;
  1666. count += 8;
  1667. if (size < chs_bytes)
  1668. return -ENOMEM;
  1669. size -= chs_bytes;
  1670. count += chs_bytes;
  1671. spec->ops.cea_alloc_to_tlv_chmap(cap, tlv_chmap, chs);
  1672. if (copy_to_user(dst, tlv_chmap, chs_bytes))
  1673. return -EFAULT;
  1674. dst += chs;
  1675. }
  1676. }
  1677. if (put_user(count, tlv + 1))
  1678. return -EFAULT;
  1679. return 0;
  1680. }
  1681. static int hdmi_chmap_ctl_get(struct snd_kcontrol *kcontrol,
  1682. struct snd_ctl_elem_value *ucontrol)
  1683. {
  1684. struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
  1685. struct hda_codec *codec = info->private_data;
  1686. struct hdmi_spec *spec = codec->spec;
  1687. int pin_idx = kcontrol->private_value;
  1688. struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
  1689. int i;
  1690. for (i = 0; i < ARRAY_SIZE(per_pin->chmap); i++)
  1691. ucontrol->value.integer.value[i] = per_pin->chmap[i];
  1692. return 0;
  1693. }
  1694. static int hdmi_chmap_ctl_put(struct snd_kcontrol *kcontrol,
  1695. struct snd_ctl_elem_value *ucontrol)
  1696. {
  1697. struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
  1698. struct hda_codec *codec = info->private_data;
  1699. struct hdmi_spec *spec = codec->spec;
  1700. int pin_idx = kcontrol->private_value;
  1701. struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
  1702. unsigned int ctl_idx;
  1703. struct snd_pcm_substream *substream;
  1704. unsigned char chmap[8];
  1705. int i, err, ca, prepared = 0;
  1706. ctl_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
  1707. substream = snd_pcm_chmap_substream(info, ctl_idx);
  1708. if (!substream || !substream->runtime)
  1709. return 0; /* just for avoiding error from alsactl restore */
  1710. switch (substream->runtime->status->state) {
  1711. case SNDRV_PCM_STATE_OPEN:
  1712. case SNDRV_PCM_STATE_SETUP:
  1713. break;
  1714. case SNDRV_PCM_STATE_PREPARED:
  1715. prepared = 1;
  1716. break;
  1717. default:
  1718. return -EBUSY;
  1719. }
  1720. memset(chmap, 0, sizeof(chmap));
  1721. for (i = 0; i < ARRAY_SIZE(chmap); i++)
  1722. chmap[i] = ucontrol->value.integer.value[i];
  1723. if (!memcmp(chmap, per_pin->chmap, sizeof(chmap)))
  1724. return 0;
  1725. ca = hdmi_manual_channel_allocation(ARRAY_SIZE(chmap), chmap);
  1726. if (ca < 0)
  1727. return -EINVAL;
  1728. if (spec->ops.chmap_validate) {
  1729. err = spec->ops.chmap_validate(ca, ARRAY_SIZE(chmap), chmap);
  1730. if (err)
  1731. return err;
  1732. }
  1733. mutex_lock(&per_pin->lock);
  1734. per_pin->chmap_set = true;
  1735. memcpy(per_pin->chmap, chmap, sizeof(chmap));
  1736. if (prepared)
  1737. hdmi_setup_audio_infoframe(codec, per_pin, per_pin->non_pcm);
  1738. mutex_unlock(&per_pin->lock);
  1739. return 0;
  1740. }
  1741. static int generic_hdmi_build_pcms(struct hda_codec *codec)
  1742. {
  1743. struct hdmi_spec *spec = codec->spec;
  1744. int pin_idx;
  1745. for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
  1746. struct hda_pcm *info;
  1747. struct hda_pcm_stream *pstr;
  1748. struct hdmi_spec_per_pin *per_pin;
  1749. per_pin = get_pin(spec, pin_idx);
  1750. sprintf(per_pin->pcm_name, "HDMI %d", pin_idx);
  1751. info = snd_array_new(&spec->pcm_rec);
  1752. if (!info)
  1753. return -ENOMEM;
  1754. info->name = per_pin->pcm_name;
  1755. info->pcm_type = HDA_PCM_TYPE_HDMI;
  1756. info->own_chmap = true;
  1757. pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
  1758. pstr->substreams = 1;
  1759. pstr->ops = generic_ops;
  1760. /* other pstr fields are set in open */
  1761. }
  1762. codec->num_pcms = spec->num_pins;
  1763. codec->pcm_info = spec->pcm_rec.list;
  1764. return 0;
  1765. }
  1766. static int generic_hdmi_build_jack(struct hda_codec *codec, int pin_idx)
  1767. {
  1768. char hdmi_str[32] = "HDMI/DP";
  1769. struct hdmi_spec *spec = codec->spec;
  1770. struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
  1771. int pcmdev = get_pcm_rec(spec, pin_idx)->device;
  1772. if (pcmdev > 0)
  1773. sprintf(hdmi_str + strlen(hdmi_str), ",pcm=%d", pcmdev);
  1774. if (!is_jack_detectable(codec, per_pin->pin_nid))
  1775. strncat(hdmi_str, " Phantom",
  1776. sizeof(hdmi_str) - strlen(hdmi_str) - 1);
  1777. return snd_hda_jack_add_kctl(codec, per_pin->pin_nid, hdmi_str, 0);
  1778. }
  1779. static int generic_hdmi_build_controls(struct hda_codec *codec)
  1780. {
  1781. struct hdmi_spec *spec = codec->spec;
  1782. int err;
  1783. int pin_idx;
  1784. for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
  1785. struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
  1786. err = generic_hdmi_build_jack(codec, pin_idx);
  1787. if (err < 0)
  1788. return err;
  1789. err = snd_hda_create_dig_out_ctls(codec,
  1790. per_pin->pin_nid,
  1791. per_pin->mux_nids[0],
  1792. HDA_PCM_TYPE_HDMI);
  1793. if (err < 0)
  1794. return err;
  1795. snd_hda_spdif_ctls_unassign(codec, pin_idx);
  1796. /* add control for ELD Bytes */
  1797. err = hdmi_create_eld_ctl(codec, pin_idx,
  1798. get_pcm_rec(spec, pin_idx)->device);
  1799. if (err < 0)
  1800. return err;
  1801. hdmi_present_sense(per_pin, 0);
  1802. }
  1803. /* add channel maps */
  1804. for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
  1805. struct snd_pcm_chmap *chmap;
  1806. struct snd_kcontrol *kctl;
  1807. int i;
  1808. if (!codec->pcm_info[pin_idx].pcm)
  1809. break;
  1810. err = snd_pcm_add_chmap_ctls(codec->pcm_info[pin_idx].pcm,
  1811. SNDRV_PCM_STREAM_PLAYBACK,
  1812. NULL, 0, pin_idx, &chmap);
  1813. if (err < 0)
  1814. return err;
  1815. /* override handlers */
  1816. chmap->private_data = codec;
  1817. kctl = chmap->kctl;
  1818. for (i = 0; i < kctl->count; i++)
  1819. kctl->vd[i].access |= SNDRV_CTL_ELEM_ACCESS_WRITE;
  1820. kctl->info = hdmi_chmap_ctl_info;
  1821. kctl->get = hdmi_chmap_ctl_get;
  1822. kctl->put = hdmi_chmap_ctl_put;
  1823. kctl->tlv.c = hdmi_chmap_ctl_tlv;
  1824. }
  1825. return 0;
  1826. }
  1827. static int generic_hdmi_init_per_pins(struct hda_codec *codec)
  1828. {
  1829. struct hdmi_spec *spec = codec->spec;
  1830. int pin_idx;
  1831. for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
  1832. struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
  1833. per_pin->codec = codec;
  1834. mutex_init(&per_pin->lock);
  1835. INIT_DELAYED_WORK(&per_pin->work, hdmi_repoll_eld);
  1836. eld_proc_new(per_pin, pin_idx);
  1837. }
  1838. return 0;
  1839. }
  1840. static int generic_hdmi_init(struct hda_codec *codec)
  1841. {
  1842. struct hdmi_spec *spec = codec->spec;
  1843. int pin_idx;
  1844. for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
  1845. struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
  1846. hda_nid_t pin_nid = per_pin->pin_nid;
  1847. hdmi_init_pin(codec, pin_nid);
  1848. snd_hda_jack_detect_enable_callback(codec, pin_nid,
  1849. codec->jackpoll_interval > 0 ? jack_callback : NULL);
  1850. }
  1851. return 0;
  1852. }
  1853. static void hdmi_array_init(struct hdmi_spec *spec, int nums)
  1854. {
  1855. snd_array_init(&spec->pins, sizeof(struct hdmi_spec_per_pin), nums);
  1856. snd_array_init(&spec->cvts, sizeof(struct hdmi_spec_per_cvt), nums);
  1857. snd_array_init(&spec->pcm_rec, sizeof(struct hda_pcm), nums);
  1858. }
  1859. static void hdmi_array_free(struct hdmi_spec *spec)
  1860. {
  1861. snd_array_free(&spec->pins);
  1862. snd_array_free(&spec->cvts);
  1863. snd_array_free(&spec->pcm_rec);
  1864. }
  1865. static void generic_hdmi_free(struct hda_codec *codec)
  1866. {
  1867. struct hdmi_spec *spec = codec->spec;
  1868. int pin_idx;
  1869. for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
  1870. struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
  1871. cancel_delayed_work(&per_pin->work);
  1872. eld_proc_free(per_pin);
  1873. }
  1874. flush_workqueue(codec->bus->workq);
  1875. hdmi_array_free(spec);
  1876. kfree(spec);
  1877. }
  1878. #ifdef CONFIG_PM
  1879. static int generic_hdmi_resume(struct hda_codec *codec)
  1880. {
  1881. struct hdmi_spec *spec = codec->spec;
  1882. int pin_idx;
  1883. codec->patch_ops.init(codec);
  1884. snd_hda_codec_resume_amp(codec);
  1885. snd_hda_codec_resume_cache(codec);
  1886. for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
  1887. struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
  1888. hdmi_present_sense(per_pin, 1);
  1889. }
  1890. return 0;
  1891. }
  1892. #endif
  1893. static const struct hda_codec_ops generic_hdmi_patch_ops = {
  1894. .init = generic_hdmi_init,
  1895. .free = generic_hdmi_free,
  1896. .build_pcms = generic_hdmi_build_pcms,
  1897. .build_controls = generic_hdmi_build_controls,
  1898. .unsol_event = hdmi_unsol_event,
  1899. #ifdef CONFIG_PM
  1900. .resume = generic_hdmi_resume,
  1901. #endif
  1902. };
  1903. static const struct hdmi_ops generic_standard_hdmi_ops = {
  1904. .pin_get_eld = snd_hdmi_get_eld,
  1905. .pin_get_slot_channel = hdmi_pin_get_slot_channel,
  1906. .pin_set_slot_channel = hdmi_pin_set_slot_channel,
  1907. .pin_setup_infoframe = hdmi_pin_setup_infoframe,
  1908. .pin_hbr_setup = hdmi_pin_hbr_setup,
  1909. .setup_stream = hdmi_setup_stream,
  1910. .chmap_cea_alloc_validate_get_type = hdmi_chmap_cea_alloc_validate_get_type,
  1911. .cea_alloc_to_tlv_chmap = hdmi_cea_alloc_to_tlv_chmap,
  1912. };
  1913. static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
  1914. hda_nid_t nid)
  1915. {
  1916. struct hdmi_spec *spec = codec->spec;
  1917. hda_nid_t conns[4];
  1918. int nconns;
  1919. nconns = snd_hda_get_connections(codec, nid, conns, ARRAY_SIZE(conns));
  1920. if (nconns == spec->num_cvts &&
  1921. !memcmp(conns, spec->cvt_nids, spec->num_cvts * sizeof(hda_nid_t)))
  1922. return;
  1923. /* override pins connection list */
  1924. codec_dbg(codec, "hdmi: haswell: override pin connection 0x%x\n", nid);
  1925. snd_hda_override_conn_list(codec, nid, spec->num_cvts, spec->cvt_nids);
  1926. }
  1927. #define INTEL_VENDOR_NID 0x08
  1928. #define INTEL_GET_VENDOR_VERB 0xf81
  1929. #define INTEL_SET_VENDOR_VERB 0x781
  1930. #define INTEL_EN_DP12 0x02 /* enable DP 1.2 features */
  1931. #define INTEL_EN_ALL_PIN_CVTS 0x01 /* enable 2nd & 3rd pins and convertors */
  1932. static void intel_haswell_enable_all_pins(struct hda_codec *codec,
  1933. bool update_tree)
  1934. {
  1935. unsigned int vendor_param;
  1936. vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
  1937. INTEL_GET_VENDOR_VERB, 0);
  1938. if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
  1939. return;
  1940. vendor_param |= INTEL_EN_ALL_PIN_CVTS;
  1941. vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
  1942. INTEL_SET_VENDOR_VERB, vendor_param);
  1943. if (vendor_param == -1)
  1944. return;
  1945. if (update_tree)
  1946. snd_hda_codec_update_widgets(codec);
  1947. }
  1948. static void intel_haswell_fixup_enable_dp12(struct hda_codec *codec)
  1949. {
  1950. unsigned int vendor_param;
  1951. vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
  1952. INTEL_GET_VENDOR_VERB, 0);
  1953. if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
  1954. return;
  1955. /* enable DP1.2 mode */
  1956. vendor_param |= INTEL_EN_DP12;
  1957. snd_hda_codec_write_cache(codec, INTEL_VENDOR_NID, 0,
  1958. INTEL_SET_VENDOR_VERB, vendor_param);
  1959. }
  1960. /* Haswell needs to re-issue the vendor-specific verbs before turning to D0.
  1961. * Otherwise you may get severe h/w communication errors.
  1962. */
  1963. static void haswell_set_power_state(struct hda_codec *codec, hda_nid_t fg,
  1964. unsigned int power_state)
  1965. {
  1966. if (power_state == AC_PWRST_D0) {
  1967. intel_haswell_enable_all_pins(codec, false);
  1968. intel_haswell_fixup_enable_dp12(codec);
  1969. }
  1970. snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE, power_state);
  1971. snd_hda_codec_set_power_to_all(codec, fg, power_state);
  1972. }
  1973. static int patch_generic_hdmi(struct hda_codec *codec)
  1974. {
  1975. struct hdmi_spec *spec;
  1976. spec = kzalloc(sizeof(*spec), GFP_KERNEL);
  1977. if (spec == NULL)
  1978. return -ENOMEM;
  1979. spec->ops = generic_standard_hdmi_ops;
  1980. codec->spec = spec;
  1981. hdmi_array_init(spec, 4);
  1982. if (is_haswell_plus(codec)) {
  1983. intel_haswell_enable_all_pins(codec, true);
  1984. intel_haswell_fixup_enable_dp12(codec);
  1985. }
  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. generic_hdmi_init_per_pins(codec);
  1999. init_channel_allocations();
  2000. return 0;
  2001. }
  2002. /*
  2003. * Shared non-generic implementations
  2004. */
  2005. static int simple_playback_build_pcms(struct hda_codec *codec)
  2006. {
  2007. struct hdmi_spec *spec = codec->spec;
  2008. struct hda_pcm *info;
  2009. unsigned int chans;
  2010. struct hda_pcm_stream *pstr;
  2011. struct hdmi_spec_per_cvt *per_cvt;
  2012. per_cvt = get_cvt(spec, 0);
  2013. chans = get_wcaps(codec, per_cvt->cvt_nid);
  2014. chans = get_wcaps_channels(chans);
  2015. info = snd_array_new(&spec->pcm_rec);
  2016. if (!info)
  2017. return -ENOMEM;
  2018. info->name = get_pin(spec, 0)->pcm_name;
  2019. sprintf(info->name, "HDMI 0");
  2020. info->pcm_type = HDA_PCM_TYPE_HDMI;
  2021. pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
  2022. *pstr = spec->pcm_playback;
  2023. pstr->nid = per_cvt->cvt_nid;
  2024. if (pstr->channels_max <= 2 && chans && chans <= 16)
  2025. pstr->channels_max = chans;
  2026. codec->num_pcms = 1;
  2027. codec->pcm_info = info;
  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. * ATI/AMD-specific implementations
  2494. */
  2495. #define is_amdhdmi_rev3_or_later(codec) \
  2496. ((codec)->vendor_id == 0x1002aa01 && ((codec)->revision_id & 0xff00) >= 0x0300)
  2497. #define has_amd_full_remap_support(codec) is_amdhdmi_rev3_or_later(codec)
  2498. /* ATI/AMD specific HDA pin verbs, see the AMD HDA Verbs specification */
  2499. #define ATI_VERB_SET_CHANNEL_ALLOCATION 0x771
  2500. #define ATI_VERB_SET_DOWNMIX_INFO 0x772
  2501. #define ATI_VERB_SET_MULTICHANNEL_01 0x777
  2502. #define ATI_VERB_SET_MULTICHANNEL_23 0x778
  2503. #define ATI_VERB_SET_MULTICHANNEL_45 0x779
  2504. #define ATI_VERB_SET_MULTICHANNEL_67 0x77a
  2505. #define ATI_VERB_SET_HBR_CONTROL 0x77c
  2506. #define ATI_VERB_SET_MULTICHANNEL_1 0x785
  2507. #define ATI_VERB_SET_MULTICHANNEL_3 0x786
  2508. #define ATI_VERB_SET_MULTICHANNEL_5 0x787
  2509. #define ATI_VERB_SET_MULTICHANNEL_7 0x788
  2510. #define ATI_VERB_SET_MULTICHANNEL_MODE 0x789
  2511. #define ATI_VERB_GET_CHANNEL_ALLOCATION 0xf71
  2512. #define ATI_VERB_GET_DOWNMIX_INFO 0xf72
  2513. #define ATI_VERB_GET_MULTICHANNEL_01 0xf77
  2514. #define ATI_VERB_GET_MULTICHANNEL_23 0xf78
  2515. #define ATI_VERB_GET_MULTICHANNEL_45 0xf79
  2516. #define ATI_VERB_GET_MULTICHANNEL_67 0xf7a
  2517. #define ATI_VERB_GET_HBR_CONTROL 0xf7c
  2518. #define ATI_VERB_GET_MULTICHANNEL_1 0xf85
  2519. #define ATI_VERB_GET_MULTICHANNEL_3 0xf86
  2520. #define ATI_VERB_GET_MULTICHANNEL_5 0xf87
  2521. #define ATI_VERB_GET_MULTICHANNEL_7 0xf88
  2522. #define ATI_VERB_GET_MULTICHANNEL_MODE 0xf89
  2523. /* AMD specific HDA cvt verbs */
  2524. #define ATI_VERB_SET_RAMP_RATE 0x770
  2525. #define ATI_VERB_GET_RAMP_RATE 0xf70
  2526. #define ATI_OUT_ENABLE 0x1
  2527. #define ATI_MULTICHANNEL_MODE_PAIRED 0
  2528. #define ATI_MULTICHANNEL_MODE_SINGLE 1
  2529. #define ATI_HBR_CAPABLE 0x01
  2530. #define ATI_HBR_ENABLE 0x10
  2531. static int atihdmi_pin_get_eld(struct hda_codec *codec, hda_nid_t nid,
  2532. unsigned char *buf, int *eld_size)
  2533. {
  2534. /* call hda_eld.c ATI/AMD-specific function */
  2535. return snd_hdmi_get_eld_ati(codec, nid, buf, eld_size,
  2536. is_amdhdmi_rev3_or_later(codec));
  2537. }
  2538. static void atihdmi_pin_setup_infoframe(struct hda_codec *codec, hda_nid_t pin_nid, int ca,
  2539. int active_channels, int conn_type)
  2540. {
  2541. snd_hda_codec_write(codec, pin_nid, 0, ATI_VERB_SET_CHANNEL_ALLOCATION, ca);
  2542. }
  2543. static int atihdmi_paired_swap_fc_lfe(int pos)
  2544. {
  2545. /*
  2546. * ATI/AMD have automatic FC/LFE swap built-in
  2547. * when in pairwise mapping mode.
  2548. */
  2549. switch (pos) {
  2550. /* see channel_allocations[].speakers[] */
  2551. case 2: return 3;
  2552. case 3: return 2;
  2553. default: break;
  2554. }
  2555. return pos;
  2556. }
  2557. static int atihdmi_paired_chmap_validate(int ca, int chs, unsigned char *map)
  2558. {
  2559. struct cea_channel_speaker_allocation *cap;
  2560. int i, j;
  2561. /* check that only channel pairs need to be remapped on old pre-rev3 ATI/AMD */
  2562. cap = &channel_allocations[get_channel_allocation_order(ca)];
  2563. for (i = 0; i < chs; ++i) {
  2564. int mask = to_spk_mask(map[i]);
  2565. bool ok = false;
  2566. bool companion_ok = false;
  2567. if (!mask)
  2568. continue;
  2569. for (j = 0 + i % 2; j < 8; j += 2) {
  2570. int chan_idx = 7 - atihdmi_paired_swap_fc_lfe(j);
  2571. if (cap->speakers[chan_idx] == mask) {
  2572. /* channel is in a supported position */
  2573. ok = true;
  2574. if (i % 2 == 0 && i + 1 < chs) {
  2575. /* even channel, check the odd companion */
  2576. int comp_chan_idx = 7 - atihdmi_paired_swap_fc_lfe(j + 1);
  2577. int comp_mask_req = to_spk_mask(map[i+1]);
  2578. int comp_mask_act = cap->speakers[comp_chan_idx];
  2579. if (comp_mask_req == comp_mask_act)
  2580. companion_ok = true;
  2581. else
  2582. return -EINVAL;
  2583. }
  2584. break;
  2585. }
  2586. }
  2587. if (!ok)
  2588. return -EINVAL;
  2589. if (companion_ok)
  2590. i++; /* companion channel already checked */
  2591. }
  2592. return 0;
  2593. }
  2594. static int atihdmi_pin_set_slot_channel(struct hda_codec *codec, hda_nid_t pin_nid,
  2595. int hdmi_slot, int stream_channel)
  2596. {
  2597. int verb;
  2598. int ati_channel_setup = 0;
  2599. if (hdmi_slot > 7)
  2600. return -EINVAL;
  2601. if (!has_amd_full_remap_support(codec)) {
  2602. hdmi_slot = atihdmi_paired_swap_fc_lfe(hdmi_slot);
  2603. /* In case this is an odd slot but without stream channel, do not
  2604. * disable the slot since the corresponding even slot could have a
  2605. * channel. In case neither have a channel, the slot pair will be
  2606. * disabled when this function is called for the even slot. */
  2607. if (hdmi_slot % 2 != 0 && stream_channel == 0xf)
  2608. return 0;
  2609. hdmi_slot -= hdmi_slot % 2;
  2610. if (stream_channel != 0xf)
  2611. stream_channel -= stream_channel % 2;
  2612. }
  2613. verb = ATI_VERB_SET_MULTICHANNEL_01 + hdmi_slot/2 + (hdmi_slot % 2) * 0x00e;
  2614. /* ati_channel_setup format: [7..4] = stream_channel_id, [1] = mute, [0] = enable */
  2615. if (stream_channel != 0xf)
  2616. ati_channel_setup = (stream_channel << 4) | ATI_OUT_ENABLE;
  2617. return snd_hda_codec_write(codec, pin_nid, 0, verb, ati_channel_setup);
  2618. }
  2619. static int atihdmi_pin_get_slot_channel(struct hda_codec *codec, hda_nid_t pin_nid,
  2620. int asp_slot)
  2621. {
  2622. bool was_odd = false;
  2623. int ati_asp_slot = asp_slot;
  2624. int verb;
  2625. int ati_channel_setup;
  2626. if (asp_slot > 7)
  2627. return -EINVAL;
  2628. if (!has_amd_full_remap_support(codec)) {
  2629. ati_asp_slot = atihdmi_paired_swap_fc_lfe(asp_slot);
  2630. if (ati_asp_slot % 2 != 0) {
  2631. ati_asp_slot -= 1;
  2632. was_odd = true;
  2633. }
  2634. }
  2635. verb = ATI_VERB_GET_MULTICHANNEL_01 + ati_asp_slot/2 + (ati_asp_slot % 2) * 0x00e;
  2636. ati_channel_setup = snd_hda_codec_read(codec, pin_nid, 0, verb, 0);
  2637. if (!(ati_channel_setup & ATI_OUT_ENABLE))
  2638. return 0xf;
  2639. return ((ati_channel_setup & 0xf0) >> 4) + !!was_odd;
  2640. }
  2641. static int atihdmi_paired_chmap_cea_alloc_validate_get_type(struct cea_channel_speaker_allocation *cap,
  2642. int channels)
  2643. {
  2644. int c;
  2645. /*
  2646. * Pre-rev3 ATI/AMD codecs operate in a paired channel mode, so
  2647. * we need to take that into account (a single channel may take 2
  2648. * channel slots if we need to carry a silent channel next to it).
  2649. * On Rev3+ AMD codecs this function is not used.
  2650. */
  2651. int chanpairs = 0;
  2652. /* We only produce even-numbered channel count TLVs */
  2653. if ((channels % 2) != 0)
  2654. return -1;
  2655. for (c = 0; c < 7; c += 2) {
  2656. if (cap->speakers[c] || cap->speakers[c+1])
  2657. chanpairs++;
  2658. }
  2659. if (chanpairs * 2 != channels)
  2660. return -1;
  2661. return SNDRV_CTL_TLVT_CHMAP_PAIRED;
  2662. }
  2663. static void atihdmi_paired_cea_alloc_to_tlv_chmap(struct cea_channel_speaker_allocation *cap,
  2664. unsigned int *chmap, int channels)
  2665. {
  2666. /* produce paired maps for pre-rev3 ATI/AMD codecs */
  2667. int count = 0;
  2668. int c;
  2669. for (c = 7; c >= 0; c--) {
  2670. int chan = 7 - atihdmi_paired_swap_fc_lfe(7 - c);
  2671. int spk = cap->speakers[chan];
  2672. if (!spk) {
  2673. /* add N/A channel if the companion channel is occupied */
  2674. if (cap->speakers[chan + (chan % 2 ? -1 : 1)])
  2675. chmap[count++] = SNDRV_CHMAP_NA;
  2676. continue;
  2677. }
  2678. chmap[count++] = spk_to_chmap(spk);
  2679. }
  2680. WARN_ON(count != channels);
  2681. }
  2682. static int atihdmi_pin_hbr_setup(struct hda_codec *codec, hda_nid_t pin_nid,
  2683. bool hbr)
  2684. {
  2685. int hbr_ctl, hbr_ctl_new;
  2686. hbr_ctl = snd_hda_codec_read(codec, pin_nid, 0, ATI_VERB_GET_HBR_CONTROL, 0);
  2687. if (hbr_ctl >= 0 && (hbr_ctl & ATI_HBR_CAPABLE)) {
  2688. if (hbr)
  2689. hbr_ctl_new = hbr_ctl | ATI_HBR_ENABLE;
  2690. else
  2691. hbr_ctl_new = hbr_ctl & ~ATI_HBR_ENABLE;
  2692. codec_dbg(codec,
  2693. "atihdmi_pin_hbr_setup: NID=0x%x, %shbr-ctl=0x%x\n",
  2694. pin_nid,
  2695. hbr_ctl == hbr_ctl_new ? "" : "new-",
  2696. hbr_ctl_new);
  2697. if (hbr_ctl != hbr_ctl_new)
  2698. snd_hda_codec_write(codec, pin_nid, 0,
  2699. ATI_VERB_SET_HBR_CONTROL,
  2700. hbr_ctl_new);
  2701. } else if (hbr)
  2702. return -EINVAL;
  2703. return 0;
  2704. }
  2705. static int atihdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
  2706. hda_nid_t pin_nid, u32 stream_tag, int format)
  2707. {
  2708. if (is_amdhdmi_rev3_or_later(codec)) {
  2709. int ramp_rate = 180; /* default as per AMD spec */
  2710. /* disable ramp-up/down for non-pcm as per AMD spec */
  2711. if (format & AC_FMT_TYPE_NON_PCM)
  2712. ramp_rate = 0;
  2713. snd_hda_codec_write(codec, cvt_nid, 0, ATI_VERB_SET_RAMP_RATE, ramp_rate);
  2714. }
  2715. return hdmi_setup_stream(codec, cvt_nid, pin_nid, stream_tag, format);
  2716. }
  2717. static int atihdmi_init(struct hda_codec *codec)
  2718. {
  2719. struct hdmi_spec *spec = codec->spec;
  2720. int pin_idx, err;
  2721. err = generic_hdmi_init(codec);
  2722. if (err)
  2723. return err;
  2724. for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
  2725. struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
  2726. /* make sure downmix information in infoframe is zero */
  2727. snd_hda_codec_write(codec, per_pin->pin_nid, 0, ATI_VERB_SET_DOWNMIX_INFO, 0);
  2728. /* enable channel-wise remap mode if supported */
  2729. if (has_amd_full_remap_support(codec))
  2730. snd_hda_codec_write(codec, per_pin->pin_nid, 0,
  2731. ATI_VERB_SET_MULTICHANNEL_MODE,
  2732. ATI_MULTICHANNEL_MODE_SINGLE);
  2733. }
  2734. return 0;
  2735. }
  2736. static int patch_atihdmi(struct hda_codec *codec)
  2737. {
  2738. struct hdmi_spec *spec;
  2739. struct hdmi_spec_per_cvt *per_cvt;
  2740. int err, cvt_idx;
  2741. err = patch_generic_hdmi(codec);
  2742. if (err)
  2743. return err;
  2744. codec->patch_ops.init = atihdmi_init;
  2745. spec = codec->spec;
  2746. spec->ops.pin_get_eld = atihdmi_pin_get_eld;
  2747. spec->ops.pin_get_slot_channel = atihdmi_pin_get_slot_channel;
  2748. spec->ops.pin_set_slot_channel = atihdmi_pin_set_slot_channel;
  2749. spec->ops.pin_setup_infoframe = atihdmi_pin_setup_infoframe;
  2750. spec->ops.pin_hbr_setup = atihdmi_pin_hbr_setup;
  2751. spec->ops.setup_stream = atihdmi_setup_stream;
  2752. if (!has_amd_full_remap_support(codec)) {
  2753. /* override to ATI/AMD-specific versions with pairwise mapping */
  2754. spec->ops.chmap_cea_alloc_validate_get_type =
  2755. atihdmi_paired_chmap_cea_alloc_validate_get_type;
  2756. spec->ops.cea_alloc_to_tlv_chmap = atihdmi_paired_cea_alloc_to_tlv_chmap;
  2757. spec->ops.chmap_validate = atihdmi_paired_chmap_validate;
  2758. }
  2759. /* ATI/AMD converters do not advertise all of their capabilities */
  2760. for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
  2761. per_cvt = get_cvt(spec, cvt_idx);
  2762. per_cvt->channels_max = max(per_cvt->channels_max, 8u);
  2763. per_cvt->rates |= SUPPORTED_RATES;
  2764. per_cvt->formats |= SUPPORTED_FORMATS;
  2765. per_cvt->maxbps = max(per_cvt->maxbps, 24u);
  2766. }
  2767. spec->channels_max = max(spec->channels_max, 8u);
  2768. return 0;
  2769. }
  2770. /* VIA HDMI Implementation */
  2771. #define VIAHDMI_CVT_NID 0x02 /* audio converter1 */
  2772. #define VIAHDMI_PIN_NID 0x03 /* HDMI output pin1 */
  2773. static int patch_via_hdmi(struct hda_codec *codec)
  2774. {
  2775. return patch_simple_hdmi(codec, VIAHDMI_CVT_NID, VIAHDMI_PIN_NID);
  2776. }
  2777. /*
  2778. * called from hda_codec.c for generic HDMI support
  2779. */
  2780. int snd_hda_parse_hdmi_codec(struct hda_codec *codec)
  2781. {
  2782. return patch_generic_hdmi(codec);
  2783. }
  2784. EXPORT_SYMBOL_GPL(snd_hda_parse_hdmi_codec);
  2785. /*
  2786. * patch entries
  2787. */
  2788. static const struct hda_codec_preset snd_hda_preset_hdmi[] = {
  2789. { .id = 0x1002793c, .name = "RS600 HDMI", .patch = patch_atihdmi },
  2790. { .id = 0x10027919, .name = "RS600 HDMI", .patch = patch_atihdmi },
  2791. { .id = 0x1002791a, .name = "RS690/780 HDMI", .patch = patch_atihdmi },
  2792. { .id = 0x1002aa01, .name = "R6xx HDMI", .patch = patch_atihdmi },
  2793. { .id = 0x10951390, .name = "SiI1390 HDMI", .patch = patch_generic_hdmi },
  2794. { .id = 0x10951392, .name = "SiI1392 HDMI", .patch = patch_generic_hdmi },
  2795. { .id = 0x17e80047, .name = "Chrontel HDMI", .patch = patch_generic_hdmi },
  2796. { .id = 0x10de0002, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
  2797. { .id = 0x10de0003, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
  2798. { .id = 0x10de0005, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
  2799. { .id = 0x10de0006, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
  2800. { .id = 0x10de0007, .name = "MCP79/7A HDMI", .patch = patch_nvhdmi_8ch_7x },
  2801. { .id = 0x10de000a, .name = "GPU 0a HDMI/DP", .patch = patch_nvhdmi },
  2802. { .id = 0x10de000b, .name = "GPU 0b HDMI/DP", .patch = patch_nvhdmi },
  2803. { .id = 0x10de000c, .name = "MCP89 HDMI", .patch = patch_nvhdmi },
  2804. { .id = 0x10de000d, .name = "GPU 0d HDMI/DP", .patch = patch_nvhdmi },
  2805. { .id = 0x10de0010, .name = "GPU 10 HDMI/DP", .patch = patch_nvhdmi },
  2806. { .id = 0x10de0011, .name = "GPU 11 HDMI/DP", .patch = patch_nvhdmi },
  2807. { .id = 0x10de0012, .name = "GPU 12 HDMI/DP", .patch = patch_nvhdmi },
  2808. { .id = 0x10de0013, .name = "GPU 13 HDMI/DP", .patch = patch_nvhdmi },
  2809. { .id = 0x10de0014, .name = "GPU 14 HDMI/DP", .patch = patch_nvhdmi },
  2810. { .id = 0x10de0015, .name = "GPU 15 HDMI/DP", .patch = patch_nvhdmi },
  2811. { .id = 0x10de0016, .name = "GPU 16 HDMI/DP", .patch = patch_nvhdmi },
  2812. /* 17 is known to be absent */
  2813. { .id = 0x10de0018, .name = "GPU 18 HDMI/DP", .patch = patch_nvhdmi },
  2814. { .id = 0x10de0019, .name = "GPU 19 HDMI/DP", .patch = patch_nvhdmi },
  2815. { .id = 0x10de001a, .name = "GPU 1a HDMI/DP", .patch = patch_nvhdmi },
  2816. { .id = 0x10de001b, .name = "GPU 1b HDMI/DP", .patch = patch_nvhdmi },
  2817. { .id = 0x10de001c, .name = "GPU 1c HDMI/DP", .patch = patch_nvhdmi },
  2818. { .id = 0x10de0028, .name = "Tegra12x HDMI", .patch = patch_nvhdmi },
  2819. { .id = 0x10de0040, .name = "GPU 40 HDMI/DP", .patch = patch_nvhdmi },
  2820. { .id = 0x10de0041, .name = "GPU 41 HDMI/DP", .patch = patch_nvhdmi },
  2821. { .id = 0x10de0042, .name = "GPU 42 HDMI/DP", .patch = patch_nvhdmi },
  2822. { .id = 0x10de0043, .name = "GPU 43 HDMI/DP", .patch = patch_nvhdmi },
  2823. { .id = 0x10de0044, .name = "GPU 44 HDMI/DP", .patch = patch_nvhdmi },
  2824. { .id = 0x10de0051, .name = "GPU 51 HDMI/DP", .patch = patch_nvhdmi },
  2825. { .id = 0x10de0060, .name = "GPU 60 HDMI/DP", .patch = patch_nvhdmi },
  2826. { .id = 0x10de0067, .name = "MCP67 HDMI", .patch = patch_nvhdmi_2ch },
  2827. { .id = 0x10de0070, .name = "GPU 70 HDMI/DP", .patch = patch_nvhdmi },
  2828. { .id = 0x10de0071, .name = "GPU 71 HDMI/DP", .patch = patch_nvhdmi },
  2829. { .id = 0x10de0072, .name = "GPU 72 HDMI/DP", .patch = patch_nvhdmi },
  2830. { .id = 0x10de8001, .name = "MCP73 HDMI", .patch = patch_nvhdmi_2ch },
  2831. { .id = 0x11069f80, .name = "VX900 HDMI/DP", .patch = patch_via_hdmi },
  2832. { .id = 0x11069f81, .name = "VX900 HDMI/DP", .patch = patch_via_hdmi },
  2833. { .id = 0x11069f84, .name = "VX11 HDMI/DP", .patch = patch_generic_hdmi },
  2834. { .id = 0x11069f85, .name = "VX11 HDMI/DP", .patch = patch_generic_hdmi },
  2835. { .id = 0x80860054, .name = "IbexPeak HDMI", .patch = patch_generic_hdmi },
  2836. { .id = 0x80862801, .name = "Bearlake HDMI", .patch = patch_generic_hdmi },
  2837. { .id = 0x80862802, .name = "Cantiga HDMI", .patch = patch_generic_hdmi },
  2838. { .id = 0x80862803, .name = "Eaglelake HDMI", .patch = patch_generic_hdmi },
  2839. { .id = 0x80862804, .name = "IbexPeak HDMI", .patch = patch_generic_hdmi },
  2840. { .id = 0x80862805, .name = "CougarPoint HDMI", .patch = patch_generic_hdmi },
  2841. { .id = 0x80862806, .name = "PantherPoint HDMI", .patch = patch_generic_hdmi },
  2842. { .id = 0x80862807, .name = "Haswell HDMI", .patch = patch_generic_hdmi },
  2843. { .id = 0x80862808, .name = "Broadwell HDMI", .patch = patch_generic_hdmi },
  2844. { .id = 0x80862880, .name = "CedarTrail HDMI", .patch = patch_generic_hdmi },
  2845. { .id = 0x80862882, .name = "Valleyview2 HDMI", .patch = patch_generic_hdmi },
  2846. { .id = 0x80862883, .name = "Braswell HDMI", .patch = patch_generic_hdmi },
  2847. { .id = 0x808629fb, .name = "Crestline HDMI", .patch = patch_generic_hdmi },
  2848. {} /* terminator */
  2849. };
  2850. MODULE_ALIAS("snd-hda-codec-id:1002793c");
  2851. MODULE_ALIAS("snd-hda-codec-id:10027919");
  2852. MODULE_ALIAS("snd-hda-codec-id:1002791a");
  2853. MODULE_ALIAS("snd-hda-codec-id:1002aa01");
  2854. MODULE_ALIAS("snd-hda-codec-id:10951390");
  2855. MODULE_ALIAS("snd-hda-codec-id:10951392");
  2856. MODULE_ALIAS("snd-hda-codec-id:10de0002");
  2857. MODULE_ALIAS("snd-hda-codec-id:10de0003");
  2858. MODULE_ALIAS("snd-hda-codec-id:10de0005");
  2859. MODULE_ALIAS("snd-hda-codec-id:10de0006");
  2860. MODULE_ALIAS("snd-hda-codec-id:10de0007");
  2861. MODULE_ALIAS("snd-hda-codec-id:10de000a");
  2862. MODULE_ALIAS("snd-hda-codec-id:10de000b");
  2863. MODULE_ALIAS("snd-hda-codec-id:10de000c");
  2864. MODULE_ALIAS("snd-hda-codec-id:10de000d");
  2865. MODULE_ALIAS("snd-hda-codec-id:10de0010");
  2866. MODULE_ALIAS("snd-hda-codec-id:10de0011");
  2867. MODULE_ALIAS("snd-hda-codec-id:10de0012");
  2868. MODULE_ALIAS("snd-hda-codec-id:10de0013");
  2869. MODULE_ALIAS("snd-hda-codec-id:10de0014");
  2870. MODULE_ALIAS("snd-hda-codec-id:10de0015");
  2871. MODULE_ALIAS("snd-hda-codec-id:10de0016");
  2872. MODULE_ALIAS("snd-hda-codec-id:10de0018");
  2873. MODULE_ALIAS("snd-hda-codec-id:10de0019");
  2874. MODULE_ALIAS("snd-hda-codec-id:10de001a");
  2875. MODULE_ALIAS("snd-hda-codec-id:10de001b");
  2876. MODULE_ALIAS("snd-hda-codec-id:10de001c");
  2877. MODULE_ALIAS("snd-hda-codec-id:10de0028");
  2878. MODULE_ALIAS("snd-hda-codec-id:10de0040");
  2879. MODULE_ALIAS("snd-hda-codec-id:10de0041");
  2880. MODULE_ALIAS("snd-hda-codec-id:10de0042");
  2881. MODULE_ALIAS("snd-hda-codec-id:10de0043");
  2882. MODULE_ALIAS("snd-hda-codec-id:10de0044");
  2883. MODULE_ALIAS("snd-hda-codec-id:10de0051");
  2884. MODULE_ALIAS("snd-hda-codec-id:10de0060");
  2885. MODULE_ALIAS("snd-hda-codec-id:10de0067");
  2886. MODULE_ALIAS("snd-hda-codec-id:10de0070");
  2887. MODULE_ALIAS("snd-hda-codec-id:10de0071");
  2888. MODULE_ALIAS("snd-hda-codec-id:10de0072");
  2889. MODULE_ALIAS("snd-hda-codec-id:10de8001");
  2890. MODULE_ALIAS("snd-hda-codec-id:11069f80");
  2891. MODULE_ALIAS("snd-hda-codec-id:11069f81");
  2892. MODULE_ALIAS("snd-hda-codec-id:11069f84");
  2893. MODULE_ALIAS("snd-hda-codec-id:11069f85");
  2894. MODULE_ALIAS("snd-hda-codec-id:17e80047");
  2895. MODULE_ALIAS("snd-hda-codec-id:80860054");
  2896. MODULE_ALIAS("snd-hda-codec-id:80862801");
  2897. MODULE_ALIAS("snd-hda-codec-id:80862802");
  2898. MODULE_ALIAS("snd-hda-codec-id:80862803");
  2899. MODULE_ALIAS("snd-hda-codec-id:80862804");
  2900. MODULE_ALIAS("snd-hda-codec-id:80862805");
  2901. MODULE_ALIAS("snd-hda-codec-id:80862806");
  2902. MODULE_ALIAS("snd-hda-codec-id:80862807");
  2903. MODULE_ALIAS("snd-hda-codec-id:80862808");
  2904. MODULE_ALIAS("snd-hda-codec-id:80862880");
  2905. MODULE_ALIAS("snd-hda-codec-id:80862882");
  2906. MODULE_ALIAS("snd-hda-codec-id:80862883");
  2907. MODULE_ALIAS("snd-hda-codec-id:808629fb");
  2908. MODULE_LICENSE("GPL");
  2909. MODULE_DESCRIPTION("HDMI HD-audio codec");
  2910. MODULE_ALIAS("snd-hda-codec-intelhdmi");
  2911. MODULE_ALIAS("snd-hda-codec-nvhdmi");
  2912. MODULE_ALIAS("snd-hda-codec-atihdmi");
  2913. static struct hda_codec_preset_list intel_list = {
  2914. .preset = snd_hda_preset_hdmi,
  2915. .owner = THIS_MODULE,
  2916. };
  2917. static int __init patch_hdmi_init(void)
  2918. {
  2919. return snd_hda_add_codec_preset(&intel_list);
  2920. }
  2921. static void __exit patch_hdmi_exit(void)
  2922. {
  2923. snd_hda_delete_codec_preset(&intel_list);
  2924. }
  2925. module_init(patch_hdmi_init)
  2926. module_exit(patch_hdmi_exit)