patch_hdmi.c 55 KB

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