msm-compress-q6-v2.c 160 KB

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