patch_hdmi.c 55 KB

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