msm_common.c 34 KB

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