msm_common.c 35 KB

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