hdmi-codec.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053
  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 <[email protected]>
  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. /*
  21. * CEA speaker placement for HDMI 1.4:
  22. *
  23. * FL FLC FC FRC FR FRW
  24. *
  25. * LFE
  26. *
  27. * RL RLC RC RRC RR
  28. *
  29. * Speaker placement has to be extended to support HDMI 2.0
  30. */
  31. enum hdmi_codec_cea_spk_placement {
  32. FL = BIT(0), /* Front Left */
  33. FC = BIT(1), /* Front Center */
  34. FR = BIT(2), /* Front Right */
  35. FLC = BIT(3), /* Front Left Center */
  36. FRC = BIT(4), /* Front Right Center */
  37. RL = BIT(5), /* Rear Left */
  38. RC = BIT(6), /* Rear Center */
  39. RR = BIT(7), /* Rear Right */
  40. RLC = BIT(8), /* Rear Left Center */
  41. RRC = BIT(9), /* Rear Right Center */
  42. LFE = BIT(10), /* Low Frequency Effect */
  43. };
  44. /*
  45. * cea Speaker allocation structure
  46. */
  47. struct hdmi_codec_cea_spk_alloc {
  48. const int ca_id;
  49. unsigned int n_ch;
  50. unsigned long mask;
  51. };
  52. /* Channel maps stereo HDMI */
  53. static const struct snd_pcm_chmap_elem hdmi_codec_stereo_chmaps[] = {
  54. { .channels = 2,
  55. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
  56. { }
  57. };
  58. /* Channel maps for multi-channel playbacks, up to 8 n_ch */
  59. static const struct snd_pcm_chmap_elem hdmi_codec_8ch_chmaps[] = {
  60. { .channels = 2, /* CA_ID 0x00 */
  61. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
  62. { .channels = 4, /* CA_ID 0x01 */
  63. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
  64. SNDRV_CHMAP_NA } },
  65. { .channels = 4, /* CA_ID 0x02 */
  66. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_NA,
  67. SNDRV_CHMAP_FC } },
  68. { .channels = 4, /* CA_ID 0x03 */
  69. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
  70. SNDRV_CHMAP_FC } },
  71. { .channels = 6, /* CA_ID 0x04 */
  72. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_NA,
  73. SNDRV_CHMAP_NA, SNDRV_CHMAP_RC, SNDRV_CHMAP_NA } },
  74. { .channels = 6, /* CA_ID 0x05 */
  75. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
  76. SNDRV_CHMAP_NA, SNDRV_CHMAP_RC, SNDRV_CHMAP_NA } },
  77. { .channels = 6, /* CA_ID 0x06 */
  78. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_NA,
  79. SNDRV_CHMAP_FC, SNDRV_CHMAP_RC, SNDRV_CHMAP_NA } },
  80. { .channels = 6, /* CA_ID 0x07 */
  81. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
  82. SNDRV_CHMAP_FC, SNDRV_CHMAP_RC, SNDRV_CHMAP_NA } },
  83. { .channels = 6, /* CA_ID 0x08 */
  84. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_NA,
  85. SNDRV_CHMAP_NA, SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
  86. { .channels = 6, /* CA_ID 0x09 */
  87. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
  88. SNDRV_CHMAP_NA, SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
  89. { .channels = 6, /* CA_ID 0x0A */
  90. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_NA,
  91. SNDRV_CHMAP_FC, SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
  92. { .channels = 6, /* CA_ID 0x0B */
  93. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
  94. SNDRV_CHMAP_FC, SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
  95. { .channels = 8, /* CA_ID 0x0C */
  96. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_NA,
  97. SNDRV_CHMAP_NA, SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
  98. SNDRV_CHMAP_RC, SNDRV_CHMAP_NA } },
  99. { .channels = 8, /* CA_ID 0x0D */
  100. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
  101. SNDRV_CHMAP_NA, SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
  102. SNDRV_CHMAP_RC, SNDRV_CHMAP_NA } },
  103. { .channels = 8, /* CA_ID 0x0E */
  104. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_NA,
  105. SNDRV_CHMAP_FC, SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
  106. SNDRV_CHMAP_RC, SNDRV_CHMAP_NA } },
  107. { .channels = 8, /* CA_ID 0x0F */
  108. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
  109. SNDRV_CHMAP_FC, SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
  110. SNDRV_CHMAP_RC, SNDRV_CHMAP_NA } },
  111. { .channels = 8, /* CA_ID 0x10 */
  112. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_NA,
  113. SNDRV_CHMAP_NA, SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
  114. SNDRV_CHMAP_RLC, SNDRV_CHMAP_RRC } },
  115. { .channels = 8, /* CA_ID 0x11 */
  116. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
  117. SNDRV_CHMAP_NA, SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
  118. SNDRV_CHMAP_RLC, SNDRV_CHMAP_RRC } },
  119. { .channels = 8, /* CA_ID 0x12 */
  120. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_NA,
  121. SNDRV_CHMAP_FC, SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
  122. SNDRV_CHMAP_RLC, SNDRV_CHMAP_RRC } },
  123. { .channels = 8, /* CA_ID 0x13 */
  124. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
  125. SNDRV_CHMAP_FC, SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
  126. SNDRV_CHMAP_RLC, SNDRV_CHMAP_RRC } },
  127. { .channels = 8, /* CA_ID 0x14 */
  128. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_NA,
  129. SNDRV_CHMAP_NA, SNDRV_CHMAP_NA, SNDRV_CHMAP_NA,
  130. SNDRV_CHMAP_FLC, SNDRV_CHMAP_FRC } },
  131. { .channels = 8, /* CA_ID 0x15 */
  132. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
  133. SNDRV_CHMAP_NA, SNDRV_CHMAP_NA, SNDRV_CHMAP_NA,
  134. SNDRV_CHMAP_FLC, SNDRV_CHMAP_FRC } },
  135. { .channels = 8, /* CA_ID 0x16 */
  136. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_NA,
  137. SNDRV_CHMAP_FC, SNDRV_CHMAP_NA, SNDRV_CHMAP_NA,
  138. SNDRV_CHMAP_FLC, SNDRV_CHMAP_FRC } },
  139. { .channels = 8, /* CA_ID 0x17 */
  140. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
  141. SNDRV_CHMAP_FC, SNDRV_CHMAP_NA, SNDRV_CHMAP_NA,
  142. SNDRV_CHMAP_FLC, SNDRV_CHMAP_FRC } },
  143. { .channels = 8, /* CA_ID 0x18 */
  144. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_NA,
  145. SNDRV_CHMAP_NA, SNDRV_CHMAP_NA, SNDRV_CHMAP_NA,
  146. SNDRV_CHMAP_FLC, SNDRV_CHMAP_FRC } },
  147. { .channels = 8, /* CA_ID 0x19 */
  148. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
  149. SNDRV_CHMAP_NA, SNDRV_CHMAP_NA, SNDRV_CHMAP_NA,
  150. SNDRV_CHMAP_FLC, SNDRV_CHMAP_FRC } },
  151. { .channels = 8, /* CA_ID 0x1A */
  152. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_NA,
  153. SNDRV_CHMAP_FC, SNDRV_CHMAP_NA, SNDRV_CHMAP_NA,
  154. SNDRV_CHMAP_FLC, SNDRV_CHMAP_FRC } },
  155. { .channels = 8, /* CA_ID 0x1B */
  156. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
  157. SNDRV_CHMAP_FC, SNDRV_CHMAP_NA, SNDRV_CHMAP_NA,
  158. SNDRV_CHMAP_FLC, SNDRV_CHMAP_FRC } },
  159. { .channels = 8, /* CA_ID 0x1C */
  160. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_NA,
  161. SNDRV_CHMAP_NA, SNDRV_CHMAP_NA, SNDRV_CHMAP_NA,
  162. SNDRV_CHMAP_FLC, SNDRV_CHMAP_FRC } },
  163. { .channels = 8, /* CA_ID 0x1D */
  164. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
  165. SNDRV_CHMAP_NA, SNDRV_CHMAP_NA, SNDRV_CHMAP_NA,
  166. SNDRV_CHMAP_FLC, SNDRV_CHMAP_FRC } },
  167. { .channels = 8, /* CA_ID 0x1E */
  168. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_NA,
  169. SNDRV_CHMAP_FC, SNDRV_CHMAP_NA, SNDRV_CHMAP_NA,
  170. SNDRV_CHMAP_FLC, SNDRV_CHMAP_FRC } },
  171. { .channels = 8, /* CA_ID 0x1F */
  172. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
  173. SNDRV_CHMAP_FC, SNDRV_CHMAP_NA, SNDRV_CHMAP_NA,
  174. SNDRV_CHMAP_FLC, SNDRV_CHMAP_FRC } },
  175. { }
  176. };
  177. /*
  178. * hdmi_codec_channel_alloc: speaker configuration available for CEA
  179. *
  180. * This is an ordered list that must match with hdmi_codec_8ch_chmaps struct
  181. * The preceding ones have better chances to be selected by
  182. * hdmi_codec_get_ch_alloc_table_idx().
  183. */
  184. static const struct hdmi_codec_cea_spk_alloc hdmi_codec_channel_alloc[] = {
  185. { .ca_id = 0x00, .n_ch = 2,
  186. .mask = FL | FR},
  187. /* 2.1 */
  188. { .ca_id = 0x01, .n_ch = 4,
  189. .mask = FL | FR | LFE},
  190. /* Dolby Surround */
  191. { .ca_id = 0x02, .n_ch = 4,
  192. .mask = FL | FR | FC },
  193. /* surround51 */
  194. { .ca_id = 0x0b, .n_ch = 6,
  195. .mask = FL | FR | LFE | FC | RL | RR},
  196. /* surround40 */
  197. { .ca_id = 0x08, .n_ch = 6,
  198. .mask = FL | FR | RL | RR },
  199. /* surround41 */
  200. { .ca_id = 0x09, .n_ch = 6,
  201. .mask = FL | FR | LFE | RL | RR },
  202. /* surround50 */
  203. { .ca_id = 0x0a, .n_ch = 6,
  204. .mask = FL | FR | FC | RL | RR },
  205. /* 6.1 */
  206. { .ca_id = 0x0f, .n_ch = 8,
  207. .mask = FL | FR | LFE | FC | RL | RR | RC },
  208. /* surround71 */
  209. { .ca_id = 0x13, .n_ch = 8,
  210. .mask = FL | FR | LFE | FC | RL | RR | RLC | RRC },
  211. /* others */
  212. { .ca_id = 0x03, .n_ch = 8,
  213. .mask = FL | FR | LFE | FC },
  214. { .ca_id = 0x04, .n_ch = 8,
  215. .mask = FL | FR | RC},
  216. { .ca_id = 0x05, .n_ch = 8,
  217. .mask = FL | FR | LFE | RC },
  218. { .ca_id = 0x06, .n_ch = 8,
  219. .mask = FL | FR | FC | RC },
  220. { .ca_id = 0x07, .n_ch = 8,
  221. .mask = FL | FR | LFE | FC | RC },
  222. { .ca_id = 0x0c, .n_ch = 8,
  223. .mask = FL | FR | RC | RL | RR },
  224. { .ca_id = 0x0d, .n_ch = 8,
  225. .mask = FL | FR | LFE | RL | RR | RC },
  226. { .ca_id = 0x0e, .n_ch = 8,
  227. .mask = FL | FR | FC | RL | RR | RC },
  228. { .ca_id = 0x10, .n_ch = 8,
  229. .mask = FL | FR | RL | RR | RLC | RRC },
  230. { .ca_id = 0x11, .n_ch = 8,
  231. .mask = FL | FR | LFE | RL | RR | RLC | RRC },
  232. { .ca_id = 0x12, .n_ch = 8,
  233. .mask = FL | FR | FC | RL | RR | RLC | RRC },
  234. { .ca_id = 0x14, .n_ch = 8,
  235. .mask = FL | FR | FLC | FRC },
  236. { .ca_id = 0x15, .n_ch = 8,
  237. .mask = FL | FR | LFE | FLC | FRC },
  238. { .ca_id = 0x16, .n_ch = 8,
  239. .mask = FL | FR | FC | FLC | FRC },
  240. { .ca_id = 0x17, .n_ch = 8,
  241. .mask = FL | FR | LFE | FC | FLC | FRC },
  242. { .ca_id = 0x18, .n_ch = 8,
  243. .mask = FL | FR | RC | FLC | FRC },
  244. { .ca_id = 0x19, .n_ch = 8,
  245. .mask = FL | FR | LFE | RC | FLC | FRC },
  246. { .ca_id = 0x1a, .n_ch = 8,
  247. .mask = FL | FR | RC | FC | FLC | FRC },
  248. { .ca_id = 0x1b, .n_ch = 8,
  249. .mask = FL | FR | LFE | RC | FC | FLC | FRC },
  250. { .ca_id = 0x1c, .n_ch = 8,
  251. .mask = FL | FR | RL | RR | FLC | FRC },
  252. { .ca_id = 0x1d, .n_ch = 8,
  253. .mask = FL | FR | LFE | RL | RR | FLC | FRC },
  254. { .ca_id = 0x1e, .n_ch = 8,
  255. .mask = FL | FR | FC | RL | RR | FLC | FRC },
  256. { .ca_id = 0x1f, .n_ch = 8,
  257. .mask = FL | FR | LFE | FC | RL | RR | FLC | FRC },
  258. };
  259. struct hdmi_codec_priv {
  260. struct hdmi_codec_pdata hcd;
  261. uint8_t eld[MAX_ELD_BYTES];
  262. struct snd_pcm_chmap *chmap_info;
  263. unsigned int chmap_idx;
  264. struct mutex lock;
  265. bool busy;
  266. struct snd_soc_jack *jack;
  267. unsigned int jack_status;
  268. u8 iec_status[AES_IEC958_STATUS_SIZE];
  269. };
  270. static const struct snd_soc_dapm_widget hdmi_widgets[] = {
  271. SND_SOC_DAPM_OUTPUT("TX"),
  272. SND_SOC_DAPM_OUTPUT("RX"),
  273. };
  274. enum {
  275. DAI_ID_I2S = 0,
  276. DAI_ID_SPDIF,
  277. };
  278. static int hdmi_eld_ctl_info(struct snd_kcontrol *kcontrol,
  279. struct snd_ctl_elem_info *uinfo)
  280. {
  281. uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
  282. uinfo->count = sizeof_field(struct hdmi_codec_priv, eld);
  283. return 0;
  284. }
  285. static int hdmi_eld_ctl_get(struct snd_kcontrol *kcontrol,
  286. struct snd_ctl_elem_value *ucontrol)
  287. {
  288. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  289. struct hdmi_codec_priv *hcp = snd_soc_component_get_drvdata(component);
  290. memcpy(ucontrol->value.bytes.data, hcp->eld, sizeof(hcp->eld));
  291. return 0;
  292. }
  293. static unsigned long hdmi_codec_spk_mask_from_alloc(int spk_alloc)
  294. {
  295. int i;
  296. static const unsigned long hdmi_codec_eld_spk_alloc_bits[] = {
  297. [0] = FL | FR, [1] = LFE, [2] = FC, [3] = RL | RR,
  298. [4] = RC, [5] = FLC | FRC, [6] = RLC | RRC,
  299. };
  300. unsigned long spk_mask = 0;
  301. for (i = 0; i < ARRAY_SIZE(hdmi_codec_eld_spk_alloc_bits); i++) {
  302. if (spk_alloc & (1 << i))
  303. spk_mask |= hdmi_codec_eld_spk_alloc_bits[i];
  304. }
  305. return spk_mask;
  306. }
  307. static void hdmi_codec_eld_chmap(struct hdmi_codec_priv *hcp)
  308. {
  309. u8 spk_alloc;
  310. unsigned long spk_mask;
  311. spk_alloc = drm_eld_get_spk_alloc(hcp->eld);
  312. spk_mask = hdmi_codec_spk_mask_from_alloc(spk_alloc);
  313. /* Detect if only stereo supported, else return 8 channels mappings */
  314. if ((spk_mask & ~(FL | FR)) && hcp->chmap_info->max_channels > 2)
  315. hcp->chmap_info->chmap = hdmi_codec_8ch_chmaps;
  316. else
  317. hcp->chmap_info->chmap = hdmi_codec_stereo_chmaps;
  318. }
  319. static int hdmi_codec_get_ch_alloc_table_idx(struct hdmi_codec_priv *hcp,
  320. unsigned char channels)
  321. {
  322. int i;
  323. u8 spk_alloc;
  324. unsigned long spk_mask;
  325. const struct hdmi_codec_cea_spk_alloc *cap = hdmi_codec_channel_alloc;
  326. spk_alloc = drm_eld_get_spk_alloc(hcp->eld);
  327. spk_mask = hdmi_codec_spk_mask_from_alloc(spk_alloc);
  328. for (i = 0; i < ARRAY_SIZE(hdmi_codec_channel_alloc); i++, cap++) {
  329. /* If spk_alloc == 0, HDMI is unplugged return stereo config*/
  330. if (!spk_alloc && cap->ca_id == 0)
  331. return i;
  332. if (cap->n_ch != channels)
  333. continue;
  334. if (!(cap->mask == (spk_mask & cap->mask)))
  335. continue;
  336. return i;
  337. }
  338. return -EINVAL;
  339. }
  340. static int hdmi_codec_chmap_ctl_get(struct snd_kcontrol *kcontrol,
  341. struct snd_ctl_elem_value *ucontrol)
  342. {
  343. unsigned const char *map;
  344. unsigned int i;
  345. struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
  346. struct hdmi_codec_priv *hcp = info->private_data;
  347. map = info->chmap[hcp->chmap_idx].map;
  348. for (i = 0; i < info->max_channels; i++) {
  349. if (hcp->chmap_idx == HDMI_CODEC_CHMAP_IDX_UNKNOWN)
  350. ucontrol->value.integer.value[i] = 0;
  351. else
  352. ucontrol->value.integer.value[i] = map[i];
  353. }
  354. return 0;
  355. }
  356. static int hdmi_codec_iec958_info(struct snd_kcontrol *kcontrol,
  357. struct snd_ctl_elem_info *uinfo)
  358. {
  359. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  360. uinfo->count = 1;
  361. return 0;
  362. }
  363. static int hdmi_codec_iec958_default_get(struct snd_kcontrol *kcontrol,
  364. struct snd_ctl_elem_value *ucontrol)
  365. {
  366. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  367. struct hdmi_codec_priv *hcp = snd_soc_component_get_drvdata(component);
  368. memcpy(ucontrol->value.iec958.status, hcp->iec_status,
  369. sizeof(hcp->iec_status));
  370. return 0;
  371. }
  372. static int hdmi_codec_iec958_default_put(struct snd_kcontrol *kcontrol,
  373. struct snd_ctl_elem_value *ucontrol)
  374. {
  375. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  376. struct hdmi_codec_priv *hcp = snd_soc_component_get_drvdata(component);
  377. memcpy(hcp->iec_status, ucontrol->value.iec958.status,
  378. sizeof(hcp->iec_status));
  379. return 0;
  380. }
  381. static int hdmi_codec_iec958_mask_get(struct snd_kcontrol *kcontrol,
  382. struct snd_ctl_elem_value *ucontrol)
  383. {
  384. memset(ucontrol->value.iec958.status, 0xff,
  385. sizeof_field(struct hdmi_codec_priv, iec_status));
  386. return 0;
  387. }
  388. static int hdmi_codec_startup(struct snd_pcm_substream *substream,
  389. struct snd_soc_dai *dai)
  390. {
  391. struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
  392. bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
  393. int ret = 0;
  394. mutex_lock(&hcp->lock);
  395. if (hcp->busy) {
  396. dev_err(dai->dev, "Only one simultaneous stream supported!\n");
  397. mutex_unlock(&hcp->lock);
  398. return -EINVAL;
  399. }
  400. if (hcp->hcd.ops->audio_startup) {
  401. ret = hcp->hcd.ops->audio_startup(dai->dev->parent, hcp->hcd.data);
  402. if (ret)
  403. goto err;
  404. }
  405. if (tx && hcp->hcd.ops->get_eld) {
  406. ret = hcp->hcd.ops->get_eld(dai->dev->parent, hcp->hcd.data,
  407. hcp->eld, sizeof(hcp->eld));
  408. if (ret)
  409. goto err;
  410. ret = snd_pcm_hw_constraint_eld(substream->runtime, hcp->eld);
  411. if (ret)
  412. goto err;
  413. /* Select chmap supported */
  414. hdmi_codec_eld_chmap(hcp);
  415. }
  416. hcp->busy = true;
  417. err:
  418. mutex_unlock(&hcp->lock);
  419. return ret;
  420. }
  421. static void hdmi_codec_shutdown(struct snd_pcm_substream *substream,
  422. struct snd_soc_dai *dai)
  423. {
  424. struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
  425. hcp->chmap_idx = HDMI_CODEC_CHMAP_IDX_UNKNOWN;
  426. hcp->hcd.ops->audio_shutdown(dai->dev->parent, hcp->hcd.data);
  427. mutex_lock(&hcp->lock);
  428. hcp->busy = false;
  429. mutex_unlock(&hcp->lock);
  430. }
  431. static int hdmi_codec_fill_codec_params(struct snd_soc_dai *dai,
  432. unsigned int sample_width,
  433. unsigned int sample_rate,
  434. unsigned int channels,
  435. struct hdmi_codec_params *hp)
  436. {
  437. struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
  438. int idx;
  439. /* Select a channel allocation that matches with ELD and pcm channels */
  440. idx = hdmi_codec_get_ch_alloc_table_idx(hcp, channels);
  441. if (idx < 0) {
  442. dev_err(dai->dev, "Not able to map channels to speakers (%d)\n",
  443. idx);
  444. hcp->chmap_idx = HDMI_CODEC_CHMAP_IDX_UNKNOWN;
  445. return idx;
  446. }
  447. memset(hp, 0, sizeof(*hp));
  448. hdmi_audio_infoframe_init(&hp->cea);
  449. hp->cea.channels = channels;
  450. hp->cea.coding_type = HDMI_AUDIO_CODING_TYPE_STREAM;
  451. hp->cea.sample_size = HDMI_AUDIO_SAMPLE_SIZE_STREAM;
  452. hp->cea.sample_frequency = HDMI_AUDIO_SAMPLE_FREQUENCY_STREAM;
  453. hp->cea.channel_allocation = hdmi_codec_channel_alloc[idx].ca_id;
  454. hp->sample_width = sample_width;
  455. hp->sample_rate = sample_rate;
  456. hp->channels = channels;
  457. hcp->chmap_idx = hdmi_codec_channel_alloc[idx].ca_id;
  458. return 0;
  459. }
  460. static int hdmi_codec_hw_params(struct snd_pcm_substream *substream,
  461. struct snd_pcm_hw_params *params,
  462. struct snd_soc_dai *dai)
  463. {
  464. struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
  465. struct hdmi_codec_daifmt *cf = dai->playback_dma_data;
  466. struct hdmi_codec_params hp = {
  467. .iec = {
  468. .status = { 0 },
  469. .subcode = { 0 },
  470. .pad = 0,
  471. .dig_subframe = { 0 },
  472. }
  473. };
  474. int ret;
  475. if (!hcp->hcd.ops->hw_params)
  476. return 0;
  477. dev_dbg(dai->dev, "%s() width %d rate %d channels %d\n", __func__,
  478. params_width(params), params_rate(params),
  479. params_channels(params));
  480. ret = hdmi_codec_fill_codec_params(dai,
  481. params_width(params),
  482. params_rate(params),
  483. params_channels(params),
  484. &hp);
  485. if (ret < 0)
  486. return ret;
  487. memcpy(hp.iec.status, hcp->iec_status, sizeof(hp.iec.status));
  488. ret = snd_pcm_fill_iec958_consumer_hw_params(params, hp.iec.status,
  489. sizeof(hp.iec.status));
  490. if (ret < 0) {
  491. dev_err(dai->dev, "Creating IEC958 channel status failed %d\n",
  492. ret);
  493. return ret;
  494. }
  495. cf->bit_fmt = params_format(params);
  496. return hcp->hcd.ops->hw_params(dai->dev->parent, hcp->hcd.data,
  497. cf, &hp);
  498. }
  499. static int hdmi_codec_prepare(struct snd_pcm_substream *substream,
  500. struct snd_soc_dai *dai)
  501. {
  502. struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
  503. struct hdmi_codec_daifmt *cf = dai->playback_dma_data;
  504. struct snd_pcm_runtime *runtime = substream->runtime;
  505. unsigned int channels = runtime->channels;
  506. unsigned int width = snd_pcm_format_width(runtime->format);
  507. unsigned int rate = runtime->rate;
  508. struct hdmi_codec_params hp;
  509. int ret;
  510. if (!hcp->hcd.ops->prepare)
  511. return 0;
  512. dev_dbg(dai->dev, "%s() width %d rate %d channels %d\n", __func__,
  513. width, rate, channels);
  514. ret = hdmi_codec_fill_codec_params(dai, width, rate, channels, &hp);
  515. if (ret < 0)
  516. return ret;
  517. memcpy(hp.iec.status, hcp->iec_status, sizeof(hp.iec.status));
  518. ret = snd_pcm_fill_iec958_consumer(runtime, hp.iec.status,
  519. sizeof(hp.iec.status));
  520. if (ret < 0) {
  521. dev_err(dai->dev, "Creating IEC958 channel status failed %d\n",
  522. ret);
  523. return ret;
  524. }
  525. cf->bit_fmt = runtime->format;
  526. return hcp->hcd.ops->prepare(dai->dev->parent, hcp->hcd.data,
  527. cf, &hp);
  528. }
  529. static int hdmi_codec_i2s_set_fmt(struct snd_soc_dai *dai,
  530. unsigned int fmt)
  531. {
  532. struct hdmi_codec_daifmt *cf = dai->playback_dma_data;
  533. /* Reset daifmt */
  534. memset(cf, 0, sizeof(*cf));
  535. switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
  536. case SND_SOC_DAIFMT_CBP_CFP:
  537. cf->bit_clk_provider = 1;
  538. cf->frame_clk_provider = 1;
  539. break;
  540. case SND_SOC_DAIFMT_CBC_CFP:
  541. cf->frame_clk_provider = 1;
  542. break;
  543. case SND_SOC_DAIFMT_CBP_CFC:
  544. cf->bit_clk_provider = 1;
  545. break;
  546. case SND_SOC_DAIFMT_CBC_CFC:
  547. break;
  548. default:
  549. return -EINVAL;
  550. }
  551. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  552. case SND_SOC_DAIFMT_NB_NF:
  553. break;
  554. case SND_SOC_DAIFMT_NB_IF:
  555. cf->frame_clk_inv = 1;
  556. break;
  557. case SND_SOC_DAIFMT_IB_NF:
  558. cf->bit_clk_inv = 1;
  559. break;
  560. case SND_SOC_DAIFMT_IB_IF:
  561. cf->frame_clk_inv = 1;
  562. cf->bit_clk_inv = 1;
  563. break;
  564. }
  565. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  566. case SND_SOC_DAIFMT_I2S:
  567. cf->fmt = HDMI_I2S;
  568. break;
  569. case SND_SOC_DAIFMT_DSP_A:
  570. cf->fmt = HDMI_DSP_A;
  571. break;
  572. case SND_SOC_DAIFMT_DSP_B:
  573. cf->fmt = HDMI_DSP_B;
  574. break;
  575. case SND_SOC_DAIFMT_RIGHT_J:
  576. cf->fmt = HDMI_RIGHT_J;
  577. break;
  578. case SND_SOC_DAIFMT_LEFT_J:
  579. cf->fmt = HDMI_LEFT_J;
  580. break;
  581. case SND_SOC_DAIFMT_AC97:
  582. cf->fmt = HDMI_AC97;
  583. break;
  584. default:
  585. dev_err(dai->dev, "Invalid DAI interface format\n");
  586. return -EINVAL;
  587. }
  588. return 0;
  589. }
  590. static int hdmi_codec_mute(struct snd_soc_dai *dai, int mute, int direction)
  591. {
  592. struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
  593. /*
  594. * ignore if direction was CAPTURE
  595. * and it had .no_capture_mute flag
  596. * see
  597. * snd_soc_dai_digital_mute()
  598. */
  599. if (hcp->hcd.ops->mute_stream &&
  600. (direction == SNDRV_PCM_STREAM_PLAYBACK ||
  601. !hcp->hcd.ops->no_capture_mute))
  602. return hcp->hcd.ops->mute_stream(dai->dev->parent,
  603. hcp->hcd.data,
  604. mute, direction);
  605. return -ENOTSUPP;
  606. }
  607. /*
  608. * This driver can select all SND_SOC_DAIFMT_CBx_CFx,
  609. * but need to be selected from Sound Card, not be auto selected.
  610. * Because it might be used from other driver.
  611. * For example,
  612. * ${LINUX}/drivers/gpu/drm/bridge/synopsys/dw-hdmi-i2s-audio.c
  613. */
  614. static u64 hdmi_codec_formats =
  615. SND_SOC_POSSIBLE_DAIFMT_NB_NF |
  616. SND_SOC_POSSIBLE_DAIFMT_NB_IF |
  617. SND_SOC_POSSIBLE_DAIFMT_IB_NF |
  618. SND_SOC_POSSIBLE_DAIFMT_IB_IF |
  619. SND_SOC_POSSIBLE_DAIFMT_I2S |
  620. SND_SOC_POSSIBLE_DAIFMT_DSP_A |
  621. SND_SOC_POSSIBLE_DAIFMT_DSP_B |
  622. SND_SOC_POSSIBLE_DAIFMT_RIGHT_J |
  623. SND_SOC_POSSIBLE_DAIFMT_LEFT_J |
  624. SND_SOC_POSSIBLE_DAIFMT_AC97;
  625. static const struct snd_soc_dai_ops hdmi_codec_i2s_dai_ops = {
  626. .startup = hdmi_codec_startup,
  627. .shutdown = hdmi_codec_shutdown,
  628. .hw_params = hdmi_codec_hw_params,
  629. .prepare = hdmi_codec_prepare,
  630. .set_fmt = hdmi_codec_i2s_set_fmt,
  631. .mute_stream = hdmi_codec_mute,
  632. .auto_selectable_formats = &hdmi_codec_formats,
  633. .num_auto_selectable_formats = 1,
  634. };
  635. static const struct snd_soc_dai_ops hdmi_codec_spdif_dai_ops = {
  636. .startup = hdmi_codec_startup,
  637. .shutdown = hdmi_codec_shutdown,
  638. .hw_params = hdmi_codec_hw_params,
  639. .mute_stream = hdmi_codec_mute,
  640. };
  641. #define HDMI_RATES (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |\
  642. SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |\
  643. SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |\
  644. SNDRV_PCM_RATE_192000)
  645. #define SPDIF_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
  646. SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_LE)
  647. /*
  648. * This list is only for formats allowed on the I2S bus. So there is
  649. * some formats listed that are not supported by HDMI interface. For
  650. * instance allowing the 32-bit formats enables 24-precision with CPU
  651. * DAIs that do not support 24-bit formats. If the extra formats cause
  652. * problems, we should add the video side driver an option to disable
  653. * them.
  654. */
  655. #define I2S_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
  656. SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_LE |\
  657. SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE)
  658. static struct snd_kcontrol_new hdmi_codec_controls[] = {
  659. {
  660. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  661. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  662. .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, MASK),
  663. .info = hdmi_codec_iec958_info,
  664. .get = hdmi_codec_iec958_mask_get,
  665. },
  666. {
  667. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  668. .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
  669. .info = hdmi_codec_iec958_info,
  670. .get = hdmi_codec_iec958_default_get,
  671. .put = hdmi_codec_iec958_default_put,
  672. },
  673. {
  674. .access = (SNDRV_CTL_ELEM_ACCESS_READ |
  675. SNDRV_CTL_ELEM_ACCESS_VOLATILE),
  676. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  677. .name = "ELD",
  678. .info = hdmi_eld_ctl_info,
  679. .get = hdmi_eld_ctl_get,
  680. },
  681. };
  682. static int hdmi_codec_pcm_new(struct snd_soc_pcm_runtime *rtd,
  683. struct snd_soc_dai *dai)
  684. {
  685. struct snd_soc_dai_driver *drv = dai->driver;
  686. struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
  687. unsigned int i;
  688. int ret;
  689. ret = snd_pcm_add_chmap_ctls(rtd->pcm, SNDRV_PCM_STREAM_PLAYBACK,
  690. NULL, drv->playback.channels_max, 0,
  691. &hcp->chmap_info);
  692. if (ret < 0)
  693. return ret;
  694. /* override handlers */
  695. hcp->chmap_info->private_data = hcp;
  696. hcp->chmap_info->kctl->get = hdmi_codec_chmap_ctl_get;
  697. /* default chmap supported is stereo */
  698. hcp->chmap_info->chmap = hdmi_codec_stereo_chmaps;
  699. hcp->chmap_idx = HDMI_CODEC_CHMAP_IDX_UNKNOWN;
  700. for (i = 0; i < ARRAY_SIZE(hdmi_codec_controls); i++) {
  701. struct snd_kcontrol *kctl;
  702. /* add ELD ctl with the device number corresponding to the PCM stream */
  703. kctl = snd_ctl_new1(&hdmi_codec_controls[i], dai->component);
  704. if (!kctl)
  705. return -ENOMEM;
  706. kctl->id.device = rtd->pcm->device;
  707. ret = snd_ctl_add(rtd->card->snd_card, kctl);
  708. if (ret < 0)
  709. return ret;
  710. }
  711. return 0;
  712. }
  713. static int hdmi_dai_probe(struct snd_soc_dai *dai)
  714. {
  715. struct snd_soc_dapm_context *dapm;
  716. struct hdmi_codec_daifmt *daifmt;
  717. struct snd_soc_dapm_route route[] = {
  718. {
  719. .sink = "TX",
  720. .source = dai->driver->playback.stream_name,
  721. },
  722. {
  723. .sink = dai->driver->capture.stream_name,
  724. .source = "RX",
  725. },
  726. };
  727. int ret;
  728. dapm = snd_soc_component_get_dapm(dai->component);
  729. ret = snd_soc_dapm_add_routes(dapm, route, 2);
  730. if (ret)
  731. return ret;
  732. daifmt = devm_kzalloc(dai->dev, sizeof(*daifmt), GFP_KERNEL);
  733. if (!daifmt)
  734. return -ENOMEM;
  735. dai->playback_dma_data = daifmt;
  736. return 0;
  737. }
  738. static void hdmi_codec_jack_report(struct hdmi_codec_priv *hcp,
  739. unsigned int jack_status)
  740. {
  741. if (hcp->jack && jack_status != hcp->jack_status) {
  742. snd_soc_jack_report(hcp->jack, jack_status, SND_JACK_LINEOUT);
  743. hcp->jack_status = jack_status;
  744. }
  745. }
  746. static void plugged_cb(struct device *dev, bool plugged)
  747. {
  748. struct hdmi_codec_priv *hcp = dev_get_drvdata(dev);
  749. if (plugged) {
  750. if (hcp->hcd.ops->get_eld) {
  751. hcp->hcd.ops->get_eld(dev->parent, hcp->hcd.data,
  752. hcp->eld, sizeof(hcp->eld));
  753. }
  754. hdmi_codec_jack_report(hcp, SND_JACK_LINEOUT);
  755. } else {
  756. hdmi_codec_jack_report(hcp, 0);
  757. memset(hcp->eld, 0, sizeof(hcp->eld));
  758. }
  759. }
  760. static int hdmi_codec_set_jack(struct snd_soc_component *component,
  761. struct snd_soc_jack *jack,
  762. void *data)
  763. {
  764. struct hdmi_codec_priv *hcp = snd_soc_component_get_drvdata(component);
  765. if (hcp->hcd.ops->hook_plugged_cb) {
  766. hcp->jack = jack;
  767. return 0;
  768. }
  769. return -ENOTSUPP;
  770. }
  771. static int hdmi_dai_spdif_probe(struct snd_soc_dai *dai)
  772. {
  773. struct hdmi_codec_daifmt *cf;
  774. int ret;
  775. ret = hdmi_dai_probe(dai);
  776. if (ret)
  777. return ret;
  778. cf = dai->playback_dma_data;
  779. cf->fmt = HDMI_SPDIF;
  780. return 0;
  781. }
  782. static const struct snd_soc_dai_driver hdmi_i2s_dai = {
  783. .name = "i2s-hifi",
  784. .id = DAI_ID_I2S,
  785. .probe = hdmi_dai_probe,
  786. .playback = {
  787. .stream_name = "I2S Playback",
  788. .channels_min = 2,
  789. .channels_max = 8,
  790. .rates = HDMI_RATES,
  791. .formats = I2S_FORMATS,
  792. .sig_bits = 24,
  793. },
  794. .capture = {
  795. .stream_name = "Capture",
  796. .channels_min = 2,
  797. .channels_max = 8,
  798. .rates = HDMI_RATES,
  799. .formats = I2S_FORMATS,
  800. .sig_bits = 24,
  801. },
  802. .ops = &hdmi_codec_i2s_dai_ops,
  803. .pcm_new = hdmi_codec_pcm_new,
  804. };
  805. static const struct snd_soc_dai_driver hdmi_spdif_dai = {
  806. .name = "spdif-hifi",
  807. .id = DAI_ID_SPDIF,
  808. .probe = hdmi_dai_spdif_probe,
  809. .playback = {
  810. .stream_name = "SPDIF Playback",
  811. .channels_min = 2,
  812. .channels_max = 2,
  813. .rates = HDMI_RATES,
  814. .formats = SPDIF_FORMATS,
  815. },
  816. .capture = {
  817. .stream_name = "Capture",
  818. .channels_min = 2,
  819. .channels_max = 2,
  820. .rates = HDMI_RATES,
  821. .formats = SPDIF_FORMATS,
  822. },
  823. .ops = &hdmi_codec_spdif_dai_ops,
  824. .pcm_new = hdmi_codec_pcm_new,
  825. };
  826. static int hdmi_of_xlate_dai_id(struct snd_soc_component *component,
  827. struct device_node *endpoint)
  828. {
  829. struct hdmi_codec_priv *hcp = snd_soc_component_get_drvdata(component);
  830. int ret = -ENOTSUPP; /* see snd_soc_get_dai_id() */
  831. if (hcp->hcd.ops->get_dai_id)
  832. ret = hcp->hcd.ops->get_dai_id(component, endpoint);
  833. return ret;
  834. }
  835. static int hdmi_probe(struct snd_soc_component *component)
  836. {
  837. struct hdmi_codec_priv *hcp = snd_soc_component_get_drvdata(component);
  838. int ret = 0;
  839. if (hcp->hcd.ops->hook_plugged_cb) {
  840. ret = hcp->hcd.ops->hook_plugged_cb(component->dev->parent,
  841. hcp->hcd.data,
  842. plugged_cb,
  843. component->dev);
  844. }
  845. return ret;
  846. }
  847. static void hdmi_remove(struct snd_soc_component *component)
  848. {
  849. struct hdmi_codec_priv *hcp = snd_soc_component_get_drvdata(component);
  850. if (hcp->hcd.ops->hook_plugged_cb)
  851. hcp->hcd.ops->hook_plugged_cb(component->dev->parent,
  852. hcp->hcd.data, NULL, NULL);
  853. }
  854. static const struct snd_soc_component_driver hdmi_driver = {
  855. .probe = hdmi_probe,
  856. .remove = hdmi_remove,
  857. .dapm_widgets = hdmi_widgets,
  858. .num_dapm_widgets = ARRAY_SIZE(hdmi_widgets),
  859. .of_xlate_dai_id = hdmi_of_xlate_dai_id,
  860. .idle_bias_on = 1,
  861. .use_pmdown_time = 1,
  862. .endianness = 1,
  863. .set_jack = hdmi_codec_set_jack,
  864. };
  865. static int hdmi_codec_probe(struct platform_device *pdev)
  866. {
  867. struct hdmi_codec_pdata *hcd = pdev->dev.platform_data;
  868. struct snd_soc_dai_driver *daidrv;
  869. struct device *dev = &pdev->dev;
  870. struct hdmi_codec_priv *hcp;
  871. int dai_count, i = 0;
  872. int ret;
  873. if (!hcd) {
  874. dev_err(dev, "%s: No platform data\n", __func__);
  875. return -EINVAL;
  876. }
  877. dai_count = hcd->i2s + hcd->spdif;
  878. if (dai_count < 1 || !hcd->ops ||
  879. (!hcd->ops->hw_params && !hcd->ops->prepare) ||
  880. !hcd->ops->audio_shutdown) {
  881. dev_err(dev, "%s: Invalid parameters\n", __func__);
  882. return -EINVAL;
  883. }
  884. hcp = devm_kzalloc(dev, sizeof(*hcp), GFP_KERNEL);
  885. if (!hcp)
  886. return -ENOMEM;
  887. hcp->hcd = *hcd;
  888. mutex_init(&hcp->lock);
  889. ret = snd_pcm_create_iec958_consumer_default(hcp->iec_status,
  890. sizeof(hcp->iec_status));
  891. if (ret < 0)
  892. return ret;
  893. daidrv = devm_kcalloc(dev, dai_count, sizeof(*daidrv), GFP_KERNEL);
  894. if (!daidrv)
  895. return -ENOMEM;
  896. if (hcd->i2s) {
  897. daidrv[i] = hdmi_i2s_dai;
  898. daidrv[i].playback.channels_max = hcd->max_i2s_channels;
  899. i++;
  900. }
  901. if (hcd->spdif)
  902. daidrv[i] = hdmi_spdif_dai;
  903. dev_set_drvdata(dev, hcp);
  904. ret = devm_snd_soc_register_component(dev, &hdmi_driver, daidrv,
  905. dai_count);
  906. if (ret) {
  907. dev_err(dev, "%s: snd_soc_register_component() failed (%d)\n",
  908. __func__, ret);
  909. return ret;
  910. }
  911. return 0;
  912. }
  913. static struct platform_driver hdmi_codec_driver = {
  914. .driver = {
  915. .name = HDMI_CODEC_DRV_NAME,
  916. },
  917. .probe = hdmi_codec_probe,
  918. };
  919. module_platform_driver(hdmi_codec_driver);
  920. MODULE_AUTHOR("Jyri Sarha <[email protected]>");
  921. MODULE_DESCRIPTION("HDMI Audio Codec Driver");
  922. MODULE_LICENSE("GPL");
  923. MODULE_ALIAS("platform:" HDMI_CODEC_DRV_NAME);