12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045
#include <linux/module.h>
#include <linux/init.h>
#include <linux/async.h>
#include <linux/cleanup.h>
#include <linux/delay.h>
#include <linux/pm.h>
#include <linux/bitops.h>
#include <linux/platform_device.h>
#include <linux/jiffies.h>
#include <linux/debugfs.h>
#include <linux/pm_runtime.h>
#include <linux/regulator/consumer.h>
#include <linux/pinctrl/consumer.h>
#include <linux/clk.h>
#include <linux/slab.h>
#include <sound/core.h>
#include <sound/pcm.h>
#include <sound/pcm_params.h>
#include <sound/soc.h>
#include <sound/initval.h>
#include "soc-internal.h"
#include <trace/events/asoc.h>
static u32 pop_time;
struct snd_soc_dapm_context {
enum snd_soc_bias_level bias_level;
bool idle_bias;
struct snd_soc_component *component;
struct snd_soc_card *card;
enum snd_soc_bias_level target_bias_level;
struct list_head list;
struct snd_soc_dapm_widget *wcache_sink;
struct snd_soc_dapm_widget *wcache_source;
#ifdef CONFIG_DEBUG_FS
struct dentry *debugfs_dapm;
#endif
};
#define DAPM_UPDATE_STAT(widget, val) widget->dapm->card->dapm_stats.val++;
#define DAPM_DIR_REVERSE(x) ((x == SND_SOC_DAPM_DIR_IN) ? \
SND_SOC_DAPM_DIR_OUT : SND_SOC_DAPM_DIR_IN)
#define dapm_for_each_direction(dir) \
for ((dir) = SND_SOC_DAPM_DIR_IN; (dir) <= SND_SOC_DAPM_DIR_OUT; \
(dir)++)
static int dapm_up_seq[] = {
[snd_soc_dapm_pre] = 1,
[snd_soc_dapm_regulator_supply] = 2,
[snd_soc_dapm_pinctrl] = 2,
[snd_soc_dapm_clock_supply] = 2,
[snd_soc_dapm_supply] = 3,
[snd_soc_dapm_dai_link] = 3,
[snd_soc_dapm_micbias] = 4,
[snd_soc_dapm_vmid] = 4,
[snd_soc_dapm_dai_in] = 5,
[snd_soc_dapm_dai_out] = 5,
[snd_soc_dapm_aif_in] = 5,
[snd_soc_dapm_aif_out] = 5,
[snd_soc_dapm_mic] = 6,
[snd_soc_dapm_siggen] = 6,
[snd_soc_dapm_input] = 6,
[snd_soc_dapm_output] = 6,
[snd_soc_dapm_mux] = 7,
[snd_soc_dapm_mux_named_ctl] = 7,
[snd_soc_dapm_demux] = 7,
[snd_soc_dapm_dac] = 8,
[snd_soc_dapm_switch] = 9,
[snd_soc_dapm_mixer] = 9,
[snd_soc_dapm_mixer_named_ctl] = 9,
[snd_soc_dapm_pga] = 10,
[snd_soc_dapm_buffer] = 10,
[snd_soc_dapm_scheduler] = 10,
[snd_soc_dapm_effect] = 10,
[snd_soc_dapm_src] = 10,
[snd_soc_dapm_asrc] = 10,
[snd_soc_dapm_encoder] = 10,
[snd_soc_dapm_decoder] = 10,
[snd_soc_dapm_adc] = 11,
[snd_soc_dapm_out_drv] = 12,
[snd_soc_dapm_hp] = 12,
[snd_soc_dapm_line] = 12,
[snd_soc_dapm_sink] = 12,
[snd_soc_dapm_spk] = 13,
[snd_soc_dapm_kcontrol] = 14,
[snd_soc_dapm_post] = 15,
};
static int dapm_down_seq[] = {
[snd_soc_dapm_pre] = 1,
[snd_soc_dapm_kcontrol] = 2,
[snd_soc_dapm_adc] = 3,
[snd_soc_dapm_spk] = 4,
[snd_soc_dapm_hp] = 5,
[snd_soc_dapm_line] = 5,
[snd_soc_dapm_out_drv] = 5,
[snd_soc_dapm_sink] = 6,
[snd_soc_dapm_pga] = 6,
[snd_soc_dapm_buffer] = 6,
[snd_soc_dapm_scheduler] = 6,
[snd_soc_dapm_effect] = 6,
[snd_soc_dapm_src] = 6,
[snd_soc_dapm_asrc] = 6,
[snd_soc_dapm_encoder] = 6,
[snd_soc_dapm_decoder] = 6,
[snd_soc_dapm_switch] = 7,
[snd_soc_dapm_mixer_named_ctl] = 7,
[snd_soc_dapm_mixer] = 7,
[snd_soc_dapm_dac] = 8,
[snd_soc_dapm_mic] = 9,
[snd_soc_dapm_siggen] = 9,
[snd_soc_dapm_input] = 9,
[snd_soc_dapm_output] = 9,
[snd_soc_dapm_micbias] = 10,
[snd_soc_dapm_vmid] = 10,
[snd_soc_dapm_mux] = 11,
[snd_soc_dapm_mux_named_ctl] = 11,
[snd_soc_dapm_demux] = 11,
[snd_soc_dapm_aif_in] = 12,
[snd_soc_dapm_aif_out] = 12,
[snd_soc_dapm_dai_in] = 12,
[snd_soc_dapm_dai_out] = 12,
[snd_soc_dapm_dai_link] = 13,
[snd_soc_dapm_supply] = 14,
[snd_soc_dapm_clock_supply] = 15,
[snd_soc_dapm_pinctrl] = 15,
[snd_soc_dapm_regulator_supply] = 15,
[snd_soc_dapm_post] = 16,
};
static void dapm_assert_locked(struct snd_soc_dapm_context *dapm)
{
if (snd_soc_card_is_instantiated(dapm->card))
snd_soc_dapm_mutex_assert_held(dapm);
}
static void dapm_pop_wait(void)
{
if (pop_time)
schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
}
__printf(2, 3)
static void dapm_pop_dbg(struct device *dev, const char *fmt, ...)
{
va_list args;
char *buf;
if (!pop_time)
return;
buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
if (buf == NULL)
return;
va_start(args, fmt);
vsnprintf(buf, PAGE_SIZE, fmt, args);
dev_info(dev, "%s", buf);
va_end(args);
kfree(buf);
}
struct snd_soc_dapm_context *snd_soc_dapm_alloc(struct device *dev)
{
return devm_kzalloc_obj(dev, struct snd_soc_dapm_context);
}
struct device *snd_soc_dapm_to_dev(struct snd_soc_dapm_context *dapm)
{
if (dapm->component)
return dapm->component->dev;
return dapm->card->dev;
}
EXPORT_SYMBOL_GPL(snd_soc_dapm_to_dev);
struct snd_soc_card *snd_soc_dapm_to_card(struct snd_soc_dapm_context *dapm)
{
return dapm->card;
}
EXPORT_SYMBOL_GPL(snd_soc_dapm_to_card);
struct snd_soc_component *snd_soc_dapm_to_component(struct snd_soc_dapm_context *dapm)
{
return dapm->component;
}
EXPORT_SYMBOL_GPL(snd_soc_dapm_to_component);
static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
{
return !list_empty(&w->dirty);
}
static void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
{
struct device *dev = snd_soc_dapm_to_dev(w->dapm);
dapm_assert_locked(w->dapm);
if (!dapm_dirty_widget(w)) {
dev_vdbg(dev, "Marking %s dirty due to %s\n",
w->name, reason);
list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
}
}
static __always_inline void dapm_widget_invalidate_paths(
struct snd_soc_dapm_widget *w, enum snd_soc_dapm_direction dir)
{
enum snd_soc_dapm_direction rdir = DAPM_DIR_REVERSE(dir);
struct snd_soc_dapm_widget *node;
struct snd_soc_dapm_path *p;
LIST_HEAD(list);
dapm_assert_locked(w->dapm);
if (w->endpoints[dir] == -1)
return;
list_add_tail(&w->work_list, &list);
w->endpoints[dir] = -1;
list_for_each_entry(w, &list, work_list) {
snd_soc_dapm_widget_for_each_path(w, dir, p) {
if (p->is_supply || !p->connect)
continue;
node = p->node[rdir];
if (node->endpoints[dir] != -1) {
node->endpoints[dir] = -1;
list_add_tail(&node->work_list, &list);
}
}
}
}
static void dapm_widget_invalidate_input_paths(struct snd_soc_dapm_widget *w)
{
dapm_widget_invalidate_paths(w, SND_SOC_DAPM_DIR_IN);
}
static void dapm_widget_invalidate_output_paths(struct snd_soc_dapm_widget *w)
{
dapm_widget_invalidate_paths(w, SND_SOC_DAPM_DIR_OUT);
}
static void dapm_path_invalidate(struct snd_soc_dapm_path *p)
{
if (p->is_supply)
return;
if (p->source->endpoints[SND_SOC_DAPM_DIR_IN] != 0)
dapm_widget_invalidate_input_paths(p->sink);
if (p->sink->endpoints[SND_SOC_DAPM_DIR_OUT] != 0)
dapm_widget_invalidate_output_paths(p->source);
}
void snd_soc_dapm_mark_endpoints_dirty(struct snd_soc_card *card)
{
struct snd_soc_dapm_widget *w;
snd_soc_dapm_mutex_lock_root(card);
for_each_card_widgets(card, w) {
if (w->is_ep) {
dapm_mark_dirty(w, "Rechecking endpoints");
if (w->is_ep & SND_SOC_DAPM_EP_SINK)
dapm_widget_invalidate_output_paths(w);
if (w->is_ep & SND_SOC_DAPM_EP_SOURCE)
dapm_widget_invalidate_input_paths(w);
}
}
snd_soc_dapm_mutex_unlock(card);
}
static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
const struct snd_soc_dapm_widget *_widget,
const char *prefix)
{
struct snd_soc_dapm_widget *w __free(kfree) = kmemdup(_widget,
sizeof(*_widget),
GFP_KERNEL);
if (!w)
return NULL;
if (prefix)
w->name = kasprintf(GFP_KERNEL, "%s %s", prefix, _widget->name);
else
w->name = kstrdup_const(_widget->name, GFP_KERNEL);
if (!w->name)
return NULL;
if (_widget->sname) {
w->sname = kstrdup_const(_widget->sname, GFP_KERNEL);
if (!w->sname) {
kfree_const(w->name);
return NULL;
}
}
return_ptr(w);
}
struct dapm_kcontrol_data {
unsigned int value;
struct snd_soc_dapm_widget *widget;
struct list_head paths;
struct snd_soc_dapm_widget_list *wlist;
};
static unsigned int dapm_read(struct snd_soc_dapm_context *dapm, int reg)
{
if (!dapm->component)
return -EIO;
return snd_soc_component_read(dapm->component, reg);
}
static void dapm_set_mixer_path_status(struct snd_soc_dapm_path *p, int i,
int nth_path)
{
struct soc_mixer_control *mc = (struct soc_mixer_control *)
p->sink->kcontrol_news[i].private_value;
unsigned int reg = mc->reg;
unsigned int invert = mc->invert;
if (reg != SND_SOC_NOPM) {
unsigned int shift = mc->shift;
unsigned int max = mc->max;
unsigned int mask = (1 << fls(max)) - 1;
unsigned int val = dapm_read(p->sink->dapm, reg);
if (snd_soc_volsw_is_stereo(mc) && nth_path > 0) {
if (reg != mc->rreg)
val = dapm_read(p->sink->dapm, mc->rreg);
val = (val >> mc->rshift) & mask;
} else {
val = (val >> shift) & mask;
}
if (invert)
val = max - val;
p->connect = !!val;
} else {
p->connect = invert;
}
}
static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
struct snd_soc_dapm_path *path, const char *control_name,
struct snd_soc_dapm_widget *w)
{
const struct snd_kcontrol_new *kcontrol = &w->kcontrol_news[0];
struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
unsigned int item;
int i;
if (e->reg != SND_SOC_NOPM) {
unsigned int val;
val = dapm_read(dapm, e->reg);
val = (val >> e->shift_l) & e->mask;
item = snd_soc_enum_val_to_item(e, val);
} else {
item = 0;
}
i = match_string(e->texts, e->items, control_name);
if (i < 0)
return -ENODEV;
path->name = e->texts[i];
path->connect = (i == item);
return 0;
}
static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
struct snd_soc_dapm_path *path, const char *control_name)
{
int i, nth_path = 0;
for (i = 0; i < path->sink->num_kcontrols; i++) {
if (!strcmp(control_name, path->sink->kcontrol_news[i].name)) {
path->name = path->sink->kcontrol_news[i].name;
dapm_set_mixer_path_status(path, i, nth_path++);
return 0;
}
}
return -ENODEV;
}
static void dapm_update_widget_flags(struct snd_soc_dapm_widget *w)
{
enum snd_soc_dapm_direction dir;
struct snd_soc_dapm_path *p;
unsigned int ep;
switch (w->id) {
case snd_soc_dapm_input:
if (w->dapm->card->fully_routed)
return;
ep = SND_SOC_DAPM_EP_SOURCE;
snd_soc_dapm_widget_for_each_source_path(w, p) {
if (p->source->id == snd_soc_dapm_micbias ||
p->source->id == snd_soc_dapm_mic ||
p->source->id == snd_soc_dapm_line ||
p->source->id == snd_soc_dapm_output) {
ep = 0;
break;
}
}
break;
case snd_soc_dapm_output:
if (w->dapm->card->fully_routed)
return;
ep = SND_SOC_DAPM_EP_SINK;
snd_soc_dapm_widget_for_each_sink_path(w, p) {
if (p->sink->id == snd_soc_dapm_spk ||
p->sink->id == snd_soc_dapm_hp ||
p->sink->id == snd_soc_dapm_line ||
p->sink->id == snd_soc_dapm_input) {
ep = 0;
break;
}
}
break;
case snd_soc_dapm_line:
ep = 0;
dapm_for_each_direction(dir) {
if (!list_empty(&w->edges[dir]))
ep |= SND_SOC_DAPM_DIR_TO_EP(dir);
}
break;
default:
return;
}
w->is_ep = ep;
}
static int dapm_check_dynamic_path(
struct snd_soc_dapm_context *dapm,
struct snd_soc_dapm_widget *source, struct snd_soc_dapm_widget *sink,
const char *control)
{
struct device *dev = snd_soc_dapm_to_dev(dapm);
bool dynamic_source = false;
bool dynamic_sink = false;
if (!control)
return 0;
switch (source->id) {
case snd_soc_dapm_demux:
dynamic_source = true;
break;
default:
break;
}
switch (sink->id) {
case snd_soc_dapm_mux:
case snd_soc_dapm_mux_named_ctl:
case snd_soc_dapm_switch:
case snd_soc_dapm_mixer:
case snd_soc_dapm_mixer_named_ctl:
dynamic_sink = true;
break;
default:
break;
}
if (dynamic_source && dynamic_sink) {
dev_err(dev,
"Direct connection between demux and mixer/mux not supported for path %s -> [%s] -> %s\n",
source->name, control, sink->name);
return -EINVAL;
} else if (!dynamic_source && !dynamic_sink) {
dev_err(dev,
"Control not supported for path %s -> [%s] -> %s\n",
source->name, control, sink->name);
return -EINVAL;
}
return 0;
}
static int dapm_add_path(
struct snd_soc_dapm_context *dapm,
struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
const char *control,
int (*connected)(struct snd_soc_dapm_widget *source,
struct snd_soc_dapm_widget *sink))
{
struct device *dev = snd_soc_dapm_to_dev(dapm);
enum snd_soc_dapm_direction dir;
struct snd_soc_dapm_path *path;
int ret;
if (wsink->is_supply && !wsource->is_supply) {
dev_err(dev,
"Connecting non-supply widget to supply widget is not supported (%s -> %s)\n",
wsource->name, wsink->name);
return -EINVAL;
}
if (connected && !wsource->is_supply) {
dev_err(dev,
"connected() callback only supported for supply widgets (%s -> %s)\n",
wsource->name, wsink->name);
return -EINVAL;
}
if (wsource->is_supply && control) {
dev_err(dev,
"Conditional paths are not supported for supply widgets (%s -> [%s] -> %s)\n",
wsource->name, control, wsink->name);
return -EINVAL;
}
ret = dapm_check_dynamic_path(dapm, wsource, wsink, control);
if (ret)
return ret;
path = kzalloc_obj(struct snd_soc_dapm_path);
if (!path)
return -ENOMEM;
path->node[SND_SOC_DAPM_DIR_IN] = wsource;
path->node[SND_SOC_DAPM_DIR_OUT] = wsink;
path->connected = connected;
INIT_LIST_HEAD(&path->list);
INIT_LIST_HEAD(&path->list_kcontrol);
if (wsource->is_supply || wsink->is_supply)
path->is_supply = 1;
if (control == NULL) {
path->connect = 1;
} else {
switch (wsource->id) {
case snd_soc_dapm_demux:
ret = dapm_connect_mux(dapm, path, control, wsource);
if (ret)
goto err;
break;
default:
break;
}
switch (wsink->id) {
case snd_soc_dapm_mux:
case snd_soc_dapm_mux_named_ctl:
ret = dapm_connect_mux(dapm, path, control, wsink);
if (ret != 0)
goto err;
break;
case snd_soc_dapm_switch:
case snd_soc_dapm_mixer:
case snd_soc_dapm_mixer_named_ctl:
ret = dapm_connect_mixer(dapm, path, control);
if (ret != 0)
goto err;
break;
default:
break;
}
}
list_add(&path->list, &dapm->card->paths);
dapm_for_each_direction(dir)
list_add(&path->list_node[dir], &path->node[dir]->edges[dir]);
dapm_for_each_direction(dir) {
dapm_update_widget_flags(path->node[dir]);
dapm_mark_dirty(path->node[dir], "Route added");
}
if (snd_soc_card_is_instantiated(dapm->card) && path->connect)
dapm_path_invalidate(path);
return 0;
err:
kfree(path);
return ret;
}
static int dapm_kcontrol_data_alloc(struct snd_soc_dapm_widget *widget,
struct snd_kcontrol *kcontrol, const char *ctrl_name)
{
struct device *dev = snd_soc_dapm_to_dev(widget->dapm);
struct dapm_kcontrol_data *data;
struct soc_mixer_control *mc;
struct soc_enum *e;
const char *name;
int ret;
data = kzalloc_obj(*data);
if (!data)
return -ENOMEM;
INIT_LIST_HEAD(&data->paths);
switch (widget->id) {
case snd_soc_dapm_switch:
case snd_soc_dapm_mixer:
case snd_soc_dapm_mixer_named_ctl:
mc = (struct soc_mixer_control *)kcontrol->private_value;
if (mc->autodisable) {
struct snd_soc_dapm_widget template;
if (snd_soc_volsw_is_stereo(mc))
dev_warn(dev,
"ASoC: Unsupported stereo autodisable control '%s'\n",
ctrl_name);
name = kasprintf(GFP_KERNEL, "%s %s", ctrl_name,
"Autodisable");
if (!name) {
ret = -ENOMEM;
goto err_data;
}
memset(&template, 0, sizeof(template));
template.reg = mc->reg;
template.mask = (1 << fls(mc->max)) - 1;
template.shift = mc->shift;
if (mc->invert)
template.off_val = mc->max;
else
template.off_val = 0;
template.on_val = template.off_val;
template.id = snd_soc_dapm_kcontrol;
template.name = name;
data->value = template.on_val;
data->widget =
snd_soc_dapm_new_control_unlocked(widget->dapm,
&template);
kfree(name);
if (IS_ERR(data->widget)) {
ret = PTR_ERR(data->widget);
goto err_data;
}
}
break;
case snd_soc_dapm_demux:
case snd_soc_dapm_mux:
case snd_soc_dapm_mux_named_ctl:
e = (struct soc_enum *)kcontrol->private_value;
if (e->autodisable) {
struct snd_soc_dapm_widget template;
name = kasprintf(GFP_KERNEL, "%s %s", ctrl_name,
"Autodisable");
if (!name) {
ret = -ENOMEM;
goto err_data;
}
memset(&template, 0, sizeof(template));
template.reg = e->reg;
template.mask = e->mask;
template.shift = e->shift_l;
template.off_val = snd_soc_enum_item_to_val(e, 0);
template.on_val = template.off_val;
template.id = snd_soc_dapm_kcontrol;
template.name = name;
data->value = template.on_val;
data->widget = snd_soc_dapm_new_control_unlocked(
widget->dapm, &template);
kfree(name);
if (IS_ERR(data->widget)) {
ret = PTR_ERR(data->widget);
goto err_data;
}
dapm_add_path(widget->dapm, data->widget,
widget, NULL, NULL);
} else if (e->reg != SND_SOC_NOPM) {
data->value = dapm_read(widget->dapm, e->reg) &
(e->mask << e->shift_l);
}
break;
default:
break;
}
kcontrol->private_data = data;
return 0;
err_data:
kfree(data);
return ret;
}
static void dapm_kcontrol_free(struct snd_kcontrol *kctl)
{
struct dapm_kcontrol_data *data = snd_kcontrol_chip(kctl);
list_del(&data->paths);
kfree(data->wlist);
kfree(data);
}
static struct snd_soc_dapm_widget_list *dapm_kcontrol_get_wlist(
const struct snd_kcontrol *kcontrol)
{
struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
return data->wlist;
}
static int dapm_kcontrol_add_widget(struct snd_kcontrol *kcontrol,
struct snd_soc_dapm_widget *widget)
{
struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
struct snd_soc_dapm_widget_list *new_wlist;
unsigned int n;
if (data->wlist)
n = data->wlist->num_widgets + 1;
else
n = 1;
new_wlist = krealloc(data->wlist,
struct_size(new_wlist, widgets, n),
GFP_KERNEL);
if (!new_wlist)
return -ENOMEM;
new_wlist->num_widgets = n;
new_wlist->widgets[n - 1] = widget;
data->wlist = new_wlist;
return 0;
}
static void dapm_kcontrol_add_path(const struct snd_kcontrol *kcontrol,
struct snd_soc_dapm_path *path)
{
struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
list_add_tail(&path->list_kcontrol, &data->paths);
}
static bool dapm_kcontrol_is_powered(const struct snd_kcontrol *kcontrol)
{
struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
if (!data->widget)
return true;
return data->widget->power;
}
static struct list_head *dapm_kcontrol_get_path_list(
const struct snd_kcontrol *kcontrol)
{
struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
return &data->paths;
}
#define dapm_kcontrol_for_each_path(path, kcontrol) \
list_for_each_entry(path, dapm_kcontrol_get_path_list(kcontrol), \
list_kcontrol)
unsigned int snd_soc_dapm_kcontrol_get_value(const struct snd_kcontrol *kcontrol)
{
struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
return data->value;
}
EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_get_value);
static bool dapm_kcontrol_set_value(const struct snd_kcontrol *kcontrol,
unsigned int value)
{
struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
if (data->value == value)
return false;
if (data->widget) {
switch (dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->id) {
case snd_soc_dapm_switch:
case snd_soc_dapm_mixer:
case snd_soc_dapm_mixer_named_ctl:
data->widget->on_val = value & data->widget->mask;
break;
case snd_soc_dapm_demux:
case snd_soc_dapm_mux:
case snd_soc_dapm_mux_named_ctl:
data->widget->on_val = value >> data->widget->shift;
break;
default:
data->widget->on_val = value;
break;
}
}
data->value = value;
return true;
}
struct snd_soc_dapm_widget *snd_soc_dapm_kcontrol_to_widget(struct snd_kcontrol *kcontrol)
{
return dapm_kcontrol_get_wlist(kcontrol)->widgets[0];
}
EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_to_widget);
struct snd_soc_dapm_context *snd_soc_dapm_kcontrol_to_dapm(struct snd_kcontrol *kcontrol)
{
return dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->dapm;
}
EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_to_dapm);
struct snd_soc_component *snd_soc_dapm_kcontrol_to_component(struct snd_kcontrol *kcontrol)
{
return snd_soc_dapm_to_component(snd_soc_dapm_kcontrol_to_dapm(kcontrol));
}
EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_to_component);
static void dapm_reset(struct snd_soc_card *card)
{
struct snd_soc_dapm_widget *w;
snd_soc_dapm_mutex_assert_held(card);
memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
for_each_card_widgets(card, w) {
w->new_power = w->power;
w->power_checked = false;
}
}
static const char *dapm_prefix(struct snd_soc_dapm_context *dapm)
{
if (!dapm->component)
return NULL;
return dapm->component->name_prefix;
}
static int dapm_update_bits(struct snd_soc_dapm_context *dapm,
int reg, unsigned int mask, unsigned int value)
{
if (!dapm->component)
return -EIO;
return snd_soc_component_update_bits(dapm->component, reg,
mask, value);
}
static int dapm_test_bits(struct snd_soc_dapm_context *dapm,
int reg, unsigned int mask, unsigned int value)
{
if (!dapm->component)
return -EIO;
return snd_soc_component_test_bits(dapm->component, reg, mask, value);
}
static void dapm_async_complete(struct snd_soc_dapm_context *dapm)
{
if (dapm->component)
snd_soc_component_async_complete(dapm->component);
}
static struct snd_soc_dapm_widget *
dapm_wcache_lookup(struct snd_soc_dapm_widget *w, const char *name)
{
if (w) {
struct list_head *wlist = &w->dapm->card->widgets;
const int depth = 2;
int i = 0;
list_for_each_entry_from(w, wlist, list) {
if (!strcmp(name, w->name))
return w;
if (++i == depth)
break;
}
}
return NULL;
}
int snd_soc_dapm_force_bias_level(struct snd_soc_dapm_context *dapm,
enum snd_soc_bias_level level)
{
int ret = 0;
if (dapm->component)
ret = snd_soc_component_set_bias_level(dapm->component, level);
if (ret == 0)
dapm->bias_level = level;
return ret;
}
EXPORT_SYMBOL_GPL(snd_soc_dapm_force_bias_level);
void snd_soc_dapm_init_bias_level(struct snd_soc_dapm_context *dapm, enum snd_soc_bias_level level)
{
dapm->bias_level = level;
}
EXPORT_SYMBOL_GPL(snd_soc_dapm_init_bias_level);
static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
enum snd_soc_bias_level level)
{
struct snd_soc_card *card = dapm->card;
int ret = 0;
trace_snd_soc_bias_level_start(dapm, level);
ret = snd_soc_card_set_bias_level(card, dapm, level);
if (ret != 0)
goto out;
if (dapm != card->dapm)
ret = snd_soc_dapm_force_bias_level(dapm, level);
if (ret != 0)
goto out;
ret = snd_soc_card_set_bias_level_post(card, dapm, level);
out:
trace_snd_soc_bias_level_done(dapm, level);
if (ret == 0)
snd_soc_dapm_init_bias_level(dapm, level);
return ret;
}
enum snd_soc_bias_level snd_soc_dapm_get_bias_level(struct snd_soc_dapm_context *dapm)
{
return dapm->bias_level;
}
EXPORT_SYMBOL_GPL(snd_soc_dapm_get_bias_level);
static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
struct snd_soc_dapm_widget *kcontrolw,
const struct snd_kcontrol_new *kcontrol_new,
struct snd_kcontrol **kcontrol)
{
struct snd_soc_dapm_widget *w;
int i;
*kcontrol = NULL;
for_each_card_widgets(dapm->card, w) {
if (w == kcontrolw || w->dapm != kcontrolw->dapm)
continue;
for (i = 0; i < w->num_kcontrols; i++) {
if (&w->kcontrol_news[i] == kcontrol_new) {
if (w->kcontrols)
*kcontrol = w->kcontrols[i];
return 1;
}
}
}
return 0;
}
static int dapm_create_or_share_kcontrol(struct snd_soc_dapm_widget *w,
int kci)
{
struct snd_soc_dapm_context *dapm = w->dapm;
struct device *dev = snd_soc_dapm_to_dev(dapm);
struct snd_card *card = dapm->card->snd_card;
const char *prefix;
size_t prefix_len;
int shared;
struct snd_kcontrol *kcontrol;
bool wname_in_long_name, kcname_in_long_name;
char *long_name = NULL;
const char *name;
int ret = 0;
prefix = dapm_prefix(dapm);
if (prefix)
prefix_len = strlen(prefix) + 1;
else
prefix_len = 0;
shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
&kcontrol);
if (!kcontrol) {
if (shared) {
wname_in_long_name = false;
kcname_in_long_name = true;
} else {
switch (w->id) {
case snd_soc_dapm_switch:
case snd_soc_dapm_mixer:
case snd_soc_dapm_pga:
case snd_soc_dapm_effect:
case snd_soc_dapm_out_drv:
case snd_soc_dapm_encoder:
case snd_soc_dapm_decoder:
wname_in_long_name = true;
kcname_in_long_name = true;
break;
case snd_soc_dapm_mux_named_ctl:
case snd_soc_dapm_mixer_named_ctl:
wname_in_long_name = false;
kcname_in_long_name = true;
break;
case snd_soc_dapm_demux:
case snd_soc_dapm_mux:
wname_in_long_name = true;
kcname_in_long_name = false;
break;
default:
return -EINVAL;
}
}
if (w->no_wname_in_kcontrol_name)
wname_in_long_name = false;
if (wname_in_long_name && kcname_in_long_name) {
long_name = kasprintf(GFP_KERNEL, "%s %s",
w->name + prefix_len,
w->kcontrol_news[kci].name);
if (long_name == NULL)
return -ENOMEM;
name = long_name;
} else if (wname_in_long_name) {
long_name = NULL;
name = w->name + prefix_len;
} else {
long_name = NULL;
name = w->kcontrol_news[kci].name;
}
kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
prefix);
if (!kcontrol) {
ret = -ENOMEM;
goto exit_free;
}
kcontrol->private_free = dapm_kcontrol_free;
ret = dapm_kcontrol_data_alloc(w, kcontrol, name);
if (ret) {
snd_ctl_free_one(kcontrol);
goto exit_free;
}
ret = snd_ctl_add(card, kcontrol);
if (ret < 0) {
dev_err(dev,
"ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
w->name, name, ret);
goto exit_free;
}
}
ret = dapm_kcontrol_add_widget(kcontrol, w);
if (ret == 0)
w->kcontrols[kci] = kcontrol;
exit_free:
kfree(long_name);
return ret;
}
static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
{
int i, ret;
struct snd_soc_dapm_path *path;
struct dapm_kcontrol_data *data;
for (i = 0; i < w->num_kcontrols; i++) {
snd_soc_dapm_widget_for_each_source_path(w, path) {
if (path->name != (char *)w->kcontrol_news[i].name)
continue;
if (!w->kcontrols[i]) {
ret = dapm_create_or_share_kcontrol(w, i);
if (ret < 0)
return ret;
}
dapm_kcontrol_add_path(w->kcontrols[i], path);
data = snd_kcontrol_chip(w->kcontrols[i]);
if (data->widget)
dapm_add_path(data->widget->dapm,
data->widget,
path->source,
NULL, NULL);
}
}
return 0;
}
static int dapm_new_mux(struct snd_soc_dapm_widget *w)
{
struct snd_soc_dapm_context *dapm = w->dapm;
struct device *dev = snd_soc_dapm_to_dev(dapm);
enum snd_soc_dapm_direction dir;
struct snd_soc_dapm_path *path;
const char *type;
int ret;
switch (w->id) {
case snd_soc_dapm_mux:
case snd_soc_dapm_mux_named_ctl:
dir = SND_SOC_DAPM_DIR_OUT;
type = "mux";
break;
case snd_soc_dapm_demux:
dir = SND_SOC_DAPM_DIR_IN;
type = "demux";
break;
default:
return -EINVAL;
}
if (w->num_kcontrols != 1) {
dev_err(dev,
"ASoC: %s %s has incorrect number of controls\n", type,
w->name);
return -EINVAL;
}
if (list_empty(&w->edges[dir])) {
dev_err(dev, "ASoC: %s %s has no paths\n", type, w->name);
return -EINVAL;
}
ret = dapm_create_or_share_kcontrol(w, 0);
if (ret < 0)
return ret;
snd_soc_dapm_widget_for_each_path(w, dir, path) {
if (path->name)
dapm_kcontrol_add_path(w->kcontrols[0], path);
}
return 0;
}
static int dapm_new_pga(struct snd_soc_dapm_widget *w)
{
int i;
for (i = 0; i < w->num_kcontrols; i++) {
int ret = dapm_create_or_share_kcontrol(w, i);
if (ret < 0)
return ret;
}
return 0;
}
static int dapm_new_dai_link(struct snd_soc_dapm_widget *w)
{
int i;
struct snd_soc_pcm_runtime *rtd = w->priv;
if (rtd->dai_link->num_c2c_params <= 1)
return 0;
for (i = 0; i < w->num_kcontrols; i++) {
struct snd_soc_dapm_context *dapm = w->dapm;
struct device *dev = snd_soc_dapm_to_dev(dapm);
struct snd_card *card = dapm->card->snd_card;
struct snd_kcontrol *kcontrol = snd_soc_cnew(&w->kcontrol_news[i],
w, w->name, NULL);
int ret = snd_ctl_add(card, kcontrol);
if (ret < 0) {
dev_err(dev,
"ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
w->name, w->kcontrol_news[i].name, ret);
return ret;
}
kcontrol->private_data = w;
w->kcontrols[i] = kcontrol;
}
return 0;
}
static int dapm_suspend_check(struct snd_soc_dapm_widget *widget)
{
struct device *dev = snd_soc_dapm_to_dev(widget->dapm);
int level = snd_power_get_state(widget->dapm->card->snd_card);
switch (level) {
case SNDRV_CTL_POWER_D3hot:
case SNDRV_CTL_POWER_D3cold:
if (widget->ignore_suspend)
dev_dbg(dev, "ASoC: %s ignoring suspend\n",
widget->name);
return widget->ignore_suspend;
default:
return 1;
}
}
static void dapm_widget_list_free(struct snd_soc_dapm_widget_list **list)
{
kfree(*list);
}
static int dapm_widget_list_create(struct snd_soc_dapm_widget_list **list,
struct list_head *widgets)
{
struct snd_soc_dapm_widget *w;
struct list_head *it;
unsigned int size = 0;
unsigned int i = 0;
list_for_each(it, widgets)
size++;
*list = kzalloc_flex(**list, widgets, size);
if (*list == NULL)
return -ENOMEM;
(*list)->num_widgets = size;
list_for_each_entry(w, widgets, work_list)
(*list)->widgets[i++] = w;
(*list)->num_widgets = i;
return 0;
}
static void dapm_invalidate_paths_ep(struct snd_soc_dapm_widget *widget,
enum snd_soc_dapm_direction dir)
{
enum snd_soc_dapm_direction rdir = DAPM_DIR_REVERSE(dir);
struct snd_soc_dapm_path *path;
widget->endpoints[dir] = -1;
snd_soc_dapm_widget_for_each_path(widget, rdir, path) {
if (path->is_supply)
continue;
if (path->walking)
return;
if (path->connect) {
path->walking = 1;
dapm_invalidate_paths_ep(path->node[dir], dir);
path->walking = 0;
}
}
}
static __always_inline int dapm_is_connected_ep(struct snd_soc_dapm_widget *widget,
struct list_head *list, enum snd_soc_dapm_direction dir,
int (*fn)(struct snd_soc_dapm_widget *, struct list_head *,
bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
enum snd_soc_dapm_direction)),
bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
enum snd_soc_dapm_direction))
{
enum snd_soc_dapm_direction rdir = DAPM_DIR_REVERSE(dir);
struct snd_soc_dapm_path *path;
int con = 0;
if (widget->endpoints[dir] >= 0)
return widget->endpoints[dir];
DAPM_UPDATE_STAT(widget, path_checks);
if (list)
list_add_tail(&widget->work_list, list);
if (custom_stop_condition && custom_stop_condition(widget, dir)) {
list = NULL;
custom_stop_condition = NULL;
}
if ((widget->is_ep & SND_SOC_DAPM_DIR_TO_EP(dir)) && widget->connected) {
widget->endpoints[dir] = dapm_suspend_check(widget);
return widget->endpoints[dir];
}
snd_soc_dapm_widget_for_each_path(widget, rdir, path) {
DAPM_UPDATE_STAT(widget, neighbour_checks);
if (path->is_supply)
continue;
if (path->walking)
return 1;
trace_snd_soc_dapm_path(widget, dir, path);
if (path->connect) {
path->walking = 1;
con += fn(path->node[dir], list, custom_stop_condition);
path->walking = 0;
}
}
widget->endpoints[dir] = con;
return con;
}
static int dapm_is_connected_output_ep(struct snd_soc_dapm_widget *widget,
struct list_head *list,
bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
enum snd_soc_dapm_direction))
{
return dapm_is_connected_ep(widget, list, SND_SOC_DAPM_DIR_OUT,
dapm_is_connected_output_ep, custom_stop_condition);
}
static int dapm_is_connected_input_ep(struct snd_soc_dapm_widget *widget,
struct list_head *list,
bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
enum snd_soc_dapm_direction))
{
return dapm_is_connected_ep(widget, list, SND_SOC_DAPM_DIR_IN,
dapm_is_connected_input_ep, custom_stop_condition);
}
int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
struct snd_soc_dapm_widget_list **list,
bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
enum snd_soc_dapm_direction))
{
struct snd_soc_card *card = dai->component->card;
struct snd_soc_dapm_widget *w = snd_soc_dai_get_widget(dai, stream);
LIST_HEAD(widgets);
int paths;
int ret;
snd_soc_dapm_mutex_lock(card);
if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
dapm_invalidate_paths_ep(w, SND_SOC_DAPM_DIR_OUT);
paths = dapm_is_connected_output_ep(w, &widgets,
custom_stop_condition);
} else {
dapm_invalidate_paths_ep(w, SND_SOC_DAPM_DIR_IN);
paths = dapm_is_connected_input_ep(w, &widgets,
custom_stop_condition);
}
list_del(widgets.next);
ret = dapm_widget_list_create(list, &widgets);
if (ret)
paths = ret;
trace_snd_soc_dapm_connected(paths, stream);
snd_soc_dapm_mutex_unlock(card);
return paths;
}
EXPORT_SYMBOL_GPL(snd_soc_dapm_dai_get_connected_widgets);
void snd_soc_dapm_dai_free_widgets(struct snd_soc_dapm_widget_list **list)
{
dapm_widget_list_free(list);
}
EXPORT_SYMBOL_GPL(snd_soc_dapm_dai_free_widgets);
int snd_soc_dapm_regulator_event(struct snd_soc_dapm_widget *w,
struct snd_kcontrol *kcontrol, int event)
{
struct device *dev = snd_soc_dapm_to_dev(w->dapm);
int ret;
dapm_async_complete(w->dapm);
if (SND_SOC_DAPM_EVENT_ON(event)) {
if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
ret = regulator_allow_bypass(w->regulator, false);
if (ret != 0)
dev_warn(dev,
"ASoC: Failed to unbypass %s: %d\n",
w->name, ret);
}
return regulator_enable(w->regulator);
} else {
if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
ret = regulator_allow_bypass(w->regulator, true);
if (ret != 0)
dev_warn(dev,
"ASoC: Failed to bypass %s: %d\n",
w->name, ret);
}
return regulator_disable_deferred(w->regulator, w->shift);
}
}
EXPORT_SYMBOL_GPL(snd_soc_dapm_regulator_event);
int snd_soc_dapm_pinctrl_event(struct snd_soc_dapm_widget *w,
struct snd_kcontrol *kcontrol, int event)
{
struct snd_soc_dapm_pinctrl_priv *priv = w->priv;
struct pinctrl *p = w->pinctrl;
struct pinctrl_state *s;
if (!p || !priv)
return -EIO;
if (SND_SOC_DAPM_EVENT_ON(event))
s = pinctrl_lookup_state(p, priv->active_state);
else
s = pinctrl_lookup_state(p, priv->sleep_state);
if (IS_ERR(s))
return PTR_ERR(s);
return pinctrl_select_state(p, s);
}
EXPORT_SYMBOL_GPL(snd_soc_dapm_pinctrl_event);
int snd_soc_dapm_clock_event(struct snd_soc_dapm_widget *w,
struct snd_kcontrol *kcontrol, int event)
{
if (!w->clk)
return -EIO;
dapm_async_complete(w->dapm);
if (SND_SOC_DAPM_EVENT_ON(event)) {
return clk_prepare_enable(w->clk);
} else {
clk_disable_unprepare(w->clk);
return 0;
}
return 0;
}
EXPORT_SYMBOL_GPL(snd_soc_dapm_clock_event);
static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
{
if (w->power_checked)
return w->new_power;
if (w->force)
w->new_power = 1;
else
w->new_power = w->power_check(w);
w->power_checked = true;
return w->new_power;
}
static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
{
int in, out;
DAPM_UPDATE_STAT(w, power_checks);
in = dapm_is_connected_input_ep(w, NULL, NULL);
out = dapm_is_connected_output_ep(w, NULL, NULL);
return out != 0 && in != 0;
}
static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
{
struct snd_soc_dapm_path *path;
DAPM_UPDATE_STAT(w, power_checks);
snd_soc_dapm_widget_for_each_sink_path(w, path) {
DAPM_UPDATE_STAT(w, neighbour_checks);
if (path->connected &&
!path->connected(path->source, path->sink))
continue;
if (dapm_widget_power_check(path->sink))
return 1;
}
return 0;
}
static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
{
return w->connected;
}
static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
struct snd_soc_dapm_widget *b,
bool power_up)
{
int *sort;
BUILD_BUG_ON(ARRAY_SIZE(dapm_up_seq) != SND_SOC_DAPM_TYPE_COUNT);
BUILD_BUG_ON(ARRAY_SIZE(dapm_down_seq) != SND_SOC_DAPM_TYPE_COUNT);
if (power_up)
sort = dapm_up_seq;
else
sort = dapm_down_seq;
WARN_ONCE(sort[a->id] == 0, "offset a->id %d not initialized\n", a->id);
WARN_ONCE(sort[b->id] == 0, "offset b->id %d not initialized\n", b->id);
if (sort[a->id] != sort[b->id])
return sort[a->id] - sort[b->id];
if (a->subseq != b->subseq) {
if (power_up)
return a->subseq - b->subseq;
else
return b->subseq - a->subseq;
}
if (a->reg != b->reg)
return a->reg - b->reg;
if (a->dapm != b->dapm)
return (unsigned long)a->dapm - (unsigned long)b->dapm;
return 0;
}
static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
struct list_head *list,
bool power_up)
{
struct snd_soc_dapm_widget *w;
list_for_each_entry(w, list, power_list)
if (dapm_seq_compare(new_widget, w, power_up) < 0) {
list_add_tail(&new_widget->power_list, &w->power_list);
return;
}
list_add_tail(&new_widget->power_list, list);
}
static void dapm_seq_check_event(struct snd_soc_card *card,
struct snd_soc_dapm_widget *w, int event)
{
struct device *dev = card->dev;
const char *ev_name;
int power;
switch (event) {
case SND_SOC_DAPM_PRE_PMU:
ev_name = "PRE_PMU";
power = 1;
break;
case SND_SOC_DAPM_POST_PMU:
ev_name = "POST_PMU";
power = 1;
break;
case SND_SOC_DAPM_PRE_PMD:
ev_name = "PRE_PMD";
power = 0;
break;
case SND_SOC_DAPM_POST_PMD:
ev_name = "POST_PMD";
power = 0;
break;
case SND_SOC_DAPM_WILL_PMU:
ev_name = "WILL_PMU";
power = 1;
break;
case SND_SOC_DAPM_WILL_PMD:
ev_name = "WILL_PMD";
power = 0;
break;
default:
WARN(1, "Unknown event %d\n", event);
return;
}
if (w->new_power != power)
return;
if (w->event && (w->event_flags & event)) {
int ret;
dapm_pop_dbg(dev, "pop test : %s %s\n", w->name, ev_name);
dapm_async_complete(w->dapm);
trace_snd_soc_dapm_widget_event_start(w, event);
ret = w->event(w, NULL, event);
trace_snd_soc_dapm_widget_event_done(w, event);
if (ret < 0)
dev_err(dev, "ASoC: %s: %s event failed: %d\n",
ev_name, w->name, ret);
}
}
static void dapm_seq_run_coalesced(struct snd_soc_card *card,
struct list_head *pending)
{
struct device *dev = card->dev;
struct snd_soc_dapm_context *dapm;
struct snd_soc_dapm_widget *w;
int reg;
unsigned int value = 0;
unsigned int mask = 0;
w = list_first_entry(pending, struct snd_soc_dapm_widget, power_list);
reg = w->reg;
dapm = w->dapm;
list_for_each_entry(w, pending, power_list) {
WARN_ON(reg != w->reg || dapm != w->dapm);
w->power = w->new_power;
mask |= w->mask << w->shift;
if (w->power)
value |= w->on_val << w->shift;
else
value |= w->off_val << w->shift;
dapm_pop_dbg(dev,
"pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
w->name, reg, value, mask);
dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
}
if (reg >= 0) {
dapm_pop_dbg(dev,
"pop test : Applying 0x%x/0x%x to %x in %dms\n",
value, mask, reg, pop_time);
dapm_pop_wait();
dapm_update_bits(dapm, reg, mask, value);
}
list_for_each_entry(w, pending, power_list) {
dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
}
}
static void dapm_seq_run(struct snd_soc_card *card,
struct list_head *list, int event, bool power_up)
{
struct device *dev = card->dev;
struct snd_soc_dapm_widget *w, *n;
struct snd_soc_dapm_context *d;
LIST_HEAD(pending);
int cur_sort = -1;
int cur_subseq = -1;
int cur_reg = SND_SOC_NOPM;
struct snd_soc_dapm_context *cur_dapm = NULL;
int i;
int *sort;
if (power_up)
sort = dapm_up_seq;
else
sort = dapm_down_seq;
list_for_each_entry_safe(w, n, list, power_list) {
int ret = 0;
if (sort[w->id] != cur_sort || w->reg != cur_reg ||
w->dapm != cur_dapm || w->subseq != cur_subseq) {
if (!list_empty(&pending))
dapm_seq_run_coalesced(card, &pending);
if (cur_dapm && cur_dapm->component) {
for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
if (sort[i] == cur_sort)
snd_soc_component_seq_notifier(
cur_dapm->component,
i, cur_subseq);
}
if (cur_dapm && w->dapm != cur_dapm)
dapm_async_complete(cur_dapm);
INIT_LIST_HEAD(&pending);
cur_sort = -1;
cur_subseq = INT_MIN;
cur_reg = SND_SOC_NOPM;
cur_dapm = NULL;
}
switch (w->id) {
case snd_soc_dapm_pre:
if (!w->event)
continue;
if (event == SND_SOC_DAPM_STREAM_START)
ret = w->event(w,
NULL, SND_SOC_DAPM_PRE_PMU);
else if (event == SND_SOC_DAPM_STREAM_STOP)
ret = w->event(w,
NULL, SND_SOC_DAPM_PRE_PMD);
break;
case snd_soc_dapm_post:
if (!w->event)
continue;
if (event == SND_SOC_DAPM_STREAM_START)
ret = w->event(w,
NULL, SND_SOC_DAPM_POST_PMU);
else if (event == SND_SOC_DAPM_STREAM_STOP)
ret = w->event(w,
NULL, SND_SOC_DAPM_POST_PMD);
break;
default:
cur_sort = sort[w->id];
cur_subseq = w->subseq;
cur_reg = w->reg;
cur_dapm = w->dapm;
list_move(&w->power_list, &pending);
break;
}
if (ret < 0)
dev_err(dev,
"ASoC: Failed to apply widget power: %d\n", ret);
}
if (!list_empty(&pending))
dapm_seq_run_coalesced(card, &pending);
if (cur_dapm && cur_dapm->component) {
for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
if (sort[i] == cur_sort)
snd_soc_component_seq_notifier(
cur_dapm->component,
i, cur_subseq);
}
for_each_card_dapms(card, d)
dapm_async_complete(d);
}
static void dapm_widget_update(struct snd_soc_card *card, struct snd_soc_dapm_update *update)
{
struct device *dev = card->dev;
struct snd_soc_dapm_widget_list *wlist;
struct snd_soc_dapm_widget *w = NULL;
unsigned int wi;
int ret;
if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
return;
wlist = dapm_kcontrol_get_wlist(update->kcontrol);
for_each_dapm_widgets(wlist, wi, w) {
if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
if (ret != 0)
dev_err(dev, "ASoC: %s DAPM pre-event failed: %d\n",
w->name, ret);
}
}
if (!w)
return;
ret = dapm_update_bits(w->dapm, update->reg, update->mask,
update->val);
if (ret < 0)
dev_err(dev, "ASoC: %s DAPM update failed: %d\n",
w->name, ret);
if (update->has_second_set) {
ret = dapm_update_bits(w->dapm, update->reg2,
update->mask2, update->val2);
if (ret < 0)
dev_err(dev,
"ASoC: %s DAPM update failed: %d\n",
w->name, ret);
}
for_each_dapm_widgets(wlist, wi, w) {
if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
if (ret != 0)
dev_err(dev, "ASoC: %s DAPM post-event failed: %d\n",
w->name, ret);
}
}
}
static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
{
struct snd_soc_dapm_context *dapm = data;
struct device *dev = snd_soc_dapm_to_dev(dapm);
int ret;
if (dapm->bias_level == SND_SOC_BIAS_OFF &&
dapm->target_bias_level != SND_SOC_BIAS_OFF) {
if (dev && cookie)
pm_runtime_get_sync(dev);
ret = snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_STANDBY);
if (ret != 0)
dev_err(dev,
"ASoC: Failed to turn on bias: %d\n", ret);
}
if ((dapm->target_bias_level == SND_SOC_BIAS_ON &&
dapm->bias_level != SND_SOC_BIAS_ON) ||
(dapm->target_bias_level != SND_SOC_BIAS_ON &&
dapm->bias_level == SND_SOC_BIAS_ON)) {
ret = snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_PREPARE);
if (ret != 0)
dev_err(dev,
"ASoC: Failed to prepare bias: %d\n", ret);
}
}
static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
{
struct snd_soc_dapm_context *dapm = data;
struct device *dev = snd_soc_dapm_to_dev(dapm);
int ret;
if (dapm->bias_level == SND_SOC_BIAS_PREPARE &&
(dapm->target_bias_level == SND_SOC_BIAS_STANDBY ||
dapm->target_bias_level == SND_SOC_BIAS_OFF)) {
ret = snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_STANDBY);
if (ret != 0)
dev_err(dev, "ASoC: Failed to apply standby bias: %d\n", ret);
}
if (dapm->bias_level == SND_SOC_BIAS_STANDBY &&
dapm->target_bias_level == SND_SOC_BIAS_OFF) {
ret = snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_OFF);
if (ret != 0)
dev_err(dev, "ASoC: Failed to turn off bias: %d\n", ret);
if (dev && cookie)
pm_runtime_put(dev);
}
if (dapm->bias_level == SND_SOC_BIAS_PREPARE &&
dapm->target_bias_level == SND_SOC_BIAS_ON) {
ret = snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_ON);
if (ret != 0)
dev_err(dev, "ASoC: Failed to apply active bias: %d\n", ret);
}
}
static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
bool power, bool connect)
{
if (!connect)
return;
if (power != peer->power)
dapm_mark_dirty(peer, "peer state change");
}
static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
struct list_head *up_list,
struct list_head *down_list)
{
struct snd_soc_dapm_path *path;
int power;
switch (w->id) {
case snd_soc_dapm_pre:
power = 0;
goto end;
case snd_soc_dapm_post:
power = 1;
goto end;
default:
break;
}
power = dapm_widget_power_check(w);
if (w->power == power)
return;
trace_snd_soc_dapm_widget_power(w, power);
snd_soc_dapm_widget_for_each_source_path(w, path)
dapm_widget_set_peer_power(path->source, power, path->connect);
if (!w->is_supply)
snd_soc_dapm_widget_for_each_sink_path(w, path)
dapm_widget_set_peer_power(path->sink, power, path->connect);
end:
if (power)
dapm_seq_insert(w, up_list, true);
else
dapm_seq_insert(w, down_list, false);
}
bool snd_soc_dapm_get_idle_bias(struct snd_soc_dapm_context *dapm)
{
if (dapm->idle_bias) {
struct snd_soc_component *component = snd_soc_dapm_to_component(dapm);
unsigned int state = snd_power_get_state(dapm->card->snd_card);
if ((state == SNDRV_CTL_POWER_D3hot || (state == SNDRV_CTL_POWER_D3cold)) &&
component)
return !component->driver->suspend_bias_off;
}
return dapm->idle_bias;
}
EXPORT_SYMBOL_GPL(snd_soc_dapm_get_idle_bias);
void snd_soc_dapm_set_idle_bias(struct snd_soc_dapm_context *dapm, bool on)
{
dapm->idle_bias = on;
}
EXPORT_SYMBOL_GPL(snd_soc_dapm_set_idle_bias);
static int dapm_power_widgets(struct snd_soc_card *card, int event,
struct snd_soc_dapm_update *update)
{
struct snd_soc_dapm_context *dapm = snd_soc_card_to_dapm(card);
struct snd_soc_dapm_widget *w;
struct snd_soc_dapm_context *d;
LIST_HEAD(up_list);
LIST_HEAD(down_list);
ASYNC_DOMAIN_EXCLUSIVE(async_domain);
enum snd_soc_bias_level bias;
int ret;
snd_soc_dapm_mutex_assert_held(card);
trace_snd_soc_dapm_start(card, event);
for_each_card_dapms(card, d) {
if (snd_soc_dapm_get_idle_bias(d))
d->target_bias_level = SND_SOC_BIAS_STANDBY;
else
d->target_bias_level = SND_SOC_BIAS_OFF;
}
dapm_reset(card);
list_for_each_entry(w, &card->dapm_dirty, dirty) {
dapm_power_one_widget(w, &up_list, &down_list);
}
for_each_card_widgets(card, w) {
switch (w->id) {
case snd_soc_dapm_pre:
case snd_soc_dapm_post:
break;
default:
list_del_init(&w->dirty);
break;
}
if (w->new_power) {
d = w->dapm;
switch (w->id) {
case snd_soc_dapm_siggen:
case snd_soc_dapm_vmid:
break;
case snd_soc_dapm_supply:
case snd_soc_dapm_regulator_supply:
case snd_soc_dapm_pinctrl:
case snd_soc_dapm_clock_supply:
case snd_soc_dapm_micbias:
if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
d->target_bias_level = SND_SOC_BIAS_STANDBY;
break;
default:
d->target_bias_level = SND_SOC_BIAS_ON;
break;
}
}
}
bias = SND_SOC_BIAS_OFF;
for_each_card_dapms(card, d)
if (d->target_bias_level > bias)
bias = d->target_bias_level;
for_each_card_dapms(card, d)
if (snd_soc_dapm_get_idle_bias(d))
d->target_bias_level = bias;
trace_snd_soc_dapm_walk_done(card);
dapm_pre_sequence_async(dapm, 0);
for_each_card_dapms(card, d) {
if (d != dapm && d->bias_level != d->target_bias_level)
async_schedule_domain(dapm_pre_sequence_async, d,
&async_domain);
}
async_synchronize_full_domain(&async_domain);
list_for_each_entry(w, &down_list, power_list) {
dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
}
list_for_each_entry(w, &up_list, power_list) {
dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
}
dapm_seq_run(card, &down_list, event, false);
dapm_widget_update(card, update);
dapm_seq_run(card, &up_list, event, true);
for_each_card_dapms(card, d) {
if (d != dapm && d->bias_level != d->target_bias_level)
async_schedule_domain(dapm_post_sequence_async, d,
&async_domain);
}
async_synchronize_full_domain(&async_domain);
dapm_post_sequence_async(dapm, 0);
for_each_card_dapms(card, d) {
if (!d->component)
continue;
ret = snd_soc_component_stream_event(d->component, event);
if (ret < 0)
return ret;
}
dapm_pop_dbg(card->dev,
"DAPM sequencing finished, waiting %dms\n", pop_time);
dapm_pop_wait();
trace_snd_soc_dapm_done(card, event);
return 0;
}
#ifdef CONFIG_DEBUG_FS
static const char * const dapm_type_name[] = {
[snd_soc_dapm_input] = "input",
[snd_soc_dapm_output] = "output",
[snd_soc_dapm_mux] = "mux",
[snd_soc_dapm_mux_named_ctl] = "mux_named_ctl",
[snd_soc_dapm_demux] = "demux",
[snd_soc_dapm_mixer] = "mixer",
[snd_soc_dapm_mixer_named_ctl] = "mixer_named_ctl",
[snd_soc_dapm_pga] = "pga",
[snd_soc_dapm_out_drv] = "out_drv",
[snd_soc_dapm_adc] = "adc",
[snd_soc_dapm_dac] = "dac",
[snd_soc_dapm_micbias] = "micbias",
[snd_soc_dapm_mic] = "mic",
[snd_soc_dapm_hp] = "hp",
[snd_soc_dapm_spk] = "spk",
[snd_soc_dapm_line] = "line",
[snd_soc_dapm_switch] = "switch",
[snd_soc_dapm_vmid] = "vmid",
[snd_soc_dapm_pre] = "pre",
[snd_soc_dapm_post] = "post",
[snd_soc_dapm_supply] = "supply",
[snd_soc_dapm_pinctrl] = "pinctrl",
[snd_soc_dapm_regulator_supply] = "regulator_supply",
[snd_soc_dapm_clock_supply] = "clock_supply",
[snd_soc_dapm_aif_in] = "aif_in",
[snd_soc_dapm_aif_out] = "aif_out",
[snd_soc_dapm_siggen] = "siggen",
[snd_soc_dapm_sink] = "sink",
[snd_soc_dapm_dai_in] = "dai_in",
[snd_soc_dapm_dai_out] = "dai_out",
[snd_soc_dapm_dai_link] = "dai_link",
[snd_soc_dapm_kcontrol] = "kcontrol",
[snd_soc_dapm_buffer] = "buffer",
[snd_soc_dapm_scheduler] = "scheduler",
[snd_soc_dapm_effect] = "effect",
[snd_soc_dapm_src] = "src",
[snd_soc_dapm_asrc] = "asrc",
[snd_soc_dapm_encoder] = "encoder",
[snd_soc_dapm_decoder] = "decoder",
};
static ssize_t dapm_widget_power_read_file(struct file *file,
char __user *user_buf,
size_t count, loff_t *ppos)
{
struct snd_soc_dapm_widget *w = file->private_data;
enum snd_soc_dapm_direction dir, rdir;
char *buf;
int in, out;
ssize_t ret;
struct snd_soc_dapm_path *p = NULL;
const char *c_name;
BUILD_BUG_ON(ARRAY_SIZE(dapm_type_name) != SND_SOC_DAPM_TYPE_COUNT);
buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
if (!buf)
return -ENOMEM;
snd_soc_dapm_mutex_lock_root(w->dapm);
if (w->is_supply) {
in = 0;
out = 0;
} else {
in = dapm_is_connected_input_ep(w, NULL, NULL);
out = dapm_is_connected_output_ep(w, NULL, NULL);
}
ret = scnprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
w->name, w->power ? "On" : "Off",
w->force ? " (forced)" : "", in, out);
if (w->reg >= 0)
ret += scnprintf(buf + ret, PAGE_SIZE - ret,
" - R%d(0x%x) mask 0x%x",
w->reg, w->reg, w->mask << w->shift);
ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
if (w->sname)
ret += scnprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
w->sname,
w->active ? "active" : "inactive");
ret += scnprintf(buf + ret, PAGE_SIZE - ret, " widget-type %s\n",
dapm_type_name[w->id]);
dapm_for_each_direction(dir) {
rdir = DAPM_DIR_REVERSE(dir);
snd_soc_dapm_widget_for_each_path(w, dir, p) {
if (p->connected && !p->connected(p->source, p->sink))
continue;
if (!p->connect)
continue;
c_name = p->node[rdir]->dapm->component ?
p->node[rdir]->dapm->component->name : NULL;
ret += scnprintf(buf + ret, PAGE_SIZE - ret,
" %s \"%s\" \"%s\" \"%s\"\n",
(rdir == SND_SOC_DAPM_DIR_IN) ? "in" : "out",
p->name ? p->name : "static",
p->node[rdir]->name, c_name);
}
}
snd_soc_dapm_mutex_unlock(w->dapm);
ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
kfree(buf);
return ret;
}
static const struct file_operations dapm_widget_power_fops = {
.open = simple_open,
.read = dapm_widget_power_read_file,
.llseek = default_llseek,
};
static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
size_t count, loff_t *ppos)
{
struct snd_soc_dapm_context *dapm = file->private_data;
char *level;
switch (dapm->bias_level) {
case SND_SOC_BIAS_ON:
level = "On\n";
break;
case SND_SOC_BIAS_PREPARE:
level = "Prepare\n";
break;
case SND_SOC_BIAS_STANDBY:
level = "Standby\n";
break;
case SND_SOC_BIAS_OFF:
level = "Off\n";
break;
default:
WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
level = "Unknown\n";
break;
}
return simple_read_from_buffer(user_buf, count, ppos, level,
strlen(level));
}
static const struct file_operations dapm_bias_fops = {
.open = simple_open,
.read = dapm_bias_read_file,
.llseek = default_llseek,
};
void snd_soc_dapm_debugfs_pop_time(struct dentry *parent)
{
debugfs_create_u32("dapm_pop_time", 0644, parent, &pop_time);
}
void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
struct dentry *parent)
{
if (IS_ERR_OR_NULL(parent))
return;
dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
debugfs_create_file("bias_level", 0444, dapm->debugfs_dapm, dapm,
&dapm_bias_fops);
}
static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
{
struct snd_soc_dapm_context *dapm = w->dapm;
if (!dapm->debugfs_dapm || !w->name)
return;
debugfs_create_file(w->name, 0444, dapm->debugfs_dapm, w,
&dapm_widget_power_fops);
}
static void dapm_debugfs_free_widget(struct snd_soc_dapm_widget *w)
{
struct snd_soc_dapm_context *dapm = w->dapm;
if (!dapm->debugfs_dapm || !w->name)
return;
debugfs_lookup_and_remove(w->name, dapm->debugfs_dapm);
}
static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
{
debugfs_remove_recursive(dapm->debugfs_dapm);
dapm->debugfs_dapm = NULL;
}
#else
void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
struct dentry *parent)
{
}
static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
{
}
static inline void dapm_debugfs_free_widget(struct snd_soc_dapm_widget *w)
{
}
static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
{
}
#endif
static void dapm_connect_path(struct snd_soc_dapm_path *path,
bool connect, const char *reason)
{
if (path->connect == connect)
return;
path->connect = connect;
dapm_mark_dirty(path->source, reason);
dapm_mark_dirty(path->sink, reason);
dapm_path_invalidate(path);
}
static int dapm_mux_update_power(struct snd_soc_card *card,
struct snd_kcontrol *kcontrol,
struct snd_soc_dapm_update *update,
int mux, struct soc_enum *e)
{
struct snd_soc_dapm_path *path;
int found = 0;
bool connect;
snd_soc_dapm_mutex_assert_held(card);
dapm_kcontrol_for_each_path(path, kcontrol) {
found = 1;
if (e && !(strcmp(path->name, e->texts[mux])))
connect = true;
else
connect = false;
dapm_connect_path(path, connect, "mux update");
}
if (found)
dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP, update);
return found;
}
int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
struct snd_soc_dapm_update *update)
{
struct snd_soc_card *card = dapm->card;
int ret;
snd_soc_dapm_mutex_lock(card);
ret = dapm_mux_update_power(card, kcontrol, update, mux, e);
snd_soc_dapm_mutex_unlock(card);
if (ret > 0)
snd_soc_dpcm_runtime_update(card);
return ret;
}
EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
static int dapm_mixer_update_power(struct snd_soc_card *card,
struct snd_kcontrol *kcontrol,
struct snd_soc_dapm_update *update,
int connect, int rconnect)
{
struct snd_soc_dapm_path *path;
int found = 0;
snd_soc_dapm_mutex_assert_held(card);
dapm_kcontrol_for_each_path(path, kcontrol) {
if (found && rconnect >= 0)
dapm_connect_path(path, rconnect, "mixer update");
else
dapm_connect_path(path, connect, "mixer update");
found = 1;
}
if (found)
dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP, update);
return found;
}
int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
struct snd_kcontrol *kcontrol, int connect,
struct snd_soc_dapm_update *update)
{
struct snd_soc_card *card = dapm->card;
int ret;
snd_soc_dapm_mutex_lock(card);
ret = dapm_mixer_update_power(card, kcontrol, update, connect, -1);
snd_soc_dapm_mutex_unlock(card);
if (ret > 0)
snd_soc_dpcm_runtime_update(card);
return ret;
}
EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
static ssize_t dapm_widget_show_component(struct snd_soc_component *component,
char *buf, int count)
{
struct snd_soc_dapm_context *dapm = snd_soc_component_to_dapm(component);
struct snd_soc_dapm_widget *w;
char *state = "not set";
if (!component->card)
return 0;
for_each_card_widgets(component->card, w) {
if (w->dapm != dapm)
continue;
switch (w->id) {
case snd_soc_dapm_hp:
case snd_soc_dapm_mic:
case snd_soc_dapm_spk:
case snd_soc_dapm_line:
case snd_soc_dapm_micbias:
case snd_soc_dapm_dac:
case snd_soc_dapm_adc:
case snd_soc_dapm_pga:
case snd_soc_dapm_effect:
case snd_soc_dapm_out_drv:
case snd_soc_dapm_encoder:
case snd_soc_dapm_decoder:
case snd_soc_dapm_mixer:
case snd_soc_dapm_mixer_named_ctl:
case snd_soc_dapm_supply:
case snd_soc_dapm_regulator_supply:
case snd_soc_dapm_pinctrl:
case snd_soc_dapm_clock_supply:
if (w->name)
count += sysfs_emit_at(buf, count, "%s: %s\n",
w->name, w->power ? "On":"Off");
break;
default:
break;
}
}
switch (snd_soc_dapm_get_bias_level(dapm)) {
case SND_SOC_BIAS_ON:
state = "On";
break;
case SND_SOC_BIAS_PREPARE:
state = "Prepare";
break;
case SND_SOC_BIAS_STANDBY:
state = "Standby";
break;
case SND_SOC_BIAS_OFF:
state = "Off";
break;
}
count += sysfs_emit_at(buf, count, "PM State: %s\n", state);
return count;
}
static ssize_t dapm_widget_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
struct snd_soc_dai *codec_dai;
int i, count = 0;
snd_soc_dapm_mutex_lock_root(rtd->card);
for_each_rtd_codec_dais(rtd, i, codec_dai) {
struct snd_soc_component *component = codec_dai->component;
count = dapm_widget_show_component(component, buf, count);
}
snd_soc_dapm_mutex_unlock(rtd->card);
return count;
}
static DEVICE_ATTR_RO(dapm_widget);
struct attribute *snd_soc_dapm_dev_attrs[] = {
&dev_attr_dapm_widget.attr,
NULL
};
static void dapm_free_path(struct snd_soc_dapm_path *path)
{
list_del(&path->list_node[SND_SOC_DAPM_DIR_IN]);
list_del(&path->list_node[SND_SOC_DAPM_DIR_OUT]);
list_del(&path->list_kcontrol);
list_del(&path->list);
kfree(path);
}
void snd_soc_dapm_free_widget(struct snd_soc_dapm_widget *w)
{
struct snd_soc_dapm_context *d;
struct snd_soc_dapm_path *p, *next_p;
enum snd_soc_dapm_direction dir;
if (!w)
return;
list_del(&w->list);
list_del(&w->dirty);
dapm_for_each_direction(dir) {
snd_soc_dapm_widget_for_each_path_safe(w, dir, p, next_p)
dapm_free_path(p);
}
dapm_debugfs_free_widget(w);
if (w->dapm) {
if (w->dapm->card) {
for_each_card_dapms(w->dapm->card, d) {
if (d->wcache_sink == w)
d->wcache_sink = NULL;
if (d->wcache_source == w)
d->wcache_source = NULL;
}
} else {
if (w->dapm->wcache_sink == w)
w->dapm->wcache_sink = NULL;
if (w->dapm->wcache_source == w)
w->dapm->wcache_source = NULL;
}
}
kfree(w->kcontrols);
kfree_const(w->name);
kfree_const(w->sname);
kfree(w);
}
EXPORT_SYMBOL_GPL(snd_soc_dapm_free_widget);
static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
{
struct snd_soc_dapm_widget *w, *next_w;
for_each_card_widgets_safe(dapm->card, w, next_w) {
if (w->dapm != dapm)
continue;
snd_soc_dapm_free_widget(w);
}
}
static struct snd_soc_dapm_widget *dapm_find_widget(
struct snd_soc_dapm_context *dapm, const char *pin,
bool search_other_contexts)
{
struct snd_soc_dapm_widget *w;
struct snd_soc_dapm_widget *fallback = NULL;
bool pin_has_prefix = snd_soc_dapm_pin_has_prefix(dapm->card, pin);
bool match;
for_each_card_widgets(dapm->card, w) {
match = false;
if (!strcmp(pin, w->name))
match = true;
else if (!pin_has_prefix && !snd_soc_dapm_widget_name_cmp(w, pin))
match = true;
if (match) {
if (w->dapm == dapm)
return w;
else
fallback = w;
}
}
if (search_other_contexts)
return fallback;
return NULL;
}
static int __dapm_set_pin(struct snd_soc_dapm_context *dapm,
const char *pin, int status)
{
struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
struct device *dev = snd_soc_dapm_to_dev(dapm);
int ret = 0;
dapm_assert_locked(dapm);
if (!w) {
dev_err(dev, "ASoC: DAPM unknown pin %s\n", pin);
return -EINVAL;
}
if (w->connected != status) {
dapm_mark_dirty(w, "pin configuration");
dapm_widget_invalidate_input_paths(w);
dapm_widget_invalidate_output_paths(w);
ret = 1;
}
w->connected = status;
if (status == 0)
w->force = 0;
return ret;
}
static int dapm_set_pin(struct snd_soc_dapm_context *dapm,
const char *pin, int status)
{
int ret = __dapm_set_pin(dapm, pin, status);
return ret < 0 ? ret : 0;
}
int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context *dapm)
{
if (!snd_soc_card_is_instantiated(dapm->card))
return 0;
return dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP, NULL);
}
EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked);
int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
{
int ret;
snd_soc_dapm_mutex_lock(dapm);
ret = snd_soc_dapm_sync_unlocked(dapm);
snd_soc_dapm_mutex_unlock(dapm);
return ret;
}
EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
static int dapm_update_dai_chan(struct snd_soc_dapm_path *p,
struct snd_soc_dapm_widget *w,
int channels)
{
struct device *dev = snd_soc_dapm_to_dev(w->dapm);
switch (w->id) {
case snd_soc_dapm_aif_out:
case snd_soc_dapm_aif_in:
break;
default:
return 0;
}
dev_dbg(dev, "%s DAI route %s -> %s\n",
w->channel < channels ? "Connecting" : "Disconnecting",
p->source->name, p->sink->name);
if (w->channel < channels)
dapm_connect_path(p, true, "dai update");
else
dapm_connect_path(p, false, "dai update");
return 0;
}
static int dapm_update_dai_unlocked(struct snd_pcm_substream *substream,
struct snd_pcm_hw_params *params,
struct snd_soc_dai *dai)
{
int dir = substream->stream;
int channels = params_channels(params);
struct snd_soc_dapm_path *p;
struct snd_soc_dapm_widget *w;
int ret;
w = snd_soc_dai_get_widget(dai, dir);
if (!w)
return 0;
dev_dbg(dai->dev, "Update DAI routes for %s %s\n", dai->name, snd_pcm_direction_name(dir));
snd_soc_dapm_widget_for_each_sink_path(w, p) {
ret = dapm_update_dai_chan(p, p->sink, channels);
if (ret < 0)
return ret;
}
snd_soc_dapm_widget_for_each_source_path(w, p) {
ret = dapm_update_dai_chan(p, p->source, channels);
if (ret < 0)
return ret;
}
return 0;
}
int snd_soc_dapm_update_dai(struct snd_pcm_substream *substream,
struct snd_pcm_hw_params *params,
struct snd_soc_dai *dai)
{
struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
int ret;
snd_soc_dapm_mutex_lock(rtd->card);
ret = dapm_update_dai_unlocked(substream, params, dai);
snd_soc_dapm_mutex_unlock(rtd->card);
return ret;
}
int snd_soc_dapm_widget_name_cmp(struct snd_soc_dapm_widget *widget, const char *s)
{
struct snd_soc_component *component = widget->dapm->component;
const char *wname = widget->name;
if (component && component->name_prefix)
wname += strlen(component->name_prefix) + 1;
return strcmp(wname, s);
}
EXPORT_SYMBOL_GPL(snd_soc_dapm_widget_name_cmp);
static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
const struct snd_soc_dapm_route *route)
{
struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
struct device *dev = snd_soc_dapm_to_dev(dapm);
const char *sink;
const char *source;
char prefixed_sink[80];
char prefixed_source[80];
const char *prefix;
unsigned int sink_ref = 0;
unsigned int source_ref = 0;
int ret;
prefix = dapm_prefix(dapm);
if (prefix) {
snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
prefix, route->sink);
sink = prefixed_sink;
snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
prefix, route->source);
source = prefixed_source;
} else {
sink = route->sink;
source = route->source;
}
wsource = dapm_wcache_lookup(dapm->wcache_source, source);
wsink = dapm_wcache_lookup(dapm->wcache_sink, sink);
if (wsink && wsource)
goto skip_search;
for_each_card_widgets(dapm->card, w) {
if (!wsink && !(strcmp(w->name, sink))) {
wtsink = w;
if (w->dapm == dapm) {
wsink = w;
if (wsource)
break;
}
sink_ref++;
if (sink_ref > 1)
dev_warn(dev,
"ASoC: sink widget %s overwritten\n",
w->name);
continue;
}
if (!wsource && !(strcmp(w->name, source))) {
wtsource = w;
if (w->dapm == dapm) {
wsource = w;
if (wsink)
break;
}
source_ref++;
if (source_ref > 1)
dev_warn(dev,
"ASoC: source widget %s overwritten\n",
w->name);
}
}
if (!wsink)
wsink = wtsink;
if (!wsource)
wsource = wtsource;
ret = -ENODEV;
if (!wsource)
goto err;
if (!wsink)
goto err;
skip_search:
dapm->wcache_sink = wsink;
dapm->wcache_source = wsource;
ret = dapm_add_path(dapm, wsource, wsink, route->control,
route->connected);
err:
if (ret)
dev_err(dev, "ASoC: Failed to add route %s%s -%s%s%s> %s%s\n",
source, !wsource ? "(*)" : "",
!route->control ? "" : "> [",
!route->control ? "" : route->control,
!route->control ? "" : "] -",
sink, !wsink ? "(*)" : "");
return ret;
}
static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
const struct snd_soc_dapm_route *route)
{
struct device *dev = snd_soc_dapm_to_dev(dapm);
struct snd_soc_dapm_path *path, *p;
const char *sink;
const char *source;
char prefixed_sink[80];
char prefixed_source[80];
const char *prefix;
if (route->control) {
dev_err(dev,
"ASoC: Removal of routes with controls not supported\n");
return -EINVAL;
}
prefix = dapm_prefix(dapm);
if (prefix) {
snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
prefix, route->sink);
sink = prefixed_sink;
snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
prefix, route->source);
source = prefixed_source;
} else {
sink = route->sink;
source = route->source;
}
path = NULL;
list_for_each_entry(p, &dapm->card->paths, list) {
if (strcmp(p->source->name, source) != 0)
continue;
if (strcmp(p->sink->name, sink) != 0)
continue;
path = p;
break;
}
if (path) {
struct snd_soc_dapm_widget *wsource = path->source;
struct snd_soc_dapm_widget *wsink = path->sink;
dapm_mark_dirty(wsource, "Route removed");
dapm_mark_dirty(wsink, "Route removed");
if (path->connect)
dapm_path_invalidate(path);
dapm_free_path(path);
dapm_update_widget_flags(wsource);
dapm_update_widget_flags(wsink);
} else {
dev_warn(dev, "ASoC: Route %s->%s does not exist\n",
source, sink);
}
return 0;
}
int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
const struct snd_soc_dapm_route *route, int num)
{
int i, ret = 0;
snd_soc_dapm_mutex_lock(dapm);
for (i = 0; i < num; i++) {
int r = snd_soc_dapm_add_route(dapm, route);
if (r < 0)
ret = r;
route++;
}
snd_soc_dapm_mutex_unlock(dapm);
return ret;
}
EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
const struct snd_soc_dapm_route *route, int num)
{
int i;
snd_soc_dapm_mutex_lock(dapm);
for (i = 0; i < num; i++) {
snd_soc_dapm_del_route(dapm, route);
route++;
}
snd_soc_dapm_mutex_unlock(dapm);
return 0;
}
EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
{
struct snd_soc_dapm_widget *w;
unsigned int val;
snd_soc_dapm_mutex_lock_root(card);
for_each_card_widgets(card, w)
{
if (w->new)
continue;
if (w->num_kcontrols) {
w->kcontrols = kzalloc_objs(struct snd_kcontrol *,
w->num_kcontrols);
if (!w->kcontrols) {
snd_soc_dapm_mutex_unlock(card);
return -ENOMEM;
}
}
switch(w->id) {
case snd_soc_dapm_switch:
case snd_soc_dapm_mixer:
case snd_soc_dapm_mixer_named_ctl:
dapm_new_mixer(w);
break;
case snd_soc_dapm_mux:
case snd_soc_dapm_mux_named_ctl:
case snd_soc_dapm_demux:
dapm_new_mux(w);
break;
case snd_soc_dapm_pga:
case snd_soc_dapm_effect:
case snd_soc_dapm_out_drv:
case snd_soc_dapm_encoder:
case snd_soc_dapm_decoder:
dapm_new_pga(w);
break;
case snd_soc_dapm_dai_link:
dapm_new_dai_link(w);
break;
default:
break;
}
if (w->reg >= 0) {
val = dapm_read(w->dapm, w->reg);
val = val >> w->shift;
val &= w->mask;
if (val == w->on_val)
w->power = 1;
}
w->new = 1;
dapm_mark_dirty(w, "new widget");
dapm_debugfs_add_widget(w);
}
dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP, NULL);
snd_soc_dapm_mutex_unlock(card);
return 0;
}
EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_value *ucontrol)
{
struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_to_dapm(kcontrol);
struct soc_mixer_control *mc =
(struct soc_mixer_control *)kcontrol->private_value;
int reg = mc->reg;
unsigned int shift = mc->shift;
int max = mc->max;
unsigned int width = fls(max);
unsigned int mask = (1 << fls(max)) - 1;
unsigned int invert = mc->invert;
unsigned int reg_val, val, rval = 0;
snd_soc_dapm_mutex_lock(dapm);
if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM) {
reg_val = dapm_read(dapm, reg);
val = (reg_val >> shift) & mask;
if (reg != mc->rreg)
reg_val = dapm_read(dapm, mc->rreg);
if (snd_soc_volsw_is_stereo(mc))
rval = (reg_val >> mc->rshift) & mask;
} else {
reg_val = snd_soc_dapm_kcontrol_get_value(kcontrol);
val = reg_val & mask;
if (snd_soc_volsw_is_stereo(mc))
rval = (reg_val >> width) & mask;
}
snd_soc_dapm_mutex_unlock(dapm);
if (invert)
ucontrol->value.integer.value[0] = max - val;
else
ucontrol->value.integer.value[0] = val;
if (snd_soc_volsw_is_stereo(mc)) {
if (invert)
ucontrol->value.integer.value[1] = max - rval;
else
ucontrol->value.integer.value[1] = rval;
}
return 0;
}
EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_value *ucontrol)
{
struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_to_dapm(kcontrol);
struct device *dev = snd_soc_dapm_to_dev(dapm);
struct snd_soc_card *card = dapm->card;
struct soc_mixer_control *mc =
(struct soc_mixer_control *)kcontrol->private_value;
int reg = mc->reg;
unsigned int shift = mc->shift;
int max = mc->max;
unsigned int width = fls(max);
unsigned int mask = (1 << width) - 1;
unsigned int invert = mc->invert;
unsigned int val, rval = 0;
int connect, rconnect = -1, change, reg_change = 0;
struct snd_soc_dapm_update update = {};
struct snd_soc_dapm_update *pupdate = NULL;
int ret = 0;
val = (ucontrol->value.integer.value[0] & mask);
connect = !!val;
if (invert)
val = max - val;
if (snd_soc_volsw_is_stereo(mc)) {
rval = (ucontrol->value.integer.value[1] & mask);
rconnect = !!rval;
if (invert)
rval = max - rval;
}
snd_soc_dapm_mutex_lock(card);
if (width > sizeof(unsigned int) * 8 / 2)
dev_warn(dev,
"ASoC: control %s field width limit exceeded\n",
kcontrol->id.name);
change = dapm_kcontrol_set_value(kcontrol, val | (rval << width));
if (reg != SND_SOC_NOPM) {
val = val << shift;
rval = rval << mc->rshift;
reg_change = dapm_test_bits(dapm, reg, mask << shift, val);
if (snd_soc_volsw_is_stereo(mc))
reg_change |= dapm_test_bits(dapm, mc->rreg,
mask << mc->rshift,
rval);
}
if (change || reg_change) {
if (reg_change) {
if (snd_soc_volsw_is_stereo(mc)) {
update.has_second_set = true;
update.reg2 = mc->rreg;
update.mask2 = mask << mc->rshift;
update.val2 = rval;
}
update.kcontrol = kcontrol;
update.reg = reg;
update.mask = mask << shift;
update.val = val;
pupdate = &update;
}
ret = dapm_mixer_update_power(card, kcontrol, pupdate, connect, rconnect);
}
snd_soc_dapm_mutex_unlock(card);
if (ret > 0)
snd_soc_dpcm_runtime_update(card);
return change;
}
EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_value *ucontrol)
{
struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_to_dapm(kcontrol);
struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
unsigned int reg_val, val;
snd_soc_dapm_mutex_lock(dapm);
if (e->reg != SND_SOC_NOPM && dapm_kcontrol_is_powered(kcontrol)) {
reg_val = dapm_read(dapm, e->reg);
} else {
reg_val = snd_soc_dapm_kcontrol_get_value(kcontrol);
}
snd_soc_dapm_mutex_unlock(dapm);
val = (reg_val >> e->shift_l) & e->mask;
ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(e, val);
if (e->shift_l != e->shift_r) {
val = (reg_val >> e->shift_r) & e->mask;
val = snd_soc_enum_val_to_item(e, val);
ucontrol->value.enumerated.item[1] = val;
}
return 0;
}
EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_value *ucontrol)
{
struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_to_dapm(kcontrol);
struct snd_soc_card *card = dapm->card;
struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
unsigned int *item = ucontrol->value.enumerated.item;
unsigned int val, change, reg_change = 0;
unsigned int mask;
struct snd_soc_dapm_update update = {};
struct snd_soc_dapm_update *pupdate = NULL;
int ret = 0;
if (item[0] >= e->items)
return -EINVAL;
val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
mask = e->mask << e->shift_l;
if (e->shift_l != e->shift_r) {
if (item[1] >= e->items)
return -EINVAL;
val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
mask |= e->mask << e->shift_r;
}
snd_soc_dapm_mutex_lock(card);
change = dapm_kcontrol_set_value(kcontrol, val);
if (e->reg != SND_SOC_NOPM)
reg_change = dapm_test_bits(dapm, e->reg, mask, val);
if (change || reg_change) {
if (reg_change) {
update.kcontrol = kcontrol;
update.reg = e->reg;
update.mask = mask;
update.val = val;
pupdate = &update;
}
ret = dapm_mux_update_power(card, kcontrol, pupdate, item[0], e);
}
snd_soc_dapm_mutex_unlock(card);
if (ret > 0)
snd_soc_dpcm_runtime_update(card);
return change;
}
EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_info *uinfo)
{
uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
uinfo->count = 1;
uinfo->value.integer.min = 0;
uinfo->value.integer.max = 1;
return 0;
}
EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
static int __snd_soc_dapm_get_pin_switch(struct snd_soc_dapm_context *dapm,
const char *pin,
struct snd_ctl_elem_value *ucontrol)
{
snd_soc_dapm_mutex_lock(dapm);
ucontrol->value.integer.value[0] = snd_soc_dapm_get_pin_status(dapm, pin);
snd_soc_dapm_mutex_unlock(dapm);
return 0;
}
int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_value *ucontrol)
{
struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
struct snd_soc_dapm_context *dapm = snd_soc_card_to_dapm(card);
const char *pin = (const char *)kcontrol->private_value;
return __snd_soc_dapm_get_pin_switch(dapm, pin, ucontrol);
}
EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
int snd_soc_dapm_get_component_pin_switch(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_value *ucontrol)
{
struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
struct snd_soc_dapm_context *dapm = snd_soc_component_to_dapm(component);
const char *pin = (const char *)kcontrol->private_value;
return __snd_soc_dapm_get_pin_switch(dapm, pin, ucontrol);
}
EXPORT_SYMBOL_GPL(snd_soc_dapm_get_component_pin_switch);
static int __dapm_put_pin_switch(struct snd_soc_dapm_context *dapm,
const char *pin,
struct snd_ctl_elem_value *ucontrol)
{
int ret;
snd_soc_dapm_mutex_lock(dapm);
ret = __dapm_set_pin(dapm, pin, !!ucontrol->value.integer.value[0]);
snd_soc_dapm_mutex_unlock(dapm);
snd_soc_dapm_sync(dapm);
return ret;
}
int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_value *ucontrol)
{
struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
struct snd_soc_dapm_context *dapm = snd_soc_card_to_dapm(card);
const char *pin = (const char *)kcontrol->private_value;
return __dapm_put_pin_switch(dapm, pin, ucontrol);
}
EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
int snd_soc_dapm_put_component_pin_switch(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_value *ucontrol)
{
struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
struct snd_soc_dapm_context *dapm = snd_soc_component_to_dapm(component);
const char *pin = (const char *)kcontrol->private_value;
return __dapm_put_pin_switch(dapm, pin, ucontrol);
}
EXPORT_SYMBOL_GPL(snd_soc_dapm_put_component_pin_switch);
struct snd_soc_dapm_widget *
snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
const struct snd_soc_dapm_widget *widget)
{
struct device *dev = snd_soc_dapm_to_dev(dapm);
enum snd_soc_dapm_direction dir;
struct snd_soc_dapm_widget *w;
int ret = -ENOMEM;
w = dapm_cnew_widget(widget, dapm_prefix(dapm));
if (!w)
goto cnew_failed;
switch (w->id) {
case snd_soc_dapm_regulator_supply:
w->regulator = devm_regulator_get(dev, widget->name);
if (IS_ERR(w->regulator)) {
ret = PTR_ERR(w->regulator);
goto request_failed;
}
if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
ret = regulator_allow_bypass(w->regulator, true);
if (ret != 0)
dev_warn(dev,
"ASoC: Failed to bypass %s: %d\n",
w->name, ret);
}
break;
case snd_soc_dapm_pinctrl:
w->pinctrl = devm_pinctrl_get(dev);
if (IS_ERR(w->pinctrl)) {
ret = PTR_ERR(w->pinctrl);
goto request_failed;
}
snd_soc_dapm_pinctrl_event(w, NULL, SND_SOC_DAPM_POST_PMD);
break;
case snd_soc_dapm_clock_supply:
w->clk = devm_clk_get(dev, widget->name);
if (IS_ERR(w->clk)) {
ret = PTR_ERR(w->clk);
goto request_failed;
}
break;
default:
break;
}
switch (w->id) {
case snd_soc_dapm_mic:
w->is_ep = SND_SOC_DAPM_EP_SOURCE;
w->power_check = dapm_generic_check_power;
break;
case snd_soc_dapm_input:
if (!dapm->card->fully_routed)
w->is_ep = SND_SOC_DAPM_EP_SOURCE;
w->power_check = dapm_generic_check_power;
break;
case snd_soc_dapm_spk:
case snd_soc_dapm_hp:
w->is_ep = SND_SOC_DAPM_EP_SINK;
w->power_check = dapm_generic_check_power;
break;
case snd_soc_dapm_output:
if (!dapm->card->fully_routed)
w->is_ep = SND_SOC_DAPM_EP_SINK;
w->power_check = dapm_generic_check_power;
break;
case snd_soc_dapm_vmid:
case snd_soc_dapm_siggen:
w->is_ep = SND_SOC_DAPM_EP_SOURCE;
w->power_check = dapm_always_on_check_power;
break;
case snd_soc_dapm_sink:
w->is_ep = SND_SOC_DAPM_EP_SINK;
w->power_check = dapm_always_on_check_power;
break;
case snd_soc_dapm_mux:
case snd_soc_dapm_mux_named_ctl:
case snd_soc_dapm_demux:
case snd_soc_dapm_switch:
case snd_soc_dapm_mixer:
case snd_soc_dapm_mixer_named_ctl:
case snd_soc_dapm_adc:
case snd_soc_dapm_aif_out:
case snd_soc_dapm_dac:
case snd_soc_dapm_aif_in:
case snd_soc_dapm_pga:
case snd_soc_dapm_buffer:
case snd_soc_dapm_scheduler:
case snd_soc_dapm_effect:
case snd_soc_dapm_src:
case snd_soc_dapm_asrc:
case snd_soc_dapm_encoder:
case snd_soc_dapm_decoder:
case snd_soc_dapm_out_drv:
case snd_soc_dapm_micbias:
case snd_soc_dapm_line:
case snd_soc_dapm_dai_link:
case snd_soc_dapm_dai_out:
case snd_soc_dapm_dai_in:
w->power_check = dapm_generic_check_power;
break;
case snd_soc_dapm_supply:
case snd_soc_dapm_regulator_supply:
case snd_soc_dapm_pinctrl:
case snd_soc_dapm_clock_supply:
case snd_soc_dapm_kcontrol:
w->is_supply = 1;
w->power_check = dapm_supply_check_power;
break;
default:
w->power_check = dapm_always_on_check_power;
break;
}
w->dapm = dapm;
INIT_LIST_HEAD(&w->list);
INIT_LIST_HEAD(&w->dirty);
list_add_tail(&w->list, &dapm->card->widgets);
dapm_for_each_direction(dir) {
INIT_LIST_HEAD(&w->edges[dir]);
w->endpoints[dir] = -1;
}
w->connected = 1;
return w;
request_failed:
dev_err_probe(dev, ret, "ASoC: Failed to request %s\n",
w->name);
kfree_const(w->name);
kfree_const(w->sname);
kfree(w);
cnew_failed:
return ERR_PTR(ret);
}
struct snd_soc_dapm_widget *
snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
const struct snd_soc_dapm_widget *widget)
{
struct snd_soc_dapm_widget *w;
snd_soc_dapm_mutex_lock(dapm);
w = snd_soc_dapm_new_control_unlocked(dapm, widget);
snd_soc_dapm_mutex_unlock(dapm);
return w;
}
EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
const struct snd_soc_dapm_widget *widget,
unsigned int num)
{
int i;
int ret = 0;
snd_soc_dapm_mutex_lock_root(dapm);
for (i = 0; i < num; i++) {
struct snd_soc_dapm_widget *w = snd_soc_dapm_new_control_unlocked(dapm, widget);
if (IS_ERR(w)) {
ret = PTR_ERR(w);
break;
}
widget++;
}
snd_soc_dapm_mutex_unlock(dapm);
return ret;
}
EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
static int dapm_dai_link_event_pre_pmu(struct snd_soc_dapm_widget *w,
struct snd_pcm_substream *substream)
{
struct device *dev = snd_soc_dapm_to_dev(w->dapm);
struct snd_soc_dapm_path *path;
struct snd_soc_dai *source, *sink;
struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
const struct snd_soc_pcm_stream *config = NULL;
struct snd_pcm_runtime *runtime = NULL;
unsigned int fmt;
int ret;
struct snd_pcm_hw_params *params __free(kfree) = kzalloc_obj(*params);
if (!params)
return -ENOMEM;
runtime = kzalloc_obj(*runtime);
if (!runtime)
return -ENOMEM;
substream->runtime = runtime;
substream->stream = SNDRV_PCM_STREAM_CAPTURE;
snd_soc_dapm_widget_for_each_source_path(w, path) {
source = path->source->priv;
ret = snd_soc_dai_startup(source, substream);
if (ret < 0)
return ret;
snd_soc_dai_active_update(source, substream->stream, 1);
}
substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
snd_soc_dapm_widget_for_each_sink_path(w, path) {
sink = path->sink->priv;
ret = snd_soc_dai_startup(sink, substream);
if (ret < 0)
return ret;
snd_soc_dai_active_update(sink, substream->stream, 1);
}
substream->hw_opened = 1;
config = rtd->dai_link->c2c_params + rtd->c2c_params_select;
if (!config) {
dev_err(dev, "ASoC: link config missing\n");
return -EINVAL;
}
if (!config->formats) {
dev_warn(dev, "ASoC: Invalid format was specified\n");
return -EINVAL;
}
fmt = ffs(config->formats) - 1;
snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
config->rate_min;
hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
config->rate_max;
hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
= config->channels_min;
hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
= config->channels_max;
substream->stream = SNDRV_PCM_STREAM_CAPTURE;
snd_soc_dapm_widget_for_each_source_path(w, path) {
source = path->source->priv;
ret = snd_soc_dai_hw_params(source, substream, params);
if (ret < 0)
return ret;
dapm_update_dai_unlocked(substream, params, source);
}
substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
snd_soc_dapm_widget_for_each_sink_path(w, path) {
sink = path->sink->priv;
ret = snd_soc_dai_hw_params(sink, substream, params);
if (ret < 0)
return ret;
dapm_update_dai_unlocked(substream, params, sink);
}
runtime->format = params_format(params);
runtime->subformat = params_subformat(params);
runtime->channels = params_channels(params);
runtime->rate = params_rate(params);
return 0;
}
static int dapm_dai_link_event(struct snd_soc_dapm_widget *w,
struct snd_kcontrol *kcontrol, int event)
{
struct snd_soc_dapm_path *path;
struct snd_soc_dai *source, *sink;
struct snd_pcm_substream *substream = w->priv;
int ret = 0, saved_stream = substream->stream;
if (WARN_ON(list_empty(&w->edges[SND_SOC_DAPM_DIR_OUT]) ||
list_empty(&w->edges[SND_SOC_DAPM_DIR_IN])))
return -EINVAL;
switch (event) {
case SND_SOC_DAPM_PRE_PMU:
ret = dapm_dai_link_event_pre_pmu(w, substream);
if (ret < 0)
goto out;
break;
case SND_SOC_DAPM_POST_PMU:
snd_soc_dapm_widget_for_each_source_path(w, path) {
source = path->source->priv;
snd_soc_dai_prepare(source, substream);
}
snd_soc_dapm_widget_for_each_sink_path(w, path) {
sink = path->sink->priv;
snd_soc_dai_prepare(sink, substream);
}
snd_soc_dapm_widget_for_each_sink_path(w, path) {
sink = path->sink->priv;
snd_soc_dai_digital_mute(sink, 0, SNDRV_PCM_STREAM_PLAYBACK);
ret = 0;
}
break;
case SND_SOC_DAPM_PRE_PMD:
snd_soc_dapm_widget_for_each_sink_path(w, path) {
sink = path->sink->priv;
snd_soc_dai_digital_mute(sink, 1, SNDRV_PCM_STREAM_PLAYBACK);
ret = 0;
}
substream->stream = SNDRV_PCM_STREAM_CAPTURE;
snd_soc_dapm_widget_for_each_source_path(w, path) {
source = path->source->priv;
snd_soc_dai_hw_free(source, substream, 0);
}
substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
snd_soc_dapm_widget_for_each_sink_path(w, path) {
sink = path->sink->priv;
snd_soc_dai_hw_free(sink, substream, 0);
}
substream->stream = SNDRV_PCM_STREAM_CAPTURE;
snd_soc_dapm_widget_for_each_source_path(w, path) {
source = path->source->priv;
snd_soc_dai_active_update(source, substream->stream, -1);
snd_soc_dai_shutdown(source, substream, 0);
}
substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
snd_soc_dapm_widget_for_each_sink_path(w, path) {
sink = path->sink->priv;
snd_soc_dai_active_update(sink, substream->stream, -1);
snd_soc_dai_shutdown(sink, substream, 0);
}
break;
case SND_SOC_DAPM_POST_PMD:
kfree(substream->runtime);
substream->runtime = NULL;
break;
default:
WARN(1, "Unknown event %d\n", event);
ret = -EINVAL;
}
out:
substream->stream = saved_stream;
return ret;
}
static int dapm_dai_link_get(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_value *ucontrol)
{
struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
struct snd_soc_pcm_runtime *rtd = w->priv;
ucontrol->value.enumerated.item[0] = rtd->c2c_params_select;
return 0;
}
static int dapm_dai_link_put(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_value *ucontrol)
{
struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
struct snd_soc_pcm_runtime *rtd = w->priv;
if (w->power)
return -EBUSY;
if (ucontrol->value.enumerated.item[0] == rtd->c2c_params_select)
return 0;
if (ucontrol->value.enumerated.item[0] >= rtd->dai_link->num_c2c_params)
return -EINVAL;
rtd->c2c_params_select = ucontrol->value.enumerated.item[0];
return 1;
}
static void dapm_free_kcontrol(struct snd_soc_card *card,
unsigned long *private_value,
int num_c2c_params,
const char **w_param_text)
{
int count;
devm_kfree(card->dev, (void *)*private_value);
if (!w_param_text)
return;
for (count = 0 ; count < num_c2c_params; count++)
devm_kfree(card->dev, (void *)w_param_text[count]);
devm_kfree(card->dev, w_param_text);
}
static struct snd_kcontrol_new *
dapm_alloc_kcontrol(struct snd_soc_card *card,
char *link_name,
const struct snd_soc_pcm_stream *c2c_params,
int num_c2c_params, const char **w_param_text,
unsigned long *private_value)
{
struct soc_enum w_param_enum[] = {
SOC_ENUM_SINGLE(0, 0, 0, NULL),
};
struct snd_kcontrol_new kcontrol_dai_link[] = {
SOC_ENUM_EXT(NULL, w_param_enum[0],
dapm_dai_link_get,
dapm_dai_link_put),
};
struct snd_kcontrol_new *kcontrol_news;
const struct snd_soc_pcm_stream *config = c2c_params;
int count;
for (count = 0 ; count < num_c2c_params; count++) {
if (!config->stream_name) {
dev_warn(card->dev,
"ASoC: anonymous config %d for dai link %s\n",
count, link_name);
w_param_text[count] =
devm_kasprintf(card->dev, GFP_KERNEL,
"Anonymous Configuration %d",
count);
} else {
w_param_text[count] = devm_kmemdup(card->dev,
config->stream_name,
strlen(config->stream_name) + 1,
GFP_KERNEL);
}
if (!w_param_text[count])
goto outfree_w_param;
config++;
}
w_param_enum[0].items = num_c2c_params;
w_param_enum[0].texts = w_param_text;
*private_value =
(unsigned long) devm_kmemdup(card->dev,
(void *)(kcontrol_dai_link[0].private_value),
sizeof(struct soc_enum), GFP_KERNEL);
if (!*private_value) {
dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
link_name);
goto outfree_w_param;
}
kcontrol_dai_link[0].private_value = *private_value;
kcontrol_news = devm_kmemdup(card->dev, &kcontrol_dai_link[0],
sizeof(struct snd_kcontrol_new),
GFP_KERNEL);
if (!kcontrol_news) {
dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
link_name);
goto outfree_w_param;
}
return kcontrol_news;
outfree_w_param:
dapm_free_kcontrol(card, private_value, num_c2c_params, w_param_text);
return NULL;
}
static struct snd_soc_dapm_widget *dapm_new_dai(struct snd_soc_card *card,
struct snd_pcm_substream *substream,
char *id)
{
struct snd_soc_dapm_context *dapm = snd_soc_card_to_dapm(card);
struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
struct snd_soc_dapm_widget template;
struct snd_soc_dapm_widget *w;
const struct snd_kcontrol_new *kcontrol_news;
int num_kcontrols;
const char **w_param_text;
unsigned long private_value = 0;
char *link_name;
int ret = -ENOMEM;
link_name = devm_kasprintf(card->dev, GFP_KERNEL, "%s-%s",
rtd->dai_link->name, id);
if (!link_name)
goto name_fail;
w_param_text = NULL;
kcontrol_news = NULL;
num_kcontrols = 0;
if (rtd->dai_link->num_c2c_params > 1) {
w_param_text = devm_kcalloc(card->dev,
rtd->dai_link->num_c2c_params,
sizeof(char *), GFP_KERNEL);
if (!w_param_text)
goto param_fail;
num_kcontrols = 1;
kcontrol_news = dapm_alloc_kcontrol(card, link_name,
rtd->dai_link->c2c_params,
rtd->dai_link->num_c2c_params,
w_param_text, &private_value);
if (!kcontrol_news)
goto param_fail;
}
memset(&template, 0, sizeof(template));
template.reg = SND_SOC_NOPM;
template.id = snd_soc_dapm_dai_link;
template.name = link_name;
template.event = dapm_dai_link_event;
template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD;
template.kcontrol_news = kcontrol_news;
template.num_kcontrols = num_kcontrols;
dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
w = snd_soc_dapm_new_control_unlocked(dapm, &template);
if (IS_ERR(w)) {
ret = PTR_ERR(w);
goto outfree_kcontrol_news;
}
w->priv = substream;
return w;
outfree_kcontrol_news:
devm_kfree(card->dev, (void *)template.kcontrol_news);
dapm_free_kcontrol(card, &private_value,
rtd->dai_link->num_c2c_params, w_param_text);
param_fail:
devm_kfree(card->dev, link_name);
name_fail:
dev_err(rtd->dev, "ASoC: Failed to create %s-%s widget: %d\n",
rtd->dai_link->name, id, ret);
return ERR_PTR(ret);
}
int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
struct snd_soc_dai *dai)
{
struct device *dev = snd_soc_dapm_to_dev(dapm);
struct snd_soc_dapm_widget template;
struct snd_soc_dapm_widget *w;
WARN_ON(dev != dai->dev);
memset(&template, 0, sizeof(template));
template.reg = SND_SOC_NOPM;
if (dai->driver->playback.stream_name) {
template.id = snd_soc_dapm_dai_in;
template.name = dai->driver->playback.stream_name;
template.sname = dai->driver->playback.stream_name;
dev_dbg(dai->dev, "ASoC: adding %s widget\n",
template.name);
w = snd_soc_dapm_new_control_unlocked(dapm, &template);
if (IS_ERR(w))
return PTR_ERR(w);
w->priv = dai;
snd_soc_dai_set_widget_playback(dai, w);
}
if (dai->driver->capture.stream_name) {
template.id = snd_soc_dapm_dai_out;
template.name = dai->driver->capture.stream_name;
template.sname = dai->driver->capture.stream_name;
dev_dbg(dai->dev, "ASoC: adding %s widget\n",
template.name);
w = snd_soc_dapm_new_control_unlocked(dapm, &template);
if (IS_ERR(w))
return PTR_ERR(w);
w->priv = dai;
snd_soc_dai_set_widget_capture(dai, w);
}
return 0;
}
EXPORT_SYMBOL_GPL(snd_soc_dapm_new_dai_widgets);
int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
{
struct snd_soc_dapm_widget *dai_w, *w;
struct snd_soc_dapm_widget *src, *sink;
struct snd_soc_dai *dai;
for_each_card_widgets(card, dai_w) {
switch (dai_w->id) {
case snd_soc_dapm_dai_in:
case snd_soc_dapm_dai_out:
break;
default:
continue;
}
if (!dai_w->priv) {
dev_dbg(card->dev, "dai widget %s has no DAI\n",
dai_w->name);
continue;
}
dai = dai_w->priv;
for_each_card_widgets(card, w) {
if (w->dapm != dai_w->dapm)
continue;
switch (w->id) {
case snd_soc_dapm_dai_in:
case snd_soc_dapm_dai_out:
continue;
default:
break;
}
if (!w->sname || !strstr(w->sname, dai_w->sname))
continue;
if (dai_w->id == snd_soc_dapm_dai_in) {
src = dai_w;
sink = w;
} else {
src = w;
sink = dai_w;
}
dev_dbg(dai->dev, "%s -> %s\n", src->name, sink->name);
dapm_add_path(w->dapm, src, sink, NULL, NULL);
}
}
return 0;
}
static void dapm_connect_dai_routes(struct snd_soc_dapm_context *dapm,
struct snd_soc_dai *src_dai,
struct snd_soc_dapm_widget *src,
struct snd_soc_dapm_widget *dai,
struct snd_soc_dai *sink_dai,
struct snd_soc_dapm_widget *sink)
{
struct device *dev = snd_soc_dapm_to_dev(dapm);
dev_dbg(dev, "connected DAI link %s:%s -> %s:%s\n",
src_dai->component->name, src->name,
sink_dai->component->name, sink->name);
if (dai) {
dapm_add_path(dapm, src, dai, NULL, NULL);
src = dai;
}
dapm_add_path(dapm, src, sink, NULL, NULL);
}
static void dapm_connect_dai_pair(struct snd_soc_card *card,
struct snd_soc_pcm_runtime *rtd,
struct snd_soc_dai *codec_dai,
struct snd_soc_dai *cpu_dai)
{
struct snd_soc_dapm_context *dapm = snd_soc_card_to_dapm(card);
struct snd_soc_dai_link *dai_link = rtd->dai_link;
struct snd_soc_dapm_widget *codec, *cpu;
struct snd_soc_dai *src_dai[] = { cpu_dai, codec_dai };
struct snd_soc_dai *sink_dai[] = { codec_dai, cpu_dai };
struct snd_soc_dapm_widget **src[] = { &cpu, &codec };
struct snd_soc_dapm_widget **sink[] = { &codec, &cpu };
char *widget_name[] = { "playback", "capture" };
int stream;
for_each_pcm_streams(stream) {
int stream_cpu, stream_codec;
stream_cpu = snd_soc_get_stream_cpu(dai_link, stream);
stream_codec = stream;
cpu = snd_soc_dai_get_widget(cpu_dai, stream_cpu);
codec = snd_soc_dai_get_widget(codec_dai, stream_codec);
if (!cpu || !codec)
continue;
if (dai_link->c2c_params && !rtd->c2c_widget[stream]) {
struct snd_pcm_substream *substream = rtd->pcm->streams[stream].substream;
struct snd_soc_dapm_widget *dai = dapm_new_dai(card, substream,
widget_name[stream]);
if (IS_ERR(dai))
continue;
rtd->c2c_widget[stream] = dai;
}
dapm_connect_dai_routes(dapm, src_dai[stream], *src[stream],
rtd->c2c_widget[stream],
sink_dai[stream], *sink[stream]);
}
}
static void dapm_dai_stream_event(struct snd_soc_dai *dai, int stream, int event)
{
struct snd_soc_dapm_widget *w;
w = snd_soc_dai_get_widget(dai, stream);
if (w) {
unsigned int ep;
dapm_mark_dirty(w, "stream event");
if (w->id == snd_soc_dapm_dai_in) {
ep = SND_SOC_DAPM_EP_SOURCE;
dapm_widget_invalidate_input_paths(w);
} else {
ep = SND_SOC_DAPM_EP_SINK;
dapm_widget_invalidate_output_paths(w);
}
switch (event) {
case SND_SOC_DAPM_STREAM_START:
w->active = 1;
w->is_ep = ep;
break;
case SND_SOC_DAPM_STREAM_STOP:
w->active = 0;
w->is_ep = 0;
break;
case SND_SOC_DAPM_STREAM_SUSPEND:
case SND_SOC_DAPM_STREAM_RESUME:
case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
break;
}
}
}
void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
{
struct snd_soc_pcm_runtime *rtd;
struct snd_soc_dai *cpu_dai;
struct snd_soc_dai *codec_dai;
for_each_card_rtds(card, rtd) {
struct snd_soc_dai_link_ch_map *ch_maps;
int i;
if (rtd->dai_link->dynamic)
continue;
for_each_rtd_ch_maps(rtd, i, ch_maps) {
cpu_dai = snd_soc_rtd_to_cpu(rtd, ch_maps->cpu);
codec_dai = snd_soc_rtd_to_codec(rtd, ch_maps->codec);
dapm_connect_dai_pair(card, rtd, codec_dai, cpu_dai);
}
}
}
int snd_soc_dapm_ignore_suspend_widgets(struct snd_soc_card *card)
{
struct snd_soc_dapm_widget *w;
int i;
for (i = 0; i < card->num_ignore_suspend_widgets; i++) {
w = dapm_find_widget(snd_soc_card_to_dapm(card),
card->ignore_suspend_widgets[i], true);
if (!w) {
dev_err(card->dev, "ASoC: DAPM unknown ignore suspend widget %s\n",
card->ignore_suspend_widgets[i]);
return -EINVAL;
}
w->ignore_suspend = 1;
}
for (i = 0; i < card->num_of_ignore_suspend_widgets; i++) {
w = dapm_find_widget(snd_soc_card_to_dapm(card),
card->of_ignore_suspend_widgets[i], true);
if (!w) {
dev_err(card->dev, "ASoC: DAPM unknown ignore suspend widget %s\n",
card->of_ignore_suspend_widgets[i]);
return -EINVAL;
}
w->ignore_suspend = 1;
}
return 0;
}
static void dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream, int event)
{
struct snd_soc_dai *dai;
int i;
for_each_rtd_dais(rtd, i, dai)
dapm_dai_stream_event(dai, stream, event);
dapm_power_widgets(rtd->card, event, NULL);
}
void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
int event)
{
struct snd_soc_card *card = rtd->card;
snd_soc_dapm_mutex_lock(card);
dapm_stream_event(rtd, stream, event);
snd_soc_dapm_mutex_unlock(card);
}
void snd_soc_dapm_stream_stop(struct snd_soc_pcm_runtime *rtd, int stream)
{
if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
if (snd_soc_runtime_ignore_pmdown_time(rtd)) {
snd_soc_dapm_stream_event(rtd,
SNDRV_PCM_STREAM_PLAYBACK,
SND_SOC_DAPM_STREAM_STOP);
} else {
rtd->pop_wait = 1;
queue_delayed_work(system_power_efficient_wq,
&rtd->delayed_work,
msecs_to_jiffies(rtd->pmdown_time));
}
} else {
snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
SND_SOC_DAPM_STREAM_STOP);
}
}
EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_stop);
int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
const char *pin)
{
return dapm_set_pin(dapm, pin, 1);
}
EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked);
int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
{
int ret;
snd_soc_dapm_mutex_lock(dapm);
ret = dapm_set_pin(dapm, pin, 1);
snd_soc_dapm_mutex_unlock(dapm);
return ret;
}
EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
const char *pin)
{
struct device *dev;
struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
if (!w) {
dev = snd_soc_dapm_to_dev(dapm);
dev_err(dev, "ASoC: unknown pin %s\n", pin);
return -EINVAL;
}
dev = snd_soc_dapm_to_dev(w->dapm);
dev_dbg(dev, "ASoC: force enable pin %s\n", pin);
if (!w->connected) {
dapm_widget_invalidate_input_paths(w);
dapm_widget_invalidate_output_paths(w);
w->connected = 1;
}
w->force = 1;
dapm_mark_dirty(w, "force enable");
return 0;
}
EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked);
int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
const char *pin)
{
int ret;
snd_soc_dapm_mutex_lock(dapm);
ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
snd_soc_dapm_mutex_unlock(dapm);
return ret;
}
EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm,
const char *pin)
{
return dapm_set_pin(dapm, pin, 0);
}
EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked);
int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
const char *pin)
{
int ret;
snd_soc_dapm_mutex_lock(dapm);
ret = dapm_set_pin(dapm, pin, 0);
snd_soc_dapm_mutex_unlock(dapm);
return ret;
}
EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
const char *pin)
{
struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
if (w)
return w->connected;
return 0;
}
EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
const char *pin)
{
struct device *dev = snd_soc_dapm_to_dev(dapm);
struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
if (!w) {
dev_err(dev, "ASoC: unknown pin %s\n", pin);
return -EINVAL;
}
w->ignore_suspend = 1;
return 0;
}
EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
bool snd_soc_dapm_pin_has_prefix(struct snd_soc_card *card, const char *pin)
{
struct snd_soc_component *component;
const char *prefix;
size_t prefix_len;
for_each_card_components(card, component) {
prefix = component->name_prefix;
if (!prefix)
continue;
prefix_len = strlen(prefix);
if (!strncmp(pin, prefix, prefix_len) && pin[prefix_len] == ' ')
return true;
}
return false;
}
EXPORT_SYMBOL_GPL(snd_soc_dapm_pin_has_prefix);
void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
{
dapm_debugfs_cleanup(dapm);
dapm_free_widgets(dapm);
list_del(&dapm->list);
}
void snd_soc_dapm_init(struct snd_soc_dapm_context *dapm,
struct snd_soc_card *card,
struct snd_soc_component *component)
{
dapm->card = card;
dapm->component = component;
dapm->bias_level = SND_SOC_BIAS_OFF;
if (component)
dapm->idle_bias = component->driver->idle_bias_on;
INIT_LIST_HEAD(&dapm->list);
list_add(&dapm->list, &card->dapm_list);
}
static void dapm_shutdown(struct snd_soc_dapm_context *dapm)
{
struct snd_soc_card *card = dapm->card;
struct snd_soc_dapm_widget *w;
LIST_HEAD(down_list);
int powerdown = 0;
snd_soc_dapm_mutex_lock_root(card);
for_each_card_widgets(dapm->card, w) {
if (w->dapm != dapm)
continue;
if (w->power) {
dapm_seq_insert(w, &down_list, false);
w->new_power = 0;
powerdown = 1;
}
}
if (powerdown) {
if (dapm->bias_level == SND_SOC_BIAS_ON)
snd_soc_dapm_set_bias_level(dapm,
SND_SOC_BIAS_PREPARE);
dapm_seq_run(card, &down_list, 0, false);
if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
snd_soc_dapm_set_bias_level(dapm,
SND_SOC_BIAS_STANDBY);
}
snd_soc_dapm_mutex_unlock(card);
}
void snd_soc_dapm_shutdown(struct snd_soc_card *card)
{
struct snd_soc_dapm_context *card_dapm = snd_soc_card_to_dapm(card);
struct snd_soc_dapm_context *dapm;
for_each_card_dapms(card, dapm) {
if (dapm != card_dapm) {
dapm_shutdown(dapm);
if (dapm->bias_level == SND_SOC_BIAS_STANDBY)
snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_OFF);
}
}
dapm_shutdown(card_dapm);
if (card_dapm->bias_level == SND_SOC_BIAS_STANDBY)
snd_soc_dapm_set_bias_level(card_dapm, SND_SOC_BIAS_OFF);
}
MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
MODULE_LICENSE("GPL");