msm_common.c 34 KB

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