msm_common.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2020-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2021-2023 Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include <linux/gpio.h>
  7. #include <linux/of_gpio.h>
  8. #include <linux/platform_device.h>
  9. #include <linux/slab.h>
  10. #include <linux/of_device.h>
  11. #include <sound/control.h>
  12. #include <sound/core.h>
  13. #include <sound/soc.h>
  14. #include <sound/pcm_params.h>
  15. #include <asoc/msm-cdc-pinctrl.h>
  16. #include <dsp/spf-core.h>
  17. #include <dsp/msm_audio_ion.h>
  18. #include <sound/info.h>
  19. #include <dsp/audio_prm.h>
  20. #include <dsp/digital-cdc-rsc-mgr.h>
  21. #include <linux/sched/walt.h>
  22. #include "msm_common.h"
  23. struct snd_card_pdata {
  24. struct kobject snd_card_kobj;
  25. int card_status;
  26. }*snd_card_pdata;
  27. #define to_asoc_mach_common_pdata(kobj) \
  28. container_of((kobj), struct msm_common_pdata, aud_dev_kobj)
  29. #define DEVICE_ENABLE 1
  30. #define DEVICE_DISABLE 0
  31. #define ARRAY_SZ 21
  32. #define BUF_SZ 32
  33. #define DIR_SZ 10
  34. #define MAX_CODEC_DAI 8
  35. #define TDM_SLOT_WIDTH_BITS 32
  36. #define TDM_MAX_SLOTS 8
  37. #define MI2S_NUM_CHANNELS 2
  38. #define SAMPLING_RATE_44P1KHZ 44100
  39. #define SAMPLING_RATE_88P2KHZ 88200
  40. #define SAMPLING_RATE_176P4KHZ 176400
  41. #define SAMPLING_RATE_352P8KHZ 352800
  42. static struct attribute device_state_attr = {
  43. .name = "state",
  44. .mode = 0660,
  45. };
  46. static struct attribute card_state_attr = {
  47. .name = "card_state",
  48. .mode = 0660,
  49. };
  50. #define MAX_PORT 20
  51. #define CODEC_CHMAP "Channel Map"
  52. enum backend_id {
  53. SLIM = 1,
  54. CODEC_DMA,
  55. };
  56. struct chmap_pdata {
  57. int id;
  58. uint32_t num_codec_dai;
  59. struct snd_soc_dai *dai[MAX_CODEC_DAI];
  60. };
  61. static const struct snd_pcm_hardware dummy_dma_hardware = {
  62. /* Random values to keep userspace happy when checking constraints */
  63. .info = SNDRV_PCM_INFO_INTERLEAVED |
  64. SNDRV_PCM_INFO_BLOCK_TRANSFER,
  65. .buffer_bytes_max = 128*1024,
  66. .period_bytes_min = PAGE_SIZE,
  67. .period_bytes_max = PAGE_SIZE*2,
  68. .periods_min = 2,
  69. .periods_max = 128,
  70. };
  71. #define MAX_USR_INPUT 10
  72. #define MAX_AUDIO_CPU_CORE_NUM 2
  73. static int qos_vote_status;
  74. static bool lpi_pcm_logging_enable;
  75. static bool vote_against_sleep_enable;
  76. static unsigned int vote_against_sleep_cnt;
  77. static struct dev_pm_qos_request latency_pm_qos_req; /* pm_qos request */
  78. static unsigned int qos_client_active_cnt;
  79. static uint32_t *audio_core_list = NULL;
  80. static uint32_t audio_core_num = MAX_AUDIO_CPU_CORE_NUM;
  81. static cpumask_t audio_cpu_map = CPU_MASK_NONE;
  82. static struct dev_pm_qos_request *msm_audio_req = NULL;
  83. static bool kregister_pm_qos_latency_controls = false;
  84. #define MSM_LL_QOS_VALUE 300 /* time in us to ensure LPM doesn't go in C3/C4 */
  85. static ssize_t aud_dev_sysfs_store(struct kobject *kobj,
  86. struct attribute *attr,
  87. const char *buf, size_t count)
  88. {
  89. ssize_t ret = -EINVAL;
  90. struct msm_common_pdata *pdata = to_asoc_mach_common_pdata(kobj);
  91. uint32_t pcm_id, state = 0;
  92. if (count > MAX_USR_INPUT) {
  93. pr_err("%s: invalid string written", __func__);
  94. goto done;
  95. }
  96. sscanf(buf, "%d %d", &pcm_id, &state);
  97. if ((pcm_id > pdata->num_aud_devs) || (pcm_id < 0)) {
  98. pr_err("%s: invalid pcm id %d \n", __func__, pcm_id);
  99. goto done;
  100. }
  101. if ((state > DEVICE_ENABLE) || (state < DEVICE_DISABLE)) {
  102. pr_err("%s: invalid state %d \n", __func__, state);
  103. goto done;
  104. }
  105. pr_debug("%s: pcm_id %d state %d \n", __func__, pcm_id, state);
  106. pdata->aud_dev_state[pcm_id] = state;
  107. ret = count;
  108. done:
  109. return ret;
  110. }
  111. static const struct sysfs_ops aud_dev_sysfs_ops = {
  112. .store = aud_dev_sysfs_store,
  113. };
  114. static struct kobj_type aud_dev_ktype = {
  115. .sysfs_ops = &aud_dev_sysfs_ops,
  116. };
  117. static int aud_dev_sysfs_init(struct msm_common_pdata *pdata)
  118. {
  119. int ret = 0;
  120. char dir[10] = "aud_dev";
  121. ret = kobject_init_and_add(&pdata->aud_dev_kobj, &aud_dev_ktype,
  122. kernel_kobj, dir);
  123. if (ret < 0) {
  124. pr_err("%s: Failed to add kobject %s, err = %d\n",
  125. __func__, dir, ret);
  126. goto done;
  127. }
  128. ret = sysfs_create_file(&pdata->aud_dev_kobj, &device_state_attr);
  129. if (ret < 0) {
  130. pr_err("%s: Failed to add wdsp_boot sysfs entry to %s\n",
  131. __func__, dir);
  132. goto fail_create_file;
  133. }
  134. return ret;
  135. fail_create_file:
  136. kobject_put(&pdata->aud_dev_kobj);
  137. done:
  138. return ret;
  139. }
  140. int snd_card_notify_user(snd_card_status_t card_status)
  141. {
  142. snd_card_pdata->card_status = card_status;
  143. sysfs_notify(&snd_card_pdata->snd_card_kobj, NULL, "card_state");
  144. if (card_status == 0)
  145. vote_against_sleep_cnt = 0;
  146. return 0;
  147. }
  148. int snd_card_set_card_status(snd_card_status_t card_status)
  149. {
  150. snd_card_pdata->card_status = card_status;
  151. return 0;
  152. }
  153. static ssize_t snd_card_sysfs_show(struct kobject *kobj,
  154. struct attribute *attr, char *buf)
  155. {
  156. return snprintf(buf, BUF_SZ, "%d", snd_card_pdata->card_status);
  157. }
  158. static ssize_t snd_card_sysfs_store(struct kobject *kobj,
  159. struct attribute *attr, const char *buf, size_t count)
  160. {
  161. sscanf(buf, "%d", &snd_card_pdata->card_status);
  162. sysfs_notify(&snd_card_pdata->snd_card_kobj, NULL, "card_state");
  163. return 0;
  164. }
  165. static const struct sysfs_ops snd_card_sysfs_ops = {
  166. .show = snd_card_sysfs_show,
  167. .store = snd_card_sysfs_store,
  168. };
  169. static struct kobj_type snd_card_ktype = {
  170. .sysfs_ops = &snd_card_sysfs_ops,
  171. };
  172. int snd_card_sysfs_init(void)
  173. {
  174. int ret = 0;
  175. char dir[DIR_SZ] = "snd_card";
  176. snd_card_pdata = kcalloc(1, sizeof(struct snd_card_pdata), GFP_KERNEL);
  177. ret = kobject_init_and_add(&snd_card_pdata->snd_card_kobj, &snd_card_ktype,
  178. kernel_kobj, dir);
  179. if (ret < 0) {
  180. pr_err("%s: Failed to add kobject %s, err = %d\n",
  181. __func__, dir, ret);
  182. goto done;
  183. }
  184. ret = sysfs_create_file(&snd_card_pdata->snd_card_kobj, &card_state_attr);
  185. if (ret < 0) {
  186. pr_err("%s: Failed to add snd_card sysfs entry to %s\n",
  187. __func__, dir);
  188. goto fail_create_file;
  189. }
  190. return ret;
  191. fail_create_file:
  192. kobject_put(&snd_card_pdata->snd_card_kobj);
  193. done:
  194. return ret;
  195. }
  196. static void check_userspace_service_state(struct snd_soc_pcm_runtime *rtd,
  197. struct msm_common_pdata *pdata)
  198. {
  199. uint32_t i;
  200. dev_info(rtd->card->dev,"%s: pcm_id %d state %d\n", __func__,
  201. rtd->num, pdata->aud_dev_state[rtd->num]);
  202. mutex_lock(&pdata->aud_dev_lock);
  203. if (pdata->aud_dev_state[rtd->num] == DEVICE_ENABLE) {
  204. dev_info(rtd->card->dev, "%s userspace service crashed\n",
  205. __func__);
  206. /*Reset the state as sysfs node wont be triggred*/
  207. pdata->aud_dev_state[rtd->num] = DEVICE_DISABLE;
  208. for (i = 0; i < pdata->num_aud_devs; i++) {
  209. if (pdata->aud_dev_state[i] == DEVICE_ENABLE)
  210. goto exit;
  211. }
  212. /*Issue close all graph cmd to DSP*/
  213. spf_core_apm_close_all();
  214. /*unmap all dma mapped buffers*/
  215. msm_audio_ion_crash_handler();
  216. }
  217. exit:
  218. mutex_unlock(&pdata->aud_dev_lock);
  219. return;
  220. }
  221. static int get_mi2s_tdm_auxpcm_intf_index(const char *stream_name)
  222. {
  223. if (!strnstr(stream_name, "TDM", strlen(stream_name)) &&
  224. !strnstr(stream_name, "MI2S", strlen(stream_name)) &&
  225. !strnstr(stream_name, "AUXPCM", strlen(stream_name)))
  226. return -EINVAL;
  227. if (strnstr(stream_name, "LPAIF_RXTX", strlen(stream_name)))
  228. return QUAT_MI2S_TDM_AUXPCM;
  229. else if (strnstr(stream_name, "LPAIF_WSA", strlen(stream_name)))
  230. return SEN_MI2S_TDM_AUXPCM;
  231. else if (strnstr(stream_name, "LPAIF_VA", strlen(stream_name)))
  232. return QUIN_MI2S_TDM_AUXPCM;
  233. else if (strnstr(stream_name, "LPAIF_AUD", strlen(stream_name))){
  234. if (strnstr(stream_name, "PRIMARY", strlen(stream_name)))
  235. return SEP_MI2S_TDM_AUXPCM;
  236. else if (strnstr(stream_name, "SECONDARY", strlen(stream_name)))
  237. return TER_MI2S_TDM_AUXPCM;
  238. }
  239. else if (strnstr(stream_name, "LPAIF", strlen(stream_name))) {
  240. if (strnstr(stream_name, "PRIMARY", strlen(stream_name)))
  241. return PRI_MI2S_TDM_AUXPCM;
  242. else if (strnstr(stream_name, "SECONDARY", strlen(stream_name)))
  243. return SEC_MI2S_TDM_AUXPCM;
  244. }
  245. pr_debug("%s: stream name %s does not match\n", __func__, stream_name);
  246. return -EINVAL;
  247. }
  248. static bool is_fractional_sample_rate(unsigned int sample_rate)
  249. {
  250. switch (sample_rate) {
  251. case SAMPLING_RATE_44P1KHZ:
  252. case SAMPLING_RATE_88P2KHZ:
  253. case SAMPLING_RATE_176P4KHZ:
  254. case SAMPLING_RATE_352P8KHZ:
  255. return true;
  256. default:
  257. return false;
  258. }
  259. return false;
  260. }
  261. static int get_mi2s_clk_id(int index)
  262. {
  263. int clk_id = -EINVAL;
  264. switch(index) {
  265. case PRI_MI2S_TDM_AUXPCM:
  266. clk_id = CLOCK_ID_PRI_MI2S_IBIT;
  267. break;
  268. case SEC_MI2S_TDM_AUXPCM:
  269. clk_id = CLOCK_ID_SEC_MI2S_IBIT;
  270. break;
  271. case TER_MI2S_TDM_AUXPCM:
  272. clk_id = CLOCK_ID_TER_MI2S_IBIT;
  273. break;
  274. case QUAT_MI2S_TDM_AUXPCM:
  275. clk_id = CLOCK_ID_QUAD_MI2S_IBIT;
  276. break;
  277. case QUIN_MI2S_TDM_AUXPCM:
  278. clk_id = CLOCK_ID_QUI_MI2S_IBIT;
  279. break;
  280. case SEN_MI2S_TDM_AUXPCM:
  281. clk_id = CLOCK_ID_SEN_MI2S_IBIT;
  282. break;
  283. case SEP_MI2S_TDM_AUXPCM:
  284. clk_id = CLOCK_ID_SEP_MI2S_IBIT;
  285. break;
  286. default:
  287. pr_err("%s: Invalid interface index: %d\n", __func__, index);
  288. }
  289. pr_debug("%s: clk id: %d\n", __func__, clk_id);
  290. return clk_id;
  291. }
  292. static int get_tdm_clk_id(int index)
  293. {
  294. int clk_id = -EINVAL;
  295. switch(index) {
  296. case PRI_MI2S_TDM_AUXPCM:
  297. clk_id = CLOCK_ID_PRI_TDM_IBIT;
  298. break;
  299. case SEC_MI2S_TDM_AUXPCM:
  300. clk_id = CLOCK_ID_SEC_TDM_IBIT;
  301. break;
  302. case TER_MI2S_TDM_AUXPCM:
  303. clk_id = CLOCK_ID_TER_TDM_IBIT;
  304. break;
  305. case QUAT_MI2S_TDM_AUXPCM:
  306. clk_id = CLOCK_ID_QUAD_TDM_IBIT;
  307. break;
  308. case QUIN_MI2S_TDM_AUXPCM:
  309. clk_id = CLOCK_ID_QUI_TDM_IBIT;
  310. break;
  311. case SEN_MI2S_TDM_AUXPCM:
  312. clk_id = CLOCK_ID_SEN_TDM_IBIT;
  313. break;
  314. case SEP_MI2S_TDM_AUXPCM:
  315. clk_id = CLOCK_ID_SEP_TDM_IBIT;
  316. break;
  317. default:
  318. pr_err("%s: Invalid interface index: %d\n", __func__, index);
  319. }
  320. pr_debug("%s: clk id: %d\n", __func__, clk_id);
  321. return clk_id;
  322. }
  323. int mi2s_tdm_hw_vote_req(struct msm_common_pdata *pdata, int enable)
  324. {
  325. int ret = 0;
  326. if (!pdata || (pdata->lpass_audio_hw_vote == NULL)) {
  327. pr_err("%s: pdata or lpass audio hw vote node NULL", __func__);
  328. return -EINVAL;
  329. }
  330. pr_debug("%s: lpass audio hw vote for fractional sample rate enable: %d\n",
  331. __func__, enable);
  332. if (enable) {
  333. if (atomic_read(&pdata->lpass_audio_hw_vote_ref_cnt) == 0) {
  334. ret = digital_cdc_rsc_mgr_hw_vote_enable(pdata->lpass_audio_hw_vote, NULL);
  335. if (ret < 0) {
  336. pr_err("%s lpass audio hw vote enable failed %d\n",
  337. __func__, ret);
  338. return ret;
  339. }
  340. }
  341. atomic_inc(&pdata->lpass_audio_hw_vote_ref_cnt);
  342. } else {
  343. atomic_dec(&pdata->lpass_audio_hw_vote_ref_cnt);
  344. if (atomic_read(&pdata->lpass_audio_hw_vote_ref_cnt) == 0)
  345. digital_cdc_rsc_mgr_hw_vote_disable(pdata->lpass_audio_hw_vote, NULL);
  346. else if (atomic_read(&pdata->lpass_audio_hw_vote_ref_cnt) < 0)
  347. atomic_set(&pdata->lpass_audio_hw_vote_ref_cnt, 0);
  348. }
  349. return ret;
  350. }
  351. int msm_common_snd_hw_params(struct snd_pcm_substream *substream,
  352. struct snd_pcm_hw_params *params)
  353. {
  354. int ret = 0;
  355. int slot_width = TDM_SLOT_WIDTH_BITS;
  356. int slots;
  357. int sample_width;
  358. unsigned int rate;
  359. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  360. const char *stream_name = rtd->dai_link->stream_name;
  361. struct snd_soc_card *card = rtd->card;
  362. struct msm_common_pdata *pdata = msm_common_get_pdata(card);
  363. int index = get_mi2s_tdm_auxpcm_intf_index(stream_name);
  364. struct clk_cfg intf_clk_cfg;
  365. dev_dbg(rtd->card->dev,
  366. "%s: substream = %s stream = %d\n",
  367. __func__, substream->name, substream->stream);
  368. if (!pdata) {
  369. dev_err(rtd->card->dev, "%s: pdata is NULL\n", __func__);
  370. return -EINVAL;
  371. }
  372. if (index >= 0) {
  373. mutex_lock(&pdata->lock[index]);
  374. if (atomic_read(&pdata->lpass_intf_clk_ref_cnt[index]) == 0) {
  375. if ((strnstr(stream_name, "TDM", strlen(stream_name)))) {
  376. slots = pdata->tdm_max_slots;
  377. rate = params_rate(params);
  378. ret = get_tdm_clk_id(index);
  379. if ( ret < 0)
  380. goto done;
  381. intf_clk_cfg.clk_id = ret;
  382. intf_clk_cfg.clk_freq_in_hz = rate * slot_width * slots;
  383. intf_clk_cfg.clk_attri = pdata->tdm_clk_attribute[index];
  384. intf_clk_cfg.clk_root = 0;
  385. if (pdata->is_audio_hw_vote_required[index] &&
  386. is_fractional_sample_rate(rate)) {
  387. ret = mi2s_tdm_hw_vote_req(pdata, 1);
  388. if (ret < 0) {
  389. pr_err("%s lpass audio hw vote enable failed %d\n",
  390. __func__, ret);
  391. goto done;
  392. }
  393. }
  394. pr_debug("%s: clk_id :%d clk freq %d\n", __func__,
  395. intf_clk_cfg.clk_id, intf_clk_cfg.clk_freq_in_hz);
  396. ret = audio_prm_set_lpass_clk_cfg(&intf_clk_cfg, 1);
  397. if (ret < 0) {
  398. pr_err("%s: prm lpass tdm clk cfg set failed ret %d\n",
  399. __func__, ret);
  400. goto done;
  401. }
  402. } else if ((strnstr(stream_name, "MI2S", strlen(stream_name)))) {
  403. ret = get_mi2s_clk_id(index);
  404. if (ret < 0)
  405. goto done;
  406. intf_clk_cfg.clk_id = ret;
  407. rate = params_rate(params);
  408. switch (params_format(params)) {
  409. case SNDRV_PCM_FORMAT_S24_LE:
  410. case SNDRV_PCM_FORMAT_S24_3LE:
  411. case SNDRV_PCM_FORMAT_S32_LE:
  412. sample_width = 32;
  413. break;
  414. case SNDRV_PCM_FORMAT_S16_LE:
  415. default:
  416. sample_width = 16;
  417. pr_debug("%s: bitwidth set to default : %d\n",
  418. __func__, sample_width);
  419. }
  420. intf_clk_cfg.clk_freq_in_hz = rate *
  421. MI2S_NUM_CHANNELS * sample_width;
  422. intf_clk_cfg.clk_attri = pdata->mi2s_clk_attribute[index];
  423. intf_clk_cfg.clk_root = CLOCK_ROOT_DEFAULT;
  424. if (pdata->is_audio_hw_vote_required[index] &&
  425. is_fractional_sample_rate(rate)) {
  426. ret = mi2s_tdm_hw_vote_req(pdata, 1);
  427. if (ret < 0) {
  428. pr_err("%s lpass audio hw vote enable failed %d\n",
  429. __func__, ret);
  430. goto done;
  431. }
  432. }
  433. pr_debug("%s: mi2s clk_id :%d clk freq %d\n", __func__,
  434. intf_clk_cfg.clk_id, intf_clk_cfg.clk_freq_in_hz);
  435. ret = audio_prm_set_lpass_clk_cfg(&intf_clk_cfg, 1);
  436. if (ret < 0) {
  437. pr_err("%s: prm lpass mi2s clk cfg set failed ret %d\n",
  438. __func__, ret);
  439. goto done;
  440. }
  441. } else {
  442. pr_err("%s: unsupported stream name: %s\n",
  443. __func__, stream_name);
  444. goto done;
  445. }
  446. }
  447. atomic_inc(&pdata->lpass_intf_clk_ref_cnt[index]);
  448. done:
  449. mutex_unlock(&pdata->lock[index]);
  450. }
  451. return ret;
  452. }
  453. int msm_common_snd_startup(struct snd_pcm_substream *substream)
  454. {
  455. int ret = 0;
  456. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  457. struct snd_soc_card *card = rtd->card;
  458. struct msm_common_pdata *pdata = msm_common_get_pdata(card);
  459. const char *stream_name = rtd->dai_link->stream_name;
  460. int index = get_mi2s_tdm_auxpcm_intf_index(stream_name);
  461. dev_dbg(rtd->card->dev,
  462. "%s: substream = %s stream = %d\n",
  463. __func__, substream->name, substream->stream);
  464. if (!pdata) {
  465. dev_err(rtd->card->dev, "%s: pdata is NULL\n", __func__);
  466. return -EINVAL;
  467. }
  468. if (!rtd->dai_link->no_pcm)
  469. snd_soc_set_runtime_hwparams(substream, &dummy_dma_hardware);
  470. if (index >= 0) {
  471. mutex_lock(&pdata->lock[index]);
  472. if (pdata->mi2s_gpio_p[index]) {
  473. if (atomic_read(&(pdata->mi2s_gpio_ref_cnt[index])) == 0) {
  474. ret = msm_cdc_pinctrl_select_active_state(
  475. pdata->mi2s_gpio_p[index]);
  476. if (ret) {
  477. pr_err("%s:pinctrl set actve fail with %d\n",
  478. __func__, ret);
  479. goto done;
  480. }
  481. }
  482. atomic_inc(&(pdata->mi2s_gpio_ref_cnt[index]));
  483. }
  484. done:
  485. mutex_unlock(&pdata->lock[index]);
  486. }
  487. return ret;
  488. }
  489. void msm_common_snd_shutdown(struct snd_pcm_substream *substream)
  490. {
  491. int ret;
  492. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  493. struct snd_soc_card *card = rtd->card;
  494. struct msm_common_pdata *pdata = msm_common_get_pdata(card);
  495. struct snd_pcm_runtime *runtime = substream->runtime;
  496. const char *stream_name = rtd->dai_link->stream_name;
  497. int index = get_mi2s_tdm_auxpcm_intf_index(stream_name);
  498. struct clk_cfg intf_clk_cfg;
  499. unsigned int rate = runtime->rate;
  500. memset(&intf_clk_cfg, 0, sizeof(struct clk_cfg));
  501. pr_debug("%s(): substream = %s stream = %d\n", __func__,
  502. substream->name, substream->stream);
  503. if (!pdata) {
  504. dev_err(card->dev, "%s: pdata is NULL\n", __func__);
  505. return;
  506. }
  507. check_userspace_service_state(rtd, pdata);
  508. if (index >= 0) {
  509. mutex_lock(&pdata->lock[index]);
  510. atomic_dec(&pdata->lpass_intf_clk_ref_cnt[index]);
  511. if (atomic_read(&pdata->lpass_intf_clk_ref_cnt[index]) == 0) {
  512. if ((strnstr(stream_name, "TDM", strlen(stream_name)))) {
  513. ret = get_tdm_clk_id(index);
  514. if (ret > 0) {
  515. intf_clk_cfg.clk_id = ret;
  516. ret = audio_prm_set_lpass_clk_cfg(&intf_clk_cfg, 0);
  517. if (ret < 0)
  518. pr_err("%s: prm tdm clk cfg set failed ret %d\n",
  519. __func__, ret);
  520. }
  521. } else if((strnstr(stream_name, "MI2S", strlen(stream_name)))) {
  522. ret = get_mi2s_clk_id(index);
  523. if (ret > 0) {
  524. intf_clk_cfg.clk_id = ret;
  525. ret = audio_prm_set_lpass_clk_cfg(&intf_clk_cfg, 0);
  526. if (ret < 0)
  527. pr_err("%s: prm mi2s clk cfg disable failed ret %d\n",
  528. __func__, ret);
  529. }
  530. } else {
  531. pr_err("%s: unsupported stream name: %s\n",
  532. __func__, stream_name);
  533. }
  534. if (pdata->is_audio_hw_vote_required[index] &&
  535. is_fractional_sample_rate(rate)) {
  536. ret = mi2s_tdm_hw_vote_req(pdata, 0);
  537. }
  538. } else if (atomic_read(&pdata->lpass_intf_clk_ref_cnt[index]) < 0) {
  539. atomic_set(&pdata->lpass_intf_clk_ref_cnt[index], 0);
  540. }
  541. if (pdata->mi2s_gpio_p[index]) {
  542. atomic_dec(&pdata->mi2s_gpio_ref_cnt[index]);
  543. if (atomic_read(&pdata->mi2s_gpio_ref_cnt[index]) == 0) {
  544. ret = msm_cdc_pinctrl_select_sleep_state(
  545. pdata->mi2s_gpio_p[index]);
  546. if (ret)
  547. dev_err(card->dev,
  548. "%s: pinctrl set actv fail %d\n",
  549. __func__, ret);
  550. } else if (atomic_read(&pdata->mi2s_gpio_ref_cnt[index]) < 0) {
  551. atomic_set(&pdata->mi2s_gpio_ref_cnt[index], 0);
  552. }
  553. }
  554. mutex_unlock(&pdata->lock[index]);
  555. }
  556. }
  557. static void msm_audio_add_qos_request(void)
  558. {
  559. int i;
  560. int cpu = 0;
  561. int ret = 0;
  562. msm_audio_req = kcalloc(num_possible_cpus(),
  563. sizeof(struct dev_pm_qos_request), GFP_KERNEL);
  564. if (!msm_audio_req)
  565. return;
  566. for (i = 0; i < audio_core_num; i++) {
  567. if (audio_core_list[i] >= num_possible_cpus())
  568. pr_err("%s incorrect cpu id: %d specified.\n",
  569. __func__, audio_core_list[i]);
  570. else
  571. cpumask_set_cpu(audio_core_list[i], &audio_cpu_map);
  572. }
  573. for_each_cpu(cpu, &audio_cpu_map) {
  574. ret = dev_pm_qos_add_request(get_cpu_device(cpu),
  575. &msm_audio_req[cpu],
  576. DEV_PM_QOS_RESUME_LATENCY,
  577. PM_QOS_CPU_LATENCY_DEFAULT_VALUE);
  578. if (ret < 0)
  579. pr_err("%s error (%d) adding resume latency to cpu %d.\n",
  580. __func__, ret, cpu);
  581. pr_debug("%s set cpu affinity to core %d.\n", __func__, cpu);
  582. }
  583. }
  584. static void msm_audio_remove_qos_request(void)
  585. {
  586. int cpu = 0;
  587. int ret = 0;
  588. if (msm_audio_req) {
  589. for_each_cpu(cpu, &audio_cpu_map) {
  590. ret = dev_pm_qos_remove_request(
  591. &msm_audio_req[cpu]);
  592. if (ret < 0)
  593. pr_err("%s error (%d) removing request from cpu %d.\n",
  594. __func__, ret, cpu);
  595. pr_debug("%s remove cpu affinity of core %d.\n", __func__, cpu);
  596. }
  597. kfree(msm_audio_req);
  598. }
  599. }
  600. int msm_common_snd_init(struct platform_device *pdev, struct snd_soc_card *card)
  601. {
  602. struct msm_common_pdata *common_pdata = NULL;
  603. int count, ret = 0;
  604. uint32_t val_array[MI2S_TDM_AUXPCM_MAX] = {0};
  605. struct clk *lpass_audio_hw_vote = NULL;
  606. uint32_t *core_val_array = NULL;
  607. common_pdata = kcalloc(1, sizeof(struct msm_common_pdata), GFP_KERNEL);
  608. if (!common_pdata)
  609. return -ENOMEM;
  610. for (count = 0; count < MI2S_TDM_AUXPCM_MAX; count++) {
  611. mutex_init(&common_pdata->lock[count]);
  612. atomic_set(&common_pdata->mi2s_gpio_ref_cnt[count], 0);
  613. }
  614. ret = of_property_read_u32(pdev->dev.of_node, "qcom,tdm-max-slots",
  615. &common_pdata->tdm_max_slots);
  616. if (ret) {
  617. dev_info(&pdev->dev, "%s: No DT match for tdm max slots\n",
  618. __func__);
  619. }
  620. if ((common_pdata->tdm_max_slots <= 0) || (common_pdata->tdm_max_slots >
  621. TDM_MAX_SLOTS)) {
  622. common_pdata->tdm_max_slots = TDM_MAX_SLOTS;
  623. dev_info(&pdev->dev, "%s: Using default tdm max slot: %d\n",
  624. __func__, common_pdata->tdm_max_slots);
  625. }
  626. /* Register LPASS audio hw vote */
  627. lpass_audio_hw_vote = devm_clk_get(&pdev->dev, "lpass_audio_hw_vote");
  628. if (IS_ERR(lpass_audio_hw_vote)) {
  629. ret = PTR_ERR(lpass_audio_hw_vote);
  630. dev_dbg(&pdev->dev, "%s: clk get %s failed %d\n",
  631. __func__, "lpass_audio_hw_vote", ret);
  632. lpass_audio_hw_vote = NULL;
  633. ret = 0;
  634. }
  635. common_pdata->lpass_audio_hw_vote = lpass_audio_hw_vote;
  636. ret = of_property_read_u32_array(pdev->dev.of_node,
  637. "qcom,mi2s-tdm-is-hw-vote-needed",
  638. val_array, MI2S_TDM_AUXPCM_MAX);
  639. if (ret) {
  640. dev_dbg(&pdev->dev, "%s:no qcom,mi2s-tdm-is-hw-vote-needed in DT node\n",
  641. __func__);
  642. } else {
  643. for (count = 0; count < MI2S_TDM_AUXPCM_MAX; count++) {
  644. common_pdata->is_audio_hw_vote_required[count] =
  645. val_array[count];
  646. }
  647. }
  648. ret = of_property_read_u32_array(pdev->dev.of_node, "qcom,tdm-clk-attribute",
  649. val_array, MI2S_TDM_AUXPCM_MAX);
  650. if (ret) {
  651. dev_info(&pdev->dev,
  652. "%s: No DT match for tdm clk attribute, set to default\n", __func__);
  653. for (count = 0; count < MI2S_TDM_AUXPCM_MAX; count++) {
  654. common_pdata->tdm_clk_attribute[count] =
  655. CLOCK_ATTRIBUTE_COUPLE_NO;
  656. }
  657. } else {
  658. for (count = 0; count < MI2S_TDM_AUXPCM_MAX; count++) {
  659. common_pdata->tdm_clk_attribute[count] =
  660. val_array[count];
  661. }
  662. }
  663. ret = of_property_read_u32_array(pdev->dev.of_node, "qcom,mi2s-clk-attribute",
  664. val_array, MI2S_TDM_AUXPCM_MAX);
  665. if (ret) {
  666. dev_info(&pdev->dev,
  667. "%s: No DT match for mi2s clk attribute, set to default\n", __func__);
  668. for (count = 0; count < MI2S_TDM_AUXPCM_MAX; count++) {
  669. common_pdata->mi2s_clk_attribute[count] =
  670. CLOCK_ATTRIBUTE_COUPLE_NO;
  671. }
  672. } else {
  673. for (count = 0; count < MI2S_TDM_AUXPCM_MAX; count++) {
  674. common_pdata->mi2s_clk_attribute[count] =
  675. val_array[count];
  676. }
  677. }
  678. common_pdata->mi2s_gpio_p[PRI_MI2S_TDM_AUXPCM] = of_parse_phandle(pdev->dev.of_node,
  679. "qcom,pri-mi2s-gpios", 0);
  680. common_pdata->mi2s_gpio_p[SEC_MI2S_TDM_AUXPCM] = of_parse_phandle(pdev->dev.of_node,
  681. "qcom,sec-mi2s-gpios", 0);
  682. common_pdata->mi2s_gpio_p[TER_MI2S_TDM_AUXPCM] = of_parse_phandle(pdev->dev.of_node,
  683. "qcom,tert-mi2s-gpios", 0);
  684. common_pdata->mi2s_gpio_p[QUAT_MI2S_TDM_AUXPCM] = of_parse_phandle(pdev->dev.of_node,
  685. "qcom,quat-mi2s-gpios", 0);
  686. common_pdata->mi2s_gpio_p[QUIN_MI2S_TDM_AUXPCM] = of_parse_phandle(pdev->dev.of_node,
  687. "qcom,quin-mi2s-gpios", 0);
  688. common_pdata->mi2s_gpio_p[SEN_MI2S_TDM_AUXPCM] = of_parse_phandle(pdev->dev.of_node,
  689. "qcom,sen-mi2s-gpios", 0);
  690. common_pdata->mi2s_gpio_p[SEP_MI2S_TDM_AUXPCM] = of_parse_phandle(pdev->dev.of_node,
  691. "qcom,sep-mi2s-gpios", 0);
  692. common_pdata->aud_dev_state = devm_kcalloc(&pdev->dev, card->num_links,
  693. sizeof(uint8_t), GFP_KERNEL);
  694. dev_info(&pdev->dev, "num_links %d \n", card->num_links);
  695. common_pdata->num_aud_devs = card->num_links;
  696. mutex_init(&common_pdata->aud_dev_lock);
  697. aud_dev_sysfs_init(common_pdata);
  698. msm_common_set_pdata(card, common_pdata);
  699. /* Add QoS request for audio tasks */
  700. core_val_array = devm_kcalloc(&pdev->dev, num_possible_cpus(), sizeof(uint32_t), GFP_KERNEL);
  701. if (!core_val_array) {
  702. dev_info(&pdev->dev, "%s: core val array is nullptr\n", __func__);
  703. goto exit;
  704. }
  705. ret = of_property_read_variable_u32_array(pdev->dev.of_node, "qcom,audio-core-list",
  706. core_val_array, 0, num_possible_cpus());
  707. dev_info(&pdev->dev, "%s: getting the core list size:%d, num_possible_cpus:%d \n",
  708. __func__, ret, num_possible_cpus());
  709. if (ret > 0 && (ret <= num_possible_cpus())) {
  710. audio_core_num = ret;
  711. audio_core_list = devm_kcalloc(&pdev->dev, audio_core_num, sizeof(uint32_t), GFP_KERNEL);
  712. if (!audio_core_list) {
  713. dev_info(&pdev->dev, "%s: calloc failed for audio core list\n", __func__);
  714. goto exit;
  715. }
  716. for (count = 0; count < audio_core_num; count++) {
  717. audio_core_list[count] = core_val_array[count];
  718. dev_info(&pdev->dev, "%s: update core %d\n", __func__, core_val_array[count]);
  719. }
  720. } else {
  721. dev_info(&pdev->dev, "%s: keep default core\n", __func__);
  722. audio_core_list = devm_kcalloc(&pdev->dev, audio_core_num, sizeof(uint32_t), GFP_KERNEL);
  723. /* set audio task affinity to core 1 & 2 as default*/
  724. if (!audio_core_list) {
  725. dev_info(&pdev->dev, "%s: calloc failed for audio core list\n", __func__);
  726. goto exit;
  727. }
  728. audio_core_list[0] = 1;
  729. audio_core_list[1] = 2;
  730. }
  731. msm_audio_add_qos_request();
  732. exit:
  733. if (audio_core_list) {
  734. devm_kfree(&pdev->dev, audio_core_list);
  735. audio_core_list = NULL;
  736. }
  737. if (core_val_array) {
  738. devm_kfree(&pdev->dev, core_val_array);
  739. }
  740. return 0;
  741. };
  742. void msm_common_snd_deinit(struct msm_common_pdata *common_pdata)
  743. {
  744. int count;
  745. if (!common_pdata)
  746. return;
  747. msm_audio_remove_qos_request();
  748. mutex_destroy(&common_pdata->aud_dev_lock);
  749. for (count = 0; count < MI2S_TDM_AUXPCM_MAX; count++) {
  750. mutex_destroy(&common_pdata->lock[count]);
  751. }
  752. }
  753. int msm_channel_map_info(struct snd_kcontrol *kcontrol,
  754. struct snd_ctl_elem_info *uinfo)
  755. {
  756. uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
  757. uinfo->count = sizeof(uint32_t) * MAX_PORT;
  758. return 0;
  759. }
  760. int msm_channel_map_get(struct snd_kcontrol *kcontrol,
  761. struct snd_ctl_elem_value *ucontrol)
  762. {
  763. struct chmap_pdata *kctl_pdata =
  764. (struct chmap_pdata *)kcontrol->private_data;
  765. struct snd_soc_dai *codec_dai = NULL;
  766. int backend_id = 0;
  767. uint32_t rx_ch[MAX_PORT] = {0}, tx_ch[MAX_PORT] = {0};
  768. uint32_t rx_ch_cnt = 0, tx_ch_cnt = 0;
  769. uint32_t *chmap_data = NULL;
  770. int ret = 0, len = 0, i = 0;
  771. if (kctl_pdata == NULL) {
  772. pr_debug("%s: chmap_pdata is not initialized\n", __func__);
  773. return -EINVAL;
  774. }
  775. codec_dai = kctl_pdata->dai[0];
  776. backend_id = kctl_pdata->id;
  777. switch (backend_id) {
  778. case SLIM: {
  779. uint32_t *chmap;
  780. uint32_t ch_cnt;
  781. ret = snd_soc_dai_get_channel_map(codec_dai,
  782. &tx_ch_cnt, tx_ch, &rx_ch_cnt, rx_ch);
  783. if (ret || (tx_ch_cnt == 0 && rx_ch_cnt == 0)) {
  784. pr_debug("%s: got incorrect channel map for backend_id:%d\n",
  785. __func__, backend_id);
  786. return ret;
  787. }
  788. if (rx_ch_cnt) {
  789. chmap = rx_ch;
  790. ch_cnt = rx_ch_cnt;
  791. } else {
  792. chmap = tx_ch;
  793. ch_cnt = tx_ch_cnt;
  794. }
  795. if (ch_cnt > 2) {
  796. pr_err("%s: Incorrect channel count: %d\n", __func__, ch_cnt);
  797. return -EINVAL;
  798. }
  799. len = sizeof(uint32_t) * (ch_cnt + 1);
  800. chmap_data = kzalloc(len, GFP_KERNEL);
  801. if (!chmap_data)
  802. return -ENOMEM;
  803. chmap_data[0] = ch_cnt;
  804. for (i = 0; i < ch_cnt; i++)
  805. chmap_data[i+1] = chmap[i];
  806. memcpy(ucontrol->value.bytes.data, chmap_data, len);
  807. break;
  808. }
  809. case CODEC_DMA: {
  810. uint32_t cur_rx_ch = 0, cur_tx_ch = 0;
  811. uint32_t cur_rx_ch_cnt = 0, cur_tx_ch_cnt = 0;
  812. for (i = 0; i < kctl_pdata->num_codec_dai; ++i) {
  813. codec_dai = kctl_pdata->dai[i];
  814. if(!codec_dai) {
  815. continue;
  816. }
  817. cur_rx_ch_cnt = 0;
  818. cur_tx_ch_cnt = 0;
  819. cur_tx_ch = 0;
  820. cur_rx_ch = 0;
  821. ret = snd_soc_dai_get_channel_map(codec_dai,
  822. &cur_tx_ch_cnt, &cur_tx_ch,
  823. &cur_rx_ch_cnt, &cur_rx_ch);
  824. /* DAIs that not supports get_channel_map should pass */
  825. if (ret && (ret != -ENOTSUPP)) {
  826. pr_err("%s: get channel map failed for backend_id:%d,"
  827. " ret:%d\n",
  828. __func__, backend_id, ret);
  829. return ret;
  830. }
  831. rx_ch_cnt += cur_rx_ch_cnt;
  832. tx_ch_cnt += cur_tx_ch_cnt;
  833. rx_ch[0] |= cur_rx_ch;
  834. tx_ch[0] |= cur_tx_ch;
  835. }
  836. /* reset return value from the loop above */
  837. ret = 0;
  838. if (rx_ch_cnt == 0 && tx_ch_cnt == 0) {
  839. pr_debug("%s: incorrect ch map for backend_id:%d, RX Channel Cnt:%d, TX Channel Cnt:%d\n",
  840. __func__, backend_id, rx_ch_cnt, tx_ch_cnt);
  841. return ret;
  842. }
  843. chmap_data = kzalloc(sizeof(uint32_t) * 2, GFP_KERNEL);
  844. if (!chmap_data)
  845. return -ENOMEM;
  846. if (rx_ch_cnt) {
  847. chmap_data[0] = rx_ch_cnt;
  848. chmap_data[1] = rx_ch[0];
  849. } else {
  850. chmap_data[0] = tx_ch_cnt;
  851. chmap_data[1] = tx_ch[0];
  852. }
  853. memcpy(ucontrol->value.bytes.data, chmap_data,
  854. sizeof(uint32_t) * 2);
  855. break;
  856. }
  857. default:
  858. pr_err("%s, Invalid backend %d\n", __func__, backend_id);
  859. ret = -EINVAL;
  860. break;
  861. }
  862. kfree(chmap_data);
  863. return ret;
  864. }
  865. void msm_common_get_backend_name(const char *stream_name, char **backend_name)
  866. {
  867. char arg[ARRAY_SZ] = {0};
  868. char value[61] = {0};
  869. sscanf(stream_name, "%20[^-]-%60s", arg, value);
  870. *backend_name = kzalloc(ARRAY_SZ, GFP_KERNEL);
  871. if (!(*backend_name))
  872. return;
  873. strlcpy(*backend_name, arg, ARRAY_SZ);
  874. }
  875. static void msm_audio_update_qos_request(u32 latency)
  876. {
  877. int cpu = 0;
  878. int ret = -1;
  879. if (msm_audio_req) {
  880. for_each_cpu(cpu, &audio_cpu_map) {
  881. ret = dev_pm_qos_update_request(
  882. &msm_audio_req[cpu], latency);
  883. if (1 == ret ) {
  884. pr_debug("%s: updated latency of core %d to %u.\n",
  885. __func__, cpu, latency);
  886. } else if (0 == ret) {
  887. pr_debug("%s: latency of core %d not changed. latency %u.\n",
  888. __func__, cpu, latency);
  889. } else {
  890. pr_err("%s: failed to update latency of core %d, error %d \n",
  891. __func__, cpu, ret);
  892. }
  893. }
  894. }
  895. }
  896. static int msm_get_and_print_cpu_map_taken(cpumask_t* expected_cpu_map) {
  897. int ret = 0;
  898. int cpu = 0;
  899. cpumask_t current_cpu_map = walt_get_cpus_taken();
  900. if (memcmp(&current_cpu_map, &CPU_MASK_NONE, sizeof(cpumask_t)) == 0) {
  901. pr_debug("%s: current cpu map is none.\n", __func__);
  902. } else {
  903. for_each_cpu(cpu, &current_cpu_map) {
  904. pr_debug("%s: current cpu core taken %d.\n", __func__, cpu);
  905. }
  906. }
  907. if (memcmp(&current_cpu_map, expected_cpu_map, sizeof(cpumask_t)) == 0)
  908. ret = 1;
  909. return ret;
  910. }
  911. static int msm_qos_ctl_put(struct snd_kcontrol *kcontrol,
  912. struct snd_ctl_elem_value *ucontrol)
  913. {
  914. cpumask_t expected_cpu_map = CPU_MASK_NONE;
  915. qos_vote_status = ucontrol->value.enumerated.item[0];
  916. pr_debug("%s: qos_vote_status = %d, qos_client_active_cnt = %d.\n",
  917. __func__, qos_vote_status, qos_client_active_cnt);
  918. if (qos_vote_status) {
  919. if (dev_pm_qos_request_active(&latency_pm_qos_req))
  920. dev_pm_qos_remove_request(&latency_pm_qos_req);
  921. qos_client_active_cnt++;
  922. if (qos_client_active_cnt == 1) {
  923. msm_audio_update_qos_request(MSM_LL_QOS_VALUE);
  924. expected_cpu_map = audio_cpu_map;
  925. if (msm_get_and_print_cpu_map_taken(&expected_cpu_map)) {
  926. pr_debug("%s: already expected, don't need to set it.\n",
  927. __func__);
  928. return 0;
  929. }
  930. walt_set_cpus_taken(&audio_cpu_map);
  931. pr_debug("%s: set cpus taken to walt for audio RT tasks.\n",
  932. __func__);
  933. if (msm_get_and_print_cpu_map_taken(&expected_cpu_map)) {
  934. pr_debug("%s: set cpus taken as expected successfully.\n",
  935. __func__);
  936. }
  937. }
  938. } else {
  939. if (qos_client_active_cnt > 0)
  940. qos_client_active_cnt--;
  941. if (qos_client_active_cnt == 0) {
  942. msm_audio_update_qos_request(PM_QOS_CPU_LATENCY_DEFAULT_VALUE);
  943. if (msm_get_and_print_cpu_map_taken(&expected_cpu_map)) {
  944. pr_debug("%s: already expected, don't need to unset it.\n",
  945. __func__);
  946. return 0;
  947. }
  948. walt_unset_cpus_taken(&audio_cpu_map);
  949. pr_debug("%s: unset cpus taken to walt for audio RT tasks.\n",
  950. __func__);
  951. if (msm_get_and_print_cpu_map_taken(&expected_cpu_map)) {
  952. pr_debug("%s: unset cpus taken as expected successfully.\n",
  953. __func__);
  954. }
  955. }
  956. }
  957. return 0;
  958. }
  959. static int msm_qos_ctl_get(struct snd_kcontrol *kcontrol,
  960. struct snd_ctl_elem_value *ucontrol)
  961. {
  962. ucontrol->value.enumerated.item[0] = qos_vote_status;
  963. return 0;
  964. }
  965. static int msm_lpi_logging_enable_put(struct snd_kcontrol *kcontrol,
  966. struct snd_ctl_elem_value *ucontrol)
  967. {
  968. lpi_pcm_logging_enable = ucontrol->value.integer.value[0];
  969. pr_debug("%s: lpi pcm logging enable: %d", __func__,
  970. lpi_pcm_logging_enable);
  971. audio_prm_set_lpi_logging_status((int)lpi_pcm_logging_enable);
  972. return 0;
  973. }
  974. static int msm_lpi_logging_enable_get(struct snd_kcontrol *kcontrol,
  975. struct snd_ctl_elem_value *ucontrol)
  976. {
  977. ucontrol->value.integer.value[0] = lpi_pcm_logging_enable;
  978. return 0;
  979. }
  980. static int msm_vote_against_sleep_ctl_put(struct snd_kcontrol *kcontrol,
  981. struct snd_ctl_elem_value *ucontrol)
  982. {
  983. int ret = 0;
  984. vote_against_sleep_enable = ucontrol->value.integer.value[0];
  985. pr_debug("%s: vote against sleep enable: %d sleep cnt: %d", __func__,
  986. vote_against_sleep_enable, vote_against_sleep_cnt);
  987. if (vote_against_sleep_enable) {
  988. vote_against_sleep_cnt++;
  989. if (vote_against_sleep_cnt == 1) {
  990. ret = audio_prm_set_vote_against_sleep(1);
  991. if (ret < 0) {
  992. --vote_against_sleep_cnt;
  993. pr_err("%s: failed to vote against sleep ret: %d\n", __func__, ret);
  994. }
  995. }
  996. } else {
  997. if (vote_against_sleep_cnt == 1)
  998. ret = audio_prm_set_vote_against_sleep(0);
  999. if (vote_against_sleep_cnt > 0)
  1000. vote_against_sleep_cnt--;
  1001. }
  1002. pr_debug("%s: vote against sleep vote ret: %d\n", __func__, ret);
  1003. return ret;
  1004. }
  1005. static int msm_vote_against_sleep_ctl_get(struct snd_kcontrol *kcontrol,
  1006. struct snd_ctl_elem_value *ucontrol)
  1007. {
  1008. ucontrol->value.integer.value[0] = vote_against_sleep_enable;
  1009. pr_debug("%s: vote against sleep enable: %d", __func__,
  1010. vote_against_sleep_enable);
  1011. return 0;
  1012. }
  1013. static const char *const qos_text[] = {"Disable", "Enable"};
  1014. static const char *const against_sleep_text[] = {"Disable", "Enable"};
  1015. static SOC_ENUM_SINGLE_EXT_DECL(qos_vote, qos_text);
  1016. static SOC_ENUM_SINGLE_EXT_DECL(sleep_against, against_sleep_text);
  1017. static const struct snd_kcontrol_new card_mixer_controls[] = {
  1018. SOC_ENUM_EXT("PM_QOS Vote", qos_vote,
  1019. msm_qos_ctl_get, msm_qos_ctl_put),
  1020. SOC_SINGLE_EXT("LPI PCM Logging Enable", 0, 0, 1, 0,
  1021. msm_lpi_logging_enable_get, msm_lpi_logging_enable_put),
  1022. SOC_ENUM_EXT("VOTE Against Sleep", sleep_against,
  1023. msm_vote_against_sleep_ctl_get, msm_vote_against_sleep_ctl_put),
  1024. };
  1025. static int msm_register_pm_qos_latency_controls(struct snd_soc_pcm_runtime *rtd)
  1026. {
  1027. struct snd_soc_component *lpass_cdc_component = NULL;
  1028. int ret = 0;
  1029. lpass_cdc_component = snd_soc_rtdcom_lookup(rtd, "lpass-cdc");
  1030. if (!lpass_cdc_component) {
  1031. pr_err("%s: could not find component for lpass-cdc\n",
  1032. __func__);
  1033. return -EINVAL;
  1034. }
  1035. ret = snd_soc_add_component_controls(lpass_cdc_component,
  1036. card_mixer_controls, ARRAY_SIZE(card_mixer_controls));
  1037. if (ret < 0) {
  1038. pr_err("%s: add common snd controls failed: %d\n",
  1039. __func__, ret);
  1040. return -EINVAL;
  1041. }
  1042. return 0;
  1043. }
  1044. int msm_common_dai_link_init(struct snd_soc_pcm_runtime *rtd)
  1045. {
  1046. struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
  1047. struct snd_soc_component *component = NULL;
  1048. struct snd_soc_dai_link *dai_link = rtd->dai_link;
  1049. struct device *dev = rtd->card->dev;
  1050. int ret = 0;
  1051. int index = 0;
  1052. const char *mixer_ctl_name = CODEC_CHMAP;
  1053. char *mixer_str = NULL;
  1054. char *backend_name = NULL;
  1055. uint32_t ctl_len = 0;
  1056. struct chmap_pdata *pdata;
  1057. struct snd_kcontrol *kctl;
  1058. struct snd_kcontrol_new msm_common_channel_map[1] = {
  1059. {
  1060. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1061. .name = "?",
  1062. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  1063. .info = msm_channel_map_info,
  1064. .get = msm_channel_map_get,
  1065. .private_value = 0,
  1066. }
  1067. };
  1068. if (!codec_dai) {
  1069. pr_err("%s: failed to get codec dai", __func__);
  1070. return -EINVAL;
  1071. }
  1072. component = codec_dai->component;
  1073. msm_common_get_backend_name(dai_link->stream_name, &backend_name);
  1074. if (!backend_name) {
  1075. pr_err("%s: failed to get backend name", __func__);
  1076. return -EINVAL;
  1077. }
  1078. pdata = devm_kzalloc(dev, sizeof(struct chmap_pdata), GFP_KERNEL);
  1079. if (!pdata) {
  1080. ret = -ENOMEM;
  1081. goto free_backend;
  1082. }
  1083. if ((!strncmp(backend_name, "SLIM", strlen("SLIM"))) ||
  1084. (!strncmp(backend_name, "CODEC_DMA", strlen("CODEC_DMA")))) {
  1085. ctl_len = strlen(dai_link->stream_name) + 1 +
  1086. strlen(mixer_ctl_name) + 1;
  1087. mixer_str = kzalloc(ctl_len, GFP_KERNEL);
  1088. if (!mixer_str) {
  1089. ret = -ENOMEM;
  1090. goto free_backend;
  1091. }
  1092. snprintf(mixer_str, ctl_len, "%s %s", dai_link->stream_name,
  1093. mixer_ctl_name);
  1094. msm_common_channel_map[0].name = mixer_str;
  1095. msm_common_channel_map[0].private_value = 0;
  1096. pr_debug("Registering new mixer ctl %s\n", mixer_str);
  1097. ret = snd_soc_add_component_controls(component,
  1098. msm_common_channel_map,
  1099. ARRAY_SIZE(msm_common_channel_map));
  1100. kctl = snd_soc_card_get_kcontrol(rtd->card, mixer_str);
  1101. if (!kctl) {
  1102. pr_err("failed to get kctl %s\n", mixer_str);
  1103. ret = -EINVAL;
  1104. goto free_mixer_str;
  1105. }
  1106. pdata->dai[0] = codec_dai;
  1107. pdata->num_codec_dai = 1;
  1108. if (!strncmp(backend_name, "SLIM", strlen("SLIM"))) {
  1109. pdata->id = SLIM;
  1110. } else {
  1111. pdata->id = CODEC_DMA;
  1112. if (rtd->dai_link->num_codecs <= MAX_CODEC_DAI) {
  1113. pdata->num_codec_dai = rtd->dai_link->num_codecs;
  1114. for_each_rtd_codec_dais(rtd, index, codec_dai) {
  1115. pdata->dai[index] = codec_dai;
  1116. }
  1117. }
  1118. }
  1119. kctl->private_data = pdata;
  1120. }
  1121. if (!kregister_pm_qos_latency_controls) {
  1122. if (!msm_register_pm_qos_latency_controls(rtd))
  1123. kregister_pm_qos_latency_controls = true;
  1124. }
  1125. free_mixer_str:
  1126. if (mixer_str) {
  1127. kfree(mixer_str);
  1128. mixer_str = NULL;
  1129. }
  1130. free_backend:
  1131. if (backend_name) {
  1132. kfree(backend_name);
  1133. backend_name = NULL;
  1134. }
  1135. return ret;
  1136. }