msm-compress-q6-v2.c 156 KB

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