hda_codec.c 149 KB

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