hdac_hdmi.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856
  1. /*
  2. * hdac_hdmi.c - ASoc HDA-HDMI codec driver for Intel platforms
  3. *
  4. * Copyright (C) 2014-2015 Intel Corp
  5. * Author: Samreen Nilofer <samreen.nilofer@intel.com>
  6. * Subhransu S. Prusty <subhransu.s.prusty@intel.com>
  7. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; version 2 of the License.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  19. */
  20. #include <linux/init.h>
  21. #include <linux/delay.h>
  22. #include <linux/module.h>
  23. #include <linux/pm_runtime.h>
  24. #include <linux/hdmi.h>
  25. #include <drm/drm_edid.h>
  26. #include <sound/pcm_params.h>
  27. #include <sound/jack.h>
  28. #include <sound/soc.h>
  29. #include <sound/hdaudio_ext.h>
  30. #include <sound/hda_i915.h>
  31. #include <sound/pcm_drm_eld.h>
  32. #include <sound/hda_chmap.h>
  33. #include "../../hda/local.h"
  34. #include "hdac_hdmi.h"
  35. #define NAME_SIZE 32
  36. #define AMP_OUT_MUTE 0xb080
  37. #define AMP_OUT_UNMUTE 0xb000
  38. #define PIN_OUT (AC_PINCTL_OUT_EN)
  39. #define HDA_MAX_CONNECTIONS 32
  40. #define HDA_MAX_CVTS 3
  41. #define ELD_MAX_SIZE 256
  42. #define ELD_FIXED_BYTES 20
  43. struct hdac_hdmi_cvt_params {
  44. unsigned int channels_min;
  45. unsigned int channels_max;
  46. u32 rates;
  47. u64 formats;
  48. unsigned int maxbps;
  49. };
  50. struct hdac_hdmi_cvt {
  51. struct list_head head;
  52. hda_nid_t nid;
  53. const char *name;
  54. struct hdac_hdmi_cvt_params params;
  55. };
  56. /* Currently only spk_alloc, more to be added */
  57. struct hdac_hdmi_parsed_eld {
  58. u8 spk_alloc;
  59. };
  60. struct hdac_hdmi_eld {
  61. bool monitor_present;
  62. bool eld_valid;
  63. int eld_size;
  64. char eld_buffer[ELD_MAX_SIZE];
  65. struct hdac_hdmi_parsed_eld info;
  66. };
  67. struct hdac_hdmi_pin {
  68. struct list_head head;
  69. hda_nid_t nid;
  70. int num_mux_nids;
  71. hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
  72. struct hdac_hdmi_eld eld;
  73. struct hdac_ext_device *edev;
  74. int repoll_count;
  75. struct delayed_work work;
  76. struct mutex lock;
  77. bool chmap_set;
  78. unsigned char chmap[8]; /* ALSA API channel-map */
  79. int channels; /* current number of channels */
  80. };
  81. struct hdac_hdmi_pcm {
  82. struct list_head head;
  83. int pcm_id;
  84. struct hdac_hdmi_pin *pin;
  85. struct hdac_hdmi_cvt *cvt;
  86. struct snd_jack *jack;
  87. };
  88. struct hdac_hdmi_dai_pin_map {
  89. int dai_id;
  90. struct hdac_hdmi_pin *pin;
  91. struct hdac_hdmi_cvt *cvt;
  92. };
  93. struct hdac_hdmi_priv {
  94. struct hdac_hdmi_dai_pin_map dai_map[HDA_MAX_CVTS];
  95. struct list_head pin_list;
  96. struct list_head cvt_list;
  97. struct list_head pcm_list;
  98. int num_pin;
  99. int num_cvt;
  100. struct mutex pin_mutex;
  101. struct hdac_chmap chmap;
  102. };
  103. static struct hdac_hdmi_pcm *get_hdmi_pcm_from_id(struct hdac_hdmi_priv *hdmi,
  104. int pcm_idx)
  105. {
  106. struct hdac_hdmi_pcm *pcm;
  107. list_for_each_entry(pcm, &hdmi->pcm_list, head) {
  108. if (pcm->pcm_id == pcm_idx)
  109. return pcm;
  110. }
  111. return NULL;
  112. }
  113. static inline struct hdac_ext_device *to_hda_ext_device(struct device *dev)
  114. {
  115. struct hdac_device *hdac = dev_to_hdac_dev(dev);
  116. return to_ehdac_device(hdac);
  117. }
  118. static unsigned int sad_format(const u8 *sad)
  119. {
  120. return ((sad[0] >> 0x3) & 0x1f);
  121. }
  122. static unsigned int sad_sample_bits_lpcm(const u8 *sad)
  123. {
  124. return (sad[2] & 7);
  125. }
  126. static int hdac_hdmi_eld_limit_formats(struct snd_pcm_runtime *runtime,
  127. void *eld)
  128. {
  129. u64 formats = SNDRV_PCM_FMTBIT_S16;
  130. int i;
  131. const u8 *sad, *eld_buf = eld;
  132. sad = drm_eld_sad(eld_buf);
  133. if (!sad)
  134. goto format_constraint;
  135. for (i = drm_eld_sad_count(eld_buf); i > 0; i--, sad += 3) {
  136. if (sad_format(sad) == 1) { /* AUDIO_CODING_TYPE_LPCM */
  137. /*
  138. * the controller support 20 and 24 bits in 32 bit
  139. * container so we set S32
  140. */
  141. if (sad_sample_bits_lpcm(sad) & 0x6)
  142. formats |= SNDRV_PCM_FMTBIT_S32;
  143. }
  144. }
  145. format_constraint:
  146. return snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT,
  147. formats);
  148. }
  149. /* HDMI ELD routines */
  150. static unsigned int hdac_hdmi_get_eld_data(struct hdac_device *codec,
  151. hda_nid_t nid, int byte_index)
  152. {
  153. unsigned int val;
  154. val = snd_hdac_codec_read(codec, nid, 0, AC_VERB_GET_HDMI_ELDD,
  155. byte_index);
  156. dev_dbg(&codec->dev, "HDMI: ELD data byte %d: 0x%x\n",
  157. byte_index, val);
  158. return val;
  159. }
  160. static int hdac_hdmi_get_eld_size(struct hdac_device *codec, hda_nid_t nid)
  161. {
  162. return snd_hdac_codec_read(codec, nid, 0, AC_VERB_GET_HDMI_DIP_SIZE,
  163. AC_DIPSIZE_ELD_BUF);
  164. }
  165. /*
  166. * This function queries the ELD size and ELD data and fills in the buffer
  167. * passed by user
  168. */
  169. static int hdac_hdmi_get_eld(struct hdac_device *codec, hda_nid_t nid,
  170. unsigned char *buf, int *eld_size)
  171. {
  172. int i, size, ret = 0;
  173. /*
  174. * ELD size is initialized to zero in caller function. If no errors and
  175. * ELD is valid, actual eld_size is assigned.
  176. */
  177. size = hdac_hdmi_get_eld_size(codec, nid);
  178. if (size < ELD_FIXED_BYTES || size > ELD_MAX_SIZE) {
  179. dev_err(&codec->dev, "HDMI: invalid ELD buf size %d\n", size);
  180. return -ERANGE;
  181. }
  182. /* set ELD buffer */
  183. for (i = 0; i < size; i++) {
  184. unsigned int val = hdac_hdmi_get_eld_data(codec, nid, i);
  185. /*
  186. * Graphics driver might be writing to ELD buffer right now.
  187. * Just abort. The caller will repoll after a while.
  188. */
  189. if (!(val & AC_ELDD_ELD_VALID)) {
  190. dev_err(&codec->dev,
  191. "HDMI: invalid ELD data byte %d\n", i);
  192. ret = -EINVAL;
  193. goto error;
  194. }
  195. val &= AC_ELDD_ELD_DATA;
  196. /*
  197. * The first byte cannot be zero. This can happen on some DVI
  198. * connections. Some Intel chips may also need some 250ms delay
  199. * to return non-zero ELD data, even when the graphics driver
  200. * correctly writes ELD content before setting ELD_valid bit.
  201. */
  202. if (!val && !i) {
  203. dev_err(&codec->dev, "HDMI: 0 ELD data\n");
  204. ret = -EINVAL;
  205. goto error;
  206. }
  207. buf[i] = val;
  208. }
  209. *eld_size = size;
  210. error:
  211. return ret;
  212. }
  213. static int hdac_hdmi_setup_stream(struct hdac_ext_device *hdac,
  214. hda_nid_t cvt_nid, hda_nid_t pin_nid,
  215. u32 stream_tag, int format)
  216. {
  217. unsigned int val;
  218. dev_dbg(&hdac->hdac.dev, "cvt nid %d pnid %d stream %d format 0x%x\n",
  219. cvt_nid, pin_nid, stream_tag, format);
  220. val = (stream_tag << 4);
  221. snd_hdac_codec_write(&hdac->hdac, cvt_nid, 0,
  222. AC_VERB_SET_CHANNEL_STREAMID, val);
  223. snd_hdac_codec_write(&hdac->hdac, cvt_nid, 0,
  224. AC_VERB_SET_STREAM_FORMAT, format);
  225. return 0;
  226. }
  227. static void
  228. hdac_hdmi_set_dip_index(struct hdac_ext_device *hdac, hda_nid_t pin_nid,
  229. int packet_index, int byte_index)
  230. {
  231. int val;
  232. val = (packet_index << 5) | (byte_index & 0x1f);
  233. snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
  234. AC_VERB_SET_HDMI_DIP_INDEX, val);
  235. }
  236. struct dp_audio_infoframe {
  237. u8 type; /* 0x84 */
  238. u8 len; /* 0x1b */
  239. u8 ver; /* 0x11 << 2 */
  240. u8 CC02_CT47; /* match with HDMI infoframe from this on */
  241. u8 SS01_SF24;
  242. u8 CXT04;
  243. u8 CA;
  244. u8 LFEPBL01_LSV36_DM_INH7;
  245. };
  246. static int hdac_hdmi_setup_audio_infoframe(struct hdac_ext_device *hdac,
  247. hda_nid_t cvt_nid, hda_nid_t pin_nid)
  248. {
  249. uint8_t buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AUDIO_INFOFRAME_SIZE];
  250. struct hdmi_audio_infoframe frame;
  251. struct dp_audio_infoframe dp_ai;
  252. struct hdac_hdmi_priv *hdmi = hdac->private_data;
  253. struct hdac_hdmi_pin *pin;
  254. u8 *dip;
  255. int ret;
  256. int i;
  257. const u8 *eld_buf;
  258. u8 conn_type;
  259. int channels, ca;
  260. list_for_each_entry(pin, &hdmi->pin_list, head) {
  261. if (pin->nid == pin_nid)
  262. break;
  263. }
  264. ca = snd_hdac_channel_allocation(&hdac->hdac, pin->eld.info.spk_alloc,
  265. pin->channels, pin->chmap_set, true, pin->chmap);
  266. channels = snd_hdac_get_active_channels(ca);
  267. hdmi->chmap.ops.set_channel_count(&hdac->hdac, cvt_nid, channels);
  268. snd_hdac_setup_channel_mapping(&hdmi->chmap, pin->nid, false, ca,
  269. pin->channels, pin->chmap, pin->chmap_set);
  270. eld_buf = pin->eld.eld_buffer;
  271. conn_type = drm_eld_get_conn_type(eld_buf);
  272. switch (conn_type) {
  273. case DRM_ELD_CONN_TYPE_HDMI:
  274. hdmi_audio_infoframe_init(&frame);
  275. frame.channels = channels;
  276. frame.channel_allocation = ca;
  277. ret = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer));
  278. if (ret < 0)
  279. return ret;
  280. break;
  281. case DRM_ELD_CONN_TYPE_DP:
  282. memset(&dp_ai, 0, sizeof(dp_ai));
  283. dp_ai.type = 0x84;
  284. dp_ai.len = 0x1b;
  285. dp_ai.ver = 0x11 << 2;
  286. dp_ai.CC02_CT47 = channels - 1;
  287. dp_ai.CA = ca;
  288. dip = (u8 *)&dp_ai;
  289. break;
  290. default:
  291. dev_err(&hdac->hdac.dev, "Invalid connection type: %d\n",
  292. conn_type);
  293. return -EIO;
  294. }
  295. /* stop infoframe transmission */
  296. hdac_hdmi_set_dip_index(hdac, pin_nid, 0x0, 0x0);
  297. snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
  298. AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_DISABLE);
  299. /* Fill infoframe. Index auto-incremented */
  300. hdac_hdmi_set_dip_index(hdac, pin_nid, 0x0, 0x0);
  301. if (conn_type == DRM_ELD_CONN_TYPE_HDMI) {
  302. for (i = 0; i < sizeof(buffer); i++)
  303. snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
  304. AC_VERB_SET_HDMI_DIP_DATA, buffer[i]);
  305. } else {
  306. for (i = 0; i < sizeof(dp_ai); i++)
  307. snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
  308. AC_VERB_SET_HDMI_DIP_DATA, dip[i]);
  309. }
  310. /* Start infoframe */
  311. hdac_hdmi_set_dip_index(hdac, pin_nid, 0x0, 0x0);
  312. snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
  313. AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_BEST);
  314. return 0;
  315. }
  316. static void hdac_hdmi_set_power_state(struct hdac_ext_device *edev,
  317. struct hdac_hdmi_dai_pin_map *dai_map, unsigned int pwr_state)
  318. {
  319. /* Power up pin widget */
  320. if (!snd_hdac_check_power_state(&edev->hdac, dai_map->pin->nid,
  321. pwr_state))
  322. snd_hdac_codec_write(&edev->hdac, dai_map->pin->nid, 0,
  323. AC_VERB_SET_POWER_STATE, pwr_state);
  324. /* Power up converter */
  325. if (!snd_hdac_check_power_state(&edev->hdac, dai_map->cvt->nid,
  326. pwr_state))
  327. snd_hdac_codec_write(&edev->hdac, dai_map->cvt->nid, 0,
  328. AC_VERB_SET_POWER_STATE, pwr_state);
  329. }
  330. static int hdac_hdmi_playback_prepare(struct snd_pcm_substream *substream,
  331. struct snd_soc_dai *dai)
  332. {
  333. struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
  334. struct hdac_hdmi_priv *hdmi = hdac->private_data;
  335. struct hdac_hdmi_dai_pin_map *dai_map;
  336. struct hdac_hdmi_pin *pin;
  337. struct hdac_ext_dma_params *dd;
  338. int ret;
  339. dai_map = &hdmi->dai_map[dai->id];
  340. pin = dai_map->pin;
  341. dd = (struct hdac_ext_dma_params *)snd_soc_dai_get_dma_data(dai, substream);
  342. dev_dbg(&hdac->hdac.dev, "stream tag from cpu dai %d format in cvt 0x%x\n",
  343. dd->stream_tag, dd->format);
  344. mutex_lock(&pin->lock);
  345. pin->channels = substream->runtime->channels;
  346. ret = hdac_hdmi_setup_audio_infoframe(hdac, dai_map->cvt->nid,
  347. dai_map->pin->nid);
  348. mutex_unlock(&pin->lock);
  349. if (ret < 0)
  350. return ret;
  351. return hdac_hdmi_setup_stream(hdac, dai_map->cvt->nid,
  352. dai_map->pin->nid, dd->stream_tag, dd->format);
  353. }
  354. static int hdac_hdmi_set_hw_params(struct snd_pcm_substream *substream,
  355. struct snd_pcm_hw_params *hparams, struct snd_soc_dai *dai)
  356. {
  357. struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
  358. struct hdac_hdmi_priv *hdmi = hdac->private_data;
  359. struct hdac_hdmi_dai_pin_map *dai_map;
  360. struct hdac_hdmi_pin *pin;
  361. struct hdac_ext_dma_params *dd;
  362. dai_map = &hdmi->dai_map[dai->id];
  363. pin = dai_map->pin;
  364. if (!pin)
  365. return -ENODEV;
  366. if ((!pin->eld.monitor_present) || (!pin->eld.eld_valid)) {
  367. dev_err(&hdac->hdac.dev, "device is not configured for this pin: %d\n",
  368. pin->nid);
  369. return -ENODEV;
  370. }
  371. dd = snd_soc_dai_get_dma_data(dai, substream);
  372. if (!dd) {
  373. dd = kzalloc(sizeof(*dd), GFP_KERNEL);
  374. if (!dd)
  375. return -ENOMEM;
  376. }
  377. dd->format = snd_hdac_calc_stream_format(params_rate(hparams),
  378. params_channels(hparams), params_format(hparams),
  379. 24, 0);
  380. snd_soc_dai_set_dma_data(dai, substream, (void *)dd);
  381. return 0;
  382. }
  383. static int hdac_hdmi_playback_cleanup(struct snd_pcm_substream *substream,
  384. struct snd_soc_dai *dai)
  385. {
  386. struct hdac_ext_device *edev = snd_soc_dai_get_drvdata(dai);
  387. struct hdac_ext_dma_params *dd;
  388. struct hdac_hdmi_priv *hdmi = edev->private_data;
  389. struct hdac_hdmi_dai_pin_map *dai_map;
  390. dai_map = &hdmi->dai_map[dai->id];
  391. dd = (struct hdac_ext_dma_params *)snd_soc_dai_get_dma_data(dai, substream);
  392. if (dd) {
  393. snd_soc_dai_set_dma_data(dai, substream, NULL);
  394. kfree(dd);
  395. }
  396. return 0;
  397. }
  398. static void hdac_hdmi_enable_cvt(struct hdac_ext_device *edev,
  399. struct hdac_hdmi_dai_pin_map *dai_map)
  400. {
  401. /* Enable transmission */
  402. snd_hdac_codec_write(&edev->hdac, dai_map->cvt->nid, 0,
  403. AC_VERB_SET_DIGI_CONVERT_1, 1);
  404. /* Category Code (CC) to zero */
  405. snd_hdac_codec_write(&edev->hdac, dai_map->cvt->nid, 0,
  406. AC_VERB_SET_DIGI_CONVERT_2, 0);
  407. }
  408. static int hdac_hdmi_enable_pin(struct hdac_ext_device *hdac,
  409. struct hdac_hdmi_dai_pin_map *dai_map)
  410. {
  411. int mux_idx;
  412. struct hdac_hdmi_pin *pin = dai_map->pin;
  413. for (mux_idx = 0; mux_idx < pin->num_mux_nids; mux_idx++) {
  414. if (pin->mux_nids[mux_idx] == dai_map->cvt->nid) {
  415. snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
  416. AC_VERB_SET_CONNECT_SEL, mux_idx);
  417. break;
  418. }
  419. }
  420. if (mux_idx == pin->num_mux_nids)
  421. return -EIO;
  422. /* Enable out path for this pin widget */
  423. snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
  424. AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
  425. hdac_hdmi_set_power_state(hdac, dai_map, AC_PWRST_D0);
  426. snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
  427. AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
  428. return 0;
  429. }
  430. static int hdac_hdmi_query_pin_connlist(struct hdac_ext_device *hdac,
  431. struct hdac_hdmi_pin *pin)
  432. {
  433. if (!(get_wcaps(&hdac->hdac, pin->nid) & AC_WCAP_CONN_LIST)) {
  434. dev_warn(&hdac->hdac.dev,
  435. "HDMI: pin %d wcaps %#x does not support connection list\n",
  436. pin->nid, get_wcaps(&hdac->hdac, pin->nid));
  437. return -EINVAL;
  438. }
  439. pin->num_mux_nids = snd_hdac_get_connections(&hdac->hdac, pin->nid,
  440. pin->mux_nids, HDA_MAX_CONNECTIONS);
  441. if (pin->num_mux_nids == 0)
  442. dev_warn(&hdac->hdac.dev, "No connections found for pin: %d\n",
  443. pin->nid);
  444. dev_dbg(&hdac->hdac.dev, "num_mux_nids %d for pin: %d\n",
  445. pin->num_mux_nids, pin->nid);
  446. return pin->num_mux_nids;
  447. }
  448. /*
  449. * Query pcm list and return pin widget to which stream is routed.
  450. *
  451. * Also query connection list of the pin, to validate the cvt to pin map.
  452. *
  453. * Same stream rendering to multiple pins simultaneously can be done
  454. * possibly, but not supported for now in driver. So return the first pin
  455. * connected.
  456. */
  457. static struct hdac_hdmi_pin *hdac_hdmi_get_pin_from_cvt(
  458. struct hdac_ext_device *edev,
  459. struct hdac_hdmi_priv *hdmi,
  460. struct hdac_hdmi_cvt *cvt)
  461. {
  462. struct hdac_hdmi_pcm *pcm;
  463. struct hdac_hdmi_pin *pin = NULL;
  464. int ret, i;
  465. list_for_each_entry(pcm, &hdmi->pcm_list, head) {
  466. if (pcm->cvt == cvt) {
  467. pin = pcm->pin;
  468. break;
  469. }
  470. }
  471. if (pin) {
  472. ret = hdac_hdmi_query_pin_connlist(edev, pin);
  473. if (ret < 0)
  474. return NULL;
  475. for (i = 0; i < pin->num_mux_nids; i++) {
  476. if (pin->mux_nids[i] == cvt->nid)
  477. return pin;
  478. }
  479. }
  480. return NULL;
  481. }
  482. /*
  483. * This tries to get a valid pin and set the HW constraints based on the
  484. * ELD. Even if a valid pin is not found return success so that device open
  485. * doesn't fail.
  486. */
  487. static int hdac_hdmi_pcm_open(struct snd_pcm_substream *substream,
  488. struct snd_soc_dai *dai)
  489. {
  490. struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
  491. struct hdac_hdmi_priv *hdmi = hdac->private_data;
  492. struct hdac_hdmi_dai_pin_map *dai_map;
  493. struct hdac_hdmi_cvt *cvt;
  494. struct hdac_hdmi_pin *pin;
  495. int ret;
  496. dai_map = &hdmi->dai_map[dai->id];
  497. cvt = dai_map->cvt;
  498. pin = hdac_hdmi_get_pin_from_cvt(hdac, hdmi, cvt);
  499. /*
  500. * To make PA and other userland happy.
  501. * userland scans devices so returning error does not help.
  502. */
  503. if (!pin)
  504. return 0;
  505. if ((!pin->eld.monitor_present) ||
  506. (!pin->eld.eld_valid)) {
  507. dev_warn(&hdac->hdac.dev,
  508. "Failed: monitor present? %d ELD valid?: %d for pin: %d\n",
  509. pin->eld.monitor_present, pin->eld.eld_valid, pin->nid);
  510. return 0;
  511. }
  512. dai_map->pin = pin;
  513. hdac_hdmi_enable_cvt(hdac, dai_map);
  514. ret = hdac_hdmi_enable_pin(hdac, dai_map);
  515. if (ret < 0)
  516. return ret;
  517. ret = hdac_hdmi_eld_limit_formats(substream->runtime,
  518. pin->eld.eld_buffer);
  519. if (ret < 0)
  520. return ret;
  521. return snd_pcm_hw_constraint_eld(substream->runtime,
  522. pin->eld.eld_buffer);
  523. }
  524. static int hdac_hdmi_trigger(struct snd_pcm_substream *substream, int cmd,
  525. struct snd_soc_dai *dai)
  526. {
  527. struct hdac_hdmi_dai_pin_map *dai_map;
  528. struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
  529. struct hdac_hdmi_priv *hdmi = hdac->private_data;
  530. int ret;
  531. dai_map = &hdmi->dai_map[dai->id];
  532. if (cmd == SNDRV_PCM_TRIGGER_RESUME) {
  533. ret = hdac_hdmi_enable_pin(hdac, dai_map);
  534. if (ret < 0)
  535. return ret;
  536. return hdac_hdmi_playback_prepare(substream, dai);
  537. }
  538. return 0;
  539. }
  540. static void hdac_hdmi_pcm_close(struct snd_pcm_substream *substream,
  541. struct snd_soc_dai *dai)
  542. {
  543. struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
  544. struct hdac_hdmi_priv *hdmi = hdac->private_data;
  545. struct hdac_hdmi_dai_pin_map *dai_map;
  546. dai_map = &hdmi->dai_map[dai->id];
  547. if (dai_map->pin) {
  548. snd_hdac_codec_write(&hdac->hdac, dai_map->cvt->nid, 0,
  549. AC_VERB_SET_CHANNEL_STREAMID, 0);
  550. snd_hdac_codec_write(&hdac->hdac, dai_map->cvt->nid, 0,
  551. AC_VERB_SET_STREAM_FORMAT, 0);
  552. hdac_hdmi_set_power_state(hdac, dai_map, AC_PWRST_D3);
  553. snd_hdac_codec_write(&hdac->hdac, dai_map->pin->nid, 0,
  554. AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
  555. mutex_lock(&dai_map->pin->lock);
  556. dai_map->pin->chmap_set = false;
  557. memset(dai_map->pin->chmap, 0, sizeof(dai_map->pin->chmap));
  558. dai_map->pin->channels = 0;
  559. mutex_unlock(&dai_map->pin->lock);
  560. dai_map->pin = NULL;
  561. }
  562. }
  563. static int
  564. hdac_hdmi_query_cvt_params(struct hdac_device *hdac, struct hdac_hdmi_cvt *cvt)
  565. {
  566. unsigned int chans;
  567. struct hdac_ext_device *edev = to_ehdac_device(hdac);
  568. struct hdac_hdmi_priv *hdmi = edev->private_data;
  569. int err;
  570. chans = get_wcaps(hdac, cvt->nid);
  571. chans = get_wcaps_channels(chans);
  572. cvt->params.channels_min = 2;
  573. cvt->params.channels_max = chans;
  574. if (chans > hdmi->chmap.channels_max)
  575. hdmi->chmap.channels_max = chans;
  576. err = snd_hdac_query_supported_pcm(hdac, cvt->nid,
  577. &cvt->params.rates,
  578. &cvt->params.formats,
  579. &cvt->params.maxbps);
  580. if (err < 0)
  581. dev_err(&hdac->dev,
  582. "Failed to query pcm params for nid %d: %d\n",
  583. cvt->nid, err);
  584. return err;
  585. }
  586. static int hdac_hdmi_fill_widget_info(struct device *dev,
  587. struct snd_soc_dapm_widget *w,
  588. enum snd_soc_dapm_type id, void *priv,
  589. const char *wname, const char *stream,
  590. struct snd_kcontrol_new *wc, int numkc)
  591. {
  592. w->id = id;
  593. w->name = devm_kstrdup(dev, wname, GFP_KERNEL);
  594. if (!w->name)
  595. return -ENOMEM;
  596. w->sname = stream;
  597. w->reg = SND_SOC_NOPM;
  598. w->shift = 0;
  599. w->kcontrol_news = wc;
  600. w->num_kcontrols = numkc;
  601. w->priv = priv;
  602. return 0;
  603. }
  604. static void hdac_hdmi_fill_route(struct snd_soc_dapm_route *route,
  605. const char *sink, const char *control, const char *src,
  606. int (*handler)(struct snd_soc_dapm_widget *src,
  607. struct snd_soc_dapm_widget *sink))
  608. {
  609. route->sink = sink;
  610. route->source = src;
  611. route->control = control;
  612. route->connected = handler;
  613. }
  614. static struct hdac_hdmi_pcm *hdac_hdmi_get_pcm(struct hdac_ext_device *edev,
  615. struct hdac_hdmi_pin *pin)
  616. {
  617. struct hdac_hdmi_priv *hdmi = edev->private_data;
  618. struct hdac_hdmi_pcm *pcm = NULL;
  619. list_for_each_entry(pcm, &hdmi->pcm_list, head) {
  620. if (pcm->pin == pin)
  621. return pcm;
  622. }
  623. return NULL;
  624. }
  625. /*
  626. * Based on user selection, map the PINs with the PCMs.
  627. */
  628. static int hdac_hdmi_set_pin_mux(struct snd_kcontrol *kcontrol,
  629. struct snd_ctl_elem_value *ucontrol)
  630. {
  631. int ret;
  632. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  633. struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
  634. struct snd_soc_dapm_context *dapm = w->dapm;
  635. struct hdac_hdmi_pin *pin = w->priv;
  636. struct hdac_ext_device *edev = to_hda_ext_device(dapm->dev);
  637. struct hdac_hdmi_priv *hdmi = edev->private_data;
  638. struct hdac_hdmi_pcm *pcm = NULL;
  639. const char *cvt_name = e->texts[ucontrol->value.enumerated.item[0]];
  640. ret = snd_soc_dapm_put_enum_double(kcontrol, ucontrol);
  641. if (ret < 0)
  642. return ret;
  643. mutex_lock(&hdmi->pin_mutex);
  644. list_for_each_entry(pcm, &hdmi->pcm_list, head) {
  645. if (pcm->pin == pin)
  646. pcm->pin = NULL;
  647. /*
  648. * Jack status is not reported during device probe as the
  649. * PCMs are not registered by then. So report it here.
  650. */
  651. if (!strcmp(cvt_name, pcm->cvt->name) && !pcm->pin) {
  652. pcm->pin = pin;
  653. if (pin->eld.monitor_present && pin->eld.eld_valid) {
  654. dev_dbg(&edev->hdac.dev,
  655. "jack report for pcm=%d\n",
  656. pcm->pcm_id);
  657. snd_jack_report(pcm->jack, SND_JACK_AVOUT);
  658. }
  659. mutex_unlock(&hdmi->pin_mutex);
  660. return ret;
  661. }
  662. }
  663. mutex_unlock(&hdmi->pin_mutex);
  664. return ret;
  665. }
  666. /*
  667. * Ideally the Mux inputs should be based on the num_muxs enumerated, but
  668. * the display driver seem to be programming the connection list for the pin
  669. * widget runtime.
  670. *
  671. * So programming all the possible inputs for the mux, the user has to take
  672. * care of selecting the right one and leaving all other inputs selected to
  673. * "NONE"
  674. */
  675. static int hdac_hdmi_create_pin_muxs(struct hdac_ext_device *edev,
  676. struct hdac_hdmi_pin *pin,
  677. struct snd_soc_dapm_widget *widget,
  678. const char *widget_name)
  679. {
  680. struct hdac_hdmi_priv *hdmi = edev->private_data;
  681. struct snd_kcontrol_new *kc;
  682. struct hdac_hdmi_cvt *cvt;
  683. struct soc_enum *se;
  684. char kc_name[NAME_SIZE];
  685. char mux_items[NAME_SIZE];
  686. /* To hold inputs to the Pin mux */
  687. char *items[HDA_MAX_CONNECTIONS];
  688. int i = 0;
  689. int num_items = hdmi->num_cvt + 1;
  690. kc = devm_kzalloc(&edev->hdac.dev, sizeof(*kc), GFP_KERNEL);
  691. if (!kc)
  692. return -ENOMEM;
  693. se = devm_kzalloc(&edev->hdac.dev, sizeof(*se), GFP_KERNEL);
  694. if (!se)
  695. return -ENOMEM;
  696. sprintf(kc_name, "Pin %d Input", pin->nid);
  697. kc->name = devm_kstrdup(&edev->hdac.dev, kc_name, GFP_KERNEL);
  698. if (!kc->name)
  699. return -ENOMEM;
  700. kc->private_value = (long)se;
  701. kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  702. kc->access = 0;
  703. kc->info = snd_soc_info_enum_double;
  704. kc->put = hdac_hdmi_set_pin_mux;
  705. kc->get = snd_soc_dapm_get_enum_double;
  706. se->reg = SND_SOC_NOPM;
  707. /* enum texts: ["NONE", "cvt #", "cvt #", ...] */
  708. se->items = num_items;
  709. se->mask = roundup_pow_of_two(se->items) - 1;
  710. sprintf(mux_items, "NONE");
  711. items[i] = devm_kstrdup(&edev->hdac.dev, mux_items, GFP_KERNEL);
  712. if (!items[i])
  713. return -ENOMEM;
  714. list_for_each_entry(cvt, &hdmi->cvt_list, head) {
  715. i++;
  716. sprintf(mux_items, "cvt %d", cvt->nid);
  717. items[i] = devm_kstrdup(&edev->hdac.dev, mux_items, GFP_KERNEL);
  718. if (!items[i])
  719. return -ENOMEM;
  720. }
  721. se->texts = devm_kmemdup(&edev->hdac.dev, items,
  722. (num_items * sizeof(char *)), GFP_KERNEL);
  723. if (!se->texts)
  724. return -ENOMEM;
  725. return hdac_hdmi_fill_widget_info(&edev->hdac.dev, widget,
  726. snd_soc_dapm_mux, pin, widget_name, NULL, kc, 1);
  727. }
  728. /* Add cvt <- input <- mux route map */
  729. static void hdac_hdmi_add_pinmux_cvt_route(struct hdac_ext_device *edev,
  730. struct snd_soc_dapm_widget *widgets,
  731. struct snd_soc_dapm_route *route, int rindex)
  732. {
  733. struct hdac_hdmi_priv *hdmi = edev->private_data;
  734. const struct snd_kcontrol_new *kc;
  735. struct soc_enum *se;
  736. int mux_index = hdmi->num_cvt + hdmi->num_pin;
  737. int i, j;
  738. for (i = 0; i < hdmi->num_pin; i++) {
  739. kc = widgets[mux_index].kcontrol_news;
  740. se = (struct soc_enum *)kc->private_value;
  741. for (j = 0; j < hdmi->num_cvt; j++) {
  742. hdac_hdmi_fill_route(&route[rindex],
  743. widgets[mux_index].name,
  744. se->texts[j + 1],
  745. widgets[j].name, NULL);
  746. rindex++;
  747. }
  748. mux_index++;
  749. }
  750. }
  751. /*
  752. * Widgets are added in the below sequence
  753. * Converter widgets for num converters enumerated
  754. * Pin widgets for num pins enumerated
  755. * Pin mux widgets to represent connenction list of pin widget
  756. *
  757. * Total widgets elements = num_cvt + num_pin + num_pin;
  758. *
  759. * Routes are added as below:
  760. * pin mux -> pin (based on num_pins)
  761. * cvt -> "Input sel control" -> pin_mux
  762. *
  763. * Total route elements:
  764. * num_pins + (pin_muxes * num_cvt)
  765. */
  766. static int create_fill_widget_route_map(struct snd_soc_dapm_context *dapm)
  767. {
  768. struct snd_soc_dapm_widget *widgets;
  769. struct snd_soc_dapm_route *route;
  770. struct hdac_ext_device *edev = to_hda_ext_device(dapm->dev);
  771. struct hdac_hdmi_priv *hdmi = edev->private_data;
  772. struct snd_soc_dai_driver *dai_drv = dapm->component->dai_drv;
  773. char widget_name[NAME_SIZE];
  774. struct hdac_hdmi_cvt *cvt;
  775. struct hdac_hdmi_pin *pin;
  776. int ret, i = 0, num_routes = 0;
  777. if (list_empty(&hdmi->cvt_list) || list_empty(&hdmi->pin_list))
  778. return -EINVAL;
  779. widgets = devm_kzalloc(dapm->dev,
  780. (sizeof(*widgets) * ((2 * hdmi->num_pin) + hdmi->num_cvt)),
  781. GFP_KERNEL);
  782. if (!widgets)
  783. return -ENOMEM;
  784. /* DAPM widgets to represent each converter widget */
  785. list_for_each_entry(cvt, &hdmi->cvt_list, head) {
  786. sprintf(widget_name, "Converter %d", cvt->nid);
  787. ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
  788. snd_soc_dapm_aif_in, &cvt->nid,
  789. widget_name, dai_drv[i].playback.stream_name, NULL, 0);
  790. if (ret < 0)
  791. return ret;
  792. i++;
  793. }
  794. list_for_each_entry(pin, &hdmi->pin_list, head) {
  795. sprintf(widget_name, "hif%d Output", pin->nid);
  796. ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
  797. snd_soc_dapm_output, &pin->nid,
  798. widget_name, NULL, NULL, 0);
  799. if (ret < 0)
  800. return ret;
  801. i++;
  802. }
  803. /* DAPM widgets to represent the connection list to pin widget */
  804. list_for_each_entry(pin, &hdmi->pin_list, head) {
  805. sprintf(widget_name, "Pin %d Mux", pin->nid);
  806. ret = hdac_hdmi_create_pin_muxs(edev, pin, &widgets[i],
  807. widget_name);
  808. if (ret < 0)
  809. return ret;
  810. i++;
  811. /* For cvt to pin_mux mapping */
  812. num_routes += hdmi->num_cvt;
  813. /* For pin_mux to pin mapping */
  814. num_routes++;
  815. }
  816. route = devm_kzalloc(dapm->dev, (sizeof(*route) * num_routes),
  817. GFP_KERNEL);
  818. if (!route)
  819. return -ENOMEM;
  820. i = 0;
  821. /* Add pin <- NULL <- mux route map */
  822. list_for_each_entry(pin, &hdmi->pin_list, head) {
  823. int sink_index = i + hdmi->num_cvt;
  824. int src_index = sink_index + hdmi->num_pin;
  825. hdac_hdmi_fill_route(&route[i],
  826. widgets[sink_index].name, NULL,
  827. widgets[src_index].name, NULL);
  828. i++;
  829. }
  830. hdac_hdmi_add_pinmux_cvt_route(edev, widgets, route, i);
  831. snd_soc_dapm_new_controls(dapm, widgets,
  832. ((2 * hdmi->num_pin) + hdmi->num_cvt));
  833. snd_soc_dapm_add_routes(dapm, route, num_routes);
  834. snd_soc_dapm_new_widgets(dapm->card);
  835. return 0;
  836. }
  837. static int hdac_hdmi_init_dai_map(struct hdac_ext_device *edev)
  838. {
  839. struct hdac_hdmi_priv *hdmi = edev->private_data;
  840. struct hdac_hdmi_dai_pin_map *dai_map;
  841. struct hdac_hdmi_cvt *cvt;
  842. int dai_id = 0;
  843. if (list_empty(&hdmi->cvt_list))
  844. return -EINVAL;
  845. list_for_each_entry(cvt, &hdmi->cvt_list, head) {
  846. dai_map = &hdmi->dai_map[dai_id];
  847. dai_map->dai_id = dai_id;
  848. dai_map->cvt = cvt;
  849. dai_id++;
  850. if (dai_id == HDA_MAX_CVTS) {
  851. dev_warn(&edev->hdac.dev,
  852. "Max dais supported: %d\n", dai_id);
  853. break;
  854. }
  855. }
  856. return 0;
  857. }
  858. static int hdac_hdmi_add_cvt(struct hdac_ext_device *edev, hda_nid_t nid)
  859. {
  860. struct hdac_hdmi_priv *hdmi = edev->private_data;
  861. struct hdac_hdmi_cvt *cvt;
  862. char name[NAME_SIZE];
  863. cvt = kzalloc(sizeof(*cvt), GFP_KERNEL);
  864. if (!cvt)
  865. return -ENOMEM;
  866. cvt->nid = nid;
  867. sprintf(name, "cvt %d", cvt->nid);
  868. cvt->name = kstrdup(name, GFP_KERNEL);
  869. list_add_tail(&cvt->head, &hdmi->cvt_list);
  870. hdmi->num_cvt++;
  871. return hdac_hdmi_query_cvt_params(&edev->hdac, cvt);
  872. }
  873. static void hdac_hdmi_parse_eld(struct hdac_ext_device *edev,
  874. struct hdac_hdmi_pin *pin)
  875. {
  876. pin->eld.info.spk_alloc = pin->eld.eld_buffer[DRM_ELD_SPEAKER];
  877. }
  878. static void hdac_hdmi_present_sense(struct hdac_hdmi_pin *pin, int repoll)
  879. {
  880. struct hdac_ext_device *edev = pin->edev;
  881. struct hdac_hdmi_priv *hdmi = edev->private_data;
  882. struct hdac_hdmi_pcm *pcm;
  883. int val;
  884. pin->repoll_count = repoll;
  885. pm_runtime_get_sync(&edev->hdac.dev);
  886. val = snd_hdac_codec_read(&edev->hdac, pin->nid, 0,
  887. AC_VERB_GET_PIN_SENSE, 0);
  888. dev_dbg(&edev->hdac.dev, "Pin sense val %x for pin: %d\n",
  889. val, pin->nid);
  890. mutex_lock(&hdmi->pin_mutex);
  891. pin->eld.monitor_present = !!(val & AC_PINSENSE_PRESENCE);
  892. pin->eld.eld_valid = !!(val & AC_PINSENSE_ELDV);
  893. pcm = hdac_hdmi_get_pcm(edev, pin);
  894. if (!pin->eld.monitor_present || !pin->eld.eld_valid) {
  895. dev_dbg(&edev->hdac.dev, "%s: disconnect for pin %d\n",
  896. __func__, pin->nid);
  897. /*
  898. * PCMs are not registered during device probe, so don't
  899. * report jack here. It will be done in usermode mux
  900. * control select.
  901. */
  902. if (pcm) {
  903. dev_dbg(&edev->hdac.dev,
  904. "jack report for pcm=%d\n", pcm->pcm_id);
  905. snd_jack_report(pcm->jack, 0);
  906. }
  907. mutex_unlock(&hdmi->pin_mutex);
  908. goto put_hdac_device;
  909. }
  910. if (pin->eld.monitor_present && pin->eld.eld_valid) {
  911. /* TODO: use i915 component for reading ELD later */
  912. if (hdac_hdmi_get_eld(&edev->hdac, pin->nid,
  913. pin->eld.eld_buffer,
  914. &pin->eld.eld_size) == 0) {
  915. if (pcm) {
  916. dev_dbg(&edev->hdac.dev,
  917. "jack report for pcm=%d\n",
  918. pcm->pcm_id);
  919. snd_jack_report(pcm->jack, SND_JACK_AVOUT);
  920. }
  921. hdac_hdmi_parse_eld(edev, pin);
  922. print_hex_dump_debug("ELD: ",
  923. DUMP_PREFIX_OFFSET, 16, 1,
  924. pin->eld.eld_buffer, pin->eld.eld_size,
  925. true);
  926. } else {
  927. pin->eld.monitor_present = false;
  928. pin->eld.eld_valid = false;
  929. if (pcm) {
  930. dev_dbg(&edev->hdac.dev,
  931. "jack report for pcm=%d\n",
  932. pcm->pcm_id);
  933. snd_jack_report(pcm->jack, 0);
  934. }
  935. }
  936. }
  937. mutex_unlock(&hdmi->pin_mutex);
  938. /*
  939. * Sometimes the pin_sense may present invalid monitor
  940. * present and eld_valid. If ELD data is not valid, loop few
  941. * more times to get correct pin sense and valid ELD.
  942. */
  943. if ((!pin->eld.monitor_present || !pin->eld.eld_valid) && repoll)
  944. schedule_delayed_work(&pin->work, msecs_to_jiffies(300));
  945. put_hdac_device:
  946. pm_runtime_put_sync(&edev->hdac.dev);
  947. }
  948. static void hdac_hdmi_repoll_eld(struct work_struct *work)
  949. {
  950. struct hdac_hdmi_pin *pin =
  951. container_of(to_delayed_work(work), struct hdac_hdmi_pin, work);
  952. /* picked from legacy HDA driver */
  953. if (pin->repoll_count++ > 6)
  954. pin->repoll_count = 0;
  955. hdac_hdmi_present_sense(pin, pin->repoll_count);
  956. }
  957. static int hdac_hdmi_add_pin(struct hdac_ext_device *edev, hda_nid_t nid)
  958. {
  959. struct hdac_hdmi_priv *hdmi = edev->private_data;
  960. struct hdac_hdmi_pin *pin;
  961. pin = kzalloc(sizeof(*pin), GFP_KERNEL);
  962. if (!pin)
  963. return -ENOMEM;
  964. pin->nid = nid;
  965. list_add_tail(&pin->head, &hdmi->pin_list);
  966. hdmi->num_pin++;
  967. pin->edev = edev;
  968. mutex_init(&pin->lock);
  969. INIT_DELAYED_WORK(&pin->work, hdac_hdmi_repoll_eld);
  970. return 0;
  971. }
  972. #define INTEL_VENDOR_NID 0x08
  973. #define INTEL_GET_VENDOR_VERB 0xf81
  974. #define INTEL_SET_VENDOR_VERB 0x781
  975. #define INTEL_EN_DP12 0x02 /* enable DP 1.2 features */
  976. #define INTEL_EN_ALL_PIN_CVTS 0x01 /* enable 2nd & 3rd pins and convertors */
  977. static void hdac_hdmi_skl_enable_all_pins(struct hdac_device *hdac)
  978. {
  979. unsigned int vendor_param;
  980. vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
  981. INTEL_GET_VENDOR_VERB, 0);
  982. if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
  983. return;
  984. vendor_param |= INTEL_EN_ALL_PIN_CVTS;
  985. vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
  986. INTEL_SET_VENDOR_VERB, vendor_param);
  987. if (vendor_param == -1)
  988. return;
  989. }
  990. static void hdac_hdmi_skl_enable_dp12(struct hdac_device *hdac)
  991. {
  992. unsigned int vendor_param;
  993. vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
  994. INTEL_GET_VENDOR_VERB, 0);
  995. if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
  996. return;
  997. /* enable DP1.2 mode */
  998. vendor_param |= INTEL_EN_DP12;
  999. vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
  1000. INTEL_SET_VENDOR_VERB, vendor_param);
  1001. if (vendor_param == -1)
  1002. return;
  1003. }
  1004. static struct snd_soc_dai_ops hdmi_dai_ops = {
  1005. .startup = hdac_hdmi_pcm_open,
  1006. .shutdown = hdac_hdmi_pcm_close,
  1007. .hw_params = hdac_hdmi_set_hw_params,
  1008. .prepare = hdac_hdmi_playback_prepare,
  1009. .trigger = hdac_hdmi_trigger,
  1010. .hw_free = hdac_hdmi_playback_cleanup,
  1011. };
  1012. /*
  1013. * Each converter can support a stream independently. So a dai is created
  1014. * based on the number of converter queried.
  1015. */
  1016. static int hdac_hdmi_create_dais(struct hdac_device *hdac,
  1017. struct snd_soc_dai_driver **dais,
  1018. struct hdac_hdmi_priv *hdmi, int num_dais)
  1019. {
  1020. struct snd_soc_dai_driver *hdmi_dais;
  1021. struct hdac_hdmi_cvt *cvt;
  1022. char name[NAME_SIZE], dai_name[NAME_SIZE];
  1023. int i = 0;
  1024. u32 rates, bps;
  1025. unsigned int rate_max = 384000, rate_min = 8000;
  1026. u64 formats;
  1027. int ret;
  1028. hdmi_dais = devm_kzalloc(&hdac->dev,
  1029. (sizeof(*hdmi_dais) * num_dais),
  1030. GFP_KERNEL);
  1031. if (!hdmi_dais)
  1032. return -ENOMEM;
  1033. list_for_each_entry(cvt, &hdmi->cvt_list, head) {
  1034. ret = snd_hdac_query_supported_pcm(hdac, cvt->nid,
  1035. &rates, &formats, &bps);
  1036. if (ret)
  1037. return ret;
  1038. sprintf(dai_name, "intel-hdmi-hifi%d", i+1);
  1039. hdmi_dais[i].name = devm_kstrdup(&hdac->dev,
  1040. dai_name, GFP_KERNEL);
  1041. if (!hdmi_dais[i].name)
  1042. return -ENOMEM;
  1043. snprintf(name, sizeof(name), "hifi%d", i+1);
  1044. hdmi_dais[i].playback.stream_name =
  1045. devm_kstrdup(&hdac->dev, name, GFP_KERNEL);
  1046. if (!hdmi_dais[i].playback.stream_name)
  1047. return -ENOMEM;
  1048. /*
  1049. * Set caps based on capability queried from the converter.
  1050. * It will be constrained runtime based on ELD queried.
  1051. */
  1052. hdmi_dais[i].playback.formats = formats;
  1053. hdmi_dais[i].playback.rates = rates;
  1054. hdmi_dais[i].playback.rate_max = rate_max;
  1055. hdmi_dais[i].playback.rate_min = rate_min;
  1056. hdmi_dais[i].playback.channels_min = 2;
  1057. hdmi_dais[i].playback.channels_max = 2;
  1058. hdmi_dais[i].ops = &hdmi_dai_ops;
  1059. i++;
  1060. }
  1061. *dais = hdmi_dais;
  1062. return 0;
  1063. }
  1064. /*
  1065. * Parse all nodes and store the cvt/pin nids in array
  1066. * Add one time initialization for pin and cvt widgets
  1067. */
  1068. static int hdac_hdmi_parse_and_map_nid(struct hdac_ext_device *edev,
  1069. struct snd_soc_dai_driver **dais, int *num_dais)
  1070. {
  1071. hda_nid_t nid;
  1072. int i, num_nodes;
  1073. struct hdac_device *hdac = &edev->hdac;
  1074. struct hdac_hdmi_priv *hdmi = edev->private_data;
  1075. int ret;
  1076. hdac_hdmi_skl_enable_all_pins(hdac);
  1077. hdac_hdmi_skl_enable_dp12(hdac);
  1078. num_nodes = snd_hdac_get_sub_nodes(hdac, hdac->afg, &nid);
  1079. if (!nid || num_nodes <= 0) {
  1080. dev_warn(&hdac->dev, "HDMI: failed to get afg sub nodes\n");
  1081. return -EINVAL;
  1082. }
  1083. hdac->num_nodes = num_nodes;
  1084. hdac->start_nid = nid;
  1085. for (i = 0; i < hdac->num_nodes; i++, nid++) {
  1086. unsigned int caps;
  1087. unsigned int type;
  1088. caps = get_wcaps(hdac, nid);
  1089. type = get_wcaps_type(caps);
  1090. if (!(caps & AC_WCAP_DIGITAL))
  1091. continue;
  1092. switch (type) {
  1093. case AC_WID_AUD_OUT:
  1094. ret = hdac_hdmi_add_cvt(edev, nid);
  1095. if (ret < 0)
  1096. return ret;
  1097. break;
  1098. case AC_WID_PIN:
  1099. ret = hdac_hdmi_add_pin(edev, nid);
  1100. if (ret < 0)
  1101. return ret;
  1102. break;
  1103. }
  1104. }
  1105. hdac->end_nid = nid;
  1106. if (!hdmi->num_pin || !hdmi->num_cvt)
  1107. return -EIO;
  1108. ret = hdac_hdmi_create_dais(hdac, dais, hdmi, hdmi->num_cvt);
  1109. if (ret) {
  1110. dev_err(&hdac->dev, "Failed to create dais with err: %d\n",
  1111. ret);
  1112. return ret;
  1113. }
  1114. *num_dais = hdmi->num_cvt;
  1115. return hdac_hdmi_init_dai_map(edev);
  1116. }
  1117. static void hdac_hdmi_eld_notify_cb(void *aptr, int port)
  1118. {
  1119. struct hdac_ext_device *edev = aptr;
  1120. struct hdac_hdmi_priv *hdmi = edev->private_data;
  1121. struct hdac_hdmi_pin *pin;
  1122. struct snd_soc_codec *codec = edev->scodec;
  1123. /* Don't know how this mapping is derived */
  1124. hda_nid_t pin_nid = port + 0x04;
  1125. dev_dbg(&edev->hdac.dev, "%s: for pin: %d\n", __func__, pin_nid);
  1126. /*
  1127. * skip notification during system suspend (but not in runtime PM);
  1128. * the state will be updated at resume. Also since the ELD and
  1129. * connection states are updated in anyway at the end of the resume,
  1130. * we can skip it when received during PM process.
  1131. */
  1132. if (snd_power_get_state(codec->component.card->snd_card) !=
  1133. SNDRV_CTL_POWER_D0)
  1134. return;
  1135. if (atomic_read(&edev->hdac.in_pm))
  1136. return;
  1137. list_for_each_entry(pin, &hdmi->pin_list, head) {
  1138. if (pin->nid == pin_nid)
  1139. hdac_hdmi_present_sense(pin, 1);
  1140. }
  1141. }
  1142. static struct i915_audio_component_audio_ops aops = {
  1143. .pin_eld_notify = hdac_hdmi_eld_notify_cb,
  1144. };
  1145. static struct snd_pcm *hdac_hdmi_get_pcm_from_id(struct snd_soc_card *card,
  1146. int device)
  1147. {
  1148. struct snd_soc_pcm_runtime *rtd;
  1149. list_for_each_entry(rtd, &card->rtd_list, list) {
  1150. if (rtd->pcm && (rtd->pcm->device == device))
  1151. return rtd->pcm;
  1152. }
  1153. return NULL;
  1154. }
  1155. int hdac_hdmi_jack_init(struct snd_soc_dai *dai, int device)
  1156. {
  1157. char jack_name[NAME_SIZE];
  1158. struct snd_soc_codec *codec = dai->codec;
  1159. struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
  1160. struct snd_soc_dapm_context *dapm =
  1161. snd_soc_component_get_dapm(&codec->component);
  1162. struct hdac_hdmi_priv *hdmi = edev->private_data;
  1163. struct hdac_hdmi_pcm *pcm;
  1164. struct snd_pcm *snd_pcm;
  1165. int err;
  1166. /*
  1167. * this is a new PCM device, create new pcm and
  1168. * add to the pcm list
  1169. */
  1170. pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
  1171. if (!pcm)
  1172. return -ENOMEM;
  1173. pcm->pcm_id = device;
  1174. pcm->cvt = hdmi->dai_map[dai->id].cvt;
  1175. snd_pcm = hdac_hdmi_get_pcm_from_id(dai->component->card, device);
  1176. if (snd_pcm) {
  1177. err = snd_hdac_add_chmap_ctls(snd_pcm, device, &hdmi->chmap);
  1178. if (err < 0) {
  1179. dev_err(&edev->hdac.dev,
  1180. "chmap control add failed with err: %d for pcm: %d\n",
  1181. err, device);
  1182. kfree(pcm);
  1183. return err;
  1184. }
  1185. }
  1186. list_add_tail(&pcm->head, &hdmi->pcm_list);
  1187. sprintf(jack_name, "HDMI/DP, pcm=%d Jack", device);
  1188. return snd_jack_new(dapm->card->snd_card, jack_name,
  1189. SND_JACK_AVOUT, &pcm->jack, true, false);
  1190. }
  1191. EXPORT_SYMBOL_GPL(hdac_hdmi_jack_init);
  1192. static int hdmi_codec_probe(struct snd_soc_codec *codec)
  1193. {
  1194. struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
  1195. struct hdac_hdmi_priv *hdmi = edev->private_data;
  1196. struct snd_soc_dapm_context *dapm =
  1197. snd_soc_component_get_dapm(&codec->component);
  1198. struct hdac_hdmi_pin *pin;
  1199. struct hdac_ext_link *hlink = NULL;
  1200. int ret;
  1201. edev->scodec = codec;
  1202. /*
  1203. * hold the ref while we probe, also no need to drop the ref on
  1204. * exit, we call pm_runtime_suspend() so that will do for us
  1205. */
  1206. hlink = snd_hdac_ext_bus_get_link(edev->ebus, dev_name(&edev->hdac.dev));
  1207. if (!hlink) {
  1208. dev_err(&edev->hdac.dev, "hdac link not found\n");
  1209. return -EIO;
  1210. }
  1211. snd_hdac_ext_bus_link_get(edev->ebus, hlink);
  1212. ret = create_fill_widget_route_map(dapm);
  1213. if (ret < 0)
  1214. return ret;
  1215. aops.audio_ptr = edev;
  1216. ret = snd_hdac_i915_register_notifier(&aops);
  1217. if (ret < 0) {
  1218. dev_err(&edev->hdac.dev, "notifier register failed: err: %d\n",
  1219. ret);
  1220. return ret;
  1221. }
  1222. list_for_each_entry(pin, &hdmi->pin_list, head)
  1223. hdac_hdmi_present_sense(pin, 1);
  1224. /* Imp: Store the card pointer in hda_codec */
  1225. edev->card = dapm->card->snd_card;
  1226. /*
  1227. * hdac_device core already sets the state to active and calls
  1228. * get_noresume. So enable runtime and set the device to suspend.
  1229. */
  1230. pm_runtime_enable(&edev->hdac.dev);
  1231. pm_runtime_put(&edev->hdac.dev);
  1232. pm_runtime_suspend(&edev->hdac.dev);
  1233. return 0;
  1234. }
  1235. static int hdmi_codec_remove(struct snd_soc_codec *codec)
  1236. {
  1237. struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
  1238. pm_runtime_disable(&edev->hdac.dev);
  1239. return 0;
  1240. }
  1241. #ifdef CONFIG_PM
  1242. static int hdmi_codec_prepare(struct device *dev)
  1243. {
  1244. struct hdac_ext_device *edev = to_hda_ext_device(dev);
  1245. struct hdac_device *hdac = &edev->hdac;
  1246. pm_runtime_get_sync(&edev->hdac.dev);
  1247. /*
  1248. * Power down afg.
  1249. * codec_read is preferred over codec_write to set the power state.
  1250. * This way verb is send to set the power state and response
  1251. * is received. So setting power state is ensured without using loop
  1252. * to read the state.
  1253. */
  1254. snd_hdac_codec_read(hdac, hdac->afg, 0, AC_VERB_SET_POWER_STATE,
  1255. AC_PWRST_D3);
  1256. return 0;
  1257. }
  1258. static void hdmi_codec_complete(struct device *dev)
  1259. {
  1260. struct hdac_ext_device *edev = to_hda_ext_device(dev);
  1261. struct hdac_hdmi_priv *hdmi = edev->private_data;
  1262. struct hdac_hdmi_pin *pin;
  1263. struct hdac_device *hdac = &edev->hdac;
  1264. /* Power up afg */
  1265. snd_hdac_codec_read(hdac, hdac->afg, 0, AC_VERB_SET_POWER_STATE,
  1266. AC_PWRST_D0);
  1267. hdac_hdmi_skl_enable_all_pins(&edev->hdac);
  1268. hdac_hdmi_skl_enable_dp12(&edev->hdac);
  1269. /*
  1270. * As the ELD notify callback request is not entertained while the
  1271. * device is in suspend state. Need to manually check detection of
  1272. * all pins here.
  1273. */
  1274. list_for_each_entry(pin, &hdmi->pin_list, head)
  1275. hdac_hdmi_present_sense(pin, 1);
  1276. pm_runtime_put_sync(&edev->hdac.dev);
  1277. }
  1278. #else
  1279. #define hdmi_codec_prepare NULL
  1280. #define hdmi_codec_complete NULL
  1281. #endif
  1282. static struct snd_soc_codec_driver hdmi_hda_codec = {
  1283. .probe = hdmi_codec_probe,
  1284. .remove = hdmi_codec_remove,
  1285. .idle_bias_off = true,
  1286. };
  1287. static void hdac_hdmi_get_chmap(struct hdac_device *hdac, int pcm_idx,
  1288. unsigned char *chmap)
  1289. {
  1290. struct hdac_ext_device *edev = to_ehdac_device(hdac);
  1291. struct hdac_hdmi_priv *hdmi = edev->private_data;
  1292. struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
  1293. struct hdac_hdmi_pin *pin = pcm->pin;
  1294. /* chmap is already set to 0 in caller */
  1295. if (!pin)
  1296. return;
  1297. memcpy(chmap, pin->chmap, ARRAY_SIZE(pin->chmap));
  1298. }
  1299. static void hdac_hdmi_set_chmap(struct hdac_device *hdac, int pcm_idx,
  1300. unsigned char *chmap, int prepared)
  1301. {
  1302. struct hdac_ext_device *edev = to_ehdac_device(hdac);
  1303. struct hdac_hdmi_priv *hdmi = edev->private_data;
  1304. struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
  1305. struct hdac_hdmi_pin *pin = pcm->pin;
  1306. mutex_lock(&pin->lock);
  1307. pin->chmap_set = true;
  1308. memcpy(pin->chmap, chmap, ARRAY_SIZE(pin->chmap));
  1309. if (prepared)
  1310. hdac_hdmi_setup_audio_infoframe(edev, pcm->cvt->nid, pin->nid);
  1311. mutex_unlock(&pin->lock);
  1312. }
  1313. static bool is_hdac_hdmi_pcm_attached(struct hdac_device *hdac, int pcm_idx)
  1314. {
  1315. struct hdac_ext_device *edev = to_ehdac_device(hdac);
  1316. struct hdac_hdmi_priv *hdmi = edev->private_data;
  1317. struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
  1318. struct hdac_hdmi_pin *pin = pcm->pin;
  1319. return pin ? true:false;
  1320. }
  1321. static int hdac_hdmi_get_spk_alloc(struct hdac_device *hdac, int pcm_idx)
  1322. {
  1323. struct hdac_ext_device *edev = to_ehdac_device(hdac);
  1324. struct hdac_hdmi_priv *hdmi = edev->private_data;
  1325. struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
  1326. struct hdac_hdmi_pin *pin = pcm->pin;
  1327. if (!pin || !pin->eld.eld_valid)
  1328. return 0;
  1329. return pin->eld.info.spk_alloc;
  1330. }
  1331. static int hdac_hdmi_dev_probe(struct hdac_ext_device *edev)
  1332. {
  1333. struct hdac_device *codec = &edev->hdac;
  1334. struct hdac_hdmi_priv *hdmi_priv;
  1335. struct snd_soc_dai_driver *hdmi_dais = NULL;
  1336. struct hdac_ext_link *hlink = NULL;
  1337. int num_dais = 0;
  1338. int ret = 0;
  1339. /* hold the ref while we probe */
  1340. hlink = snd_hdac_ext_bus_get_link(edev->ebus, dev_name(&edev->hdac.dev));
  1341. if (!hlink) {
  1342. dev_err(&edev->hdac.dev, "hdac link not found\n");
  1343. return -EIO;
  1344. }
  1345. snd_hdac_ext_bus_link_get(edev->ebus, hlink);
  1346. hdmi_priv = devm_kzalloc(&codec->dev, sizeof(*hdmi_priv), GFP_KERNEL);
  1347. if (hdmi_priv == NULL)
  1348. return -ENOMEM;
  1349. edev->private_data = hdmi_priv;
  1350. snd_hdac_register_chmap_ops(codec, &hdmi_priv->chmap);
  1351. hdmi_priv->chmap.ops.get_chmap = hdac_hdmi_get_chmap;
  1352. hdmi_priv->chmap.ops.set_chmap = hdac_hdmi_set_chmap;
  1353. hdmi_priv->chmap.ops.is_pcm_attached = is_hdac_hdmi_pcm_attached;
  1354. hdmi_priv->chmap.ops.get_spk_alloc = hdac_hdmi_get_spk_alloc;
  1355. dev_set_drvdata(&codec->dev, edev);
  1356. INIT_LIST_HEAD(&hdmi_priv->pin_list);
  1357. INIT_LIST_HEAD(&hdmi_priv->cvt_list);
  1358. INIT_LIST_HEAD(&hdmi_priv->pcm_list);
  1359. mutex_init(&hdmi_priv->pin_mutex);
  1360. /*
  1361. * Turned off in the runtime_suspend during the first explicit
  1362. * pm_runtime_suspend call.
  1363. */
  1364. ret = snd_hdac_display_power(edev->hdac.bus, true);
  1365. if (ret < 0) {
  1366. dev_err(&edev->hdac.dev,
  1367. "Cannot turn on display power on i915 err: %d\n",
  1368. ret);
  1369. return ret;
  1370. }
  1371. ret = hdac_hdmi_parse_and_map_nid(edev, &hdmi_dais, &num_dais);
  1372. if (ret < 0) {
  1373. dev_err(&codec->dev,
  1374. "Failed in parse and map nid with err: %d\n", ret);
  1375. return ret;
  1376. }
  1377. /* ASoC specific initialization */
  1378. ret = snd_soc_register_codec(&codec->dev, &hdmi_hda_codec,
  1379. hdmi_dais, num_dais);
  1380. snd_hdac_ext_bus_link_put(edev->ebus, hlink);
  1381. return ret;
  1382. }
  1383. static int hdac_hdmi_dev_remove(struct hdac_ext_device *edev)
  1384. {
  1385. struct hdac_hdmi_priv *hdmi = edev->private_data;
  1386. struct hdac_hdmi_pin *pin, *pin_next;
  1387. struct hdac_hdmi_cvt *cvt, *cvt_next;
  1388. struct hdac_hdmi_pcm *pcm, *pcm_next;
  1389. snd_soc_unregister_codec(&edev->hdac.dev);
  1390. list_for_each_entry_safe(pcm, pcm_next, &hdmi->pcm_list, head) {
  1391. pcm->cvt = NULL;
  1392. pcm->pin = NULL;
  1393. list_del(&pcm->head);
  1394. kfree(pcm);
  1395. }
  1396. list_for_each_entry_safe(cvt, cvt_next, &hdmi->cvt_list, head) {
  1397. list_del(&cvt->head);
  1398. kfree(cvt->name);
  1399. kfree(cvt);
  1400. }
  1401. list_for_each_entry_safe(pin, pin_next, &hdmi->pin_list, head) {
  1402. list_del(&pin->head);
  1403. kfree(pin);
  1404. }
  1405. return 0;
  1406. }
  1407. #ifdef CONFIG_PM
  1408. static int hdac_hdmi_runtime_suspend(struct device *dev)
  1409. {
  1410. struct hdac_ext_device *edev = to_hda_ext_device(dev);
  1411. struct hdac_device *hdac = &edev->hdac;
  1412. struct hdac_bus *bus = hdac->bus;
  1413. struct hdac_ext_bus *ebus = hbus_to_ebus(bus);
  1414. struct hdac_ext_link *hlink = NULL;
  1415. int err;
  1416. dev_dbg(dev, "Enter: %s\n", __func__);
  1417. /* controller may not have been initialized for the first time */
  1418. if (!bus)
  1419. return 0;
  1420. /*
  1421. * Power down afg.
  1422. * codec_read is preferred over codec_write to set the power state.
  1423. * This way verb is send to set the power state and response
  1424. * is received. So setting power state is ensured without using loop
  1425. * to read the state.
  1426. */
  1427. snd_hdac_codec_read(hdac, hdac->afg, 0, AC_VERB_SET_POWER_STATE,
  1428. AC_PWRST_D3);
  1429. err = snd_hdac_display_power(bus, false);
  1430. if (err < 0) {
  1431. dev_err(bus->dev, "Cannot turn on display power on i915\n");
  1432. return err;
  1433. }
  1434. hlink = snd_hdac_ext_bus_get_link(ebus, dev_name(dev));
  1435. if (!hlink) {
  1436. dev_err(dev, "hdac link not found\n");
  1437. return -EIO;
  1438. }
  1439. snd_hdac_ext_bus_link_put(ebus, hlink);
  1440. return 0;
  1441. }
  1442. static int hdac_hdmi_runtime_resume(struct device *dev)
  1443. {
  1444. struct hdac_ext_device *edev = to_hda_ext_device(dev);
  1445. struct hdac_device *hdac = &edev->hdac;
  1446. struct hdac_bus *bus = hdac->bus;
  1447. struct hdac_ext_bus *ebus = hbus_to_ebus(bus);
  1448. struct hdac_ext_link *hlink = NULL;
  1449. int err;
  1450. dev_dbg(dev, "Enter: %s\n", __func__);
  1451. /* controller may not have been initialized for the first time */
  1452. if (!bus)
  1453. return 0;
  1454. hlink = snd_hdac_ext_bus_get_link(ebus, dev_name(dev));
  1455. if (!hlink) {
  1456. dev_err(dev, "hdac link not found\n");
  1457. return -EIO;
  1458. }
  1459. snd_hdac_ext_bus_link_get(ebus, hlink);
  1460. err = snd_hdac_display_power(bus, true);
  1461. if (err < 0) {
  1462. dev_err(bus->dev, "Cannot turn on display power on i915\n");
  1463. return err;
  1464. }
  1465. hdac_hdmi_skl_enable_all_pins(&edev->hdac);
  1466. hdac_hdmi_skl_enable_dp12(&edev->hdac);
  1467. /* Power up afg */
  1468. snd_hdac_codec_read(hdac, hdac->afg, 0, AC_VERB_SET_POWER_STATE,
  1469. AC_PWRST_D0);
  1470. return 0;
  1471. }
  1472. #else
  1473. #define hdac_hdmi_runtime_suspend NULL
  1474. #define hdac_hdmi_runtime_resume NULL
  1475. #endif
  1476. static const struct dev_pm_ops hdac_hdmi_pm = {
  1477. SET_RUNTIME_PM_OPS(hdac_hdmi_runtime_suspend, hdac_hdmi_runtime_resume, NULL)
  1478. .prepare = hdmi_codec_prepare,
  1479. .complete = hdmi_codec_complete,
  1480. };
  1481. static const struct hda_device_id hdmi_list[] = {
  1482. HDA_CODEC_EXT_ENTRY(0x80862809, 0x100000, "Skylake HDMI", 0),
  1483. HDA_CODEC_EXT_ENTRY(0x8086280a, 0x100000, "Broxton HDMI", 0),
  1484. HDA_CODEC_EXT_ENTRY(0x8086280b, 0x100000, "Kabylake HDMI", 0),
  1485. {}
  1486. };
  1487. MODULE_DEVICE_TABLE(hdaudio, hdmi_list);
  1488. static struct hdac_ext_driver hdmi_driver = {
  1489. . hdac = {
  1490. .driver = {
  1491. .name = "HDMI HDA Codec",
  1492. .pm = &hdac_hdmi_pm,
  1493. },
  1494. .id_table = hdmi_list,
  1495. },
  1496. .probe = hdac_hdmi_dev_probe,
  1497. .remove = hdac_hdmi_dev_remove,
  1498. };
  1499. static int __init hdmi_init(void)
  1500. {
  1501. return snd_hda_ext_driver_register(&hdmi_driver);
  1502. }
  1503. static void __exit hdmi_exit(void)
  1504. {
  1505. snd_hda_ext_driver_unregister(&hdmi_driver);
  1506. }
  1507. module_init(hdmi_init);
  1508. module_exit(hdmi_exit);
  1509. MODULE_LICENSE("GPL v2");
  1510. MODULE_DESCRIPTION("HDMI HD codec");
  1511. MODULE_AUTHOR("Samreen Nilofer<samreen.nilofer@intel.com>");
  1512. MODULE_AUTHOR("Subhransu S. Prusty<subhransu.s.prusty@intel.com>");