hda_codec.c 148 KB

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