patch_hdmi.c 55 KB

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