hda_codec.c 148 KB

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