hda_codec.c 148 KB

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