msm-pcm-q6-v2.c 83 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2012-2020, 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/mutex.h>
  10. #include <linux/wait.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/slab.h>
  13. #include <sound/core.h>
  14. #include <sound/soc.h>
  15. #include <sound/soc-dapm.h>
  16. #include <sound/pcm.h>
  17. #include <sound/initval.h>
  18. #include <sound/control.h>
  19. #include <sound/timer.h>
  20. #include <asm/dma.h>
  21. #include <linux/dma-mapping.h>
  22. #include <audio/linux/msm_audio.h>
  23. #include <linux/of_device.h>
  24. #include <sound/tlv.h>
  25. #include <sound/pcm_params.h>
  26. #include <audio/sound/devdep_params.h>
  27. #include <dsp/msm_audio_ion.h>
  28. #include <dsp/q6audio-v2.h>
  29. #include <dsp/q6core.h>
  30. #include <dsp/q6asm-v2.h>
  31. #include "msm-pcm-q6-v2.h"
  32. #include "msm-pcm-routing-v2.h"
  33. #include "msm-qti-pp-config.h"
  34. #define DRV_NAME "msm-pcm-q6-v2"
  35. #define TIMEOUT_MS 1000
  36. enum stream_state {
  37. IDLE = 0,
  38. STOPPED,
  39. RUNNING,
  40. };
  41. static struct audio_locks the_locks;
  42. #define PCM_MASTER_VOL_MAX_STEPS 0x2000
  43. static const DECLARE_TLV_DB_LINEAR(msm_pcm_vol_gain, 0,
  44. PCM_MASTER_VOL_MAX_STEPS);
  45. struct snd_msm {
  46. struct snd_card *card;
  47. struct snd_pcm *pcm;
  48. };
  49. #define CMD_EOS_MIN_TIMEOUT_LENGTH 50
  50. #define CMD_EOS_TIMEOUT_MULTIPLIER (HZ * 50)
  51. #define MAX_PB_COPY_RETRIES 3
  52. static struct snd_pcm_hardware msm_pcm_hardware_capture = {
  53. .info = (SNDRV_PCM_INFO_MMAP |
  54. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  55. SNDRV_PCM_INFO_MMAP_VALID |
  56. SNDRV_PCM_INFO_INTERLEAVED |
  57. SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME),
  58. .formats = (SNDRV_PCM_FMTBIT_S16_LE |
  59. SNDRV_PCM_FMTBIT_S24_LE |
  60. SNDRV_PCM_FMTBIT_S24_3LE |
  61. SNDRV_PCM_FMTBIT_S32_LE),
  62. .rates = SNDRV_PCM_RATE_8000_384000,
  63. .rate_min = 8000,
  64. .rate_max = 384000,
  65. .channels_min = 1,
  66. .channels_max = 4,
  67. .buffer_bytes_max = CAPTURE_MAX_NUM_PERIODS *
  68. CAPTURE_MAX_PERIOD_SIZE,
  69. .period_bytes_min = CAPTURE_MIN_PERIOD_SIZE,
  70. .period_bytes_max = CAPTURE_MAX_PERIOD_SIZE,
  71. .periods_min = CAPTURE_MIN_NUM_PERIODS,
  72. .periods_max = CAPTURE_MAX_NUM_PERIODS,
  73. .fifo_size = 0,
  74. };
  75. static struct snd_pcm_hardware msm_pcm_hardware_playback = {
  76. .info = (SNDRV_PCM_INFO_MMAP |
  77. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  78. SNDRV_PCM_INFO_MMAP_VALID |
  79. SNDRV_PCM_INFO_INTERLEAVED |
  80. SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME),
  81. .formats = (SNDRV_PCM_FMTBIT_S16_LE |
  82. SNDRV_PCM_FMTBIT_S24_LE |
  83. SNDRV_PCM_FMTBIT_S24_3LE |
  84. SNDRV_PCM_FMTBIT_S32_LE),
  85. .rates = SNDRV_PCM_RATE_8000_384000,
  86. .rate_min = 8000,
  87. .rate_max = 384000,
  88. .channels_min = 1,
  89. .channels_max = 8,
  90. .buffer_bytes_max = PLAYBACK_MAX_NUM_PERIODS *
  91. PLAYBACK_MAX_PERIOD_SIZE,
  92. .period_bytes_min = PLAYBACK_MIN_PERIOD_SIZE,
  93. .period_bytes_max = PLAYBACK_MAX_PERIOD_SIZE,
  94. .periods_min = PLAYBACK_MIN_NUM_PERIODS,
  95. .periods_max = PLAYBACK_MAX_NUM_PERIODS,
  96. .fifo_size = 0,
  97. };
  98. /* Conventional and unconventional sample rate supported */
  99. static unsigned int supported_sample_rates[] = {
  100. 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000,
  101. 88200, 96000, 176400, 192000, 352800, 384000
  102. };
  103. static struct snd_pcm_hw_constraint_list constraints_sample_rates = {
  104. .count = ARRAY_SIZE(supported_sample_rates),
  105. .list = supported_sample_rates,
  106. .mask = 0,
  107. };
  108. static void msm_pcm_route_event_handler(enum msm_pcm_routing_event event,
  109. void *priv_data)
  110. {
  111. struct msm_audio *prtd = priv_data;
  112. WARN_ON(!prtd);
  113. pr_debug("%s: event %x\n", __func__, event);
  114. switch (event) {
  115. case MSM_PCM_RT_EVT_BUF_RECFG:
  116. q6asm_cmd(prtd->audio_client, CMD_PAUSE);
  117. q6asm_cmd(prtd->audio_client, CMD_FLUSH);
  118. q6asm_run(prtd->audio_client, 0, 0, 0);
  119. /* fallthrough */
  120. default:
  121. break;
  122. }
  123. }
  124. static void event_handler(uint32_t opcode,
  125. uint32_t token, uint32_t *payload, void *priv)
  126. {
  127. struct msm_audio *prtd = priv;
  128. struct snd_pcm_substream *substream = prtd->substream;
  129. uint32_t *ptrmem = (uint32_t *)payload;
  130. uint32_t idx = 0;
  131. uint32_t size = 0;
  132. uint8_t buf_index;
  133. struct snd_soc_pcm_runtime *rtd;
  134. int ret = 0;
  135. switch (opcode) {
  136. case ASM_DATA_EVENT_WRITE_DONE_V2: {
  137. pr_debug("ASM_DATA_EVENT_WRITE_DONE_V2\n");
  138. pr_debug("Buffer Consumed = 0x%08x\n", *ptrmem);
  139. prtd->pcm_irq_pos += prtd->pcm_count;
  140. if (atomic_read(&prtd->start))
  141. snd_pcm_period_elapsed(substream);
  142. atomic_inc(&prtd->out_count);
  143. wake_up(&the_locks.write_wait);
  144. if (!atomic_read(&prtd->start))
  145. break;
  146. if (!prtd->mmap_flag || prtd->reset_event)
  147. break;
  148. if (q6asm_is_cpu_buf_avail_nolock(IN,
  149. prtd->audio_client,
  150. &size, &idx)) {
  151. pr_debug("%s:writing %d bytes of buffer to dsp 2\n",
  152. __func__, prtd->pcm_count);
  153. q6asm_write_nolock(prtd->audio_client,
  154. prtd->pcm_count, 0, 0, NO_TIMESTAMP);
  155. }
  156. break;
  157. }
  158. case ASM_DATA_EVENT_RENDERED_EOS:
  159. case ASM_DATA_EVENT_RENDERED_EOS_V2:
  160. pr_debug("ASM_DATA_EVENT_RENDERED_EOS\n");
  161. clear_bit(CMD_EOS, &prtd->cmd_pending);
  162. wake_up(&the_locks.eos_wait);
  163. break;
  164. case ASM_DATA_EVENT_READ_DONE_V2: {
  165. pr_debug("ASM_DATA_EVENT_READ_DONE_V2\n");
  166. buf_index = q6asm_get_buf_index_from_token(token);
  167. if (buf_index >= CAPTURE_MAX_NUM_PERIODS) {
  168. pr_err("%s: buffer index %u is out of range.\n",
  169. __func__, buf_index);
  170. return;
  171. }
  172. pr_debug("%s: token=0x%08x buf_index=0x%08x\n",
  173. __func__, token, buf_index);
  174. prtd->in_frame_info[buf_index].size = payload[4];
  175. prtd->in_frame_info[buf_index].offset = payload[5];
  176. /* assume data size = 0 during flushing */
  177. if (prtd->in_frame_info[buf_index].size) {
  178. prtd->pcm_irq_pos +=
  179. prtd->in_frame_info[buf_index].size;
  180. pr_debug("pcm_irq_pos=%d\n", prtd->pcm_irq_pos);
  181. if (atomic_read(&prtd->start))
  182. snd_pcm_period_elapsed(substream);
  183. if (atomic_read(&prtd->in_count) <= prtd->periods)
  184. atomic_inc(&prtd->in_count);
  185. wake_up(&the_locks.read_wait);
  186. if (prtd->mmap_flag &&
  187. q6asm_is_cpu_buf_avail_nolock(OUT,
  188. prtd->audio_client,
  189. &size, &idx) &&
  190. (substream->runtime->status->state ==
  191. SNDRV_PCM_STATE_RUNNING))
  192. q6asm_read_nolock(prtd->audio_client);
  193. } else {
  194. pr_debug("%s: reclaim flushed buf in_count %x\n",
  195. __func__, atomic_read(&prtd->in_count));
  196. prtd->pcm_irq_pos += prtd->pcm_count;
  197. if (prtd->mmap_flag) {
  198. if (q6asm_is_cpu_buf_avail_nolock(OUT,
  199. prtd->audio_client,
  200. &size, &idx) &&
  201. (substream->runtime->status->state ==
  202. SNDRV_PCM_STATE_RUNNING))
  203. q6asm_read_nolock(prtd->audio_client);
  204. } else {
  205. atomic_inc(&prtd->in_count);
  206. }
  207. if (atomic_read(&prtd->in_count) == prtd->periods) {
  208. pr_info("%s: reclaimed all bufs\n", __func__);
  209. if (atomic_read(&prtd->start))
  210. snd_pcm_period_elapsed(substream);
  211. wake_up(&the_locks.read_wait);
  212. }
  213. }
  214. break;
  215. }
  216. case ASM_STREAM_PP_EVENT:
  217. case ASM_STREAM_CMD_ENCDEC_EVENTS: {
  218. pr_debug("%s: ASM_STREAM_EVENT (0x%x)\n", __func__, opcode);
  219. if (!substream) {
  220. pr_err("%s: substream is NULL.\n", __func__);
  221. return;
  222. }
  223. rtd = substream->private_data;
  224. if (!rtd) {
  225. pr_err("%s: rtd is NULL\n", __func__);
  226. return;
  227. }
  228. ret = msm_adsp_inform_mixer_ctl(rtd, payload);
  229. if (ret) {
  230. pr_err("%s: failed to inform mixer ctl. err = %d\n",
  231. __func__, ret);
  232. return;
  233. }
  234. break;
  235. }
  236. case APR_BASIC_RSP_RESULT: {
  237. switch (payload[0]) {
  238. case ASM_SESSION_CMD_RUN_V2:
  239. if (substream->stream
  240. != SNDRV_PCM_STREAM_PLAYBACK) {
  241. atomic_set(&prtd->start, 1);
  242. break;
  243. }
  244. if (prtd->mmap_flag) {
  245. pr_debug("%s:writing %d bytes of buffer to dsp\n",
  246. __func__,
  247. prtd->pcm_count);
  248. q6asm_write_nolock(prtd->audio_client,
  249. prtd->pcm_count,
  250. 0, 0, NO_TIMESTAMP);
  251. } else {
  252. while (atomic_read(&prtd->out_needed)) {
  253. pr_debug("%s:writing %d bytes of buffer to dsp\n",
  254. __func__,
  255. prtd->pcm_count);
  256. q6asm_write_nolock(prtd->audio_client,
  257. prtd->pcm_count,
  258. 0, 0, NO_TIMESTAMP);
  259. atomic_dec(&prtd->out_needed);
  260. wake_up(&the_locks.write_wait);
  261. };
  262. }
  263. atomic_set(&prtd->start, 1);
  264. break;
  265. case ASM_STREAM_CMD_REGISTER_PP_EVENTS:
  266. pr_debug("%s: ASM_STREAM_CMD_REGISTER_PP_EVENTS:",
  267. __func__);
  268. break;
  269. default:
  270. pr_debug("%s:Payload = [0x%x]stat[0x%x]\n",
  271. __func__, payload[0], payload[1]);
  272. break;
  273. }
  274. }
  275. break;
  276. case RESET_EVENTS:
  277. pr_debug("%s RESET_EVENTS\n", __func__);
  278. prtd->pcm_irq_pos += prtd->pcm_count;
  279. atomic_inc(&prtd->out_count);
  280. atomic_inc(&prtd->in_count);
  281. prtd->reset_event = true;
  282. if (atomic_read(&prtd->start))
  283. snd_pcm_period_elapsed(substream);
  284. wake_up(&the_locks.eos_wait);
  285. wake_up(&the_locks.write_wait);
  286. wake_up(&the_locks.read_wait);
  287. break;
  288. default:
  289. pr_debug("Not Supported Event opcode[0x%x]\n", opcode);
  290. break;
  291. }
  292. }
  293. static int msm_pcm_playback_prepare(struct snd_pcm_substream *substream)
  294. {
  295. struct snd_pcm_runtime *runtime = substream->runtime;
  296. struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
  297. struct snd_soc_component *component =
  298. snd_soc_rtdcom_lookup(soc_prtd, DRV_NAME);
  299. struct msm_audio *prtd = runtime->private_data;
  300. struct msm_plat_data *pdata;
  301. struct snd_pcm_hw_params *params;
  302. int ret;
  303. uint32_t fmt_type = FORMAT_LINEAR_PCM;
  304. uint16_t bits_per_sample;
  305. uint16_t sample_word_size;
  306. if (!component) {
  307. pr_err("%s: component is NULL\n", __func__);
  308. return -EINVAL;
  309. }
  310. pdata = (struct msm_plat_data *)
  311. dev_get_drvdata(component->dev);
  312. if (!pdata) {
  313. pr_err("%s: platform data not populated\n", __func__);
  314. return -EINVAL;
  315. }
  316. if (!prtd || !prtd->audio_client) {
  317. pr_err("%s: private data null or audio client freed\n",
  318. __func__);
  319. return -EINVAL;
  320. }
  321. params = &soc_prtd->dpcm[substream->stream].hw_params;
  322. pr_debug("%s\n", __func__);
  323. prtd->pcm_size = snd_pcm_lib_buffer_bytes(substream);
  324. prtd->pcm_count = snd_pcm_lib_period_bytes(substream);
  325. prtd->pcm_irq_pos = 0;
  326. /* rate and channels are sent to audio driver */
  327. prtd->samp_rate = runtime->rate;
  328. prtd->channel_mode = runtime->channels;
  329. if (prtd->enabled)
  330. return 0;
  331. prtd->audio_client->perf_mode = pdata->perf_mode;
  332. pr_debug("%s: perf: %x\n", __func__, pdata->perf_mode);
  333. switch (params_format(params)) {
  334. case SNDRV_PCM_FORMAT_S32_LE:
  335. bits_per_sample = 32;
  336. sample_word_size = 32;
  337. break;
  338. case SNDRV_PCM_FORMAT_S24_LE:
  339. bits_per_sample = 24;
  340. sample_word_size = 32;
  341. break;
  342. case SNDRV_PCM_FORMAT_S24_3LE:
  343. bits_per_sample = 24;
  344. sample_word_size = 24;
  345. break;
  346. case SNDRV_PCM_FORMAT_S16_LE:
  347. default:
  348. bits_per_sample = 16;
  349. sample_word_size = 16;
  350. break;
  351. }
  352. if (prtd->compress_enable) {
  353. fmt_type = FORMAT_GEN_COMPR;
  354. pr_debug("%s: Compressed enabled!\n", __func__);
  355. ret = q6asm_open_write_compressed(prtd->audio_client, fmt_type,
  356. COMPRESSED_PASSTHROUGH_GEN);
  357. if (ret < 0) {
  358. pr_err("%s: q6asm_open_write_compressed failed (%d)\n",
  359. __func__, ret);
  360. q6asm_audio_client_free(prtd->audio_client);
  361. prtd->audio_client = NULL;
  362. return -ENOMEM;
  363. }
  364. } else {
  365. if ((q6core_get_avcs_api_version_per_service(
  366. APRV2_IDS_SERVICE_ID_ADSP_ASM_V) >=
  367. ADSP_ASM_API_VERSION_V2))
  368. ret = q6asm_open_write_v5(prtd->audio_client,
  369. fmt_type, bits_per_sample);
  370. else
  371. ret = q6asm_open_write_v4(prtd->audio_client,
  372. fmt_type, bits_per_sample);
  373. if (ret < 0) {
  374. pr_err("%s: q6asm_open_write failed (%d)\n",
  375. __func__, ret);
  376. q6asm_audio_client_free(prtd->audio_client);
  377. prtd->audio_client = NULL;
  378. return -ENOMEM;
  379. }
  380. ret = q6asm_send_cal(prtd->audio_client);
  381. if (ret < 0)
  382. pr_debug("%s : Send cal failed : %d", __func__, ret);
  383. }
  384. pr_debug("%s: session ID %d\n", __func__,
  385. prtd->audio_client->session);
  386. prtd->session_id = prtd->audio_client->session;
  387. if (prtd->compress_enable) {
  388. ret = msm_pcm_routing_reg_phy_compr_stream(
  389. soc_prtd->dai_link->id,
  390. prtd->audio_client->perf_mode,
  391. prtd->session_id,
  392. SNDRV_PCM_STREAM_PLAYBACK,
  393. COMPRESSED_PASSTHROUGH_GEN);
  394. } else {
  395. ret = msm_pcm_routing_reg_phy_stream(soc_prtd->dai_link->id,
  396. prtd->audio_client->perf_mode,
  397. prtd->session_id, substream->stream);
  398. }
  399. if (ret) {
  400. pr_err("%s: stream reg failed ret:%d\n", __func__, ret);
  401. return ret;
  402. }
  403. if (prtd->compress_enable) {
  404. ret = q6asm_media_format_block_gen_compr(
  405. prtd->audio_client, runtime->rate,
  406. runtime->channels, !prtd->set_channel_map,
  407. prtd->channel_map, bits_per_sample);
  408. } else {
  409. if ((q6core_get_avcs_api_version_per_service(
  410. APRV2_IDS_SERVICE_ID_ADSP_ASM_V) >=
  411. ADSP_ASM_API_VERSION_V2)) {
  412. ret = q6asm_media_format_block_multi_ch_pcm_v5(
  413. prtd->audio_client, runtime->rate,
  414. runtime->channels, !prtd->set_channel_map,
  415. prtd->channel_map, bits_per_sample,
  416. sample_word_size, ASM_LITTLE_ENDIAN,
  417. DEFAULT_QF);
  418. } else {
  419. ret = q6asm_media_format_block_multi_ch_pcm_v4(
  420. prtd->audio_client, runtime->rate,
  421. runtime->channels, !prtd->set_channel_map,
  422. prtd->channel_map, bits_per_sample,
  423. sample_word_size, ASM_LITTLE_ENDIAN,
  424. DEFAULT_QF);
  425. }
  426. }
  427. if (ret < 0)
  428. pr_info("%s: CMD Format block failed\n", __func__);
  429. atomic_set(&prtd->out_count, runtime->periods);
  430. prtd->enabled = 1;
  431. prtd->cmd_pending = 0;
  432. prtd->cmd_interrupt = 0;
  433. return 0;
  434. }
  435. static int msm_pcm_capture_prepare(struct snd_pcm_substream *substream)
  436. {
  437. struct snd_pcm_runtime *runtime = substream->runtime;
  438. struct msm_audio *prtd = runtime->private_data;
  439. struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
  440. struct snd_soc_component *component =
  441. snd_soc_rtdcom_lookup(soc_prtd, DRV_NAME);
  442. struct msm_plat_data *pdata;
  443. struct snd_pcm_hw_params *params;
  444. struct msm_pcm_routing_evt event;
  445. int ret = 0;
  446. int i = 0;
  447. uint16_t bits_per_sample = 16;
  448. uint16_t sample_word_size;
  449. if (!component) {
  450. pr_err("%s: component is NULL\n", __func__);
  451. return -EINVAL;
  452. }
  453. pdata = (struct msm_plat_data *)
  454. dev_get_drvdata(component->dev);
  455. if (!pdata) {
  456. pr_err("%s: platform data not populated\n", __func__);
  457. return -EINVAL;
  458. }
  459. if (!prtd || !prtd->audio_client) {
  460. pr_err("%s: private data null or audio client freed\n",
  461. __func__);
  462. return -EINVAL;
  463. }
  464. if (prtd->enabled == IDLE) {
  465. pr_debug("%s:perf_mode=%d periods=%d\n", __func__,
  466. pdata->perf_mode, runtime->periods);
  467. params = &soc_prtd->dpcm[substream->stream].hw_params;
  468. if ((params_format(params) == SNDRV_PCM_FORMAT_S24_LE) ||
  469. (params_format(params) == SNDRV_PCM_FORMAT_S24_3LE))
  470. bits_per_sample = 24;
  471. else if (params_format(params) == SNDRV_PCM_FORMAT_S32_LE)
  472. bits_per_sample = 32;
  473. /* ULL mode is not supported in capture path */
  474. if (pdata->perf_mode == LEGACY_PCM_MODE)
  475. prtd->audio_client->perf_mode = LEGACY_PCM_MODE;
  476. else
  477. prtd->audio_client->perf_mode = LOW_LATENCY_PCM_MODE;
  478. pr_debug("%s Opening %d-ch PCM read stream, perf_mode %d\n",
  479. __func__, params_channels(params),
  480. prtd->audio_client->perf_mode);
  481. if ((q6core_get_avcs_api_version_per_service(
  482. APRV2_IDS_SERVICE_ID_ADSP_ASM_V) >=
  483. ADSP_ASM_API_VERSION_V2))
  484. ret = q6asm_open_read_v5(prtd->audio_client,
  485. FORMAT_LINEAR_PCM,
  486. bits_per_sample, false, ENC_CFG_ID_NONE);
  487. else
  488. ret = q6asm_open_read_v4(prtd->audio_client,
  489. FORMAT_LINEAR_PCM,
  490. bits_per_sample, false, ENC_CFG_ID_NONE);
  491. if (ret < 0) {
  492. pr_err("%s: q6asm_open_read failed\n", __func__);
  493. q6asm_audio_client_free(prtd->audio_client);
  494. prtd->audio_client = NULL;
  495. return -ENOMEM;
  496. }
  497. ret = q6asm_send_cal(prtd->audio_client);
  498. if (ret < 0)
  499. pr_debug("%s : Send cal failed : %d", __func__, ret);
  500. pr_debug("%s: session ID %d\n",
  501. __func__, prtd->audio_client->session);
  502. prtd->session_id = prtd->audio_client->session;
  503. event.event_func = msm_pcm_route_event_handler;
  504. event.priv_data = (void *) prtd;
  505. ret = msm_pcm_routing_reg_phy_stream_v2(
  506. soc_prtd->dai_link->id,
  507. prtd->audio_client->perf_mode,
  508. prtd->session_id, substream->stream,
  509. event);
  510. if (ret) {
  511. pr_err("%s: stream reg failed ret:%d\n", __func__, ret);
  512. return ret;
  513. }
  514. }
  515. prtd->pcm_size = snd_pcm_lib_buffer_bytes(substream);
  516. prtd->pcm_count = snd_pcm_lib_period_bytes(substream);
  517. prtd->pcm_irq_pos = 0;
  518. /* rate and channels are sent to audio driver */
  519. prtd->samp_rate = runtime->rate;
  520. prtd->channel_mode = runtime->channels;
  521. if (prtd->enabled == IDLE || prtd->enabled == STOPPED) {
  522. for (i = 0; i < runtime->periods; i++)
  523. q6asm_read(prtd->audio_client);
  524. prtd->periods = runtime->periods;
  525. }
  526. if (prtd->enabled != IDLE)
  527. return 0;
  528. switch (runtime->format) {
  529. case SNDRV_PCM_FORMAT_S32_LE:
  530. bits_per_sample = 32;
  531. sample_word_size = 32;
  532. break;
  533. case SNDRV_PCM_FORMAT_S24_LE:
  534. bits_per_sample = 24;
  535. sample_word_size = 32;
  536. break;
  537. case SNDRV_PCM_FORMAT_S24_3LE:
  538. bits_per_sample = 24;
  539. sample_word_size = 24;
  540. break;
  541. case SNDRV_PCM_FORMAT_S16_LE:
  542. default:
  543. bits_per_sample = 16;
  544. sample_word_size = 16;
  545. break;
  546. }
  547. pr_debug("%s: Samp_rate = %d Channel = %d bit width = %d, word size = %d\n",
  548. __func__, prtd->samp_rate, prtd->channel_mode,
  549. bits_per_sample, sample_word_size);
  550. if ((q6core_get_avcs_api_version_per_service(
  551. APRV2_IDS_SERVICE_ID_ADSP_ASM_V) >=
  552. ADSP_ASM_API_VERSION_V2))
  553. ret = q6asm_enc_cfg_blk_pcm_format_support_v5(
  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. else
  562. ret = q6asm_enc_cfg_blk_pcm_format_support_v4(
  563. prtd->audio_client,
  564. prtd->samp_rate,
  565. prtd->channel_mode,
  566. bits_per_sample,
  567. sample_word_size,
  568. ASM_LITTLE_ENDIAN,
  569. DEFAULT_QF);
  570. if (ret < 0)
  571. pr_debug("%s: cmd cfg pcm was block failed", __func__);
  572. prtd->enabled = RUNNING;
  573. return ret;
  574. }
  575. static int msm_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  576. {
  577. int ret = 0;
  578. struct snd_pcm_runtime *runtime = substream->runtime;
  579. struct msm_audio *prtd = runtime->private_data;
  580. switch (cmd) {
  581. case SNDRV_PCM_TRIGGER_START:
  582. case SNDRV_PCM_TRIGGER_RESUME:
  583. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  584. pr_debug("%s: Trigger start\n", __func__);
  585. ret = q6asm_run_nowait(prtd->audio_client, 0, 0, 0);
  586. break;
  587. case SNDRV_PCM_TRIGGER_STOP:
  588. pr_debug("SNDRV_PCM_TRIGGER_STOP\n");
  589. atomic_set(&prtd->start, 0);
  590. if (substream->stream != SNDRV_PCM_STREAM_PLAYBACK) {
  591. prtd->enabled = STOPPED;
  592. ret = q6asm_cmd_nowait(prtd->audio_client, CMD_PAUSE);
  593. break;
  594. }
  595. /* pending CMD_EOS isn't expected */
  596. WARN_ON_ONCE(test_bit(CMD_EOS, &prtd->cmd_pending));
  597. set_bit(CMD_EOS, &prtd->cmd_pending);
  598. ret = q6asm_cmd_nowait(prtd->audio_client, CMD_EOS);
  599. if (ret)
  600. clear_bit(CMD_EOS, &prtd->cmd_pending);
  601. break;
  602. case SNDRV_PCM_TRIGGER_SUSPEND:
  603. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  604. pr_debug("SNDRV_PCM_TRIGGER_PAUSE\n");
  605. ret = q6asm_cmd_nowait(prtd->audio_client, CMD_PAUSE);
  606. atomic_set(&prtd->start, 0);
  607. break;
  608. default:
  609. ret = -EINVAL;
  610. break;
  611. }
  612. return ret;
  613. }
  614. static int msm_pcm_open(struct snd_pcm_substream *substream)
  615. {
  616. struct snd_pcm_runtime *runtime = substream->runtime;
  617. struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
  618. struct snd_soc_component *component =
  619. snd_soc_rtdcom_lookup(soc_prtd, DRV_NAME);
  620. struct msm_audio *prtd;
  621. struct msm_plat_data *pdata;
  622. enum apr_subsys_state subsys_state;
  623. int ret = 0;
  624. if (!component) {
  625. pr_err("%s: component is NULL\n", __func__);
  626. return -EINVAL;
  627. }
  628. pdata = (struct msm_plat_data *)
  629. dev_get_drvdata(component->dev);
  630. if (!pdata) {
  631. pr_err("%s: platform data not populated\n", __func__);
  632. return -EINVAL;
  633. }
  634. subsys_state = apr_get_subsys_state();
  635. if (subsys_state == APR_SUBSYS_DOWN) {
  636. pr_debug("%s: adsp is down\n", __func__);
  637. return -ENETRESET;
  638. }
  639. prtd = kzalloc(sizeof(struct msm_audio), GFP_KERNEL);
  640. if (prtd == NULL)
  641. return -ENOMEM;
  642. prtd->substream = substream;
  643. prtd->audio_client = q6asm_audio_client_alloc(
  644. (app_cb)event_handler, prtd);
  645. if (!prtd->audio_client) {
  646. pr_info("%s: Could not allocate memory\n", __func__);
  647. kfree(prtd);
  648. prtd = NULL;
  649. return -ENOMEM;
  650. }
  651. prtd->audio_client->dev = component->dev;
  652. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  653. runtime->hw = msm_pcm_hardware_playback;
  654. /* Capture path */
  655. else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  656. runtime->hw = msm_pcm_hardware_capture;
  657. else {
  658. pr_err("Invalid Stream type %d\n", substream->stream);
  659. return -EINVAL;
  660. }
  661. ret = snd_pcm_hw_constraint_list(runtime, 0,
  662. SNDRV_PCM_HW_PARAM_RATE,
  663. &constraints_sample_rates);
  664. if (ret < 0)
  665. pr_info("snd_pcm_hw_constraint_list failed\n");
  666. /* Ensure that buffer size is a multiple of period size */
  667. ret = snd_pcm_hw_constraint_integer(runtime,
  668. SNDRV_PCM_HW_PARAM_PERIODS);
  669. if (ret < 0)
  670. pr_info("snd_pcm_hw_constraint_integer failed\n");
  671. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  672. ret = snd_pcm_hw_constraint_minmax(runtime,
  673. SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
  674. PLAYBACK_MIN_NUM_PERIODS * PLAYBACK_MIN_PERIOD_SIZE,
  675. PLAYBACK_MAX_NUM_PERIODS * PLAYBACK_MAX_PERIOD_SIZE);
  676. if (ret < 0) {
  677. pr_err("constraint for buffer bytes min max ret = %d\n",
  678. ret);
  679. }
  680. }
  681. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
  682. ret = snd_pcm_hw_constraint_minmax(runtime,
  683. SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
  684. CAPTURE_MIN_NUM_PERIODS * CAPTURE_MIN_PERIOD_SIZE,
  685. CAPTURE_MAX_NUM_PERIODS * CAPTURE_MAX_PERIOD_SIZE);
  686. if (ret < 0) {
  687. pr_err("constraint for buffer bytes min max ret = %d\n",
  688. ret);
  689. }
  690. }
  691. ret = snd_pcm_hw_constraint_step(runtime, 0,
  692. SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 32);
  693. if (ret < 0) {
  694. pr_err("constraint for period bytes step ret = %d\n",
  695. ret);
  696. }
  697. ret = snd_pcm_hw_constraint_step(runtime, 0,
  698. SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 32);
  699. if (ret < 0) {
  700. pr_err("constraint for buffer bytes step ret = %d\n",
  701. ret);
  702. }
  703. prtd->enabled = IDLE;
  704. prtd->dsp_cnt = 0;
  705. prtd->set_channel_map = false;
  706. prtd->reset_event = false;
  707. runtime->private_data = prtd;
  708. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  709. msm_adsp_init_mixer_ctl_pp_event_queue(soc_prtd);
  710. /* Vote to update the Rx thread priority to RT Thread for playback */
  711. if ((substream->stream == SNDRV_PCM_STREAM_PLAYBACK) &&
  712. (pdata->perf_mode == LOW_LATENCY_PCM_MODE))
  713. apr_start_rx_rt(prtd->audio_client->apr);
  714. return 0;
  715. }
  716. static int msm_pcm_playback_copy(struct snd_pcm_substream *substream, int a,
  717. unsigned long hwoff, void __user *buf, unsigned long fbytes)
  718. {
  719. int ret = 0;
  720. int xfer = 0;
  721. char *bufptr = NULL;
  722. void *data = NULL;
  723. uint32_t idx = 0;
  724. uint32_t size = 0;
  725. uint32_t retries = 0;
  726. struct snd_pcm_runtime *runtime = substream->runtime;
  727. struct msm_audio *prtd = runtime->private_data;
  728. pr_debug("%s: prtd->out_count = %d\n",
  729. __func__, atomic_read(&prtd->out_count));
  730. while ((fbytes > 0) && (retries < MAX_PB_COPY_RETRIES)) {
  731. if (prtd->reset_event) {
  732. pr_err("%s: In SSR return ENETRESET before wait\n",
  733. __func__);
  734. return -ENETRESET;
  735. }
  736. ret = wait_event_timeout(the_locks.write_wait,
  737. (atomic_read(&prtd->out_count)),
  738. msecs_to_jiffies(TIMEOUT_MS));
  739. if (!ret) {
  740. pr_err("%s: wait_event_timeout failed\n", __func__);
  741. ret = -ETIMEDOUT;
  742. goto fail;
  743. }
  744. ret = 0;
  745. if (prtd->reset_event) {
  746. pr_err("%s: In SSR return ENETRESET after wait\n",
  747. __func__);
  748. return -ENETRESET;
  749. }
  750. if (!atomic_read(&prtd->out_count)) {
  751. pr_err("%s: pcm stopped out_count 0\n", __func__);
  752. return 0;
  753. }
  754. data = q6asm_is_cpu_buf_avail(IN, prtd->audio_client, &size,
  755. &idx);
  756. if (data == NULL) {
  757. retries++;
  758. continue;
  759. } else {
  760. retries = 0;
  761. }
  762. if (fbytes > size)
  763. xfer = size;
  764. else
  765. xfer = fbytes;
  766. bufptr = data;
  767. if (bufptr) {
  768. pr_debug("%s:fbytes =%lu: xfer=%d size=%d\n",
  769. __func__, fbytes, xfer, size);
  770. if (copy_from_user(bufptr, buf, xfer)) {
  771. ret = -EFAULT;
  772. pr_err("%s: copy_from_user failed\n",
  773. __func__);
  774. q6asm_cpu_buf_release(IN, prtd->audio_client);
  775. goto fail;
  776. }
  777. buf += xfer;
  778. fbytes -= xfer;
  779. pr_debug("%s:fbytes = %lu: xfer=%d\n", __func__,
  780. fbytes, xfer);
  781. if (atomic_read(&prtd->start)) {
  782. pr_debug("%s:writing %d bytes of buffer to dsp\n",
  783. __func__, xfer);
  784. ret = q6asm_write(prtd->audio_client, xfer,
  785. 0, 0, NO_TIMESTAMP);
  786. if (ret < 0) {
  787. ret = -EFAULT;
  788. q6asm_cpu_buf_release(IN,
  789. prtd->audio_client);
  790. goto fail;
  791. }
  792. } else
  793. atomic_inc(&prtd->out_needed);
  794. atomic_dec(&prtd->out_count);
  795. }
  796. }
  797. fail:
  798. if (retries >= MAX_PB_COPY_RETRIES)
  799. ret = -ENOMEM;
  800. return ret;
  801. }
  802. static int msm_pcm_playback_close(struct snd_pcm_substream *substream)
  803. {
  804. struct snd_pcm_runtime *runtime = substream->runtime;
  805. struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
  806. struct msm_audio *prtd = runtime->private_data;
  807. struct snd_soc_component *component =
  808. snd_soc_rtdcom_lookup(soc_prtd, DRV_NAME);
  809. struct msm_plat_data *pdata;
  810. uint32_t timeout;
  811. int dir = 0;
  812. int ret = 0;
  813. pr_debug("%s: cmd_pending 0x%lx\n", __func__, prtd->cmd_pending);
  814. if (!component) {
  815. pr_err("%s: component is NULL\n", __func__);
  816. return -EINVAL;
  817. }
  818. pdata = (struct msm_plat_data *) dev_get_drvdata(component->dev);
  819. if (!pdata) {
  820. pr_err("%s: platform data is NULL\n", __func__);
  821. return -EINVAL;
  822. }
  823. mutex_lock(&pdata->lock);
  824. if (prtd->audio_client) {
  825. dir = IN;
  826. /*
  827. * Unvote to downgrade the Rx thread priority from
  828. * RT Thread for Low-Latency use case.
  829. */
  830. if (pdata) {
  831. if (pdata->perf_mode == LOW_LATENCY_PCM_MODE)
  832. apr_end_rx_rt(prtd->audio_client->apr);
  833. }
  834. /* determine timeout length */
  835. if (runtime->frame_bits == 0 || runtime->rate == 0) {
  836. timeout = CMD_EOS_MIN_TIMEOUT_LENGTH;
  837. } else {
  838. timeout = (runtime->period_size *
  839. CMD_EOS_TIMEOUT_MULTIPLIER) /
  840. ((runtime->frame_bits / 8) *
  841. runtime->rate);
  842. if (timeout < CMD_EOS_MIN_TIMEOUT_LENGTH)
  843. timeout = CMD_EOS_MIN_TIMEOUT_LENGTH;
  844. }
  845. pr_debug("%s: CMD_EOS timeout is %d\n", __func__, timeout);
  846. ret = wait_event_timeout(the_locks.eos_wait,
  847. !test_bit(CMD_EOS, &prtd->cmd_pending),
  848. timeout);
  849. if (!ret)
  850. pr_err("%s: CMD_EOS failed, cmd_pending 0x%lx\n",
  851. __func__, prtd->cmd_pending);
  852. q6asm_cmd(prtd->audio_client, CMD_CLOSE);
  853. q6asm_audio_client_buf_free_contiguous(dir,
  854. prtd->audio_client);
  855. q6asm_audio_client_free(prtd->audio_client);
  856. }
  857. msm_pcm_routing_dereg_phy_stream(soc_prtd->dai_link->id,
  858. SNDRV_PCM_STREAM_PLAYBACK);
  859. msm_adsp_clean_mixer_ctl_pp_event_queue(soc_prtd);
  860. kfree(prtd);
  861. runtime->private_data = NULL;
  862. mutex_unlock(&pdata->lock);
  863. return 0;
  864. }
  865. static int msm_pcm_capture_copy(struct snd_pcm_substream *substream,
  866. int channel, unsigned long hwoff, void __user *buf,
  867. unsigned long fbytes)
  868. {
  869. int ret = 0;
  870. int xfer;
  871. char *bufptr;
  872. void *data = NULL;
  873. uint32_t idx = 0;
  874. uint32_t size = 0;
  875. uint32_t offset = 0;
  876. struct snd_pcm_runtime *runtime = substream->runtime;
  877. struct msm_audio *prtd = substream->runtime->private_data;
  878. pr_debug("%s\n", __func__);
  879. pr_debug("appl_ptr %d\n", (int)runtime->control->appl_ptr);
  880. pr_debug("hw_ptr %d\n", (int)runtime->status->hw_ptr);
  881. pr_debug("avail_min %d\n", (int)runtime->control->avail_min);
  882. if (prtd->reset_event) {
  883. pr_err("%s: In SSR return ENETRESET before wait\n", __func__);
  884. return -ENETRESET;
  885. }
  886. ret = wait_event_timeout(the_locks.read_wait,
  887. (atomic_read(&prtd->in_count)),
  888. msecs_to_jiffies(TIMEOUT_MS));
  889. if (!ret) {
  890. pr_debug("%s: wait_event_timeout failed\n", __func__);
  891. goto fail;
  892. }
  893. if (prtd->reset_event) {
  894. pr_err("%s: In SSR return ENETRESET after wait\n", __func__);
  895. return -ENETRESET;
  896. }
  897. if (!atomic_read(&prtd->in_count)) {
  898. pr_debug("%s: pcm stopped in_count 0\n", __func__);
  899. return 0;
  900. }
  901. pr_debug("Checking if valid buffer is available...%pK\n",
  902. data);
  903. data = q6asm_is_cpu_buf_avail(OUT, prtd->audio_client, &size, &idx);
  904. bufptr = data;
  905. pr_debug("Size = %d\n", size);
  906. pr_debug("fbytes = %lu\n", fbytes);
  907. pr_debug("idx = %d\n", idx);
  908. if (bufptr) {
  909. xfer = fbytes;
  910. if (xfer > size)
  911. xfer = size;
  912. offset = prtd->in_frame_info[idx].offset;
  913. pr_debug("Offset value = %d\n", offset);
  914. if (copy_to_user(buf, bufptr+offset, xfer)) {
  915. pr_err("Failed to copy buf to user\n");
  916. ret = -EFAULT;
  917. q6asm_cpu_buf_release(OUT, prtd->audio_client);
  918. goto fail;
  919. }
  920. fbytes -= xfer;
  921. size -= xfer;
  922. prtd->in_frame_info[idx].offset += xfer;
  923. pr_debug("%s:fbytes = %lu: size=%d: xfer=%d\n",
  924. __func__, fbytes, size, xfer);
  925. pr_debug(" Sending next buffer to dsp\n");
  926. memset(&prtd->in_frame_info[idx], 0,
  927. sizeof(struct msm_audio_in_frame_info));
  928. atomic_dec(&prtd->in_count);
  929. ret = q6asm_read(prtd->audio_client);
  930. if (ret < 0) {
  931. pr_err("q6asm read failed\n");
  932. ret = -EFAULT;
  933. q6asm_cpu_buf_release(OUT, prtd->audio_client);
  934. goto fail;
  935. }
  936. } else
  937. pr_err("No valid buffer\n");
  938. pr_debug("Returning from capture_copy... %d\n", ret);
  939. fail:
  940. return ret;
  941. }
  942. static int msm_pcm_capture_close(struct snd_pcm_substream *substream)
  943. {
  944. struct snd_pcm_runtime *runtime = substream->runtime;
  945. struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
  946. struct msm_audio *prtd = runtime->private_data;
  947. struct snd_soc_component *component =
  948. snd_soc_rtdcom_lookup(soc_prtd, DRV_NAME);
  949. struct msm_plat_data *pdata;
  950. int dir = OUT;
  951. pr_debug("%s\n", __func__);
  952. if (!component) {
  953. pr_err("%s: component is NULL\n", __func__);
  954. return -EINVAL;
  955. }
  956. pdata = (struct msm_plat_data *) dev_get_drvdata(component->dev);
  957. if (!pdata) {
  958. pr_err("%s: platform data is NULL\n", __func__);
  959. return -EINVAL;
  960. }
  961. mutex_lock(&pdata->lock);
  962. if (prtd->audio_client) {
  963. q6asm_cmd(prtd->audio_client, CMD_CLOSE);
  964. q6asm_audio_client_buf_free_contiguous(dir,
  965. prtd->audio_client);
  966. q6asm_audio_client_free(prtd->audio_client);
  967. }
  968. msm_pcm_routing_dereg_phy_stream(soc_prtd->dai_link->id,
  969. SNDRV_PCM_STREAM_CAPTURE);
  970. kfree(prtd);
  971. runtime->private_data = NULL;
  972. mutex_unlock(&pdata->lock);
  973. return 0;
  974. }
  975. static int msm_pcm_copy(struct snd_pcm_substream *substream, int a,
  976. unsigned long hwoff, void __user *buf, unsigned long fbytes)
  977. {
  978. int ret = 0;
  979. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  980. ret = msm_pcm_playback_copy(substream, a, hwoff, buf, fbytes);
  981. else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  982. ret = msm_pcm_capture_copy(substream, a, hwoff, buf, fbytes);
  983. return ret;
  984. }
  985. static int msm_pcm_close(struct snd_pcm_substream *substream)
  986. {
  987. int ret = 0;
  988. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  989. ret = msm_pcm_playback_close(substream);
  990. else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  991. ret = msm_pcm_capture_close(substream);
  992. return ret;
  993. }
  994. static int msm_pcm_prepare(struct snd_pcm_substream *substream)
  995. {
  996. int ret = 0;
  997. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  998. ret = msm_pcm_playback_prepare(substream);
  999. else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  1000. ret = msm_pcm_capture_prepare(substream);
  1001. return ret;
  1002. }
  1003. static snd_pcm_uframes_t msm_pcm_pointer(struct snd_pcm_substream *substream)
  1004. {
  1005. struct snd_pcm_runtime *runtime = substream->runtime;
  1006. struct msm_audio *prtd = runtime->private_data;
  1007. if (prtd->pcm_irq_pos >= prtd->pcm_size)
  1008. prtd->pcm_irq_pos = 0;
  1009. pr_debug("pcm_irq_pos = %d\n", prtd->pcm_irq_pos);
  1010. return bytes_to_frames(runtime, (prtd->pcm_irq_pos));
  1011. }
  1012. static int msm_pcm_mmap(struct snd_pcm_substream *substream,
  1013. struct vm_area_struct *vma)
  1014. {
  1015. struct snd_pcm_runtime *runtime = substream->runtime;
  1016. struct msm_audio *prtd = runtime->private_data;
  1017. struct audio_client *ac = prtd->audio_client;
  1018. struct audio_port_data *apd = ac->port;
  1019. struct audio_buffer *ab;
  1020. int dir = -1;
  1021. prtd->mmap_flag = 1;
  1022. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  1023. dir = IN;
  1024. else
  1025. dir = OUT;
  1026. ab = &(apd[dir].buf[0]);
  1027. return msm_audio_ion_mmap(ab, vma);
  1028. }
  1029. static int msm_pcm_hw_params(struct snd_pcm_substream *substream,
  1030. struct snd_pcm_hw_params *params)
  1031. {
  1032. struct snd_pcm_runtime *runtime = substream->runtime;
  1033. struct msm_audio *prtd = runtime->private_data;
  1034. struct snd_dma_buffer *dma_buf = &substream->dma_buffer;
  1035. struct audio_buffer *buf;
  1036. int dir, ret;
  1037. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  1038. dir = IN;
  1039. else
  1040. dir = OUT;
  1041. ret = q6asm_audio_client_buf_alloc_contiguous(dir,
  1042. prtd->audio_client,
  1043. (params_buffer_bytes(params) / params_periods(params)),
  1044. params_periods(params));
  1045. if (ret < 0) {
  1046. pr_err("Audio Start: Buffer Allocation failed rc = %d\n",
  1047. ret);
  1048. return -ENOMEM;
  1049. }
  1050. buf = prtd->audio_client->port[dir].buf;
  1051. if (buf == NULL || buf[0].data == NULL)
  1052. return -ENOMEM;
  1053. pr_debug("%s:buf = %pK\n", __func__, buf);
  1054. dma_buf->dev.type = SNDRV_DMA_TYPE_DEV;
  1055. dma_buf->dev.dev = substream->pcm->card->dev;
  1056. dma_buf->private_data = NULL;
  1057. dma_buf->area = buf[0].data;
  1058. dma_buf->addr = buf[0].phys;
  1059. dma_buf->bytes = params_buffer_bytes(params);
  1060. if (!dma_buf->area)
  1061. return -ENOMEM;
  1062. snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
  1063. return 0;
  1064. }
  1065. static int msm_pcm_ioctl(struct snd_pcm_substream *substream,
  1066. unsigned int cmd, void __user *arg)
  1067. {
  1068. struct msm_audio *prtd = NULL;
  1069. struct snd_soc_pcm_runtime *rtd = NULL;
  1070. uint64_t ses_time = 0, abs_time = 0;
  1071. int64_t av_offset = 0;
  1072. int32_t clock_id = -EINVAL;
  1073. int rc = 0;
  1074. struct snd_pcm_prsnt_position userarg;
  1075. if (!substream || !substream->private_data) {
  1076. pr_err("%s: Invalid %s\n", __func__,
  1077. (!substream) ? "substream" : "private_data");
  1078. return -EINVAL;
  1079. }
  1080. if (!substream->runtime) {
  1081. pr_err("%s substream runtime not found\n", __func__);
  1082. return -EINVAL;
  1083. }
  1084. prtd = substream->runtime->private_data;
  1085. if (!prtd) {
  1086. pr_err("%s prtd is null.\n", __func__);
  1087. return -EINVAL;
  1088. }
  1089. rtd = substream->private_data;
  1090. switch (cmd) {
  1091. case SNDRV_PCM_IOCTL_DSP_POSITION:
  1092. dev_dbg(rtd->dev, "%s: SNDRV_PCM_DSP_POSITION", __func__);
  1093. if (!arg) {
  1094. dev_err(rtd->dev, "%s: Invalid params DSP_POSITION\n",
  1095. __func__);
  1096. rc = -EINVAL;
  1097. goto done;
  1098. }
  1099. memset(&userarg, 0, sizeof(userarg));
  1100. if (copy_from_user(&userarg, arg, sizeof(userarg))) {
  1101. dev_err(rtd->dev, "%s: err copyuser DSP_POSITION\n",
  1102. __func__);
  1103. rc = -EFAULT;
  1104. goto done;
  1105. }
  1106. clock_id = userarg.clock_id;
  1107. rc = q6asm_get_session_time_v2(prtd->audio_client, &ses_time,
  1108. &abs_time);
  1109. if (rc) {
  1110. pr_err("%s: q6asm_get_session_time_v2 failed, rc=%d\n",
  1111. __func__, rc);
  1112. goto done;
  1113. }
  1114. userarg.frames = div64_u64((ses_time * prtd->samp_rate),
  1115. 1000000);
  1116. rc = avcs_core_query_timer_offset(&av_offset, clock_id);
  1117. if (rc) {
  1118. pr_err("%s: avcs offset query failed, rc=%d\n",
  1119. __func__, rc);
  1120. goto done;
  1121. }
  1122. userarg.timestamp = abs_time + av_offset;
  1123. if (copy_to_user(arg, &userarg, sizeof(userarg))) {
  1124. dev_err(rtd->dev, "%s: err copy to user DSP_POSITION\n",
  1125. __func__);
  1126. rc = -EFAULT;
  1127. goto done;
  1128. }
  1129. pr_debug("%s, vals f %lld, t %lld, avoff %lld, abst %lld, sess_time %llu sr %d\n",
  1130. __func__, userarg.frames, userarg.timestamp,
  1131. av_offset, abs_time, ses_time, prtd->samp_rate);
  1132. break;
  1133. default:
  1134. rc = snd_pcm_lib_ioctl(substream, cmd, arg);
  1135. break;
  1136. }
  1137. done:
  1138. return rc;
  1139. }
  1140. #ifdef CONFIG_COMPAT
  1141. #if IS_ENABLED(CONFIG_AUDIO_QGKI)
  1142. static int msm_pcm_compat_ioctl(struct snd_pcm_substream *substream,
  1143. unsigned int cmd, void __user *arg)
  1144. {
  1145. return msm_pcm_ioctl(substream, cmd, arg);
  1146. }
  1147. #endif /* CONFIG_AUDIO_QGKI */
  1148. #else
  1149. #define msm_pcm_compat_ioctl NULL
  1150. #endif
  1151. static const struct snd_pcm_ops msm_pcm_ops = {
  1152. .open = msm_pcm_open,
  1153. .copy_user = msm_pcm_copy,
  1154. .hw_params = msm_pcm_hw_params,
  1155. .close = msm_pcm_close,
  1156. .ioctl = msm_pcm_ioctl,
  1157. #if IS_ENABLED(CONFIG_AUDIO_QGKI)
  1158. .compat_ioctl = msm_pcm_compat_ioctl,
  1159. #endif /* CONFIG_AUDIO_QGKI */
  1160. .prepare = msm_pcm_prepare,
  1161. .trigger = msm_pcm_trigger,
  1162. .pointer = msm_pcm_pointer,
  1163. .mmap = msm_pcm_mmap,
  1164. };
  1165. static int msm_pcm_adsp_stream_cmd_put(struct snd_kcontrol *kcontrol,
  1166. struct snd_ctl_elem_value *ucontrol)
  1167. {
  1168. struct snd_soc_component *component =
  1169. snd_soc_kcontrol_component(kcontrol);
  1170. struct msm_plat_data *pdata = NULL;
  1171. struct snd_pcm_substream *substream;
  1172. struct msm_audio *prtd;
  1173. int ret = 0;
  1174. struct msm_adsp_event_data *event_data = NULL;
  1175. if (!component) {
  1176. pr_err("%s: component is NULL\n", __func__);
  1177. return -EINVAL;
  1178. }
  1179. pdata = dev_get_drvdata(component->dev);
  1180. if (!pdata) {
  1181. pr_err("%s pdata is NULL\n", __func__);
  1182. return -ENODEV;
  1183. }
  1184. mutex_lock(&pdata->lock);
  1185. substream = pdata->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
  1186. if (!substream) {
  1187. pr_err("%s substream not found\n", __func__);
  1188. ret = -EINVAL;
  1189. goto done;
  1190. }
  1191. if (!substream->runtime) {
  1192. pr_err("%s substream runtime not found\n", __func__);
  1193. ret = -EINVAL;
  1194. goto done;
  1195. }
  1196. if (substream->ref_count <= 0) {
  1197. pr_err_ratelimited("%s substream ref_count:%d invalid\n",
  1198. __func__, substream->ref_count);
  1199. ret = -EINVAL;
  1200. goto done;
  1201. }
  1202. prtd = substream->runtime->private_data;
  1203. if (prtd == NULL) {
  1204. pr_err("%s prtd is null.\n", __func__);
  1205. ret = -EINVAL;
  1206. goto done;
  1207. }
  1208. if (prtd->audio_client == NULL) {
  1209. pr_err("%s prtd is null.\n", __func__);
  1210. ret = -EINVAL;
  1211. goto done;
  1212. }
  1213. event_data = (struct msm_adsp_event_data *)ucontrol->value.bytes.data;
  1214. if ((event_data->event_type < ADSP_STREAM_PP_EVENT) ||
  1215. (event_data->event_type >= ADSP_STREAM_EVENT_MAX)) {
  1216. pr_err("%s: invalid event_type=%d",
  1217. __func__, event_data->event_type);
  1218. ret = -EINVAL;
  1219. goto done;
  1220. }
  1221. if (event_data->payload_len > sizeof(ucontrol->value.bytes.data)
  1222. - sizeof(struct msm_adsp_event_data)) {
  1223. pr_err("%s param length=%d exceeds limit",
  1224. __func__, event_data->payload_len);
  1225. ret = -EINVAL;
  1226. goto done;
  1227. }
  1228. ret = q6asm_send_stream_cmd(prtd->audio_client, event_data);
  1229. if (ret < 0)
  1230. pr_err("%s: failed to send stream event cmd, err = %d\n",
  1231. __func__, ret);
  1232. done:
  1233. mutex_unlock(&pdata->lock);
  1234. return ret;
  1235. }
  1236. static int msm_pcm_add_audio_adsp_stream_cmd_control(
  1237. struct snd_soc_pcm_runtime *rtd)
  1238. {
  1239. struct snd_soc_component *component = NULL;
  1240. const char *mixer_ctl_name = DSP_STREAM_CMD;
  1241. const char *deviceNo = "NN";
  1242. char *mixer_str = NULL;
  1243. int ctl_len = 0, ret = 0;
  1244. struct snd_kcontrol_new fe_audio_adsp_stream_cmd_config_control[1] = {
  1245. {
  1246. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1247. .name = "?",
  1248. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  1249. .info = msm_adsp_stream_cmd_info,
  1250. .put = msm_pcm_adsp_stream_cmd_put,
  1251. .private_value = 0,
  1252. }
  1253. };
  1254. if (!rtd) {
  1255. pr_err("%s rtd is NULL\n", __func__);
  1256. ret = -EINVAL;
  1257. goto done;
  1258. }
  1259. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  1260. if (!component) {
  1261. pr_err("%s: component is NULL\n", __func__);
  1262. return -EINVAL;
  1263. }
  1264. ctl_len = strlen(mixer_ctl_name) + 1 + strlen(deviceNo) + 1;
  1265. mixer_str = kzalloc(ctl_len, GFP_KERNEL);
  1266. if (!mixer_str) {
  1267. ret = -ENOMEM;
  1268. goto done;
  1269. }
  1270. snprintf(mixer_str, ctl_len, "%s %d", mixer_ctl_name, rtd->pcm->device);
  1271. fe_audio_adsp_stream_cmd_config_control[0].name = mixer_str;
  1272. fe_audio_adsp_stream_cmd_config_control[0].private_value =
  1273. rtd->dai_link->id;
  1274. pr_debug("Registering new mixer ctl %s\n", mixer_str);
  1275. ret = snd_soc_add_component_controls(component,
  1276. fe_audio_adsp_stream_cmd_config_control,
  1277. ARRAY_SIZE(fe_audio_adsp_stream_cmd_config_control));
  1278. if (ret < 0)
  1279. pr_err("%s: failed add ctl %s. err = %d\n",
  1280. __func__, mixer_str, ret);
  1281. kfree(mixer_str);
  1282. done:
  1283. return ret;
  1284. }
  1285. static int msm_pcm_add_audio_adsp_stream_callback_control(
  1286. struct snd_soc_pcm_runtime *rtd)
  1287. {
  1288. struct snd_soc_component *component = NULL;
  1289. const char *mixer_ctl_name = DSP_STREAM_CALLBACK;
  1290. const char *deviceNo = "NN";
  1291. char *mixer_str = NULL;
  1292. int ctl_len = 0, ret = 0;
  1293. struct snd_kcontrol *kctl;
  1294. struct snd_kcontrol_new fe_audio_adsp_callback_config_control[1] = {
  1295. {
  1296. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1297. .name = "?",
  1298. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  1299. .info = msm_adsp_stream_callback_info,
  1300. .get = msm_adsp_stream_callback_get,
  1301. .private_value = 0,
  1302. }
  1303. };
  1304. if (!rtd) {
  1305. pr_err("%s NULL rtd\n", __func__);
  1306. ret = -EINVAL;
  1307. goto done;
  1308. }
  1309. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  1310. if (!component) {
  1311. pr_err("%s: component is NULL\n", __func__);
  1312. return -EINVAL;
  1313. }
  1314. pr_debug("%s: added new pcm FE with name %s, id %d, cpu dai %s, device no %d\n",
  1315. __func__, rtd->dai_link->name, rtd->dai_link->id,
  1316. rtd->dai_link->cpus->dai_name, rtd->pcm->device);
  1317. ctl_len = strlen(mixer_ctl_name) + 1 + strlen(deviceNo) + 1;
  1318. mixer_str = kzalloc(ctl_len, GFP_KERNEL);
  1319. if (!mixer_str) {
  1320. ret = -ENOMEM;
  1321. goto done;
  1322. }
  1323. snprintf(mixer_str, ctl_len, "%s %d", mixer_ctl_name, rtd->pcm->device);
  1324. fe_audio_adsp_callback_config_control[0].name = mixer_str;
  1325. fe_audio_adsp_callback_config_control[0].private_value =
  1326. rtd->dai_link->id;
  1327. pr_debug("%s: Registering new mixer ctl %s\n", __func__, mixer_str);
  1328. ret = snd_soc_add_component_controls(component,
  1329. fe_audio_adsp_callback_config_control,
  1330. ARRAY_SIZE(fe_audio_adsp_callback_config_control));
  1331. if (ret < 0) {
  1332. pr_err("%s: failed to add ctl %s. err = %d\n",
  1333. __func__, mixer_str, ret);
  1334. ret = -EINVAL;
  1335. goto free_mixer_str;
  1336. }
  1337. kctl = snd_soc_card_get_kcontrol(rtd->card, mixer_str);
  1338. if (!kctl) {
  1339. pr_err("%s: failed to get kctl %s.\n", __func__, mixer_str);
  1340. ret = -EINVAL;
  1341. goto free_mixer_str;
  1342. }
  1343. kctl->private_data = NULL;
  1344. free_mixer_str:
  1345. kfree(mixer_str);
  1346. done:
  1347. return ret;
  1348. }
  1349. #if IS_ENABLED(CONFIG_AUDIO_QGKI)
  1350. static int msm_pcm_set_volume(struct msm_audio *prtd, uint32_t volume)
  1351. {
  1352. int rc = 0;
  1353. if (prtd && prtd->audio_client) {
  1354. pr_debug("%s: channels %d volume 0x%x\n", __func__,
  1355. prtd->channel_mode, volume);
  1356. rc = q6asm_set_volume(prtd->audio_client, volume);
  1357. if (rc < 0) {
  1358. pr_err("%s: Send Volume command failed rc=%d\n",
  1359. __func__, rc);
  1360. }
  1361. }
  1362. return rc;
  1363. }
  1364. static int msm_pcm_volume_ctl_get(struct snd_kcontrol *kcontrol,
  1365. struct snd_ctl_elem_value *ucontrol)
  1366. {
  1367. struct snd_pcm_volume *vol = snd_kcontrol_chip(kcontrol);
  1368. struct msm_plat_data *pdata = NULL;
  1369. struct snd_pcm_substream *substream = NULL;
  1370. struct snd_soc_pcm_runtime *soc_prtd = NULL;
  1371. struct snd_soc_component *component = NULL;
  1372. struct msm_audio *prtd;
  1373. pr_debug("%s\n", __func__);
  1374. if (!vol) {
  1375. pr_err("%s: vol is NULL\n", __func__);
  1376. return -ENODEV;
  1377. }
  1378. if (!vol->pcm) {
  1379. pr_err("%s: vol->pcm is NULL\n", __func__);
  1380. return -ENODEV;
  1381. }
  1382. substream = vol->pcm->streams[vol->stream].substream;
  1383. if (!substream) {
  1384. pr_err("%s substream not found\n", __func__);
  1385. return -ENODEV;
  1386. }
  1387. soc_prtd = substream->private_data;
  1388. if (!substream->runtime || !soc_prtd) {
  1389. pr_debug("%s substream runtime or private_data not found\n",
  1390. __func__);
  1391. return 0;
  1392. }
  1393. component = snd_soc_rtdcom_lookup(soc_prtd, DRV_NAME);
  1394. if (!component) {
  1395. pr_err("%s: component is NULL\n", __func__);
  1396. return -EINVAL;
  1397. }
  1398. pdata = (struct msm_plat_data *) dev_get_drvdata(component->dev);
  1399. if (!pdata) {
  1400. pr_err("%s: pdata not found\n", __func__);
  1401. return -ENODEV;
  1402. }
  1403. mutex_lock(&pdata->lock);
  1404. if (substream->ref_count > 0) {
  1405. prtd = substream->runtime->private_data;
  1406. if (prtd)
  1407. ucontrol->value.integer.value[0] = prtd->volume;
  1408. }
  1409. mutex_unlock(&pdata->lock);
  1410. return 0;
  1411. }
  1412. static int msm_pcm_volume_ctl_put(struct snd_kcontrol *kcontrol,
  1413. struct snd_ctl_elem_value *ucontrol)
  1414. {
  1415. int rc = 0;
  1416. struct snd_pcm_volume *vol = snd_kcontrol_chip(kcontrol);
  1417. struct msm_plat_data *pdata = NULL;
  1418. struct snd_pcm_substream *substream =
  1419. vol->pcm->streams[vol->stream].substream;
  1420. struct snd_soc_pcm_runtime *soc_prtd = NULL;
  1421. struct snd_soc_component *component = NULL;
  1422. struct msm_audio *prtd;
  1423. int volume = ucontrol->value.integer.value[0];
  1424. pr_debug("%s: volume : 0x%x\n", __func__, volume);
  1425. if (!substream) {
  1426. pr_err("%s: substream not found\n", __func__);
  1427. return -ENODEV;
  1428. }
  1429. soc_prtd = substream->private_data;
  1430. if (!substream->runtime || !soc_prtd) {
  1431. pr_err("%s: substream runtime or private_data not found\n",
  1432. __func__);
  1433. return 0;
  1434. }
  1435. component = snd_soc_rtdcom_lookup(soc_prtd, DRV_NAME);
  1436. if (!component) {
  1437. pr_err("%s: component is NULL\n", __func__);
  1438. return -EINVAL;
  1439. }
  1440. pdata = (struct msm_plat_data *) dev_get_drvdata(component->dev);
  1441. if (!pdata) {
  1442. pr_err("%s: pdata not found\n", __func__);
  1443. return -ENODEV;
  1444. }
  1445. mutex_lock(&pdata->lock);
  1446. if (substream->ref_count > 0) {
  1447. prtd = substream->runtime->private_data;
  1448. if (prtd) {
  1449. rc = msm_pcm_set_volume(prtd, volume);
  1450. prtd->volume = volume;
  1451. }
  1452. }
  1453. mutex_unlock(&pdata->lock);
  1454. return rc;
  1455. }
  1456. static int msm_pcm_add_volume_control(struct snd_soc_pcm_runtime *rtd,
  1457. int stream)
  1458. {
  1459. int ret = 0;
  1460. struct snd_pcm *pcm = rtd->pcm;
  1461. struct snd_pcm_volume *volume_info;
  1462. struct snd_kcontrol *kctl;
  1463. dev_dbg(rtd->dev, "%s, volume control add\n", __func__);
  1464. ret = snd_pcm_add_volume_ctls(pcm, stream,
  1465. NULL, 1, rtd->dai_link->id,
  1466. &volume_info);
  1467. if (ret < 0) {
  1468. pr_err("%s volume control failed ret %d\n", __func__, ret);
  1469. return ret;
  1470. }
  1471. kctl = volume_info->kctl;
  1472. kctl->put = msm_pcm_volume_ctl_put;
  1473. kctl->get = msm_pcm_volume_ctl_get;
  1474. kctl->tlv.p = msm_pcm_vol_gain;
  1475. return 0;
  1476. }
  1477. #else
  1478. static int msm_pcm_add_volume_control(struct snd_soc_pcm_runtime *rtd,
  1479. int stream)
  1480. {
  1481. return 0;
  1482. }
  1483. #endif /* CONFIG_AUDIO_QGKI */
  1484. static int msm_pcm_compress_ctl_info(struct snd_kcontrol *kcontrol,
  1485. struct snd_ctl_elem_info *uinfo)
  1486. {
  1487. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1488. uinfo->count = 1;
  1489. uinfo->value.integer.min = 0;
  1490. uinfo->value.integer.max = 0x2000;
  1491. return 0;
  1492. }
  1493. static int msm_pcm_compress_ctl_get(struct snd_kcontrol *kcontrol,
  1494. struct snd_ctl_elem_value *ucontrol)
  1495. {
  1496. struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
  1497. struct msm_plat_data *pdata = dev_get_drvdata(comp->dev);
  1498. struct snd_pcm_substream *substream;
  1499. struct msm_audio *prtd;
  1500. if (!pdata) {
  1501. pr_err("%s pdata is NULL\n", __func__);
  1502. return -ENODEV;
  1503. }
  1504. substream = pdata->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
  1505. if (!substream) {
  1506. pr_err("%s substream not found\n", __func__);
  1507. return -EINVAL;
  1508. }
  1509. if (!substream->runtime) {
  1510. pr_debug("%s substream runtime not found\n", __func__);
  1511. return 0;
  1512. }
  1513. mutex_lock(&pdata->lock);
  1514. if (substream->ref_count > 0) {
  1515. prtd = substream->runtime->private_data;
  1516. if (prtd)
  1517. ucontrol->value.integer.value[0] = prtd->compress_enable;
  1518. }
  1519. mutex_unlock(&pdata->lock);
  1520. return 0;
  1521. }
  1522. static int msm_pcm_compress_ctl_put(struct snd_kcontrol *kcontrol,
  1523. struct snd_ctl_elem_value *ucontrol)
  1524. {
  1525. int rc = 0;
  1526. struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
  1527. struct msm_plat_data *pdata = dev_get_drvdata(comp->dev);
  1528. struct snd_pcm_substream *substream;
  1529. struct msm_audio *prtd;
  1530. int compress = ucontrol->value.integer.value[0];
  1531. if (!pdata) {
  1532. pr_err("%s pdata is NULL\n", __func__);
  1533. return -ENODEV;
  1534. }
  1535. substream = pdata->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
  1536. pr_debug("%s: compress : 0x%x\n", __func__, compress);
  1537. if (!substream) {
  1538. pr_err("%s substream not found\n", __func__);
  1539. return -EINVAL;
  1540. }
  1541. if (!substream->runtime) {
  1542. pr_err("%s substream runtime not found\n", __func__);
  1543. return 0;
  1544. }
  1545. mutex_lock(&pdata->lock);
  1546. if (substream->ref_count > 0) {
  1547. prtd = substream->runtime->private_data;
  1548. if (prtd) {
  1549. pr_debug("%s: setting compress flag to 0x%x\n",
  1550. __func__, compress);
  1551. prtd->compress_enable = compress;
  1552. }
  1553. }
  1554. mutex_unlock(&pdata->lock);
  1555. return rc;
  1556. }
  1557. static int msm_pcm_add_compress_control(struct snd_soc_pcm_runtime *rtd)
  1558. {
  1559. struct snd_soc_component *component = NULL;
  1560. const char *mixer_ctl_name = "Playback ";
  1561. const char *mixer_ctl_end_name = " Compress";
  1562. const char *deviceNo = "NN";
  1563. char *mixer_str = NULL;
  1564. int ctl_len;
  1565. int ret = 0;
  1566. struct msm_plat_data *pdata;
  1567. struct snd_kcontrol_new pcm_compress_control[1] = {
  1568. {
  1569. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1570. .name = "?",
  1571. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  1572. .info = msm_pcm_compress_ctl_info,
  1573. .get = msm_pcm_compress_ctl_get,
  1574. .put = msm_pcm_compress_ctl_put,
  1575. .private_value = 0,
  1576. }
  1577. };
  1578. if (!rtd) {
  1579. pr_err("%s: NULL rtd\n", __func__);
  1580. return -EINVAL;
  1581. }
  1582. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  1583. if (!component) {
  1584. pr_err("%s: component is NULL\n", __func__);
  1585. return -EINVAL;
  1586. }
  1587. ctl_len = strlen(mixer_ctl_name) + strlen(deviceNo) +
  1588. strlen(mixer_ctl_end_name) + 1;
  1589. mixer_str = kzalloc(ctl_len, GFP_KERNEL);
  1590. if (!mixer_str)
  1591. return -ENOMEM;
  1592. snprintf(mixer_str, ctl_len, "%s%d%s", mixer_ctl_name,
  1593. rtd->pcm->device, mixer_ctl_end_name);
  1594. pcm_compress_control[0].name = mixer_str;
  1595. pcm_compress_control[0].private_value = rtd->dai_link->id;
  1596. pr_debug("%s: Registering new mixer ctl %s\n", __func__, mixer_str);
  1597. pdata = dev_get_drvdata(component->dev);
  1598. if (pdata) {
  1599. if (!pdata->pcm) {
  1600. pdata->pcm = rtd->pcm;
  1601. snd_soc_add_component_controls(component,
  1602. pcm_compress_control,
  1603. ARRAY_SIZE
  1604. (pcm_compress_control));
  1605. pr_debug("%s: add control success plt = %pK\n",
  1606. __func__, component);
  1607. }
  1608. } else {
  1609. pr_err("%s: NULL pdata\n", __func__);
  1610. ret = -EINVAL;
  1611. }
  1612. kfree(mixer_str);
  1613. return ret;
  1614. }
  1615. static int msm_pcm_chmap_ctl_put(struct snd_kcontrol *kcontrol,
  1616. struct snd_ctl_elem_value *ucontrol)
  1617. {
  1618. int i;
  1619. struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
  1620. unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
  1621. struct snd_pcm_substream *substream;
  1622. struct msm_audio *prtd;
  1623. struct snd_soc_pcm_runtime *rtd = NULL;
  1624. struct msm_plat_data *pdata = NULL;
  1625. struct msm_pcm_channel_mixer *chmixer_pspd = NULL;
  1626. struct snd_soc_component *component = NULL;
  1627. u64 fe_id = 0;
  1628. pr_debug("%s", __func__);
  1629. substream = snd_pcm_chmap_substream(info, idx);
  1630. if (!substream)
  1631. return -ENODEV;
  1632. rtd = substream->private_data;
  1633. if (rtd) {
  1634. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  1635. if (component) {
  1636. pdata = (struct msm_plat_data *)
  1637. dev_get_drvdata(component->dev);
  1638. if (!pdata) {
  1639. pr_err("%s: pdata not found\n", __func__);
  1640. return -ENODEV;
  1641. }
  1642. } else {
  1643. pr_err("%s: component is NULL\n", __func__);
  1644. return -EINVAL;
  1645. }
  1646. }
  1647. if (!rtd)
  1648. return 0;
  1649. mutex_lock(&pdata->lock);
  1650. if (substream->ref_count <= 0) {
  1651. pr_err_ratelimited("%s: substream ref_count:%d invalid\n",
  1652. __func__, substream->ref_count);
  1653. mutex_unlock(&pdata->lock);
  1654. return -EINVAL;
  1655. }
  1656. prtd = substream->runtime ? substream->runtime->private_data : NULL;
  1657. if (prtd) {
  1658. prtd->set_channel_map = true;
  1659. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  1660. prtd->channel_map[i] =
  1661. (char)(ucontrol->value.integer.value[i]);
  1662. /* update chmixer_pspd chmap cached with routing driver as well */
  1663. if (rtd) {
  1664. if (component) {
  1665. fe_id = rtd->dai_link->id;
  1666. chmixer_pspd = pdata ?
  1667. pdata->chmixer_pspd[fe_id][SESSION_TYPE_RX] : NULL;
  1668. if (chmixer_pspd && chmixer_pspd->enable) {
  1669. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  1670. chmixer_pspd->in_ch_map[i] = prtd->channel_map[i];
  1671. chmixer_pspd->override_in_ch_map = true;
  1672. msm_pcm_routing_set_channel_mixer_cfg(fe_id,
  1673. SESSION_TYPE_RX, chmixer_pspd);
  1674. }
  1675. }
  1676. }
  1677. }
  1678. mutex_unlock(&pdata->lock);
  1679. return 0;
  1680. }
  1681. static int msm_pcm_chmap_ctl_get(struct snd_kcontrol *kcontrol,
  1682. struct snd_ctl_elem_value *ucontrol)
  1683. {
  1684. int i;
  1685. struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
  1686. unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
  1687. struct snd_pcm_substream *substream;
  1688. struct msm_audio *prtd;
  1689. struct snd_soc_pcm_runtime *rtd = NULL;
  1690. struct msm_plat_data *pdata = NULL;
  1691. struct snd_soc_component *component = NULL;
  1692. pr_debug("%s", __func__);
  1693. substream = snd_pcm_chmap_substream(info, idx);
  1694. if (!substream)
  1695. return -ENODEV;
  1696. rtd = substream->private_data;
  1697. if (rtd) {
  1698. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  1699. if (component) {
  1700. pdata = (struct msm_plat_data *)
  1701. dev_get_drvdata(component->dev);
  1702. if (!pdata) {
  1703. pr_err("%s: pdata not found\n", __func__);
  1704. return -ENODEV;
  1705. }
  1706. } else {
  1707. pr_err("%s: component is NULL\n", __func__);
  1708. return -EINVAL;
  1709. }
  1710. }
  1711. memset(ucontrol->value.integer.value, 0,
  1712. sizeof(ucontrol->value.integer.value));
  1713. if (!rtd)
  1714. return 0; /* no channels set */
  1715. mutex_lock(&pdata->lock);
  1716. if (substream->ref_count <= 0) {
  1717. pr_err_ratelimited("%s: substream ref_count:%d invalid\n",
  1718. __func__, substream->ref_count);
  1719. mutex_unlock(&pdata->lock);
  1720. return -EINVAL;
  1721. }
  1722. prtd = substream->runtime ? substream->runtime->private_data : NULL;
  1723. if (prtd && prtd->set_channel_map == true) {
  1724. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  1725. ucontrol->value.integer.value[i] =
  1726. (int)prtd->channel_map[i];
  1727. } else {
  1728. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  1729. ucontrol->value.integer.value[i] = 0;
  1730. }
  1731. mutex_unlock(&pdata->lock);
  1732. return 0;
  1733. }
  1734. static int msm_pcm_add_chmap_controls(struct snd_soc_pcm_runtime *rtd)
  1735. {
  1736. struct snd_pcm *pcm = rtd->pcm;
  1737. struct snd_pcm_chmap *chmap_info;
  1738. struct snd_kcontrol *kctl;
  1739. char device_num[12];
  1740. int i, ret = 0;
  1741. pr_debug("%s, Channel map cntrl add\n", __func__);
  1742. ret = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  1743. snd_pcm_std_chmaps,
  1744. PCM_FORMAT_MAX_NUM_CHANNEL_V8, 0,
  1745. &chmap_info);
  1746. if (ret < 0) {
  1747. pr_err("%s, channel map cntrl add failed\n", __func__);
  1748. return ret;
  1749. }
  1750. kctl = chmap_info->kctl;
  1751. for (i = 0; i < kctl->count; i++)
  1752. kctl->vd[i].access |= SNDRV_CTL_ELEM_ACCESS_WRITE;
  1753. snprintf(device_num, sizeof(device_num), "%d", pcm->device);
  1754. strlcat(kctl->id.name, device_num, sizeof(kctl->id.name));
  1755. pr_debug("%s, Overwriting channel map control name to: %s\n",
  1756. __func__, kctl->id.name);
  1757. kctl->put = msm_pcm_chmap_ctl_put;
  1758. kctl->get = msm_pcm_chmap_ctl_get;
  1759. return 0;
  1760. }
  1761. #if IS_ENABLED(CONFIG_AUDIO_QGKI)
  1762. static int msm_pcm_playback_app_type_cfg_ctl_put(struct snd_kcontrol *kcontrol,
  1763. struct snd_ctl_elem_value *ucontrol)
  1764. {
  1765. u64 fe_id = kcontrol->private_value;
  1766. int session_type = SESSION_TYPE_RX;
  1767. int be_id = ucontrol->value.integer.value[3];
  1768. struct msm_pcm_stream_app_type_cfg cfg_data = {0, 0, 48000};
  1769. int ret = 0;
  1770. cfg_data.app_type = ucontrol->value.integer.value[0];
  1771. cfg_data.acdb_dev_id = ucontrol->value.integer.value[1];
  1772. if (ucontrol->value.integer.value[2] != 0)
  1773. cfg_data.sample_rate = ucontrol->value.integer.value[2];
  1774. pr_debug("%s: fe_id- %llu session_type- %d be_id- %d app_type- %d acdb_dev_id- %d sample_rate- %d\n",
  1775. __func__, fe_id, session_type, be_id,
  1776. cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
  1777. ret = msm_pcm_routing_reg_stream_app_type_cfg(fe_id, session_type,
  1778. be_id, &cfg_data);
  1779. if (ret < 0)
  1780. pr_err("%s: msm_pcm_routing_reg_stream_app_type_cfg failed returned %d\n",
  1781. __func__, ret);
  1782. return ret;
  1783. }
  1784. static int msm_pcm_playback_app_type_cfg_ctl_get(struct snd_kcontrol *kcontrol,
  1785. struct snd_ctl_elem_value *ucontrol)
  1786. {
  1787. u64 fe_id = kcontrol->private_value;
  1788. int session_type = SESSION_TYPE_RX;
  1789. int be_id = 0;
  1790. struct msm_pcm_stream_app_type_cfg cfg_data = {0};
  1791. int ret = 0;
  1792. ret = msm_pcm_routing_get_stream_app_type_cfg(fe_id, session_type,
  1793. &be_id, &cfg_data);
  1794. if (ret < 0) {
  1795. pr_err("%s: msm_pcm_routing_get_stream_app_type_cfg failed returned %d\n",
  1796. __func__, ret);
  1797. goto done;
  1798. }
  1799. ucontrol->value.integer.value[0] = cfg_data.app_type;
  1800. ucontrol->value.integer.value[1] = cfg_data.acdb_dev_id;
  1801. ucontrol->value.integer.value[2] = cfg_data.sample_rate;
  1802. ucontrol->value.integer.value[3] = be_id;
  1803. pr_debug("%s: fedai_id %llu, session_type %d, be_id %d, app_type %d, acdb_dev_id %d, sample_rate %d\n",
  1804. __func__, fe_id, session_type, be_id,
  1805. cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
  1806. done:
  1807. return ret;
  1808. }
  1809. static int msm_pcm_capture_app_type_cfg_ctl_put(struct snd_kcontrol *kcontrol,
  1810. struct snd_ctl_elem_value *ucontrol)
  1811. {
  1812. u64 fe_id = kcontrol->private_value;
  1813. int session_type = SESSION_TYPE_TX;
  1814. int be_id = ucontrol->value.integer.value[3];
  1815. struct msm_pcm_stream_app_type_cfg cfg_data = {0, 0, 48000};
  1816. int ret = 0;
  1817. cfg_data.app_type = ucontrol->value.integer.value[0];
  1818. cfg_data.acdb_dev_id = ucontrol->value.integer.value[1];
  1819. if (ucontrol->value.integer.value[2] != 0)
  1820. cfg_data.sample_rate = ucontrol->value.integer.value[2];
  1821. pr_debug("%s: fe_id- %llu session_type- %d be_id- %d app_type- %d acdb_dev_id- %d sample_rate- %d\n",
  1822. __func__, fe_id, session_type, be_id,
  1823. cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
  1824. ret = msm_pcm_routing_reg_stream_app_type_cfg(fe_id, session_type,
  1825. be_id, &cfg_data);
  1826. if (ret < 0)
  1827. pr_err("%s: msm_pcm_routing_reg_stream_app_type_cfg failed returned %d\n",
  1828. __func__, ret);
  1829. return ret;
  1830. }
  1831. static int msm_pcm_capture_app_type_cfg_ctl_get(struct snd_kcontrol *kcontrol,
  1832. struct snd_ctl_elem_value *ucontrol)
  1833. {
  1834. u64 fe_id = kcontrol->private_value;
  1835. int session_type = SESSION_TYPE_TX;
  1836. int be_id = 0;
  1837. struct msm_pcm_stream_app_type_cfg cfg_data = {0};
  1838. int ret = 0;
  1839. ret = msm_pcm_routing_get_stream_app_type_cfg(fe_id, session_type,
  1840. &be_id, &cfg_data);
  1841. if (ret < 0) {
  1842. pr_err("%s: msm_pcm_routing_get_stream_app_type_cfg failed returned %d\n",
  1843. __func__, ret);
  1844. goto done;
  1845. }
  1846. ucontrol->value.integer.value[0] = cfg_data.app_type;
  1847. ucontrol->value.integer.value[1] = cfg_data.acdb_dev_id;
  1848. ucontrol->value.integer.value[2] = cfg_data.sample_rate;
  1849. ucontrol->value.integer.value[3] = be_id;
  1850. pr_debug("%s: fedai_id %llu, session_type %d, be_id %d, app_type %d, acdb_dev_id %d, sample_rate %d\n",
  1851. __func__, fe_id, session_type, be_id,
  1852. cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
  1853. done:
  1854. return ret;
  1855. }
  1856. static int msm_pcm_add_app_type_controls(struct snd_soc_pcm_runtime *rtd)
  1857. {
  1858. struct snd_pcm *pcm = rtd->pcm;
  1859. struct snd_pcm_usr *app_type_info;
  1860. struct snd_kcontrol *kctl;
  1861. const char *playback_mixer_ctl_name = "Audio Stream";
  1862. const char *capture_mixer_ctl_name = "Audio Stream Capture";
  1863. const char *deviceNo = "NN";
  1864. const char *suffix = "App Type Cfg";
  1865. int ctl_len, ret = 0;
  1866. if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
  1867. ctl_len = strlen(playback_mixer_ctl_name) + 1 +
  1868. strlen(deviceNo) + 1 + strlen(suffix) + 1;
  1869. pr_debug("%s: Playback app type cntrl add\n", __func__);
  1870. ret = snd_pcm_add_usr_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  1871. NULL, 1, ctl_len, rtd->dai_link->id,
  1872. &app_type_info);
  1873. if (ret < 0) {
  1874. pr_err("%s: playback app type cntrl add failed: %d\n",
  1875. __func__, ret);
  1876. return ret;
  1877. }
  1878. kctl = app_type_info->kctl;
  1879. snprintf(kctl->id.name, ctl_len, "%s %d %s",
  1880. playback_mixer_ctl_name, rtd->pcm->device, suffix);
  1881. kctl->put = msm_pcm_playback_app_type_cfg_ctl_put;
  1882. kctl->get = msm_pcm_playback_app_type_cfg_ctl_get;
  1883. }
  1884. if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
  1885. ctl_len = strlen(capture_mixer_ctl_name) + 1 +
  1886. strlen(deviceNo) + 1 + strlen(suffix) + 1;
  1887. pr_debug("%s: Capture app type cntrl add\n", __func__);
  1888. ret = snd_pcm_add_usr_ctls(pcm, SNDRV_PCM_STREAM_CAPTURE,
  1889. NULL, 1, ctl_len, rtd->dai_link->id,
  1890. &app_type_info);
  1891. if (ret < 0) {
  1892. pr_err("%s: capture app type cntrl add failed: %d\n",
  1893. __func__, ret);
  1894. return ret;
  1895. }
  1896. kctl = app_type_info->kctl;
  1897. snprintf(kctl->id.name, ctl_len, "%s %d %s",
  1898. capture_mixer_ctl_name, rtd->pcm->device, suffix);
  1899. kctl->put = msm_pcm_capture_app_type_cfg_ctl_put;
  1900. kctl->get = msm_pcm_capture_app_type_cfg_ctl_get;
  1901. }
  1902. return 0;
  1903. }
  1904. #else
  1905. static int msm_pcm_add_app_type_controls(struct snd_soc_pcm_runtime *rtd)
  1906. {
  1907. return 0;
  1908. }
  1909. #endif /* CONFIG_AUDIO_QGKI */
  1910. static struct msm_pcm_channel_mixer *msm_pcm_get_chmixer(
  1911. struct msm_plat_data *pdata,
  1912. u64 fe_id, int session_type)
  1913. {
  1914. if (!pdata) {
  1915. pr_err("%s: missing pdata\n", __func__);
  1916. return NULL;
  1917. }
  1918. if (fe_id >= MSM_FRONTEND_DAI_MM_SIZE) {
  1919. pr_err("%s: invalid FE %llu\n", __func__, fe_id);
  1920. return NULL;
  1921. }
  1922. if ((session_type != SESSION_TYPE_TX) &&
  1923. (session_type != SESSION_TYPE_RX)) {
  1924. pr_err("%s: invalid session type %d\n", __func__, session_type);
  1925. return NULL;
  1926. }
  1927. return pdata->chmixer_pspd[fe_id][session_type];
  1928. }
  1929. static int msm_pcm_channel_mixer_cfg_ctl_put(struct snd_kcontrol *kcontrol,
  1930. struct snd_ctl_elem_value *ucontrol)
  1931. {
  1932. u64 fe_id = kcontrol->private_value & 0xFF;
  1933. int session_type = (kcontrol->private_value >> 8) & 0xFF;
  1934. int ret = 0;
  1935. int stream_id = 0;
  1936. int be_id = 0, i = 0;
  1937. struct msm_audio *prtd = NULL;
  1938. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  1939. struct msm_plat_data *pdata = dev_get_drvdata(component->dev);
  1940. struct snd_pcm *pcm = NULL;
  1941. struct snd_pcm_substream *substream = NULL;
  1942. struct msm_pcm_channel_mixer *chmixer_pspd = NULL;
  1943. u8 asm_ch_map[PCM_FORMAT_MAX_NUM_CHANNEL_V8] = {0};
  1944. bool reset_override_out_ch_map = false;
  1945. bool reset_override_in_ch_map = false;
  1946. pcm = pdata->pcm_device[fe_id];
  1947. if (!pcm) {
  1948. pr_err("%s invalid pcm handle for fe_id %llu\n",
  1949. __func__, fe_id);
  1950. return -EINVAL;
  1951. }
  1952. if (session_type == SESSION_TYPE_RX)
  1953. substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
  1954. else
  1955. substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
  1956. if (!substream) {
  1957. pr_err("%s substream not found\n", __func__);
  1958. return -EINVAL;
  1959. }
  1960. chmixer_pspd = msm_pcm_get_chmixer(pdata, fe_id, session_type);
  1961. if (!chmixer_pspd) {
  1962. pr_err("%s: invalid chmixer_pspd in pdata", __func__);
  1963. return -EINVAL;
  1964. }
  1965. chmixer_pspd->enable = ucontrol->value.integer.value[0];
  1966. chmixer_pspd->rule = ucontrol->value.integer.value[1];
  1967. chmixer_pspd->input_channel = ucontrol->value.integer.value[2];
  1968. chmixer_pspd->output_channel = ucontrol->value.integer.value[3];
  1969. chmixer_pspd->port_idx = ucontrol->value.integer.value[4];
  1970. if (chmixer_pspd->input_channel < 0 ||
  1971. chmixer_pspd->input_channel > PCM_FORMAT_MAX_NUM_CHANNEL_V8 ||
  1972. chmixer_pspd->output_channel < 0 ||
  1973. chmixer_pspd->output_channel > PCM_FORMAT_MAX_NUM_CHANNEL_V8) {
  1974. pr_err("%s: Invalid channels, in %d, out %d\n",
  1975. __func__, chmixer_pspd->input_channel,
  1976. chmixer_pspd->output_channel);
  1977. return -EINVAL;
  1978. }
  1979. mutex_lock(&pdata->lock);
  1980. if (substream->ref_count <= 0) {
  1981. pr_err_ratelimited("%s: substream ref_count:%d invalid\n",
  1982. __func__, substream->ref_count);
  1983. mutex_unlock(&pdata->lock);
  1984. return -EINVAL;
  1985. }
  1986. prtd = substream->runtime ? substream->runtime->private_data : NULL;
  1987. if (chmixer_pspd->enable && prtd) {
  1988. if (session_type == SESSION_TYPE_RX &&
  1989. !chmixer_pspd->override_in_ch_map) {
  1990. if (prtd->set_channel_map) {
  1991. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  1992. chmixer_pspd->in_ch_map[i] = prtd->channel_map[i];
  1993. } else {
  1994. q6asm_map_channels(asm_ch_map,
  1995. chmixer_pspd->input_channel, false);
  1996. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  1997. chmixer_pspd->in_ch_map[i] = asm_ch_map[i];
  1998. }
  1999. chmixer_pspd->override_in_ch_map = true;
  2000. reset_override_in_ch_map = true;
  2001. } else if (session_type == SESSION_TYPE_TX &&
  2002. !chmixer_pspd->override_out_ch_map) {
  2003. /*
  2004. * Channel map set in prtd is for plyback only,
  2005. * hence always use default for capture path.
  2006. */
  2007. q6asm_map_channels(asm_ch_map,
  2008. chmixer_pspd->output_channel, false);
  2009. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  2010. chmixer_pspd->out_ch_map[i] = asm_ch_map[i];
  2011. chmixer_pspd->override_out_ch_map = true;
  2012. reset_override_out_ch_map = true;
  2013. }
  2014. } else {
  2015. chmixer_pspd->override_out_ch_map = false;
  2016. chmixer_pspd->override_in_ch_map = false;
  2017. }
  2018. /* cache value and take effect during adm_open stage */
  2019. msm_pcm_routing_set_channel_mixer_cfg(fe_id,
  2020. session_type,
  2021. chmixer_pspd);
  2022. if (chmixer_pspd->enable && prtd && prtd->audio_client) {
  2023. stream_id = prtd->audio_client->session;
  2024. be_id = chmixer_pspd->port_idx;
  2025. msm_pcm_routing_set_channel_mixer_runtime(be_id,
  2026. stream_id,
  2027. session_type,
  2028. chmixer_pspd);
  2029. }
  2030. if (reset_override_out_ch_map)
  2031. chmixer_pspd->override_out_ch_map = false;
  2032. if (reset_override_in_ch_map)
  2033. chmixer_pspd->override_in_ch_map = false;
  2034. mutex_unlock(&pdata->lock);
  2035. return ret;
  2036. }
  2037. static int msm_pcm_channel_mixer_cfg_ctl_get(struct snd_kcontrol *kcontrol,
  2038. struct snd_ctl_elem_value *ucontrol)
  2039. {
  2040. u64 fe_id = kcontrol->private_value & 0xFF;
  2041. int session_type = (kcontrol->private_value >> 8) & 0xFF;
  2042. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  2043. struct msm_plat_data *pdata = dev_get_drvdata(component->dev);
  2044. struct msm_pcm_channel_mixer *chmixer_pspd;
  2045. chmixer_pspd = msm_pcm_get_chmixer(pdata, fe_id, session_type);
  2046. if (!chmixer_pspd) {
  2047. pr_err("%s: invalid chmixer_pspd in pdata", __func__);
  2048. return -EINVAL;
  2049. }
  2050. ucontrol->value.integer.value[0] = chmixer_pspd->enable;
  2051. ucontrol->value.integer.value[1] = chmixer_pspd->rule;
  2052. ucontrol->value.integer.value[2] = chmixer_pspd->input_channel;
  2053. ucontrol->value.integer.value[3] = chmixer_pspd->output_channel;
  2054. ucontrol->value.integer.value[4] = chmixer_pspd->port_idx;
  2055. return 0;
  2056. }
  2057. static int msm_pcm_channel_mixer_output_map_ctl_put(
  2058. struct snd_kcontrol *kcontrol,
  2059. struct snd_ctl_elem_value *ucontrol)
  2060. {
  2061. u64 fe_id = kcontrol->private_value & 0xFF;
  2062. int session_type = (kcontrol->private_value >> 8) & 0xFF;
  2063. int i = 0;
  2064. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  2065. struct msm_plat_data *pdata = dev_get_drvdata(component->dev);
  2066. struct msm_pcm_channel_mixer *chmixer_pspd;
  2067. chmixer_pspd = msm_pcm_get_chmixer(pdata, fe_id, session_type);
  2068. if (!chmixer_pspd) {
  2069. pr_err("%s: invalid chmixer_pspd in pdata", __func__);
  2070. return -EINVAL;
  2071. }
  2072. chmixer_pspd->override_out_ch_map = true;
  2073. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  2074. chmixer_pspd->out_ch_map[i] =
  2075. ucontrol->value.integer.value[i];
  2076. return 0;
  2077. }
  2078. static int msm_pcm_channel_mixer_output_map_ctl_get(
  2079. struct snd_kcontrol *kcontrol,
  2080. struct snd_ctl_elem_value *ucontrol)
  2081. {
  2082. u64 fe_id = kcontrol->private_value & 0xFF;
  2083. int session_type = (kcontrol->private_value >> 8) & 0xFF;
  2084. int i = 0;
  2085. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  2086. struct msm_plat_data *pdata = dev_get_drvdata(component->dev);
  2087. struct msm_pcm_channel_mixer *chmixer_pspd;
  2088. chmixer_pspd = msm_pcm_get_chmixer(pdata, fe_id, session_type);
  2089. if (!chmixer_pspd) {
  2090. pr_err("%s: invalid chmixer_pspd in pdata", __func__);
  2091. return -EINVAL;
  2092. }
  2093. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  2094. ucontrol->value.integer.value[i] =
  2095. chmixer_pspd->out_ch_map[i];
  2096. return 0;
  2097. }
  2098. static int msm_pcm_channel_mixer_input_map_ctl_put(
  2099. struct snd_kcontrol *kcontrol,
  2100. struct snd_ctl_elem_value *ucontrol)
  2101. {
  2102. u64 fe_id = kcontrol->private_value & 0xFF;
  2103. int session_type = (kcontrol->private_value >> 8) & 0xFF;
  2104. int i = 0;
  2105. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  2106. struct msm_plat_data *pdata = dev_get_drvdata(component->dev);
  2107. struct msm_pcm_channel_mixer *chmixer_pspd;
  2108. chmixer_pspd = msm_pcm_get_chmixer(pdata, fe_id, session_type);
  2109. if (!chmixer_pspd) {
  2110. pr_err("%s: invalid chmixer_pspd in pdata", __func__);
  2111. return -EINVAL;
  2112. }
  2113. chmixer_pspd->override_in_ch_map = true;
  2114. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  2115. chmixer_pspd->in_ch_map[i] = ucontrol->value.integer.value[i];
  2116. return 0;
  2117. }
  2118. static int msm_pcm_channel_mixer_input_map_ctl_get(
  2119. struct snd_kcontrol *kcontrol,
  2120. struct snd_ctl_elem_value *ucontrol)
  2121. {
  2122. u64 fe_id = kcontrol->private_value & 0xFF;
  2123. int session_type = (kcontrol->private_value >> 8) & 0xFF;
  2124. int i = 0;
  2125. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  2126. struct msm_plat_data *pdata = dev_get_drvdata(component->dev);
  2127. struct msm_pcm_channel_mixer *chmixer_pspd;
  2128. chmixer_pspd = msm_pcm_get_chmixer(pdata, fe_id, session_type);
  2129. if (!chmixer_pspd) {
  2130. pr_err("%s: invalid chmixer_pspd in pdata", __func__);
  2131. return -EINVAL;
  2132. }
  2133. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  2134. ucontrol->value.integer.value[i] =
  2135. chmixer_pspd->in_ch_map[i];
  2136. return 0;
  2137. }
  2138. static int msm_pcm_channel_mixer_weight_ctl_put(
  2139. struct snd_kcontrol *kcontrol,
  2140. struct snd_ctl_elem_value *ucontrol)
  2141. {
  2142. u64 fe_id = kcontrol->private_value & 0xFF;
  2143. int session_type = (kcontrol->private_value >> 8) & 0xFF;
  2144. int channel = (kcontrol->private_value >> 16) & 0xFF;
  2145. int i = 0;
  2146. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  2147. struct msm_plat_data *pdata = dev_get_drvdata(component->dev);
  2148. struct msm_pcm_channel_mixer *chmixer_pspd;
  2149. chmixer_pspd = msm_pcm_get_chmixer(pdata, fe_id, session_type);
  2150. if (!chmixer_pspd) {
  2151. pr_err("%s: invalid chmixer_pspd in pdata", __func__);
  2152. return -EINVAL;
  2153. }
  2154. if (channel <= 0 || channel > PCM_FORMAT_MAX_NUM_CHANNEL_V8) {
  2155. pr_err("%s: invalid channel number %d\n", __func__, channel);
  2156. return -EINVAL;
  2157. }
  2158. channel--;
  2159. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  2160. chmixer_pspd->channel_weight[channel][i] =
  2161. ucontrol->value.integer.value[i];
  2162. return 0;
  2163. }
  2164. static int msm_pcm_channel_mixer_weight_ctl_get(
  2165. struct snd_kcontrol *kcontrol,
  2166. struct snd_ctl_elem_value *ucontrol)
  2167. {
  2168. u64 fe_id = kcontrol->private_value & 0xFF;
  2169. int session_type = (kcontrol->private_value >> 8) & 0xFF;
  2170. int channel = (kcontrol->private_value >> 16) & 0xFF;
  2171. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  2172. struct msm_plat_data *pdata = dev_get_drvdata(component->dev);
  2173. int i = 0;
  2174. struct msm_pcm_channel_mixer *chmixer_pspd;
  2175. if (channel <= 0 || channel > PCM_FORMAT_MAX_NUM_CHANNEL_V8) {
  2176. pr_err("%s: invalid channel number %d\n", __func__, channel);
  2177. return -EINVAL;
  2178. }
  2179. channel--;
  2180. chmixer_pspd = msm_pcm_get_chmixer(pdata, fe_id, session_type);
  2181. if (!chmixer_pspd) {
  2182. pr_err("%s: invalid chmixer_pspd in pdata", __func__);
  2183. return -EINVAL;
  2184. }
  2185. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  2186. ucontrol->value.integer.value[i] =
  2187. chmixer_pspd->channel_weight[channel][i];
  2188. return 0;
  2189. }
  2190. static int msm_pcm_add_platform_controls(struct snd_kcontrol_new *kctl,
  2191. struct snd_soc_pcm_runtime *rtd, const char *name_prefix,
  2192. const char *name_suffix, int session_type, int channels)
  2193. {
  2194. int ret = -EINVAL;
  2195. char *mixer_name = NULL;
  2196. struct snd_pcm *pcm = rtd->pcm;
  2197. const char *deviceNo = "NN";
  2198. const char *channelNo = "NN";
  2199. int ctl_len = 0;
  2200. struct snd_soc_component *component = NULL;
  2201. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  2202. if (!component) {
  2203. pr_err("%s: component is NULL\n", __func__);
  2204. return -EINVAL;
  2205. }
  2206. ctl_len = strlen(name_prefix) + 1 + strlen(deviceNo) + 1 +
  2207. strlen(channelNo) + 1 + strlen(name_suffix) + 1;
  2208. mixer_name = kzalloc(ctl_len, GFP_KERNEL);
  2209. if (mixer_name == NULL)
  2210. return -ENOMEM;
  2211. if (channels >= 0) {
  2212. snprintf(mixer_name, ctl_len, "%s %d %s %d",
  2213. name_prefix, pcm->device, name_suffix, channels);
  2214. kctl->private_value = (rtd->dai_link->id) | (session_type << 8) |
  2215. (channels << 16);
  2216. } else {
  2217. snprintf(mixer_name, ctl_len, "%s %d %s",
  2218. name_prefix, pcm->device, name_suffix);
  2219. kctl->private_value = (rtd->dai_link->id) | (session_type << 8);
  2220. }
  2221. kctl->name = mixer_name;
  2222. ret = snd_soc_add_component_controls(component, kctl, 1);
  2223. kfree(mixer_name);
  2224. return ret;
  2225. }
  2226. static int msm_pcm_channel_mixer_output_map_info(struct snd_kcontrol *kcontrol,
  2227. struct snd_ctl_elem_info *uinfo)
  2228. {
  2229. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2230. uinfo->count = PCM_FORMAT_MAX_NUM_CHANNEL_V8;
  2231. /* Valid channel map value ranges from 1 to 64 */
  2232. uinfo->value.integer.min = 1;
  2233. uinfo->value.integer.max = 64;
  2234. return 0;
  2235. }
  2236. static int msm_pcm_add_channel_mixer_output_map_controls(
  2237. struct snd_soc_pcm_runtime *rtd)
  2238. {
  2239. struct snd_pcm *pcm = rtd->pcm;
  2240. const char *playback_mixer_ctl_name = "AudStr";
  2241. const char *capture_mixer_ctl_name = "AudStr Capture";
  2242. const char *suffix = "ChMixer Output Map";
  2243. int session_type = 0, ret = 0, channel = -1;
  2244. struct snd_kcontrol_new channel_mixer_output_map_control = {
  2245. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2246. .name = "?",
  2247. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  2248. .info = msm_pcm_channel_mixer_output_map_info,
  2249. .put = msm_pcm_channel_mixer_output_map_ctl_put,
  2250. .get = msm_pcm_channel_mixer_output_map_ctl_get,
  2251. .private_value = 0,
  2252. };
  2253. if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream != NULL) {
  2254. session_type = SESSION_TYPE_RX;
  2255. ret = msm_pcm_add_platform_controls(&channel_mixer_output_map_control,
  2256. rtd, playback_mixer_ctl_name, suffix, session_type, channel);
  2257. if (ret < 0)
  2258. goto fail;
  2259. }
  2260. if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream != NULL) {
  2261. session_type = SESSION_TYPE_TX;
  2262. ret = msm_pcm_add_platform_controls(&channel_mixer_output_map_control,
  2263. rtd, capture_mixer_ctl_name, suffix, session_type, channel);
  2264. if (ret < 0)
  2265. goto fail;
  2266. }
  2267. return 0;
  2268. fail:
  2269. pr_err("%s: failed add platform ctl, err = %d\n",
  2270. __func__, ret);
  2271. return ret;
  2272. }
  2273. static int msm_pcm_channel_mixer_input_map_info(struct snd_kcontrol *kcontrol,
  2274. struct snd_ctl_elem_info *uinfo)
  2275. {
  2276. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2277. uinfo->count = PCM_FORMAT_MAX_NUM_CHANNEL_V8;
  2278. /* Valid channel map value ranges from 1 to 64 */
  2279. uinfo->value.integer.min = 1;
  2280. uinfo->value.integer.max = 64;
  2281. return 0;
  2282. }
  2283. static int msm_pcm_add_channel_mixer_input_map_controls(
  2284. struct snd_soc_pcm_runtime *rtd)
  2285. {
  2286. struct snd_pcm *pcm = rtd->pcm;
  2287. const char *playback_mixer_ctl_name = "AudStr";
  2288. const char *capture_mixer_ctl_name = "AudStr Capture";
  2289. const char *suffix = "ChMixer Input Map";
  2290. int session_type = 0, ret = 0, channel = -1;
  2291. struct snd_kcontrol_new channel_mixer_input_map_control = {
  2292. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2293. .name = "?",
  2294. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  2295. .info = msm_pcm_channel_mixer_input_map_info,
  2296. .put = msm_pcm_channel_mixer_input_map_ctl_put,
  2297. .get = msm_pcm_channel_mixer_input_map_ctl_get,
  2298. .private_value = 0,
  2299. };
  2300. if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream != NULL) {
  2301. session_type = SESSION_TYPE_RX;
  2302. ret = msm_pcm_add_platform_controls(&channel_mixer_input_map_control,
  2303. rtd, playback_mixer_ctl_name, suffix, session_type, channel);
  2304. if (ret < 0)
  2305. goto fail;
  2306. }
  2307. if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream != NULL) {
  2308. session_type = SESSION_TYPE_TX;
  2309. ret = msm_pcm_add_platform_controls(&channel_mixer_input_map_control,
  2310. rtd, capture_mixer_ctl_name, suffix, session_type, channel);
  2311. if (ret < 0)
  2312. goto fail;
  2313. }
  2314. return 0;
  2315. fail:
  2316. pr_err("%s: failed add platform ctl, err = %d\n",
  2317. __func__, ret);
  2318. return ret;
  2319. }
  2320. static int msm_pcm_channel_mixer_cfg_info(struct snd_kcontrol *kcontrol,
  2321. struct snd_ctl_elem_info *uinfo)
  2322. {
  2323. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2324. /* five int values: enable, rule, in_channels, out_channels and port_id */
  2325. uinfo->count = 5;
  2326. /* Valid range is all positive values to support above controls */
  2327. uinfo->value.integer.min = 0;
  2328. uinfo->value.integer.max = INT_MAX;
  2329. return 0;
  2330. }
  2331. static int msm_pcm_add_channel_mixer_cfg_controls(
  2332. struct snd_soc_pcm_runtime *rtd)
  2333. {
  2334. struct snd_pcm *pcm = rtd->pcm;
  2335. const char *playback_mixer_ctl_name = "AudStr";
  2336. const char *capture_mixer_ctl_name = "AudStr Capture";
  2337. const char *suffix = "ChMixer Cfg";
  2338. int session_type = 0, ret = 0, channel = -1;
  2339. struct msm_plat_data *pdata = NULL;
  2340. struct snd_soc_component *component = NULL;
  2341. struct snd_kcontrol_new channel_mixer_cfg_control = {
  2342. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2343. .name = "?",
  2344. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  2345. .info = msm_pcm_channel_mixer_cfg_info,
  2346. .put = msm_pcm_channel_mixer_cfg_ctl_put,
  2347. .get = msm_pcm_channel_mixer_cfg_ctl_get,
  2348. .private_value = 0,
  2349. };
  2350. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  2351. if (!component) {
  2352. pr_err("%s: component is NULL\n", __func__);
  2353. return -EINVAL;
  2354. }
  2355. pdata = (struct msm_plat_data *)
  2356. dev_get_drvdata(component->dev);
  2357. pdata->pcm_device[rtd->dai_link->id] = rtd->pcm;
  2358. if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream != NULL) {
  2359. session_type = SESSION_TYPE_RX;
  2360. ret = msm_pcm_add_platform_controls(&channel_mixer_cfg_control,
  2361. rtd, playback_mixer_ctl_name, suffix, session_type, channel);
  2362. if (ret < 0)
  2363. goto fail;
  2364. }
  2365. if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream != NULL) {
  2366. session_type = SESSION_TYPE_TX;
  2367. ret = msm_pcm_add_platform_controls(&channel_mixer_cfg_control,
  2368. rtd, capture_mixer_ctl_name, suffix, session_type, channel);
  2369. if (ret < 0)
  2370. goto fail;
  2371. }
  2372. return 0;
  2373. fail:
  2374. pr_err("%s: failed add platform ctl, err = %d\n",
  2375. __func__, ret);
  2376. return ret;
  2377. }
  2378. static int msm_pcm_channel_mixer_weight_info(struct snd_kcontrol *kcontrol,
  2379. struct snd_ctl_elem_info *uinfo)
  2380. {
  2381. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2382. uinfo->count = PCM_FORMAT_MAX_NUM_CHANNEL_V8;
  2383. /* Valid range: 0 to 0x4000(Unity) gain weightage */
  2384. uinfo->value.integer.min = 0;
  2385. uinfo->value.integer.max = 0x4000;
  2386. return 0;
  2387. }
  2388. static int msm_pcm_add_channel_mixer_weight_controls(
  2389. struct snd_soc_pcm_runtime *rtd,
  2390. int channel)
  2391. {
  2392. struct snd_pcm *pcm = rtd->pcm;
  2393. const char *playback_mixer_ctl_name = "AudStr";
  2394. const char *capture_mixer_ctl_name = "AudStr Capture";
  2395. const char *suffix = "ChMixer Weight Ch";
  2396. int session_type = 0, ret = 0;
  2397. struct snd_kcontrol_new channel_mixer_weight_control = {
  2398. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2399. .name = "?",
  2400. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  2401. .info = msm_pcm_channel_mixer_weight_info,
  2402. .put = msm_pcm_channel_mixer_weight_ctl_put,
  2403. .get = msm_pcm_channel_mixer_weight_ctl_get,
  2404. .private_value = 0,
  2405. };
  2406. if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream != NULL) {
  2407. session_type = SESSION_TYPE_RX;
  2408. ret = msm_pcm_add_platform_controls(&channel_mixer_weight_control,
  2409. rtd, playback_mixer_ctl_name, suffix, session_type, channel);
  2410. if (ret < 0)
  2411. goto fail;
  2412. }
  2413. if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream != NULL) {
  2414. session_type = SESSION_TYPE_TX;
  2415. ret = msm_pcm_add_platform_controls(&channel_mixer_weight_control,
  2416. rtd, capture_mixer_ctl_name, suffix, session_type, channel);
  2417. if (ret < 0)
  2418. goto fail;
  2419. }
  2420. return 0;
  2421. fail:
  2422. pr_err("%s: failed add platform ctl, err = %d\n",
  2423. __func__, ret);
  2424. return ret;
  2425. }
  2426. static int msm_pcm_add_channel_mixer_controls(struct snd_soc_pcm_runtime *rtd)
  2427. {
  2428. int i, ret = 0;
  2429. struct snd_pcm *pcm = NULL;
  2430. struct msm_plat_data *pdata = NULL;
  2431. struct snd_soc_component *component = NULL;
  2432. if (!rtd || !rtd->pcm) {
  2433. pr_err("%s invalid rtd or pcm\n", __func__);
  2434. return -EINVAL;
  2435. }
  2436. pcm = rtd->pcm;
  2437. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  2438. if (!component) {
  2439. pr_err("%s: component is NULL\n", __func__);
  2440. return -EINVAL;
  2441. }
  2442. pdata = (struct msm_plat_data *)
  2443. dev_get_drvdata(component->dev);
  2444. if (!pdata) {
  2445. pr_err("%s: platform data not populated\n", __func__);
  2446. return -EINVAL;
  2447. }
  2448. if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream &&
  2449. !pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_RX]) {
  2450. pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_RX] =
  2451. kzalloc(sizeof(struct msm_pcm_channel_mixer), GFP_KERNEL);
  2452. if (!pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_RX]) {
  2453. ret = -ENOMEM;
  2454. goto fail;
  2455. }
  2456. }
  2457. if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream &&
  2458. !pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_TX]) {
  2459. pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_TX] =
  2460. kzalloc(sizeof(struct msm_pcm_channel_mixer), GFP_KERNEL);
  2461. if (!pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_TX]) {
  2462. ret = -ENOMEM;
  2463. goto fail;
  2464. }
  2465. }
  2466. ret = msm_pcm_add_channel_mixer_cfg_controls(rtd);
  2467. if (ret) {
  2468. pr_err("%s: pcm add channel mixer cfg controls failed:%d\n",
  2469. __func__, ret);
  2470. goto fail;
  2471. }
  2472. ret = msm_pcm_add_channel_mixer_input_map_controls(rtd);
  2473. if (ret) {
  2474. pr_err("%s: pcm add channel mixer input map controls failed:%d\n",
  2475. __func__, ret);
  2476. goto fail;
  2477. }
  2478. ret = msm_pcm_add_channel_mixer_output_map_controls(rtd);
  2479. if (ret) {
  2480. pr_err("%s: pcm add channel mixer output map controls failed:%d\n",
  2481. __func__, ret);
  2482. goto fail;
  2483. }
  2484. for (i = 1; i <= PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++) {
  2485. ret = msm_pcm_add_channel_mixer_weight_controls(rtd, i);
  2486. if (ret) {
  2487. pr_err("%s: pcm add channel mixer weight controls failed:%d\n",
  2488. __func__, ret);
  2489. goto fail;
  2490. }
  2491. }
  2492. return 0;
  2493. fail:
  2494. kfree(pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_RX]);
  2495. kfree(pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_TX]);
  2496. pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_RX] = NULL;
  2497. pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_TX] = NULL;
  2498. return ret;
  2499. }
  2500. static int msm_pcm_add_controls(struct snd_soc_pcm_runtime *rtd)
  2501. {
  2502. int ret = 0;
  2503. pr_debug("%s\n", __func__);
  2504. ret = msm_pcm_add_chmap_controls(rtd);
  2505. if (ret)
  2506. pr_err("%s: pcm add controls failed:%d\n", __func__, ret);
  2507. ret = msm_pcm_add_app_type_controls(rtd);
  2508. if (ret)
  2509. pr_err("%s: pcm add app type controls failed:%d\n",
  2510. __func__, ret);
  2511. ret = msm_pcm_add_channel_mixer_controls(rtd);
  2512. if (ret)
  2513. pr_err("%s: pcm add channel mixer controls failed:%d\n",
  2514. __func__, ret);
  2515. return ret;
  2516. }
  2517. static int msm_asoc_pcm_new(struct snd_soc_pcm_runtime *rtd)
  2518. {
  2519. struct snd_card *card = rtd->card->snd_card;
  2520. int ret = 0;
  2521. if (!card->dev->coherent_dma_mask)
  2522. card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
  2523. ret = msm_pcm_add_controls(rtd);
  2524. if (ret) {
  2525. pr_err("%s, kctl add failed:%d\n", __func__, ret);
  2526. return ret;
  2527. }
  2528. ret = msm_pcm_add_volume_control(rtd, SNDRV_PCM_STREAM_PLAYBACK);
  2529. if (ret)
  2530. pr_err("%s: Could not add pcm Volume Control %d\n",
  2531. __func__, ret);
  2532. ret = msm_pcm_add_volume_control(rtd, SNDRV_PCM_STREAM_CAPTURE);
  2533. if (ret)
  2534. pr_err("%s: Could not add pcm Volume Control %d\n",
  2535. __func__, ret);
  2536. ret = msm_pcm_add_compress_control(rtd);
  2537. if (ret)
  2538. pr_err("%s: Could not add pcm Compress Control %d\n",
  2539. __func__, ret);
  2540. ret = msm_pcm_add_audio_adsp_stream_cmd_control(rtd);
  2541. if (ret)
  2542. pr_err("%s: Could not add pcm ADSP Stream Cmd Control\n",
  2543. __func__);
  2544. ret = msm_pcm_add_audio_adsp_stream_callback_control(rtd);
  2545. if (ret)
  2546. pr_err("%s: Could not add pcm ADSP Stream Callback Control\n",
  2547. __func__);
  2548. return ret;
  2549. }
  2550. #if IS_ENABLED(CONFIG_AUDIO_QGKI)
  2551. static snd_pcm_sframes_t msm_pcm_delay_blk(struct snd_pcm_substream *substream,
  2552. struct snd_soc_dai *dai)
  2553. {
  2554. struct snd_pcm_runtime *runtime = substream->runtime;
  2555. struct msm_audio *prtd = runtime->private_data;
  2556. struct audio_client *ac = prtd->audio_client;
  2557. snd_pcm_sframes_t frames;
  2558. int ret;
  2559. ret = q6asm_get_path_delay(prtd->audio_client);
  2560. if (ret) {
  2561. pr_err("%s: get_path_delay failed, ret=%d\n", __func__, ret);
  2562. return 0;
  2563. }
  2564. /* convert microseconds to frames */
  2565. frames = ac->path_delay / 1000 * runtime->rate / 1000;
  2566. /* also convert the remainder from the initial division */
  2567. frames += ac->path_delay % 1000 * runtime->rate / 1000000;
  2568. /* overcompensate for the loss of precision (empirical) */
  2569. frames += 2;
  2570. return frames;
  2571. }
  2572. #endif /* CONFIG_AUDIO_QGKI */
  2573. static struct snd_soc_component_driver msm_soc_component = {
  2574. .name = DRV_NAME,
  2575. .ops = &msm_pcm_ops,
  2576. .pcm_new = msm_asoc_pcm_new,
  2577. #if IS_ENABLED(CONFIG_AUDIO_QGKI)
  2578. .delay_blk = msm_pcm_delay_blk,
  2579. #endif /* CONFIG_AUDIO_QGKI */
  2580. };
  2581. static int msm_pcm_probe(struct platform_device *pdev)
  2582. {
  2583. int rc;
  2584. int id;
  2585. struct msm_plat_data *pdata;
  2586. const char *latency_level;
  2587. rc = of_property_read_u32(pdev->dev.of_node,
  2588. "qcom,msm-pcm-dsp-id", &id);
  2589. if (rc) {
  2590. dev_err(&pdev->dev, "%s: qcom,msm-pcm-dsp-id missing in DT node\n",
  2591. __func__);
  2592. return rc;
  2593. }
  2594. pdata = kzalloc(sizeof(struct msm_plat_data), GFP_KERNEL);
  2595. if (!pdata)
  2596. return -ENOMEM;
  2597. if (of_property_read_bool(pdev->dev.of_node,
  2598. "qcom,msm-pcm-low-latency")) {
  2599. pdata->perf_mode = LOW_LATENCY_PCM_MODE;
  2600. rc = of_property_read_string(pdev->dev.of_node,
  2601. "qcom,latency-level", &latency_level);
  2602. if (!rc) {
  2603. if (!strcmp(latency_level, "ultra"))
  2604. pdata->perf_mode = ULTRA_LOW_LATENCY_PCM_MODE;
  2605. else if (!strcmp(latency_level, "ull-pp"))
  2606. pdata->perf_mode =
  2607. ULL_POST_PROCESSING_PCM_MODE;
  2608. }
  2609. } else {
  2610. pdata->perf_mode = LEGACY_PCM_MODE;
  2611. }
  2612. mutex_init(&pdata->lock);
  2613. dev_set_drvdata(&pdev->dev, pdata);
  2614. dev_dbg(&pdev->dev, "%s: dev name %s\n",
  2615. __func__, dev_name(&pdev->dev));
  2616. return snd_soc_register_component(&pdev->dev,
  2617. &msm_soc_component,
  2618. NULL, 0);
  2619. }
  2620. static int msm_pcm_remove(struct platform_device *pdev)
  2621. {
  2622. struct msm_plat_data *pdata;
  2623. int i = 0;
  2624. pdata = dev_get_drvdata(&pdev->dev);
  2625. if (pdata) {
  2626. for (i = 0; i < MSM_FRONTEND_DAI_MM_SIZE; i++) {
  2627. kfree(pdata->chmixer_pspd[i][SESSION_TYPE_RX]);
  2628. kfree(pdata->chmixer_pspd[i][SESSION_TYPE_TX]);
  2629. }
  2630. }
  2631. mutex_destroy(&pdata->lock);
  2632. kfree(pdata);
  2633. snd_soc_unregister_component(&pdev->dev);
  2634. return 0;
  2635. }
  2636. static const struct of_device_id msm_pcm_dt_match[] = {
  2637. {.compatible = "qcom,msm-pcm-dsp"},
  2638. {}
  2639. };
  2640. MODULE_DEVICE_TABLE(of, msm_pcm_dt_match);
  2641. static struct platform_driver msm_pcm_driver = {
  2642. .driver = {
  2643. .name = "msm-pcm-dsp",
  2644. .owner = THIS_MODULE,
  2645. .of_match_table = msm_pcm_dt_match,
  2646. .suppress_bind_attrs = true,
  2647. },
  2648. .probe = msm_pcm_probe,
  2649. .remove = msm_pcm_remove,
  2650. };
  2651. int __init msm_pcm_dsp_init(void)
  2652. {
  2653. init_waitqueue_head(&the_locks.enable_wait);
  2654. init_waitqueue_head(&the_locks.eos_wait);
  2655. init_waitqueue_head(&the_locks.write_wait);
  2656. init_waitqueue_head(&the_locks.read_wait);
  2657. return platform_driver_register(&msm_pcm_driver);
  2658. }
  2659. void msm_pcm_dsp_exit(void)
  2660. {
  2661. platform_driver_unregister(&msm_pcm_driver);
  2662. }
  2663. MODULE_DESCRIPTION("PCM module platform driver");
  2664. MODULE_LICENSE("GPL v2");