hda_codec.c 141 KB

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