hda_codec.c 151 KB

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