File Index
Files and directories
Sign in
FILE

hdmi-codec.c

File preview with syntax highlighting.

linux-6.19.14/sound/soc/codecs/hdmi-codec.c C
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * ALSA SoC codec for HDMI encoder drivers
   4 * Copyright (C) 2015 Texas Instruments Incorporated - https://www.ti.com/
   5 * Author: Jyri Sarha <jsarha@ti.com>
   6 */
   7#include <linux/module.h>
   8#include <linux/string.h>
   9#include <sound/core.h>
  10#include <sound/jack.h>
  11#include <sound/pcm.h>
  12#include <sound/pcm_params.h>
  13#include <sound/soc.h>
  14#include <sound/tlv.h>
  15#include <sound/pcm_drm_eld.h>
  16#include <sound/hdmi-codec.h>
  17#include <sound/pcm_iec958.h>
  18
  19#include <drm/drm_crtc.h> /* This is only to get MAX_ELD_BYTES */
  20#include <drm/drm_eld.h>
  21
  22#define HDMI_CODEC_CHMAP_IDX_UNKNOWN  -1
  23
  24/*
  25 * CEA speaker placement for HDMI 1.4:
  26 *
  27 *  FL  FLC   FC   FRC   FR   FRW
  28 *
  29 *                                  LFE
  30 *
  31 *  RL  RLC   RC   RRC   RR
  32 *
  33 *  Speaker placement has to be extended to support HDMI 2.0
  34 */
  35enum hdmi_codec_cea_spk_placement {
  36	FL  = BIT(0),	/* Front Left           */
  37	FC  = BIT(1),	/* Front Center         */
  38	FR  = BIT(2),	/* Front Right          */
  39	FLC = BIT(3),	/* Front Left Center    */
  40	FRC = BIT(4),	/* Front Right Center   */
  41	RL  = BIT(5),	/* Rear Left            */
  42	RC  = BIT(6),	/* Rear Center          */
  43	RR  = BIT(7),	/* Rear Right           */
  44	RLC = BIT(8),	/* Rear Left Center     */
  45	RRC = BIT(9),	/* Rear Right Center    */
  46	LFE = BIT(10),	/* Low Frequency Effect */
  47};
  48
  49/*
  50 * cea Speaker allocation structure
  51 */
  52struct hdmi_codec_cea_spk_alloc {
  53	const int ca_id;
  54	unsigned int n_ch;
  55	unsigned long mask;
  56};
  57
  58/* Channel maps  stereo HDMI */
  59static const struct snd_pcm_chmap_elem hdmi_codec_stereo_chmaps[] = {
  60	{ .channels = 2,
  61	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
  62	{ }
  63};
  64
  65/* Channel maps for multi-channel playbacks, up to 8 n_ch */
  66static const struct snd_pcm_chmap_elem hdmi_codec_8ch_chmaps[] = {
  67	{ .channels = 2, /* CA_ID 0x00 */
  68	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
  69	{ .channels = 4, /* CA_ID 0x01 */
  70	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
  71		   SNDRV_CHMAP_NA } },
  72	{ .channels = 4, /* CA_ID 0x02 */
  73	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_NA,
  74		   SNDRV_CHMAP_FC } },
  75	{ .channels = 4, /* CA_ID 0x03 */
  76	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
  77		   SNDRV_CHMAP_FC } },
  78	{ .channels = 6, /* CA_ID 0x04 */
  79	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_NA,
  80		   SNDRV_CHMAP_NA, SNDRV_CHMAP_RC, SNDRV_CHMAP_NA } },
  81	{ .channels = 6, /* CA_ID 0x05 */
  82	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
  83		   SNDRV_CHMAP_NA, SNDRV_CHMAP_RC, SNDRV_CHMAP_NA } },
  84	{ .channels = 6, /* CA_ID 0x06 */
  85	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_NA,
  86		   SNDRV_CHMAP_FC, SNDRV_CHMAP_RC, SNDRV_CHMAP_NA } },
  87	{ .channels = 6, /* CA_ID 0x07 */
  88	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
  89		   SNDRV_CHMAP_FC, SNDRV_CHMAP_RC, SNDRV_CHMAP_NA } },
  90	{ .channels = 6, /* CA_ID 0x08 */
  91	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_NA,
  92		   SNDRV_CHMAP_NA, SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
  93	{ .channels = 6, /* CA_ID 0x09 */
  94	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
  95		   SNDRV_CHMAP_NA, SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
  96	{ .channels = 6, /* CA_ID 0x0A */
  97	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_NA,
  98		   SNDRV_CHMAP_FC, SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
  99	{ .channels = 6, /* CA_ID 0x0B */
 100	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
 101		   SNDRV_CHMAP_FC, SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
 102	{ .channels = 8, /* CA_ID 0x0C */
 103	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_NA,
 104		   SNDRV_CHMAP_NA, SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
 105		   SNDRV_CHMAP_RC, SNDRV_CHMAP_NA } },
 106	{ .channels = 8, /* CA_ID 0x0D */
 107	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
 108		   SNDRV_CHMAP_NA, SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
 109		   SNDRV_CHMAP_RC, SNDRV_CHMAP_NA } },
 110	{ .channels = 8, /* CA_ID 0x0E */
 111	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_NA,
 112		   SNDRV_CHMAP_FC, SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
 113		   SNDRV_CHMAP_RC, SNDRV_CHMAP_NA } },
 114	{ .channels = 8, /* CA_ID 0x0F */
 115	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
 116		   SNDRV_CHMAP_FC, SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
 117		   SNDRV_CHMAP_RC, SNDRV_CHMAP_NA } },
 118	{ .channels = 8, /* CA_ID 0x10 */
 119	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_NA,
 120		   SNDRV_CHMAP_NA, SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
 121		   SNDRV_CHMAP_RLC, SNDRV_CHMAP_RRC } },
 122	{ .channels = 8, /* CA_ID 0x11 */
 123	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
 124		   SNDRV_CHMAP_NA, SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
 125		   SNDRV_CHMAP_RLC, SNDRV_CHMAP_RRC } },
 126	{ .channels = 8, /* CA_ID 0x12 */
 127	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_NA,
 128		   SNDRV_CHMAP_FC, SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
 129		   SNDRV_CHMAP_RLC, SNDRV_CHMAP_RRC } },
 130	{ .channels = 8, /* CA_ID 0x13 */
 131	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
 132		   SNDRV_CHMAP_FC, SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
 133		   SNDRV_CHMAP_RLC, SNDRV_CHMAP_RRC } },
 134	{ .channels = 8, /* CA_ID 0x14 */
 135	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_NA,
 136		   SNDRV_CHMAP_NA, SNDRV_CHMAP_NA, SNDRV_CHMAP_NA,
 137		   SNDRV_CHMAP_FLC, SNDRV_CHMAP_FRC } },
 138	{ .channels = 8, /* CA_ID 0x15 */
 139	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
 140		   SNDRV_CHMAP_NA, SNDRV_CHMAP_NA, SNDRV_CHMAP_NA,
 141		   SNDRV_CHMAP_FLC, SNDRV_CHMAP_FRC } },
 142	{ .channels = 8, /* CA_ID 0x16 */
 143	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_NA,
 144		   SNDRV_CHMAP_FC, SNDRV_CHMAP_NA, SNDRV_CHMAP_NA,
 145		   SNDRV_CHMAP_FLC, SNDRV_CHMAP_FRC } },
 146	{ .channels = 8, /* CA_ID 0x17 */
 147	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
 148		   SNDRV_CHMAP_FC, SNDRV_CHMAP_NA, SNDRV_CHMAP_NA,
 149		   SNDRV_CHMAP_FLC, SNDRV_CHMAP_FRC } },
 150	{ .channels = 8, /* CA_ID 0x18 */
 151	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_NA,
 152		   SNDRV_CHMAP_NA, SNDRV_CHMAP_NA, SNDRV_CHMAP_NA,
 153		   SNDRV_CHMAP_FLC, SNDRV_CHMAP_FRC } },
 154	{ .channels = 8, /* CA_ID 0x19 */
 155	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
 156		   SNDRV_CHMAP_NA, SNDRV_CHMAP_NA, SNDRV_CHMAP_NA,
 157		   SNDRV_CHMAP_FLC, SNDRV_CHMAP_FRC } },
 158	{ .channels = 8, /* CA_ID 0x1A */
 159	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_NA,
 160		   SNDRV_CHMAP_FC, SNDRV_CHMAP_NA, SNDRV_CHMAP_NA,
 161		   SNDRV_CHMAP_FLC, SNDRV_CHMAP_FRC } },
 162	{ .channels = 8, /* CA_ID 0x1B */
 163	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
 164		   SNDRV_CHMAP_FC, SNDRV_CHMAP_NA, SNDRV_CHMAP_NA,
 165		   SNDRV_CHMAP_FLC, SNDRV_CHMAP_FRC } },
 166	{ .channels = 8, /* CA_ID 0x1C */
 167	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_NA,
 168		   SNDRV_CHMAP_NA, SNDRV_CHMAP_NA, SNDRV_CHMAP_NA,
 169		   SNDRV_CHMAP_FLC, SNDRV_CHMAP_FRC } },
 170	{ .channels = 8, /* CA_ID 0x1D */
 171	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
 172		   SNDRV_CHMAP_NA, SNDRV_CHMAP_NA, SNDRV_CHMAP_NA,
 173		   SNDRV_CHMAP_FLC, SNDRV_CHMAP_FRC } },
 174	{ .channels = 8, /* CA_ID 0x1E */
 175	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_NA,
 176		   SNDRV_CHMAP_FC, SNDRV_CHMAP_NA, SNDRV_CHMAP_NA,
 177		   SNDRV_CHMAP_FLC, SNDRV_CHMAP_FRC } },
 178	{ .channels = 8, /* CA_ID 0x1F */
 179	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
 180		   SNDRV_CHMAP_FC, SNDRV_CHMAP_NA, SNDRV_CHMAP_NA,
 181		   SNDRV_CHMAP_FLC, SNDRV_CHMAP_FRC } },
 182	{ }
 183};
 184
 185/*
 186 * hdmi_codec_channel_alloc: speaker configuration available for CEA
 187 *
 188 * This is an ordered list where ca_id must exist in hdmi_codec_8ch_chmaps
 189 * The preceding ones have better chances to be selected by
 190 * hdmi_codec_get_ch_alloc_table_idx().
 191 */
 192static const struct hdmi_codec_cea_spk_alloc hdmi_codec_channel_alloc[] = {
 193	{ .ca_id = 0x00, .n_ch = 2,
 194	  .mask = FL | FR },
 195	{ .ca_id = 0x03, .n_ch = 4,
 196	  .mask = FL | FR | LFE | FC },
 197	{ .ca_id = 0x02, .n_ch = 4,
 198	  .mask = FL | FR | FC },
 199	{ .ca_id = 0x01, .n_ch = 4,
 200	  .mask = FL | FR | LFE },
 201	{ .ca_id = 0x0b, .n_ch = 6,
 202	  .mask = FL | FR | LFE | FC | RL | RR },
 203	{ .ca_id = 0x0a, .n_ch = 6,
 204	  .mask = FL | FR | FC | RL | RR },
 205	{ .ca_id = 0x09, .n_ch = 6,
 206	  .mask = FL | FR | LFE | RL | RR },
 207	{ .ca_id = 0x08, .n_ch = 6,
 208	  .mask = FL | FR | RL | RR },
 209	{ .ca_id = 0x07, .n_ch = 6,
 210	  .mask = FL | FR | LFE | FC | RC },
 211	{ .ca_id = 0x06, .n_ch = 6,
 212	  .mask = FL | FR | FC | RC },
 213	{ .ca_id = 0x05, .n_ch = 6,
 214	  .mask = FL | FR | LFE | RC },
 215	{ .ca_id = 0x04, .n_ch = 6,
 216	  .mask = FL | FR | RC },
 217	{ .ca_id = 0x13, .n_ch = 8,
 218	  .mask = FL | FR | LFE | FC | RL | RR | RLC | RRC },
 219	{ .ca_id = 0x1f, .n_ch = 8,
 220	  .mask = FL | FR | LFE | FC | RL | RR | FLC | FRC },
 221	{ .ca_id = 0x12, .n_ch = 8,
 222	  .mask = FL | FR | FC | RL | RR | RLC | RRC },
 223	{ .ca_id = 0x1e, .n_ch = 8,
 224	  .mask = FL | FR | FC | RL | RR | FLC | FRC },
 225	{ .ca_id = 0x11, .n_ch = 8,
 226	  .mask = FL | FR | LFE | RL | RR | RLC | RRC },
 227	{ .ca_id = 0x1d, .n_ch = 8,
 228	  .mask = FL | FR | LFE | RL | RR | FLC | FRC },
 229	{ .ca_id = 0x10, .n_ch = 8,
 230	  .mask = FL | FR | RL | RR | RLC | RRC },
 231	{ .ca_id = 0x1c, .n_ch = 8,
 232	  .mask = FL | FR | RL | RR | FLC | FRC },
 233	{ .ca_id = 0x0f, .n_ch = 8,
 234	  .mask = FL | FR | LFE | FC | RL | RR | RC },
 235	{ .ca_id = 0x1b, .n_ch = 8,
 236	  .mask = FL | FR | LFE | RC | FC | FLC | FRC },
 237	{ .ca_id = 0x0e, .n_ch = 8,
 238	  .mask = FL | FR | FC | RL | RR | RC },
 239	{ .ca_id = 0x1a, .n_ch = 8,
 240	  .mask = FL | FR | RC | FC | FLC | FRC },
 241	{ .ca_id = 0x0d, .n_ch = 8,
 242	  .mask = FL | FR | LFE | RL | RR | RC },
 243	{ .ca_id = 0x19, .n_ch = 8,
 244	  .mask = FL | FR | LFE | RC | FLC | FRC },
 245	{ .ca_id = 0x0c, .n_ch = 8,
 246	  .mask = FL | FR | RC | RL | RR },
 247	{ .ca_id = 0x18, .n_ch = 8,
 248	  .mask = FL | FR | RC | FLC | FRC },
 249	{ .ca_id = 0x17, .n_ch = 8,
 250	  .mask = FL | FR | LFE | FC | FLC | FRC },
 251	{ .ca_id = 0x16, .n_ch = 8,
 252	  .mask = FL | FR | FC | FLC | FRC },
 253	{ .ca_id = 0x15, .n_ch = 8,
 254	  .mask = FL | FR | LFE | FLC | FRC },
 255	{ .ca_id = 0x14, .n_ch = 8,
 256	  .mask = FL | FR | FLC | FRC },
 257	{ .ca_id = 0x0b, .n_ch = 8,
 258	  .mask = FL | FR | LFE | FC | RL | RR },
 259	{ .ca_id = 0x0a, .n_ch = 8,
 260	  .mask = FL | FR | FC | RL | RR },
 261	{ .ca_id = 0x09, .n_ch = 8,
 262	  .mask = FL | FR | LFE | RL | RR },
 263	{ .ca_id = 0x08, .n_ch = 8,
 264	  .mask = FL | FR | RL | RR },
 265	{ .ca_id = 0x07, .n_ch = 8,
 266	  .mask = FL | FR | LFE | FC | RC },
 267	{ .ca_id = 0x06, .n_ch = 8,
 268	  .mask = FL | FR | FC | RC },
 269	{ .ca_id = 0x05, .n_ch = 8,
 270	  .mask = FL | FR | LFE | RC },
 271	{ .ca_id = 0x04, .n_ch = 8,
 272	  .mask = FL | FR | RC },
 273	{ .ca_id = 0x03, .n_ch = 8,
 274	  .mask = FL | FR | LFE | FC },
 275	{ .ca_id = 0x02, .n_ch = 8,
 276	  .mask = FL | FR | FC },
 277	{ .ca_id = 0x01, .n_ch = 8,
 278	  .mask = FL | FR | LFE },
 279};
 280
 281struct hdmi_codec_priv {
 282	struct hdmi_codec_pdata hcd;
 283	uint8_t eld[MAX_ELD_BYTES];
 284	struct snd_parsed_hdmi_eld eld_parsed;
 285	struct snd_pcm_chmap *chmap_info;
 286	unsigned int chmap_idx;
 287	struct mutex lock;
 288	bool busy;
 289	struct snd_soc_jack *jack;
 290	unsigned int jack_status;
 291	u8 iec_status[AES_IEC958_STATUS_SIZE];
 292	struct snd_info_entry *proc_entry;
 293};
 294
 295static const struct snd_soc_dapm_widget hdmi_widgets[] = {
 296	SND_SOC_DAPM_OUTPUT("TX"),
 297	SND_SOC_DAPM_OUTPUT("RX"),
 298};
 299
 300enum {
 301	DAI_ID_I2S = 0,
 302	DAI_ID_SPDIF,
 303};
 304
 305static int hdmi_eld_ctl_info(struct snd_kcontrol *kcontrol,
 306			     struct snd_ctl_elem_info *uinfo)
 307{
 308	uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
 309	uinfo->count = sizeof_field(struct hdmi_codec_priv, eld);
 310
 311	return 0;
 312}
 313
 314static int hdmi_eld_ctl_get(struct snd_kcontrol *kcontrol,
 315			    struct snd_ctl_elem_value *ucontrol)
 316{
 317	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
 318	struct hdmi_codec_priv *hcp = snd_soc_component_get_drvdata(component);
 319
 320	memcpy(ucontrol->value.bytes.data, hcp->eld, sizeof(hcp->eld));
 321
 322	return 0;
 323}
 324
 325static unsigned long hdmi_codec_spk_mask_from_alloc(int spk_alloc)
 326{
 327	int i;
 328	static const unsigned long hdmi_codec_eld_spk_alloc_bits[] = {
 329		[0] = FL | FR, [1] = LFE, [2] = FC, [3] = RL | RR,
 330		[4] = RC, [5] = FLC | FRC, [6] = RLC | RRC,
 331	};
 332	unsigned long spk_mask = 0;
 333
 334	for (i = 0; i < ARRAY_SIZE(hdmi_codec_eld_spk_alloc_bits); i++) {
 335		if (spk_alloc & (1 << i))
 336			spk_mask |= hdmi_codec_eld_spk_alloc_bits[i];
 337	}
 338
 339	return spk_mask;
 340}
 341
 342static void hdmi_codec_eld_chmap(struct hdmi_codec_priv *hcp)
 343{
 344	u8 spk_alloc;
 345	unsigned long spk_mask;
 346
 347	spk_alloc = drm_eld_get_spk_alloc(hcp->eld);
 348	spk_mask = hdmi_codec_spk_mask_from_alloc(spk_alloc);
 349
 350	/* Detect if only stereo supported, else return 8 channels mappings */
 351	if ((spk_mask & ~(FL | FR)) && hcp->chmap_info->max_channels > 2)
 352		hcp->chmap_info->chmap = hdmi_codec_8ch_chmaps;
 353	else
 354		hcp->chmap_info->chmap = hdmi_codec_stereo_chmaps;
 355}
 356
 357static int hdmi_codec_get_ch_alloc_table_idx(struct hdmi_codec_priv *hcp,
 358					     unsigned char channels)
 359{
 360	int i;
 361	u8 spk_alloc;
 362	unsigned long spk_mask;
 363	const struct hdmi_codec_cea_spk_alloc *cap = hdmi_codec_channel_alloc;
 364
 365	spk_alloc = drm_eld_get_spk_alloc(hcp->eld);
 366	spk_mask = hdmi_codec_spk_mask_from_alloc(spk_alloc);
 367
 368	for (i = 0; i < ARRAY_SIZE(hdmi_codec_channel_alloc); i++, cap++) {
 369		/* If spk_alloc == 0, HDMI is unplugged return stereo config*/
 370		if (!spk_alloc && cap->ca_id == 0)
 371			return i;
 372		if (cap->n_ch != channels)
 373			continue;
 374		if (!(cap->mask == (spk_mask & cap->mask)))
 375			continue;
 376		return i;
 377	}
 378
 379	return -EINVAL;
 380}
 381static int hdmi_codec_chmap_ctl_get(struct snd_kcontrol *kcontrol,
 382			      struct snd_ctl_elem_value *ucontrol)
 383{
 384	unsigned const char *map;
 385	unsigned int i;
 386	struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
 387	struct hdmi_codec_priv *hcp = info->private_data;
 388
 389	if (hcp->chmap_idx != HDMI_CODEC_CHMAP_IDX_UNKNOWN)
 390		map = info->chmap[hcp->chmap_idx].map;
 391
 392	for (i = 0; i < info->max_channels; i++) {
 393		if (hcp->chmap_idx == HDMI_CODEC_CHMAP_IDX_UNKNOWN)
 394			ucontrol->value.integer.value[i] = 0;
 395		else
 396			ucontrol->value.integer.value[i] = map[i];
 397	}
 398
 399	return 0;
 400}
 401
 402static int hdmi_codec_iec958_info(struct snd_kcontrol *kcontrol,
 403				  struct snd_ctl_elem_info *uinfo)
 404{
 405	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
 406	uinfo->count = 1;
 407	return 0;
 408}
 409
 410static int hdmi_codec_iec958_default_get(struct snd_kcontrol *kcontrol,
 411					 struct snd_ctl_elem_value *ucontrol)
 412{
 413	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
 414	struct hdmi_codec_priv *hcp = snd_soc_component_get_drvdata(component);
 415
 416	memcpy(ucontrol->value.iec958.status, hcp->iec_status,
 417	       sizeof(hcp->iec_status));
 418
 419	return 0;
 420}
 421
 422static int hdmi_codec_iec958_default_put(struct snd_kcontrol *kcontrol,
 423					 struct snd_ctl_elem_value *ucontrol)
 424{
 425	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
 426	struct hdmi_codec_priv *hcp = snd_soc_component_get_drvdata(component);
 427
 428	memcpy(hcp->iec_status, ucontrol->value.iec958.status,
 429	       sizeof(hcp->iec_status));
 430
 431	return 0;
 432}
 433
 434static int hdmi_codec_iec958_mask_get(struct snd_kcontrol *kcontrol,
 435				      struct snd_ctl_elem_value *ucontrol)
 436{
 437	memset(ucontrol->value.iec958.status, 0xff,
 438	       sizeof_field(struct hdmi_codec_priv, iec_status));
 439
 440	return 0;
 441}
 442
 443static int hdmi_codec_startup(struct snd_pcm_substream *substream,
 444			      struct snd_soc_dai *dai)
 445{
 446	struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
 447	bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
 448	bool has_capture = !hcp->hcd.no_i2s_capture;
 449	bool has_playback = !hcp->hcd.no_i2s_playback;
 450	int ret = 0;
 451
 452	if (!((has_playback && tx) || (has_capture && !tx)))
 453		return 0;
 454
 455	mutex_lock(&hcp->lock);
 456	if (hcp->busy) {
 457		dev_err(dai->dev, "Only one simultaneous stream supported!\n");
 458		mutex_unlock(&hcp->lock);
 459		return -EINVAL;
 460	}
 461
 462	if (hcp->hcd.ops->audio_startup) {
 463		ret = hcp->hcd.ops->audio_startup(dai->dev->parent, hcp->hcd.data);
 464		if (ret)
 465			goto err;
 466	}
 467
 468	if (tx && hcp->hcd.ops->get_eld) {
 469		ret = hcp->hcd.ops->get_eld(dai->dev->parent, hcp->hcd.data,
 470					    hcp->eld, sizeof(hcp->eld));
 471		if (ret)
 472			goto err;
 473
 474		snd_parse_eld(dai->dev, &hcp->eld_parsed,
 475			      hcp->eld, sizeof(hcp->eld));
 476
 477		ret = snd_pcm_hw_constraint_eld(substream->runtime, hcp->eld);
 478		if (ret)
 479			goto err;
 480
 481		/* Select chmap supported */
 482		hdmi_codec_eld_chmap(hcp);
 483	}
 484
 485	hcp->busy = true;
 486
 487err:
 488	mutex_unlock(&hcp->lock);
 489	return ret;
 490}
 491
 492static void hdmi_codec_shutdown(struct snd_pcm_substream *substream,
 493				struct snd_soc_dai *dai)
 494{
 495	struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
 496	bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
 497	bool has_capture = !hcp->hcd.no_i2s_capture;
 498	bool has_playback = !hcp->hcd.no_i2s_playback;
 499
 500	if (!((has_playback && tx) || (has_capture && !tx)))
 501		return;
 502
 503	hcp->chmap_idx = HDMI_CODEC_CHMAP_IDX_UNKNOWN;
 504	hcp->hcd.ops->audio_shutdown(dai->dev->parent, hcp->hcd.data);
 505
 506	mutex_lock(&hcp->lock);
 507	hcp->busy = false;
 508	mutex_unlock(&hcp->lock);
 509}
 510
 511static int hdmi_codec_fill_codec_params(struct snd_soc_dai *dai,
 512					unsigned int sample_width,
 513					unsigned int sample_rate,
 514					unsigned int channels,
 515					struct hdmi_codec_params *hp)
 516{
 517	struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
 518	int idx = HDMI_CODEC_CHMAP_IDX_UNKNOWN;
 519	u8 ca_id = 0;
 520	bool pcm_audio = !(hcp->iec_status[0] & IEC958_AES0_NONAUDIO);
 521
 522	if (pcm_audio) {
 523		/* Select a channel allocation that matches with ELD and pcm channels */
 524		idx = hdmi_codec_get_ch_alloc_table_idx(hcp, channels);
 525
 526		if (idx < 0) {
 527			dev_err(dai->dev, "Not able to map channels to speakers (%d)\n",
 528				idx);
 529			hcp->chmap_idx = HDMI_CODEC_CHMAP_IDX_UNKNOWN;
 530			return idx;
 531		}
 532
 533		ca_id = hdmi_codec_channel_alloc[idx].ca_id;
 534	}
 535
 536	memset(hp, 0, sizeof(*hp));
 537
 538	hdmi_audio_infoframe_init(&hp->cea);
 539
 540	if (pcm_audio)
 541		hp->cea.channels = channels;
 542	else
 543		hp->cea.channels = 0;
 544
 545	hp->cea.coding_type = HDMI_AUDIO_CODING_TYPE_STREAM;
 546	hp->cea.sample_size = HDMI_AUDIO_SAMPLE_SIZE_STREAM;
 547	hp->cea.sample_frequency = HDMI_AUDIO_SAMPLE_FREQUENCY_STREAM;
 548	hp->cea.channel_allocation = ca_id;
 549
 550	hp->sample_width = sample_width;
 551	hp->sample_rate = sample_rate;
 552	hp->channels = channels;
 553
 554	if (pcm_audio)
 555		hcp->chmap_idx = ca_id;
 556	else
 557		hcp->chmap_idx = HDMI_CODEC_CHMAP_IDX_UNKNOWN;
 558
 559	return 0;
 560}
 561
 562static int hdmi_codec_hw_params(struct snd_pcm_substream *substream,
 563				struct snd_pcm_hw_params *params,
 564				struct snd_soc_dai *dai)
 565{
 566	struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
 567	struct hdmi_codec_daifmt *cf = snd_soc_dai_dma_data_get_playback(dai);
 568	struct hdmi_codec_params hp = {
 569		.iec = {
 570			.status = { 0 },
 571			.subcode = { 0 },
 572			.pad = 0,
 573			.dig_subframe = { 0 },
 574		}
 575	};
 576	int ret;
 577
 578	if (!hcp->hcd.ops->hw_params)
 579		return 0;
 580
 581	dev_dbg(dai->dev, "%s() width %d rate %d channels %d\n", __func__,
 582		params_width(params), params_rate(params),
 583		params_channels(params));
 584
 585	ret = hdmi_codec_fill_codec_params(dai,
 586					   params_width(params),
 587					   params_rate(params),
 588					   params_channels(params),
 589					   &hp);
 590	if (ret < 0)
 591		return ret;
 592
 593	memcpy(hp.iec.status, hcp->iec_status, sizeof(hp.iec.status));
 594	ret = snd_pcm_fill_iec958_consumer_hw_params(params, hp.iec.status,
 595						     sizeof(hp.iec.status));
 596	if (ret < 0) {
 597		dev_err(dai->dev, "Creating IEC958 channel status failed %d\n",
 598			ret);
 599		return ret;
 600	}
 601
 602	cf->bit_fmt = params_format(params);
 603	return hcp->hcd.ops->hw_params(dai->dev->parent, hcp->hcd.data,
 604				       cf, &hp);
 605}
 606
 607static int hdmi_codec_prepare(struct snd_pcm_substream *substream,
 608			      struct snd_soc_dai *dai)
 609{
 610	struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
 611	struct hdmi_codec_daifmt *cf = snd_soc_dai_dma_data_get_playback(dai);
 612	struct snd_pcm_runtime *runtime = substream->runtime;
 613	unsigned int channels = runtime->channels;
 614	unsigned int width = snd_pcm_format_width(runtime->format);
 615	unsigned int rate = runtime->rate;
 616	struct hdmi_codec_params hp;
 617	int ret;
 618
 619	if (!hcp->hcd.ops->prepare)
 620		return 0;
 621
 622	dev_dbg(dai->dev, "%s() width %d rate %d channels %d\n", __func__,
 623		width, rate, channels);
 624
 625	ret = hdmi_codec_fill_codec_params(dai, width, rate, channels, &hp);
 626	if (ret < 0)
 627		return ret;
 628
 629	memcpy(hp.iec.status, hcp->iec_status, sizeof(hp.iec.status));
 630	ret = snd_pcm_fill_iec958_consumer(runtime, hp.iec.status,
 631					   sizeof(hp.iec.status));
 632	if (ret < 0) {
 633		dev_err(dai->dev, "Creating IEC958 channel status failed %d\n",
 634			ret);
 635		return ret;
 636	}
 637
 638	cf->bit_fmt = runtime->format;
 639	return hcp->hcd.ops->prepare(dai->dev->parent, hcp->hcd.data,
 640				     cf, &hp);
 641}
 642
 643static int hdmi_codec_i2s_set_fmt(struct snd_soc_dai *dai,
 644				  unsigned int fmt)
 645{
 646	struct hdmi_codec_daifmt *cf = snd_soc_dai_dma_data_get_playback(dai);
 647
 648	/* Reset daifmt */
 649	memset(cf, 0, sizeof(*cf));
 650
 651	switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
 652	case SND_SOC_DAIFMT_CBP_CFP:
 653		cf->bit_clk_provider = 1;
 654		cf->frame_clk_provider = 1;
 655		break;
 656	case SND_SOC_DAIFMT_CBC_CFP:
 657		cf->frame_clk_provider = 1;
 658		break;
 659	case SND_SOC_DAIFMT_CBP_CFC:
 660		cf->bit_clk_provider = 1;
 661		break;
 662	case SND_SOC_DAIFMT_CBC_CFC:
 663		break;
 664	default:
 665		return -EINVAL;
 666	}
 667
 668	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
 669	case SND_SOC_DAIFMT_NB_NF:
 670		break;
 671	case SND_SOC_DAIFMT_NB_IF:
 672		cf->frame_clk_inv = 1;
 673		break;
 674	case SND_SOC_DAIFMT_IB_NF:
 675		cf->bit_clk_inv = 1;
 676		break;
 677	case SND_SOC_DAIFMT_IB_IF:
 678		cf->frame_clk_inv = 1;
 679		cf->bit_clk_inv = 1;
 680		break;
 681	}
 682
 683	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
 684	case SND_SOC_DAIFMT_I2S:
 685		cf->fmt = HDMI_I2S;
 686		break;
 687	case SND_SOC_DAIFMT_DSP_A:
 688		cf->fmt = HDMI_DSP_A;
 689		break;
 690	case SND_SOC_DAIFMT_DSP_B:
 691		cf->fmt = HDMI_DSP_B;
 692		break;
 693	case SND_SOC_DAIFMT_RIGHT_J:
 694		cf->fmt = HDMI_RIGHT_J;
 695		break;
 696	case SND_SOC_DAIFMT_LEFT_J:
 697		cf->fmt = HDMI_LEFT_J;
 698		break;
 699	case SND_SOC_DAIFMT_AC97:
 700		cf->fmt = HDMI_AC97;
 701		break;
 702	default:
 703		dev_err(dai->dev, "Invalid DAI interface format\n");
 704		return -EINVAL;
 705	}
 706
 707	return 0;
 708}
 709
 710static int hdmi_codec_mute(struct snd_soc_dai *dai, int mute, int direction)
 711{
 712	struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
 713
 714	/*
 715	 * ignore if direction was CAPTURE
 716	 * and it had .no_capture_mute flag
 717	 * see
 718	 *	snd_soc_dai_digital_mute()
 719	 */
 720	if (hcp->hcd.ops->mute_stream &&
 721	    (direction == SNDRV_PCM_STREAM_PLAYBACK ||
 722	     !hcp->hcd.no_capture_mute))
 723		return hcp->hcd.ops->mute_stream(dai->dev->parent,
 724						 hcp->hcd.data,
 725						 mute, direction);
 726
 727	return -ENOTSUPP;
 728}
 729
 730/*
 731 * This driver can select all SND_SOC_DAIFMT_CBx_CFx,
 732 * but need to be selected from Sound Card, not be auto selected.
 733 * Because it might be used from other driver.
 734 * For example,
 735 *	${LINUX}/drivers/gpu/drm/bridge/synopsys/dw-hdmi-i2s-audio.c
 736 */
 737static const u64 hdmi_codec_formats =
 738	SND_SOC_POSSIBLE_DAIFMT_NB_NF	|
 739	SND_SOC_POSSIBLE_DAIFMT_NB_IF	|
 740	SND_SOC_POSSIBLE_DAIFMT_IB_NF	|
 741	SND_SOC_POSSIBLE_DAIFMT_IB_IF	|
 742	SND_SOC_POSSIBLE_DAIFMT_I2S	|
 743	SND_SOC_POSSIBLE_DAIFMT_DSP_A	|
 744	SND_SOC_POSSIBLE_DAIFMT_DSP_B	|
 745	SND_SOC_POSSIBLE_DAIFMT_RIGHT_J	|
 746	SND_SOC_POSSIBLE_DAIFMT_LEFT_J	|
 747	SND_SOC_POSSIBLE_DAIFMT_AC97;
 748
 749#define HDMI_RATES	(SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |\
 750			 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |\
 751			 SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |\
 752			 SNDRV_PCM_RATE_192000)
 753
 754#define SPDIF_FORMATS	(SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
 755			 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_LE)
 756
 757/*
 758 * This list is only for formats allowed on the I2S bus. So there is
 759 * some formats listed that are not supported by HDMI interface. For
 760 * instance allowing the 32-bit formats enables 24-precision with CPU
 761 * DAIs that do not support 24-bit formats. If the extra formats cause
 762 * problems, we should add the video side driver an option to disable
 763 * them.
 764 */
 765#define I2S_FORMATS	(SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
 766			 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_LE |\
 767			 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE)
 768
 769static struct snd_kcontrol_new hdmi_codec_controls[] = {
 770	{
 771		.access = SNDRV_CTL_ELEM_ACCESS_READ,
 772		.iface = SNDRV_CTL_ELEM_IFACE_PCM,
 773		.name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, MASK),
 774		.info = hdmi_codec_iec958_info,
 775		.get = hdmi_codec_iec958_mask_get,
 776	},
 777	{
 778		.iface = SNDRV_CTL_ELEM_IFACE_PCM,
 779		.name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
 780		.info = hdmi_codec_iec958_info,
 781		.get = hdmi_codec_iec958_default_get,
 782		.put = hdmi_codec_iec958_default_put,
 783	},
 784	{
 785		.access	= (SNDRV_CTL_ELEM_ACCESS_READ |
 786			   SNDRV_CTL_ELEM_ACCESS_VOLATILE),
 787		.iface	= SNDRV_CTL_ELEM_IFACE_PCM,
 788		.name	= "ELD",
 789		.info	= hdmi_eld_ctl_info,
 790		.get	= hdmi_eld_ctl_get,
 791	},
 792};
 793
 794static int hdmi_codec_pcm_new(struct snd_soc_pcm_runtime *rtd,
 795			      struct snd_soc_dai *dai)
 796{
 797	struct snd_soc_dai_driver *drv = dai->driver;
 798	struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
 799	unsigned int i;
 800	int ret;
 801
 802	ret =  snd_pcm_add_chmap_ctls(rtd->pcm, SNDRV_PCM_STREAM_PLAYBACK,
 803				      NULL, drv->playback.channels_max, 0,
 804				      &hcp->chmap_info);
 805	if (ret < 0)
 806		return ret;
 807
 808	/* override handlers */
 809	hcp->chmap_info->private_data = hcp;
 810	hcp->chmap_info->kctl->get = hdmi_codec_chmap_ctl_get;
 811
 812	/* default chmap supported is stereo */
 813	hcp->chmap_info->chmap = hdmi_codec_stereo_chmaps;
 814	hcp->chmap_idx = HDMI_CODEC_CHMAP_IDX_UNKNOWN;
 815
 816	for (i = 0; i < ARRAY_SIZE(hdmi_codec_controls); i++) {
 817		struct snd_kcontrol *kctl;
 818
 819		/* add ELD ctl with the device number corresponding to the PCM stream */
 820		kctl = snd_ctl_new1(&hdmi_codec_controls[i], dai->component);
 821		if (!kctl)
 822			return -ENOMEM;
 823
 824		kctl->id.device = rtd->pcm->device;
 825		ret = snd_ctl_add(rtd->card->snd_card, kctl);
 826		if (ret < 0)
 827			return ret;
 828	}
 829
 830	return 0;
 831}
 832
 833#ifdef CONFIG_SND_PROC_FS
 834static void print_eld_info(struct snd_info_entry *entry,
 835			   struct snd_info_buffer *buffer)
 836{
 837	struct hdmi_codec_priv *hcp = entry->private_data;
 838
 839	snd_print_eld_info(&hcp->eld_parsed, buffer);
 840}
 841
 842static int hdmi_dai_proc_new(struct hdmi_codec_priv *hcp,
 843			     struct snd_soc_dai *dai)
 844{
 845	struct snd_soc_component *component = dai->component;
 846	struct snd_soc_card *card = component->card;
 847	struct snd_soc_dai *d;
 848	struct snd_soc_pcm_runtime *rtd;
 849	struct snd_info_entry *entry;
 850	char name[32];
 851	int err, i, id = 0;
 852
 853	/*
 854	 * To avoid duplicate proc entry, find its rtd and use rtd->id
 855	 * instead of dai->id
 856	 */
 857	for_each_card_rtds(card, rtd) {
 858		for_each_rtd_dais(rtd, i, d)
 859			if (d == dai) {
 860				id = rtd->id;
 861				goto found;
 862			}
 863	}
 864found:
 865	snprintf(name, sizeof(name), "eld#%d", id);
 866	err = snd_card_proc_new(card->snd_card, name, &entry);
 867	if (err < 0)
 868		return err;
 869
 870	snd_info_set_text_ops(entry, hcp, print_eld_info);
 871	hcp->proc_entry = entry;
 872
 873	return 0;
 874}
 875
 876static void hdmi_dai_proc_free(struct hdmi_codec_priv *hcp)
 877{
 878	snd_info_free_entry(hcp->proc_entry);
 879	hcp->proc_entry = NULL;
 880}
 881#else
 882static int hdmi_dai_proc_new(struct hdmi_codec_priv *hcp,
 883			     struct snd_soc_dai *dai)
 884{
 885	return 0;
 886}
 887
 888static void hdmi_dai_proc_free(struct hdmi_codec_priv *hcp)
 889{
 890}
 891#endif
 892
 893static int hdmi_dai_probe(struct snd_soc_dai *dai)
 894{
 895	struct hdmi_codec_priv *hcp =
 896		snd_soc_component_get_drvdata(dai->component);
 897	struct snd_soc_dapm_context *dapm;
 898	struct hdmi_codec_daifmt *daifmt;
 899	struct snd_soc_dapm_route route[] = {
 900		{
 901			.sink = "TX",
 902			.source = dai->driver->playback.stream_name,
 903		},
 904		{
 905			.sink = dai->driver->capture.stream_name,
 906			.source = "RX",
 907		},
 908	};
 909	int ret, i;
 910
 911	dapm = snd_soc_component_to_dapm(dai->component);
 912
 913	/* One of the directions might be omitted for unidirectional DAIs */
 914	for (i = 0; i < ARRAY_SIZE(route); i++) {
 915		if (!route[i].source || !route[i].sink)
 916			continue;
 917
 918		ret = snd_soc_dapm_add_routes(dapm, &route[i], 1);
 919		if (ret)
 920			return ret;
 921	}
 922
 923	daifmt = devm_kzalloc(dai->dev, sizeof(*daifmt), GFP_KERNEL);
 924	if (!daifmt)
 925		return -ENOMEM;
 926
 927	snd_soc_dai_dma_data_set_playback(dai, daifmt);
 928
 929	return hdmi_dai_proc_new(hcp, dai);
 930}
 931
 932static int hdmi_dai_remove(struct snd_soc_dai *dai)
 933{
 934	struct hdmi_codec_priv *hcp =
 935		snd_soc_component_get_drvdata(dai->component);
 936
 937	hdmi_dai_proc_free(hcp);
 938	return 0;
 939}
 940
 941static void hdmi_codec_jack_report(struct hdmi_codec_priv *hcp,
 942				   unsigned int jack_status)
 943{
 944	if (jack_status != hcp->jack_status) {
 945		if (hcp->jack)
 946			snd_soc_jack_report(hcp->jack, jack_status, SND_JACK_AVOUT);
 947		hcp->jack_status = jack_status;
 948	}
 949}
 950
 951static void plugged_cb(struct device *dev, bool plugged)
 952{
 953	struct hdmi_codec_priv *hcp = dev_get_drvdata(dev);
 954	int ret;
 955
 956	if (plugged) {
 957		if (hcp->hcd.ops->get_eld) {
 958			hcp->hcd.ops->get_eld(dev->parent, hcp->hcd.data,
 959					    hcp->eld, sizeof(hcp->eld));
 960			ret = snd_parse_eld(dev, &hcp->eld_parsed,
 961					    hcp->eld, sizeof(hcp->eld));
 962			if (ret < 0)
 963				dev_dbg(dev, "Failed to parse ELD: %d\n", ret);
 964			else
 965				snd_show_eld(dev, &hcp->eld_parsed);
 966		}
 967		hdmi_codec_jack_report(hcp, SND_JACK_AVOUT);
 968	} else {
 969		hdmi_codec_jack_report(hcp, 0);
 970		memset(hcp->eld, 0, sizeof(hcp->eld));
 971	}
 972}
 973
 974static int hdmi_codec_set_jack(struct snd_soc_component *component,
 975			       struct snd_soc_jack *jack,
 976			       void *data)
 977{
 978	struct hdmi_codec_priv *hcp = snd_soc_component_get_drvdata(component);
 979
 980	if (hcp->hcd.ops->hook_plugged_cb) {
 981		hcp->jack = jack;
 982
 983		/*
 984		 * Report the initial jack status which may have been provided
 985		 * by the parent hdmi driver while the hpd hook was registered.
 986		 */
 987		snd_soc_jack_report(jack, hcp->jack_status, SND_JACK_AVOUT);
 988
 989		return 0;
 990	}
 991
 992	return -ENOTSUPP;
 993}
 994
 995static int hdmi_dai_spdif_probe(struct snd_soc_dai *dai)
 996{
 997	struct hdmi_codec_daifmt *cf;
 998	int ret;
 999
1000	ret = hdmi_dai_probe(dai);
1001	if (ret)
1002		return ret;
1003
1004	cf = snd_soc_dai_dma_data_get_playback(dai);
1005	cf->fmt = HDMI_SPDIF;
1006
1007	return 0;
1008}
1009
1010static const struct snd_soc_dai_ops hdmi_codec_i2s_dai_ops = {
1011	.probe				= hdmi_dai_probe,
1012	.remove				= hdmi_dai_remove,
1013	.startup			= hdmi_codec_startup,
1014	.shutdown			= hdmi_codec_shutdown,
1015	.hw_params			= hdmi_codec_hw_params,
1016	.prepare			= hdmi_codec_prepare,
1017	.set_fmt			= hdmi_codec_i2s_set_fmt,
1018	.mute_stream			= hdmi_codec_mute,
1019	.pcm_new			= hdmi_codec_pcm_new,
1020	.auto_selectable_formats	= &hdmi_codec_formats,
1021	.num_auto_selectable_formats	= 1,
1022};
1023
1024static const struct snd_soc_dai_ops hdmi_codec_spdif_dai_ops = {
1025	.probe		= hdmi_dai_spdif_probe,
1026	.startup	= hdmi_codec_startup,
1027	.shutdown	= hdmi_codec_shutdown,
1028	.hw_params	= hdmi_codec_hw_params,
1029	.prepare	= hdmi_codec_prepare,
1030	.mute_stream	= hdmi_codec_mute,
1031	.pcm_new	= hdmi_codec_pcm_new,
1032};
1033
1034static const struct snd_soc_dai_driver hdmi_i2s_dai = {
1035	.name = "i2s-hifi",
1036	.id = DAI_ID_I2S,
1037	.playback = {
1038		.stream_name = "I2S Playback",
1039		.channels_min = 2,
1040		.channels_max = 8,
1041		.rates = HDMI_RATES,
1042		.formats = I2S_FORMATS,
1043		.sig_bits = 24,
1044	},
1045	.capture = {
1046		.stream_name = "Capture",
1047		.channels_min = 2,
1048		.channels_max = 8,
1049		.rates = HDMI_RATES,
1050		.formats = I2S_FORMATS,
1051		.sig_bits = 24,
1052	},
1053	.ops = &hdmi_codec_i2s_dai_ops,
1054};
1055
1056static const struct snd_soc_dai_driver hdmi_spdif_dai = {
1057	.name = "spdif-hifi",
1058	.id = DAI_ID_SPDIF,
1059	.playback = {
1060		.stream_name = "SPDIF Playback",
1061		.channels_min = 2,
1062		.channels_max = 2,
1063		.rates = HDMI_RATES,
1064		.formats = SPDIF_FORMATS,
1065	},
1066	.capture = {
1067		.stream_name = "Capture",
1068		.channels_min = 2,
1069		.channels_max = 2,
1070		.rates = HDMI_RATES,
1071		.formats = SPDIF_FORMATS,
1072	},
1073	.ops = &hdmi_codec_spdif_dai_ops,
1074};
1075
1076static int hdmi_of_xlate_dai_id(struct snd_soc_component *component,
1077				 struct device_node *endpoint)
1078{
1079	struct hdmi_codec_priv *hcp = snd_soc_component_get_drvdata(component);
1080	int ret = -ENOTSUPP; /* see snd_soc_get_dai_id() */
1081
1082	if (hcp->hcd.ops->get_dai_id)
1083		ret = hcp->hcd.ops->get_dai_id(component, endpoint, hcp->hcd.data);
1084
1085	return ret;
1086}
1087
1088static int hdmi_probe(struct snd_soc_component *component)
1089{
1090	struct hdmi_codec_priv *hcp = snd_soc_component_get_drvdata(component);
1091	int ret = 0;
1092
1093	if (hcp->hcd.ops->hook_plugged_cb) {
1094		ret = hcp->hcd.ops->hook_plugged_cb(component->dev->parent,
1095						    hcp->hcd.data,
1096						    plugged_cb,
1097						    component->dev);
1098	}
1099
1100	return ret;
1101}
1102
1103static void hdmi_remove(struct snd_soc_component *component)
1104{
1105	struct hdmi_codec_priv *hcp = snd_soc_component_get_drvdata(component);
1106
1107	if (hcp->hcd.ops->hook_plugged_cb)
1108		hcp->hcd.ops->hook_plugged_cb(component->dev->parent,
1109					      hcp->hcd.data, NULL, NULL);
1110}
1111
1112static const struct snd_soc_component_driver hdmi_driver = {
1113	.probe			= hdmi_probe,
1114	.remove			= hdmi_remove,
1115	.dapm_widgets		= hdmi_widgets,
1116	.num_dapm_widgets	= ARRAY_SIZE(hdmi_widgets),
1117	.of_xlate_dai_id	= hdmi_of_xlate_dai_id,
1118	.idle_bias_on		= 1,
1119	.use_pmdown_time	= 1,
1120	.endianness		= 1,
1121	.set_jack		= hdmi_codec_set_jack,
1122};
1123
1124static int hdmi_codec_probe(struct platform_device *pdev)
1125{
1126	struct hdmi_codec_pdata *hcd = pdev->dev.platform_data;
1127	struct snd_soc_dai_driver *daidrv;
1128	struct device *dev = &pdev->dev;
1129	struct hdmi_codec_priv *hcp;
1130	int dai_count, i = 0;
1131	int ret;
1132
1133	if (!hcd) {
1134		dev_err(dev, "%s: No platform data\n", __func__);
1135		return -EINVAL;
1136	}
1137
1138	dai_count = hcd->i2s + hcd->spdif;
1139	if (dai_count < 1 || !hcd->ops ||
1140	    (!hcd->ops->hw_params && !hcd->ops->prepare) ||
1141	    !hcd->ops->audio_shutdown) {
1142		dev_err(dev, "%s: Invalid parameters\n", __func__);
1143		return -EINVAL;
1144	}
1145
1146	hcp = devm_kzalloc(dev, sizeof(*hcp), GFP_KERNEL);
1147	if (!hcp)
1148		return -ENOMEM;
1149
1150	hcp->hcd = *hcd;
1151	mutex_init(&hcp->lock);
1152
1153	ret = snd_pcm_create_iec958_consumer_default(hcp->iec_status,
1154						     sizeof(hcp->iec_status));
1155	if (ret < 0)
1156		return ret;
1157
1158	daidrv = devm_kcalloc(dev, dai_count, sizeof(*daidrv), GFP_KERNEL);
1159	if (!daidrv)
1160		return -ENOMEM;
1161
1162	if (hcd->i2s) {
1163		daidrv[i] = hdmi_i2s_dai;
1164		daidrv[i].playback.channels_max = hcd->max_i2s_channels;
1165		if (hcd->i2s_formats) {
1166			daidrv[i].playback.formats = hcd->i2s_formats;
1167			daidrv[i].capture.formats = hcd->i2s_formats;
1168		}
1169		if (hcd->no_i2s_playback)
1170			memset(&daidrv[i].playback, 0,
1171			       sizeof(daidrv[i].playback));
1172		if (hcd->no_i2s_capture)
1173			memset(&daidrv[i].capture, 0,
1174			       sizeof(daidrv[i].capture));
1175		i++;
1176	}
1177
1178	if (hcd->spdif) {
1179		daidrv[i] = hdmi_spdif_dai;
1180		if (hcd->no_spdif_playback)
1181			memset(&daidrv[i].playback, 0,
1182			       sizeof(daidrv[i].playback));
1183		if (hcd->no_spdif_capture)
1184			memset(&daidrv[i].capture, 0,
1185			       sizeof(daidrv[i].capture));
1186	}
1187
1188	dev_set_drvdata(dev, hcp);
1189
1190	ret = devm_snd_soc_register_component(dev, &hdmi_driver, daidrv,
1191					      dai_count);
1192	if (ret) {
1193		dev_err(dev, "%s: snd_soc_register_component() failed (%d)\n",
1194			__func__, ret);
1195		return ret;
1196	}
1197	return 0;
1198}
1199
1200static struct platform_driver hdmi_codec_driver = {
1201	.driver = {
1202		.name = HDMI_CODEC_DRV_NAME,
1203	},
1204	.probe = hdmi_codec_probe,
1205};
1206
1207module_platform_driver(hdmi_codec_driver);
1208
1209MODULE_AUTHOR("Jyri Sarha <jsarha@ti.com>");
1210MODULE_DESCRIPTION("HDMI Audio Codec Driver");
1211MODULE_LICENSE("GPL");
1212MODULE_ALIAS("platform:" HDMI_CODEC_DRV_NAME);
~/ Read-only file preview.