msm-pcm-q6-v2.c 54 KB

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