msm-compress-q6-v2.c 161 KB

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