hda_codec.c 143 KB

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