msm-pcm-q6-v2.c 54 KB

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