msm-dts-srs-tm-config.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2012-2014, 2016-2018, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/err.h>
  6. #include <linux/module.h>
  7. #include <linux/bitops.h>
  8. #include <linux/mutex.h>
  9. #include <linux/atomic.h>
  10. #include <sound/asound.h>
  11. #include <sound/control.h>
  12. #include <dsp/msm_audio_ion.h>
  13. #include <dsp/q6adm-v2.h>
  14. #include <dsp/msm-dts-srs-tm-config.h>
  15. static int srs_port_id[AFE_MAX_PORTS] = {-1};
  16. static int srs_copp_idx[AFE_MAX_PORTS] = {-1};
  17. static union srs_trumedia_params_u msm_srs_trumedia_params;
  18. struct dma_buf *dma_buf;
  19. static struct param_outband po;
  20. static atomic_t ref_cnt;
  21. #define ION_MEM_SIZE (8 * 1024)
  22. static int set_port_id(int port_id, int copp_idx)
  23. {
  24. int index = adm_validate_and_get_port_index(port_id);
  25. if (index < 0) {
  26. pr_err("%s: Invalid port idx %d port_id %#x\n", __func__, index,
  27. port_id);
  28. return -EINVAL;
  29. }
  30. srs_port_id[index] = port_id;
  31. srs_copp_idx[index] = copp_idx;
  32. return 0;
  33. }
  34. static void msm_dts_srs_tm_send_params(__s32 port_id, __u32 techs)
  35. {
  36. __s32 index = adm_validate_and_get_port_index(port_id);
  37. if (index < 0) {
  38. pr_err("%s: Invalid port idx %d port_id 0x%x\n",
  39. __func__, index, port_id);
  40. return;
  41. }
  42. if ((srs_copp_idx[index] < 0) ||
  43. (srs_copp_idx[index] >= MAX_COPPS_PER_PORT)) {
  44. pr_debug("%s: send params called before copp open. so, caching\n",
  45. __func__);
  46. return;
  47. }
  48. pr_debug("SRS %s: called, port_id = %d, techs flags = %u\n",
  49. __func__, port_id, techs);
  50. /* force all if techs is set to 1 */
  51. if (techs == 1)
  52. techs = 0xFFFFFFFF;
  53. if (techs & (1 << SRS_ID_WOWHD))
  54. srs_trumedia_open(port_id, srs_copp_idx[index], SRS_ID_WOWHD,
  55. (void *)&msm_srs_trumedia_params.srs_params.wowhd);
  56. if (techs & (1 << SRS_ID_CSHP))
  57. srs_trumedia_open(port_id, srs_copp_idx[index], SRS_ID_CSHP,
  58. (void *)&msm_srs_trumedia_params.srs_params.cshp);
  59. if (techs & (1 << SRS_ID_HPF))
  60. srs_trumedia_open(port_id, srs_copp_idx[index], SRS_ID_HPF,
  61. (void *)&msm_srs_trumedia_params.srs_params.hpf);
  62. if (techs & (1 << SRS_ID_AEQ))
  63. srs_trumedia_open(port_id, srs_copp_idx[index], SRS_ID_AEQ,
  64. (void *)&msm_srs_trumedia_params.srs_params.aeq);
  65. if (techs & (1 << SRS_ID_HL))
  66. srs_trumedia_open(port_id, srs_copp_idx[index], SRS_ID_HL,
  67. (void *)&msm_srs_trumedia_params.srs_params.hl);
  68. if (techs & (1 << SRS_ID_GEQ))
  69. srs_trumedia_open(port_id, srs_copp_idx[index], SRS_ID_GEQ,
  70. (void *)&msm_srs_trumedia_params.srs_params.geq);
  71. if (techs & (1 << SRS_ID_GLOBAL))
  72. srs_trumedia_open(port_id, srs_copp_idx[index], SRS_ID_GLOBAL,
  73. (void *)&msm_srs_trumedia_params.srs_params.global);
  74. }
  75. static int msm_dts_srs_trumedia_control_get(struct snd_kcontrol *kcontrol,
  76. struct snd_ctl_elem_value *ucontrol)
  77. {
  78. ucontrol->value.integer.value[0] = 0;
  79. return 0;
  80. }
  81. static int msm_dts_srs_trumedia_control_set_(int port_id,
  82. struct snd_kcontrol *kcontrol,
  83. struct snd_ctl_elem_value *ucontrol)
  84. {
  85. __u16 offset, value, max = sizeof(msm_srs_trumedia_params) >> 1;
  86. if (SRS_CMD_UPLOAD ==
  87. (ucontrol->value.integer.value[0] & SRS_CMD_UPLOAD)) {
  88. __u32 techs = ucontrol->value.integer.value[0] & 0xFF;
  89. __s32 index = adm_validate_and_get_port_index(port_id);
  90. if (index < 0) {
  91. pr_err("%s: Invalid port idx %d port_id 0x%x\n",
  92. __func__, index, port_id);
  93. return -EINVAL;
  94. }
  95. pr_debug("SRS %s: send params request, flag = %u\n",
  96. __func__, techs);
  97. if (srs_port_id[index] >= 0 && techs)
  98. msm_dts_srs_tm_send_params(port_id, techs);
  99. return 0;
  100. }
  101. offset = (__u16)((ucontrol->value.integer.value[0] &
  102. SRS_PARAM_OFFSET_MASK) >> 16);
  103. value = (__u16)(ucontrol->value.integer.value[0] &
  104. SRS_PARAM_VALUE_MASK);
  105. if (offset < max) {
  106. msm_srs_trumedia_params.raw_params[offset] = value;
  107. pr_debug("SRS %s: index set... (max %d, requested %d, value 0x%X)\n",
  108. __func__, max, offset, value);
  109. } else {
  110. pr_err("SRS %s: index out of bounds! (max %d, requested %d)\n",
  111. __func__, max, offset);
  112. }
  113. return 0;
  114. }
  115. static int msm_dts_srs_trumedia_control_set(struct snd_kcontrol *kcontrol,
  116. struct snd_ctl_elem_value *ucontrol)
  117. {
  118. int ret, port_id;
  119. pr_debug("SRS control normal called\n");
  120. msm_dts_srs_acquire_lock();
  121. port_id = SLIMBUS_0_RX;
  122. ret = msm_dts_srs_trumedia_control_set_(port_id, kcontrol, ucontrol);
  123. msm_dts_srs_release_lock();
  124. return ret;
  125. }
  126. static int msm_dts_srs_trumedia_control_i2s_set(struct snd_kcontrol *kcontrol,
  127. struct snd_ctl_elem_value *ucontrol)
  128. {
  129. int ret, port_id;
  130. pr_debug("SRS control I2S called\n");
  131. msm_dts_srs_acquire_lock();
  132. port_id = PRIMARY_I2S_RX;
  133. ret = msm_dts_srs_trumedia_control_set_(port_id, kcontrol, ucontrol);
  134. msm_dts_srs_release_lock();
  135. return ret;
  136. }
  137. static int msm_dts_srs_trumedia_control_mi2s_set(struct snd_kcontrol *kcontrol,
  138. struct snd_ctl_elem_value *ucontrol)
  139. {
  140. int ret, port_id;
  141. pr_debug("SRS control MI2S called\n");
  142. msm_dts_srs_acquire_lock();
  143. port_id = AFE_PORT_ID_PRIMARY_MI2S_RX;
  144. ret = msm_dts_srs_trumedia_control_set_(port_id, kcontrol, ucontrol);
  145. msm_dts_srs_release_lock();
  146. return ret;
  147. }
  148. static int msm_dts_srs_trumedia_control_hdmi_set(struct snd_kcontrol *kcontrol,
  149. struct snd_ctl_elem_value *ucontrol)
  150. {
  151. int ret, port_id;
  152. pr_debug("SRS control HDMI called\n");
  153. msm_dts_srs_acquire_lock();
  154. port_id = HDMI_RX;
  155. ret = msm_dts_srs_trumedia_control_set_(port_id, kcontrol, ucontrol);
  156. msm_dts_srs_release_lock();
  157. return ret;
  158. }
  159. static const struct snd_kcontrol_new lpa_srs_trumedia_controls[] = {
  160. {.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  161. .name = "SRS TruMedia",
  162. .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |
  163. SNDRV_CTL_ELEM_ACCESS_READWRITE,
  164. .info = snd_soc_info_volsw,
  165. .get = msm_dts_srs_trumedia_control_get,
  166. .put = msm_dts_srs_trumedia_control_set,
  167. .private_value = ((unsigned long)&(struct soc_mixer_control)
  168. {.reg = SND_SOC_NOPM,
  169. .rreg = SND_SOC_NOPM,
  170. .shift = 0,
  171. .rshift = 0,
  172. .max = 0xFFFFFFFF,
  173. .platform_max = 0xFFFFFFFF,
  174. .invert = 0
  175. })
  176. }
  177. };
  178. static const struct snd_kcontrol_new lpa_srs_trumedia_controls_hdmi[] = {
  179. {.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  180. .name = "SRS TruMedia HDMI",
  181. .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |
  182. SNDRV_CTL_ELEM_ACCESS_READWRITE,
  183. .info = snd_soc_info_volsw,
  184. .get = msm_dts_srs_trumedia_control_get,
  185. .put = msm_dts_srs_trumedia_control_hdmi_set,
  186. .private_value = ((unsigned long)&(struct soc_mixer_control)
  187. {.reg = SND_SOC_NOPM,
  188. .rreg = SND_SOC_NOPM,
  189. .shift = 0,
  190. .rshift = 0,
  191. .max = 0xFFFFFFFF,
  192. .platform_max = 0xFFFFFFFF,
  193. .invert = 0
  194. })
  195. }
  196. };
  197. static const struct snd_kcontrol_new lpa_srs_trumedia_controls_i2s[] = {
  198. {.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  199. .name = "SRS TruMedia I2S",
  200. .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |
  201. SNDRV_CTL_ELEM_ACCESS_READWRITE,
  202. .info = snd_soc_info_volsw,
  203. .get = msm_dts_srs_trumedia_control_get,
  204. .put = msm_dts_srs_trumedia_control_i2s_set,
  205. .private_value = ((unsigned long)&(struct soc_mixer_control)
  206. {.reg = SND_SOC_NOPM,
  207. .rreg = SND_SOC_NOPM,
  208. .shift = 0,
  209. .rshift = 0,
  210. .max = 0xFFFFFFFF,
  211. .platform_max = 0xFFFFFFFF,
  212. .invert = 0
  213. })
  214. }
  215. };
  216. static const struct snd_kcontrol_new lpa_srs_trumedia_controls_mi2s[] = {
  217. {
  218. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  219. .name = "SRS TruMedia MI2S",
  220. .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |
  221. SNDRV_CTL_ELEM_ACCESS_READWRITE,
  222. .info = snd_soc_info_volsw,
  223. .get = msm_dts_srs_trumedia_control_get,
  224. .put = msm_dts_srs_trumedia_control_mi2s_set,
  225. .private_value = ((unsigned long)&(struct soc_mixer_control)
  226. {
  227. .reg = SND_SOC_NOPM,
  228. .rreg = SND_SOC_NOPM,
  229. .shift = 0,
  230. .rshift = 0,
  231. .max = 0xFFFFFFFF,
  232. .platform_max = 0xFFFFFFFF,
  233. .invert = 0
  234. })
  235. }
  236. };
  237. /**
  238. * msm_dts_srs_tm_add_controls -
  239. * Add DTS SRS module controls
  240. *
  241. * @component: component to which controls can be registered
  242. *
  243. */
  244. void msm_dts_srs_tm_add_controls(struct snd_soc_component *component)
  245. {
  246. snd_soc_add_component_controls(component,
  247. lpa_srs_trumedia_controls,
  248. ARRAY_SIZE(lpa_srs_trumedia_controls));
  249. snd_soc_add_component_controls(component,
  250. lpa_srs_trumedia_controls_hdmi,
  251. ARRAY_SIZE(lpa_srs_trumedia_controls_hdmi));
  252. snd_soc_add_component_controls(component,
  253. lpa_srs_trumedia_controls_i2s,
  254. ARRAY_SIZE(lpa_srs_trumedia_controls_i2s));
  255. snd_soc_add_component_controls(component,
  256. lpa_srs_trumedia_controls_mi2s,
  257. ARRAY_SIZE(lpa_srs_trumedia_controls_mi2s));
  258. }
  259. EXPORT_SYMBOL(msm_dts_srs_tm_add_controls);
  260. static int reg_ion_mem(void)
  261. {
  262. int rc;
  263. rc = msm_audio_ion_alloc(&dma_buf, ION_MEM_SIZE,
  264. &po.paddr, (size_t *)&po.size,
  265. &po.kvaddr);
  266. if (rc != 0)
  267. pr_err("%s: failed to allocate memory.\n", __func__);
  268. pr_debug("%s: exited dma_buf = %pK, phys_addr = %lu, length = %d, vaddr = %pK, rc = 0x%x\n",
  269. __func__, dma_buf, (long)po.paddr,
  270. (unsigned int)po.size, po.kvaddr, rc);
  271. return rc;
  272. }
  273. void msm_dts_srs_tm_ion_memmap(struct param_outband *po_)
  274. {
  275. if (po.kvaddr == NULL) {
  276. pr_debug("%s: callingreg_ion_mem()\n", __func__);
  277. reg_ion_mem();
  278. }
  279. po_->size = ION_MEM_SIZE;
  280. po_->kvaddr = po.kvaddr;
  281. po_->paddr = po.paddr;
  282. }
  283. static void unreg_ion_mem(void)
  284. {
  285. msm_audio_ion_free(dma_buf);
  286. dma_buf = NULL;
  287. po.kvaddr = NULL;
  288. po.paddr = 0;
  289. po.size = 0;
  290. }
  291. /**
  292. * msm_dts_srs_tm_deinit -
  293. * De-Initializes DTS SRS module
  294. *
  295. * @port_id: Port ID number
  296. *
  297. */
  298. void msm_dts_srs_tm_deinit(int port_id)
  299. {
  300. set_port_id(port_id, -1);
  301. atomic_dec(&ref_cnt);
  302. if (po.kvaddr != NULL) {
  303. if (!atomic_read(&ref_cnt)) {
  304. pr_debug("%s: calling unreg_ion_mem()\n", __func__);
  305. unreg_ion_mem();
  306. }
  307. }
  308. }
  309. EXPORT_SYMBOL(msm_dts_srs_tm_deinit);
  310. /**
  311. * msm_dts_srs_tm_init -
  312. * Initializes DTS SRS module
  313. *
  314. * @port_id: Port ID number
  315. * @copp_idx: COPP index
  316. *
  317. */
  318. void msm_dts_srs_tm_init(int port_id, int copp_idx)
  319. {
  320. int cur_ref_cnt = 0;
  321. if (set_port_id(port_id, copp_idx) < 0) {
  322. pr_err("%s: Invalid port_id: %d\n", __func__, port_id);
  323. return;
  324. }
  325. cur_ref_cnt = atomic_read(&ref_cnt);
  326. atomic_inc(&ref_cnt);
  327. if (!cur_ref_cnt && po.kvaddr == NULL) {
  328. pr_debug("%s: calling reg_ion_mem()\n", __func__);
  329. if (reg_ion_mem() != 0) {
  330. atomic_dec(&ref_cnt);
  331. po.kvaddr = NULL;
  332. return;
  333. }
  334. }
  335. msm_dts_srs_tm_send_params(port_id, 1);
  336. }
  337. EXPORT_SYMBOL(msm_dts_srs_tm_init);