msm_common.c 29 KB

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