patch_hdmi.c 56 KB

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