patch_hdmi.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009
  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. const 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. /* Enable pin out: some machines with GM965 gets broken output when
  374. * the pin is disabled or changed while using with HDMI
  375. */
  376. snd_hda_codec_write(codec, pin_nid, 0,
  377. AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
  378. }
  379. static int hdmi_get_channel_count(struct hda_codec *codec, hda_nid_t cvt_nid)
  380. {
  381. return 1 + snd_hda_codec_read(codec, cvt_nid, 0,
  382. AC_VERB_GET_CVT_CHAN_COUNT, 0);
  383. }
  384. static void hdmi_set_channel_count(struct hda_codec *codec,
  385. hda_nid_t cvt_nid, int chs)
  386. {
  387. if (chs != hdmi_get_channel_count(codec, cvt_nid))
  388. snd_hda_codec_write(codec, cvt_nid, 0,
  389. AC_VERB_SET_CVT_CHAN_COUNT, chs - 1);
  390. }
  391. /*
  392. * Channel mapping routines
  393. */
  394. /*
  395. * Compute derived values in channel_allocations[].
  396. */
  397. static void init_channel_allocations(void)
  398. {
  399. int i, j;
  400. struct cea_channel_speaker_allocation *p;
  401. for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
  402. p = channel_allocations + i;
  403. p->channels = 0;
  404. p->spk_mask = 0;
  405. for (j = 0; j < ARRAY_SIZE(p->speakers); j++)
  406. if (p->speakers[j]) {
  407. p->channels++;
  408. p->spk_mask |= p->speakers[j];
  409. }
  410. }
  411. }
  412. /*
  413. * The transformation takes two steps:
  414. *
  415. * eld->spk_alloc => (eld_speaker_allocation_bits[]) => spk_mask
  416. * spk_mask => (channel_allocations[]) => ai->CA
  417. *
  418. * TODO: it could select the wrong CA from multiple candidates.
  419. */
  420. static int hdmi_channel_allocation(struct hdmi_eld *eld, int channels)
  421. {
  422. int i;
  423. int ca = 0;
  424. int spk_mask = 0;
  425. char buf[SND_PRINT_CHANNEL_ALLOCATION_ADVISED_BUFSIZE];
  426. /*
  427. * CA defaults to 0 for basic stereo audio
  428. */
  429. if (channels <= 2)
  430. return 0;
  431. /*
  432. * expand ELD's speaker allocation mask
  433. *
  434. * ELD tells the speaker mask in a compact(paired) form,
  435. * expand ELD's notions to match the ones used by Audio InfoFrame.
  436. */
  437. for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
  438. if (eld->spk_alloc & (1 << i))
  439. spk_mask |= eld_speaker_allocation_bits[i];
  440. }
  441. /* search for the first working match in the CA table */
  442. for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
  443. if (channels == channel_allocations[i].channels &&
  444. (spk_mask & channel_allocations[i].spk_mask) ==
  445. channel_allocations[i].spk_mask) {
  446. ca = channel_allocations[i].ca_index;
  447. break;
  448. }
  449. }
  450. if (!ca) {
  451. /* if there was no match, select the regular ALSA channel
  452. * allocation with the matching number of channels */
  453. for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
  454. if (channels == channel_allocations[i].channels) {
  455. ca = channel_allocations[i].ca_index;
  456. break;
  457. }
  458. }
  459. }
  460. snd_print_channel_allocation(eld->spk_alloc, buf, sizeof(buf));
  461. snd_printdd("HDMI: select CA 0x%x for %d-channel allocation: %s\n",
  462. ca, channels, buf);
  463. return ca;
  464. }
  465. static void hdmi_debug_channel_mapping(struct hda_codec *codec,
  466. hda_nid_t pin_nid)
  467. {
  468. #ifdef CONFIG_SND_DEBUG_VERBOSE
  469. int i;
  470. int slot;
  471. for (i = 0; i < 8; i++) {
  472. slot = snd_hda_codec_read(codec, pin_nid, 0,
  473. AC_VERB_GET_HDMI_CHAN_SLOT, i);
  474. printk(KERN_DEBUG "HDMI: ASP channel %d => slot %d\n",
  475. slot >> 4, slot & 0xf);
  476. }
  477. #endif
  478. }
  479. static void hdmi_setup_channel_mapping(struct hda_codec *codec,
  480. hda_nid_t pin_nid,
  481. int ca)
  482. {
  483. int i;
  484. int err;
  485. if (hdmi_channel_mapping[ca][1] == 0) {
  486. for (i = 0; i < channel_allocations[ca].channels; i++)
  487. hdmi_channel_mapping[ca][i] = i | (i << 4);
  488. for (; i < 8; i++)
  489. hdmi_channel_mapping[ca][i] = 0xf | (i << 4);
  490. }
  491. for (i = 0; i < 8; i++) {
  492. err = snd_hda_codec_write(codec, pin_nid, 0,
  493. AC_VERB_SET_HDMI_CHAN_SLOT,
  494. hdmi_channel_mapping[ca][i]);
  495. if (err) {
  496. snd_printdd(KERN_NOTICE
  497. "HDMI: channel mapping failed\n");
  498. break;
  499. }
  500. }
  501. hdmi_debug_channel_mapping(codec, pin_nid);
  502. }
  503. /*
  504. * Audio InfoFrame routines
  505. */
  506. /*
  507. * Enable Audio InfoFrame Transmission
  508. */
  509. static void hdmi_start_infoframe_trans(struct hda_codec *codec,
  510. hda_nid_t pin_nid)
  511. {
  512. hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
  513. snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
  514. AC_DIPXMIT_BEST);
  515. }
  516. /*
  517. * Disable Audio InfoFrame Transmission
  518. */
  519. static void hdmi_stop_infoframe_trans(struct hda_codec *codec,
  520. hda_nid_t pin_nid)
  521. {
  522. hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
  523. snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
  524. AC_DIPXMIT_DISABLE);
  525. }
  526. static void hdmi_debug_dip_size(struct hda_codec *codec, hda_nid_t pin_nid)
  527. {
  528. #ifdef CONFIG_SND_DEBUG_VERBOSE
  529. int i;
  530. int size;
  531. size = snd_hdmi_get_eld_size(codec, pin_nid);
  532. printk(KERN_DEBUG "HDMI: ELD buf size is %d\n", size);
  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. printk(KERN_DEBUG "HDMI: DIP GP[%d] buf size is %d\n", i, size);
  537. }
  538. #endif
  539. }
  540. static void hdmi_clear_dip_buffers(struct hda_codec *codec, hda_nid_t pin_nid)
  541. {
  542. #ifdef BE_PARANOID
  543. int i, j;
  544. int size;
  545. int pi, bi;
  546. for (i = 0; i < 8; i++) {
  547. size = snd_hda_codec_read(codec, pin_nid, 0,
  548. AC_VERB_GET_HDMI_DIP_SIZE, i);
  549. if (size == 0)
  550. continue;
  551. hdmi_set_dip_index(codec, pin_nid, i, 0x0);
  552. for (j = 1; j < 1000; j++) {
  553. hdmi_write_dip_byte(codec, pin_nid, 0x0);
  554. hdmi_get_dip_index(codec, pin_nid, &pi, &bi);
  555. if (pi != i)
  556. snd_printd(KERN_INFO "dip index %d: %d != %d\n",
  557. bi, pi, i);
  558. if (bi == 0) /* byte index wrapped around */
  559. break;
  560. }
  561. snd_printd(KERN_INFO
  562. "HDMI: DIP GP[%d] buf reported size=%d, written=%d\n",
  563. i, size, j);
  564. }
  565. #endif
  566. }
  567. static void hdmi_checksum_audio_infoframe(struct hdmi_audio_infoframe *hdmi_ai)
  568. {
  569. u8 *bytes = (u8 *)hdmi_ai;
  570. u8 sum = 0;
  571. int i;
  572. hdmi_ai->checksum = 0;
  573. for (i = 0; i < sizeof(*hdmi_ai); i++)
  574. sum += bytes[i];
  575. hdmi_ai->checksum = -sum;
  576. }
  577. static void hdmi_fill_audio_infoframe(struct hda_codec *codec,
  578. hda_nid_t pin_nid,
  579. u8 *dip, int size)
  580. {
  581. int i;
  582. hdmi_debug_dip_size(codec, pin_nid);
  583. hdmi_clear_dip_buffers(codec, pin_nid); /* be paranoid */
  584. hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
  585. for (i = 0; i < size; i++)
  586. hdmi_write_dip_byte(codec, pin_nid, dip[i]);
  587. }
  588. static bool hdmi_infoframe_uptodate(struct hda_codec *codec, hda_nid_t pin_nid,
  589. u8 *dip, int size)
  590. {
  591. u8 val;
  592. int i;
  593. if (snd_hda_codec_read(codec, pin_nid, 0, AC_VERB_GET_HDMI_DIP_XMIT, 0)
  594. != AC_DIPXMIT_BEST)
  595. return false;
  596. hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
  597. for (i = 0; i < size; i++) {
  598. val = snd_hda_codec_read(codec, pin_nid, 0,
  599. AC_VERB_GET_HDMI_DIP_DATA, 0);
  600. if (val != dip[i])
  601. return false;
  602. }
  603. return true;
  604. }
  605. static void hdmi_setup_audio_infoframe(struct hda_codec *codec, int pin_idx,
  606. struct snd_pcm_substream *substream)
  607. {
  608. struct hdmi_spec *spec = codec->spec;
  609. struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
  610. hda_nid_t pin_nid = per_pin->pin_nid;
  611. int channels = substream->runtime->channels;
  612. struct hdmi_eld *eld;
  613. int ca;
  614. union audio_infoframe ai;
  615. eld = &spec->pins[pin_idx].sink_eld;
  616. if (!eld->monitor_present)
  617. return;
  618. ca = hdmi_channel_allocation(eld, channels);
  619. memset(&ai, 0, sizeof(ai));
  620. if (eld->conn_type == 0) { /* HDMI */
  621. struct hdmi_audio_infoframe *hdmi_ai = &ai.hdmi;
  622. hdmi_ai->type = 0x84;
  623. hdmi_ai->ver = 0x01;
  624. hdmi_ai->len = 0x0a;
  625. hdmi_ai->CC02_CT47 = channels - 1;
  626. hdmi_ai->CA = ca;
  627. hdmi_checksum_audio_infoframe(hdmi_ai);
  628. } else if (eld->conn_type == 1) { /* DisplayPort */
  629. struct dp_audio_infoframe *dp_ai = &ai.dp;
  630. dp_ai->type = 0x84;
  631. dp_ai->len = 0x1b;
  632. dp_ai->ver = 0x11 << 2;
  633. dp_ai->CC02_CT47 = channels - 1;
  634. dp_ai->CA = ca;
  635. } else {
  636. snd_printd("HDMI: unknown connection type at pin %d\n",
  637. pin_nid);
  638. return;
  639. }
  640. /*
  641. * sizeof(ai) is used instead of sizeof(*hdmi_ai) or
  642. * sizeof(*dp_ai) to avoid partial match/update problems when
  643. * the user switches between HDMI/DP monitors.
  644. */
  645. if (!hdmi_infoframe_uptodate(codec, pin_nid, ai.bytes,
  646. sizeof(ai))) {
  647. snd_printdd("hdmi_setup_audio_infoframe: "
  648. "pin=%d channels=%d\n",
  649. pin_nid,
  650. channels);
  651. hdmi_setup_channel_mapping(codec, pin_nid, ca);
  652. hdmi_stop_infoframe_trans(codec, pin_nid);
  653. hdmi_fill_audio_infoframe(codec, pin_nid,
  654. ai.bytes, sizeof(ai));
  655. hdmi_start_infoframe_trans(codec, pin_nid);
  656. }
  657. }
  658. /*
  659. * Unsolicited events
  660. */
  661. static void hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll);
  662. static void hdmi_intrinsic_event(struct hda_codec *codec, unsigned int res)
  663. {
  664. struct hdmi_spec *spec = codec->spec;
  665. int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
  666. int pin_nid;
  667. int pin_idx;
  668. struct hda_jack_tbl *jack;
  669. jack = snd_hda_jack_tbl_get_from_tag(codec, tag);
  670. if (!jack)
  671. return;
  672. pin_nid = jack->nid;
  673. jack->jack_dirty = 1;
  674. _snd_printd(SND_PR_VERBOSE,
  675. "HDMI hot plug event: Codec=%d Pin=%d Presence_Detect=%d ELD_Valid=%d\n",
  676. codec->addr, pin_nid,
  677. !!(res & AC_UNSOL_RES_PD), !!(res & AC_UNSOL_RES_ELDV));
  678. pin_idx = pin_nid_to_pin_index(spec, pin_nid);
  679. if (pin_idx < 0)
  680. return;
  681. hdmi_present_sense(&spec->pins[pin_idx], 1);
  682. snd_hda_jack_report_sync(codec);
  683. }
  684. static void hdmi_non_intrinsic_event(struct hda_codec *codec, unsigned int res)
  685. {
  686. int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
  687. int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
  688. int cp_state = !!(res & AC_UNSOL_RES_CP_STATE);
  689. int cp_ready = !!(res & AC_UNSOL_RES_CP_READY);
  690. printk(KERN_INFO
  691. "HDMI CP event: CODEC=%d PIN=%d SUBTAG=0x%x CP_STATE=%d CP_READY=%d\n",
  692. codec->addr,
  693. tag,
  694. subtag,
  695. cp_state,
  696. cp_ready);
  697. /* TODO */
  698. if (cp_state)
  699. ;
  700. if (cp_ready)
  701. ;
  702. }
  703. static void hdmi_unsol_event(struct hda_codec *codec, unsigned int res)
  704. {
  705. int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
  706. int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
  707. if (!snd_hda_jack_tbl_get_from_tag(codec, tag)) {
  708. snd_printd(KERN_INFO "Unexpected HDMI event tag 0x%x\n", tag);
  709. return;
  710. }
  711. if (subtag == 0)
  712. hdmi_intrinsic_event(codec, res);
  713. else
  714. hdmi_non_intrinsic_event(codec, res);
  715. }
  716. /*
  717. * Callbacks
  718. */
  719. /* HBR should be Non-PCM, 8 channels */
  720. #define is_hbr_format(format) \
  721. ((format & AC_FMT_TYPE_NON_PCM) && (format & AC_FMT_CHAN_MASK) == 7)
  722. static int hdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
  723. hda_nid_t pin_nid, u32 stream_tag, int format)
  724. {
  725. int pinctl;
  726. int new_pinctl = 0;
  727. if (snd_hda_query_pin_caps(codec, pin_nid) & AC_PINCAP_HBR) {
  728. pinctl = snd_hda_codec_read(codec, pin_nid, 0,
  729. AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
  730. new_pinctl = pinctl & ~AC_PINCTL_EPT;
  731. if (is_hbr_format(format))
  732. new_pinctl |= AC_PINCTL_EPT_HBR;
  733. else
  734. new_pinctl |= AC_PINCTL_EPT_NATIVE;
  735. snd_printdd("hdmi_setup_stream: "
  736. "NID=0x%x, %spinctl=0x%x\n",
  737. pin_nid,
  738. pinctl == new_pinctl ? "" : "new-",
  739. new_pinctl);
  740. if (pinctl != new_pinctl)
  741. snd_hda_codec_write(codec, pin_nid, 0,
  742. AC_VERB_SET_PIN_WIDGET_CONTROL,
  743. new_pinctl);
  744. }
  745. if (is_hbr_format(format) && !new_pinctl) {
  746. snd_printdd("hdmi_setup_stream: HBR is not supported\n");
  747. return -EINVAL;
  748. }
  749. snd_hda_codec_setup_stream(codec, cvt_nid, stream_tag, 0, format);
  750. return 0;
  751. }
  752. /*
  753. * HDA PCM callbacks
  754. */
  755. static int hdmi_pcm_open(struct hda_pcm_stream *hinfo,
  756. struct hda_codec *codec,
  757. struct snd_pcm_substream *substream)
  758. {
  759. struct hdmi_spec *spec = codec->spec;
  760. struct snd_pcm_runtime *runtime = substream->runtime;
  761. int pin_idx, cvt_idx, mux_idx = 0;
  762. struct hdmi_spec_per_pin *per_pin;
  763. struct hdmi_eld *eld;
  764. struct hdmi_spec_per_cvt *per_cvt = NULL;
  765. /* Validate hinfo */
  766. pin_idx = hinfo_to_pin_index(spec, hinfo);
  767. if (snd_BUG_ON(pin_idx < 0))
  768. return -EINVAL;
  769. per_pin = &spec->pins[pin_idx];
  770. eld = &per_pin->sink_eld;
  771. /* Dynamically assign converter to stream */
  772. for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
  773. per_cvt = &spec->cvts[cvt_idx];
  774. /* Must not already be assigned */
  775. if (per_cvt->assigned)
  776. continue;
  777. /* Must be in pin's mux's list of converters */
  778. for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++)
  779. if (per_pin->mux_nids[mux_idx] == per_cvt->cvt_nid)
  780. break;
  781. /* Not in mux list */
  782. if (mux_idx == per_pin->num_mux_nids)
  783. continue;
  784. break;
  785. }
  786. /* No free converters */
  787. if (cvt_idx == spec->num_cvts)
  788. return -ENODEV;
  789. /* Claim converter */
  790. per_cvt->assigned = 1;
  791. hinfo->nid = per_cvt->cvt_nid;
  792. snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0,
  793. AC_VERB_SET_CONNECT_SEL,
  794. mux_idx);
  795. snd_hda_spdif_ctls_assign(codec, pin_idx, per_cvt->cvt_nid);
  796. /* Initially set the converter's capabilities */
  797. hinfo->channels_min = per_cvt->channels_min;
  798. hinfo->channels_max = per_cvt->channels_max;
  799. hinfo->rates = per_cvt->rates;
  800. hinfo->formats = per_cvt->formats;
  801. hinfo->maxbps = per_cvt->maxbps;
  802. /* Restrict capabilities by ELD if this isn't disabled */
  803. if (!static_hdmi_pcm && eld->eld_valid) {
  804. snd_hdmi_eld_update_pcm_info(eld, hinfo);
  805. if (hinfo->channels_min > hinfo->channels_max ||
  806. !hinfo->rates || !hinfo->formats) {
  807. per_cvt->assigned = 0;
  808. hinfo->nid = 0;
  809. snd_hda_spdif_ctls_unassign(codec, pin_idx);
  810. return -ENODEV;
  811. }
  812. }
  813. /* Store the updated parameters */
  814. runtime->hw.channels_min = hinfo->channels_min;
  815. runtime->hw.channels_max = hinfo->channels_max;
  816. runtime->hw.formats = hinfo->formats;
  817. runtime->hw.rates = hinfo->rates;
  818. snd_pcm_hw_constraint_step(substream->runtime, 0,
  819. SNDRV_PCM_HW_PARAM_CHANNELS, 2);
  820. return 0;
  821. }
  822. /*
  823. * HDA/HDMI auto parsing
  824. */
  825. static int hdmi_read_pin_conn(struct hda_codec *codec, int pin_idx)
  826. {
  827. struct hdmi_spec *spec = codec->spec;
  828. struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
  829. hda_nid_t pin_nid = per_pin->pin_nid;
  830. if (!(get_wcaps(codec, pin_nid) & AC_WCAP_CONN_LIST)) {
  831. snd_printk(KERN_WARNING
  832. "HDMI: pin %d wcaps %#x "
  833. "does not support connection list\n",
  834. pin_nid, get_wcaps(codec, pin_nid));
  835. return -EINVAL;
  836. }
  837. per_pin->num_mux_nids = snd_hda_get_connections(codec, pin_nid,
  838. per_pin->mux_nids,
  839. HDA_MAX_CONNECTIONS);
  840. return 0;
  841. }
  842. static void hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll)
  843. {
  844. struct hda_codec *codec = per_pin->codec;
  845. struct hdmi_eld *eld = &per_pin->sink_eld;
  846. hda_nid_t pin_nid = per_pin->pin_nid;
  847. /*
  848. * Always execute a GetPinSense verb here, even when called from
  849. * hdmi_intrinsic_event; for some NVIDIA HW, the unsolicited
  850. * response's PD bit is not the real PD value, but indicates that
  851. * the real PD value changed. An older version of the HD-audio
  852. * specification worked this way. Hence, we just ignore the data in
  853. * the unsolicited response to avoid custom WARs.
  854. */
  855. int present = snd_hda_pin_sense(codec, pin_nid);
  856. bool eld_valid = false;
  857. memset(eld, 0, offsetof(struct hdmi_eld, eld_buffer));
  858. eld->monitor_present = !!(present & AC_PINSENSE_PRESENCE);
  859. if (eld->monitor_present)
  860. eld_valid = !!(present & AC_PINSENSE_ELDV);
  861. _snd_printd(SND_PR_VERBOSE,
  862. "HDMI status: Codec=%d Pin=%d Presence_Detect=%d ELD_Valid=%d\n",
  863. codec->addr, pin_nid, eld->monitor_present, eld_valid);
  864. eld->eld_valid = false;
  865. if (eld_valid) {
  866. if (!snd_hdmi_get_eld(eld, codec, pin_nid))
  867. snd_hdmi_show_eld(eld);
  868. else if (repoll) {
  869. queue_delayed_work(codec->bus->workq,
  870. &per_pin->work,
  871. msecs_to_jiffies(300));
  872. }
  873. }
  874. }
  875. static void hdmi_repoll_eld(struct work_struct *work)
  876. {
  877. struct hdmi_spec_per_pin *per_pin =
  878. container_of(to_delayed_work(work), struct hdmi_spec_per_pin, work);
  879. if (per_pin->repoll_count++ > 6)
  880. per_pin->repoll_count = 0;
  881. hdmi_present_sense(per_pin, per_pin->repoll_count);
  882. }
  883. static int hdmi_add_pin(struct hda_codec *codec, hda_nid_t pin_nid)
  884. {
  885. struct hdmi_spec *spec = codec->spec;
  886. unsigned int caps, config;
  887. int pin_idx;
  888. struct hdmi_spec_per_pin *per_pin;
  889. int err;
  890. caps = snd_hda_param_read(codec, pin_nid, AC_PAR_PIN_CAP);
  891. if (!(caps & (AC_PINCAP_HDMI | AC_PINCAP_DP)))
  892. return 0;
  893. config = snd_hda_codec_read(codec, pin_nid, 0,
  894. AC_VERB_GET_CONFIG_DEFAULT, 0);
  895. if (get_defcfg_connect(config) == AC_JACK_PORT_NONE)
  896. return 0;
  897. if (snd_BUG_ON(spec->num_pins >= MAX_HDMI_PINS))
  898. return -E2BIG;
  899. pin_idx = spec->num_pins;
  900. per_pin = &spec->pins[pin_idx];
  901. per_pin->pin_nid = pin_nid;
  902. err = hdmi_read_pin_conn(codec, pin_idx);
  903. if (err < 0)
  904. return err;
  905. spec->num_pins++;
  906. return 0;
  907. }
  908. static int hdmi_add_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
  909. {
  910. struct hdmi_spec *spec = codec->spec;
  911. int cvt_idx;
  912. struct hdmi_spec_per_cvt *per_cvt;
  913. unsigned int chans;
  914. int err;
  915. if (snd_BUG_ON(spec->num_cvts >= MAX_HDMI_CVTS))
  916. return -E2BIG;
  917. chans = get_wcaps(codec, cvt_nid);
  918. chans = get_wcaps_channels(chans);
  919. cvt_idx = spec->num_cvts;
  920. per_cvt = &spec->cvts[cvt_idx];
  921. per_cvt->cvt_nid = cvt_nid;
  922. per_cvt->channels_min = 2;
  923. if (chans <= 16)
  924. per_cvt->channels_max = chans;
  925. err = snd_hda_query_supported_pcm(codec, cvt_nid,
  926. &per_cvt->rates,
  927. &per_cvt->formats,
  928. &per_cvt->maxbps);
  929. if (err < 0)
  930. return err;
  931. spec->num_cvts++;
  932. return 0;
  933. }
  934. static int hdmi_parse_codec(struct hda_codec *codec)
  935. {
  936. hda_nid_t nid;
  937. int i, nodes;
  938. nodes = snd_hda_get_sub_nodes(codec, codec->afg, &nid);
  939. if (!nid || nodes < 0) {
  940. snd_printk(KERN_WARNING "HDMI: failed to get afg sub nodes\n");
  941. return -EINVAL;
  942. }
  943. for (i = 0; i < nodes; i++, nid++) {
  944. unsigned int caps;
  945. unsigned int type;
  946. caps = snd_hda_param_read(codec, nid, AC_PAR_AUDIO_WIDGET_CAP);
  947. type = get_wcaps_type(caps);
  948. if (!(caps & AC_WCAP_DIGITAL))
  949. continue;
  950. switch (type) {
  951. case AC_WID_AUD_OUT:
  952. hdmi_add_cvt(codec, nid);
  953. break;
  954. case AC_WID_PIN:
  955. hdmi_add_pin(codec, nid);
  956. break;
  957. }
  958. }
  959. /*
  960. * G45/IbexPeak don't support EPSS: the unsolicited pin hot plug event
  961. * can be lost and presence sense verb will become inaccurate if the
  962. * HDA link is powered off at hot plug or hw initialization time.
  963. */
  964. #ifdef CONFIG_SND_HDA_POWER_SAVE
  965. if (!(snd_hda_param_read(codec, codec->afg, AC_PAR_POWER_STATE) &
  966. AC_PWRST_EPSS))
  967. codec->bus->power_keep_link_on = 1;
  968. #endif
  969. return 0;
  970. }
  971. /*
  972. */
  973. static char *get_hdmi_pcm_name(int idx)
  974. {
  975. static char names[MAX_HDMI_PINS][8];
  976. sprintf(&names[idx][0], "HDMI %d", idx);
  977. return &names[idx][0];
  978. }
  979. /*
  980. * HDMI callbacks
  981. */
  982. static int generic_hdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
  983. struct hda_codec *codec,
  984. unsigned int stream_tag,
  985. unsigned int format,
  986. struct snd_pcm_substream *substream)
  987. {
  988. hda_nid_t cvt_nid = hinfo->nid;
  989. struct hdmi_spec *spec = codec->spec;
  990. int pin_idx = hinfo_to_pin_index(spec, hinfo);
  991. hda_nid_t pin_nid = spec->pins[pin_idx].pin_nid;
  992. hdmi_set_channel_count(codec, cvt_nid, substream->runtime->channels);
  993. hdmi_setup_audio_infoframe(codec, pin_idx, substream);
  994. return hdmi_setup_stream(codec, cvt_nid, pin_nid, stream_tag, format);
  995. }
  996. static int generic_hdmi_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
  997. struct hda_codec *codec,
  998. struct snd_pcm_substream *substream)
  999. {
  1000. struct hdmi_spec *spec = codec->spec;
  1001. int cvt_idx, pin_idx;
  1002. struct hdmi_spec_per_cvt *per_cvt;
  1003. struct hdmi_spec_per_pin *per_pin;
  1004. snd_hda_codec_cleanup_stream(codec, hinfo->nid);
  1005. if (hinfo->nid) {
  1006. cvt_idx = cvt_nid_to_cvt_index(spec, hinfo->nid);
  1007. if (snd_BUG_ON(cvt_idx < 0))
  1008. return -EINVAL;
  1009. per_cvt = &spec->cvts[cvt_idx];
  1010. snd_BUG_ON(!per_cvt->assigned);
  1011. per_cvt->assigned = 0;
  1012. hinfo->nid = 0;
  1013. pin_idx = hinfo_to_pin_index(spec, hinfo);
  1014. if (snd_BUG_ON(pin_idx < 0))
  1015. return -EINVAL;
  1016. per_pin = &spec->pins[pin_idx];
  1017. snd_hda_spdif_ctls_unassign(codec, pin_idx);
  1018. }
  1019. return 0;
  1020. }
  1021. static const struct hda_pcm_ops generic_ops = {
  1022. .open = hdmi_pcm_open,
  1023. .prepare = generic_hdmi_playback_pcm_prepare,
  1024. .cleanup = generic_hdmi_playback_pcm_cleanup,
  1025. };
  1026. static int generic_hdmi_build_pcms(struct hda_codec *codec)
  1027. {
  1028. struct hdmi_spec *spec = codec->spec;
  1029. int pin_idx;
  1030. for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
  1031. struct hda_pcm *info;
  1032. struct hda_pcm_stream *pstr;
  1033. info = &spec->pcm_rec[pin_idx];
  1034. info->name = get_hdmi_pcm_name(pin_idx);
  1035. info->pcm_type = HDA_PCM_TYPE_HDMI;
  1036. pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
  1037. pstr->substreams = 1;
  1038. pstr->ops = generic_ops;
  1039. /* other pstr fields are set in open */
  1040. }
  1041. codec->num_pcms = spec->num_pins;
  1042. codec->pcm_info = spec->pcm_rec;
  1043. return 0;
  1044. }
  1045. static int generic_hdmi_build_jack(struct hda_codec *codec, int pin_idx)
  1046. {
  1047. char hdmi_str[32] = "HDMI/DP";
  1048. struct hdmi_spec *spec = codec->spec;
  1049. struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
  1050. int pcmdev = spec->pcm_rec[pin_idx].device;
  1051. if (pcmdev > 0)
  1052. sprintf(hdmi_str + strlen(hdmi_str), ",pcm=%d", pcmdev);
  1053. return snd_hda_jack_add_kctl(codec, per_pin->pin_nid, hdmi_str, 0);
  1054. }
  1055. static int generic_hdmi_build_controls(struct hda_codec *codec)
  1056. {
  1057. struct hdmi_spec *spec = codec->spec;
  1058. int err;
  1059. int pin_idx;
  1060. for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
  1061. struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
  1062. err = generic_hdmi_build_jack(codec, pin_idx);
  1063. if (err < 0)
  1064. return err;
  1065. err = snd_hda_create_spdif_out_ctls(codec,
  1066. per_pin->pin_nid,
  1067. per_pin->mux_nids[0]);
  1068. if (err < 0)
  1069. return err;
  1070. snd_hda_spdif_ctls_unassign(codec, pin_idx);
  1071. /* add control for ELD Bytes */
  1072. err = hdmi_create_eld_ctl(codec,
  1073. pin_idx,
  1074. spec->pcm_rec[pin_idx].device);
  1075. if (err < 0)
  1076. return err;
  1077. hdmi_present_sense(per_pin, 0);
  1078. }
  1079. return 0;
  1080. }
  1081. static int generic_hdmi_init_per_pins(struct hda_codec *codec)
  1082. {
  1083. struct hdmi_spec *spec = codec->spec;
  1084. int pin_idx;
  1085. for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
  1086. struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
  1087. struct hdmi_eld *eld = &per_pin->sink_eld;
  1088. per_pin->codec = codec;
  1089. INIT_DELAYED_WORK(&per_pin->work, hdmi_repoll_eld);
  1090. snd_hda_eld_proc_new(codec, eld, pin_idx);
  1091. }
  1092. return 0;
  1093. }
  1094. static int generic_hdmi_init(struct hda_codec *codec)
  1095. {
  1096. struct hdmi_spec *spec = codec->spec;
  1097. int pin_idx;
  1098. for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
  1099. struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
  1100. hda_nid_t pin_nid = per_pin->pin_nid;
  1101. hdmi_init_pin(codec, pin_nid);
  1102. snd_hda_jack_detect_enable(codec, pin_nid, pin_nid);
  1103. }
  1104. snd_hda_jack_report_sync(codec);
  1105. return 0;
  1106. }
  1107. static void generic_hdmi_free(struct hda_codec *codec)
  1108. {
  1109. struct hdmi_spec *spec = codec->spec;
  1110. int pin_idx;
  1111. for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
  1112. struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
  1113. struct hdmi_eld *eld = &per_pin->sink_eld;
  1114. cancel_delayed_work(&per_pin->work);
  1115. snd_hda_eld_proc_free(codec, eld);
  1116. }
  1117. flush_workqueue(codec->bus->workq);
  1118. kfree(spec);
  1119. }
  1120. static const struct hda_codec_ops generic_hdmi_patch_ops = {
  1121. .init = generic_hdmi_init,
  1122. .free = generic_hdmi_free,
  1123. .build_pcms = generic_hdmi_build_pcms,
  1124. .build_controls = generic_hdmi_build_controls,
  1125. .unsol_event = hdmi_unsol_event,
  1126. };
  1127. static int patch_generic_hdmi(struct hda_codec *codec)
  1128. {
  1129. struct hdmi_spec *spec;
  1130. spec = kzalloc(sizeof(*spec), GFP_KERNEL);
  1131. if (spec == NULL)
  1132. return -ENOMEM;
  1133. codec->spec = spec;
  1134. if (hdmi_parse_codec(codec) < 0) {
  1135. codec->spec = NULL;
  1136. kfree(spec);
  1137. return -EINVAL;
  1138. }
  1139. codec->patch_ops = generic_hdmi_patch_ops;
  1140. generic_hdmi_init_per_pins(codec);
  1141. init_channel_allocations();
  1142. return 0;
  1143. }
  1144. /*
  1145. * Shared non-generic implementations
  1146. */
  1147. static int simple_playback_build_pcms(struct hda_codec *codec)
  1148. {
  1149. struct hdmi_spec *spec = codec->spec;
  1150. struct hda_pcm *info = spec->pcm_rec;
  1151. int i;
  1152. codec->num_pcms = spec->num_cvts;
  1153. codec->pcm_info = info;
  1154. for (i = 0; i < codec->num_pcms; i++, info++) {
  1155. unsigned int chans;
  1156. struct hda_pcm_stream *pstr;
  1157. chans = get_wcaps(codec, spec->cvts[i].cvt_nid);
  1158. chans = get_wcaps_channels(chans);
  1159. info->name = get_hdmi_pcm_name(i);
  1160. info->pcm_type = HDA_PCM_TYPE_HDMI;
  1161. pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
  1162. snd_BUG_ON(!spec->pcm_playback);
  1163. *pstr = *spec->pcm_playback;
  1164. pstr->nid = spec->cvts[i].cvt_nid;
  1165. if (pstr->channels_max <= 2 && chans && chans <= 16)
  1166. pstr->channels_max = chans;
  1167. }
  1168. return 0;
  1169. }
  1170. static int simple_playback_build_controls(struct hda_codec *codec)
  1171. {
  1172. struct hdmi_spec *spec = codec->spec;
  1173. int err;
  1174. int i;
  1175. for (i = 0; i < codec->num_pcms; i++) {
  1176. err = snd_hda_create_spdif_out_ctls(codec,
  1177. spec->cvts[i].cvt_nid,
  1178. spec->cvts[i].cvt_nid);
  1179. if (err < 0)
  1180. return err;
  1181. }
  1182. return 0;
  1183. }
  1184. static void simple_playback_free(struct hda_codec *codec)
  1185. {
  1186. struct hdmi_spec *spec = codec->spec;
  1187. kfree(spec);
  1188. }
  1189. /*
  1190. * Nvidia specific implementations
  1191. */
  1192. #define Nv_VERB_SET_Channel_Allocation 0xF79
  1193. #define Nv_VERB_SET_Info_Frame_Checksum 0xF7A
  1194. #define Nv_VERB_SET_Audio_Protection_On 0xF98
  1195. #define Nv_VERB_SET_Audio_Protection_Off 0xF99
  1196. #define nvhdmi_master_con_nid_7x 0x04
  1197. #define nvhdmi_master_pin_nid_7x 0x05
  1198. static const hda_nid_t nvhdmi_con_nids_7x[4] = {
  1199. /*front, rear, clfe, rear_surr */
  1200. 0x6, 0x8, 0xa, 0xc,
  1201. };
  1202. static const struct hda_verb nvhdmi_basic_init_7x[] = {
  1203. /* set audio protect on */
  1204. { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
  1205. /* enable digital output on pin widget */
  1206. { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
  1207. { 0x7, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
  1208. { 0x9, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
  1209. { 0xb, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
  1210. { 0xd, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
  1211. {} /* terminator */
  1212. };
  1213. #ifdef LIMITED_RATE_FMT_SUPPORT
  1214. /* support only the safe format and rate */
  1215. #define SUPPORTED_RATES SNDRV_PCM_RATE_48000
  1216. #define SUPPORTED_MAXBPS 16
  1217. #define SUPPORTED_FORMATS SNDRV_PCM_FMTBIT_S16_LE
  1218. #else
  1219. /* support all rates and formats */
  1220. #define SUPPORTED_RATES \
  1221. (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
  1222. SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |\
  1223. SNDRV_PCM_RATE_192000)
  1224. #define SUPPORTED_MAXBPS 24
  1225. #define SUPPORTED_FORMATS \
  1226. (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE)
  1227. #endif
  1228. static int nvhdmi_7x_init(struct hda_codec *codec)
  1229. {
  1230. snd_hda_sequence_write(codec, nvhdmi_basic_init_7x);
  1231. return 0;
  1232. }
  1233. static unsigned int channels_2_6_8[] = {
  1234. 2, 6, 8
  1235. };
  1236. static unsigned int channels_2_8[] = {
  1237. 2, 8
  1238. };
  1239. static struct snd_pcm_hw_constraint_list hw_constraints_2_6_8_channels = {
  1240. .count = ARRAY_SIZE(channels_2_6_8),
  1241. .list = channels_2_6_8,
  1242. .mask = 0,
  1243. };
  1244. static struct snd_pcm_hw_constraint_list hw_constraints_2_8_channels = {
  1245. .count = ARRAY_SIZE(channels_2_8),
  1246. .list = channels_2_8,
  1247. .mask = 0,
  1248. };
  1249. static int simple_playback_pcm_open(struct hda_pcm_stream *hinfo,
  1250. struct hda_codec *codec,
  1251. struct snd_pcm_substream *substream)
  1252. {
  1253. struct hdmi_spec *spec = codec->spec;
  1254. struct snd_pcm_hw_constraint_list *hw_constraints_channels = NULL;
  1255. switch (codec->preset->id) {
  1256. case 0x10de0002:
  1257. case 0x10de0003:
  1258. case 0x10de0005:
  1259. case 0x10de0006:
  1260. hw_constraints_channels = &hw_constraints_2_8_channels;
  1261. break;
  1262. case 0x10de0007:
  1263. hw_constraints_channels = &hw_constraints_2_6_8_channels;
  1264. break;
  1265. default:
  1266. break;
  1267. }
  1268. if (hw_constraints_channels != NULL) {
  1269. snd_pcm_hw_constraint_list(substream->runtime, 0,
  1270. SNDRV_PCM_HW_PARAM_CHANNELS,
  1271. hw_constraints_channels);
  1272. } else {
  1273. snd_pcm_hw_constraint_step(substream->runtime, 0,
  1274. SNDRV_PCM_HW_PARAM_CHANNELS, 2);
  1275. }
  1276. return snd_hda_multi_out_dig_open(codec, &spec->multiout);
  1277. }
  1278. static int simple_playback_pcm_close(struct hda_pcm_stream *hinfo,
  1279. struct hda_codec *codec,
  1280. struct snd_pcm_substream *substream)
  1281. {
  1282. struct hdmi_spec *spec = codec->spec;
  1283. return snd_hda_multi_out_dig_close(codec, &spec->multiout);
  1284. }
  1285. static int simple_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
  1286. struct hda_codec *codec,
  1287. unsigned int stream_tag,
  1288. unsigned int format,
  1289. struct snd_pcm_substream *substream)
  1290. {
  1291. struct hdmi_spec *spec = codec->spec;
  1292. return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
  1293. stream_tag, format, substream);
  1294. }
  1295. static void nvhdmi_8ch_7x_set_info_frame_parameters(struct hda_codec *codec,
  1296. int channels)
  1297. {
  1298. unsigned int chanmask;
  1299. int chan = channels ? (channels - 1) : 1;
  1300. switch (channels) {
  1301. default:
  1302. case 0:
  1303. case 2:
  1304. chanmask = 0x00;
  1305. break;
  1306. case 4:
  1307. chanmask = 0x08;
  1308. break;
  1309. case 6:
  1310. chanmask = 0x0b;
  1311. break;
  1312. case 8:
  1313. chanmask = 0x13;
  1314. break;
  1315. }
  1316. /* Set the audio infoframe channel allocation and checksum fields. The
  1317. * channel count is computed implicitly by the hardware. */
  1318. snd_hda_codec_write(codec, 0x1, 0,
  1319. Nv_VERB_SET_Channel_Allocation, chanmask);
  1320. snd_hda_codec_write(codec, 0x1, 0,
  1321. Nv_VERB_SET_Info_Frame_Checksum,
  1322. (0x71 - chan - chanmask));
  1323. }
  1324. static int nvhdmi_8ch_7x_pcm_close(struct hda_pcm_stream *hinfo,
  1325. struct hda_codec *codec,
  1326. struct snd_pcm_substream *substream)
  1327. {
  1328. struct hdmi_spec *spec = codec->spec;
  1329. int i;
  1330. snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x,
  1331. 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
  1332. for (i = 0; i < 4; i++) {
  1333. /* set the stream id */
  1334. snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
  1335. AC_VERB_SET_CHANNEL_STREAMID, 0);
  1336. /* set the stream format */
  1337. snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
  1338. AC_VERB_SET_STREAM_FORMAT, 0);
  1339. }
  1340. /* The audio hardware sends a channel count of 0x7 (8ch) when all the
  1341. * streams are disabled. */
  1342. nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
  1343. return snd_hda_multi_out_dig_close(codec, &spec->multiout);
  1344. }
  1345. static int nvhdmi_8ch_7x_pcm_prepare(struct hda_pcm_stream *hinfo,
  1346. struct hda_codec *codec,
  1347. unsigned int stream_tag,
  1348. unsigned int format,
  1349. struct snd_pcm_substream *substream)
  1350. {
  1351. int chs;
  1352. unsigned int dataDCC2, channel_id;
  1353. int i;
  1354. struct hdmi_spec *spec = codec->spec;
  1355. struct hda_spdif_out *spdif =
  1356. snd_hda_spdif_out_of_nid(codec, spec->cvts[0].cvt_nid);
  1357. mutex_lock(&codec->spdif_mutex);
  1358. chs = substream->runtime->channels;
  1359. dataDCC2 = 0x2;
  1360. /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
  1361. if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
  1362. snd_hda_codec_write(codec,
  1363. nvhdmi_master_con_nid_7x,
  1364. 0,
  1365. AC_VERB_SET_DIGI_CONVERT_1,
  1366. spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
  1367. /* set the stream id */
  1368. snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
  1369. AC_VERB_SET_CHANNEL_STREAMID, (stream_tag << 4) | 0x0);
  1370. /* set the stream format */
  1371. snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
  1372. AC_VERB_SET_STREAM_FORMAT, format);
  1373. /* turn on again (if needed) */
  1374. /* enable and set the channel status audio/data flag */
  1375. if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE)) {
  1376. snd_hda_codec_write(codec,
  1377. nvhdmi_master_con_nid_7x,
  1378. 0,
  1379. AC_VERB_SET_DIGI_CONVERT_1,
  1380. spdif->ctls & 0xff);
  1381. snd_hda_codec_write(codec,
  1382. nvhdmi_master_con_nid_7x,
  1383. 0,
  1384. AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
  1385. }
  1386. for (i = 0; i < 4; i++) {
  1387. if (chs == 2)
  1388. channel_id = 0;
  1389. else
  1390. channel_id = i * 2;
  1391. /* turn off SPDIF once;
  1392. *otherwise the IEC958 bits won't be updated
  1393. */
  1394. if (codec->spdif_status_reset &&
  1395. (spdif->ctls & AC_DIG1_ENABLE))
  1396. snd_hda_codec_write(codec,
  1397. nvhdmi_con_nids_7x[i],
  1398. 0,
  1399. AC_VERB_SET_DIGI_CONVERT_1,
  1400. spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
  1401. /* set the stream id */
  1402. snd_hda_codec_write(codec,
  1403. nvhdmi_con_nids_7x[i],
  1404. 0,
  1405. AC_VERB_SET_CHANNEL_STREAMID,
  1406. (stream_tag << 4) | channel_id);
  1407. /* set the stream format */
  1408. snd_hda_codec_write(codec,
  1409. nvhdmi_con_nids_7x[i],
  1410. 0,
  1411. AC_VERB_SET_STREAM_FORMAT,
  1412. format);
  1413. /* turn on again (if needed) */
  1414. /* enable and set the channel status audio/data flag */
  1415. if (codec->spdif_status_reset &&
  1416. (spdif->ctls & AC_DIG1_ENABLE)) {
  1417. snd_hda_codec_write(codec,
  1418. nvhdmi_con_nids_7x[i],
  1419. 0,
  1420. AC_VERB_SET_DIGI_CONVERT_1,
  1421. spdif->ctls & 0xff);
  1422. snd_hda_codec_write(codec,
  1423. nvhdmi_con_nids_7x[i],
  1424. 0,
  1425. AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
  1426. }
  1427. }
  1428. nvhdmi_8ch_7x_set_info_frame_parameters(codec, chs);
  1429. mutex_unlock(&codec->spdif_mutex);
  1430. return 0;
  1431. }
  1432. static const struct hda_pcm_stream nvhdmi_pcm_playback_8ch_7x = {
  1433. .substreams = 1,
  1434. .channels_min = 2,
  1435. .channels_max = 8,
  1436. .nid = nvhdmi_master_con_nid_7x,
  1437. .rates = SUPPORTED_RATES,
  1438. .maxbps = SUPPORTED_MAXBPS,
  1439. .formats = SUPPORTED_FORMATS,
  1440. .ops = {
  1441. .open = simple_playback_pcm_open,
  1442. .close = nvhdmi_8ch_7x_pcm_close,
  1443. .prepare = nvhdmi_8ch_7x_pcm_prepare
  1444. },
  1445. };
  1446. static const struct hda_pcm_stream nvhdmi_pcm_playback_2ch = {
  1447. .substreams = 1,
  1448. .channels_min = 2,
  1449. .channels_max = 2,
  1450. .nid = nvhdmi_master_con_nid_7x,
  1451. .rates = SUPPORTED_RATES,
  1452. .maxbps = SUPPORTED_MAXBPS,
  1453. .formats = SUPPORTED_FORMATS,
  1454. .ops = {
  1455. .open = simple_playback_pcm_open,
  1456. .close = simple_playback_pcm_close,
  1457. .prepare = simple_playback_pcm_prepare
  1458. },
  1459. };
  1460. static const struct hda_codec_ops nvhdmi_patch_ops_8ch_7x = {
  1461. .build_controls = simple_playback_build_controls,
  1462. .build_pcms = simple_playback_build_pcms,
  1463. .init = nvhdmi_7x_init,
  1464. .free = simple_playback_free,
  1465. };
  1466. static const struct hda_codec_ops nvhdmi_patch_ops_2ch = {
  1467. .build_controls = simple_playback_build_controls,
  1468. .build_pcms = simple_playback_build_pcms,
  1469. .init = nvhdmi_7x_init,
  1470. .free = simple_playback_free,
  1471. };
  1472. static int patch_nvhdmi_2ch(struct hda_codec *codec)
  1473. {
  1474. struct hdmi_spec *spec;
  1475. spec = kzalloc(sizeof(*spec), GFP_KERNEL);
  1476. if (spec == NULL)
  1477. return -ENOMEM;
  1478. codec->spec = spec;
  1479. spec->multiout.num_dacs = 0; /* no analog */
  1480. spec->multiout.max_channels = 2;
  1481. spec->multiout.dig_out_nid = nvhdmi_master_con_nid_7x;
  1482. spec->num_cvts = 1;
  1483. spec->cvts[0].cvt_nid = nvhdmi_master_con_nid_7x;
  1484. spec->pcm_playback = &nvhdmi_pcm_playback_2ch;
  1485. codec->patch_ops = nvhdmi_patch_ops_2ch;
  1486. return 0;
  1487. }
  1488. static int patch_nvhdmi_8ch_7x(struct hda_codec *codec)
  1489. {
  1490. struct hdmi_spec *spec;
  1491. int err = patch_nvhdmi_2ch(codec);
  1492. if (err < 0)
  1493. return err;
  1494. spec = codec->spec;
  1495. spec->multiout.max_channels = 8;
  1496. spec->pcm_playback = &nvhdmi_pcm_playback_8ch_7x;
  1497. codec->patch_ops = nvhdmi_patch_ops_8ch_7x;
  1498. /* Initialize the audio infoframe channel mask and checksum to something
  1499. * valid */
  1500. nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
  1501. return 0;
  1502. }
  1503. /*
  1504. * ATI-specific implementations
  1505. *
  1506. * FIXME: we may omit the whole this and use the generic code once after
  1507. * it's confirmed to work.
  1508. */
  1509. #define ATIHDMI_CVT_NID 0x02 /* audio converter */
  1510. #define ATIHDMI_PIN_NID 0x03 /* HDMI output pin */
  1511. static int atihdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
  1512. struct hda_codec *codec,
  1513. unsigned int stream_tag,
  1514. unsigned int format,
  1515. struct snd_pcm_substream *substream)
  1516. {
  1517. struct hdmi_spec *spec = codec->spec;
  1518. int chans = substream->runtime->channels;
  1519. int i, err;
  1520. err = simple_playback_pcm_prepare(hinfo, codec, stream_tag, format,
  1521. substream);
  1522. if (err < 0)
  1523. return err;
  1524. snd_hda_codec_write(codec, spec->cvts[0].cvt_nid, 0,
  1525. AC_VERB_SET_CVT_CHAN_COUNT, chans - 1);
  1526. /* FIXME: XXX */
  1527. for (i = 0; i < chans; i++) {
  1528. snd_hda_codec_write(codec, spec->cvts[0].cvt_nid, 0,
  1529. AC_VERB_SET_HDMI_CHAN_SLOT,
  1530. (i << 4) | i);
  1531. }
  1532. return 0;
  1533. }
  1534. static const struct hda_pcm_stream atihdmi_pcm_digital_playback = {
  1535. .substreams = 1,
  1536. .channels_min = 2,
  1537. .channels_max = 2,
  1538. .nid = ATIHDMI_CVT_NID,
  1539. .ops = {
  1540. .open = simple_playback_pcm_open,
  1541. .close = simple_playback_pcm_close,
  1542. .prepare = atihdmi_playback_pcm_prepare
  1543. },
  1544. };
  1545. static const struct hda_verb atihdmi_basic_init[] = {
  1546. /* enable digital output on pin widget */
  1547. { 0x03, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
  1548. {} /* terminator */
  1549. };
  1550. static int atihdmi_init(struct hda_codec *codec)
  1551. {
  1552. struct hdmi_spec *spec = codec->spec;
  1553. snd_hda_sequence_write(codec, atihdmi_basic_init);
  1554. /* SI codec requires to unmute the pin */
  1555. if (get_wcaps(codec, spec->pins[0].pin_nid) & AC_WCAP_OUT_AMP)
  1556. snd_hda_codec_write(codec, spec->pins[0].pin_nid, 0,
  1557. AC_VERB_SET_AMP_GAIN_MUTE,
  1558. AMP_OUT_UNMUTE);
  1559. return 0;
  1560. }
  1561. static const struct hda_codec_ops atihdmi_patch_ops = {
  1562. .build_controls = simple_playback_build_controls,
  1563. .build_pcms = simple_playback_build_pcms,
  1564. .init = atihdmi_init,
  1565. .free = simple_playback_free,
  1566. };
  1567. static int patch_atihdmi(struct hda_codec *codec)
  1568. {
  1569. struct hdmi_spec *spec;
  1570. spec = kzalloc(sizeof(*spec), GFP_KERNEL);
  1571. if (spec == NULL)
  1572. return -ENOMEM;
  1573. codec->spec = spec;
  1574. spec->multiout.num_dacs = 0; /* no analog */
  1575. spec->multiout.max_channels = 2;
  1576. spec->multiout.dig_out_nid = ATIHDMI_CVT_NID;
  1577. spec->num_cvts = 1;
  1578. spec->cvts[0].cvt_nid = ATIHDMI_CVT_NID;
  1579. spec->pins[0].pin_nid = ATIHDMI_PIN_NID;
  1580. spec->pcm_playback = &atihdmi_pcm_digital_playback;
  1581. codec->patch_ops = atihdmi_patch_ops;
  1582. return 0;
  1583. }
  1584. /*
  1585. * patch entries
  1586. */
  1587. static const struct hda_codec_preset snd_hda_preset_hdmi[] = {
  1588. { .id = 0x1002793c, .name = "RS600 HDMI", .patch = patch_atihdmi },
  1589. { .id = 0x10027919, .name = "RS600 HDMI", .patch = patch_atihdmi },
  1590. { .id = 0x1002791a, .name = "RS690/780 HDMI", .patch = patch_atihdmi },
  1591. { .id = 0x1002aa01, .name = "R6xx HDMI", .patch = patch_generic_hdmi },
  1592. { .id = 0x10951390, .name = "SiI1390 HDMI", .patch = patch_generic_hdmi },
  1593. { .id = 0x10951392, .name = "SiI1392 HDMI", .patch = patch_generic_hdmi },
  1594. { .id = 0x17e80047, .name = "Chrontel HDMI", .patch = patch_generic_hdmi },
  1595. { .id = 0x10de0002, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
  1596. { .id = 0x10de0003, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
  1597. { .id = 0x10de0005, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
  1598. { .id = 0x10de0006, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
  1599. { .id = 0x10de0007, .name = "MCP79/7A HDMI", .patch = patch_nvhdmi_8ch_7x },
  1600. { .id = 0x10de000a, .name = "GPU 0a HDMI/DP", .patch = patch_generic_hdmi },
  1601. { .id = 0x10de000b, .name = "GPU 0b HDMI/DP", .patch = patch_generic_hdmi },
  1602. { .id = 0x10de000c, .name = "MCP89 HDMI", .patch = patch_generic_hdmi },
  1603. { .id = 0x10de000d, .name = "GPU 0d HDMI/DP", .patch = patch_generic_hdmi },
  1604. { .id = 0x10de0010, .name = "GPU 10 HDMI/DP", .patch = patch_generic_hdmi },
  1605. { .id = 0x10de0011, .name = "GPU 11 HDMI/DP", .patch = patch_generic_hdmi },
  1606. { .id = 0x10de0012, .name = "GPU 12 HDMI/DP", .patch = patch_generic_hdmi },
  1607. { .id = 0x10de0013, .name = "GPU 13 HDMI/DP", .patch = patch_generic_hdmi },
  1608. { .id = 0x10de0014, .name = "GPU 14 HDMI/DP", .patch = patch_generic_hdmi },
  1609. { .id = 0x10de0015, .name = "GPU 15 HDMI/DP", .patch = patch_generic_hdmi },
  1610. { .id = 0x10de0016, .name = "GPU 16 HDMI/DP", .patch = patch_generic_hdmi },
  1611. /* 17 is known to be absent */
  1612. { .id = 0x10de0018, .name = "GPU 18 HDMI/DP", .patch = patch_generic_hdmi },
  1613. { .id = 0x10de0019, .name = "GPU 19 HDMI/DP", .patch = patch_generic_hdmi },
  1614. { .id = 0x10de001a, .name = "GPU 1a HDMI/DP", .patch = patch_generic_hdmi },
  1615. { .id = 0x10de001b, .name = "GPU 1b HDMI/DP", .patch = patch_generic_hdmi },
  1616. { .id = 0x10de001c, .name = "GPU 1c HDMI/DP", .patch = patch_generic_hdmi },
  1617. { .id = 0x10de0040, .name = "GPU 40 HDMI/DP", .patch = patch_generic_hdmi },
  1618. { .id = 0x10de0041, .name = "GPU 41 HDMI/DP", .patch = patch_generic_hdmi },
  1619. { .id = 0x10de0042, .name = "GPU 42 HDMI/DP", .patch = patch_generic_hdmi },
  1620. { .id = 0x10de0043, .name = "GPU 43 HDMI/DP", .patch = patch_generic_hdmi },
  1621. { .id = 0x10de0044, .name = "GPU 44 HDMI/DP", .patch = patch_generic_hdmi },
  1622. { .id = 0x10de0051, .name = "GPU 51 HDMI/DP", .patch = patch_generic_hdmi },
  1623. { .id = 0x10de0060, .name = "GPU 60 HDMI/DP", .patch = patch_generic_hdmi },
  1624. { .id = 0x10de0067, .name = "MCP67 HDMI", .patch = patch_nvhdmi_2ch },
  1625. { .id = 0x10de8001, .name = "MCP73 HDMI", .patch = patch_nvhdmi_2ch },
  1626. { .id = 0x80860054, .name = "IbexPeak HDMI", .patch = patch_generic_hdmi },
  1627. { .id = 0x80862801, .name = "Bearlake HDMI", .patch = patch_generic_hdmi },
  1628. { .id = 0x80862802, .name = "Cantiga HDMI", .patch = patch_generic_hdmi },
  1629. { .id = 0x80862803, .name = "Eaglelake HDMI", .patch = patch_generic_hdmi },
  1630. { .id = 0x80862804, .name = "IbexPeak HDMI", .patch = patch_generic_hdmi },
  1631. { .id = 0x80862805, .name = "CougarPoint HDMI", .patch = patch_generic_hdmi },
  1632. { .id = 0x80862806, .name = "PantherPoint HDMI", .patch = patch_generic_hdmi },
  1633. { .id = 0x80862880, .name = "CedarTrail HDMI", .patch = patch_generic_hdmi },
  1634. { .id = 0x808629fb, .name = "Crestline HDMI", .patch = patch_generic_hdmi },
  1635. {} /* terminator */
  1636. };
  1637. MODULE_ALIAS("snd-hda-codec-id:1002793c");
  1638. MODULE_ALIAS("snd-hda-codec-id:10027919");
  1639. MODULE_ALIAS("snd-hda-codec-id:1002791a");
  1640. MODULE_ALIAS("snd-hda-codec-id:1002aa01");
  1641. MODULE_ALIAS("snd-hda-codec-id:10951390");
  1642. MODULE_ALIAS("snd-hda-codec-id:10951392");
  1643. MODULE_ALIAS("snd-hda-codec-id:10de0002");
  1644. MODULE_ALIAS("snd-hda-codec-id:10de0003");
  1645. MODULE_ALIAS("snd-hda-codec-id:10de0005");
  1646. MODULE_ALIAS("snd-hda-codec-id:10de0006");
  1647. MODULE_ALIAS("snd-hda-codec-id:10de0007");
  1648. MODULE_ALIAS("snd-hda-codec-id:10de000a");
  1649. MODULE_ALIAS("snd-hda-codec-id:10de000b");
  1650. MODULE_ALIAS("snd-hda-codec-id:10de000c");
  1651. MODULE_ALIAS("snd-hda-codec-id:10de000d");
  1652. MODULE_ALIAS("snd-hda-codec-id:10de0010");
  1653. MODULE_ALIAS("snd-hda-codec-id:10de0011");
  1654. MODULE_ALIAS("snd-hda-codec-id:10de0012");
  1655. MODULE_ALIAS("snd-hda-codec-id:10de0013");
  1656. MODULE_ALIAS("snd-hda-codec-id:10de0014");
  1657. MODULE_ALIAS("snd-hda-codec-id:10de0015");
  1658. MODULE_ALIAS("snd-hda-codec-id:10de0016");
  1659. MODULE_ALIAS("snd-hda-codec-id:10de0018");
  1660. MODULE_ALIAS("snd-hda-codec-id:10de0019");
  1661. MODULE_ALIAS("snd-hda-codec-id:10de001a");
  1662. MODULE_ALIAS("snd-hda-codec-id:10de001b");
  1663. MODULE_ALIAS("snd-hda-codec-id:10de001c");
  1664. MODULE_ALIAS("snd-hda-codec-id:10de0040");
  1665. MODULE_ALIAS("snd-hda-codec-id:10de0041");
  1666. MODULE_ALIAS("snd-hda-codec-id:10de0042");
  1667. MODULE_ALIAS("snd-hda-codec-id:10de0043");
  1668. MODULE_ALIAS("snd-hda-codec-id:10de0044");
  1669. MODULE_ALIAS("snd-hda-codec-id:10de0051");
  1670. MODULE_ALIAS("snd-hda-codec-id:10de0060");
  1671. MODULE_ALIAS("snd-hda-codec-id:10de0067");
  1672. MODULE_ALIAS("snd-hda-codec-id:10de8001");
  1673. MODULE_ALIAS("snd-hda-codec-id:17e80047");
  1674. MODULE_ALIAS("snd-hda-codec-id:80860054");
  1675. MODULE_ALIAS("snd-hda-codec-id:80862801");
  1676. MODULE_ALIAS("snd-hda-codec-id:80862802");
  1677. MODULE_ALIAS("snd-hda-codec-id:80862803");
  1678. MODULE_ALIAS("snd-hda-codec-id:80862804");
  1679. MODULE_ALIAS("snd-hda-codec-id:80862805");
  1680. MODULE_ALIAS("snd-hda-codec-id:80862806");
  1681. MODULE_ALIAS("snd-hda-codec-id:80862880");
  1682. MODULE_ALIAS("snd-hda-codec-id:808629fb");
  1683. MODULE_LICENSE("GPL");
  1684. MODULE_DESCRIPTION("HDMI HD-audio codec");
  1685. MODULE_ALIAS("snd-hda-codec-intelhdmi");
  1686. MODULE_ALIAS("snd-hda-codec-nvhdmi");
  1687. MODULE_ALIAS("snd-hda-codec-atihdmi");
  1688. static struct hda_codec_preset_list intel_list = {
  1689. .preset = snd_hda_preset_hdmi,
  1690. .owner = THIS_MODULE,
  1691. };
  1692. static int __init patch_hdmi_init(void)
  1693. {
  1694. return snd_hda_add_codec_preset(&intel_list);
  1695. }
  1696. static void __exit patch_hdmi_exit(void)
  1697. {
  1698. snd_hda_delete_codec_preset(&intel_list);
  1699. }
  1700. module_init(patch_hdmi_init)
  1701. module_exit(patch_hdmi_exit)