PageRenderTime 52ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/sound/soc/codecs/hdmi-codec.c

https://codeberg.org/ddevault/linux
C | 853 lines | 695 code | 98 blank | 60 comment | 48 complexity | 4a4f14ce1cb56cd106f11643d712d1b1 MD5 | raw file
Possible License(s): GPL-2.0
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * ALSA SoC codec for HDMI encoder drivers
  4. * Copyright (C) 2015 Texas Instruments Incorporated - http://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. #include <drm/drm_crtc.h> /* This is only to get MAX_ELD_BYTES */
  19. #define HDMI_CODEC_CHMAP_IDX_UNKNOWN -1
  20. struct hdmi_codec_channel_map_table {
  21. unsigned char map; /* ALSA API channel map position */
  22. unsigned long spk_mask; /* speaker position bit mask */
  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. */
  35. enum 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. * cea Speaker allocation structure
  50. */
  51. struct hdmi_codec_cea_spk_alloc {
  52. const int ca_id;
  53. unsigned int n_ch;
  54. unsigned long mask;
  55. };
  56. /* Channel maps stereo HDMI */
  57. static const struct snd_pcm_chmap_elem hdmi_codec_stereo_chmaps[] = {
  58. { .channels = 2,
  59. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
  60. { }
  61. };
  62. /* Channel maps for multi-channel playbacks, up to 8 n_ch */
  63. static const struct snd_pcm_chmap_elem hdmi_codec_8ch_chmaps[] = {
  64. { .channels = 2, /* CA_ID 0x00 */
  65. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
  66. { .channels = 4, /* CA_ID 0x01 */
  67. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
  68. SNDRV_CHMAP_NA } },
  69. { .channels = 4, /* CA_ID 0x02 */
  70. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_NA,
  71. SNDRV_CHMAP_FC } },
  72. { .channels = 4, /* CA_ID 0x03 */
  73. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
  74. SNDRV_CHMAP_FC } },
  75. { .channels = 6, /* CA_ID 0x04 */
  76. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_NA,
  77. SNDRV_CHMAP_NA, SNDRV_CHMAP_RC, SNDRV_CHMAP_NA } },
  78. { .channels = 6, /* CA_ID 0x05 */
  79. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
  80. SNDRV_CHMAP_NA, SNDRV_CHMAP_RC, SNDRV_CHMAP_NA } },
  81. { .channels = 6, /* CA_ID 0x06 */
  82. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_NA,
  83. SNDRV_CHMAP_FC, SNDRV_CHMAP_RC, SNDRV_CHMAP_NA } },
  84. { .channels = 6, /* CA_ID 0x07 */
  85. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
  86. SNDRV_CHMAP_FC, SNDRV_CHMAP_RC, SNDRV_CHMAP_NA } },
  87. { .channels = 6, /* CA_ID 0x08 */
  88. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_NA,
  89. SNDRV_CHMAP_NA, SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
  90. { .channels = 6, /* CA_ID 0x09 */
  91. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
  92. SNDRV_CHMAP_NA, SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
  93. { .channels = 6, /* CA_ID 0x0A */
  94. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_NA,
  95. SNDRV_CHMAP_FC, SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
  96. { .channels = 6, /* CA_ID 0x0B */
  97. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
  98. SNDRV_CHMAP_FC, SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
  99. { .channels = 8, /* CA_ID 0x0C */
  100. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_NA,
  101. SNDRV_CHMAP_NA, SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
  102. SNDRV_CHMAP_RC, SNDRV_CHMAP_NA } },
  103. { .channels = 8, /* CA_ID 0x0D */
  104. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
  105. SNDRV_CHMAP_NA, SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
  106. SNDRV_CHMAP_RC, SNDRV_CHMAP_NA } },
  107. { .channels = 8, /* CA_ID 0x0E */
  108. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_NA,
  109. SNDRV_CHMAP_FC, SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
  110. SNDRV_CHMAP_RC, SNDRV_CHMAP_NA } },
  111. { .channels = 8, /* CA_ID 0x0F */
  112. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
  113. SNDRV_CHMAP_FC, SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
  114. SNDRV_CHMAP_RC, SNDRV_CHMAP_NA } },
  115. { .channels = 8, /* CA_ID 0x10 */
  116. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_NA,
  117. SNDRV_CHMAP_NA, SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
  118. SNDRV_CHMAP_RLC, SNDRV_CHMAP_RRC } },
  119. { .channels = 8, /* CA_ID 0x11 */
  120. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
  121. SNDRV_CHMAP_NA, SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
  122. SNDRV_CHMAP_RLC, SNDRV_CHMAP_RRC } },
  123. { .channels = 8, /* CA_ID 0x12 */
  124. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_NA,
  125. SNDRV_CHMAP_FC, SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
  126. SNDRV_CHMAP_RLC, SNDRV_CHMAP_RRC } },
  127. { .channels = 8, /* CA_ID 0x13 */
  128. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
  129. SNDRV_CHMAP_FC, SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
  130. SNDRV_CHMAP_RLC, SNDRV_CHMAP_RRC } },
  131. { .channels = 8, /* CA_ID 0x14 */
  132. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_NA,
  133. SNDRV_CHMAP_NA, SNDRV_CHMAP_NA, SNDRV_CHMAP_NA,
  134. SNDRV_CHMAP_FLC, SNDRV_CHMAP_FRC } },
  135. { .channels = 8, /* CA_ID 0x15 */
  136. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
  137. SNDRV_CHMAP_NA, SNDRV_CHMAP_NA, SNDRV_CHMAP_NA,
  138. SNDRV_CHMAP_FLC, SNDRV_CHMAP_FRC } },
  139. { .channels = 8, /* CA_ID 0x16 */
  140. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_NA,
  141. SNDRV_CHMAP_FC, SNDRV_CHMAP_NA, SNDRV_CHMAP_NA,
  142. SNDRV_CHMAP_FLC, SNDRV_CHMAP_FRC } },
  143. { .channels = 8, /* CA_ID 0x17 */
  144. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
  145. SNDRV_CHMAP_FC, SNDRV_CHMAP_NA, SNDRV_CHMAP_NA,
  146. SNDRV_CHMAP_FLC, SNDRV_CHMAP_FRC } },
  147. { .channels = 8, /* CA_ID 0x18 */
  148. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_NA,
  149. SNDRV_CHMAP_NA, SNDRV_CHMAP_NA, SNDRV_CHMAP_NA,
  150. SNDRV_CHMAP_FLC, SNDRV_CHMAP_FRC } },
  151. { .channels = 8, /* CA_ID 0x19 */
  152. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
  153. SNDRV_CHMAP_NA, SNDRV_CHMAP_NA, SNDRV_CHMAP_NA,
  154. SNDRV_CHMAP_FLC, SNDRV_CHMAP_FRC } },
  155. { .channels = 8, /* CA_ID 0x1A */
  156. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_NA,
  157. SNDRV_CHMAP_FC, SNDRV_CHMAP_NA, SNDRV_CHMAP_NA,
  158. SNDRV_CHMAP_FLC, SNDRV_CHMAP_FRC } },
  159. { .channels = 8, /* CA_ID 0x1B */
  160. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
  161. SNDRV_CHMAP_FC, SNDRV_CHMAP_NA, SNDRV_CHMAP_NA,
  162. SNDRV_CHMAP_FLC, SNDRV_CHMAP_FRC } },
  163. { .channels = 8, /* CA_ID 0x1C */
  164. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_NA,
  165. SNDRV_CHMAP_NA, SNDRV_CHMAP_NA, SNDRV_CHMAP_NA,
  166. SNDRV_CHMAP_FLC, SNDRV_CHMAP_FRC } },
  167. { .channels = 8, /* CA_ID 0x1D */
  168. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
  169. SNDRV_CHMAP_NA, SNDRV_CHMAP_NA, SNDRV_CHMAP_NA,
  170. SNDRV_CHMAP_FLC, SNDRV_CHMAP_FRC } },
  171. { .channels = 8, /* CA_ID 0x1E */
  172. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_NA,
  173. SNDRV_CHMAP_FC, SNDRV_CHMAP_NA, SNDRV_CHMAP_NA,
  174. SNDRV_CHMAP_FLC, SNDRV_CHMAP_FRC } },
  175. { .channels = 8, /* CA_ID 0x1F */
  176. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
  177. SNDRV_CHMAP_FC, SNDRV_CHMAP_NA, SNDRV_CHMAP_NA,
  178. SNDRV_CHMAP_FLC, SNDRV_CHMAP_FRC } },
  179. { }
  180. };
  181. /*
  182. * hdmi_codec_channel_alloc: speaker configuration available for CEA
  183. *
  184. * This is an ordered list that must match with hdmi_codec_8ch_chmaps struct
  185. * The preceding ones have better chances to be selected by
  186. * hdmi_codec_get_ch_alloc_table_idx().
  187. */
  188. static const struct hdmi_codec_cea_spk_alloc hdmi_codec_channel_alloc[] = {
  189. { .ca_id = 0x00, .n_ch = 2,
  190. .mask = FL | FR},
  191. /* 2.1 */
  192. { .ca_id = 0x01, .n_ch = 4,
  193. .mask = FL | FR | LFE},
  194. /* Dolby Surround */
  195. { .ca_id = 0x02, .n_ch = 4,
  196. .mask = FL | FR | FC },
  197. /* surround51 */
  198. { .ca_id = 0x0b, .n_ch = 6,
  199. .mask = FL | FR | LFE | FC | RL | RR},
  200. /* surround40 */
  201. { .ca_id = 0x08, .n_ch = 6,
  202. .mask = FL | FR | RL | RR },
  203. /* surround41 */
  204. { .ca_id = 0x09, .n_ch = 6,
  205. .mask = FL | FR | LFE | RL | RR },
  206. /* surround50 */
  207. { .ca_id = 0x0a, .n_ch = 6,
  208. .mask = FL | FR | FC | RL | RR },
  209. /* 6.1 */
  210. { .ca_id = 0x0f, .n_ch = 8,
  211. .mask = FL | FR | LFE | FC | RL | RR | RC },
  212. /* surround71 */
  213. { .ca_id = 0x13, .n_ch = 8,
  214. .mask = FL | FR | LFE | FC | RL | RR | RLC | RRC },
  215. /* others */
  216. { .ca_id = 0x03, .n_ch = 8,
  217. .mask = FL | FR | LFE | FC },
  218. { .ca_id = 0x04, .n_ch = 8,
  219. .mask = FL | FR | RC},
  220. { .ca_id = 0x05, .n_ch = 8,
  221. .mask = FL | FR | LFE | RC },
  222. { .ca_id = 0x06, .n_ch = 8,
  223. .mask = FL | FR | FC | RC },
  224. { .ca_id = 0x07, .n_ch = 8,
  225. .mask = FL | FR | LFE | FC | RC },
  226. { .ca_id = 0x0c, .n_ch = 8,
  227. .mask = FL | FR | RC | RL | RR },
  228. { .ca_id = 0x0d, .n_ch = 8,
  229. .mask = FL | FR | LFE | RL | RR | RC },
  230. { .ca_id = 0x0e, .n_ch = 8,
  231. .mask = FL | FR | FC | RL | RR | RC },
  232. { .ca_id = 0x10, .n_ch = 8,
  233. .mask = FL | FR | RL | RR | RLC | RRC },
  234. { .ca_id = 0x11, .n_ch = 8,
  235. .mask = FL | FR | LFE | RL | RR | RLC | RRC },
  236. { .ca_id = 0x12, .n_ch = 8,
  237. .mask = FL | FR | FC | RL | RR | RLC | RRC },
  238. { .ca_id = 0x14, .n_ch = 8,
  239. .mask = FL | FR | FLC | FRC },
  240. { .ca_id = 0x15, .n_ch = 8,
  241. .mask = FL | FR | LFE | FLC | FRC },
  242. { .ca_id = 0x16, .n_ch = 8,
  243. .mask = FL | FR | FC | FLC | FRC },
  244. { .ca_id = 0x17, .n_ch = 8,
  245. .mask = FL | FR | LFE | FC | FLC | FRC },
  246. { .ca_id = 0x18, .n_ch = 8,
  247. .mask = FL | FR | RC | FLC | FRC },
  248. { .ca_id = 0x19, .n_ch = 8,
  249. .mask = FL | FR | LFE | RC | FLC | FRC },
  250. { .ca_id = 0x1a, .n_ch = 8,
  251. .mask = FL | FR | RC | FC | FLC | FRC },
  252. { .ca_id = 0x1b, .n_ch = 8,
  253. .mask = FL | FR | LFE | RC | FC | FLC | FRC },
  254. { .ca_id = 0x1c, .n_ch = 8,
  255. .mask = FL | FR | RL | RR | FLC | FRC },
  256. { .ca_id = 0x1d, .n_ch = 8,
  257. .mask = FL | FR | LFE | RL | RR | FLC | FRC },
  258. { .ca_id = 0x1e, .n_ch = 8,
  259. .mask = FL | FR | FC | RL | RR | FLC | FRC },
  260. { .ca_id = 0x1f, .n_ch = 8,
  261. .mask = FL | FR | LFE | FC | RL | RR | FLC | FRC },
  262. };
  263. struct hdmi_codec_priv {
  264. struct hdmi_codec_pdata hcd;
  265. uint8_t eld[MAX_ELD_BYTES];
  266. struct snd_pcm_chmap *chmap_info;
  267. unsigned int chmap_idx;
  268. struct mutex lock;
  269. struct snd_soc_jack *jack;
  270. unsigned int jack_status;
  271. };
  272. static const struct snd_soc_dapm_widget hdmi_widgets[] = {
  273. SND_SOC_DAPM_OUTPUT("TX"),
  274. };
  275. enum {
  276. DAI_ID_I2S = 0,
  277. DAI_ID_SPDIF,
  278. };
  279. static int hdmi_eld_ctl_info(struct snd_kcontrol *kcontrol,
  280. struct snd_ctl_elem_info *uinfo)
  281. {
  282. uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
  283. uinfo->count = FIELD_SIZEOF(struct hdmi_codec_priv, eld);
  284. return 0;
  285. }
  286. static int hdmi_eld_ctl_get(struct snd_kcontrol *kcontrol,
  287. struct snd_ctl_elem_value *ucontrol)
  288. {
  289. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  290. struct hdmi_codec_priv *hcp = snd_soc_component_get_drvdata(component);
  291. memcpy(ucontrol->value.bytes.data, hcp->eld, sizeof(hcp->eld));
  292. return 0;
  293. }
  294. static unsigned long hdmi_codec_spk_mask_from_alloc(int spk_alloc)
  295. {
  296. int i;
  297. static const unsigned long hdmi_codec_eld_spk_alloc_bits[] = {
  298. [0] = FL | FR, [1] = LFE, [2] = FC, [3] = RL | RR,
  299. [4] = RC, [5] = FLC | FRC, [6] = RLC | RRC,
  300. };
  301. unsigned long spk_mask = 0;
  302. for (i = 0; i < ARRAY_SIZE(hdmi_codec_eld_spk_alloc_bits); i++) {
  303. if (spk_alloc & (1 << i))
  304. spk_mask |= hdmi_codec_eld_spk_alloc_bits[i];
  305. }
  306. return spk_mask;
  307. }
  308. static void hdmi_codec_eld_chmap(struct hdmi_codec_priv *hcp)
  309. {
  310. u8 spk_alloc;
  311. unsigned long spk_mask;
  312. spk_alloc = drm_eld_get_spk_alloc(hcp->eld);
  313. spk_mask = hdmi_codec_spk_mask_from_alloc(spk_alloc);
  314. /* Detect if only stereo supported, else return 8 channels mappings */
  315. if ((spk_mask & ~(FL | FR)) && hcp->chmap_info->max_channels > 2)
  316. hcp->chmap_info->chmap = hdmi_codec_8ch_chmaps;
  317. else
  318. hcp->chmap_info->chmap = hdmi_codec_stereo_chmaps;
  319. }
  320. static int hdmi_codec_get_ch_alloc_table_idx(struct hdmi_codec_priv *hcp,
  321. unsigned char channels)
  322. {
  323. int i;
  324. u8 spk_alloc;
  325. unsigned long spk_mask;
  326. const struct hdmi_codec_cea_spk_alloc *cap = hdmi_codec_channel_alloc;
  327. spk_alloc = drm_eld_get_spk_alloc(hcp->eld);
  328. spk_mask = hdmi_codec_spk_mask_from_alloc(spk_alloc);
  329. for (i = 0; i < ARRAY_SIZE(hdmi_codec_channel_alloc); i++, cap++) {
  330. /* If spk_alloc == 0, HDMI is unplugged return stereo config*/
  331. if (!spk_alloc && cap->ca_id == 0)
  332. return i;
  333. if (cap->n_ch != channels)
  334. continue;
  335. if (!(cap->mask == (spk_mask & cap->mask)))
  336. continue;
  337. return i;
  338. }
  339. return -EINVAL;
  340. }
  341. static int hdmi_codec_chmap_ctl_get(struct snd_kcontrol *kcontrol,
  342. struct snd_ctl_elem_value *ucontrol)
  343. {
  344. unsigned const char *map;
  345. unsigned int i;
  346. struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
  347. struct hdmi_codec_priv *hcp = info->private_data;
  348. map = info->chmap[hcp->chmap_idx].map;
  349. for (i = 0; i < info->max_channels; i++) {
  350. if (hcp->chmap_idx == HDMI_CODEC_CHMAP_IDX_UNKNOWN)
  351. ucontrol->value.integer.value[i] = 0;
  352. else
  353. ucontrol->value.integer.value[i] = map[i];
  354. }
  355. return 0;
  356. }
  357. static int hdmi_codec_startup(struct snd_pcm_substream *substream,
  358. struct snd_soc_dai *dai)
  359. {
  360. struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
  361. int ret = 0;
  362. ret = mutex_trylock(&hcp->lock);
  363. if (!ret) {
  364. dev_err(dai->dev, "Only one simultaneous stream supported!\n");
  365. return -EINVAL;
  366. }
  367. if (hcp->hcd.ops->audio_startup) {
  368. ret = hcp->hcd.ops->audio_startup(dai->dev->parent, hcp->hcd.data);
  369. if (ret)
  370. goto err;
  371. }
  372. if (hcp->hcd.ops->get_eld) {
  373. ret = hcp->hcd.ops->get_eld(dai->dev->parent, hcp->hcd.data,
  374. hcp->eld, sizeof(hcp->eld));
  375. if (!ret) {
  376. ret = snd_pcm_hw_constraint_eld(substream->runtime,
  377. hcp->eld);
  378. if (ret)
  379. goto err;
  380. }
  381. /* Select chmap supported */
  382. hdmi_codec_eld_chmap(hcp);
  383. }
  384. return 0;
  385. err:
  386. /* Release the exclusive lock on error */
  387. mutex_unlock(&hcp->lock);
  388. return ret;
  389. }
  390. static void hdmi_codec_shutdown(struct snd_pcm_substream *substream,
  391. struct snd_soc_dai *dai)
  392. {
  393. struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
  394. hcp->chmap_idx = HDMI_CODEC_CHMAP_IDX_UNKNOWN;
  395. hcp->hcd.ops->audio_shutdown(dai->dev->parent, hcp->hcd.data);
  396. mutex_unlock(&hcp->lock);
  397. }
  398. static int hdmi_codec_hw_params(struct snd_pcm_substream *substream,
  399. struct snd_pcm_hw_params *params,
  400. struct snd_soc_dai *dai)
  401. {
  402. struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
  403. struct hdmi_codec_daifmt *cf = dai->playback_dma_data;
  404. struct hdmi_codec_params hp = {
  405. .iec = {
  406. .status = { 0 },
  407. .subcode = { 0 },
  408. .pad = 0,
  409. .dig_subframe = { 0 },
  410. }
  411. };
  412. int ret, idx;
  413. dev_dbg(dai->dev, "%s() width %d rate %d channels %d\n", __func__,
  414. params_width(params), params_rate(params),
  415. params_channels(params));
  416. ret = snd_pcm_create_iec958_consumer_hw_params(params, hp.iec.status,
  417. sizeof(hp.iec.status));
  418. if (ret < 0) {
  419. dev_err(dai->dev, "Creating IEC958 channel status failed %d\n",
  420. ret);
  421. return ret;
  422. }
  423. hdmi_audio_infoframe_init(&hp.cea);
  424. hp.cea.channels = params_channels(params);
  425. hp.cea.coding_type = HDMI_AUDIO_CODING_TYPE_STREAM;
  426. hp.cea.sample_size = HDMI_AUDIO_SAMPLE_SIZE_STREAM;
  427. hp.cea.sample_frequency = HDMI_AUDIO_SAMPLE_FREQUENCY_STREAM;
  428. /* Select a channel allocation that matches with ELD and pcm channels */
  429. idx = hdmi_codec_get_ch_alloc_table_idx(hcp, hp.cea.channels);
  430. if (idx < 0) {
  431. dev_err(dai->dev, "Not able to map channels to speakers (%d)\n",
  432. idx);
  433. hcp->chmap_idx = HDMI_CODEC_CHMAP_IDX_UNKNOWN;
  434. return idx;
  435. }
  436. hp.cea.channel_allocation = hdmi_codec_channel_alloc[idx].ca_id;
  437. hcp->chmap_idx = hdmi_codec_channel_alloc[idx].ca_id;
  438. hp.sample_width = params_width(params);
  439. hp.sample_rate = params_rate(params);
  440. hp.channels = params_channels(params);
  441. return hcp->hcd.ops->hw_params(dai->dev->parent, hcp->hcd.data,
  442. cf, &hp);
  443. }
  444. static int hdmi_codec_i2s_set_fmt(struct snd_soc_dai *dai,
  445. unsigned int fmt)
  446. {
  447. struct hdmi_codec_daifmt *cf = dai->playback_dma_data;
  448. /* Reset daifmt */
  449. memset(cf, 0, sizeof(*cf));
  450. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  451. case SND_SOC_DAIFMT_CBM_CFM:
  452. cf->bit_clk_master = 1;
  453. cf->frame_clk_master = 1;
  454. break;
  455. case SND_SOC_DAIFMT_CBS_CFM:
  456. cf->frame_clk_master = 1;
  457. break;
  458. case SND_SOC_DAIFMT_CBM_CFS:
  459. cf->bit_clk_master = 1;
  460. break;
  461. case SND_SOC_DAIFMT_CBS_CFS:
  462. break;
  463. default:
  464. return -EINVAL;
  465. }
  466. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  467. case SND_SOC_DAIFMT_NB_NF:
  468. break;
  469. case SND_SOC_DAIFMT_NB_IF:
  470. cf->frame_clk_inv = 1;
  471. break;
  472. case SND_SOC_DAIFMT_IB_NF:
  473. cf->bit_clk_inv = 1;
  474. break;
  475. case SND_SOC_DAIFMT_IB_IF:
  476. cf->frame_clk_inv = 1;
  477. cf->bit_clk_inv = 1;
  478. break;
  479. }
  480. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  481. case SND_SOC_DAIFMT_I2S:
  482. cf->fmt = HDMI_I2S;
  483. break;
  484. case SND_SOC_DAIFMT_DSP_A:
  485. cf->fmt = HDMI_DSP_A;
  486. break;
  487. case SND_SOC_DAIFMT_DSP_B:
  488. cf->fmt = HDMI_DSP_B;
  489. break;
  490. case SND_SOC_DAIFMT_RIGHT_J:
  491. cf->fmt = HDMI_RIGHT_J;
  492. break;
  493. case SND_SOC_DAIFMT_LEFT_J:
  494. cf->fmt = HDMI_LEFT_J;
  495. break;
  496. case SND_SOC_DAIFMT_AC97:
  497. cf->fmt = HDMI_AC97;
  498. break;
  499. default:
  500. dev_err(dai->dev, "Invalid DAI interface format\n");
  501. return -EINVAL;
  502. }
  503. return 0;
  504. }
  505. static int hdmi_codec_digital_mute(struct snd_soc_dai *dai, int mute)
  506. {
  507. struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
  508. if (hcp->hcd.ops->digital_mute)
  509. return hcp->hcd.ops->digital_mute(dai->dev->parent,
  510. hcp->hcd.data, mute);
  511. return 0;
  512. }
  513. static const struct snd_soc_dai_ops hdmi_codec_i2s_dai_ops = {
  514. .startup = hdmi_codec_startup,
  515. .shutdown = hdmi_codec_shutdown,
  516. .hw_params = hdmi_codec_hw_params,
  517. .set_fmt = hdmi_codec_i2s_set_fmt,
  518. .digital_mute = hdmi_codec_digital_mute,
  519. };
  520. static const struct snd_soc_dai_ops hdmi_codec_spdif_dai_ops = {
  521. .startup = hdmi_codec_startup,
  522. .shutdown = hdmi_codec_shutdown,
  523. .hw_params = hdmi_codec_hw_params,
  524. .digital_mute = hdmi_codec_digital_mute,
  525. };
  526. #define HDMI_RATES (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |\
  527. SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |\
  528. SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |\
  529. SNDRV_PCM_RATE_192000)
  530. #define SPDIF_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |\
  531. SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE |\
  532. SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE |\
  533. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE)
  534. /*
  535. * This list is only for formats allowed on the I2S bus. So there is
  536. * some formats listed that are not supported by HDMI interface. For
  537. * instance allowing the 32-bit formats enables 24-precision with CPU
  538. * DAIs that do not support 24-bit formats. If the extra formats cause
  539. * problems, we should add the video side driver an option to disable
  540. * them.
  541. */
  542. #define I2S_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |\
  543. SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE |\
  544. SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE |\
  545. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE |\
  546. SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE)
  547. static int hdmi_codec_pcm_new(struct snd_soc_pcm_runtime *rtd,
  548. struct snd_soc_dai *dai)
  549. {
  550. struct snd_soc_dai_driver *drv = dai->driver;
  551. struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
  552. struct snd_kcontrol *kctl;
  553. struct snd_kcontrol_new hdmi_eld_ctl = {
  554. .access = SNDRV_CTL_ELEM_ACCESS_READ |
  555. SNDRV_CTL_ELEM_ACCESS_VOLATILE,
  556. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  557. .name = "ELD",
  558. .info = hdmi_eld_ctl_info,
  559. .get = hdmi_eld_ctl_get,
  560. .device = rtd->pcm->device,
  561. };
  562. int ret;
  563. ret = snd_pcm_add_chmap_ctls(rtd->pcm, SNDRV_PCM_STREAM_PLAYBACK,
  564. NULL, drv->playback.channels_max, 0,
  565. &hcp->chmap_info);
  566. if (ret < 0)
  567. return ret;
  568. /* override handlers */
  569. hcp->chmap_info->private_data = hcp;
  570. hcp->chmap_info->kctl->get = hdmi_codec_chmap_ctl_get;
  571. /* default chmap supported is stereo */
  572. hcp->chmap_info->chmap = hdmi_codec_stereo_chmaps;
  573. hcp->chmap_idx = HDMI_CODEC_CHMAP_IDX_UNKNOWN;
  574. /* add ELD ctl with the device number corresponding to the PCM stream */
  575. kctl = snd_ctl_new1(&hdmi_eld_ctl, dai->component);
  576. if (!kctl)
  577. return -ENOMEM;
  578. return snd_ctl_add(rtd->card->snd_card, kctl);
  579. }
  580. static int hdmi_dai_probe(struct snd_soc_dai *dai)
  581. {
  582. struct snd_soc_dapm_context *dapm;
  583. struct hdmi_codec_daifmt *daifmt;
  584. struct snd_soc_dapm_route route = {
  585. .sink = "TX",
  586. .source = dai->driver->playback.stream_name,
  587. };
  588. int ret;
  589. dapm = snd_soc_component_get_dapm(dai->component);
  590. ret = snd_soc_dapm_add_routes(dapm, &route, 1);
  591. if (ret)
  592. return ret;
  593. daifmt = kzalloc(sizeof(*daifmt), GFP_KERNEL);
  594. if (!daifmt)
  595. return -ENOMEM;
  596. dai->playback_dma_data = daifmt;
  597. return 0;
  598. }
  599. static void hdmi_codec_jack_report(struct hdmi_codec_priv *hcp,
  600. unsigned int jack_status)
  601. {
  602. if (hcp->jack && jack_status != hcp->jack_status) {
  603. snd_soc_jack_report(hcp->jack, jack_status, SND_JACK_LINEOUT);
  604. hcp->jack_status = jack_status;
  605. }
  606. }
  607. static void plugged_cb(struct device *dev, bool plugged)
  608. {
  609. struct hdmi_codec_priv *hcp = dev_get_drvdata(dev);
  610. if (plugged)
  611. hdmi_codec_jack_report(hcp, SND_JACK_LINEOUT);
  612. else
  613. hdmi_codec_jack_report(hcp, 0);
  614. }
  615. /**
  616. * hdmi_codec_set_jack_detect - register HDMI plugged callback
  617. * @component: the hdmi-codec instance
  618. * @jack: ASoC jack to report (dis)connection events on
  619. */
  620. int hdmi_codec_set_jack_detect(struct snd_soc_component *component,
  621. struct snd_soc_jack *jack)
  622. {
  623. struct hdmi_codec_priv *hcp = snd_soc_component_get_drvdata(component);
  624. int ret = -EOPNOTSUPP;
  625. if (hcp->hcd.ops->hook_plugged_cb) {
  626. hcp->jack = jack;
  627. ret = hcp->hcd.ops->hook_plugged_cb(component->dev->parent,
  628. hcp->hcd.data,
  629. plugged_cb,
  630. component->dev);
  631. if (ret)
  632. hcp->jack = NULL;
  633. }
  634. return ret;
  635. }
  636. EXPORT_SYMBOL_GPL(hdmi_codec_set_jack_detect);
  637. static int hdmi_dai_spdif_probe(struct snd_soc_dai *dai)
  638. {
  639. struct hdmi_codec_daifmt *cf = dai->playback_dma_data;
  640. int ret;
  641. ret = hdmi_dai_probe(dai);
  642. if (ret)
  643. return ret;
  644. cf = dai->playback_dma_data;
  645. cf->fmt = HDMI_SPDIF;
  646. return 0;
  647. }
  648. static int hdmi_codec_dai_remove(struct snd_soc_dai *dai)
  649. {
  650. kfree(dai->playback_dma_data);
  651. return 0;
  652. }
  653. static const struct snd_soc_dai_driver hdmi_i2s_dai = {
  654. .name = "i2s-hifi",
  655. .id = DAI_ID_I2S,
  656. .probe = hdmi_dai_probe,
  657. .remove = hdmi_codec_dai_remove,
  658. .playback = {
  659. .stream_name = "I2S Playback",
  660. .channels_min = 2,
  661. .channels_max = 8,
  662. .rates = HDMI_RATES,
  663. .formats = I2S_FORMATS,
  664. .sig_bits = 24,
  665. },
  666. .ops = &hdmi_codec_i2s_dai_ops,
  667. .pcm_new = hdmi_codec_pcm_new,
  668. };
  669. static const struct snd_soc_dai_driver hdmi_spdif_dai = {
  670. .name = "spdif-hifi",
  671. .id = DAI_ID_SPDIF,
  672. .probe = hdmi_dai_spdif_probe,
  673. .remove = hdmi_codec_dai_remove,
  674. .playback = {
  675. .stream_name = "SPDIF Playback",
  676. .channels_min = 2,
  677. .channels_max = 2,
  678. .rates = HDMI_RATES,
  679. .formats = SPDIF_FORMATS,
  680. },
  681. .ops = &hdmi_codec_spdif_dai_ops,
  682. .pcm_new = hdmi_codec_pcm_new,
  683. };
  684. static int hdmi_of_xlate_dai_id(struct snd_soc_component *component,
  685. struct device_node *endpoint)
  686. {
  687. struct hdmi_codec_priv *hcp = snd_soc_component_get_drvdata(component);
  688. int ret = -ENOTSUPP; /* see snd_soc_get_dai_id() */
  689. if (hcp->hcd.ops->get_dai_id)
  690. ret = hcp->hcd.ops->get_dai_id(component, endpoint);
  691. return ret;
  692. }
  693. static const struct snd_soc_component_driver hdmi_driver = {
  694. .dapm_widgets = hdmi_widgets,
  695. .num_dapm_widgets = ARRAY_SIZE(hdmi_widgets),
  696. .of_xlate_dai_id = hdmi_of_xlate_dai_id,
  697. .idle_bias_on = 1,
  698. .use_pmdown_time = 1,
  699. .endianness = 1,
  700. .non_legacy_dai_naming = 1,
  701. };
  702. static int hdmi_codec_probe(struct platform_device *pdev)
  703. {
  704. struct hdmi_codec_pdata *hcd = pdev->dev.platform_data;
  705. struct snd_soc_dai_driver *daidrv;
  706. struct device *dev = &pdev->dev;
  707. struct hdmi_codec_priv *hcp;
  708. int dai_count, i = 0;
  709. int ret;
  710. if (!hcd) {
  711. dev_err(dev, "%s: No platform data\n", __func__);
  712. return -EINVAL;
  713. }
  714. dai_count = hcd->i2s + hcd->spdif;
  715. if (dai_count < 1 || !hcd->ops || !hcd->ops->hw_params ||
  716. !hcd->ops->audio_shutdown) {
  717. dev_err(dev, "%s: Invalid parameters\n", __func__);
  718. return -EINVAL;
  719. }
  720. hcp = devm_kzalloc(dev, sizeof(*hcp), GFP_KERNEL);
  721. if (!hcp)
  722. return -ENOMEM;
  723. hcp->hcd = *hcd;
  724. mutex_init(&hcp->lock);
  725. daidrv = devm_kcalloc(dev, dai_count, sizeof(*daidrv), GFP_KERNEL);
  726. if (!daidrv)
  727. return -ENOMEM;
  728. if (hcd->i2s) {
  729. daidrv[i] = hdmi_i2s_dai;
  730. daidrv[i].playback.channels_max = hcd->max_i2s_channels;
  731. i++;
  732. }
  733. if (hcd->spdif)
  734. daidrv[i] = hdmi_spdif_dai;
  735. dev_set_drvdata(dev, hcp);
  736. ret = devm_snd_soc_register_component(dev, &hdmi_driver, daidrv,
  737. dai_count);
  738. if (ret) {
  739. dev_err(dev, "%s: snd_soc_register_component() failed (%d)\n",
  740. __func__, ret);
  741. return ret;
  742. }
  743. return 0;
  744. }
  745. static struct platform_driver hdmi_codec_driver = {
  746. .driver = {
  747. .name = HDMI_CODEC_DRV_NAME,
  748. },
  749. .probe = hdmi_codec_probe,
  750. };
  751. module_platform_driver(hdmi_codec_driver);
  752. MODULE_AUTHOR("Jyri Sarha <jsarha@ti.com>");
  753. MODULE_DESCRIPTION("HDMI Audio Codec Driver");
  754. MODULE_LICENSE("GPL");
  755. MODULE_ALIAS("platform:" HDMI_CODEC_DRV_NAME);