msm-compress-q6-v2.c 155 KB

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