msm-compress-q6-v2.c 166 KB

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