msm_hdmi_codec_rx.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2012-2019, The Linux Foundation. All rights reserved.
  3. */
  4. #include <linux/platform_device.h>
  5. #include <linux/slab.h>
  6. #include <linux/module.h>
  7. #include <linux/of_device.h>
  8. #include <linux/err.h>
  9. #include <sound/core.h>
  10. #include <sound/pcm.h>
  11. #include <sound/soc.h>
  12. #include <linux/msm_ext_display.h>
  13. #define DRV_NAME "HDMI_codec"
  14. #define MSM_EXT_DISP_PCM_RATES SNDRV_PCM_RATE_48000
  15. #define AUD_EXT_DISP_ACK_DISCONNECT (AUDIO_ACK_CONNECT ^ AUDIO_ACK_CONNECT)
  16. #define AUD_EXT_DISP_ACK_CONNECT (AUDIO_ACK_CONNECT)
  17. #define AUD_EXT_DISP_ACK_ENABLE (AUDIO_ACK_SET_ENABLE | AUDIO_ACK_ENABLE)
  18. #define SOC_EXT_DISP_AUDIO_TYPE(index) \
  19. static SOC_ENUM_SINGLE_DECL(ext_disp_audio_type##index, SND_SOC_NOPM, \
  20. index, ext_disp_audio_type_text)
  21. #define SOC_EXT_DISP_AUDIO_ACK_STATE(index) \
  22. static SOC_ENUM_SINGLE_DECL(ext_disp_audio_ack_state##index, \
  23. SND_SOC_NOPM, index, ext_disp_audio_ack_text)
  24. #define SWITCH_DP_CODEC(codec_info, codec_data, dai_id) \
  25. codec_info.type = EXT_DISPLAY_TYPE_DP; \
  26. codec_info.ctrl_id = codec_data->ctl[dai_id]; \
  27. codec_info.stream_id = codec_data->stream[dai_id]; \
  28. msm_ext_disp_select_audio_codec(codec_data->ext_disp_core_pdev, \
  29. &codec_info)
  30. enum {
  31. DP_CONTROLLER0 = 0,
  32. DP_CONTROLLER_MAX,
  33. };
  34. enum {
  35. DP_STREAM0 = 0,
  36. DP_STREAM1,
  37. DP_STREAM_MAX,
  38. };
  39. enum {
  40. DP_DAI1 = 0,
  41. DP_DAI2,
  42. HDMI_DAI,
  43. DP_DAI_MAX,
  44. };
  45. static const char *const ext_disp_audio_type_text[] = {"None", "HDMI", "DP"};
  46. static const char *const ext_disp_audio_ack_text[] = {"Disconnect", "Connect",
  47. "Ack_Enable"};
  48. SOC_EXT_DISP_AUDIO_TYPE(0);
  49. SOC_EXT_DISP_AUDIO_ACK_STATE(0);
  50. SOC_EXT_DISP_AUDIO_TYPE(1);
  51. SOC_EXT_DISP_AUDIO_ACK_STATE(1);
  52. struct msm_ext_disp_audio_codec_rx_data {
  53. struct platform_device *ext_disp_core_pdev;
  54. struct msm_ext_disp_audio_codec_ops ext_disp_ops;
  55. int cable_status;
  56. struct mutex dp_ops_lock;
  57. int stream[DP_DAI_MAX];
  58. int ctl[DP_DAI_MAX];
  59. };
  60. static int msm_ext_disp_edid_ctl_info(struct snd_kcontrol *kcontrol,
  61. struct snd_ctl_elem_info *uinfo)
  62. {
  63. struct snd_soc_component *component =
  64. snd_soc_kcontrol_component(kcontrol);
  65. struct msm_ext_disp_audio_codec_rx_data *codec_data;
  66. struct msm_ext_disp_audio_edid_blk edid_blk;
  67. int rc = 0;
  68. struct msm_ext_disp_codec_id codec_info;
  69. int dai_id = kcontrol->private_value;
  70. codec_data = snd_soc_component_get_drvdata(component);
  71. if (!codec_data) {
  72. dev_err(component->dev, "%s: codec_data is NULL\n", __func__);
  73. return -EINVAL;
  74. }
  75. if (!codec_data->ext_disp_ops.get_audio_edid_blk) {
  76. dev_dbg(component->dev, "%s: get_audio_edid_blk() is NULL\n",
  77. __func__);
  78. uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
  79. uinfo->count = 0;
  80. return 0;
  81. }
  82. dev_dbg(component->dev, "%s: DP ctl id %d Stream id %d\n", __func__,
  83. codec_data->ctl[dai_id], codec_data->stream[dai_id]);
  84. mutex_lock(&codec_data->dp_ops_lock);
  85. SWITCH_DP_CODEC(codec_info, codec_data, dai_id);
  86. rc = codec_data->ext_disp_ops.get_audio_edid_blk(
  87. codec_data->ext_disp_core_pdev, &edid_blk);
  88. mutex_unlock(&codec_data->dp_ops_lock);
  89. if (rc >= 0) {
  90. uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
  91. uinfo->count = edid_blk.audio_data_blk_size +
  92. edid_blk.spk_alloc_data_blk_size;
  93. }
  94. dev_dbg(component->dev, "%s: count: %d\n", __func__, uinfo->count);
  95. return rc;
  96. }
  97. static int msm_ext_disp_edid_get(struct snd_kcontrol *kcontrol,
  98. struct snd_ctl_elem_value *ucontrol) {
  99. struct snd_soc_component *component =
  100. snd_soc_kcontrol_component(kcontrol);
  101. struct msm_ext_disp_audio_codec_rx_data *codec_data;
  102. struct msm_ext_disp_audio_edid_blk edid_blk;
  103. struct msm_ext_disp_codec_id codec_info;
  104. int rc = 0;
  105. int dai_id = kcontrol->private_value;
  106. codec_data = snd_soc_component_get_drvdata(component);
  107. if (!codec_data || !codec_data->ext_disp_ops.get_audio_edid_blk) {
  108. dev_err(component->dev, "%s: codec_data or get_audio_edid_blk() is NULL\n",
  109. __func__);
  110. return -EINVAL;
  111. }
  112. dev_dbg(component->dev, "%s: DP ctl id %d Stream id %d\n", __func__,
  113. codec_data->ctl[dai_id], codec_data->stream[dai_id]);
  114. mutex_lock(&codec_data->dp_ops_lock);
  115. SWITCH_DP_CODEC(codec_info, codec_data, dai_id);
  116. rc = codec_data->ext_disp_ops.get_audio_edid_blk(
  117. codec_data->ext_disp_core_pdev, &edid_blk);
  118. mutex_unlock(&codec_data->dp_ops_lock);
  119. if (rc >= 0) {
  120. if (sizeof(ucontrol->value.bytes.data) <
  121. (edid_blk.audio_data_blk_size +
  122. edid_blk.spk_alloc_data_blk_size)) {
  123. dev_err(component->dev,
  124. "%s: Not enough memory to copy EDID data\n",
  125. __func__);
  126. return -ENOMEM;
  127. }
  128. memcpy(ucontrol->value.bytes.data,
  129. edid_blk.audio_data_blk,
  130. edid_blk.audio_data_blk_size);
  131. memcpy((ucontrol->value.bytes.data +
  132. edid_blk.audio_data_blk_size),
  133. edid_blk.spk_alloc_data_blk,
  134. edid_blk.spk_alloc_data_blk_size);
  135. dev_dbg(component->dev, "%s: data_blk_size:%d, spk_alloc_data_blk_size:%d\n",
  136. __func__, edid_blk.audio_data_blk_size,
  137. edid_blk.spk_alloc_data_blk_size);
  138. }
  139. return rc;
  140. }
  141. static int msm_ext_disp_audio_type_get(struct snd_kcontrol *kcontrol,
  142. struct snd_ctl_elem_value *ucontrol)
  143. {
  144. struct snd_soc_component *component =
  145. snd_soc_kcontrol_component(kcontrol);
  146. struct msm_ext_disp_audio_codec_rx_data *codec_data;
  147. enum msm_ext_disp_cable_state cable_state;
  148. enum msm_ext_disp_type disp_type;
  149. struct msm_ext_disp_codec_id codec_info;
  150. int rc = 0;
  151. int dai_id = ((struct soc_enum *) kcontrol->private_value)->shift_l;
  152. codec_data = snd_soc_component_get_drvdata(component);
  153. if (!codec_data ||
  154. !codec_data->ext_disp_ops.get_audio_edid_blk ||
  155. !codec_data->ext_disp_ops.get_intf_id) {
  156. dev_err(component->dev, "%s: codec_data, get_audio_edid_blk() or get_intf_id is NULL\n",
  157. __func__);
  158. return -EINVAL;
  159. }
  160. dev_dbg(component->dev, "%s: DP ctl id %d Stream id %d\n", __func__,
  161. codec_data->ctl[dai_id], codec_data->stream[dai_id]);
  162. mutex_lock(&codec_data->dp_ops_lock);
  163. SWITCH_DP_CODEC(codec_info, codec_data, dai_id);
  164. cable_state = codec_data->ext_disp_ops.cable_status(
  165. codec_data->ext_disp_core_pdev, 1);
  166. if (cable_state < 0) {
  167. dev_err(component->dev, "%s: Error retrieving cable state from ext_disp, err:%d\n",
  168. __func__, cable_state);
  169. rc = cable_state;
  170. goto cable_err;
  171. }
  172. codec_data->cable_status = cable_state;
  173. if (cable_state == EXT_DISPLAY_CABLE_DISCONNECT) {
  174. dev_err(component->dev, "%s: Display cable disconnected\n",
  175. __func__);
  176. ucontrol->value.integer.value[0] = 0;
  177. rc = 0;
  178. goto cable_err;
  179. }
  180. disp_type = codec_data->ext_disp_ops.get_intf_id(
  181. codec_data->ext_disp_core_pdev);
  182. mutex_unlock(&codec_data->dp_ops_lock);
  183. if (disp_type >= 0) {
  184. switch (disp_type) {
  185. case EXT_DISPLAY_TYPE_DP:
  186. ucontrol->value.integer.value[0] = 2;
  187. rc = 0;
  188. break;
  189. case EXT_DISPLAY_TYPE_HDMI:
  190. ucontrol->value.integer.value[0] = 1;
  191. rc = 0;
  192. break;
  193. default:
  194. rc = -EINVAL;
  195. dev_err(component->dev, "%s: Invalid disp_type:%d\n",
  196. __func__, disp_type);
  197. goto done;
  198. }
  199. dev_dbg(component->dev, "%s: Display type: %d\n",
  200. __func__, disp_type);
  201. } else {
  202. dev_err(component->dev, "%s: Error retrieving disp_type from ext_disp, err:%d\n",
  203. __func__, disp_type);
  204. rc = disp_type;
  205. }
  206. return rc;
  207. cable_err:
  208. mutex_unlock(&codec_data->dp_ops_lock);
  209. done:
  210. return rc;
  211. }
  212. static int msm_ext_disp_audio_ack_set(struct snd_kcontrol *kcontrol,
  213. struct snd_ctl_elem_value *ucontrol)
  214. {
  215. struct snd_soc_component *component =
  216. snd_soc_kcontrol_component(kcontrol);
  217. struct msm_ext_disp_audio_codec_rx_data *codec_data;
  218. u32 ack_state = 0;
  219. struct msm_ext_disp_codec_id codec_info;
  220. int rc = 0;
  221. int dai_id = ((struct soc_enum *) kcontrol->private_value)->shift_l;
  222. codec_data = snd_soc_component_get_drvdata(component);
  223. if (!codec_data ||
  224. !codec_data->ext_disp_ops.acknowledge) {
  225. dev_err(component->dev,
  226. "%s: codec_data or ops acknowledge() is NULL\n",
  227. __func__);
  228. rc = -EINVAL;
  229. goto done;
  230. }
  231. dev_dbg(component->dev, "%s: DP ctl id %d Stream id %d\n", __func__,
  232. codec_data->ctl[dai_id], codec_data->stream[dai_id]);
  233. switch (ucontrol->value.enumerated.item[0]) {
  234. case 0:
  235. ack_state = AUD_EXT_DISP_ACK_DISCONNECT;
  236. break;
  237. case 1:
  238. ack_state = AUD_EXT_DISP_ACK_CONNECT;
  239. break;
  240. case 2:
  241. ack_state = AUD_EXT_DISP_ACK_ENABLE;
  242. break;
  243. default:
  244. rc = -EINVAL;
  245. dev_err(component->dev,
  246. "%s: invalid value %d for mixer ctl\n",
  247. __func__, ucontrol->value.enumerated.item[0]);
  248. goto done;
  249. }
  250. dev_dbg(component->dev, "%s: control %d, ack set value 0x%x\n",
  251. __func__, ucontrol->value.enumerated.item[0], ack_state);
  252. mutex_lock(&codec_data->dp_ops_lock);
  253. SWITCH_DP_CODEC(codec_info, codec_data, dai_id);
  254. rc = codec_data->ext_disp_ops.acknowledge(
  255. codec_data->ext_disp_core_pdev, ack_state);
  256. mutex_unlock(&codec_data->dp_ops_lock);
  257. if (rc < 0) {
  258. dev_err(component->dev, "%s: error from acknowledge(), err:%d\n",
  259. __func__, rc);
  260. }
  261. done:
  262. return rc;
  263. }
  264. static int msm_ext_disp_audio_device_get(struct snd_kcontrol *kcontrol,
  265. struct snd_ctl_elem_value *ucontrol)
  266. {
  267. struct snd_soc_component *component =
  268. snd_soc_kcontrol_component(kcontrol);
  269. struct msm_ext_disp_audio_codec_rx_data *codec_data;
  270. int rc = 0;
  271. int dai_id = ((struct soc_multi_mixer_control *)
  272. kcontrol->private_value)->shift;
  273. if (dai_id < 0 || dai_id > DP_DAI2) {
  274. dev_err(component->dev,
  275. "%s: invalid dai id: %d\n", __func__, dai_id);
  276. rc = -EINVAL;
  277. goto done;
  278. }
  279. codec_data = snd_soc_component_get_drvdata(component);
  280. if (!codec_data) {
  281. dev_err(component->dev,
  282. "%s: codec_data or ops acknowledge() is NULL\n",
  283. __func__);
  284. rc = -EINVAL;
  285. goto done;
  286. }
  287. ucontrol->value.integer.value[0] = codec_data->ctl[dai_id];
  288. ucontrol->value.integer.value[1] = codec_data->stream[dai_id];
  289. done:
  290. return rc;
  291. }
  292. static int msm_ext_disp_audio_device_set(struct snd_kcontrol *kcontrol,
  293. struct snd_ctl_elem_value *ucontrol)
  294. {
  295. struct snd_soc_component *component =
  296. snd_soc_kcontrol_component(kcontrol);
  297. struct msm_ext_disp_audio_codec_rx_data *codec_data;
  298. int rc = 0;
  299. int dai_id = ((struct soc_multi_mixer_control *)
  300. kcontrol->private_value)->shift;
  301. if (dai_id < 0 || dai_id > DP_DAI2) {
  302. dev_err(component->dev,
  303. "%s: invalid dai id: %d\n", __func__, dai_id);
  304. rc = -EINVAL;
  305. goto done;
  306. }
  307. codec_data = snd_soc_component_get_drvdata(component);
  308. if (!codec_data) {
  309. dev_err(component->dev,
  310. "%s: codec_data or ops acknowledge() is NULL\n",
  311. __func__);
  312. rc = -EINVAL;
  313. goto done;
  314. }
  315. if ((ucontrol->value.integer.value[0] > (DP_CONTROLLER_MAX - 1)) ||
  316. (ucontrol->value.integer.value[1] > (DP_STREAM_MAX - 1)) ||
  317. (ucontrol->value.integer.value[0] < 0) ||
  318. (ucontrol->value.integer.value[1] < 0)) {
  319. dev_err(component->dev,
  320. "%s: DP audio control index invalid\n",
  321. __func__);
  322. rc = -EINVAL;
  323. goto done;
  324. }
  325. mutex_lock(&codec_data->dp_ops_lock);
  326. codec_data->ctl[dai_id] = ucontrol->value.integer.value[0];
  327. codec_data->stream[dai_id] = ucontrol->value.integer.value[1];
  328. mutex_unlock(&codec_data->dp_ops_lock);
  329. done:
  330. return rc;
  331. }
  332. static const struct snd_kcontrol_new msm_ext_disp_codec_rx_controls[] = {
  333. {
  334. .access = SNDRV_CTL_ELEM_ACCESS_READ |
  335. SNDRV_CTL_ELEM_ACCESS_VOLATILE,
  336. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  337. .name = "HDMI EDID",
  338. .info = msm_ext_disp_edid_ctl_info,
  339. .get = msm_ext_disp_edid_get,
  340. .private_value = HDMI_DAI,
  341. },
  342. {
  343. .access = SNDRV_CTL_ELEM_ACCESS_READ |
  344. SNDRV_CTL_ELEM_ACCESS_VOLATILE,
  345. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  346. .name = "Display Port EDID",
  347. .info = msm_ext_disp_edid_ctl_info,
  348. .get = msm_ext_disp_edid_get,
  349. .private_value = DP_DAI1,
  350. },
  351. {
  352. .access = SNDRV_CTL_ELEM_ACCESS_READ |
  353. SNDRV_CTL_ELEM_ACCESS_VOLATILE,
  354. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  355. .name = "Display Port1 EDID",
  356. .info = msm_ext_disp_edid_ctl_info,
  357. .get = msm_ext_disp_edid_get,
  358. .private_value = DP_DAI2,
  359. },
  360. SOC_ENUM_EXT("External Display Type",
  361. ext_disp_audio_type0,
  362. msm_ext_disp_audio_type_get, NULL),
  363. SOC_ENUM_EXT("External Display1 Type",
  364. ext_disp_audio_type1,
  365. msm_ext_disp_audio_type_get, NULL),
  366. SOC_ENUM_EXT("External Display Audio Ack",
  367. ext_disp_audio_ack_state0,
  368. NULL, msm_ext_disp_audio_ack_set),
  369. SOC_ENUM_EXT("External Display1 Audio Ack",
  370. ext_disp_audio_ack_state1,
  371. NULL, msm_ext_disp_audio_ack_set),
  372. SOC_SINGLE_MULTI_EXT("External Display Audio Device",
  373. SND_SOC_NOPM, DP_DAI1, DP_STREAM_MAX - 1, 0, 2,
  374. msm_ext_disp_audio_device_get,
  375. msm_ext_disp_audio_device_set),
  376. SOC_SINGLE_MULTI_EXT("External Display1 Audio Device",
  377. SND_SOC_NOPM, DP_DAI2, DP_STREAM_MAX - 1, 0, 2,
  378. msm_ext_disp_audio_device_get,
  379. msm_ext_disp_audio_device_set),
  380. };
  381. static int msm_ext_disp_audio_codec_rx_dai_startup(
  382. struct snd_pcm_substream *substream,
  383. struct snd_soc_dai *dai)
  384. {
  385. int ret = 0;
  386. struct msm_ext_disp_codec_id codec_info;
  387. struct msm_ext_disp_audio_codec_rx_data *codec_data =
  388. dev_get_drvdata(dai->component->dev);
  389. if (!codec_data || !codec_data->ext_disp_ops.cable_status) {
  390. dev_err(dai->dev, "%s() codec_data or cable_status is null\n",
  391. __func__);
  392. return -EINVAL;
  393. }
  394. dev_dbg(dai->component->dev, "%s: DP ctl id %d Stream id %d\n",
  395. __func__,
  396. codec_data->ctl[dai->id], codec_data->stream[dai->id]);
  397. mutex_lock(&codec_data->dp_ops_lock);
  398. SWITCH_DP_CODEC(codec_info, codec_data, dai->id);
  399. codec_data->cable_status =
  400. codec_data->ext_disp_ops.cable_status(
  401. codec_data->ext_disp_core_pdev, 1);
  402. mutex_unlock(&codec_data->dp_ops_lock);
  403. if (codec_data->cable_status < 0) {
  404. dev_err(dai->dev,
  405. "%s() ext disp core is not ready (ret val = %d)\n",
  406. __func__, codec_data->cable_status);
  407. ret = codec_data->cable_status;
  408. } else if (!codec_data->cable_status) {
  409. dev_err(dai->dev,
  410. "%s() ext disp cable is not connected (ret val = %d)\n",
  411. __func__, codec_data->cable_status);
  412. ret = -ENODEV;
  413. }
  414. return ret;
  415. }
  416. static int msm_ext_disp_audio_codec_rx_dai_hw_params(
  417. struct snd_pcm_substream *substream,
  418. struct snd_pcm_hw_params *params,
  419. struct snd_soc_dai *dai)
  420. {
  421. u32 channel_allocation = 0;
  422. u32 level_shift = 0; /* 0dB */
  423. bool down_mix = 0;
  424. u32 num_channels = params_channels(params);
  425. struct msm_ext_disp_codec_id codec_info;
  426. int rc = 0;
  427. struct msm_ext_disp_audio_setup_params audio_setup_params = {0};
  428. struct msm_ext_disp_audio_codec_rx_data *codec_data =
  429. dev_get_drvdata(dai->component->dev);
  430. if (!codec_data || !codec_data->ext_disp_ops.audio_info_setup) {
  431. dev_err(dai->dev, "%s: codec_data or audio_info_setup is null\n",
  432. __func__);
  433. return -EINVAL;
  434. }
  435. dev_dbg(dai->component->dev, "%s: DP ctl id %d Stream id %d\n",
  436. __func__,
  437. codec_data->ctl[dai->id], codec_data->stream[dai->id]);
  438. if (codec_data->cable_status < 0) {
  439. dev_err_ratelimited(dai->dev,
  440. "%s() ext disp core is not ready (ret val = %d)\n",
  441. __func__, codec_data->cable_status);
  442. return codec_data->cable_status;
  443. } else if (!codec_data->cable_status) {
  444. dev_err_ratelimited(dai->dev,
  445. "%s() ext disp cable is not connected (ret val = %d)\n",
  446. __func__, codec_data->cable_status);
  447. return -ENODEV;
  448. }
  449. /*refer to HDMI spec CEA-861-E: Table 28 Audio InfoFrame Data Byte 4*/
  450. switch (num_channels) {
  451. case 2:
  452. channel_allocation = 0;
  453. break;
  454. case 3:
  455. channel_allocation = 0x02;/*default to FL/FR/FC*/
  456. audio_setup_params.sample_present = 0x3;
  457. break;
  458. case 4:
  459. channel_allocation = 0x06;/*default to FL/FR/FC/RC*/
  460. audio_setup_params.sample_present = 0x7;
  461. break;
  462. case 5:
  463. channel_allocation = 0x0A;/*default to FL/FR/FC/RR/RL*/
  464. audio_setup_params.sample_present = 0x7;
  465. break;
  466. case 6:
  467. channel_allocation = 0x0B;
  468. audio_setup_params.sample_present = 0x7;
  469. break;
  470. case 7:
  471. channel_allocation = 0x12;/*default to FL/FR/FC/RL/RR/RRC/RLC*/
  472. audio_setup_params.sample_present = 0xf;
  473. break;
  474. case 8:
  475. channel_allocation = 0x13;
  476. audio_setup_params.sample_present = 0xf;
  477. break;
  478. default:
  479. dev_err(dai->dev, "invalid Channels = %u\n", num_channels);
  480. return -EINVAL;
  481. }
  482. dev_dbg(dai->dev,
  483. "%s() num_ch %u samplerate %u channel_allocation = %u\n",
  484. __func__, num_channels, params_rate(params),
  485. channel_allocation);
  486. audio_setup_params.sample_rate_hz = params_rate(params);
  487. audio_setup_params.num_of_channels = num_channels;
  488. audio_setup_params.channel_allocation = channel_allocation;
  489. audio_setup_params.level_shift = level_shift;
  490. audio_setup_params.down_mix = down_mix;
  491. mutex_lock(&codec_data->dp_ops_lock);
  492. SWITCH_DP_CODEC(codec_info, codec_data, dai->id);
  493. rc = codec_data->ext_disp_ops.audio_info_setup(
  494. codec_data->ext_disp_core_pdev, &audio_setup_params);
  495. mutex_unlock(&codec_data->dp_ops_lock);
  496. if (rc < 0) {
  497. dev_err_ratelimited(dai->dev,
  498. "%s() ext disp core is not ready, rc: %d\n",
  499. __func__, rc);
  500. }
  501. return rc;
  502. }
  503. static void msm_ext_disp_audio_codec_rx_dai_shutdown(
  504. struct snd_pcm_substream *substream,
  505. struct snd_soc_dai *dai)
  506. {
  507. int rc = 0;
  508. struct msm_ext_disp_codec_id codec_info;
  509. struct msm_ext_disp_audio_codec_rx_data *codec_data =
  510. dev_get_drvdata(dai->component->dev);
  511. if (!codec_data || !codec_data->ext_disp_ops.teardown_done ||
  512. !codec_data->ext_disp_ops.cable_status) {
  513. dev_err(dai->dev, "%s: codec data or teardown_done or cable_status is null\n",
  514. __func__);
  515. return;
  516. }
  517. dev_dbg(dai->component->dev, "%s: DP ctl id %d Stream id %d\n",
  518. __func__,
  519. codec_data->ctl[dai->id], codec_data->stream[dai->id]);
  520. mutex_lock(&codec_data->dp_ops_lock);
  521. SWITCH_DP_CODEC(codec_info, codec_data, dai->id);
  522. rc = codec_data->ext_disp_ops.cable_status(
  523. codec_data->ext_disp_core_pdev, 0);
  524. if (rc < 0) {
  525. dev_err(dai->dev,
  526. "%s: ext disp core had problems releasing audio flag\n",
  527. __func__);
  528. }
  529. codec_data->ext_disp_ops.teardown_done(
  530. codec_data->ext_disp_core_pdev);
  531. mutex_unlock(&codec_data->dp_ops_lock);
  532. }
  533. static int msm_ext_disp_audio_codec_rx_probe(
  534. struct snd_soc_component *component)
  535. {
  536. struct msm_ext_disp_audio_codec_rx_data *codec_data;
  537. struct device_node *of_node_parent = NULL;
  538. codec_data = kzalloc(sizeof(struct msm_ext_disp_audio_codec_rx_data),
  539. GFP_KERNEL);
  540. if (!codec_data) {
  541. dev_err(component->dev, "%s(): fail to allocate dai data\n",
  542. __func__);
  543. return -ENOMEM;
  544. }
  545. of_node_parent = of_get_parent(component->dev->of_node);
  546. if (!of_node_parent) {
  547. dev_err(component->dev, "%s(): Parent device tree node not found\n",
  548. __func__);
  549. kfree(codec_data);
  550. return -ENODEV;
  551. }
  552. codec_data->ext_disp_core_pdev = of_find_device_by_node(of_node_parent);
  553. if (!codec_data->ext_disp_core_pdev) {
  554. dev_err(component->dev, "%s(): can't get parent pdev\n",
  555. __func__);
  556. kfree(codec_data);
  557. return -ENODEV;
  558. }
  559. if (msm_ext_disp_register_audio_codec(codec_data->ext_disp_core_pdev,
  560. &codec_data->ext_disp_ops)) {
  561. dev_err(component->dev, "%s(): can't register with ext disp core",
  562. __func__);
  563. kfree(codec_data);
  564. return -ENODEV;
  565. }
  566. mutex_init(&codec_data->dp_ops_lock);
  567. dev_set_drvdata(component->dev, codec_data);
  568. dev_dbg(component->dev, "%s(): registered %s with ext disp core\n",
  569. __func__, component->name);
  570. return 0;
  571. }
  572. static void msm_ext_disp_audio_codec_rx_remove(
  573. struct snd_soc_component *component)
  574. {
  575. struct msm_ext_disp_audio_codec_rx_data *codec_data;
  576. codec_data = dev_get_drvdata(component->dev);
  577. mutex_destroy(&codec_data->dp_ops_lock);
  578. kfree(codec_data);
  579. return;
  580. }
  581. static struct snd_soc_dai_ops msm_ext_disp_audio_codec_rx_dai_ops = {
  582. .startup = msm_ext_disp_audio_codec_rx_dai_startup,
  583. .hw_params = msm_ext_disp_audio_codec_rx_dai_hw_params,
  584. .shutdown = msm_ext_disp_audio_codec_rx_dai_shutdown
  585. };
  586. static struct snd_soc_dai_driver msm_ext_disp_audio_codec_rx_dais[] = {
  587. {
  588. .name = "msm_hdmi_audio_codec_rx_dai",
  589. .id = HDMI_DAI,
  590. .playback = {
  591. .stream_name = "HDMI Playback",
  592. .channels_min = 1,
  593. .channels_max = 8,
  594. .rate_min = 48000,
  595. .rate_max = 48000,
  596. .rates = MSM_EXT_DISP_PCM_RATES,
  597. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  598. },
  599. .ops = &msm_ext_disp_audio_codec_rx_dai_ops,
  600. },
  601. {
  602. .name = "msm_dp_audio_codec_rx_dai",
  603. .id = DP_DAI1,
  604. .playback = {
  605. .stream_name = "Display Port Playback",
  606. .channels_min = 1,
  607. .channels_max = 8,
  608. .rate_min = 48000,
  609. .rate_max = 192000,
  610. .rates = SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_96000 |
  611. SNDRV_PCM_RATE_192000,
  612. .formats = SNDRV_PCM_FMTBIT_S16_LE |
  613. SNDRV_PCM_FMTBIT_S24_LE |
  614. SNDRV_PCM_FMTBIT_S24_3LE,
  615. },
  616. .ops = &msm_ext_disp_audio_codec_rx_dai_ops,
  617. },
  618. {
  619. .name = "msm_dp_audio_codec_rx1_dai",
  620. .id = DP_DAI2,
  621. .playback = {
  622. .stream_name = "Display Port1 Playback",
  623. .channels_min = 1,
  624. .channels_max = 8,
  625. .rate_min = 48000,
  626. .rate_max = 192000,
  627. .rates = SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_96000 |
  628. SNDRV_PCM_RATE_192000,
  629. .formats = SNDRV_PCM_FMTBIT_S16_LE |
  630. SNDRV_PCM_FMTBIT_S24_LE |
  631. SNDRV_PCM_FMTBIT_S24_3LE,
  632. },
  633. .ops = &msm_ext_disp_audio_codec_rx_dai_ops,
  634. },
  635. };
  636. static const struct snd_soc_component_driver msm_ext_disp_codec_rx_driver = {
  637. .name = DRV_NAME,
  638. .probe = msm_ext_disp_audio_codec_rx_probe,
  639. .remove = msm_ext_disp_audio_codec_rx_remove,
  640. .controls = msm_ext_disp_codec_rx_controls,
  641. .num_controls = ARRAY_SIZE(msm_ext_disp_codec_rx_controls),
  642. };
  643. static int msm_ext_disp_audio_codec_rx_plat_probe(
  644. struct platform_device *pdev)
  645. {
  646. dev_dbg(&pdev->dev, "%s(): dev name %s\n", __func__,
  647. dev_name(&pdev->dev));
  648. return snd_soc_register_component(&pdev->dev,
  649. &msm_ext_disp_codec_rx_driver,
  650. msm_ext_disp_audio_codec_rx_dais,
  651. ARRAY_SIZE(msm_ext_disp_audio_codec_rx_dais));
  652. }
  653. static int msm_ext_disp_audio_codec_rx_plat_remove(
  654. struct platform_device *pdev)
  655. {
  656. snd_soc_unregister_component(&pdev->dev);
  657. return 0;
  658. }
  659. static const struct of_device_id msm_ext_disp_audio_codec_rx_dt_match[] = {
  660. { .compatible = "qcom,msm-ext-disp-audio-codec-rx", },
  661. {}
  662. };
  663. MODULE_DEVICE_TABLE(of, msm_ext_disp_audio_codec_rx_dt_match);
  664. static struct platform_driver msm_ext_disp_audio_codec_rx_driver = {
  665. .driver = {
  666. .name = "msm-ext-disp-audio-codec-rx",
  667. .owner = THIS_MODULE,
  668. .of_match_table = msm_ext_disp_audio_codec_rx_dt_match,
  669. .suppress_bind_attrs = true,
  670. },
  671. .probe = msm_ext_disp_audio_codec_rx_plat_probe,
  672. .remove = msm_ext_disp_audio_codec_rx_plat_remove,
  673. };
  674. static int __init msm_ext_disp_audio_codec_rx_init(void)
  675. {
  676. int rc = 0;
  677. rc = platform_driver_register(&msm_ext_disp_audio_codec_rx_driver);
  678. if (rc) {
  679. pr_err("%s: failed to register ext disp codec driver err:%d\n",
  680. __func__, rc);
  681. }
  682. return rc;
  683. }
  684. module_init(msm_ext_disp_audio_codec_rx_init);
  685. static void __exit msm_ext_disp_audio_codec_rx_exit(void)
  686. {
  687. platform_driver_unregister(&msm_ext_disp_audio_codec_rx_driver);
  688. }
  689. module_exit(msm_ext_disp_audio_codec_rx_exit);
  690. MODULE_DESCRIPTION("MSM External Display Audio CODEC Driver");
  691. MODULE_LICENSE("GPL v2");