patch_hdmi.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007
  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 hdmi_pcm_close(struct hda_pcm_stream *hinfo,
  985. struct hda_codec *codec,
  986. struct snd_pcm_substream *substream)
  987. {
  988. struct hdmi_spec *spec = codec->spec;
  989. int cvt_idx, pin_idx;
  990. struct hdmi_spec_per_cvt *per_cvt;
  991. struct hdmi_spec_per_pin *per_pin;
  992. int pinctl;
  993. if (hinfo->nid) {
  994. cvt_idx = cvt_nid_to_cvt_index(spec, hinfo->nid);
  995. if (snd_BUG_ON(cvt_idx < 0))
  996. return -EINVAL;
  997. per_cvt = &spec->cvts[cvt_idx];
  998. snd_BUG_ON(!per_cvt->assigned);
  999. per_cvt->assigned = 0;
  1000. hinfo->nid = 0;
  1001. pin_idx = hinfo_to_pin_index(spec, hinfo);
  1002. if (snd_BUG_ON(pin_idx < 0))
  1003. return -EINVAL;
  1004. per_pin = &spec->pins[pin_idx];
  1005. pinctl = snd_hda_codec_read(codec, per_pin->pin_nid, 0,
  1006. AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
  1007. snd_hda_codec_write(codec, per_pin->pin_nid, 0,
  1008. AC_VERB_SET_PIN_WIDGET_CONTROL,
  1009. pinctl & ~PIN_OUT);
  1010. snd_hda_spdif_ctls_unassign(codec, pin_idx);
  1011. }
  1012. return 0;
  1013. }
  1014. static const struct hda_pcm_ops generic_ops = {
  1015. .open = hdmi_pcm_open,
  1016. .close = hdmi_pcm_close,
  1017. .prepare = generic_hdmi_playback_pcm_prepare,
  1018. };
  1019. static int generic_hdmi_build_pcms(struct hda_codec *codec)
  1020. {
  1021. struct hdmi_spec *spec = codec->spec;
  1022. int pin_idx;
  1023. for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
  1024. struct hda_pcm *info;
  1025. struct hda_pcm_stream *pstr;
  1026. info = &spec->pcm_rec[pin_idx];
  1027. info->name = get_hdmi_pcm_name(pin_idx);
  1028. info->pcm_type = HDA_PCM_TYPE_HDMI;
  1029. pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
  1030. pstr->substreams = 1;
  1031. pstr->ops = generic_ops;
  1032. /* other pstr fields are set in open */
  1033. }
  1034. codec->num_pcms = spec->num_pins;
  1035. codec->pcm_info = spec->pcm_rec;
  1036. return 0;
  1037. }
  1038. static int generic_hdmi_build_jack(struct hda_codec *codec, int pin_idx)
  1039. {
  1040. char hdmi_str[32] = "HDMI/DP";
  1041. struct hdmi_spec *spec = codec->spec;
  1042. struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
  1043. int pcmdev = spec->pcm_rec[pin_idx].device;
  1044. if (pcmdev > 0)
  1045. sprintf(hdmi_str + strlen(hdmi_str), ",pcm=%d", pcmdev);
  1046. return snd_hda_jack_add_kctl(codec, per_pin->pin_nid, hdmi_str, 0);
  1047. }
  1048. static int generic_hdmi_build_controls(struct hda_codec *codec)
  1049. {
  1050. struct hdmi_spec *spec = codec->spec;
  1051. int err;
  1052. int pin_idx;
  1053. for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
  1054. struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
  1055. err = generic_hdmi_build_jack(codec, pin_idx);
  1056. if (err < 0)
  1057. return err;
  1058. err = snd_hda_create_spdif_out_ctls(codec,
  1059. per_pin->pin_nid,
  1060. per_pin->mux_nids[0]);
  1061. if (err < 0)
  1062. return err;
  1063. snd_hda_spdif_ctls_unassign(codec, pin_idx);
  1064. /* add control for ELD Bytes */
  1065. err = hdmi_create_eld_ctl(codec,
  1066. pin_idx,
  1067. spec->pcm_rec[pin_idx].device);
  1068. if (err < 0)
  1069. return err;
  1070. hdmi_present_sense(per_pin, 0);
  1071. }
  1072. return 0;
  1073. }
  1074. static int generic_hdmi_init_per_pins(struct hda_codec *codec)
  1075. {
  1076. struct hdmi_spec *spec = codec->spec;
  1077. int pin_idx;
  1078. for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
  1079. struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
  1080. struct hdmi_eld *eld = &per_pin->sink_eld;
  1081. per_pin->codec = codec;
  1082. INIT_DELAYED_WORK(&per_pin->work, hdmi_repoll_eld);
  1083. snd_hda_eld_proc_new(codec, eld, pin_idx);
  1084. }
  1085. return 0;
  1086. }
  1087. static int generic_hdmi_init(struct hda_codec *codec)
  1088. {
  1089. struct hdmi_spec *spec = codec->spec;
  1090. int pin_idx;
  1091. for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
  1092. struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
  1093. hda_nid_t pin_nid = per_pin->pin_nid;
  1094. hdmi_init_pin(codec, pin_nid);
  1095. snd_hda_jack_detect_enable(codec, pin_nid, pin_nid);
  1096. }
  1097. snd_hda_jack_report_sync(codec);
  1098. return 0;
  1099. }
  1100. static void generic_hdmi_free(struct hda_codec *codec)
  1101. {
  1102. struct hdmi_spec *spec = codec->spec;
  1103. int pin_idx;
  1104. for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
  1105. struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
  1106. struct hdmi_eld *eld = &per_pin->sink_eld;
  1107. cancel_delayed_work(&per_pin->work);
  1108. snd_hda_eld_proc_free(codec, eld);
  1109. }
  1110. flush_workqueue(codec->bus->workq);
  1111. kfree(spec);
  1112. }
  1113. static const struct hda_codec_ops generic_hdmi_patch_ops = {
  1114. .init = generic_hdmi_init,
  1115. .free = generic_hdmi_free,
  1116. .build_pcms = generic_hdmi_build_pcms,
  1117. .build_controls = generic_hdmi_build_controls,
  1118. .unsol_event = hdmi_unsol_event,
  1119. };
  1120. static int patch_generic_hdmi(struct hda_codec *codec)
  1121. {
  1122. struct hdmi_spec *spec;
  1123. spec = kzalloc(sizeof(*spec), GFP_KERNEL);
  1124. if (spec == NULL)
  1125. return -ENOMEM;
  1126. codec->spec = spec;
  1127. if (hdmi_parse_codec(codec) < 0) {
  1128. codec->spec = NULL;
  1129. kfree(spec);
  1130. return -EINVAL;
  1131. }
  1132. codec->patch_ops = generic_hdmi_patch_ops;
  1133. generic_hdmi_init_per_pins(codec);
  1134. init_channel_allocations();
  1135. return 0;
  1136. }
  1137. /*
  1138. * Shared non-generic implementations
  1139. */
  1140. static int simple_playback_build_pcms(struct hda_codec *codec)
  1141. {
  1142. struct hdmi_spec *spec = codec->spec;
  1143. struct hda_pcm *info = spec->pcm_rec;
  1144. unsigned int chans;
  1145. struct hda_pcm_stream *pstr;
  1146. codec->num_pcms = 1;
  1147. codec->pcm_info = info;
  1148. chans = get_wcaps(codec, spec->cvts[0].cvt_nid);
  1149. chans = get_wcaps_channels(chans);
  1150. info->name = get_hdmi_pcm_name(0);
  1151. info->pcm_type = HDA_PCM_TYPE_HDMI;
  1152. pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
  1153. *pstr = spec->pcm_playback;
  1154. pstr->nid = spec->cvts[0].cvt_nid;
  1155. if (pstr->channels_max <= 2 && chans && chans <= 16)
  1156. pstr->channels_max = chans;
  1157. return 0;
  1158. }
  1159. /* unsolicited event for jack sensing */
  1160. static void simple_hdmi_unsol_event(struct hda_codec *codec,
  1161. unsigned int res)
  1162. {
  1163. snd_hda_jack_set_dirty_all(codec);
  1164. snd_hda_jack_report_sync(codec);
  1165. }
  1166. /* generic_hdmi_build_jack can be used for simple_hdmi, too,
  1167. * as long as spec->pins[] is set correctly
  1168. */
  1169. #define simple_hdmi_build_jack generic_hdmi_build_jack
  1170. static int simple_playback_build_controls(struct hda_codec *codec)
  1171. {
  1172. struct hdmi_spec *spec = codec->spec;
  1173. int err;
  1174. err = snd_hda_create_spdif_out_ctls(codec,
  1175. spec->cvts[0].cvt_nid,
  1176. spec->cvts[0].cvt_nid);
  1177. if (err < 0)
  1178. return err;
  1179. return simple_hdmi_build_jack(codec, 0);
  1180. }
  1181. static int simple_playback_init(struct hda_codec *codec)
  1182. {
  1183. struct hdmi_spec *spec = codec->spec;
  1184. hda_nid_t pin = spec->pins[0].pin_nid;
  1185. snd_hda_codec_write(codec, pin, 0,
  1186. AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
  1187. /* some codecs require to unmute the pin */
  1188. if (get_wcaps(codec, pin) & AC_WCAP_OUT_AMP)
  1189. snd_hda_codec_write(codec, pin, 0, AC_VERB_SET_AMP_GAIN_MUTE,
  1190. AMP_OUT_UNMUTE);
  1191. snd_hda_jack_detect_enable(codec, pin, pin);
  1192. snd_hda_jack_report_sync(codec);
  1193. return 0;
  1194. }
  1195. static void simple_playback_free(struct hda_codec *codec)
  1196. {
  1197. struct hdmi_spec *spec = codec->spec;
  1198. kfree(spec);
  1199. }
  1200. /*
  1201. * Nvidia specific implementations
  1202. */
  1203. #define Nv_VERB_SET_Channel_Allocation 0xF79
  1204. #define Nv_VERB_SET_Info_Frame_Checksum 0xF7A
  1205. #define Nv_VERB_SET_Audio_Protection_On 0xF98
  1206. #define Nv_VERB_SET_Audio_Protection_Off 0xF99
  1207. #define nvhdmi_master_con_nid_7x 0x04
  1208. #define nvhdmi_master_pin_nid_7x 0x05
  1209. static const hda_nid_t nvhdmi_con_nids_7x[4] = {
  1210. /*front, rear, clfe, rear_surr */
  1211. 0x6, 0x8, 0xa, 0xc,
  1212. };
  1213. static const struct hda_verb nvhdmi_basic_init_7x_2ch[] = {
  1214. /* set audio protect on */
  1215. { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
  1216. /* enable digital output on pin widget */
  1217. { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
  1218. {} /* terminator */
  1219. };
  1220. static const struct hda_verb nvhdmi_basic_init_7x_8ch[] = {
  1221. /* set audio protect on */
  1222. { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
  1223. /* enable digital output on pin widget */
  1224. { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
  1225. { 0x7, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
  1226. { 0x9, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
  1227. { 0xb, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
  1228. { 0xd, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
  1229. {} /* terminator */
  1230. };
  1231. #ifdef LIMITED_RATE_FMT_SUPPORT
  1232. /* support only the safe format and rate */
  1233. #define SUPPORTED_RATES SNDRV_PCM_RATE_48000
  1234. #define SUPPORTED_MAXBPS 16
  1235. #define SUPPORTED_FORMATS SNDRV_PCM_FMTBIT_S16_LE
  1236. #else
  1237. /* support all rates and formats */
  1238. #define SUPPORTED_RATES \
  1239. (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
  1240. SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |\
  1241. SNDRV_PCM_RATE_192000)
  1242. #define SUPPORTED_MAXBPS 24
  1243. #define SUPPORTED_FORMATS \
  1244. (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE)
  1245. #endif
  1246. static int nvhdmi_7x_init_2ch(struct hda_codec *codec)
  1247. {
  1248. snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_2ch);
  1249. return 0;
  1250. }
  1251. static int nvhdmi_7x_init_8ch(struct hda_codec *codec)
  1252. {
  1253. snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_8ch);
  1254. return 0;
  1255. }
  1256. static unsigned int channels_2_6_8[] = {
  1257. 2, 6, 8
  1258. };
  1259. static unsigned int channels_2_8[] = {
  1260. 2, 8
  1261. };
  1262. static struct snd_pcm_hw_constraint_list hw_constraints_2_6_8_channels = {
  1263. .count = ARRAY_SIZE(channels_2_6_8),
  1264. .list = channels_2_6_8,
  1265. .mask = 0,
  1266. };
  1267. static struct snd_pcm_hw_constraint_list hw_constraints_2_8_channels = {
  1268. .count = ARRAY_SIZE(channels_2_8),
  1269. .list = channels_2_8,
  1270. .mask = 0,
  1271. };
  1272. static int simple_playback_pcm_open(struct hda_pcm_stream *hinfo,
  1273. struct hda_codec *codec,
  1274. struct snd_pcm_substream *substream)
  1275. {
  1276. struct hdmi_spec *spec = codec->spec;
  1277. struct snd_pcm_hw_constraint_list *hw_constraints_channels = NULL;
  1278. switch (codec->preset->id) {
  1279. case 0x10de0002:
  1280. case 0x10de0003:
  1281. case 0x10de0005:
  1282. case 0x10de0006:
  1283. hw_constraints_channels = &hw_constraints_2_8_channels;
  1284. break;
  1285. case 0x10de0007:
  1286. hw_constraints_channels = &hw_constraints_2_6_8_channels;
  1287. break;
  1288. default:
  1289. break;
  1290. }
  1291. if (hw_constraints_channels != NULL) {
  1292. snd_pcm_hw_constraint_list(substream->runtime, 0,
  1293. SNDRV_PCM_HW_PARAM_CHANNELS,
  1294. hw_constraints_channels);
  1295. } else {
  1296. snd_pcm_hw_constraint_step(substream->runtime, 0,
  1297. SNDRV_PCM_HW_PARAM_CHANNELS, 2);
  1298. }
  1299. return snd_hda_multi_out_dig_open(codec, &spec->multiout);
  1300. }
  1301. static int simple_playback_pcm_close(struct hda_pcm_stream *hinfo,
  1302. struct hda_codec *codec,
  1303. struct snd_pcm_substream *substream)
  1304. {
  1305. struct hdmi_spec *spec = codec->spec;
  1306. return snd_hda_multi_out_dig_close(codec, &spec->multiout);
  1307. }
  1308. static int simple_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
  1309. struct hda_codec *codec,
  1310. unsigned int stream_tag,
  1311. unsigned int format,
  1312. struct snd_pcm_substream *substream)
  1313. {
  1314. struct hdmi_spec *spec = codec->spec;
  1315. return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
  1316. stream_tag, format, substream);
  1317. }
  1318. static const struct hda_pcm_stream simple_pcm_playback = {
  1319. .substreams = 1,
  1320. .channels_min = 2,
  1321. .channels_max = 2,
  1322. .ops = {
  1323. .open = simple_playback_pcm_open,
  1324. .close = simple_playback_pcm_close,
  1325. .prepare = simple_playback_pcm_prepare
  1326. },
  1327. };
  1328. static const struct hda_codec_ops simple_hdmi_patch_ops = {
  1329. .build_controls = simple_playback_build_controls,
  1330. .build_pcms = simple_playback_build_pcms,
  1331. .init = simple_playback_init,
  1332. .free = simple_playback_free,
  1333. .unsol_event = simple_hdmi_unsol_event,
  1334. };
  1335. static int patch_simple_hdmi(struct hda_codec *codec,
  1336. hda_nid_t cvt_nid, hda_nid_t pin_nid)
  1337. {
  1338. struct hdmi_spec *spec;
  1339. spec = kzalloc(sizeof(*spec), GFP_KERNEL);
  1340. if (!spec)
  1341. return -ENOMEM;
  1342. codec->spec = spec;
  1343. spec->multiout.num_dacs = 0; /* no analog */
  1344. spec->multiout.max_channels = 2;
  1345. spec->multiout.dig_out_nid = cvt_nid;
  1346. spec->num_cvts = 1;
  1347. spec->num_pins = 1;
  1348. spec->cvts[0].cvt_nid = cvt_nid;
  1349. spec->pins[0].pin_nid = pin_nid;
  1350. spec->pcm_playback = simple_pcm_playback;
  1351. codec->patch_ops = simple_hdmi_patch_ops;
  1352. return 0;
  1353. }
  1354. static void nvhdmi_8ch_7x_set_info_frame_parameters(struct hda_codec *codec,
  1355. int channels)
  1356. {
  1357. unsigned int chanmask;
  1358. int chan = channels ? (channels - 1) : 1;
  1359. switch (channels) {
  1360. default:
  1361. case 0:
  1362. case 2:
  1363. chanmask = 0x00;
  1364. break;
  1365. case 4:
  1366. chanmask = 0x08;
  1367. break;
  1368. case 6:
  1369. chanmask = 0x0b;
  1370. break;
  1371. case 8:
  1372. chanmask = 0x13;
  1373. break;
  1374. }
  1375. /* Set the audio infoframe channel allocation and checksum fields. The
  1376. * channel count is computed implicitly by the hardware. */
  1377. snd_hda_codec_write(codec, 0x1, 0,
  1378. Nv_VERB_SET_Channel_Allocation, chanmask);
  1379. snd_hda_codec_write(codec, 0x1, 0,
  1380. Nv_VERB_SET_Info_Frame_Checksum,
  1381. (0x71 - chan - chanmask));
  1382. }
  1383. static int nvhdmi_8ch_7x_pcm_close(struct hda_pcm_stream *hinfo,
  1384. struct hda_codec *codec,
  1385. struct snd_pcm_substream *substream)
  1386. {
  1387. struct hdmi_spec *spec = codec->spec;
  1388. int i;
  1389. snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x,
  1390. 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
  1391. for (i = 0; i < 4; i++) {
  1392. /* set the stream id */
  1393. snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
  1394. AC_VERB_SET_CHANNEL_STREAMID, 0);
  1395. /* set the stream format */
  1396. snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
  1397. AC_VERB_SET_STREAM_FORMAT, 0);
  1398. }
  1399. /* The audio hardware sends a channel count of 0x7 (8ch) when all the
  1400. * streams are disabled. */
  1401. nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
  1402. return snd_hda_multi_out_dig_close(codec, &spec->multiout);
  1403. }
  1404. static int nvhdmi_8ch_7x_pcm_prepare(struct hda_pcm_stream *hinfo,
  1405. struct hda_codec *codec,
  1406. unsigned int stream_tag,
  1407. unsigned int format,
  1408. struct snd_pcm_substream *substream)
  1409. {
  1410. int chs;
  1411. unsigned int dataDCC2, channel_id;
  1412. int i;
  1413. struct hdmi_spec *spec = codec->spec;
  1414. struct hda_spdif_out *spdif;
  1415. mutex_lock(&codec->spdif_mutex);
  1416. spdif = snd_hda_spdif_out_of_nid(codec, spec->cvts[0].cvt_nid);
  1417. chs = substream->runtime->channels;
  1418. dataDCC2 = 0x2;
  1419. /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
  1420. if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
  1421. snd_hda_codec_write(codec,
  1422. nvhdmi_master_con_nid_7x,
  1423. 0,
  1424. AC_VERB_SET_DIGI_CONVERT_1,
  1425. spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
  1426. /* set the stream id */
  1427. snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
  1428. AC_VERB_SET_CHANNEL_STREAMID, (stream_tag << 4) | 0x0);
  1429. /* set the stream format */
  1430. snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
  1431. AC_VERB_SET_STREAM_FORMAT, format);
  1432. /* turn on again (if needed) */
  1433. /* enable and set the channel status audio/data flag */
  1434. if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE)) {
  1435. snd_hda_codec_write(codec,
  1436. nvhdmi_master_con_nid_7x,
  1437. 0,
  1438. AC_VERB_SET_DIGI_CONVERT_1,
  1439. spdif->ctls & 0xff);
  1440. snd_hda_codec_write(codec,
  1441. nvhdmi_master_con_nid_7x,
  1442. 0,
  1443. AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
  1444. }
  1445. for (i = 0; i < 4; i++) {
  1446. if (chs == 2)
  1447. channel_id = 0;
  1448. else
  1449. channel_id = i * 2;
  1450. /* turn off SPDIF once;
  1451. *otherwise the IEC958 bits won't be updated
  1452. */
  1453. if (codec->spdif_status_reset &&
  1454. (spdif->ctls & AC_DIG1_ENABLE))
  1455. snd_hda_codec_write(codec,
  1456. nvhdmi_con_nids_7x[i],
  1457. 0,
  1458. AC_VERB_SET_DIGI_CONVERT_1,
  1459. spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
  1460. /* set the stream id */
  1461. snd_hda_codec_write(codec,
  1462. nvhdmi_con_nids_7x[i],
  1463. 0,
  1464. AC_VERB_SET_CHANNEL_STREAMID,
  1465. (stream_tag << 4) | channel_id);
  1466. /* set the stream format */
  1467. snd_hda_codec_write(codec,
  1468. nvhdmi_con_nids_7x[i],
  1469. 0,
  1470. AC_VERB_SET_STREAM_FORMAT,
  1471. format);
  1472. /* turn on again (if needed) */
  1473. /* enable and set the channel status audio/data flag */
  1474. if (codec->spdif_status_reset &&
  1475. (spdif->ctls & AC_DIG1_ENABLE)) {
  1476. snd_hda_codec_write(codec,
  1477. nvhdmi_con_nids_7x[i],
  1478. 0,
  1479. AC_VERB_SET_DIGI_CONVERT_1,
  1480. spdif->ctls & 0xff);
  1481. snd_hda_codec_write(codec,
  1482. nvhdmi_con_nids_7x[i],
  1483. 0,
  1484. AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
  1485. }
  1486. }
  1487. nvhdmi_8ch_7x_set_info_frame_parameters(codec, chs);
  1488. mutex_unlock(&codec->spdif_mutex);
  1489. return 0;
  1490. }
  1491. static const struct hda_pcm_stream nvhdmi_pcm_playback_8ch_7x = {
  1492. .substreams = 1,
  1493. .channels_min = 2,
  1494. .channels_max = 8,
  1495. .nid = nvhdmi_master_con_nid_7x,
  1496. .rates = SUPPORTED_RATES,
  1497. .maxbps = SUPPORTED_MAXBPS,
  1498. .formats = SUPPORTED_FORMATS,
  1499. .ops = {
  1500. .open = simple_playback_pcm_open,
  1501. .close = nvhdmi_8ch_7x_pcm_close,
  1502. .prepare = nvhdmi_8ch_7x_pcm_prepare
  1503. },
  1504. };
  1505. static int patch_nvhdmi_2ch(struct hda_codec *codec)
  1506. {
  1507. struct hdmi_spec *spec;
  1508. int err = patch_simple_hdmi(codec, nvhdmi_master_con_nid_7x,
  1509. nvhdmi_master_pin_nid_7x);
  1510. if (err < 0)
  1511. return err;
  1512. codec->patch_ops.init = nvhdmi_7x_init_2ch;
  1513. /* override the PCM rates, etc, as the codec doesn't give full list */
  1514. spec = codec->spec;
  1515. spec->pcm_playback.rates = SUPPORTED_RATES;
  1516. spec->pcm_playback.maxbps = SUPPORTED_MAXBPS;
  1517. spec->pcm_playback.formats = SUPPORTED_FORMATS;
  1518. return 0;
  1519. }
  1520. static int patch_nvhdmi_8ch_7x(struct hda_codec *codec)
  1521. {
  1522. struct hdmi_spec *spec;
  1523. int err = patch_nvhdmi_2ch(codec);
  1524. if (err < 0)
  1525. return err;
  1526. spec = codec->spec;
  1527. spec->multiout.max_channels = 8;
  1528. spec->pcm_playback = nvhdmi_pcm_playback_8ch_7x;
  1529. codec->patch_ops.init = nvhdmi_7x_init_8ch;
  1530. /* Initialize the audio infoframe channel mask and checksum to something
  1531. * valid */
  1532. nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
  1533. return 0;
  1534. }
  1535. /*
  1536. * ATI-specific implementations
  1537. *
  1538. * FIXME: we may omit the whole this and use the generic code once after
  1539. * it's confirmed to work.
  1540. */
  1541. #define ATIHDMI_CVT_NID 0x02 /* audio converter */
  1542. #define ATIHDMI_PIN_NID 0x03 /* HDMI output pin */
  1543. static int atihdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
  1544. struct hda_codec *codec,
  1545. unsigned int stream_tag,
  1546. unsigned int format,
  1547. struct snd_pcm_substream *substream)
  1548. {
  1549. struct hdmi_spec *spec = codec->spec;
  1550. int chans = substream->runtime->channels;
  1551. int i, err;
  1552. err = simple_playback_pcm_prepare(hinfo, codec, stream_tag, format,
  1553. substream);
  1554. if (err < 0)
  1555. return err;
  1556. snd_hda_codec_write(codec, spec->cvts[0].cvt_nid, 0,
  1557. AC_VERB_SET_CVT_CHAN_COUNT, chans - 1);
  1558. /* FIXME: XXX */
  1559. for (i = 0; i < chans; i++) {
  1560. snd_hda_codec_write(codec, spec->cvts[0].cvt_nid, 0,
  1561. AC_VERB_SET_HDMI_CHAN_SLOT,
  1562. (i << 4) | i);
  1563. }
  1564. return 0;
  1565. }
  1566. static int patch_atihdmi(struct hda_codec *codec)
  1567. {
  1568. struct hdmi_spec *spec;
  1569. int err = patch_simple_hdmi(codec, ATIHDMI_CVT_NID, ATIHDMI_PIN_NID);
  1570. if (err < 0)
  1571. return err;
  1572. spec = codec->spec;
  1573. spec->pcm_playback.ops.prepare = atihdmi_playback_pcm_prepare;
  1574. return 0;
  1575. }
  1576. /* VIA HDMI Implementation */
  1577. #define VIAHDMI_CVT_NID 0x02 /* audio converter1 */
  1578. #define VIAHDMI_PIN_NID 0x03 /* HDMI output pin1 */
  1579. static int patch_via_hdmi(struct hda_codec *codec)
  1580. {
  1581. return patch_simple_hdmi(codec, VIAHDMI_CVT_NID, VIAHDMI_PIN_NID);
  1582. }
  1583. /*
  1584. * patch entries
  1585. */
  1586. static const struct hda_codec_preset snd_hda_preset_hdmi[] = {
  1587. { .id = 0x1002793c, .name = "RS600 HDMI", .patch = patch_atihdmi },
  1588. { .id = 0x10027919, .name = "RS600 HDMI", .patch = patch_atihdmi },
  1589. { .id = 0x1002791a, .name = "RS690/780 HDMI", .patch = patch_atihdmi },
  1590. { .id = 0x1002aa01, .name = "R6xx HDMI", .patch = patch_generic_hdmi },
  1591. { .id = 0x10951390, .name = "SiI1390 HDMI", .patch = patch_generic_hdmi },
  1592. { .id = 0x10951392, .name = "SiI1392 HDMI", .patch = patch_generic_hdmi },
  1593. { .id = 0x17e80047, .name = "Chrontel HDMI", .patch = patch_generic_hdmi },
  1594. { .id = 0x10de0002, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
  1595. { .id = 0x10de0003, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
  1596. { .id = 0x10de0005, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
  1597. { .id = 0x10de0006, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
  1598. { .id = 0x10de0007, .name = "MCP79/7A HDMI", .patch = patch_nvhdmi_8ch_7x },
  1599. { .id = 0x10de000a, .name = "GPU 0a HDMI/DP", .patch = patch_generic_hdmi },
  1600. { .id = 0x10de000b, .name = "GPU 0b HDMI/DP", .patch = patch_generic_hdmi },
  1601. { .id = 0x10de000c, .name = "MCP89 HDMI", .patch = patch_generic_hdmi },
  1602. { .id = 0x10de000d, .name = "GPU 0d HDMI/DP", .patch = patch_generic_hdmi },
  1603. { .id = 0x10de0010, .name = "GPU 10 HDMI/DP", .patch = patch_generic_hdmi },
  1604. { .id = 0x10de0011, .name = "GPU 11 HDMI/DP", .patch = patch_generic_hdmi },
  1605. { .id = 0x10de0012, .name = "GPU 12 HDMI/DP", .patch = patch_generic_hdmi },
  1606. { .id = 0x10de0013, .name = "GPU 13 HDMI/DP", .patch = patch_generic_hdmi },
  1607. { .id = 0x10de0014, .name = "GPU 14 HDMI/DP", .patch = patch_generic_hdmi },
  1608. { .id = 0x10de0015, .name = "GPU 15 HDMI/DP", .patch = patch_generic_hdmi },
  1609. { .id = 0x10de0016, .name = "GPU 16 HDMI/DP", .patch = patch_generic_hdmi },
  1610. /* 17 is known to be absent */
  1611. { .id = 0x10de0018, .name = "GPU 18 HDMI/DP", .patch = patch_generic_hdmi },
  1612. { .id = 0x10de0019, .name = "GPU 19 HDMI/DP", .patch = patch_generic_hdmi },
  1613. { .id = 0x10de001a, .name = "GPU 1a HDMI/DP", .patch = patch_generic_hdmi },
  1614. { .id = 0x10de001b, .name = "GPU 1b HDMI/DP", .patch = patch_generic_hdmi },
  1615. { .id = 0x10de001c, .name = "GPU 1c HDMI/DP", .patch = patch_generic_hdmi },
  1616. { .id = 0x10de0040, .name = "GPU 40 HDMI/DP", .patch = patch_generic_hdmi },
  1617. { .id = 0x10de0041, .name = "GPU 41 HDMI/DP", .patch = patch_generic_hdmi },
  1618. { .id = 0x10de0042, .name = "GPU 42 HDMI/DP", .patch = patch_generic_hdmi },
  1619. { .id = 0x10de0043, .name = "GPU 43 HDMI/DP", .patch = patch_generic_hdmi },
  1620. { .id = 0x10de0044, .name = "GPU 44 HDMI/DP", .patch = patch_generic_hdmi },
  1621. { .id = 0x10de0051, .name = "GPU 51 HDMI/DP", .patch = patch_generic_hdmi },
  1622. { .id = 0x10de0067, .name = "MCP67 HDMI", .patch = patch_nvhdmi_2ch },
  1623. { .id = 0x10de8001, .name = "MCP73 HDMI", .patch = patch_nvhdmi_2ch },
  1624. { .id = 0x11069f80, .name = "VX900 HDMI/DP", .patch = patch_via_hdmi },
  1625. { .id = 0x11069f81, .name = "VX900 HDMI/DP", .patch = patch_via_hdmi },
  1626. { .id = 0x11069f84, .name = "VX11 HDMI/DP", .patch = patch_generic_hdmi },
  1627. { .id = 0x11069f85, .name = "VX11 HDMI/DP", .patch = patch_generic_hdmi },
  1628. { .id = 0x80860054, .name = "IbexPeak HDMI", .patch = patch_generic_hdmi },
  1629. { .id = 0x80862801, .name = "Bearlake HDMI", .patch = patch_generic_hdmi },
  1630. { .id = 0x80862802, .name = "Cantiga HDMI", .patch = patch_generic_hdmi },
  1631. { .id = 0x80862803, .name = "Eaglelake HDMI", .patch = patch_generic_hdmi },
  1632. { .id = 0x80862804, .name = "IbexPeak HDMI", .patch = patch_generic_hdmi },
  1633. { .id = 0x80862805, .name = "CougarPoint HDMI", .patch = patch_generic_hdmi },
  1634. { .id = 0x80862806, .name = "PantherPoint HDMI", .patch = patch_generic_hdmi },
  1635. { .id = 0x80862807, .name = "Haswell HDMI", .patch = patch_generic_hdmi },
  1636. { .id = 0x80862880, .name = "CedarTrail HDMI", .patch = patch_generic_hdmi },
  1637. { .id = 0x808629fb, .name = "Crestline HDMI", .patch = patch_generic_hdmi },
  1638. {} /* terminator */
  1639. };
  1640. MODULE_ALIAS("snd-hda-codec-id:1002793c");
  1641. MODULE_ALIAS("snd-hda-codec-id:10027919");
  1642. MODULE_ALIAS("snd-hda-codec-id:1002791a");
  1643. MODULE_ALIAS("snd-hda-codec-id:1002aa01");
  1644. MODULE_ALIAS("snd-hda-codec-id:10951390");
  1645. MODULE_ALIAS("snd-hda-codec-id:10951392");
  1646. MODULE_ALIAS("snd-hda-codec-id:10de0002");
  1647. MODULE_ALIAS("snd-hda-codec-id:10de0003");
  1648. MODULE_ALIAS("snd-hda-codec-id:10de0005");
  1649. MODULE_ALIAS("snd-hda-codec-id:10de0006");
  1650. MODULE_ALIAS("snd-hda-codec-id:10de0007");
  1651. MODULE_ALIAS("snd-hda-codec-id:10de000a");
  1652. MODULE_ALIAS("snd-hda-codec-id:10de000b");
  1653. MODULE_ALIAS("snd-hda-codec-id:10de000c");
  1654. MODULE_ALIAS("snd-hda-codec-id:10de000d");
  1655. MODULE_ALIAS("snd-hda-codec-id:10de0010");
  1656. MODULE_ALIAS("snd-hda-codec-id:10de0011");
  1657. MODULE_ALIAS("snd-hda-codec-id:10de0012");
  1658. MODULE_ALIAS("snd-hda-codec-id:10de0013");
  1659. MODULE_ALIAS("snd-hda-codec-id:10de0014");
  1660. MODULE_ALIAS("snd-hda-codec-id:10de0015");
  1661. MODULE_ALIAS("snd-hda-codec-id:10de0016");
  1662. MODULE_ALIAS("snd-hda-codec-id:10de0018");
  1663. MODULE_ALIAS("snd-hda-codec-id:10de0019");
  1664. MODULE_ALIAS("snd-hda-codec-id:10de001a");
  1665. MODULE_ALIAS("snd-hda-codec-id:10de001b");
  1666. MODULE_ALIAS("snd-hda-codec-id:10de001c");
  1667. MODULE_ALIAS("snd-hda-codec-id:10de0040");
  1668. MODULE_ALIAS("snd-hda-codec-id:10de0041");
  1669. MODULE_ALIAS("snd-hda-codec-id:10de0042");
  1670. MODULE_ALIAS("snd-hda-codec-id:10de0043");
  1671. MODULE_ALIAS("snd-hda-codec-id:10de0044");
  1672. MODULE_ALIAS("snd-hda-codec-id:10de0051");
  1673. MODULE_ALIAS("snd-hda-codec-id:10de0067");
  1674. MODULE_ALIAS("snd-hda-codec-id:10de8001");
  1675. MODULE_ALIAS("snd-hda-codec-id:11069f80");
  1676. MODULE_ALIAS("snd-hda-codec-id:11069f81");
  1677. MODULE_ALIAS("snd-hda-codec-id:11069f84");
  1678. MODULE_ALIAS("snd-hda-codec-id:11069f85");
  1679. MODULE_ALIAS("snd-hda-codec-id:17e80047");
  1680. MODULE_ALIAS("snd-hda-codec-id:80860054");
  1681. MODULE_ALIAS("snd-hda-codec-id:80862801");
  1682. MODULE_ALIAS("snd-hda-codec-id:80862802");
  1683. MODULE_ALIAS("snd-hda-codec-id:80862803");
  1684. MODULE_ALIAS("snd-hda-codec-id:80862804");
  1685. MODULE_ALIAS("snd-hda-codec-id:80862805");
  1686. MODULE_ALIAS("snd-hda-codec-id:80862806");
  1687. MODULE_ALIAS("snd-hda-codec-id:80862807");
  1688. MODULE_ALIAS("snd-hda-codec-id:80862880");
  1689. MODULE_ALIAS("snd-hda-codec-id:808629fb");
  1690. MODULE_LICENSE("GPL");
  1691. MODULE_DESCRIPTION("HDMI HD-audio codec");
  1692. MODULE_ALIAS("snd-hda-codec-intelhdmi");
  1693. MODULE_ALIAS("snd-hda-codec-nvhdmi");
  1694. MODULE_ALIAS("snd-hda-codec-atihdmi");
  1695. static struct hda_codec_preset_list intel_list = {
  1696. .preset = snd_hda_preset_hdmi,
  1697. .owner = THIS_MODULE,
  1698. };
  1699. static int __init patch_hdmi_init(void)
  1700. {
  1701. return snd_hda_add_codec_preset(&intel_list);
  1702. }
  1703. static void __exit patch_hdmi_exit(void)
  1704. {
  1705. snd_hda_delete_codec_preset(&intel_list);
  1706. }
  1707. module_init(patch_hdmi_init)
  1708. module_exit(patch_hdmi_exit)