msm_common.c 36 KB

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