msm-compress-q6-v2.c 159 KB

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