msm-compress-q6-v2.c 160 KB

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