hda_codec.c 158 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968
  1. /*
  2. * Universal Interface for Intel High Definition Audio Codec
  3. *
  4. * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
  5. *
  6. *
  7. * This driver is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This driver is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/mm.h>
  22. #include <linux/init.h>
  23. #include <linux/delay.h>
  24. #include <linux/slab.h>
  25. #include <linux/mutex.h>
  26. #include <linux/module.h>
  27. #include <linux/async.h>
  28. #include <sound/core.h>
  29. #include "hda_codec.h"
  30. #include <sound/asoundef.h>
  31. #include <sound/tlv.h>
  32. #include <sound/initval.h>
  33. #include <sound/jack.h>
  34. #include "hda_local.h"
  35. #include "hda_beep.h"
  36. #include "hda_jack.h"
  37. #include <sound/hda_hwdep.h>
  38. #define CREATE_TRACE_POINTS
  39. #include "hda_trace.h"
  40. /*
  41. * vendor / preset table
  42. */
  43. struct hda_vendor_id {
  44. unsigned int id;
  45. const char *name;
  46. };
  47. /* codec vendor labels */
  48. static struct hda_vendor_id hda_vendor_ids[] = {
  49. { 0x1002, "ATI" },
  50. { 0x1013, "Cirrus Logic" },
  51. { 0x1057, "Motorola" },
  52. { 0x1095, "Silicon Image" },
  53. { 0x10de, "Nvidia" },
  54. { 0x10ec, "Realtek" },
  55. { 0x1102, "Creative" },
  56. { 0x1106, "VIA" },
  57. { 0x111d, "IDT" },
  58. { 0x11c1, "LSI" },
  59. { 0x11d4, "Analog Devices" },
  60. { 0x13f6, "C-Media" },
  61. { 0x14f1, "Conexant" },
  62. { 0x17e8, "Chrontel" },
  63. { 0x1854, "LG" },
  64. { 0x1aec, "Wolfson Microelectronics" },
  65. { 0x1af4, "QEMU" },
  66. { 0x434d, "C-Media" },
  67. { 0x8086, "Intel" },
  68. { 0x8384, "SigmaTel" },
  69. {} /* terminator */
  70. };
  71. static DEFINE_MUTEX(preset_mutex);
  72. static LIST_HEAD(hda_preset_tables);
  73. /**
  74. * snd_hda_add_codec_preset - Add a codec preset to the chain
  75. * @preset: codec preset table to add
  76. */
  77. int snd_hda_add_codec_preset(struct hda_codec_preset_list *preset)
  78. {
  79. mutex_lock(&preset_mutex);
  80. list_add_tail(&preset->list, &hda_preset_tables);
  81. mutex_unlock(&preset_mutex);
  82. return 0;
  83. }
  84. EXPORT_SYMBOL_GPL(snd_hda_add_codec_preset);
  85. /**
  86. * snd_hda_delete_codec_preset - Delete a codec preset from the chain
  87. * @preset: codec preset table to delete
  88. */
  89. int snd_hda_delete_codec_preset(struct hda_codec_preset_list *preset)
  90. {
  91. mutex_lock(&preset_mutex);
  92. list_del(&preset->list);
  93. mutex_unlock(&preset_mutex);
  94. return 0;
  95. }
  96. EXPORT_SYMBOL_GPL(snd_hda_delete_codec_preset);
  97. #ifdef CONFIG_PM
  98. #define codec_in_pm(codec) ((codec)->in_pm)
  99. static void hda_power_work(struct work_struct *work);
  100. static void hda_keep_power_on(struct hda_codec *codec);
  101. #define hda_codec_is_power_on(codec) ((codec)->power_on)
  102. static void hda_call_pm_notify(struct hda_codec *codec, bool power_up)
  103. {
  104. struct hda_bus *bus = codec->bus;
  105. if ((power_up && codec->pm_up_notified) ||
  106. (!power_up && !codec->pm_up_notified))
  107. return;
  108. if (bus->ops.pm_notify)
  109. bus->ops.pm_notify(bus, power_up);
  110. codec->pm_up_notified = power_up;
  111. }
  112. #else
  113. #define codec_in_pm(codec) 0
  114. static inline void hda_keep_power_on(struct hda_codec *codec) {}
  115. #define hda_codec_is_power_on(codec) 1
  116. #define hda_call_pm_notify(codec, state) {}
  117. #endif
  118. /**
  119. * snd_hda_get_jack_location - Give a location string of the jack
  120. * @cfg: pin default config value
  121. *
  122. * Parse the pin default config value and returns the string of the
  123. * jack location, e.g. "Rear", "Front", etc.
  124. */
  125. const char *snd_hda_get_jack_location(u32 cfg)
  126. {
  127. static char *bases[7] = {
  128. "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
  129. };
  130. static unsigned char specials_idx[] = {
  131. 0x07, 0x08,
  132. 0x17, 0x18, 0x19,
  133. 0x37, 0x38
  134. };
  135. static char *specials[] = {
  136. "Rear Panel", "Drive Bar",
  137. "Riser", "HDMI", "ATAPI",
  138. "Mobile-In", "Mobile-Out"
  139. };
  140. int i;
  141. cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
  142. if ((cfg & 0x0f) < 7)
  143. return bases[cfg & 0x0f];
  144. for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
  145. if (cfg == specials_idx[i])
  146. return specials[i];
  147. }
  148. return "UNKNOWN";
  149. }
  150. EXPORT_SYMBOL_GPL(snd_hda_get_jack_location);
  151. /**
  152. * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
  153. * @cfg: pin default config value
  154. *
  155. * Parse the pin default config value and returns the string of the
  156. * jack connectivity, i.e. external or internal connection.
  157. */
  158. const char *snd_hda_get_jack_connectivity(u32 cfg)
  159. {
  160. static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
  161. return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
  162. }
  163. EXPORT_SYMBOL_GPL(snd_hda_get_jack_connectivity);
  164. /**
  165. * snd_hda_get_jack_type - Give a type string of the jack
  166. * @cfg: pin default config value
  167. *
  168. * Parse the pin default config value and returns the string of the
  169. * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
  170. */
  171. const char *snd_hda_get_jack_type(u32 cfg)
  172. {
  173. static char *jack_types[16] = {
  174. "Line Out", "Speaker", "HP Out", "CD",
  175. "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
  176. "Line In", "Aux", "Mic", "Telephony",
  177. "SPDIF In", "Digital In", "Reserved", "Other"
  178. };
  179. return jack_types[(cfg & AC_DEFCFG_DEVICE)
  180. >> AC_DEFCFG_DEVICE_SHIFT];
  181. }
  182. EXPORT_SYMBOL_GPL(snd_hda_get_jack_type);
  183. /*
  184. * Compose a 32bit command word to be sent to the HD-audio controller
  185. */
  186. static inline unsigned int
  187. make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int flags,
  188. unsigned int verb, unsigned int parm)
  189. {
  190. u32 val;
  191. if ((codec->addr & ~0xf) || (nid & ~0x7f) ||
  192. (verb & ~0xfff) || (parm & ~0xffff)) {
  193. codec_err(codec, "hda-codec: out of range cmd %x:%x:%x:%x\n",
  194. codec->addr, nid, verb, parm);
  195. return ~0;
  196. }
  197. val = (u32)codec->addr << 28;
  198. val |= (u32)nid << 20;
  199. val |= verb << 8;
  200. val |= parm;
  201. return val;
  202. }
  203. /*
  204. * Send and receive a verb
  205. */
  206. static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
  207. int flags, unsigned int *res)
  208. {
  209. struct hda_bus *bus = codec->bus;
  210. int err;
  211. if (cmd == ~0)
  212. return -1;
  213. if (res)
  214. *res = -1;
  215. again:
  216. snd_hda_power_up(codec);
  217. mutex_lock(&bus->cmd_mutex);
  218. if (flags & HDA_RW_NO_RESPONSE_FALLBACK)
  219. bus->no_response_fallback = 1;
  220. for (;;) {
  221. trace_hda_send_cmd(codec, cmd);
  222. err = bus->ops.command(bus, cmd);
  223. if (err != -EAGAIN)
  224. break;
  225. /* process pending verbs */
  226. bus->ops.get_response(bus, codec->addr);
  227. }
  228. if (!err && res) {
  229. *res = bus->ops.get_response(bus, codec->addr);
  230. trace_hda_get_response(codec, *res);
  231. }
  232. bus->no_response_fallback = 0;
  233. mutex_unlock(&bus->cmd_mutex);
  234. snd_hda_power_down(codec);
  235. if (!codec_in_pm(codec) && res && *res == -1 && bus->rirb_error) {
  236. if (bus->response_reset) {
  237. codec_dbg(codec,
  238. "resetting BUS due to fatal communication error\n");
  239. trace_hda_bus_reset(bus);
  240. bus->ops.bus_reset(bus);
  241. }
  242. goto again;
  243. }
  244. /* clear reset-flag when the communication gets recovered */
  245. if (!err || codec_in_pm(codec))
  246. bus->response_reset = 0;
  247. return err;
  248. }
  249. /**
  250. * snd_hda_codec_read - send a command and get the response
  251. * @codec: the HDA codec
  252. * @nid: NID to send the command
  253. * @flags: optional bit flags
  254. * @verb: the verb to send
  255. * @parm: the parameter for the verb
  256. *
  257. * Send a single command and read the corresponding response.
  258. *
  259. * Returns the obtained response value, or -1 for an error.
  260. */
  261. unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
  262. int flags,
  263. unsigned int verb, unsigned int parm)
  264. {
  265. unsigned cmd = make_codec_cmd(codec, nid, flags, verb, parm);
  266. unsigned int res;
  267. if (codec_exec_verb(codec, cmd, flags, &res))
  268. return -1;
  269. return res;
  270. }
  271. EXPORT_SYMBOL_GPL(snd_hda_codec_read);
  272. /**
  273. * snd_hda_codec_write - send a single command without waiting for response
  274. * @codec: the HDA codec
  275. * @nid: NID to send the command
  276. * @flags: optional bit flags
  277. * @verb: the verb to send
  278. * @parm: the parameter for the verb
  279. *
  280. * Send a single command without waiting for response.
  281. *
  282. * Returns 0 if successful, or a negative error code.
  283. */
  284. int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int flags,
  285. unsigned int verb, unsigned int parm)
  286. {
  287. unsigned int cmd = make_codec_cmd(codec, nid, flags, verb, parm);
  288. unsigned int res;
  289. return codec_exec_verb(codec, cmd, flags,
  290. codec->bus->sync_write ? &res : NULL);
  291. }
  292. EXPORT_SYMBOL_GPL(snd_hda_codec_write);
  293. /**
  294. * snd_hda_sequence_write - sequence writes
  295. * @codec: the HDA codec
  296. * @seq: VERB array to send
  297. *
  298. * Send the commands sequentially from the given array.
  299. * The array must be terminated with NID=0.
  300. */
  301. void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
  302. {
  303. for (; seq->nid; seq++)
  304. snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
  305. }
  306. EXPORT_SYMBOL_GPL(snd_hda_sequence_write);
  307. /**
  308. * snd_hda_get_sub_nodes - get the range of sub nodes
  309. * @codec: the HDA codec
  310. * @nid: NID to parse
  311. * @start_id: the pointer to store the start NID
  312. *
  313. * Parse the NID and store the start NID of its sub-nodes.
  314. * Returns the number of sub-nodes.
  315. */
  316. int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
  317. hda_nid_t *start_id)
  318. {
  319. unsigned int parm;
  320. parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
  321. if (parm == -1) {
  322. *start_id = 0;
  323. return 0;
  324. }
  325. *start_id = (parm >> 16) & 0x7fff;
  326. return (int)(parm & 0x7fff);
  327. }
  328. EXPORT_SYMBOL_GPL(snd_hda_get_sub_nodes);
  329. /* connection list element */
  330. struct hda_conn_list {
  331. struct list_head list;
  332. int len;
  333. hda_nid_t nid;
  334. hda_nid_t conns[0];
  335. };
  336. /* look up the cached results */
  337. static struct hda_conn_list *
  338. lookup_conn_list(struct hda_codec *codec, hda_nid_t nid)
  339. {
  340. struct hda_conn_list *p;
  341. list_for_each_entry(p, &codec->conn_list, list) {
  342. if (p->nid == nid)
  343. return p;
  344. }
  345. return NULL;
  346. }
  347. static int add_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
  348. const hda_nid_t *list)
  349. {
  350. struct hda_conn_list *p;
  351. p = kmalloc(sizeof(*p) + len * sizeof(hda_nid_t), GFP_KERNEL);
  352. if (!p)
  353. return -ENOMEM;
  354. p->len = len;
  355. p->nid = nid;
  356. memcpy(p->conns, list, len * sizeof(hda_nid_t));
  357. list_add(&p->list, &codec->conn_list);
  358. return 0;
  359. }
  360. static void remove_conn_list(struct hda_codec *codec)
  361. {
  362. while (!list_empty(&codec->conn_list)) {
  363. struct hda_conn_list *p;
  364. p = list_first_entry(&codec->conn_list, typeof(*p), list);
  365. list_del(&p->list);
  366. kfree(p);
  367. }
  368. }
  369. /* read the connection and add to the cache */
  370. static int read_and_add_raw_conns(struct hda_codec *codec, hda_nid_t nid)
  371. {
  372. hda_nid_t list[32];
  373. hda_nid_t *result = list;
  374. int len;
  375. len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list));
  376. if (len == -ENOSPC) {
  377. len = snd_hda_get_num_raw_conns(codec, nid);
  378. result = kmalloc(sizeof(hda_nid_t) * len, GFP_KERNEL);
  379. if (!result)
  380. return -ENOMEM;
  381. len = snd_hda_get_raw_connections(codec, nid, result, len);
  382. }
  383. if (len >= 0)
  384. len = snd_hda_override_conn_list(codec, nid, len, result);
  385. if (result != list)
  386. kfree(result);
  387. return len;
  388. }
  389. /**
  390. * snd_hda_get_conn_list - get connection list
  391. * @codec: the HDA codec
  392. * @nid: NID to parse
  393. * @listp: the pointer to store NID list
  394. *
  395. * Parses the connection list of the given widget and stores the pointer
  396. * to the list of NIDs.
  397. *
  398. * Returns the number of connections, or a negative error code.
  399. *
  400. * Note that the returned pointer isn't protected against the list
  401. * modification. If snd_hda_override_conn_list() might be called
  402. * concurrently, protect with a mutex appropriately.
  403. */
  404. int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
  405. const hda_nid_t **listp)
  406. {
  407. bool added = false;
  408. for (;;) {
  409. int err;
  410. const struct hda_conn_list *p;
  411. /* if the connection-list is already cached, read it */
  412. p = lookup_conn_list(codec, nid);
  413. if (p) {
  414. if (listp)
  415. *listp = p->conns;
  416. return p->len;
  417. }
  418. if (snd_BUG_ON(added))
  419. return -EINVAL;
  420. err = read_and_add_raw_conns(codec, nid);
  421. if (err < 0)
  422. return err;
  423. added = true;
  424. }
  425. }
  426. EXPORT_SYMBOL_GPL(snd_hda_get_conn_list);
  427. /**
  428. * snd_hda_get_connections - copy connection list
  429. * @codec: the HDA codec
  430. * @nid: NID to parse
  431. * @conn_list: connection list array; when NULL, checks only the size
  432. * @max_conns: max. number of connections to store
  433. *
  434. * Parses the connection list of the given widget and stores the list
  435. * of NIDs.
  436. *
  437. * Returns the number of connections, or a negative error code.
  438. */
  439. int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
  440. hda_nid_t *conn_list, int max_conns)
  441. {
  442. const hda_nid_t *list;
  443. int len = snd_hda_get_conn_list(codec, nid, &list);
  444. if (len > 0 && conn_list) {
  445. if (len > max_conns) {
  446. codec_err(codec, "Too many connections %d for NID 0x%x\n",
  447. len, nid);
  448. return -EINVAL;
  449. }
  450. memcpy(conn_list, list, len * sizeof(hda_nid_t));
  451. }
  452. return len;
  453. }
  454. EXPORT_SYMBOL_GPL(snd_hda_get_connections);
  455. /* return CONNLIST_LEN parameter of the given widget */
  456. static unsigned int get_num_conns(struct hda_codec *codec, hda_nid_t nid)
  457. {
  458. unsigned int wcaps = get_wcaps(codec, nid);
  459. unsigned int parm;
  460. if (!(wcaps & AC_WCAP_CONN_LIST) &&
  461. get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
  462. return 0;
  463. parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
  464. if (parm == -1)
  465. parm = 0;
  466. return parm;
  467. }
  468. int snd_hda_get_num_raw_conns(struct hda_codec *codec, hda_nid_t nid)
  469. {
  470. return snd_hda_get_raw_connections(codec, nid, NULL, 0);
  471. }
  472. /**
  473. * snd_hda_get_raw_connections - copy connection list without cache
  474. * @codec: the HDA codec
  475. * @nid: NID to parse
  476. * @conn_list: connection list array
  477. * @max_conns: max. number of connections to store
  478. *
  479. * Like snd_hda_get_connections(), copy the connection list but without
  480. * checking through the connection-list cache.
  481. * Currently called only from hda_proc.c, so not exported.
  482. */
  483. int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid,
  484. hda_nid_t *conn_list, int max_conns)
  485. {
  486. unsigned int parm;
  487. int i, conn_len, conns;
  488. unsigned int shift, num_elems, mask;
  489. hda_nid_t prev_nid;
  490. int null_count = 0;
  491. parm = get_num_conns(codec, nid);
  492. if (!parm)
  493. return 0;
  494. if (parm & AC_CLIST_LONG) {
  495. /* long form */
  496. shift = 16;
  497. num_elems = 2;
  498. } else {
  499. /* short form */
  500. shift = 8;
  501. num_elems = 4;
  502. }
  503. conn_len = parm & AC_CLIST_LENGTH;
  504. mask = (1 << (shift-1)) - 1;
  505. if (!conn_len)
  506. return 0; /* no connection */
  507. if (conn_len == 1) {
  508. /* single connection */
  509. parm = snd_hda_codec_read(codec, nid, 0,
  510. AC_VERB_GET_CONNECT_LIST, 0);
  511. if (parm == -1 && codec->bus->rirb_error)
  512. return -EIO;
  513. if (conn_list)
  514. conn_list[0] = parm & mask;
  515. return 1;
  516. }
  517. /* multi connection */
  518. conns = 0;
  519. prev_nid = 0;
  520. for (i = 0; i < conn_len; i++) {
  521. int range_val;
  522. hda_nid_t val, n;
  523. if (i % num_elems == 0) {
  524. parm = snd_hda_codec_read(codec, nid, 0,
  525. AC_VERB_GET_CONNECT_LIST, i);
  526. if (parm == -1 && codec->bus->rirb_error)
  527. return -EIO;
  528. }
  529. range_val = !!(parm & (1 << (shift-1))); /* ranges */
  530. val = parm & mask;
  531. if (val == 0 && null_count++) { /* no second chance */
  532. codec_dbg(codec,
  533. "invalid CONNECT_LIST verb %x[%i]:%x\n",
  534. nid, i, parm);
  535. return 0;
  536. }
  537. parm >>= shift;
  538. if (range_val) {
  539. /* ranges between the previous and this one */
  540. if (!prev_nid || prev_nid >= val) {
  541. codec_warn(codec,
  542. "invalid dep_range_val %x:%x\n",
  543. prev_nid, val);
  544. continue;
  545. }
  546. for (n = prev_nid + 1; n <= val; n++) {
  547. if (conn_list) {
  548. if (conns >= max_conns)
  549. return -ENOSPC;
  550. conn_list[conns] = n;
  551. }
  552. conns++;
  553. }
  554. } else {
  555. if (conn_list) {
  556. if (conns >= max_conns)
  557. return -ENOSPC;
  558. conn_list[conns] = val;
  559. }
  560. conns++;
  561. }
  562. prev_nid = val;
  563. }
  564. return conns;
  565. }
  566. /**
  567. * snd_hda_override_conn_list - add/modify the connection-list to cache
  568. * @codec: the HDA codec
  569. * @nid: NID to parse
  570. * @len: number of connection list entries
  571. * @list: the list of connection entries
  572. *
  573. * Add or modify the given connection-list to the cache. If the corresponding
  574. * cache already exists, invalidate it and append a new one.
  575. *
  576. * Returns zero or a negative error code.
  577. */
  578. int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
  579. const hda_nid_t *list)
  580. {
  581. struct hda_conn_list *p;
  582. p = lookup_conn_list(codec, nid);
  583. if (p) {
  584. list_del(&p->list);
  585. kfree(p);
  586. }
  587. return add_conn_list(codec, nid, len, list);
  588. }
  589. EXPORT_SYMBOL_GPL(snd_hda_override_conn_list);
  590. /**
  591. * snd_hda_get_conn_index - get the connection index of the given NID
  592. * @codec: the HDA codec
  593. * @mux: NID containing the list
  594. * @nid: NID to select
  595. * @recursive: 1 when searching NID recursively, otherwise 0
  596. *
  597. * Parses the connection list of the widget @mux and checks whether the
  598. * widget @nid is present. If it is, return the connection index.
  599. * Otherwise it returns -1.
  600. */
  601. int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
  602. hda_nid_t nid, int recursive)
  603. {
  604. const hda_nid_t *conn;
  605. int i, nums;
  606. nums = snd_hda_get_conn_list(codec, mux, &conn);
  607. for (i = 0; i < nums; i++)
  608. if (conn[i] == nid)
  609. return i;
  610. if (!recursive)
  611. return -1;
  612. if (recursive > 10) {
  613. codec_dbg(codec, "too deep connection for 0x%x\n", nid);
  614. return -1;
  615. }
  616. recursive++;
  617. for (i = 0; i < nums; i++) {
  618. unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
  619. if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
  620. continue;
  621. if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
  622. return i;
  623. }
  624. return -1;
  625. }
  626. EXPORT_SYMBOL_GPL(snd_hda_get_conn_index);
  627. /* return DEVLIST_LEN parameter of the given widget */
  628. static unsigned int get_num_devices(struct hda_codec *codec, hda_nid_t nid)
  629. {
  630. unsigned int wcaps = get_wcaps(codec, nid);
  631. unsigned int parm;
  632. if (!codec->dp_mst || !(wcaps & AC_WCAP_DIGITAL) ||
  633. get_wcaps_type(wcaps) != AC_WID_PIN)
  634. return 0;
  635. parm = snd_hda_param_read(codec, nid, AC_PAR_DEVLIST_LEN);
  636. if (parm == -1 && codec->bus->rirb_error)
  637. parm = 0;
  638. return parm & AC_DEV_LIST_LEN_MASK;
  639. }
  640. /**
  641. * snd_hda_get_devices - copy device list without cache
  642. * @codec: the HDA codec
  643. * @nid: NID of the pin to parse
  644. * @dev_list: device list array
  645. * @max_devices: max. number of devices to store
  646. *
  647. * Copy the device list. This info is dynamic and so not cached.
  648. * Currently called only from hda_proc.c, so not exported.
  649. */
  650. int snd_hda_get_devices(struct hda_codec *codec, hda_nid_t nid,
  651. u8 *dev_list, int max_devices)
  652. {
  653. unsigned int parm;
  654. int i, dev_len, devices;
  655. parm = get_num_devices(codec, nid);
  656. if (!parm) /* not multi-stream capable */
  657. return 0;
  658. dev_len = parm + 1;
  659. dev_len = dev_len < max_devices ? dev_len : max_devices;
  660. devices = 0;
  661. while (devices < dev_len) {
  662. parm = snd_hda_codec_read(codec, nid, 0,
  663. AC_VERB_GET_DEVICE_LIST, devices);
  664. if (parm == -1 && codec->bus->rirb_error)
  665. break;
  666. for (i = 0; i < 8; i++) {
  667. dev_list[devices] = (u8)parm;
  668. parm >>= 4;
  669. devices++;
  670. if (devices >= dev_len)
  671. break;
  672. }
  673. }
  674. return devices;
  675. }
  676. /**
  677. * snd_hda_queue_unsol_event - add an unsolicited event to queue
  678. * @bus: the BUS
  679. * @res: unsolicited event (lower 32bit of RIRB entry)
  680. * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
  681. *
  682. * Adds the given event to the queue. The events are processed in
  683. * the workqueue asynchronously. Call this function in the interrupt
  684. * hanlder when RIRB receives an unsolicited event.
  685. *
  686. * Returns 0 if successful, or a negative error code.
  687. */
  688. int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
  689. {
  690. struct hda_bus_unsolicited *unsol;
  691. unsigned int wp;
  692. if (!bus || !bus->workq)
  693. return 0;
  694. trace_hda_unsol_event(bus, res, res_ex);
  695. unsol = bus->unsol;
  696. if (!unsol)
  697. return 0;
  698. wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
  699. unsol->wp = wp;
  700. wp <<= 1;
  701. unsol->queue[wp] = res;
  702. unsol->queue[wp + 1] = res_ex;
  703. queue_work(bus->workq, &unsol->work);
  704. return 0;
  705. }
  706. EXPORT_SYMBOL_GPL(snd_hda_queue_unsol_event);
  707. /*
  708. * process queued unsolicited events
  709. */
  710. static void process_unsol_events(struct work_struct *work)
  711. {
  712. struct hda_bus_unsolicited *unsol =
  713. container_of(work, struct hda_bus_unsolicited, work);
  714. struct hda_bus *bus = unsol->bus;
  715. struct hda_codec *codec;
  716. unsigned int rp, caddr, res;
  717. while (unsol->rp != unsol->wp) {
  718. rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
  719. unsol->rp = rp;
  720. rp <<= 1;
  721. res = unsol->queue[rp];
  722. caddr = unsol->queue[rp + 1];
  723. if (!(caddr & (1 << 4))) /* no unsolicited event? */
  724. continue;
  725. codec = bus->caddr_tbl[caddr & 0x0f];
  726. if (codec && codec->patch_ops.unsol_event)
  727. codec->patch_ops.unsol_event(codec, res);
  728. }
  729. }
  730. /*
  731. * initialize unsolicited queue
  732. */
  733. static int init_unsol_queue(struct hda_bus *bus)
  734. {
  735. struct hda_bus_unsolicited *unsol;
  736. if (bus->unsol) /* already initialized */
  737. return 0;
  738. unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
  739. if (!unsol) {
  740. dev_err(bus->card->dev, "can't allocate unsolicited queue\n");
  741. return -ENOMEM;
  742. }
  743. INIT_WORK(&unsol->work, process_unsol_events);
  744. unsol->bus = bus;
  745. bus->unsol = unsol;
  746. return 0;
  747. }
  748. /*
  749. * destructor
  750. */
  751. static void snd_hda_bus_free(struct hda_bus *bus)
  752. {
  753. if (!bus)
  754. return;
  755. WARN_ON(!list_empty(&bus->codec_list));
  756. if (bus->workq)
  757. flush_workqueue(bus->workq);
  758. kfree(bus->unsol);
  759. if (bus->ops.private_free)
  760. bus->ops.private_free(bus);
  761. if (bus->workq)
  762. destroy_workqueue(bus->workq);
  763. kfree(bus);
  764. }
  765. static int snd_hda_bus_dev_free(struct snd_device *device)
  766. {
  767. snd_hda_bus_free(device->device_data);
  768. return 0;
  769. }
  770. static int snd_hda_bus_dev_disconnect(struct snd_device *device)
  771. {
  772. struct hda_bus *bus = device->device_data;
  773. bus->shutdown = 1;
  774. return 0;
  775. }
  776. /**
  777. * snd_hda_bus_new - create a HDA bus
  778. * @card: the card entry
  779. * @temp: the template for hda_bus information
  780. * @busp: the pointer to store the created bus instance
  781. *
  782. * Returns 0 if successful, or a negative error code.
  783. */
  784. int snd_hda_bus_new(struct snd_card *card,
  785. const struct hda_bus_template *temp,
  786. struct hda_bus **busp)
  787. {
  788. struct hda_bus *bus;
  789. int err;
  790. static struct snd_device_ops dev_ops = {
  791. .dev_disconnect = snd_hda_bus_dev_disconnect,
  792. .dev_free = snd_hda_bus_dev_free,
  793. };
  794. if (snd_BUG_ON(!temp))
  795. return -EINVAL;
  796. if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
  797. return -EINVAL;
  798. if (busp)
  799. *busp = NULL;
  800. bus = kzalloc(sizeof(*bus), GFP_KERNEL);
  801. if (bus == NULL) {
  802. dev_err(card->dev, "can't allocate struct hda_bus\n");
  803. return -ENOMEM;
  804. }
  805. bus->card = card;
  806. bus->private_data = temp->private_data;
  807. bus->pci = temp->pci;
  808. bus->modelname = temp->modelname;
  809. bus->power_save = temp->power_save;
  810. bus->ops = temp->ops;
  811. mutex_init(&bus->cmd_mutex);
  812. mutex_init(&bus->prepare_mutex);
  813. INIT_LIST_HEAD(&bus->codec_list);
  814. snprintf(bus->workq_name, sizeof(bus->workq_name),
  815. "hd-audio%d", card->number);
  816. bus->workq = create_singlethread_workqueue(bus->workq_name);
  817. if (!bus->workq) {
  818. dev_err(card->dev, "cannot create workqueue %s\n",
  819. bus->workq_name);
  820. kfree(bus);
  821. return -ENOMEM;
  822. }
  823. err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
  824. if (err < 0) {
  825. snd_hda_bus_free(bus);
  826. return err;
  827. }
  828. if (busp)
  829. *busp = bus;
  830. return 0;
  831. }
  832. EXPORT_SYMBOL_GPL(snd_hda_bus_new);
  833. #if IS_ENABLED(CONFIG_SND_HDA_GENERIC)
  834. #define is_generic_config(codec) \
  835. (codec->modelname && !strcmp(codec->modelname, "generic"))
  836. #else
  837. #define is_generic_config(codec) 0
  838. #endif
  839. #ifdef MODULE
  840. #define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
  841. #else
  842. #define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
  843. #endif
  844. /*
  845. * find a matching codec preset
  846. */
  847. static const struct hda_codec_preset *
  848. find_codec_preset(struct hda_codec *codec)
  849. {
  850. struct hda_codec_preset_list *tbl;
  851. const struct hda_codec_preset *preset;
  852. unsigned int mod_requested = 0;
  853. again:
  854. mutex_lock(&preset_mutex);
  855. list_for_each_entry(tbl, &hda_preset_tables, list) {
  856. if (!try_module_get(tbl->owner)) {
  857. codec_err(codec, "cannot module_get\n");
  858. continue;
  859. }
  860. for (preset = tbl->preset; preset->id; preset++) {
  861. u32 mask = preset->mask;
  862. if (preset->afg && preset->afg != codec->afg)
  863. continue;
  864. if (preset->mfg && preset->mfg != codec->mfg)
  865. continue;
  866. if (!mask)
  867. mask = ~0;
  868. if (preset->id == (codec->vendor_id & mask) &&
  869. (!preset->rev ||
  870. preset->rev == codec->revision_id)) {
  871. mutex_unlock(&preset_mutex);
  872. codec->owner = tbl->owner;
  873. return preset;
  874. }
  875. }
  876. module_put(tbl->owner);
  877. }
  878. mutex_unlock(&preset_mutex);
  879. if (mod_requested < HDA_MODREQ_MAX_COUNT) {
  880. if (!mod_requested)
  881. request_module("snd-hda-codec-id:%08x",
  882. codec->vendor_id);
  883. else
  884. request_module("snd-hda-codec-id:%04x*",
  885. (codec->vendor_id >> 16) & 0xffff);
  886. mod_requested++;
  887. goto again;
  888. }
  889. return NULL;
  890. }
  891. /*
  892. * get_codec_name - store the codec name
  893. */
  894. static int get_codec_name(struct hda_codec *codec)
  895. {
  896. const struct hda_vendor_id *c;
  897. const char *vendor = NULL;
  898. u16 vendor_id = codec->vendor_id >> 16;
  899. char tmp[16];
  900. if (codec->vendor_name)
  901. goto get_chip_name;
  902. for (c = hda_vendor_ids; c->id; c++) {
  903. if (c->id == vendor_id) {
  904. vendor = c->name;
  905. break;
  906. }
  907. }
  908. if (!vendor) {
  909. sprintf(tmp, "Generic %04x", vendor_id);
  910. vendor = tmp;
  911. }
  912. codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
  913. if (!codec->vendor_name)
  914. return -ENOMEM;
  915. get_chip_name:
  916. if (codec->chip_name)
  917. return 0;
  918. if (codec->preset && codec->preset->name)
  919. codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
  920. else {
  921. sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
  922. codec->chip_name = kstrdup(tmp, GFP_KERNEL);
  923. }
  924. if (!codec->chip_name)
  925. return -ENOMEM;
  926. return 0;
  927. }
  928. /*
  929. * look for an AFG and MFG nodes
  930. */
  931. static void setup_fg_nodes(struct hda_codec *codec)
  932. {
  933. int i, total_nodes, function_id;
  934. hda_nid_t nid;
  935. total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
  936. for (i = 0; i < total_nodes; i++, nid++) {
  937. function_id = snd_hda_param_read(codec, nid,
  938. AC_PAR_FUNCTION_TYPE);
  939. switch (function_id & 0xff) {
  940. case AC_GRP_AUDIO_FUNCTION:
  941. codec->afg = nid;
  942. codec->afg_function_id = function_id & 0xff;
  943. codec->afg_unsol = (function_id >> 8) & 1;
  944. break;
  945. case AC_GRP_MODEM_FUNCTION:
  946. codec->mfg = nid;
  947. codec->mfg_function_id = function_id & 0xff;
  948. codec->mfg_unsol = (function_id >> 8) & 1;
  949. break;
  950. default:
  951. break;
  952. }
  953. }
  954. }
  955. /*
  956. * read widget caps for each widget and store in cache
  957. */
  958. static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
  959. {
  960. int i;
  961. hda_nid_t nid;
  962. codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
  963. &codec->start_nid);
  964. codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
  965. if (!codec->wcaps)
  966. return -ENOMEM;
  967. nid = codec->start_nid;
  968. for (i = 0; i < codec->num_nodes; i++, nid++)
  969. codec->wcaps[i] = snd_hda_param_read(codec, nid,
  970. AC_PAR_AUDIO_WIDGET_CAP);
  971. return 0;
  972. }
  973. /* read all pin default configurations and save codec->init_pins */
  974. static int read_pin_defaults(struct hda_codec *codec)
  975. {
  976. int i;
  977. hda_nid_t nid = codec->start_nid;
  978. for (i = 0; i < codec->num_nodes; i++, nid++) {
  979. struct hda_pincfg *pin;
  980. unsigned int wcaps = get_wcaps(codec, nid);
  981. unsigned int wid_type = get_wcaps_type(wcaps);
  982. if (wid_type != AC_WID_PIN)
  983. continue;
  984. pin = snd_array_new(&codec->init_pins);
  985. if (!pin)
  986. return -ENOMEM;
  987. pin->nid = nid;
  988. pin->cfg = snd_hda_codec_read(codec, nid, 0,
  989. AC_VERB_GET_CONFIG_DEFAULT, 0);
  990. pin->ctrl = snd_hda_codec_read(codec, nid, 0,
  991. AC_VERB_GET_PIN_WIDGET_CONTROL,
  992. 0);
  993. }
  994. return 0;
  995. }
  996. /* look up the given pin config list and return the item matching with NID */
  997. static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
  998. struct snd_array *array,
  999. hda_nid_t nid)
  1000. {
  1001. int i;
  1002. for (i = 0; i < array->used; i++) {
  1003. struct hda_pincfg *pin = snd_array_elem(array, i);
  1004. if (pin->nid == nid)
  1005. return pin;
  1006. }
  1007. return NULL;
  1008. }
  1009. /* set the current pin config value for the given NID.
  1010. * the value is cached, and read via snd_hda_codec_get_pincfg()
  1011. */
  1012. int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
  1013. hda_nid_t nid, unsigned int cfg)
  1014. {
  1015. struct hda_pincfg *pin;
  1016. /* the check below may be invalid when pins are added by a fixup
  1017. * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled
  1018. * for now
  1019. */
  1020. /*
  1021. if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
  1022. return -EINVAL;
  1023. */
  1024. pin = look_up_pincfg(codec, list, nid);
  1025. if (!pin) {
  1026. pin = snd_array_new(list);
  1027. if (!pin)
  1028. return -ENOMEM;
  1029. pin->nid = nid;
  1030. }
  1031. pin->cfg = cfg;
  1032. return 0;
  1033. }
  1034. /**
  1035. * snd_hda_codec_set_pincfg - Override a pin default configuration
  1036. * @codec: the HDA codec
  1037. * @nid: NID to set the pin config
  1038. * @cfg: the pin default config value
  1039. *
  1040. * Override a pin default configuration value in the cache.
  1041. * This value can be read by snd_hda_codec_get_pincfg() in a higher
  1042. * priority than the real hardware value.
  1043. */
  1044. int snd_hda_codec_set_pincfg(struct hda_codec *codec,
  1045. hda_nid_t nid, unsigned int cfg)
  1046. {
  1047. return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
  1048. }
  1049. EXPORT_SYMBOL_GPL(snd_hda_codec_set_pincfg);
  1050. /**
  1051. * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
  1052. * @codec: the HDA codec
  1053. * @nid: NID to get the pin config
  1054. *
  1055. * Get the current pin config value of the given pin NID.
  1056. * If the pincfg value is cached or overridden via sysfs or driver,
  1057. * returns the cached value.
  1058. */
  1059. unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
  1060. {
  1061. struct hda_pincfg *pin;
  1062. #ifdef CONFIG_SND_HDA_RECONFIG
  1063. {
  1064. unsigned int cfg = 0;
  1065. mutex_lock(&codec->user_mutex);
  1066. pin = look_up_pincfg(codec, &codec->user_pins, nid);
  1067. if (pin)
  1068. cfg = pin->cfg;
  1069. mutex_unlock(&codec->user_mutex);
  1070. if (cfg)
  1071. return cfg;
  1072. }
  1073. #endif
  1074. pin = look_up_pincfg(codec, &codec->driver_pins, nid);
  1075. if (pin)
  1076. return pin->cfg;
  1077. pin = look_up_pincfg(codec, &codec->init_pins, nid);
  1078. if (pin)
  1079. return pin->cfg;
  1080. return 0;
  1081. }
  1082. EXPORT_SYMBOL_GPL(snd_hda_codec_get_pincfg);
  1083. /**
  1084. * snd_hda_codec_set_pin_target - remember the current pinctl target value
  1085. * @codec: the HDA codec
  1086. * @nid: pin NID
  1087. * @val: assigned pinctl value
  1088. *
  1089. * This function stores the given value to a pinctl target value in the
  1090. * pincfg table. This isn't always as same as the actually written value
  1091. * but can be referred at any time via snd_hda_codec_get_pin_target().
  1092. */
  1093. int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid,
  1094. unsigned int val)
  1095. {
  1096. struct hda_pincfg *pin;
  1097. pin = look_up_pincfg(codec, &codec->init_pins, nid);
  1098. if (!pin)
  1099. return -EINVAL;
  1100. pin->target = val;
  1101. return 0;
  1102. }
  1103. EXPORT_SYMBOL_GPL(snd_hda_codec_set_pin_target);
  1104. /**
  1105. * snd_hda_codec_get_pin_target - return the current pinctl target value
  1106. * @codec: the HDA codec
  1107. * @nid: pin NID
  1108. */
  1109. int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid)
  1110. {
  1111. struct hda_pincfg *pin;
  1112. pin = look_up_pincfg(codec, &codec->init_pins, nid);
  1113. if (!pin)
  1114. return 0;
  1115. return pin->target;
  1116. }
  1117. EXPORT_SYMBOL_GPL(snd_hda_codec_get_pin_target);
  1118. /**
  1119. * snd_hda_shutup_pins - Shut up all pins
  1120. * @codec: the HDA codec
  1121. *
  1122. * Clear all pin controls to shup up before suspend for avoiding click noise.
  1123. * The controls aren't cached so that they can be resumed properly.
  1124. */
  1125. void snd_hda_shutup_pins(struct hda_codec *codec)
  1126. {
  1127. int i;
  1128. /* don't shut up pins when unloading the driver; otherwise it breaks
  1129. * the default pin setup at the next load of the driver
  1130. */
  1131. if (codec->bus->shutdown)
  1132. return;
  1133. for (i = 0; i < codec->init_pins.used; i++) {
  1134. struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
  1135. /* use read here for syncing after issuing each verb */
  1136. snd_hda_codec_read(codec, pin->nid, 0,
  1137. AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
  1138. }
  1139. codec->pins_shutup = 1;
  1140. }
  1141. EXPORT_SYMBOL_GPL(snd_hda_shutup_pins);
  1142. #ifdef CONFIG_PM
  1143. /* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
  1144. static void restore_shutup_pins(struct hda_codec *codec)
  1145. {
  1146. int i;
  1147. if (!codec->pins_shutup)
  1148. return;
  1149. if (codec->bus->shutdown)
  1150. return;
  1151. for (i = 0; i < codec->init_pins.used; i++) {
  1152. struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
  1153. snd_hda_codec_write(codec, pin->nid, 0,
  1154. AC_VERB_SET_PIN_WIDGET_CONTROL,
  1155. pin->ctrl);
  1156. }
  1157. codec->pins_shutup = 0;
  1158. }
  1159. #endif
  1160. static void hda_jackpoll_work(struct work_struct *work)
  1161. {
  1162. struct hda_codec *codec =
  1163. container_of(work, struct hda_codec, jackpoll_work.work);
  1164. snd_hda_jack_set_dirty_all(codec);
  1165. snd_hda_jack_poll_all(codec);
  1166. if (!codec->jackpoll_interval)
  1167. return;
  1168. queue_delayed_work(codec->bus->workq, &codec->jackpoll_work,
  1169. codec->jackpoll_interval);
  1170. }
  1171. static void init_hda_cache(struct hda_cache_rec *cache,
  1172. unsigned int record_size);
  1173. static void free_hda_cache(struct hda_cache_rec *cache);
  1174. /* release all pincfg lists */
  1175. static void free_init_pincfgs(struct hda_codec *codec)
  1176. {
  1177. snd_array_free(&codec->driver_pins);
  1178. #ifdef CONFIG_SND_HDA_RECONFIG
  1179. snd_array_free(&codec->user_pins);
  1180. #endif
  1181. snd_array_free(&codec->init_pins);
  1182. }
  1183. /*
  1184. * audio-converter setup caches
  1185. */
  1186. struct hda_cvt_setup {
  1187. hda_nid_t nid;
  1188. u8 stream_tag;
  1189. u8 channel_id;
  1190. u16 format_id;
  1191. unsigned char active; /* cvt is currently used */
  1192. unsigned char dirty; /* setups should be cleared */
  1193. };
  1194. /* get or create a cache entry for the given audio converter NID */
  1195. static struct hda_cvt_setup *
  1196. get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
  1197. {
  1198. struct hda_cvt_setup *p;
  1199. int i;
  1200. for (i = 0; i < codec->cvt_setups.used; i++) {
  1201. p = snd_array_elem(&codec->cvt_setups, i);
  1202. if (p->nid == nid)
  1203. return p;
  1204. }
  1205. p = snd_array_new(&codec->cvt_setups);
  1206. if (p)
  1207. p->nid = nid;
  1208. return p;
  1209. }
  1210. /*
  1211. * Dynamic symbol binding for the codec parsers
  1212. */
  1213. #define load_parser(codec, sym) \
  1214. ((codec)->parser = (int (*)(struct hda_codec *))symbol_request(sym))
  1215. static void unload_parser(struct hda_codec *codec)
  1216. {
  1217. if (codec->parser)
  1218. symbol_put_addr(codec->parser);
  1219. codec->parser = NULL;
  1220. }
  1221. /*
  1222. * codec destructor
  1223. */
  1224. static void snd_hda_codec_free(struct hda_codec *codec)
  1225. {
  1226. if (!codec)
  1227. return;
  1228. cancel_delayed_work_sync(&codec->jackpoll_work);
  1229. snd_hda_jack_tbl_clear(codec);
  1230. free_init_pincfgs(codec);
  1231. #ifdef CONFIG_PM
  1232. cancel_delayed_work(&codec->power_work);
  1233. flush_workqueue(codec->bus->workq);
  1234. #endif
  1235. list_del(&codec->list);
  1236. snd_array_free(&codec->mixers);
  1237. snd_array_free(&codec->nids);
  1238. snd_array_free(&codec->cvt_setups);
  1239. snd_array_free(&codec->spdif_out);
  1240. remove_conn_list(codec);
  1241. codec->bus->caddr_tbl[codec->addr] = NULL;
  1242. if (codec->patch_ops.free)
  1243. codec->patch_ops.free(codec);
  1244. hda_call_pm_notify(codec, false); /* cancel leftover refcounts */
  1245. snd_hda_sysfs_clear(codec);
  1246. unload_parser(codec);
  1247. module_put(codec->owner);
  1248. free_hda_cache(&codec->amp_cache);
  1249. free_hda_cache(&codec->cmd_cache);
  1250. kfree(codec->vendor_name);
  1251. kfree(codec->chip_name);
  1252. kfree(codec->modelname);
  1253. kfree(codec->wcaps);
  1254. codec->bus->num_codecs--;
  1255. put_device(&codec->dev);
  1256. }
  1257. static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec,
  1258. hda_nid_t fg, unsigned int power_state);
  1259. static unsigned int hda_set_power_state(struct hda_codec *codec,
  1260. unsigned int power_state);
  1261. static int snd_hda_codec_dev_register(struct snd_device *device)
  1262. {
  1263. struct hda_codec *codec = device->device_data;
  1264. int err = device_add(&codec->dev);
  1265. if (err < 0)
  1266. return err;
  1267. snd_hda_register_beep_device(codec);
  1268. return 0;
  1269. }
  1270. static int snd_hda_codec_dev_disconnect(struct snd_device *device)
  1271. {
  1272. struct hda_codec *codec = device->device_data;
  1273. snd_hda_detach_beep_device(codec);
  1274. device_del(&codec->dev);
  1275. return 0;
  1276. }
  1277. static int snd_hda_codec_dev_free(struct snd_device *device)
  1278. {
  1279. snd_hda_codec_free(device->device_data);
  1280. return 0;
  1281. }
  1282. /* just free the container */
  1283. static void snd_hda_codec_dev_release(struct device *dev)
  1284. {
  1285. kfree(container_of(dev, struct hda_codec, dev));
  1286. }
  1287. /**
  1288. * snd_hda_codec_new - create a HDA codec
  1289. * @bus: the bus to assign
  1290. * @codec_addr: the codec address
  1291. * @codecp: the pointer to store the generated codec
  1292. *
  1293. * Returns 0 if successful, or a negative error code.
  1294. */
  1295. int snd_hda_codec_new(struct hda_bus *bus,
  1296. unsigned int codec_addr,
  1297. struct hda_codec **codecp)
  1298. {
  1299. struct hda_codec *codec;
  1300. char component[31];
  1301. hda_nid_t fg;
  1302. int err;
  1303. static struct snd_device_ops dev_ops = {
  1304. .dev_register = snd_hda_codec_dev_register,
  1305. .dev_disconnect = snd_hda_codec_dev_disconnect,
  1306. .dev_free = snd_hda_codec_dev_free,
  1307. };
  1308. if (snd_BUG_ON(!bus))
  1309. return -EINVAL;
  1310. if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
  1311. return -EINVAL;
  1312. if (bus->caddr_tbl[codec_addr]) {
  1313. dev_err(bus->card->dev,
  1314. "address 0x%x is already occupied\n",
  1315. codec_addr);
  1316. return -EBUSY;
  1317. }
  1318. codec = kzalloc(sizeof(*codec), GFP_KERNEL);
  1319. if (codec == NULL) {
  1320. dev_err(bus->card->dev, "can't allocate struct hda_codec\n");
  1321. return -ENOMEM;
  1322. }
  1323. device_initialize(&codec->dev);
  1324. codec->dev.parent = &bus->card->card_dev;
  1325. codec->dev.class = sound_class;
  1326. codec->dev.release = snd_hda_codec_dev_release;
  1327. codec->dev.groups = snd_hda_dev_attr_groups;
  1328. dev_set_name(&codec->dev, "hdaudioC%dD%d", bus->card->number,
  1329. codec_addr);
  1330. dev_set_drvdata(&codec->dev, codec); /* for sysfs */
  1331. codec->bus = bus;
  1332. codec->addr = codec_addr;
  1333. mutex_init(&codec->spdif_mutex);
  1334. mutex_init(&codec->control_mutex);
  1335. mutex_init(&codec->hash_mutex);
  1336. init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
  1337. init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
  1338. snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
  1339. snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
  1340. snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
  1341. snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
  1342. snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
  1343. snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
  1344. snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16);
  1345. snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8);
  1346. INIT_LIST_HEAD(&codec->conn_list);
  1347. INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
  1348. codec->depop_delay = -1;
  1349. codec->fixup_id = HDA_FIXUP_ID_NOT_SET;
  1350. #ifdef CONFIG_PM
  1351. spin_lock_init(&codec->power_lock);
  1352. INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
  1353. /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
  1354. * the caller has to power down appropriatley after initialization
  1355. * phase.
  1356. */
  1357. hda_keep_power_on(codec);
  1358. #endif
  1359. snd_hda_sysfs_init(codec);
  1360. if (codec->bus->modelname) {
  1361. codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
  1362. if (!codec->modelname) {
  1363. err = -ENODEV;
  1364. goto error;
  1365. }
  1366. }
  1367. list_add_tail(&codec->list, &bus->codec_list);
  1368. bus->num_codecs++;
  1369. bus->caddr_tbl[codec_addr] = codec;
  1370. codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
  1371. AC_PAR_VENDOR_ID);
  1372. if (codec->vendor_id == -1)
  1373. /* read again, hopefully the access method was corrected
  1374. * in the last read...
  1375. */
  1376. codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
  1377. AC_PAR_VENDOR_ID);
  1378. codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
  1379. AC_PAR_SUBSYSTEM_ID);
  1380. codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
  1381. AC_PAR_REV_ID);
  1382. setup_fg_nodes(codec);
  1383. if (!codec->afg && !codec->mfg) {
  1384. dev_err(bus->card->dev, "no AFG or MFG node found\n");
  1385. err = -ENODEV;
  1386. goto error;
  1387. }
  1388. fg = codec->afg ? codec->afg : codec->mfg;
  1389. err = read_widget_caps(codec, fg);
  1390. if (err < 0) {
  1391. dev_err(bus->card->dev, "cannot malloc\n");
  1392. goto error;
  1393. }
  1394. err = read_pin_defaults(codec);
  1395. if (err < 0)
  1396. goto error;
  1397. if (!codec->subsystem_id) {
  1398. codec->subsystem_id =
  1399. snd_hda_codec_read(codec, fg, 0,
  1400. AC_VERB_GET_SUBSYSTEM_ID, 0);
  1401. }
  1402. #ifdef CONFIG_PM
  1403. codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec, fg,
  1404. AC_PWRST_CLKSTOP);
  1405. #endif
  1406. codec->epss = snd_hda_codec_get_supported_ps(codec, fg,
  1407. AC_PWRST_EPSS);
  1408. #ifdef CONFIG_PM
  1409. if (!codec->d3_stop_clk || !codec->epss)
  1410. bus->power_keep_link_on = 1;
  1411. #endif
  1412. /* power-up all before initialization */
  1413. hda_set_power_state(codec, AC_PWRST_D0);
  1414. snd_hda_codec_proc_new(codec);
  1415. snd_hda_create_hwdep(codec);
  1416. sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
  1417. codec->subsystem_id, codec->revision_id);
  1418. snd_component_add(codec->bus->card, component);
  1419. err = snd_device_new(bus->card, SNDRV_DEV_CODEC, codec, &dev_ops);
  1420. if (err < 0)
  1421. goto error;
  1422. if (codecp)
  1423. *codecp = codec;
  1424. return 0;
  1425. error:
  1426. snd_hda_codec_free(codec);
  1427. return err;
  1428. }
  1429. EXPORT_SYMBOL_GPL(snd_hda_codec_new);
  1430. /**
  1431. * snd_hda_codec_update_widgets - Refresh widget caps and pin defaults
  1432. * @codec: the HDA codec
  1433. *
  1434. * Forcibly refresh the all widget caps and the init pin configurations of
  1435. * the given codec.
  1436. */
  1437. int snd_hda_codec_update_widgets(struct hda_codec *codec)
  1438. {
  1439. hda_nid_t fg;
  1440. int err;
  1441. /* Assume the function group node does not change,
  1442. * only the widget nodes may change.
  1443. */
  1444. kfree(codec->wcaps);
  1445. fg = codec->afg ? codec->afg : codec->mfg;
  1446. err = read_widget_caps(codec, fg);
  1447. if (err < 0) {
  1448. codec_err(codec, "cannot malloc\n");
  1449. return err;
  1450. }
  1451. snd_array_free(&codec->init_pins);
  1452. err = read_pin_defaults(codec);
  1453. return err;
  1454. }
  1455. EXPORT_SYMBOL_GPL(snd_hda_codec_update_widgets);
  1456. #if IS_ENABLED(CONFIG_SND_HDA_CODEC_HDMI)
  1457. /* if all audio out widgets are digital, let's assume the codec as a HDMI/DP */
  1458. static bool is_likely_hdmi_codec(struct hda_codec *codec)
  1459. {
  1460. hda_nid_t nid = codec->start_nid;
  1461. int i;
  1462. for (i = 0; i < codec->num_nodes; i++, nid++) {
  1463. unsigned int wcaps = get_wcaps(codec, nid);
  1464. switch (get_wcaps_type(wcaps)) {
  1465. case AC_WID_AUD_IN:
  1466. return false; /* HDMI parser supports only HDMI out */
  1467. case AC_WID_AUD_OUT:
  1468. if (!(wcaps & AC_WCAP_DIGITAL))
  1469. return false;
  1470. break;
  1471. }
  1472. }
  1473. return true;
  1474. }
  1475. #else
  1476. /* no HDMI codec parser support */
  1477. #define is_likely_hdmi_codec(codec) false
  1478. #endif /* CONFIG_SND_HDA_CODEC_HDMI */
  1479. /**
  1480. * snd_hda_codec_configure - (Re-)configure the HD-audio codec
  1481. * @codec: the HDA codec
  1482. *
  1483. * Start parsing of the given codec tree and (re-)initialize the whole
  1484. * patch instance.
  1485. *
  1486. * Returns 0 if successful or a negative error code.
  1487. */
  1488. int snd_hda_codec_configure(struct hda_codec *codec)
  1489. {
  1490. int (*patch)(struct hda_codec *) = NULL;
  1491. int err;
  1492. codec->preset = find_codec_preset(codec);
  1493. if (!codec->vendor_name || !codec->chip_name) {
  1494. err = get_codec_name(codec);
  1495. if (err < 0)
  1496. return err;
  1497. }
  1498. if (!is_generic_config(codec) && codec->preset)
  1499. patch = codec->preset->patch;
  1500. if (!patch) {
  1501. unload_parser(codec); /* to be sure */
  1502. if (is_likely_hdmi_codec(codec)) {
  1503. #if IS_MODULE(CONFIG_SND_HDA_CODEC_HDMI)
  1504. patch = load_parser(codec, snd_hda_parse_hdmi_codec);
  1505. #elif IS_BUILTIN(CONFIG_SND_HDA_CODEC_HDMI)
  1506. patch = snd_hda_parse_hdmi_codec;
  1507. #endif
  1508. }
  1509. if (!patch) {
  1510. #if IS_MODULE(CONFIG_SND_HDA_GENERIC)
  1511. patch = load_parser(codec, snd_hda_parse_generic_codec);
  1512. #elif IS_BUILTIN(CONFIG_SND_HDA_GENERIC)
  1513. patch = snd_hda_parse_generic_codec;
  1514. #endif
  1515. }
  1516. if (!patch) {
  1517. codec_err(codec, "No codec parser is available\n");
  1518. return -ENODEV;
  1519. }
  1520. }
  1521. err = patch(codec);
  1522. if (err < 0) {
  1523. unload_parser(codec);
  1524. return err;
  1525. }
  1526. if (codec->patch_ops.unsol_event) {
  1527. err = init_unsol_queue(codec->bus);
  1528. if (err < 0)
  1529. return err;
  1530. }
  1531. /* audio codec should override the mixer name */
  1532. if (codec->afg || !*codec->bus->card->mixername)
  1533. snprintf(codec->bus->card->mixername,
  1534. sizeof(codec->bus->card->mixername),
  1535. "%s %s", codec->vendor_name, codec->chip_name);
  1536. return 0;
  1537. }
  1538. EXPORT_SYMBOL_GPL(snd_hda_codec_configure);
  1539. /* update the stream-id if changed */
  1540. static void update_pcm_stream_id(struct hda_codec *codec,
  1541. struct hda_cvt_setup *p, hda_nid_t nid,
  1542. u32 stream_tag, int channel_id)
  1543. {
  1544. unsigned int oldval, newval;
  1545. if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
  1546. oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
  1547. newval = (stream_tag << 4) | channel_id;
  1548. if (oldval != newval)
  1549. snd_hda_codec_write(codec, nid, 0,
  1550. AC_VERB_SET_CHANNEL_STREAMID,
  1551. newval);
  1552. p->stream_tag = stream_tag;
  1553. p->channel_id = channel_id;
  1554. }
  1555. }
  1556. /* update the format-id if changed */
  1557. static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
  1558. hda_nid_t nid, int format)
  1559. {
  1560. unsigned int oldval;
  1561. if (p->format_id != format) {
  1562. oldval = snd_hda_codec_read(codec, nid, 0,
  1563. AC_VERB_GET_STREAM_FORMAT, 0);
  1564. if (oldval != format) {
  1565. msleep(1);
  1566. snd_hda_codec_write(codec, nid, 0,
  1567. AC_VERB_SET_STREAM_FORMAT,
  1568. format);
  1569. }
  1570. p->format_id = format;
  1571. }
  1572. }
  1573. /**
  1574. * snd_hda_codec_setup_stream - set up the codec for streaming
  1575. * @codec: the CODEC to set up
  1576. * @nid: the NID to set up
  1577. * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
  1578. * @channel_id: channel id to pass, zero based.
  1579. * @format: stream format.
  1580. */
  1581. void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
  1582. u32 stream_tag,
  1583. int channel_id, int format)
  1584. {
  1585. struct hda_codec *c;
  1586. struct hda_cvt_setup *p;
  1587. int type;
  1588. int i;
  1589. if (!nid)
  1590. return;
  1591. codec_dbg(codec,
  1592. "hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
  1593. nid, stream_tag, channel_id, format);
  1594. p = get_hda_cvt_setup(codec, nid);
  1595. if (!p)
  1596. return;
  1597. if (codec->pcm_format_first)
  1598. update_pcm_format(codec, p, nid, format);
  1599. update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
  1600. if (!codec->pcm_format_first)
  1601. update_pcm_format(codec, p, nid, format);
  1602. p->active = 1;
  1603. p->dirty = 0;
  1604. /* make other inactive cvts with the same stream-tag dirty */
  1605. type = get_wcaps_type(get_wcaps(codec, nid));
  1606. list_for_each_entry(c, &codec->bus->codec_list, list) {
  1607. for (i = 0; i < c->cvt_setups.used; i++) {
  1608. p = snd_array_elem(&c->cvt_setups, i);
  1609. if (!p->active && p->stream_tag == stream_tag &&
  1610. get_wcaps_type(get_wcaps(c, p->nid)) == type)
  1611. p->dirty = 1;
  1612. }
  1613. }
  1614. }
  1615. EXPORT_SYMBOL_GPL(snd_hda_codec_setup_stream);
  1616. static void really_cleanup_stream(struct hda_codec *codec,
  1617. struct hda_cvt_setup *q);
  1618. /**
  1619. * __snd_hda_codec_cleanup_stream - clean up the codec for closing
  1620. * @codec: the CODEC to clean up
  1621. * @nid: the NID to clean up
  1622. * @do_now: really clean up the stream instead of clearing the active flag
  1623. */
  1624. void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
  1625. int do_now)
  1626. {
  1627. struct hda_cvt_setup *p;
  1628. if (!nid)
  1629. return;
  1630. if (codec->no_sticky_stream)
  1631. do_now = 1;
  1632. codec_dbg(codec, "hda_codec_cleanup_stream: NID=0x%x\n", nid);
  1633. p = get_hda_cvt_setup(codec, nid);
  1634. if (p) {
  1635. /* here we just clear the active flag when do_now isn't set;
  1636. * actual clean-ups will be done later in
  1637. * purify_inactive_streams() called from snd_hda_codec_prpapre()
  1638. */
  1639. if (do_now)
  1640. really_cleanup_stream(codec, p);
  1641. else
  1642. p->active = 0;
  1643. }
  1644. }
  1645. EXPORT_SYMBOL_GPL(__snd_hda_codec_cleanup_stream);
  1646. static void really_cleanup_stream(struct hda_codec *codec,
  1647. struct hda_cvt_setup *q)
  1648. {
  1649. hda_nid_t nid = q->nid;
  1650. if (q->stream_tag || q->channel_id)
  1651. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
  1652. if (q->format_id)
  1653. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
  1654. );
  1655. memset(q, 0, sizeof(*q));
  1656. q->nid = nid;
  1657. }
  1658. /* clean up the all conflicting obsolete streams */
  1659. static void purify_inactive_streams(struct hda_codec *codec)
  1660. {
  1661. struct hda_codec *c;
  1662. int i;
  1663. list_for_each_entry(c, &codec->bus->codec_list, list) {
  1664. for (i = 0; i < c->cvt_setups.used; i++) {
  1665. struct hda_cvt_setup *p;
  1666. p = snd_array_elem(&c->cvt_setups, i);
  1667. if (p->dirty)
  1668. really_cleanup_stream(c, p);
  1669. }
  1670. }
  1671. }
  1672. #ifdef CONFIG_PM
  1673. /* clean up all streams; called from suspend */
  1674. static void hda_cleanup_all_streams(struct hda_codec *codec)
  1675. {
  1676. int i;
  1677. for (i = 0; i < codec->cvt_setups.used; i++) {
  1678. struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
  1679. if (p->stream_tag)
  1680. really_cleanup_stream(codec, p);
  1681. }
  1682. }
  1683. #endif
  1684. /*
  1685. * amp access functions
  1686. */
  1687. /* FIXME: more better hash key? */
  1688. #define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
  1689. #define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
  1690. #define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
  1691. #define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
  1692. #define INFO_AMP_CAPS (1<<0)
  1693. #define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
  1694. /* initialize the hash table */
  1695. static void init_hda_cache(struct hda_cache_rec *cache,
  1696. unsigned int record_size)
  1697. {
  1698. memset(cache, 0, sizeof(*cache));
  1699. memset(cache->hash, 0xff, sizeof(cache->hash));
  1700. snd_array_init(&cache->buf, record_size, 64);
  1701. }
  1702. static void free_hda_cache(struct hda_cache_rec *cache)
  1703. {
  1704. snd_array_free(&cache->buf);
  1705. }
  1706. /* query the hash. allocate an entry if not found. */
  1707. static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
  1708. {
  1709. u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
  1710. u16 cur = cache->hash[idx];
  1711. struct hda_cache_head *info;
  1712. while (cur != 0xffff) {
  1713. info = snd_array_elem(&cache->buf, cur);
  1714. if (info->key == key)
  1715. return info;
  1716. cur = info->next;
  1717. }
  1718. return NULL;
  1719. }
  1720. /* query the hash. allocate an entry if not found. */
  1721. static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
  1722. u32 key)
  1723. {
  1724. struct hda_cache_head *info = get_hash(cache, key);
  1725. if (!info) {
  1726. u16 idx, cur;
  1727. /* add a new hash entry */
  1728. info = snd_array_new(&cache->buf);
  1729. if (!info)
  1730. return NULL;
  1731. cur = snd_array_index(&cache->buf, info);
  1732. info->key = key;
  1733. info->val = 0;
  1734. info->dirty = 0;
  1735. idx = key % (u16)ARRAY_SIZE(cache->hash);
  1736. info->next = cache->hash[idx];
  1737. cache->hash[idx] = cur;
  1738. }
  1739. return info;
  1740. }
  1741. /* query and allocate an amp hash entry */
  1742. static inline struct hda_amp_info *
  1743. get_alloc_amp_hash(struct hda_codec *codec, u32 key)
  1744. {
  1745. return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
  1746. }
  1747. /* overwrite the value with the key in the caps hash */
  1748. static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val)
  1749. {
  1750. struct hda_amp_info *info;
  1751. mutex_lock(&codec->hash_mutex);
  1752. info = get_alloc_amp_hash(codec, key);
  1753. if (!info) {
  1754. mutex_unlock(&codec->hash_mutex);
  1755. return -EINVAL;
  1756. }
  1757. info->amp_caps = val;
  1758. info->head.val |= INFO_AMP_CAPS;
  1759. mutex_unlock(&codec->hash_mutex);
  1760. return 0;
  1761. }
  1762. /* query the value from the caps hash; if not found, fetch the current
  1763. * value from the given function and store in the hash
  1764. */
  1765. static unsigned int
  1766. query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key,
  1767. unsigned int (*func)(struct hda_codec *, hda_nid_t, int))
  1768. {
  1769. struct hda_amp_info *info;
  1770. unsigned int val;
  1771. mutex_lock(&codec->hash_mutex);
  1772. info = get_alloc_amp_hash(codec, key);
  1773. if (!info) {
  1774. mutex_unlock(&codec->hash_mutex);
  1775. return 0;
  1776. }
  1777. if (!(info->head.val & INFO_AMP_CAPS)) {
  1778. mutex_unlock(&codec->hash_mutex); /* for reentrance */
  1779. val = func(codec, nid, dir);
  1780. write_caps_hash(codec, key, val);
  1781. } else {
  1782. val = info->amp_caps;
  1783. mutex_unlock(&codec->hash_mutex);
  1784. }
  1785. return val;
  1786. }
  1787. static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid,
  1788. int direction)
  1789. {
  1790. if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
  1791. nid = codec->afg;
  1792. return snd_hda_param_read(codec, nid,
  1793. direction == HDA_OUTPUT ?
  1794. AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
  1795. }
  1796. /**
  1797. * query_amp_caps - query AMP capabilities
  1798. * @codec: the HD-auio codec
  1799. * @nid: the NID to query
  1800. * @direction: either #HDA_INPUT or #HDA_OUTPUT
  1801. *
  1802. * Query AMP capabilities for the given widget and direction.
  1803. * Returns the obtained capability bits.
  1804. *
  1805. * When cap bits have been already read, this doesn't read again but
  1806. * returns the cached value.
  1807. */
  1808. u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
  1809. {
  1810. return query_caps_hash(codec, nid, direction,
  1811. HDA_HASH_KEY(nid, direction, 0),
  1812. read_amp_cap);
  1813. }
  1814. EXPORT_SYMBOL_GPL(query_amp_caps);
  1815. /**
  1816. * snd_hda_check_amp_caps - query AMP capabilities
  1817. * @codec: the HD-audio codec
  1818. * @nid: the NID to query
  1819. * @dir: either #HDA_INPUT or #HDA_OUTPUT
  1820. * @bits: bit mask to check the result
  1821. *
  1822. * Check whether the widget has the given amp capability for the direction.
  1823. */
  1824. bool snd_hda_check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
  1825. int dir, unsigned int bits)
  1826. {
  1827. if (!nid)
  1828. return false;
  1829. if (get_wcaps(codec, nid) & (1 << (dir + 1)))
  1830. if (query_amp_caps(codec, nid, dir) & bits)
  1831. return true;
  1832. return false;
  1833. }
  1834. EXPORT_SYMBOL_GPL(snd_hda_check_amp_caps);
  1835. /**
  1836. * snd_hda_override_amp_caps - Override the AMP capabilities
  1837. * @codec: the CODEC to clean up
  1838. * @nid: the NID to clean up
  1839. * @dir: either #HDA_INPUT or #HDA_OUTPUT
  1840. * @caps: the capability bits to set
  1841. *
  1842. * Override the cached AMP caps bits value by the given one.
  1843. * This function is useful if the driver needs to adjust the AMP ranges,
  1844. * e.g. limit to 0dB, etc.
  1845. *
  1846. * Returns zero if successful or a negative error code.
  1847. */
  1848. int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
  1849. unsigned int caps)
  1850. {
  1851. return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps);
  1852. }
  1853. EXPORT_SYMBOL_GPL(snd_hda_override_amp_caps);
  1854. static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid,
  1855. int dir)
  1856. {
  1857. return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
  1858. }
  1859. /**
  1860. * snd_hda_query_pin_caps - Query PIN capabilities
  1861. * @codec: the HD-auio codec
  1862. * @nid: the NID to query
  1863. *
  1864. * Query PIN capabilities for the given widget.
  1865. * Returns the obtained capability bits.
  1866. *
  1867. * When cap bits have been already read, this doesn't read again but
  1868. * returns the cached value.
  1869. */
  1870. u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
  1871. {
  1872. return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid),
  1873. read_pin_cap);
  1874. }
  1875. EXPORT_SYMBOL_GPL(snd_hda_query_pin_caps);
  1876. /**
  1877. * snd_hda_override_pin_caps - Override the pin capabilities
  1878. * @codec: the CODEC
  1879. * @nid: the NID to override
  1880. * @caps: the capability bits to set
  1881. *
  1882. * Override the cached PIN capabilitiy bits value by the given one.
  1883. *
  1884. * Returns zero if successful or a negative error code.
  1885. */
  1886. int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
  1887. unsigned int caps)
  1888. {
  1889. return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps);
  1890. }
  1891. EXPORT_SYMBOL_GPL(snd_hda_override_pin_caps);
  1892. /* read or sync the hash value with the current value;
  1893. * call within hash_mutex
  1894. */
  1895. static struct hda_amp_info *
  1896. update_amp_hash(struct hda_codec *codec, hda_nid_t nid, int ch,
  1897. int direction, int index, bool init_only)
  1898. {
  1899. struct hda_amp_info *info;
  1900. unsigned int parm, val = 0;
  1901. bool val_read = false;
  1902. retry:
  1903. info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
  1904. if (!info)
  1905. return NULL;
  1906. if (!(info->head.val & INFO_AMP_VOL(ch))) {
  1907. if (!val_read) {
  1908. mutex_unlock(&codec->hash_mutex);
  1909. parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
  1910. parm |= direction == HDA_OUTPUT ?
  1911. AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
  1912. parm |= index;
  1913. val = snd_hda_codec_read(codec, nid, 0,
  1914. AC_VERB_GET_AMP_GAIN_MUTE, parm);
  1915. val &= 0xff;
  1916. val_read = true;
  1917. mutex_lock(&codec->hash_mutex);
  1918. goto retry;
  1919. }
  1920. info->vol[ch] = val;
  1921. info->head.val |= INFO_AMP_VOL(ch);
  1922. } else if (init_only)
  1923. return NULL;
  1924. return info;
  1925. }
  1926. /*
  1927. * write the current volume in info to the h/w
  1928. */
  1929. static void put_vol_mute(struct hda_codec *codec, unsigned int amp_caps,
  1930. hda_nid_t nid, int ch, int direction, int index,
  1931. int val)
  1932. {
  1933. u32 parm;
  1934. parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
  1935. parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
  1936. parm |= index << AC_AMP_SET_INDEX_SHIFT;
  1937. if ((val & HDA_AMP_MUTE) && !(amp_caps & AC_AMPCAP_MUTE) &&
  1938. (amp_caps & AC_AMPCAP_MIN_MUTE))
  1939. ; /* set the zero value as a fake mute */
  1940. else
  1941. parm |= val;
  1942. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
  1943. }
  1944. /**
  1945. * snd_hda_codec_amp_read - Read AMP value
  1946. * @codec: HD-audio codec
  1947. * @nid: NID to read the AMP value
  1948. * @ch: channel (left=0 or right=1)
  1949. * @direction: #HDA_INPUT or #HDA_OUTPUT
  1950. * @index: the index value (only for input direction)
  1951. *
  1952. * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
  1953. */
  1954. int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
  1955. int direction, int index)
  1956. {
  1957. struct hda_amp_info *info;
  1958. unsigned int val = 0;
  1959. mutex_lock(&codec->hash_mutex);
  1960. info = update_amp_hash(codec, nid, ch, direction, index, false);
  1961. if (info)
  1962. val = info->vol[ch];
  1963. mutex_unlock(&codec->hash_mutex);
  1964. return val;
  1965. }
  1966. EXPORT_SYMBOL_GPL(snd_hda_codec_amp_read);
  1967. static int codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
  1968. int direction, int idx, int mask, int val,
  1969. bool init_only)
  1970. {
  1971. struct hda_amp_info *info;
  1972. unsigned int caps;
  1973. unsigned int cache_only;
  1974. if (snd_BUG_ON(mask & ~0xff))
  1975. mask &= 0xff;
  1976. val &= mask;
  1977. mutex_lock(&codec->hash_mutex);
  1978. info = update_amp_hash(codec, nid, ch, direction, idx, init_only);
  1979. if (!info) {
  1980. mutex_unlock(&codec->hash_mutex);
  1981. return 0;
  1982. }
  1983. val |= info->vol[ch] & ~mask;
  1984. if (info->vol[ch] == val) {
  1985. mutex_unlock(&codec->hash_mutex);
  1986. return 0;
  1987. }
  1988. info->vol[ch] = val;
  1989. cache_only = info->head.dirty = codec->cached_write;
  1990. caps = info->amp_caps;
  1991. mutex_unlock(&codec->hash_mutex);
  1992. if (!cache_only)
  1993. put_vol_mute(codec, caps, nid, ch, direction, idx, val);
  1994. return 1;
  1995. }
  1996. /**
  1997. * snd_hda_codec_amp_update - update the AMP value
  1998. * @codec: HD-audio codec
  1999. * @nid: NID to read the AMP value
  2000. * @ch: channel (left=0 or right=1)
  2001. * @direction: #HDA_INPUT or #HDA_OUTPUT
  2002. * @idx: the index value (only for input direction)
  2003. * @mask: bit mask to set
  2004. * @val: the bits value to set
  2005. *
  2006. * Update the AMP value with a bit mask.
  2007. * Returns 0 if the value is unchanged, 1 if changed.
  2008. */
  2009. int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
  2010. int direction, int idx, int mask, int val)
  2011. {
  2012. return codec_amp_update(codec, nid, ch, direction, idx, mask, val, false);
  2013. }
  2014. EXPORT_SYMBOL_GPL(snd_hda_codec_amp_update);
  2015. /**
  2016. * snd_hda_codec_amp_stereo - update the AMP stereo values
  2017. * @codec: HD-audio codec
  2018. * @nid: NID to read the AMP value
  2019. * @direction: #HDA_INPUT or #HDA_OUTPUT
  2020. * @idx: the index value (only for input direction)
  2021. * @mask: bit mask to set
  2022. * @val: the bits value to set
  2023. *
  2024. * Update the AMP values like snd_hda_codec_amp_update(), but for a
  2025. * stereo widget with the same mask and value.
  2026. */
  2027. int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
  2028. int direction, int idx, int mask, int val)
  2029. {
  2030. int ch, ret = 0;
  2031. if (snd_BUG_ON(mask & ~0xff))
  2032. mask &= 0xff;
  2033. for (ch = 0; ch < 2; ch++)
  2034. ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
  2035. idx, mask, val);
  2036. return ret;
  2037. }
  2038. EXPORT_SYMBOL_GPL(snd_hda_codec_amp_stereo);
  2039. /**
  2040. * snd_hda_codec_amp_init - initialize the AMP value
  2041. * @codec: the HDA codec
  2042. * @nid: NID to read the AMP value
  2043. * @ch: channel (left=0 or right=1)
  2044. * @dir: #HDA_INPUT or #HDA_OUTPUT
  2045. * @idx: the index value (only for input direction)
  2046. * @mask: bit mask to set
  2047. * @val: the bits value to set
  2048. *
  2049. * Works like snd_hda_codec_amp_update() but it writes the value only at
  2050. * the first access. If the amp was already initialized / updated beforehand,
  2051. * this does nothing.
  2052. */
  2053. int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
  2054. int dir, int idx, int mask, int val)
  2055. {
  2056. return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true);
  2057. }
  2058. EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init);
  2059. /**
  2060. * snd_hda_codec_amp_init_stereo - initialize the stereo AMP value
  2061. * @codec: the HDA codec
  2062. * @nid: NID to read the AMP value
  2063. * @dir: #HDA_INPUT or #HDA_OUTPUT
  2064. * @idx: the index value (only for input direction)
  2065. * @mask: bit mask to set
  2066. * @val: the bits value to set
  2067. *
  2068. * Call snd_hda_codec_amp_init() for both stereo channels.
  2069. */
  2070. int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
  2071. int dir, int idx, int mask, int val)
  2072. {
  2073. int ch, ret = 0;
  2074. if (snd_BUG_ON(mask & ~0xff))
  2075. mask &= 0xff;
  2076. for (ch = 0; ch < 2; ch++)
  2077. ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
  2078. idx, mask, val);
  2079. return ret;
  2080. }
  2081. EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init_stereo);
  2082. /**
  2083. * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
  2084. * @codec: HD-audio codec
  2085. *
  2086. * Resume the all amp commands from the cache.
  2087. */
  2088. void snd_hda_codec_resume_amp(struct hda_codec *codec)
  2089. {
  2090. int i;
  2091. mutex_lock(&codec->hash_mutex);
  2092. codec->cached_write = 0;
  2093. for (i = 0; i < codec->amp_cache.buf.used; i++) {
  2094. struct hda_amp_info *buffer;
  2095. u32 key;
  2096. hda_nid_t nid;
  2097. unsigned int idx, dir, ch;
  2098. struct hda_amp_info info;
  2099. buffer = snd_array_elem(&codec->amp_cache.buf, i);
  2100. if (!buffer->head.dirty)
  2101. continue;
  2102. buffer->head.dirty = 0;
  2103. info = *buffer;
  2104. key = info.head.key;
  2105. if (!key)
  2106. continue;
  2107. nid = key & 0xff;
  2108. idx = (key >> 16) & 0xff;
  2109. dir = (key >> 24) & 0xff;
  2110. for (ch = 0; ch < 2; ch++) {
  2111. if (!(info.head.val & INFO_AMP_VOL(ch)))
  2112. continue;
  2113. mutex_unlock(&codec->hash_mutex);
  2114. put_vol_mute(codec, info.amp_caps, nid, ch, dir, idx,
  2115. info.vol[ch]);
  2116. mutex_lock(&codec->hash_mutex);
  2117. }
  2118. }
  2119. mutex_unlock(&codec->hash_mutex);
  2120. }
  2121. EXPORT_SYMBOL_GPL(snd_hda_codec_resume_amp);
  2122. static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
  2123. unsigned int ofs)
  2124. {
  2125. u32 caps = query_amp_caps(codec, nid, dir);
  2126. /* get num steps */
  2127. caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
  2128. if (ofs < caps)
  2129. caps -= ofs;
  2130. return caps;
  2131. }
  2132. /**
  2133. * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
  2134. * @kcontrol: referred ctl element
  2135. * @uinfo: pointer to get/store the data
  2136. *
  2137. * The control element is supposed to have the private_value field
  2138. * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
  2139. */
  2140. int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
  2141. struct snd_ctl_elem_info *uinfo)
  2142. {
  2143. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2144. u16 nid = get_amp_nid(kcontrol);
  2145. u8 chs = get_amp_channels(kcontrol);
  2146. int dir = get_amp_direction(kcontrol);
  2147. unsigned int ofs = get_amp_offset(kcontrol);
  2148. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2149. uinfo->count = chs == 3 ? 2 : 1;
  2150. uinfo->value.integer.min = 0;
  2151. uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
  2152. if (!uinfo->value.integer.max) {
  2153. codec_warn(codec,
  2154. "num_steps = 0 for NID=0x%x (ctl = %s)\n",
  2155. nid, kcontrol->id.name);
  2156. return -EINVAL;
  2157. }
  2158. return 0;
  2159. }
  2160. EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_info);
  2161. static inline unsigned int
  2162. read_amp_value(struct hda_codec *codec, hda_nid_t nid,
  2163. int ch, int dir, int idx, unsigned int ofs)
  2164. {
  2165. unsigned int val;
  2166. val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
  2167. val &= HDA_AMP_VOLMASK;
  2168. if (val >= ofs)
  2169. val -= ofs;
  2170. else
  2171. val = 0;
  2172. return val;
  2173. }
  2174. static inline int
  2175. update_amp_value(struct hda_codec *codec, hda_nid_t nid,
  2176. int ch, int dir, int idx, unsigned int ofs,
  2177. unsigned int val)
  2178. {
  2179. unsigned int maxval;
  2180. if (val > 0)
  2181. val += ofs;
  2182. /* ofs = 0: raw max value */
  2183. maxval = get_amp_max_value(codec, nid, dir, 0);
  2184. if (val > maxval)
  2185. val = maxval;
  2186. return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
  2187. HDA_AMP_VOLMASK, val);
  2188. }
  2189. /**
  2190. * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
  2191. * @kcontrol: ctl element
  2192. * @ucontrol: pointer to get/store the data
  2193. *
  2194. * The control element is supposed to have the private_value field
  2195. * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
  2196. */
  2197. int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
  2198. struct snd_ctl_elem_value *ucontrol)
  2199. {
  2200. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2201. hda_nid_t nid = get_amp_nid(kcontrol);
  2202. int chs = get_amp_channels(kcontrol);
  2203. int dir = get_amp_direction(kcontrol);
  2204. int idx = get_amp_index(kcontrol);
  2205. unsigned int ofs = get_amp_offset(kcontrol);
  2206. long *valp = ucontrol->value.integer.value;
  2207. if (chs & 1)
  2208. *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
  2209. if (chs & 2)
  2210. *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
  2211. return 0;
  2212. }
  2213. EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_get);
  2214. /**
  2215. * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
  2216. * @kcontrol: ctl element
  2217. * @ucontrol: pointer to get/store the data
  2218. *
  2219. * The control element is supposed to have the private_value field
  2220. * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
  2221. */
  2222. int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
  2223. struct snd_ctl_elem_value *ucontrol)
  2224. {
  2225. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2226. hda_nid_t nid = get_amp_nid(kcontrol);
  2227. int chs = get_amp_channels(kcontrol);
  2228. int dir = get_amp_direction(kcontrol);
  2229. int idx = get_amp_index(kcontrol);
  2230. unsigned int ofs = get_amp_offset(kcontrol);
  2231. long *valp = ucontrol->value.integer.value;
  2232. int change = 0;
  2233. snd_hda_power_up(codec);
  2234. if (chs & 1) {
  2235. change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
  2236. valp++;
  2237. }
  2238. if (chs & 2)
  2239. change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
  2240. snd_hda_power_down(codec);
  2241. return change;
  2242. }
  2243. EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_put);
  2244. /**
  2245. * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
  2246. * @kcontrol: ctl element
  2247. * @op_flag: operation flag
  2248. * @size: byte size of input TLV
  2249. * @_tlv: TLV data
  2250. *
  2251. * The control element is supposed to have the private_value field
  2252. * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
  2253. */
  2254. int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
  2255. unsigned int size, unsigned int __user *_tlv)
  2256. {
  2257. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2258. hda_nid_t nid = get_amp_nid(kcontrol);
  2259. int dir = get_amp_direction(kcontrol);
  2260. unsigned int ofs = get_amp_offset(kcontrol);
  2261. bool min_mute = get_amp_min_mute(kcontrol);
  2262. u32 caps, val1, val2;
  2263. if (size < 4 * sizeof(unsigned int))
  2264. return -ENOMEM;
  2265. caps = query_amp_caps(codec, nid, dir);
  2266. val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
  2267. val2 = (val2 + 1) * 25;
  2268. val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
  2269. val1 += ofs;
  2270. val1 = ((int)val1) * ((int)val2);
  2271. if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
  2272. val2 |= TLV_DB_SCALE_MUTE;
  2273. if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
  2274. return -EFAULT;
  2275. if (put_user(2 * sizeof(unsigned int), _tlv + 1))
  2276. return -EFAULT;
  2277. if (put_user(val1, _tlv + 2))
  2278. return -EFAULT;
  2279. if (put_user(val2, _tlv + 3))
  2280. return -EFAULT;
  2281. return 0;
  2282. }
  2283. EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_tlv);
  2284. /**
  2285. * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
  2286. * @codec: HD-audio codec
  2287. * @nid: NID of a reference widget
  2288. * @dir: #HDA_INPUT or #HDA_OUTPUT
  2289. * @tlv: TLV data to be stored, at least 4 elements
  2290. *
  2291. * Set (static) TLV data for a virtual master volume using the AMP caps
  2292. * obtained from the reference NID.
  2293. * The volume range is recalculated as if the max volume is 0dB.
  2294. */
  2295. void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
  2296. unsigned int *tlv)
  2297. {
  2298. u32 caps;
  2299. int nums, step;
  2300. caps = query_amp_caps(codec, nid, dir);
  2301. nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
  2302. step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
  2303. step = (step + 1) * 25;
  2304. tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
  2305. tlv[1] = 2 * sizeof(unsigned int);
  2306. tlv[2] = -nums * step;
  2307. tlv[3] = step;
  2308. }
  2309. EXPORT_SYMBOL_GPL(snd_hda_set_vmaster_tlv);
  2310. /* find a mixer control element with the given name */
  2311. static struct snd_kcontrol *
  2312. find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
  2313. {
  2314. struct snd_ctl_elem_id id;
  2315. memset(&id, 0, sizeof(id));
  2316. id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  2317. id.device = dev;
  2318. id.index = idx;
  2319. if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
  2320. return NULL;
  2321. strcpy(id.name, name);
  2322. return snd_ctl_find_id(codec->bus->card, &id);
  2323. }
  2324. /**
  2325. * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
  2326. * @codec: HD-audio codec
  2327. * @name: ctl id name string
  2328. *
  2329. * Get the control element with the given id string and IFACE_MIXER.
  2330. */
  2331. struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
  2332. const char *name)
  2333. {
  2334. return find_mixer_ctl(codec, name, 0, 0);
  2335. }
  2336. EXPORT_SYMBOL_GPL(snd_hda_find_mixer_ctl);
  2337. static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
  2338. int start_idx)
  2339. {
  2340. int i, idx;
  2341. /* 16 ctlrs should be large enough */
  2342. for (i = 0, idx = start_idx; i < 16; i++, idx++) {
  2343. if (!find_mixer_ctl(codec, name, 0, idx))
  2344. return idx;
  2345. }
  2346. return -EBUSY;
  2347. }
  2348. /**
  2349. * snd_hda_ctl_add - Add a control element and assign to the codec
  2350. * @codec: HD-audio codec
  2351. * @nid: corresponding NID (optional)
  2352. * @kctl: the control element to assign
  2353. *
  2354. * Add the given control element to an array inside the codec instance.
  2355. * All control elements belonging to a codec are supposed to be added
  2356. * by this function so that a proper clean-up works at the free or
  2357. * reconfiguration time.
  2358. *
  2359. * If non-zero @nid is passed, the NID is assigned to the control element.
  2360. * The assignment is shown in the codec proc file.
  2361. *
  2362. * snd_hda_ctl_add() checks the control subdev id field whether
  2363. * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
  2364. * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
  2365. * specifies if kctl->private_value is a HDA amplifier value.
  2366. */
  2367. int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
  2368. struct snd_kcontrol *kctl)
  2369. {
  2370. int err;
  2371. unsigned short flags = 0;
  2372. struct hda_nid_item *item;
  2373. if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
  2374. flags |= HDA_NID_ITEM_AMP;
  2375. if (nid == 0)
  2376. nid = get_amp_nid_(kctl->private_value);
  2377. }
  2378. if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
  2379. nid = kctl->id.subdevice & 0xffff;
  2380. if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
  2381. kctl->id.subdevice = 0;
  2382. err = snd_ctl_add(codec->bus->card, kctl);
  2383. if (err < 0)
  2384. return err;
  2385. item = snd_array_new(&codec->mixers);
  2386. if (!item)
  2387. return -ENOMEM;
  2388. item->kctl = kctl;
  2389. item->nid = nid;
  2390. item->flags = flags;
  2391. return 0;
  2392. }
  2393. EXPORT_SYMBOL_GPL(snd_hda_ctl_add);
  2394. /**
  2395. * snd_hda_add_nid - Assign a NID to a control element
  2396. * @codec: HD-audio codec
  2397. * @nid: corresponding NID (optional)
  2398. * @kctl: the control element to assign
  2399. * @index: index to kctl
  2400. *
  2401. * Add the given control element to an array inside the codec instance.
  2402. * This function is used when #snd_hda_ctl_add cannot be used for 1:1
  2403. * NID:KCTL mapping - for example "Capture Source" selector.
  2404. */
  2405. int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
  2406. unsigned int index, hda_nid_t nid)
  2407. {
  2408. struct hda_nid_item *item;
  2409. if (nid > 0) {
  2410. item = snd_array_new(&codec->nids);
  2411. if (!item)
  2412. return -ENOMEM;
  2413. item->kctl = kctl;
  2414. item->index = index;
  2415. item->nid = nid;
  2416. return 0;
  2417. }
  2418. codec_err(codec, "no NID for mapping control %s:%d:%d\n",
  2419. kctl->id.name, kctl->id.index, index);
  2420. return -EINVAL;
  2421. }
  2422. EXPORT_SYMBOL_GPL(snd_hda_add_nid);
  2423. /**
  2424. * snd_hda_ctls_clear - Clear all controls assigned to the given codec
  2425. * @codec: HD-audio codec
  2426. */
  2427. void snd_hda_ctls_clear(struct hda_codec *codec)
  2428. {
  2429. int i;
  2430. struct hda_nid_item *items = codec->mixers.list;
  2431. for (i = 0; i < codec->mixers.used; i++)
  2432. snd_ctl_remove(codec->bus->card, items[i].kctl);
  2433. snd_array_free(&codec->mixers);
  2434. snd_array_free(&codec->nids);
  2435. }
  2436. /**
  2437. * snd_hda_lock_devices - pseudo device locking
  2438. * @bus: the BUS
  2439. *
  2440. * toggle card->shutdown to allow/disallow the device access (as a hack)
  2441. */
  2442. int snd_hda_lock_devices(struct hda_bus *bus)
  2443. {
  2444. struct snd_card *card = bus->card;
  2445. struct hda_codec *codec;
  2446. spin_lock(&card->files_lock);
  2447. if (card->shutdown)
  2448. goto err_unlock;
  2449. card->shutdown = 1;
  2450. if (!list_empty(&card->ctl_files))
  2451. goto err_clear;
  2452. list_for_each_entry(codec, &bus->codec_list, list) {
  2453. int pcm;
  2454. for (pcm = 0; pcm < codec->num_pcms; pcm++) {
  2455. struct hda_pcm *cpcm = &codec->pcm_info[pcm];
  2456. if (!cpcm->pcm)
  2457. continue;
  2458. if (cpcm->pcm->streams[0].substream_opened ||
  2459. cpcm->pcm->streams[1].substream_opened)
  2460. goto err_clear;
  2461. }
  2462. }
  2463. spin_unlock(&card->files_lock);
  2464. return 0;
  2465. err_clear:
  2466. card->shutdown = 0;
  2467. err_unlock:
  2468. spin_unlock(&card->files_lock);
  2469. return -EINVAL;
  2470. }
  2471. EXPORT_SYMBOL_GPL(snd_hda_lock_devices);
  2472. /**
  2473. * snd_hda_unlock_devices - pseudo device unlocking
  2474. * @bus: the BUS
  2475. */
  2476. void snd_hda_unlock_devices(struct hda_bus *bus)
  2477. {
  2478. struct snd_card *card = bus->card;
  2479. card = bus->card;
  2480. spin_lock(&card->files_lock);
  2481. card->shutdown = 0;
  2482. spin_unlock(&card->files_lock);
  2483. }
  2484. EXPORT_SYMBOL_GPL(snd_hda_unlock_devices);
  2485. /**
  2486. * snd_hda_codec_reset - Clear all objects assigned to the codec
  2487. * @codec: HD-audio codec
  2488. *
  2489. * This frees the all PCM and control elements assigned to the codec, and
  2490. * clears the caches and restores the pin default configurations.
  2491. *
  2492. * When a device is being used, it returns -EBSY. If successfully freed,
  2493. * returns zero.
  2494. */
  2495. int snd_hda_codec_reset(struct hda_codec *codec)
  2496. {
  2497. struct hda_bus *bus = codec->bus;
  2498. struct snd_card *card = bus->card;
  2499. int i;
  2500. if (snd_hda_lock_devices(bus) < 0)
  2501. return -EBUSY;
  2502. /* OK, let it free */
  2503. cancel_delayed_work_sync(&codec->jackpoll_work);
  2504. #ifdef CONFIG_PM
  2505. cancel_delayed_work_sync(&codec->power_work);
  2506. flush_workqueue(bus->workq);
  2507. #endif
  2508. snd_hda_ctls_clear(codec);
  2509. /* release PCMs */
  2510. for (i = 0; i < codec->num_pcms; i++) {
  2511. if (codec->pcm_info[i].pcm) {
  2512. snd_device_free(card, codec->pcm_info[i].pcm);
  2513. clear_bit(codec->pcm_info[i].device,
  2514. bus->pcm_dev_bits);
  2515. }
  2516. }
  2517. snd_hda_detach_beep_device(codec);
  2518. if (codec->patch_ops.free)
  2519. codec->patch_ops.free(codec);
  2520. memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
  2521. snd_hda_jack_tbl_clear(codec);
  2522. codec->proc_widget_hook = NULL;
  2523. codec->spec = NULL;
  2524. free_hda_cache(&codec->amp_cache);
  2525. free_hda_cache(&codec->cmd_cache);
  2526. init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
  2527. init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
  2528. /* free only driver_pins so that init_pins + user_pins are restored */
  2529. snd_array_free(&codec->driver_pins);
  2530. snd_array_free(&codec->cvt_setups);
  2531. snd_array_free(&codec->spdif_out);
  2532. snd_array_free(&codec->verbs);
  2533. codec->num_pcms = 0;
  2534. codec->pcm_info = NULL;
  2535. codec->preset = NULL;
  2536. codec->slave_dig_outs = NULL;
  2537. codec->spdif_status_reset = 0;
  2538. unload_parser(codec);
  2539. module_put(codec->owner);
  2540. codec->owner = NULL;
  2541. /* allow device access again */
  2542. snd_hda_unlock_devices(bus);
  2543. return 0;
  2544. }
  2545. typedef int (*map_slave_func_t)(struct hda_codec *, void *, struct snd_kcontrol *);
  2546. /* apply the function to all matching slave ctls in the mixer list */
  2547. static int map_slaves(struct hda_codec *codec, const char * const *slaves,
  2548. const char *suffix, map_slave_func_t func, void *data)
  2549. {
  2550. struct hda_nid_item *items;
  2551. const char * const *s;
  2552. int i, err;
  2553. items = codec->mixers.list;
  2554. for (i = 0; i < codec->mixers.used; i++) {
  2555. struct snd_kcontrol *sctl = items[i].kctl;
  2556. if (!sctl || sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
  2557. continue;
  2558. for (s = slaves; *s; s++) {
  2559. char tmpname[sizeof(sctl->id.name)];
  2560. const char *name = *s;
  2561. if (suffix) {
  2562. snprintf(tmpname, sizeof(tmpname), "%s %s",
  2563. name, suffix);
  2564. name = tmpname;
  2565. }
  2566. if (!strcmp(sctl->id.name, name)) {
  2567. err = func(codec, data, sctl);
  2568. if (err)
  2569. return err;
  2570. break;
  2571. }
  2572. }
  2573. }
  2574. return 0;
  2575. }
  2576. static int check_slave_present(struct hda_codec *codec,
  2577. void *data, struct snd_kcontrol *sctl)
  2578. {
  2579. return 1;
  2580. }
  2581. /* guess the value corresponding to 0dB */
  2582. static int get_kctl_0dB_offset(struct hda_codec *codec,
  2583. struct snd_kcontrol *kctl, int *step_to_check)
  2584. {
  2585. int _tlv[4];
  2586. const int *tlv = NULL;
  2587. int val = -1;
  2588. if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
  2589. /* FIXME: set_fs() hack for obtaining user-space TLV data */
  2590. mm_segment_t fs = get_fs();
  2591. set_fs(get_ds());
  2592. if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
  2593. tlv = _tlv;
  2594. set_fs(fs);
  2595. } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
  2596. tlv = kctl->tlv.p;
  2597. if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE) {
  2598. int step = tlv[3];
  2599. step &= ~TLV_DB_SCALE_MUTE;
  2600. if (!step)
  2601. return -1;
  2602. if (*step_to_check && *step_to_check != step) {
  2603. codec_err(codec, "Mismatching dB step for vmaster slave (%d!=%d)\n",
  2604. - *step_to_check, step);
  2605. return -1;
  2606. }
  2607. *step_to_check = step;
  2608. val = -tlv[2] / step;
  2609. }
  2610. return val;
  2611. }
  2612. /* call kctl->put with the given value(s) */
  2613. static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
  2614. {
  2615. struct snd_ctl_elem_value *ucontrol;
  2616. ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
  2617. if (!ucontrol)
  2618. return -ENOMEM;
  2619. ucontrol->value.integer.value[0] = val;
  2620. ucontrol->value.integer.value[1] = val;
  2621. kctl->put(kctl, ucontrol);
  2622. kfree(ucontrol);
  2623. return 0;
  2624. }
  2625. /* initialize the slave volume with 0dB */
  2626. static int init_slave_0dB(struct hda_codec *codec,
  2627. void *data, struct snd_kcontrol *slave)
  2628. {
  2629. int offset = get_kctl_0dB_offset(codec, slave, data);
  2630. if (offset > 0)
  2631. put_kctl_with_value(slave, offset);
  2632. return 0;
  2633. }
  2634. /* unmute the slave */
  2635. static int init_slave_unmute(struct hda_codec *codec,
  2636. void *data, struct snd_kcontrol *slave)
  2637. {
  2638. return put_kctl_with_value(slave, 1);
  2639. }
  2640. static int add_slave(struct hda_codec *codec,
  2641. void *data, struct snd_kcontrol *slave)
  2642. {
  2643. return snd_ctl_add_slave(data, slave);
  2644. }
  2645. /**
  2646. * __snd_hda_add_vmaster - create a virtual master control and add slaves
  2647. * @codec: HD-audio codec
  2648. * @name: vmaster control name
  2649. * @tlv: TLV data (optional)
  2650. * @slaves: slave control names (optional)
  2651. * @suffix: suffix string to each slave name (optional)
  2652. * @init_slave_vol: initialize slaves to unmute/0dB
  2653. * @ctl_ret: store the vmaster kcontrol in return
  2654. *
  2655. * Create a virtual master control with the given name. The TLV data
  2656. * must be either NULL or a valid data.
  2657. *
  2658. * @slaves is a NULL-terminated array of strings, each of which is a
  2659. * slave control name. All controls with these names are assigned to
  2660. * the new virtual master control.
  2661. *
  2662. * This function returns zero if successful or a negative error code.
  2663. */
  2664. int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
  2665. unsigned int *tlv, const char * const *slaves,
  2666. const char *suffix, bool init_slave_vol,
  2667. struct snd_kcontrol **ctl_ret)
  2668. {
  2669. struct snd_kcontrol *kctl;
  2670. int err;
  2671. if (ctl_ret)
  2672. *ctl_ret = NULL;
  2673. err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
  2674. if (err != 1) {
  2675. codec_dbg(codec, "No slave found for %s\n", name);
  2676. return 0;
  2677. }
  2678. kctl = snd_ctl_make_virtual_master(name, tlv);
  2679. if (!kctl)
  2680. return -ENOMEM;
  2681. err = snd_hda_ctl_add(codec, 0, kctl);
  2682. if (err < 0)
  2683. return err;
  2684. err = map_slaves(codec, slaves, suffix, add_slave, kctl);
  2685. if (err < 0)
  2686. return err;
  2687. /* init with master mute & zero volume */
  2688. put_kctl_with_value(kctl, 0);
  2689. if (init_slave_vol) {
  2690. int step = 0;
  2691. map_slaves(codec, slaves, suffix,
  2692. tlv ? init_slave_0dB : init_slave_unmute, &step);
  2693. }
  2694. if (ctl_ret)
  2695. *ctl_ret = kctl;
  2696. return 0;
  2697. }
  2698. EXPORT_SYMBOL_GPL(__snd_hda_add_vmaster);
  2699. /*
  2700. * mute-LED control using vmaster
  2701. */
  2702. static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
  2703. struct snd_ctl_elem_info *uinfo)
  2704. {
  2705. static const char * const texts[] = {
  2706. "On", "Off", "Follow Master"
  2707. };
  2708. return snd_ctl_enum_info(uinfo, 1, 3, texts);
  2709. }
  2710. static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
  2711. struct snd_ctl_elem_value *ucontrol)
  2712. {
  2713. struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
  2714. ucontrol->value.enumerated.item[0] = hook->mute_mode;
  2715. return 0;
  2716. }
  2717. static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
  2718. struct snd_ctl_elem_value *ucontrol)
  2719. {
  2720. struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
  2721. unsigned int old_mode = hook->mute_mode;
  2722. hook->mute_mode = ucontrol->value.enumerated.item[0];
  2723. if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
  2724. hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
  2725. if (old_mode == hook->mute_mode)
  2726. return 0;
  2727. snd_hda_sync_vmaster_hook(hook);
  2728. return 1;
  2729. }
  2730. static struct snd_kcontrol_new vmaster_mute_mode = {
  2731. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2732. .name = "Mute-LED Mode",
  2733. .info = vmaster_mute_mode_info,
  2734. .get = vmaster_mute_mode_get,
  2735. .put = vmaster_mute_mode_put,
  2736. };
  2737. /**
  2738. * snd_hda_add_vmaster_hook - Add a vmaster hook for mute-LED
  2739. * @codec: the HDA codec
  2740. * @hook: the vmaster hook object
  2741. * @expose_enum_ctl: flag to create an enum ctl
  2742. *
  2743. * Add a mute-LED hook with the given vmaster switch kctl.
  2744. * When @expose_enum_ctl is set, "Mute-LED Mode" control is automatically
  2745. * created and associated with the given hook.
  2746. */
  2747. int snd_hda_add_vmaster_hook(struct hda_codec *codec,
  2748. struct hda_vmaster_mute_hook *hook,
  2749. bool expose_enum_ctl)
  2750. {
  2751. struct snd_kcontrol *kctl;
  2752. if (!hook->hook || !hook->sw_kctl)
  2753. return 0;
  2754. snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
  2755. hook->codec = codec;
  2756. hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
  2757. if (!expose_enum_ctl)
  2758. return 0;
  2759. kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
  2760. if (!kctl)
  2761. return -ENOMEM;
  2762. return snd_hda_ctl_add(codec, 0, kctl);
  2763. }
  2764. EXPORT_SYMBOL_GPL(snd_hda_add_vmaster_hook);
  2765. /**
  2766. * snd_hda_sync_vmaster_hook - Sync vmaster hook
  2767. * @hook: the vmaster hook
  2768. *
  2769. * Call the hook with the current value for synchronization.
  2770. * Should be called in init callback.
  2771. */
  2772. void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
  2773. {
  2774. if (!hook->hook || !hook->codec)
  2775. return;
  2776. /* don't call vmaster hook in the destructor since it might have
  2777. * been already destroyed
  2778. */
  2779. if (hook->codec->bus->shutdown)
  2780. return;
  2781. switch (hook->mute_mode) {
  2782. case HDA_VMUTE_FOLLOW_MASTER:
  2783. snd_ctl_sync_vmaster_hook(hook->sw_kctl);
  2784. break;
  2785. default:
  2786. hook->hook(hook->codec, hook->mute_mode);
  2787. break;
  2788. }
  2789. }
  2790. EXPORT_SYMBOL_GPL(snd_hda_sync_vmaster_hook);
  2791. /**
  2792. * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
  2793. * @kcontrol: referred ctl element
  2794. * @uinfo: pointer to get/store the data
  2795. *
  2796. * The control element is supposed to have the private_value field
  2797. * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
  2798. */
  2799. int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
  2800. struct snd_ctl_elem_info *uinfo)
  2801. {
  2802. int chs = get_amp_channels(kcontrol);
  2803. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  2804. uinfo->count = chs == 3 ? 2 : 1;
  2805. uinfo->value.integer.min = 0;
  2806. uinfo->value.integer.max = 1;
  2807. return 0;
  2808. }
  2809. EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_info);
  2810. /**
  2811. * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
  2812. * @kcontrol: ctl element
  2813. * @ucontrol: pointer to get/store the data
  2814. *
  2815. * The control element is supposed to have the private_value field
  2816. * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
  2817. */
  2818. int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
  2819. struct snd_ctl_elem_value *ucontrol)
  2820. {
  2821. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2822. hda_nid_t nid = get_amp_nid(kcontrol);
  2823. int chs = get_amp_channels(kcontrol);
  2824. int dir = get_amp_direction(kcontrol);
  2825. int idx = get_amp_index(kcontrol);
  2826. long *valp = ucontrol->value.integer.value;
  2827. if (chs & 1)
  2828. *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
  2829. HDA_AMP_MUTE) ? 0 : 1;
  2830. if (chs & 2)
  2831. *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
  2832. HDA_AMP_MUTE) ? 0 : 1;
  2833. return 0;
  2834. }
  2835. EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_get);
  2836. /**
  2837. * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
  2838. * @kcontrol: ctl element
  2839. * @ucontrol: pointer to get/store the data
  2840. *
  2841. * The control element is supposed to have the private_value field
  2842. * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
  2843. */
  2844. int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
  2845. struct snd_ctl_elem_value *ucontrol)
  2846. {
  2847. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2848. hda_nid_t nid = get_amp_nid(kcontrol);
  2849. int chs = get_amp_channels(kcontrol);
  2850. int dir = get_amp_direction(kcontrol);
  2851. int idx = get_amp_index(kcontrol);
  2852. long *valp = ucontrol->value.integer.value;
  2853. int change = 0;
  2854. snd_hda_power_up(codec);
  2855. if (chs & 1) {
  2856. change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
  2857. HDA_AMP_MUTE,
  2858. *valp ? 0 : HDA_AMP_MUTE);
  2859. valp++;
  2860. }
  2861. if (chs & 2)
  2862. change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
  2863. HDA_AMP_MUTE,
  2864. *valp ? 0 : HDA_AMP_MUTE);
  2865. hda_call_check_power_status(codec, nid);
  2866. snd_hda_power_down(codec);
  2867. return change;
  2868. }
  2869. EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_put);
  2870. /*
  2871. * bound volume controls
  2872. *
  2873. * bind multiple volumes (# indices, from 0)
  2874. */
  2875. #define AMP_VAL_IDX_SHIFT 19
  2876. #define AMP_VAL_IDX_MASK (0x0f<<19)
  2877. /**
  2878. * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
  2879. * @kcontrol: ctl element
  2880. * @ucontrol: pointer to get/store the data
  2881. *
  2882. * The control element is supposed to have the private_value field
  2883. * set up via HDA_BIND_MUTE*() macros.
  2884. */
  2885. int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
  2886. struct snd_ctl_elem_value *ucontrol)
  2887. {
  2888. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2889. unsigned long pval;
  2890. int err;
  2891. mutex_lock(&codec->control_mutex);
  2892. pval = kcontrol->private_value;
  2893. kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
  2894. err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
  2895. kcontrol->private_value = pval;
  2896. mutex_unlock(&codec->control_mutex);
  2897. return err;
  2898. }
  2899. EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_get);
  2900. /**
  2901. * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
  2902. * @kcontrol: ctl element
  2903. * @ucontrol: pointer to get/store the data
  2904. *
  2905. * The control element is supposed to have the private_value field
  2906. * set up via HDA_BIND_MUTE*() macros.
  2907. */
  2908. int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
  2909. struct snd_ctl_elem_value *ucontrol)
  2910. {
  2911. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2912. unsigned long pval;
  2913. int i, indices, err = 0, change = 0;
  2914. mutex_lock(&codec->control_mutex);
  2915. pval = kcontrol->private_value;
  2916. indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
  2917. for (i = 0; i < indices; i++) {
  2918. kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
  2919. (i << AMP_VAL_IDX_SHIFT);
  2920. err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
  2921. if (err < 0)
  2922. break;
  2923. change |= err;
  2924. }
  2925. kcontrol->private_value = pval;
  2926. mutex_unlock(&codec->control_mutex);
  2927. return err < 0 ? err : change;
  2928. }
  2929. EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_put);
  2930. /**
  2931. * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
  2932. * @kcontrol: referred ctl element
  2933. * @uinfo: pointer to get/store the data
  2934. *
  2935. * The control element is supposed to have the private_value field
  2936. * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
  2937. */
  2938. int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
  2939. struct snd_ctl_elem_info *uinfo)
  2940. {
  2941. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2942. struct hda_bind_ctls *c;
  2943. int err;
  2944. mutex_lock(&codec->control_mutex);
  2945. c = (struct hda_bind_ctls *)kcontrol->private_value;
  2946. kcontrol->private_value = *c->values;
  2947. err = c->ops->info(kcontrol, uinfo);
  2948. kcontrol->private_value = (long)c;
  2949. mutex_unlock(&codec->control_mutex);
  2950. return err;
  2951. }
  2952. EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_info);
  2953. /**
  2954. * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
  2955. * @kcontrol: ctl element
  2956. * @ucontrol: pointer to get/store the data
  2957. *
  2958. * The control element is supposed to have the private_value field
  2959. * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
  2960. */
  2961. int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
  2962. struct snd_ctl_elem_value *ucontrol)
  2963. {
  2964. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2965. struct hda_bind_ctls *c;
  2966. int err;
  2967. mutex_lock(&codec->control_mutex);
  2968. c = (struct hda_bind_ctls *)kcontrol->private_value;
  2969. kcontrol->private_value = *c->values;
  2970. err = c->ops->get(kcontrol, ucontrol);
  2971. kcontrol->private_value = (long)c;
  2972. mutex_unlock(&codec->control_mutex);
  2973. return err;
  2974. }
  2975. EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_get);
  2976. /**
  2977. * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
  2978. * @kcontrol: ctl element
  2979. * @ucontrol: pointer to get/store the data
  2980. *
  2981. * The control element is supposed to have the private_value field
  2982. * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
  2983. */
  2984. int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
  2985. struct snd_ctl_elem_value *ucontrol)
  2986. {
  2987. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2988. struct hda_bind_ctls *c;
  2989. unsigned long *vals;
  2990. int err = 0, change = 0;
  2991. mutex_lock(&codec->control_mutex);
  2992. c = (struct hda_bind_ctls *)kcontrol->private_value;
  2993. for (vals = c->values; *vals; vals++) {
  2994. kcontrol->private_value = *vals;
  2995. err = c->ops->put(kcontrol, ucontrol);
  2996. if (err < 0)
  2997. break;
  2998. change |= err;
  2999. }
  3000. kcontrol->private_value = (long)c;
  3001. mutex_unlock(&codec->control_mutex);
  3002. return err < 0 ? err : change;
  3003. }
  3004. EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_put);
  3005. /**
  3006. * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
  3007. * @kcontrol: ctl element
  3008. * @op_flag: operation flag
  3009. * @size: byte size of input TLV
  3010. * @tlv: TLV data
  3011. *
  3012. * The control element is supposed to have the private_value field
  3013. * set up via HDA_BIND_VOL() macro.
  3014. */
  3015. int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
  3016. unsigned int size, unsigned int __user *tlv)
  3017. {
  3018. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  3019. struct hda_bind_ctls *c;
  3020. int err;
  3021. mutex_lock(&codec->control_mutex);
  3022. c = (struct hda_bind_ctls *)kcontrol->private_value;
  3023. kcontrol->private_value = *c->values;
  3024. err = c->ops->tlv(kcontrol, op_flag, size, tlv);
  3025. kcontrol->private_value = (long)c;
  3026. mutex_unlock(&codec->control_mutex);
  3027. return err;
  3028. }
  3029. EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_tlv);
  3030. struct hda_ctl_ops snd_hda_bind_vol = {
  3031. .info = snd_hda_mixer_amp_volume_info,
  3032. .get = snd_hda_mixer_amp_volume_get,
  3033. .put = snd_hda_mixer_amp_volume_put,
  3034. .tlv = snd_hda_mixer_amp_tlv
  3035. };
  3036. EXPORT_SYMBOL_GPL(snd_hda_bind_vol);
  3037. struct hda_ctl_ops snd_hda_bind_sw = {
  3038. .info = snd_hda_mixer_amp_switch_info,
  3039. .get = snd_hda_mixer_amp_switch_get,
  3040. .put = snd_hda_mixer_amp_switch_put,
  3041. .tlv = snd_hda_mixer_amp_tlv
  3042. };
  3043. EXPORT_SYMBOL_GPL(snd_hda_bind_sw);
  3044. /*
  3045. * SPDIF out controls
  3046. */
  3047. static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
  3048. struct snd_ctl_elem_info *uinfo)
  3049. {
  3050. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  3051. uinfo->count = 1;
  3052. return 0;
  3053. }
  3054. static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
  3055. struct snd_ctl_elem_value *ucontrol)
  3056. {
  3057. ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
  3058. IEC958_AES0_NONAUDIO |
  3059. IEC958_AES0_CON_EMPHASIS_5015 |
  3060. IEC958_AES0_CON_NOT_COPYRIGHT;
  3061. ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
  3062. IEC958_AES1_CON_ORIGINAL;
  3063. return 0;
  3064. }
  3065. static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
  3066. struct snd_ctl_elem_value *ucontrol)
  3067. {
  3068. ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
  3069. IEC958_AES0_NONAUDIO |
  3070. IEC958_AES0_PRO_EMPHASIS_5015;
  3071. return 0;
  3072. }
  3073. static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
  3074. struct snd_ctl_elem_value *ucontrol)
  3075. {
  3076. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  3077. int idx = kcontrol->private_value;
  3078. struct hda_spdif_out *spdif;
  3079. mutex_lock(&codec->spdif_mutex);
  3080. spdif = snd_array_elem(&codec->spdif_out, idx);
  3081. ucontrol->value.iec958.status[0] = spdif->status & 0xff;
  3082. ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
  3083. ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
  3084. ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
  3085. mutex_unlock(&codec->spdif_mutex);
  3086. return 0;
  3087. }
  3088. /* convert from SPDIF status bits to HDA SPDIF bits
  3089. * bit 0 (DigEn) is always set zero (to be filled later)
  3090. */
  3091. static unsigned short convert_from_spdif_status(unsigned int sbits)
  3092. {
  3093. unsigned short val = 0;
  3094. if (sbits & IEC958_AES0_PROFESSIONAL)
  3095. val |= AC_DIG1_PROFESSIONAL;
  3096. if (sbits & IEC958_AES0_NONAUDIO)
  3097. val |= AC_DIG1_NONAUDIO;
  3098. if (sbits & IEC958_AES0_PROFESSIONAL) {
  3099. if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
  3100. IEC958_AES0_PRO_EMPHASIS_5015)
  3101. val |= AC_DIG1_EMPHASIS;
  3102. } else {
  3103. if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
  3104. IEC958_AES0_CON_EMPHASIS_5015)
  3105. val |= AC_DIG1_EMPHASIS;
  3106. if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
  3107. val |= AC_DIG1_COPYRIGHT;
  3108. if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
  3109. val |= AC_DIG1_LEVEL;
  3110. val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
  3111. }
  3112. return val;
  3113. }
  3114. /* convert to SPDIF status bits from HDA SPDIF bits
  3115. */
  3116. static unsigned int convert_to_spdif_status(unsigned short val)
  3117. {
  3118. unsigned int sbits = 0;
  3119. if (val & AC_DIG1_NONAUDIO)
  3120. sbits |= IEC958_AES0_NONAUDIO;
  3121. if (val & AC_DIG1_PROFESSIONAL)
  3122. sbits |= IEC958_AES0_PROFESSIONAL;
  3123. if (sbits & IEC958_AES0_PROFESSIONAL) {
  3124. if (val & AC_DIG1_EMPHASIS)
  3125. sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
  3126. } else {
  3127. if (val & AC_DIG1_EMPHASIS)
  3128. sbits |= IEC958_AES0_CON_EMPHASIS_5015;
  3129. if (!(val & AC_DIG1_COPYRIGHT))
  3130. sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
  3131. if (val & AC_DIG1_LEVEL)
  3132. sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
  3133. sbits |= val & (0x7f << 8);
  3134. }
  3135. return sbits;
  3136. }
  3137. /* set digital convert verbs both for the given NID and its slaves */
  3138. static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
  3139. int verb, int val)
  3140. {
  3141. const hda_nid_t *d;
  3142. snd_hda_codec_write_cache(codec, nid, 0, verb, val);
  3143. d = codec->slave_dig_outs;
  3144. if (!d)
  3145. return;
  3146. for (; *d; d++)
  3147. snd_hda_codec_write_cache(codec, *d, 0, verb, val);
  3148. }
  3149. static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
  3150. int dig1, int dig2)
  3151. {
  3152. if (dig1 != -1)
  3153. set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
  3154. if (dig2 != -1)
  3155. set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
  3156. }
  3157. static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
  3158. struct snd_ctl_elem_value *ucontrol)
  3159. {
  3160. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  3161. int idx = kcontrol->private_value;
  3162. struct hda_spdif_out *spdif;
  3163. hda_nid_t nid;
  3164. unsigned short val;
  3165. int change;
  3166. mutex_lock(&codec->spdif_mutex);
  3167. spdif = snd_array_elem(&codec->spdif_out, idx);
  3168. nid = spdif->nid;
  3169. spdif->status = ucontrol->value.iec958.status[0] |
  3170. ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
  3171. ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
  3172. ((unsigned int)ucontrol->value.iec958.status[3] << 24);
  3173. val = convert_from_spdif_status(spdif->status);
  3174. val |= spdif->ctls & 1;
  3175. change = spdif->ctls != val;
  3176. spdif->ctls = val;
  3177. if (change && nid != (u16)-1)
  3178. set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
  3179. mutex_unlock(&codec->spdif_mutex);
  3180. return change;
  3181. }
  3182. #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
  3183. static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
  3184. struct snd_ctl_elem_value *ucontrol)
  3185. {
  3186. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  3187. int idx = kcontrol->private_value;
  3188. struct hda_spdif_out *spdif;
  3189. mutex_lock(&codec->spdif_mutex);
  3190. spdif = snd_array_elem(&codec->spdif_out, idx);
  3191. ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
  3192. mutex_unlock(&codec->spdif_mutex);
  3193. return 0;
  3194. }
  3195. static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
  3196. int dig1, int dig2)
  3197. {
  3198. set_dig_out_convert(codec, nid, dig1, dig2);
  3199. /* unmute amp switch (if any) */
  3200. if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
  3201. (dig1 & AC_DIG1_ENABLE))
  3202. snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
  3203. HDA_AMP_MUTE, 0);
  3204. }
  3205. static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
  3206. struct snd_ctl_elem_value *ucontrol)
  3207. {
  3208. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  3209. int idx = kcontrol->private_value;
  3210. struct hda_spdif_out *spdif;
  3211. hda_nid_t nid;
  3212. unsigned short val;
  3213. int change;
  3214. mutex_lock(&codec->spdif_mutex);
  3215. spdif = snd_array_elem(&codec->spdif_out, idx);
  3216. nid = spdif->nid;
  3217. val = spdif->ctls & ~AC_DIG1_ENABLE;
  3218. if (ucontrol->value.integer.value[0])
  3219. val |= AC_DIG1_ENABLE;
  3220. change = spdif->ctls != val;
  3221. spdif->ctls = val;
  3222. if (change && nid != (u16)-1)
  3223. set_spdif_ctls(codec, nid, val & 0xff, -1);
  3224. mutex_unlock(&codec->spdif_mutex);
  3225. return change;
  3226. }
  3227. static struct snd_kcontrol_new dig_mixes[] = {
  3228. {
  3229. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  3230. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  3231. .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
  3232. .info = snd_hda_spdif_mask_info,
  3233. .get = snd_hda_spdif_cmask_get,
  3234. },
  3235. {
  3236. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  3237. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  3238. .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
  3239. .info = snd_hda_spdif_mask_info,
  3240. .get = snd_hda_spdif_pmask_get,
  3241. },
  3242. {
  3243. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  3244. .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
  3245. .info = snd_hda_spdif_mask_info,
  3246. .get = snd_hda_spdif_default_get,
  3247. .put = snd_hda_spdif_default_put,
  3248. },
  3249. {
  3250. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  3251. .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
  3252. .info = snd_hda_spdif_out_switch_info,
  3253. .get = snd_hda_spdif_out_switch_get,
  3254. .put = snd_hda_spdif_out_switch_put,
  3255. },
  3256. { } /* end */
  3257. };
  3258. /**
  3259. * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
  3260. * @codec: the HDA codec
  3261. * @associated_nid: NID that new ctls associated with
  3262. * @cvt_nid: converter NID
  3263. * @type: HDA_PCM_TYPE_*
  3264. * Creates controls related with the digital output.
  3265. * Called from each patch supporting the digital out.
  3266. *
  3267. * Returns 0 if successful, or a negative error code.
  3268. */
  3269. int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
  3270. hda_nid_t associated_nid,
  3271. hda_nid_t cvt_nid,
  3272. int type)
  3273. {
  3274. int err;
  3275. struct snd_kcontrol *kctl;
  3276. struct snd_kcontrol_new *dig_mix;
  3277. int idx = 0;
  3278. const int spdif_index = 16;
  3279. struct hda_spdif_out *spdif;
  3280. struct hda_bus *bus = codec->bus;
  3281. if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
  3282. type == HDA_PCM_TYPE_SPDIF) {
  3283. idx = spdif_index;
  3284. } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
  3285. type == HDA_PCM_TYPE_HDMI) {
  3286. /* suppose a single SPDIF device */
  3287. for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
  3288. kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0);
  3289. if (!kctl)
  3290. break;
  3291. kctl->id.index = spdif_index;
  3292. }
  3293. bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
  3294. }
  3295. if (!bus->primary_dig_out_type)
  3296. bus->primary_dig_out_type = type;
  3297. idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx);
  3298. if (idx < 0) {
  3299. codec_err(codec, "too many IEC958 outputs\n");
  3300. return -EBUSY;
  3301. }
  3302. spdif = snd_array_new(&codec->spdif_out);
  3303. if (!spdif)
  3304. return -ENOMEM;
  3305. for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
  3306. kctl = snd_ctl_new1(dig_mix, codec);
  3307. if (!kctl)
  3308. return -ENOMEM;
  3309. kctl->id.index = idx;
  3310. kctl->private_value = codec->spdif_out.used - 1;
  3311. err = snd_hda_ctl_add(codec, associated_nid, kctl);
  3312. if (err < 0)
  3313. return err;
  3314. }
  3315. spdif->nid = cvt_nid;
  3316. spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
  3317. AC_VERB_GET_DIGI_CONVERT_1, 0);
  3318. spdif->status = convert_to_spdif_status(spdif->ctls);
  3319. return 0;
  3320. }
  3321. EXPORT_SYMBOL_GPL(snd_hda_create_dig_out_ctls);
  3322. /**
  3323. * snd_hda_spdif_out_of_nid - get the hda_spdif_out entry from the given NID
  3324. * @codec: the HDA codec
  3325. * @nid: widget NID
  3326. *
  3327. * call within spdif_mutex lock
  3328. */
  3329. struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
  3330. hda_nid_t nid)
  3331. {
  3332. int i;
  3333. for (i = 0; i < codec->spdif_out.used; i++) {
  3334. struct hda_spdif_out *spdif =
  3335. snd_array_elem(&codec->spdif_out, i);
  3336. if (spdif->nid == nid)
  3337. return spdif;
  3338. }
  3339. return NULL;
  3340. }
  3341. EXPORT_SYMBOL_GPL(snd_hda_spdif_out_of_nid);
  3342. /**
  3343. * snd_hda_spdif_ctls_unassign - Unassign the given SPDIF ctl
  3344. * @codec: the HDA codec
  3345. * @idx: the SPDIF ctl index
  3346. *
  3347. * Unassign the widget from the given SPDIF control.
  3348. */
  3349. void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
  3350. {
  3351. struct hda_spdif_out *spdif;
  3352. mutex_lock(&codec->spdif_mutex);
  3353. spdif = snd_array_elem(&codec->spdif_out, idx);
  3354. spdif->nid = (u16)-1;
  3355. mutex_unlock(&codec->spdif_mutex);
  3356. }
  3357. EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_unassign);
  3358. /**
  3359. * snd_hda_spdif_ctls_assign - Assign the SPDIF controls to the given NID
  3360. * @codec: the HDA codec
  3361. * @idx: the SPDIF ctl idx
  3362. * @nid: widget NID
  3363. *
  3364. * Assign the widget to the SPDIF control with the given index.
  3365. */
  3366. void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
  3367. {
  3368. struct hda_spdif_out *spdif;
  3369. unsigned short val;
  3370. mutex_lock(&codec->spdif_mutex);
  3371. spdif = snd_array_elem(&codec->spdif_out, idx);
  3372. if (spdif->nid != nid) {
  3373. spdif->nid = nid;
  3374. val = spdif->ctls;
  3375. set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
  3376. }
  3377. mutex_unlock(&codec->spdif_mutex);
  3378. }
  3379. EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_assign);
  3380. /*
  3381. * SPDIF sharing with analog output
  3382. */
  3383. static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
  3384. struct snd_ctl_elem_value *ucontrol)
  3385. {
  3386. struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
  3387. ucontrol->value.integer.value[0] = mout->share_spdif;
  3388. return 0;
  3389. }
  3390. static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
  3391. struct snd_ctl_elem_value *ucontrol)
  3392. {
  3393. struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
  3394. mout->share_spdif = !!ucontrol->value.integer.value[0];
  3395. return 0;
  3396. }
  3397. static struct snd_kcontrol_new spdif_share_sw = {
  3398. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  3399. .name = "IEC958 Default PCM Playback Switch",
  3400. .info = snd_ctl_boolean_mono_info,
  3401. .get = spdif_share_sw_get,
  3402. .put = spdif_share_sw_put,
  3403. };
  3404. /**
  3405. * snd_hda_create_spdif_share_sw - create Default PCM switch
  3406. * @codec: the HDA codec
  3407. * @mout: multi-out instance
  3408. */
  3409. int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
  3410. struct hda_multi_out *mout)
  3411. {
  3412. struct snd_kcontrol *kctl;
  3413. if (!mout->dig_out_nid)
  3414. return 0;
  3415. kctl = snd_ctl_new1(&spdif_share_sw, mout);
  3416. if (!kctl)
  3417. return -ENOMEM;
  3418. /* ATTENTION: here mout is passed as private_data, instead of codec */
  3419. return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl);
  3420. }
  3421. EXPORT_SYMBOL_GPL(snd_hda_create_spdif_share_sw);
  3422. /*
  3423. * SPDIF input
  3424. */
  3425. #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
  3426. static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
  3427. struct snd_ctl_elem_value *ucontrol)
  3428. {
  3429. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  3430. ucontrol->value.integer.value[0] = codec->spdif_in_enable;
  3431. return 0;
  3432. }
  3433. static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
  3434. struct snd_ctl_elem_value *ucontrol)
  3435. {
  3436. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  3437. hda_nid_t nid = kcontrol->private_value;
  3438. unsigned int val = !!ucontrol->value.integer.value[0];
  3439. int change;
  3440. mutex_lock(&codec->spdif_mutex);
  3441. change = codec->spdif_in_enable != val;
  3442. if (change) {
  3443. codec->spdif_in_enable = val;
  3444. snd_hda_codec_write_cache(codec, nid, 0,
  3445. AC_VERB_SET_DIGI_CONVERT_1, val);
  3446. }
  3447. mutex_unlock(&codec->spdif_mutex);
  3448. return change;
  3449. }
  3450. static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
  3451. struct snd_ctl_elem_value *ucontrol)
  3452. {
  3453. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  3454. hda_nid_t nid = kcontrol->private_value;
  3455. unsigned short val;
  3456. unsigned int sbits;
  3457. val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
  3458. sbits = convert_to_spdif_status(val);
  3459. ucontrol->value.iec958.status[0] = sbits;
  3460. ucontrol->value.iec958.status[1] = sbits >> 8;
  3461. ucontrol->value.iec958.status[2] = sbits >> 16;
  3462. ucontrol->value.iec958.status[3] = sbits >> 24;
  3463. return 0;
  3464. }
  3465. static struct snd_kcontrol_new dig_in_ctls[] = {
  3466. {
  3467. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  3468. .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
  3469. .info = snd_hda_spdif_in_switch_info,
  3470. .get = snd_hda_spdif_in_switch_get,
  3471. .put = snd_hda_spdif_in_switch_put,
  3472. },
  3473. {
  3474. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  3475. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  3476. .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
  3477. .info = snd_hda_spdif_mask_info,
  3478. .get = snd_hda_spdif_in_status_get,
  3479. },
  3480. { } /* end */
  3481. };
  3482. /**
  3483. * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
  3484. * @codec: the HDA codec
  3485. * @nid: audio in widget NID
  3486. *
  3487. * Creates controls related with the SPDIF input.
  3488. * Called from each patch supporting the SPDIF in.
  3489. *
  3490. * Returns 0 if successful, or a negative error code.
  3491. */
  3492. int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
  3493. {
  3494. int err;
  3495. struct snd_kcontrol *kctl;
  3496. struct snd_kcontrol_new *dig_mix;
  3497. int idx;
  3498. idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
  3499. if (idx < 0) {
  3500. codec_err(codec, "too many IEC958 inputs\n");
  3501. return -EBUSY;
  3502. }
  3503. for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
  3504. kctl = snd_ctl_new1(dig_mix, codec);
  3505. if (!kctl)
  3506. return -ENOMEM;
  3507. kctl->private_value = nid;
  3508. err = snd_hda_ctl_add(codec, nid, kctl);
  3509. if (err < 0)
  3510. return err;
  3511. }
  3512. codec->spdif_in_enable =
  3513. snd_hda_codec_read(codec, nid, 0,
  3514. AC_VERB_GET_DIGI_CONVERT_1, 0) &
  3515. AC_DIG1_ENABLE;
  3516. return 0;
  3517. }
  3518. EXPORT_SYMBOL_GPL(snd_hda_create_spdif_in_ctls);
  3519. /*
  3520. * command cache
  3521. */
  3522. /* build a 31bit cache key with the widget id and the command parameter */
  3523. #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
  3524. #define get_cmd_cache_nid(key) ((key) & 0xff)
  3525. #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
  3526. /**
  3527. * snd_hda_codec_write_cache - send a single command with caching
  3528. * @codec: the HDA codec
  3529. * @nid: NID to send the command
  3530. * @flags: optional bit flags
  3531. * @verb: the verb to send
  3532. * @parm: the parameter for the verb
  3533. *
  3534. * Send a single command without waiting for response.
  3535. *
  3536. * Returns 0 if successful, or a negative error code.
  3537. */
  3538. int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
  3539. int flags, unsigned int verb, unsigned int parm)
  3540. {
  3541. int err;
  3542. struct hda_cache_head *c;
  3543. u32 key;
  3544. unsigned int cache_only;
  3545. cache_only = codec->cached_write;
  3546. if (!cache_only) {
  3547. err = snd_hda_codec_write(codec, nid, flags, verb, parm);
  3548. if (err < 0)
  3549. return err;
  3550. }
  3551. /* parm may contain the verb stuff for get/set amp */
  3552. verb = verb | (parm >> 8);
  3553. parm &= 0xff;
  3554. key = build_cmd_cache_key(nid, verb);
  3555. mutex_lock(&codec->bus->cmd_mutex);
  3556. c = get_alloc_hash(&codec->cmd_cache, key);
  3557. if (c) {
  3558. c->val = parm;
  3559. c->dirty = cache_only;
  3560. }
  3561. mutex_unlock(&codec->bus->cmd_mutex);
  3562. return 0;
  3563. }
  3564. EXPORT_SYMBOL_GPL(snd_hda_codec_write_cache);
  3565. /**
  3566. * snd_hda_codec_update_cache - check cache and write the cmd only when needed
  3567. * @codec: the HDA codec
  3568. * @nid: NID to send the command
  3569. * @flags: optional bit flags
  3570. * @verb: the verb to send
  3571. * @parm: the parameter for the verb
  3572. *
  3573. * This function works like snd_hda_codec_write_cache(), but it doesn't send
  3574. * command if the parameter is already identical with the cached value.
  3575. * If not, it sends the command and refreshes the cache.
  3576. *
  3577. * Returns 0 if successful, or a negative error code.
  3578. */
  3579. int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
  3580. int flags, unsigned int verb, unsigned int parm)
  3581. {
  3582. struct hda_cache_head *c;
  3583. u32 key;
  3584. /* parm may contain the verb stuff for get/set amp */
  3585. verb = verb | (parm >> 8);
  3586. parm &= 0xff;
  3587. key = build_cmd_cache_key(nid, verb);
  3588. mutex_lock(&codec->bus->cmd_mutex);
  3589. c = get_hash(&codec->cmd_cache, key);
  3590. if (c && c->val == parm) {
  3591. mutex_unlock(&codec->bus->cmd_mutex);
  3592. return 0;
  3593. }
  3594. mutex_unlock(&codec->bus->cmd_mutex);
  3595. return snd_hda_codec_write_cache(codec, nid, flags, verb, parm);
  3596. }
  3597. EXPORT_SYMBOL_GPL(snd_hda_codec_update_cache);
  3598. /**
  3599. * snd_hda_codec_resume_cache - Resume the all commands from the cache
  3600. * @codec: HD-audio codec
  3601. *
  3602. * Execute all verbs recorded in the command caches to resume.
  3603. */
  3604. void snd_hda_codec_resume_cache(struct hda_codec *codec)
  3605. {
  3606. int i;
  3607. mutex_lock(&codec->hash_mutex);
  3608. codec->cached_write = 0;
  3609. for (i = 0; i < codec->cmd_cache.buf.used; i++) {
  3610. struct hda_cache_head *buffer;
  3611. u32 key;
  3612. buffer = snd_array_elem(&codec->cmd_cache.buf, i);
  3613. key = buffer->key;
  3614. if (!key)
  3615. continue;
  3616. if (!buffer->dirty)
  3617. continue;
  3618. buffer->dirty = 0;
  3619. mutex_unlock(&codec->hash_mutex);
  3620. snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
  3621. get_cmd_cache_cmd(key), buffer->val);
  3622. mutex_lock(&codec->hash_mutex);
  3623. }
  3624. mutex_unlock(&codec->hash_mutex);
  3625. }
  3626. EXPORT_SYMBOL_GPL(snd_hda_codec_resume_cache);
  3627. /**
  3628. * snd_hda_sequence_write_cache - sequence writes with caching
  3629. * @codec: the HDA codec
  3630. * @seq: VERB array to send
  3631. *
  3632. * Send the commands sequentially from the given array.
  3633. * Thte commands are recorded on cache for power-save and resume.
  3634. * The array must be terminated with NID=0.
  3635. */
  3636. void snd_hda_sequence_write_cache(struct hda_codec *codec,
  3637. const struct hda_verb *seq)
  3638. {
  3639. for (; seq->nid; seq++)
  3640. snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
  3641. seq->param);
  3642. }
  3643. EXPORT_SYMBOL_GPL(snd_hda_sequence_write_cache);
  3644. /**
  3645. * snd_hda_codec_flush_cache - Execute all pending (cached) amps / verbs
  3646. * @codec: HD-audio codec
  3647. */
  3648. void snd_hda_codec_flush_cache(struct hda_codec *codec)
  3649. {
  3650. snd_hda_codec_resume_amp(codec);
  3651. snd_hda_codec_resume_cache(codec);
  3652. }
  3653. EXPORT_SYMBOL_GPL(snd_hda_codec_flush_cache);
  3654. /**
  3655. * snd_hda_codec_set_power_to_all - Set the power state to all widgets
  3656. * @codec: the HDA codec
  3657. * @fg: function group (not used now)
  3658. * @power_state: the power state to set (AC_PWRST_*)
  3659. *
  3660. * Set the given power state to all widgets that have the power control.
  3661. * If the codec has power_filter set, it evaluates the power state and
  3662. * filter out if it's unchanged as D3.
  3663. */
  3664. void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
  3665. unsigned int power_state)
  3666. {
  3667. hda_nid_t nid = codec->start_nid;
  3668. int i;
  3669. for (i = 0; i < codec->num_nodes; i++, nid++) {
  3670. unsigned int wcaps = get_wcaps(codec, nid);
  3671. unsigned int state = power_state;
  3672. if (!(wcaps & AC_WCAP_POWER))
  3673. continue;
  3674. if (codec->power_filter) {
  3675. state = codec->power_filter(codec, nid, power_state);
  3676. if (state != power_state && power_state == AC_PWRST_D3)
  3677. continue;
  3678. }
  3679. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
  3680. state);
  3681. }
  3682. }
  3683. EXPORT_SYMBOL_GPL(snd_hda_codec_set_power_to_all);
  3684. /*
  3685. * supported power states check
  3686. */
  3687. static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg,
  3688. unsigned int power_state)
  3689. {
  3690. int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE);
  3691. if (sup == -1)
  3692. return false;
  3693. if (sup & power_state)
  3694. return true;
  3695. else
  3696. return false;
  3697. }
  3698. /*
  3699. * wait until the state is reached, returns the current state
  3700. */
  3701. static unsigned int hda_sync_power_state(struct hda_codec *codec,
  3702. hda_nid_t fg,
  3703. unsigned int power_state)
  3704. {
  3705. unsigned long end_time = jiffies + msecs_to_jiffies(500);
  3706. unsigned int state, actual_state;
  3707. for (;;) {
  3708. state = snd_hda_codec_read(codec, fg, 0,
  3709. AC_VERB_GET_POWER_STATE, 0);
  3710. if (state & AC_PWRST_ERROR)
  3711. break;
  3712. actual_state = (state >> 4) & 0x0f;
  3713. if (actual_state == power_state)
  3714. break;
  3715. if (time_after_eq(jiffies, end_time))
  3716. break;
  3717. /* wait until the codec reachs to the target state */
  3718. msleep(1);
  3719. }
  3720. return state;
  3721. }
  3722. /**
  3723. * snd_hda_codec_eapd_power_filter - A power filter callback for EAPD
  3724. * @codec: the HDA codec
  3725. * @nid: widget NID
  3726. * @power_state: power state to evalue
  3727. *
  3728. * Don't power down the widget if it controls eapd and EAPD_BTLENABLE is set.
  3729. * This can be used a codec power_filter callback.
  3730. */
  3731. unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec,
  3732. hda_nid_t nid,
  3733. unsigned int power_state)
  3734. {
  3735. if (nid == codec->afg || nid == codec->mfg)
  3736. return power_state;
  3737. if (power_state == AC_PWRST_D3 &&
  3738. get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN &&
  3739. (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
  3740. int eapd = snd_hda_codec_read(codec, nid, 0,
  3741. AC_VERB_GET_EAPD_BTLENABLE, 0);
  3742. if (eapd & 0x02)
  3743. return AC_PWRST_D0;
  3744. }
  3745. return power_state;
  3746. }
  3747. EXPORT_SYMBOL_GPL(snd_hda_codec_eapd_power_filter);
  3748. /*
  3749. * set power state of the codec, and return the power state
  3750. */
  3751. static unsigned int hda_set_power_state(struct hda_codec *codec,
  3752. unsigned int power_state)
  3753. {
  3754. hda_nid_t fg = codec->afg ? codec->afg : codec->mfg;
  3755. int count;
  3756. unsigned int state;
  3757. int flags = 0;
  3758. /* this delay seems necessary to avoid click noise at power-down */
  3759. if (power_state == AC_PWRST_D3) {
  3760. if (codec->depop_delay < 0)
  3761. msleep(codec->epss ? 10 : 100);
  3762. else if (codec->depop_delay > 0)
  3763. msleep(codec->depop_delay);
  3764. flags = HDA_RW_NO_RESPONSE_FALLBACK;
  3765. }
  3766. /* repeat power states setting at most 10 times*/
  3767. for (count = 0; count < 10; count++) {
  3768. if (codec->patch_ops.set_power_state)
  3769. codec->patch_ops.set_power_state(codec, fg,
  3770. power_state);
  3771. else {
  3772. state = power_state;
  3773. if (codec->power_filter)
  3774. state = codec->power_filter(codec, fg, state);
  3775. if (state == power_state || power_state != AC_PWRST_D3)
  3776. snd_hda_codec_read(codec, fg, flags,
  3777. AC_VERB_SET_POWER_STATE,
  3778. state);
  3779. snd_hda_codec_set_power_to_all(codec, fg, power_state);
  3780. }
  3781. state = hda_sync_power_state(codec, fg, power_state);
  3782. if (!(state & AC_PWRST_ERROR))
  3783. break;
  3784. }
  3785. return state;
  3786. }
  3787. /* sync power states of all widgets;
  3788. * this is called at the end of codec parsing
  3789. */
  3790. static void sync_power_up_states(struct hda_codec *codec)
  3791. {
  3792. hda_nid_t nid = codec->start_nid;
  3793. int i;
  3794. /* don't care if no filter is used */
  3795. if (!codec->power_filter)
  3796. return;
  3797. for (i = 0; i < codec->num_nodes; i++, nid++) {
  3798. unsigned int wcaps = get_wcaps(codec, nid);
  3799. unsigned int target;
  3800. if (!(wcaps & AC_WCAP_POWER))
  3801. continue;
  3802. target = codec->power_filter(codec, nid, AC_PWRST_D0);
  3803. if (target == AC_PWRST_D0)
  3804. continue;
  3805. if (!snd_hda_check_power_state(codec, nid, target))
  3806. snd_hda_codec_write(codec, nid, 0,
  3807. AC_VERB_SET_POWER_STATE, target);
  3808. }
  3809. }
  3810. #ifdef CONFIG_SND_HDA_RECONFIG
  3811. /* execute additional init verbs */
  3812. static void hda_exec_init_verbs(struct hda_codec *codec)
  3813. {
  3814. if (codec->init_verbs.list)
  3815. snd_hda_sequence_write(codec, codec->init_verbs.list);
  3816. }
  3817. #else
  3818. static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
  3819. #endif
  3820. #ifdef CONFIG_PM
  3821. /*
  3822. * call suspend and power-down; used both from PM and power-save
  3823. * this function returns the power state in the end
  3824. */
  3825. static unsigned int hda_call_codec_suspend(struct hda_codec *codec, bool in_wq)
  3826. {
  3827. unsigned int state;
  3828. codec->in_pm = 1;
  3829. if (codec->patch_ops.suspend)
  3830. codec->patch_ops.suspend(codec);
  3831. hda_cleanup_all_streams(codec);
  3832. state = hda_set_power_state(codec, AC_PWRST_D3);
  3833. /* Cancel delayed work if we aren't currently running from it. */
  3834. if (!in_wq)
  3835. cancel_delayed_work_sync(&codec->power_work);
  3836. spin_lock(&codec->power_lock);
  3837. snd_hda_update_power_acct(codec);
  3838. trace_hda_power_down(codec);
  3839. codec->power_on = 0;
  3840. codec->power_transition = 0;
  3841. codec->power_jiffies = jiffies;
  3842. spin_unlock(&codec->power_lock);
  3843. codec->in_pm = 0;
  3844. return state;
  3845. }
  3846. /* mark all entries of cmd and amp caches dirty */
  3847. static void hda_mark_cmd_cache_dirty(struct hda_codec *codec)
  3848. {
  3849. int i;
  3850. for (i = 0; i < codec->cmd_cache.buf.used; i++) {
  3851. struct hda_cache_head *cmd;
  3852. cmd = snd_array_elem(&codec->cmd_cache.buf, i);
  3853. cmd->dirty = 1;
  3854. }
  3855. for (i = 0; i < codec->amp_cache.buf.used; i++) {
  3856. struct hda_amp_info *amp;
  3857. amp = snd_array_elem(&codec->amp_cache.buf, i);
  3858. amp->head.dirty = 1;
  3859. }
  3860. }
  3861. /*
  3862. * kick up codec; used both from PM and power-save
  3863. */
  3864. static void hda_call_codec_resume(struct hda_codec *codec)
  3865. {
  3866. codec->in_pm = 1;
  3867. hda_mark_cmd_cache_dirty(codec);
  3868. /* set as if powered on for avoiding re-entering the resume
  3869. * in the resume / power-save sequence
  3870. */
  3871. hda_keep_power_on(codec);
  3872. hda_set_power_state(codec, AC_PWRST_D0);
  3873. restore_shutup_pins(codec);
  3874. hda_exec_init_verbs(codec);
  3875. snd_hda_jack_set_dirty_all(codec);
  3876. if (codec->patch_ops.resume)
  3877. codec->patch_ops.resume(codec);
  3878. else {
  3879. if (codec->patch_ops.init)
  3880. codec->patch_ops.init(codec);
  3881. snd_hda_codec_resume_amp(codec);
  3882. snd_hda_codec_resume_cache(codec);
  3883. }
  3884. if (codec->jackpoll_interval)
  3885. hda_jackpoll_work(&codec->jackpoll_work.work);
  3886. else
  3887. snd_hda_jack_report_sync(codec);
  3888. codec->in_pm = 0;
  3889. snd_hda_power_down(codec); /* flag down before returning */
  3890. }
  3891. #endif /* CONFIG_PM */
  3892. /**
  3893. * snd_hda_build_controls - build mixer controls
  3894. * @bus: the BUS
  3895. *
  3896. * Creates mixer controls for each codec included in the bus.
  3897. *
  3898. * Returns 0 if successful, otherwise a negative error code.
  3899. */
  3900. int snd_hda_build_controls(struct hda_bus *bus)
  3901. {
  3902. struct hda_codec *codec;
  3903. list_for_each_entry(codec, &bus->codec_list, list) {
  3904. int err = snd_hda_codec_build_controls(codec);
  3905. if (err < 0) {
  3906. codec_err(codec,
  3907. "cannot build controls for #%d (error %d)\n",
  3908. codec->addr, err);
  3909. err = snd_hda_codec_reset(codec);
  3910. if (err < 0) {
  3911. codec_err(codec,
  3912. "cannot revert codec\n");
  3913. return err;
  3914. }
  3915. }
  3916. }
  3917. return 0;
  3918. }
  3919. EXPORT_SYMBOL_GPL(snd_hda_build_controls);
  3920. /*
  3921. * add standard channel maps if not specified
  3922. */
  3923. static int add_std_chmaps(struct hda_codec *codec)
  3924. {
  3925. int i, str, err;
  3926. for (i = 0; i < codec->num_pcms; i++) {
  3927. for (str = 0; str < 2; str++) {
  3928. struct snd_pcm *pcm = codec->pcm_info[i].pcm;
  3929. struct hda_pcm_stream *hinfo =
  3930. &codec->pcm_info[i].stream[str];
  3931. struct snd_pcm_chmap *chmap;
  3932. const struct snd_pcm_chmap_elem *elem;
  3933. if (codec->pcm_info[i].own_chmap)
  3934. continue;
  3935. if (!pcm || !hinfo->substreams)
  3936. continue;
  3937. elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
  3938. err = snd_pcm_add_chmap_ctls(pcm, str, elem,
  3939. hinfo->channels_max,
  3940. 0, &chmap);
  3941. if (err < 0)
  3942. return err;
  3943. chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
  3944. }
  3945. }
  3946. return 0;
  3947. }
  3948. /* default channel maps for 2.1 speakers;
  3949. * since HD-audio supports only stereo, odd number channels are omitted
  3950. */
  3951. const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
  3952. { .channels = 2,
  3953. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
  3954. { .channels = 4,
  3955. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
  3956. SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
  3957. { }
  3958. };
  3959. EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
  3960. int snd_hda_codec_build_controls(struct hda_codec *codec)
  3961. {
  3962. int err = 0;
  3963. hda_exec_init_verbs(codec);
  3964. /* continue to initialize... */
  3965. if (codec->patch_ops.init)
  3966. err = codec->patch_ops.init(codec);
  3967. if (!err && codec->patch_ops.build_controls)
  3968. err = codec->patch_ops.build_controls(codec);
  3969. if (err < 0)
  3970. return err;
  3971. /* we create chmaps here instead of build_pcms */
  3972. err = add_std_chmaps(codec);
  3973. if (err < 0)
  3974. return err;
  3975. if (codec->jackpoll_interval)
  3976. hda_jackpoll_work(&codec->jackpoll_work.work);
  3977. else
  3978. snd_hda_jack_report_sync(codec); /* call at the last init point */
  3979. sync_power_up_states(codec);
  3980. return 0;
  3981. }
  3982. /*
  3983. * stream formats
  3984. */
  3985. struct hda_rate_tbl {
  3986. unsigned int hz;
  3987. unsigned int alsa_bits;
  3988. unsigned int hda_fmt;
  3989. };
  3990. /* rate = base * mult / div */
  3991. #define HDA_RATE(base, mult, div) \
  3992. (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
  3993. (((div) - 1) << AC_FMT_DIV_SHIFT))
  3994. static struct hda_rate_tbl rate_bits[] = {
  3995. /* rate in Hz, ALSA rate bitmask, HDA format value */
  3996. /* autodetected value used in snd_hda_query_supported_pcm */
  3997. { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
  3998. { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
  3999. { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
  4000. { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
  4001. { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
  4002. { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
  4003. { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
  4004. { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
  4005. { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
  4006. { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
  4007. { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
  4008. #define AC_PAR_PCM_RATE_BITS 11
  4009. /* up to bits 10, 384kHZ isn't supported properly */
  4010. /* not autodetected value */
  4011. { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
  4012. { 0 } /* terminator */
  4013. };
  4014. /**
  4015. * snd_hda_calc_stream_format - calculate format bitset
  4016. * @codec: HD-audio codec
  4017. * @rate: the sample rate
  4018. * @channels: the number of channels
  4019. * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
  4020. * @maxbps: the max. bps
  4021. * @spdif_ctls: HD-audio SPDIF status bits (0 if irrelevant)
  4022. *
  4023. * Calculate the format bitset from the given rate, channels and th PCM format.
  4024. *
  4025. * Return zero if invalid.
  4026. */
  4027. unsigned int snd_hda_calc_stream_format(struct hda_codec *codec,
  4028. unsigned int rate,
  4029. unsigned int channels,
  4030. unsigned int format,
  4031. unsigned int maxbps,
  4032. unsigned short spdif_ctls)
  4033. {
  4034. int i;
  4035. unsigned int val = 0;
  4036. for (i = 0; rate_bits[i].hz; i++)
  4037. if (rate_bits[i].hz == rate) {
  4038. val = rate_bits[i].hda_fmt;
  4039. break;
  4040. }
  4041. if (!rate_bits[i].hz) {
  4042. codec_dbg(codec, "invalid rate %d\n", rate);
  4043. return 0;
  4044. }
  4045. if (channels == 0 || channels > 8) {
  4046. codec_dbg(codec, "invalid channels %d\n", channels);
  4047. return 0;
  4048. }
  4049. val |= channels - 1;
  4050. switch (snd_pcm_format_width(format)) {
  4051. case 8:
  4052. val |= AC_FMT_BITS_8;
  4053. break;
  4054. case 16:
  4055. val |= AC_FMT_BITS_16;
  4056. break;
  4057. case 20:
  4058. case 24:
  4059. case 32:
  4060. if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
  4061. val |= AC_FMT_BITS_32;
  4062. else if (maxbps >= 24)
  4063. val |= AC_FMT_BITS_24;
  4064. else
  4065. val |= AC_FMT_BITS_20;
  4066. break;
  4067. default:
  4068. codec_dbg(codec, "invalid format width %d\n",
  4069. snd_pcm_format_width(format));
  4070. return 0;
  4071. }
  4072. if (spdif_ctls & AC_DIG1_NONAUDIO)
  4073. val |= AC_FMT_TYPE_NON_PCM;
  4074. return val;
  4075. }
  4076. EXPORT_SYMBOL_GPL(snd_hda_calc_stream_format);
  4077. static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid,
  4078. int dir)
  4079. {
  4080. unsigned int val = 0;
  4081. if (nid != codec->afg &&
  4082. (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
  4083. val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
  4084. if (!val || val == -1)
  4085. val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
  4086. if (!val || val == -1)
  4087. return 0;
  4088. return val;
  4089. }
  4090. static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
  4091. {
  4092. return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid),
  4093. get_pcm_param);
  4094. }
  4095. static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid,
  4096. int dir)
  4097. {
  4098. unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
  4099. if (!streams || streams == -1)
  4100. streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
  4101. if (!streams || streams == -1)
  4102. return 0;
  4103. return streams;
  4104. }
  4105. static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
  4106. {
  4107. return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid),
  4108. get_stream_param);
  4109. }
  4110. /**
  4111. * snd_hda_query_supported_pcm - query the supported PCM rates and formats
  4112. * @codec: the HDA codec
  4113. * @nid: NID to query
  4114. * @ratesp: the pointer to store the detected rate bitflags
  4115. * @formatsp: the pointer to store the detected formats
  4116. * @bpsp: the pointer to store the detected format widths
  4117. *
  4118. * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
  4119. * or @bsps argument is ignored.
  4120. *
  4121. * Returns 0 if successful, otherwise a negative error code.
  4122. */
  4123. int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
  4124. u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
  4125. {
  4126. unsigned int i, val, wcaps;
  4127. wcaps = get_wcaps(codec, nid);
  4128. val = query_pcm_param(codec, nid);
  4129. if (ratesp) {
  4130. u32 rates = 0;
  4131. for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
  4132. if (val & (1 << i))
  4133. rates |= rate_bits[i].alsa_bits;
  4134. }
  4135. if (rates == 0) {
  4136. codec_err(codec,
  4137. "rates == 0 (nid=0x%x, val=0x%x, ovrd=%i)\n",
  4138. nid, val,
  4139. (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
  4140. return -EIO;
  4141. }
  4142. *ratesp = rates;
  4143. }
  4144. if (formatsp || bpsp) {
  4145. u64 formats = 0;
  4146. unsigned int streams, bps;
  4147. streams = query_stream_param(codec, nid);
  4148. if (!streams)
  4149. return -EIO;
  4150. bps = 0;
  4151. if (streams & AC_SUPFMT_PCM) {
  4152. if (val & AC_SUPPCM_BITS_8) {
  4153. formats |= SNDRV_PCM_FMTBIT_U8;
  4154. bps = 8;
  4155. }
  4156. if (val & AC_SUPPCM_BITS_16) {
  4157. formats |= SNDRV_PCM_FMTBIT_S16_LE;
  4158. bps = 16;
  4159. }
  4160. if (wcaps & AC_WCAP_DIGITAL) {
  4161. if (val & AC_SUPPCM_BITS_32)
  4162. formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
  4163. if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
  4164. formats |= SNDRV_PCM_FMTBIT_S32_LE;
  4165. if (val & AC_SUPPCM_BITS_24)
  4166. bps = 24;
  4167. else if (val & AC_SUPPCM_BITS_20)
  4168. bps = 20;
  4169. } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
  4170. AC_SUPPCM_BITS_32)) {
  4171. formats |= SNDRV_PCM_FMTBIT_S32_LE;
  4172. if (val & AC_SUPPCM_BITS_32)
  4173. bps = 32;
  4174. else if (val & AC_SUPPCM_BITS_24)
  4175. bps = 24;
  4176. else if (val & AC_SUPPCM_BITS_20)
  4177. bps = 20;
  4178. }
  4179. }
  4180. #if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
  4181. if (streams & AC_SUPFMT_FLOAT32) {
  4182. formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
  4183. if (!bps)
  4184. bps = 32;
  4185. }
  4186. #endif
  4187. if (streams == AC_SUPFMT_AC3) {
  4188. /* should be exclusive */
  4189. /* temporary hack: we have still no proper support
  4190. * for the direct AC3 stream...
  4191. */
  4192. formats |= SNDRV_PCM_FMTBIT_U8;
  4193. bps = 8;
  4194. }
  4195. if (formats == 0) {
  4196. codec_err(codec,
  4197. "formats == 0 (nid=0x%x, val=0x%x, ovrd=%i, streams=0x%x)\n",
  4198. nid, val,
  4199. (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
  4200. streams);
  4201. return -EIO;
  4202. }
  4203. if (formatsp)
  4204. *formatsp = formats;
  4205. if (bpsp)
  4206. *bpsp = bps;
  4207. }
  4208. return 0;
  4209. }
  4210. EXPORT_SYMBOL_GPL(snd_hda_query_supported_pcm);
  4211. /**
  4212. * snd_hda_is_supported_format - Check the validity of the format
  4213. * @codec: HD-audio codec
  4214. * @nid: NID to check
  4215. * @format: the HD-audio format value to check
  4216. *
  4217. * Check whether the given node supports the format value.
  4218. *
  4219. * Returns 1 if supported, 0 if not.
  4220. */
  4221. int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
  4222. unsigned int format)
  4223. {
  4224. int i;
  4225. unsigned int val = 0, rate, stream;
  4226. val = query_pcm_param(codec, nid);
  4227. if (!val)
  4228. return 0;
  4229. rate = format & 0xff00;
  4230. for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
  4231. if (rate_bits[i].hda_fmt == rate) {
  4232. if (val & (1 << i))
  4233. break;
  4234. return 0;
  4235. }
  4236. if (i >= AC_PAR_PCM_RATE_BITS)
  4237. return 0;
  4238. stream = query_stream_param(codec, nid);
  4239. if (!stream)
  4240. return 0;
  4241. if (stream & AC_SUPFMT_PCM) {
  4242. switch (format & 0xf0) {
  4243. case 0x00:
  4244. if (!(val & AC_SUPPCM_BITS_8))
  4245. return 0;
  4246. break;
  4247. case 0x10:
  4248. if (!(val & AC_SUPPCM_BITS_16))
  4249. return 0;
  4250. break;
  4251. case 0x20:
  4252. if (!(val & AC_SUPPCM_BITS_20))
  4253. return 0;
  4254. break;
  4255. case 0x30:
  4256. if (!(val & AC_SUPPCM_BITS_24))
  4257. return 0;
  4258. break;
  4259. case 0x40:
  4260. if (!(val & AC_SUPPCM_BITS_32))
  4261. return 0;
  4262. break;
  4263. default:
  4264. return 0;
  4265. }
  4266. } else {
  4267. /* FIXME: check for float32 and AC3? */
  4268. }
  4269. return 1;
  4270. }
  4271. EXPORT_SYMBOL_GPL(snd_hda_is_supported_format);
  4272. /*
  4273. * PCM stuff
  4274. */
  4275. static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
  4276. struct hda_codec *codec,
  4277. struct snd_pcm_substream *substream)
  4278. {
  4279. return 0;
  4280. }
  4281. static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
  4282. struct hda_codec *codec,
  4283. unsigned int stream_tag,
  4284. unsigned int format,
  4285. struct snd_pcm_substream *substream)
  4286. {
  4287. snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
  4288. return 0;
  4289. }
  4290. static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
  4291. struct hda_codec *codec,
  4292. struct snd_pcm_substream *substream)
  4293. {
  4294. snd_hda_codec_cleanup_stream(codec, hinfo->nid);
  4295. return 0;
  4296. }
  4297. static int set_pcm_default_values(struct hda_codec *codec,
  4298. struct hda_pcm_stream *info)
  4299. {
  4300. int err;
  4301. /* query support PCM information from the given NID */
  4302. if (info->nid && (!info->rates || !info->formats)) {
  4303. err = snd_hda_query_supported_pcm(codec, info->nid,
  4304. info->rates ? NULL : &info->rates,
  4305. info->formats ? NULL : &info->formats,
  4306. info->maxbps ? NULL : &info->maxbps);
  4307. if (err < 0)
  4308. return err;
  4309. }
  4310. if (info->ops.open == NULL)
  4311. info->ops.open = hda_pcm_default_open_close;
  4312. if (info->ops.close == NULL)
  4313. info->ops.close = hda_pcm_default_open_close;
  4314. if (info->ops.prepare == NULL) {
  4315. if (snd_BUG_ON(!info->nid))
  4316. return -EINVAL;
  4317. info->ops.prepare = hda_pcm_default_prepare;
  4318. }
  4319. if (info->ops.cleanup == NULL) {
  4320. if (snd_BUG_ON(!info->nid))
  4321. return -EINVAL;
  4322. info->ops.cleanup = hda_pcm_default_cleanup;
  4323. }
  4324. return 0;
  4325. }
  4326. /*
  4327. * codec prepare/cleanup entries
  4328. */
  4329. /**
  4330. * snd_hda_codec_prepare - Prepare a stream
  4331. * @codec: the HDA codec
  4332. * @hinfo: PCM information
  4333. * @stream: stream tag to assign
  4334. * @format: format id to assign
  4335. * @substream: PCM substream to assign
  4336. *
  4337. * Calls the prepare callback set by the codec with the given arguments.
  4338. * Clean up the inactive streams when successful.
  4339. */
  4340. int snd_hda_codec_prepare(struct hda_codec *codec,
  4341. struct hda_pcm_stream *hinfo,
  4342. unsigned int stream,
  4343. unsigned int format,
  4344. struct snd_pcm_substream *substream)
  4345. {
  4346. int ret;
  4347. mutex_lock(&codec->bus->prepare_mutex);
  4348. ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
  4349. if (ret >= 0)
  4350. purify_inactive_streams(codec);
  4351. mutex_unlock(&codec->bus->prepare_mutex);
  4352. return ret;
  4353. }
  4354. EXPORT_SYMBOL_GPL(snd_hda_codec_prepare);
  4355. /**
  4356. * snd_hda_codec_cleanup - Prepare a stream
  4357. * @codec: the HDA codec
  4358. * @hinfo: PCM information
  4359. * @substream: PCM substream
  4360. *
  4361. * Calls the cleanup callback set by the codec with the given arguments.
  4362. */
  4363. void snd_hda_codec_cleanup(struct hda_codec *codec,
  4364. struct hda_pcm_stream *hinfo,
  4365. struct snd_pcm_substream *substream)
  4366. {
  4367. mutex_lock(&codec->bus->prepare_mutex);
  4368. hinfo->ops.cleanup(hinfo, codec, substream);
  4369. mutex_unlock(&codec->bus->prepare_mutex);
  4370. }
  4371. EXPORT_SYMBOL_GPL(snd_hda_codec_cleanup);
  4372. /* global */
  4373. const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
  4374. "Audio", "SPDIF", "HDMI", "Modem"
  4375. };
  4376. /*
  4377. * get the empty PCM device number to assign
  4378. */
  4379. static int get_empty_pcm_device(struct hda_bus *bus, unsigned int type)
  4380. {
  4381. /* audio device indices; not linear to keep compatibility */
  4382. /* assigned to static slots up to dev#10; if more needed, assign
  4383. * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y)
  4384. */
  4385. static int audio_idx[HDA_PCM_NTYPES][5] = {
  4386. [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
  4387. [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
  4388. [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
  4389. [HDA_PCM_TYPE_MODEM] = { 6, -1 },
  4390. };
  4391. int i;
  4392. if (type >= HDA_PCM_NTYPES) {
  4393. dev_err(bus->card->dev, "Invalid PCM type %d\n", type);
  4394. return -EINVAL;
  4395. }
  4396. for (i = 0; audio_idx[type][i] >= 0; i++) {
  4397. #ifndef CONFIG_SND_DYNAMIC_MINORS
  4398. if (audio_idx[type][i] >= 8)
  4399. break;
  4400. #endif
  4401. if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
  4402. return audio_idx[type][i];
  4403. }
  4404. #ifdef CONFIG_SND_DYNAMIC_MINORS
  4405. /* non-fixed slots starting from 10 */
  4406. for (i = 10; i < 32; i++) {
  4407. if (!test_and_set_bit(i, bus->pcm_dev_bits))
  4408. return i;
  4409. }
  4410. #endif
  4411. dev_warn(bus->card->dev, "Too many %s devices\n",
  4412. snd_hda_pcm_type_name[type]);
  4413. #ifndef CONFIG_SND_DYNAMIC_MINORS
  4414. dev_warn(bus->card->dev,
  4415. "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n");
  4416. #endif
  4417. return -EAGAIN;
  4418. }
  4419. /*
  4420. * attach a new PCM stream
  4421. */
  4422. static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
  4423. {
  4424. struct hda_bus *bus = codec->bus;
  4425. struct hda_pcm_stream *info;
  4426. int stream, err;
  4427. if (snd_BUG_ON(!pcm->name))
  4428. return -EINVAL;
  4429. for (stream = 0; stream < 2; stream++) {
  4430. info = &pcm->stream[stream];
  4431. if (info->substreams) {
  4432. err = set_pcm_default_values(codec, info);
  4433. if (err < 0)
  4434. return err;
  4435. }
  4436. }
  4437. return bus->ops.attach_pcm(bus, codec, pcm);
  4438. }
  4439. /* assign all PCMs of the given codec */
  4440. int snd_hda_codec_build_pcms(struct hda_codec *codec)
  4441. {
  4442. unsigned int pcm;
  4443. int err;
  4444. if (!codec->num_pcms) {
  4445. if (!codec->patch_ops.build_pcms)
  4446. return 0;
  4447. err = codec->patch_ops.build_pcms(codec);
  4448. if (err < 0) {
  4449. codec_err(codec,
  4450. "cannot build PCMs for #%d (error %d)\n",
  4451. codec->addr, err);
  4452. err = snd_hda_codec_reset(codec);
  4453. if (err < 0) {
  4454. codec_err(codec,
  4455. "cannot revert codec\n");
  4456. return err;
  4457. }
  4458. }
  4459. }
  4460. for (pcm = 0; pcm < codec->num_pcms; pcm++) {
  4461. struct hda_pcm *cpcm = &codec->pcm_info[pcm];
  4462. int dev;
  4463. if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
  4464. continue; /* no substreams assigned */
  4465. if (!cpcm->pcm) {
  4466. dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
  4467. if (dev < 0)
  4468. continue; /* no fatal error */
  4469. cpcm->device = dev;
  4470. err = snd_hda_attach_pcm(codec, cpcm);
  4471. if (err < 0) {
  4472. codec_err(codec,
  4473. "cannot attach PCM stream %d for codec #%d\n",
  4474. dev, codec->addr);
  4475. continue; /* no fatal error */
  4476. }
  4477. }
  4478. }
  4479. return 0;
  4480. }
  4481. /**
  4482. * snd_hda_build_pcms - build PCM information
  4483. * @bus: the BUS
  4484. *
  4485. * Create PCM information for each codec included in the bus.
  4486. *
  4487. * The build_pcms codec patch is requested to set up codec->num_pcms and
  4488. * codec->pcm_info properly. The array is referred by the top-level driver
  4489. * to create its PCM instances.
  4490. * The allocated codec->pcm_info should be released in codec->patch_ops.free
  4491. * callback.
  4492. *
  4493. * At least, substreams, channels_min and channels_max must be filled for
  4494. * each stream. substreams = 0 indicates that the stream doesn't exist.
  4495. * When rates and/or formats are zero, the supported values are queried
  4496. * from the given nid. The nid is used also by the default ops.prepare
  4497. * and ops.cleanup callbacks.
  4498. *
  4499. * The driver needs to call ops.open in its open callback. Similarly,
  4500. * ops.close is supposed to be called in the close callback.
  4501. * ops.prepare should be called in the prepare or hw_params callback
  4502. * with the proper parameters for set up.
  4503. * ops.cleanup should be called in hw_free for clean up of streams.
  4504. *
  4505. * This function returns 0 if successful, or a negative error code.
  4506. */
  4507. int snd_hda_build_pcms(struct hda_bus *bus)
  4508. {
  4509. struct hda_codec *codec;
  4510. list_for_each_entry(codec, &bus->codec_list, list) {
  4511. int err = snd_hda_codec_build_pcms(codec);
  4512. if (err < 0)
  4513. return err;
  4514. }
  4515. return 0;
  4516. }
  4517. EXPORT_SYMBOL_GPL(snd_hda_build_pcms);
  4518. /**
  4519. * snd_hda_add_new_ctls - create controls from the array
  4520. * @codec: the HDA codec
  4521. * @knew: the array of struct snd_kcontrol_new
  4522. *
  4523. * This helper function creates and add new controls in the given array.
  4524. * The array must be terminated with an empty entry as terminator.
  4525. *
  4526. * Returns 0 if successful, or a negative error code.
  4527. */
  4528. int snd_hda_add_new_ctls(struct hda_codec *codec,
  4529. const struct snd_kcontrol_new *knew)
  4530. {
  4531. int err;
  4532. for (; knew->name; knew++) {
  4533. struct snd_kcontrol *kctl;
  4534. int addr = 0, idx = 0;
  4535. if (knew->iface == -1) /* skip this codec private value */
  4536. continue;
  4537. for (;;) {
  4538. kctl = snd_ctl_new1(knew, codec);
  4539. if (!kctl)
  4540. return -ENOMEM;
  4541. if (addr > 0)
  4542. kctl->id.device = addr;
  4543. if (idx > 0)
  4544. kctl->id.index = idx;
  4545. err = snd_hda_ctl_add(codec, 0, kctl);
  4546. if (!err)
  4547. break;
  4548. /* try first with another device index corresponding to
  4549. * the codec addr; if it still fails (or it's the
  4550. * primary codec), then try another control index
  4551. */
  4552. if (!addr && codec->addr)
  4553. addr = codec->addr;
  4554. else if (!idx && !knew->index) {
  4555. idx = find_empty_mixer_ctl_idx(codec,
  4556. knew->name, 0);
  4557. if (idx <= 0)
  4558. return err;
  4559. } else
  4560. return err;
  4561. }
  4562. }
  4563. return 0;
  4564. }
  4565. EXPORT_SYMBOL_GPL(snd_hda_add_new_ctls);
  4566. #ifdef CONFIG_PM
  4567. static void hda_power_work(struct work_struct *work)
  4568. {
  4569. struct hda_codec *codec =
  4570. container_of(work, struct hda_codec, power_work.work);
  4571. struct hda_bus *bus = codec->bus;
  4572. unsigned int state;
  4573. spin_lock(&codec->power_lock);
  4574. if (codec->power_transition > 0) { /* during power-up sequence? */
  4575. spin_unlock(&codec->power_lock);
  4576. return;
  4577. }
  4578. if (!codec->power_on || codec->power_count) {
  4579. codec->power_transition = 0;
  4580. spin_unlock(&codec->power_lock);
  4581. return;
  4582. }
  4583. spin_unlock(&codec->power_lock);
  4584. state = hda_call_codec_suspend(codec, true);
  4585. if (!bus->power_keep_link_on && (state & AC_PWRST_CLK_STOP_OK))
  4586. hda_call_pm_notify(codec, false);
  4587. }
  4588. static void hda_keep_power_on(struct hda_codec *codec)
  4589. {
  4590. spin_lock(&codec->power_lock);
  4591. codec->power_count++;
  4592. codec->power_on = 1;
  4593. codec->power_jiffies = jiffies;
  4594. spin_unlock(&codec->power_lock);
  4595. hda_call_pm_notify(codec, true);
  4596. }
  4597. /* update the power on/off account with the current jiffies */
  4598. void snd_hda_update_power_acct(struct hda_codec *codec)
  4599. {
  4600. unsigned long delta = jiffies - codec->power_jiffies;
  4601. if (codec->power_on)
  4602. codec->power_on_acct += delta;
  4603. else
  4604. codec->power_off_acct += delta;
  4605. codec->power_jiffies += delta;
  4606. }
  4607. /* Transition to powered up, if wait_power_down then wait for a pending
  4608. * transition to D3 to complete. A pending D3 transition is indicated
  4609. * with power_transition == -1. */
  4610. /* call this with codec->power_lock held! */
  4611. static void __snd_hda_power_up(struct hda_codec *codec, bool wait_power_down)
  4612. {
  4613. /* Return if power_on or transitioning to power_on, unless currently
  4614. * powering down. */
  4615. if ((codec->power_on || codec->power_transition > 0) &&
  4616. !(wait_power_down && codec->power_transition < 0))
  4617. return;
  4618. spin_unlock(&codec->power_lock);
  4619. cancel_delayed_work_sync(&codec->power_work);
  4620. spin_lock(&codec->power_lock);
  4621. /* If the power down delayed work was cancelled above before starting,
  4622. * then there is no need to go through power up here.
  4623. */
  4624. if (codec->power_on) {
  4625. if (codec->power_transition < 0)
  4626. codec->power_transition = 0;
  4627. return;
  4628. }
  4629. trace_hda_power_up(codec);
  4630. snd_hda_update_power_acct(codec);
  4631. codec->power_on = 1;
  4632. codec->power_jiffies = jiffies;
  4633. codec->power_transition = 1; /* avoid reentrance */
  4634. spin_unlock(&codec->power_lock);
  4635. hda_call_codec_resume(codec);
  4636. spin_lock(&codec->power_lock);
  4637. codec->power_transition = 0;
  4638. }
  4639. #define power_save(codec) \
  4640. ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
  4641. /* Transition to powered down */
  4642. static void __snd_hda_power_down(struct hda_codec *codec)
  4643. {
  4644. if (!codec->power_on || codec->power_count || codec->power_transition)
  4645. return;
  4646. if (power_save(codec)) {
  4647. codec->power_transition = -1; /* avoid reentrance */
  4648. queue_delayed_work(codec->bus->workq, &codec->power_work,
  4649. msecs_to_jiffies(power_save(codec) * 1000));
  4650. }
  4651. }
  4652. /**
  4653. * snd_hda_power_save - Power-up/down/sync the codec
  4654. * @codec: HD-audio codec
  4655. * @delta: the counter delta to change
  4656. * @d3wait: sync for D3 transition complete
  4657. *
  4658. * Change the power-up counter via @delta, and power up or down the hardware
  4659. * appropriately. For the power-down, queue to the delayed action.
  4660. * Passing zero to @delta means to synchronize the power state.
  4661. */
  4662. void snd_hda_power_save(struct hda_codec *codec, int delta, bool d3wait)
  4663. {
  4664. spin_lock(&codec->power_lock);
  4665. codec->power_count += delta;
  4666. trace_hda_power_count(codec);
  4667. if (delta > 0)
  4668. __snd_hda_power_up(codec, d3wait);
  4669. else
  4670. __snd_hda_power_down(codec);
  4671. spin_unlock(&codec->power_lock);
  4672. }
  4673. EXPORT_SYMBOL_GPL(snd_hda_power_save);
  4674. /**
  4675. * snd_hda_check_amp_list_power - Check the amp list and update the power
  4676. * @codec: HD-audio codec
  4677. * @check: the object containing an AMP list and the status
  4678. * @nid: NID to check / update
  4679. *
  4680. * Check whether the given NID is in the amp list. If it's in the list,
  4681. * check the current AMP status, and update the the power-status according
  4682. * to the mute status.
  4683. *
  4684. * This function is supposed to be set or called from the check_power_status
  4685. * patch ops.
  4686. */
  4687. int snd_hda_check_amp_list_power(struct hda_codec *codec,
  4688. struct hda_loopback_check *check,
  4689. hda_nid_t nid)
  4690. {
  4691. const struct hda_amp_list *p;
  4692. int ch, v;
  4693. if (!check->amplist)
  4694. return 0;
  4695. for (p = check->amplist; p->nid; p++) {
  4696. if (p->nid == nid)
  4697. break;
  4698. }
  4699. if (!p->nid)
  4700. return 0; /* nothing changed */
  4701. for (p = check->amplist; p->nid; p++) {
  4702. for (ch = 0; ch < 2; ch++) {
  4703. v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
  4704. p->idx);
  4705. if (!(v & HDA_AMP_MUTE) && v > 0) {
  4706. if (!check->power_on) {
  4707. check->power_on = 1;
  4708. snd_hda_power_up(codec);
  4709. }
  4710. return 1;
  4711. }
  4712. }
  4713. }
  4714. if (check->power_on) {
  4715. check->power_on = 0;
  4716. snd_hda_power_down(codec);
  4717. }
  4718. return 0;
  4719. }
  4720. EXPORT_SYMBOL_GPL(snd_hda_check_amp_list_power);
  4721. #endif
  4722. /*
  4723. * Channel mode helper
  4724. */
  4725. /**
  4726. * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
  4727. * @codec: the HDA codec
  4728. * @uinfo: pointer to get/store the data
  4729. * @chmode: channel mode array
  4730. * @num_chmodes: channel mode array size
  4731. */
  4732. int snd_hda_ch_mode_info(struct hda_codec *codec,
  4733. struct snd_ctl_elem_info *uinfo,
  4734. const struct hda_channel_mode *chmode,
  4735. int num_chmodes)
  4736. {
  4737. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  4738. uinfo->count = 1;
  4739. uinfo->value.enumerated.items = num_chmodes;
  4740. if (uinfo->value.enumerated.item >= num_chmodes)
  4741. uinfo->value.enumerated.item = num_chmodes - 1;
  4742. sprintf(uinfo->value.enumerated.name, "%dch",
  4743. chmode[uinfo->value.enumerated.item].channels);
  4744. return 0;
  4745. }
  4746. EXPORT_SYMBOL_GPL(snd_hda_ch_mode_info);
  4747. /**
  4748. * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
  4749. * @codec: the HDA codec
  4750. * @ucontrol: pointer to get/store the data
  4751. * @chmode: channel mode array
  4752. * @num_chmodes: channel mode array size
  4753. * @max_channels: max number of channels
  4754. */
  4755. int snd_hda_ch_mode_get(struct hda_codec *codec,
  4756. struct snd_ctl_elem_value *ucontrol,
  4757. const struct hda_channel_mode *chmode,
  4758. int num_chmodes,
  4759. int max_channels)
  4760. {
  4761. int i;
  4762. for (i = 0; i < num_chmodes; i++) {
  4763. if (max_channels == chmode[i].channels) {
  4764. ucontrol->value.enumerated.item[0] = i;
  4765. break;
  4766. }
  4767. }
  4768. return 0;
  4769. }
  4770. EXPORT_SYMBOL_GPL(snd_hda_ch_mode_get);
  4771. /**
  4772. * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
  4773. * @codec: the HDA codec
  4774. * @ucontrol: pointer to get/store the data
  4775. * @chmode: channel mode array
  4776. * @num_chmodes: channel mode array size
  4777. * @max_channelsp: pointer to store the max channels
  4778. */
  4779. int snd_hda_ch_mode_put(struct hda_codec *codec,
  4780. struct snd_ctl_elem_value *ucontrol,
  4781. const struct hda_channel_mode *chmode,
  4782. int num_chmodes,
  4783. int *max_channelsp)
  4784. {
  4785. unsigned int mode;
  4786. mode = ucontrol->value.enumerated.item[0];
  4787. if (mode >= num_chmodes)
  4788. return -EINVAL;
  4789. if (*max_channelsp == chmode[mode].channels)
  4790. return 0;
  4791. /* change the current channel setting */
  4792. *max_channelsp = chmode[mode].channels;
  4793. if (chmode[mode].sequence)
  4794. snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
  4795. return 1;
  4796. }
  4797. EXPORT_SYMBOL_GPL(snd_hda_ch_mode_put);
  4798. /*
  4799. * input MUX helper
  4800. */
  4801. /**
  4802. * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
  4803. * @imux: imux helper object
  4804. * @uinfo: pointer to get/store the data
  4805. */
  4806. int snd_hda_input_mux_info(const struct hda_input_mux *imux,
  4807. struct snd_ctl_elem_info *uinfo)
  4808. {
  4809. unsigned int index;
  4810. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  4811. uinfo->count = 1;
  4812. uinfo->value.enumerated.items = imux->num_items;
  4813. if (!imux->num_items)
  4814. return 0;
  4815. index = uinfo->value.enumerated.item;
  4816. if (index >= imux->num_items)
  4817. index = imux->num_items - 1;
  4818. strcpy(uinfo->value.enumerated.name, imux->items[index].label);
  4819. return 0;
  4820. }
  4821. EXPORT_SYMBOL_GPL(snd_hda_input_mux_info);
  4822. /**
  4823. * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
  4824. * @codec: the HDA codec
  4825. * @imux: imux helper object
  4826. * @ucontrol: pointer to get/store the data
  4827. * @nid: input mux NID
  4828. * @cur_val: pointer to get/store the current imux value
  4829. */
  4830. int snd_hda_input_mux_put(struct hda_codec *codec,
  4831. const struct hda_input_mux *imux,
  4832. struct snd_ctl_elem_value *ucontrol,
  4833. hda_nid_t nid,
  4834. unsigned int *cur_val)
  4835. {
  4836. unsigned int idx;
  4837. if (!imux->num_items)
  4838. return 0;
  4839. idx = ucontrol->value.enumerated.item[0];
  4840. if (idx >= imux->num_items)
  4841. idx = imux->num_items - 1;
  4842. if (*cur_val == idx)
  4843. return 0;
  4844. snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
  4845. imux->items[idx].index);
  4846. *cur_val = idx;
  4847. return 1;
  4848. }
  4849. EXPORT_SYMBOL_GPL(snd_hda_input_mux_put);
  4850. /**
  4851. * snd_hda_enum_helper_info - Helper for simple enum ctls
  4852. * @kcontrol: ctl element
  4853. * @uinfo: pointer to get/store the data
  4854. * @num_items: number of enum items
  4855. * @texts: enum item string array
  4856. *
  4857. * process kcontrol info callback of a simple string enum array
  4858. * when @num_items is 0 or @texts is NULL, assume a boolean enum array
  4859. */
  4860. int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
  4861. struct snd_ctl_elem_info *uinfo,
  4862. int num_items, const char * const *texts)
  4863. {
  4864. static const char * const texts_default[] = {
  4865. "Disabled", "Enabled"
  4866. };
  4867. if (!texts || !num_items) {
  4868. num_items = 2;
  4869. texts = texts_default;
  4870. }
  4871. return snd_ctl_enum_info(uinfo, 1, num_items, texts);
  4872. }
  4873. EXPORT_SYMBOL_GPL(snd_hda_enum_helper_info);
  4874. /*
  4875. * Multi-channel / digital-out PCM helper functions
  4876. */
  4877. /* setup SPDIF output stream */
  4878. static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
  4879. unsigned int stream_tag, unsigned int format)
  4880. {
  4881. struct hda_spdif_out *spdif;
  4882. unsigned int curr_fmt;
  4883. bool reset;
  4884. spdif = snd_hda_spdif_out_of_nid(codec, nid);
  4885. curr_fmt = snd_hda_codec_read(codec, nid, 0,
  4886. AC_VERB_GET_STREAM_FORMAT, 0);
  4887. reset = codec->spdif_status_reset &&
  4888. (spdif->ctls & AC_DIG1_ENABLE) &&
  4889. curr_fmt != format;
  4890. /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
  4891. updated */
  4892. if (reset)
  4893. set_dig_out_convert(codec, nid,
  4894. spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
  4895. -1);
  4896. snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
  4897. if (codec->slave_dig_outs) {
  4898. const hda_nid_t *d;
  4899. for (d = codec->slave_dig_outs; *d; d++)
  4900. snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
  4901. format);
  4902. }
  4903. /* turn on again (if needed) */
  4904. if (reset)
  4905. set_dig_out_convert(codec, nid,
  4906. spdif->ctls & 0xff, -1);
  4907. }
  4908. static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
  4909. {
  4910. snd_hda_codec_cleanup_stream(codec, nid);
  4911. if (codec->slave_dig_outs) {
  4912. const hda_nid_t *d;
  4913. for (d = codec->slave_dig_outs; *d; d++)
  4914. snd_hda_codec_cleanup_stream(codec, *d);
  4915. }
  4916. }
  4917. /**
  4918. * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
  4919. * @bus: HD-audio bus
  4920. */
  4921. void snd_hda_bus_reboot_notify(struct hda_bus *bus)
  4922. {
  4923. struct hda_codec *codec;
  4924. if (!bus)
  4925. return;
  4926. list_for_each_entry(codec, &bus->codec_list, list) {
  4927. if (hda_codec_is_power_on(codec) &&
  4928. codec->patch_ops.reboot_notify)
  4929. codec->patch_ops.reboot_notify(codec);
  4930. }
  4931. }
  4932. EXPORT_SYMBOL_GPL(snd_hda_bus_reboot_notify);
  4933. /**
  4934. * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
  4935. * @codec: the HDA codec
  4936. * @mout: hda_multi_out object
  4937. */
  4938. int snd_hda_multi_out_dig_open(struct hda_codec *codec,
  4939. struct hda_multi_out *mout)
  4940. {
  4941. mutex_lock(&codec->spdif_mutex);
  4942. if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
  4943. /* already opened as analog dup; reset it once */
  4944. cleanup_dig_out_stream(codec, mout->dig_out_nid);
  4945. mout->dig_out_used = HDA_DIG_EXCLUSIVE;
  4946. mutex_unlock(&codec->spdif_mutex);
  4947. return 0;
  4948. }
  4949. EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_open);
  4950. /**
  4951. * snd_hda_multi_out_dig_prepare - prepare the digital out stream
  4952. * @codec: the HDA codec
  4953. * @mout: hda_multi_out object
  4954. * @stream_tag: stream tag to assign
  4955. * @format: format id to assign
  4956. * @substream: PCM substream to assign
  4957. */
  4958. int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
  4959. struct hda_multi_out *mout,
  4960. unsigned int stream_tag,
  4961. unsigned int format,
  4962. struct snd_pcm_substream *substream)
  4963. {
  4964. mutex_lock(&codec->spdif_mutex);
  4965. setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
  4966. mutex_unlock(&codec->spdif_mutex);
  4967. return 0;
  4968. }
  4969. EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_prepare);
  4970. /**
  4971. * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
  4972. * @codec: the HDA codec
  4973. * @mout: hda_multi_out object
  4974. */
  4975. int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
  4976. struct hda_multi_out *mout)
  4977. {
  4978. mutex_lock(&codec->spdif_mutex);
  4979. cleanup_dig_out_stream(codec, mout->dig_out_nid);
  4980. mutex_unlock(&codec->spdif_mutex);
  4981. return 0;
  4982. }
  4983. EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_cleanup);
  4984. /**
  4985. * snd_hda_multi_out_dig_close - release the digital out stream
  4986. * @codec: the HDA codec
  4987. * @mout: hda_multi_out object
  4988. */
  4989. int snd_hda_multi_out_dig_close(struct hda_codec *codec,
  4990. struct hda_multi_out *mout)
  4991. {
  4992. mutex_lock(&codec->spdif_mutex);
  4993. mout->dig_out_used = 0;
  4994. mutex_unlock(&codec->spdif_mutex);
  4995. return 0;
  4996. }
  4997. EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_close);
  4998. /**
  4999. * snd_hda_multi_out_analog_open - open analog outputs
  5000. * @codec: the HDA codec
  5001. * @mout: hda_multi_out object
  5002. * @substream: PCM substream to assign
  5003. * @hinfo: PCM information to assign
  5004. *
  5005. * Open analog outputs and set up the hw-constraints.
  5006. * If the digital outputs can be opened as slave, open the digital
  5007. * outputs, too.
  5008. */
  5009. int snd_hda_multi_out_analog_open(struct hda_codec *codec,
  5010. struct hda_multi_out *mout,
  5011. struct snd_pcm_substream *substream,
  5012. struct hda_pcm_stream *hinfo)
  5013. {
  5014. struct snd_pcm_runtime *runtime = substream->runtime;
  5015. runtime->hw.channels_max = mout->max_channels;
  5016. if (mout->dig_out_nid) {
  5017. if (!mout->analog_rates) {
  5018. mout->analog_rates = hinfo->rates;
  5019. mout->analog_formats = hinfo->formats;
  5020. mout->analog_maxbps = hinfo->maxbps;
  5021. } else {
  5022. runtime->hw.rates = mout->analog_rates;
  5023. runtime->hw.formats = mout->analog_formats;
  5024. hinfo->maxbps = mout->analog_maxbps;
  5025. }
  5026. if (!mout->spdif_rates) {
  5027. snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
  5028. &mout->spdif_rates,
  5029. &mout->spdif_formats,
  5030. &mout->spdif_maxbps);
  5031. }
  5032. mutex_lock(&codec->spdif_mutex);
  5033. if (mout->share_spdif) {
  5034. if ((runtime->hw.rates & mout->spdif_rates) &&
  5035. (runtime->hw.formats & mout->spdif_formats)) {
  5036. runtime->hw.rates &= mout->spdif_rates;
  5037. runtime->hw.formats &= mout->spdif_formats;
  5038. if (mout->spdif_maxbps < hinfo->maxbps)
  5039. hinfo->maxbps = mout->spdif_maxbps;
  5040. } else {
  5041. mout->share_spdif = 0;
  5042. /* FIXME: need notify? */
  5043. }
  5044. }
  5045. mutex_unlock(&codec->spdif_mutex);
  5046. }
  5047. return snd_pcm_hw_constraint_step(substream->runtime, 0,
  5048. SNDRV_PCM_HW_PARAM_CHANNELS, 2);
  5049. }
  5050. EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_open);
  5051. /**
  5052. * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
  5053. * @codec: the HDA codec
  5054. * @mout: hda_multi_out object
  5055. * @stream_tag: stream tag to assign
  5056. * @format: format id to assign
  5057. * @substream: PCM substream to assign
  5058. *
  5059. * Set up the i/o for analog out.
  5060. * When the digital out is available, copy the front out to digital out, too.
  5061. */
  5062. int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
  5063. struct hda_multi_out *mout,
  5064. unsigned int stream_tag,
  5065. unsigned int format,
  5066. struct snd_pcm_substream *substream)
  5067. {
  5068. const hda_nid_t *nids = mout->dac_nids;
  5069. int chs = substream->runtime->channels;
  5070. struct hda_spdif_out *spdif;
  5071. int i;
  5072. mutex_lock(&codec->spdif_mutex);
  5073. spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
  5074. if (mout->dig_out_nid && mout->share_spdif &&
  5075. mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
  5076. if (chs == 2 &&
  5077. snd_hda_is_supported_format(codec, mout->dig_out_nid,
  5078. format) &&
  5079. !(spdif->status & IEC958_AES0_NONAUDIO)) {
  5080. mout->dig_out_used = HDA_DIG_ANALOG_DUP;
  5081. setup_dig_out_stream(codec, mout->dig_out_nid,
  5082. stream_tag, format);
  5083. } else {
  5084. mout->dig_out_used = 0;
  5085. cleanup_dig_out_stream(codec, mout->dig_out_nid);
  5086. }
  5087. }
  5088. mutex_unlock(&codec->spdif_mutex);
  5089. /* front */
  5090. snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
  5091. 0, format);
  5092. if (!mout->no_share_stream &&
  5093. mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
  5094. /* headphone out will just decode front left/right (stereo) */
  5095. snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
  5096. 0, format);
  5097. /* extra outputs copied from front */
  5098. for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
  5099. if (!mout->no_share_stream && mout->hp_out_nid[i])
  5100. snd_hda_codec_setup_stream(codec,
  5101. mout->hp_out_nid[i],
  5102. stream_tag, 0, format);
  5103. /* surrounds */
  5104. for (i = 1; i < mout->num_dacs; i++) {
  5105. if (chs >= (i + 1) * 2) /* independent out */
  5106. snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
  5107. i * 2, format);
  5108. else if (!mout->no_share_stream) /* copy front */
  5109. snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
  5110. 0, format);
  5111. }
  5112. /* extra surrounds */
  5113. for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) {
  5114. int ch = 0;
  5115. if (!mout->extra_out_nid[i])
  5116. break;
  5117. if (chs >= (i + 1) * 2)
  5118. ch = i * 2;
  5119. else if (!mout->no_share_stream)
  5120. break;
  5121. snd_hda_codec_setup_stream(codec, mout->extra_out_nid[i],
  5122. stream_tag, ch, format);
  5123. }
  5124. return 0;
  5125. }
  5126. EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_prepare);
  5127. /**
  5128. * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
  5129. * @codec: the HDA codec
  5130. * @mout: hda_multi_out object
  5131. */
  5132. int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
  5133. struct hda_multi_out *mout)
  5134. {
  5135. const hda_nid_t *nids = mout->dac_nids;
  5136. int i;
  5137. for (i = 0; i < mout->num_dacs; i++)
  5138. snd_hda_codec_cleanup_stream(codec, nids[i]);
  5139. if (mout->hp_nid)
  5140. snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
  5141. for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
  5142. if (mout->hp_out_nid[i])
  5143. snd_hda_codec_cleanup_stream(codec,
  5144. mout->hp_out_nid[i]);
  5145. for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
  5146. if (mout->extra_out_nid[i])
  5147. snd_hda_codec_cleanup_stream(codec,
  5148. mout->extra_out_nid[i]);
  5149. mutex_lock(&codec->spdif_mutex);
  5150. if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
  5151. cleanup_dig_out_stream(codec, mout->dig_out_nid);
  5152. mout->dig_out_used = 0;
  5153. }
  5154. mutex_unlock(&codec->spdif_mutex);
  5155. return 0;
  5156. }
  5157. EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_cleanup);
  5158. /**
  5159. * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
  5160. * @codec: the HDA codec
  5161. * @pin: referred pin NID
  5162. *
  5163. * Guess the suitable VREF pin bits to be set as the pin-control value.
  5164. * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
  5165. */
  5166. unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
  5167. {
  5168. unsigned int pincap;
  5169. unsigned int oldval;
  5170. oldval = snd_hda_codec_read(codec, pin, 0,
  5171. AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
  5172. pincap = snd_hda_query_pin_caps(codec, pin);
  5173. pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
  5174. /* Exception: if the default pin setup is vref50, we give it priority */
  5175. if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
  5176. return AC_PINCTL_VREF_80;
  5177. else if (pincap & AC_PINCAP_VREF_50)
  5178. return AC_PINCTL_VREF_50;
  5179. else if (pincap & AC_PINCAP_VREF_100)
  5180. return AC_PINCTL_VREF_100;
  5181. else if (pincap & AC_PINCAP_VREF_GRD)
  5182. return AC_PINCTL_VREF_GRD;
  5183. return AC_PINCTL_VREF_HIZ;
  5184. }
  5185. EXPORT_SYMBOL_GPL(snd_hda_get_default_vref);
  5186. /**
  5187. * snd_hda_correct_pin_ctl - correct the pin ctl value for matching with the pin cap
  5188. * @codec: the HDA codec
  5189. * @pin: referred pin NID
  5190. * @val: pin ctl value to audit
  5191. */
  5192. unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
  5193. hda_nid_t pin, unsigned int val)
  5194. {
  5195. static unsigned int cap_lists[][2] = {
  5196. { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
  5197. { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
  5198. { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
  5199. { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
  5200. };
  5201. unsigned int cap;
  5202. if (!val)
  5203. return 0;
  5204. cap = snd_hda_query_pin_caps(codec, pin);
  5205. if (!cap)
  5206. return val; /* don't know what to do... */
  5207. if (val & AC_PINCTL_OUT_EN) {
  5208. if (!(cap & AC_PINCAP_OUT))
  5209. val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
  5210. else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
  5211. val &= ~AC_PINCTL_HP_EN;
  5212. }
  5213. if (val & AC_PINCTL_IN_EN) {
  5214. if (!(cap & AC_PINCAP_IN))
  5215. val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
  5216. else {
  5217. unsigned int vcap, vref;
  5218. int i;
  5219. vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
  5220. vref = val & AC_PINCTL_VREFEN;
  5221. for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
  5222. if (vref == cap_lists[i][0] &&
  5223. !(vcap & cap_lists[i][1])) {
  5224. if (i == ARRAY_SIZE(cap_lists) - 1)
  5225. vref = AC_PINCTL_VREF_HIZ;
  5226. else
  5227. vref = cap_lists[i + 1][0];
  5228. }
  5229. }
  5230. val &= ~AC_PINCTL_VREFEN;
  5231. val |= vref;
  5232. }
  5233. }
  5234. return val;
  5235. }
  5236. EXPORT_SYMBOL_GPL(snd_hda_correct_pin_ctl);
  5237. /**
  5238. * _snd_hda_pin_ctl - Helper to set pin ctl value
  5239. * @codec: the HDA codec
  5240. * @pin: referred pin NID
  5241. * @val: pin control value to set
  5242. * @cached: access over codec pinctl cache or direct write
  5243. *
  5244. * This function is a helper to set a pin ctl value more safely.
  5245. * It corrects the pin ctl value via snd_hda_correct_pin_ctl(), stores the
  5246. * value in pin target array via snd_hda_codec_set_pin_target(), then
  5247. * actually writes the value via either snd_hda_codec_update_cache() or
  5248. * snd_hda_codec_write() depending on @cached flag.
  5249. */
  5250. int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
  5251. unsigned int val, bool cached)
  5252. {
  5253. val = snd_hda_correct_pin_ctl(codec, pin, val);
  5254. snd_hda_codec_set_pin_target(codec, pin, val);
  5255. if (cached)
  5256. return snd_hda_codec_update_cache(codec, pin, 0,
  5257. AC_VERB_SET_PIN_WIDGET_CONTROL, val);
  5258. else
  5259. return snd_hda_codec_write(codec, pin, 0,
  5260. AC_VERB_SET_PIN_WIDGET_CONTROL, val);
  5261. }
  5262. EXPORT_SYMBOL_GPL(_snd_hda_set_pin_ctl);
  5263. /**
  5264. * snd_hda_add_imux_item - Add an item to input_mux
  5265. * @codec: the HDA codec
  5266. * @imux: imux helper object
  5267. * @label: the name of imux item to assign
  5268. * @index: index number of imux item to assign
  5269. * @type_idx: pointer to store the resultant label index
  5270. *
  5271. * When the same label is used already in the existing items, the number
  5272. * suffix is appended to the label. This label index number is stored
  5273. * to type_idx when non-NULL pointer is given.
  5274. */
  5275. int snd_hda_add_imux_item(struct hda_codec *codec,
  5276. struct hda_input_mux *imux, const char *label,
  5277. int index, int *type_idx)
  5278. {
  5279. int i, label_idx = 0;
  5280. if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
  5281. codec_err(codec, "hda_codec: Too many imux items!\n");
  5282. return -EINVAL;
  5283. }
  5284. for (i = 0; i < imux->num_items; i++) {
  5285. if (!strncmp(label, imux->items[i].label, strlen(label)))
  5286. label_idx++;
  5287. }
  5288. if (type_idx)
  5289. *type_idx = label_idx;
  5290. if (label_idx > 0)
  5291. snprintf(imux->items[imux->num_items].label,
  5292. sizeof(imux->items[imux->num_items].label),
  5293. "%s %d", label, label_idx);
  5294. else
  5295. strlcpy(imux->items[imux->num_items].label, label,
  5296. sizeof(imux->items[imux->num_items].label));
  5297. imux->items[imux->num_items].index = index;
  5298. imux->num_items++;
  5299. return 0;
  5300. }
  5301. EXPORT_SYMBOL_GPL(snd_hda_add_imux_item);
  5302. #ifdef CONFIG_PM
  5303. /*
  5304. * power management
  5305. */
  5306. static void hda_async_suspend(void *data, async_cookie_t cookie)
  5307. {
  5308. hda_call_codec_suspend(data, false);
  5309. }
  5310. static void hda_async_resume(void *data, async_cookie_t cookie)
  5311. {
  5312. hda_call_codec_resume(data);
  5313. }
  5314. /**
  5315. * snd_hda_suspend - suspend the codecs
  5316. * @bus: the HDA bus
  5317. *
  5318. * Returns 0 if successful.
  5319. */
  5320. int snd_hda_suspend(struct hda_bus *bus)
  5321. {
  5322. struct hda_codec *codec;
  5323. ASYNC_DOMAIN_EXCLUSIVE(domain);
  5324. list_for_each_entry(codec, &bus->codec_list, list) {
  5325. cancel_delayed_work_sync(&codec->jackpoll_work);
  5326. if (hda_codec_is_power_on(codec)) {
  5327. if (bus->num_codecs > 1)
  5328. async_schedule_domain(hda_async_suspend, codec,
  5329. &domain);
  5330. else
  5331. hda_call_codec_suspend(codec, false);
  5332. }
  5333. }
  5334. if (bus->num_codecs > 1)
  5335. async_synchronize_full_domain(&domain);
  5336. return 0;
  5337. }
  5338. EXPORT_SYMBOL_GPL(snd_hda_suspend);
  5339. /**
  5340. * snd_hda_resume - resume the codecs
  5341. * @bus: the HDA bus
  5342. *
  5343. * Returns 0 if successful.
  5344. */
  5345. int snd_hda_resume(struct hda_bus *bus)
  5346. {
  5347. struct hda_codec *codec;
  5348. ASYNC_DOMAIN_EXCLUSIVE(domain);
  5349. list_for_each_entry(codec, &bus->codec_list, list) {
  5350. if (bus->num_codecs > 1)
  5351. async_schedule_domain(hda_async_resume, codec, &domain);
  5352. else
  5353. hda_call_codec_resume(codec);
  5354. }
  5355. if (bus->num_codecs > 1)
  5356. async_synchronize_full_domain(&domain);
  5357. return 0;
  5358. }
  5359. EXPORT_SYMBOL_GPL(snd_hda_resume);
  5360. #endif /* CONFIG_PM */
  5361. /*
  5362. * generic arrays
  5363. */
  5364. /**
  5365. * snd_array_new - get a new element from the given array
  5366. * @array: the array object
  5367. *
  5368. * Get a new element from the given array. If it exceeds the
  5369. * pre-allocated array size, re-allocate the array.
  5370. *
  5371. * Returns NULL if allocation failed.
  5372. */
  5373. void *snd_array_new(struct snd_array *array)
  5374. {
  5375. if (snd_BUG_ON(!array->elem_size))
  5376. return NULL;
  5377. if (array->used >= array->alloced) {
  5378. int num = array->alloced + array->alloc_align;
  5379. int size = (num + 1) * array->elem_size;
  5380. void *nlist;
  5381. if (snd_BUG_ON(num >= 4096))
  5382. return NULL;
  5383. nlist = krealloc(array->list, size, GFP_KERNEL | __GFP_ZERO);
  5384. if (!nlist)
  5385. return NULL;
  5386. array->list = nlist;
  5387. array->alloced = num;
  5388. }
  5389. return snd_array_elem(array, array->used++);
  5390. }
  5391. EXPORT_SYMBOL_GPL(snd_array_new);
  5392. /**
  5393. * snd_array_free - free the given array elements
  5394. * @array: the array object
  5395. */
  5396. void snd_array_free(struct snd_array *array)
  5397. {
  5398. kfree(array->list);
  5399. array->used = 0;
  5400. array->alloced = 0;
  5401. array->list = NULL;
  5402. }
  5403. EXPORT_SYMBOL_GPL(snd_array_free);
  5404. /**
  5405. * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
  5406. * @pcm: PCM caps bits
  5407. * @buf: the string buffer to write
  5408. * @buflen: the max buffer length
  5409. *
  5410. * used by hda_proc.c and hda_eld.c
  5411. */
  5412. void snd_print_pcm_bits(int pcm, char *buf, int buflen)
  5413. {
  5414. static unsigned int bits[] = { 8, 16, 20, 24, 32 };
  5415. int i, j;
  5416. for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
  5417. if (pcm & (AC_SUPPCM_BITS_8 << i))
  5418. j += snprintf(buf + j, buflen - j, " %d", bits[i]);
  5419. buf[j] = '\0'; /* necessary when j == 0 */
  5420. }
  5421. EXPORT_SYMBOL_GPL(snd_print_pcm_bits);
  5422. MODULE_DESCRIPTION("HDA codec core");
  5423. MODULE_LICENSE("GPL");