patch_hdmi.c 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406
  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. *
  10. * Authors:
  11. * Wu Fengguang <wfg@linux.intel.com>
  12. *
  13. * Maintained by:
  14. * Wu Fengguang <wfg@linux.intel.com>
  15. *
  16. * This program is free software; you can redistribute it and/or modify it
  17. * under the terms of the GNU General Public License as published by the Free
  18. * Software Foundation; either version 2 of the License, or (at your option)
  19. * any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful, but
  22. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  23. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  24. * for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, write to the Free Software Foundation,
  28. * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  29. */
  30. #include <linux/init.h>
  31. #include <linux/delay.h>
  32. #include <linux/slab.h>
  33. #include <linux/module.h>
  34. #include <sound/core.h>
  35. #include <sound/jack.h>
  36. #include <sound/asoundef.h>
  37. #include <sound/tlv.h>
  38. #include "hda_codec.h"
  39. #include "hda_local.h"
  40. #include "hda_jack.h"
  41. static bool static_hdmi_pcm;
  42. module_param(static_hdmi_pcm, bool, 0644);
  43. MODULE_PARM_DESC(static_hdmi_pcm, "Don't restrict PCM parameters per ELD info");
  44. /*
  45. * The HDMI/DisplayPort configuration can be highly dynamic. A graphics device
  46. * could support N independent pipes, each of them can be connected to one or
  47. * more ports (DVI, HDMI or DisplayPort).
  48. *
  49. * The HDA correspondence of pipes/ports are converter/pin nodes.
  50. */
  51. #define MAX_HDMI_CVTS 8
  52. #define MAX_HDMI_PINS 8
  53. struct hdmi_spec_per_cvt {
  54. hda_nid_t cvt_nid;
  55. int assigned;
  56. unsigned int channels_min;
  57. unsigned int channels_max;
  58. u32 rates;
  59. u64 formats;
  60. unsigned int maxbps;
  61. };
  62. 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. struct hda_codec *codec;
  67. struct hdmi_eld sink_eld;
  68. struct delayed_work work;
  69. int repoll_count;
  70. bool non_pcm;
  71. bool chmap_set; /* channel-map override by ALSA API? */
  72. unsigned char chmap[8]; /* ALSA API channel-map */
  73. };
  74. struct hdmi_spec {
  75. int num_cvts;
  76. struct hdmi_spec_per_cvt cvts[MAX_HDMI_CVTS];
  77. int num_pins;
  78. struct hdmi_spec_per_pin pins[MAX_HDMI_PINS];
  79. struct hda_pcm pcm_rec[MAX_HDMI_PINS];
  80. unsigned int channels_max; /* max over all cvts */
  81. /*
  82. * Non-generic ATI/NVIDIA specific
  83. */
  84. struct hda_multi_out multiout;
  85. struct hda_pcm_stream pcm_playback;
  86. };
  87. struct hdmi_audio_infoframe {
  88. u8 type; /* 0x84 */
  89. u8 ver; /* 0x01 */
  90. u8 len; /* 0x0a */
  91. u8 checksum;
  92. u8 CC02_CT47; /* CC in bits 0:2, CT in 4:7 */
  93. u8 SS01_SF24;
  94. u8 CXT04;
  95. u8 CA;
  96. u8 LFEPBL01_LSV36_DM_INH7;
  97. };
  98. struct dp_audio_infoframe {
  99. u8 type; /* 0x84 */
  100. u8 len; /* 0x1b */
  101. u8 ver; /* 0x11 << 2 */
  102. u8 CC02_CT47; /* match with HDMI infoframe from this on */
  103. u8 SS01_SF24;
  104. u8 CXT04;
  105. u8 CA;
  106. u8 LFEPBL01_LSV36_DM_INH7;
  107. };
  108. union audio_infoframe {
  109. struct hdmi_audio_infoframe hdmi;
  110. struct dp_audio_infoframe dp;
  111. u8 bytes[0];
  112. };
  113. /*
  114. * CEA speaker placement:
  115. *
  116. * FLH FCH FRH
  117. * FLW FL FLC FC FRC FR FRW
  118. *
  119. * LFE
  120. * TC
  121. *
  122. * RL RLC RC RRC RR
  123. *
  124. * The Left/Right Surround channel _notions_ LS/RS in SMPTE 320M corresponds to
  125. * CEA RL/RR; The SMPTE channel _assignment_ C/LFE is swapped to CEA LFE/FC.
  126. */
  127. enum cea_speaker_placement {
  128. FL = (1 << 0), /* Front Left */
  129. FC = (1 << 1), /* Front Center */
  130. FR = (1 << 2), /* Front Right */
  131. FLC = (1 << 3), /* Front Left Center */
  132. FRC = (1 << 4), /* Front Right Center */
  133. RL = (1 << 5), /* Rear Left */
  134. RC = (1 << 6), /* Rear Center */
  135. RR = (1 << 7), /* Rear Right */
  136. RLC = (1 << 8), /* Rear Left Center */
  137. RRC = (1 << 9), /* Rear Right Center */
  138. LFE = (1 << 10), /* Low Frequency Effect */
  139. FLW = (1 << 11), /* Front Left Wide */
  140. FRW = (1 << 12), /* Front Right Wide */
  141. FLH = (1 << 13), /* Front Left High */
  142. FCH = (1 << 14), /* Front Center High */
  143. FRH = (1 << 15), /* Front Right High */
  144. TC = (1 << 16), /* Top Center */
  145. };
  146. /*
  147. * ELD SA bits in the CEA Speaker Allocation data block
  148. */
  149. static int eld_speaker_allocation_bits[] = {
  150. [0] = FL | FR,
  151. [1] = LFE,
  152. [2] = FC,
  153. [3] = RL | RR,
  154. [4] = RC,
  155. [5] = FLC | FRC,
  156. [6] = RLC | RRC,
  157. /* the following are not defined in ELD yet */
  158. [7] = FLW | FRW,
  159. [8] = FLH | FRH,
  160. [9] = TC,
  161. [10] = FCH,
  162. };
  163. struct cea_channel_speaker_allocation {
  164. int ca_index;
  165. int speakers[8];
  166. /* derived values, just for convenience */
  167. int channels;
  168. int spk_mask;
  169. };
  170. /*
  171. * ALSA sequence is:
  172. *
  173. * surround40 surround41 surround50 surround51 surround71
  174. * ch0 front left = = = =
  175. * ch1 front right = = = =
  176. * ch2 rear left = = = =
  177. * ch3 rear right = = = =
  178. * ch4 LFE center center center
  179. * ch5 LFE LFE
  180. * ch6 side left
  181. * ch7 side right
  182. *
  183. * surround71 = {FL, FR, RLC, RRC, FC, LFE, RL, RR}
  184. */
  185. static int hdmi_channel_mapping[0x32][8] = {
  186. /* stereo */
  187. [0x00] = { 0x00, 0x11, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7 },
  188. /* 2.1 */
  189. [0x01] = { 0x00, 0x11, 0x22, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7 },
  190. /* Dolby Surround */
  191. [0x02] = { 0x00, 0x11, 0x23, 0xf2, 0xf4, 0xf5, 0xf6, 0xf7 },
  192. /* surround40 */
  193. [0x08] = { 0x00, 0x11, 0x24, 0x35, 0xf3, 0xf2, 0xf6, 0xf7 },
  194. /* 4ch */
  195. [0x03] = { 0x00, 0x11, 0x23, 0x32, 0x44, 0xf5, 0xf6, 0xf7 },
  196. /* surround41 */
  197. [0x09] = { 0x00, 0x11, 0x24, 0x35, 0x42, 0xf3, 0xf6, 0xf7 },
  198. /* surround50 */
  199. [0x0a] = { 0x00, 0x11, 0x24, 0x35, 0x43, 0xf2, 0xf6, 0xf7 },
  200. /* surround51 */
  201. [0x0b] = { 0x00, 0x11, 0x24, 0x35, 0x43, 0x52, 0xf6, 0xf7 },
  202. /* 7.1 */
  203. [0x13] = { 0x00, 0x11, 0x26, 0x37, 0x43, 0x52, 0x64, 0x75 },
  204. };
  205. /*
  206. * This is an ordered list!
  207. *
  208. * The preceding ones have better chances to be selected by
  209. * hdmi_channel_allocation().
  210. */
  211. static struct cea_channel_speaker_allocation channel_allocations[] = {
  212. /* channel: 7 6 5 4 3 2 1 0 */
  213. { .ca_index = 0x00, .speakers = { 0, 0, 0, 0, 0, 0, FR, FL } },
  214. /* 2.1 */
  215. { .ca_index = 0x01, .speakers = { 0, 0, 0, 0, 0, LFE, FR, FL } },
  216. /* Dolby Surround */
  217. { .ca_index = 0x02, .speakers = { 0, 0, 0, 0, FC, 0, FR, FL } },
  218. /* surround40 */
  219. { .ca_index = 0x08, .speakers = { 0, 0, RR, RL, 0, 0, FR, FL } },
  220. /* surround41 */
  221. { .ca_index = 0x09, .speakers = { 0, 0, RR, RL, 0, LFE, FR, FL } },
  222. /* surround50 */
  223. { .ca_index = 0x0a, .speakers = { 0, 0, RR, RL, FC, 0, FR, FL } },
  224. /* surround51 */
  225. { .ca_index = 0x0b, .speakers = { 0, 0, RR, RL, FC, LFE, FR, FL } },
  226. /* 6.1 */
  227. { .ca_index = 0x0f, .speakers = { 0, RC, RR, RL, FC, LFE, FR, FL } },
  228. /* surround71 */
  229. { .ca_index = 0x13, .speakers = { RRC, RLC, RR, RL, FC, LFE, FR, FL } },
  230. { .ca_index = 0x03, .speakers = { 0, 0, 0, 0, FC, LFE, FR, FL } },
  231. { .ca_index = 0x04, .speakers = { 0, 0, 0, RC, 0, 0, FR, FL } },
  232. { .ca_index = 0x05, .speakers = { 0, 0, 0, RC, 0, LFE, FR, FL } },
  233. { .ca_index = 0x06, .speakers = { 0, 0, 0, RC, FC, 0, FR, FL } },
  234. { .ca_index = 0x07, .speakers = { 0, 0, 0, RC, FC, LFE, FR, FL } },
  235. { .ca_index = 0x0c, .speakers = { 0, RC, RR, RL, 0, 0, FR, FL } },
  236. { .ca_index = 0x0d, .speakers = { 0, RC, RR, RL, 0, LFE, FR, FL } },
  237. { .ca_index = 0x0e, .speakers = { 0, RC, RR, RL, FC, 0, FR, FL } },
  238. { .ca_index = 0x10, .speakers = { RRC, RLC, RR, RL, 0, 0, FR, FL } },
  239. { .ca_index = 0x11, .speakers = { RRC, RLC, RR, RL, 0, LFE, FR, FL } },
  240. { .ca_index = 0x12, .speakers = { RRC, RLC, RR, RL, FC, 0, FR, FL } },
  241. { .ca_index = 0x14, .speakers = { FRC, FLC, 0, 0, 0, 0, FR, FL } },
  242. { .ca_index = 0x15, .speakers = { FRC, FLC, 0, 0, 0, LFE, FR, FL } },
  243. { .ca_index = 0x16, .speakers = { FRC, FLC, 0, 0, FC, 0, FR, FL } },
  244. { .ca_index = 0x17, .speakers = { FRC, FLC, 0, 0, FC, LFE, FR, FL } },
  245. { .ca_index = 0x18, .speakers = { FRC, FLC, 0, RC, 0, 0, FR, FL } },
  246. { .ca_index = 0x19, .speakers = { FRC, FLC, 0, RC, 0, LFE, FR, FL } },
  247. { .ca_index = 0x1a, .speakers = { FRC, FLC, 0, RC, FC, 0, FR, FL } },
  248. { .ca_index = 0x1b, .speakers = { FRC, FLC, 0, RC, FC, LFE, FR, FL } },
  249. { .ca_index = 0x1c, .speakers = { FRC, FLC, RR, RL, 0, 0, FR, FL } },
  250. { .ca_index = 0x1d, .speakers = { FRC, FLC, RR, RL, 0, LFE, FR, FL } },
  251. { .ca_index = 0x1e, .speakers = { FRC, FLC, RR, RL, FC, 0, FR, FL } },
  252. { .ca_index = 0x1f, .speakers = { FRC, FLC, RR, RL, FC, LFE, FR, FL } },
  253. { .ca_index = 0x20, .speakers = { 0, FCH, RR, RL, FC, 0, FR, FL } },
  254. { .ca_index = 0x21, .speakers = { 0, FCH, RR, RL, FC, LFE, FR, FL } },
  255. { .ca_index = 0x22, .speakers = { TC, 0, RR, RL, FC, 0, FR, FL } },
  256. { .ca_index = 0x23, .speakers = { TC, 0, RR, RL, FC, LFE, FR, FL } },
  257. { .ca_index = 0x24, .speakers = { FRH, FLH, RR, RL, 0, 0, FR, FL } },
  258. { .ca_index = 0x25, .speakers = { FRH, FLH, RR, RL, 0, LFE, FR, FL } },
  259. { .ca_index = 0x26, .speakers = { FRW, FLW, RR, RL, 0, 0, FR, FL } },
  260. { .ca_index = 0x27, .speakers = { FRW, FLW, RR, RL, 0, LFE, FR, FL } },
  261. { .ca_index = 0x28, .speakers = { TC, RC, RR, RL, FC, 0, FR, FL } },
  262. { .ca_index = 0x29, .speakers = { TC, RC, RR, RL, FC, LFE, FR, FL } },
  263. { .ca_index = 0x2a, .speakers = { FCH, RC, RR, RL, FC, 0, FR, FL } },
  264. { .ca_index = 0x2b, .speakers = { FCH, RC, RR, RL, FC, LFE, FR, FL } },
  265. { .ca_index = 0x2c, .speakers = { TC, FCH, RR, RL, FC, 0, FR, FL } },
  266. { .ca_index = 0x2d, .speakers = { TC, FCH, RR, RL, FC, LFE, FR, FL } },
  267. { .ca_index = 0x2e, .speakers = { FRH, FLH, RR, RL, FC, 0, FR, FL } },
  268. { .ca_index = 0x2f, .speakers = { FRH, FLH, RR, RL, FC, LFE, FR, FL } },
  269. { .ca_index = 0x30, .speakers = { FRW, FLW, RR, RL, FC, 0, FR, FL } },
  270. { .ca_index = 0x31, .speakers = { FRW, FLW, RR, RL, FC, LFE, FR, FL } },
  271. };
  272. /*
  273. * HDMI routines
  274. */
  275. static int pin_nid_to_pin_index(struct hdmi_spec *spec, hda_nid_t pin_nid)
  276. {
  277. int pin_idx;
  278. for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++)
  279. if (spec->pins[pin_idx].pin_nid == pin_nid)
  280. return pin_idx;
  281. snd_printk(KERN_WARNING "HDMI: pin nid %d not registered\n", pin_nid);
  282. return -EINVAL;
  283. }
  284. static int hinfo_to_pin_index(struct hdmi_spec *spec,
  285. struct hda_pcm_stream *hinfo)
  286. {
  287. int pin_idx;
  288. for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++)
  289. if (&spec->pcm_rec[pin_idx].stream[0] == hinfo)
  290. return pin_idx;
  291. snd_printk(KERN_WARNING "HDMI: hinfo %p not registered\n", hinfo);
  292. return -EINVAL;
  293. }
  294. static int cvt_nid_to_cvt_index(struct hdmi_spec *spec, hda_nid_t cvt_nid)
  295. {
  296. int cvt_idx;
  297. for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++)
  298. if (spec->cvts[cvt_idx].cvt_nid == cvt_nid)
  299. return cvt_idx;
  300. snd_printk(KERN_WARNING "HDMI: cvt nid %d not registered\n", cvt_nid);
  301. return -EINVAL;
  302. }
  303. static int hdmi_eld_ctl_info(struct snd_kcontrol *kcontrol,
  304. struct snd_ctl_elem_info *uinfo)
  305. {
  306. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  307. struct hdmi_spec *spec;
  308. int pin_idx;
  309. spec = codec->spec;
  310. uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
  311. pin_idx = kcontrol->private_value;
  312. uinfo->count = spec->pins[pin_idx].sink_eld.eld_size;
  313. return 0;
  314. }
  315. static int hdmi_eld_ctl_get(struct snd_kcontrol *kcontrol,
  316. struct snd_ctl_elem_value *ucontrol)
  317. {
  318. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  319. struct hdmi_spec *spec;
  320. int pin_idx;
  321. spec = codec->spec;
  322. pin_idx = kcontrol->private_value;
  323. memcpy(ucontrol->value.bytes.data,
  324. spec->pins[pin_idx].sink_eld.eld_buffer, ELD_MAX_SIZE);
  325. return 0;
  326. }
  327. static struct snd_kcontrol_new eld_bytes_ctl = {
  328. .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
  329. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  330. .name = "ELD",
  331. .info = hdmi_eld_ctl_info,
  332. .get = hdmi_eld_ctl_get,
  333. };
  334. static int hdmi_create_eld_ctl(struct hda_codec *codec, int pin_idx,
  335. int device)
  336. {
  337. struct snd_kcontrol *kctl;
  338. struct hdmi_spec *spec = codec->spec;
  339. int err;
  340. kctl = snd_ctl_new1(&eld_bytes_ctl, codec);
  341. if (!kctl)
  342. return -ENOMEM;
  343. kctl->private_value = pin_idx;
  344. kctl->id.device = device;
  345. err = snd_hda_ctl_add(codec, spec->pins[pin_idx].pin_nid, kctl);
  346. if (err < 0)
  347. return err;
  348. return 0;
  349. }
  350. #ifdef BE_PARANOID
  351. static void hdmi_get_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
  352. int *packet_index, int *byte_index)
  353. {
  354. int val;
  355. val = snd_hda_codec_read(codec, pin_nid, 0,
  356. AC_VERB_GET_HDMI_DIP_INDEX, 0);
  357. *packet_index = val >> 5;
  358. *byte_index = val & 0x1f;
  359. }
  360. #endif
  361. static void hdmi_set_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
  362. int packet_index, int byte_index)
  363. {
  364. int val;
  365. val = (packet_index << 5) | (byte_index & 0x1f);
  366. snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val);
  367. }
  368. static void hdmi_write_dip_byte(struct hda_codec *codec, hda_nid_t pin_nid,
  369. unsigned char val)
  370. {
  371. snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_DATA, val);
  372. }
  373. static void hdmi_init_pin(struct hda_codec *codec, hda_nid_t pin_nid)
  374. {
  375. /* Unmute */
  376. if (get_wcaps(codec, pin_nid) & AC_WCAP_OUT_AMP)
  377. snd_hda_codec_write(codec, pin_nid, 0,
  378. AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
  379. /* Disable pin out until stream is active*/
  380. snd_hda_codec_write(codec, pin_nid, 0,
  381. AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
  382. }
  383. static int hdmi_get_channel_count(struct hda_codec *codec, hda_nid_t cvt_nid)
  384. {
  385. return 1 + snd_hda_codec_read(codec, cvt_nid, 0,
  386. AC_VERB_GET_CVT_CHAN_COUNT, 0);
  387. }
  388. static void hdmi_set_channel_count(struct hda_codec *codec,
  389. hda_nid_t cvt_nid, int chs)
  390. {
  391. if (chs != hdmi_get_channel_count(codec, cvt_nid))
  392. snd_hda_codec_write(codec, cvt_nid, 0,
  393. AC_VERB_SET_CVT_CHAN_COUNT, chs - 1);
  394. }
  395. /*
  396. * Channel mapping routines
  397. */
  398. /*
  399. * Compute derived values in channel_allocations[].
  400. */
  401. static void init_channel_allocations(void)
  402. {
  403. int i, j;
  404. struct cea_channel_speaker_allocation *p;
  405. for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
  406. p = channel_allocations + i;
  407. p->channels = 0;
  408. p->spk_mask = 0;
  409. for (j = 0; j < ARRAY_SIZE(p->speakers); j++)
  410. if (p->speakers[j]) {
  411. p->channels++;
  412. p->spk_mask |= p->speakers[j];
  413. }
  414. }
  415. }
  416. static int get_channel_allocation_order(int ca)
  417. {
  418. int i;
  419. for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
  420. if (channel_allocations[i].ca_index == ca)
  421. break;
  422. }
  423. return i;
  424. }
  425. /*
  426. * The transformation takes two steps:
  427. *
  428. * eld->spk_alloc => (eld_speaker_allocation_bits[]) => spk_mask
  429. * spk_mask => (channel_allocations[]) => ai->CA
  430. *
  431. * TODO: it could select the wrong CA from multiple candidates.
  432. */
  433. static int hdmi_channel_allocation(struct hdmi_eld *eld, int channels)
  434. {
  435. int i;
  436. int ca = 0;
  437. int spk_mask = 0;
  438. char buf[SND_PRINT_CHANNEL_ALLOCATION_ADVISED_BUFSIZE];
  439. /*
  440. * CA defaults to 0 for basic stereo audio
  441. */
  442. if (channels <= 2)
  443. return 0;
  444. /*
  445. * expand ELD's speaker allocation mask
  446. *
  447. * ELD tells the speaker mask in a compact(paired) form,
  448. * expand ELD's notions to match the ones used by Audio InfoFrame.
  449. */
  450. for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
  451. if (eld->spk_alloc & (1 << i))
  452. spk_mask |= eld_speaker_allocation_bits[i];
  453. }
  454. /* search for the first working match in the CA table */
  455. for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
  456. if (channels == channel_allocations[i].channels &&
  457. (spk_mask & channel_allocations[i].spk_mask) ==
  458. channel_allocations[i].spk_mask) {
  459. ca = channel_allocations[i].ca_index;
  460. break;
  461. }
  462. }
  463. snd_print_channel_allocation(eld->spk_alloc, buf, sizeof(buf));
  464. snd_printdd("HDMI: select CA 0x%x for %d-channel allocation: %s\n",
  465. ca, channels, buf);
  466. return ca;
  467. }
  468. static void hdmi_debug_channel_mapping(struct hda_codec *codec,
  469. hda_nid_t pin_nid)
  470. {
  471. #ifdef CONFIG_SND_DEBUG_VERBOSE
  472. int i;
  473. int slot;
  474. for (i = 0; i < 8; i++) {
  475. slot = snd_hda_codec_read(codec, pin_nid, 0,
  476. AC_VERB_GET_HDMI_CHAN_SLOT, i);
  477. printk(KERN_DEBUG "HDMI: ASP channel %d => slot %d\n",
  478. slot >> 4, slot & 0xf);
  479. }
  480. #endif
  481. }
  482. static void hdmi_std_setup_channel_mapping(struct hda_codec *codec,
  483. hda_nid_t pin_nid,
  484. bool non_pcm,
  485. int ca)
  486. {
  487. int i;
  488. int err;
  489. int order;
  490. int non_pcm_mapping[8];
  491. order = get_channel_allocation_order(ca);
  492. if (hdmi_channel_mapping[ca][1] == 0) {
  493. for (i = 0; i < channel_allocations[order].channels; i++)
  494. hdmi_channel_mapping[ca][i] = i | (i << 4);
  495. for (; i < 8; i++)
  496. hdmi_channel_mapping[ca][i] = 0xf | (i << 4);
  497. }
  498. if (non_pcm) {
  499. for (i = 0; i < channel_allocations[order].channels; i++)
  500. non_pcm_mapping[i] = i | (i << 4);
  501. for (; i < 8; i++)
  502. non_pcm_mapping[i] = 0xf | (i << 4);
  503. }
  504. for (i = 0; i < 8; i++) {
  505. err = snd_hda_codec_write(codec, pin_nid, 0,
  506. AC_VERB_SET_HDMI_CHAN_SLOT,
  507. non_pcm ? non_pcm_mapping[i] : hdmi_channel_mapping[ca][i]);
  508. if (err) {
  509. snd_printdd(KERN_NOTICE
  510. "HDMI: channel mapping failed\n");
  511. break;
  512. }
  513. }
  514. hdmi_debug_channel_mapping(codec, pin_nid);
  515. }
  516. struct channel_map_table {
  517. unsigned char map; /* ALSA API channel map position */
  518. unsigned char cea_slot; /* CEA slot value */
  519. int spk_mask; /* speaker position bit mask */
  520. };
  521. static struct channel_map_table map_tables[] = {
  522. { SNDRV_CHMAP_FL, 0x00, FL },
  523. { SNDRV_CHMAP_FR, 0x01, FR },
  524. { SNDRV_CHMAP_RL, 0x04, RL },
  525. { SNDRV_CHMAP_RR, 0x05, RR },
  526. { SNDRV_CHMAP_LFE, 0x02, LFE },
  527. { SNDRV_CHMAP_FC, 0x03, FC },
  528. { SNDRV_CHMAP_RLC, 0x06, RLC },
  529. { SNDRV_CHMAP_RRC, 0x07, RRC },
  530. {} /* terminator */
  531. };
  532. /* from ALSA API channel position to speaker bit mask */
  533. static int to_spk_mask(unsigned char c)
  534. {
  535. struct channel_map_table *t = map_tables;
  536. for (; t->map; t++) {
  537. if (t->map == c)
  538. return t->spk_mask;
  539. }
  540. return 0;
  541. }
  542. /* from ALSA API channel position to CEA slot */
  543. static int to_cea_slot(unsigned char c)
  544. {
  545. struct channel_map_table *t = map_tables;
  546. for (; t->map; t++) {
  547. if (t->map == c)
  548. return t->cea_slot;
  549. }
  550. return 0x0f;
  551. }
  552. /* from CEA slot to ALSA API channel position */
  553. static int from_cea_slot(unsigned char c)
  554. {
  555. struct channel_map_table *t = map_tables;
  556. for (; t->map; t++) {
  557. if (t->cea_slot == c)
  558. return t->map;
  559. }
  560. return 0;
  561. }
  562. /* from speaker bit mask to ALSA API channel position */
  563. static int spk_to_chmap(int spk)
  564. {
  565. struct channel_map_table *t = map_tables;
  566. for (; t->map; t++) {
  567. if (t->spk_mask == spk)
  568. return t->map;
  569. }
  570. return 0;
  571. }
  572. /* get the CA index corresponding to the given ALSA API channel map */
  573. static int hdmi_manual_channel_allocation(int chs, unsigned char *map)
  574. {
  575. int i, spks = 0, spk_mask = 0;
  576. for (i = 0; i < chs; i++) {
  577. int mask = to_spk_mask(map[i]);
  578. if (mask) {
  579. spk_mask |= mask;
  580. spks++;
  581. }
  582. }
  583. for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
  584. if ((chs == channel_allocations[i].channels ||
  585. spks == channel_allocations[i].channels) &&
  586. (spk_mask & channel_allocations[i].spk_mask) ==
  587. channel_allocations[i].spk_mask)
  588. return channel_allocations[i].ca_index;
  589. }
  590. return -1;
  591. }
  592. /* set up the channel slots for the given ALSA API channel map */
  593. static int hdmi_manual_setup_channel_mapping(struct hda_codec *codec,
  594. hda_nid_t pin_nid,
  595. int chs, unsigned char *map)
  596. {
  597. int i;
  598. for (i = 0; i < 8; i++) {
  599. int val, err;
  600. if (i < chs)
  601. val = to_cea_slot(map[i]);
  602. else
  603. val = 0xf;
  604. val |= (i << 4);
  605. err = snd_hda_codec_write(codec, pin_nid, 0,
  606. AC_VERB_SET_HDMI_CHAN_SLOT, val);
  607. if (err)
  608. return -EINVAL;
  609. }
  610. return 0;
  611. }
  612. /* store ALSA API channel map from the current default map */
  613. static void hdmi_setup_fake_chmap(unsigned char *map, int ca)
  614. {
  615. int i;
  616. for (i = 0; i < 8; i++) {
  617. if (i < channel_allocations[ca].channels)
  618. map[i] = from_cea_slot((hdmi_channel_mapping[ca][i] >> 4) & 0x0f);
  619. else
  620. map[i] = 0;
  621. }
  622. }
  623. static void hdmi_setup_channel_mapping(struct hda_codec *codec,
  624. hda_nid_t pin_nid, bool non_pcm, int ca,
  625. int channels, unsigned char *map)
  626. {
  627. if (!non_pcm && map) {
  628. hdmi_manual_setup_channel_mapping(codec, pin_nid,
  629. channels, map);
  630. } else {
  631. hdmi_std_setup_channel_mapping(codec, pin_nid, non_pcm, ca);
  632. hdmi_setup_fake_chmap(map, ca);
  633. }
  634. }
  635. /*
  636. * Audio InfoFrame routines
  637. */
  638. /*
  639. * Enable Audio InfoFrame Transmission
  640. */
  641. static void hdmi_start_infoframe_trans(struct hda_codec *codec,
  642. hda_nid_t pin_nid)
  643. {
  644. hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
  645. snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
  646. AC_DIPXMIT_BEST);
  647. }
  648. /*
  649. * Disable Audio InfoFrame Transmission
  650. */
  651. static void hdmi_stop_infoframe_trans(struct hda_codec *codec,
  652. hda_nid_t pin_nid)
  653. {
  654. hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
  655. snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
  656. AC_DIPXMIT_DISABLE);
  657. }
  658. static void hdmi_debug_dip_size(struct hda_codec *codec, hda_nid_t pin_nid)
  659. {
  660. #ifdef CONFIG_SND_DEBUG_VERBOSE
  661. int i;
  662. int size;
  663. size = snd_hdmi_get_eld_size(codec, pin_nid);
  664. printk(KERN_DEBUG "HDMI: ELD buf size is %d\n", size);
  665. for (i = 0; i < 8; i++) {
  666. size = snd_hda_codec_read(codec, pin_nid, 0,
  667. AC_VERB_GET_HDMI_DIP_SIZE, i);
  668. printk(KERN_DEBUG "HDMI: DIP GP[%d] buf size is %d\n", i, size);
  669. }
  670. #endif
  671. }
  672. static void hdmi_clear_dip_buffers(struct hda_codec *codec, hda_nid_t pin_nid)
  673. {
  674. #ifdef BE_PARANOID
  675. int i, j;
  676. int size;
  677. int pi, bi;
  678. for (i = 0; i < 8; i++) {
  679. size = snd_hda_codec_read(codec, pin_nid, 0,
  680. AC_VERB_GET_HDMI_DIP_SIZE, i);
  681. if (size == 0)
  682. continue;
  683. hdmi_set_dip_index(codec, pin_nid, i, 0x0);
  684. for (j = 1; j < 1000; j++) {
  685. hdmi_write_dip_byte(codec, pin_nid, 0x0);
  686. hdmi_get_dip_index(codec, pin_nid, &pi, &bi);
  687. if (pi != i)
  688. snd_printd(KERN_INFO "dip index %d: %d != %d\n",
  689. bi, pi, i);
  690. if (bi == 0) /* byte index wrapped around */
  691. break;
  692. }
  693. snd_printd(KERN_INFO
  694. "HDMI: DIP GP[%d] buf reported size=%d, written=%d\n",
  695. i, size, j);
  696. }
  697. #endif
  698. }
  699. static void hdmi_checksum_audio_infoframe(struct hdmi_audio_infoframe *hdmi_ai)
  700. {
  701. u8 *bytes = (u8 *)hdmi_ai;
  702. u8 sum = 0;
  703. int i;
  704. hdmi_ai->checksum = 0;
  705. for (i = 0; i < sizeof(*hdmi_ai); i++)
  706. sum += bytes[i];
  707. hdmi_ai->checksum = -sum;
  708. }
  709. static void hdmi_fill_audio_infoframe(struct hda_codec *codec,
  710. hda_nid_t pin_nid,
  711. u8 *dip, int size)
  712. {
  713. int i;
  714. hdmi_debug_dip_size(codec, pin_nid);
  715. hdmi_clear_dip_buffers(codec, pin_nid); /* be paranoid */
  716. hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
  717. for (i = 0; i < size; i++)
  718. hdmi_write_dip_byte(codec, pin_nid, dip[i]);
  719. }
  720. static bool hdmi_infoframe_uptodate(struct hda_codec *codec, hda_nid_t pin_nid,
  721. u8 *dip, int size)
  722. {
  723. u8 val;
  724. int i;
  725. if (snd_hda_codec_read(codec, pin_nid, 0, AC_VERB_GET_HDMI_DIP_XMIT, 0)
  726. != AC_DIPXMIT_BEST)
  727. return false;
  728. hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
  729. for (i = 0; i < size; i++) {
  730. val = snd_hda_codec_read(codec, pin_nid, 0,
  731. AC_VERB_GET_HDMI_DIP_DATA, 0);
  732. if (val != dip[i])
  733. return false;
  734. }
  735. return true;
  736. }
  737. static void hdmi_setup_audio_infoframe(struct hda_codec *codec, int pin_idx,
  738. bool non_pcm,
  739. struct snd_pcm_substream *substream)
  740. {
  741. struct hdmi_spec *spec = codec->spec;
  742. struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
  743. hda_nid_t pin_nid = per_pin->pin_nid;
  744. int channels = substream->runtime->channels;
  745. struct hdmi_eld *eld;
  746. int ca;
  747. union audio_infoframe ai;
  748. eld = &spec->pins[pin_idx].sink_eld;
  749. if (!eld->monitor_present)
  750. return;
  751. if (!non_pcm && per_pin->chmap_set)
  752. ca = hdmi_manual_channel_allocation(channels, per_pin->chmap);
  753. else
  754. ca = hdmi_channel_allocation(eld, channels);
  755. if (ca < 0)
  756. ca = 0;
  757. memset(&ai, 0, sizeof(ai));
  758. if (eld->conn_type == 0) { /* HDMI */
  759. struct hdmi_audio_infoframe *hdmi_ai = &ai.hdmi;
  760. hdmi_ai->type = 0x84;
  761. hdmi_ai->ver = 0x01;
  762. hdmi_ai->len = 0x0a;
  763. hdmi_ai->CC02_CT47 = channels - 1;
  764. hdmi_ai->CA = ca;
  765. hdmi_checksum_audio_infoframe(hdmi_ai);
  766. } else if (eld->conn_type == 1) { /* DisplayPort */
  767. struct dp_audio_infoframe *dp_ai = &ai.dp;
  768. dp_ai->type = 0x84;
  769. dp_ai->len = 0x1b;
  770. dp_ai->ver = 0x11 << 2;
  771. dp_ai->CC02_CT47 = channels - 1;
  772. dp_ai->CA = ca;
  773. } else {
  774. snd_printd("HDMI: unknown connection type at pin %d\n",
  775. pin_nid);
  776. return;
  777. }
  778. /*
  779. * sizeof(ai) is used instead of sizeof(*hdmi_ai) or
  780. * sizeof(*dp_ai) to avoid partial match/update problems when
  781. * the user switches between HDMI/DP monitors.
  782. */
  783. if (!hdmi_infoframe_uptodate(codec, pin_nid, ai.bytes,
  784. sizeof(ai))) {
  785. snd_printdd("hdmi_setup_audio_infoframe: "
  786. "pin=%d channels=%d\n",
  787. pin_nid,
  788. channels);
  789. hdmi_setup_channel_mapping(codec, pin_nid, non_pcm, ca,
  790. channels, per_pin->chmap);
  791. hdmi_stop_infoframe_trans(codec, pin_nid);
  792. hdmi_fill_audio_infoframe(codec, pin_nid,
  793. ai.bytes, sizeof(ai));
  794. hdmi_start_infoframe_trans(codec, pin_nid);
  795. } else {
  796. /* For non-pcm audio switch, setup new channel mapping
  797. * accordingly */
  798. if (per_pin->non_pcm != non_pcm)
  799. hdmi_setup_channel_mapping(codec, pin_nid, non_pcm, ca,
  800. channels, per_pin->chmap);
  801. }
  802. per_pin->non_pcm = non_pcm;
  803. }
  804. /*
  805. * Unsolicited events
  806. */
  807. static void hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll);
  808. static void hdmi_intrinsic_event(struct hda_codec *codec, unsigned int res)
  809. {
  810. struct hdmi_spec *spec = codec->spec;
  811. int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
  812. int pin_nid;
  813. int pin_idx;
  814. struct hda_jack_tbl *jack;
  815. jack = snd_hda_jack_tbl_get_from_tag(codec, tag);
  816. if (!jack)
  817. return;
  818. pin_nid = jack->nid;
  819. jack->jack_dirty = 1;
  820. _snd_printd(SND_PR_VERBOSE,
  821. "HDMI hot plug event: Codec=%d Pin=%d Presence_Detect=%d ELD_Valid=%d\n",
  822. codec->addr, pin_nid,
  823. !!(res & AC_UNSOL_RES_PD), !!(res & AC_UNSOL_RES_ELDV));
  824. pin_idx = pin_nid_to_pin_index(spec, pin_nid);
  825. if (pin_idx < 0)
  826. return;
  827. hdmi_present_sense(&spec->pins[pin_idx], 1);
  828. snd_hda_jack_report_sync(codec);
  829. }
  830. static void hdmi_non_intrinsic_event(struct hda_codec *codec, unsigned int res)
  831. {
  832. int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
  833. int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
  834. int cp_state = !!(res & AC_UNSOL_RES_CP_STATE);
  835. int cp_ready = !!(res & AC_UNSOL_RES_CP_READY);
  836. printk(KERN_INFO
  837. "HDMI CP event: CODEC=%d TAG=%d SUBTAG=0x%x CP_STATE=%d CP_READY=%d\n",
  838. codec->addr,
  839. tag,
  840. subtag,
  841. cp_state,
  842. cp_ready);
  843. /* TODO */
  844. if (cp_state)
  845. ;
  846. if (cp_ready)
  847. ;
  848. }
  849. static void hdmi_unsol_event(struct hda_codec *codec, unsigned int res)
  850. {
  851. int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
  852. int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
  853. if (!snd_hda_jack_tbl_get_from_tag(codec, tag)) {
  854. snd_printd(KERN_INFO "Unexpected HDMI event tag 0x%x\n", tag);
  855. return;
  856. }
  857. if (subtag == 0)
  858. hdmi_intrinsic_event(codec, res);
  859. else
  860. hdmi_non_intrinsic_event(codec, res);
  861. }
  862. /*
  863. * Callbacks
  864. */
  865. /* HBR should be Non-PCM, 8 channels */
  866. #define is_hbr_format(format) \
  867. ((format & AC_FMT_TYPE_NON_PCM) && (format & AC_FMT_CHAN_MASK) == 7)
  868. static int hdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
  869. hda_nid_t pin_nid, u32 stream_tag, int format)
  870. {
  871. int pinctl;
  872. int new_pinctl = 0;
  873. if (snd_hda_query_pin_caps(codec, pin_nid) & AC_PINCAP_HBR) {
  874. pinctl = snd_hda_codec_read(codec, pin_nid, 0,
  875. AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
  876. new_pinctl = pinctl & ~AC_PINCTL_EPT;
  877. if (is_hbr_format(format))
  878. new_pinctl |= AC_PINCTL_EPT_HBR;
  879. else
  880. new_pinctl |= AC_PINCTL_EPT_NATIVE;
  881. snd_printdd("hdmi_setup_stream: "
  882. "NID=0x%x, %spinctl=0x%x\n",
  883. pin_nid,
  884. pinctl == new_pinctl ? "" : "new-",
  885. new_pinctl);
  886. if (pinctl != new_pinctl)
  887. snd_hda_codec_write(codec, pin_nid, 0,
  888. AC_VERB_SET_PIN_WIDGET_CONTROL,
  889. new_pinctl);
  890. }
  891. if (is_hbr_format(format) && !new_pinctl) {
  892. snd_printdd("hdmi_setup_stream: HBR is not supported\n");
  893. return -EINVAL;
  894. }
  895. snd_hda_codec_setup_stream(codec, cvt_nid, stream_tag, 0, format);
  896. return 0;
  897. }
  898. /*
  899. * HDA PCM callbacks
  900. */
  901. static int hdmi_pcm_open(struct hda_pcm_stream *hinfo,
  902. struct hda_codec *codec,
  903. struct snd_pcm_substream *substream)
  904. {
  905. struct hdmi_spec *spec = codec->spec;
  906. struct snd_pcm_runtime *runtime = substream->runtime;
  907. int pin_idx, cvt_idx, mux_idx = 0;
  908. struct hdmi_spec_per_pin *per_pin;
  909. struct hdmi_eld *eld;
  910. struct hdmi_spec_per_cvt *per_cvt = NULL;
  911. /* Validate hinfo */
  912. pin_idx = hinfo_to_pin_index(spec, hinfo);
  913. if (snd_BUG_ON(pin_idx < 0))
  914. return -EINVAL;
  915. per_pin = &spec->pins[pin_idx];
  916. eld = &per_pin->sink_eld;
  917. /* Dynamically assign converter to stream */
  918. for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
  919. per_cvt = &spec->cvts[cvt_idx];
  920. /* Must not already be assigned */
  921. if (per_cvt->assigned)
  922. continue;
  923. /* Must be in pin's mux's list of converters */
  924. for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++)
  925. if (per_pin->mux_nids[mux_idx] == per_cvt->cvt_nid)
  926. break;
  927. /* Not in mux list */
  928. if (mux_idx == per_pin->num_mux_nids)
  929. continue;
  930. break;
  931. }
  932. /* No free converters */
  933. if (cvt_idx == spec->num_cvts)
  934. return -ENODEV;
  935. /* Claim converter */
  936. per_cvt->assigned = 1;
  937. hinfo->nid = per_cvt->cvt_nid;
  938. snd_hda_codec_write(codec, per_pin->pin_nid, 0,
  939. AC_VERB_SET_CONNECT_SEL,
  940. mux_idx);
  941. snd_hda_spdif_ctls_assign(codec, pin_idx, per_cvt->cvt_nid);
  942. /* Initially set the converter's capabilities */
  943. hinfo->channels_min = per_cvt->channels_min;
  944. hinfo->channels_max = per_cvt->channels_max;
  945. hinfo->rates = per_cvt->rates;
  946. hinfo->formats = per_cvt->formats;
  947. hinfo->maxbps = per_cvt->maxbps;
  948. /* Restrict capabilities by ELD if this isn't disabled */
  949. if (!static_hdmi_pcm && eld->eld_valid) {
  950. snd_hdmi_eld_update_pcm_info(eld, hinfo);
  951. if (hinfo->channels_min > hinfo->channels_max ||
  952. !hinfo->rates || !hinfo->formats)
  953. return -ENODEV;
  954. }
  955. /* Store the updated parameters */
  956. runtime->hw.channels_min = hinfo->channels_min;
  957. runtime->hw.channels_max = hinfo->channels_max;
  958. runtime->hw.formats = hinfo->formats;
  959. runtime->hw.rates = hinfo->rates;
  960. snd_pcm_hw_constraint_step(substream->runtime, 0,
  961. SNDRV_PCM_HW_PARAM_CHANNELS, 2);
  962. return 0;
  963. }
  964. /*
  965. * HDA/HDMI auto parsing
  966. */
  967. static int hdmi_read_pin_conn(struct hda_codec *codec, int pin_idx)
  968. {
  969. struct hdmi_spec *spec = codec->spec;
  970. struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
  971. hda_nid_t pin_nid = per_pin->pin_nid;
  972. if (!(get_wcaps(codec, pin_nid) & AC_WCAP_CONN_LIST)) {
  973. snd_printk(KERN_WARNING
  974. "HDMI: pin %d wcaps %#x "
  975. "does not support connection list\n",
  976. pin_nid, get_wcaps(codec, pin_nid));
  977. return -EINVAL;
  978. }
  979. per_pin->num_mux_nids = snd_hda_get_connections(codec, pin_nid,
  980. per_pin->mux_nids,
  981. HDA_MAX_CONNECTIONS);
  982. return 0;
  983. }
  984. static void hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll)
  985. {
  986. struct hda_codec *codec = per_pin->codec;
  987. struct hdmi_eld *eld = &per_pin->sink_eld;
  988. hda_nid_t pin_nid = per_pin->pin_nid;
  989. /*
  990. * Always execute a GetPinSense verb here, even when called from
  991. * hdmi_intrinsic_event; for some NVIDIA HW, the unsolicited
  992. * response's PD bit is not the real PD value, but indicates that
  993. * the real PD value changed. An older version of the HD-audio
  994. * specification worked this way. Hence, we just ignore the data in
  995. * the unsolicited response to avoid custom WARs.
  996. */
  997. int present = snd_hda_pin_sense(codec, pin_nid);
  998. bool eld_valid = false;
  999. memset(eld, 0, offsetof(struct hdmi_eld, eld_buffer));
  1000. eld->monitor_present = !!(present & AC_PINSENSE_PRESENCE);
  1001. if (eld->monitor_present)
  1002. eld_valid = !!(present & AC_PINSENSE_ELDV);
  1003. _snd_printd(SND_PR_VERBOSE,
  1004. "HDMI status: Codec=%d Pin=%d Presence_Detect=%d ELD_Valid=%d\n",
  1005. codec->addr, pin_nid, eld->monitor_present, eld_valid);
  1006. if (eld_valid) {
  1007. if (!snd_hdmi_get_eld(eld, codec, pin_nid))
  1008. snd_hdmi_show_eld(eld);
  1009. else if (repoll) {
  1010. queue_delayed_work(codec->bus->workq,
  1011. &per_pin->work,
  1012. msecs_to_jiffies(300));
  1013. }
  1014. }
  1015. }
  1016. static void hdmi_repoll_eld(struct work_struct *work)
  1017. {
  1018. struct hdmi_spec_per_pin *per_pin =
  1019. container_of(to_delayed_work(work), struct hdmi_spec_per_pin, work);
  1020. if (per_pin->repoll_count++ > 6)
  1021. per_pin->repoll_count = 0;
  1022. hdmi_present_sense(per_pin, per_pin->repoll_count);
  1023. }
  1024. static int hdmi_add_pin(struct hda_codec *codec, hda_nid_t pin_nid)
  1025. {
  1026. struct hdmi_spec *spec = codec->spec;
  1027. unsigned int caps, config;
  1028. int pin_idx;
  1029. struct hdmi_spec_per_pin *per_pin;
  1030. int err;
  1031. caps = snd_hda_param_read(codec, pin_nid, AC_PAR_PIN_CAP);
  1032. if (!(caps & (AC_PINCAP_HDMI | AC_PINCAP_DP)))
  1033. return 0;
  1034. config = snd_hda_codec_read(codec, pin_nid, 0,
  1035. AC_VERB_GET_CONFIG_DEFAULT, 0);
  1036. if (get_defcfg_connect(config) == AC_JACK_PORT_NONE)
  1037. return 0;
  1038. if (snd_BUG_ON(spec->num_pins >= MAX_HDMI_PINS))
  1039. return -E2BIG;
  1040. pin_idx = spec->num_pins;
  1041. per_pin = &spec->pins[pin_idx];
  1042. per_pin->pin_nid = pin_nid;
  1043. per_pin->non_pcm = false;
  1044. err = hdmi_read_pin_conn(codec, pin_idx);
  1045. if (err < 0)
  1046. return err;
  1047. spec->num_pins++;
  1048. return 0;
  1049. }
  1050. static int hdmi_add_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
  1051. {
  1052. struct hdmi_spec *spec = codec->spec;
  1053. int cvt_idx;
  1054. struct hdmi_spec_per_cvt *per_cvt;
  1055. unsigned int chans;
  1056. int err;
  1057. if (snd_BUG_ON(spec->num_cvts >= MAX_HDMI_CVTS))
  1058. return -E2BIG;
  1059. chans = get_wcaps(codec, cvt_nid);
  1060. chans = get_wcaps_channels(chans);
  1061. cvt_idx = spec->num_cvts;
  1062. per_cvt = &spec->cvts[cvt_idx];
  1063. per_cvt->cvt_nid = cvt_nid;
  1064. per_cvt->channels_min = 2;
  1065. if (chans <= 16) {
  1066. per_cvt->channels_max = chans;
  1067. if (chans > spec->channels_max)
  1068. spec->channels_max = chans;
  1069. }
  1070. err = snd_hda_query_supported_pcm(codec, cvt_nid,
  1071. &per_cvt->rates,
  1072. &per_cvt->formats,
  1073. &per_cvt->maxbps);
  1074. if (err < 0)
  1075. return err;
  1076. spec->num_cvts++;
  1077. return 0;
  1078. }
  1079. static int hdmi_parse_codec(struct hda_codec *codec)
  1080. {
  1081. hda_nid_t nid;
  1082. int i, nodes;
  1083. nodes = snd_hda_get_sub_nodes(codec, codec->afg, &nid);
  1084. if (!nid || nodes < 0) {
  1085. snd_printk(KERN_WARNING "HDMI: failed to get afg sub nodes\n");
  1086. return -EINVAL;
  1087. }
  1088. for (i = 0; i < nodes; i++, nid++) {
  1089. unsigned int caps;
  1090. unsigned int type;
  1091. caps = snd_hda_param_read(codec, nid, AC_PAR_AUDIO_WIDGET_CAP);
  1092. type = get_wcaps_type(caps);
  1093. if (!(caps & AC_WCAP_DIGITAL))
  1094. continue;
  1095. switch (type) {
  1096. case AC_WID_AUD_OUT:
  1097. hdmi_add_cvt(codec, nid);
  1098. break;
  1099. case AC_WID_PIN:
  1100. hdmi_add_pin(codec, nid);
  1101. break;
  1102. }
  1103. }
  1104. #ifdef CONFIG_PM
  1105. /* We're seeing some problems with unsolicited hot plug events on
  1106. * PantherPoint after S3, if this is not enabled */
  1107. if (codec->vendor_id == 0x80862806)
  1108. codec->bus->power_keep_link_on = 1;
  1109. /*
  1110. * G45/IbexPeak don't support EPSS: the unsolicited pin hot plug event
  1111. * can be lost and presence sense verb will become inaccurate if the
  1112. * HDA link is powered off at hot plug or hw initialization time.
  1113. */
  1114. else if (!(snd_hda_param_read(codec, codec->afg, AC_PAR_POWER_STATE) &
  1115. AC_PWRST_EPSS))
  1116. codec->bus->power_keep_link_on = 1;
  1117. #endif
  1118. return 0;
  1119. }
  1120. /*
  1121. */
  1122. static char *get_hdmi_pcm_name(int idx)
  1123. {
  1124. static char names[MAX_HDMI_PINS][8];
  1125. sprintf(&names[idx][0], "HDMI %d", idx);
  1126. return &names[idx][0];
  1127. }
  1128. static bool check_non_pcm_per_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
  1129. {
  1130. struct hda_spdif_out *spdif;
  1131. bool non_pcm;
  1132. mutex_lock(&codec->spdif_mutex);
  1133. spdif = snd_hda_spdif_out_of_nid(codec, cvt_nid);
  1134. non_pcm = !!(spdif->status & IEC958_AES0_NONAUDIO);
  1135. mutex_unlock(&codec->spdif_mutex);
  1136. return non_pcm;
  1137. }
  1138. /*
  1139. * HDMI callbacks
  1140. */
  1141. static int generic_hdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
  1142. struct hda_codec *codec,
  1143. unsigned int stream_tag,
  1144. unsigned int format,
  1145. struct snd_pcm_substream *substream)
  1146. {
  1147. hda_nid_t cvt_nid = hinfo->nid;
  1148. struct hdmi_spec *spec = codec->spec;
  1149. int pin_idx = hinfo_to_pin_index(spec, hinfo);
  1150. hda_nid_t pin_nid = spec->pins[pin_idx].pin_nid;
  1151. int pinctl;
  1152. bool non_pcm;
  1153. non_pcm = check_non_pcm_per_cvt(codec, cvt_nid);
  1154. hdmi_set_channel_count(codec, cvt_nid, substream->runtime->channels);
  1155. hdmi_setup_audio_infoframe(codec, pin_idx, non_pcm, substream);
  1156. pinctl = snd_hda_codec_read(codec, pin_nid, 0,
  1157. AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
  1158. snd_hda_codec_write(codec, pin_nid, 0,
  1159. AC_VERB_SET_PIN_WIDGET_CONTROL, pinctl | PIN_OUT);
  1160. return hdmi_setup_stream(codec, cvt_nid, pin_nid, stream_tag, format);
  1161. }
  1162. static int generic_hdmi_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
  1163. struct hda_codec *codec,
  1164. struct snd_pcm_substream *substream)
  1165. {
  1166. snd_hda_codec_cleanup_stream(codec, hinfo->nid);
  1167. return 0;
  1168. }
  1169. static int hdmi_pcm_close(struct hda_pcm_stream *hinfo,
  1170. struct hda_codec *codec,
  1171. struct snd_pcm_substream *substream)
  1172. {
  1173. struct hdmi_spec *spec = codec->spec;
  1174. int cvt_idx, pin_idx;
  1175. struct hdmi_spec_per_cvt *per_cvt;
  1176. struct hdmi_spec_per_pin *per_pin;
  1177. int pinctl;
  1178. if (hinfo->nid) {
  1179. cvt_idx = cvt_nid_to_cvt_index(spec, hinfo->nid);
  1180. if (snd_BUG_ON(cvt_idx < 0))
  1181. return -EINVAL;
  1182. per_cvt = &spec->cvts[cvt_idx];
  1183. snd_BUG_ON(!per_cvt->assigned);
  1184. per_cvt->assigned = 0;
  1185. hinfo->nid = 0;
  1186. pin_idx = hinfo_to_pin_index(spec, hinfo);
  1187. if (snd_BUG_ON(pin_idx < 0))
  1188. return -EINVAL;
  1189. per_pin = &spec->pins[pin_idx];
  1190. pinctl = snd_hda_codec_read(codec, per_pin->pin_nid, 0,
  1191. AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
  1192. snd_hda_codec_write(codec, per_pin->pin_nid, 0,
  1193. AC_VERB_SET_PIN_WIDGET_CONTROL,
  1194. pinctl & ~PIN_OUT);
  1195. snd_hda_spdif_ctls_unassign(codec, pin_idx);
  1196. per_pin->chmap_set = false;
  1197. memset(per_pin->chmap, 0, sizeof(per_pin->chmap));
  1198. }
  1199. return 0;
  1200. }
  1201. static const struct hda_pcm_ops generic_ops = {
  1202. .open = hdmi_pcm_open,
  1203. .close = hdmi_pcm_close,
  1204. .prepare = generic_hdmi_playback_pcm_prepare,
  1205. .cleanup = generic_hdmi_playback_pcm_cleanup,
  1206. };
  1207. /*
  1208. * ALSA API channel-map control callbacks
  1209. */
  1210. static int hdmi_chmap_ctl_info(struct snd_kcontrol *kcontrol,
  1211. struct snd_ctl_elem_info *uinfo)
  1212. {
  1213. struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
  1214. struct hda_codec *codec = info->private_data;
  1215. struct hdmi_spec *spec = codec->spec;
  1216. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1217. uinfo->count = spec->channels_max;
  1218. uinfo->value.integer.min = 0;
  1219. uinfo->value.integer.max = SNDRV_CHMAP_LAST;
  1220. return 0;
  1221. }
  1222. static int hdmi_chmap_ctl_tlv(struct snd_kcontrol *kcontrol, int op_flag,
  1223. unsigned int size, unsigned int __user *tlv)
  1224. {
  1225. struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
  1226. struct hda_codec *codec = info->private_data;
  1227. struct hdmi_spec *spec = codec->spec;
  1228. const unsigned int valid_mask =
  1229. FL | FR | RL | RR | LFE | FC | RLC | RRC;
  1230. unsigned int __user *dst;
  1231. int chs, count = 0;
  1232. if (size < 8)
  1233. return -ENOMEM;
  1234. if (put_user(SNDRV_CTL_TLVT_CONTAINER, tlv))
  1235. return -EFAULT;
  1236. size -= 8;
  1237. dst = tlv + 2;
  1238. for (chs = 2; chs <= spec->channels_max; chs++) {
  1239. int i, c;
  1240. struct cea_channel_speaker_allocation *cap;
  1241. cap = channel_allocations;
  1242. for (i = 0; i < ARRAY_SIZE(channel_allocations); i++, cap++) {
  1243. int chs_bytes = chs * 4;
  1244. if (cap->channels != chs)
  1245. continue;
  1246. if (cap->spk_mask & ~valid_mask)
  1247. continue;
  1248. if (size < 8)
  1249. return -ENOMEM;
  1250. if (put_user(SNDRV_CTL_TLVT_CHMAP_VAR, dst) ||
  1251. put_user(chs_bytes, dst + 1))
  1252. return -EFAULT;
  1253. dst += 2;
  1254. size -= 8;
  1255. count += 8;
  1256. if (size < chs_bytes)
  1257. return -ENOMEM;
  1258. size -= chs_bytes;
  1259. count += chs_bytes;
  1260. for (c = 7; c >= 0; c--) {
  1261. int spk = cap->speakers[c];
  1262. if (!spk)
  1263. continue;
  1264. if (put_user(spk_to_chmap(spk), dst))
  1265. return -EFAULT;
  1266. dst++;
  1267. }
  1268. }
  1269. }
  1270. if (put_user(count, tlv + 1))
  1271. return -EFAULT;
  1272. return 0;
  1273. }
  1274. static int hdmi_chmap_ctl_get(struct snd_kcontrol *kcontrol,
  1275. struct snd_ctl_elem_value *ucontrol)
  1276. {
  1277. struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
  1278. struct hda_codec *codec = info->private_data;
  1279. struct hdmi_spec *spec = codec->spec;
  1280. int pin_idx = kcontrol->private_value;
  1281. struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
  1282. int i;
  1283. for (i = 0; i < ARRAY_SIZE(per_pin->chmap); i++)
  1284. ucontrol->value.integer.value[i] = per_pin->chmap[i];
  1285. return 0;
  1286. }
  1287. static int hdmi_chmap_ctl_put(struct snd_kcontrol *kcontrol,
  1288. struct snd_ctl_elem_value *ucontrol)
  1289. {
  1290. struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
  1291. struct hda_codec *codec = info->private_data;
  1292. struct hdmi_spec *spec = codec->spec;
  1293. int pin_idx = kcontrol->private_value;
  1294. struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
  1295. unsigned int ctl_idx;
  1296. struct snd_pcm_substream *substream;
  1297. unsigned char chmap[8];
  1298. int i, ca, prepared = 0;
  1299. ctl_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
  1300. substream = snd_pcm_chmap_substream(info, ctl_idx);
  1301. if (!substream || !substream->runtime)
  1302. return -EBADFD;
  1303. switch (substream->runtime->status->state) {
  1304. case SNDRV_PCM_STATE_OPEN:
  1305. case SNDRV_PCM_STATE_SETUP:
  1306. break;
  1307. case SNDRV_PCM_STATE_PREPARED:
  1308. prepared = 1;
  1309. break;
  1310. default:
  1311. return -EBUSY;
  1312. }
  1313. memset(chmap, 0, sizeof(chmap));
  1314. for (i = 0; i < ARRAY_SIZE(chmap); i++)
  1315. chmap[i] = ucontrol->value.integer.value[i];
  1316. if (!memcmp(chmap, per_pin->chmap, sizeof(chmap)))
  1317. return 0;
  1318. ca = hdmi_manual_channel_allocation(ARRAY_SIZE(chmap), chmap);
  1319. if (ca < 0)
  1320. return -EINVAL;
  1321. per_pin->chmap_set = true;
  1322. memcpy(per_pin->chmap, chmap, sizeof(chmap));
  1323. if (prepared)
  1324. hdmi_setup_audio_infoframe(codec, pin_idx, per_pin->non_pcm,
  1325. substream);
  1326. return 0;
  1327. }
  1328. static int generic_hdmi_build_pcms(struct hda_codec *codec)
  1329. {
  1330. struct hdmi_spec *spec = codec->spec;
  1331. int pin_idx;
  1332. for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
  1333. struct hda_pcm *info;
  1334. struct hda_pcm_stream *pstr;
  1335. info = &spec->pcm_rec[pin_idx];
  1336. info->name = get_hdmi_pcm_name(pin_idx);
  1337. info->pcm_type = HDA_PCM_TYPE_HDMI;
  1338. info->own_chmap = true;
  1339. pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
  1340. pstr->substreams = 1;
  1341. pstr->ops = generic_ops;
  1342. /* other pstr fields are set in open */
  1343. }
  1344. codec->num_pcms = spec->num_pins;
  1345. codec->pcm_info = spec->pcm_rec;
  1346. return 0;
  1347. }
  1348. static int generic_hdmi_build_jack(struct hda_codec *codec, int pin_idx)
  1349. {
  1350. char hdmi_str[32] = "HDMI/DP";
  1351. struct hdmi_spec *spec = codec->spec;
  1352. struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
  1353. int pcmdev = spec->pcm_rec[pin_idx].device;
  1354. if (pcmdev > 0)
  1355. sprintf(hdmi_str + strlen(hdmi_str), ",pcm=%d", pcmdev);
  1356. return snd_hda_jack_add_kctl(codec, per_pin->pin_nid, hdmi_str, 0);
  1357. }
  1358. static int generic_hdmi_build_controls(struct hda_codec *codec)
  1359. {
  1360. struct hdmi_spec *spec = codec->spec;
  1361. int err;
  1362. int pin_idx;
  1363. for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
  1364. struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
  1365. err = generic_hdmi_build_jack(codec, pin_idx);
  1366. if (err < 0)
  1367. return err;
  1368. err = snd_hda_create_dig_out_ctls(codec,
  1369. per_pin->pin_nid,
  1370. per_pin->mux_nids[0],
  1371. HDA_PCM_TYPE_HDMI);
  1372. if (err < 0)
  1373. return err;
  1374. snd_hda_spdif_ctls_unassign(codec, pin_idx);
  1375. /* add control for ELD Bytes */
  1376. err = hdmi_create_eld_ctl(codec,
  1377. pin_idx,
  1378. spec->pcm_rec[pin_idx].device);
  1379. if (err < 0)
  1380. return err;
  1381. hdmi_present_sense(per_pin, 0);
  1382. }
  1383. /* add channel maps */
  1384. for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
  1385. struct snd_pcm_chmap *chmap;
  1386. struct snd_kcontrol *kctl;
  1387. int i;
  1388. err = snd_pcm_add_chmap_ctls(codec->pcm_info[pin_idx].pcm,
  1389. SNDRV_PCM_STREAM_PLAYBACK,
  1390. NULL, 0, pin_idx, &chmap);
  1391. if (err < 0)
  1392. return err;
  1393. /* override handlers */
  1394. chmap->private_data = codec;
  1395. kctl = chmap->kctl;
  1396. for (i = 0; i < kctl->count; i++)
  1397. kctl->vd[i].access |= SNDRV_CTL_ELEM_ACCESS_WRITE;
  1398. kctl->info = hdmi_chmap_ctl_info;
  1399. kctl->get = hdmi_chmap_ctl_get;
  1400. kctl->put = hdmi_chmap_ctl_put;
  1401. kctl->tlv.c = hdmi_chmap_ctl_tlv;
  1402. }
  1403. return 0;
  1404. }
  1405. static int generic_hdmi_init_per_pins(struct hda_codec *codec)
  1406. {
  1407. struct hdmi_spec *spec = codec->spec;
  1408. int pin_idx;
  1409. for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
  1410. struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
  1411. struct hdmi_eld *eld = &per_pin->sink_eld;
  1412. per_pin->codec = codec;
  1413. INIT_DELAYED_WORK(&per_pin->work, hdmi_repoll_eld);
  1414. snd_hda_eld_proc_new(codec, eld, pin_idx);
  1415. }
  1416. return 0;
  1417. }
  1418. static int generic_hdmi_init(struct hda_codec *codec)
  1419. {
  1420. struct hdmi_spec *spec = codec->spec;
  1421. int pin_idx;
  1422. for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
  1423. struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
  1424. hda_nid_t pin_nid = per_pin->pin_nid;
  1425. hdmi_init_pin(codec, pin_nid);
  1426. snd_hda_jack_detect_enable(codec, pin_nid, pin_nid);
  1427. }
  1428. return 0;
  1429. }
  1430. static void generic_hdmi_free(struct hda_codec *codec)
  1431. {
  1432. struct hdmi_spec *spec = codec->spec;
  1433. int pin_idx;
  1434. for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
  1435. struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
  1436. struct hdmi_eld *eld = &per_pin->sink_eld;
  1437. cancel_delayed_work(&per_pin->work);
  1438. snd_hda_eld_proc_free(codec, eld);
  1439. }
  1440. flush_workqueue(codec->bus->workq);
  1441. kfree(spec);
  1442. }
  1443. static const struct hda_codec_ops generic_hdmi_patch_ops = {
  1444. .init = generic_hdmi_init,
  1445. .free = generic_hdmi_free,
  1446. .build_pcms = generic_hdmi_build_pcms,
  1447. .build_controls = generic_hdmi_build_controls,
  1448. .unsol_event = hdmi_unsol_event,
  1449. };
  1450. static int patch_generic_hdmi(struct hda_codec *codec)
  1451. {
  1452. struct hdmi_spec *spec;
  1453. spec = kzalloc(sizeof(*spec), GFP_KERNEL);
  1454. if (spec == NULL)
  1455. return -ENOMEM;
  1456. codec->spec = spec;
  1457. if (hdmi_parse_codec(codec) < 0) {
  1458. codec->spec = NULL;
  1459. kfree(spec);
  1460. return -EINVAL;
  1461. }
  1462. codec->patch_ops = generic_hdmi_patch_ops;
  1463. generic_hdmi_init_per_pins(codec);
  1464. init_channel_allocations();
  1465. return 0;
  1466. }
  1467. /*
  1468. * Shared non-generic implementations
  1469. */
  1470. static int simple_playback_build_pcms(struct hda_codec *codec)
  1471. {
  1472. struct hdmi_spec *spec = codec->spec;
  1473. struct hda_pcm *info = spec->pcm_rec;
  1474. unsigned int chans;
  1475. struct hda_pcm_stream *pstr;
  1476. codec->num_pcms = 1;
  1477. codec->pcm_info = info;
  1478. chans = get_wcaps(codec, spec->cvts[0].cvt_nid);
  1479. chans = get_wcaps_channels(chans);
  1480. info->name = get_hdmi_pcm_name(0);
  1481. info->pcm_type = HDA_PCM_TYPE_HDMI;
  1482. pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
  1483. *pstr = spec->pcm_playback;
  1484. pstr->nid = spec->cvts[0].cvt_nid;
  1485. if (pstr->channels_max <= 2 && chans && chans <= 16)
  1486. pstr->channels_max = chans;
  1487. return 0;
  1488. }
  1489. /* unsolicited event for jack sensing */
  1490. static void simple_hdmi_unsol_event(struct hda_codec *codec,
  1491. unsigned int res)
  1492. {
  1493. snd_hda_jack_set_dirty_all(codec);
  1494. snd_hda_jack_report_sync(codec);
  1495. }
  1496. /* generic_hdmi_build_jack can be used for simple_hdmi, too,
  1497. * as long as spec->pins[] is set correctly
  1498. */
  1499. #define simple_hdmi_build_jack generic_hdmi_build_jack
  1500. static int simple_playback_build_controls(struct hda_codec *codec)
  1501. {
  1502. struct hdmi_spec *spec = codec->spec;
  1503. int err;
  1504. err = snd_hda_create_spdif_out_ctls(codec,
  1505. spec->cvts[0].cvt_nid,
  1506. spec->cvts[0].cvt_nid);
  1507. if (err < 0)
  1508. return err;
  1509. return simple_hdmi_build_jack(codec, 0);
  1510. }
  1511. static int simple_playback_init(struct hda_codec *codec)
  1512. {
  1513. struct hdmi_spec *spec = codec->spec;
  1514. hda_nid_t pin = spec->pins[0].pin_nid;
  1515. snd_hda_codec_write(codec, pin, 0,
  1516. AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
  1517. /* some codecs require to unmute the pin */
  1518. if (get_wcaps(codec, pin) & AC_WCAP_OUT_AMP)
  1519. snd_hda_codec_write(codec, pin, 0, AC_VERB_SET_AMP_GAIN_MUTE,
  1520. AMP_OUT_UNMUTE);
  1521. snd_hda_jack_detect_enable(codec, pin, pin);
  1522. return 0;
  1523. }
  1524. static void simple_playback_free(struct hda_codec *codec)
  1525. {
  1526. struct hdmi_spec *spec = codec->spec;
  1527. kfree(spec);
  1528. }
  1529. /*
  1530. * Nvidia specific implementations
  1531. */
  1532. #define Nv_VERB_SET_Channel_Allocation 0xF79
  1533. #define Nv_VERB_SET_Info_Frame_Checksum 0xF7A
  1534. #define Nv_VERB_SET_Audio_Protection_On 0xF98
  1535. #define Nv_VERB_SET_Audio_Protection_Off 0xF99
  1536. #define nvhdmi_master_con_nid_7x 0x04
  1537. #define nvhdmi_master_pin_nid_7x 0x05
  1538. static const hda_nid_t nvhdmi_con_nids_7x[4] = {
  1539. /*front, rear, clfe, rear_surr */
  1540. 0x6, 0x8, 0xa, 0xc,
  1541. };
  1542. static const struct hda_verb nvhdmi_basic_init_7x_2ch[] = {
  1543. /* set audio protect on */
  1544. { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
  1545. /* enable digital output on pin widget */
  1546. { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
  1547. {} /* terminator */
  1548. };
  1549. static const struct hda_verb nvhdmi_basic_init_7x_8ch[] = {
  1550. /* set audio protect on */
  1551. { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
  1552. /* enable digital output on pin widget */
  1553. { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
  1554. { 0x7, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
  1555. { 0x9, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
  1556. { 0xb, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
  1557. { 0xd, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
  1558. {} /* terminator */
  1559. };
  1560. #ifdef LIMITED_RATE_FMT_SUPPORT
  1561. /* support only the safe format and rate */
  1562. #define SUPPORTED_RATES SNDRV_PCM_RATE_48000
  1563. #define SUPPORTED_MAXBPS 16
  1564. #define SUPPORTED_FORMATS SNDRV_PCM_FMTBIT_S16_LE
  1565. #else
  1566. /* support all rates and formats */
  1567. #define SUPPORTED_RATES \
  1568. (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
  1569. SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |\
  1570. SNDRV_PCM_RATE_192000)
  1571. #define SUPPORTED_MAXBPS 24
  1572. #define SUPPORTED_FORMATS \
  1573. (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE)
  1574. #endif
  1575. static int nvhdmi_7x_init_2ch(struct hda_codec *codec)
  1576. {
  1577. snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_2ch);
  1578. return 0;
  1579. }
  1580. static int nvhdmi_7x_init_8ch(struct hda_codec *codec)
  1581. {
  1582. snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_8ch);
  1583. return 0;
  1584. }
  1585. static unsigned int channels_2_6_8[] = {
  1586. 2, 6, 8
  1587. };
  1588. static unsigned int channels_2_8[] = {
  1589. 2, 8
  1590. };
  1591. static struct snd_pcm_hw_constraint_list hw_constraints_2_6_8_channels = {
  1592. .count = ARRAY_SIZE(channels_2_6_8),
  1593. .list = channels_2_6_8,
  1594. .mask = 0,
  1595. };
  1596. static struct snd_pcm_hw_constraint_list hw_constraints_2_8_channels = {
  1597. .count = ARRAY_SIZE(channels_2_8),
  1598. .list = channels_2_8,
  1599. .mask = 0,
  1600. };
  1601. static int simple_playback_pcm_open(struct hda_pcm_stream *hinfo,
  1602. struct hda_codec *codec,
  1603. struct snd_pcm_substream *substream)
  1604. {
  1605. struct hdmi_spec *spec = codec->spec;
  1606. struct snd_pcm_hw_constraint_list *hw_constraints_channels = NULL;
  1607. switch (codec->preset->id) {
  1608. case 0x10de0002:
  1609. case 0x10de0003:
  1610. case 0x10de0005:
  1611. case 0x10de0006:
  1612. hw_constraints_channels = &hw_constraints_2_8_channels;
  1613. break;
  1614. case 0x10de0007:
  1615. hw_constraints_channels = &hw_constraints_2_6_8_channels;
  1616. break;
  1617. default:
  1618. break;
  1619. }
  1620. if (hw_constraints_channels != NULL) {
  1621. snd_pcm_hw_constraint_list(substream->runtime, 0,
  1622. SNDRV_PCM_HW_PARAM_CHANNELS,
  1623. hw_constraints_channels);
  1624. } else {
  1625. snd_pcm_hw_constraint_step(substream->runtime, 0,
  1626. SNDRV_PCM_HW_PARAM_CHANNELS, 2);
  1627. }
  1628. return snd_hda_multi_out_dig_open(codec, &spec->multiout);
  1629. }
  1630. static int simple_playback_pcm_close(struct hda_pcm_stream *hinfo,
  1631. struct hda_codec *codec,
  1632. struct snd_pcm_substream *substream)
  1633. {
  1634. struct hdmi_spec *spec = codec->spec;
  1635. return snd_hda_multi_out_dig_close(codec, &spec->multiout);
  1636. }
  1637. static int simple_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
  1638. struct hda_codec *codec,
  1639. unsigned int stream_tag,
  1640. unsigned int format,
  1641. struct snd_pcm_substream *substream)
  1642. {
  1643. struct hdmi_spec *spec = codec->spec;
  1644. return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
  1645. stream_tag, format, substream);
  1646. }
  1647. static const struct hda_pcm_stream simple_pcm_playback = {
  1648. .substreams = 1,
  1649. .channels_min = 2,
  1650. .channels_max = 2,
  1651. .ops = {
  1652. .open = simple_playback_pcm_open,
  1653. .close = simple_playback_pcm_close,
  1654. .prepare = simple_playback_pcm_prepare
  1655. },
  1656. };
  1657. static const struct hda_codec_ops simple_hdmi_patch_ops = {
  1658. .build_controls = simple_playback_build_controls,
  1659. .build_pcms = simple_playback_build_pcms,
  1660. .init = simple_playback_init,
  1661. .free = simple_playback_free,
  1662. .unsol_event = simple_hdmi_unsol_event,
  1663. };
  1664. static int patch_simple_hdmi(struct hda_codec *codec,
  1665. hda_nid_t cvt_nid, hda_nid_t pin_nid)
  1666. {
  1667. struct hdmi_spec *spec;
  1668. spec = kzalloc(sizeof(*spec), GFP_KERNEL);
  1669. if (!spec)
  1670. return -ENOMEM;
  1671. codec->spec = spec;
  1672. spec->multiout.num_dacs = 0; /* no analog */
  1673. spec->multiout.max_channels = 2;
  1674. spec->multiout.dig_out_nid = cvt_nid;
  1675. spec->num_cvts = 1;
  1676. spec->num_pins = 1;
  1677. spec->cvts[0].cvt_nid = cvt_nid;
  1678. spec->pins[0].pin_nid = pin_nid;
  1679. spec->pcm_playback = simple_pcm_playback;
  1680. codec->patch_ops = simple_hdmi_patch_ops;
  1681. return 0;
  1682. }
  1683. static void nvhdmi_8ch_7x_set_info_frame_parameters(struct hda_codec *codec,
  1684. int channels)
  1685. {
  1686. unsigned int chanmask;
  1687. int chan = channels ? (channels - 1) : 1;
  1688. switch (channels) {
  1689. default:
  1690. case 0:
  1691. case 2:
  1692. chanmask = 0x00;
  1693. break;
  1694. case 4:
  1695. chanmask = 0x08;
  1696. break;
  1697. case 6:
  1698. chanmask = 0x0b;
  1699. break;
  1700. case 8:
  1701. chanmask = 0x13;
  1702. break;
  1703. }
  1704. /* Set the audio infoframe channel allocation and checksum fields. The
  1705. * channel count is computed implicitly by the hardware. */
  1706. snd_hda_codec_write(codec, 0x1, 0,
  1707. Nv_VERB_SET_Channel_Allocation, chanmask);
  1708. snd_hda_codec_write(codec, 0x1, 0,
  1709. Nv_VERB_SET_Info_Frame_Checksum,
  1710. (0x71 - chan - chanmask));
  1711. }
  1712. static int nvhdmi_8ch_7x_pcm_close(struct hda_pcm_stream *hinfo,
  1713. struct hda_codec *codec,
  1714. struct snd_pcm_substream *substream)
  1715. {
  1716. struct hdmi_spec *spec = codec->spec;
  1717. int i;
  1718. snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x,
  1719. 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
  1720. for (i = 0; i < 4; i++) {
  1721. /* set the stream id */
  1722. snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
  1723. AC_VERB_SET_CHANNEL_STREAMID, 0);
  1724. /* set the stream format */
  1725. snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
  1726. AC_VERB_SET_STREAM_FORMAT, 0);
  1727. }
  1728. /* The audio hardware sends a channel count of 0x7 (8ch) when all the
  1729. * streams are disabled. */
  1730. nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
  1731. return snd_hda_multi_out_dig_close(codec, &spec->multiout);
  1732. }
  1733. static int nvhdmi_8ch_7x_pcm_prepare(struct hda_pcm_stream *hinfo,
  1734. struct hda_codec *codec,
  1735. unsigned int stream_tag,
  1736. unsigned int format,
  1737. struct snd_pcm_substream *substream)
  1738. {
  1739. int chs;
  1740. unsigned int dataDCC2, channel_id;
  1741. int i;
  1742. struct hdmi_spec *spec = codec->spec;
  1743. struct hda_spdif_out *spdif;
  1744. mutex_lock(&codec->spdif_mutex);
  1745. spdif = snd_hda_spdif_out_of_nid(codec, spec->cvts[0].cvt_nid);
  1746. chs = substream->runtime->channels;
  1747. dataDCC2 = 0x2;
  1748. /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
  1749. if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
  1750. snd_hda_codec_write(codec,
  1751. nvhdmi_master_con_nid_7x,
  1752. 0,
  1753. AC_VERB_SET_DIGI_CONVERT_1,
  1754. spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
  1755. /* set the stream id */
  1756. snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
  1757. AC_VERB_SET_CHANNEL_STREAMID, (stream_tag << 4) | 0x0);
  1758. /* set the stream format */
  1759. snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
  1760. AC_VERB_SET_STREAM_FORMAT, format);
  1761. /* turn on again (if needed) */
  1762. /* enable and set the channel status audio/data flag */
  1763. if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE)) {
  1764. snd_hda_codec_write(codec,
  1765. nvhdmi_master_con_nid_7x,
  1766. 0,
  1767. AC_VERB_SET_DIGI_CONVERT_1,
  1768. spdif->ctls & 0xff);
  1769. snd_hda_codec_write(codec,
  1770. nvhdmi_master_con_nid_7x,
  1771. 0,
  1772. AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
  1773. }
  1774. for (i = 0; i < 4; i++) {
  1775. if (chs == 2)
  1776. channel_id = 0;
  1777. else
  1778. channel_id = i * 2;
  1779. /* turn off SPDIF once;
  1780. *otherwise the IEC958 bits won't be updated
  1781. */
  1782. if (codec->spdif_status_reset &&
  1783. (spdif->ctls & AC_DIG1_ENABLE))
  1784. snd_hda_codec_write(codec,
  1785. nvhdmi_con_nids_7x[i],
  1786. 0,
  1787. AC_VERB_SET_DIGI_CONVERT_1,
  1788. spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
  1789. /* set the stream id */
  1790. snd_hda_codec_write(codec,
  1791. nvhdmi_con_nids_7x[i],
  1792. 0,
  1793. AC_VERB_SET_CHANNEL_STREAMID,
  1794. (stream_tag << 4) | channel_id);
  1795. /* set the stream format */
  1796. snd_hda_codec_write(codec,
  1797. nvhdmi_con_nids_7x[i],
  1798. 0,
  1799. AC_VERB_SET_STREAM_FORMAT,
  1800. format);
  1801. /* turn on again (if needed) */
  1802. /* enable and set the channel status audio/data flag */
  1803. if (codec->spdif_status_reset &&
  1804. (spdif->ctls & AC_DIG1_ENABLE)) {
  1805. snd_hda_codec_write(codec,
  1806. nvhdmi_con_nids_7x[i],
  1807. 0,
  1808. AC_VERB_SET_DIGI_CONVERT_1,
  1809. spdif->ctls & 0xff);
  1810. snd_hda_codec_write(codec,
  1811. nvhdmi_con_nids_7x[i],
  1812. 0,
  1813. AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
  1814. }
  1815. }
  1816. nvhdmi_8ch_7x_set_info_frame_parameters(codec, chs);
  1817. mutex_unlock(&codec->spdif_mutex);
  1818. return 0;
  1819. }
  1820. static const struct hda_pcm_stream nvhdmi_pcm_playback_8ch_7x = {
  1821. .substreams = 1,
  1822. .channels_min = 2,
  1823. .channels_max = 8,
  1824. .nid = nvhdmi_master_con_nid_7x,
  1825. .rates = SUPPORTED_RATES,
  1826. .maxbps = SUPPORTED_MAXBPS,
  1827. .formats = SUPPORTED_FORMATS,
  1828. .ops = {
  1829. .open = simple_playback_pcm_open,
  1830. .close = nvhdmi_8ch_7x_pcm_close,
  1831. .prepare = nvhdmi_8ch_7x_pcm_prepare
  1832. },
  1833. };
  1834. static int patch_nvhdmi_2ch(struct hda_codec *codec)
  1835. {
  1836. struct hdmi_spec *spec;
  1837. int err = patch_simple_hdmi(codec, nvhdmi_master_con_nid_7x,
  1838. nvhdmi_master_pin_nid_7x);
  1839. if (err < 0)
  1840. return err;
  1841. codec->patch_ops.init = nvhdmi_7x_init_2ch;
  1842. /* override the PCM rates, etc, as the codec doesn't give full list */
  1843. spec = codec->spec;
  1844. spec->pcm_playback.rates = SUPPORTED_RATES;
  1845. spec->pcm_playback.maxbps = SUPPORTED_MAXBPS;
  1846. spec->pcm_playback.formats = SUPPORTED_FORMATS;
  1847. return 0;
  1848. }
  1849. static int nvhdmi_7x_8ch_build_pcms(struct hda_codec *codec)
  1850. {
  1851. struct hdmi_spec *spec = codec->spec;
  1852. int err = simple_playback_build_pcms(codec);
  1853. spec->pcm_rec[0].own_chmap = true;
  1854. return err;
  1855. }
  1856. static int nvhdmi_7x_8ch_build_controls(struct hda_codec *codec)
  1857. {
  1858. struct hdmi_spec *spec = codec->spec;
  1859. struct snd_pcm_chmap *chmap;
  1860. int err;
  1861. err = simple_playback_build_controls(codec);
  1862. if (err < 0)
  1863. return err;
  1864. /* add channel maps */
  1865. err = snd_pcm_add_chmap_ctls(spec->pcm_rec[0].pcm,
  1866. SNDRV_PCM_STREAM_PLAYBACK,
  1867. snd_pcm_alt_chmaps, 8, 0, &chmap);
  1868. if (err < 0)
  1869. return err;
  1870. switch (codec->preset->id) {
  1871. case 0x10de0002:
  1872. case 0x10de0003:
  1873. case 0x10de0005:
  1874. case 0x10de0006:
  1875. chmap->channel_mask = (1U << 2) | (1U << 8);
  1876. break;
  1877. case 0x10de0007:
  1878. chmap->channel_mask = (1U << 2) | (1U << 6) | (1U << 8);
  1879. }
  1880. return 0;
  1881. }
  1882. static int patch_nvhdmi_8ch_7x(struct hda_codec *codec)
  1883. {
  1884. struct hdmi_spec *spec;
  1885. int err = patch_nvhdmi_2ch(codec);
  1886. if (err < 0)
  1887. return err;
  1888. spec = codec->spec;
  1889. spec->multiout.max_channels = 8;
  1890. spec->pcm_playback = nvhdmi_pcm_playback_8ch_7x;
  1891. codec->patch_ops.init = nvhdmi_7x_init_8ch;
  1892. codec->patch_ops.build_pcms = nvhdmi_7x_8ch_build_pcms;
  1893. codec->patch_ops.build_controls = nvhdmi_7x_8ch_build_controls;
  1894. /* Initialize the audio infoframe channel mask and checksum to something
  1895. * valid */
  1896. nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
  1897. return 0;
  1898. }
  1899. /*
  1900. * ATI-specific implementations
  1901. *
  1902. * FIXME: we may omit the whole this and use the generic code once after
  1903. * it's confirmed to work.
  1904. */
  1905. #define ATIHDMI_CVT_NID 0x02 /* audio converter */
  1906. #define ATIHDMI_PIN_NID 0x03 /* HDMI output pin */
  1907. static int atihdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
  1908. struct hda_codec *codec,
  1909. unsigned int stream_tag,
  1910. unsigned int format,
  1911. struct snd_pcm_substream *substream)
  1912. {
  1913. struct hdmi_spec *spec = codec->spec;
  1914. int chans = substream->runtime->channels;
  1915. int i, err;
  1916. err = simple_playback_pcm_prepare(hinfo, codec, stream_tag, format,
  1917. substream);
  1918. if (err < 0)
  1919. return err;
  1920. snd_hda_codec_write(codec, spec->cvts[0].cvt_nid, 0,
  1921. AC_VERB_SET_CVT_CHAN_COUNT, chans - 1);
  1922. /* FIXME: XXX */
  1923. for (i = 0; i < chans; i++) {
  1924. snd_hda_codec_write(codec, spec->cvts[0].cvt_nid, 0,
  1925. AC_VERB_SET_HDMI_CHAN_SLOT,
  1926. (i << 4) | i);
  1927. }
  1928. return 0;
  1929. }
  1930. static int patch_atihdmi(struct hda_codec *codec)
  1931. {
  1932. struct hdmi_spec *spec;
  1933. int err = patch_simple_hdmi(codec, ATIHDMI_CVT_NID, ATIHDMI_PIN_NID);
  1934. if (err < 0)
  1935. return err;
  1936. spec = codec->spec;
  1937. spec->pcm_playback.ops.prepare = atihdmi_playback_pcm_prepare;
  1938. return 0;
  1939. }
  1940. /* VIA HDMI Implementation */
  1941. #define VIAHDMI_CVT_NID 0x02 /* audio converter1 */
  1942. #define VIAHDMI_PIN_NID 0x03 /* HDMI output pin1 */
  1943. static int patch_via_hdmi(struct hda_codec *codec)
  1944. {
  1945. return patch_simple_hdmi(codec, VIAHDMI_CVT_NID, VIAHDMI_PIN_NID);
  1946. }
  1947. /*
  1948. * patch entries
  1949. */
  1950. static const struct hda_codec_preset snd_hda_preset_hdmi[] = {
  1951. { .id = 0x1002793c, .name = "RS600 HDMI", .patch = patch_atihdmi },
  1952. { .id = 0x10027919, .name = "RS600 HDMI", .patch = patch_atihdmi },
  1953. { .id = 0x1002791a, .name = "RS690/780 HDMI", .patch = patch_atihdmi },
  1954. { .id = 0x1002aa01, .name = "R6xx HDMI", .patch = patch_generic_hdmi },
  1955. { .id = 0x10951390, .name = "SiI1390 HDMI", .patch = patch_generic_hdmi },
  1956. { .id = 0x10951392, .name = "SiI1392 HDMI", .patch = patch_generic_hdmi },
  1957. { .id = 0x17e80047, .name = "Chrontel HDMI", .patch = patch_generic_hdmi },
  1958. { .id = 0x10de0002, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
  1959. { .id = 0x10de0003, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
  1960. { .id = 0x10de0005, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
  1961. { .id = 0x10de0006, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
  1962. { .id = 0x10de0007, .name = "MCP79/7A HDMI", .patch = patch_nvhdmi_8ch_7x },
  1963. { .id = 0x10de000a, .name = "GPU 0a HDMI/DP", .patch = patch_generic_hdmi },
  1964. { .id = 0x10de000b, .name = "GPU 0b HDMI/DP", .patch = patch_generic_hdmi },
  1965. { .id = 0x10de000c, .name = "MCP89 HDMI", .patch = patch_generic_hdmi },
  1966. { .id = 0x10de000d, .name = "GPU 0d HDMI/DP", .patch = patch_generic_hdmi },
  1967. { .id = 0x10de0010, .name = "GPU 10 HDMI/DP", .patch = patch_generic_hdmi },
  1968. { .id = 0x10de0011, .name = "GPU 11 HDMI/DP", .patch = patch_generic_hdmi },
  1969. { .id = 0x10de0012, .name = "GPU 12 HDMI/DP", .patch = patch_generic_hdmi },
  1970. { .id = 0x10de0013, .name = "GPU 13 HDMI/DP", .patch = patch_generic_hdmi },
  1971. { .id = 0x10de0014, .name = "GPU 14 HDMI/DP", .patch = patch_generic_hdmi },
  1972. { .id = 0x10de0015, .name = "GPU 15 HDMI/DP", .patch = patch_generic_hdmi },
  1973. { .id = 0x10de0016, .name = "GPU 16 HDMI/DP", .patch = patch_generic_hdmi },
  1974. /* 17 is known to be absent */
  1975. { .id = 0x10de0018, .name = "GPU 18 HDMI/DP", .patch = patch_generic_hdmi },
  1976. { .id = 0x10de0019, .name = "GPU 19 HDMI/DP", .patch = patch_generic_hdmi },
  1977. { .id = 0x10de001a, .name = "GPU 1a HDMI/DP", .patch = patch_generic_hdmi },
  1978. { .id = 0x10de001b, .name = "GPU 1b HDMI/DP", .patch = patch_generic_hdmi },
  1979. { .id = 0x10de001c, .name = "GPU 1c HDMI/DP", .patch = patch_generic_hdmi },
  1980. { .id = 0x10de0040, .name = "GPU 40 HDMI/DP", .patch = patch_generic_hdmi },
  1981. { .id = 0x10de0041, .name = "GPU 41 HDMI/DP", .patch = patch_generic_hdmi },
  1982. { .id = 0x10de0042, .name = "GPU 42 HDMI/DP", .patch = patch_generic_hdmi },
  1983. { .id = 0x10de0043, .name = "GPU 43 HDMI/DP", .patch = patch_generic_hdmi },
  1984. { .id = 0x10de0044, .name = "GPU 44 HDMI/DP", .patch = patch_generic_hdmi },
  1985. { .id = 0x10de0051, .name = "GPU 51 HDMI/DP", .patch = patch_generic_hdmi },
  1986. { .id = 0x10de0067, .name = "MCP67 HDMI", .patch = patch_nvhdmi_2ch },
  1987. { .id = 0x10de8001, .name = "MCP73 HDMI", .patch = patch_nvhdmi_2ch },
  1988. { .id = 0x11069f80, .name = "VX900 HDMI/DP", .patch = patch_via_hdmi },
  1989. { .id = 0x11069f81, .name = "VX900 HDMI/DP", .patch = patch_via_hdmi },
  1990. { .id = 0x11069f84, .name = "VX11 HDMI/DP", .patch = patch_generic_hdmi },
  1991. { .id = 0x11069f85, .name = "VX11 HDMI/DP", .patch = patch_generic_hdmi },
  1992. { .id = 0x80860054, .name = "IbexPeak HDMI", .patch = patch_generic_hdmi },
  1993. { .id = 0x80862801, .name = "Bearlake HDMI", .patch = patch_generic_hdmi },
  1994. { .id = 0x80862802, .name = "Cantiga HDMI", .patch = patch_generic_hdmi },
  1995. { .id = 0x80862803, .name = "Eaglelake HDMI", .patch = patch_generic_hdmi },
  1996. { .id = 0x80862804, .name = "IbexPeak HDMI", .patch = patch_generic_hdmi },
  1997. { .id = 0x80862805, .name = "CougarPoint HDMI", .patch = patch_generic_hdmi },
  1998. { .id = 0x80862806, .name = "PantherPoint HDMI", .patch = patch_generic_hdmi },
  1999. { .id = 0x80862807, .name = "Haswell HDMI", .patch = patch_generic_hdmi },
  2000. { .id = 0x80862880, .name = "CedarTrail HDMI", .patch = patch_generic_hdmi },
  2001. { .id = 0x808629fb, .name = "Crestline HDMI", .patch = patch_generic_hdmi },
  2002. {} /* terminator */
  2003. };
  2004. MODULE_ALIAS("snd-hda-codec-id:1002793c");
  2005. MODULE_ALIAS("snd-hda-codec-id:10027919");
  2006. MODULE_ALIAS("snd-hda-codec-id:1002791a");
  2007. MODULE_ALIAS("snd-hda-codec-id:1002aa01");
  2008. MODULE_ALIAS("snd-hda-codec-id:10951390");
  2009. MODULE_ALIAS("snd-hda-codec-id:10951392");
  2010. MODULE_ALIAS("snd-hda-codec-id:10de0002");
  2011. MODULE_ALIAS("snd-hda-codec-id:10de0003");
  2012. MODULE_ALIAS("snd-hda-codec-id:10de0005");
  2013. MODULE_ALIAS("snd-hda-codec-id:10de0006");
  2014. MODULE_ALIAS("snd-hda-codec-id:10de0007");
  2015. MODULE_ALIAS("snd-hda-codec-id:10de000a");
  2016. MODULE_ALIAS("snd-hda-codec-id:10de000b");
  2017. MODULE_ALIAS("snd-hda-codec-id:10de000c");
  2018. MODULE_ALIAS("snd-hda-codec-id:10de000d");
  2019. MODULE_ALIAS("snd-hda-codec-id:10de0010");
  2020. MODULE_ALIAS("snd-hda-codec-id:10de0011");
  2021. MODULE_ALIAS("snd-hda-codec-id:10de0012");
  2022. MODULE_ALIAS("snd-hda-codec-id:10de0013");
  2023. MODULE_ALIAS("snd-hda-codec-id:10de0014");
  2024. MODULE_ALIAS("snd-hda-codec-id:10de0015");
  2025. MODULE_ALIAS("snd-hda-codec-id:10de0016");
  2026. MODULE_ALIAS("snd-hda-codec-id:10de0018");
  2027. MODULE_ALIAS("snd-hda-codec-id:10de0019");
  2028. MODULE_ALIAS("snd-hda-codec-id:10de001a");
  2029. MODULE_ALIAS("snd-hda-codec-id:10de001b");
  2030. MODULE_ALIAS("snd-hda-codec-id:10de001c");
  2031. MODULE_ALIAS("snd-hda-codec-id:10de0040");
  2032. MODULE_ALIAS("snd-hda-codec-id:10de0041");
  2033. MODULE_ALIAS("snd-hda-codec-id:10de0042");
  2034. MODULE_ALIAS("snd-hda-codec-id:10de0043");
  2035. MODULE_ALIAS("snd-hda-codec-id:10de0044");
  2036. MODULE_ALIAS("snd-hda-codec-id:10de0051");
  2037. MODULE_ALIAS("snd-hda-codec-id:10de0067");
  2038. MODULE_ALIAS("snd-hda-codec-id:10de8001");
  2039. MODULE_ALIAS("snd-hda-codec-id:11069f80");
  2040. MODULE_ALIAS("snd-hda-codec-id:11069f81");
  2041. MODULE_ALIAS("snd-hda-codec-id:11069f84");
  2042. MODULE_ALIAS("snd-hda-codec-id:11069f85");
  2043. MODULE_ALIAS("snd-hda-codec-id:17e80047");
  2044. MODULE_ALIAS("snd-hda-codec-id:80860054");
  2045. MODULE_ALIAS("snd-hda-codec-id:80862801");
  2046. MODULE_ALIAS("snd-hda-codec-id:80862802");
  2047. MODULE_ALIAS("snd-hda-codec-id:80862803");
  2048. MODULE_ALIAS("snd-hda-codec-id:80862804");
  2049. MODULE_ALIAS("snd-hda-codec-id:80862805");
  2050. MODULE_ALIAS("snd-hda-codec-id:80862806");
  2051. MODULE_ALIAS("snd-hda-codec-id:80862807");
  2052. MODULE_ALIAS("snd-hda-codec-id:80862880");
  2053. MODULE_ALIAS("snd-hda-codec-id:808629fb");
  2054. MODULE_LICENSE("GPL");
  2055. MODULE_DESCRIPTION("HDMI HD-audio codec");
  2056. MODULE_ALIAS("snd-hda-codec-intelhdmi");
  2057. MODULE_ALIAS("snd-hda-codec-nvhdmi");
  2058. MODULE_ALIAS("snd-hda-codec-atihdmi");
  2059. static struct hda_codec_preset_list intel_list = {
  2060. .preset = snd_hda_preset_hdmi,
  2061. .owner = THIS_MODULE,
  2062. };
  2063. static int __init patch_hdmi_init(void)
  2064. {
  2065. return snd_hda_add_codec_preset(&intel_list);
  2066. }
  2067. static void __exit patch_hdmi_exit(void)
  2068. {
  2069. snd_hda_delete_codec_preset(&intel_list);
  2070. }
  2071. module_init(patch_hdmi_init)
  2072. module_exit(patch_hdmi_exit)