msm_common.c 35 KB

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