PageRenderTime 171ms CodeModel.GetById 18ms RepoModel.GetById 2ms app.codeStats 0ms

/sound/soc/codecs/hdac_hdmi.c

http://github.com/mirrors/linux-2.6
C | 2207 lines | 1602 code | 394 blank | 211 comment | 218 complexity | 209516e4b93a7df94b3d54e0a9e5183a MD5 | raw file
Possible License(s): LGPL-2.0, AGPL-1.0, GPL-2.0

Large files files are truncated, but you can click here to view the full file

  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * hdac_hdmi.c - ASoc HDA-HDMI codec driver for Intel platforms
  4. *
  5. * Copyright (C) 2014-2015 Intel Corp
  6. * Author: Samreen Nilofer <samreen.nilofer@intel.com>
  7. * Subhransu S. Prusty <subhransu.s.prusty@intel.com>
  8. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  9. *
  10. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  11. */
  12. #include <linux/init.h>
  13. #include <linux/delay.h>
  14. #include <linux/module.h>
  15. #include <linux/pm_runtime.h>
  16. #include <linux/hdmi.h>
  17. #include <drm/drm_edid.h>
  18. #include <sound/pcm_params.h>
  19. #include <sound/jack.h>
  20. #include <sound/soc.h>
  21. #include <sound/hdaudio_ext.h>
  22. #include <sound/hda_i915.h>
  23. #include <sound/pcm_drm_eld.h>
  24. #include <sound/hda_chmap.h>
  25. #include "../../hda/local.h"
  26. #include "hdac_hdmi.h"
  27. #define NAME_SIZE 32
  28. #define AMP_OUT_MUTE 0xb080
  29. #define AMP_OUT_UNMUTE 0xb000
  30. #define PIN_OUT (AC_PINCTL_OUT_EN)
  31. #define HDA_MAX_CONNECTIONS 32
  32. #define HDA_MAX_CVTS 3
  33. #define HDA_MAX_PORTS 3
  34. #define ELD_MAX_SIZE 256
  35. #define ELD_FIXED_BYTES 20
  36. #define ELD_VER_CEA_861D 2
  37. #define ELD_VER_PARTIAL 31
  38. #define ELD_MAX_MNL 16
  39. struct hdac_hdmi_cvt_params {
  40. unsigned int channels_min;
  41. unsigned int channels_max;
  42. u32 rates;
  43. u64 formats;
  44. unsigned int maxbps;
  45. };
  46. struct hdac_hdmi_cvt {
  47. struct list_head head;
  48. hda_nid_t nid;
  49. const char *name;
  50. struct hdac_hdmi_cvt_params params;
  51. };
  52. /* Currently only spk_alloc, more to be added */
  53. struct hdac_hdmi_parsed_eld {
  54. u8 spk_alloc;
  55. };
  56. struct hdac_hdmi_eld {
  57. bool monitor_present;
  58. bool eld_valid;
  59. int eld_size;
  60. char eld_buffer[ELD_MAX_SIZE];
  61. struct hdac_hdmi_parsed_eld info;
  62. };
  63. struct hdac_hdmi_pin {
  64. struct list_head head;
  65. hda_nid_t nid;
  66. bool mst_capable;
  67. struct hdac_hdmi_port *ports;
  68. int num_ports;
  69. struct hdac_device *hdev;
  70. };
  71. struct hdac_hdmi_port {
  72. struct list_head head;
  73. int id;
  74. struct hdac_hdmi_pin *pin;
  75. int num_mux_nids;
  76. hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
  77. struct hdac_hdmi_eld eld;
  78. const char *jack_pin;
  79. bool is_connect;
  80. struct snd_soc_dapm_context *dapm;
  81. const char *output_pin;
  82. struct work_struct dapm_work;
  83. };
  84. struct hdac_hdmi_pcm {
  85. struct list_head head;
  86. int pcm_id;
  87. struct list_head port_list;
  88. struct hdac_hdmi_cvt *cvt;
  89. struct snd_soc_jack *jack;
  90. int stream_tag;
  91. int channels;
  92. int format;
  93. bool chmap_set;
  94. unsigned char chmap[8]; /* ALSA API channel-map */
  95. struct mutex lock;
  96. int jack_event;
  97. };
  98. struct hdac_hdmi_dai_port_map {
  99. int dai_id;
  100. struct hdac_hdmi_port *port;
  101. struct hdac_hdmi_cvt *cvt;
  102. };
  103. struct hdac_hdmi_drv_data {
  104. unsigned int vendor_nid;
  105. };
  106. struct hdac_hdmi_priv {
  107. struct hdac_device *hdev;
  108. struct snd_soc_component *component;
  109. struct snd_card *card;
  110. struct hdac_hdmi_dai_port_map dai_map[HDA_MAX_CVTS];
  111. struct list_head pin_list;
  112. struct list_head cvt_list;
  113. struct list_head pcm_list;
  114. int num_pin;
  115. int num_cvt;
  116. int num_ports;
  117. struct mutex pin_mutex;
  118. struct hdac_chmap chmap;
  119. struct hdac_hdmi_drv_data *drv_data;
  120. struct snd_soc_dai_driver *dai_drv;
  121. };
  122. #define hdev_to_hdmi_priv(_hdev) dev_get_drvdata(&(_hdev)->dev)
  123. static struct hdac_hdmi_pcm *
  124. hdac_hdmi_get_pcm_from_cvt(struct hdac_hdmi_priv *hdmi,
  125. struct hdac_hdmi_cvt *cvt)
  126. {
  127. struct hdac_hdmi_pcm *pcm;
  128. list_for_each_entry(pcm, &hdmi->pcm_list, head) {
  129. if (pcm->cvt == cvt)
  130. return pcm;
  131. }
  132. return NULL;
  133. }
  134. static void hdac_hdmi_jack_report(struct hdac_hdmi_pcm *pcm,
  135. struct hdac_hdmi_port *port, bool is_connect)
  136. {
  137. struct hdac_device *hdev = port->pin->hdev;
  138. port->is_connect = is_connect;
  139. if (is_connect) {
  140. /*
  141. * Report Jack connect event when a device is connected
  142. * for the first time where same PCM is attached to multiple
  143. * ports.
  144. */
  145. if (pcm->jack_event == 0) {
  146. dev_dbg(&hdev->dev,
  147. "jack report for pcm=%d\n",
  148. pcm->pcm_id);
  149. snd_soc_jack_report(pcm->jack, SND_JACK_AVOUT,
  150. SND_JACK_AVOUT);
  151. }
  152. pcm->jack_event++;
  153. } else {
  154. /*
  155. * Report Jack disconnect event when a device is disconnected
  156. * is the only last connected device when same PCM is attached
  157. * to multiple ports.
  158. */
  159. if (pcm->jack_event == 1)
  160. snd_soc_jack_report(pcm->jack, 0, SND_JACK_AVOUT);
  161. if (pcm->jack_event > 0)
  162. pcm->jack_event--;
  163. }
  164. }
  165. static void hdac_hdmi_port_dapm_update(struct hdac_hdmi_port *port)
  166. {
  167. if (port->is_connect)
  168. snd_soc_dapm_enable_pin(port->dapm, port->jack_pin);
  169. else
  170. snd_soc_dapm_disable_pin(port->dapm, port->jack_pin);
  171. snd_soc_dapm_sync(port->dapm);
  172. }
  173. static void hdac_hdmi_jack_dapm_work(struct work_struct *work)
  174. {
  175. struct hdac_hdmi_port *port;
  176. port = container_of(work, struct hdac_hdmi_port, dapm_work);
  177. hdac_hdmi_port_dapm_update(port);
  178. }
  179. static void hdac_hdmi_jack_report_sync(struct hdac_hdmi_pcm *pcm,
  180. struct hdac_hdmi_port *port, bool is_connect)
  181. {
  182. hdac_hdmi_jack_report(pcm, port, is_connect);
  183. hdac_hdmi_port_dapm_update(port);
  184. }
  185. /* MST supported verbs */
  186. /*
  187. * Get the no devices that can be connected to a port on the Pin widget.
  188. */
  189. static int hdac_hdmi_get_port_len(struct hdac_device *hdev, hda_nid_t nid)
  190. {
  191. unsigned int caps;
  192. unsigned int type, param;
  193. caps = get_wcaps(hdev, nid);
  194. type = get_wcaps_type(caps);
  195. if (!(caps & AC_WCAP_DIGITAL) || (type != AC_WID_PIN))
  196. return 0;
  197. param = snd_hdac_read_parm_uncached(hdev, nid, AC_PAR_DEVLIST_LEN);
  198. if (param == -1)
  199. return param;
  200. return param & AC_DEV_LIST_LEN_MASK;
  201. }
  202. /*
  203. * Get the port entry select on the pin. Return the port entry
  204. * id selected on the pin. Return 0 means the first port entry
  205. * is selected or MST is not supported.
  206. */
  207. static int hdac_hdmi_port_select_get(struct hdac_device *hdev,
  208. struct hdac_hdmi_port *port)
  209. {
  210. return snd_hdac_codec_read(hdev, port->pin->nid,
  211. 0, AC_VERB_GET_DEVICE_SEL, 0);
  212. }
  213. /*
  214. * Sets the selected port entry for the configuring Pin widget verb.
  215. * returns error if port set is not equal to port get otherwise success
  216. */
  217. static int hdac_hdmi_port_select_set(struct hdac_device *hdev,
  218. struct hdac_hdmi_port *port)
  219. {
  220. int num_ports;
  221. if (!port->pin->mst_capable)
  222. return 0;
  223. /* AC_PAR_DEVLIST_LEN is 0 based. */
  224. num_ports = hdac_hdmi_get_port_len(hdev, port->pin->nid);
  225. if (num_ports < 0)
  226. return -EIO;
  227. /*
  228. * Device List Length is a 0 based integer value indicating the
  229. * number of sink device that a MST Pin Widget can support.
  230. */
  231. if (num_ports + 1 < port->id)
  232. return 0;
  233. snd_hdac_codec_write(hdev, port->pin->nid, 0,
  234. AC_VERB_SET_DEVICE_SEL, port->id);
  235. if (port->id != hdac_hdmi_port_select_get(hdev, port))
  236. return -EIO;
  237. dev_dbg(&hdev->dev, "Selected the port=%d\n", port->id);
  238. return 0;
  239. }
  240. static struct hdac_hdmi_pcm *get_hdmi_pcm_from_id(struct hdac_hdmi_priv *hdmi,
  241. int pcm_idx)
  242. {
  243. struct hdac_hdmi_pcm *pcm;
  244. list_for_each_entry(pcm, &hdmi->pcm_list, head) {
  245. if (pcm->pcm_id == pcm_idx)
  246. return pcm;
  247. }
  248. return NULL;
  249. }
  250. static unsigned int sad_format(const u8 *sad)
  251. {
  252. return ((sad[0] >> 0x3) & 0x1f);
  253. }
  254. static unsigned int sad_sample_bits_lpcm(const u8 *sad)
  255. {
  256. return (sad[2] & 7);
  257. }
  258. static int hdac_hdmi_eld_limit_formats(struct snd_pcm_runtime *runtime,
  259. void *eld)
  260. {
  261. u64 formats = SNDRV_PCM_FMTBIT_S16;
  262. int i;
  263. const u8 *sad, *eld_buf = eld;
  264. sad = drm_eld_sad(eld_buf);
  265. if (!sad)
  266. goto format_constraint;
  267. for (i = drm_eld_sad_count(eld_buf); i > 0; i--, sad += 3) {
  268. if (sad_format(sad) == 1) { /* AUDIO_CODING_TYPE_LPCM */
  269. /*
  270. * the controller support 20 and 24 bits in 32 bit
  271. * container so we set S32
  272. */
  273. if (sad_sample_bits_lpcm(sad) & 0x6)
  274. formats |= SNDRV_PCM_FMTBIT_S32;
  275. }
  276. }
  277. format_constraint:
  278. return snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT,
  279. formats);
  280. }
  281. static void
  282. hdac_hdmi_set_dip_index(struct hdac_device *hdev, hda_nid_t pin_nid,
  283. int packet_index, int byte_index)
  284. {
  285. int val;
  286. val = (packet_index << 5) | (byte_index & 0x1f);
  287. snd_hdac_codec_write(hdev, pin_nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val);
  288. }
  289. struct dp_audio_infoframe {
  290. u8 type; /* 0x84 */
  291. u8 len; /* 0x1b */
  292. u8 ver; /* 0x11 << 2 */
  293. u8 CC02_CT47; /* match with HDMI infoframe from this on */
  294. u8 SS01_SF24;
  295. u8 CXT04;
  296. u8 CA;
  297. u8 LFEPBL01_LSV36_DM_INH7;
  298. };
  299. static int hdac_hdmi_setup_audio_infoframe(struct hdac_device *hdev,
  300. struct hdac_hdmi_pcm *pcm, struct hdac_hdmi_port *port)
  301. {
  302. uint8_t buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AUDIO_INFOFRAME_SIZE];
  303. struct hdmi_audio_infoframe frame;
  304. struct hdac_hdmi_pin *pin = port->pin;
  305. struct dp_audio_infoframe dp_ai;
  306. struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
  307. struct hdac_hdmi_cvt *cvt = pcm->cvt;
  308. u8 *dip;
  309. int ret;
  310. int i;
  311. const u8 *eld_buf;
  312. u8 conn_type;
  313. int channels, ca;
  314. ca = snd_hdac_channel_allocation(hdev, port->eld.info.spk_alloc,
  315. pcm->channels, pcm->chmap_set, true, pcm->chmap);
  316. channels = snd_hdac_get_active_channels(ca);
  317. hdmi->chmap.ops.set_channel_count(hdev, cvt->nid, channels);
  318. snd_hdac_setup_channel_mapping(&hdmi->chmap, pin->nid, false, ca,
  319. pcm->channels, pcm->chmap, pcm->chmap_set);
  320. eld_buf = port->eld.eld_buffer;
  321. conn_type = drm_eld_get_conn_type(eld_buf);
  322. switch (conn_type) {
  323. case DRM_ELD_CONN_TYPE_HDMI:
  324. hdmi_audio_infoframe_init(&frame);
  325. frame.channels = channels;
  326. frame.channel_allocation = ca;
  327. ret = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer));
  328. if (ret < 0)
  329. return ret;
  330. break;
  331. case DRM_ELD_CONN_TYPE_DP:
  332. memset(&dp_ai, 0, sizeof(dp_ai));
  333. dp_ai.type = 0x84;
  334. dp_ai.len = 0x1b;
  335. dp_ai.ver = 0x11 << 2;
  336. dp_ai.CC02_CT47 = channels - 1;
  337. dp_ai.CA = ca;
  338. dip = (u8 *)&dp_ai;
  339. break;
  340. default:
  341. dev_err(&hdev->dev, "Invalid connection type: %d\n", conn_type);
  342. return -EIO;
  343. }
  344. /* stop infoframe transmission */
  345. hdac_hdmi_set_dip_index(hdev, pin->nid, 0x0, 0x0);
  346. snd_hdac_codec_write(hdev, pin->nid, 0,
  347. AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_DISABLE);
  348. /* Fill infoframe. Index auto-incremented */
  349. hdac_hdmi_set_dip_index(hdev, pin->nid, 0x0, 0x0);
  350. if (conn_type == DRM_ELD_CONN_TYPE_HDMI) {
  351. for (i = 0; i < sizeof(buffer); i++)
  352. snd_hdac_codec_write(hdev, pin->nid, 0,
  353. AC_VERB_SET_HDMI_DIP_DATA, buffer[i]);
  354. } else {
  355. for (i = 0; i < sizeof(dp_ai); i++)
  356. snd_hdac_codec_write(hdev, pin->nid, 0,
  357. AC_VERB_SET_HDMI_DIP_DATA, dip[i]);
  358. }
  359. /* Start infoframe */
  360. hdac_hdmi_set_dip_index(hdev, pin->nid, 0x0, 0x0);
  361. snd_hdac_codec_write(hdev, pin->nid, 0,
  362. AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_BEST);
  363. return 0;
  364. }
  365. static int hdac_hdmi_set_tdm_slot(struct snd_soc_dai *dai,
  366. unsigned int tx_mask, unsigned int rx_mask,
  367. int slots, int slot_width)
  368. {
  369. struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai);
  370. struct hdac_device *hdev = hdmi->hdev;
  371. struct hdac_hdmi_dai_port_map *dai_map;
  372. struct hdac_hdmi_pcm *pcm;
  373. dev_dbg(&hdev->dev, "%s: strm_tag: %d\n", __func__, tx_mask);
  374. dai_map = &hdmi->dai_map[dai->id];
  375. pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
  376. if (pcm)
  377. pcm->stream_tag = (tx_mask << 4);
  378. return 0;
  379. }
  380. static int hdac_hdmi_set_hw_params(struct snd_pcm_substream *substream,
  381. struct snd_pcm_hw_params *hparams, struct snd_soc_dai *dai)
  382. {
  383. struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai);
  384. struct hdac_hdmi_dai_port_map *dai_map;
  385. struct hdac_hdmi_pcm *pcm;
  386. int format;
  387. dai_map = &hdmi->dai_map[dai->id];
  388. format = snd_hdac_calc_stream_format(params_rate(hparams),
  389. params_channels(hparams), params_format(hparams),
  390. dai->driver->playback.sig_bits, 0);
  391. pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
  392. if (!pcm)
  393. return -EIO;
  394. pcm->format = format;
  395. pcm->channels = params_channels(hparams);
  396. return 0;
  397. }
  398. static int hdac_hdmi_query_port_connlist(struct hdac_device *hdev,
  399. struct hdac_hdmi_pin *pin,
  400. struct hdac_hdmi_port *port)
  401. {
  402. if (!(get_wcaps(hdev, pin->nid) & AC_WCAP_CONN_LIST)) {
  403. dev_warn(&hdev->dev,
  404. "HDMI: pin %d wcaps %#x does not support connection list\n",
  405. pin->nid, get_wcaps(hdev, pin->nid));
  406. return -EINVAL;
  407. }
  408. if (hdac_hdmi_port_select_set(hdev, port) < 0)
  409. return -EIO;
  410. port->num_mux_nids = snd_hdac_get_connections(hdev, pin->nid,
  411. port->mux_nids, HDA_MAX_CONNECTIONS);
  412. if (port->num_mux_nids == 0)
  413. dev_warn(&hdev->dev,
  414. "No connections found for pin:port %d:%d\n",
  415. pin->nid, port->id);
  416. dev_dbg(&hdev->dev, "num_mux_nids %d for pin:port %d:%d\n",
  417. port->num_mux_nids, pin->nid, port->id);
  418. return port->num_mux_nids;
  419. }
  420. /*
  421. * Query pcm list and return port to which stream is routed.
  422. *
  423. * Also query connection list of the pin, to validate the cvt to port map.
  424. *
  425. * Same stream rendering to multiple ports simultaneously can be done
  426. * possibly, but not supported for now in driver. So return the first port
  427. * connected.
  428. */
  429. static struct hdac_hdmi_port *hdac_hdmi_get_port_from_cvt(
  430. struct hdac_device *hdev,
  431. struct hdac_hdmi_priv *hdmi,
  432. struct hdac_hdmi_cvt *cvt)
  433. {
  434. struct hdac_hdmi_pcm *pcm;
  435. struct hdac_hdmi_port *port = NULL;
  436. int ret, i;
  437. list_for_each_entry(pcm, &hdmi->pcm_list, head) {
  438. if (pcm->cvt == cvt) {
  439. if (list_empty(&pcm->port_list))
  440. continue;
  441. list_for_each_entry(port, &pcm->port_list, head) {
  442. mutex_lock(&pcm->lock);
  443. ret = hdac_hdmi_query_port_connlist(hdev,
  444. port->pin, port);
  445. mutex_unlock(&pcm->lock);
  446. if (ret < 0)
  447. continue;
  448. for (i = 0; i < port->num_mux_nids; i++) {
  449. if (port->mux_nids[i] == cvt->nid &&
  450. port->eld.monitor_present &&
  451. port->eld.eld_valid)
  452. return port;
  453. }
  454. }
  455. }
  456. }
  457. return NULL;
  458. }
  459. /*
  460. * Go through all converters and ensure connection is set to
  461. * the correct pin as set via kcontrols.
  462. */
  463. static void hdac_hdmi_verify_connect_sel_all_pins(struct hdac_device *hdev)
  464. {
  465. struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
  466. struct hdac_hdmi_port *port;
  467. struct hdac_hdmi_cvt *cvt;
  468. int cvt_idx = 0;
  469. list_for_each_entry(cvt, &hdmi->cvt_list, head) {
  470. port = hdac_hdmi_get_port_from_cvt(hdev, hdmi, cvt);
  471. if (port && port->pin) {
  472. snd_hdac_codec_write(hdev, port->pin->nid, 0,
  473. AC_VERB_SET_CONNECT_SEL, cvt_idx);
  474. dev_dbg(&hdev->dev, "%s: %s set connect %d -> %d\n",
  475. __func__, cvt->name, port->pin->nid, cvt_idx);
  476. }
  477. ++cvt_idx;
  478. }
  479. }
  480. /*
  481. * This tries to get a valid pin and set the HW constraints based on the
  482. * ELD. Even if a valid pin is not found return success so that device open
  483. * doesn't fail.
  484. */
  485. static int hdac_hdmi_pcm_open(struct snd_pcm_substream *substream,
  486. struct snd_soc_dai *dai)
  487. {
  488. struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai);
  489. struct hdac_device *hdev = hdmi->hdev;
  490. struct hdac_hdmi_dai_port_map *dai_map;
  491. struct hdac_hdmi_cvt *cvt;
  492. struct hdac_hdmi_port *port;
  493. int ret;
  494. dai_map = &hdmi->dai_map[dai->id];
  495. cvt = dai_map->cvt;
  496. port = hdac_hdmi_get_port_from_cvt(hdev, hdmi, cvt);
  497. /*
  498. * To make PA and other userland happy.
  499. * userland scans devices so returning error does not help.
  500. */
  501. if (!port)
  502. return 0;
  503. if ((!port->eld.monitor_present) ||
  504. (!port->eld.eld_valid)) {
  505. dev_warn(&hdev->dev,
  506. "Failed: present?:%d ELD valid?:%d pin:port: %d:%d\n",
  507. port->eld.monitor_present, port->eld.eld_valid,
  508. port->pin->nid, port->id);
  509. return 0;
  510. }
  511. dai_map->port = port;
  512. ret = hdac_hdmi_eld_limit_formats(substream->runtime,
  513. port->eld.eld_buffer);
  514. if (ret < 0)
  515. return ret;
  516. return snd_pcm_hw_constraint_eld(substream->runtime,
  517. port->eld.eld_buffer);
  518. }
  519. static void hdac_hdmi_pcm_close(struct snd_pcm_substream *substream,
  520. struct snd_soc_dai *dai)
  521. {
  522. struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai);
  523. struct hdac_hdmi_dai_port_map *dai_map;
  524. struct hdac_hdmi_pcm *pcm;
  525. dai_map = &hdmi->dai_map[dai->id];
  526. pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
  527. if (pcm) {
  528. mutex_lock(&pcm->lock);
  529. pcm->chmap_set = false;
  530. memset(pcm->chmap, 0, sizeof(pcm->chmap));
  531. pcm->channels = 0;
  532. mutex_unlock(&pcm->lock);
  533. }
  534. if (dai_map->port)
  535. dai_map->port = NULL;
  536. }
  537. static int
  538. hdac_hdmi_query_cvt_params(struct hdac_device *hdev, struct hdac_hdmi_cvt *cvt)
  539. {
  540. unsigned int chans;
  541. struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
  542. int err;
  543. chans = get_wcaps(hdev, cvt->nid);
  544. chans = get_wcaps_channels(chans);
  545. cvt->params.channels_min = 2;
  546. cvt->params.channels_max = chans;
  547. if (chans > hdmi->chmap.channels_max)
  548. hdmi->chmap.channels_max = chans;
  549. err = snd_hdac_query_supported_pcm(hdev, cvt->nid,
  550. &cvt->params.rates,
  551. &cvt->params.formats,
  552. &cvt->params.maxbps);
  553. if (err < 0)
  554. dev_err(&hdev->dev,
  555. "Failed to query pcm params for nid %d: %d\n",
  556. cvt->nid, err);
  557. return err;
  558. }
  559. static int hdac_hdmi_fill_widget_info(struct device *dev,
  560. struct snd_soc_dapm_widget *w, enum snd_soc_dapm_type id,
  561. void *priv, const char *wname, const char *stream,
  562. struct snd_kcontrol_new *wc, int numkc,
  563. int (*event)(struct snd_soc_dapm_widget *,
  564. struct snd_kcontrol *, int), unsigned short event_flags)
  565. {
  566. w->id = id;
  567. w->name = devm_kstrdup(dev, wname, GFP_KERNEL);
  568. if (!w->name)
  569. return -ENOMEM;
  570. w->sname = stream;
  571. w->reg = SND_SOC_NOPM;
  572. w->shift = 0;
  573. w->kcontrol_news = wc;
  574. w->num_kcontrols = numkc;
  575. w->priv = priv;
  576. w->event = event;
  577. w->event_flags = event_flags;
  578. return 0;
  579. }
  580. static void hdac_hdmi_fill_route(struct snd_soc_dapm_route *route,
  581. const char *sink, const char *control, const char *src,
  582. int (*handler)(struct snd_soc_dapm_widget *src,
  583. struct snd_soc_dapm_widget *sink))
  584. {
  585. route->sink = sink;
  586. route->source = src;
  587. route->control = control;
  588. route->connected = handler;
  589. }
  590. static struct hdac_hdmi_pcm *hdac_hdmi_get_pcm(struct hdac_device *hdev,
  591. struct hdac_hdmi_port *port)
  592. {
  593. struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
  594. struct hdac_hdmi_pcm *pcm = NULL;
  595. struct hdac_hdmi_port *p;
  596. list_for_each_entry(pcm, &hdmi->pcm_list, head) {
  597. if (list_empty(&pcm->port_list))
  598. continue;
  599. list_for_each_entry(p, &pcm->port_list, head) {
  600. if (p->id == port->id && port->pin == p->pin)
  601. return pcm;
  602. }
  603. }
  604. return NULL;
  605. }
  606. static void hdac_hdmi_set_power_state(struct hdac_device *hdev,
  607. hda_nid_t nid, unsigned int pwr_state)
  608. {
  609. int count;
  610. unsigned int state;
  611. if (get_wcaps(hdev, nid) & AC_WCAP_POWER) {
  612. if (!snd_hdac_check_power_state(hdev, nid, pwr_state)) {
  613. for (count = 0; count < 10; count++) {
  614. snd_hdac_codec_read(hdev, nid, 0,
  615. AC_VERB_SET_POWER_STATE,
  616. pwr_state);
  617. state = snd_hdac_sync_power_state(hdev,
  618. nid, pwr_state);
  619. if (!(state & AC_PWRST_ERROR))
  620. break;
  621. }
  622. }
  623. }
  624. }
  625. static void hdac_hdmi_set_amp(struct hdac_device *hdev,
  626. hda_nid_t nid, int val)
  627. {
  628. if (get_wcaps(hdev, nid) & AC_WCAP_OUT_AMP)
  629. snd_hdac_codec_write(hdev, nid, 0,
  630. AC_VERB_SET_AMP_GAIN_MUTE, val);
  631. }
  632. static int hdac_hdmi_pin_output_widget_event(struct snd_soc_dapm_widget *w,
  633. struct snd_kcontrol *kc, int event)
  634. {
  635. struct hdac_hdmi_port *port = w->priv;
  636. struct hdac_device *hdev = dev_to_hdac_dev(w->dapm->dev);
  637. struct hdac_hdmi_pcm *pcm;
  638. dev_dbg(&hdev->dev, "%s: widget: %s event: %x\n",
  639. __func__, w->name, event);
  640. pcm = hdac_hdmi_get_pcm(hdev, port);
  641. if (!pcm)
  642. return -EIO;
  643. /* set the device if pin is mst_capable */
  644. if (hdac_hdmi_port_select_set(hdev, port) < 0)
  645. return -EIO;
  646. switch (event) {
  647. case SND_SOC_DAPM_PRE_PMU:
  648. hdac_hdmi_set_power_state(hdev, port->pin->nid, AC_PWRST_D0);
  649. /* Enable out path for this pin widget */
  650. snd_hdac_codec_write(hdev, port->pin->nid, 0,
  651. AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
  652. hdac_hdmi_set_amp(hdev, port->pin->nid, AMP_OUT_UNMUTE);
  653. return hdac_hdmi_setup_audio_infoframe(hdev, pcm, port);
  654. case SND_SOC_DAPM_POST_PMD:
  655. hdac_hdmi_set_amp(hdev, port->pin->nid, AMP_OUT_MUTE);
  656. /* Disable out path for this pin widget */
  657. snd_hdac_codec_write(hdev, port->pin->nid, 0,
  658. AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
  659. hdac_hdmi_set_power_state(hdev, port->pin->nid, AC_PWRST_D3);
  660. break;
  661. }
  662. return 0;
  663. }
  664. static int hdac_hdmi_cvt_output_widget_event(struct snd_soc_dapm_widget *w,
  665. struct snd_kcontrol *kc, int event)
  666. {
  667. struct hdac_hdmi_cvt *cvt = w->priv;
  668. struct hdac_device *hdev = dev_to_hdac_dev(w->dapm->dev);
  669. struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
  670. struct hdac_hdmi_pcm *pcm;
  671. dev_dbg(&hdev->dev, "%s: widget: %s event: %x\n",
  672. __func__, w->name, event);
  673. pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, cvt);
  674. if (!pcm)
  675. return -EIO;
  676. switch (event) {
  677. case SND_SOC_DAPM_PRE_PMU:
  678. hdac_hdmi_set_power_state(hdev, cvt->nid, AC_PWRST_D0);
  679. /* Enable transmission */
  680. snd_hdac_codec_write(hdev, cvt->nid, 0,
  681. AC_VERB_SET_DIGI_CONVERT_1, 1);
  682. /* Category Code (CC) to zero */
  683. snd_hdac_codec_write(hdev, cvt->nid, 0,
  684. AC_VERB_SET_DIGI_CONVERT_2, 0);
  685. snd_hdac_codec_write(hdev, cvt->nid, 0,
  686. AC_VERB_SET_CHANNEL_STREAMID, pcm->stream_tag);
  687. snd_hdac_codec_write(hdev, cvt->nid, 0,
  688. AC_VERB_SET_STREAM_FORMAT, pcm->format);
  689. /*
  690. * The connection indices are shared by all converters and
  691. * may interfere with each other. Ensure correct
  692. * routing for all converters at stream start.
  693. */
  694. hdac_hdmi_verify_connect_sel_all_pins(hdev);
  695. break;
  696. case SND_SOC_DAPM_POST_PMD:
  697. snd_hdac_codec_write(hdev, cvt->nid, 0,
  698. AC_VERB_SET_CHANNEL_STREAMID, 0);
  699. snd_hdac_codec_write(hdev, cvt->nid, 0,
  700. AC_VERB_SET_STREAM_FORMAT, 0);
  701. hdac_hdmi_set_power_state(hdev, cvt->nid, AC_PWRST_D3);
  702. break;
  703. }
  704. return 0;
  705. }
  706. static int hdac_hdmi_pin_mux_widget_event(struct snd_soc_dapm_widget *w,
  707. struct snd_kcontrol *kc, int event)
  708. {
  709. struct hdac_hdmi_port *port = w->priv;
  710. struct hdac_device *hdev = dev_to_hdac_dev(w->dapm->dev);
  711. int mux_idx;
  712. dev_dbg(&hdev->dev, "%s: widget: %s event: %x\n",
  713. __func__, w->name, event);
  714. if (!kc)
  715. kc = w->kcontrols[0];
  716. mux_idx = dapm_kcontrol_get_value(kc);
  717. /* set the device if pin is mst_capable */
  718. if (hdac_hdmi_port_select_set(hdev, port) < 0)
  719. return -EIO;
  720. if (mux_idx > 0) {
  721. snd_hdac_codec_write(hdev, port->pin->nid, 0,
  722. AC_VERB_SET_CONNECT_SEL, (mux_idx - 1));
  723. }
  724. return 0;
  725. }
  726. /*
  727. * Based on user selection, map the PINs with the PCMs.
  728. */
  729. static int hdac_hdmi_set_pin_port_mux(struct snd_kcontrol *kcontrol,
  730. struct snd_ctl_elem_value *ucontrol)
  731. {
  732. int ret;
  733. struct hdac_hdmi_port *p, *p_next;
  734. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  735. struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
  736. struct snd_soc_dapm_context *dapm = w->dapm;
  737. struct hdac_hdmi_port *port = w->priv;
  738. struct hdac_device *hdev = dev_to_hdac_dev(dapm->dev);
  739. struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
  740. struct hdac_hdmi_pcm *pcm = NULL;
  741. const char *cvt_name = e->texts[ucontrol->value.enumerated.item[0]];
  742. ret = snd_soc_dapm_put_enum_double(kcontrol, ucontrol);
  743. if (ret < 0)
  744. return ret;
  745. if (port == NULL)
  746. return -EINVAL;
  747. mutex_lock(&hdmi->pin_mutex);
  748. list_for_each_entry(pcm, &hdmi->pcm_list, head) {
  749. if (list_empty(&pcm->port_list))
  750. continue;
  751. list_for_each_entry_safe(p, p_next, &pcm->port_list, head) {
  752. if (p == port && p->id == port->id &&
  753. p->pin == port->pin) {
  754. hdac_hdmi_jack_report_sync(pcm, port, false);
  755. list_del(&p->head);
  756. }
  757. }
  758. }
  759. /*
  760. * Jack status is not reported during device probe as the
  761. * PCMs are not registered by then. So report it here.
  762. */
  763. list_for_each_entry(pcm, &hdmi->pcm_list, head) {
  764. if (!strcmp(cvt_name, pcm->cvt->name)) {
  765. list_add_tail(&port->head, &pcm->port_list);
  766. if (port->eld.monitor_present && port->eld.eld_valid) {
  767. hdac_hdmi_jack_report_sync(pcm, port, true);
  768. mutex_unlock(&hdmi->pin_mutex);
  769. return ret;
  770. }
  771. }
  772. }
  773. mutex_unlock(&hdmi->pin_mutex);
  774. return ret;
  775. }
  776. /*
  777. * Ideally the Mux inputs should be based on the num_muxs enumerated, but
  778. * the display driver seem to be programming the connection list for the pin
  779. * widget runtime.
  780. *
  781. * So programming all the possible inputs for the mux, the user has to take
  782. * care of selecting the right one and leaving all other inputs selected to
  783. * "NONE"
  784. */
  785. static int hdac_hdmi_create_pin_port_muxs(struct hdac_device *hdev,
  786. struct hdac_hdmi_port *port,
  787. struct snd_soc_dapm_widget *widget,
  788. const char *widget_name)
  789. {
  790. struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
  791. struct hdac_hdmi_pin *pin = port->pin;
  792. struct snd_kcontrol_new *kc;
  793. struct hdac_hdmi_cvt *cvt;
  794. struct soc_enum *se;
  795. char kc_name[NAME_SIZE];
  796. char mux_items[NAME_SIZE];
  797. /* To hold inputs to the Pin mux */
  798. char *items[HDA_MAX_CONNECTIONS];
  799. int i = 0;
  800. int num_items = hdmi->num_cvt + 1;
  801. kc = devm_kzalloc(&hdev->dev, sizeof(*kc), GFP_KERNEL);
  802. if (!kc)
  803. return -ENOMEM;
  804. se = devm_kzalloc(&hdev->dev, sizeof(*se), GFP_KERNEL);
  805. if (!se)
  806. return -ENOMEM;
  807. snprintf(kc_name, NAME_SIZE, "Pin %d port %d Input",
  808. pin->nid, port->id);
  809. kc->name = devm_kstrdup(&hdev->dev, kc_name, GFP_KERNEL);
  810. if (!kc->name)
  811. return -ENOMEM;
  812. kc->private_value = (long)se;
  813. kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  814. kc->access = 0;
  815. kc->info = snd_soc_info_enum_double;
  816. kc->put = hdac_hdmi_set_pin_port_mux;
  817. kc->get = snd_soc_dapm_get_enum_double;
  818. se->reg = SND_SOC_NOPM;
  819. /* enum texts: ["NONE", "cvt #", "cvt #", ...] */
  820. se->items = num_items;
  821. se->mask = roundup_pow_of_two(se->items) - 1;
  822. sprintf(mux_items, "NONE");
  823. items[i] = devm_kstrdup(&hdev->dev, mux_items, GFP_KERNEL);
  824. if (!items[i])
  825. return -ENOMEM;
  826. list_for_each_entry(cvt, &hdmi->cvt_list, head) {
  827. i++;
  828. sprintf(mux_items, "cvt %d", cvt->nid);
  829. items[i] = devm_kstrdup(&hdev->dev, mux_items, GFP_KERNEL);
  830. if (!items[i])
  831. return -ENOMEM;
  832. }
  833. se->texts = devm_kmemdup(&hdev->dev, items,
  834. (num_items * sizeof(char *)), GFP_KERNEL);
  835. if (!se->texts)
  836. return -ENOMEM;
  837. return hdac_hdmi_fill_widget_info(&hdev->dev, widget,
  838. snd_soc_dapm_mux, port, widget_name, NULL, kc, 1,
  839. hdac_hdmi_pin_mux_widget_event,
  840. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_REG);
  841. }
  842. /* Add cvt <- input <- mux route map */
  843. static void hdac_hdmi_add_pinmux_cvt_route(struct hdac_device *hdev,
  844. struct snd_soc_dapm_widget *widgets,
  845. struct snd_soc_dapm_route *route, int rindex)
  846. {
  847. struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
  848. const struct snd_kcontrol_new *kc;
  849. struct soc_enum *se;
  850. int mux_index = hdmi->num_cvt + hdmi->num_ports;
  851. int i, j;
  852. for (i = 0; i < hdmi->num_ports; i++) {
  853. kc = widgets[mux_index].kcontrol_news;
  854. se = (struct soc_enum *)kc->private_value;
  855. for (j = 0; j < hdmi->num_cvt; j++) {
  856. hdac_hdmi_fill_route(&route[rindex],
  857. widgets[mux_index].name,
  858. se->texts[j + 1],
  859. widgets[j].name, NULL);
  860. rindex++;
  861. }
  862. mux_index++;
  863. }
  864. }
  865. /*
  866. * Widgets are added in the below sequence
  867. * Converter widgets for num converters enumerated
  868. * Pin-port widgets for num ports for Pins enumerated
  869. * Pin-port mux widgets to represent connenction list of pin widget
  870. *
  871. * For each port, one Mux and One output widget is added
  872. * Total widgets elements = num_cvt + (num_ports * 2);
  873. *
  874. * Routes are added as below:
  875. * pin-port mux -> pin (based on num_ports)
  876. * cvt -> "Input sel control" -> pin-port_mux
  877. *
  878. * Total route elements:
  879. * num_ports + (pin_muxes * num_cvt)
  880. */
  881. static int create_fill_widget_route_map(struct snd_soc_dapm_context *dapm)
  882. {
  883. struct snd_soc_dapm_widget *widgets;
  884. struct snd_soc_dapm_route *route;
  885. struct hdac_device *hdev = dev_to_hdac_dev(dapm->dev);
  886. struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
  887. struct snd_soc_dai_driver *dai_drv = hdmi->dai_drv;
  888. char widget_name[NAME_SIZE];
  889. struct hdac_hdmi_cvt *cvt;
  890. struct hdac_hdmi_pin *pin;
  891. int ret, i = 0, num_routes = 0, j;
  892. if (list_empty(&hdmi->cvt_list) || list_empty(&hdmi->pin_list))
  893. return -EINVAL;
  894. widgets = devm_kzalloc(dapm->dev, (sizeof(*widgets) *
  895. ((2 * hdmi->num_ports) + hdmi->num_cvt)),
  896. GFP_KERNEL);
  897. if (!widgets)
  898. return -ENOMEM;
  899. /* DAPM widgets to represent each converter widget */
  900. list_for_each_entry(cvt, &hdmi->cvt_list, head) {
  901. sprintf(widget_name, "Converter %d", cvt->nid);
  902. ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
  903. snd_soc_dapm_aif_in, cvt,
  904. widget_name, dai_drv[i].playback.stream_name, NULL, 0,
  905. hdac_hdmi_cvt_output_widget_event,
  906. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD);
  907. if (ret < 0)
  908. return ret;
  909. i++;
  910. }
  911. list_for_each_entry(pin, &hdmi->pin_list, head) {
  912. for (j = 0; j < pin->num_ports; j++) {
  913. sprintf(widget_name, "hif%d-%d Output",
  914. pin->nid, pin->ports[j].id);
  915. ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
  916. snd_soc_dapm_output, &pin->ports[j],
  917. widget_name, NULL, NULL, 0,
  918. hdac_hdmi_pin_output_widget_event,
  919. SND_SOC_DAPM_PRE_PMU |
  920. SND_SOC_DAPM_POST_PMD);
  921. if (ret < 0)
  922. return ret;
  923. pin->ports[j].output_pin = widgets[i].name;
  924. i++;
  925. }
  926. }
  927. /* DAPM widgets to represent the connection list to pin widget */
  928. list_for_each_entry(pin, &hdmi->pin_list, head) {
  929. for (j = 0; j < pin->num_ports; j++) {
  930. sprintf(widget_name, "Pin%d-Port%d Mux",
  931. pin->nid, pin->ports[j].id);
  932. ret = hdac_hdmi_create_pin_port_muxs(hdev,
  933. &pin->ports[j], &widgets[i],
  934. widget_name);
  935. if (ret < 0)
  936. return ret;
  937. i++;
  938. /* For cvt to pin_mux mapping */
  939. num_routes += hdmi->num_cvt;
  940. /* For pin_mux to pin mapping */
  941. num_routes++;
  942. }
  943. }
  944. route = devm_kzalloc(dapm->dev, (sizeof(*route) * num_routes),
  945. GFP_KERNEL);
  946. if (!route)
  947. return -ENOMEM;
  948. i = 0;
  949. /* Add pin <- NULL <- mux route map */
  950. list_for_each_entry(pin, &hdmi->pin_list, head) {
  951. for (j = 0; j < pin->num_ports; j++) {
  952. int sink_index = i + hdmi->num_cvt;
  953. int src_index = sink_index + pin->num_ports *
  954. hdmi->num_pin;
  955. hdac_hdmi_fill_route(&route[i],
  956. widgets[sink_index].name, NULL,
  957. widgets[src_index].name, NULL);
  958. i++;
  959. }
  960. }
  961. hdac_hdmi_add_pinmux_cvt_route(hdev, widgets, route, i);
  962. snd_soc_dapm_new_controls(dapm, widgets,
  963. ((2 * hdmi->num_ports) + hdmi->num_cvt));
  964. snd_soc_dapm_add_routes(dapm, route, num_routes);
  965. snd_soc_dapm_new_widgets(dapm->card);
  966. return 0;
  967. }
  968. static int hdac_hdmi_init_dai_map(struct hdac_device *hdev)
  969. {
  970. struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
  971. struct hdac_hdmi_dai_port_map *dai_map;
  972. struct hdac_hdmi_cvt *cvt;
  973. int dai_id = 0;
  974. if (list_empty(&hdmi->cvt_list))
  975. return -EINVAL;
  976. list_for_each_entry(cvt, &hdmi->cvt_list, head) {
  977. dai_map = &hdmi->dai_map[dai_id];
  978. dai_map->dai_id = dai_id;
  979. dai_map->cvt = cvt;
  980. dai_id++;
  981. if (dai_id == HDA_MAX_CVTS) {
  982. dev_warn(&hdev->dev,
  983. "Max dais supported: %d\n", dai_id);
  984. break;
  985. }
  986. }
  987. return 0;
  988. }
  989. static int hdac_hdmi_add_cvt(struct hdac_device *hdev, hda_nid_t nid)
  990. {
  991. struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
  992. struct hdac_hdmi_cvt *cvt;
  993. char name[NAME_SIZE];
  994. cvt = devm_kzalloc(&hdev->dev, sizeof(*cvt), GFP_KERNEL);
  995. if (!cvt)
  996. return -ENOMEM;
  997. cvt->nid = nid;
  998. sprintf(name, "cvt %d", cvt->nid);
  999. cvt->name = devm_kstrdup(&hdev->dev, name, GFP_KERNEL);
  1000. if (!cvt->name)
  1001. return -ENOMEM;
  1002. list_add_tail(&cvt->head, &hdmi->cvt_list);
  1003. hdmi->num_cvt++;
  1004. return hdac_hdmi_query_cvt_params(hdev, cvt);
  1005. }
  1006. static int hdac_hdmi_parse_eld(struct hdac_device *hdev,
  1007. struct hdac_hdmi_port *port)
  1008. {
  1009. unsigned int ver, mnl;
  1010. ver = (port->eld.eld_buffer[DRM_ELD_VER] & DRM_ELD_VER_MASK)
  1011. >> DRM_ELD_VER_SHIFT;
  1012. if (ver != ELD_VER_CEA_861D && ver != ELD_VER_PARTIAL) {
  1013. dev_err(&hdev->dev, "HDMI: Unknown ELD version %d\n", ver);
  1014. return -EINVAL;
  1015. }
  1016. mnl = (port->eld.eld_buffer[DRM_ELD_CEA_EDID_VER_MNL] &
  1017. DRM_ELD_MNL_MASK) >> DRM_ELD_MNL_SHIFT;
  1018. if (mnl > ELD_MAX_MNL) {
  1019. dev_err(&hdev->dev, "HDMI: MNL Invalid %d\n", mnl);
  1020. return -EINVAL;
  1021. }
  1022. port->eld.info.spk_alloc = port->eld.eld_buffer[DRM_ELD_SPEAKER];
  1023. return 0;
  1024. }
  1025. static void hdac_hdmi_present_sense(struct hdac_hdmi_pin *pin,
  1026. struct hdac_hdmi_port *port)
  1027. {
  1028. struct hdac_device *hdev = pin->hdev;
  1029. struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
  1030. struct hdac_hdmi_pcm *pcm;
  1031. int size = 0;
  1032. int port_id = -1;
  1033. if (!hdmi)
  1034. return;
  1035. /*
  1036. * In case of non MST pin, get_eld info API expectes port
  1037. * to be -1.
  1038. */
  1039. mutex_lock(&hdmi->pin_mutex);
  1040. port->eld.monitor_present = false;
  1041. if (pin->mst_capable)
  1042. port_id = port->id;
  1043. size = snd_hdac_acomp_get_eld(hdev, pin->nid, port_id,
  1044. &port->eld.monitor_present,
  1045. port->eld.eld_buffer,
  1046. ELD_MAX_SIZE);
  1047. if (size > 0) {
  1048. size = min(size, ELD_MAX_SIZE);
  1049. if (hdac_hdmi_parse_eld(hdev, port) < 0)
  1050. size = -EINVAL;
  1051. }
  1052. if (size > 0) {
  1053. port->eld.eld_valid = true;
  1054. port->eld.eld_size = size;
  1055. } else {
  1056. port->eld.eld_valid = false;
  1057. port->eld.eld_size = 0;
  1058. }
  1059. pcm = hdac_hdmi_get_pcm(hdev, port);
  1060. if (!port->eld.monitor_present || !port->eld.eld_valid) {
  1061. dev_err(&hdev->dev, "%s: disconnect for pin:port %d:%d\n",
  1062. __func__, pin->nid, port->id);
  1063. /*
  1064. * PCMs are not registered during device probe, so don't
  1065. * report jack here. It will be done in usermode mux
  1066. * control select.
  1067. */
  1068. if (pcm) {
  1069. hdac_hdmi_jack_report(pcm, port, false);
  1070. schedule_work(&port->dapm_work);
  1071. }
  1072. mutex_unlock(&hdmi->pin_mutex);
  1073. return;
  1074. }
  1075. if (port->eld.monitor_present && port->eld.eld_valid) {
  1076. if (pcm) {
  1077. hdac_hdmi_jack_report(pcm, port, true);
  1078. schedule_work(&port->dapm_work);
  1079. }
  1080. print_hex_dump_debug("ELD: ", DUMP_PREFIX_OFFSET, 16, 1,
  1081. port->eld.eld_buffer, port->eld.eld_size, false);
  1082. }
  1083. mutex_unlock(&hdmi->pin_mutex);
  1084. }
  1085. static int hdac_hdmi_add_ports(struct hdac_device *hdev,
  1086. struct hdac_hdmi_pin *pin)
  1087. {
  1088. struct hdac_hdmi_port *ports;
  1089. int max_ports = HDA_MAX_PORTS;
  1090. int i;
  1091. /*
  1092. * FIXME: max_port may vary for each platform, so pass this as
  1093. * as driver data or query from i915 interface when this API is
  1094. * implemented.
  1095. */
  1096. ports = devm_kcalloc(&hdev->dev, max_ports, sizeof(*ports), GFP_KERNEL);
  1097. if (!ports)
  1098. return -ENOMEM;
  1099. for (i = 0; i < max_ports; i++) {
  1100. ports[i].id = i;
  1101. ports[i].pin = pin;
  1102. INIT_WORK(&ports[i].dapm_work, hdac_hdmi_jack_dapm_work);
  1103. }
  1104. pin->ports = ports;
  1105. pin->num_ports = max_ports;
  1106. return 0;
  1107. }
  1108. static int hdac_hdmi_add_pin(struct hdac_device *hdev, hda_nid_t nid)
  1109. {
  1110. struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
  1111. struct hdac_hdmi_pin *pin;
  1112. int ret;
  1113. pin = devm_kzalloc(&hdev->dev, sizeof(*pin), GFP_KERNEL);
  1114. if (!pin)
  1115. return -ENOMEM;
  1116. pin->nid = nid;
  1117. pin->mst_capable = false;
  1118. pin->hdev = hdev;
  1119. ret = hdac_hdmi_add_ports(hdev, pin);
  1120. if (ret < 0)
  1121. return ret;
  1122. list_add_tail(&pin->head, &hdmi->pin_list);
  1123. hdmi->num_pin++;
  1124. hdmi->num_ports += pin->num_ports;
  1125. return 0;
  1126. }
  1127. #define INTEL_VENDOR_NID 0x08
  1128. #define INTEL_GLK_VENDOR_NID 0x0b
  1129. #define INTEL_GET_VENDOR_VERB 0xf81
  1130. #define INTEL_SET_VENDOR_VERB 0x781
  1131. #define INTEL_EN_DP12 0x02 /* enable DP 1.2 features */
  1132. #define INTEL_EN_ALL_PIN_CVTS 0x01 /* enable 2nd & 3rd pins and convertors */
  1133. static void hdac_hdmi_skl_enable_all_pins(struct hdac_device *hdev)
  1134. {
  1135. unsigned int vendor_param;
  1136. struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
  1137. unsigned int vendor_nid = hdmi->drv_data->vendor_nid;
  1138. vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0,
  1139. INTEL_GET_VENDOR_VERB, 0);
  1140. if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
  1141. return;
  1142. vendor_param |= INTEL_EN_ALL_PIN_CVTS;
  1143. vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0,
  1144. INTEL_SET_VENDOR_VERB, vendor_param);
  1145. if (vendor_param == -1)
  1146. return;
  1147. }
  1148. static void hdac_hdmi_skl_enable_dp12(struct hdac_device *hdev)
  1149. {
  1150. unsigned int vendor_param;
  1151. struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
  1152. unsigned int vendor_nid = hdmi->drv_data->vendor_nid;
  1153. vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0,
  1154. INTEL_GET_VENDOR_VERB, 0);
  1155. if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
  1156. return;
  1157. /* enable DP1.2 mode */
  1158. vendor_param |= INTEL_EN_DP12;
  1159. vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0,
  1160. INTEL_SET_VENDOR_VERB, vendor_param);
  1161. if (vendor_param == -1)
  1162. return;
  1163. }
  1164. static const struct snd_soc_dai_ops hdmi_dai_ops = {
  1165. .startup = hdac_hdmi_pcm_open,
  1166. .shutdown = hdac_hdmi_pcm_close,
  1167. .hw_params = hdac_hdmi_set_hw_params,
  1168. .set_tdm_slot = hdac_hdmi_set_tdm_slot,
  1169. };
  1170. /*
  1171. * Each converter can support a stream independently. So a dai is created
  1172. * based on the number of converter queried.
  1173. */
  1174. static int hdac_hdmi_create_dais(struct hdac_device *hdev,
  1175. struct snd_soc_dai_driver **dais,
  1176. struct hdac_hdmi_priv *hdmi, int num_dais)
  1177. {
  1178. struct snd_soc_dai_driver *hdmi_dais;
  1179. struct hdac_hdmi_cvt *cvt;
  1180. char name[NAME_SIZE], dai_name[NAME_SIZE];
  1181. int i = 0;
  1182. u32 rates, bps;
  1183. unsigned int rate_max = 384000, rate_min = 8000;
  1184. u64 formats;
  1185. int ret;
  1186. hdmi_dais = devm_kzalloc(&hdev->dev,
  1187. (sizeof(*hdmi_dais) * num_dais),
  1188. GFP_KERNEL);
  1189. if (!hdmi_dais)
  1190. return -ENOMEM;
  1191. list_for_each_entry(cvt, &hdmi->cvt_list, head) {
  1192. ret = snd_hdac_query_supported_pcm(hdev, cvt->nid,
  1193. &rates, &formats, &bps);
  1194. if (ret)
  1195. return ret;
  1196. /* Filter out 44.1, 88.2 and 176.4Khz */
  1197. rates &= ~(SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_88200 |
  1198. SNDRV_PCM_RATE_176400);
  1199. if (!rates)
  1200. return -EINVAL;
  1201. sprintf(dai_name, "intel-hdmi-hifi%d", i+1);
  1202. hdmi_dais[i].name = devm_kstrdup(&hdev->dev,
  1203. dai_name, GFP_KERNEL);
  1204. if (!hdmi_dais[i].name)
  1205. return -ENOMEM;
  1206. snprintf(name, sizeof(name), "hifi%d", i+1);
  1207. hdmi_dais[i].playback.stream_name =
  1208. devm_kstrdup(&hdev->dev, name, GFP_KERNEL);
  1209. if (!hdmi_dais[i].playback.stream_name)
  1210. return -ENOMEM;
  1211. /*
  1212. * Set caps based on capability queried from the converter.
  1213. * It will be constrained runtime based on ELD queried.
  1214. */
  1215. hdmi_dais[i].playback.formats = formats;
  1216. hdmi_dais[i].playback.rates = rates;
  1217. hdmi_dais[i].playback.rate_max = rate_max;
  1218. hdmi_dais[i].playback.rate_min = rate_min;
  1219. hdmi_dais[i].playback.channels_min = 2;
  1220. hdmi_dais[i].playback.channels_max = 2;
  1221. hdmi_dais[i].playback.sig_bits = bps;
  1222. hdmi_dais[i].ops = &hdmi_dai_ops;
  1223. i++;
  1224. }
  1225. *dais = hdmi_dais;
  1226. hdmi->dai_drv = hdmi_dais;
  1227. return 0;
  1228. }
  1229. /*
  1230. * Parse all nodes and store the cvt/pin nids in array
  1231. * Add one time initialization for pin and cvt widgets
  1232. */
  1233. static int hdac_hdmi_parse_and_map_nid(struct hdac_device *hdev,
  1234. struct snd_soc_dai_driver **dais, int *num_dais)
  1235. {
  1236. hda_nid_t nid;
  1237. int i, num_nodes;
  1238. struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
  1239. int ret;
  1240. hdac_hdmi_skl_enable_all_pins(hdev);
  1241. hdac_hdmi_skl_enable_dp12(hdev);
  1242. num_nodes = snd_hdac_get_sub_nodes(hdev, hdev->afg, &nid);
  1243. if (!nid || num_nodes <= 0) {
  1244. dev_warn(&hdev->dev, "HDMI: failed to get afg sub nodes\n");
  1245. return -EINVAL;
  1246. }
  1247. for (i = 0; i < num_nodes; i++, nid++) {
  1248. unsigned int caps;
  1249. unsigned int type;
  1250. caps = get_wcaps(hdev, nid);
  1251. type = get_wcaps_type(caps);
  1252. if (!(caps & AC_WCAP_DIGITAL))
  1253. continue;
  1254. switch (type) {
  1255. case AC_WID_AUD_OUT:
  1256. ret = hdac_hdmi_add_cvt(hdev, nid);
  1257. if (ret < 0)
  1258. return ret;
  1259. break;
  1260. case AC_WID_PIN:
  1261. ret = hdac_hdmi_add_pin(hdev, nid);
  1262. if (ret < 0)
  1263. return ret;
  1264. break;
  1265. }
  1266. }
  1267. if (!hdmi->num_pin || !hdmi->num_cvt) {
  1268. ret = -EIO;
  1269. dev_err(&hdev->dev, "Bad pin/cvt setup in %s\n", __func__);
  1270. return ret;
  1271. }
  1272. ret = hdac_hdmi_create_dais(hdev, dais, hdmi, hdmi->num_cvt);
  1273. if (ret) {
  1274. dev_err(&hdev->dev, "Failed to create dais with err: %d\n",
  1275. ret);
  1276. return ret;
  1277. }
  1278. *num_dais = hdmi->num_cvt;
  1279. ret = hdac_hdmi_init_dai_map(hdev);
  1280. if (ret < 0)
  1281. dev_err(&hdev->dev, "Failed to init DAI map with err: %d\n",
  1282. ret);
  1283. return ret;
  1284. }
  1285. static int hdac_hdmi_pin2port(void *aptr, int pin)
  1286. {
  1287. return pin - 4; /* map NID 0x05 -> port #1 */
  1288. }
  1289. static void hdac_hdmi_eld_notify_cb(void *aptr, int port, int pipe)
  1290. {
  1291. struct hdac_device *hdev = aptr;
  1292. struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
  1293. struct hdac_hdmi_pin *pin = NULL;
  1294. struct hdac_hdmi_port *hport = NULL;
  1295. struct snd_soc_component *component = hdmi->component;
  1296. int i;
  1297. /* Don't know how this mapping is derived */
  1298. hda_nid_t pin_nid = port + 0x04;
  1299. dev_dbg(&hdev->dev, "%s: for pin:%d port=%d\n", __func__,
  1300. pin_nid, pipe);
  1301. /*
  1302. * skip notification during system suspend (but not in runtime PM);
  1303. * the state will be updated at resume. Also since the ELD and
  1304. * connection states are updated in anyway at the end of the resume,
  1305. * we can skip it when received during PM process.
  1306. */
  1307. if (snd_power_get_state(component->card->snd_card) !=
  1308. SNDRV_CTL_POWER_D0)
  1309. return;
  1310. if (atomic_read(&hdev->in_pm))
  1311. return;
  1312. list_for_each_entry(pin, &hdmi->pin_list, head) {
  1313. if (pin->nid != pin_nid)
  1314. continue;
  1315. /* In case of non MST pin, pipe is -1 */
  1316. if (pipe == -1) {
  1317. pin->mst_capable = false;
  1318. /* if not MST, default is port[0] */
  1319. hport = &pin->ports[0];
  1320. } else {
  1321. for (i = 0; i < pin->num_ports; i++) {
  1322. pin->mst_capable = true;
  1323. if (pin->ports[i].id == pipe) {
  1324. hport = &pin->ports[i];
  1325. break;
  1326. }
  1327. }
  1328. }
  1329. if (hport)
  1330. hdac_hdmi_present_sense(pin, hport);
  1331. }
  1332. }
  1333. static struct drm_audio_component_audio_ops aops = {
  1334. .pin2port = hdac_hdmi_pin2port,
  1335. .pin_eld_notify = hdac_hdmi_eld_notify_cb,
  1336. };
  1337. static struct snd_pcm *hdac_hdmi_get_pcm_from_id(struct snd_soc_card *card,
  1338. int device)
  1339. {
  1340. struct snd_soc_pcm_runtime *rtd;
  1341. for_each_card_rtds(card, rtd) {
  1342. if (rtd->pcm && (rtd->pcm->device == device))
  1343. return rtd->pcm;
  1344. }
  1345. return NULL;
  1346. }
  1347. /* create jack pin kcontrols */
  1348. static int create_fill_jack_kcontrols(struct snd_soc_card *card,
  1349. struct hdac_device *hdev)
  1350. {
  1351. struct hdac_hdmi_pin *pin;
  1352. struct snd_kcontrol_new *kc;
  1353. char kc_name[NAME_SIZE], xname[NAME_SIZE];
  1354. char *name;
  1355. int i = 0, j;
  1356. struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
  1357. struct snd_soc_component *component = hdmi->component;
  1358. kc = devm_kcalloc(component->dev, hdmi->num_ports,
  1359. sizeof(*kc), GFP_KERNEL);
  1360. if (!kc)
  1361. return -ENOMEM;
  1362. list_for_each_entry(pin, &hdmi->pin_list, head) {
  1363. for (j = 0; j < pin->num_ports; j++) {
  1364. snprintf(xname, sizeof(xname), "hif%d-%d Jack",
  1365. pin->nid, pin->ports[j].id);
  1366. name = devm_kstrdup(component->dev, xname, GFP_KERNEL);
  1367. if (!name)
  1368. return -ENOMEM;
  1369. snprintf(kc_name, sizeof(kc_name), "%s Switch", xname);
  1370. kc[i].name = devm_kstrdup(component->dev, kc_name,
  1371. GFP_KERNEL);
  1372. if (!kc[i].name)
  1373. return -ENOMEM;
  1374. kc[i].private_value = (unsigned long)name;
  1375. kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  1376. kc[i].access = 0;
  1377. kc[i].info = snd_soc_dapm_info_pin_switch;
  1378. kc[i].put = snd_soc_dapm_put_pin_switch;
  1379. kc[i].get = snd_soc_dapm_get_pin_switch;
  1380. i++;
  1381. }
  1382. }
  1383. return snd_soc_add_card_controls(card, kc, i);
  1384. }
  1385. int hdac_hdmi_jack_port_init(struct snd_soc_component *component,
  1386. struct snd_soc_dapm_context *dapm)
  1387. {
  1388. struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component);
  1389. struct hdac_device *hdev = hdmi->hdev;
  1390. struct hdac_hdmi_pin *pin;
  1391. struct snd_soc_dapm_widget *widgets;
  1392. struct snd_soc_dapm_route *route;
  1393. char w_name[NAME_SIZE];
  1394. int i = 0, j, ret;
  1395. widgets = devm_kcalloc(dapm->dev, hdmi->num_ports,
  1396. sizeof(*widgets), GFP_KERNEL);
  1397. if (!widgets)
  1398. return -ENOMEM;
  1399. route = devm_kcalloc(dapm->dev, hdmi->num_ports,
  1400. sizeof(*route), GFP_KERNEL);
  1401. if (!route)
  1402. return -ENOMEM;
  1403. /* create Jack DAPM widget */
  1404. list_for_each_entry(pin, &hdmi->pin_list, head) {
  1405. for (j = 0; j < pin->num_ports; j++) {
  1406. snprintf(w_name, sizeof(w_name), "hif%d-%d Jack",
  1407. pin->nid, pin->ports[j].id);
  1408. ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
  1409. snd_soc_dapm_spk, NULL,
  1410. w_name, NULL, NULL, 0, NULL, 0);
  1411. if (ret < 0)
  1412. return ret;
  1413. pin->ports[j].jack_pin = widgets[i].name;
  1414. pin->ports[j].dapm = dapm;
  1415. /* add to route from Jack widget to output */
  1416. hdac_hdmi_fill_route(&route[i], pin->ports[j].jack_pin,
  1417. NULL, pin->ports[j].output_pin, NULL);
  1418. i++;
  1419. }
  1420. }
  1421. /* Add Route from Jack widget to the output widget */
  1422. ret = snd_soc_dapm_new_controls(dapm, widgets, hdmi->num_ports);
  1423. if (ret < 0)
  1424. return ret;
  1425. ret = snd_soc_dapm_add_routes(dapm, route, hdmi->num_ports);
  1426. if (ret < 0)
  1427. return ret;
  1428. ret = snd_soc_dapm_new_widgets(dapm->card);
  1429. if (ret < 0)
  1430. return ret;
  1431. /* Add Jack Pin switch Kcontrol */
  1432. ret = create_fill_jack_kcontrols(dapm->card, hdev);
  1433. if (ret < 0)
  1434. return ret;
  1435. /* default set the Jack Pin switch to OFF */
  1436. list_for_each_entry(pin, &hdmi->pin_list, head) {
  1437. for (j = 0; j < pin->num_ports; j++)
  1438. snd_soc_dapm_disable_pin(pin->ports[j].dapm,
  1439. pin->ports[j].jack_pin);
  1440. }
  1441. return 0;
  1442. }
  1443. EXPORT_SYMBOL_GPL(hdac_hdmi_jack_port_init);
  1444. int hdac_hdmi_jack_init(struct snd_soc_dai *dai, int device,
  1445. struct snd_soc_jack *jack)
  1446. {
  1447. struct snd_soc_component *component = dai->component;
  1448. struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component);
  1449. struct hdac_device *hdev = hdmi->hdev;
  1450. struct hdac_hdmi_pcm *pcm;
  1451. struct snd_pcm *snd_pcm;
  1452. int err;
  1453. /*
  1454. * this is a new PCM device, create new pcm and
  1455. * add to the pcm list
  1456. */
  1457. pcm = devm_kzalloc(&hdev->dev, sizeof(*pcm), GFP_KERNEL);
  1458. if (!pcm)
  1459. return -ENOMEM;
  1460. pcm->pcm_id = device;
  1461. pcm->cvt = hdmi->dai_map[dai->id].cvt;
  1462. pcm->jack_event = 0;
  1463. pcm->jack = jack;
  1464. mutex_init(&pcm->lock);
  1465. INIT_LIST_HEAD(&pcm->port_list);
  1466. snd_pcm = hdac_hdmi_get_pcm_from_id(dai->component->card, device);
  1467. if (snd_pcm) {
  1468. err = snd_hdac_add_chmap_ctls(snd_pcm, device, &hdmi->chmap);
  1469. if (err < 0) {
  1470. dev_err(&hdev->dev,
  1471. "chmap control add failed with err: %d for pcm: %d\n",
  1472. err, device);
  1473. return err;
  1474. }
  1475. }
  1476. list_add_tail(&pcm->head, &hdmi->pcm_list);
  1477. return 0;
  1478. }
  1479. EXPORT_SYMBOL_GPL(hdac_hdmi_jack_init);
  1480. static void hdac_hdmi_present_sense_all_pins(struct hdac_device *hdev,
  1481. struct hdac_hdmi_priv *hdmi, bool detect_pin_caps)
  1482. {
  1483. int i;
  1484. struct hdac_hdmi_pin *pin;
  1485. list_for_each_entry(pin, &hdmi->pin_list, head) {
  1486. if (detect_pin_caps) {
  1487. if (hdac_hdmi_get_port_len(hdev, pin->nid) == 0)
  1488. pin->mst_capable = false;
  1489. else
  1490. pin->mst_capable = true;
  1491. }
  1492. for (i = 0; i < pin->num_ports; i++) {
  1493. if (!pin->mst_capable && i > 0)
  1494. continue;
  1495. hdac_hdmi_present_sense(pin, &pin->ports[i]);
  1496. }
  1497. }
  1498. }
  1499. static int hdmi_codec_probe(struct snd_soc_component *component)
  1500. {
  1501. struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component);
  1502. struct hdac_device *hdev = hdmi->hdev;
  1503. struct snd_soc_dapm_context *dapm =
  1504. snd_soc_component_get_dapm(component);
  1505. struct hdac_ext_link *hlink = NULL;
  1506. int ret;
  1507. hdmi->component = component;
  1508. /*
  1509. * hold the ref while we probe, also no need to drop the ref on
  1510. * exit, we call pm_runtime_suspend() so that will do for us
  1511. */
  1512. hlink = snd_hdac_ext_bus_get_link(hdev->bus, dev_name(&hdev->dev));
  1513. if (!hlink) {
  1514. dev_err(&hdev->dev, "hdac link not found\n");
  1515. return -EIO;
  1516. }
  1517. snd_hdac_ext_bus_link_get(hdev->bus, hlink);
  1518. ret = create_fill_widget_route_map(dapm);
  1519. if (ret < 0)
  1520. return ret;
  1521. aops.audio_ptr = hdev;
  1522. ret = snd_hdac_acomp_register_notifier(hdev->bus, &aops);
  1523. if (ret < 0) {
  1524. dev_err(&hdev->dev, "notifier register failed: err: %d\n", ret);
  1525. return ret;
  1526. }
  1527. hdac_hdmi_present_sense_all_pins(hdev, hdmi, true);
  1528. /* Imp: Store the card pointer in hda_codec */
  1529. hdmi->card = dapm->card->snd_card;
  1530. /*
  1531. * Setup a device_link between card device and HDMI codec device.
  1532. * The card device is the consumer and the HDMI codec device is
  1533. * the supplier. With this setting, we can make sure that the audio
  1534. * domain in display power will be always turned on before operating
  1535. * on the HDMI audio codec registers.
  1536. * Let's use the flag DL_FLAG_AUTOREMOVE_CONSUMER. This can make
  1537. * sure the device link is freed when the machine driver is removed.
  1538. */
  1539. device_link_add(component->card->dev, &hdev->dev, DL_FLAG_RPM_ACTIVE |
  1540. DL_FLAG_AUTOREMOVE_CONSUMER);
  1541. /*
  1542. * hdac_device core already sets the state to active and calls
  1543. * get_noresume. So enable runtime and set the device to suspend.
  1544. */
  1545. pm_runtime_enable(&hdev->dev);
  1546. pm_runtime_put(&hdev->dev);
  1547. pm_runtime_suspend(&hdev->dev);
  1548. return 0;
  1549. }
  1550. static void hdmi_codec_remove(struct snd_soc_component *component)
  1551. {
  1552. struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component);
  1553. struct hdac_device *hdev = hdmi->hdev;
  1554. int ret;
  1555. ret = snd_hdac_acomp_register_notifier(hdev->bus, NULL);
  1556. if (ret < 0)
  1557. dev_err(&hdev->dev, "notifier unregister failed: err: %d\n",
  1558. ret);
  1559. pm_runtime_disable(&hdev->dev);
  1560. }
  1561. #ifdef CONFIG_PM_SLEEP
  1562. static int hdmi_codec_resume(struct device *dev)
  1563. {
  1564. struct hdac_device *hdev = dev_to_hdac_dev(dev);
  1565. struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
  1566. int ret;
  1567. ret = pm_runtime_force_resume(dev);
  1568. if (ret < 0)
  1569. return ret;
  1570. /*
  1571. * As the ELD notify callback request is not entertained while the
  1572. * device is in suspend state. Need to manually check detection of
  1573. * all pins here. pin capablity change is not support, so use the
  1574. * already set pin caps.
  1575. *
  1576. * NOTE: this is safe to call even if the codec doesn't actually resume.
  1577. * The pin check involves only with DRM audio component hooks, so it
  1578. * works even if the HD-audio side is still dreaming peacefully.
  1579. */
  1580. hdac_hdmi_present_sense_all_pins(hdev, hdmi, false);
  1581. return 0;
  1582. }
  1583. #else
  1584. #define hdmi_codec_resume NULL
  1585. #endif
  1586. static const struct snd_soc_component_driver hdmi_hda_codec = {
  1587. .probe = hdmi_codec_probe,
  1588. .remove = hdmi_codec_remove,
  1589. .use_pmdown_time = 1,
  1590. .endianness = 1,
  1591. .non_legacy_dai_naming = 1,
  1592. };
  1593. static void hdac_hdmi_get_chmap(struct hdac_device *hdev, int pcm_idx,
  1594. unsigned char *chmap)
  1595. {
  1596. struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
  1597. struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
  1598. memcpy(chmap, pcm->chmap, ARRAY_SIZE(pcm->chmap));
  1599. }
  1600. static void hdac_hdmi_set_chmap(struct hdac_device *hdev, int pcm_idx,
  1601. unsigned char *chmap, int prepared)
  1602. {
  1603. struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
  1604. struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
  1605. struct hdac_hdmi_port *port;
  1606. if (!pcm)
  1607. return;
  1608. if (list_empty(&pcm->port_list))
  1609. return;
  1610. mutex_lock(&pcm->lock);
  1611. pcm->chmap_set = true;
  1612. memcpy(pcm->chmap, chmap, ARRAY_SIZE(pcm->chmap));
  1613. list_for_each_entry(port, &pcm->port_list, head)
  1614. if (prepared)
  1615. hdac_hdmi_setup_audio_infoframe(hdev, pcm, port);
  1616. mutex_unlock(&pcm->lock);
  1617. }
  1618. static bool is_hdac_hdmi_pcm_attached(struct hdac_device *hdev, int pcm_idx)
  1619. {
  1620. struct hdac_hdmi_priv *hdm

Large files files are truncated, but you can click here to view the full file