msm-compress-q6-v2.c 167 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786
  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/math64.h>
  10. #include <linux/wait.h>
  11. #include <linux/mutex.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/slab.h>
  14. #include <sound/core.h>
  15. #include <sound/soc.h>
  16. #include <sound/soc-dapm.h>
  17. #include <sound/pcm.h>
  18. #include <sound/initval.h>
  19. #include <sound/control.h>
  20. #include <sound/pcm_params.h>
  21. #include <audio/sound/audio_effects.h>
  22. #include <audio/sound/audio_compressed_formats.h>
  23. #include <asm/dma.h>
  24. #include <linux/dma-mapping.h>
  25. #include <audio/linux/msm_audio.h>
  26. #include <sound/timer.h>
  27. #include <sound/tlv.h>
  28. #include <sound/compress_params.h>
  29. #include <sound/compress_offload.h>
  30. #include <sound/compress_driver.h>
  31. #include <dsp/msm_audio_ion.h>
  32. #include <dsp/apr_audio-v2.h>
  33. #include <dsp/q6asm-v2.h>
  34. #include <dsp/q6core.h>
  35. #include <dsp/msm-audio-effects-q6-v2.h>
  36. #include "msm-pcm-routing-v2.h"
  37. #include "msm-qti-pp-config.h"
  38. #define DRV_NAME "msm-compress-q6-v2"
  39. #define TIMEOUT_MS 1000
  40. #define DSP_PP_BUFFERING_IN_MSEC 25
  41. #define PARTIAL_DRAIN_ACK_EARLY_BY_MSEC 150
  42. #define MP3_OUTPUT_FRAME_SZ 1152
  43. #define AAC_OUTPUT_FRAME_SZ 1024
  44. #define AC3_OUTPUT_FRAME_SZ 1536
  45. #define EAC3_OUTPUT_FRAME_SZ 1536
  46. #define DSP_NUM_OUTPUT_FRAME_BUFFERED 2
  47. #define FLAC_BLK_SIZE_LIMIT 65535
  48. /* Timestamp mode payload offsets */
  49. #define CAPTURE_META_DATA_TS_OFFSET_LSW 6
  50. #define CAPTURE_META_DATA_TS_OFFSET_MSW 7
  51. /* decoder parameter length */
  52. #define DDP_DEC_MAX_NUM_PARAM 18
  53. /* Default values used if user space does not set */
  54. #define COMPR_PLAYBACK_MIN_FRAGMENT_SIZE (8 * 1024)
  55. #define COMPR_PLAYBACK_MAX_FRAGMENT_SIZE (128 * 1024)
  56. #define COMPR_PLAYBACK_MIN_NUM_FRAGMENTS (4)
  57. #define COMPR_PLAYBACK_MAX_NUM_FRAGMENTS (16 * 4)
  58. #define COMPRESSED_LR_VOL_MAX_STEPS 0x2000
  59. const DECLARE_TLV_DB_LINEAR(msm_compr_vol_gain, 0,
  60. COMPRESSED_LR_VOL_MAX_STEPS);
  61. /* Stream id switches between 1 and 2 */
  62. #define NEXT_STREAM_ID(stream_id) ((stream_id & 1) + 1)
  63. #define STREAM_ARRAY_INDEX(stream_id) (stream_id - 1)
  64. #define MAX_NUMBER_OF_STREAMS 2
  65. #define SND_DEC_DDP_MAX_PARAMS 18
  66. #ifndef COMPRESSED_PERF_MODE_FLAG
  67. #define COMPRESSED_PERF_MODE_FLAG 0
  68. #endif
  69. #define DSD_BLOCK_SIZE_4 4
  70. struct msm_compr_gapless_state {
  71. bool set_next_stream_id;
  72. int32_t stream_opened[MAX_NUMBER_OF_STREAMS];
  73. uint32_t initial_samples_drop;
  74. uint32_t trailing_samples_drop;
  75. uint32_t gapless_transition;
  76. bool use_dsp_gapless_mode;
  77. union snd_codec_options codec_options;
  78. };
  79. static unsigned int supported_sample_rates[] = {
  80. 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000, 64000,
  81. 88200, 96000, 128000, 144000, 176400, 192000, 352800, 384000, 2822400,
  82. 5644800
  83. };
  84. struct msm_compr_pdata {
  85. struct snd_compr_stream *cstream[MSM_FRONTEND_DAI_MAX];
  86. uint32_t volume[MSM_FRONTEND_DAI_MAX][2]; /* For both L & R */
  87. struct msm_compr_audio_effects *audio_effects[MSM_FRONTEND_DAI_MAX];
  88. bool use_dsp_gapless_mode;
  89. bool use_legacy_api; /* indicates use older asm apis*/
  90. struct msm_compr_dec_params *dec_params[MSM_FRONTEND_DAI_MAX];
  91. struct msm_compr_ch_map *ch_map[MSM_FRONTEND_DAI_MAX];
  92. bool is_in_use[MSM_FRONTEND_DAI_MAX];
  93. struct msm_pcm_channel_mixer *chmixer_pspd[MSM_FRONTEND_DAI_MM_SIZE];
  94. struct mutex lock;
  95. };
  96. struct msm_compr_audio {
  97. struct snd_compr_stream *cstream;
  98. struct snd_compr_caps compr_cap;
  99. struct snd_compr_codec_caps codec_caps;
  100. struct snd_compr_params codec_param;
  101. struct audio_client *audio_client;
  102. uint32_t codec;
  103. uint32_t compr_passthr;
  104. void *buffer; /* virtual address */
  105. phys_addr_t buffer_paddr; /* physical address */
  106. uint32_t app_pointer;
  107. uint32_t buffer_size;
  108. uint32_t byte_offset;
  109. uint64_t copied_total; /* bytes consumed by DSP */
  110. uint64_t bytes_received; /* from userspace */
  111. uint64_t bytes_sent; /* to DSP */
  112. uint64_t received_total; /* bytes received from DSP */
  113. uint64_t bytes_copied; /* to userspace */
  114. uint64_t bytes_read; /* from DSP */
  115. uint32_t bytes_read_offset; /* bytes read offset */
  116. uint32_t ts_header_offset; /* holds the timestamp header offset */
  117. int32_t first_buffer;
  118. int32_t last_buffer;
  119. #if !IS_ENABLED(CONFIG_AUDIO_QGKI)
  120. int32_t zero_buffer;
  121. #endif
  122. int32_t partial_drain_delay;
  123. uint16_t session_id;
  124. uint32_t sample_rate;
  125. uint32_t num_channels;
  126. /*
  127. * convention - commands coming from the same thread
  128. * can use the common cmd_ack var. Others (e.g drain/EOS)
  129. * must use separate vars to track command status.
  130. */
  131. uint32_t cmd_ack;
  132. uint32_t cmd_interrupt;
  133. uint32_t drain_ready;
  134. uint32_t eos_ack;
  135. uint32_t stream_available;
  136. uint32_t next_stream;
  137. uint32_t run_mode;
  138. uint32_t start_delay_lsw;
  139. uint32_t start_delay_msw;
  140. uint64_t marker_timestamp;
  141. struct msm_compr_gapless_state gapless_state;
  142. atomic_t start;
  143. atomic_t eos;
  144. atomic_t drain;
  145. #if !IS_ENABLED(CONFIG_AUDIO_QGKI)
  146. atomic_t partial_drain;
  147. #endif
  148. atomic_t xrun;
  149. atomic_t close;
  150. atomic_t wait_on_close;
  151. atomic_t error;
  152. wait_queue_head_t eos_wait;
  153. wait_queue_head_t drain_wait;
  154. wait_queue_head_t close_wait;
  155. wait_queue_head_t wait_for_stream_avail;
  156. spinlock_t lock;
  157. };
  158. const u32 compr_codecs[] = {
  159. SND_AUDIOCODEC_AC3, SND_AUDIOCODEC_EAC3, SND_AUDIOCODEC_DTS,
  160. SND_AUDIOCODEC_TRUEHD, SND_AUDIOCODEC_IEC61937};
  161. struct query_audio_effect {
  162. uint32_t mod_id;
  163. uint32_t parm_id;
  164. uint32_t size;
  165. uint32_t offset;
  166. uint32_t device;
  167. };
  168. struct msm_compr_audio_effects {
  169. struct bass_boost_params bass_boost;
  170. struct pbe_params pbe;
  171. struct virtualizer_params virtualizer;
  172. struct reverb_params reverb;
  173. struct eq_params equalizer;
  174. struct soft_volume_params volume;
  175. struct query_audio_effect query;
  176. };
  177. struct snd_dec_ddp {
  178. __u32 params_length;
  179. __u32 params_id[SND_DEC_DDP_MAX_PARAMS];
  180. __u32 params_value[SND_DEC_DDP_MAX_PARAMS];
  181. } __attribute__((packed, aligned(4)));
  182. struct msm_compr_dec_params {
  183. struct snd_dec_ddp ddp_params;
  184. };
  185. struct msm_compr_ch_map {
  186. bool set_ch_map;
  187. char channel_map[PCM_FORMAT_MAX_NUM_CHANNEL_V8];
  188. };
  189. static int msm_compr_send_dec_params(struct snd_compr_stream *cstream,
  190. struct msm_compr_dec_params *dec_params,
  191. int stream_id);
  192. static int msm_compr_set_render_mode(struct msm_compr_audio *prtd,
  193. uint32_t render_mode) {
  194. int ret = -EINVAL;
  195. struct audio_client *ac = prtd->audio_client;
  196. pr_debug("%s, got render mode %u\n", __func__, render_mode);
  197. if (render_mode == SNDRV_COMPRESS_RENDER_MODE_AUDIO_MASTER) {
  198. render_mode = ASM_SESSION_MTMX_STRTR_PARAM_RENDER_DEFAULT;
  199. } else if (render_mode == SNDRV_COMPRESS_RENDER_MODE_STC_MASTER) {
  200. render_mode = ASM_SESSION_MTMX_STRTR_PARAM_RENDER_LOCAL_STC;
  201. prtd->run_mode = ASM_SESSION_CMD_RUN_STARTIME_RUN_WITH_DELAY;
  202. } else {
  203. pr_err("%s, Invalid render mode %u\n", __func__,
  204. render_mode);
  205. ret = -EINVAL;
  206. goto exit;
  207. }
  208. ret = q6asm_send_mtmx_strtr_render_mode(ac, render_mode);
  209. if (ret) {
  210. pr_err("%s, Render mode can't be set error %d\n", __func__,
  211. ret);
  212. }
  213. exit:
  214. return ret;
  215. }
  216. static int msm_compr_set_clk_rec_mode(struct audio_client *ac,
  217. uint32_t clk_rec_mode) {
  218. int ret = -EINVAL;
  219. pr_debug("%s, got clk rec mode %u\n", __func__, clk_rec_mode);
  220. if (clk_rec_mode == SNDRV_COMPRESS_CLK_REC_MODE_NONE) {
  221. clk_rec_mode = ASM_SESSION_MTMX_STRTR_PARAM_CLK_REC_NONE;
  222. } else if (clk_rec_mode == SNDRV_COMPRESS_CLK_REC_MODE_AUTO) {
  223. clk_rec_mode = ASM_SESSION_MTMX_STRTR_PARAM_CLK_REC_AUTO;
  224. } else {
  225. pr_err("%s, Invalid clk rec_mode mode %u\n", __func__,
  226. clk_rec_mode);
  227. ret = -EINVAL;
  228. goto exit;
  229. }
  230. ret = q6asm_send_mtmx_strtr_clk_rec_mode(ac, clk_rec_mode);
  231. if (ret) {
  232. pr_err("%s, clk rec mode can't be set, error %d\n", __func__,
  233. ret);
  234. }
  235. exit:
  236. return ret;
  237. }
  238. static int msm_compr_set_render_window(struct audio_client *ac,
  239. uint32_t ws_lsw, uint32_t ws_msw,
  240. uint32_t we_lsw, uint32_t we_msw)
  241. {
  242. int ret = -EINVAL;
  243. struct asm_session_mtmx_strtr_param_window_v2_t asm_mtmx_strtr_window;
  244. uint32_t param_id;
  245. pr_debug("%s, ws_lsw 0x%x ws_msw 0x%x we_lsw 0x%x we_ms 0x%x\n",
  246. __func__, ws_lsw, ws_msw, we_lsw, we_msw);
  247. memset(&asm_mtmx_strtr_window, 0,
  248. sizeof(struct asm_session_mtmx_strtr_param_window_v2_t));
  249. asm_mtmx_strtr_window.window_lsw = ws_lsw;
  250. asm_mtmx_strtr_window.window_msw = ws_msw;
  251. param_id = ASM_SESSION_MTMX_STRTR_PARAM_RENDER_WINDOW_START_V2;
  252. ret = q6asm_send_mtmx_strtr_window(ac, &asm_mtmx_strtr_window,
  253. param_id);
  254. if (ret) {
  255. pr_err("%s, start window can't be set error %d\n", __func__,
  256. ret);
  257. goto exit;
  258. }
  259. asm_mtmx_strtr_window.window_lsw = we_lsw;
  260. asm_mtmx_strtr_window.window_msw = we_msw;
  261. param_id = ASM_SESSION_MTMX_STRTR_PARAM_RENDER_WINDOW_END_V2;
  262. ret = q6asm_send_mtmx_strtr_window(ac, &asm_mtmx_strtr_window,
  263. param_id);
  264. if (ret) {
  265. pr_err("%s, end window can't be set error %d\n", __func__,
  266. ret);
  267. }
  268. exit:
  269. return ret;
  270. }
  271. static int msm_compr_enable_adjust_session_clock(struct audio_client *ac,
  272. bool enable)
  273. {
  274. int ret;
  275. pr_debug("%s, enable adjust_session %d\n", __func__, enable);
  276. ret = q6asm_send_mtmx_strtr_enable_adjust_session_clock(ac, enable);
  277. if (ret)
  278. pr_err("%s, adjust session clock can't be set error %d\n",
  279. __func__, ret);
  280. return ret;
  281. }
  282. static int msm_compr_adjust_session_clock(struct audio_client *ac,
  283. uint32_t adjust_session_lsw, uint32_t adjust_session_msw)
  284. {
  285. int ret;
  286. pr_debug("%s, adjust_session_time_msw 0x%x adjust_session_time_lsw 0x%x\n",
  287. __func__, adjust_session_msw, adjust_session_lsw);
  288. ret = q6asm_adjust_session_clock(ac,
  289. adjust_session_lsw,
  290. adjust_session_msw);
  291. if (ret)
  292. pr_err("%s, adjust session clock can't be set error %d\n",
  293. __func__, ret);
  294. return ret;
  295. }
  296. static int msm_compr_set_volume(struct snd_compr_stream *cstream,
  297. uint32_t volume_l, uint32_t volume_r)
  298. {
  299. struct msm_compr_audio *prtd;
  300. int rc = 0;
  301. uint32_t avg_vol, gain_list[VOLUME_CONTROL_MAX_CHANNELS];
  302. uint32_t num_channels;
  303. struct snd_soc_pcm_runtime *rtd;
  304. struct snd_soc_component *component = NULL;
  305. struct msm_compr_pdata *pdata;
  306. bool use_default = true;
  307. u8 *chmap = NULL;
  308. pr_debug("%s: volume_l %d volume_r %d\n",
  309. __func__, volume_l, volume_r);
  310. if (!cstream || !cstream->runtime) {
  311. pr_err("%s: session not active\n", __func__);
  312. return -EPERM;
  313. }
  314. rtd = cstream->private_data;
  315. prtd = cstream->runtime->private_data;
  316. if (!rtd || !prtd || !prtd->audio_client) {
  317. pr_err("%s: invalid rtd, prtd or audio client", __func__);
  318. return rc;
  319. }
  320. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  321. if (!component) {
  322. pr_err("%s: invalid component\n", __func__);
  323. return rc;
  324. }
  325. pdata = snd_soc_component_get_drvdata(component);
  326. if (!pdata)
  327. return -EINVAL;
  328. if (prtd->compr_passthr != LEGACY_PCM) {
  329. pr_debug("%s: No volume config for passthrough %d\n",
  330. __func__, prtd->compr_passthr);
  331. return rc;
  332. }
  333. if (!rtd->dai_link || !pdata->ch_map[rtd->dai_link->id])
  334. return -EINVAL;
  335. use_default = !(pdata->ch_map[rtd->dai_link->id]->set_ch_map);
  336. chmap = pdata->ch_map[rtd->dai_link->id]->channel_map;
  337. num_channels = prtd->num_channels;
  338. if (prtd->num_channels > 2) {
  339. /*
  340. * Currently the left and right gains are averaged an applied
  341. * to all channels. This might not be desirable. But currently,
  342. * there exists no API in userspace to send a list of gains for
  343. * each channel either. If such an API does become available,
  344. * the mixer control must be updated to accept more than 2
  345. * channel gains.
  346. *
  347. */
  348. avg_vol = (volume_l + volume_r) / 2;
  349. rc = q6asm_set_volume(prtd->audio_client, avg_vol);
  350. } else {
  351. gain_list[0] = volume_l;
  352. gain_list[1] = volume_r;
  353. gain_list[2] = volume_l;
  354. if (use_default)
  355. num_channels = 3;
  356. rc = q6asm_set_multich_gain(prtd->audio_client, num_channels,
  357. gain_list, chmap, use_default);
  358. }
  359. if (rc < 0)
  360. pr_err("%s: Send vol gain command failed rc=%d\n",
  361. __func__, rc);
  362. return rc;
  363. }
  364. static int msm_compr_send_ddp_cfg(struct audio_client *ac,
  365. struct snd_dec_ddp *ddp,
  366. int stream_id)
  367. {
  368. int i, rc;
  369. pr_debug("%s\n", __func__);
  370. for (i = 0; i < ddp->params_length; i++) {
  371. rc = q6asm_ds1_set_stream_endp_params(ac, ddp->params_id[i],
  372. ddp->params_value[i],
  373. stream_id);
  374. if (rc) {
  375. pr_err("sending params_id: %d failed\n",
  376. ddp->params_id[i]);
  377. return rc;
  378. }
  379. }
  380. return 0;
  381. }
  382. static int msm_compr_send_buffer(struct msm_compr_audio *prtd)
  383. {
  384. int buffer_length;
  385. uint64_t bytes_available;
  386. struct audio_aio_write_param param;
  387. struct snd_codec_metadata *buff_addr;
  388. if (!atomic_read(&prtd->start)) {
  389. pr_err("%s: stream is not in started state\n", __func__);
  390. return -EINVAL;
  391. }
  392. if (atomic_read(&prtd->xrun)) {
  393. WARN(1, "%s called while xrun is true", __func__);
  394. return -EPERM;
  395. }
  396. pr_debug("%s: bytes_received = %llu copied_total = %llu\n",
  397. __func__, prtd->bytes_received, prtd->copied_total);
  398. if (prtd->first_buffer && prtd->gapless_state.use_dsp_gapless_mode &&
  399. prtd->compr_passthr == LEGACY_PCM)
  400. q6asm_stream_send_meta_data(prtd->audio_client,
  401. prtd->audio_client->stream_id,
  402. prtd->gapless_state.initial_samples_drop,
  403. prtd->gapless_state.trailing_samples_drop);
  404. buffer_length = prtd->codec_param.buffer.fragment_size;
  405. bytes_available = prtd->bytes_received - prtd->copied_total;
  406. if (bytes_available < prtd->codec_param.buffer.fragment_size)
  407. buffer_length = bytes_available;
  408. if (prtd->byte_offset + buffer_length > prtd->buffer_size) {
  409. buffer_length = (prtd->buffer_size - prtd->byte_offset);
  410. pr_debug("%s: wrap around situation, send partial data %d now",
  411. __func__, buffer_length);
  412. }
  413. if (buffer_length) {
  414. param.paddr = prtd->buffer_paddr + prtd->byte_offset;
  415. WARN(prtd->byte_offset % 32 != 0, "offset %x not multiple of 32\n",
  416. prtd->byte_offset);
  417. } else {
  418. param.paddr = prtd->buffer_paddr;
  419. }
  420. param.len = buffer_length;
  421. if (prtd->ts_header_offset) {
  422. buff_addr = (struct snd_codec_metadata *)
  423. (prtd->buffer + prtd->byte_offset);
  424. param.len = buff_addr->length;
  425. param.msw_ts = (uint32_t)
  426. ((buff_addr->timestamp & 0xFFFFFFFF00000000LL) >> 32);
  427. param.lsw_ts = (uint32_t) (buff_addr->timestamp & 0xFFFFFFFFLL);
  428. param.paddr += prtd->ts_header_offset;
  429. param.flags = SET_TIMESTAMP;
  430. param.metadata_len = prtd->ts_header_offset;
  431. } else {
  432. param.msw_ts = 0;
  433. param.lsw_ts = 0;
  434. param.flags = NO_TIMESTAMP;
  435. param.metadata_len = 0;
  436. }
  437. param.uid = buffer_length;
  438. param.last_buffer = prtd->last_buffer;
  439. pr_debug("%s: sending %d bytes to DSP byte_offset = %d\n",
  440. __func__, param.len, prtd->byte_offset);
  441. if (q6asm_async_write(prtd->audio_client, &param) < 0) {
  442. pr_err("%s:q6asm_async_write failed\n", __func__);
  443. } else {
  444. prtd->bytes_sent += buffer_length;
  445. if (prtd->first_buffer)
  446. prtd->first_buffer = 0;
  447. }
  448. return 0;
  449. }
  450. static int msm_compr_read_buffer(struct msm_compr_audio *prtd)
  451. {
  452. int buffer_length;
  453. uint64_t bytes_available;
  454. uint64_t buffer_sent;
  455. struct audio_aio_read_param param;
  456. int ret;
  457. if (!atomic_read(&prtd->start)) {
  458. pr_err("%s: stream is not in started state\n", __func__);
  459. return -EINVAL;
  460. }
  461. buffer_length = prtd->codec_param.buffer.fragment_size -
  462. prtd->ts_header_offset;
  463. bytes_available = prtd->received_total - prtd->bytes_copied;
  464. buffer_sent = prtd->bytes_read - prtd->bytes_copied;
  465. if (buffer_sent + buffer_length + prtd->ts_header_offset
  466. > prtd->buffer_size) {
  467. pr_debug(" %s : Buffer is Full bytes_available: %llu\n",
  468. __func__, bytes_available);
  469. return 0;
  470. }
  471. memset(&param, 0x0, sizeof(struct audio_aio_read_param));
  472. param.paddr = prtd->buffer_paddr + prtd->bytes_read_offset +
  473. prtd->ts_header_offset;
  474. param.len = buffer_length;
  475. param.uid = buffer_length;
  476. /* reserved[1] is for flags */
  477. param.flags = prtd->codec_param.codec.reserved[1];
  478. pr_debug("%s: reading %d bytes from DSP byte_offset = %llu\n",
  479. __func__, buffer_length, prtd->bytes_read);
  480. ret = q6asm_async_read(prtd->audio_client, &param);
  481. if (ret < 0) {
  482. pr_err("%s: q6asm_async_read failed - %d\n",
  483. __func__, ret);
  484. return ret;
  485. }
  486. prtd->bytes_read += buffer_length + prtd->ts_header_offset;
  487. prtd->bytes_read_offset += buffer_length + prtd->ts_header_offset;
  488. if (prtd->bytes_read_offset >= prtd->buffer_size)
  489. prtd->bytes_read_offset -= prtd->buffer_size;
  490. return 0;
  491. }
  492. static void compr_event_handler(uint32_t opcode,
  493. uint32_t token, uint32_t *payload, void *priv)
  494. {
  495. struct msm_compr_audio *prtd = priv;
  496. struct snd_compr_stream *cstream;
  497. struct audio_client *ac;
  498. uint32_t chan_mode = 0;
  499. uint32_t sample_rate = 0;
  500. uint64_t bytes_available;
  501. int stream_id;
  502. uint32_t stream_index;
  503. unsigned long flags;
  504. uint64_t read_size;
  505. uint32_t *buff_addr;
  506. struct snd_soc_pcm_runtime *rtd;
  507. int ret = 0;
  508. if (!prtd) {
  509. pr_err("%s: prtd is NULL\n", __func__);
  510. return;
  511. }
  512. cstream = prtd->cstream;
  513. if (!cstream) {
  514. pr_err("%s: cstream is NULL\n", __func__);
  515. return;
  516. }
  517. ac = prtd->audio_client;
  518. /*
  519. * Token for rest of the compressed commands use to set
  520. * session id, stream id, dir etc.
  521. */
  522. stream_id = q6asm_get_stream_id_from_token(token);
  523. pr_debug("%s opcode =%08x\n", __func__, opcode);
  524. switch (opcode) {
  525. case ASM_DATA_EVENT_WRITE_DONE_V2:
  526. spin_lock_irqsave(&prtd->lock, flags);
  527. if (payload[3]) {
  528. pr_err("%s: WRITE FAILED w/ err 0x%x !, paddr 0x%x, byte_offset=%d,copied_total=%llu,token=%d\n",
  529. __func__,
  530. payload[3],
  531. payload[0],
  532. prtd->byte_offset,
  533. prtd->copied_total, token);
  534. if (atomic_cmpxchg(&prtd->drain, 1, 0) &&
  535. prtd->last_buffer) {
  536. pr_debug("%s: wake up on drain\n", __func__);
  537. #if IS_ENABLED(CONFIG_AUDIO_QGKI)
  538. prtd->drain_ready = 1;
  539. wake_up(&prtd->drain_wait);
  540. #endif
  541. prtd->last_buffer = 0;
  542. } else {
  543. atomic_set(&prtd->start, 0);
  544. }
  545. } else {
  546. pr_debug("ASM_DATA_EVENT_WRITE_DONE_V2 offset %d, length %d\n",
  547. prtd->byte_offset, token);
  548. }
  549. /*
  550. * Token for WRITE command represents the amount of data
  551. * written to ADSP in the last write, update offset and
  552. * total copied data accordingly.
  553. */
  554. if (prtd->ts_header_offset) {
  555. /* Always assume that the data will be sent to DSP on
  556. * frame boundary.
  557. * i.e, one frame of userspace write will result in
  558. * one kernel write to DSP. This is needed as
  559. * timestamp will be sent per frame.
  560. */
  561. prtd->byte_offset +=
  562. prtd->codec_param.buffer.fragment_size;
  563. prtd->copied_total +=
  564. prtd->codec_param.buffer.fragment_size;
  565. } else {
  566. prtd->byte_offset += token;
  567. prtd->copied_total += token;
  568. }
  569. if (prtd->byte_offset >= prtd->buffer_size)
  570. prtd->byte_offset -= prtd->buffer_size;
  571. snd_compr_fragment_elapsed(cstream);
  572. if (!atomic_read(&prtd->start)) {
  573. /* Writes must be restarted from _copy() */
  574. pr_debug("write_done received while not started, treat as xrun");
  575. atomic_set(&prtd->xrun, 1);
  576. spin_unlock_irqrestore(&prtd->lock, flags);
  577. break;
  578. }
  579. #if !IS_ENABLED(CONFIG_AUDIO_QGKI)
  580. if (prtd->zero_buffer) {
  581. pr_debug("write_done for zero buffer\n");
  582. prtd->zero_buffer = 0;
  583. /* move to next stream and reset vars */
  584. pr_debug("%s: Moving to next stream in gapless\n",
  585. __func__);
  586. ac->stream_id = NEXT_STREAM_ID(ac->stream_id);
  587. prtd->byte_offset = 0;
  588. prtd->app_pointer = 0;
  589. prtd->first_buffer = 1;
  590. prtd->last_buffer = 0;
  591. /*
  592. * Set gapless transition flag only if EOS hasn't been
  593. * acknowledged already.
  594. */
  595. if (atomic_read(&prtd->eos))
  596. prtd->gapless_state.gapless_transition = 1;
  597. prtd->marker_timestamp = 0;
  598. /*
  599. * Don't reset these as these vars map to
  600. * total_bytes_transferred and total_bytes_available
  601. * directly, only total_bytes_transferred will be
  602. * updated in the next avail() ioctl
  603. * prtd->copied_total = 0;
  604. * prtd->bytes_received = 0;
  605. */
  606. atomic_set(&prtd->drain, 0);
  607. atomic_set(&prtd->xrun, 1);
  608. pr_debug("%s: issue CMD_RUN", __func__);
  609. q6asm_run_nowait(prtd->audio_client, 0, 0, 0);
  610. snd_compr_drain_notify(cstream);
  611. spin_unlock_irqrestore(&prtd->lock, flags);
  612. break;
  613. }
  614. bytes_available = prtd->bytes_received - prtd->copied_total;
  615. if (bytes_available == 0) {
  616. pr_debug("%s:bytes_available is 0\n", __func__);
  617. if (prtd->last_buffer)
  618. prtd->last_buffer = 0;
  619. if (atomic_read(&prtd->partial_drain) &&
  620. prtd->gapless_state.set_next_stream_id &&
  621. !prtd->zero_buffer) {
  622. pr_debug("%s:Partial Drain Case\n", __func__);
  623. pr_debug("%s:Send EOS command\n", __func__);
  624. /* send EOS */
  625. prtd->eos_ack = 0;
  626. atomic_set(&prtd->eos, 1);
  627. atomic_set(&prtd->drain, 0);
  628. q6asm_stream_cmd_nowait(ac, CMD_EOS, ac->stream_id);
  629. /* send a zero length buffer in case of partial drain*/
  630. atomic_set(&prtd->xrun, 0);
  631. pr_debug("%s:Send zero size buffer\n", __func__);
  632. msm_compr_send_buffer(prtd);
  633. prtd->zero_buffer = 1;
  634. } else {
  635. /*
  636. * moving to next stream failed, so reset the gapless state
  637. * set next stream id for the same session so that the same
  638. * stream can be used for gapless playback
  639. */
  640. pr_debug("%s:Drain Case\n", __func__);
  641. pr_debug("%s:Reset Gapless params \n", __func__);
  642. prtd->gapless_state.set_next_stream_id = false;
  643. prtd->gapless_state.gapless_transition = 0;
  644. pr_debug("%s:Send EOS command\n", __func__);
  645. prtd->eos_ack = 0;
  646. atomic_set(&prtd->eos, 1);
  647. atomic_set(&prtd->drain, 0);
  648. q6asm_stream_cmd_nowait(ac, CMD_EOS, ac->stream_id);
  649. prtd->cmd_interrupt = 0;
  650. }
  651. } else if (bytes_available < cstream->runtime->fragment_size) {
  652. pr_debug("%s:Partial Buffer Case \n", __func__);
  653. atomic_set(&prtd->xrun, 1);
  654. if (prtd->last_buffer)
  655. prtd->last_buffer = 0;
  656. if (atomic_read(&prtd->drain)) {
  657. if (bytes_available > 0) {
  658. pr_debug("%s: send %d partial bytes at the end",
  659. __func__, bytes_available);
  660. atomic_set(&prtd->xrun, 0);
  661. prtd->last_buffer = 1;
  662. msm_compr_send_buffer(prtd);
  663. }
  664. }
  665. #else
  666. bytes_available = prtd->bytes_received - prtd->copied_total;
  667. if (bytes_available < cstream->runtime->fragment_size) {
  668. pr_debug("WRITE_DONE Insufficient data to send. break out\n");
  669. atomic_set(&prtd->xrun, 1);
  670. if (prtd->last_buffer)
  671. prtd->last_buffer = 0;
  672. if (atomic_read(&prtd->drain)) {
  673. pr_debug("wake up on drain\n");
  674. prtd->drain_ready = 1;
  675. wake_up(&prtd->drain_wait);
  676. atomic_set(&prtd->drain, 0);
  677. }
  678. #endif
  679. } else if ((bytes_available == cstream->runtime->fragment_size)
  680. && atomic_read(&prtd->drain)) {
  681. prtd->last_buffer = 1;
  682. msm_compr_send_buffer(prtd);
  683. prtd->last_buffer = 0;
  684. } else
  685. msm_compr_send_buffer(prtd);
  686. spin_unlock_irqrestore(&prtd->lock, flags);
  687. break;
  688. case ASM_DATA_EVENT_READ_DONE_V2:
  689. spin_lock_irqsave(&prtd->lock, flags);
  690. pr_debug("ASM_DATA_EVENT_READ_DONE_V2 offset %d, length %d\n",
  691. prtd->byte_offset, payload[4]);
  692. if (prtd->ts_header_offset) {
  693. /* Update the header for received buffer */
  694. buff_addr = prtd->buffer + prtd->byte_offset;
  695. /* Write the actual length of the received buffer */
  696. *buff_addr = payload[4];
  697. buff_addr++;
  698. /* Write the offset */
  699. *buff_addr = prtd->ts_header_offset;
  700. buff_addr++;
  701. /* Write the TS LSW */
  702. *buff_addr = payload[CAPTURE_META_DATA_TS_OFFSET_LSW];
  703. buff_addr++;
  704. /* Write the TS MSW */
  705. *buff_addr = payload[CAPTURE_META_DATA_TS_OFFSET_MSW];
  706. }
  707. /* Always assume read_size is same as fragment_size */
  708. read_size = prtd->codec_param.buffer.fragment_size;
  709. prtd->byte_offset += read_size;
  710. prtd->received_total += read_size;
  711. if (prtd->byte_offset >= prtd->buffer_size)
  712. prtd->byte_offset -= prtd->buffer_size;
  713. snd_compr_fragment_elapsed(cstream);
  714. if (!atomic_read(&prtd->start)) {
  715. pr_debug("read_done received while not started, treat as xrun");
  716. atomic_set(&prtd->xrun, 1);
  717. spin_unlock_irqrestore(&prtd->lock, flags);
  718. break;
  719. }
  720. msm_compr_read_buffer(prtd);
  721. spin_unlock_irqrestore(&prtd->lock, flags);
  722. break;
  723. case ASM_DATA_EVENT_RENDERED_EOS:
  724. case ASM_DATA_EVENT_RENDERED_EOS_V2:
  725. spin_lock_irqsave(&prtd->lock, flags);
  726. pr_debug("%s: ASM_DATA_CMDRSP_EOS token 0x%x,stream id %d\n",
  727. __func__, token, stream_id);
  728. if (atomic_read(&prtd->eos) &&
  729. !prtd->gapless_state.set_next_stream_id) {
  730. pr_debug("ASM_DATA_CMDRSP_EOS wake up\n");
  731. prtd->eos_ack = 1;
  732. #if IS_ENABLED(CONFIG_AUDIO_QGKI)
  733. wake_up(&prtd->eos_wait);
  734. #else
  735. pr_debug("%s:issue CMD_PAUSE stream_id %d",
  736. __func__, ac->stream_id);
  737. q6asm_stream_cmd_nowait(ac, CMD_PAUSE, ac->stream_id);
  738. prtd->cmd_ack = 0;
  739. pr_debug("%s:DRAIN,don't wait for EOS ack\n", __func__);
  740. /*
  741. * Don't reset these as these vars map to
  742. * total_bytes_transferred and total_bytes_available.
  743. * Just total_bytes_transferred will be updated
  744. * in the next avail() ioctl.
  745. * prtd->copied_total = 0;
  746. * prtd->bytes_received = 0;
  747. * do not reset prtd->bytes_sent as well as the same
  748. * session is used for gapless playback
  749. */
  750. prtd->byte_offset = 0;
  751. prtd->app_pointer = 0;
  752. prtd->first_buffer = 1;
  753. prtd->last_buffer = 0;
  754. atomic_set(&prtd->drain, 0);
  755. atomic_set(&prtd->xrun, 1);
  756. pr_debug("%s:issue CMD_FLUSH ac->stream_id %d",
  757. __func__, ac->stream_id);
  758. q6asm_run_nowait(prtd->audio_client, 0, 0, 0);
  759. snd_compr_drain_notify(cstream);
  760. #endif
  761. }
  762. atomic_set(&prtd->eos, 0);
  763. stream_index = STREAM_ARRAY_INDEX(stream_id);
  764. if (stream_index >= MAX_NUMBER_OF_STREAMS ||
  765. stream_index < 0) {
  766. pr_err("%s: Invalid stream index %d", __func__,
  767. stream_index);
  768. spin_unlock_irqrestore(&prtd->lock, flags);
  769. break;
  770. }
  771. if (prtd->gapless_state.set_next_stream_id &&
  772. prtd->gapless_state.stream_opened[stream_index]) {
  773. pr_debug("%s: CMD_CLOSE stream_id %d\n",
  774. __func__, stream_id);
  775. q6asm_stream_cmd_nowait(ac, CMD_CLOSE, stream_id);
  776. atomic_set(&prtd->close, 1);
  777. prtd->gapless_state.stream_opened[stream_index] = 0;
  778. prtd->gapless_state.set_next_stream_id = false;
  779. }
  780. if (prtd->gapless_state.gapless_transition)
  781. prtd->gapless_state.gapless_transition = 0;
  782. spin_unlock_irqrestore(&prtd->lock, flags);
  783. break;
  784. case ASM_STREAM_PP_EVENT:
  785. case ASM_STREAM_CMD_ENCDEC_EVENTS:
  786. pr_debug("%s: ASM_STREAM_EVENT(0x%x)\n", __func__, opcode);
  787. rtd = cstream->private_data;
  788. if (!rtd) {
  789. pr_err("%s: rtd is NULL\n", __func__);
  790. return;
  791. }
  792. ret = msm_adsp_inform_mixer_ctl(rtd, payload);
  793. if (ret) {
  794. pr_err("%s: failed to inform mixer ctrl. err = %d\n",
  795. __func__, ret);
  796. return;
  797. }
  798. break;
  799. case ASM_DATA_EVENT_SR_CM_CHANGE_NOTIFY:
  800. case ASM_DATA_EVENT_ENC_SR_CM_CHANGE_NOTIFY: {
  801. pr_debug("ASM_DATA_EVENT_SR_CM_CHANGE_NOTIFY\n");
  802. chan_mode = payload[1] >> 16;
  803. sample_rate = payload[2] >> 16;
  804. if (prtd && (chan_mode != prtd->num_channels ||
  805. sample_rate != prtd->sample_rate)) {
  806. prtd->num_channels = chan_mode;
  807. prtd->sample_rate = sample_rate;
  808. }
  809. }
  810. /* Fallthrough here */
  811. case APR_BASIC_RSP_RESULT: {
  812. switch (payload[0]) {
  813. case ASM_SESSION_CMD_RUN_V2:
  814. /* check if the first buffer need to be sent to DSP */
  815. pr_debug("ASM_SESSION_CMD_RUN_V2\n");
  816. /* FIXME: A state is a better way, dealing with this */
  817. spin_lock_irqsave(&prtd->lock, flags);
  818. if (cstream->direction == SND_COMPRESS_CAPTURE) {
  819. atomic_set(&prtd->start, 1);
  820. msm_compr_read_buffer(prtd);
  821. spin_unlock_irqrestore(&prtd->lock, flags);
  822. break;
  823. }
  824. if (!prtd->bytes_sent) {
  825. bytes_available = prtd->bytes_received -
  826. prtd->copied_total;
  827. if (bytes_available <
  828. cstream->runtime->fragment_size) {
  829. pr_debug("CMD_RUN_V2 Insufficient data to send. break out\n");
  830. atomic_set(&prtd->xrun, 1);
  831. } else {
  832. msm_compr_send_buffer(prtd);
  833. }
  834. }
  835. /*
  836. * The condition below ensures playback finishes in the
  837. * follow cornercase
  838. * WRITE(last buffer)
  839. * WAIT_FOR_DRAIN
  840. * PAUSE
  841. * WRITE_DONE(X)
  842. * RESUME
  843. */
  844. if ((prtd->copied_total == prtd->bytes_sent) &&
  845. atomic_read(&prtd->drain)) {
  846. bytes_available = prtd->bytes_received - prtd->copied_total;
  847. if (bytes_available < cstream->runtime->fragment_size) {
  848. pr_debug("%s: RUN ack, wake up & continue pending drain\n",
  849. __func__);
  850. if (prtd->last_buffer)
  851. prtd->last_buffer = 0;
  852. prtd->drain_ready = 1;
  853. #if IS_ENABLED(CONFIG_AUDIO_QGKI)
  854. wake_up(&prtd->drain_wait);
  855. #endif
  856. atomic_set(&prtd->drain, 0);
  857. } else if (atomic_read(&prtd->xrun)) {
  858. pr_debug("%s: RUN ack, continue write cycle\n", __func__);
  859. atomic_set(&prtd->xrun, 0);
  860. msm_compr_send_buffer(prtd);
  861. }
  862. }
  863. spin_unlock_irqrestore(&prtd->lock, flags);
  864. break;
  865. case ASM_STREAM_CMD_FLUSH:
  866. pr_debug("%s: ASM_STREAM_CMD_FLUSH:", __func__);
  867. pr_debug("token 0x%x, stream id %d\n", token,
  868. stream_id);
  869. prtd->cmd_ack = 1;
  870. break;
  871. case ASM_DATA_CMD_REMOVE_INITIAL_SILENCE:
  872. pr_debug("%s: ASM_DATA_CMD_REMOVE_INITIAL_SILENCE:",
  873. __func__);
  874. pr_debug("token 0x%x, stream id = %d\n", token,
  875. stream_id);
  876. break;
  877. case ASM_DATA_CMD_REMOVE_TRAILING_SILENCE:
  878. pr_debug("%s: ASM_DATA_CMD_REMOVE_TRAILING_SILENCE:",
  879. __func__);
  880. pr_debug("token = 0x%x, stream id = %d\n", token,
  881. stream_id);
  882. break;
  883. case ASM_STREAM_CMD_CLOSE:
  884. pr_debug("%s: ASM_DATA_CMD_CLOSE:", __func__);
  885. pr_debug("token 0x%x, stream id %d\n", token,
  886. stream_id);
  887. /*
  888. * wakeup wait for stream avail on stream 3
  889. * after stream 1 ends.
  890. */
  891. if (prtd->next_stream) {
  892. pr_debug("%s:CLOSE:wakeup wait for stream\n",
  893. __func__);
  894. prtd->stream_available = 1;
  895. wake_up(&prtd->wait_for_stream_avail);
  896. prtd->next_stream = 0;
  897. }
  898. if (atomic_read(&prtd->close) &&
  899. atomic_read(&prtd->wait_on_close)) {
  900. prtd->cmd_ack = 1;
  901. wake_up(&prtd->close_wait);
  902. }
  903. atomic_set(&prtd->close, 0);
  904. break;
  905. case ASM_STREAM_CMD_REGISTER_PP_EVENTS:
  906. pr_debug("%s: ASM_STREAM_CMD_REGISTER_PP_EVENTS:",
  907. __func__);
  908. break;
  909. default:
  910. break;
  911. }
  912. break;
  913. }
  914. case ASM_SESSION_CMDRSP_GET_SESSIONTIME_V3:
  915. pr_debug("%s: ASM_SESSION_CMDRSP_GET_SESSIONTIME_V3\n",
  916. __func__);
  917. break;
  918. case RESET_EVENTS:
  919. pr_err("%s: Received reset events CB, move to error state",
  920. __func__);
  921. spin_lock_irqsave(&prtd->lock, flags);
  922. /*
  923. * Since ADSP is down, let this driver pretend that it copied
  924. * all the bytes received, so that next write will be triggered
  925. */
  926. prtd->copied_total = prtd->bytes_received;
  927. snd_compr_fragment_elapsed(cstream);
  928. atomic_set(&prtd->error, 1);
  929. #if IS_ENABLED(CONFIG_AUDIO_QGKI)
  930. wake_up(&prtd->drain_wait);
  931. if (atomic_cmpxchg(&prtd->eos, 1, 0)) {
  932. pr_debug("%s:unblock eos wait queues", __func__);
  933. wake_up(&prtd->eos_wait);
  934. }
  935. #endif
  936. spin_unlock_irqrestore(&prtd->lock, flags);
  937. break;
  938. default:
  939. pr_debug("%s: Not Supported Event opcode[0x%x]\n",
  940. __func__, opcode);
  941. break;
  942. }
  943. }
  944. static int msm_compr_get_partial_drain_delay(int frame_sz, int sample_rate)
  945. {
  946. int delay_time_ms = 0;
  947. delay_time_ms = ((DSP_NUM_OUTPUT_FRAME_BUFFERED * frame_sz * 1000) /
  948. sample_rate) + DSP_PP_BUFFERING_IN_MSEC;
  949. delay_time_ms = delay_time_ms > PARTIAL_DRAIN_ACK_EARLY_BY_MSEC ?
  950. delay_time_ms - PARTIAL_DRAIN_ACK_EARLY_BY_MSEC : 0;
  951. pr_debug("%s: frame_sz %d, sample_rate %d, partial drain delay %d\n",
  952. __func__, frame_sz, sample_rate, delay_time_ms);
  953. return delay_time_ms;
  954. }
  955. static void populate_codec_list(struct msm_compr_audio *prtd)
  956. {
  957. pr_debug("%s\n", __func__);
  958. prtd->compr_cap.direction = SND_COMPRESS_PLAYBACK;
  959. prtd->compr_cap.min_fragment_size =
  960. COMPR_PLAYBACK_MIN_FRAGMENT_SIZE;
  961. prtd->compr_cap.max_fragment_size =
  962. COMPR_PLAYBACK_MAX_FRAGMENT_SIZE;
  963. prtd->compr_cap.min_fragments =
  964. COMPR_PLAYBACK_MIN_NUM_FRAGMENTS;
  965. prtd->compr_cap.max_fragments =
  966. COMPR_PLAYBACK_MAX_NUM_FRAGMENTS;
  967. prtd->compr_cap.num_codecs = 10;
  968. prtd->compr_cap.codecs[0] = SND_AUDIOCODEC_MP3;
  969. prtd->compr_cap.codecs[1] = SND_AUDIOCODEC_AAC;
  970. prtd->compr_cap.codecs[2] = SND_AUDIOCODEC_AC3;
  971. prtd->compr_cap.codecs[3] = SND_AUDIOCODEC_EAC3;
  972. prtd->compr_cap.codecs[4] = SND_AUDIOCODEC_MP2;
  973. prtd->compr_cap.codecs[5] = SND_AUDIOCODEC_PCM;
  974. prtd->compr_cap.codecs[6] = SND_AUDIOCODEC_DTS;
  975. prtd->compr_cap.codecs[7] = SND_AUDIOCODEC_TRUEHD;
  976. prtd->compr_cap.codecs[8] = SND_AUDIOCODEC_IEC61937;
  977. prtd->compr_cap.codecs[9] = SND_AUDIOCODEC_BESPOKE;
  978. }
  979. static int msm_compr_get_decoder_format(struct msm_compr_audio *prtd,
  980. int *frame_sz, bool *is_format_gapless)
  981. {
  982. int ret = 0;
  983. __s32 *gdec;
  984. pr_debug("%s: generic format = %x", __func__,
  985. prtd->codec_param.codec.options.generic.reserved[0]);
  986. switch(prtd->codec_param.codec.options.generic.reserved[0]) {
  987. case AUDIO_COMP_FORMAT_ALAC:
  988. ret = FORMAT_ALAC;
  989. break;
  990. case AUDIO_COMP_FORMAT_APE:
  991. ret = FORMAT_APE;
  992. break;
  993. case AUDIO_COMP_FORMAT_APTX:
  994. ret = FORMAT_APTX;
  995. break;
  996. case AUDIO_COMP_FORMAT_DSD:
  997. ret = FORMAT_DSD;
  998. break;
  999. case AUDIO_COMP_FORMAT_FLAC:
  1000. ret = FORMAT_FLAC;
  1001. *is_format_gapless = true;
  1002. gdec = &(prtd->codec_param.codec.options.generic.reserved[1]);
  1003. *frame_sz = ((struct snd_generic_dec_flac *)gdec)->min_blk_size;
  1004. break;
  1005. case AUDIO_COMP_FORMAT_VORBIS:
  1006. ret = FORMAT_VORBIS;
  1007. break;
  1008. case AUDIO_COMP_FORMAT_WMA:
  1009. ret = FORMAT_WMA_V9;
  1010. break;
  1011. case AUDIO_COMP_FORMAT_WMA_PRO:
  1012. ret = FORMAT_WMA_V10PRO;
  1013. break;
  1014. default:
  1015. ret = -EINVAL;
  1016. break;
  1017. }
  1018. return ret;
  1019. }
  1020. static int msm_compr_send_media_format_block(struct snd_compr_stream *cstream,
  1021. int stream_id,
  1022. bool use_gapless_codec_options)
  1023. {
  1024. struct snd_compr_runtime *runtime = cstream->runtime;
  1025. struct msm_compr_audio *prtd = runtime->private_data;
  1026. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  1027. struct snd_soc_component *component =NULL;
  1028. struct msm_compr_pdata *pdata = NULL;
  1029. struct asm_aac_cfg aac_cfg;
  1030. struct asm_wma_cfg wma_cfg;
  1031. struct asm_wmapro_cfg wma_pro_cfg;
  1032. struct asm_flac_cfg flac_cfg;
  1033. struct asm_vorbis_cfg vorbis_cfg;
  1034. struct asm_alac_cfg alac_cfg;
  1035. struct asm_ape_cfg ape_cfg;
  1036. struct asm_dsd_cfg dsd_cfg;
  1037. struct aptx_dec_bt_addr_cfg aptx_cfg;
  1038. struct asm_amrwbplus_cfg amrwbplus_cfg;
  1039. union snd_codec_options *codec_options;
  1040. int ret = 0;
  1041. uint16_t bit_width;
  1042. bool use_default_chmap = true;
  1043. char *chmap = NULL;
  1044. uint16_t sample_word_size;
  1045. __s32 *gdec;
  1046. pr_debug("%s: use_gapless_codec_options %d\n",
  1047. __func__, use_gapless_codec_options);
  1048. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  1049. if (!component) {
  1050. pr_err("%s: component is NULL\n", __func__);
  1051. return -EINVAL;
  1052. }
  1053. pdata = snd_soc_component_get_drvdata(component);
  1054. if (use_gapless_codec_options)
  1055. codec_options = &(prtd->gapless_state.codec_options);
  1056. else
  1057. codec_options = &(prtd->codec_param.codec.options);
  1058. if (!codec_options) {
  1059. pr_err("%s: codec_options is NULL\n", __func__);
  1060. return -EINVAL;
  1061. }
  1062. switch (prtd->codec) {
  1063. case FORMAT_LINEAR_PCM:
  1064. pr_debug("SND_AUDIOCODEC_PCM\n");
  1065. if (pdata->ch_map[rtd->dai_link->id]) {
  1066. use_default_chmap =
  1067. !(pdata->ch_map[rtd->dai_link->id]->set_ch_map);
  1068. chmap =
  1069. pdata->ch_map[rtd->dai_link->id]->channel_map;
  1070. }
  1071. switch (prtd->codec_param.codec.format) {
  1072. case SNDRV_PCM_FORMAT_S32_LE:
  1073. bit_width = 32;
  1074. sample_word_size = 32;
  1075. break;
  1076. case SNDRV_PCM_FORMAT_S24_LE:
  1077. bit_width = 24;
  1078. sample_word_size = 32;
  1079. break;
  1080. case SNDRV_PCM_FORMAT_S24_3LE:
  1081. bit_width = 24;
  1082. sample_word_size = 24;
  1083. break;
  1084. case SNDRV_PCM_FORMAT_S16_LE:
  1085. default:
  1086. bit_width = 16;
  1087. sample_word_size = 16;
  1088. break;
  1089. }
  1090. if (q6core_get_avcs_api_version_per_service(
  1091. APRV2_IDS_SERVICE_ID_ADSP_ASM_V) >=
  1092. ADSP_ASM_API_VERSION_V2) {
  1093. ret = q6asm_media_format_block_pcm_format_support_v5(
  1094. prtd->audio_client,
  1095. prtd->sample_rate,
  1096. prtd->num_channels,
  1097. bit_width, stream_id,
  1098. use_default_chmap,
  1099. chmap,
  1100. sample_word_size,
  1101. ASM_LITTLE_ENDIAN,
  1102. DEFAULT_QF);
  1103. } else {
  1104. ret = q6asm_media_format_block_pcm_format_support_v4(
  1105. prtd->audio_client,
  1106. prtd->sample_rate,
  1107. prtd->num_channels,
  1108. bit_width, stream_id,
  1109. use_default_chmap,
  1110. chmap,
  1111. sample_word_size,
  1112. ASM_LITTLE_ENDIAN,
  1113. DEFAULT_QF);
  1114. }
  1115. if (ret < 0)
  1116. pr_err("%s: CMD Format block failed\n", __func__);
  1117. break;
  1118. case FORMAT_MP3:
  1119. pr_debug("SND_AUDIOCODEC_MP3\n");
  1120. /* no media format block needed */
  1121. break;
  1122. case FORMAT_MPEG4_AAC:
  1123. pr_debug("SND_AUDIOCODEC_AAC\n");
  1124. memset(&aac_cfg, 0x0, sizeof(struct asm_aac_cfg));
  1125. aac_cfg.aot = AAC_ENC_MODE_EAAC_P;
  1126. if (prtd->codec_param.codec.format ==
  1127. SND_AUDIOSTREAMFORMAT_MP4ADTS)
  1128. aac_cfg.format = 0x0;
  1129. else if (prtd->codec_param.codec.format ==
  1130. SND_AUDIOSTREAMFORMAT_MP4LATM)
  1131. aac_cfg.format = 0x04;
  1132. else
  1133. aac_cfg.format = 0x03;
  1134. aac_cfg.ch_cfg = prtd->num_channels;
  1135. aac_cfg.sample_rate = prtd->sample_rate;
  1136. ret = q6asm_stream_media_format_block_aac(prtd->audio_client,
  1137. &aac_cfg, stream_id);
  1138. if (ret < 0)
  1139. pr_err("%s: CMD Format block failed\n", __func__);
  1140. break;
  1141. case FORMAT_AC3:
  1142. pr_debug("SND_AUDIOCODEC_AC3\n");
  1143. break;
  1144. case FORMAT_EAC3:
  1145. pr_debug("SND_AUDIOCODEC_EAC3\n");
  1146. break;
  1147. case FORMAT_WMA_V9:
  1148. pr_debug("SND_AUDIOCODEC_WMA\n");
  1149. memset(&wma_cfg, 0x0, sizeof(struct asm_wma_cfg));
  1150. wma_cfg.format_tag = prtd->codec_param.codec.format;
  1151. wma_cfg.ch_cfg = prtd->codec_param.codec.ch_in;
  1152. wma_cfg.sample_rate = prtd->sample_rate;
  1153. gdec = &(codec_options->generic.reserved[1]);
  1154. wma_cfg.avg_bytes_per_sec =
  1155. ((struct snd_generic_dec_wma *)gdec)->avg_bit_rate/8;
  1156. wma_cfg.block_align =
  1157. ((struct snd_generic_dec_wma *)gdec)->super_block_align;
  1158. wma_cfg.valid_bits_per_sample =
  1159. ((struct snd_generic_dec_wma *)gdec)->bits_per_sample;
  1160. wma_cfg.ch_mask =
  1161. ((struct snd_generic_dec_wma *)gdec)->channelmask;
  1162. wma_cfg.encode_opt =
  1163. ((struct snd_generic_dec_wma *)gdec)->encodeopt;
  1164. ret = q6asm_media_format_block_wma(prtd->audio_client,
  1165. &wma_cfg, stream_id);
  1166. if (ret < 0)
  1167. pr_err("%s: CMD Format block failed\n", __func__);
  1168. break;
  1169. case FORMAT_WMA_V10PRO:
  1170. pr_debug("SND_AUDIOCODEC_WMA_PRO\n");
  1171. memset(&wma_pro_cfg, 0x0, sizeof(struct asm_wmapro_cfg));
  1172. wma_pro_cfg.format_tag = prtd->codec_param.codec.format;
  1173. wma_pro_cfg.ch_cfg = prtd->codec_param.codec.ch_in;
  1174. wma_pro_cfg.sample_rate = prtd->sample_rate;
  1175. gdec = &(codec_options->generic.reserved[1]);
  1176. wma_cfg.avg_bytes_per_sec =
  1177. ((struct snd_generic_dec_wma *)gdec)->avg_bit_rate/8;
  1178. wma_pro_cfg.block_align =
  1179. ((struct snd_generic_dec_wma *)gdec)->super_block_align;
  1180. wma_pro_cfg.valid_bits_per_sample =
  1181. ((struct snd_generic_dec_wma *)gdec)->bits_per_sample;
  1182. wma_pro_cfg.ch_mask =
  1183. ((struct snd_generic_dec_wma *)gdec)->channelmask;
  1184. wma_pro_cfg.encode_opt =
  1185. ((struct snd_generic_dec_wma *)gdec)->encodeopt;
  1186. wma_pro_cfg.adv_encode_opt =
  1187. ((struct snd_generic_dec_wma *)gdec)->encodeopt1;
  1188. wma_pro_cfg.adv_encode_opt2 =
  1189. ((struct snd_generic_dec_wma *)gdec)->encodeopt2;
  1190. ret = q6asm_media_format_block_wmapro(prtd->audio_client,
  1191. &wma_pro_cfg, stream_id);
  1192. if (ret < 0)
  1193. pr_err("%s: CMD Format block failed\n", __func__);
  1194. break;
  1195. case FORMAT_MP2:
  1196. pr_debug("%s: SND_AUDIOCODEC_MP2\n", __func__);
  1197. break;
  1198. case FORMAT_FLAC:
  1199. pr_debug("%s: SND_AUDIOCODEC_FLAC\n", __func__);
  1200. memset(&flac_cfg, 0x0, sizeof(struct asm_flac_cfg));
  1201. flac_cfg.ch_cfg = prtd->num_channels;
  1202. flac_cfg.sample_rate = prtd->sample_rate;
  1203. flac_cfg.stream_info_present = 1;
  1204. gdec = &(codec_options->generic.reserved[1]);
  1205. flac_cfg.sample_size =
  1206. ((struct snd_generic_dec_flac *)gdec)->sample_size;
  1207. flac_cfg.min_blk_size =
  1208. ((struct snd_generic_dec_flac *)gdec)->min_blk_size;
  1209. flac_cfg.max_blk_size =
  1210. ((struct snd_generic_dec_flac *)gdec)->max_blk_size;
  1211. flac_cfg.max_frame_size =
  1212. ((struct snd_generic_dec_flac *)gdec)->max_frame_size;
  1213. flac_cfg.min_frame_size =
  1214. ((struct snd_generic_dec_flac *)gdec)->min_frame_size;
  1215. ret = q6asm_stream_media_format_block_flac(prtd->audio_client,
  1216. &flac_cfg, stream_id);
  1217. if (ret < 0)
  1218. pr_err("%s: CMD Format block failed ret %d\n",
  1219. __func__, ret);
  1220. break;
  1221. case FORMAT_VORBIS:
  1222. pr_debug("%s: SND_AUDIOCODEC_VORBIS\n", __func__);
  1223. memset(&vorbis_cfg, 0x0, sizeof(struct asm_vorbis_cfg));
  1224. gdec = &(codec_options->generic.reserved[1]);
  1225. vorbis_cfg.bit_stream_fmt =
  1226. ((struct snd_generic_dec_vorbis *)gdec)->bit_stream_fmt;
  1227. ret = q6asm_stream_media_format_block_vorbis(
  1228. prtd->audio_client, &vorbis_cfg,
  1229. stream_id);
  1230. if (ret < 0)
  1231. pr_err("%s: CMD Format block failed ret %d\n",
  1232. __func__, ret);
  1233. break;
  1234. case FORMAT_ALAC:
  1235. pr_debug("%s: SND_AUDIOCODEC_ALAC\n", __func__);
  1236. memset(&alac_cfg, 0x0, sizeof(struct asm_alac_cfg));
  1237. alac_cfg.num_channels = prtd->num_channels;
  1238. alac_cfg.sample_rate = prtd->sample_rate;
  1239. gdec = &(codec_options->generic.reserved[1]);
  1240. alac_cfg.frame_length =
  1241. ((struct snd_generic_dec_alac *)gdec)->frame_length;
  1242. alac_cfg.compatible_version =
  1243. ((struct snd_generic_dec_alac *)gdec)->compatible_version;
  1244. alac_cfg.bit_depth =
  1245. ((struct snd_generic_dec_alac *)gdec)->bit_depth;
  1246. alac_cfg.pb = ((struct snd_generic_dec_alac *)gdec)->pb;
  1247. alac_cfg.mb = ((struct snd_generic_dec_alac *)gdec)->mb;
  1248. alac_cfg.kb = ((struct snd_generic_dec_alac *)gdec)->kb;
  1249. alac_cfg.max_run =
  1250. ((struct snd_generic_dec_alac *)gdec)->max_run;
  1251. alac_cfg.max_frame_bytes =
  1252. ((struct snd_generic_dec_alac *)gdec)->max_frame_bytes;
  1253. alac_cfg.avg_bit_rate =
  1254. ((struct snd_generic_dec_alac *)gdec)->avg_bit_rate;
  1255. alac_cfg.channel_layout_tag =
  1256. ((struct snd_generic_dec_alac *)gdec)->channel_layout_tag;
  1257. ret = q6asm_media_format_block_alac(prtd->audio_client,
  1258. &alac_cfg, stream_id);
  1259. if (ret < 0)
  1260. pr_err("%s: CMD Format block failed ret %d\n",
  1261. __func__, ret);
  1262. break;
  1263. case FORMAT_APE:
  1264. pr_debug("%s: SND_AUDIOCODEC_APE\n", __func__);
  1265. memset(&ape_cfg, 0x0, sizeof(struct asm_ape_cfg));
  1266. ape_cfg.num_channels = prtd->num_channels;
  1267. ape_cfg.sample_rate = prtd->sample_rate;
  1268. gdec = &(codec_options->generic.reserved[1]);
  1269. ape_cfg.compatible_version =
  1270. ((struct snd_generic_dec_ape *)gdec)->compatible_version;
  1271. ape_cfg.compression_level =
  1272. ((struct snd_generic_dec_ape *)gdec)->compression_level;
  1273. ape_cfg.format_flags =
  1274. ((struct snd_generic_dec_ape *)gdec)->format_flags;
  1275. ape_cfg.blocks_per_frame =
  1276. ((struct snd_generic_dec_ape *)gdec)->blocks_per_frame;
  1277. ape_cfg.final_frame_blocks =
  1278. ((struct snd_generic_dec_ape *)gdec)->final_frame_blocks;
  1279. ape_cfg.total_frames =
  1280. ((struct snd_generic_dec_ape *)gdec)->total_frames;
  1281. ape_cfg.bits_per_sample =
  1282. ((struct snd_generic_dec_ape *)gdec)->bits_per_sample;
  1283. ape_cfg.seek_table_present =
  1284. ((struct snd_generic_dec_ape *)gdec)->seek_table_present;
  1285. ret = q6asm_media_format_block_ape(prtd->audio_client,
  1286. &ape_cfg, stream_id);
  1287. if (ret < 0)
  1288. pr_err("%s: CMD Format block failed ret %d\n",
  1289. __func__, ret);
  1290. break;
  1291. case FORMAT_DTS:
  1292. pr_debug("SND_AUDIOCODEC_DTS\n");
  1293. /* no media format block needed */
  1294. break;
  1295. case FORMAT_DSD:
  1296. pr_debug("%s: SND_AUDIOCODEC_DSD\n", __func__);
  1297. memset(&dsd_cfg, 0x0, sizeof(struct asm_dsd_cfg));
  1298. dsd_cfg.num_channels = prtd->num_channels;
  1299. dsd_cfg.dsd_data_rate = prtd->sample_rate;
  1300. dsd_cfg.num_version = 0;
  1301. dsd_cfg.is_bitwise_big_endian = 1;
  1302. dsd_cfg.dsd_channel_block_size = 1;
  1303. gdec = &(codec_options->generic.reserved[1]);
  1304. if (((struct snd_generic_dec_dsd *)gdec)->blk_size ==
  1305. DSD_BLOCK_SIZE_4)
  1306. dsd_cfg.dsd_channel_block_size =
  1307. ((struct snd_generic_dec_dsd *)gdec)->blk_size;
  1308. ret = q6asm_media_format_block_dsd(prtd->audio_client,
  1309. &dsd_cfg, stream_id);
  1310. if (ret < 0)
  1311. pr_err("%s: CMD DSD Format block failed ret %d\n",
  1312. __func__, ret);
  1313. break;
  1314. case FORMAT_TRUEHD:
  1315. pr_debug("SND_AUDIOCODEC_TRUEHD\n");
  1316. /* no media format block needed */
  1317. break;
  1318. case FORMAT_IEC61937:
  1319. pr_debug("SND_AUDIOCODEC_IEC61937\n");
  1320. ret = q6asm_media_format_block_iec(prtd->audio_client,
  1321. prtd->sample_rate,
  1322. prtd->num_channels);
  1323. if (ret < 0)
  1324. pr_err("%s: CMD IEC61937 Format block failed ret %d\n",
  1325. __func__, ret);
  1326. break;
  1327. case FORMAT_APTX:
  1328. pr_debug("SND_AUDIOCODEC_APTX\n");
  1329. memset(&aptx_cfg, 0x0, sizeof(struct aptx_dec_bt_addr_cfg));
  1330. ret = q6asm_stream_media_format_block_aptx_dec(
  1331. prtd->audio_client,
  1332. prtd->sample_rate,
  1333. stream_id);
  1334. if (ret >= 0) {
  1335. gdec = &(codec_options->generic.reserved[1]);
  1336. aptx_cfg.nap =
  1337. ((struct snd_generic_dec_aptx *)gdec)->nap;
  1338. aptx_cfg.uap =
  1339. ((struct snd_generic_dec_aptx *)gdec)->uap;
  1340. aptx_cfg.lap =
  1341. ((struct snd_generic_dec_aptx *)gdec)->lap;
  1342. q6asm_set_aptx_dec_bt_addr(prtd->audio_client,
  1343. &aptx_cfg);
  1344. } else {
  1345. pr_err("%s: CMD Format block failed ret %d\n",
  1346. __func__, ret);
  1347. }
  1348. break;
  1349. case FORMAT_AMRNB:
  1350. pr_debug("SND_AUDIOCODEC_AMR\n");
  1351. /* no media format block needed */
  1352. break;
  1353. case FORMAT_AMRWB:
  1354. pr_debug("SND_AUDIOCODEC_AMRWB\n");
  1355. /* no media format block needed */
  1356. break;
  1357. case FORMAT_AMR_WB_PLUS:
  1358. pr_debug("SND_AUDIOCODEC_AMRWBPLUS\n");
  1359. memset(&amrwbplus_cfg, 0x0, sizeof(struct asm_amrwbplus_cfg));
  1360. gdec = &(codec_options->generic.reserved[1]);
  1361. amrwbplus_cfg.amr_frame_fmt =
  1362. ((struct snd_generic_dec_amrwb_plus *)gdec)->bit_stream_fmt;
  1363. ret = q6asm_media_format_block_amrwbplus(
  1364. prtd->audio_client,
  1365. &amrwbplus_cfg);
  1366. if (ret < 0)
  1367. pr_err("%s: CMD AMRWBPLUS Format block failed ret %d\n",
  1368. __func__, ret);
  1369. break;
  1370. default:
  1371. pr_debug("%s, unsupported format, skip", __func__);
  1372. break;
  1373. }
  1374. return ret;
  1375. }
  1376. static int msm_compr_init_pp_params(struct snd_compr_stream *cstream,
  1377. struct audio_client *ac)
  1378. {
  1379. int ret = 0;
  1380. struct asm_softvolume_params softvol = {
  1381. .period = SOFT_VOLUME_PERIOD,
  1382. .step = SOFT_VOLUME_STEP,
  1383. .rampingcurve = SOFT_VOLUME_CURVE_LINEAR,
  1384. };
  1385. switch (ac->topology) {
  1386. default:
  1387. ret = q6asm_set_softvolume_v2(ac, &softvol,
  1388. SOFT_VOLUME_INSTANCE_1);
  1389. if (ret < 0)
  1390. pr_err("%s: Send SoftVolume Param failed ret=%d\n",
  1391. __func__, ret);
  1392. break;
  1393. }
  1394. return ret;
  1395. }
  1396. static int msm_compr_configure_dsp_for_playback
  1397. (struct snd_compr_stream *cstream)
  1398. {
  1399. struct snd_compr_runtime *runtime = cstream->runtime;
  1400. struct msm_compr_audio *prtd = runtime->private_data;
  1401. struct snd_soc_pcm_runtime *soc_prtd = cstream->private_data;
  1402. uint16_t bits_per_sample = 16;
  1403. int dir = IN, ret = 0;
  1404. struct audio_client *ac = prtd->audio_client;
  1405. uint32_t stream_index;
  1406. struct asm_softpause_params softpause = {
  1407. .enable = SOFT_PAUSE_ENABLE,
  1408. .period = SOFT_PAUSE_PERIOD,
  1409. .step = SOFT_PAUSE_STEP,
  1410. .rampingcurve = SOFT_PAUSE_CURVE_LINEAR,
  1411. };
  1412. struct asm_softvolume_params softvol = {
  1413. .period = SOFT_VOLUME_PERIOD,
  1414. .step = SOFT_VOLUME_STEP,
  1415. .rampingcurve = SOFT_VOLUME_CURVE_LINEAR,
  1416. };
  1417. struct snd_kcontrol *kctl;
  1418. struct snd_ctl_elem_value kctl_elem_value;
  1419. uint16_t target_asm_bit_width = 0;
  1420. pr_debug("%s: stream_id %d\n", __func__, ac->stream_id);
  1421. stream_index = STREAM_ARRAY_INDEX(ac->stream_id);
  1422. if (stream_index >= MAX_NUMBER_OF_STREAMS || stream_index < 0) {
  1423. pr_err("%s: Invalid stream index:%d", __func__, stream_index);
  1424. return -EINVAL;
  1425. }
  1426. kctl = snd_soc_card_get_kcontrol(soc_prtd->card,
  1427. DSP_BIT_WIDTH_MIXER_CTL);
  1428. if (kctl) {
  1429. kctl->get(kctl, &kctl_elem_value);
  1430. target_asm_bit_width = kctl_elem_value.value.integer.value[0];
  1431. if (target_asm_bit_width > 0) {
  1432. pr_debug("%s enforce ASM bitwidth to %d from %d\n",
  1433. __func__,
  1434. target_asm_bit_width,
  1435. bits_per_sample);
  1436. bits_per_sample = target_asm_bit_width;
  1437. }
  1438. } else {
  1439. pr_info("%s: failed to get mixer ctl for %s.\n",
  1440. __func__, DSP_BIT_WIDTH_MIXER_CTL);
  1441. }
  1442. if ((prtd->codec_param.codec.format == SNDRV_PCM_FORMAT_S24_LE) ||
  1443. (prtd->codec_param.codec.format == SNDRV_PCM_FORMAT_S24_3LE))
  1444. bits_per_sample = 24;
  1445. else if (prtd->codec_param.codec.format == SNDRV_PCM_FORMAT_S32_LE)
  1446. bits_per_sample = 32;
  1447. if (prtd->compr_passthr != LEGACY_PCM) {
  1448. ret = q6asm_open_write_compressed(ac, prtd->codec,
  1449. prtd->compr_passthr);
  1450. if (ret < 0) {
  1451. pr_err("%s:ASM open write err[%d] for compr_type[%d]\n",
  1452. __func__, ret, prtd->compr_passthr);
  1453. return ret;
  1454. }
  1455. prtd->gapless_state.stream_opened[stream_index] = 1;
  1456. ret = msm_pcm_routing_reg_phy_compr_stream(
  1457. soc_prtd->dai_link->id,
  1458. ac->perf_mode,
  1459. prtd->session_id,
  1460. SNDRV_PCM_STREAM_PLAYBACK,
  1461. prtd->compr_passthr);
  1462. if (ret) {
  1463. pr_err("%s: compr stream reg failed:%d\n", __func__,
  1464. ret);
  1465. return ret;
  1466. }
  1467. } else {
  1468. pr_debug("%s: stream_id %d bits_per_sample %d\n",
  1469. __func__, ac->stream_id, bits_per_sample);
  1470. if (q6core_get_avcs_api_version_per_service(
  1471. APRV2_IDS_SERVICE_ID_ADSP_ASM_V) >=
  1472. ADSP_ASM_API_VERSION_V2)
  1473. ret = q6asm_stream_open_write_v5(ac,
  1474. prtd->codec, bits_per_sample,
  1475. ac->stream_id,
  1476. prtd->gapless_state.use_dsp_gapless_mode);
  1477. else
  1478. ret = q6asm_stream_open_write_v4(ac,
  1479. prtd->codec, bits_per_sample,
  1480. ac->stream_id,
  1481. prtd->gapless_state.use_dsp_gapless_mode);
  1482. if (ret < 0) {
  1483. pr_err("%s:ASM open write err[%d] for compr type[%d]\n",
  1484. __func__, ret, prtd->compr_passthr);
  1485. return -ENOMEM;
  1486. }
  1487. prtd->gapless_state.stream_opened[stream_index] = 1;
  1488. pr_debug("%s: BE id %d\n", __func__, soc_prtd->dai_link->id);
  1489. ret = msm_pcm_routing_reg_phy_stream(soc_prtd->dai_link->id,
  1490. ac->perf_mode,
  1491. prtd->session_id,
  1492. SNDRV_PCM_STREAM_PLAYBACK);
  1493. if (ret) {
  1494. pr_err("%s: stream reg failed:%d\n", __func__, ret);
  1495. return ret;
  1496. }
  1497. }
  1498. ret = msm_compr_set_volume(cstream, 0, 0);
  1499. if (ret < 0)
  1500. pr_err("%s : Set Volume failed : %d", __func__, ret);
  1501. if (prtd->compr_passthr != LEGACY_PCM) {
  1502. pr_debug("%s : Don't send cal and PP params for compress path",
  1503. __func__);
  1504. } else {
  1505. ret = q6asm_send_cal(ac);
  1506. if (ret < 0)
  1507. pr_debug("%s : Send cal failed : %d", __func__, ret);
  1508. ret = q6asm_set_softpause(ac, &softpause);
  1509. if (ret < 0)
  1510. pr_err("%s: Send SoftPause Param failed ret=%d\n",
  1511. __func__, ret);
  1512. ret = q6asm_set_softvolume(ac, &softvol);
  1513. if (ret < 0)
  1514. pr_err("%s: Send SoftVolume Param failed ret=%d\n",
  1515. __func__, ret);
  1516. }
  1517. ret = q6asm_set_io_mode(ac, (COMPRESSED_STREAM_IO | ASYNC_IO_MODE));
  1518. if (ret < 0) {
  1519. pr_err("%s: Set IO mode failed\n", __func__);
  1520. return -EINVAL;
  1521. }
  1522. runtime->fragments = prtd->codec_param.buffer.fragments;
  1523. runtime->fragment_size = prtd->codec_param.buffer.fragment_size;
  1524. pr_debug("allocate %d buffers each of size %d\n",
  1525. runtime->fragments,
  1526. runtime->fragment_size);
  1527. ret = q6asm_audio_client_buf_alloc_contiguous(dir, ac,
  1528. runtime->fragment_size,
  1529. runtime->fragments);
  1530. if (ret < 0) {
  1531. pr_err("Audio Start: Buffer Allocation failed rc = %d\n", ret);
  1532. return -ENOMEM;
  1533. }
  1534. prtd->byte_offset = 0;
  1535. prtd->copied_total = 0;
  1536. prtd->app_pointer = 0;
  1537. prtd->bytes_received = 0;
  1538. prtd->bytes_sent = 0;
  1539. prtd->buffer = ac->port[dir].buf[0].data;
  1540. prtd->buffer_paddr = ac->port[dir].buf[0].phys;
  1541. prtd->buffer_size = runtime->fragments * runtime->fragment_size;
  1542. /* Bit-0 of flags represent timestamp mode */
  1543. /* reserved[1] is for flags */
  1544. if (prtd->codec_param.codec.reserved[1] & COMPRESSED_TIMESTAMP_FLAG)
  1545. prtd->ts_header_offset = sizeof(struct snd_codec_metadata);
  1546. else
  1547. prtd->ts_header_offset = 0;
  1548. ret = msm_compr_send_media_format_block(cstream, ac->stream_id, false);
  1549. if (ret < 0)
  1550. pr_err("%s, failed to send media format block\n", __func__);
  1551. return ret;
  1552. }
  1553. static int msm_compr_configure_dsp_for_capture(struct snd_compr_stream *cstream)
  1554. {
  1555. struct snd_compr_runtime *runtime = cstream->runtime;
  1556. struct msm_compr_audio *prtd = runtime->private_data;
  1557. struct snd_soc_pcm_runtime *soc_prtd = cstream->private_data;
  1558. uint16_t bits_per_sample;
  1559. uint16_t sample_word_size;
  1560. int dir = OUT, ret = 0;
  1561. struct audio_client *ac = prtd->audio_client;
  1562. uint32_t stream_index;
  1563. uint32_t enc_cfg_id = ENC_CFG_ID_NONE;
  1564. bool compress_ts = false;
  1565. switch (prtd->codec_param.codec.format) {
  1566. case SNDRV_PCM_FORMAT_S24_LE:
  1567. bits_per_sample = 24;
  1568. sample_word_size = 32;
  1569. break;
  1570. case SNDRV_PCM_FORMAT_S24_3LE:
  1571. bits_per_sample = 24;
  1572. sample_word_size = 24;
  1573. break;
  1574. case SNDRV_PCM_FORMAT_S32_LE:
  1575. bits_per_sample = 32;
  1576. sample_word_size = 32;
  1577. break;
  1578. case SNDRV_PCM_FORMAT_S16_LE:
  1579. default:
  1580. bits_per_sample = 16;
  1581. sample_word_size = 16;
  1582. if (prtd->codec == FORMAT_BESPOKE)
  1583. enc_cfg_id =
  1584. prtd->codec_param.codec.options.generic.reserved[0];
  1585. break;
  1586. }
  1587. pr_debug("%s: stream_id %d bits_per_sample %d compr_passthr %d\n",
  1588. __func__, ac->stream_id, bits_per_sample,
  1589. prtd->compr_passthr);
  1590. if (prtd->compr_passthr != LEGACY_PCM) {
  1591. ret = q6asm_open_read_compressed(prtd->audio_client,
  1592. prtd->codec, prtd->compr_passthr);
  1593. if (ret < 0) {
  1594. pr_err("%s:ASM open read err[%d] for compr_type[%d]\n",
  1595. __func__, ret, prtd->compr_passthr);
  1596. return ret;
  1597. }
  1598. ret = msm_pcm_routing_reg_phy_compr_stream(
  1599. soc_prtd->dai_link->id,
  1600. ac->perf_mode,
  1601. prtd->session_id,
  1602. SNDRV_PCM_STREAM_CAPTURE,
  1603. prtd->compr_passthr);
  1604. if (ret) {
  1605. pr_err("%s: compr stream reg failed:%d\n",
  1606. __func__, ret);
  1607. return ret;
  1608. }
  1609. } else {
  1610. /* reserved[1] is for flags */
  1611. if (prtd->codec_param.codec.reserved[1]
  1612. & COMPRESSED_TIMESTAMP_FLAG)
  1613. compress_ts = true;
  1614. if (q6core_get_avcs_api_version_per_service(
  1615. APRV2_IDS_SERVICE_ID_ADSP_ASM_V) >=
  1616. ADSP_ASM_API_VERSION_V2)
  1617. ret = q6asm_open_read_v5(prtd->audio_client,
  1618. prtd->codec, bits_per_sample,
  1619. compress_ts, enc_cfg_id);
  1620. else
  1621. ret = q6asm_open_read_v4(prtd->audio_client,
  1622. prtd->codec, bits_per_sample,
  1623. compress_ts, enc_cfg_id);
  1624. if (ret < 0) {
  1625. pr_err("%s: q6asm_open_read failed:%d\n",
  1626. __func__, ret);
  1627. return ret;
  1628. }
  1629. ret = msm_pcm_routing_reg_phy_stream(soc_prtd->dai_link->id,
  1630. ac->perf_mode,
  1631. prtd->session_id,
  1632. SNDRV_PCM_STREAM_CAPTURE);
  1633. if (ret) {
  1634. pr_err("%s: stream reg failed:%d\n", __func__, ret);
  1635. return ret;
  1636. }
  1637. }
  1638. ret = q6asm_set_io_mode(ac, (COMPRESSED_STREAM_IO | ASYNC_IO_MODE));
  1639. if (ret < 0) {
  1640. pr_err("%s: Set IO mode failed\n", __func__);
  1641. return -EINVAL;
  1642. }
  1643. stream_index = STREAM_ARRAY_INDEX(ac->stream_id);
  1644. if (stream_index >= MAX_NUMBER_OF_STREAMS || stream_index < 0) {
  1645. pr_err("%s: Invalid stream index:%d", __func__, stream_index);
  1646. return -EINVAL;
  1647. }
  1648. runtime->fragments = prtd->codec_param.buffer.fragments;
  1649. runtime->fragment_size = prtd->codec_param.buffer.fragment_size;
  1650. pr_debug("%s: allocate %d buffers each of size %d\n",
  1651. __func__, runtime->fragments,
  1652. runtime->fragment_size);
  1653. ret = q6asm_audio_client_buf_alloc_contiguous(dir, ac,
  1654. runtime->fragment_size,
  1655. runtime->fragments);
  1656. if (ret < 0) {
  1657. pr_err("Audio Start: Buffer Allocation failed rc = %d\n", ret);
  1658. return -ENOMEM;
  1659. }
  1660. prtd->byte_offset = 0;
  1661. prtd->received_total = 0;
  1662. prtd->app_pointer = 0;
  1663. prtd->bytes_copied = 0;
  1664. prtd->bytes_read = 0;
  1665. prtd->bytes_read_offset = 0;
  1666. prtd->buffer = ac->port[dir].buf[0].data;
  1667. prtd->buffer_paddr = ac->port[dir].buf[0].phys;
  1668. prtd->buffer_size = runtime->fragments * runtime->fragment_size;
  1669. /* Bit-0 of flags represent timestamp mode */
  1670. /* reserved[1] is for flags */
  1671. if (prtd->codec_param.codec.reserved[1] & COMPRESSED_TIMESTAMP_FLAG)
  1672. prtd->ts_header_offset = sizeof(struct snd_codec_metadata);
  1673. else
  1674. prtd->ts_header_offset = 0;
  1675. pr_debug("%s: sample_rate = %d channels = %d bps = %d sample_word_size = %d\n",
  1676. __func__, prtd->sample_rate, prtd->num_channels,
  1677. bits_per_sample, sample_word_size);
  1678. if (prtd->codec == FORMAT_BESPOKE) {
  1679. /*
  1680. * For BESPOKE codec, encoder specific config params are
  1681. * included as part of generic.
  1682. */
  1683. ret = q6asm_enc_cfg_blk_custom(prtd->audio_client, prtd->sample_rate,
  1684. prtd->num_channels, prtd->codec,
  1685. (void *)&prtd->codec_param.codec.options.generic);
  1686. } else if (prtd->compr_passthr == LEGACY_PCM) {
  1687. if (q6core_get_avcs_api_version_per_service(
  1688. APRV2_IDS_SERVICE_ID_ADSP_ASM_V) >=
  1689. ADSP_ASM_API_VERSION_V2)
  1690. ret = q6asm_enc_cfg_blk_pcm_format_support_v5(
  1691. prtd->audio_client,
  1692. prtd->sample_rate, prtd->num_channels,
  1693. bits_per_sample, sample_word_size,
  1694. ASM_LITTLE_ENDIAN, DEFAULT_QF);
  1695. else
  1696. ret = q6asm_enc_cfg_blk_pcm_format_support_v4(
  1697. prtd->audio_client,
  1698. prtd->sample_rate, prtd->num_channels,
  1699. bits_per_sample, sample_word_size,
  1700. ASM_LITTLE_ENDIAN, DEFAULT_QF);
  1701. }
  1702. return ret;
  1703. }
  1704. static int msm_compr_playback_open(struct snd_compr_stream *cstream)
  1705. {
  1706. struct snd_compr_runtime *runtime = cstream->runtime;
  1707. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  1708. struct snd_soc_component *component = NULL;
  1709. struct msm_compr_audio *prtd = NULL;
  1710. struct msm_compr_pdata *pdata = NULL;
  1711. enum apr_subsys_state subsys_state;
  1712. pr_debug("%s\n", __func__);
  1713. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  1714. if (!component) {
  1715. pr_err("%s: component is NULL\n", __func__);
  1716. return -EINVAL;
  1717. }
  1718. pdata = snd_soc_component_get_drvdata(component);
  1719. if (pdata->is_in_use[rtd->dai_link->id] == true) {
  1720. pr_err("%s: %s is already in use, err: %d\n",
  1721. __func__, rtd->dai_link->cpus->dai_name, -EBUSY);
  1722. return -EBUSY;
  1723. }
  1724. subsys_state = apr_get_subsys_state();
  1725. if (subsys_state == APR_SUBSYS_DOWN) {
  1726. pr_debug("%s: adsp is down\n", __func__);
  1727. return -ENETRESET;
  1728. }
  1729. prtd = kzalloc(sizeof(struct msm_compr_audio), GFP_KERNEL);
  1730. if (prtd == NULL) {
  1731. pr_err("Failed to allocate memory for msm_compr_audio\n");
  1732. return -ENOMEM;
  1733. }
  1734. runtime->private_data = NULL;
  1735. prtd->cstream = cstream;
  1736. pdata->cstream[rtd->dai_link->id] = cstream;
  1737. pdata->audio_effects[rtd->dai_link->id] =
  1738. kzalloc(sizeof(struct msm_compr_audio_effects), GFP_KERNEL);
  1739. if (pdata->audio_effects[rtd->dai_link->id] == NULL) {
  1740. pr_err("%s: Could not allocate memory for effects\n", __func__);
  1741. pdata->cstream[rtd->dai_link->id] = NULL;
  1742. kfree(prtd);
  1743. return -ENOMEM;
  1744. }
  1745. pdata->dec_params[rtd->dai_link->id] =
  1746. kzalloc(sizeof(struct msm_compr_dec_params), GFP_KERNEL);
  1747. if (pdata->dec_params[rtd->dai_link->id] == NULL) {
  1748. pr_err("%s: Could not allocate memory for dec params\n",
  1749. __func__);
  1750. kfree(pdata->audio_effects[rtd->dai_link->id]);
  1751. pdata->audio_effects[rtd->dai_link->id] = NULL;
  1752. pdata->cstream[rtd->dai_link->id] = NULL;
  1753. kfree(prtd);
  1754. return -ENOMEM;
  1755. }
  1756. prtd->codec = FORMAT_MP3;
  1757. prtd->bytes_received = 0;
  1758. prtd->bytes_sent = 0;
  1759. prtd->copied_total = 0;
  1760. prtd->byte_offset = 0;
  1761. prtd->sample_rate = 44100;
  1762. prtd->num_channels = 2;
  1763. prtd->drain_ready = 0;
  1764. prtd->last_buffer = 0;
  1765. prtd->first_buffer = 1;
  1766. prtd->partial_drain_delay = 0;
  1767. prtd->next_stream = 0;
  1768. memset(&prtd->gapless_state, 0, sizeof(struct msm_compr_gapless_state));
  1769. /*
  1770. * Update the use_dsp_gapless_mode from gapless struture with the value
  1771. * part of platform data.
  1772. */
  1773. prtd->gapless_state.use_dsp_gapless_mode = pdata->use_dsp_gapless_mode;
  1774. pr_debug("%s: gapless mode %d", __func__, pdata->use_dsp_gapless_mode);
  1775. spin_lock_init(&prtd->lock);
  1776. atomic_set(&prtd->eos, 0);
  1777. atomic_set(&prtd->start, 0);
  1778. atomic_set(&prtd->drain, 0);
  1779. #if !IS_ENABLED(CONFIG_AUDIO_QGKI)
  1780. atomic_set(&prtd->partial_drain, 0);
  1781. #endif
  1782. atomic_set(&prtd->xrun, 0);
  1783. atomic_set(&prtd->close, 0);
  1784. atomic_set(&prtd->wait_on_close, 0);
  1785. atomic_set(&prtd->error, 0);
  1786. init_waitqueue_head(&prtd->eos_wait);
  1787. init_waitqueue_head(&prtd->drain_wait);
  1788. init_waitqueue_head(&prtd->close_wait);
  1789. init_waitqueue_head(&prtd->wait_for_stream_avail);
  1790. runtime->private_data = prtd;
  1791. populate_codec_list(prtd);
  1792. prtd->audio_client = q6asm_audio_client_alloc(
  1793. (app_cb)compr_event_handler, prtd);
  1794. if (prtd->audio_client == NULL) {
  1795. pr_err("%s: Could not allocate memory for client\n", __func__);
  1796. kfree(pdata->audio_effects[rtd->dai_link->id]);
  1797. pdata->audio_effects[rtd->dai_link->id] = NULL;
  1798. kfree(pdata->dec_params[rtd->dai_link->id]);
  1799. pdata->dec_params[rtd->dai_link->id] = NULL;
  1800. pdata->cstream[rtd->dai_link->id] = NULL;
  1801. kfree(prtd);
  1802. runtime->private_data = NULL;
  1803. return -ENOMEM;
  1804. }
  1805. pr_debug("%s: session ID %d\n", __func__, prtd->audio_client->session);
  1806. prtd->audio_client->perf_mode = false;
  1807. prtd->session_id = prtd->audio_client->session;
  1808. msm_adsp_init_mixer_ctl_pp_event_queue(rtd);
  1809. pdata->is_in_use[rtd->dai_link->id] = true;
  1810. return 0;
  1811. }
  1812. static int msm_compr_capture_open(struct snd_compr_stream *cstream)
  1813. {
  1814. struct snd_compr_runtime *runtime = cstream->runtime;
  1815. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  1816. struct snd_soc_component *component = NULL;
  1817. struct msm_compr_audio *prtd;
  1818. struct msm_compr_pdata *pdata = NULL;
  1819. enum apr_subsys_state subsys_state;
  1820. pr_debug("%s\n", __func__);
  1821. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  1822. if (!component) {
  1823. pr_err("%s: component is NULL\n", __func__);
  1824. return -EINVAL;
  1825. }
  1826. pdata = snd_soc_component_get_drvdata(component);
  1827. if (!pdata) {
  1828. pr_err("%s: pdata is NULL\n", __func__);
  1829. return -EINVAL;
  1830. }
  1831. subsys_state = apr_get_subsys_state();
  1832. if (subsys_state == APR_SUBSYS_DOWN) {
  1833. pr_debug("%s: adsp is down\n", __func__);
  1834. return -ENETRESET;
  1835. }
  1836. prtd = kzalloc(sizeof(struct msm_compr_audio), GFP_KERNEL);
  1837. if (!prtd) {
  1838. pr_err("Failed to allocate memory for msm_compr_audio\n");
  1839. return -ENOMEM;
  1840. }
  1841. runtime->private_data = NULL;
  1842. prtd->cstream = cstream;
  1843. pdata->cstream[rtd->dai_link->id] = cstream;
  1844. prtd->audio_client = q6asm_audio_client_alloc(
  1845. (app_cb)compr_event_handler, prtd);
  1846. if (!prtd->audio_client) {
  1847. pr_err("%s: Could not allocate memory for client\n", __func__);
  1848. pdata->cstream[rtd->dai_link->id] = NULL;
  1849. kfree(prtd);
  1850. return -ENOMEM;
  1851. }
  1852. pr_debug("%s: session ID %d\n", __func__, prtd->audio_client->session);
  1853. prtd->audio_client->perf_mode = false;
  1854. prtd->session_id = prtd->audio_client->session;
  1855. prtd->codec = FORMAT_LINEAR_PCM;
  1856. prtd->bytes_copied = 0;
  1857. prtd->bytes_read = 0;
  1858. prtd->bytes_read_offset = 0;
  1859. prtd->received_total = 0;
  1860. prtd->byte_offset = 0;
  1861. prtd->sample_rate = 48000;
  1862. prtd->num_channels = 2;
  1863. prtd->first_buffer = 0;
  1864. spin_lock_init(&prtd->lock);
  1865. atomic_set(&prtd->eos, 0);
  1866. atomic_set(&prtd->start, 0);
  1867. atomic_set(&prtd->drain, 0);
  1868. #if !IS_ENABLED(CONFIG_AUDIO_QGKI)
  1869. atomic_set(&prtd->partial_drain, 0);
  1870. #endif
  1871. atomic_set(&prtd->xrun, 0);
  1872. atomic_set(&prtd->close, 0);
  1873. atomic_set(&prtd->wait_on_close, 0);
  1874. atomic_set(&prtd->error, 0);
  1875. init_waitqueue_head(&prtd->eos_wait);
  1876. init_waitqueue_head(&prtd->drain_wait);
  1877. init_waitqueue_head(&prtd->close_wait);
  1878. init_waitqueue_head(&prtd->wait_for_stream_avail);
  1879. runtime->private_data = prtd;
  1880. return 0;
  1881. }
  1882. static int msm_compr_open(struct snd_compr_stream *cstream)
  1883. {
  1884. int ret = 0;
  1885. if (cstream->direction == SND_COMPRESS_PLAYBACK)
  1886. ret = msm_compr_playback_open(cstream);
  1887. else if (cstream->direction == SND_COMPRESS_CAPTURE)
  1888. ret = msm_compr_capture_open(cstream);
  1889. return ret;
  1890. }
  1891. static int msm_compr_playback_free(struct snd_compr_stream *cstream)
  1892. {
  1893. struct snd_compr_runtime *runtime;
  1894. struct msm_compr_audio *prtd;
  1895. struct snd_soc_pcm_runtime *soc_prtd;
  1896. struct snd_soc_component *component = NULL;
  1897. struct msm_compr_pdata *pdata;
  1898. struct audio_client *ac;
  1899. int dir = IN, ret = 0, stream_id;
  1900. unsigned long flags;
  1901. uint32_t stream_index;
  1902. pr_debug("%s\n", __func__);
  1903. if (!cstream) {
  1904. pr_err("%s cstream is null\n", __func__);
  1905. return 0;
  1906. }
  1907. runtime = cstream->runtime;
  1908. soc_prtd = cstream->private_data;
  1909. if (!runtime || !soc_prtd) {
  1910. pr_err("%s runtime or soc_prtd is null\n",
  1911. __func__);
  1912. return 0;
  1913. }
  1914. component = snd_soc_rtdcom_lookup(soc_prtd, DRV_NAME);
  1915. if (!component) {
  1916. pr_err("%s component is null\n", __func__);
  1917. return 0;
  1918. }
  1919. prtd = runtime->private_data;
  1920. if (!prtd) {
  1921. pr_err("%s prtd is null\n", __func__);
  1922. return 0;
  1923. }
  1924. prtd->cmd_interrupt = 1;
  1925. #if IS_ENABLED(CONFIG_AUDIO_QGKI)
  1926. wake_up(&prtd->drain_wait);
  1927. #endif
  1928. pdata = snd_soc_component_get_drvdata(component);
  1929. ac = prtd->audio_client;
  1930. if (!pdata || !ac) {
  1931. pr_err("%s pdata or ac is null\n", __func__);
  1932. return 0;
  1933. }
  1934. if (atomic_read(&prtd->eos)) {
  1935. ret = wait_event_timeout(prtd->eos_wait,
  1936. prtd->eos_ack,
  1937. msecs_to_jiffies(TIMEOUT_MS));
  1938. if (!ret)
  1939. pr_err("%s: CMD_EOS failed\n", __func__);
  1940. }
  1941. if (atomic_read(&prtd->close)) {
  1942. prtd->cmd_ack = 0;
  1943. atomic_set(&prtd->wait_on_close, 1);
  1944. ret = wait_event_timeout(prtd->close_wait,
  1945. prtd->cmd_ack,
  1946. msecs_to_jiffies(TIMEOUT_MS));
  1947. if (!ret)
  1948. pr_err("%s: CMD_CLOSE failed\n", __func__);
  1949. }
  1950. spin_lock_irqsave(&prtd->lock, flags);
  1951. stream_id = ac->stream_id;
  1952. stream_index = STREAM_ARRAY_INDEX(NEXT_STREAM_ID(stream_id));
  1953. if ((stream_index < MAX_NUMBER_OF_STREAMS && stream_index >= 0) &&
  1954. (prtd->gapless_state.stream_opened[stream_index])) {
  1955. prtd->gapless_state.stream_opened[stream_index] = 0;
  1956. spin_unlock_irqrestore(&prtd->lock, flags);
  1957. pr_debug(" close stream %d", NEXT_STREAM_ID(stream_id));
  1958. q6asm_stream_cmd(ac, CMD_CLOSE, NEXT_STREAM_ID(stream_id));
  1959. spin_lock_irqsave(&prtd->lock, flags);
  1960. }
  1961. stream_index = STREAM_ARRAY_INDEX(stream_id);
  1962. if ((stream_index < MAX_NUMBER_OF_STREAMS && stream_index >= 0) &&
  1963. (prtd->gapless_state.stream_opened[stream_index])) {
  1964. prtd->gapless_state.stream_opened[stream_index] = 0;
  1965. spin_unlock_irqrestore(&prtd->lock, flags);
  1966. pr_debug("close stream %d", stream_id);
  1967. q6asm_stream_cmd(ac, CMD_CLOSE, stream_id);
  1968. spin_lock_irqsave(&prtd->lock, flags);
  1969. }
  1970. spin_unlock_irqrestore(&prtd->lock, flags);
  1971. mutex_lock(&pdata->lock);
  1972. pdata->cstream[soc_prtd->dai_link->id] = NULL;
  1973. if (cstream->direction == SND_COMPRESS_PLAYBACK) {
  1974. msm_pcm_routing_dereg_phy_stream(soc_prtd->dai_link->id,
  1975. SNDRV_PCM_STREAM_PLAYBACK);
  1976. }
  1977. q6asm_audio_client_buf_free_contiguous(dir, ac);
  1978. q6asm_audio_client_free(ac);
  1979. msm_adsp_clean_mixer_ctl_pp_event_queue(soc_prtd);
  1980. if (pdata->audio_effects[soc_prtd->dai_link->id] != NULL) {
  1981. kfree(pdata->audio_effects[soc_prtd->dai_link->id]);
  1982. pdata->audio_effects[soc_prtd->dai_link->id] = NULL;
  1983. }
  1984. if (pdata->dec_params[soc_prtd->dai_link->id] != NULL) {
  1985. kfree(pdata->dec_params[soc_prtd->dai_link->id]);
  1986. pdata->dec_params[soc_prtd->dai_link->id] = NULL;
  1987. }
  1988. if (pdata->ch_map[soc_prtd->dai_link->id]) {
  1989. pdata->ch_map[soc_prtd->dai_link->id]->set_ch_map = false;
  1990. }
  1991. pdata->is_in_use[soc_prtd->dai_link->id] = false;
  1992. kfree(prtd);
  1993. runtime->private_data = NULL;
  1994. mutex_unlock(&pdata->lock);
  1995. return 0;
  1996. }
  1997. static int msm_compr_capture_free(struct snd_compr_stream *cstream)
  1998. {
  1999. struct snd_compr_runtime *runtime;
  2000. struct msm_compr_audio *prtd;
  2001. struct snd_soc_pcm_runtime *soc_prtd;
  2002. struct snd_soc_component *component = NULL;
  2003. struct msm_compr_pdata *pdata;
  2004. struct audio_client *ac;
  2005. int dir = OUT, stream_id;
  2006. unsigned long flags;
  2007. uint32_t stream_index;
  2008. if (!cstream) {
  2009. pr_err("%s cstream is null\n", __func__);
  2010. return 0;
  2011. }
  2012. runtime = cstream->runtime;
  2013. soc_prtd = cstream->private_data;
  2014. if (!runtime || !soc_prtd) {
  2015. pr_err("%s runtime or soc_prtd is null\n", __func__);
  2016. return 0;
  2017. }
  2018. component = snd_soc_rtdcom_lookup(soc_prtd, DRV_NAME);
  2019. if (!component) {
  2020. pr_err("%s component is null\n", __func__);
  2021. return 0;
  2022. }
  2023. prtd = runtime->private_data;
  2024. if (!prtd) {
  2025. pr_err("%s prtd is null\n", __func__);
  2026. return 0;
  2027. }
  2028. pdata = snd_soc_component_get_drvdata(component);
  2029. ac = prtd->audio_client;
  2030. if (!pdata || !ac) {
  2031. pr_err("%s pdata or ac is null\n", __func__);
  2032. return 0;
  2033. }
  2034. spin_lock_irqsave(&prtd->lock, flags);
  2035. stream_id = ac->stream_id;
  2036. stream_index = STREAM_ARRAY_INDEX(stream_id);
  2037. if ((stream_index < MAX_NUMBER_OF_STREAMS && stream_index >= 0)) {
  2038. spin_unlock_irqrestore(&prtd->lock, flags);
  2039. pr_debug("close stream %d", stream_id);
  2040. q6asm_stream_cmd(ac, CMD_CLOSE, stream_id);
  2041. spin_lock_irqsave(&prtd->lock, flags);
  2042. }
  2043. spin_unlock_irqrestore(&prtd->lock, flags);
  2044. mutex_lock(&pdata->lock);
  2045. pdata->cstream[soc_prtd->dai_link->id] = NULL;
  2046. msm_pcm_routing_dereg_phy_stream(soc_prtd->dai_link->id,
  2047. SNDRV_PCM_STREAM_CAPTURE);
  2048. q6asm_audio_client_buf_free_contiguous(dir, ac);
  2049. q6asm_audio_client_free(ac);
  2050. kfree(prtd);
  2051. runtime->private_data = NULL;
  2052. mutex_unlock(&pdata->lock);
  2053. return 0;
  2054. }
  2055. static int msm_compr_free(struct snd_compr_stream *cstream)
  2056. {
  2057. int ret = 0;
  2058. if (cstream->direction == SND_COMPRESS_PLAYBACK)
  2059. ret = msm_compr_playback_free(cstream);
  2060. else if (cstream->direction == SND_COMPRESS_CAPTURE)
  2061. ret = msm_compr_capture_free(cstream);
  2062. return ret;
  2063. }
  2064. static bool msm_compr_validate_codec_compr(__u32 codec_id)
  2065. {
  2066. int32_t i;
  2067. for (i = 0; i < ARRAY_SIZE(compr_codecs); i++) {
  2068. if (compr_codecs[i] == codec_id)
  2069. return true;
  2070. }
  2071. return false;
  2072. }
  2073. /* compress stream operations */
  2074. static int msm_compr_set_params(struct snd_compr_stream *cstream,
  2075. struct snd_compr_params *params)
  2076. {
  2077. struct snd_compr_runtime *runtime = cstream->runtime;
  2078. struct msm_compr_audio *prtd = runtime->private_data;
  2079. int ret = 0, frame_sz = 0;
  2080. int i, num_rates;
  2081. bool is_format_gapless = false;
  2082. pr_debug("%s\n", __func__);
  2083. num_rates = sizeof(supported_sample_rates)/sizeof(unsigned int);
  2084. for (i = 0; i < num_rates; i++)
  2085. if (params->codec.sample_rate == supported_sample_rates[i])
  2086. break;
  2087. if (i == num_rates)
  2088. return -EINVAL;
  2089. memcpy(&prtd->codec_param, params, sizeof(struct snd_compr_params));
  2090. /* ToDo: remove duplicates */
  2091. prtd->num_channels = prtd->codec_param.codec.ch_in;
  2092. prtd->sample_rate = prtd->codec_param.codec.sample_rate;
  2093. pr_debug("%s: sample_rate %d\n", __func__, prtd->sample_rate);
  2094. /* prtd->codec_param.codec.reserved[0] is for compr_passthr */
  2095. if ((prtd->codec_param.codec.reserved[0] >= LEGACY_PCM &&
  2096. prtd->codec_param.
  2097. codec.reserved[0] <= COMPRESSED_PASSTHROUGH_DSD) ||
  2098. (prtd->codec_param.
  2099. codec.reserved[0] == COMPRESSED_PASSTHROUGH_IEC61937))
  2100. prtd->compr_passthr = prtd->codec_param.codec.reserved[0];
  2101. else
  2102. prtd->compr_passthr = LEGACY_PCM;
  2103. pr_debug("%s: compr_passthr = %d", __func__, prtd->compr_passthr);
  2104. if (prtd->compr_passthr != LEGACY_PCM) {
  2105. pr_debug("%s: Reset gapless mode playback for compr_type[%d]\n",
  2106. __func__, prtd->compr_passthr);
  2107. prtd->gapless_state.use_dsp_gapless_mode = 0;
  2108. if (!msm_compr_validate_codec_compr(params->codec.id)) {
  2109. pr_err("%s codec not supported in passthrough,id =%d\n",
  2110. __func__, params->codec.id);
  2111. return -EINVAL;
  2112. }
  2113. }
  2114. /* reserved[1] is for flags */
  2115. if (params->codec.reserved[1] & COMPRESSED_PERF_MODE_FLAG) {
  2116. pr_debug("%s: setting perf mode = %d", __func__, LOW_LATENCY_PCM_MODE);
  2117. prtd->audio_client->perf_mode = LOW_LATENCY_PCM_MODE;
  2118. }
  2119. switch (params->codec.id) {
  2120. case SND_AUDIOCODEC_PCM: {
  2121. pr_debug("SND_AUDIOCODEC_PCM\n");
  2122. prtd->codec = FORMAT_LINEAR_PCM;
  2123. is_format_gapless = true;
  2124. break;
  2125. }
  2126. case SND_AUDIOCODEC_MP3: {
  2127. pr_debug("SND_AUDIOCODEC_MP3\n");
  2128. prtd->codec = FORMAT_MP3;
  2129. frame_sz = MP3_OUTPUT_FRAME_SZ;
  2130. is_format_gapless = true;
  2131. break;
  2132. }
  2133. case SND_AUDIOCODEC_AAC: {
  2134. pr_debug("SND_AUDIOCODEC_AAC\n");
  2135. prtd->codec = FORMAT_MPEG4_AAC;
  2136. frame_sz = AAC_OUTPUT_FRAME_SZ;
  2137. is_format_gapless = true;
  2138. break;
  2139. }
  2140. case SND_AUDIOCODEC_AC3: {
  2141. pr_debug("SND_AUDIOCODEC_AC3\n");
  2142. prtd->codec = FORMAT_AC3;
  2143. frame_sz = AC3_OUTPUT_FRAME_SZ;
  2144. is_format_gapless = true;
  2145. break;
  2146. }
  2147. case SND_AUDIOCODEC_EAC3: {
  2148. pr_debug("SND_AUDIOCODEC_EAC3\n");
  2149. prtd->codec = FORMAT_EAC3;
  2150. frame_sz = EAC3_OUTPUT_FRAME_SZ;
  2151. is_format_gapless = true;
  2152. break;
  2153. }
  2154. case SND_AUDIOCODEC_MP2: {
  2155. pr_debug("SND_AUDIOCODEC_MP2\n");
  2156. prtd->codec = FORMAT_MP2;
  2157. break;
  2158. }
  2159. case SND_AUDIOCODEC_DTS: {
  2160. pr_debug("%s: SND_AUDIOCODEC_DTS\n", __func__);
  2161. prtd->codec = FORMAT_DTS;
  2162. break;
  2163. }
  2164. case SND_AUDIOCODEC_TRUEHD: {
  2165. pr_debug("%s: SND_AUDIOCODEC_TRUEHD\n", __func__);
  2166. prtd->codec = FORMAT_TRUEHD;
  2167. break;
  2168. }
  2169. case SND_AUDIOCODEC_IEC61937: {
  2170. pr_debug("%s: SND_AUDIOCODEC_IEC61937\n", __func__);
  2171. prtd->codec = FORMAT_IEC61937;
  2172. break;
  2173. }
  2174. case SND_AUDIOCODEC_BESPOKE: {
  2175. pr_debug("%s: SND_AUDIOCODEC_BESPOKE\n", __func__);
  2176. ret = msm_compr_get_decoder_format(prtd, &frame_sz,
  2177. &is_format_gapless);
  2178. if (ret < 0)
  2179. return ret;
  2180. prtd->codec = ret;
  2181. break;
  2182. }
  2183. case SND_AUDIOCODEC_AMR: {
  2184. pr_debug("%s:SND_AUDIOCODEC_AMR\n", __func__);
  2185. prtd->codec = FORMAT_AMRNB;
  2186. break;
  2187. }
  2188. case SND_AUDIOCODEC_AMRWB: {
  2189. pr_debug("%s:SND_AUDIOCODEC_AMRWB\n", __func__);
  2190. prtd->codec = FORMAT_AMRWB;
  2191. break;
  2192. }
  2193. case SND_AUDIOCODEC_AMRWBPLUS: {
  2194. pr_debug("%s:SND_AUDIOCODEC_AMRWBPLUS\n", __func__);
  2195. prtd->codec = FORMAT_AMR_WB_PLUS;
  2196. break;
  2197. }
  2198. default:
  2199. pr_err("codec not supported, id =%d\n", params->codec.id);
  2200. return -EINVAL;
  2201. }
  2202. if (!is_format_gapless)
  2203. prtd->gapless_state.use_dsp_gapless_mode = false;
  2204. prtd->partial_drain_delay =
  2205. msm_compr_get_partial_drain_delay(frame_sz, prtd->sample_rate);
  2206. if (cstream->direction == SND_COMPRESS_PLAYBACK)
  2207. ret = msm_compr_configure_dsp_for_playback(cstream);
  2208. else if (cstream->direction == SND_COMPRESS_CAPTURE)
  2209. ret = msm_compr_configure_dsp_for_capture(cstream);
  2210. return ret;
  2211. }
  2212. #if IS_ENABLED(CONFIG_AUDIO_QGKI)
  2213. static int msm_compr_drain_buffer(struct msm_compr_audio *prtd,
  2214. unsigned long *flags)
  2215. {
  2216. int rc = 0;
  2217. atomic_set(&prtd->drain, 1);
  2218. prtd->drain_ready = 0;
  2219. spin_unlock_irqrestore(&prtd->lock, *flags);
  2220. pr_debug("%s: wait for buffer to be drained\n", __func__);
  2221. rc = wait_event_interruptible(prtd->drain_wait,
  2222. prtd->drain_ready ||
  2223. prtd->cmd_interrupt ||
  2224. atomic_read(&prtd->xrun) ||
  2225. atomic_read(&prtd->error));
  2226. pr_debug("%s: out of buffer drain wait with ret %d\n", __func__, rc);
  2227. spin_lock_irqsave(&prtd->lock, *flags);
  2228. if (prtd->cmd_interrupt) {
  2229. pr_debug("%s: buffer drain interrupted by flush)\n", __func__);
  2230. rc = -EINTR;
  2231. prtd->cmd_interrupt = 0;
  2232. }
  2233. if (atomic_read(&prtd->error)) {
  2234. pr_err("%s: Got RESET EVENTS notification, return\n",
  2235. __func__);
  2236. rc = -ENETRESET;
  2237. }
  2238. return rc;
  2239. }
  2240. #endif
  2241. static int msm_compr_wait_for_stream_avail(struct msm_compr_audio *prtd,
  2242. unsigned long *flags)
  2243. {
  2244. int rc = 0;
  2245. pr_debug("next session is already in opened state\n");
  2246. prtd->next_stream = 1;
  2247. prtd->cmd_interrupt = 0;
  2248. spin_unlock_irqrestore(&prtd->lock, *flags);
  2249. /*
  2250. * Wait for stream to be available, or the wait to be interrupted by
  2251. * commands like flush or till a timeout of one second.
  2252. */
  2253. rc = wait_event_timeout(prtd->wait_for_stream_avail,
  2254. prtd->stream_available || prtd->cmd_interrupt, 1 * HZ);
  2255. pr_err("%s:prtd->stream_available %d, prtd->cmd_interrupt %d rc %d\n",
  2256. __func__, prtd->stream_available, prtd->cmd_interrupt, rc);
  2257. spin_lock_irqsave(&prtd->lock, *flags);
  2258. if (rc == 0) {
  2259. pr_err("%s: wait_for_stream_avail timed out\n",
  2260. __func__);
  2261. rc = -ETIMEDOUT;
  2262. } else if (prtd->cmd_interrupt == 1) {
  2263. /*
  2264. * This scenario might not happen as we do not allow
  2265. * flush in transition state.
  2266. */
  2267. pr_debug("%s: wait_for_stream_avail interrupted\n", __func__);
  2268. prtd->cmd_interrupt = 0;
  2269. prtd->stream_available = 0;
  2270. rc = -EINTR;
  2271. } else {
  2272. prtd->stream_available = 0;
  2273. rc = 0;
  2274. }
  2275. pr_debug("%s : rc = %d", __func__, rc);
  2276. return rc;
  2277. }
  2278. static int msm_compr_trigger(struct snd_compr_stream *cstream, int cmd)
  2279. {
  2280. struct snd_compr_runtime *runtime = cstream->runtime;
  2281. struct msm_compr_audio *prtd = runtime->private_data;
  2282. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  2283. struct snd_soc_component *component = NULL;
  2284. struct msm_compr_pdata *pdata = NULL;
  2285. uint32_t *volume = NULL;
  2286. struct audio_client *ac = prtd->audio_client;
  2287. unsigned long fe_id = rtd->dai_link->id;
  2288. int rc = 0;
  2289. #if IS_ENABLED(CONFIG_AUDIO_QGKI)
  2290. int bytes_to_write;
  2291. #endif
  2292. unsigned long flags;
  2293. int stream_id;
  2294. uint32_t stream_index;
  2295. uint16_t bits_per_sample = 16;
  2296. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  2297. if (!component) {
  2298. pr_err("%s: component is NULL\n", __func__);
  2299. return -EINVAL;
  2300. }
  2301. pdata = snd_soc_component_get_drvdata(component);
  2302. if (!pdata) {
  2303. pr_err("%s: pdata is NULL\n", __func__);
  2304. return -EINVAL;
  2305. }
  2306. volume = pdata->volume[rtd->dai_link->id];
  2307. spin_lock_irqsave(&prtd->lock, flags);
  2308. if (atomic_read(&prtd->error)) {
  2309. pr_err("%s Got RESET EVENTS notification, return immediately",
  2310. __func__);
  2311. spin_unlock_irqrestore(&prtd->lock, flags);
  2312. return 0;
  2313. }
  2314. spin_unlock_irqrestore(&prtd->lock, flags);
  2315. switch (cmd) {
  2316. case SNDRV_PCM_TRIGGER_START:
  2317. pr_debug("%s: SNDRV_PCM_TRIGGER_START\n", __func__);
  2318. atomic_set(&prtd->start, 1);
  2319. /*
  2320. * compr_set_volume and compr_init_pp_params
  2321. * are used to configure ASM volume hence not
  2322. * needed for compress passthrough playback.
  2323. *
  2324. * compress passthrough volume is controlled in
  2325. * ADM by adm_send_compressed_device_mute()
  2326. */
  2327. if (prtd->compr_passthr == LEGACY_PCM &&
  2328. cstream->direction == SND_COMPRESS_PLAYBACK) {
  2329. /* set volume for the stream before RUN */
  2330. rc = msm_compr_set_volume(cstream,
  2331. volume[0], volume[1]);
  2332. if (rc)
  2333. pr_err("%s : Set Volume failed : %d\n",
  2334. __func__, rc);
  2335. rc = msm_compr_init_pp_params(cstream, ac);
  2336. if (rc)
  2337. pr_err("%s : init PP params failed : %d\n",
  2338. __func__, rc);
  2339. } else {
  2340. msm_compr_read_buffer(prtd);
  2341. }
  2342. /* issue RUN command for the stream */
  2343. q6asm_run_nowait(prtd->audio_client, prtd->run_mode,
  2344. prtd->start_delay_msw, prtd->start_delay_lsw);
  2345. break;
  2346. case SNDRV_PCM_TRIGGER_STOP:
  2347. spin_lock_irqsave(&prtd->lock, flags);
  2348. pr_debug("%s: SNDRV_PCM_TRIGGER_STOP transition %d\n", __func__,
  2349. prtd->gapless_state.gapless_transition);
  2350. stream_id = ac->stream_id;
  2351. atomic_set(&prtd->start, 0);
  2352. if (cstream->direction == SND_COMPRESS_CAPTURE) {
  2353. q6asm_cmd_nowait(prtd->audio_client, CMD_PAUSE);
  2354. atomic_set(&prtd->xrun, 0);
  2355. prtd->received_total = 0;
  2356. prtd->bytes_copied = 0;
  2357. prtd->bytes_read = 0;
  2358. prtd->bytes_read_offset = 0;
  2359. prtd->byte_offset = 0;
  2360. prtd->app_pointer = 0;
  2361. spin_unlock_irqrestore(&prtd->lock, flags);
  2362. break;
  2363. }
  2364. if (prtd->next_stream) {
  2365. pr_debug("%s: interrupt next track wait queues\n",
  2366. __func__);
  2367. prtd->cmd_interrupt = 1;
  2368. wake_up(&prtd->wait_for_stream_avail);
  2369. prtd->next_stream = 0;
  2370. }
  2371. if (atomic_read(&prtd->eos)) {
  2372. pr_debug("%s: interrupt eos wait queues", __func__);
  2373. /*
  2374. * Gapless playback does not wait for eos, do not set
  2375. * cmd_int and do not wake up eos_wait during gapless
  2376. * transition
  2377. */
  2378. #if IS_ENABLED(CONFIG_AUDIO_QGKI)
  2379. if (!prtd->gapless_state.gapless_transition) {
  2380. prtd->cmd_interrupt = 1;
  2381. wake_up(&prtd->eos_wait);
  2382. }
  2383. #endif
  2384. atomic_set(&prtd->eos, 0);
  2385. }
  2386. if (atomic_read(&prtd->drain)) {
  2387. pr_debug("%s: interrupt drain wait queues", __func__);
  2388. prtd->cmd_interrupt = 1;
  2389. prtd->drain_ready = 1;
  2390. #if IS_ENABLED(CONFIG_AUDIO_QGKI)
  2391. wake_up(&prtd->drain_wait);
  2392. #endif
  2393. atomic_set(&prtd->drain, 0);
  2394. }
  2395. prtd->last_buffer = 0;
  2396. prtd->cmd_ack = 0;
  2397. if (!prtd->gapless_state.gapless_transition) {
  2398. pr_debug("issue CMD_FLUSH stream_id %d\n", stream_id);
  2399. spin_unlock_irqrestore(&prtd->lock, flags);
  2400. q6asm_stream_cmd(
  2401. prtd->audio_client, CMD_FLUSH, stream_id);
  2402. spin_lock_irqsave(&prtd->lock, flags);
  2403. } else {
  2404. prtd->first_buffer = 0;
  2405. }
  2406. /* FIXME. only reset if flush was successful */
  2407. prtd->byte_offset = 0;
  2408. prtd->copied_total = 0;
  2409. prtd->app_pointer = 0;
  2410. prtd->bytes_received = 0;
  2411. prtd->bytes_sent = 0;
  2412. prtd->marker_timestamp = 0;
  2413. atomic_set(&prtd->xrun, 0);
  2414. spin_unlock_irqrestore(&prtd->lock, flags);
  2415. break;
  2416. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  2417. pr_debug("SNDRV_PCM_TRIGGER_PAUSE_PUSH transition %d\n",
  2418. prtd->gapless_state.gapless_transition);
  2419. if (!prtd->gapless_state.gapless_transition) {
  2420. pr_debug("issue CMD_PAUSE stream_id %d\n",
  2421. ac->stream_id);
  2422. q6asm_stream_cmd_nowait(ac, CMD_PAUSE, ac->stream_id);
  2423. atomic_set(&prtd->start, 0);
  2424. }
  2425. break;
  2426. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  2427. pr_debug("SNDRV_PCM_TRIGGER_PAUSE_RELEASE transition %d\n",
  2428. prtd->gapless_state.gapless_transition);
  2429. if (!prtd->gapless_state.gapless_transition) {
  2430. atomic_set(&prtd->start, 1);
  2431. q6asm_run_nowait(prtd->audio_client, prtd->run_mode,
  2432. 0, 0);
  2433. }
  2434. break;
  2435. case SND_COMPR_TRIGGER_PARTIAL_DRAIN:
  2436. pr_debug("%s: SND_COMPR_TRIGGER_PARTIAL_DRAIN\n", __func__);
  2437. #if !IS_ENABLED(CONFIG_AUDIO_QGKI)
  2438. spin_lock_irqsave(&prtd->lock, flags);
  2439. atomic_set(&prtd->partial_drain, 1);
  2440. #endif
  2441. if (!prtd->gapless_state.use_dsp_gapless_mode) {
  2442. pr_debug("%s: set partial drain as drain\n", __func__);
  2443. cmd = SND_COMPR_TRIGGER_DRAIN;
  2444. #if !IS_ENABLED(CONFIG_AUDIO_QGKI)
  2445. atomic_set(&prtd->partial_drain, 0);
  2446. #endif
  2447. }
  2448. #if !IS_ENABLED(CONFIG_AUDIO_QGKI)
  2449. spin_unlock_irqrestore(&prtd->lock, flags);
  2450. #endif
  2451. case SND_COMPR_TRIGGER_DRAIN:
  2452. pr_debug("%s: SNDRV_COMPRESS_DRAIN\n", __func__);
  2453. /* Make sure all the data is sent to DSP before sending EOS */
  2454. spin_lock_irqsave(&prtd->lock, flags);
  2455. if (!atomic_read(&prtd->start)) {
  2456. pr_err("%s: stream is not in started state\n",
  2457. __func__);
  2458. rc = -EPERM;
  2459. #if !IS_ENABLED(CONFIG_AUDIO_QGKI)
  2460. atomic_set(&prtd->partial_drain, 0);
  2461. #endif
  2462. spin_unlock_irqrestore(&prtd->lock, flags);
  2463. break;
  2464. }
  2465. #if IS_ENABLED(CONFIG_AUDIO_QGKI)
  2466. if (prtd->bytes_received > prtd->copied_total) {
  2467. pr_debug("%s: wait till all the data is sent to dsp\n",
  2468. __func__);
  2469. rc = msm_compr_drain_buffer(prtd, &flags);
  2470. if (rc || !atomic_read(&prtd->start)) {
  2471. if (rc != -ENETRESET)
  2472. rc = -EINTR;
  2473. spin_unlock_irqrestore(&prtd->lock, flags);
  2474. break;
  2475. }
  2476. /*
  2477. * FIXME: Bug.
  2478. * Write(32767)
  2479. * Start
  2480. * Drain <- Indefinite wait
  2481. * sol1 : if (prtd->copied_total) then wait?
  2482. * sol2 : (prtd->cmd_interrupt || prtd->drain_ready ||
  2483. * atomic_read(xrun)
  2484. */
  2485. bytes_to_write = prtd->bytes_received
  2486. - prtd->copied_total;
  2487. WARN(bytes_to_write > runtime->fragment_size,
  2488. "last write %d cannot be > than fragment_size",
  2489. bytes_to_write);
  2490. if (bytes_to_write > 0) {
  2491. pr_debug("%s: send %d partial bytes at the end",
  2492. __func__, bytes_to_write);
  2493. atomic_set(&prtd->xrun, 0);
  2494. prtd->last_buffer = 1;
  2495. msm_compr_send_buffer(prtd);
  2496. }
  2497. }
  2498. if ((cmd == SND_COMPR_TRIGGER_PARTIAL_DRAIN) &&
  2499. (prtd->gapless_state.set_next_stream_id)) {
  2500. /* wait for the last buffer to be returned */
  2501. if (prtd->last_buffer) {
  2502. pr_debug("%s: last buffer drain\n", __func__);
  2503. rc = msm_compr_drain_buffer(prtd, &flags);
  2504. if (rc || !atomic_read(&prtd->start)) {
  2505. spin_unlock_irqrestore(&prtd->lock,
  2506. flags);
  2507. break;
  2508. }
  2509. }
  2510. /* send EOS */
  2511. prtd->eos_ack = 0;
  2512. atomic_set(&prtd->eos, 1);
  2513. pr_debug("issue CMD_EOS stream_id %d\n", ac->stream_id);
  2514. q6asm_stream_cmd_nowait(ac, CMD_EOS, ac->stream_id);
  2515. pr_info("PARTIAL DRAIN, do not wait for EOS ack\n");
  2516. /* send a zero length buffer */
  2517. atomic_set(&prtd->xrun, 0);
  2518. msm_compr_send_buffer(prtd);
  2519. /* wait for the zero length buffer to be returned */
  2520. pr_debug("%s: zero length buffer drain\n", __func__);
  2521. rc = msm_compr_drain_buffer(prtd, &flags);
  2522. if (rc || !atomic_read(&prtd->start)) {
  2523. spin_unlock_irqrestore(&prtd->lock, flags);
  2524. break;
  2525. }
  2526. /* sleep for additional duration partial drain */
  2527. atomic_set(&prtd->drain, 1);
  2528. prtd->drain_ready = 0;
  2529. pr_debug("%s, additional sleep: %d\n", __func__,
  2530. prtd->partial_drain_delay);
  2531. spin_unlock_irqrestore(&prtd->lock, flags);
  2532. rc = wait_event_timeout(prtd->drain_wait,
  2533. prtd->drain_ready || prtd->cmd_interrupt,
  2534. msecs_to_jiffies(prtd->partial_drain_delay));
  2535. pr_debug("%s: out of additional wait for low sample rate\n",
  2536. __func__);
  2537. spin_lock_irqsave(&prtd->lock, flags);
  2538. if (prtd->cmd_interrupt) {
  2539. pr_debug("%s: additional wait interrupted by flush)\n",
  2540. __func__);
  2541. rc = -EINTR;
  2542. prtd->cmd_interrupt = 0;
  2543. spin_unlock_irqrestore(&prtd->lock, flags);
  2544. break;
  2545. }
  2546. /* move to next stream and reset vars */
  2547. pr_debug("%s: Moving to next stream in gapless\n",
  2548. __func__);
  2549. ac->stream_id = NEXT_STREAM_ID(ac->stream_id);
  2550. prtd->byte_offset = 0;
  2551. prtd->app_pointer = 0;
  2552. prtd->first_buffer = 1;
  2553. prtd->last_buffer = 0;
  2554. /*
  2555. * Set gapless transition flag only if EOS hasn't been
  2556. * acknowledged already.
  2557. */
  2558. if (atomic_read(&prtd->eos))
  2559. prtd->gapless_state.gapless_transition = 1;
  2560. prtd->marker_timestamp = 0;
  2561. /*
  2562. * Don't reset these as these vars map to
  2563. * total_bytes_transferred and total_bytes_available
  2564. * directly, only total_bytes_transferred will be
  2565. * updated in the next avail() ioctl
  2566. * prtd->copied_total = 0;
  2567. * prtd->bytes_received = 0;
  2568. */
  2569. atomic_set(&prtd->drain, 0);
  2570. atomic_set(&prtd->xrun, 1);
  2571. pr_debug("%s: issue CMD_RUN", __func__);
  2572. q6asm_run_nowait(prtd->audio_client, 0, 0, 0);
  2573. spin_unlock_irqrestore(&prtd->lock, flags);
  2574. break;
  2575. }
  2576. /*
  2577. * moving to next stream failed, so reset the gapless state
  2578. * set next stream id for the same session so that the same
  2579. * stream can be used for gapless playback
  2580. */
  2581. prtd->gapless_state.set_next_stream_id = false;
  2582. prtd->gapless_state.gapless_transition = 0;
  2583. pr_debug("%s:CMD_EOS stream_id %d\n", __func__, ac->stream_id);
  2584. prtd->eos_ack = 0;
  2585. atomic_set(&prtd->eos, 1);
  2586. q6asm_stream_cmd_nowait(ac, CMD_EOS, ac->stream_id);
  2587. spin_unlock_irqrestore(&prtd->lock, flags);
  2588. /* Wait indefinitely for DRAIN. Flush can also signal this*/
  2589. rc = wait_event_interruptible(prtd->eos_wait,
  2590. (prtd->eos_ack ||
  2591. prtd->cmd_interrupt ||
  2592. atomic_read(&prtd->error)));
  2593. if (rc < 0)
  2594. pr_err("%s: EOS wait failed\n", __func__);
  2595. pr_debug("%s: SNDRV_COMPRESS_DRAIN out of wait for EOS\n",
  2596. __func__);
  2597. if (prtd->cmd_interrupt)
  2598. rc = -EINTR;
  2599. if (atomic_read(&prtd->error)) {
  2600. pr_err("%s: Got RESET EVENTS notification, return\n",
  2601. __func__);
  2602. rc = -ENETRESET;
  2603. }
  2604. /*FIXME : what if a flush comes while PC is here */
  2605. if (rc == 0) {
  2606. /*
  2607. * Failed to open second stream in DSP for gapless
  2608. * so prepare the current stream in session
  2609. * for gapless playback
  2610. */
  2611. spin_lock_irqsave(&prtd->lock, flags);
  2612. pr_debug("%s:issue CMD_PAUSE stream_id %d",
  2613. __func__, ac->stream_id);
  2614. q6asm_stream_cmd_nowait(ac, CMD_PAUSE, ac->stream_id);
  2615. prtd->cmd_ack = 0;
  2616. spin_unlock_irqrestore(&prtd->lock, flags);
  2617. /*
  2618. * Cache this time as last known time
  2619. */
  2620. if (pdata->use_legacy_api)
  2621. q6asm_get_session_time_legacy(
  2622. prtd->audio_client,
  2623. &prtd->marker_timestamp);
  2624. else
  2625. q6asm_get_session_time(prtd->audio_client,
  2626. &prtd->marker_timestamp);
  2627. spin_lock_irqsave(&prtd->lock, flags);
  2628. /*
  2629. * Don't reset these as these vars map to
  2630. * total_bytes_transferred and total_bytes_available.
  2631. * Just total_bytes_transferred will be updated
  2632. * in the next avail() ioctl.
  2633. * prtd->copied_total = 0;
  2634. * prtd->bytes_received = 0;
  2635. * do not reset prtd->bytes_sent as well as the same
  2636. * session is used for gapless playback
  2637. */
  2638. prtd->byte_offset = 0;
  2639. prtd->app_pointer = 0;
  2640. prtd->first_buffer = 1;
  2641. prtd->last_buffer = 0;
  2642. atomic_set(&prtd->drain, 0);
  2643. atomic_set(&prtd->xrun, 1);
  2644. spin_unlock_irqrestore(&prtd->lock, flags);
  2645. pr_debug("%s:issue CMD_FLUSH ac->stream_id %d",
  2646. __func__, ac->stream_id);
  2647. q6asm_stream_cmd(ac, CMD_FLUSH, ac->stream_id);
  2648. q6asm_run_nowait(prtd->audio_client, 0, 0, 0);
  2649. }
  2650. #else
  2651. atomic_set(&prtd->drain, 1);
  2652. spin_unlock_irqrestore(&prtd->lock, flags);
  2653. #endif
  2654. prtd->cmd_interrupt = 0;
  2655. break;
  2656. case SND_COMPR_TRIGGER_NEXT_TRACK:
  2657. if (!prtd->gapless_state.use_dsp_gapless_mode) {
  2658. pr_debug("%s: ignore trigger next track\n", __func__);
  2659. rc = 0;
  2660. break;
  2661. }
  2662. pr_debug("%s: SND_COMPR_TRIGGER_NEXT_TRACK\n", __func__);
  2663. spin_lock_irqsave(&prtd->lock, flags);
  2664. rc = 0;
  2665. /* next stream in gapless */
  2666. stream_id = NEXT_STREAM_ID(ac->stream_id);
  2667. /*
  2668. * Wait if stream 1 has not completed before honoring next
  2669. * track for stream 3. Scenario happens if second clip is
  2670. * small and fills in one buffer so next track will be
  2671. * called immediately.
  2672. */
  2673. stream_index = STREAM_ARRAY_INDEX(stream_id);
  2674. if (stream_index >= MAX_NUMBER_OF_STREAMS ||
  2675. stream_index < 0) {
  2676. pr_err("%s: Invalid stream index: %d", __func__,
  2677. stream_index);
  2678. spin_unlock_irqrestore(&prtd->lock, flags);
  2679. rc = -EINVAL;
  2680. break;
  2681. }
  2682. if (prtd->gapless_state.stream_opened[stream_index]) {
  2683. if (prtd->gapless_state.gapless_transition) {
  2684. rc = msm_compr_wait_for_stream_avail(prtd,
  2685. &flags);
  2686. } else {
  2687. /*
  2688. * If session is already opened break out if
  2689. * the state is not gapless transition. This
  2690. * is when seek happens after the last buffer
  2691. * is sent to the driver. Next track would be
  2692. * called again after last buffer is sent.
  2693. */
  2694. pr_debug("next session is in opened state\n");
  2695. spin_unlock_irqrestore(&prtd->lock, flags);
  2696. break;
  2697. }
  2698. }
  2699. spin_unlock_irqrestore(&prtd->lock, flags);
  2700. if (rc < 0) {
  2701. /*
  2702. * if return type EINTR then reset to zero. Tiny
  2703. * compress treats EINTR as error and prevents PARTIAL
  2704. * DRAIN. EINTR is not an error. wait for stream avail
  2705. * is interrupted by some other command like FLUSH.
  2706. */
  2707. if (rc == -EINTR) {
  2708. pr_debug("%s: EINTR reset rc to 0\n", __func__);
  2709. rc = 0;
  2710. }
  2711. break;
  2712. }
  2713. if (prtd->codec_param.codec.format == SNDRV_PCM_FORMAT_S24_LE)
  2714. bits_per_sample = 24;
  2715. else if (prtd->codec_param.codec.format ==
  2716. SNDRV_PCM_FORMAT_S32_LE)
  2717. bits_per_sample = 32;
  2718. pr_debug("%s: open_write stream_id %d bits_per_sample %d",
  2719. __func__, stream_id, bits_per_sample);
  2720. if (q6core_get_avcs_api_version_per_service(
  2721. APRV2_IDS_SERVICE_ID_ADSP_ASM_V) >=
  2722. ADSP_ASM_API_VERSION_V2)
  2723. rc = q6asm_stream_open_write_v5(prtd->audio_client,
  2724. prtd->codec, bits_per_sample,
  2725. stream_id,
  2726. prtd->gapless_state.use_dsp_gapless_mode);
  2727. else
  2728. rc = q6asm_stream_open_write_v4(prtd->audio_client,
  2729. prtd->codec, bits_per_sample,
  2730. stream_id,
  2731. prtd->gapless_state.use_dsp_gapless_mode);
  2732. if (rc < 0) {
  2733. pr_err("%s: Session out open failed for gapless [%d]\n",
  2734. __func__, rc);
  2735. break;
  2736. }
  2737. spin_lock_irqsave(&prtd->lock, flags);
  2738. prtd->gapless_state.stream_opened[stream_index] = 1;
  2739. prtd->gapless_state.set_next_stream_id = true;
  2740. spin_unlock_irqrestore(&prtd->lock, flags);
  2741. rc = msm_compr_send_media_format_block(cstream,
  2742. stream_id, false);
  2743. if (rc < 0) {
  2744. pr_err("%s, failed to send media format block\n",
  2745. __func__);
  2746. break;
  2747. }
  2748. msm_compr_send_dec_params(cstream, pdata->dec_params[fe_id],
  2749. stream_id);
  2750. break;
  2751. }
  2752. return rc;
  2753. }
  2754. static int msm_compr_pointer(struct snd_compr_stream *cstream,
  2755. struct snd_compr_tstamp *arg)
  2756. {
  2757. struct snd_compr_runtime *runtime = cstream->runtime;
  2758. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  2759. struct msm_compr_audio *prtd = runtime->private_data;
  2760. struct snd_soc_component *component = NULL;
  2761. struct msm_compr_pdata *pdata = NULL;
  2762. struct snd_compr_tstamp tstamp;
  2763. uint64_t timestamp = 0;
  2764. int rc = 0, first_buffer;
  2765. unsigned long flags;
  2766. uint32_t gapless_transition;
  2767. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  2768. if (!component) {
  2769. pr_err("%s: component is NULL\n", __func__);
  2770. return -EINVAL;
  2771. }
  2772. pdata = snd_soc_component_get_drvdata(component);
  2773. if (!pdata) {
  2774. pr_err("%s: pdata is NULL\n", __func__);
  2775. return -EINVAL;
  2776. }
  2777. pr_debug("%s\n", __func__);
  2778. memset(&tstamp, 0x0, sizeof(struct snd_compr_tstamp));
  2779. spin_lock_irqsave(&prtd->lock, flags);
  2780. tstamp.sampling_rate = prtd->sample_rate;
  2781. tstamp.byte_offset = prtd->byte_offset;
  2782. if (cstream->direction == SND_COMPRESS_PLAYBACK)
  2783. tstamp.copied_total = prtd->copied_total;
  2784. else if (cstream->direction == SND_COMPRESS_CAPTURE)
  2785. tstamp.copied_total = prtd->received_total;
  2786. first_buffer = prtd->first_buffer;
  2787. if (atomic_read(&prtd->error)) {
  2788. pr_err_ratelimited("%s Got RESET EVENTS notification, return error\n",
  2789. __func__);
  2790. if (cstream->direction == SND_COMPRESS_PLAYBACK)
  2791. runtime->total_bytes_transferred = prtd->copied_total;
  2792. else
  2793. runtime->total_bytes_available = prtd->received_total;
  2794. tstamp.pcm_io_frames = 0;
  2795. memcpy(arg, &tstamp, sizeof(struct snd_compr_tstamp));
  2796. spin_unlock_irqrestore(&prtd->lock, flags);
  2797. return -ENETRESET;
  2798. }
  2799. if (cstream->direction == SND_COMPRESS_PLAYBACK) {
  2800. gapless_transition = prtd->gapless_state.gapless_transition;
  2801. spin_unlock_irqrestore(&prtd->lock, flags);
  2802. if (gapless_transition)
  2803. pr_debug("%s session time in gapless transition",
  2804. __func__);
  2805. /*
  2806. *- Do not query if no buffer has been given.
  2807. *- Do not query on a gapless transition.
  2808. * Playback for the 2nd stream can start (thus returning time
  2809. * starting from 0) before the driver knows about EOS of first
  2810. * stream.
  2811. */
  2812. if (!first_buffer || gapless_transition) {
  2813. if (pdata->use_legacy_api)
  2814. rc = q6asm_get_session_time_legacy(
  2815. prtd->audio_client, &prtd->marker_timestamp);
  2816. else
  2817. rc = q6asm_get_session_time(
  2818. prtd->audio_client, &prtd->marker_timestamp);
  2819. if (rc < 0) {
  2820. if (atomic_read(&prtd->error))
  2821. return -ENETRESET;
  2822. else
  2823. return rc;
  2824. }
  2825. }
  2826. } else {
  2827. spin_unlock_irqrestore(&prtd->lock, flags);
  2828. }
  2829. timestamp = prtd->marker_timestamp;
  2830. /* DSP returns timestamp in usec */
  2831. pr_debug("%s: timestamp = %lld usec\n", __func__, timestamp);
  2832. timestamp *= prtd->sample_rate;
  2833. tstamp.pcm_io_frames = (snd_pcm_uframes_t)div64_u64(timestamp, 1000000);
  2834. memcpy(arg, &tstamp, sizeof(struct snd_compr_tstamp));
  2835. return 0;
  2836. }
  2837. static int msm_compr_ack(struct snd_compr_stream *cstream,
  2838. size_t count)
  2839. {
  2840. struct snd_compr_runtime *runtime = cstream->runtime;
  2841. struct msm_compr_audio *prtd = runtime->private_data;
  2842. void *src, *dstn;
  2843. size_t copy;
  2844. unsigned long flags;
  2845. WARN(1, "This path is untested");
  2846. return -EINVAL;
  2847. pr_debug("%s: count = %zd\n", __func__, count);
  2848. if (!prtd->buffer) {
  2849. pr_err("%s: Buffer is not allocated yet ??\n", __func__);
  2850. return -EINVAL;
  2851. }
  2852. src = runtime->buffer + prtd->app_pointer;
  2853. dstn = prtd->buffer + prtd->app_pointer;
  2854. if (count < prtd->buffer_size - prtd->app_pointer) {
  2855. memcpy(dstn, src, count);
  2856. prtd->app_pointer += count;
  2857. } else {
  2858. copy = prtd->buffer_size - prtd->app_pointer;
  2859. memcpy(dstn, src, copy);
  2860. memcpy(prtd->buffer, runtime->buffer, count - copy);
  2861. prtd->app_pointer = count - copy;
  2862. }
  2863. /*
  2864. * If the stream is started and all the bytes received were
  2865. * copied to DSP, the newly received bytes should be
  2866. * sent right away
  2867. */
  2868. spin_lock_irqsave(&prtd->lock, flags);
  2869. if (atomic_read(&prtd->start) &&
  2870. prtd->bytes_received == prtd->copied_total) {
  2871. prtd->bytes_received += count;
  2872. msm_compr_send_buffer(prtd);
  2873. } else
  2874. prtd->bytes_received += count;
  2875. spin_unlock_irqrestore(&prtd->lock, flags);
  2876. return 0;
  2877. }
  2878. static int msm_compr_playback_copy(struct snd_compr_stream *cstream,
  2879. char __user *buf, size_t count)
  2880. {
  2881. struct snd_compr_runtime *runtime = cstream->runtime;
  2882. struct msm_compr_audio *prtd = runtime->private_data;
  2883. void *dstn;
  2884. size_t copy;
  2885. uint64_t bytes_available = 0;
  2886. unsigned long flags;
  2887. pr_debug("%s: count = %zd\n", __func__, count);
  2888. if (!prtd->buffer) {
  2889. pr_err("%s: Buffer is not allocated yet ??", __func__);
  2890. return 0;
  2891. }
  2892. spin_lock_irqsave(&prtd->lock, flags);
  2893. if (atomic_read(&prtd->error)) {
  2894. pr_err("%s Got RESET EVENTS notification", __func__);
  2895. spin_unlock_irqrestore(&prtd->lock, flags);
  2896. return -ENETRESET;
  2897. }
  2898. spin_unlock_irqrestore(&prtd->lock, flags);
  2899. dstn = prtd->buffer + prtd->app_pointer;
  2900. if (count < prtd->buffer_size - prtd->app_pointer) {
  2901. if (copy_from_user(dstn, buf, count))
  2902. return -EFAULT;
  2903. prtd->app_pointer += count;
  2904. } else {
  2905. copy = prtd->buffer_size - prtd->app_pointer;
  2906. if (copy_from_user(dstn, buf, copy))
  2907. return -EFAULT;
  2908. if (copy_from_user(prtd->buffer, buf + copy, count - copy))
  2909. return -EFAULT;
  2910. prtd->app_pointer = count - copy;
  2911. }
  2912. /*
  2913. * If stream is started and there has been an xrun,
  2914. * since the available bytes fits fragment_size, copy the data
  2915. * right away.
  2916. */
  2917. spin_lock_irqsave(&prtd->lock, flags);
  2918. prtd->bytes_received += count;
  2919. if (atomic_read(&prtd->start)) {
  2920. if (atomic_read(&prtd->xrun)) {
  2921. pr_debug("%s: in xrun, count = %zd\n", __func__, count);
  2922. bytes_available = prtd->bytes_received -
  2923. prtd->copied_total;
  2924. if (bytes_available >= runtime->fragment_size) {
  2925. pr_debug("%s: handle xrun, bytes_to_write = %llu\n",
  2926. __func__, bytes_available);
  2927. atomic_set(&prtd->xrun, 0);
  2928. msm_compr_send_buffer(prtd);
  2929. } /* else not sufficient data */
  2930. } /* writes will continue on the next write_done */
  2931. }
  2932. spin_unlock_irqrestore(&prtd->lock, flags);
  2933. return count;
  2934. }
  2935. static int msm_compr_capture_copy(struct snd_compr_stream *cstream,
  2936. char __user *buf, size_t count)
  2937. {
  2938. struct snd_compr_runtime *runtime = cstream->runtime;
  2939. struct msm_compr_audio *prtd = runtime->private_data;
  2940. void *source;
  2941. unsigned long flags;
  2942. pr_debug("%s: count = %zd\n", __func__, count);
  2943. if (!prtd->buffer) {
  2944. pr_err("%s: Buffer is not allocated yet ??", __func__);
  2945. return 0;
  2946. }
  2947. spin_lock_irqsave(&prtd->lock, flags);
  2948. if (atomic_read(&prtd->error)) {
  2949. pr_err("%s Got RESET EVENTS notification", __func__);
  2950. spin_unlock_irqrestore(&prtd->lock, flags);
  2951. return -ENETRESET;
  2952. }
  2953. source = prtd->buffer + prtd->app_pointer;
  2954. /* check if we have requested amount of data to copy to user*/
  2955. if (count <= prtd->received_total - prtd->bytes_copied) {
  2956. spin_unlock_irqrestore(&prtd->lock, flags);
  2957. if (copy_to_user(buf, source, count)) {
  2958. pr_err("copy_to_user failed");
  2959. return -EFAULT;
  2960. }
  2961. spin_lock_irqsave(&prtd->lock, flags);
  2962. prtd->app_pointer += count;
  2963. if (prtd->app_pointer >= prtd->buffer_size)
  2964. prtd->app_pointer -= prtd->buffer_size;
  2965. prtd->bytes_copied += count;
  2966. }
  2967. msm_compr_read_buffer(prtd);
  2968. spin_unlock_irqrestore(&prtd->lock, flags);
  2969. return count;
  2970. }
  2971. static int msm_compr_copy(struct snd_compr_stream *cstream,
  2972. char __user *buf, size_t count)
  2973. {
  2974. int ret = 0;
  2975. pr_debug(" In %s\n", __func__);
  2976. if (cstream->direction == SND_COMPRESS_PLAYBACK)
  2977. ret = msm_compr_playback_copy(cstream, buf, count);
  2978. else if (cstream->direction == SND_COMPRESS_CAPTURE)
  2979. ret = msm_compr_capture_copy(cstream, buf, count);
  2980. return ret;
  2981. }
  2982. static int msm_compr_get_caps(struct snd_compr_stream *cstream,
  2983. struct snd_compr_caps *arg)
  2984. {
  2985. struct snd_compr_runtime *runtime = cstream->runtime;
  2986. struct msm_compr_audio *prtd = runtime->private_data;
  2987. int ret = 0;
  2988. pr_debug("%s\n", __func__);
  2989. if ((arg != NULL) && (prtd != NULL)) {
  2990. memcpy(arg, &prtd->compr_cap, sizeof(struct snd_compr_caps));
  2991. } else {
  2992. ret = -EINVAL;
  2993. pr_err("%s: arg (0x%pK), prtd (0x%pK)\n", __func__, arg, prtd);
  2994. }
  2995. return ret;
  2996. }
  2997. static int msm_compr_get_codec_caps(struct snd_compr_stream *cstream,
  2998. struct snd_compr_codec_caps *codec)
  2999. {
  3000. pr_debug("%s\n", __func__);
  3001. switch (codec->codec) {
  3002. case SND_AUDIOCODEC_MP3:
  3003. codec->num_descriptors = 2;
  3004. codec->descriptor[0].max_ch = 2;
  3005. memcpy(codec->descriptor[0].sample_rates,
  3006. supported_sample_rates,
  3007. sizeof(supported_sample_rates));
  3008. codec->descriptor[0].num_sample_rates =
  3009. sizeof(supported_sample_rates)/sizeof(unsigned int);
  3010. codec->descriptor[0].bit_rate[0] = 320; /* 320kbps */
  3011. codec->descriptor[0].bit_rate[1] = 128;
  3012. codec->descriptor[0].num_bitrates = 2;
  3013. codec->descriptor[0].profiles = 0;
  3014. codec->descriptor[0].modes = SND_AUDIOCHANMODE_MP3_STEREO;
  3015. codec->descriptor[0].formats = 0;
  3016. break;
  3017. case SND_AUDIOCODEC_AAC:
  3018. codec->num_descriptors = 2;
  3019. codec->descriptor[1].max_ch = 2;
  3020. memcpy(codec->descriptor[1].sample_rates,
  3021. supported_sample_rates,
  3022. sizeof(supported_sample_rates));
  3023. codec->descriptor[1].num_sample_rates =
  3024. sizeof(supported_sample_rates)/sizeof(unsigned int);
  3025. codec->descriptor[1].bit_rate[0] = 320; /* 320kbps */
  3026. codec->descriptor[1].bit_rate[1] = 128;
  3027. codec->descriptor[1].num_bitrates = 2;
  3028. codec->descriptor[1].profiles = 0;
  3029. codec->descriptor[1].modes = 0;
  3030. codec->descriptor[1].formats =
  3031. (SND_AUDIOSTREAMFORMAT_MP4ADTS |
  3032. SND_AUDIOSTREAMFORMAT_RAW);
  3033. break;
  3034. case SND_AUDIOCODEC_AC3:
  3035. case SND_AUDIOCODEC_EAC3:
  3036. case SND_AUDIOCODEC_DTS:
  3037. case SND_AUDIOCODEC_TRUEHD:
  3038. case SND_AUDIOCODEC_IEC61937:
  3039. case SND_AUDIOCODEC_BESPOKE:
  3040. break;
  3041. default:
  3042. pr_err("%s: Unsupported audio codec %d\n",
  3043. __func__, codec->codec);
  3044. return -EINVAL;
  3045. }
  3046. return 0;
  3047. }
  3048. static int msm_compr_set_metadata(struct snd_compr_stream *cstream,
  3049. struct snd_compr_metadata *metadata)
  3050. {
  3051. struct msm_compr_audio *prtd;
  3052. struct audio_client *ac;
  3053. pr_debug("%s\n", __func__);
  3054. if (!metadata || !cstream)
  3055. return -EINVAL;
  3056. prtd = cstream->runtime->private_data;
  3057. if (!prtd || !prtd->audio_client) {
  3058. pr_err("%s: prtd or audio client is NULL\n", __func__);
  3059. return -EINVAL;
  3060. }
  3061. if (((metadata->key == SNDRV_COMPRESS_ENCODER_PADDING) ||
  3062. (metadata->key == SNDRV_COMPRESS_ENCODER_DELAY)) &&
  3063. (prtd->compr_passthr != LEGACY_PCM)) {
  3064. pr_debug("%s: No trailing silence for compress_type[%d]\n",
  3065. __func__, prtd->compr_passthr);
  3066. return 0;
  3067. }
  3068. ac = prtd->audio_client;
  3069. if (metadata->key == SNDRV_COMPRESS_ENCODER_PADDING) {
  3070. pr_debug("%s, got encoder padding %u",
  3071. __func__, metadata->value[0]);
  3072. prtd->gapless_state.trailing_samples_drop = metadata->value[0];
  3073. } else if (metadata->key == SNDRV_COMPRESS_ENCODER_DELAY) {
  3074. pr_debug("%s, got encoder delay %u",
  3075. __func__, metadata->value[0]);
  3076. prtd->gapless_state.initial_samples_drop = metadata->value[0];
  3077. } else if (metadata->key == SNDRV_COMPRESS_RENDER_MODE) {
  3078. return msm_compr_set_render_mode(prtd, metadata->value[0]);
  3079. } else if (metadata->key == SNDRV_COMPRESS_CLK_REC_MODE) {
  3080. return msm_compr_set_clk_rec_mode(ac, metadata->value[0]);
  3081. } else if (metadata->key == SNDRV_COMPRESS_RENDER_WINDOW) {
  3082. return msm_compr_set_render_window(
  3083. ac,
  3084. metadata->value[0],
  3085. metadata->value[1],
  3086. metadata->value[2],
  3087. metadata->value[3]);
  3088. } else if (metadata->key == SNDRV_COMPRESS_START_DELAY) {
  3089. prtd->start_delay_lsw = metadata->value[0];
  3090. prtd->start_delay_msw = metadata->value[1];
  3091. } else if (metadata->key ==
  3092. SNDRV_COMPRESS_ENABLE_ADJUST_SESSION_CLOCK) {
  3093. return msm_compr_enable_adjust_session_clock(ac,
  3094. metadata->value[0]);
  3095. } else if (metadata->key == SNDRV_COMPRESS_ADJUST_SESSION_CLOCK) {
  3096. return msm_compr_adjust_session_clock(ac,
  3097. metadata->value[0],
  3098. metadata->value[1]);
  3099. }
  3100. return 0;
  3101. }
  3102. static int msm_compr_get_metadata(struct snd_compr_stream *cstream,
  3103. struct snd_compr_metadata *metadata)
  3104. {
  3105. struct msm_compr_audio *prtd;
  3106. struct audio_client *ac;
  3107. int ret = -EINVAL;
  3108. uint64_t ses_time = 0, frames = 0, abs_time = 0;
  3109. uint64_t *val = NULL;
  3110. int64_t av_offset = 0;
  3111. int32_t clock_id = -EINVAL;
  3112. pr_debug("%s\n", __func__);
  3113. if (!metadata || !cstream || !cstream->runtime)
  3114. return ret;
  3115. if (metadata->key != SNDRV_COMPRESS_PATH_DELAY &&
  3116. metadata->key != SNDRV_COMPRESS_DSP_POSITION) {
  3117. pr_err("%s, unsupported key %d\n", __func__, metadata->key);
  3118. return ret;
  3119. }
  3120. prtd = cstream->runtime->private_data;
  3121. if (!prtd || !prtd->audio_client) {
  3122. pr_err("%s: prtd or audio client is NULL\n", __func__);
  3123. return ret;
  3124. }
  3125. switch (metadata->key) {
  3126. case SNDRV_COMPRESS_PATH_DELAY:
  3127. ac = prtd->audio_client;
  3128. ret = q6asm_get_path_delay(prtd->audio_client);
  3129. if (ret) {
  3130. pr_err("%s: get_path_delay failed, ret=%d\n",
  3131. __func__, ret);
  3132. return ret;
  3133. }
  3134. pr_debug("%s, path delay(in us) %u\n", __func__,
  3135. ac->path_delay);
  3136. metadata->value[0] = ac->path_delay;
  3137. break;
  3138. case SNDRV_COMPRESS_DSP_POSITION:
  3139. clock_id = metadata->value[0];
  3140. pr_debug("%s, clock_id %d\n", __func__, clock_id);
  3141. ret = q6asm_get_session_time_v2(prtd->audio_client,
  3142. &ses_time, &abs_time);
  3143. if (ret) {
  3144. pr_err("%s: q6asm_get_session_time_v2 failed, ret=%d\n",
  3145. __func__, ret);
  3146. return ret;
  3147. }
  3148. frames = div64_u64((ses_time * prtd->sample_rate), 1000000);
  3149. ret = avcs_core_query_timer_offset(&av_offset, clock_id);
  3150. if (ret) {
  3151. pr_err("%s: avcs query failed, ret=%d\n",
  3152. __func__, ret);
  3153. return ret;
  3154. }
  3155. val = (uint64_t *) &metadata->value[1];
  3156. val[0] = frames;
  3157. val[1] = abs_time + av_offset;
  3158. pr_debug("%s, vals frames %lld, time %lld, avoff %lld, abst %lld, sess_time %llu sr %d\n",
  3159. __func__, val[0], val[1], av_offset, abs_time,
  3160. ses_time, prtd->sample_rate);
  3161. break;
  3162. default:
  3163. pr_err("%s, unsupported key %d\n", __func__, metadata->key);
  3164. break;
  3165. }
  3166. return ret;
  3167. }
  3168. #if IS_ENABLED(CONFIG_AUDIO_QGKI)
  3169. static int msm_compr_set_next_track_param(struct snd_compr_stream *cstream,
  3170. union snd_codec_options *codec_options)
  3171. {
  3172. struct msm_compr_audio *prtd;
  3173. struct audio_client *ac;
  3174. int ret = 0;
  3175. if (!codec_options || !cstream)
  3176. return -EINVAL;
  3177. prtd = cstream->runtime->private_data;
  3178. if (!prtd || !prtd->audio_client) {
  3179. pr_err("%s: prtd or audio client is NULL\n", __func__);
  3180. return -EINVAL;
  3181. }
  3182. ac = prtd->audio_client;
  3183. pr_debug("%s: got codec options for codec type %u",
  3184. __func__, prtd->codec);
  3185. switch (prtd->codec) {
  3186. case FORMAT_WMA_V9:
  3187. case FORMAT_WMA_V10PRO:
  3188. case FORMAT_FLAC:
  3189. case FORMAT_VORBIS:
  3190. case FORMAT_ALAC:
  3191. case FORMAT_APE:
  3192. case FORMAT_AMRNB:
  3193. case FORMAT_AMRWB:
  3194. case FORMAT_AMR_WB_PLUS:
  3195. memcpy(&(prtd->gapless_state.codec_options),
  3196. codec_options,
  3197. sizeof(union snd_codec_options));
  3198. ret = msm_compr_send_media_format_block(cstream,
  3199. ac->stream_id, true);
  3200. if (ret < 0) {
  3201. pr_err("%s: failed to send media format block\n",
  3202. __func__);
  3203. }
  3204. break;
  3205. default:
  3206. pr_debug("%s: Ignore sending CMD Format block\n",
  3207. __func__);
  3208. break;
  3209. }
  3210. return ret;
  3211. }
  3212. #endif /* CONFIG_AUDIO_QGKI */
  3213. static int msm_compr_volume_put(struct snd_kcontrol *kcontrol,
  3214. struct snd_ctl_elem_value *ucontrol)
  3215. {
  3216. struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
  3217. unsigned long fe_id = kcontrol->private_value;
  3218. struct msm_compr_pdata *pdata = (struct msm_compr_pdata *)
  3219. snd_soc_component_get_drvdata(comp);
  3220. struct snd_compr_stream *cstream = NULL;
  3221. uint32_t *volume = NULL;
  3222. if (fe_id >= MSM_FRONTEND_DAI_MAX) {
  3223. pr_err("%s Received out of bounds fe_id %lu\n",
  3224. __func__, fe_id);
  3225. return -EINVAL;
  3226. }
  3227. cstream = pdata->cstream[fe_id];
  3228. volume = pdata->volume[fe_id];
  3229. volume[0] = ucontrol->value.integer.value[0];
  3230. volume[1] = ucontrol->value.integer.value[1];
  3231. pr_debug("%s: fe_id %lu left_vol %d right_vol %d\n",
  3232. __func__, fe_id, volume[0], volume[1]);
  3233. if (cstream)
  3234. msm_compr_set_volume(cstream, volume[0], volume[1]);
  3235. return 0;
  3236. }
  3237. static int msm_compr_volume_get(struct snd_kcontrol *kcontrol,
  3238. struct snd_ctl_elem_value *ucontrol)
  3239. {
  3240. struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
  3241. unsigned long fe_id = kcontrol->private_value;
  3242. struct msm_compr_pdata *pdata =
  3243. snd_soc_component_get_drvdata(comp);
  3244. uint32_t *volume = NULL;
  3245. if (fe_id >= MSM_FRONTEND_DAI_MAX) {
  3246. pr_err("%s Received out of bound fe_id %lu\n", __func__, fe_id);
  3247. return -EINVAL;
  3248. }
  3249. volume = pdata->volume[fe_id];
  3250. pr_debug("%s: fe_id %lu\n", __func__, fe_id);
  3251. ucontrol->value.integer.value[0] = volume[0];
  3252. ucontrol->value.integer.value[1] = volume[1];
  3253. return 0;
  3254. }
  3255. static int msm_compr_audio_effects_config_put(struct snd_kcontrol *kcontrol,
  3256. struct snd_ctl_elem_value *ucontrol)
  3257. {
  3258. struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
  3259. unsigned long fe_id = kcontrol->private_value;
  3260. struct msm_compr_pdata *pdata = (struct msm_compr_pdata *)
  3261. snd_soc_component_get_drvdata(comp);
  3262. struct msm_compr_audio_effects *audio_effects = NULL;
  3263. struct snd_compr_stream *cstream = NULL;
  3264. struct msm_compr_audio *prtd = NULL;
  3265. long *values = &(ucontrol->value.integer.value[0]);
  3266. int ret = 0;
  3267. int effects_module;
  3268. pr_debug("%s\n", __func__);
  3269. if (fe_id >= MSM_FRONTEND_DAI_MAX) {
  3270. pr_err("%s Received out of bounds fe_id %lu\n",
  3271. __func__, fe_id);
  3272. return -EINVAL;
  3273. }
  3274. mutex_lock(&pdata->lock);
  3275. cstream = pdata->cstream[fe_id];
  3276. audio_effects = pdata->audio_effects[fe_id];
  3277. if (!cstream || !audio_effects) {
  3278. pr_err("%s: stream or effects inactive\n", __func__);
  3279. ret = -EINVAL;
  3280. goto done;
  3281. }
  3282. prtd = cstream->runtime->private_data;
  3283. if (!prtd) {
  3284. pr_err("%s: cannot set audio effects\n", __func__);
  3285. ret = -EINVAL;
  3286. goto done;
  3287. }
  3288. if (prtd->compr_passthr != LEGACY_PCM) {
  3289. pr_debug("%s: No effects for compr_type[%d]\n",
  3290. __func__, prtd->compr_passthr);
  3291. goto done;
  3292. }
  3293. pr_debug("%s: Effects supported for compr_type[%d]\n",
  3294. __func__, prtd->compr_passthr);
  3295. effects_module = *values++;
  3296. switch (effects_module) {
  3297. case VIRTUALIZER_MODULE:
  3298. pr_debug("%s: VIRTUALIZER_MODULE\n", __func__);
  3299. if (msm_audio_effects_is_effmodule_supp_in_top(effects_module,
  3300. prtd->audio_client->topology))
  3301. msm_audio_effects_virtualizer_handler(
  3302. prtd->audio_client,
  3303. &(audio_effects->virtualizer),
  3304. values);
  3305. break;
  3306. case REVERB_MODULE:
  3307. pr_debug("%s: REVERB_MODULE\n", __func__);
  3308. if (msm_audio_effects_is_effmodule_supp_in_top(effects_module,
  3309. prtd->audio_client->topology))
  3310. msm_audio_effects_reverb_handler(prtd->audio_client,
  3311. &(audio_effects->reverb),
  3312. values);
  3313. break;
  3314. case BASS_BOOST_MODULE:
  3315. pr_debug("%s: BASS_BOOST_MODULE\n", __func__);
  3316. if (msm_audio_effects_is_effmodule_supp_in_top(effects_module,
  3317. prtd->audio_client->topology))
  3318. msm_audio_effects_bass_boost_handler(prtd->audio_client,
  3319. &(audio_effects->bass_boost),
  3320. values);
  3321. break;
  3322. case PBE_MODULE:
  3323. pr_debug("%s: PBE_MODULE\n", __func__);
  3324. if (msm_audio_effects_is_effmodule_supp_in_top(effects_module,
  3325. prtd->audio_client->topology))
  3326. msm_audio_effects_pbe_handler(prtd->audio_client,
  3327. &(audio_effects->pbe),
  3328. values);
  3329. break;
  3330. case EQ_MODULE:
  3331. pr_debug("%s: EQ_MODULE\n", __func__);
  3332. if (msm_audio_effects_is_effmodule_supp_in_top(effects_module,
  3333. prtd->audio_client->topology))
  3334. msm_audio_effects_popless_eq_handler(prtd->audio_client,
  3335. &(audio_effects->equalizer),
  3336. values);
  3337. break;
  3338. case SOFT_VOLUME_MODULE:
  3339. pr_debug("%s: SOFT_VOLUME_MODULE\n", __func__);
  3340. break;
  3341. case SOFT_VOLUME2_MODULE:
  3342. pr_debug("%s: SOFT_VOLUME2_MODULE\n", __func__);
  3343. if (msm_audio_effects_is_effmodule_supp_in_top(effects_module,
  3344. prtd->audio_client->topology))
  3345. msm_audio_effects_volume_handler_v2(prtd->audio_client,
  3346. &(audio_effects->volume),
  3347. values, SOFT_VOLUME_INSTANCE_2);
  3348. break;
  3349. default:
  3350. pr_err("%s Invalid effects config module\n", __func__);
  3351. ret = -EINVAL;
  3352. }
  3353. done:
  3354. mutex_unlock(&pdata->lock);
  3355. return ret;
  3356. }
  3357. static int msm_compr_audio_effects_config_get(struct snd_kcontrol *kcontrol,
  3358. struct snd_ctl_elem_value *ucontrol)
  3359. {
  3360. struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
  3361. unsigned long fe_id = kcontrol->private_value;
  3362. struct msm_compr_pdata *pdata = (struct msm_compr_pdata *)
  3363. snd_soc_component_get_drvdata(comp);
  3364. struct msm_compr_audio_effects *audio_effects = NULL;
  3365. struct snd_compr_stream *cstream = NULL;
  3366. int ret = 0;
  3367. struct msm_compr_audio *prtd = NULL;
  3368. pr_debug("%s\n", __func__);
  3369. if (fe_id >= MSM_FRONTEND_DAI_MAX) {
  3370. pr_err("%s Received out of bounds fe_id %lu\n",
  3371. __func__, fe_id);
  3372. return -EINVAL;
  3373. }
  3374. mutex_lock(&pdata->lock);
  3375. cstream = pdata->cstream[fe_id];
  3376. audio_effects = pdata->audio_effects[fe_id];
  3377. if (!cstream || !audio_effects) {
  3378. pr_debug("%s: stream or effects inactive\n", __func__);
  3379. ret = -EINVAL;
  3380. goto done;
  3381. }
  3382. prtd = cstream->runtime->private_data;
  3383. if (!prtd) {
  3384. pr_err("%s: cannot set audio effects\n", __func__);
  3385. ret = -EINVAL;
  3386. }
  3387. done:
  3388. mutex_unlock(&pdata->lock);
  3389. return ret;
  3390. }
  3391. static int msm_compr_query_audio_effect_put(struct snd_kcontrol *kcontrol,
  3392. struct snd_ctl_elem_value *ucontrol)
  3393. {
  3394. struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
  3395. unsigned long fe_id = kcontrol->private_value;
  3396. struct msm_compr_pdata *pdata = (struct msm_compr_pdata *)
  3397. snd_soc_component_get_drvdata(comp);
  3398. struct msm_compr_audio_effects *audio_effects = NULL;
  3399. struct snd_compr_stream *cstream = NULL;
  3400. struct msm_compr_audio *prtd = NULL;
  3401. int ret = 0;
  3402. long *values = &(ucontrol->value.integer.value[0]);
  3403. if (fe_id >= MSM_FRONTEND_DAI_MAX) {
  3404. pr_err("%s Received out of bounds fe_id %lu\n",
  3405. __func__, fe_id);
  3406. return -EINVAL;
  3407. }
  3408. mutex_lock(&pdata->lock);
  3409. cstream = pdata->cstream[fe_id];
  3410. audio_effects = pdata->audio_effects[fe_id];
  3411. if (!cstream || !audio_effects) {
  3412. pr_err("%s: stream or effects inactive\n", __func__);
  3413. ret = -EINVAL;
  3414. goto done;
  3415. }
  3416. prtd = cstream->runtime->private_data;
  3417. if (!prtd) {
  3418. pr_err("%s: cannot set audio effects\n", __func__);
  3419. ret = -EINVAL;
  3420. goto done;
  3421. }
  3422. if (prtd->compr_passthr != LEGACY_PCM) {
  3423. pr_err("%s: No effects for compr_type[%d]\n",
  3424. __func__, prtd->compr_passthr);
  3425. ret = -EPERM;
  3426. goto done;
  3427. }
  3428. audio_effects->query.mod_id = (u32)*values++;
  3429. audio_effects->query.parm_id = (u32)*values++;
  3430. audio_effects->query.size = (u32)*values++;
  3431. audio_effects->query.offset = (u32)*values++;
  3432. audio_effects->query.device = (u32)*values++;
  3433. done:
  3434. mutex_unlock(&pdata->lock);
  3435. return ret;
  3436. }
  3437. static int msm_compr_query_audio_effect_get(struct snd_kcontrol *kcontrol,
  3438. struct snd_ctl_elem_value *ucontrol)
  3439. {
  3440. struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
  3441. unsigned long fe_id = kcontrol->private_value;
  3442. struct msm_compr_pdata *pdata = (struct msm_compr_pdata *)
  3443. snd_soc_component_get_drvdata(comp);
  3444. struct msm_compr_audio_effects *audio_effects = NULL;
  3445. struct snd_compr_stream *cstream = NULL;
  3446. struct msm_compr_audio *prtd = NULL;
  3447. int ret = 0;
  3448. long *values = &(ucontrol->value.integer.value[0]);
  3449. if (fe_id >= MSM_FRONTEND_DAI_MAX) {
  3450. pr_err("%s Received out of bounds fe_id %lu\n",
  3451. __func__, fe_id);
  3452. return -EINVAL;
  3453. }
  3454. mutex_lock(&pdata->lock);
  3455. cstream = pdata->cstream[fe_id];
  3456. audio_effects = pdata->audio_effects[fe_id];
  3457. if (!cstream || !audio_effects) {
  3458. pr_debug("%s: stream or effects inactive\n", __func__);
  3459. ret = -EINVAL;
  3460. goto done;
  3461. }
  3462. prtd = cstream->runtime->private_data;
  3463. if (!prtd) {
  3464. pr_err("%s: cannot set audio effects\n", __func__);
  3465. ret = -EINVAL;
  3466. goto done;
  3467. }
  3468. values[0] = (long)audio_effects->query.mod_id;
  3469. values[1] = (long)audio_effects->query.parm_id;
  3470. values[2] = (long)audio_effects->query.size;
  3471. values[3] = (long)audio_effects->query.offset;
  3472. values[4] = (long)audio_effects->query.device;
  3473. done:
  3474. mutex_unlock(&pdata->lock);
  3475. return ret;
  3476. }
  3477. static int msm_compr_send_dec_params(struct snd_compr_stream *cstream,
  3478. struct msm_compr_dec_params *dec_params,
  3479. int stream_id)
  3480. {
  3481. int rc = 0;
  3482. struct msm_compr_audio *prtd = NULL;
  3483. struct snd_dec_ddp *ddp = &dec_params->ddp_params;
  3484. if (!cstream || !dec_params) {
  3485. pr_err("%s: stream or dec_params inactive\n", __func__);
  3486. rc = -EINVAL;
  3487. goto end;
  3488. }
  3489. prtd = cstream->runtime->private_data;
  3490. if (!prtd) {
  3491. pr_err("%s: cannot set dec_params\n", __func__);
  3492. rc = -EINVAL;
  3493. goto end;
  3494. }
  3495. switch (prtd->codec) {
  3496. case FORMAT_MP3:
  3497. case FORMAT_MPEG4_AAC:
  3498. case FORMAT_TRUEHD:
  3499. case FORMAT_IEC61937:
  3500. case FORMAT_APTX:
  3501. pr_debug("%s: no runtime parameters for codec: %d\n", __func__,
  3502. prtd->codec);
  3503. break;
  3504. case FORMAT_AC3:
  3505. case FORMAT_EAC3:
  3506. if (prtd->compr_passthr != LEGACY_PCM) {
  3507. pr_debug("%s: No DDP param for compr_type[%d]\n",
  3508. __func__, prtd->compr_passthr);
  3509. break;
  3510. }
  3511. rc = msm_compr_send_ddp_cfg(prtd->audio_client, ddp, stream_id);
  3512. if (rc < 0)
  3513. pr_err("%s: DDP CMD CFG failed %d\n", __func__, rc);
  3514. break;
  3515. default:
  3516. break;
  3517. }
  3518. end:
  3519. return rc;
  3520. }
  3521. static int msm_compr_dec_params_put(struct snd_kcontrol *kcontrol,
  3522. struct snd_ctl_elem_value *ucontrol)
  3523. {
  3524. struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
  3525. unsigned long fe_id = kcontrol->private_value;
  3526. struct msm_compr_pdata *pdata = (struct msm_compr_pdata *)
  3527. snd_soc_component_get_drvdata(comp);
  3528. struct msm_compr_dec_params *dec_params = NULL;
  3529. struct snd_compr_stream *cstream = NULL;
  3530. struct msm_compr_audio *prtd = NULL;
  3531. long *values = &(ucontrol->value.integer.value[0]);
  3532. int rc = 0;
  3533. pr_debug("%s\n", __func__);
  3534. if (fe_id >= MSM_FRONTEND_DAI_MAX) {
  3535. pr_err("%s Received out of bounds fe_id %lu\n",
  3536. __func__, fe_id);
  3537. return -EINVAL;
  3538. }
  3539. cstream = pdata->cstream[fe_id];
  3540. dec_params = pdata->dec_params[fe_id];
  3541. if (!cstream || !dec_params) {
  3542. pr_err("%s: stream or dec_params inactive\n", __func__);
  3543. return -EINVAL;
  3544. }
  3545. prtd = cstream->runtime->private_data;
  3546. if (!prtd) {
  3547. pr_err("%s: cannot set dec_params\n", __func__);
  3548. return -EINVAL;
  3549. }
  3550. mutex_lock(&pdata->lock);
  3551. switch (prtd->codec) {
  3552. case FORMAT_MP3:
  3553. case FORMAT_MPEG4_AAC:
  3554. case FORMAT_FLAC:
  3555. case FORMAT_VORBIS:
  3556. case FORMAT_ALAC:
  3557. case FORMAT_APE:
  3558. case FORMAT_DTS:
  3559. case FORMAT_DSD:
  3560. case FORMAT_TRUEHD:
  3561. case FORMAT_IEC61937:
  3562. case FORMAT_APTX:
  3563. case FORMAT_AMRNB:
  3564. case FORMAT_AMRWB:
  3565. case FORMAT_AMR_WB_PLUS:
  3566. pr_debug("%s: no runtime parameters for codec: %d\n", __func__,
  3567. prtd->codec);
  3568. break;
  3569. case FORMAT_AC3:
  3570. case FORMAT_EAC3: {
  3571. struct snd_dec_ddp *ddp = &dec_params->ddp_params;
  3572. int cnt;
  3573. if (prtd->compr_passthr != LEGACY_PCM) {
  3574. pr_debug("%s: No DDP param for compr_type[%d]\n",
  3575. __func__, prtd->compr_passthr);
  3576. break;
  3577. }
  3578. ddp->params_length = (*values++);
  3579. if (ddp->params_length > DDP_DEC_MAX_NUM_PARAM) {
  3580. pr_err("%s: invalid num of params:: %d\n", __func__,
  3581. ddp->params_length);
  3582. rc = -EINVAL;
  3583. goto end;
  3584. }
  3585. for (cnt = 0; cnt < ddp->params_length; cnt++) {
  3586. ddp->params_id[cnt] = *values++;
  3587. ddp->params_value[cnt] = *values++;
  3588. }
  3589. prtd = cstream->runtime->private_data;
  3590. if (prtd && prtd->audio_client)
  3591. rc = msm_compr_send_dec_params(cstream, dec_params,
  3592. prtd->audio_client->stream_id);
  3593. break;
  3594. }
  3595. default:
  3596. break;
  3597. }
  3598. end:
  3599. pr_debug("%s: ret %d\n", __func__, rc);
  3600. mutex_unlock(&pdata->lock);
  3601. return rc;
  3602. }
  3603. static int msm_compr_dec_params_get(struct snd_kcontrol *kcontrol,
  3604. struct snd_ctl_elem_value *ucontrol)
  3605. {
  3606. /* dummy function */
  3607. return 0;
  3608. }
  3609. static int msm_compr_playback_app_type_cfg_put(struct snd_kcontrol *kcontrol,
  3610. struct snd_ctl_elem_value *ucontrol)
  3611. {
  3612. u64 fe_id = kcontrol->private_value;
  3613. int session_type = SESSION_TYPE_RX;
  3614. int be_id = ucontrol->value.integer.value[3];
  3615. struct msm_pcm_stream_app_type_cfg cfg_data = {0, 0, 48000};
  3616. int ret = 0;
  3617. cfg_data.app_type = ucontrol->value.integer.value[0];
  3618. cfg_data.acdb_dev_id = ucontrol->value.integer.value[1];
  3619. if (ucontrol->value.integer.value[2] != 0)
  3620. cfg_data.sample_rate = ucontrol->value.integer.value[2];
  3621. pr_debug("%s: fe_id- %llu session_type- %d be_id- %d app_type- %d acdb_dev_id- %d sample_rate- %d\n",
  3622. __func__, fe_id, session_type, be_id,
  3623. cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
  3624. ret = msm_pcm_routing_reg_stream_app_type_cfg(fe_id, session_type,
  3625. be_id, &cfg_data);
  3626. if (ret < 0)
  3627. pr_err("%s: msm_pcm_routing_reg_stream_app_type_cfg failed returned %d\n",
  3628. __func__, ret);
  3629. return ret;
  3630. }
  3631. static int msm_compr_playback_app_type_cfg_get(struct snd_kcontrol *kcontrol,
  3632. struct snd_ctl_elem_value *ucontrol)
  3633. {
  3634. u64 fe_id = kcontrol->private_value;
  3635. int session_type = SESSION_TYPE_RX;
  3636. int be_id = 0;
  3637. struct msm_pcm_stream_app_type_cfg cfg_data = {0};
  3638. int ret = 0;
  3639. ret = msm_pcm_routing_get_stream_app_type_cfg(fe_id, session_type,
  3640. &be_id, &cfg_data);
  3641. if (ret < 0) {
  3642. pr_err("%s: msm_pcm_routing_get_stream_app_type_cfg failed returned %d\n",
  3643. __func__, ret);
  3644. goto done;
  3645. }
  3646. ucontrol->value.integer.value[0] = cfg_data.app_type;
  3647. ucontrol->value.integer.value[1] = cfg_data.acdb_dev_id;
  3648. ucontrol->value.integer.value[2] = cfg_data.sample_rate;
  3649. ucontrol->value.integer.value[3] = be_id;
  3650. pr_debug("%s: fedai_id %llu, session_type %d, be_id %d, app_type %d, acdb_dev_id %d, sample_rate %d\n",
  3651. __func__, fe_id, session_type, be_id,
  3652. cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
  3653. done:
  3654. return ret;
  3655. }
  3656. static int msm_compr_capture_app_type_cfg_put(struct snd_kcontrol *kcontrol,
  3657. struct snd_ctl_elem_value *ucontrol)
  3658. {
  3659. u64 fe_id = kcontrol->private_value;
  3660. int session_type = SESSION_TYPE_TX;
  3661. int be_id = ucontrol->value.integer.value[3];
  3662. struct msm_pcm_stream_app_type_cfg cfg_data = {0, 0, 48000};
  3663. int ret = 0;
  3664. cfg_data.app_type = ucontrol->value.integer.value[0];
  3665. cfg_data.acdb_dev_id = ucontrol->value.integer.value[1];
  3666. if (ucontrol->value.integer.value[2] != 0)
  3667. cfg_data.sample_rate = ucontrol->value.integer.value[2];
  3668. pr_debug("%s: fe_id- %llu session_type- %d be_id- %d app_type- %d acdb_dev_id- %d sample_rate- %d\n",
  3669. __func__, fe_id, session_type, be_id,
  3670. cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
  3671. ret = msm_pcm_routing_reg_stream_app_type_cfg(fe_id, session_type,
  3672. be_id, &cfg_data);
  3673. if (ret < 0)
  3674. pr_err("%s: msm_pcm_routing_reg_stream_app_type_cfg failed returned %d\n",
  3675. __func__, ret);
  3676. return ret;
  3677. }
  3678. static int msm_compr_capture_app_type_cfg_get(struct snd_kcontrol *kcontrol,
  3679. struct snd_ctl_elem_value *ucontrol)
  3680. {
  3681. u64 fe_id = kcontrol->private_value;
  3682. int session_type = SESSION_TYPE_TX;
  3683. int be_id = 0;
  3684. struct msm_pcm_stream_app_type_cfg cfg_data = {0};
  3685. int ret = 0;
  3686. ret = msm_pcm_routing_get_stream_app_type_cfg(fe_id, session_type,
  3687. &be_id, &cfg_data);
  3688. if (ret < 0) {
  3689. pr_err("%s: msm_pcm_routing_get_stream_app_type_cfg failed returned %d\n",
  3690. __func__, ret);
  3691. goto done;
  3692. }
  3693. ucontrol->value.integer.value[0] = cfg_data.app_type;
  3694. ucontrol->value.integer.value[1] = cfg_data.acdb_dev_id;
  3695. ucontrol->value.integer.value[2] = cfg_data.sample_rate;
  3696. ucontrol->value.integer.value[3] = be_id;
  3697. pr_debug("%s: fedai_id %llu, session_type %d, be_id %d, app_type %d, acdb_dev_id %d, sample_rate %d\n",
  3698. __func__, fe_id, session_type, be_id,
  3699. cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
  3700. done:
  3701. return ret;
  3702. }
  3703. static int msm_compr_channel_map_put(struct snd_kcontrol *kcontrol,
  3704. struct snd_ctl_elem_value *ucontrol)
  3705. {
  3706. struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
  3707. u64 fe_id = kcontrol->private_value;
  3708. struct msm_compr_pdata *pdata = (struct msm_compr_pdata *)
  3709. snd_soc_component_get_drvdata(comp);
  3710. int rc = 0, i;
  3711. struct msm_pcm_channel_mixer *chmixer_pspd = NULL;
  3712. pr_debug("%s: fe_id- %llu\n", __func__, fe_id);
  3713. if (fe_id >= MSM_FRONTEND_DAI_MM_SIZE) {
  3714. pr_err("%s Received out of bounds fe_id %llu\n",
  3715. __func__, fe_id);
  3716. rc = -EINVAL;
  3717. goto end;
  3718. }
  3719. if (pdata->ch_map[fe_id]) {
  3720. pdata->ch_map[fe_id]->set_ch_map = true;
  3721. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  3722. pdata->ch_map[fe_id]->channel_map[i] =
  3723. (char)(ucontrol->value.integer.value[i]);
  3724. /* update chmixer_pspd chmap cached with routing driver as well */
  3725. chmixer_pspd = pdata->chmixer_pspd[fe_id];
  3726. if (chmixer_pspd && chmixer_pspd->enable) {
  3727. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  3728. chmixer_pspd->in_ch_map[i] =
  3729. pdata->ch_map[fe_id]->channel_map[i];
  3730. chmixer_pspd->override_in_ch_map = true;
  3731. msm_pcm_routing_set_channel_mixer_cfg(fe_id,
  3732. SESSION_TYPE_RX, chmixer_pspd);
  3733. }
  3734. } else {
  3735. pr_debug("%s: no memory for ch_map, default will be set\n",
  3736. __func__);
  3737. }
  3738. end:
  3739. pr_debug("%s: ret %d\n", __func__, rc);
  3740. return rc;
  3741. }
  3742. static int msm_compr_channel_map_get(struct snd_kcontrol *kcontrol,
  3743. struct snd_ctl_elem_value *ucontrol)
  3744. {
  3745. struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
  3746. u64 fe_id = kcontrol->private_value;
  3747. struct msm_compr_pdata *pdata = (struct msm_compr_pdata *)
  3748. snd_soc_component_get_drvdata(comp);
  3749. int rc = 0, i;
  3750. pr_debug("%s: fe_id- %llu\n", __func__, fe_id);
  3751. if (fe_id >= MSM_FRONTEND_DAI_MM_SIZE) {
  3752. pr_err("%s: Received out of bounds fe_id %llu\n",
  3753. __func__, fe_id);
  3754. rc = -EINVAL;
  3755. goto end;
  3756. }
  3757. if (pdata->ch_map[fe_id]) {
  3758. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  3759. ucontrol->value.integer.value[i] =
  3760. pdata->ch_map[fe_id]->channel_map[i];
  3761. }
  3762. end:
  3763. pr_debug("%s: ret %d\n", __func__, rc);
  3764. return rc;
  3765. }
  3766. static int msm_compr_adsp_stream_cmd_put(struct snd_kcontrol *kcontrol,
  3767. struct snd_ctl_elem_value *ucontrol)
  3768. {
  3769. struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
  3770. unsigned long fe_id = kcontrol->private_value;
  3771. struct msm_compr_pdata *pdata = (struct msm_compr_pdata *)
  3772. snd_soc_component_get_drvdata(comp);
  3773. struct snd_compr_stream *cstream = NULL;
  3774. struct msm_compr_audio *prtd;
  3775. int ret = 0;
  3776. struct msm_adsp_event_data *event_data = NULL;
  3777. if (fe_id >= MSM_FRONTEND_DAI_MAX) {
  3778. pr_err("%s Received invalid fe_id %lu\n",
  3779. __func__, fe_id);
  3780. return -EINVAL;
  3781. }
  3782. cstream = pdata->cstream[fe_id];
  3783. if (cstream == NULL) {
  3784. pr_err("%s cstream is null\n", __func__);
  3785. return -EINVAL;
  3786. }
  3787. prtd = cstream->runtime->private_data;
  3788. if (!prtd) {
  3789. pr_err("%s: prtd is null\n", __func__);
  3790. return -EINVAL;
  3791. }
  3792. mutex_lock(&pdata->lock);
  3793. if (prtd->audio_client == NULL) {
  3794. pr_err("%s: audio_client is null\n", __func__);
  3795. ret = -EINVAL;
  3796. goto done;
  3797. }
  3798. event_data = (struct msm_adsp_event_data *)ucontrol->value.bytes.data;
  3799. if ((event_data->event_type < ADSP_STREAM_PP_EVENT) ||
  3800. (event_data->event_type >= ADSP_STREAM_EVENT_MAX)) {
  3801. pr_err("%s: invalid event_type=%d",
  3802. __func__, event_data->event_type);
  3803. ret = -EINVAL;
  3804. goto done;
  3805. }
  3806. if (event_data->payload_len > sizeof(ucontrol->value.bytes.data)
  3807. - sizeof(struct msm_adsp_event_data)) {
  3808. pr_err("%s param length=%d exceeds limit",
  3809. __func__, event_data->payload_len);
  3810. ret = -EINVAL;
  3811. goto done;
  3812. }
  3813. ret = q6asm_send_stream_cmd(prtd->audio_client, event_data);
  3814. if (ret < 0)
  3815. pr_err("%s: failed to send stream event cmd, err = %d\n",
  3816. __func__, ret);
  3817. done:
  3818. mutex_unlock(&pdata->lock);
  3819. return ret;
  3820. }
  3821. static int msm_compr_ion_fd_map_put(struct snd_kcontrol *kcontrol,
  3822. struct snd_ctl_elem_value *ucontrol)
  3823. {
  3824. struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
  3825. unsigned long fe_id = kcontrol->private_value;
  3826. struct msm_compr_pdata *pdata = (struct msm_compr_pdata *)
  3827. snd_soc_component_get_drvdata(comp);
  3828. struct snd_compr_stream *cstream = NULL;
  3829. struct msm_compr_audio *prtd;
  3830. int fd;
  3831. int ret = 0;
  3832. if (fe_id >= MSM_FRONTEND_DAI_MAX) {
  3833. pr_err("%s Received out of bounds invalid fe_id %lu\n",
  3834. __func__, fe_id);
  3835. return -EINVAL;
  3836. }
  3837. cstream = pdata->cstream[fe_id];
  3838. if (cstream == NULL) {
  3839. pr_err("%s cstream is null\n", __func__);
  3840. return -EINVAL;
  3841. }
  3842. prtd = cstream->runtime->private_data;
  3843. if (!prtd) {
  3844. pr_err("%s: prtd is null\n", __func__);
  3845. return -EINVAL;
  3846. }
  3847. mutex_lock(&pdata->lock);
  3848. if (prtd->audio_client == NULL) {
  3849. pr_err("%s: audio_client is null\n", __func__);
  3850. ret = -EINVAL;
  3851. goto done;
  3852. }
  3853. memcpy(&fd, ucontrol->value.bytes.data, sizeof(fd));
  3854. ret = q6asm_send_ion_fd(prtd->audio_client, fd);
  3855. if (ret < 0)
  3856. pr_err("%s: failed to register ion fd\n", __func__);
  3857. done:
  3858. mutex_unlock(&pdata->lock);
  3859. return ret;
  3860. }
  3861. static int msm_compr_rtic_event_ack_put(struct snd_kcontrol *kcontrol,
  3862. struct snd_ctl_elem_value *ucontrol)
  3863. {
  3864. struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
  3865. unsigned long fe_id = kcontrol->private_value;
  3866. struct msm_compr_pdata *pdata = (struct msm_compr_pdata *)
  3867. snd_soc_component_get_drvdata(comp);
  3868. struct snd_compr_stream *cstream = NULL;
  3869. struct msm_compr_audio *prtd;
  3870. int ret = 0;
  3871. int param_length = 0;
  3872. if (fe_id >= MSM_FRONTEND_DAI_MAX) {
  3873. pr_err("%s Received invalid fe_id %lu\n",
  3874. __func__, fe_id);
  3875. return -EINVAL;
  3876. }
  3877. mutex_lock(&pdata->lock);
  3878. cstream = pdata->cstream[fe_id];
  3879. if (cstream == NULL) {
  3880. pr_err("%s cstream is null\n", __func__);
  3881. ret = -EINVAL;
  3882. goto done;
  3883. }
  3884. prtd = cstream->runtime->private_data;
  3885. if (!prtd) {
  3886. pr_err("%s: prtd is null\n", __func__);
  3887. ret = -EINVAL;
  3888. goto done;
  3889. }
  3890. if (prtd->audio_client == NULL) {
  3891. pr_err("%s: audio_client is null\n", __func__);
  3892. ret = -EINVAL;
  3893. goto done;
  3894. }
  3895. memcpy(&param_length, ucontrol->value.bytes.data,
  3896. sizeof(param_length));
  3897. if ((param_length + sizeof(param_length))
  3898. >= sizeof(ucontrol->value.bytes.data)) {
  3899. pr_err("%s param length=%d exceeds limit",
  3900. __func__, param_length);
  3901. ret = -EINVAL;
  3902. goto done;
  3903. }
  3904. ret = q6asm_send_rtic_event_ack(prtd->audio_client,
  3905. ucontrol->value.bytes.data + sizeof(param_length),
  3906. param_length);
  3907. if (ret < 0)
  3908. pr_err("%s: failed to send rtic event ack, err = %d\n",
  3909. __func__, ret);
  3910. done:
  3911. mutex_unlock(&pdata->lock);
  3912. return ret;
  3913. }
  3914. static int msm_compr_gapless_put(struct snd_kcontrol *kcontrol,
  3915. struct snd_ctl_elem_value *ucontrol)
  3916. {
  3917. struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
  3918. struct msm_compr_pdata *pdata = (struct msm_compr_pdata *)
  3919. snd_soc_component_get_drvdata(comp);
  3920. pdata->use_dsp_gapless_mode = ucontrol->value.integer.value[0];
  3921. pr_debug("%s: value: %ld\n", __func__,
  3922. ucontrol->value.integer.value[0]);
  3923. return 0;
  3924. }
  3925. static int msm_compr_gapless_get(struct snd_kcontrol *kcontrol,
  3926. struct snd_ctl_elem_value *ucontrol)
  3927. {
  3928. struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
  3929. struct msm_compr_pdata *pdata =
  3930. snd_soc_component_get_drvdata(comp);
  3931. pr_debug("%s:gapless mode %d\n", __func__, pdata->use_dsp_gapless_mode);
  3932. ucontrol->value.integer.value[0] = pdata->use_dsp_gapless_mode;
  3933. return 0;
  3934. }
  3935. static const struct snd_kcontrol_new msm_compr_gapless_controls[] = {
  3936. SOC_SINGLE_EXT("Compress Gapless Playback",
  3937. 0, 0, 1, 0,
  3938. msm_compr_gapless_get,
  3939. msm_compr_gapless_put),
  3940. };
  3941. static int msm_compr_probe(struct snd_soc_component *component)
  3942. {
  3943. struct msm_compr_pdata *pdata;
  3944. int i;
  3945. int rc;
  3946. const char *qdsp_version;
  3947. pr_debug("%s\n", __func__);
  3948. pdata = (struct msm_compr_pdata *) dev_get_drvdata(component->dev);
  3949. if (!pdata) {
  3950. pr_err("%s platform data not set\n", __func__);
  3951. return -EINVAL;
  3952. }
  3953. snd_soc_component_set_drvdata(component, pdata);
  3954. for (i = 0; i < MSM_FRONTEND_DAI_MAX; i++) {
  3955. pdata->volume[i][0] = COMPRESSED_LR_VOL_MAX_STEPS;
  3956. pdata->volume[i][1] = COMPRESSED_LR_VOL_MAX_STEPS;
  3957. pdata->audio_effects[i] = NULL;
  3958. pdata->dec_params[i] = NULL;
  3959. pdata->cstream[i] = NULL;
  3960. pdata->ch_map[i] = NULL;
  3961. pdata->is_in_use[i] = false;
  3962. }
  3963. snd_soc_add_component_controls(component, msm_compr_gapless_controls,
  3964. ARRAY_SIZE(msm_compr_gapless_controls));
  3965. rc = of_property_read_string(component->dev->of_node,
  3966. "qcom,adsp-version", &qdsp_version);
  3967. if (!rc) {
  3968. if (!strcmp(qdsp_version, "MDSP 1.2"))
  3969. pdata->use_legacy_api = true;
  3970. else
  3971. pdata->use_legacy_api = false;
  3972. } else
  3973. pdata->use_legacy_api = false;
  3974. pr_debug("%s: use legacy api %d\n", __func__, pdata->use_legacy_api);
  3975. /*
  3976. * use_dsp_gapless_mode part of platform data(pdata) is updated from HAL
  3977. * through a mixer control before compress driver is opened. The mixer
  3978. * control is used to decide if dsp gapless mode needs to be enabled.
  3979. * Gapless is disabled by default.
  3980. */
  3981. pdata->use_dsp_gapless_mode = false;
  3982. return 0;
  3983. }
  3984. static int msm_compr_volume_info(struct snd_kcontrol *kcontrol,
  3985. struct snd_ctl_elem_info *uinfo)
  3986. {
  3987. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  3988. uinfo->count = 2;
  3989. uinfo->value.integer.min = 0;
  3990. uinfo->value.integer.max = COMPRESSED_LR_VOL_MAX_STEPS;
  3991. return 0;
  3992. }
  3993. static int msm_compr_audio_effects_config_info(struct snd_kcontrol *kcontrol,
  3994. struct snd_ctl_elem_info *uinfo)
  3995. {
  3996. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  3997. uinfo->count = MAX_PP_PARAMS_SZ;
  3998. uinfo->value.integer.min = 0;
  3999. uinfo->value.integer.max = 0xFFFFFFFF;
  4000. return 0;
  4001. }
  4002. static int msm_compr_query_audio_effect_info(struct snd_kcontrol *kcontrol,
  4003. struct snd_ctl_elem_info *uinfo)
  4004. {
  4005. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  4006. uinfo->count = 128;
  4007. uinfo->value.integer.min = 0;
  4008. uinfo->value.integer.max = 0xFFFFFFFF;
  4009. return 0;
  4010. }
  4011. static int msm_compr_dec_params_info(struct snd_kcontrol *kcontrol,
  4012. struct snd_ctl_elem_info *uinfo)
  4013. {
  4014. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  4015. uinfo->count = 128;
  4016. uinfo->value.integer.min = 0;
  4017. uinfo->value.integer.max = 0xFFFFFFFF;
  4018. return 0;
  4019. }
  4020. static int msm_compr_app_type_cfg_info(struct snd_kcontrol *kcontrol,
  4021. struct snd_ctl_elem_info *uinfo)
  4022. {
  4023. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  4024. uinfo->count = 5;
  4025. uinfo->value.integer.min = 0;
  4026. uinfo->value.integer.max = 0xFFFFFFFF;
  4027. return 0;
  4028. }
  4029. static int msm_compr_channel_map_info(struct snd_kcontrol *kcontrol,
  4030. struct snd_ctl_elem_info *uinfo)
  4031. {
  4032. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  4033. uinfo->count = PCM_FORMAT_MAX_NUM_CHANNEL_V8;
  4034. uinfo->value.integer.min = 0;
  4035. /* See PCM_MAX_CHANNEL_MAP in apr_audio-v2.h */
  4036. uinfo->value.integer.max = PCM_MAX_CHANNEL_MAP;
  4037. return 0;
  4038. }
  4039. static int msm_compr_add_volume_control(struct snd_soc_pcm_runtime *rtd)
  4040. {
  4041. struct snd_soc_component *component = NULL;
  4042. const char *mixer_ctl_name = "Compress Playback";
  4043. const char *deviceNo = "NN";
  4044. const char *suffix = "Volume";
  4045. char *mixer_str = NULL;
  4046. int ctl_len;
  4047. struct snd_kcontrol_new fe_volume_control[1] = {
  4048. {
  4049. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  4050. .name = "?",
  4051. .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |
  4052. SNDRV_CTL_ELEM_ACCESS_READWRITE,
  4053. .info = msm_compr_volume_info,
  4054. .tlv.p = msm_compr_vol_gain,
  4055. .get = msm_compr_volume_get,
  4056. .put = msm_compr_volume_put,
  4057. .private_value = 0,
  4058. }
  4059. };
  4060. if (!rtd) {
  4061. pr_err("%s NULL rtd\n", __func__);
  4062. return 0;
  4063. }
  4064. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  4065. if (!component) {
  4066. pr_err("%s: component is NULL\n", __func__);
  4067. return 0;
  4068. }
  4069. pr_debug("%s: added new compr FE with name %s, id %d, cpu dai %s, device no %d\n",
  4070. __func__, rtd->dai_link->name, rtd->dai_link->id,
  4071. rtd->dai_link->cpus->dai_name, rtd->pcm->device);
  4072. ctl_len = strlen(mixer_ctl_name) + 1 + strlen(deviceNo) + 1 +
  4073. strlen(suffix) + 1;
  4074. mixer_str = kzalloc(ctl_len, GFP_KERNEL);
  4075. if (!mixer_str) {
  4076. pr_err("failed to allocate mixer ctrl str of len %d", ctl_len);
  4077. return 0;
  4078. }
  4079. snprintf(mixer_str, ctl_len, "%s %d %s", mixer_ctl_name,
  4080. rtd->pcm->device, suffix);
  4081. fe_volume_control[0].name = mixer_str;
  4082. fe_volume_control[0].private_value = rtd->dai_link->id;
  4083. pr_debug("Registering new mixer ctl %s", mixer_str);
  4084. snd_soc_add_component_controls(component, fe_volume_control,
  4085. ARRAY_SIZE(fe_volume_control));
  4086. kfree(mixer_str);
  4087. return 0;
  4088. }
  4089. static int msm_compr_add_audio_effects_control(struct snd_soc_pcm_runtime *rtd)
  4090. {
  4091. struct snd_soc_component *component = NULL;
  4092. const char *mixer_ctl_name = "Audio Effects Config";
  4093. const char *deviceNo = "NN";
  4094. char *mixer_str = NULL;
  4095. int ctl_len;
  4096. struct snd_kcontrol_new fe_audio_effects_config_control[1] = {
  4097. {
  4098. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  4099. .name = "?",
  4100. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  4101. .info = msm_compr_audio_effects_config_info,
  4102. .get = msm_compr_audio_effects_config_get,
  4103. .put = msm_compr_audio_effects_config_put,
  4104. .private_value = 0,
  4105. }
  4106. };
  4107. if (!rtd) {
  4108. pr_err("%s NULL rtd\n", __func__);
  4109. return 0;
  4110. }
  4111. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  4112. if (!component) {
  4113. pr_err("%s: component is NULL\n", __func__);
  4114. return 0;
  4115. }
  4116. pr_debug("%s: added new compr FE with name %s, id %d, cpu dai %s, device no %d\n",
  4117. __func__, rtd->dai_link->name, rtd->dai_link->id,
  4118. rtd->dai_link->cpus->dai_name, rtd->pcm->device);
  4119. ctl_len = strlen(mixer_ctl_name) + 1 + strlen(deviceNo) + 1;
  4120. mixer_str = kzalloc(ctl_len, GFP_KERNEL);
  4121. if (!mixer_str)
  4122. return 0;
  4123. snprintf(mixer_str, ctl_len, "%s %d", mixer_ctl_name, rtd->pcm->device);
  4124. fe_audio_effects_config_control[0].name = mixer_str;
  4125. fe_audio_effects_config_control[0].private_value = rtd->dai_link->id;
  4126. pr_debug("Registering new mixer ctl %s\n", mixer_str);
  4127. snd_soc_add_component_controls(component,
  4128. fe_audio_effects_config_control,
  4129. ARRAY_SIZE(fe_audio_effects_config_control));
  4130. kfree(mixer_str);
  4131. return 0;
  4132. }
  4133. static int msm_compr_add_query_audio_effect_control(
  4134. struct snd_soc_pcm_runtime *rtd)
  4135. {
  4136. struct snd_soc_component *component = NULL;
  4137. const char *mixer_ctl_name = "Query Audio Effect Param";
  4138. const char *deviceNo = "NN";
  4139. char *mixer_str = NULL;
  4140. int ctl_len;
  4141. struct snd_kcontrol_new fe_query_audio_effect_control[1] = {
  4142. {
  4143. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  4144. .name = "?",
  4145. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  4146. .info = msm_compr_query_audio_effect_info,
  4147. .get = msm_compr_query_audio_effect_get,
  4148. .put = msm_compr_query_audio_effect_put,
  4149. .private_value = 0,
  4150. }
  4151. };
  4152. if (!rtd) {
  4153. pr_err("%s NULL rtd\n", __func__);
  4154. return 0;
  4155. }
  4156. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  4157. if (!component) {
  4158. pr_err("%s: component is NULL\n", __func__);
  4159. return 0;
  4160. }
  4161. pr_debug("%s: added new compr FE with name %s, id %d, cpu dai %s, device no %d\n",
  4162. __func__, rtd->dai_link->name, rtd->dai_link->id,
  4163. rtd->dai_link->cpus->dai_name, rtd->pcm->device);
  4164. ctl_len = strlen(mixer_ctl_name) + 1 + strlen(deviceNo) + 1;
  4165. mixer_str = kzalloc(ctl_len, GFP_KERNEL);
  4166. if (!mixer_str) {
  4167. pr_err("failed to allocate mixer ctrl str of len %d", ctl_len);
  4168. return 0;
  4169. }
  4170. snprintf(mixer_str, ctl_len, "%s %d", mixer_ctl_name, rtd->pcm->device);
  4171. fe_query_audio_effect_control[0].name = mixer_str;
  4172. fe_query_audio_effect_control[0].private_value = rtd->dai_link->id;
  4173. pr_debug("%s: registering new mixer ctl %s\n", __func__, mixer_str);
  4174. snd_soc_add_component_controls(component,
  4175. fe_query_audio_effect_control,
  4176. ARRAY_SIZE(fe_query_audio_effect_control));
  4177. kfree(mixer_str);
  4178. return 0;
  4179. }
  4180. static int msm_compr_add_audio_adsp_stream_cmd_control(
  4181. struct snd_soc_pcm_runtime *rtd)
  4182. {
  4183. struct snd_soc_component *component = NULL;
  4184. const char *mixer_ctl_name = DSP_STREAM_CMD;
  4185. const char *deviceNo = "NN";
  4186. char *mixer_str = NULL;
  4187. int ctl_len = 0, ret = 0;
  4188. struct snd_kcontrol_new fe_audio_adsp_stream_cmd_config_control[1] = {
  4189. {
  4190. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  4191. .name = "?",
  4192. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  4193. .info = msm_adsp_stream_cmd_info,
  4194. .put = msm_compr_adsp_stream_cmd_put,
  4195. .private_value = 0,
  4196. }
  4197. };
  4198. if (!rtd) {
  4199. pr_err("%s NULL rtd\n", __func__);
  4200. return -EINVAL;
  4201. }
  4202. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  4203. if (!component) {
  4204. pr_err("%s: component is NULL\n", __func__);
  4205. return -EINVAL;
  4206. }
  4207. ctl_len = strlen(mixer_ctl_name) + 1 + strlen(deviceNo) + 1;
  4208. mixer_str = kzalloc(ctl_len, GFP_KERNEL);
  4209. if (!mixer_str)
  4210. return -ENOMEM;
  4211. snprintf(mixer_str, ctl_len, "%s %d", mixer_ctl_name, rtd->pcm->device);
  4212. fe_audio_adsp_stream_cmd_config_control[0].name = mixer_str;
  4213. fe_audio_adsp_stream_cmd_config_control[0].private_value =
  4214. rtd->dai_link->id;
  4215. pr_debug("%s: Registering new mixer ctl %s\n", __func__, mixer_str);
  4216. ret = snd_soc_add_component_controls(component,
  4217. fe_audio_adsp_stream_cmd_config_control,
  4218. ARRAY_SIZE(fe_audio_adsp_stream_cmd_config_control));
  4219. if (ret < 0)
  4220. pr_err("%s: failed to add ctl %s. err = %d\n",
  4221. __func__, mixer_str, ret);
  4222. kfree(mixer_str);
  4223. return ret;
  4224. }
  4225. static int msm_compr_add_audio_adsp_stream_callback_control(
  4226. struct snd_soc_pcm_runtime *rtd)
  4227. {
  4228. struct snd_soc_component *component = NULL;
  4229. const char *mixer_ctl_name = DSP_STREAM_CALLBACK;
  4230. const char *deviceNo = "NN";
  4231. char *mixer_str = NULL;
  4232. int ctl_len = 0, ret = 0;
  4233. struct snd_kcontrol *kctl;
  4234. struct snd_kcontrol_new fe_audio_adsp_callback_config_control[1] = {
  4235. {
  4236. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  4237. .name = "?",
  4238. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  4239. .info = msm_adsp_stream_callback_info,
  4240. .get = msm_adsp_stream_callback_get,
  4241. .private_value = 0,
  4242. }
  4243. };
  4244. if (!rtd) {
  4245. pr_err("%s: rtd is NULL\n", __func__);
  4246. ret = -EINVAL;
  4247. goto done;
  4248. }
  4249. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  4250. if (!component) {
  4251. pr_err("%s: component is NULL\n", __func__);
  4252. return -EINVAL;
  4253. }
  4254. ctl_len = strlen(mixer_ctl_name) + 1 + strlen(deviceNo) + 1;
  4255. mixer_str = kzalloc(ctl_len, GFP_KERNEL);
  4256. if (!mixer_str) {
  4257. ret = -ENOMEM;
  4258. goto done;
  4259. }
  4260. snprintf(mixer_str, ctl_len, "%s %d", mixer_ctl_name, rtd->pcm->device);
  4261. fe_audio_adsp_callback_config_control[0].name = mixer_str;
  4262. fe_audio_adsp_callback_config_control[0].private_value =
  4263. rtd->dai_link->id;
  4264. pr_debug("%s: Registering new mixer ctl %s\n", __func__, mixer_str);
  4265. ret = snd_soc_add_component_controls(component,
  4266. fe_audio_adsp_callback_config_control,
  4267. ARRAY_SIZE(fe_audio_adsp_callback_config_control));
  4268. if (ret < 0) {
  4269. pr_err("%s: failed to add ctl %s. err = %d\n",
  4270. __func__, mixer_str, ret);
  4271. ret = -EINVAL;
  4272. goto free_mixer_str;
  4273. }
  4274. kctl = snd_soc_card_get_kcontrol(rtd->card, mixer_str);
  4275. if (!kctl) {
  4276. pr_err("%s: failed to get kctl %s.\n", __func__, mixer_str);
  4277. ret = -EINVAL;
  4278. goto free_mixer_str;
  4279. }
  4280. kctl->private_data = NULL;
  4281. free_mixer_str:
  4282. kfree(mixer_str);
  4283. done:
  4284. return ret;
  4285. }
  4286. static int msm_compr_add_dec_runtime_params_control(
  4287. struct snd_soc_pcm_runtime *rtd)
  4288. {
  4289. struct snd_soc_component *component = NULL;
  4290. const char *mixer_ctl_name = "Audio Stream";
  4291. const char *deviceNo = "NN";
  4292. const char *suffix = "Dec Params";
  4293. char *mixer_str = NULL;
  4294. int ctl_len;
  4295. struct snd_kcontrol_new fe_dec_params_control[1] = {
  4296. {
  4297. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  4298. .name = "?",
  4299. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  4300. .info = msm_compr_dec_params_info,
  4301. .get = msm_compr_dec_params_get,
  4302. .put = msm_compr_dec_params_put,
  4303. .private_value = 0,
  4304. }
  4305. };
  4306. if (!rtd) {
  4307. pr_err("%s NULL rtd\n", __func__);
  4308. return 0;
  4309. }
  4310. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  4311. if (!component) {
  4312. pr_err("%s: component is NULL\n", __func__);
  4313. return 0;
  4314. }
  4315. pr_debug("%s: added new compr FE with name %s, id %d, cpu dai %s, device no %d\n",
  4316. __func__, rtd->dai_link->name, rtd->dai_link->id,
  4317. rtd->dai_link->cpus->dai_name, rtd->pcm->device);
  4318. ctl_len = strlen(mixer_ctl_name) + 1 + strlen(deviceNo) + 1 +
  4319. strlen(suffix) + 1;
  4320. mixer_str = kzalloc(ctl_len, GFP_KERNEL);
  4321. if (!mixer_str)
  4322. return 0;
  4323. snprintf(mixer_str, ctl_len, "%s %d %s", mixer_ctl_name,
  4324. rtd->pcm->device, suffix);
  4325. fe_dec_params_control[0].name = mixer_str;
  4326. fe_dec_params_control[0].private_value = rtd->dai_link->id;
  4327. pr_debug("Registering new mixer ctl %s", mixer_str);
  4328. snd_soc_add_component_controls(component,
  4329. fe_dec_params_control,
  4330. ARRAY_SIZE(fe_dec_params_control));
  4331. kfree(mixer_str);
  4332. return 0;
  4333. }
  4334. static int msm_compr_add_app_type_cfg_control(struct snd_soc_pcm_runtime *rtd)
  4335. {
  4336. struct snd_soc_component *component = NULL;
  4337. const char *playback_mixer_ctl_name = "Audio Stream";
  4338. const char *capture_mixer_ctl_name = "Audio Stream Capture";
  4339. const char *deviceNo = "NN";
  4340. const char *suffix = "App Type Cfg";
  4341. char *mixer_str = NULL;
  4342. int ctl_len;
  4343. struct snd_kcontrol_new fe_app_type_cfg_control[1] = {
  4344. {
  4345. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  4346. .name = "?",
  4347. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  4348. .info = msm_compr_app_type_cfg_info,
  4349. .put = msm_compr_playback_app_type_cfg_put,
  4350. .get = msm_compr_playback_app_type_cfg_get,
  4351. .private_value = 0,
  4352. }
  4353. };
  4354. if (!rtd) {
  4355. pr_err("%s NULL rtd\n", __func__);
  4356. return 0;
  4357. }
  4358. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  4359. if (!component) {
  4360. pr_err("%s: component is NULL\n", __func__);
  4361. return 0;
  4362. }
  4363. pr_debug("%s: added new compr FE ctl with name %s, id %d, cpu dai %s, device no %d\n",
  4364. __func__, rtd->dai_link->name, rtd->dai_link->id,
  4365. rtd->dai_link->cpus->dai_name, rtd->pcm->device);
  4366. if (rtd->compr->direction == SND_COMPRESS_PLAYBACK)
  4367. ctl_len = strlen(playback_mixer_ctl_name) + 1 + strlen(deviceNo)
  4368. + 1 + strlen(suffix) + 1;
  4369. else
  4370. ctl_len = strlen(capture_mixer_ctl_name) + 1 + strlen(deviceNo)
  4371. + 1 + strlen(suffix) + 1;
  4372. mixer_str = kzalloc(ctl_len, GFP_KERNEL);
  4373. if (!mixer_str)
  4374. return 0;
  4375. if (rtd->compr->direction == SND_COMPRESS_PLAYBACK)
  4376. snprintf(mixer_str, ctl_len, "%s %d %s",
  4377. playback_mixer_ctl_name, rtd->pcm->device, suffix);
  4378. else
  4379. snprintf(mixer_str, ctl_len, "%s %d %s",
  4380. capture_mixer_ctl_name, rtd->pcm->device, suffix);
  4381. fe_app_type_cfg_control[0].name = mixer_str;
  4382. fe_app_type_cfg_control[0].private_value = rtd->dai_link->id;
  4383. if (rtd->compr->direction == SND_COMPRESS_PLAYBACK) {
  4384. fe_app_type_cfg_control[0].put =
  4385. msm_compr_playback_app_type_cfg_put;
  4386. fe_app_type_cfg_control[0].get =
  4387. msm_compr_playback_app_type_cfg_get;
  4388. } else {
  4389. fe_app_type_cfg_control[0].put =
  4390. msm_compr_capture_app_type_cfg_put;
  4391. fe_app_type_cfg_control[0].get =
  4392. msm_compr_capture_app_type_cfg_get;
  4393. }
  4394. pr_debug("Registering new mixer ctl %s", mixer_str);
  4395. snd_soc_add_component_controls(component,
  4396. fe_app_type_cfg_control,
  4397. ARRAY_SIZE(fe_app_type_cfg_control));
  4398. kfree(mixer_str);
  4399. return 0;
  4400. }
  4401. static int msm_compr_add_channel_map_control(struct snd_soc_pcm_runtime *rtd)
  4402. {
  4403. struct snd_soc_component *component = NULL;
  4404. const char *mixer_ctl_name = "Playback Channel Map";
  4405. const char *deviceNo = "NN";
  4406. char *mixer_str = NULL;
  4407. struct msm_compr_pdata *pdata = NULL;
  4408. int ctl_len;
  4409. struct snd_kcontrol_new fe_channel_map_control[1] = {
  4410. {
  4411. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  4412. .name = "?",
  4413. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  4414. .info = msm_compr_channel_map_info,
  4415. .get = msm_compr_channel_map_get,
  4416. .put = msm_compr_channel_map_put,
  4417. .private_value = 0,
  4418. }
  4419. };
  4420. if (!rtd) {
  4421. pr_err("%s: NULL rtd\n", __func__);
  4422. return -EINVAL;
  4423. }
  4424. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  4425. if (!component) {
  4426. pr_err("%s: component is NULL\n", __func__);
  4427. return -EINVAL;
  4428. }
  4429. pr_debug("%s: added new compr FE with name %s, id %d, cpu dai %s, device no %d\n",
  4430. __func__, rtd->dai_link->name, rtd->dai_link->id,
  4431. rtd->dai_link->cpus->dai_name, rtd->pcm->device);
  4432. ctl_len = strlen(mixer_ctl_name) + strlen(deviceNo) + 1;
  4433. mixer_str = kzalloc(ctl_len, GFP_KERNEL);
  4434. if (!mixer_str)
  4435. return -ENOMEM;
  4436. snprintf(mixer_str, ctl_len, "%s%d", mixer_ctl_name, rtd->pcm->device);
  4437. fe_channel_map_control[0].name = mixer_str;
  4438. fe_channel_map_control[0].private_value = rtd->dai_link->id;
  4439. pr_debug("%s: Registering new mixer ctl %s\n", __func__, mixer_str);
  4440. snd_soc_add_component_controls(component,
  4441. fe_channel_map_control,
  4442. ARRAY_SIZE(fe_channel_map_control));
  4443. pdata = snd_soc_component_get_drvdata(component);
  4444. pdata->ch_map[rtd->dai_link->id] =
  4445. kzalloc(sizeof(struct msm_compr_ch_map), GFP_KERNEL);
  4446. if (!pdata->ch_map[rtd->dai_link->id]) {
  4447. pr_err("%s: Could not allocate memory for channel map\n",
  4448. __func__);
  4449. kfree(mixer_str);
  4450. return -ENOMEM;
  4451. }
  4452. kfree(mixer_str);
  4453. return 0;
  4454. }
  4455. static int msm_compr_add_io_fd_cmd_control(struct snd_soc_pcm_runtime *rtd)
  4456. {
  4457. struct snd_soc_component *component = NULL;
  4458. const char *mixer_ctl_name = "Playback ION FD";
  4459. const char *deviceNo = "NN";
  4460. char *mixer_str = NULL;
  4461. int ctl_len = 0, ret = 0;
  4462. struct snd_kcontrol_new fe_ion_fd_config_control[1] = {
  4463. {
  4464. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  4465. .name = "?",
  4466. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  4467. .info = msm_adsp_stream_cmd_info,
  4468. .put = msm_compr_ion_fd_map_put,
  4469. .private_value = 0,
  4470. }
  4471. };
  4472. if (!rtd) {
  4473. pr_err("%s NULL rtd\n", __func__);
  4474. return -EINVAL;
  4475. }
  4476. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  4477. if (!component) {
  4478. pr_err("%s: component is NULL\n", __func__);
  4479. return -EINVAL;
  4480. }
  4481. ctl_len = strlen(mixer_ctl_name) + 1 + strlen(deviceNo) + 1;
  4482. mixer_str = kzalloc(ctl_len, GFP_KERNEL);
  4483. if (!mixer_str)
  4484. return -ENOMEM;
  4485. snprintf(mixer_str, ctl_len, "%s %d", mixer_ctl_name, rtd->pcm->device);
  4486. fe_ion_fd_config_control[0].name = mixer_str;
  4487. fe_ion_fd_config_control[0].private_value = rtd->dai_link->id;
  4488. pr_debug("%s: Registering new mixer ctl %s\n", __func__, mixer_str);
  4489. ret = snd_soc_add_component_controls(component,
  4490. fe_ion_fd_config_control,
  4491. ARRAY_SIZE(fe_ion_fd_config_control));
  4492. if (ret < 0)
  4493. pr_err("%s: failed to add ctl %s\n", __func__, mixer_str);
  4494. kfree(mixer_str);
  4495. return ret;
  4496. }
  4497. static int msm_compr_add_event_ack_cmd_control(struct snd_soc_pcm_runtime *rtd)
  4498. {
  4499. struct snd_soc_component *component = NULL;
  4500. const char *mixer_ctl_name = "Playback Event Ack";
  4501. const char *deviceNo = "NN";
  4502. char *mixer_str = NULL;
  4503. int ctl_len = 0, ret = 0;
  4504. struct snd_kcontrol_new fe_event_ack_config_control[1] = {
  4505. {
  4506. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  4507. .name = "?",
  4508. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  4509. .info = msm_adsp_stream_cmd_info,
  4510. .put = msm_compr_rtic_event_ack_put,
  4511. .private_value = 0,
  4512. }
  4513. };
  4514. if (!rtd) {
  4515. pr_err("%s NULL rtd\n", __func__);
  4516. return -EINVAL;
  4517. }
  4518. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  4519. if (!component) {
  4520. pr_err("%s: component is NULL\n", __func__);
  4521. return -EINVAL;
  4522. }
  4523. ctl_len = strlen(mixer_ctl_name) + 1 + strlen(deviceNo) + 1;
  4524. mixer_str = kzalloc(ctl_len, GFP_KERNEL);
  4525. if (!mixer_str)
  4526. return -ENOMEM;
  4527. snprintf(mixer_str, ctl_len, "%s %d", mixer_ctl_name, rtd->pcm->device);
  4528. fe_event_ack_config_control[0].name = mixer_str;
  4529. fe_event_ack_config_control[0].private_value = rtd->dai_link->id;
  4530. pr_debug("%s: Registering new mixer ctl %s\n", __func__, mixer_str);
  4531. ret = snd_soc_add_component_controls(component,
  4532. fe_event_ack_config_control,
  4533. ARRAY_SIZE(fe_event_ack_config_control));
  4534. if (ret < 0)
  4535. pr_err("%s: failed to add ctl %s\n", __func__, mixer_str);
  4536. kfree(mixer_str);
  4537. return ret;
  4538. }
  4539. static struct msm_pcm_channel_mixer *msm_compr_get_chmixer(
  4540. struct msm_compr_pdata *pdata, u64 fe_id)
  4541. {
  4542. if (!pdata) {
  4543. pr_err("%s: missing pdata\n", __func__);
  4544. return NULL;
  4545. }
  4546. if (fe_id >= MSM_FRONTEND_DAI_MM_SIZE) {
  4547. pr_err("%s: invalid FE %llu\n", __func__, fe_id);
  4548. return NULL;
  4549. }
  4550. return pdata->chmixer_pspd[fe_id];
  4551. }
  4552. static int msm_compr_channel_mixer_cfg_ctl_put(struct snd_kcontrol *kcontrol,
  4553. struct snd_ctl_elem_value *ucontrol)
  4554. {
  4555. u64 fe_id = kcontrol->private_value & 0xFF;
  4556. int session_type = (kcontrol->private_value >> 8) & 0xFF;
  4557. int ret = 0, i = 0, stream_id = 0, be_id = 0;
  4558. struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
  4559. struct msm_compr_pdata *pdata = (struct msm_compr_pdata *)
  4560. snd_soc_component_get_drvdata(comp);
  4561. struct snd_compr_stream *cstream = NULL;
  4562. struct msm_compr_audio *prtd = NULL;
  4563. struct msm_pcm_channel_mixer *chmixer_pspd = NULL;
  4564. u8 asm_ch_map[PCM_FORMAT_MAX_NUM_CHANNEL_V8] = {0};
  4565. bool reset_override_out_ch_map = false;
  4566. bool reset_override_in_ch_map = false;
  4567. if ((session_type != SESSION_TYPE_TX) &&
  4568. (session_type != SESSION_TYPE_RX)) {
  4569. pr_err("%s: invalid session type %d\n", __func__, session_type);
  4570. return -EINVAL;
  4571. }
  4572. chmixer_pspd = msm_compr_get_chmixer(pdata, fe_id);
  4573. if (!chmixer_pspd) {
  4574. pr_err("%s: invalid chmixer_pspd in pdata", __func__);
  4575. return -EINVAL;
  4576. }
  4577. chmixer_pspd->enable = ucontrol->value.integer.value[0];
  4578. chmixer_pspd->rule = ucontrol->value.integer.value[1];
  4579. chmixer_pspd->input_channel = ucontrol->value.integer.value[2];
  4580. chmixer_pspd->output_channel = ucontrol->value.integer.value[3];
  4581. chmixer_pspd->port_idx = ucontrol->value.integer.value[4];
  4582. if (chmixer_pspd->input_channel < 0 ||
  4583. chmixer_pspd->input_channel > PCM_FORMAT_MAX_NUM_CHANNEL_V8 ||
  4584. chmixer_pspd->output_channel < 0 ||
  4585. chmixer_pspd->output_channel > PCM_FORMAT_MAX_NUM_CHANNEL_V8) {
  4586. pr_err("%s: Invalid channels, in %d, out %d\n",
  4587. __func__, chmixer_pspd->input_channel,
  4588. chmixer_pspd->output_channel);
  4589. return -EINVAL;
  4590. }
  4591. if (chmixer_pspd->enable) {
  4592. if (session_type == SESSION_TYPE_RX &&
  4593. !chmixer_pspd->override_in_ch_map) {
  4594. if (pdata->ch_map[fe_id]->set_ch_map) {
  4595. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  4596. chmixer_pspd->in_ch_map[i] =
  4597. pdata->ch_map[fe_id]->channel_map[i];
  4598. } else {
  4599. q6asm_map_channels(asm_ch_map,
  4600. chmixer_pspd->input_channel, false);
  4601. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  4602. chmixer_pspd->in_ch_map[i] = asm_ch_map[i];
  4603. }
  4604. chmixer_pspd->override_in_ch_map = true;
  4605. reset_override_in_ch_map = true;
  4606. } else if (session_type == SESSION_TYPE_TX &&
  4607. !chmixer_pspd->override_out_ch_map) {
  4608. if (pdata->ch_map[fe_id]->set_ch_map) {
  4609. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  4610. chmixer_pspd->out_ch_map[i] =
  4611. pdata->ch_map[fe_id]->channel_map[i];
  4612. } else {
  4613. q6asm_map_channels(asm_ch_map,
  4614. chmixer_pspd->output_channel, false);
  4615. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  4616. chmixer_pspd->out_ch_map[i] = asm_ch_map[i];
  4617. }
  4618. chmixer_pspd->override_out_ch_map = true;
  4619. reset_override_out_ch_map = true;
  4620. }
  4621. } else {
  4622. chmixer_pspd->override_out_ch_map = false;
  4623. chmixer_pspd->override_in_ch_map = false;
  4624. }
  4625. /* cache value and take effect during adm_open stage */
  4626. msm_pcm_routing_set_channel_mixer_cfg(fe_id,
  4627. session_type,
  4628. chmixer_pspd);
  4629. cstream = pdata->cstream[fe_id];
  4630. if (chmixer_pspd->enable && cstream && cstream->runtime) {
  4631. prtd = cstream->runtime->private_data;
  4632. if (prtd && prtd->audio_client) {
  4633. stream_id = prtd->audio_client->session;
  4634. be_id = chmixer_pspd->port_idx;
  4635. msm_pcm_routing_set_channel_mixer_runtime(be_id,
  4636. stream_id, session_type, chmixer_pspd);
  4637. }
  4638. }
  4639. if (reset_override_out_ch_map)
  4640. chmixer_pspd->override_out_ch_map = false;
  4641. if (reset_override_in_ch_map)
  4642. chmixer_pspd->override_in_ch_map = false;
  4643. return ret;
  4644. }
  4645. static int msm_compr_channel_mixer_cfg_ctl_get(struct snd_kcontrol *kcontrol,
  4646. struct snd_ctl_elem_value *ucontrol)
  4647. {
  4648. u64 fe_id = kcontrol->private_value & 0xFF;
  4649. struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
  4650. struct msm_compr_pdata *pdata = (struct msm_compr_pdata *)
  4651. snd_soc_component_get_drvdata(comp);
  4652. struct msm_pcm_channel_mixer *chmixer_pspd;
  4653. chmixer_pspd = msm_compr_get_chmixer(pdata, fe_id);
  4654. if (!chmixer_pspd) {
  4655. pr_err("%s: invalid chmixer_pspd in pdata", __func__);
  4656. return -EINVAL;
  4657. }
  4658. ucontrol->value.integer.value[0] = chmixer_pspd->enable;
  4659. ucontrol->value.integer.value[1] = chmixer_pspd->rule;
  4660. ucontrol->value.integer.value[2] = chmixer_pspd->input_channel;
  4661. ucontrol->value.integer.value[3] = chmixer_pspd->output_channel;
  4662. ucontrol->value.integer.value[4] = chmixer_pspd->port_idx;
  4663. return 0;
  4664. }
  4665. static int msm_compr_channel_mixer_output_map_ctl_put(
  4666. struct snd_kcontrol *kcontrol,
  4667. struct snd_ctl_elem_value *ucontrol)
  4668. {
  4669. u64 fe_id = kcontrol->private_value & 0xFF;
  4670. int i = 0;
  4671. struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
  4672. struct msm_compr_pdata *pdata = (struct msm_compr_pdata *)
  4673. snd_soc_component_get_drvdata(comp);
  4674. struct msm_pcm_channel_mixer *chmixer_pspd;
  4675. chmixer_pspd = msm_compr_get_chmixer(pdata, fe_id);
  4676. if (!chmixer_pspd) {
  4677. pr_err("%s: invalid chmixer_pspd in pdata", __func__);
  4678. return -EINVAL;
  4679. }
  4680. chmixer_pspd->override_out_ch_map = true;
  4681. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  4682. chmixer_pspd->out_ch_map[i] =
  4683. ucontrol->value.integer.value[i];
  4684. return 0;
  4685. }
  4686. static int msm_compr_channel_mixer_output_map_ctl_get(
  4687. struct snd_kcontrol *kcontrol,
  4688. struct snd_ctl_elem_value *ucontrol)
  4689. {
  4690. u64 fe_id = kcontrol->private_value & 0xFF;
  4691. int i = 0;
  4692. struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
  4693. struct msm_compr_pdata *pdata = (struct msm_compr_pdata *)
  4694. snd_soc_component_get_drvdata(comp);
  4695. struct msm_pcm_channel_mixer *chmixer_pspd;
  4696. chmixer_pspd = msm_compr_get_chmixer(pdata, fe_id);
  4697. if (!chmixer_pspd) {
  4698. pr_err("%s: invalid chmixer_pspd in pdata", __func__);
  4699. return -EINVAL;
  4700. }
  4701. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  4702. ucontrol->value.integer.value[i] =
  4703. chmixer_pspd->out_ch_map[i];
  4704. return 0;
  4705. }
  4706. static int msm_compr_channel_mixer_input_map_ctl_put(
  4707. struct snd_kcontrol *kcontrol,
  4708. struct snd_ctl_elem_value *ucontrol)
  4709. {
  4710. u64 fe_id = kcontrol->private_value & 0xFF;
  4711. int i = 0;
  4712. struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
  4713. struct msm_compr_pdata *pdata = (struct msm_compr_pdata *)
  4714. snd_soc_component_get_drvdata(comp);
  4715. struct msm_pcm_channel_mixer *chmixer_pspd;
  4716. chmixer_pspd = msm_compr_get_chmixer(pdata, fe_id);
  4717. if (!chmixer_pspd) {
  4718. pr_err("%s: invalid chmixer_pspd in pdata", __func__);
  4719. return -EINVAL;
  4720. }
  4721. chmixer_pspd->override_in_ch_map = true;
  4722. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  4723. chmixer_pspd->in_ch_map[i] = ucontrol->value.integer.value[i];
  4724. return 0;
  4725. }
  4726. static int msm_compr_channel_mixer_input_map_ctl_get(
  4727. struct snd_kcontrol *kcontrol,
  4728. struct snd_ctl_elem_value *ucontrol)
  4729. {
  4730. u64 fe_id = kcontrol->private_value & 0xFF;
  4731. int i = 0;
  4732. struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
  4733. struct msm_compr_pdata *pdata = (struct msm_compr_pdata *)
  4734. snd_soc_component_get_drvdata(comp);
  4735. struct msm_pcm_channel_mixer *chmixer_pspd;
  4736. chmixer_pspd = msm_compr_get_chmixer(pdata, fe_id);
  4737. if (!chmixer_pspd) {
  4738. pr_err("%s: invalid chmixer_pspd in pdata", __func__);
  4739. return -EINVAL;
  4740. }
  4741. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  4742. ucontrol->value.integer.value[i] =
  4743. chmixer_pspd->in_ch_map[i];
  4744. return 0;
  4745. }
  4746. static int msm_compr_channel_mixer_weight_ctl_put(
  4747. struct snd_kcontrol *kcontrol,
  4748. struct snd_ctl_elem_value *ucontrol)
  4749. {
  4750. u64 fe_id = kcontrol->private_value & 0xFF;
  4751. int channel = (kcontrol->private_value >> 16) & 0xFF;
  4752. int i = 0;
  4753. struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
  4754. struct msm_compr_pdata *pdata = (struct msm_compr_pdata *)
  4755. snd_soc_component_get_drvdata(comp);
  4756. struct msm_pcm_channel_mixer *chmixer_pspd;
  4757. chmixer_pspd = msm_compr_get_chmixer(pdata, fe_id);
  4758. if (!chmixer_pspd) {
  4759. pr_err("%s: invalid chmixer_pspd in pdata", __func__);
  4760. return -EINVAL;
  4761. }
  4762. if (channel <= 0 || channel > PCM_FORMAT_MAX_NUM_CHANNEL_V8) {
  4763. pr_err("%s: invalid channel number %d\n", __func__, channel);
  4764. return -EINVAL;
  4765. }
  4766. channel--;
  4767. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  4768. chmixer_pspd->channel_weight[channel][i] =
  4769. ucontrol->value.integer.value[i];
  4770. return 0;
  4771. }
  4772. static int msm_compr_channel_mixer_weight_ctl_get(
  4773. struct snd_kcontrol *kcontrol,
  4774. struct snd_ctl_elem_value *ucontrol)
  4775. {
  4776. u64 fe_id = kcontrol->private_value & 0xFF;
  4777. int channel = (kcontrol->private_value >> 16) & 0xFF;
  4778. struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
  4779. struct msm_compr_pdata *pdata = (struct msm_compr_pdata *)
  4780. snd_soc_component_get_drvdata(comp);
  4781. int i = 0;
  4782. struct msm_pcm_channel_mixer *chmixer_pspd;
  4783. if (channel <= 0 || channel > PCM_FORMAT_MAX_NUM_CHANNEL_V8) {
  4784. pr_err("%s: invalid channel number %d\n", __func__, channel);
  4785. return -EINVAL;
  4786. }
  4787. channel--;
  4788. chmixer_pspd = msm_compr_get_chmixer(pdata, fe_id);
  4789. if (!chmixer_pspd) {
  4790. pr_err("%s: invalid chmixer_pspd in pdata", __func__);
  4791. return -EINVAL;
  4792. }
  4793. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  4794. ucontrol->value.integer.value[i] =
  4795. chmixer_pspd->channel_weight[channel][i];
  4796. return 0;
  4797. }
  4798. static int msm_compr_add_platform_controls(struct snd_kcontrol_new *kctl,
  4799. struct snd_soc_pcm_runtime *rtd, const char *name_prefix,
  4800. const char *name_suffix, int session_type, int channels)
  4801. {
  4802. int ret = -EINVAL;
  4803. char *mixer_name = NULL;
  4804. const char *deviceNo = "NN";
  4805. const char *channelNo = "NN";
  4806. int ctl_len = 0;
  4807. struct snd_soc_component *component = NULL;
  4808. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  4809. if (!component) {
  4810. pr_err("%s: component is NULL\n", __func__);
  4811. return -EINVAL;
  4812. }
  4813. ctl_len = strlen(name_prefix) + 1 + strlen(deviceNo) + 1 +
  4814. strlen(channelNo) + 1 + strlen(name_suffix) + 1;
  4815. mixer_name = kzalloc(ctl_len, GFP_KERNEL);
  4816. if (mixer_name == NULL)
  4817. return -ENOMEM;
  4818. if (channels >= 0) {
  4819. snprintf(mixer_name, ctl_len, "%s %d %s %d",
  4820. name_prefix, rtd->pcm->device, name_suffix, channels);
  4821. kctl->private_value = (rtd->dai_link->id) | (channels << 16);
  4822. } else {
  4823. snprintf(mixer_name, ctl_len, "%s %d %s",
  4824. name_prefix, rtd->pcm->device, name_suffix);
  4825. kctl->private_value = (rtd->dai_link->id);
  4826. }
  4827. if (session_type != INVALID_SESSION)
  4828. kctl->private_value |= (session_type << 8);
  4829. kctl->name = mixer_name;
  4830. ret = snd_soc_add_component_controls(component, kctl, 1);
  4831. kfree(mixer_name);
  4832. return ret;
  4833. }
  4834. static int msm_compr_channel_mixer_output_map_info(struct snd_kcontrol *kcontrol,
  4835. struct snd_ctl_elem_info *uinfo)
  4836. {
  4837. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  4838. uinfo->count = PCM_FORMAT_MAX_NUM_CHANNEL_V8;
  4839. /* Valid channel map value ranges from 1 to 64 */
  4840. uinfo->value.integer.min = 1;
  4841. uinfo->value.integer.max = 64;
  4842. return 0;
  4843. }
  4844. static int msm_compr_add_channel_mixer_output_map_controls(
  4845. struct snd_soc_pcm_runtime *rtd)
  4846. {
  4847. const char *playback_mixer_ctl_name = "AudStr";
  4848. const char *capture_mixer_ctl_name = "AudStr Capture";
  4849. const char *suffix = "ChMixer Output Map";
  4850. const char *mixer_ctl_name = NULL;
  4851. int ret = 0, session_type = INVALID_SESSION, channel = -1;
  4852. struct snd_kcontrol_new channel_mixer_output_map_control = {
  4853. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  4854. .name = "?",
  4855. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  4856. .info = msm_compr_channel_mixer_output_map_info,
  4857. .put = msm_compr_channel_mixer_output_map_ctl_put,
  4858. .get = msm_compr_channel_mixer_output_map_ctl_get,
  4859. .private_value = 0,
  4860. };
  4861. mixer_ctl_name = rtd->compr->direction == SND_COMPRESS_PLAYBACK ?
  4862. playback_mixer_ctl_name : capture_mixer_ctl_name ;
  4863. ret = msm_compr_add_platform_controls(&channel_mixer_output_map_control,
  4864. rtd, mixer_ctl_name, suffix, session_type, channel);
  4865. if (ret < 0) {
  4866. pr_err("%s: failed add platform ctl, err = %d\n",
  4867. __func__, ret);
  4868. }
  4869. return ret;
  4870. }
  4871. static int msm_compr_channel_mixer_input_map_info(struct snd_kcontrol *kcontrol,
  4872. struct snd_ctl_elem_info *uinfo)
  4873. {
  4874. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  4875. uinfo->count = PCM_FORMAT_MAX_NUM_CHANNEL_V8;
  4876. /* Valid channel map value ranges from 1 to 64 */
  4877. uinfo->value.integer.min = 1;
  4878. uinfo->value.integer.max = 64;
  4879. return 0;
  4880. }
  4881. static int msm_compr_add_channel_mixer_input_map_controls(
  4882. struct snd_soc_pcm_runtime *rtd)
  4883. {
  4884. const char *playback_mixer_ctl_name = "AudStr";
  4885. const char *capture_mixer_ctl_name = "AudStr Capture";
  4886. const char *suffix = "ChMixer Input Map";
  4887. const char *mixer_ctl_name = NULL;
  4888. int ret = 0, session_type = INVALID_SESSION, channel = -1;
  4889. struct snd_kcontrol_new channel_mixer_input_map_control = {
  4890. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  4891. .name = "?",
  4892. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  4893. .info = msm_compr_channel_mixer_input_map_info,
  4894. .put = msm_compr_channel_mixer_input_map_ctl_put,
  4895. .get = msm_compr_channel_mixer_input_map_ctl_get,
  4896. .private_value = 0,
  4897. };
  4898. mixer_ctl_name = rtd->compr->direction == SND_COMPRESS_PLAYBACK ?
  4899. playback_mixer_ctl_name : capture_mixer_ctl_name ;
  4900. ret = msm_compr_add_platform_controls(&channel_mixer_input_map_control,
  4901. rtd, mixer_ctl_name, suffix, session_type, channel);
  4902. if (ret < 0) {
  4903. pr_err("%s: failed add platform ctl, err = %d\n",
  4904. __func__, ret);
  4905. }
  4906. return ret;
  4907. }
  4908. static int msm_compr_channel_mixer_cfg_info(struct snd_kcontrol *kcontrol,
  4909. struct snd_ctl_elem_info *uinfo)
  4910. {
  4911. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  4912. /* five int values: enable, rule, in_channels, out_channels and port_id */
  4913. uinfo->count = 5;
  4914. /* Valid range is all positive values to support above controls */
  4915. uinfo->value.integer.min = 0;
  4916. uinfo->value.integer.max = INT_MAX;
  4917. return 0;
  4918. }
  4919. static int msm_compr_add_channel_mixer_cfg_controls(
  4920. struct snd_soc_pcm_runtime *rtd)
  4921. {
  4922. const char *playback_mixer_ctl_name = "AudStr";
  4923. const char *capture_mixer_ctl_name = "AudStr Capture";
  4924. const char *suffix = "ChMixer Cfg";
  4925. const char *mixer_ctl_name = NULL;
  4926. int ret = 0, session_type = INVALID_SESSION, channel = -1;
  4927. struct snd_kcontrol_new channel_mixer_cfg_control = {
  4928. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  4929. .name = "?",
  4930. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  4931. .info = msm_compr_channel_mixer_cfg_info,
  4932. .put = msm_compr_channel_mixer_cfg_ctl_put,
  4933. .get = msm_compr_channel_mixer_cfg_ctl_get,
  4934. .private_value = 0,
  4935. };
  4936. if (rtd->compr->direction == SND_COMPRESS_PLAYBACK) {
  4937. session_type = SESSION_TYPE_RX;
  4938. mixer_ctl_name = playback_mixer_ctl_name;
  4939. } else {
  4940. session_type = SESSION_TYPE_TX;
  4941. mixer_ctl_name = capture_mixer_ctl_name;
  4942. }
  4943. ret = msm_compr_add_platform_controls(&channel_mixer_cfg_control,
  4944. rtd, mixer_ctl_name, suffix, session_type, channel);
  4945. if (ret < 0) {
  4946. pr_err("%s: failed add platform ctl, err = %d\n",
  4947. __func__, ret);
  4948. }
  4949. return ret;
  4950. }
  4951. static int msm_compr_channel_mixer_weight_info(struct snd_kcontrol *kcontrol,
  4952. struct snd_ctl_elem_info *uinfo)
  4953. {
  4954. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  4955. uinfo->count = PCM_FORMAT_MAX_NUM_CHANNEL_V8;
  4956. /* Valid range: 0 to 0x4000(Unity) gain weightage */
  4957. uinfo->value.integer.min = 0;
  4958. uinfo->value.integer.max = 0x4000;
  4959. return 0;
  4960. }
  4961. static int msm_compr_add_channel_mixer_weight_controls(
  4962. struct snd_soc_pcm_runtime *rtd,
  4963. int channel)
  4964. {
  4965. const char *playback_mixer_ctl_name = "AudStr";
  4966. const char *capture_mixer_ctl_name = "AudStr Capture";
  4967. const char *suffix = "ChMixer Weight Ch";
  4968. const char *mixer_ctl_name = NULL;
  4969. int ret = 0, session_type = INVALID_SESSION;
  4970. struct snd_kcontrol_new channel_mixer_weight_control = {
  4971. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  4972. .name = "?",
  4973. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  4974. .info = msm_compr_channel_mixer_weight_info,
  4975. .put = msm_compr_channel_mixer_weight_ctl_put,
  4976. .get = msm_compr_channel_mixer_weight_ctl_get,
  4977. .private_value = 0,
  4978. };
  4979. mixer_ctl_name = rtd->compr->direction == SND_COMPRESS_PLAYBACK ?
  4980. playback_mixer_ctl_name : capture_mixer_ctl_name ;
  4981. ret = msm_compr_add_platform_controls(&channel_mixer_weight_control,
  4982. rtd, mixer_ctl_name, suffix, session_type, channel);
  4983. if (ret < 0) {
  4984. pr_err("%s: failed add platform ctl, err = %d\n",
  4985. __func__, ret);
  4986. }
  4987. return ret;
  4988. }
  4989. static int msm_compr_add_channel_mixer_controls(struct snd_soc_pcm_runtime *rtd)
  4990. {
  4991. int i, ret = 0;
  4992. struct msm_compr_pdata *pdata = NULL;
  4993. struct snd_soc_component *component = NULL;
  4994. if (!rtd) {
  4995. pr_err("%s NULL rtd\n", __func__);
  4996. return -EINVAL;
  4997. }
  4998. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  4999. if (!component) {
  5000. pr_err("%s: component is NULL\n", __func__);
  5001. return -EINVAL;
  5002. }
  5003. pdata = (struct msm_compr_pdata *)
  5004. snd_soc_component_get_drvdata(component);
  5005. if (!pdata) {
  5006. pr_err("%s: platform data not populated\n", __func__);
  5007. return -EINVAL;
  5008. }
  5009. if (!pdata->chmixer_pspd[rtd->dai_link->id]) {
  5010. pdata->chmixer_pspd[rtd->dai_link->id] =
  5011. kzalloc(sizeof(struct msm_pcm_channel_mixer), GFP_KERNEL);
  5012. if (!pdata->chmixer_pspd[rtd->dai_link->id])
  5013. return -ENOMEM;
  5014. }
  5015. ret = msm_compr_add_channel_mixer_cfg_controls(rtd);
  5016. if (ret) {
  5017. pr_err("%s: pcm add channel mixer cfg controls failed:%d\n",
  5018. __func__, ret);
  5019. goto fail;
  5020. }
  5021. ret = msm_compr_add_channel_mixer_input_map_controls(rtd);
  5022. if (ret) {
  5023. pr_err("%s: pcm add channel mixer input map controls failed:%d\n",
  5024. __func__, ret);
  5025. goto fail;
  5026. }
  5027. ret = msm_compr_add_channel_mixer_output_map_controls(rtd);
  5028. if (ret) {
  5029. pr_err("%s: pcm add channel mixer output map controls failed:%d\n",
  5030. __func__, ret);
  5031. goto fail;
  5032. }
  5033. for (i = 1; i <= PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++) {
  5034. ret = msm_compr_add_channel_mixer_weight_controls(rtd, i);
  5035. if (ret) {
  5036. pr_err("%s: pcm add channel mixer weight controls failed:%d\n",
  5037. __func__, ret);
  5038. goto fail;
  5039. }
  5040. }
  5041. return 0;
  5042. fail:
  5043. kfree(pdata->chmixer_pspd[rtd->dai_link->id]);
  5044. pdata->chmixer_pspd[rtd->dai_link->id] = NULL;
  5045. return ret;
  5046. }
  5047. int msm_compr_new(struct snd_soc_pcm_runtime *rtd, int num)
  5048. {
  5049. int rc = 0;
  5050. if (rtd == NULL) {
  5051. pr_err("%s: RTD is NULL\n", __func__);
  5052. return 0;
  5053. }
  5054. rc = snd_soc_new_compress(rtd, num);
  5055. if (rc)
  5056. pr_err("%s: Fail to create pcm for compress\n", __func__);
  5057. rc = msm_compr_add_volume_control(rtd);
  5058. if (rc)
  5059. pr_err("%s: Could not add Compr Volume Control\n", __func__);
  5060. rc = msm_compr_add_audio_effects_control(rtd);
  5061. if (rc)
  5062. pr_err("%s: Could not add Compr Audio Effects Control\n",
  5063. __func__);
  5064. rc = msm_compr_add_audio_adsp_stream_cmd_control(rtd);
  5065. if (rc)
  5066. pr_err("%s: Could not add Compr ADSP Stream Cmd Control\n",
  5067. __func__);
  5068. rc = msm_compr_add_audio_adsp_stream_callback_control(rtd);
  5069. if (rc)
  5070. pr_err("%s: Could not add Compr ADSP Stream Callback Control\n",
  5071. __func__);
  5072. rc = msm_compr_add_io_fd_cmd_control(rtd);
  5073. if (rc)
  5074. pr_err("%s: Could not add Compr ion fd Control\n",
  5075. __func__);
  5076. rc = msm_compr_add_event_ack_cmd_control(rtd);
  5077. if (rc)
  5078. pr_err("%s: Could not add Compr event ack Control\n",
  5079. __func__);
  5080. rc = msm_compr_add_query_audio_effect_control(rtd);
  5081. if (rc)
  5082. pr_err("%s: Could not add Compr Query Audio Effect Control\n",
  5083. __func__);
  5084. rc = msm_compr_add_dec_runtime_params_control(rtd);
  5085. if (rc)
  5086. pr_err("%s: Could not add Compr Dec runtime params Control\n",
  5087. __func__);
  5088. rc = msm_compr_add_app_type_cfg_control(rtd);
  5089. if (rc)
  5090. pr_err("%s: Could not add Compr App Type Cfg Control\n",
  5091. __func__);
  5092. rc = msm_compr_add_channel_map_control(rtd);
  5093. if (rc)
  5094. pr_err("%s: Could not add Compr Channel Map Control\n",
  5095. __func__);
  5096. rc = msm_compr_add_channel_mixer_controls(rtd);
  5097. if (rc)
  5098. pr_err("%s: Could not add Compr Channel Mixer Controls\n",
  5099. __func__);
  5100. return 0;
  5101. }
  5102. EXPORT_SYMBOL(msm_compr_new);
  5103. static struct snd_compr_ops msm_compr_ops = {
  5104. .open = msm_compr_open,
  5105. .free = msm_compr_free,
  5106. .trigger = msm_compr_trigger,
  5107. .pointer = msm_compr_pointer,
  5108. .set_params = msm_compr_set_params,
  5109. .set_metadata = msm_compr_set_metadata,
  5110. .get_metadata = msm_compr_get_metadata,
  5111. #if IS_ENABLED(CONFIG_AUDIO_QGKI)
  5112. .set_next_track_param = msm_compr_set_next_track_param,
  5113. #endif /* CONFIG_AUDIO_QGKI */
  5114. .ack = msm_compr_ack,
  5115. .copy = msm_compr_copy,
  5116. .get_caps = msm_compr_get_caps,
  5117. .get_codec_caps = msm_compr_get_codec_caps,
  5118. };
  5119. static struct snd_soc_component_driver msm_soc_component = {
  5120. .name = DRV_NAME,
  5121. .probe = msm_compr_probe,
  5122. .compr_ops = &msm_compr_ops,
  5123. };
  5124. static int msm_compr_dev_probe(struct platform_device *pdev)
  5125. {
  5126. struct msm_compr_pdata *pdata = NULL;
  5127. pr_debug("%s: dev name %s\n", __func__, dev_name(&pdev->dev));
  5128. pdata = (struct msm_compr_pdata *)
  5129. kzalloc(sizeof(*pdata), GFP_KERNEL);
  5130. if (!pdata)
  5131. return -ENOMEM;
  5132. mutex_init(&pdata->lock);
  5133. dev_set_drvdata(&pdev->dev, pdata);
  5134. return snd_soc_register_component(&pdev->dev,
  5135. &msm_soc_component, NULL, 0);
  5136. }
  5137. static int msm_compr_remove(struct platform_device *pdev)
  5138. {
  5139. int i = 0;
  5140. struct msm_compr_pdata *pdata = NULL;
  5141. pdata = dev_get_drvdata(&pdev->dev);
  5142. if (pdata) {
  5143. for (i = 0; i < MSM_FRONTEND_DAI_MM_SIZE; i++)
  5144. kfree(pdata->chmixer_pspd[i]);
  5145. }
  5146. mutex_destroy(&pdata->lock);
  5147. kfree(pdata);
  5148. snd_soc_unregister_component(&pdev->dev);
  5149. return 0;
  5150. }
  5151. static const struct of_device_id msm_compr_dt_match[] = {
  5152. {.compatible = "qcom,msm-compress-dsp"},
  5153. {}
  5154. };
  5155. MODULE_DEVICE_TABLE(of, msm_compr_dt_match);
  5156. static struct platform_driver msm_compr_driver = {
  5157. .driver = {
  5158. .name = "msm-compress-dsp",
  5159. .owner = THIS_MODULE,
  5160. .of_match_table = msm_compr_dt_match,
  5161. .suppress_bind_attrs = true,
  5162. },
  5163. .probe = msm_compr_dev_probe,
  5164. .remove = msm_compr_remove,
  5165. };
  5166. int __init msm_compress_dsp_init(void)
  5167. {
  5168. return platform_driver_register(&msm_compr_driver);
  5169. }
  5170. void msm_compress_dsp_exit(void)
  5171. {
  5172. platform_driver_unregister(&msm_compr_driver);
  5173. }
  5174. MODULE_DESCRIPTION("Compress Offload platform driver");
  5175. MODULE_LICENSE("GPL v2");