msm-pcm-q6-v2.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904
  1. /* Copyright (c) 2012-2017, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #include <linux/init.h>
  13. #include <linux/err.h>
  14. #include <linux/module.h>
  15. #include <linux/moduleparam.h>
  16. #include <linux/time.h>
  17. #include <linux/wait.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/slab.h>
  20. #include <sound/core.h>
  21. #include <sound/soc.h>
  22. #include <sound/soc-dapm.h>
  23. #include <sound/pcm.h>
  24. #include <sound/initval.h>
  25. #include <sound/control.h>
  26. #include <sound/timer.h>
  27. #include <asm/dma.h>
  28. #include <linux/dma-mapping.h>
  29. #include <linux/msm_audio.h>
  30. #include <linux/of_device.h>
  31. #include <sound/tlv.h>
  32. #include <sound/pcm_params.h>
  33. #include <dsp/msm_audio_ion.h>
  34. #include <dsp/q6audio-v2.h>
  35. #include "msm-pcm-q6-v2.h"
  36. #include "msm-pcm-routing-v2.h"
  37. #include "msm-qti-pp-config.h"
  38. enum stream_state {
  39. IDLE = 0,
  40. STOPPED,
  41. RUNNING,
  42. };
  43. static struct audio_locks the_locks;
  44. #define PCM_MASTER_VOL_MAX_STEPS 0x2000
  45. static const DECLARE_TLV_DB_LINEAR(msm_pcm_vol_gain, 0,
  46. PCM_MASTER_VOL_MAX_STEPS);
  47. struct snd_msm {
  48. struct snd_card *card;
  49. struct snd_pcm *pcm;
  50. };
  51. #define CMD_EOS_MIN_TIMEOUT_LENGTH 50
  52. #define CMD_EOS_TIMEOUT_MULTIPLIER (HZ * 50)
  53. #define MAX_PB_COPY_RETRIES 3
  54. static struct snd_pcm_hardware msm_pcm_hardware_capture = {
  55. .info = (SNDRV_PCM_INFO_MMAP |
  56. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  57. SNDRV_PCM_INFO_MMAP_VALID |
  58. SNDRV_PCM_INFO_INTERLEAVED |
  59. SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME),
  60. .formats = (SNDRV_PCM_FMTBIT_S16_LE |
  61. SNDRV_PCM_FMTBIT_S24_LE |
  62. SNDRV_PCM_FMTBIT_S24_3LE |
  63. SNDRV_PCM_FMTBIT_S32_LE),
  64. .rates = SNDRV_PCM_RATE_8000_384000,
  65. .rate_min = 8000,
  66. .rate_max = 384000,
  67. .channels_min = 1,
  68. .channels_max = 4,
  69. .buffer_bytes_max = CAPTURE_MAX_NUM_PERIODS *
  70. CAPTURE_MAX_PERIOD_SIZE,
  71. .period_bytes_min = CAPTURE_MIN_PERIOD_SIZE,
  72. .period_bytes_max = CAPTURE_MAX_PERIOD_SIZE,
  73. .periods_min = CAPTURE_MIN_NUM_PERIODS,
  74. .periods_max = CAPTURE_MAX_NUM_PERIODS,
  75. .fifo_size = 0,
  76. };
  77. static struct snd_pcm_hardware msm_pcm_hardware_playback = {
  78. .info = (SNDRV_PCM_INFO_MMAP |
  79. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  80. SNDRV_PCM_INFO_MMAP_VALID |
  81. SNDRV_PCM_INFO_INTERLEAVED |
  82. SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME),
  83. .formats = (SNDRV_PCM_FMTBIT_S16_LE |
  84. SNDRV_PCM_FMTBIT_S24_LE |
  85. SNDRV_PCM_FMTBIT_S24_3LE |
  86. SNDRV_PCM_FMTBIT_S32_LE),
  87. .rates = SNDRV_PCM_RATE_8000_384000,
  88. .rate_min = 8000,
  89. .rate_max = 384000,
  90. .channels_min = 1,
  91. .channels_max = 8,
  92. .buffer_bytes_max = PLAYBACK_MAX_NUM_PERIODS *
  93. PLAYBACK_MAX_PERIOD_SIZE,
  94. .period_bytes_min = PLAYBACK_MIN_PERIOD_SIZE,
  95. .period_bytes_max = PLAYBACK_MAX_PERIOD_SIZE,
  96. .periods_min = PLAYBACK_MIN_NUM_PERIODS,
  97. .periods_max = PLAYBACK_MAX_NUM_PERIODS,
  98. .fifo_size = 0,
  99. };
  100. /* Conventional and unconventional sample rate supported */
  101. static unsigned int supported_sample_rates[] = {
  102. 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000,
  103. 88200, 96000, 176400, 192000, 352800, 384000
  104. };
  105. static struct snd_pcm_hw_constraint_list constraints_sample_rates = {
  106. .count = ARRAY_SIZE(supported_sample_rates),
  107. .list = supported_sample_rates,
  108. .mask = 0,
  109. };
  110. static void msm_pcm_route_event_handler(enum msm_pcm_routing_event event,
  111. void *priv_data)
  112. {
  113. struct msm_audio *prtd = priv_data;
  114. WARN_ON(!prtd);
  115. pr_debug("%s: event %x\n", __func__, event);
  116. switch (event) {
  117. case MSM_PCM_RT_EVT_BUF_RECFG:
  118. q6asm_cmd(prtd->audio_client, CMD_PAUSE);
  119. q6asm_cmd(prtd->audio_client, CMD_FLUSH);
  120. q6asm_run(prtd->audio_client, 0, 0, 0);
  121. /* fallthrough */
  122. default:
  123. break;
  124. }
  125. }
  126. static void event_handler(uint32_t opcode,
  127. uint32_t token, uint32_t *payload, void *priv)
  128. {
  129. struct msm_audio *prtd = priv;
  130. struct snd_pcm_substream *substream = prtd->substream;
  131. uint32_t *ptrmem = (uint32_t *)payload;
  132. uint32_t idx = 0;
  133. uint32_t size = 0;
  134. uint8_t buf_index;
  135. struct snd_soc_pcm_runtime *rtd;
  136. int ret = 0;
  137. switch (opcode) {
  138. case ASM_DATA_EVENT_WRITE_DONE_V2: {
  139. pr_debug("ASM_DATA_EVENT_WRITE_DONE_V2\n");
  140. pr_debug("Buffer Consumed = 0x%08x\n", *ptrmem);
  141. prtd->pcm_irq_pos += prtd->pcm_count;
  142. if (atomic_read(&prtd->start))
  143. snd_pcm_period_elapsed(substream);
  144. atomic_inc(&prtd->out_count);
  145. wake_up(&the_locks.write_wait);
  146. if (!atomic_read(&prtd->start))
  147. break;
  148. if (!prtd->mmap_flag || prtd->reset_event)
  149. break;
  150. if (q6asm_is_cpu_buf_avail_nolock(IN,
  151. prtd->audio_client,
  152. &size, &idx)) {
  153. pr_debug("%s:writing %d bytes of buffer to dsp 2\n",
  154. __func__, prtd->pcm_count);
  155. q6asm_write_nolock(prtd->audio_client,
  156. prtd->pcm_count, 0, 0, NO_TIMESTAMP);
  157. }
  158. break;
  159. }
  160. case ASM_DATA_EVENT_RENDERED_EOS:
  161. pr_debug("ASM_DATA_EVENT_RENDERED_EOS\n");
  162. clear_bit(CMD_EOS, &prtd->cmd_pending);
  163. wake_up(&the_locks.eos_wait);
  164. break;
  165. case ASM_DATA_EVENT_READ_DONE_V2: {
  166. pr_debug("ASM_DATA_EVENT_READ_DONE_V2\n");
  167. buf_index = q6asm_get_buf_index_from_token(token);
  168. pr_debug("%s: token=0x%08x buf_index=0x%08x\n",
  169. __func__, token, buf_index);
  170. prtd->in_frame_info[buf_index].size = payload[4];
  171. prtd->in_frame_info[buf_index].offset = payload[5];
  172. /* assume data size = 0 during flushing */
  173. if (prtd->in_frame_info[buf_index].size) {
  174. prtd->pcm_irq_pos +=
  175. prtd->in_frame_info[buf_index].size;
  176. pr_debug("pcm_irq_pos=%d\n", prtd->pcm_irq_pos);
  177. if (atomic_read(&prtd->start))
  178. snd_pcm_period_elapsed(substream);
  179. if (atomic_read(&prtd->in_count) <= prtd->periods)
  180. atomic_inc(&prtd->in_count);
  181. wake_up(&the_locks.read_wait);
  182. if (prtd->mmap_flag &&
  183. q6asm_is_cpu_buf_avail_nolock(OUT,
  184. prtd->audio_client,
  185. &size, &idx) &&
  186. (substream->runtime->status->state ==
  187. SNDRV_PCM_STATE_RUNNING))
  188. q6asm_read_nolock(prtd->audio_client);
  189. } else {
  190. pr_debug("%s: reclaim flushed buf in_count %x\n",
  191. __func__, atomic_read(&prtd->in_count));
  192. prtd->pcm_irq_pos += prtd->pcm_count;
  193. if (prtd->mmap_flag) {
  194. if (q6asm_is_cpu_buf_avail_nolock(OUT,
  195. prtd->audio_client,
  196. &size, &idx) &&
  197. (substream->runtime->status->state ==
  198. SNDRV_PCM_STATE_RUNNING))
  199. q6asm_read_nolock(prtd->audio_client);
  200. } else {
  201. atomic_inc(&prtd->in_count);
  202. }
  203. if (atomic_read(&prtd->in_count) == prtd->periods) {
  204. pr_info("%s: reclaimed all bufs\n", __func__);
  205. if (atomic_read(&prtd->start))
  206. snd_pcm_period_elapsed(substream);
  207. wake_up(&the_locks.read_wait);
  208. }
  209. }
  210. break;
  211. }
  212. case ASM_STREAM_PP_EVENT:
  213. case ASM_STREAM_CMD_ENCDEC_EVENTS: {
  214. pr_debug("%s: ASM_STREAM_EVENT (0x%x)\n", __func__, opcode);
  215. if (!substream) {
  216. pr_err("%s: substream is NULL.\n", __func__);
  217. return;
  218. }
  219. rtd = substream->private_data;
  220. if (!rtd) {
  221. pr_err("%s: rtd is NULL\n", __func__);
  222. return;
  223. }
  224. ret = msm_adsp_inform_mixer_ctl(rtd, payload);
  225. if (ret) {
  226. pr_err("%s: failed to inform mixer ctl. err = %d\n",
  227. __func__, ret);
  228. return;
  229. }
  230. break;
  231. }
  232. case APR_BASIC_RSP_RESULT: {
  233. switch (payload[0]) {
  234. case ASM_SESSION_CMD_RUN_V2:
  235. if (substream->stream
  236. != SNDRV_PCM_STREAM_PLAYBACK) {
  237. atomic_set(&prtd->start, 1);
  238. break;
  239. }
  240. if (prtd->mmap_flag) {
  241. pr_debug("%s:writing %d bytes of buffer to dsp\n",
  242. __func__,
  243. prtd->pcm_count);
  244. q6asm_write_nolock(prtd->audio_client,
  245. prtd->pcm_count,
  246. 0, 0, NO_TIMESTAMP);
  247. } else {
  248. while (atomic_read(&prtd->out_needed)) {
  249. pr_debug("%s:writing %d bytes of buffer to dsp\n",
  250. __func__,
  251. prtd->pcm_count);
  252. q6asm_write_nolock(prtd->audio_client,
  253. prtd->pcm_count,
  254. 0, 0, NO_TIMESTAMP);
  255. atomic_dec(&prtd->out_needed);
  256. wake_up(&the_locks.write_wait);
  257. };
  258. }
  259. atomic_set(&prtd->start, 1);
  260. break;
  261. case ASM_STREAM_CMD_REGISTER_PP_EVENTS:
  262. pr_debug("%s: ASM_STREAM_CMD_REGISTER_PP_EVENTS:",
  263. __func__);
  264. break;
  265. default:
  266. pr_debug("%s:Payload = [0x%x]stat[0x%x]\n",
  267. __func__, payload[0], payload[1]);
  268. break;
  269. }
  270. }
  271. break;
  272. case RESET_EVENTS:
  273. pr_debug("%s RESET_EVENTS\n", __func__);
  274. prtd->pcm_irq_pos += prtd->pcm_count;
  275. atomic_inc(&prtd->out_count);
  276. atomic_inc(&prtd->in_count);
  277. prtd->reset_event = true;
  278. if (atomic_read(&prtd->start))
  279. snd_pcm_period_elapsed(substream);
  280. wake_up(&the_locks.eos_wait);
  281. wake_up(&the_locks.write_wait);
  282. wake_up(&the_locks.read_wait);
  283. break;
  284. default:
  285. pr_debug("Not Supported Event opcode[0x%x]\n", opcode);
  286. break;
  287. }
  288. }
  289. static int msm_pcm_playback_prepare(struct snd_pcm_substream *substream)
  290. {
  291. struct snd_pcm_runtime *runtime = substream->runtime;
  292. struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
  293. struct msm_audio *prtd = runtime->private_data;
  294. struct msm_plat_data *pdata;
  295. struct snd_pcm_hw_params *params;
  296. int ret;
  297. uint32_t fmt_type = FORMAT_LINEAR_PCM;
  298. uint16_t bits_per_sample;
  299. uint16_t sample_word_size;
  300. pdata = (struct msm_plat_data *)
  301. dev_get_drvdata(soc_prtd->platform->dev);
  302. if (!pdata) {
  303. pr_err("%s: platform data not populated\n", __func__);
  304. return -EINVAL;
  305. }
  306. if (!prtd || !prtd->audio_client) {
  307. pr_err("%s: private data null or audio client freed\n",
  308. __func__);
  309. return -EINVAL;
  310. }
  311. params = &soc_prtd->dpcm[substream->stream].hw_params;
  312. pr_debug("%s\n", __func__);
  313. prtd->pcm_size = snd_pcm_lib_buffer_bytes(substream);
  314. prtd->pcm_count = snd_pcm_lib_period_bytes(substream);
  315. prtd->pcm_irq_pos = 0;
  316. /* rate and channels are sent to audio driver */
  317. prtd->samp_rate = runtime->rate;
  318. prtd->channel_mode = runtime->channels;
  319. if (prtd->enabled)
  320. return 0;
  321. prtd->audio_client->perf_mode = pdata->perf_mode;
  322. pr_debug("%s: perf: %x\n", __func__, pdata->perf_mode);
  323. switch (params_format(params)) {
  324. case SNDRV_PCM_FORMAT_S32_LE:
  325. bits_per_sample = 32;
  326. sample_word_size = 32;
  327. break;
  328. case SNDRV_PCM_FORMAT_S24_LE:
  329. bits_per_sample = 24;
  330. sample_word_size = 32;
  331. break;
  332. case SNDRV_PCM_FORMAT_S24_3LE:
  333. bits_per_sample = 24;
  334. sample_word_size = 24;
  335. break;
  336. case SNDRV_PCM_FORMAT_S16_LE:
  337. default:
  338. bits_per_sample = 16;
  339. sample_word_size = 16;
  340. break;
  341. }
  342. if (prtd->compress_enable) {
  343. fmt_type = FORMAT_GEN_COMPR;
  344. pr_debug("%s: Compressed enabled!\n", __func__);
  345. ret = q6asm_open_write_compressed(prtd->audio_client, fmt_type,
  346. COMPRESSED_PASSTHROUGH_GEN);
  347. if (ret < 0) {
  348. pr_err("%s: q6asm_open_write_compressed failed (%d)\n",
  349. __func__, ret);
  350. q6asm_audio_client_free(prtd->audio_client);
  351. prtd->audio_client = NULL;
  352. return -ENOMEM;
  353. }
  354. } else {
  355. ret = q6asm_open_write_v4(prtd->audio_client,
  356. fmt_type, bits_per_sample);
  357. if (ret < 0) {
  358. pr_err("%s: q6asm_open_write_v4 failed (%d)\n",
  359. __func__, ret);
  360. q6asm_audio_client_free(prtd->audio_client);
  361. prtd->audio_client = NULL;
  362. return -ENOMEM;
  363. }
  364. ret = q6asm_send_cal(prtd->audio_client);
  365. if (ret < 0)
  366. pr_debug("%s : Send cal failed : %d", __func__, ret);
  367. }
  368. pr_debug("%s: session ID %d\n", __func__,
  369. prtd->audio_client->session);
  370. prtd->session_id = prtd->audio_client->session;
  371. if (prtd->compress_enable) {
  372. ret = msm_pcm_routing_reg_phy_compr_stream(
  373. soc_prtd->dai_link->id,
  374. prtd->audio_client->perf_mode,
  375. prtd->session_id,
  376. SNDRV_PCM_STREAM_PLAYBACK,
  377. COMPRESSED_PASSTHROUGH_GEN);
  378. } else {
  379. ret = msm_pcm_routing_reg_phy_stream(soc_prtd->dai_link->id,
  380. prtd->audio_client->perf_mode,
  381. prtd->session_id, substream->stream);
  382. }
  383. if (ret) {
  384. pr_err("%s: stream reg failed ret:%d\n", __func__, ret);
  385. return ret;
  386. }
  387. if (prtd->compress_enable) {
  388. ret = q6asm_media_format_block_gen_compr(
  389. prtd->audio_client, runtime->rate,
  390. runtime->channels, !prtd->set_channel_map,
  391. prtd->channel_map, bits_per_sample);
  392. } else {
  393. ret = q6asm_media_format_block_multi_ch_pcm_v4(
  394. prtd->audio_client, runtime->rate,
  395. runtime->channels, !prtd->set_channel_map,
  396. prtd->channel_map, bits_per_sample,
  397. sample_word_size, ASM_LITTLE_ENDIAN,
  398. DEFAULT_QF);
  399. }
  400. if (ret < 0)
  401. pr_info("%s: CMD Format block failed\n", __func__);
  402. atomic_set(&prtd->out_count, runtime->periods);
  403. prtd->enabled = 1;
  404. prtd->cmd_pending = 0;
  405. prtd->cmd_interrupt = 0;
  406. return 0;
  407. }
  408. static int msm_pcm_capture_prepare(struct snd_pcm_substream *substream)
  409. {
  410. struct snd_pcm_runtime *runtime = substream->runtime;
  411. struct msm_audio *prtd = runtime->private_data;
  412. struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
  413. struct msm_plat_data *pdata;
  414. struct snd_pcm_hw_params *params;
  415. struct msm_pcm_routing_evt event;
  416. int ret = 0;
  417. int i = 0;
  418. uint16_t bits_per_sample = 16;
  419. uint16_t sample_word_size;
  420. pdata = (struct msm_plat_data *)
  421. dev_get_drvdata(soc_prtd->platform->dev);
  422. if (!pdata) {
  423. pr_err("%s: platform data not populated\n", __func__);
  424. return -EINVAL;
  425. }
  426. if (!prtd || !prtd->audio_client) {
  427. pr_err("%s: private data null or audio client freed\n",
  428. __func__);
  429. return -EINVAL;
  430. }
  431. if (prtd->enabled == IDLE) {
  432. pr_debug("%s:perf_mode=%d periods=%d\n", __func__,
  433. pdata->perf_mode, runtime->periods);
  434. params = &soc_prtd->dpcm[substream->stream].hw_params;
  435. if ((params_format(params) == SNDRV_PCM_FORMAT_S24_LE) ||
  436. (params_format(params) == SNDRV_PCM_FORMAT_S24_3LE))
  437. bits_per_sample = 24;
  438. else if (params_format(params) == SNDRV_PCM_FORMAT_S32_LE)
  439. bits_per_sample = 32;
  440. /* ULL mode is not supported in capture path */
  441. if (pdata->perf_mode == LEGACY_PCM_MODE)
  442. prtd->audio_client->perf_mode = LEGACY_PCM_MODE;
  443. else
  444. prtd->audio_client->perf_mode = LOW_LATENCY_PCM_MODE;
  445. pr_debug("%s Opening %d-ch PCM read stream, perf_mode %d\n",
  446. __func__, params_channels(params),
  447. prtd->audio_client->perf_mode);
  448. ret = q6asm_open_read_v4(prtd->audio_client, FORMAT_LINEAR_PCM,
  449. bits_per_sample, false);
  450. if (ret < 0) {
  451. pr_err("%s: q6asm_open_read failed\n", __func__);
  452. q6asm_audio_client_free(prtd->audio_client);
  453. prtd->audio_client = NULL;
  454. return -ENOMEM;
  455. }
  456. ret = q6asm_send_cal(prtd->audio_client);
  457. if (ret < 0)
  458. pr_debug("%s : Send cal failed : %d", __func__, ret);
  459. pr_debug("%s: session ID %d\n",
  460. __func__, prtd->audio_client->session);
  461. prtd->session_id = prtd->audio_client->session;
  462. event.event_func = msm_pcm_route_event_handler;
  463. event.priv_data = (void *) prtd;
  464. ret = msm_pcm_routing_reg_phy_stream_v2(
  465. soc_prtd->dai_link->id,
  466. prtd->audio_client->perf_mode,
  467. prtd->session_id, substream->stream,
  468. event);
  469. if (ret) {
  470. pr_err("%s: stream reg failed ret:%d\n", __func__, ret);
  471. return ret;
  472. }
  473. }
  474. prtd->pcm_size = snd_pcm_lib_buffer_bytes(substream);
  475. prtd->pcm_count = snd_pcm_lib_period_bytes(substream);
  476. prtd->pcm_irq_pos = 0;
  477. /* rate and channels are sent to audio driver */
  478. prtd->samp_rate = runtime->rate;
  479. prtd->channel_mode = runtime->channels;
  480. if (prtd->enabled == IDLE || prtd->enabled == STOPPED) {
  481. for (i = 0; i < runtime->periods; i++)
  482. q6asm_read(prtd->audio_client);
  483. prtd->periods = runtime->periods;
  484. }
  485. if (prtd->enabled != IDLE)
  486. return 0;
  487. switch (runtime->format) {
  488. case SNDRV_PCM_FORMAT_S32_LE:
  489. bits_per_sample = 32;
  490. sample_word_size = 32;
  491. break;
  492. case SNDRV_PCM_FORMAT_S24_LE:
  493. bits_per_sample = 24;
  494. sample_word_size = 32;
  495. break;
  496. case SNDRV_PCM_FORMAT_S24_3LE:
  497. bits_per_sample = 24;
  498. sample_word_size = 24;
  499. break;
  500. case SNDRV_PCM_FORMAT_S16_LE:
  501. default:
  502. bits_per_sample = 16;
  503. sample_word_size = 16;
  504. break;
  505. }
  506. pr_debug("%s: Samp_rate = %d Channel = %d bit width = %d, word size = %d\n",
  507. __func__, prtd->samp_rate, prtd->channel_mode,
  508. bits_per_sample, sample_word_size);
  509. ret = q6asm_enc_cfg_blk_pcm_format_support_v4(prtd->audio_client,
  510. prtd->samp_rate,
  511. prtd->channel_mode,
  512. bits_per_sample,
  513. sample_word_size,
  514. ASM_LITTLE_ENDIAN,
  515. DEFAULT_QF);
  516. if (ret < 0)
  517. pr_debug("%s: cmd cfg pcm was block failed", __func__);
  518. prtd->enabled = RUNNING;
  519. return ret;
  520. }
  521. static int msm_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  522. {
  523. int ret = 0;
  524. struct snd_pcm_runtime *runtime = substream->runtime;
  525. struct msm_audio *prtd = runtime->private_data;
  526. switch (cmd) {
  527. case SNDRV_PCM_TRIGGER_START:
  528. case SNDRV_PCM_TRIGGER_RESUME:
  529. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  530. pr_debug("%s: Trigger start\n", __func__);
  531. ret = q6asm_run_nowait(prtd->audio_client, 0, 0, 0);
  532. break;
  533. case SNDRV_PCM_TRIGGER_STOP:
  534. pr_debug("SNDRV_PCM_TRIGGER_STOP\n");
  535. atomic_set(&prtd->start, 0);
  536. if (substream->stream != SNDRV_PCM_STREAM_PLAYBACK) {
  537. prtd->enabled = STOPPED;
  538. ret = q6asm_cmd_nowait(prtd->audio_client, CMD_PAUSE);
  539. break;
  540. }
  541. /* pending CMD_EOS isn't expected */
  542. WARN_ON_ONCE(test_bit(CMD_EOS, &prtd->cmd_pending));
  543. set_bit(CMD_EOS, &prtd->cmd_pending);
  544. ret = q6asm_cmd_nowait(prtd->audio_client, CMD_EOS);
  545. if (ret)
  546. clear_bit(CMD_EOS, &prtd->cmd_pending);
  547. break;
  548. case SNDRV_PCM_TRIGGER_SUSPEND:
  549. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  550. pr_debug("SNDRV_PCM_TRIGGER_PAUSE\n");
  551. ret = q6asm_cmd_nowait(prtd->audio_client, CMD_PAUSE);
  552. atomic_set(&prtd->start, 0);
  553. break;
  554. default:
  555. ret = -EINVAL;
  556. break;
  557. }
  558. return ret;
  559. }
  560. static int msm_pcm_open(struct snd_pcm_substream *substream)
  561. {
  562. struct snd_pcm_runtime *runtime = substream->runtime;
  563. struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
  564. struct msm_audio *prtd;
  565. struct msm_plat_data *pdata;
  566. int ret = 0;
  567. pdata = (struct msm_plat_data *)
  568. dev_get_drvdata(soc_prtd->platform->dev);
  569. if (!pdata) {
  570. pr_err("%s: platform data not populated\n", __func__);
  571. return -EINVAL;
  572. }
  573. prtd = kzalloc(sizeof(struct msm_audio), GFP_KERNEL);
  574. if (prtd == NULL)
  575. return -ENOMEM;
  576. prtd->substream = substream;
  577. prtd->audio_client = q6asm_audio_client_alloc(
  578. (app_cb)event_handler, prtd);
  579. if (!prtd->audio_client) {
  580. pr_info("%s: Could not allocate memory\n", __func__);
  581. kfree(prtd);
  582. return -ENOMEM;
  583. }
  584. prtd->audio_client->dev = soc_prtd->platform->dev;
  585. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  586. runtime->hw = msm_pcm_hardware_playback;
  587. /* Capture path */
  588. else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  589. runtime->hw = msm_pcm_hardware_capture;
  590. else {
  591. pr_err("Invalid Stream type %d\n", substream->stream);
  592. return -EINVAL;
  593. }
  594. ret = snd_pcm_hw_constraint_list(runtime, 0,
  595. SNDRV_PCM_HW_PARAM_RATE,
  596. &constraints_sample_rates);
  597. if (ret < 0)
  598. pr_info("snd_pcm_hw_constraint_list failed\n");
  599. /* Ensure that buffer size is a multiple of period size */
  600. ret = snd_pcm_hw_constraint_integer(runtime,
  601. SNDRV_PCM_HW_PARAM_PERIODS);
  602. if (ret < 0)
  603. pr_info("snd_pcm_hw_constraint_integer failed\n");
  604. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  605. ret = snd_pcm_hw_constraint_minmax(runtime,
  606. SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
  607. PLAYBACK_MIN_NUM_PERIODS * PLAYBACK_MIN_PERIOD_SIZE,
  608. PLAYBACK_MAX_NUM_PERIODS * PLAYBACK_MAX_PERIOD_SIZE);
  609. if (ret < 0) {
  610. pr_err("constraint for buffer bytes min max ret = %d\n",
  611. ret);
  612. }
  613. }
  614. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
  615. ret = snd_pcm_hw_constraint_minmax(runtime,
  616. SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
  617. CAPTURE_MIN_NUM_PERIODS * CAPTURE_MIN_PERIOD_SIZE,
  618. CAPTURE_MAX_NUM_PERIODS * CAPTURE_MAX_PERIOD_SIZE);
  619. if (ret < 0) {
  620. pr_err("constraint for buffer bytes min max ret = %d\n",
  621. ret);
  622. }
  623. }
  624. ret = snd_pcm_hw_constraint_step(runtime, 0,
  625. SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 32);
  626. if (ret < 0) {
  627. pr_err("constraint for period bytes step ret = %d\n",
  628. ret);
  629. }
  630. ret = snd_pcm_hw_constraint_step(runtime, 0,
  631. SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 32);
  632. if (ret < 0) {
  633. pr_err("constraint for buffer bytes step ret = %d\n",
  634. ret);
  635. }
  636. prtd->enabled = IDLE;
  637. prtd->dsp_cnt = 0;
  638. prtd->set_channel_map = false;
  639. prtd->reset_event = false;
  640. runtime->private_data = prtd;
  641. msm_adsp_init_mixer_ctl_pp_event_queue(soc_prtd);
  642. /* Vote to update the Rx thread priority to RT Thread for playback */
  643. if ((substream->stream == SNDRV_PCM_STREAM_PLAYBACK) &&
  644. (pdata->perf_mode == LOW_LATENCY_PCM_MODE))
  645. apr_start_rx_rt(prtd->audio_client->apr);
  646. return 0;
  647. }
  648. static int msm_pcm_playback_copy(struct snd_pcm_substream *substream, int a,
  649. snd_pcm_uframes_t hwoff, void __user *buf, snd_pcm_uframes_t frames)
  650. {
  651. int ret = 0;
  652. int fbytes = 0;
  653. int xfer = 0;
  654. char *bufptr = NULL;
  655. void *data = NULL;
  656. uint32_t idx = 0;
  657. uint32_t size = 0;
  658. uint32_t retries = 0;
  659. struct snd_pcm_runtime *runtime = substream->runtime;
  660. struct msm_audio *prtd = runtime->private_data;
  661. fbytes = frames_to_bytes(runtime, frames);
  662. pr_debug("%s: prtd->out_count = %d\n",
  663. __func__, atomic_read(&prtd->out_count));
  664. while ((fbytes > 0) && (retries < MAX_PB_COPY_RETRIES)) {
  665. if (prtd->reset_event) {
  666. pr_err("%s: In SSR return ENETRESET before wait\n",
  667. __func__);
  668. return -ENETRESET;
  669. }
  670. ret = wait_event_timeout(the_locks.write_wait,
  671. (atomic_read(&prtd->out_count)), 5 * HZ);
  672. if (!ret) {
  673. pr_err("%s: wait_event_timeout failed\n", __func__);
  674. ret = -ETIMEDOUT;
  675. goto fail;
  676. }
  677. ret = 0;
  678. if (prtd->reset_event) {
  679. pr_err("%s: In SSR return ENETRESET after wait\n",
  680. __func__);
  681. return -ENETRESET;
  682. }
  683. if (!atomic_read(&prtd->out_count)) {
  684. pr_err("%s: pcm stopped out_count 0\n", __func__);
  685. return 0;
  686. }
  687. data = q6asm_is_cpu_buf_avail(IN, prtd->audio_client, &size,
  688. &idx);
  689. if (data == NULL) {
  690. retries++;
  691. continue;
  692. } else {
  693. retries = 0;
  694. }
  695. if (fbytes > size)
  696. xfer = size;
  697. else
  698. xfer = fbytes;
  699. bufptr = data;
  700. if (bufptr) {
  701. pr_debug("%s:fbytes =%d: xfer=%d size=%d\n",
  702. __func__, fbytes, xfer, size);
  703. if (copy_from_user(bufptr, buf, xfer)) {
  704. ret = -EFAULT;
  705. pr_err("%s: copy_from_user failed\n",
  706. __func__);
  707. q6asm_cpu_buf_release(IN, prtd->audio_client);
  708. goto fail;
  709. }
  710. buf += xfer;
  711. fbytes -= xfer;
  712. pr_debug("%s:fbytes = %d: xfer=%d\n", __func__, fbytes,
  713. xfer);
  714. if (atomic_read(&prtd->start)) {
  715. pr_debug("%s:writing %d bytes of buffer to dsp\n",
  716. __func__, xfer);
  717. ret = q6asm_write(prtd->audio_client, xfer,
  718. 0, 0, NO_TIMESTAMP);
  719. if (ret < 0) {
  720. ret = -EFAULT;
  721. q6asm_cpu_buf_release(IN,
  722. prtd->audio_client);
  723. goto fail;
  724. }
  725. } else
  726. atomic_inc(&prtd->out_needed);
  727. atomic_dec(&prtd->out_count);
  728. }
  729. }
  730. fail:
  731. if (retries >= MAX_PB_COPY_RETRIES)
  732. ret = -ENOMEM;
  733. return ret;
  734. }
  735. static int msm_pcm_playback_close(struct snd_pcm_substream *substream)
  736. {
  737. struct snd_pcm_runtime *runtime = substream->runtime;
  738. struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
  739. struct msm_audio *prtd = runtime->private_data;
  740. struct msm_plat_data *pdata;
  741. uint32_t timeout;
  742. int dir = 0;
  743. int ret = 0;
  744. pr_debug("%s: cmd_pending 0x%lx\n", __func__, prtd->cmd_pending);
  745. if (prtd->audio_client) {
  746. dir = IN;
  747. /*
  748. * Unvote to downgrade the Rx thread priority from
  749. * RT Thread for Low-Latency use case.
  750. */
  751. pdata = (struct msm_plat_data *)
  752. dev_get_drvdata(soc_prtd->platform->dev);
  753. if (pdata) {
  754. if (pdata->perf_mode == LOW_LATENCY_PCM_MODE)
  755. apr_end_rx_rt(prtd->audio_client->apr);
  756. }
  757. /* determine timeout length */
  758. if (runtime->frame_bits == 0 || runtime->rate == 0) {
  759. timeout = CMD_EOS_MIN_TIMEOUT_LENGTH;
  760. } else {
  761. timeout = (runtime->period_size *
  762. CMD_EOS_TIMEOUT_MULTIPLIER) /
  763. ((runtime->frame_bits / 8) *
  764. runtime->rate);
  765. if (timeout < CMD_EOS_MIN_TIMEOUT_LENGTH)
  766. timeout = CMD_EOS_MIN_TIMEOUT_LENGTH;
  767. }
  768. pr_debug("%s: CMD_EOS timeout is %d\n", __func__, timeout);
  769. ret = wait_event_timeout(the_locks.eos_wait,
  770. !test_bit(CMD_EOS, &prtd->cmd_pending),
  771. timeout);
  772. if (!ret)
  773. pr_err("%s: CMD_EOS failed, cmd_pending 0x%lx\n",
  774. __func__, prtd->cmd_pending);
  775. q6asm_cmd(prtd->audio_client, CMD_CLOSE);
  776. q6asm_audio_client_buf_free_contiguous(dir,
  777. prtd->audio_client);
  778. q6asm_audio_client_free(prtd->audio_client);
  779. }
  780. msm_pcm_routing_dereg_phy_stream(soc_prtd->dai_link->id,
  781. SNDRV_PCM_STREAM_PLAYBACK);
  782. msm_adsp_clean_mixer_ctl_pp_event_queue(soc_prtd);
  783. kfree(prtd);
  784. runtime->private_data = NULL;
  785. return 0;
  786. }
  787. static int msm_pcm_capture_copy(struct snd_pcm_substream *substream,
  788. int channel, snd_pcm_uframes_t hwoff, void __user *buf,
  789. snd_pcm_uframes_t frames)
  790. {
  791. int ret = 0;
  792. int fbytes = 0;
  793. int xfer;
  794. char *bufptr;
  795. void *data = NULL;
  796. static uint32_t idx;
  797. static uint32_t size;
  798. uint32_t offset = 0;
  799. struct snd_pcm_runtime *runtime = substream->runtime;
  800. struct msm_audio *prtd = substream->runtime->private_data;
  801. pr_debug("%s\n", __func__);
  802. fbytes = frames_to_bytes(runtime, frames);
  803. pr_debug("appl_ptr %d\n", (int)runtime->control->appl_ptr);
  804. pr_debug("hw_ptr %d\n", (int)runtime->status->hw_ptr);
  805. pr_debug("avail_min %d\n", (int)runtime->control->avail_min);
  806. if (prtd->reset_event) {
  807. pr_err("%s: In SSR return ENETRESET before wait\n", __func__);
  808. return -ENETRESET;
  809. }
  810. ret = wait_event_timeout(the_locks.read_wait,
  811. (atomic_read(&prtd->in_count)), 5 * HZ);
  812. if (!ret) {
  813. pr_debug("%s: wait_event_timeout failed\n", __func__);
  814. goto fail;
  815. }
  816. if (prtd->reset_event) {
  817. pr_err("%s: In SSR return ENETRESET after wait\n", __func__);
  818. return -ENETRESET;
  819. }
  820. if (!atomic_read(&prtd->in_count)) {
  821. pr_debug("%s: pcm stopped in_count 0\n", __func__);
  822. return 0;
  823. }
  824. pr_debug("Checking if valid buffer is available...%pK\n",
  825. data);
  826. data = q6asm_is_cpu_buf_avail(OUT, prtd->audio_client, &size, &idx);
  827. bufptr = data;
  828. pr_debug("Size = %d\n", size);
  829. pr_debug("fbytes = %d\n", fbytes);
  830. pr_debug("idx = %d\n", idx);
  831. if (bufptr) {
  832. xfer = fbytes;
  833. if (xfer > size)
  834. xfer = size;
  835. offset = prtd->in_frame_info[idx].offset;
  836. pr_debug("Offset value = %d\n", offset);
  837. if (copy_to_user(buf, bufptr+offset, xfer)) {
  838. pr_err("Failed to copy buf to user\n");
  839. ret = -EFAULT;
  840. q6asm_cpu_buf_release(OUT, prtd->audio_client);
  841. goto fail;
  842. }
  843. fbytes -= xfer;
  844. size -= xfer;
  845. prtd->in_frame_info[idx].offset += xfer;
  846. pr_debug("%s:fbytes = %d: size=%d: xfer=%d\n",
  847. __func__, fbytes, size, xfer);
  848. pr_debug(" Sending next buffer to dsp\n");
  849. memset(&prtd->in_frame_info[idx], 0,
  850. sizeof(struct msm_audio_in_frame_info));
  851. atomic_dec(&prtd->in_count);
  852. ret = q6asm_read(prtd->audio_client);
  853. if (ret < 0) {
  854. pr_err("q6asm read failed\n");
  855. ret = -EFAULT;
  856. q6asm_cpu_buf_release(OUT, prtd->audio_client);
  857. goto fail;
  858. }
  859. } else
  860. pr_err("No valid buffer\n");
  861. pr_debug("Returning from capture_copy... %d\n", ret);
  862. fail:
  863. return ret;
  864. }
  865. static int msm_pcm_capture_close(struct snd_pcm_substream *substream)
  866. {
  867. struct snd_pcm_runtime *runtime = substream->runtime;
  868. struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
  869. struct msm_audio *prtd = runtime->private_data;
  870. int dir = OUT;
  871. pr_debug("%s\n", __func__);
  872. if (prtd->audio_client) {
  873. q6asm_cmd(prtd->audio_client, CMD_CLOSE);
  874. q6asm_audio_client_buf_free_contiguous(dir,
  875. prtd->audio_client);
  876. q6asm_audio_client_free(prtd->audio_client);
  877. }
  878. msm_pcm_routing_dereg_phy_stream(soc_prtd->dai_link->id,
  879. SNDRV_PCM_STREAM_CAPTURE);
  880. kfree(prtd);
  881. runtime->private_data = NULL;
  882. return 0;
  883. }
  884. static int msm_pcm_copy(struct snd_pcm_substream *substream, int a,
  885. snd_pcm_uframes_t hwoff, void __user *buf, snd_pcm_uframes_t frames)
  886. {
  887. int ret = 0;
  888. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  889. ret = msm_pcm_playback_copy(substream, a, hwoff, buf, frames);
  890. else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  891. ret = msm_pcm_capture_copy(substream, a, hwoff, buf, frames);
  892. return ret;
  893. }
  894. static int msm_pcm_close(struct snd_pcm_substream *substream)
  895. {
  896. int ret = 0;
  897. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  898. ret = msm_pcm_playback_close(substream);
  899. else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  900. ret = msm_pcm_capture_close(substream);
  901. return ret;
  902. }
  903. static int msm_pcm_prepare(struct snd_pcm_substream *substream)
  904. {
  905. int ret = 0;
  906. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  907. ret = msm_pcm_playback_prepare(substream);
  908. else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  909. ret = msm_pcm_capture_prepare(substream);
  910. return ret;
  911. }
  912. static snd_pcm_uframes_t msm_pcm_pointer(struct snd_pcm_substream *substream)
  913. {
  914. struct snd_pcm_runtime *runtime = substream->runtime;
  915. struct msm_audio *prtd = runtime->private_data;
  916. if (prtd->pcm_irq_pos >= prtd->pcm_size)
  917. prtd->pcm_irq_pos = 0;
  918. pr_debug("pcm_irq_pos = %d\n", prtd->pcm_irq_pos);
  919. return bytes_to_frames(runtime, (prtd->pcm_irq_pos));
  920. }
  921. static int msm_pcm_mmap(struct snd_pcm_substream *substream,
  922. struct vm_area_struct *vma)
  923. {
  924. struct snd_pcm_runtime *runtime = substream->runtime;
  925. struct msm_audio *prtd = runtime->private_data;
  926. struct audio_client *ac = prtd->audio_client;
  927. struct audio_port_data *apd = ac->port;
  928. struct audio_buffer *ab;
  929. int dir = -1;
  930. prtd->mmap_flag = 1;
  931. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  932. dir = IN;
  933. else
  934. dir = OUT;
  935. ab = &(apd[dir].buf[0]);
  936. return msm_audio_ion_mmap(ab, vma);
  937. }
  938. static int msm_pcm_hw_params(struct snd_pcm_substream *substream,
  939. struct snd_pcm_hw_params *params)
  940. {
  941. struct snd_pcm_runtime *runtime = substream->runtime;
  942. struct msm_audio *prtd = runtime->private_data;
  943. struct snd_dma_buffer *dma_buf = &substream->dma_buffer;
  944. struct audio_buffer *buf;
  945. int dir, ret;
  946. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  947. dir = IN;
  948. else
  949. dir = OUT;
  950. ret = q6asm_audio_client_buf_alloc_contiguous(dir,
  951. prtd->audio_client,
  952. (params_buffer_bytes(params) / params_periods(params)),
  953. params_periods(params));
  954. if (ret < 0) {
  955. pr_err("Audio Start: Buffer Allocation failed rc = %d\n",
  956. ret);
  957. return -ENOMEM;
  958. }
  959. buf = prtd->audio_client->port[dir].buf;
  960. if (buf == NULL || buf[0].data == NULL)
  961. return -ENOMEM;
  962. pr_debug("%s:buf = %pK\n", __func__, buf);
  963. dma_buf->dev.type = SNDRV_DMA_TYPE_DEV;
  964. dma_buf->dev.dev = substream->pcm->card->dev;
  965. dma_buf->private_data = NULL;
  966. dma_buf->area = buf[0].data;
  967. dma_buf->addr = buf[0].phys;
  968. dma_buf->bytes = params_buffer_bytes(params);
  969. if (!dma_buf->area)
  970. return -ENOMEM;
  971. snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
  972. return 0;
  973. }
  974. static const struct snd_pcm_ops msm_pcm_ops = {
  975. .open = msm_pcm_open,
  976. .copy = msm_pcm_copy,
  977. .hw_params = msm_pcm_hw_params,
  978. .close = msm_pcm_close,
  979. .ioctl = snd_pcm_lib_ioctl,
  980. .prepare = msm_pcm_prepare,
  981. .trigger = msm_pcm_trigger,
  982. .pointer = msm_pcm_pointer,
  983. .mmap = msm_pcm_mmap,
  984. };
  985. static int msm_pcm_adsp_stream_cmd_put(struct snd_kcontrol *kcontrol,
  986. struct snd_ctl_elem_value *ucontrol)
  987. {
  988. struct snd_soc_component *pcm = snd_kcontrol_chip(kcontrol);
  989. struct snd_soc_platform *platform = snd_soc_component_to_platform(pcm);
  990. struct msm_plat_data *pdata = dev_get_drvdata(platform->dev);
  991. struct snd_pcm_substream *substream;
  992. struct msm_audio *prtd;
  993. int ret = 0;
  994. struct msm_adsp_event_data *event_data = NULL;
  995. if (!pdata) {
  996. pr_err("%s pdata is NULL\n", __func__);
  997. ret = -ENODEV;
  998. goto done;
  999. }
  1000. substream = pdata->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
  1001. if (!substream) {
  1002. pr_err("%s substream not found\n", __func__);
  1003. ret = -EINVAL;
  1004. goto done;
  1005. }
  1006. if (!substream->runtime) {
  1007. pr_err("%s substream runtime not found\n", __func__);
  1008. ret = -EINVAL;
  1009. goto done;
  1010. }
  1011. prtd = substream->runtime->private_data;
  1012. if (prtd->audio_client == NULL) {
  1013. pr_err("%s prtd is null.\n", __func__);
  1014. ret = -EINVAL;
  1015. goto done;
  1016. }
  1017. event_data = (struct msm_adsp_event_data *)ucontrol->value.bytes.data;
  1018. if ((event_data->event_type < ADSP_STREAM_PP_EVENT) ||
  1019. (event_data->event_type >= ADSP_STREAM_EVENT_MAX)) {
  1020. pr_err("%s: invalid event_type=%d",
  1021. __func__, event_data->event_type);
  1022. ret = -EINVAL;
  1023. goto done;
  1024. }
  1025. if ((sizeof(struct msm_adsp_event_data) + event_data->payload_len) >=
  1026. sizeof(ucontrol->value.bytes.data)) {
  1027. pr_err("%s param length=%d exceeds limit",
  1028. __func__, event_data->payload_len);
  1029. ret = -EINVAL;
  1030. goto done;
  1031. }
  1032. ret = q6asm_send_stream_cmd(prtd->audio_client, event_data);
  1033. if (ret < 0)
  1034. pr_err("%s: failed to send stream event cmd, err = %d\n",
  1035. __func__, ret);
  1036. done:
  1037. return ret;
  1038. }
  1039. static int msm_pcm_add_audio_adsp_stream_cmd_control(
  1040. struct snd_soc_pcm_runtime *rtd)
  1041. {
  1042. const char *mixer_ctl_name = DSP_STREAM_CMD;
  1043. const char *deviceNo = "NN";
  1044. char *mixer_str = NULL;
  1045. int ctl_len = 0, ret = 0;
  1046. struct snd_kcontrol_new fe_audio_adsp_stream_cmd_config_control[1] = {
  1047. {
  1048. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1049. .name = "?",
  1050. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  1051. .info = msm_adsp_stream_cmd_info,
  1052. .put = msm_pcm_adsp_stream_cmd_put,
  1053. .private_value = 0,
  1054. }
  1055. };
  1056. if (!rtd) {
  1057. pr_err("%s rtd is NULL\n", __func__);
  1058. ret = -EINVAL;
  1059. goto done;
  1060. }
  1061. ctl_len = strlen(mixer_ctl_name) + 1 + strlen(deviceNo) + 1;
  1062. mixer_str = kzalloc(ctl_len, GFP_KERNEL);
  1063. if (!mixer_str) {
  1064. ret = -ENOMEM;
  1065. goto done;
  1066. }
  1067. snprintf(mixer_str, ctl_len, "%s %d", mixer_ctl_name, rtd->pcm->device);
  1068. fe_audio_adsp_stream_cmd_config_control[0].name = mixer_str;
  1069. fe_audio_adsp_stream_cmd_config_control[0].private_value =
  1070. rtd->dai_link->id;
  1071. pr_debug("Registering new mixer ctl %s\n", mixer_str);
  1072. ret = snd_soc_add_platform_controls(rtd->platform,
  1073. fe_audio_adsp_stream_cmd_config_control,
  1074. ARRAY_SIZE(fe_audio_adsp_stream_cmd_config_control));
  1075. if (ret < 0)
  1076. pr_err("%s: failed add ctl %s. err = %d\n",
  1077. __func__, mixer_str, ret);
  1078. kfree(mixer_str);
  1079. done:
  1080. return ret;
  1081. }
  1082. static int msm_pcm_add_audio_adsp_stream_callback_control(
  1083. struct snd_soc_pcm_runtime *rtd)
  1084. {
  1085. const char *mixer_ctl_name = DSP_STREAM_CALLBACK;
  1086. const char *deviceNo = "NN";
  1087. char *mixer_str = NULL;
  1088. int ctl_len = 0, ret = 0;
  1089. struct snd_kcontrol *kctl;
  1090. struct snd_kcontrol_new fe_audio_adsp_callback_config_control[1] = {
  1091. {
  1092. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1093. .name = "?",
  1094. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  1095. .info = msm_adsp_stream_callback_info,
  1096. .get = msm_adsp_stream_callback_get,
  1097. .private_value = 0,
  1098. }
  1099. };
  1100. if (!rtd) {
  1101. pr_err("%s NULL rtd\n", __func__);
  1102. ret = -EINVAL;
  1103. goto done;
  1104. }
  1105. pr_debug("%s: added new pcm FE with name %s, id %d, cpu dai %s, device no %d\n",
  1106. __func__, rtd->dai_link->name, rtd->dai_link->id,
  1107. rtd->dai_link->cpu_dai_name, rtd->pcm->device);
  1108. ctl_len = strlen(mixer_ctl_name) + 1 + strlen(deviceNo) + 1;
  1109. mixer_str = kzalloc(ctl_len, GFP_KERNEL);
  1110. if (!mixer_str) {
  1111. ret = -ENOMEM;
  1112. goto done;
  1113. }
  1114. snprintf(mixer_str, ctl_len, "%s %d", mixer_ctl_name, rtd->pcm->device);
  1115. fe_audio_adsp_callback_config_control[0].name = mixer_str;
  1116. fe_audio_adsp_callback_config_control[0].private_value =
  1117. rtd->dai_link->id;
  1118. pr_debug("%s: Registering new mixer ctl %s\n", __func__, mixer_str);
  1119. ret = snd_soc_add_platform_controls(rtd->platform,
  1120. fe_audio_adsp_callback_config_control,
  1121. ARRAY_SIZE(fe_audio_adsp_callback_config_control));
  1122. if (ret < 0) {
  1123. pr_err("%s: failed to add ctl %s. err = %d\n",
  1124. __func__, mixer_str, ret);
  1125. ret = -EINVAL;
  1126. goto free_mixer_str;
  1127. }
  1128. kctl = snd_soc_card_get_kcontrol(rtd->card, mixer_str);
  1129. if (!kctl) {
  1130. pr_err("%s: failed to get kctl %s.\n", __func__, mixer_str);
  1131. ret = -EINVAL;
  1132. goto free_mixer_str;
  1133. }
  1134. kctl->private_data = NULL;
  1135. free_mixer_str:
  1136. kfree(mixer_str);
  1137. done:
  1138. return ret;
  1139. }
  1140. static int msm_pcm_set_volume(struct msm_audio *prtd, uint32_t volume)
  1141. {
  1142. int rc = 0;
  1143. if (prtd && prtd->audio_client) {
  1144. pr_debug("%s: channels %d volume 0x%x\n", __func__,
  1145. prtd->channel_mode, volume);
  1146. rc = q6asm_set_volume(prtd->audio_client, volume);
  1147. if (rc < 0) {
  1148. pr_err("%s: Send Volume command failed rc=%d\n",
  1149. __func__, rc);
  1150. }
  1151. }
  1152. return rc;
  1153. }
  1154. static int msm_pcm_volume_ctl_get(struct snd_kcontrol *kcontrol,
  1155. struct snd_ctl_elem_value *ucontrol)
  1156. {
  1157. struct snd_pcm_volume *vol = snd_kcontrol_chip(kcontrol);
  1158. struct snd_pcm_substream *substream =
  1159. vol->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
  1160. struct msm_audio *prtd;
  1161. pr_debug("%s\n", __func__);
  1162. if (!substream) {
  1163. pr_err("%s substream not found\n", __func__);
  1164. return -ENODEV;
  1165. }
  1166. if (!substream->runtime) {
  1167. pr_err("%s substream runtime not found\n", __func__);
  1168. return 0;
  1169. }
  1170. prtd = substream->runtime->private_data;
  1171. if (prtd)
  1172. ucontrol->value.integer.value[0] = prtd->volume;
  1173. return 0;
  1174. }
  1175. static int msm_pcm_volume_ctl_put(struct snd_kcontrol *kcontrol,
  1176. struct snd_ctl_elem_value *ucontrol)
  1177. {
  1178. int rc = 0;
  1179. struct snd_pcm_volume *vol = snd_kcontrol_chip(kcontrol);
  1180. struct snd_pcm_substream *substream =
  1181. vol->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
  1182. struct msm_audio *prtd;
  1183. int volume = ucontrol->value.integer.value[0];
  1184. pr_debug("%s: volume : 0x%x\n", __func__, volume);
  1185. if (!substream) {
  1186. pr_err("%s substream not found\n", __func__);
  1187. return -ENODEV;
  1188. }
  1189. if (!substream->runtime) {
  1190. pr_err("%s substream runtime not found\n", __func__);
  1191. return 0;
  1192. }
  1193. prtd = substream->runtime->private_data;
  1194. if (prtd) {
  1195. rc = msm_pcm_set_volume(prtd, volume);
  1196. prtd->volume = volume;
  1197. }
  1198. return rc;
  1199. }
  1200. static int msm_pcm_add_volume_control(struct snd_soc_pcm_runtime *rtd)
  1201. {
  1202. int ret = 0;
  1203. struct snd_pcm *pcm = rtd->pcm;
  1204. struct snd_pcm_volume *volume_info;
  1205. struct snd_kcontrol *kctl;
  1206. dev_dbg(rtd->dev, "%s, Volume control add\n", __func__);
  1207. ret = snd_pcm_add_volume_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  1208. NULL, 1, rtd->dai_link->id,
  1209. &volume_info);
  1210. if (ret < 0) {
  1211. pr_err("%s volume control failed ret %d\n", __func__, ret);
  1212. return ret;
  1213. }
  1214. kctl = volume_info->kctl;
  1215. kctl->put = msm_pcm_volume_ctl_put;
  1216. kctl->get = msm_pcm_volume_ctl_get;
  1217. kctl->tlv.p = msm_pcm_vol_gain;
  1218. return 0;
  1219. }
  1220. static int msm_pcm_compress_ctl_info(struct snd_kcontrol *kcontrol,
  1221. struct snd_ctl_elem_info *uinfo)
  1222. {
  1223. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1224. uinfo->count = 1;
  1225. uinfo->value.integer.min = 0;
  1226. uinfo->value.integer.max = 0x2000;
  1227. return 0;
  1228. }
  1229. static int msm_pcm_compress_ctl_get(struct snd_kcontrol *kcontrol,
  1230. struct snd_ctl_elem_value *ucontrol)
  1231. {
  1232. struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
  1233. struct snd_soc_platform *platform = snd_soc_component_to_platform(comp);
  1234. struct msm_plat_data *pdata = dev_get_drvdata(platform->dev);
  1235. struct snd_pcm_substream *substream;
  1236. struct msm_audio *prtd;
  1237. if (!pdata) {
  1238. pr_err("%s pdata is NULL\n", __func__);
  1239. return -ENODEV;
  1240. }
  1241. substream = pdata->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
  1242. if (!substream) {
  1243. pr_err("%s substream not found\n", __func__);
  1244. return -EINVAL;
  1245. }
  1246. if (!substream->runtime) {
  1247. pr_err("%s substream runtime not found\n", __func__);
  1248. return 0;
  1249. }
  1250. prtd = substream->runtime->private_data;
  1251. if (prtd)
  1252. ucontrol->value.integer.value[0] = prtd->compress_enable;
  1253. return 0;
  1254. }
  1255. static int msm_pcm_compress_ctl_put(struct snd_kcontrol *kcontrol,
  1256. struct snd_ctl_elem_value *ucontrol)
  1257. {
  1258. int rc = 0;
  1259. struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
  1260. struct snd_soc_platform *platform = snd_soc_component_to_platform(comp);
  1261. struct msm_plat_data *pdata = dev_get_drvdata(platform->dev);
  1262. struct snd_pcm_substream *substream;
  1263. struct msm_audio *prtd;
  1264. int compress = ucontrol->value.integer.value[0];
  1265. if (!pdata) {
  1266. pr_err("%s pdata is NULL\n", __func__);
  1267. return -ENODEV;
  1268. }
  1269. substream = pdata->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
  1270. pr_debug("%s: compress : 0x%x\n", __func__, compress);
  1271. if (!substream) {
  1272. pr_err("%s substream not found\n", __func__);
  1273. return -EINVAL;
  1274. }
  1275. if (!substream->runtime) {
  1276. pr_err("%s substream runtime not found\n", __func__);
  1277. return 0;
  1278. }
  1279. prtd = substream->runtime->private_data;
  1280. if (prtd) {
  1281. pr_debug("%s: setting compress flag to 0x%x\n",
  1282. __func__, compress);
  1283. prtd->compress_enable = compress;
  1284. }
  1285. return rc;
  1286. }
  1287. static int msm_pcm_add_compress_control(struct snd_soc_pcm_runtime *rtd)
  1288. {
  1289. const char *mixer_ctl_name = "Playback ";
  1290. const char *mixer_ctl_end_name = " Compress";
  1291. const char *deviceNo = "NN";
  1292. char *mixer_str = NULL;
  1293. int ctl_len;
  1294. int ret = 0;
  1295. struct msm_plat_data *pdata;
  1296. struct snd_kcontrol_new pcm_compress_control[1] = {
  1297. {
  1298. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1299. .name = "?",
  1300. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  1301. .info = msm_pcm_compress_ctl_info,
  1302. .get = msm_pcm_compress_ctl_get,
  1303. .put = msm_pcm_compress_ctl_put,
  1304. .private_value = 0,
  1305. }
  1306. };
  1307. if (!rtd) {
  1308. pr_err("%s: NULL rtd\n", __func__);
  1309. return -EINVAL;
  1310. }
  1311. ctl_len = strlen(mixer_ctl_name) + strlen(deviceNo) +
  1312. strlen(mixer_ctl_end_name) + 1;
  1313. mixer_str = kzalloc(ctl_len, GFP_KERNEL);
  1314. if (!mixer_str)
  1315. return -ENOMEM;
  1316. snprintf(mixer_str, ctl_len, "%s%d%s", mixer_ctl_name,
  1317. rtd->pcm->device, mixer_ctl_end_name);
  1318. pcm_compress_control[0].name = mixer_str;
  1319. pcm_compress_control[0].private_value = rtd->dai_link->id;
  1320. pr_debug("%s: Registering new mixer ctl %s\n", __func__, mixer_str);
  1321. pdata = dev_get_drvdata(rtd->platform->dev);
  1322. if (pdata) {
  1323. if (!pdata->pcm) {
  1324. pdata->pcm = rtd->pcm;
  1325. snd_soc_add_platform_controls(rtd->platform,
  1326. pcm_compress_control,
  1327. ARRAY_SIZE
  1328. (pcm_compress_control));
  1329. pr_debug("%s: add control success plt = %pK\n",
  1330. __func__, rtd->platform);
  1331. }
  1332. } else {
  1333. pr_err("%s: NULL pdata\n", __func__);
  1334. ret = -EINVAL;
  1335. }
  1336. kfree(mixer_str);
  1337. return ret;
  1338. }
  1339. static int msm_pcm_chmap_ctl_put(struct snd_kcontrol *kcontrol,
  1340. struct snd_ctl_elem_value *ucontrol)
  1341. {
  1342. int i;
  1343. struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
  1344. unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
  1345. struct snd_pcm_substream *substream;
  1346. struct msm_audio *prtd;
  1347. pr_debug("%s", __func__);
  1348. substream = snd_pcm_chmap_substream(info, idx);
  1349. if (!substream)
  1350. return -ENODEV;
  1351. if (!substream->runtime)
  1352. return 0;
  1353. prtd = substream->runtime->private_data;
  1354. if (prtd) {
  1355. prtd->set_channel_map = true;
  1356. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL; i++)
  1357. prtd->channel_map[i] =
  1358. (char)(ucontrol->value.integer.value[i]);
  1359. }
  1360. return 0;
  1361. }
  1362. static int msm_pcm_chmap_ctl_get(struct snd_kcontrol *kcontrol,
  1363. struct snd_ctl_elem_value *ucontrol)
  1364. {
  1365. int i;
  1366. struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
  1367. unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
  1368. struct snd_pcm_substream *substream;
  1369. struct msm_audio *prtd;
  1370. pr_debug("%s", __func__);
  1371. substream = snd_pcm_chmap_substream(info, idx);
  1372. if (!substream)
  1373. return -ENODEV;
  1374. memset(ucontrol->value.integer.value, 0,
  1375. sizeof(ucontrol->value.integer.value));
  1376. if (!substream->runtime)
  1377. return 0; /* no channels set */
  1378. prtd = substream->runtime->private_data;
  1379. if (prtd && prtd->set_channel_map == true) {
  1380. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL; i++)
  1381. ucontrol->value.integer.value[i] =
  1382. (int)prtd->channel_map[i];
  1383. } else {
  1384. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL; i++)
  1385. ucontrol->value.integer.value[i] = 0;
  1386. }
  1387. return 0;
  1388. }
  1389. static int msm_pcm_add_chmap_controls(struct snd_soc_pcm_runtime *rtd)
  1390. {
  1391. struct snd_pcm *pcm = rtd->pcm;
  1392. struct snd_pcm_chmap *chmap_info;
  1393. struct snd_kcontrol *kctl;
  1394. char device_num[12];
  1395. int i, ret = 0;
  1396. pr_debug("%s, Channel map cntrl add\n", __func__);
  1397. ret = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  1398. snd_pcm_std_chmaps,
  1399. PCM_FORMAT_MAX_NUM_CHANNEL, 0,
  1400. &chmap_info);
  1401. if (ret < 0) {
  1402. pr_err("%s, channel map cntrl add failed\n", __func__);
  1403. return ret;
  1404. }
  1405. kctl = chmap_info->kctl;
  1406. for (i = 0; i < kctl->count; i++)
  1407. kctl->vd[i].access |= SNDRV_CTL_ELEM_ACCESS_WRITE;
  1408. snprintf(device_num, sizeof(device_num), "%d", pcm->device);
  1409. strlcat(kctl->id.name, device_num, sizeof(kctl->id.name));
  1410. pr_debug("%s, Overwriting channel map control name to: %s\n",
  1411. __func__, kctl->id.name);
  1412. kctl->put = msm_pcm_chmap_ctl_put;
  1413. kctl->get = msm_pcm_chmap_ctl_get;
  1414. return 0;
  1415. }
  1416. static int msm_pcm_playback_app_type_cfg_ctl_put(struct snd_kcontrol *kcontrol,
  1417. struct snd_ctl_elem_value *ucontrol)
  1418. {
  1419. u64 fe_id = kcontrol->private_value;
  1420. int session_type = SESSION_TYPE_RX;
  1421. int be_id = ucontrol->value.integer.value[3];
  1422. struct msm_pcm_stream_app_type_cfg cfg_data = {0, 0, 48000};
  1423. int ret = 0;
  1424. cfg_data.app_type = ucontrol->value.integer.value[0];
  1425. cfg_data.acdb_dev_id = ucontrol->value.integer.value[1];
  1426. if (ucontrol->value.integer.value[2] != 0)
  1427. cfg_data.sample_rate = ucontrol->value.integer.value[2];
  1428. pr_debug("%s: fe_id- %llu session_type- %d be_id- %d app_type- %d acdb_dev_id- %d sample_rate- %d\n",
  1429. __func__, fe_id, session_type, be_id,
  1430. cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
  1431. ret = msm_pcm_routing_reg_stream_app_type_cfg(fe_id, session_type,
  1432. be_id, &cfg_data);
  1433. if (ret < 0)
  1434. pr_err("%s: msm_pcm_routing_reg_stream_app_type_cfg failed returned %d\n",
  1435. __func__, ret);
  1436. return ret;
  1437. }
  1438. static int msm_pcm_playback_app_type_cfg_ctl_get(struct snd_kcontrol *kcontrol,
  1439. struct snd_ctl_elem_value *ucontrol)
  1440. {
  1441. u64 fe_id = kcontrol->private_value;
  1442. int session_type = SESSION_TYPE_RX;
  1443. int be_id = 0;
  1444. struct msm_pcm_stream_app_type_cfg cfg_data = {0};
  1445. int ret = 0;
  1446. ret = msm_pcm_routing_get_stream_app_type_cfg(fe_id, session_type,
  1447. &be_id, &cfg_data);
  1448. if (ret < 0) {
  1449. pr_err("%s: msm_pcm_routing_get_stream_app_type_cfg failed returned %d\n",
  1450. __func__, ret);
  1451. goto done;
  1452. }
  1453. ucontrol->value.integer.value[0] = cfg_data.app_type;
  1454. ucontrol->value.integer.value[1] = cfg_data.acdb_dev_id;
  1455. ucontrol->value.integer.value[2] = cfg_data.sample_rate;
  1456. ucontrol->value.integer.value[3] = be_id;
  1457. pr_debug("%s: fedai_id %llu, session_type %d, be_id %d, app_type %d, acdb_dev_id %d, sample_rate %d\n",
  1458. __func__, fe_id, session_type, be_id,
  1459. cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
  1460. done:
  1461. return ret;
  1462. }
  1463. static int msm_pcm_capture_app_type_cfg_ctl_put(struct snd_kcontrol *kcontrol,
  1464. struct snd_ctl_elem_value *ucontrol)
  1465. {
  1466. u64 fe_id = kcontrol->private_value;
  1467. int session_type = SESSION_TYPE_TX;
  1468. int be_id = ucontrol->value.integer.value[3];
  1469. struct msm_pcm_stream_app_type_cfg cfg_data = {0, 0, 48000};
  1470. int ret = 0;
  1471. cfg_data.app_type = ucontrol->value.integer.value[0];
  1472. cfg_data.acdb_dev_id = ucontrol->value.integer.value[1];
  1473. if (ucontrol->value.integer.value[2] != 0)
  1474. cfg_data.sample_rate = ucontrol->value.integer.value[2];
  1475. pr_debug("%s: fe_id- %llu session_type- %d be_id- %d app_type- %d acdb_dev_id- %d sample_rate- %d\n",
  1476. __func__, fe_id, session_type, be_id,
  1477. cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
  1478. ret = msm_pcm_routing_reg_stream_app_type_cfg(fe_id, session_type,
  1479. be_id, &cfg_data);
  1480. if (ret < 0)
  1481. pr_err("%s: msm_pcm_routing_reg_stream_app_type_cfg failed returned %d\n",
  1482. __func__, ret);
  1483. return ret;
  1484. }
  1485. static int msm_pcm_capture_app_type_cfg_ctl_get(struct snd_kcontrol *kcontrol,
  1486. struct snd_ctl_elem_value *ucontrol)
  1487. {
  1488. u64 fe_id = kcontrol->private_value;
  1489. int session_type = SESSION_TYPE_TX;
  1490. int be_id = 0;
  1491. struct msm_pcm_stream_app_type_cfg cfg_data = {0};
  1492. int ret = 0;
  1493. ret = msm_pcm_routing_get_stream_app_type_cfg(fe_id, session_type,
  1494. &be_id, &cfg_data);
  1495. if (ret < 0) {
  1496. pr_err("%s: msm_pcm_routing_get_stream_app_type_cfg failed returned %d\n",
  1497. __func__, ret);
  1498. goto done;
  1499. }
  1500. ucontrol->value.integer.value[0] = cfg_data.app_type;
  1501. ucontrol->value.integer.value[1] = cfg_data.acdb_dev_id;
  1502. ucontrol->value.integer.value[2] = cfg_data.sample_rate;
  1503. ucontrol->value.integer.value[3] = be_id;
  1504. pr_debug("%s: fedai_id %llu, session_type %d, be_id %d, app_type %d, acdb_dev_id %d, sample_rate %d\n",
  1505. __func__, fe_id, session_type, be_id,
  1506. cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
  1507. done:
  1508. return ret;
  1509. }
  1510. static int msm_pcm_add_app_type_controls(struct snd_soc_pcm_runtime *rtd)
  1511. {
  1512. struct snd_pcm *pcm = rtd->pcm;
  1513. struct snd_pcm_usr *app_type_info;
  1514. struct snd_kcontrol *kctl;
  1515. const char *playback_mixer_ctl_name = "Audio Stream";
  1516. const char *capture_mixer_ctl_name = "Audio Stream Capture";
  1517. const char *deviceNo = "NN";
  1518. const char *suffix = "App Type Cfg";
  1519. int ctl_len, ret = 0;
  1520. if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
  1521. ctl_len = strlen(playback_mixer_ctl_name) + 1 +
  1522. strlen(deviceNo) + 1 + strlen(suffix) + 1;
  1523. pr_debug("%s: Playback app type cntrl add\n", __func__);
  1524. ret = snd_pcm_add_usr_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  1525. NULL, 1, ctl_len, rtd->dai_link->id,
  1526. &app_type_info);
  1527. if (ret < 0) {
  1528. pr_err("%s: playback app type cntrl add failed: %d\n",
  1529. __func__, ret);
  1530. return ret;
  1531. }
  1532. kctl = app_type_info->kctl;
  1533. snprintf(kctl->id.name, ctl_len, "%s %d %s",
  1534. playback_mixer_ctl_name, rtd->pcm->device, suffix);
  1535. kctl->put = msm_pcm_playback_app_type_cfg_ctl_put;
  1536. kctl->get = msm_pcm_playback_app_type_cfg_ctl_get;
  1537. }
  1538. if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
  1539. ctl_len = strlen(capture_mixer_ctl_name) + 1 +
  1540. strlen(deviceNo) + 1 + strlen(suffix) + 1;
  1541. pr_debug("%s: Capture app type cntrl add\n", __func__);
  1542. ret = snd_pcm_add_usr_ctls(pcm, SNDRV_PCM_STREAM_CAPTURE,
  1543. NULL, 1, ctl_len, rtd->dai_link->id,
  1544. &app_type_info);
  1545. if (ret < 0) {
  1546. pr_err("%s: capture app type cntrl add failed: %d\n",
  1547. __func__, ret);
  1548. return ret;
  1549. }
  1550. kctl = app_type_info->kctl;
  1551. snprintf(kctl->id.name, ctl_len, "%s %d %s",
  1552. capture_mixer_ctl_name, rtd->pcm->device, suffix);
  1553. kctl->put = msm_pcm_capture_app_type_cfg_ctl_put;
  1554. kctl->get = msm_pcm_capture_app_type_cfg_ctl_get;
  1555. }
  1556. return 0;
  1557. }
  1558. static int msm_pcm_add_controls(struct snd_soc_pcm_runtime *rtd)
  1559. {
  1560. int ret = 0;
  1561. pr_debug("%s\n", __func__);
  1562. ret = msm_pcm_add_chmap_controls(rtd);
  1563. if (ret)
  1564. pr_err("%s: pcm add controls failed:%d\n", __func__, ret);
  1565. ret = msm_pcm_add_app_type_controls(rtd);
  1566. if (ret)
  1567. pr_err("%s: pcm add app type controls failed:%d\n",
  1568. __func__, ret);
  1569. return ret;
  1570. }
  1571. static int msm_asoc_pcm_new(struct snd_soc_pcm_runtime *rtd)
  1572. {
  1573. struct snd_card *card = rtd->card->snd_card;
  1574. int ret = 0;
  1575. if (!card->dev->coherent_dma_mask)
  1576. card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
  1577. ret = msm_pcm_add_controls(rtd);
  1578. if (ret) {
  1579. pr_err("%s, kctl add failed:%d\n", __func__, ret);
  1580. return ret;
  1581. }
  1582. ret = msm_pcm_add_volume_control(rtd);
  1583. if (ret)
  1584. pr_err("%s: Could not add pcm Volume Control %d\n",
  1585. __func__, ret);
  1586. ret = msm_pcm_add_compress_control(rtd);
  1587. if (ret)
  1588. pr_err("%s: Could not add pcm Compress Control %d\n",
  1589. __func__, ret);
  1590. ret = msm_pcm_add_audio_adsp_stream_cmd_control(rtd);
  1591. if (ret)
  1592. pr_err("%s: Could not add pcm ADSP Stream Cmd Control\n",
  1593. __func__);
  1594. ret = msm_pcm_add_audio_adsp_stream_callback_control(rtd);
  1595. if (ret)
  1596. pr_err("%s: Could not add pcm ADSP Stream Callback Control\n",
  1597. __func__);
  1598. return ret;
  1599. }
  1600. static snd_pcm_sframes_t msm_pcm_delay_blk(struct snd_pcm_substream *substream,
  1601. struct snd_soc_dai *dai)
  1602. {
  1603. struct snd_pcm_runtime *runtime = substream->runtime;
  1604. struct msm_audio *prtd = runtime->private_data;
  1605. struct audio_client *ac = prtd->audio_client;
  1606. snd_pcm_sframes_t frames;
  1607. int ret;
  1608. ret = q6asm_get_path_delay(prtd->audio_client);
  1609. if (ret) {
  1610. pr_err("%s: get_path_delay failed, ret=%d\n", __func__, ret);
  1611. return 0;
  1612. }
  1613. /* convert microseconds to frames */
  1614. frames = ac->path_delay / 1000 * runtime->rate / 1000;
  1615. /* also convert the remainder from the initial division */
  1616. frames += ac->path_delay % 1000 * runtime->rate / 1000000;
  1617. /* overcompensate for the loss of precision (empirical) */
  1618. frames += 2;
  1619. return frames;
  1620. }
  1621. static struct snd_soc_platform_driver msm_soc_platform = {
  1622. .ops = &msm_pcm_ops,
  1623. .pcm_new = msm_asoc_pcm_new,
  1624. .delay_blk = msm_pcm_delay_blk,
  1625. };
  1626. static int msm_pcm_probe(struct platform_device *pdev)
  1627. {
  1628. int rc;
  1629. int id;
  1630. struct msm_plat_data *pdata;
  1631. const char *latency_level;
  1632. rc = of_property_read_u32(pdev->dev.of_node,
  1633. "qcom,msm-pcm-dsp-id", &id);
  1634. if (rc) {
  1635. dev_err(&pdev->dev, "%s: qcom,msm-pcm-dsp-id missing in DT node\n",
  1636. __func__);
  1637. return rc;
  1638. }
  1639. pdata = kzalloc(sizeof(struct msm_plat_data), GFP_KERNEL);
  1640. if (!pdata)
  1641. return -ENOMEM;
  1642. if (of_property_read_bool(pdev->dev.of_node,
  1643. "qcom,msm-pcm-low-latency")) {
  1644. pdata->perf_mode = LOW_LATENCY_PCM_MODE;
  1645. rc = of_property_read_string(pdev->dev.of_node,
  1646. "qcom,latency-level", &latency_level);
  1647. if (!rc) {
  1648. if (!strcmp(latency_level, "ultra"))
  1649. pdata->perf_mode = ULTRA_LOW_LATENCY_PCM_MODE;
  1650. else if (!strcmp(latency_level, "ull-pp"))
  1651. pdata->perf_mode =
  1652. ULL_POST_PROCESSING_PCM_MODE;
  1653. }
  1654. } else {
  1655. pdata->perf_mode = LEGACY_PCM_MODE;
  1656. }
  1657. dev_set_drvdata(&pdev->dev, pdata);
  1658. dev_dbg(&pdev->dev, "%s: dev name %s\n",
  1659. __func__, dev_name(&pdev->dev));
  1660. return snd_soc_register_platform(&pdev->dev,
  1661. &msm_soc_platform);
  1662. }
  1663. static int msm_pcm_remove(struct platform_device *pdev)
  1664. {
  1665. struct msm_plat_data *pdata;
  1666. pdata = dev_get_drvdata(&pdev->dev);
  1667. kfree(pdata);
  1668. snd_soc_unregister_platform(&pdev->dev);
  1669. return 0;
  1670. }
  1671. static const struct of_device_id msm_pcm_dt_match[] = {
  1672. {.compatible = "qcom,msm-pcm-dsp"},
  1673. {}
  1674. };
  1675. MODULE_DEVICE_TABLE(of, msm_pcm_dt_match);
  1676. static struct platform_driver msm_pcm_driver = {
  1677. .driver = {
  1678. .name = "msm-pcm-dsp",
  1679. .owner = THIS_MODULE,
  1680. .of_match_table = msm_pcm_dt_match,
  1681. },
  1682. .probe = msm_pcm_probe,
  1683. .remove = msm_pcm_remove,
  1684. };
  1685. int __init msm_pcm_dsp_init(void)
  1686. {
  1687. init_waitqueue_head(&the_locks.enable_wait);
  1688. init_waitqueue_head(&the_locks.eos_wait);
  1689. init_waitqueue_head(&the_locks.write_wait);
  1690. init_waitqueue_head(&the_locks.read_wait);
  1691. return platform_driver_register(&msm_pcm_driver);
  1692. }
  1693. void msm_pcm_dsp_exit(void)
  1694. {
  1695. platform_driver_unregister(&msm_pcm_driver);
  1696. }
  1697. MODULE_DESCRIPTION("PCM module platform driver");
  1698. MODULE_LICENSE("GPL v2");