hda_codec.c 138 KB

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