msm_common.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303
  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. (index == QUIN_MI2S_TDM_AUXPCM))) {
  388. ret = mi2s_tdm_hw_vote_req(pdata, 1);
  389. if (ret < 0) {
  390. pr_err("%s lpass audio hw vote enable failed %d\n",
  391. __func__, ret);
  392. goto done;
  393. }
  394. }
  395. pr_debug("%s: clk_id :%d clk freq %d\n", __func__,
  396. intf_clk_cfg.clk_id, intf_clk_cfg.clk_freq_in_hz);
  397. ret = audio_prm_set_lpass_clk_cfg(&intf_clk_cfg, 1);
  398. if (ret < 0) {
  399. pr_err("%s: prm lpass tdm clk cfg set failed ret %d\n",
  400. __func__, ret);
  401. goto done;
  402. }
  403. } else if ((strnstr(stream_name, "MI2S", strlen(stream_name)))) {
  404. ret = get_mi2s_clk_id(index);
  405. if (ret < 0)
  406. goto done;
  407. intf_clk_cfg.clk_id = ret;
  408. rate = params_rate(params);
  409. switch (params_format(params)) {
  410. case SNDRV_PCM_FORMAT_S24_LE:
  411. case SNDRV_PCM_FORMAT_S24_3LE:
  412. case SNDRV_PCM_FORMAT_S32_LE:
  413. sample_width = 32;
  414. break;
  415. case SNDRV_PCM_FORMAT_S16_LE:
  416. default:
  417. sample_width = 16;
  418. pr_debug("%s: bitwidth set to default : %d\n",
  419. __func__, sample_width);
  420. }
  421. intf_clk_cfg.clk_freq_in_hz = rate *
  422. MI2S_NUM_CHANNELS * sample_width;
  423. intf_clk_cfg.clk_attri = pdata->mi2s_clk_attribute[index];
  424. intf_clk_cfg.clk_root = CLOCK_ROOT_DEFAULT;
  425. if (pdata->is_audio_hw_vote_required[index] &&
  426. (is_fractional_sample_rate(rate) ||
  427. (index == QUIN_MI2S_TDM_AUXPCM))) {
  428. ret = mi2s_tdm_hw_vote_req(pdata, 1);
  429. if (ret < 0) {
  430. pr_err("%s lpass audio hw vote enable failed %d\n",
  431. __func__, ret);
  432. goto done;
  433. }
  434. }
  435. pr_debug("%s: mi2s clk_id :%d clk freq %d\n", __func__,
  436. intf_clk_cfg.clk_id, intf_clk_cfg.clk_freq_in_hz);
  437. ret = audio_prm_set_lpass_clk_cfg(&intf_clk_cfg, 1);
  438. if (ret < 0) {
  439. pr_err("%s: prm lpass mi2s clk cfg set failed ret %d\n",
  440. __func__, ret);
  441. goto done;
  442. }
  443. } else {
  444. pr_err("%s: unsupported stream name: %s\n",
  445. __func__, stream_name);
  446. goto done;
  447. }
  448. }
  449. atomic_inc(&pdata->lpass_intf_clk_ref_cnt[index]);
  450. done:
  451. mutex_unlock(&pdata->lock[index]);
  452. }
  453. return ret;
  454. }
  455. int msm_common_snd_startup(struct snd_pcm_substream *substream)
  456. {
  457. int ret = 0;
  458. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  459. struct snd_soc_card *card = rtd->card;
  460. struct msm_common_pdata *pdata = msm_common_get_pdata(card);
  461. const char *stream_name = rtd->dai_link->stream_name;
  462. int index = get_mi2s_tdm_auxpcm_intf_index(stream_name);
  463. dev_dbg(rtd->card->dev,
  464. "%s: substream = %s stream = %d\n",
  465. __func__, substream->name, substream->stream);
  466. if (!pdata) {
  467. dev_err(rtd->card->dev, "%s: pdata is NULL\n", __func__);
  468. return -EINVAL;
  469. }
  470. if (!rtd->dai_link->no_pcm)
  471. snd_soc_set_runtime_hwparams(substream, &dummy_dma_hardware);
  472. if (index >= 0) {
  473. mutex_lock(&pdata->lock[index]);
  474. if (pdata->mi2s_gpio_p[index]) {
  475. if (atomic_read(&(pdata->mi2s_gpio_ref_cnt[index])) == 0) {
  476. ret = msm_cdc_pinctrl_select_active_state(
  477. pdata->mi2s_gpio_p[index]);
  478. if (ret) {
  479. pr_err("%s:pinctrl set actve fail with %d\n",
  480. __func__, ret);
  481. goto done;
  482. }
  483. }
  484. atomic_inc(&(pdata->mi2s_gpio_ref_cnt[index]));
  485. }
  486. done:
  487. mutex_unlock(&pdata->lock[index]);
  488. }
  489. return ret;
  490. }
  491. void msm_common_snd_shutdown(struct snd_pcm_substream *substream)
  492. {
  493. int ret;
  494. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  495. struct snd_soc_card *card = rtd->card;
  496. struct msm_common_pdata *pdata = msm_common_get_pdata(card);
  497. struct snd_pcm_runtime *runtime = substream->runtime;
  498. const char *stream_name = rtd->dai_link->stream_name;
  499. int index = get_mi2s_tdm_auxpcm_intf_index(stream_name);
  500. struct clk_cfg intf_clk_cfg;
  501. unsigned int rate = runtime->rate;
  502. memset(&intf_clk_cfg, 0, sizeof(struct clk_cfg));
  503. pr_debug("%s(): substream = %s stream = %d\n", __func__,
  504. substream->name, substream->stream);
  505. if (!pdata) {
  506. dev_err(card->dev, "%s: pdata is NULL\n", __func__);
  507. return;
  508. }
  509. check_userspace_service_state(rtd, pdata);
  510. if (index >= 0) {
  511. mutex_lock(&pdata->lock[index]);
  512. atomic_dec(&pdata->lpass_intf_clk_ref_cnt[index]);
  513. if (atomic_read(&pdata->lpass_intf_clk_ref_cnt[index]) == 0) {
  514. if ((strnstr(stream_name, "TDM", strlen(stream_name)))) {
  515. ret = get_tdm_clk_id(index);
  516. if (ret > 0) {
  517. intf_clk_cfg.clk_id = ret;
  518. ret = audio_prm_set_lpass_clk_cfg(&intf_clk_cfg, 0);
  519. if (ret < 0)
  520. pr_err("%s: prm tdm clk cfg set failed ret %d\n",
  521. __func__, ret);
  522. }
  523. } else if((strnstr(stream_name, "MI2S", strlen(stream_name)))) {
  524. ret = get_mi2s_clk_id(index);
  525. if (ret > 0) {
  526. intf_clk_cfg.clk_id = ret;
  527. ret = audio_prm_set_lpass_clk_cfg(&intf_clk_cfg, 0);
  528. if (ret < 0)
  529. pr_err("%s: prm mi2s clk cfg disable failed ret %d\n",
  530. __func__, ret);
  531. }
  532. } else {
  533. pr_err("%s: unsupported stream name: %s\n",
  534. __func__, stream_name);
  535. }
  536. if (pdata->is_audio_hw_vote_required[index] &&
  537. (is_fractional_sample_rate(rate) ||
  538. (index == QUIN_MI2S_TDM_AUXPCM))) {
  539. ret = mi2s_tdm_hw_vote_req(pdata, 0);
  540. }
  541. } else if (atomic_read(&pdata->lpass_intf_clk_ref_cnt[index]) < 0) {
  542. atomic_set(&pdata->lpass_intf_clk_ref_cnt[index], 0);
  543. }
  544. if (pdata->mi2s_gpio_p[index]) {
  545. atomic_dec(&pdata->mi2s_gpio_ref_cnt[index]);
  546. if (atomic_read(&pdata->mi2s_gpio_ref_cnt[index]) == 0) {
  547. ret = msm_cdc_pinctrl_select_sleep_state(
  548. pdata->mi2s_gpio_p[index]);
  549. if (ret)
  550. dev_err(card->dev,
  551. "%s: pinctrl set actv fail %d\n",
  552. __func__, ret);
  553. } else if (atomic_read(&pdata->mi2s_gpio_ref_cnt[index]) < 0) {
  554. atomic_set(&pdata->mi2s_gpio_ref_cnt[index], 0);
  555. }
  556. }
  557. mutex_unlock(&pdata->lock[index]);
  558. }
  559. }
  560. static void msm_audio_add_qos_request(void)
  561. {
  562. int i;
  563. int cpu = 0;
  564. int ret = 0;
  565. msm_audio_req = kcalloc(num_possible_cpus(),
  566. sizeof(struct dev_pm_qos_request), GFP_KERNEL);
  567. if (!msm_audio_req)
  568. return;
  569. for (i = 0; i < audio_core_num; i++) {
  570. if (audio_core_list[i] >= num_possible_cpus())
  571. pr_err("%s incorrect cpu id: %d specified.\n",
  572. __func__, audio_core_list[i]);
  573. else
  574. cpumask_set_cpu(audio_core_list[i], &audio_cpu_map);
  575. }
  576. for_each_cpu(cpu, &audio_cpu_map) {
  577. ret = dev_pm_qos_add_request(get_cpu_device(cpu),
  578. &msm_audio_req[cpu],
  579. DEV_PM_QOS_RESUME_LATENCY,
  580. PM_QOS_CPU_LATENCY_DEFAULT_VALUE);
  581. if (ret < 0)
  582. pr_err("%s error (%d) adding resume latency to cpu %d.\n",
  583. __func__, ret, cpu);
  584. pr_debug("%s set cpu affinity to core %d.\n", __func__, cpu);
  585. }
  586. }
  587. static void msm_audio_remove_qos_request(void)
  588. {
  589. int cpu = 0;
  590. int ret = 0;
  591. if (msm_audio_req) {
  592. for_each_cpu(cpu, &audio_cpu_map) {
  593. ret = dev_pm_qos_remove_request(
  594. &msm_audio_req[cpu]);
  595. if (ret < 0)
  596. pr_err("%s error (%d) removing request from cpu %d.\n",
  597. __func__, ret, cpu);
  598. pr_debug("%s remove cpu affinity of core %d.\n", __func__, cpu);
  599. }
  600. kfree(msm_audio_req);
  601. }
  602. }
  603. int msm_common_snd_init(struct platform_device *pdev, struct snd_soc_card *card)
  604. {
  605. struct msm_common_pdata *common_pdata = NULL;
  606. int count, ret = 0;
  607. uint32_t val_array[MI2S_TDM_AUXPCM_MAX] = {0};
  608. struct clk *lpass_audio_hw_vote = NULL;
  609. uint32_t *core_val_array = NULL;
  610. common_pdata = kcalloc(1, sizeof(struct msm_common_pdata), GFP_KERNEL);
  611. if (!common_pdata)
  612. return -ENOMEM;
  613. for (count = 0; count < MI2S_TDM_AUXPCM_MAX; count++) {
  614. mutex_init(&common_pdata->lock[count]);
  615. atomic_set(&common_pdata->mi2s_gpio_ref_cnt[count], 0);
  616. }
  617. ret = of_property_read_u32(pdev->dev.of_node, "qcom,tdm-max-slots",
  618. &common_pdata->tdm_max_slots);
  619. if (ret) {
  620. dev_info(&pdev->dev, "%s: No DT match for tdm max slots\n",
  621. __func__);
  622. }
  623. if ((common_pdata->tdm_max_slots <= 0) || (common_pdata->tdm_max_slots >
  624. TDM_MAX_SLOTS)) {
  625. common_pdata->tdm_max_slots = TDM_MAX_SLOTS;
  626. dev_info(&pdev->dev, "%s: Using default tdm max slot: %d\n",
  627. __func__, common_pdata->tdm_max_slots);
  628. }
  629. /* Register LPASS audio hw vote */
  630. lpass_audio_hw_vote = devm_clk_get(&pdev->dev, "lpass_audio_hw_vote");
  631. if (IS_ERR(lpass_audio_hw_vote)) {
  632. ret = PTR_ERR(lpass_audio_hw_vote);
  633. dev_dbg(&pdev->dev, "%s: clk get %s failed %d\n",
  634. __func__, "lpass_audio_hw_vote", ret);
  635. lpass_audio_hw_vote = NULL;
  636. ret = 0;
  637. }
  638. common_pdata->lpass_audio_hw_vote = lpass_audio_hw_vote;
  639. ret = of_property_read_u32_array(pdev->dev.of_node,
  640. "qcom,mi2s-tdm-is-hw-vote-needed",
  641. val_array, MI2S_TDM_AUXPCM_MAX);
  642. if (ret) {
  643. dev_dbg(&pdev->dev, "%s:no qcom,mi2s-tdm-is-hw-vote-needed in DT node\n",
  644. __func__);
  645. } else {
  646. for (count = 0; count < MI2S_TDM_AUXPCM_MAX; count++) {
  647. common_pdata->is_audio_hw_vote_required[count] =
  648. val_array[count];
  649. }
  650. }
  651. ret = of_property_read_u32_array(pdev->dev.of_node, "qcom,tdm-clk-attribute",
  652. val_array, MI2S_TDM_AUXPCM_MAX);
  653. if (ret) {
  654. dev_info(&pdev->dev,
  655. "%s: No DT match for tdm clk attribute, set to default\n", __func__);
  656. for (count = 0; count < MI2S_TDM_AUXPCM_MAX; count++) {
  657. common_pdata->tdm_clk_attribute[count] =
  658. CLOCK_ATTRIBUTE_COUPLE_NO;
  659. }
  660. } else {
  661. for (count = 0; count < MI2S_TDM_AUXPCM_MAX; count++) {
  662. common_pdata->tdm_clk_attribute[count] =
  663. val_array[count];
  664. }
  665. }
  666. ret = of_property_read_u32_array(pdev->dev.of_node, "qcom,mi2s-clk-attribute",
  667. val_array, MI2S_TDM_AUXPCM_MAX);
  668. if (ret) {
  669. dev_info(&pdev->dev,
  670. "%s: No DT match for mi2s clk attribute, set to default\n", __func__);
  671. for (count = 0; count < MI2S_TDM_AUXPCM_MAX; count++) {
  672. common_pdata->mi2s_clk_attribute[count] =
  673. CLOCK_ATTRIBUTE_COUPLE_NO;
  674. }
  675. } else {
  676. for (count = 0; count < MI2S_TDM_AUXPCM_MAX; count++) {
  677. common_pdata->mi2s_clk_attribute[count] =
  678. val_array[count];
  679. }
  680. }
  681. common_pdata->mi2s_gpio_p[PRI_MI2S_TDM_AUXPCM] = of_parse_phandle(pdev->dev.of_node,
  682. "qcom,pri-mi2s-gpios", 0);
  683. common_pdata->mi2s_gpio_p[SEC_MI2S_TDM_AUXPCM] = of_parse_phandle(pdev->dev.of_node,
  684. "qcom,sec-mi2s-gpios", 0);
  685. common_pdata->mi2s_gpio_p[TER_MI2S_TDM_AUXPCM] = of_parse_phandle(pdev->dev.of_node,
  686. "qcom,tert-mi2s-gpios", 0);
  687. common_pdata->mi2s_gpio_p[QUAT_MI2S_TDM_AUXPCM] = of_parse_phandle(pdev->dev.of_node,
  688. "qcom,quat-mi2s-gpios", 0);
  689. common_pdata->mi2s_gpio_p[QUIN_MI2S_TDM_AUXPCM] = of_parse_phandle(pdev->dev.of_node,
  690. "qcom,quin-mi2s-gpios", 0);
  691. common_pdata->mi2s_gpio_p[SEN_MI2S_TDM_AUXPCM] = of_parse_phandle(pdev->dev.of_node,
  692. "qcom,sen-mi2s-gpios", 0);
  693. common_pdata->mi2s_gpio_p[SEP_MI2S_TDM_AUXPCM] = of_parse_phandle(pdev->dev.of_node,
  694. "qcom,sep-mi2s-gpios", 0);
  695. common_pdata->aud_dev_state = devm_kcalloc(&pdev->dev, card->num_links,
  696. sizeof(uint8_t), GFP_KERNEL);
  697. dev_info(&pdev->dev, "num_links %d \n", card->num_links);
  698. common_pdata->num_aud_devs = card->num_links;
  699. mutex_init(&common_pdata->aud_dev_lock);
  700. aud_dev_sysfs_init(common_pdata);
  701. msm_common_set_pdata(card, common_pdata);
  702. /* Add QoS request for audio tasks */
  703. core_val_array = devm_kcalloc(&pdev->dev, num_possible_cpus(), sizeof(uint32_t), GFP_KERNEL);
  704. if (!core_val_array) {
  705. dev_info(&pdev->dev, "%s: core val array is nullptr\n", __func__);
  706. goto exit;
  707. }
  708. ret = of_property_read_variable_u32_array(pdev->dev.of_node, "qcom,audio-core-list",
  709. core_val_array, 0, num_possible_cpus());
  710. dev_info(&pdev->dev, "%s: getting the core list size:%d, num_possible_cpus:%d \n",
  711. __func__, ret, num_possible_cpus());
  712. if (ret > 0 && (ret <= num_possible_cpus())) {
  713. audio_core_num = ret;
  714. audio_core_list = devm_kcalloc(&pdev->dev, audio_core_num, sizeof(uint32_t), GFP_KERNEL);
  715. if (!audio_core_list) {
  716. dev_info(&pdev->dev, "%s: calloc failed for audio core list\n", __func__);
  717. goto exit;
  718. }
  719. for (count = 0; count < audio_core_num; count++) {
  720. audio_core_list[count] = core_val_array[count];
  721. dev_info(&pdev->dev, "%s: update core %d\n", __func__, core_val_array[count]);
  722. }
  723. } else {
  724. dev_info(&pdev->dev, "%s: keep default core\n", __func__);
  725. audio_core_list = devm_kcalloc(&pdev->dev, audio_core_num, sizeof(uint32_t), GFP_KERNEL);
  726. /* set audio task affinity to core 1 & 2 as default*/
  727. if (!audio_core_list) {
  728. dev_info(&pdev->dev, "%s: calloc failed for audio core list\n", __func__);
  729. goto exit;
  730. }
  731. audio_core_list[0] = 1;
  732. audio_core_list[1] = 2;
  733. }
  734. msm_audio_add_qos_request();
  735. exit:
  736. if (audio_core_list) {
  737. devm_kfree(&pdev->dev, audio_core_list);
  738. audio_core_list = NULL;
  739. }
  740. if (core_val_array) {
  741. devm_kfree(&pdev->dev, core_val_array);
  742. }
  743. return 0;
  744. };
  745. void msm_common_snd_deinit(struct msm_common_pdata *common_pdata)
  746. {
  747. int count;
  748. if (!common_pdata)
  749. return;
  750. msm_audio_remove_qos_request();
  751. mutex_destroy(&common_pdata->aud_dev_lock);
  752. for (count = 0; count < MI2S_TDM_AUXPCM_MAX; count++) {
  753. mutex_destroy(&common_pdata->lock[count]);
  754. }
  755. }
  756. int msm_channel_map_info(struct snd_kcontrol *kcontrol,
  757. struct snd_ctl_elem_info *uinfo)
  758. {
  759. uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
  760. uinfo->count = sizeof(uint32_t) * MAX_PORT;
  761. return 0;
  762. }
  763. int msm_channel_map_get(struct snd_kcontrol *kcontrol,
  764. struct snd_ctl_elem_value *ucontrol)
  765. {
  766. struct chmap_pdata *kctl_pdata =
  767. (struct chmap_pdata *)kcontrol->private_data;
  768. struct snd_soc_dai *codec_dai = NULL;
  769. int backend_id = 0;
  770. uint32_t rx_ch[MAX_PORT] = {0}, tx_ch[MAX_PORT] = {0};
  771. uint32_t rx_ch_cnt = 0, tx_ch_cnt = 0;
  772. uint32_t *chmap_data = NULL;
  773. int ret = 0, len = 0, i = 0;
  774. if (kctl_pdata == NULL) {
  775. pr_debug("%s: chmap_pdata is not initialized\n", __func__);
  776. return -EINVAL;
  777. }
  778. codec_dai = kctl_pdata->dai[0];
  779. backend_id = kctl_pdata->id;
  780. switch (backend_id) {
  781. case SLIM: {
  782. uint32_t *chmap;
  783. uint32_t ch_cnt;
  784. ret = snd_soc_dai_get_channel_map(codec_dai,
  785. &tx_ch_cnt, tx_ch, &rx_ch_cnt, rx_ch);
  786. if (ret || (tx_ch_cnt == 0 && rx_ch_cnt == 0)) {
  787. pr_debug("%s: got incorrect channel map for backend_id:%d\n",
  788. __func__, backend_id);
  789. return ret;
  790. }
  791. if (rx_ch_cnt) {
  792. chmap = rx_ch;
  793. ch_cnt = rx_ch_cnt;
  794. } else {
  795. chmap = tx_ch;
  796. ch_cnt = tx_ch_cnt;
  797. }
  798. if (ch_cnt > 2) {
  799. pr_err("%s: Incorrect channel count: %d\n", __func__, ch_cnt);
  800. return -EINVAL;
  801. }
  802. len = sizeof(uint32_t) * (ch_cnt + 1);
  803. chmap_data = kzalloc(len, GFP_KERNEL);
  804. if (!chmap_data)
  805. return -ENOMEM;
  806. chmap_data[0] = ch_cnt;
  807. for (i = 0; i < ch_cnt; i++)
  808. chmap_data[i+1] = chmap[i];
  809. memcpy(ucontrol->value.bytes.data, chmap_data, len);
  810. break;
  811. }
  812. case CODEC_DMA: {
  813. uint32_t cur_rx_ch = 0, cur_tx_ch = 0;
  814. uint32_t cur_rx_ch_cnt = 0, cur_tx_ch_cnt = 0;
  815. for (i = 0; i < kctl_pdata->num_codec_dai; ++i) {
  816. codec_dai = kctl_pdata->dai[i];
  817. if(!codec_dai) {
  818. continue;
  819. }
  820. cur_rx_ch_cnt = 0;
  821. cur_tx_ch_cnt = 0;
  822. cur_tx_ch = 0;
  823. cur_rx_ch = 0;
  824. ret = snd_soc_dai_get_channel_map(codec_dai,
  825. &cur_tx_ch_cnt, &cur_tx_ch,
  826. &cur_rx_ch_cnt, &cur_rx_ch);
  827. /* DAIs that not supports get_channel_map should pass */
  828. if (ret && (ret != -ENOTSUPP)) {
  829. pr_err("%s: get channel map failed for backend_id:%d,"
  830. " ret:%d\n",
  831. __func__, backend_id, ret);
  832. return ret;
  833. }
  834. rx_ch_cnt += cur_rx_ch_cnt;
  835. tx_ch_cnt += cur_tx_ch_cnt;
  836. rx_ch[0] |= cur_rx_ch;
  837. tx_ch[0] |= cur_tx_ch;
  838. }
  839. /* reset return value from the loop above */
  840. ret = 0;
  841. if (rx_ch_cnt == 0 && tx_ch_cnt == 0) {
  842. pr_debug("%s: incorrect ch map for backend_id:%d, RX Channel Cnt:%d, TX Channel Cnt:%d\n",
  843. __func__, backend_id, rx_ch_cnt, tx_ch_cnt);
  844. return ret;
  845. }
  846. chmap_data = kzalloc(sizeof(uint32_t) * 2, GFP_KERNEL);
  847. if (!chmap_data)
  848. return -ENOMEM;
  849. if (rx_ch_cnt) {
  850. chmap_data[0] = rx_ch_cnt;
  851. chmap_data[1] = rx_ch[0];
  852. } else {
  853. chmap_data[0] = tx_ch_cnt;
  854. chmap_data[1] = tx_ch[0];
  855. }
  856. memcpy(ucontrol->value.bytes.data, chmap_data,
  857. sizeof(uint32_t) * 2);
  858. break;
  859. }
  860. default:
  861. pr_err("%s, Invalid backend %d\n", __func__, backend_id);
  862. ret = -EINVAL;
  863. break;
  864. }
  865. kfree(chmap_data);
  866. return ret;
  867. }
  868. void msm_common_get_backend_name(const char *stream_name, char **backend_name)
  869. {
  870. char arg[ARRAY_SZ] = {0};
  871. char value[61] = {0};
  872. sscanf(stream_name, "%20[^-]-%60s", arg, value);
  873. *backend_name = kzalloc(ARRAY_SZ, GFP_KERNEL);
  874. if (!(*backend_name))
  875. return;
  876. strlcpy(*backend_name, arg, ARRAY_SZ);
  877. }
  878. static void msm_audio_update_qos_request(u32 latency)
  879. {
  880. int cpu = 0;
  881. int ret = -1;
  882. if (msm_audio_req) {
  883. for_each_cpu(cpu, &audio_cpu_map) {
  884. ret = dev_pm_qos_update_request(
  885. &msm_audio_req[cpu], latency);
  886. if (1 == ret ) {
  887. pr_debug("%s: updated latency of core %d to %u.\n",
  888. __func__, cpu, latency);
  889. } else if (0 == ret) {
  890. pr_debug("%s: latency of core %d not changed. latency %u.\n",
  891. __func__, cpu, latency);
  892. } else {
  893. pr_err("%s: failed to update latency of core %d, error %d \n",
  894. __func__, cpu, ret);
  895. }
  896. }
  897. }
  898. }
  899. static int msm_get_and_print_cpu_map_taken(cpumask_t* expected_cpu_map) {
  900. int ret = 0;
  901. int cpu = 0;
  902. cpumask_t current_cpu_map = walt_get_cpus_taken();
  903. if (memcmp(&current_cpu_map, &CPU_MASK_NONE, sizeof(cpumask_t)) == 0) {
  904. pr_debug("%s: current cpu map is none.\n", __func__);
  905. } else {
  906. for_each_cpu(cpu, &current_cpu_map) {
  907. pr_debug("%s: current cpu core taken %d.\n", __func__, cpu);
  908. }
  909. }
  910. if (memcmp(&current_cpu_map, expected_cpu_map, sizeof(cpumask_t)) == 0)
  911. ret = 1;
  912. return ret;
  913. }
  914. static int msm_qos_ctl_put(struct snd_kcontrol *kcontrol,
  915. struct snd_ctl_elem_value *ucontrol)
  916. {
  917. cpumask_t expected_cpu_map = CPU_MASK_NONE;
  918. qos_vote_status = ucontrol->value.enumerated.item[0];
  919. pr_debug("%s: qos_vote_status = %d, qos_client_active_cnt = %d.\n",
  920. __func__, qos_vote_status, qos_client_active_cnt);
  921. if (qos_vote_status) {
  922. if (dev_pm_qos_request_active(&latency_pm_qos_req))
  923. dev_pm_qos_remove_request(&latency_pm_qos_req);
  924. qos_client_active_cnt++;
  925. if (qos_client_active_cnt == 1) {
  926. msm_audio_update_qos_request(MSM_LL_QOS_VALUE);
  927. expected_cpu_map = audio_cpu_map;
  928. if (msm_get_and_print_cpu_map_taken(&expected_cpu_map)) {
  929. pr_debug("%s: already expected, don't need to set it.\n",
  930. __func__);
  931. return 0;
  932. }
  933. walt_set_cpus_taken(&audio_cpu_map);
  934. pr_debug("%s: set cpus taken to walt for audio RT tasks.\n",
  935. __func__);
  936. if (msm_get_and_print_cpu_map_taken(&expected_cpu_map)) {
  937. pr_debug("%s: set cpus taken as expected successfully.\n",
  938. __func__);
  939. }
  940. }
  941. } else {
  942. if (qos_client_active_cnt > 0)
  943. qos_client_active_cnt--;
  944. if (qos_client_active_cnt == 0) {
  945. msm_audio_update_qos_request(PM_QOS_CPU_LATENCY_DEFAULT_VALUE);
  946. if (msm_get_and_print_cpu_map_taken(&expected_cpu_map)) {
  947. pr_debug("%s: already expected, don't need to unset it.\n",
  948. __func__);
  949. return 0;
  950. }
  951. walt_unset_cpus_taken(&audio_cpu_map);
  952. pr_debug("%s: unset cpus taken to walt for audio RT tasks.\n",
  953. __func__);
  954. if (msm_get_and_print_cpu_map_taken(&expected_cpu_map)) {
  955. pr_debug("%s: unset cpus taken as expected successfully.\n",
  956. __func__);
  957. }
  958. }
  959. }
  960. return 0;
  961. }
  962. static int msm_qos_ctl_get(struct snd_kcontrol *kcontrol,
  963. struct snd_ctl_elem_value *ucontrol)
  964. {
  965. ucontrol->value.enumerated.item[0] = qos_vote_status;
  966. return 0;
  967. }
  968. static int msm_lpi_logging_enable_put(struct snd_kcontrol *kcontrol,
  969. struct snd_ctl_elem_value *ucontrol)
  970. {
  971. lpi_pcm_logging_enable = ucontrol->value.integer.value[0];
  972. pr_debug("%s: lpi pcm logging enable: %d", __func__,
  973. lpi_pcm_logging_enable);
  974. audio_prm_set_lpi_logging_status((int)lpi_pcm_logging_enable);
  975. return 0;
  976. }
  977. static int msm_lpi_logging_enable_get(struct snd_kcontrol *kcontrol,
  978. struct snd_ctl_elem_value *ucontrol)
  979. {
  980. ucontrol->value.integer.value[0] = lpi_pcm_logging_enable;
  981. return 0;
  982. }
  983. static int msm_vote_against_sleep_ctl_put(struct snd_kcontrol *kcontrol,
  984. struct snd_ctl_elem_value *ucontrol)
  985. {
  986. int ret = 0;
  987. vote_against_sleep_enable = ucontrol->value.integer.value[0];
  988. pr_debug("%s: vote against sleep enable: %d sleep cnt: %d", __func__,
  989. vote_against_sleep_enable, vote_against_sleep_cnt);
  990. if (vote_against_sleep_enable) {
  991. vote_against_sleep_cnt++;
  992. if (vote_against_sleep_cnt == 1) {
  993. ret = audio_prm_set_vote_against_sleep(1);
  994. if (ret < 0) {
  995. --vote_against_sleep_cnt;
  996. pr_err("%s: failed to vote against sleep ret: %d\n", __func__, ret);
  997. }
  998. }
  999. } else {
  1000. if (vote_against_sleep_cnt == 1)
  1001. ret = audio_prm_set_vote_against_sleep(0);
  1002. if (vote_against_sleep_cnt > 0)
  1003. vote_against_sleep_cnt--;
  1004. }
  1005. pr_debug("%s: vote against sleep vote ret: %d\n", __func__, ret);
  1006. return ret;
  1007. }
  1008. static int msm_vote_against_sleep_ctl_get(struct snd_kcontrol *kcontrol,
  1009. struct snd_ctl_elem_value *ucontrol)
  1010. {
  1011. ucontrol->value.integer.value[0] = vote_against_sleep_enable;
  1012. pr_debug("%s: vote against sleep enable: %d", __func__,
  1013. vote_against_sleep_enable);
  1014. return 0;
  1015. }
  1016. static const char *const qos_text[] = {"Disable", "Enable"};
  1017. static const char *const against_sleep_text[] = {"Disable", "Enable"};
  1018. static SOC_ENUM_SINGLE_EXT_DECL(qos_vote, qos_text);
  1019. static SOC_ENUM_SINGLE_EXT_DECL(sleep_against, against_sleep_text);
  1020. static const struct snd_kcontrol_new card_mixer_controls[] = {
  1021. SOC_ENUM_EXT("PM_QOS Vote", qos_vote,
  1022. msm_qos_ctl_get, msm_qos_ctl_put),
  1023. SOC_SINGLE_EXT("LPI PCM Logging Enable", 0, 0, 1, 0,
  1024. msm_lpi_logging_enable_get, msm_lpi_logging_enable_put),
  1025. SOC_ENUM_EXT("VOTE Against Sleep", sleep_against,
  1026. msm_vote_against_sleep_ctl_get, msm_vote_against_sleep_ctl_put),
  1027. };
  1028. static int msm_register_pm_qos_latency_controls(struct snd_soc_pcm_runtime *rtd)
  1029. {
  1030. struct snd_soc_component *lpass_cdc_component = NULL;
  1031. int ret = 0;
  1032. lpass_cdc_component = snd_soc_rtdcom_lookup(rtd, "lpass-cdc");
  1033. if (!lpass_cdc_component) {
  1034. pr_err("%s: could not find component for lpass-cdc\n",
  1035. __func__);
  1036. return -EINVAL;
  1037. }
  1038. ret = snd_soc_add_component_controls(lpass_cdc_component,
  1039. card_mixer_controls, ARRAY_SIZE(card_mixer_controls));
  1040. if (ret < 0) {
  1041. pr_err("%s: add common snd controls failed: %d\n",
  1042. __func__, ret);
  1043. return -EINVAL;
  1044. }
  1045. return 0;
  1046. }
  1047. int msm_common_dai_link_init(struct snd_soc_pcm_runtime *rtd)
  1048. {
  1049. struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
  1050. struct snd_soc_component *component = NULL;
  1051. struct snd_soc_dai_link *dai_link = rtd->dai_link;
  1052. struct device *dev = rtd->card->dev;
  1053. int ret = 0;
  1054. int index = 0;
  1055. const char *mixer_ctl_name = CODEC_CHMAP;
  1056. char *mixer_str = NULL;
  1057. char *backend_name = NULL;
  1058. uint32_t ctl_len = 0;
  1059. struct chmap_pdata *pdata;
  1060. struct snd_kcontrol *kctl;
  1061. struct snd_kcontrol_new msm_common_channel_map[1] = {
  1062. {
  1063. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1064. .name = "?",
  1065. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  1066. .info = msm_channel_map_info,
  1067. .get = msm_channel_map_get,
  1068. .private_value = 0,
  1069. }
  1070. };
  1071. if (!codec_dai) {
  1072. pr_err("%s: failed to get codec dai", __func__);
  1073. return -EINVAL;
  1074. }
  1075. component = codec_dai->component;
  1076. msm_common_get_backend_name(dai_link->stream_name, &backend_name);
  1077. if (!backend_name) {
  1078. pr_err("%s: failed to get backend name", __func__);
  1079. return -EINVAL;
  1080. }
  1081. pdata = devm_kzalloc(dev, sizeof(struct chmap_pdata), GFP_KERNEL);
  1082. if (!pdata) {
  1083. ret = -ENOMEM;
  1084. goto free_backend;
  1085. }
  1086. if ((!strncmp(backend_name, "SLIM", strlen("SLIM"))) ||
  1087. (!strncmp(backend_name, "CODEC_DMA", strlen("CODEC_DMA")))) {
  1088. ctl_len = strlen(dai_link->stream_name) + 1 +
  1089. strlen(mixer_ctl_name) + 1;
  1090. mixer_str = kzalloc(ctl_len, GFP_KERNEL);
  1091. if (!mixer_str) {
  1092. ret = -ENOMEM;
  1093. goto free_backend;
  1094. }
  1095. snprintf(mixer_str, ctl_len, "%s %s", dai_link->stream_name,
  1096. mixer_ctl_name);
  1097. msm_common_channel_map[0].name = mixer_str;
  1098. msm_common_channel_map[0].private_value = 0;
  1099. pr_debug("Registering new mixer ctl %s\n", mixer_str);
  1100. ret = snd_soc_add_component_controls(component,
  1101. msm_common_channel_map,
  1102. ARRAY_SIZE(msm_common_channel_map));
  1103. kctl = snd_soc_card_get_kcontrol(rtd->card, mixer_str);
  1104. if (!kctl) {
  1105. pr_err("failed to get kctl %s\n", mixer_str);
  1106. ret = -EINVAL;
  1107. goto free_mixer_str;
  1108. }
  1109. pdata->dai[0] = codec_dai;
  1110. pdata->num_codec_dai = 1;
  1111. if (!strncmp(backend_name, "SLIM", strlen("SLIM"))) {
  1112. pdata->id = SLIM;
  1113. } else {
  1114. pdata->id = CODEC_DMA;
  1115. if (rtd->dai_link->num_codecs <= MAX_CODEC_DAI) {
  1116. pdata->num_codec_dai = rtd->dai_link->num_codecs;
  1117. for_each_rtd_codec_dais(rtd, index, codec_dai) {
  1118. pdata->dai[index] = codec_dai;
  1119. }
  1120. }
  1121. }
  1122. kctl->private_data = pdata;
  1123. }
  1124. if (!kregister_pm_qos_latency_controls) {
  1125. if (!msm_register_pm_qos_latency_controls(rtd))
  1126. kregister_pm_qos_latency_controls = true;
  1127. }
  1128. free_mixer_str:
  1129. if (mixer_str) {
  1130. kfree(mixer_str);
  1131. mixer_str = NULL;
  1132. }
  1133. free_backend:
  1134. if (backend_name) {
  1135. kfree(backend_name);
  1136. backend_name = NULL;
  1137. }
  1138. return ret;
  1139. }