msm-compress-q6-v2.c 166 KB

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