msm-pcm-q6-v2.c 82 KB

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