patch_hdmi.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984
  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. static bool static_hdmi_pcm;
  39. module_param(static_hdmi_pcm, bool, 0644);
  40. MODULE_PARM_DESC(static_hdmi_pcm, "Don't restrict PCM parameters per ELD info");
  41. /*
  42. * The HDMI/DisplayPort configuration can be highly dynamic. A graphics device
  43. * could support N independent pipes, each of them can be connected to one or
  44. * more ports (DVI, HDMI or DisplayPort).
  45. *
  46. * The HDA correspondence of pipes/ports are converter/pin nodes.
  47. */
  48. #define MAX_HDMI_CVTS 4
  49. #define MAX_HDMI_PINS 4
  50. struct hdmi_spec_per_cvt {
  51. hda_nid_t cvt_nid;
  52. int assigned;
  53. unsigned int channels_min;
  54. unsigned int channels_max;
  55. u32 rates;
  56. u64 formats;
  57. unsigned int maxbps;
  58. };
  59. struct hdmi_spec_per_pin {
  60. hda_nid_t pin_nid;
  61. int num_mux_nids;
  62. hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
  63. struct hda_codec *codec;
  64. struct hdmi_eld sink_eld;
  65. struct delayed_work work;
  66. int repoll_count;
  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, int repoll);
  649. static void hdmi_intrinsic_event(struct hda_codec *codec, unsigned int res)
  650. {
  651. struct hdmi_spec *spec = codec->spec;
  652. int pin_nid = res >> AC_UNSOL_RES_TAG_SHIFT;
  653. int pd = !!(res & AC_UNSOL_RES_PD);
  654. int eldv = !!(res & AC_UNSOL_RES_ELDV);
  655. int pin_idx;
  656. printk(KERN_INFO
  657. "HDMI hot plug event: Codec=%d Pin=%d Presence_Detect=%d ELD_Valid=%d\n",
  658. codec->addr, pin_nid, pd, eldv);
  659. pin_idx = pin_nid_to_pin_index(spec, pin_nid);
  660. if (pin_idx < 0)
  661. return;
  662. hdmi_present_sense(&spec->pins[pin_idx], 1);
  663. }
  664. static void hdmi_non_intrinsic_event(struct hda_codec *codec, unsigned int res)
  665. {
  666. int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
  667. int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
  668. int cp_state = !!(res & AC_UNSOL_RES_CP_STATE);
  669. int cp_ready = !!(res & AC_UNSOL_RES_CP_READY);
  670. printk(KERN_INFO
  671. "HDMI CP event: CODEC=%d PIN=%d SUBTAG=0x%x CP_STATE=%d CP_READY=%d\n",
  672. codec->addr,
  673. tag,
  674. subtag,
  675. cp_state,
  676. cp_ready);
  677. /* TODO */
  678. if (cp_state)
  679. ;
  680. if (cp_ready)
  681. ;
  682. }
  683. static void hdmi_unsol_event(struct hda_codec *codec, unsigned int res)
  684. {
  685. struct hdmi_spec *spec = codec->spec;
  686. int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
  687. int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
  688. if (pin_nid_to_pin_index(spec, tag) < 0) {
  689. snd_printd(KERN_INFO "Unexpected HDMI event tag 0x%x\n", tag);
  690. return;
  691. }
  692. if (subtag == 0)
  693. hdmi_intrinsic_event(codec, res);
  694. else
  695. hdmi_non_intrinsic_event(codec, res);
  696. }
  697. /*
  698. * Callbacks
  699. */
  700. /* HBR should be Non-PCM, 8 channels */
  701. #define is_hbr_format(format) \
  702. ((format & AC_FMT_TYPE_NON_PCM) && (format & AC_FMT_CHAN_MASK) == 7)
  703. static int hdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
  704. hda_nid_t pin_nid, u32 stream_tag, int format)
  705. {
  706. int pinctl;
  707. int new_pinctl = 0;
  708. if (snd_hda_query_pin_caps(codec, pin_nid) & AC_PINCAP_HBR) {
  709. pinctl = snd_hda_codec_read(codec, pin_nid, 0,
  710. AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
  711. new_pinctl = pinctl & ~AC_PINCTL_EPT;
  712. if (is_hbr_format(format))
  713. new_pinctl |= AC_PINCTL_EPT_HBR;
  714. else
  715. new_pinctl |= AC_PINCTL_EPT_NATIVE;
  716. snd_printdd("hdmi_setup_stream: "
  717. "NID=0x%x, %spinctl=0x%x\n",
  718. pin_nid,
  719. pinctl == new_pinctl ? "" : "new-",
  720. new_pinctl);
  721. if (pinctl != new_pinctl)
  722. snd_hda_codec_write(codec, pin_nid, 0,
  723. AC_VERB_SET_PIN_WIDGET_CONTROL,
  724. new_pinctl);
  725. }
  726. if (is_hbr_format(format) && !new_pinctl) {
  727. snd_printdd("hdmi_setup_stream: HBR is not supported\n");
  728. return -EINVAL;
  729. }
  730. snd_hda_codec_setup_stream(codec, cvt_nid, stream_tag, 0, format);
  731. return 0;
  732. }
  733. /*
  734. * HDA PCM callbacks
  735. */
  736. static int hdmi_pcm_open(struct hda_pcm_stream *hinfo,
  737. struct hda_codec *codec,
  738. struct snd_pcm_substream *substream)
  739. {
  740. struct hdmi_spec *spec = codec->spec;
  741. struct snd_pcm_runtime *runtime = substream->runtime;
  742. int pin_idx, cvt_idx, mux_idx = 0;
  743. struct hdmi_spec_per_pin *per_pin;
  744. struct hdmi_eld *eld;
  745. struct hdmi_spec_per_cvt *per_cvt = NULL;
  746. int pinctl;
  747. /* Validate hinfo */
  748. pin_idx = hinfo_to_pin_index(spec, hinfo);
  749. if (snd_BUG_ON(pin_idx < 0))
  750. return -EINVAL;
  751. per_pin = &spec->pins[pin_idx];
  752. eld = &per_pin->sink_eld;
  753. /* Dynamically assign converter to stream */
  754. for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
  755. per_cvt = &spec->cvts[cvt_idx];
  756. /* Must not already be assigned */
  757. if (per_cvt->assigned)
  758. continue;
  759. /* Must be in pin's mux's list of converters */
  760. for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++)
  761. if (per_pin->mux_nids[mux_idx] == per_cvt->cvt_nid)
  762. break;
  763. /* Not in mux list */
  764. if (mux_idx == per_pin->num_mux_nids)
  765. continue;
  766. break;
  767. }
  768. /* No free converters */
  769. if (cvt_idx == spec->num_cvts)
  770. return -ENODEV;
  771. /* Claim converter */
  772. per_cvt->assigned = 1;
  773. hinfo->nid = per_cvt->cvt_nid;
  774. snd_hda_codec_write(codec, per_pin->pin_nid, 0,
  775. AC_VERB_SET_CONNECT_SEL,
  776. mux_idx);
  777. pinctl = snd_hda_codec_read(codec, per_pin->pin_nid, 0,
  778. AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
  779. snd_hda_codec_write(codec, per_pin->pin_nid, 0,
  780. AC_VERB_SET_PIN_WIDGET_CONTROL,
  781. pinctl | PIN_OUT);
  782. snd_hda_spdif_ctls_assign(codec, pin_idx, per_cvt->cvt_nid);
  783. /* Initially set the converter's capabilities */
  784. hinfo->channels_min = per_cvt->channels_min;
  785. hinfo->channels_max = per_cvt->channels_max;
  786. hinfo->rates = per_cvt->rates;
  787. hinfo->formats = per_cvt->formats;
  788. hinfo->maxbps = per_cvt->maxbps;
  789. /* Restrict capabilities by ELD if this isn't disabled */
  790. if (!static_hdmi_pcm && eld->eld_valid) {
  791. snd_hdmi_eld_update_pcm_info(eld, hinfo);
  792. if (hinfo->channels_min > hinfo->channels_max ||
  793. !hinfo->rates || !hinfo->formats)
  794. return -ENODEV;
  795. }
  796. /* Store the updated parameters */
  797. runtime->hw.channels_min = hinfo->channels_min;
  798. runtime->hw.channels_max = hinfo->channels_max;
  799. runtime->hw.formats = hinfo->formats;
  800. runtime->hw.rates = hinfo->rates;
  801. snd_pcm_hw_constraint_step(substream->runtime, 0,
  802. SNDRV_PCM_HW_PARAM_CHANNELS, 2);
  803. return 0;
  804. }
  805. /*
  806. * HDA/HDMI auto parsing
  807. */
  808. static int hdmi_read_pin_conn(struct hda_codec *codec, int pin_idx)
  809. {
  810. struct hdmi_spec *spec = codec->spec;
  811. struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
  812. hda_nid_t pin_nid = per_pin->pin_nid;
  813. if (!(get_wcaps(codec, pin_nid) & AC_WCAP_CONN_LIST)) {
  814. snd_printk(KERN_WARNING
  815. "HDMI: pin %d wcaps %#x "
  816. "does not support connection list\n",
  817. pin_nid, get_wcaps(codec, pin_nid));
  818. return -EINVAL;
  819. }
  820. per_pin->num_mux_nids = snd_hda_get_connections(codec, pin_nid,
  821. per_pin->mux_nids,
  822. HDA_MAX_CONNECTIONS);
  823. return 0;
  824. }
  825. static void hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll)
  826. {
  827. struct hda_codec *codec = per_pin->codec;
  828. struct hdmi_eld *eld = &per_pin->sink_eld;
  829. hda_nid_t pin_nid = per_pin->pin_nid;
  830. /*
  831. * Always execute a GetPinSense verb here, even when called from
  832. * hdmi_intrinsic_event; for some NVIDIA HW, the unsolicited
  833. * response's PD bit is not the real PD value, but indicates that
  834. * the real PD value changed. An older version of the HD-audio
  835. * specification worked this way. Hence, we just ignore the data in
  836. * the unsolicited response to avoid custom WARs.
  837. */
  838. int present = snd_hda_pin_sense(codec, pin_nid);
  839. bool eld_valid = false;
  840. memset(eld, 0, offsetof(struct hdmi_eld, eld_buffer));
  841. eld->monitor_present = !!(present & AC_PINSENSE_PRESENCE);
  842. if (eld->monitor_present)
  843. eld_valid = !!(present & AC_PINSENSE_ELDV);
  844. printk(KERN_INFO
  845. "HDMI status: Codec=%d Pin=%d Presence_Detect=%d ELD_Valid=%d\n",
  846. codec->addr, pin_nid, eld->monitor_present, eld_valid);
  847. if (eld_valid) {
  848. if (!snd_hdmi_get_eld(eld, codec, pin_nid))
  849. snd_hdmi_show_eld(eld);
  850. else if (repoll) {
  851. queue_delayed_work(codec->bus->workq,
  852. &per_pin->work,
  853. msecs_to_jiffies(300));
  854. }
  855. }
  856. snd_hda_input_jack_report(codec, pin_nid);
  857. }
  858. static void hdmi_repoll_eld(struct work_struct *work)
  859. {
  860. struct hdmi_spec_per_pin *per_pin =
  861. container_of(to_delayed_work(work), struct hdmi_spec_per_pin, work);
  862. if (per_pin->repoll_count++ > 6)
  863. per_pin->repoll_count = 0;
  864. hdmi_present_sense(per_pin, per_pin->repoll_count);
  865. }
  866. static int hdmi_add_pin(struct hda_codec *codec, hda_nid_t pin_nid)
  867. {
  868. struct hdmi_spec *spec = codec->spec;
  869. unsigned int caps, config;
  870. int pin_idx;
  871. struct hdmi_spec_per_pin *per_pin;
  872. int err;
  873. caps = snd_hda_param_read(codec, pin_nid, AC_PAR_PIN_CAP);
  874. if (!(caps & (AC_PINCAP_HDMI | AC_PINCAP_DP)))
  875. return 0;
  876. config = snd_hda_codec_read(codec, pin_nid, 0,
  877. AC_VERB_GET_CONFIG_DEFAULT, 0);
  878. if (get_defcfg_connect(config) == AC_JACK_PORT_NONE)
  879. return 0;
  880. if (snd_BUG_ON(spec->num_pins >= MAX_HDMI_PINS))
  881. return -E2BIG;
  882. pin_idx = spec->num_pins;
  883. per_pin = &spec->pins[pin_idx];
  884. per_pin->pin_nid = pin_nid;
  885. err = hdmi_read_pin_conn(codec, pin_idx);
  886. if (err < 0)
  887. return err;
  888. spec->num_pins++;
  889. return 0;
  890. }
  891. static int hdmi_add_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
  892. {
  893. struct hdmi_spec *spec = codec->spec;
  894. int cvt_idx;
  895. struct hdmi_spec_per_cvt *per_cvt;
  896. unsigned int chans;
  897. int err;
  898. if (snd_BUG_ON(spec->num_cvts >= MAX_HDMI_CVTS))
  899. return -E2BIG;
  900. chans = get_wcaps(codec, cvt_nid);
  901. chans = get_wcaps_channels(chans);
  902. cvt_idx = spec->num_cvts;
  903. per_cvt = &spec->cvts[cvt_idx];
  904. per_cvt->cvt_nid = cvt_nid;
  905. per_cvt->channels_min = 2;
  906. if (chans <= 16)
  907. per_cvt->channels_max = chans;
  908. err = snd_hda_query_supported_pcm(codec, cvt_nid,
  909. &per_cvt->rates,
  910. &per_cvt->formats,
  911. &per_cvt->maxbps);
  912. if (err < 0)
  913. return err;
  914. spec->num_cvts++;
  915. return 0;
  916. }
  917. static int hdmi_parse_codec(struct hda_codec *codec)
  918. {
  919. hda_nid_t nid;
  920. int i, nodes;
  921. nodes = snd_hda_get_sub_nodes(codec, codec->afg, &nid);
  922. if (!nid || nodes < 0) {
  923. snd_printk(KERN_WARNING "HDMI: failed to get afg sub nodes\n");
  924. return -EINVAL;
  925. }
  926. for (i = 0; i < nodes; i++, nid++) {
  927. unsigned int caps;
  928. unsigned int type;
  929. caps = snd_hda_param_read(codec, nid, AC_PAR_AUDIO_WIDGET_CAP);
  930. type = get_wcaps_type(caps);
  931. if (!(caps & AC_WCAP_DIGITAL))
  932. continue;
  933. switch (type) {
  934. case AC_WID_AUD_OUT:
  935. hdmi_add_cvt(codec, nid);
  936. break;
  937. case AC_WID_PIN:
  938. hdmi_add_pin(codec, nid);
  939. break;
  940. }
  941. }
  942. /*
  943. * G45/IbexPeak don't support EPSS: the unsolicited pin hot plug event
  944. * can be lost and presence sense verb will become inaccurate if the
  945. * HDA link is powered off at hot plug or hw initialization time.
  946. */
  947. #ifdef CONFIG_SND_HDA_POWER_SAVE
  948. if (!(snd_hda_param_read(codec, codec->afg, AC_PAR_POWER_STATE) &
  949. AC_PWRST_EPSS))
  950. codec->bus->power_keep_link_on = 1;
  951. #endif
  952. return 0;
  953. }
  954. /*
  955. */
  956. static char *generic_hdmi_pcm_names[MAX_HDMI_PINS] = {
  957. "HDMI 0",
  958. "HDMI 1",
  959. "HDMI 2",
  960. "HDMI 3",
  961. };
  962. /*
  963. * HDMI callbacks
  964. */
  965. static int generic_hdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
  966. struct hda_codec *codec,
  967. unsigned int stream_tag,
  968. unsigned int format,
  969. struct snd_pcm_substream *substream)
  970. {
  971. hda_nid_t cvt_nid = hinfo->nid;
  972. struct hdmi_spec *spec = codec->spec;
  973. int pin_idx = hinfo_to_pin_index(spec, hinfo);
  974. hda_nid_t pin_nid = spec->pins[pin_idx].pin_nid;
  975. hdmi_set_channel_count(codec, cvt_nid, substream->runtime->channels);
  976. hdmi_setup_audio_infoframe(codec, pin_idx, substream);
  977. return hdmi_setup_stream(codec, cvt_nid, pin_nid, stream_tag, format);
  978. }
  979. static int generic_hdmi_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
  980. struct hda_codec *codec,
  981. struct snd_pcm_substream *substream)
  982. {
  983. struct hdmi_spec *spec = codec->spec;
  984. int cvt_idx, pin_idx;
  985. struct hdmi_spec_per_cvt *per_cvt;
  986. struct hdmi_spec_per_pin *per_pin;
  987. int pinctl;
  988. snd_hda_codec_cleanup_stream(codec, hinfo->nid);
  989. if (hinfo->nid) {
  990. cvt_idx = cvt_nid_to_cvt_index(spec, hinfo->nid);
  991. if (snd_BUG_ON(cvt_idx < 0))
  992. return -EINVAL;
  993. per_cvt = &spec->cvts[cvt_idx];
  994. snd_BUG_ON(!per_cvt->assigned);
  995. per_cvt->assigned = 0;
  996. hinfo->nid = 0;
  997. pin_idx = hinfo_to_pin_index(spec, hinfo);
  998. if (snd_BUG_ON(pin_idx < 0))
  999. return -EINVAL;
  1000. per_pin = &spec->pins[pin_idx];
  1001. pinctl = snd_hda_codec_read(codec, per_pin->pin_nid, 0,
  1002. AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
  1003. snd_hda_codec_write(codec, per_pin->pin_nid, 0,
  1004. AC_VERB_SET_PIN_WIDGET_CONTROL,
  1005. pinctl & ~PIN_OUT);
  1006. snd_hda_spdif_ctls_unassign(codec, pin_idx);
  1007. }
  1008. return 0;
  1009. }
  1010. static const struct hda_pcm_ops generic_ops = {
  1011. .open = hdmi_pcm_open,
  1012. .prepare = generic_hdmi_playback_pcm_prepare,
  1013. .cleanup = generic_hdmi_playback_pcm_cleanup,
  1014. };
  1015. static int generic_hdmi_build_pcms(struct hda_codec *codec)
  1016. {
  1017. struct hdmi_spec *spec = codec->spec;
  1018. int pin_idx;
  1019. for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
  1020. struct hda_pcm *info;
  1021. struct hda_pcm_stream *pstr;
  1022. info = &spec->pcm_rec[pin_idx];
  1023. info->name = generic_hdmi_pcm_names[pin_idx];
  1024. info->pcm_type = HDA_PCM_TYPE_HDMI;
  1025. pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
  1026. pstr->substreams = 1;
  1027. pstr->ops = generic_ops;
  1028. /* other pstr fields are set in open */
  1029. }
  1030. codec->num_pcms = spec->num_pins;
  1031. codec->pcm_info = spec->pcm_rec;
  1032. return 0;
  1033. }
  1034. static int generic_hdmi_build_jack(struct hda_codec *codec, int pin_idx)
  1035. {
  1036. int err;
  1037. char hdmi_str[32];
  1038. struct hdmi_spec *spec = codec->spec;
  1039. struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
  1040. int pcmdev = spec->pcm_rec[pin_idx].device;
  1041. snprintf(hdmi_str, sizeof(hdmi_str), "HDMI/DP,pcm=%d", pcmdev);
  1042. err = snd_hda_input_jack_add(codec, per_pin->pin_nid,
  1043. SND_JACK_VIDEOOUT, pcmdev > 0 ? hdmi_str : NULL);
  1044. if (err < 0)
  1045. return err;
  1046. hdmi_present_sense(per_pin, 0);
  1047. return 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. }
  1072. return 0;
  1073. }
  1074. static int generic_hdmi_init(struct hda_codec *codec)
  1075. {
  1076. struct hdmi_spec *spec = codec->spec;
  1077. int pin_idx;
  1078. for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
  1079. struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
  1080. hda_nid_t pin_nid = per_pin->pin_nid;
  1081. struct hdmi_eld *eld = &per_pin->sink_eld;
  1082. hdmi_init_pin(codec, pin_nid);
  1083. snd_hda_codec_write(codec, pin_nid, 0,
  1084. AC_VERB_SET_UNSOLICITED_ENABLE,
  1085. AC_USRSP_EN | pin_nid);
  1086. per_pin->codec = codec;
  1087. INIT_DELAYED_WORK(&per_pin->work, hdmi_repoll_eld);
  1088. snd_hda_eld_proc_new(codec, eld, pin_idx);
  1089. }
  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. snd_hda_input_jack_free(codec);
  1103. flush_workqueue(codec->bus->workq);
  1104. kfree(spec);
  1105. }
  1106. static const struct hda_codec_ops generic_hdmi_patch_ops = {
  1107. .init = generic_hdmi_init,
  1108. .free = generic_hdmi_free,
  1109. .build_pcms = generic_hdmi_build_pcms,
  1110. .build_controls = generic_hdmi_build_controls,
  1111. .unsol_event = hdmi_unsol_event,
  1112. };
  1113. static int patch_generic_hdmi(struct hda_codec *codec)
  1114. {
  1115. struct hdmi_spec *spec;
  1116. spec = kzalloc(sizeof(*spec), GFP_KERNEL);
  1117. if (spec == NULL)
  1118. return -ENOMEM;
  1119. codec->spec = spec;
  1120. if (hdmi_parse_codec(codec) < 0) {
  1121. codec->spec = NULL;
  1122. kfree(spec);
  1123. return -EINVAL;
  1124. }
  1125. codec->patch_ops = generic_hdmi_patch_ops;
  1126. init_channel_allocations();
  1127. return 0;
  1128. }
  1129. /*
  1130. * Shared non-generic implementations
  1131. */
  1132. static int simple_playback_build_pcms(struct hda_codec *codec)
  1133. {
  1134. struct hdmi_spec *spec = codec->spec;
  1135. struct hda_pcm *info = spec->pcm_rec;
  1136. int i;
  1137. codec->num_pcms = spec->num_cvts;
  1138. codec->pcm_info = info;
  1139. for (i = 0; i < codec->num_pcms; i++, info++) {
  1140. unsigned int chans;
  1141. struct hda_pcm_stream *pstr;
  1142. chans = get_wcaps(codec, spec->cvts[i].cvt_nid);
  1143. chans = get_wcaps_channels(chans);
  1144. info->name = generic_hdmi_pcm_names[i];
  1145. info->pcm_type = HDA_PCM_TYPE_HDMI;
  1146. pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
  1147. snd_BUG_ON(!spec->pcm_playback);
  1148. *pstr = *spec->pcm_playback;
  1149. pstr->nid = spec->cvts[i].cvt_nid;
  1150. if (pstr->channels_max <= 2 && chans && chans <= 16)
  1151. pstr->channels_max = chans;
  1152. }
  1153. return 0;
  1154. }
  1155. static int simple_playback_build_controls(struct hda_codec *codec)
  1156. {
  1157. struct hdmi_spec *spec = codec->spec;
  1158. int err;
  1159. int i;
  1160. for (i = 0; i < codec->num_pcms; i++) {
  1161. err = snd_hda_create_spdif_out_ctls(codec,
  1162. spec->cvts[i].cvt_nid,
  1163. spec->cvts[i].cvt_nid);
  1164. if (err < 0)
  1165. return err;
  1166. }
  1167. return 0;
  1168. }
  1169. static void simple_playback_free(struct hda_codec *codec)
  1170. {
  1171. struct hdmi_spec *spec = codec->spec;
  1172. kfree(spec);
  1173. }
  1174. /*
  1175. * Nvidia specific implementations
  1176. */
  1177. #define Nv_VERB_SET_Channel_Allocation 0xF79
  1178. #define Nv_VERB_SET_Info_Frame_Checksum 0xF7A
  1179. #define Nv_VERB_SET_Audio_Protection_On 0xF98
  1180. #define Nv_VERB_SET_Audio_Protection_Off 0xF99
  1181. #define nvhdmi_master_con_nid_7x 0x04
  1182. #define nvhdmi_master_pin_nid_7x 0x05
  1183. static const hda_nid_t nvhdmi_con_nids_7x[4] = {
  1184. /*front, rear, clfe, rear_surr */
  1185. 0x6, 0x8, 0xa, 0xc,
  1186. };
  1187. static const struct hda_verb nvhdmi_basic_init_7x[] = {
  1188. /* set audio protect on */
  1189. { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
  1190. /* enable digital output on pin widget */
  1191. { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
  1192. { 0x7, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
  1193. { 0x9, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
  1194. { 0xb, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
  1195. { 0xd, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
  1196. {} /* terminator */
  1197. };
  1198. #ifdef LIMITED_RATE_FMT_SUPPORT
  1199. /* support only the safe format and rate */
  1200. #define SUPPORTED_RATES SNDRV_PCM_RATE_48000
  1201. #define SUPPORTED_MAXBPS 16
  1202. #define SUPPORTED_FORMATS SNDRV_PCM_FMTBIT_S16_LE
  1203. #else
  1204. /* support all rates and formats */
  1205. #define SUPPORTED_RATES \
  1206. (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
  1207. SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |\
  1208. SNDRV_PCM_RATE_192000)
  1209. #define SUPPORTED_MAXBPS 24
  1210. #define SUPPORTED_FORMATS \
  1211. (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE)
  1212. #endif
  1213. static int nvhdmi_7x_init(struct hda_codec *codec)
  1214. {
  1215. snd_hda_sequence_write(codec, nvhdmi_basic_init_7x);
  1216. return 0;
  1217. }
  1218. static unsigned int channels_2_6_8[] = {
  1219. 2, 6, 8
  1220. };
  1221. static unsigned int channels_2_8[] = {
  1222. 2, 8
  1223. };
  1224. static struct snd_pcm_hw_constraint_list hw_constraints_2_6_8_channels = {
  1225. .count = ARRAY_SIZE(channels_2_6_8),
  1226. .list = channels_2_6_8,
  1227. .mask = 0,
  1228. };
  1229. static struct snd_pcm_hw_constraint_list hw_constraints_2_8_channels = {
  1230. .count = ARRAY_SIZE(channels_2_8),
  1231. .list = channels_2_8,
  1232. .mask = 0,
  1233. };
  1234. static int simple_playback_pcm_open(struct hda_pcm_stream *hinfo,
  1235. struct hda_codec *codec,
  1236. struct snd_pcm_substream *substream)
  1237. {
  1238. struct hdmi_spec *spec = codec->spec;
  1239. struct snd_pcm_hw_constraint_list *hw_constraints_channels = NULL;
  1240. switch (codec->preset->id) {
  1241. case 0x10de0002:
  1242. case 0x10de0003:
  1243. case 0x10de0005:
  1244. case 0x10de0006:
  1245. hw_constraints_channels = &hw_constraints_2_8_channels;
  1246. break;
  1247. case 0x10de0007:
  1248. hw_constraints_channels = &hw_constraints_2_6_8_channels;
  1249. break;
  1250. default:
  1251. break;
  1252. }
  1253. if (hw_constraints_channels != NULL) {
  1254. snd_pcm_hw_constraint_list(substream->runtime, 0,
  1255. SNDRV_PCM_HW_PARAM_CHANNELS,
  1256. hw_constraints_channels);
  1257. } else {
  1258. snd_pcm_hw_constraint_step(substream->runtime, 0,
  1259. SNDRV_PCM_HW_PARAM_CHANNELS, 2);
  1260. }
  1261. return snd_hda_multi_out_dig_open(codec, &spec->multiout);
  1262. }
  1263. static int simple_playback_pcm_close(struct hda_pcm_stream *hinfo,
  1264. struct hda_codec *codec,
  1265. struct snd_pcm_substream *substream)
  1266. {
  1267. struct hdmi_spec *spec = codec->spec;
  1268. return snd_hda_multi_out_dig_close(codec, &spec->multiout);
  1269. }
  1270. static int simple_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
  1271. struct hda_codec *codec,
  1272. unsigned int stream_tag,
  1273. unsigned int format,
  1274. struct snd_pcm_substream *substream)
  1275. {
  1276. struct hdmi_spec *spec = codec->spec;
  1277. return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
  1278. stream_tag, format, substream);
  1279. }
  1280. static void nvhdmi_8ch_7x_set_info_frame_parameters(struct hda_codec *codec,
  1281. int channels)
  1282. {
  1283. unsigned int chanmask;
  1284. int chan = channels ? (channels - 1) : 1;
  1285. switch (channels) {
  1286. default:
  1287. case 0:
  1288. case 2:
  1289. chanmask = 0x00;
  1290. break;
  1291. case 4:
  1292. chanmask = 0x08;
  1293. break;
  1294. case 6:
  1295. chanmask = 0x0b;
  1296. break;
  1297. case 8:
  1298. chanmask = 0x13;
  1299. break;
  1300. }
  1301. /* Set the audio infoframe channel allocation and checksum fields. The
  1302. * channel count is computed implicitly by the hardware. */
  1303. snd_hda_codec_write(codec, 0x1, 0,
  1304. Nv_VERB_SET_Channel_Allocation, chanmask);
  1305. snd_hda_codec_write(codec, 0x1, 0,
  1306. Nv_VERB_SET_Info_Frame_Checksum,
  1307. (0x71 - chan - chanmask));
  1308. }
  1309. static int nvhdmi_8ch_7x_pcm_close(struct hda_pcm_stream *hinfo,
  1310. struct hda_codec *codec,
  1311. struct snd_pcm_substream *substream)
  1312. {
  1313. struct hdmi_spec *spec = codec->spec;
  1314. int i;
  1315. snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x,
  1316. 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
  1317. for (i = 0; i < 4; i++) {
  1318. /* set the stream id */
  1319. snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
  1320. AC_VERB_SET_CHANNEL_STREAMID, 0);
  1321. /* set the stream format */
  1322. snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
  1323. AC_VERB_SET_STREAM_FORMAT, 0);
  1324. }
  1325. /* The audio hardware sends a channel count of 0x7 (8ch) when all the
  1326. * streams are disabled. */
  1327. nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
  1328. return snd_hda_multi_out_dig_close(codec, &spec->multiout);
  1329. }
  1330. static int nvhdmi_8ch_7x_pcm_prepare(struct hda_pcm_stream *hinfo,
  1331. struct hda_codec *codec,
  1332. unsigned int stream_tag,
  1333. unsigned int format,
  1334. struct snd_pcm_substream *substream)
  1335. {
  1336. int chs;
  1337. unsigned int dataDCC2, channel_id;
  1338. int i;
  1339. struct hdmi_spec *spec = codec->spec;
  1340. struct hda_spdif_out *spdif =
  1341. snd_hda_spdif_out_of_nid(codec, spec->cvts[0].cvt_nid);
  1342. mutex_lock(&codec->spdif_mutex);
  1343. chs = substream->runtime->channels;
  1344. dataDCC2 = 0x2;
  1345. /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
  1346. if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
  1347. snd_hda_codec_write(codec,
  1348. nvhdmi_master_con_nid_7x,
  1349. 0,
  1350. AC_VERB_SET_DIGI_CONVERT_1,
  1351. spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
  1352. /* set the stream id */
  1353. snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
  1354. AC_VERB_SET_CHANNEL_STREAMID, (stream_tag << 4) | 0x0);
  1355. /* set the stream format */
  1356. snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
  1357. AC_VERB_SET_STREAM_FORMAT, format);
  1358. /* turn on again (if needed) */
  1359. /* enable and set the channel status audio/data flag */
  1360. if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE)) {
  1361. snd_hda_codec_write(codec,
  1362. nvhdmi_master_con_nid_7x,
  1363. 0,
  1364. AC_VERB_SET_DIGI_CONVERT_1,
  1365. spdif->ctls & 0xff);
  1366. snd_hda_codec_write(codec,
  1367. nvhdmi_master_con_nid_7x,
  1368. 0,
  1369. AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
  1370. }
  1371. for (i = 0; i < 4; i++) {
  1372. if (chs == 2)
  1373. channel_id = 0;
  1374. else
  1375. channel_id = i * 2;
  1376. /* turn off SPDIF once;
  1377. *otherwise the IEC958 bits won't be updated
  1378. */
  1379. if (codec->spdif_status_reset &&
  1380. (spdif->ctls & AC_DIG1_ENABLE))
  1381. snd_hda_codec_write(codec,
  1382. nvhdmi_con_nids_7x[i],
  1383. 0,
  1384. AC_VERB_SET_DIGI_CONVERT_1,
  1385. spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
  1386. /* set the stream id */
  1387. snd_hda_codec_write(codec,
  1388. nvhdmi_con_nids_7x[i],
  1389. 0,
  1390. AC_VERB_SET_CHANNEL_STREAMID,
  1391. (stream_tag << 4) | channel_id);
  1392. /* set the stream format */
  1393. snd_hda_codec_write(codec,
  1394. nvhdmi_con_nids_7x[i],
  1395. 0,
  1396. AC_VERB_SET_STREAM_FORMAT,
  1397. format);
  1398. /* turn on again (if needed) */
  1399. /* enable and set the channel status audio/data flag */
  1400. if (codec->spdif_status_reset &&
  1401. (spdif->ctls & AC_DIG1_ENABLE)) {
  1402. snd_hda_codec_write(codec,
  1403. nvhdmi_con_nids_7x[i],
  1404. 0,
  1405. AC_VERB_SET_DIGI_CONVERT_1,
  1406. spdif->ctls & 0xff);
  1407. snd_hda_codec_write(codec,
  1408. nvhdmi_con_nids_7x[i],
  1409. 0,
  1410. AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
  1411. }
  1412. }
  1413. nvhdmi_8ch_7x_set_info_frame_parameters(codec, chs);
  1414. mutex_unlock(&codec->spdif_mutex);
  1415. return 0;
  1416. }
  1417. static const struct hda_pcm_stream nvhdmi_pcm_playback_8ch_7x = {
  1418. .substreams = 1,
  1419. .channels_min = 2,
  1420. .channels_max = 8,
  1421. .nid = nvhdmi_master_con_nid_7x,
  1422. .rates = SUPPORTED_RATES,
  1423. .maxbps = SUPPORTED_MAXBPS,
  1424. .formats = SUPPORTED_FORMATS,
  1425. .ops = {
  1426. .open = simple_playback_pcm_open,
  1427. .close = nvhdmi_8ch_7x_pcm_close,
  1428. .prepare = nvhdmi_8ch_7x_pcm_prepare
  1429. },
  1430. };
  1431. static const struct hda_pcm_stream nvhdmi_pcm_playback_2ch = {
  1432. .substreams = 1,
  1433. .channels_min = 2,
  1434. .channels_max = 2,
  1435. .nid = nvhdmi_master_con_nid_7x,
  1436. .rates = SUPPORTED_RATES,
  1437. .maxbps = SUPPORTED_MAXBPS,
  1438. .formats = SUPPORTED_FORMATS,
  1439. .ops = {
  1440. .open = simple_playback_pcm_open,
  1441. .close = simple_playback_pcm_close,
  1442. .prepare = simple_playback_pcm_prepare
  1443. },
  1444. };
  1445. static const struct hda_codec_ops nvhdmi_patch_ops_8ch_7x = {
  1446. .build_controls = simple_playback_build_controls,
  1447. .build_pcms = simple_playback_build_pcms,
  1448. .init = nvhdmi_7x_init,
  1449. .free = simple_playback_free,
  1450. };
  1451. static const struct hda_codec_ops nvhdmi_patch_ops_2ch = {
  1452. .build_controls = simple_playback_build_controls,
  1453. .build_pcms = simple_playback_build_pcms,
  1454. .init = nvhdmi_7x_init,
  1455. .free = simple_playback_free,
  1456. };
  1457. static int patch_nvhdmi_2ch(struct hda_codec *codec)
  1458. {
  1459. struct hdmi_spec *spec;
  1460. spec = kzalloc(sizeof(*spec), GFP_KERNEL);
  1461. if (spec == NULL)
  1462. return -ENOMEM;
  1463. codec->spec = spec;
  1464. spec->multiout.num_dacs = 0; /* no analog */
  1465. spec->multiout.max_channels = 2;
  1466. spec->multiout.dig_out_nid = nvhdmi_master_con_nid_7x;
  1467. spec->num_cvts = 1;
  1468. spec->cvts[0].cvt_nid = nvhdmi_master_con_nid_7x;
  1469. spec->pcm_playback = &nvhdmi_pcm_playback_2ch;
  1470. codec->patch_ops = nvhdmi_patch_ops_2ch;
  1471. return 0;
  1472. }
  1473. static int patch_nvhdmi_8ch_7x(struct hda_codec *codec)
  1474. {
  1475. struct hdmi_spec *spec;
  1476. int err = patch_nvhdmi_2ch(codec);
  1477. if (err < 0)
  1478. return err;
  1479. spec = codec->spec;
  1480. spec->multiout.max_channels = 8;
  1481. spec->pcm_playback = &nvhdmi_pcm_playback_8ch_7x;
  1482. codec->patch_ops = nvhdmi_patch_ops_8ch_7x;
  1483. /* Initialize the audio infoframe channel mask and checksum to something
  1484. * valid */
  1485. nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
  1486. return 0;
  1487. }
  1488. /*
  1489. * ATI-specific implementations
  1490. *
  1491. * FIXME: we may omit the whole this and use the generic code once after
  1492. * it's confirmed to work.
  1493. */
  1494. #define ATIHDMI_CVT_NID 0x02 /* audio converter */
  1495. #define ATIHDMI_PIN_NID 0x03 /* HDMI output pin */
  1496. static int atihdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
  1497. struct hda_codec *codec,
  1498. unsigned int stream_tag,
  1499. unsigned int format,
  1500. struct snd_pcm_substream *substream)
  1501. {
  1502. struct hdmi_spec *spec = codec->spec;
  1503. int chans = substream->runtime->channels;
  1504. int i, err;
  1505. err = simple_playback_pcm_prepare(hinfo, codec, stream_tag, format,
  1506. substream);
  1507. if (err < 0)
  1508. return err;
  1509. snd_hda_codec_write(codec, spec->cvts[0].cvt_nid, 0,
  1510. AC_VERB_SET_CVT_CHAN_COUNT, chans - 1);
  1511. /* FIXME: XXX */
  1512. for (i = 0; i < chans; i++) {
  1513. snd_hda_codec_write(codec, spec->cvts[0].cvt_nid, 0,
  1514. AC_VERB_SET_HDMI_CHAN_SLOT,
  1515. (i << 4) | i);
  1516. }
  1517. return 0;
  1518. }
  1519. static const struct hda_pcm_stream atihdmi_pcm_digital_playback = {
  1520. .substreams = 1,
  1521. .channels_min = 2,
  1522. .channels_max = 2,
  1523. .nid = ATIHDMI_CVT_NID,
  1524. .ops = {
  1525. .open = simple_playback_pcm_open,
  1526. .close = simple_playback_pcm_close,
  1527. .prepare = atihdmi_playback_pcm_prepare
  1528. },
  1529. };
  1530. static const struct hda_verb atihdmi_basic_init[] = {
  1531. /* enable digital output on pin widget */
  1532. { 0x03, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
  1533. {} /* terminator */
  1534. };
  1535. static int atihdmi_init(struct hda_codec *codec)
  1536. {
  1537. struct hdmi_spec *spec = codec->spec;
  1538. snd_hda_sequence_write(codec, atihdmi_basic_init);
  1539. /* SI codec requires to unmute the pin */
  1540. if (get_wcaps(codec, spec->pins[0].pin_nid) & AC_WCAP_OUT_AMP)
  1541. snd_hda_codec_write(codec, spec->pins[0].pin_nid, 0,
  1542. AC_VERB_SET_AMP_GAIN_MUTE,
  1543. AMP_OUT_UNMUTE);
  1544. return 0;
  1545. }
  1546. static const struct hda_codec_ops atihdmi_patch_ops = {
  1547. .build_controls = simple_playback_build_controls,
  1548. .build_pcms = simple_playback_build_pcms,
  1549. .init = atihdmi_init,
  1550. .free = simple_playback_free,
  1551. };
  1552. static int patch_atihdmi(struct hda_codec *codec)
  1553. {
  1554. struct hdmi_spec *spec;
  1555. spec = kzalloc(sizeof(*spec), GFP_KERNEL);
  1556. if (spec == NULL)
  1557. return -ENOMEM;
  1558. codec->spec = spec;
  1559. spec->multiout.num_dacs = 0; /* no analog */
  1560. spec->multiout.max_channels = 2;
  1561. spec->multiout.dig_out_nid = ATIHDMI_CVT_NID;
  1562. spec->num_cvts = 1;
  1563. spec->cvts[0].cvt_nid = ATIHDMI_CVT_NID;
  1564. spec->pins[0].pin_nid = ATIHDMI_PIN_NID;
  1565. spec->pcm_playback = &atihdmi_pcm_digital_playback;
  1566. codec->patch_ops = atihdmi_patch_ops;
  1567. return 0;
  1568. }
  1569. /*
  1570. * patch entries
  1571. */
  1572. static const struct hda_codec_preset snd_hda_preset_hdmi[] = {
  1573. { .id = 0x1002793c, .name = "RS600 HDMI", .patch = patch_atihdmi },
  1574. { .id = 0x10027919, .name = "RS600 HDMI", .patch = patch_atihdmi },
  1575. { .id = 0x1002791a, .name = "RS690/780 HDMI", .patch = patch_atihdmi },
  1576. { .id = 0x1002aa01, .name = "R6xx HDMI", .patch = patch_generic_hdmi },
  1577. { .id = 0x10951390, .name = "SiI1390 HDMI", .patch = patch_generic_hdmi },
  1578. { .id = 0x10951392, .name = "SiI1392 HDMI", .patch = patch_generic_hdmi },
  1579. { .id = 0x17e80047, .name = "Chrontel HDMI", .patch = patch_generic_hdmi },
  1580. { .id = 0x10de0002, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
  1581. { .id = 0x10de0003, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
  1582. { .id = 0x10de0005, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
  1583. { .id = 0x10de0006, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
  1584. { .id = 0x10de0007, .name = "MCP79/7A HDMI", .patch = patch_nvhdmi_8ch_7x },
  1585. { .id = 0x10de000a, .name = "GPU 0a HDMI/DP", .patch = patch_generic_hdmi },
  1586. { .id = 0x10de000b, .name = "GPU 0b HDMI/DP", .patch = patch_generic_hdmi },
  1587. { .id = 0x10de000c, .name = "MCP89 HDMI", .patch = patch_generic_hdmi },
  1588. { .id = 0x10de000d, .name = "GPU 0d HDMI/DP", .patch = patch_generic_hdmi },
  1589. { .id = 0x10de0010, .name = "GPU 10 HDMI/DP", .patch = patch_generic_hdmi },
  1590. { .id = 0x10de0011, .name = "GPU 11 HDMI/DP", .patch = patch_generic_hdmi },
  1591. { .id = 0x10de0012, .name = "GPU 12 HDMI/DP", .patch = patch_generic_hdmi },
  1592. { .id = 0x10de0013, .name = "GPU 13 HDMI/DP", .patch = patch_generic_hdmi },
  1593. { .id = 0x10de0014, .name = "GPU 14 HDMI/DP", .patch = patch_generic_hdmi },
  1594. { .id = 0x10de0015, .name = "GPU 15 HDMI/DP", .patch = patch_generic_hdmi },
  1595. { .id = 0x10de0016, .name = "GPU 16 HDMI/DP", .patch = patch_generic_hdmi },
  1596. /* 17 is known to be absent */
  1597. { .id = 0x10de0018, .name = "GPU 18 HDMI/DP", .patch = patch_generic_hdmi },
  1598. { .id = 0x10de0019, .name = "GPU 19 HDMI/DP", .patch = patch_generic_hdmi },
  1599. { .id = 0x10de001a, .name = "GPU 1a HDMI/DP", .patch = patch_generic_hdmi },
  1600. { .id = 0x10de001b, .name = "GPU 1b HDMI/DP", .patch = patch_generic_hdmi },
  1601. { .id = 0x10de001c, .name = "GPU 1c HDMI/DP", .patch = patch_generic_hdmi },
  1602. { .id = 0x10de0040, .name = "GPU 40 HDMI/DP", .patch = patch_generic_hdmi },
  1603. { .id = 0x10de0041, .name = "GPU 41 HDMI/DP", .patch = patch_generic_hdmi },
  1604. { .id = 0x10de0042, .name = "GPU 42 HDMI/DP", .patch = patch_generic_hdmi },
  1605. { .id = 0x10de0043, .name = "GPU 43 HDMI/DP", .patch = patch_generic_hdmi },
  1606. { .id = 0x10de0044, .name = "GPU 44 HDMI/DP", .patch = patch_generic_hdmi },
  1607. { .id = 0x10de0067, .name = "MCP67 HDMI", .patch = patch_nvhdmi_2ch },
  1608. { .id = 0x10de8001, .name = "MCP73 HDMI", .patch = patch_nvhdmi_2ch },
  1609. { .id = 0x80860054, .name = "IbexPeak HDMI", .patch = patch_generic_hdmi },
  1610. { .id = 0x80862801, .name = "Bearlake HDMI", .patch = patch_generic_hdmi },
  1611. { .id = 0x80862802, .name = "Cantiga HDMI", .patch = patch_generic_hdmi },
  1612. { .id = 0x80862803, .name = "Eaglelake HDMI", .patch = patch_generic_hdmi },
  1613. { .id = 0x80862804, .name = "IbexPeak HDMI", .patch = patch_generic_hdmi },
  1614. { .id = 0x80862805, .name = "CougarPoint HDMI", .patch = patch_generic_hdmi },
  1615. { .id = 0x80862806, .name = "PantherPoint HDMI", .patch = patch_generic_hdmi },
  1616. { .id = 0x808629fb, .name = "Crestline HDMI", .patch = patch_generic_hdmi },
  1617. {} /* terminator */
  1618. };
  1619. MODULE_ALIAS("snd-hda-codec-id:1002793c");
  1620. MODULE_ALIAS("snd-hda-codec-id:10027919");
  1621. MODULE_ALIAS("snd-hda-codec-id:1002791a");
  1622. MODULE_ALIAS("snd-hda-codec-id:1002aa01");
  1623. MODULE_ALIAS("snd-hda-codec-id:10951390");
  1624. MODULE_ALIAS("snd-hda-codec-id:10951392");
  1625. MODULE_ALIAS("snd-hda-codec-id:10de0002");
  1626. MODULE_ALIAS("snd-hda-codec-id:10de0003");
  1627. MODULE_ALIAS("snd-hda-codec-id:10de0005");
  1628. MODULE_ALIAS("snd-hda-codec-id:10de0006");
  1629. MODULE_ALIAS("snd-hda-codec-id:10de0007");
  1630. MODULE_ALIAS("snd-hda-codec-id:10de000a");
  1631. MODULE_ALIAS("snd-hda-codec-id:10de000b");
  1632. MODULE_ALIAS("snd-hda-codec-id:10de000c");
  1633. MODULE_ALIAS("snd-hda-codec-id:10de000d");
  1634. MODULE_ALIAS("snd-hda-codec-id:10de0010");
  1635. MODULE_ALIAS("snd-hda-codec-id:10de0011");
  1636. MODULE_ALIAS("snd-hda-codec-id:10de0012");
  1637. MODULE_ALIAS("snd-hda-codec-id:10de0013");
  1638. MODULE_ALIAS("snd-hda-codec-id:10de0014");
  1639. MODULE_ALIAS("snd-hda-codec-id:10de0015");
  1640. MODULE_ALIAS("snd-hda-codec-id:10de0016");
  1641. MODULE_ALIAS("snd-hda-codec-id:10de0018");
  1642. MODULE_ALIAS("snd-hda-codec-id:10de0019");
  1643. MODULE_ALIAS("snd-hda-codec-id:10de001a");
  1644. MODULE_ALIAS("snd-hda-codec-id:10de001b");
  1645. MODULE_ALIAS("snd-hda-codec-id:10de001c");
  1646. MODULE_ALIAS("snd-hda-codec-id:10de0040");
  1647. MODULE_ALIAS("snd-hda-codec-id:10de0041");
  1648. MODULE_ALIAS("snd-hda-codec-id:10de0042");
  1649. MODULE_ALIAS("snd-hda-codec-id:10de0043");
  1650. MODULE_ALIAS("snd-hda-codec-id:10de0044");
  1651. MODULE_ALIAS("snd-hda-codec-id:10de0067");
  1652. MODULE_ALIAS("snd-hda-codec-id:10de8001");
  1653. MODULE_ALIAS("snd-hda-codec-id:17e80047");
  1654. MODULE_ALIAS("snd-hda-codec-id:80860054");
  1655. MODULE_ALIAS("snd-hda-codec-id:80862801");
  1656. MODULE_ALIAS("snd-hda-codec-id:80862802");
  1657. MODULE_ALIAS("snd-hda-codec-id:80862803");
  1658. MODULE_ALIAS("snd-hda-codec-id:80862804");
  1659. MODULE_ALIAS("snd-hda-codec-id:80862805");
  1660. MODULE_ALIAS("snd-hda-codec-id:80862806");
  1661. MODULE_ALIAS("snd-hda-codec-id:808629fb");
  1662. MODULE_LICENSE("GPL");
  1663. MODULE_DESCRIPTION("HDMI HD-audio codec");
  1664. MODULE_ALIAS("snd-hda-codec-intelhdmi");
  1665. MODULE_ALIAS("snd-hda-codec-nvhdmi");
  1666. MODULE_ALIAS("snd-hda-codec-atihdmi");
  1667. static struct hda_codec_preset_list intel_list = {
  1668. .preset = snd_hda_preset_hdmi,
  1669. .owner = THIS_MODULE,
  1670. };
  1671. static int __init patch_hdmi_init(void)
  1672. {
  1673. return snd_hda_add_codec_preset(&intel_list);
  1674. }
  1675. static void __exit patch_hdmi_exit(void)
  1676. {
  1677. snd_hda_delete_codec_preset(&intel_list);
  1678. }
  1679. module_init(patch_hdmi_init)
  1680. module_exit(patch_hdmi_exit)