hda_codec.c 143 KB

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