atihdmi.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * ATI/AMD codec support
  4. */
  5. #include <linux/init.h>
  6. #include <linux/slab.h>
  7. #include <linux/module.h>
  8. #include <linux/unaligned.h>
  9. #include <sound/core.h>
  10. #include <sound/tlv.h>
  11. #include <sound/hdaudio.h>
  12. #include <sound/hda_codec.h>
  13. #include "hda_local.h"
  14. #include "hdmi_local.h"
  15. #define is_amdhdmi_rev3_or_later(codec) \
  16. ((codec)->core.vendor_id == 0x1002aa01 && \
  17. ((codec)->core.revision_id & 0xff00) >= 0x0300)
  18. #define has_amd_full_remap_support(codec) is_amdhdmi_rev3_or_later(codec)
  19. /* ATI/AMD specific HDA pin verbs, see the AMD HDA Verbs specification */
  20. #define ATI_VERB_SET_CHANNEL_ALLOCATION 0x771
  21. #define ATI_VERB_SET_DOWNMIX_INFO 0x772
  22. #define ATI_VERB_SET_MULTICHANNEL_01 0x777
  23. #define ATI_VERB_SET_MULTICHANNEL_23 0x778
  24. #define ATI_VERB_SET_MULTICHANNEL_45 0x779
  25. #define ATI_VERB_SET_MULTICHANNEL_67 0x77a
  26. #define ATI_VERB_SET_HBR_CONTROL 0x77c
  27. #define ATI_VERB_SET_MULTICHANNEL_1 0x785
  28. #define ATI_VERB_SET_MULTICHANNEL_3 0x786
  29. #define ATI_VERB_SET_MULTICHANNEL_5 0x787
  30. #define ATI_VERB_SET_MULTICHANNEL_7 0x788
  31. #define ATI_VERB_SET_MULTICHANNEL_MODE 0x789
  32. #define ATI_VERB_GET_CHANNEL_ALLOCATION 0xf71
  33. #define ATI_VERB_GET_DOWNMIX_INFO 0xf72
  34. #define ATI_VERB_GET_MULTICHANNEL_01 0xf77
  35. #define ATI_VERB_GET_MULTICHANNEL_23 0xf78
  36. #define ATI_VERB_GET_MULTICHANNEL_45 0xf79
  37. #define ATI_VERB_GET_MULTICHANNEL_67 0xf7a
  38. #define ATI_VERB_GET_HBR_CONTROL 0xf7c
  39. #define ATI_VERB_GET_MULTICHANNEL_1 0xf85
  40. #define ATI_VERB_GET_MULTICHANNEL_3 0xf86
  41. #define ATI_VERB_GET_MULTICHANNEL_5 0xf87
  42. #define ATI_VERB_GET_MULTICHANNEL_7 0xf88
  43. #define ATI_VERB_GET_MULTICHANNEL_MODE 0xf89
  44. /* AMD specific HDA cvt verbs */
  45. #define ATI_VERB_SET_RAMP_RATE 0x770
  46. #define ATI_VERB_GET_RAMP_RATE 0xf70
  47. #define ATI_OUT_ENABLE 0x1
  48. #define ATI_MULTICHANNEL_MODE_PAIRED 0
  49. #define ATI_MULTICHANNEL_MODE_SINGLE 1
  50. #define ATI_HBR_CAPABLE 0x01
  51. #define ATI_HBR_ENABLE 0x10
  52. /* ATI/AMD specific ELD emulation */
  53. #define ATI_VERB_SET_AUDIO_DESCRIPTOR 0x776
  54. #define ATI_VERB_SET_SINK_INFO_INDEX 0x780
  55. #define ATI_VERB_GET_SPEAKER_ALLOCATION 0xf70
  56. #define ATI_VERB_GET_AUDIO_DESCRIPTOR 0xf76
  57. #define ATI_VERB_GET_AUDIO_VIDEO_DELAY 0xf7b
  58. #define ATI_VERB_GET_SINK_INFO_INDEX 0xf80
  59. #define ATI_VERB_GET_SINK_INFO_DATA 0xf81
  60. #define ATI_SPKALLOC_SPKALLOC 0x007f
  61. #define ATI_SPKALLOC_TYPE_HDMI 0x0100
  62. #define ATI_SPKALLOC_TYPE_DISPLAYPORT 0x0200
  63. /* first three bytes are just standard SAD */
  64. #define ATI_AUDIODESC_CHANNELS 0x00000007
  65. #define ATI_AUDIODESC_RATES 0x0000ff00
  66. #define ATI_AUDIODESC_LPCM_STEREO_RATES 0xff000000
  67. /* in standard HDMI VSDB format */
  68. #define ATI_DELAY_VIDEO_LATENCY 0x000000ff
  69. #define ATI_DELAY_AUDIO_LATENCY 0x0000ff00
  70. enum ati_sink_info_idx {
  71. ATI_INFO_IDX_MANUFACTURER_ID = 0,
  72. ATI_INFO_IDX_PRODUCT_ID = 1,
  73. ATI_INFO_IDX_SINK_DESC_LEN = 2,
  74. ATI_INFO_IDX_PORT_ID_LOW = 3,
  75. ATI_INFO_IDX_PORT_ID_HIGH = 4,
  76. ATI_INFO_IDX_SINK_DESC_FIRST = 5,
  77. ATI_INFO_IDX_SINK_DESC_LAST = 22, /* max len 18 bytes */
  78. };
  79. static int get_eld_ati(struct hda_codec *codec, hda_nid_t nid,
  80. unsigned char *buf, int *eld_size, bool rev3_or_later)
  81. {
  82. int spkalloc, ati_sad, aud_synch;
  83. int sink_desc_len = 0;
  84. int pos, i;
  85. /* ATI/AMD does not have ELD, emulate it */
  86. spkalloc = snd_hda_codec_read(codec, nid, 0, ATI_VERB_GET_SPEAKER_ALLOCATION, 0);
  87. if (spkalloc <= 0) {
  88. codec_info(codec, "HDMI ATI/AMD: no speaker allocation for ELD\n");
  89. return -EINVAL;
  90. }
  91. memset(buf, 0, ELD_FIXED_BYTES + ELD_MAX_MNL + ELD_MAX_SAD * 3);
  92. /* version */
  93. buf[0] = ELD_VER_CEA_861D << 3;
  94. /* speaker allocation from EDID */
  95. buf[7] = spkalloc & ATI_SPKALLOC_SPKALLOC;
  96. /* is DisplayPort? */
  97. if (spkalloc & ATI_SPKALLOC_TYPE_DISPLAYPORT)
  98. buf[5] |= 0x04;
  99. pos = ELD_FIXED_BYTES;
  100. if (rev3_or_later) {
  101. int sink_info;
  102. snd_hda_codec_write(codec, nid, 0, ATI_VERB_SET_SINK_INFO_INDEX, ATI_INFO_IDX_PORT_ID_LOW);
  103. sink_info = snd_hda_codec_read(codec, nid, 0, ATI_VERB_GET_SINK_INFO_DATA, 0);
  104. put_unaligned_le32(sink_info, buf + 8);
  105. snd_hda_codec_write(codec, nid, 0, ATI_VERB_SET_SINK_INFO_INDEX, ATI_INFO_IDX_PORT_ID_HIGH);
  106. sink_info = snd_hda_codec_read(codec, nid, 0, ATI_VERB_GET_SINK_INFO_DATA, 0);
  107. put_unaligned_le32(sink_info, buf + 12);
  108. snd_hda_codec_write(codec, nid, 0, ATI_VERB_SET_SINK_INFO_INDEX, ATI_INFO_IDX_MANUFACTURER_ID);
  109. sink_info = snd_hda_codec_read(codec, nid, 0, ATI_VERB_GET_SINK_INFO_DATA, 0);
  110. put_unaligned_le16(sink_info, buf + 16);
  111. snd_hda_codec_write(codec, nid, 0, ATI_VERB_SET_SINK_INFO_INDEX, ATI_INFO_IDX_PRODUCT_ID);
  112. sink_info = snd_hda_codec_read(codec, nid, 0, ATI_VERB_GET_SINK_INFO_DATA, 0);
  113. put_unaligned_le16(sink_info, buf + 18);
  114. snd_hda_codec_write(codec, nid, 0, ATI_VERB_SET_SINK_INFO_INDEX, ATI_INFO_IDX_SINK_DESC_LEN);
  115. sink_desc_len = snd_hda_codec_read(codec, nid, 0, ATI_VERB_GET_SINK_INFO_DATA, 0);
  116. if (sink_desc_len > ELD_MAX_MNL) {
  117. codec_info(codec, "HDMI ATI/AMD: Truncating HDMI sink description with length %d\n",
  118. sink_desc_len);
  119. sink_desc_len = ELD_MAX_MNL;
  120. }
  121. buf[4] |= sink_desc_len;
  122. for (i = 0; i < sink_desc_len; i++) {
  123. snd_hda_codec_write(codec, nid, 0, ATI_VERB_SET_SINK_INFO_INDEX, ATI_INFO_IDX_SINK_DESC_FIRST + i);
  124. buf[pos++] = snd_hda_codec_read(codec, nid, 0, ATI_VERB_GET_SINK_INFO_DATA, 0);
  125. }
  126. }
  127. for (i = AUDIO_CODING_TYPE_LPCM; i <= AUDIO_CODING_TYPE_WMAPRO; i++) {
  128. if (i == AUDIO_CODING_TYPE_SACD || i == AUDIO_CODING_TYPE_DST)
  129. continue; /* not handled by ATI/AMD */
  130. snd_hda_codec_write(codec, nid, 0, ATI_VERB_SET_AUDIO_DESCRIPTOR, i << 3);
  131. ati_sad = snd_hda_codec_read(codec, nid, 0, ATI_VERB_GET_AUDIO_DESCRIPTOR, 0);
  132. if (ati_sad <= 0)
  133. continue;
  134. if (ati_sad & ATI_AUDIODESC_RATES) {
  135. /* format is supported, copy SAD as-is */
  136. buf[pos++] = (ati_sad & 0x0000ff) >> 0;
  137. buf[pos++] = (ati_sad & 0x00ff00) >> 8;
  138. buf[pos++] = (ati_sad & 0xff0000) >> 16;
  139. }
  140. if (i == AUDIO_CODING_TYPE_LPCM
  141. && (ati_sad & ATI_AUDIODESC_LPCM_STEREO_RATES)
  142. && (ati_sad & ATI_AUDIODESC_LPCM_STEREO_RATES) >> 16 != (ati_sad & ATI_AUDIODESC_RATES)) {
  143. /* for PCM there is a separate stereo rate mask */
  144. buf[pos++] = ((ati_sad & 0x000000ff) & ~ATI_AUDIODESC_CHANNELS) | 0x1;
  145. /* rates from the extra byte */
  146. buf[pos++] = (ati_sad & 0xff000000) >> 24;
  147. buf[pos++] = (ati_sad & 0x00ff0000) >> 16;
  148. }
  149. }
  150. if (pos == ELD_FIXED_BYTES + sink_desc_len) {
  151. codec_info(codec, "HDMI ATI/AMD: no audio descriptors for ELD\n");
  152. return -EINVAL;
  153. }
  154. /*
  155. * HDMI VSDB latency format:
  156. * separately for both audio and video:
  157. * 0 field not valid or unknown latency
  158. * [1..251] msecs = (x-1)*2 (max 500ms with x = 251 = 0xfb)
  159. * 255 audio/video not supported
  160. *
  161. * HDA latency format:
  162. * single value indicating video latency relative to audio:
  163. * 0 unknown or 0ms
  164. * [1..250] msecs = x*2 (max 500ms with x = 250 = 0xfa)
  165. * [251..255] reserved
  166. */
  167. aud_synch = snd_hda_codec_read(codec, nid, 0, ATI_VERB_GET_AUDIO_VIDEO_DELAY, 0);
  168. if ((aud_synch & ATI_DELAY_VIDEO_LATENCY) && (aud_synch & ATI_DELAY_AUDIO_LATENCY)) {
  169. int video_latency_hdmi = (aud_synch & ATI_DELAY_VIDEO_LATENCY);
  170. int audio_latency_hdmi = (aud_synch & ATI_DELAY_AUDIO_LATENCY) >> 8;
  171. if (video_latency_hdmi <= 0xfb && audio_latency_hdmi <= 0xfb &&
  172. video_latency_hdmi > audio_latency_hdmi)
  173. buf[6] = video_latency_hdmi - audio_latency_hdmi;
  174. /* else unknown/invalid or 0ms or video ahead of audio, so use zero */
  175. }
  176. /* SAD count */
  177. buf[5] |= ((pos - ELD_FIXED_BYTES - sink_desc_len) / 3) << 4;
  178. /* Baseline ELD block length is 4-byte aligned */
  179. pos = round_up(pos, 4);
  180. /* Baseline ELD length (4-byte header is not counted in) */
  181. buf[2] = (pos - 4) / 4;
  182. *eld_size = pos;
  183. return 0;
  184. }
  185. static int atihdmi_pin_get_eld(struct hda_codec *codec, hda_nid_t nid,
  186. int dev_id, unsigned char *buf, int *eld_size)
  187. {
  188. WARN_ON(dev_id != 0);
  189. /* call hda_eld.c ATI/AMD-specific function */
  190. return get_eld_ati(codec, nid, buf, eld_size,
  191. is_amdhdmi_rev3_or_later(codec));
  192. }
  193. static void atihdmi_pin_setup_infoframe(struct hda_codec *codec,
  194. hda_nid_t pin_nid, int dev_id, int ca,
  195. int active_channels, int conn_type)
  196. {
  197. WARN_ON(dev_id != 0);
  198. snd_hda_codec_write(codec, pin_nid, 0, ATI_VERB_SET_CHANNEL_ALLOCATION, ca);
  199. }
  200. static int atihdmi_paired_swap_fc_lfe(int pos)
  201. {
  202. /*
  203. * ATI/AMD have automatic FC/LFE swap built-in
  204. * when in pairwise mapping mode.
  205. */
  206. switch (pos) {
  207. /* see channel_allocations[].speakers[] */
  208. case 2: return 3;
  209. case 3: return 2;
  210. default: return pos;
  211. }
  212. }
  213. static int atihdmi_paired_chmap_validate(struct hdac_chmap *chmap,
  214. int ca, int chs, unsigned char *map)
  215. {
  216. struct hdac_cea_channel_speaker_allocation *cap;
  217. int i, j;
  218. /* check that only channel pairs need to be remapped on old pre-rev3 ATI/AMD */
  219. cap = snd_hdac_get_ch_alloc_from_ca(ca);
  220. for (i = 0; i < chs; ++i) {
  221. int mask = snd_hdac_chmap_to_spk_mask(map[i]);
  222. bool ok = false;
  223. bool companion_ok = false;
  224. if (!mask)
  225. continue;
  226. for (j = 0 + i % 2; j < 8; j += 2) {
  227. int chan_idx = 7 - atihdmi_paired_swap_fc_lfe(j);
  228. if (cap->speakers[chan_idx] == mask) {
  229. /* channel is in a supported position */
  230. ok = true;
  231. if (i % 2 == 0 && i + 1 < chs) {
  232. /* even channel, check the odd companion */
  233. int comp_chan_idx = 7 - atihdmi_paired_swap_fc_lfe(j + 1);
  234. int comp_mask_req = snd_hdac_chmap_to_spk_mask(map[i+1]);
  235. int comp_mask_act = cap->speakers[comp_chan_idx];
  236. if (comp_mask_req == comp_mask_act)
  237. companion_ok = true;
  238. else
  239. return -EINVAL;
  240. }
  241. break;
  242. }
  243. }
  244. if (!ok)
  245. return -EINVAL;
  246. if (companion_ok)
  247. i++; /* companion channel already checked */
  248. }
  249. return 0;
  250. }
  251. static int atihdmi_pin_set_slot_channel(struct hdac_device *hdac,
  252. hda_nid_t pin_nid, int hdmi_slot, int stream_channel)
  253. {
  254. struct hda_codec *codec = hdac_to_hda_codec(hdac);
  255. int verb;
  256. int ati_channel_setup = 0;
  257. if (hdmi_slot > 7)
  258. return -EINVAL;
  259. if (!has_amd_full_remap_support(codec)) {
  260. hdmi_slot = atihdmi_paired_swap_fc_lfe(hdmi_slot);
  261. /* In case this is an odd slot but without stream channel, do not
  262. * disable the slot since the corresponding even slot could have a
  263. * channel. In case neither have a channel, the slot pair will be
  264. * disabled when this function is called for the even slot.
  265. */
  266. if (hdmi_slot % 2 != 0 && stream_channel == 0xf)
  267. return 0;
  268. hdmi_slot -= hdmi_slot % 2;
  269. if (stream_channel != 0xf)
  270. stream_channel -= stream_channel % 2;
  271. }
  272. verb = ATI_VERB_SET_MULTICHANNEL_01 + hdmi_slot/2 + (hdmi_slot % 2) * 0x00e;
  273. /* ati_channel_setup format: [7..4] = stream_channel_id, [1] = mute, [0] = enable */
  274. if (stream_channel != 0xf)
  275. ati_channel_setup = (stream_channel << 4) | ATI_OUT_ENABLE;
  276. return snd_hda_codec_write(codec, pin_nid, 0, verb, ati_channel_setup);
  277. }
  278. static int atihdmi_pin_get_slot_channel(struct hdac_device *hdac,
  279. hda_nid_t pin_nid, int asp_slot)
  280. {
  281. struct hda_codec *codec = hdac_to_hda_codec(hdac);
  282. bool was_odd = false;
  283. int ati_asp_slot = asp_slot;
  284. int verb;
  285. int ati_channel_setup;
  286. if (asp_slot > 7)
  287. return -EINVAL;
  288. if (!has_amd_full_remap_support(codec)) {
  289. ati_asp_slot = atihdmi_paired_swap_fc_lfe(asp_slot);
  290. if (ati_asp_slot % 2 != 0) {
  291. ati_asp_slot -= 1;
  292. was_odd = true;
  293. }
  294. }
  295. verb = ATI_VERB_GET_MULTICHANNEL_01 + ati_asp_slot/2 + (ati_asp_slot % 2) * 0x00e;
  296. ati_channel_setup = snd_hda_codec_read(codec, pin_nid, 0, verb, 0);
  297. if (!(ati_channel_setup & ATI_OUT_ENABLE))
  298. return 0xf;
  299. return ((ati_channel_setup & 0xf0) >> 4) + !!was_odd;
  300. }
  301. static int atihdmi_paired_chmap_cea_alloc_validate_get_type(
  302. struct hdac_chmap *chmap,
  303. struct hdac_cea_channel_speaker_allocation *cap,
  304. int channels)
  305. {
  306. int c;
  307. /*
  308. * Pre-rev3 ATI/AMD codecs operate in a paired channel mode, so
  309. * we need to take that into account (a single channel may take 2
  310. * channel slots if we need to carry a silent channel next to it).
  311. * On Rev3+ AMD codecs this function is not used.
  312. */
  313. int chanpairs = 0;
  314. /* We only produce even-numbered channel count TLVs */
  315. if ((channels % 2) != 0)
  316. return -1;
  317. for (c = 0; c < 7; c += 2) {
  318. if (cap->speakers[c] || cap->speakers[c+1])
  319. chanpairs++;
  320. }
  321. if (chanpairs * 2 != channels)
  322. return -1;
  323. return SNDRV_CTL_TLVT_CHMAP_PAIRED;
  324. }
  325. static void atihdmi_paired_cea_alloc_to_tlv_chmap(struct hdac_chmap *hchmap,
  326. struct hdac_cea_channel_speaker_allocation *cap,
  327. unsigned int *chmap, int channels)
  328. {
  329. /* produce paired maps for pre-rev3 ATI/AMD codecs */
  330. int count = 0;
  331. int c;
  332. for (c = 7; c >= 0; c--) {
  333. int chan = 7 - atihdmi_paired_swap_fc_lfe(7 - c);
  334. int spk = cap->speakers[chan];
  335. if (!spk) {
  336. /* add N/A channel if the companion channel is occupied */
  337. if (cap->speakers[chan + (chan % 2 ? -1 : 1)])
  338. chmap[count++] = SNDRV_CHMAP_NA;
  339. continue;
  340. }
  341. chmap[count++] = snd_hdac_spk_to_chmap(spk);
  342. }
  343. WARN_ON(count != channels);
  344. }
  345. static int atihdmi_pin_hbr_setup(struct hda_codec *codec, hda_nid_t pin_nid,
  346. int dev_id, bool hbr)
  347. {
  348. int hbr_ctl, hbr_ctl_new;
  349. WARN_ON(dev_id != 0);
  350. hbr_ctl = snd_hda_codec_read(codec, pin_nid, 0, ATI_VERB_GET_HBR_CONTROL, 0);
  351. if (hbr_ctl >= 0 && (hbr_ctl & ATI_HBR_CAPABLE)) {
  352. if (hbr)
  353. hbr_ctl_new = hbr_ctl | ATI_HBR_ENABLE;
  354. else
  355. hbr_ctl_new = hbr_ctl & ~ATI_HBR_ENABLE;
  356. codec_dbg(codec,
  357. "%s: NID=0x%x, %shbr-ctl=0x%x\n",
  358. __func__,
  359. pin_nid,
  360. hbr_ctl == hbr_ctl_new ? "" : "new-",
  361. hbr_ctl_new);
  362. if (hbr_ctl != hbr_ctl_new)
  363. snd_hda_codec_write(codec, pin_nid, 0,
  364. ATI_VERB_SET_HBR_CONTROL,
  365. hbr_ctl_new);
  366. } else if (hbr)
  367. return -EINVAL;
  368. return 0;
  369. }
  370. static int atihdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
  371. hda_nid_t pin_nid, int dev_id,
  372. u32 stream_tag, int format)
  373. {
  374. if (is_amdhdmi_rev3_or_later(codec)) {
  375. int ramp_rate = 180; /* default as per AMD spec */
  376. /* disable ramp-up/down for non-pcm as per AMD spec */
  377. if (format & AC_FMT_TYPE_NON_PCM)
  378. ramp_rate = 0;
  379. snd_hda_codec_write(codec, cvt_nid, 0, ATI_VERB_SET_RAMP_RATE, ramp_rate);
  380. }
  381. return snd_hda_hdmi_setup_stream(codec, cvt_nid, pin_nid, dev_id,
  382. stream_tag, format);
  383. }
  384. static int atihdmi_init(struct hda_codec *codec)
  385. {
  386. struct hdmi_spec *spec = codec->spec;
  387. int pin_idx, err;
  388. err = snd_hda_hdmi_generic_init(codec);
  389. if (err)
  390. return err;
  391. for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
  392. struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
  393. /* make sure downmix information in infoframe is zero */
  394. snd_hda_codec_write(codec, per_pin->pin_nid, 0, ATI_VERB_SET_DOWNMIX_INFO, 0);
  395. /* enable channel-wise remap mode if supported */
  396. if (has_amd_full_remap_support(codec))
  397. snd_hda_codec_write(codec, per_pin->pin_nid, 0,
  398. ATI_VERB_SET_MULTICHANNEL_MODE,
  399. ATI_MULTICHANNEL_MODE_SINGLE);
  400. }
  401. codec->auto_runtime_pm = 1;
  402. return 0;
  403. }
  404. /* map from pin NID to port; port is 0-based */
  405. /* for AMD: assume widget NID starting from 3, with step 2 (3, 5, 7, ...) */
  406. static int atihdmi_pin2port(void *audio_ptr, int pin_nid)
  407. {
  408. return pin_nid / 2 - 1;
  409. }
  410. /* reverse-map from port to pin NID: see above */
  411. static int atihdmi_port2pin(struct hda_codec *codec, int port)
  412. {
  413. return port * 2 + 3;
  414. }
  415. static const struct drm_audio_component_audio_ops atihdmi_audio_ops = {
  416. .pin2port = atihdmi_pin2port,
  417. .pin_eld_notify = snd_hda_hdmi_acomp_pin_eld_notify,
  418. .master_bind = snd_hda_hdmi_acomp_master_bind,
  419. .master_unbind = snd_hda_hdmi_acomp_master_unbind,
  420. };
  421. static int atihdmi_probe(struct hda_codec *codec, const struct hda_device_id *id)
  422. {
  423. struct hdmi_spec *spec;
  424. struct hdmi_spec_per_cvt *per_cvt;
  425. int err, cvt_idx;
  426. err = snd_hda_hdmi_generic_probe(codec);
  427. if (err)
  428. return err;
  429. spec = codec->spec;
  430. spec->static_pcm_mapping = true;
  431. spec->ops.pin_get_eld = atihdmi_pin_get_eld;
  432. spec->ops.pin_setup_infoframe = atihdmi_pin_setup_infoframe;
  433. spec->ops.pin_hbr_setup = atihdmi_pin_hbr_setup;
  434. spec->ops.setup_stream = atihdmi_setup_stream;
  435. spec->chmap.ops.pin_get_slot_channel = atihdmi_pin_get_slot_channel;
  436. spec->chmap.ops.pin_set_slot_channel = atihdmi_pin_set_slot_channel;
  437. if (!has_amd_full_remap_support(codec)) {
  438. /* override to ATI/AMD-specific versions with pairwise mapping */
  439. spec->chmap.ops.chmap_cea_alloc_validate_get_type =
  440. atihdmi_paired_chmap_cea_alloc_validate_get_type;
  441. spec->chmap.ops.cea_alloc_to_tlv_chmap =
  442. atihdmi_paired_cea_alloc_to_tlv_chmap;
  443. spec->chmap.ops.chmap_validate = atihdmi_paired_chmap_validate;
  444. }
  445. /* ATI/AMD converters do not advertise all of their capabilities */
  446. for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
  447. per_cvt = get_cvt(spec, cvt_idx);
  448. per_cvt->channels_max = max(per_cvt->channels_max, 8u);
  449. per_cvt->rates |= SUPPORTED_RATES;
  450. per_cvt->formats |= SUPPORTED_FORMATS;
  451. per_cvt->maxbps = max(per_cvt->maxbps, 24u);
  452. }
  453. spec->chmap.channels_max = max(spec->chmap.channels_max, 8u);
  454. /* AMD GPUs have neither EPSS nor CLKSTOP bits, hence preventing
  455. * the link-down as is. Tell the core to allow it.
  456. */
  457. codec->link_down_at_suspend = 1;
  458. snd_hda_hdmi_acomp_init(codec, &atihdmi_audio_ops, atihdmi_port2pin);
  459. return 0;
  460. }
  461. static const struct hda_codec_ops atihdmi_codec_ops = {
  462. .probe = atihdmi_probe,
  463. .remove = snd_hda_hdmi_generic_remove,
  464. .init = atihdmi_init,
  465. .build_pcms = snd_hda_hdmi_generic_build_pcms,
  466. .build_controls = snd_hda_hdmi_generic_build_controls,
  467. .unsol_event = snd_hda_hdmi_generic_unsol_event,
  468. .suspend = snd_hda_hdmi_generic_suspend,
  469. .resume = snd_hda_hdmi_generic_resume,
  470. };
  471. /*
  472. * driver entries
  473. */
  474. static const struct hda_device_id snd_hda_id_atihdmi[] = {
  475. HDA_CODEC_ID(0x1002793c, "RS600 HDMI"),
  476. HDA_CODEC_ID(0x10027919, "RS600 HDMI"),
  477. HDA_CODEC_ID(0x1002791a, "RS690/780 HDMI"),
  478. HDA_CODEC_ID(0x1002aa01, "R6xx HDMI"),
  479. {} /* terminator */
  480. };
  481. MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_atihdmi);
  482. MODULE_LICENSE("GPL");
  483. MODULE_DESCRIPTION("AMD/ATI HDMI HD-audio codec");
  484. MODULE_IMPORT_NS("SND_HDA_CODEC_HDMI");
  485. static struct hda_codec_driver atihdmi_driver = {
  486. .id = snd_hda_id_atihdmi,
  487. .ops = &atihdmi_codec_ops,
  488. };
  489. module_hda_codec_driver(atihdmi_driver);