hda_codec.c 113 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172
  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/mutex.h>
  26. #include <linux/module.h>
  27. #include <linux/async.h>
  28. #include <linux/pm.h>
  29. #include <linux/pm_runtime.h>
  30. #include <sound/core.h>
  31. #include "hda_codec.h"
  32. #include <sound/asoundef.h>
  33. #include <sound/tlv.h>
  34. #include <sound/initval.h>
  35. #include <sound/jack.h>
  36. #include "hda_local.h"
  37. #include "hda_beep.h"
  38. #include "hda_jack.h"
  39. #include <sound/hda_hwdep.h>
  40. #ifdef CONFIG_PM
  41. #define codec_in_pm(codec) atomic_read(&(codec)->core.in_pm)
  42. #define hda_codec_is_power_on(codec) \
  43. (!pm_runtime_suspended(hda_codec_dev(codec)))
  44. #else
  45. #define codec_in_pm(codec) 0
  46. #define hda_codec_is_power_on(codec) 1
  47. #endif
  48. #define codec_has_epss(codec) \
  49. ((codec)->core.power_caps & AC_PWRST_EPSS)
  50. #define codec_has_clkstop(codec) \
  51. ((codec)->core.power_caps & AC_PWRST_CLKSTOP)
  52. /**
  53. * snd_hda_get_jack_location - Give a location string of the jack
  54. * @cfg: pin default config value
  55. *
  56. * Parse the pin default config value and returns the string of the
  57. * jack location, e.g. "Rear", "Front", etc.
  58. */
  59. const char *snd_hda_get_jack_location(u32 cfg)
  60. {
  61. static char *bases[7] = {
  62. "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
  63. };
  64. static unsigned char specials_idx[] = {
  65. 0x07, 0x08,
  66. 0x17, 0x18, 0x19,
  67. 0x37, 0x38
  68. };
  69. static char *specials[] = {
  70. "Rear Panel", "Drive Bar",
  71. "Riser", "HDMI", "ATAPI",
  72. "Mobile-In", "Mobile-Out"
  73. };
  74. int i;
  75. cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
  76. if ((cfg & 0x0f) < 7)
  77. return bases[cfg & 0x0f];
  78. for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
  79. if (cfg == specials_idx[i])
  80. return specials[i];
  81. }
  82. return "UNKNOWN";
  83. }
  84. EXPORT_SYMBOL_GPL(snd_hda_get_jack_location);
  85. /**
  86. * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
  87. * @cfg: pin default config value
  88. *
  89. * Parse the pin default config value and returns the string of the
  90. * jack connectivity, i.e. external or internal connection.
  91. */
  92. const char *snd_hda_get_jack_connectivity(u32 cfg)
  93. {
  94. static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
  95. return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
  96. }
  97. EXPORT_SYMBOL_GPL(snd_hda_get_jack_connectivity);
  98. /**
  99. * snd_hda_get_jack_type - Give a type string of the jack
  100. * @cfg: pin default config value
  101. *
  102. * Parse the pin default config value and returns the string of the
  103. * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
  104. */
  105. const char *snd_hda_get_jack_type(u32 cfg)
  106. {
  107. static char *jack_types[16] = {
  108. "Line Out", "Speaker", "HP Out", "CD",
  109. "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
  110. "Line In", "Aux", "Mic", "Telephony",
  111. "SPDIF In", "Digital In", "Reserved", "Other"
  112. };
  113. return jack_types[(cfg & AC_DEFCFG_DEVICE)
  114. >> AC_DEFCFG_DEVICE_SHIFT];
  115. }
  116. EXPORT_SYMBOL_GPL(snd_hda_get_jack_type);
  117. /*
  118. * Send and receive a verb - passed to exec_verb override for hdac_device
  119. */
  120. static int codec_exec_verb(struct hdac_device *dev, unsigned int cmd,
  121. unsigned int flags, unsigned int *res)
  122. {
  123. struct hda_codec *codec = container_of(dev, struct hda_codec, core);
  124. struct hda_bus *bus = codec->bus;
  125. int err;
  126. if (cmd == ~0)
  127. return -1;
  128. again:
  129. snd_hda_power_up_pm(codec);
  130. mutex_lock(&bus->core.cmd_mutex);
  131. if (flags & HDA_RW_NO_RESPONSE_FALLBACK)
  132. bus->no_response_fallback = 1;
  133. err = snd_hdac_bus_exec_verb_unlocked(&bus->core, codec->core.addr,
  134. cmd, res);
  135. bus->no_response_fallback = 0;
  136. mutex_unlock(&bus->core.cmd_mutex);
  137. snd_hda_power_down_pm(codec);
  138. if (!codec_in_pm(codec) && res && err == -EAGAIN) {
  139. if (bus->response_reset) {
  140. codec_dbg(codec,
  141. "resetting BUS due to fatal communication error\n");
  142. snd_hda_bus_reset(bus);
  143. }
  144. goto again;
  145. }
  146. /* clear reset-flag when the communication gets recovered */
  147. if (!err || codec_in_pm(codec))
  148. bus->response_reset = 0;
  149. return err;
  150. }
  151. /**
  152. * snd_hda_codec_read - send a command and get the response
  153. * @codec: the HDA codec
  154. * @nid: NID to send the command
  155. * @flags: optional bit flags
  156. * @verb: the verb to send
  157. * @parm: the parameter for the verb
  158. *
  159. * Send a single command and read the corresponding response.
  160. *
  161. * Returns the obtained response value, or -1 for an error.
  162. */
  163. unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
  164. int flags,
  165. unsigned int verb, unsigned int parm)
  166. {
  167. unsigned int cmd = snd_hdac_make_cmd(&codec->core, nid, verb, parm);
  168. unsigned int res;
  169. if (snd_hdac_exec_verb(&codec->core, cmd, flags, &res))
  170. return -1;
  171. return res;
  172. }
  173. EXPORT_SYMBOL_GPL(snd_hda_codec_read);
  174. /**
  175. * snd_hda_codec_write - send a single command without waiting for response
  176. * @codec: the HDA codec
  177. * @nid: NID to send the command
  178. * @flags: optional bit flags
  179. * @verb: the verb to send
  180. * @parm: the parameter for the verb
  181. *
  182. * Send a single command without waiting for response.
  183. *
  184. * Returns 0 if successful, or a negative error code.
  185. */
  186. int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int flags,
  187. unsigned int verb, unsigned int parm)
  188. {
  189. unsigned int cmd = snd_hdac_make_cmd(&codec->core, nid, verb, parm);
  190. return snd_hdac_exec_verb(&codec->core, cmd, flags, NULL);
  191. }
  192. EXPORT_SYMBOL_GPL(snd_hda_codec_write);
  193. /**
  194. * snd_hda_sequence_write - sequence writes
  195. * @codec: the HDA codec
  196. * @seq: VERB array to send
  197. *
  198. * Send the commands sequentially from the given array.
  199. * The array must be terminated with NID=0.
  200. */
  201. void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
  202. {
  203. for (; seq->nid; seq++)
  204. snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
  205. }
  206. EXPORT_SYMBOL_GPL(snd_hda_sequence_write);
  207. /* connection list element */
  208. struct hda_conn_list {
  209. struct list_head list;
  210. int len;
  211. hda_nid_t nid;
  212. hda_nid_t conns[0];
  213. };
  214. /* look up the cached results */
  215. static struct hda_conn_list *
  216. lookup_conn_list(struct hda_codec *codec, hda_nid_t nid)
  217. {
  218. struct hda_conn_list *p;
  219. list_for_each_entry(p, &codec->conn_list, list) {
  220. if (p->nid == nid)
  221. return p;
  222. }
  223. return NULL;
  224. }
  225. static int add_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
  226. const hda_nid_t *list)
  227. {
  228. struct hda_conn_list *p;
  229. p = kmalloc(sizeof(*p) + len * sizeof(hda_nid_t), GFP_KERNEL);
  230. if (!p)
  231. return -ENOMEM;
  232. p->len = len;
  233. p->nid = nid;
  234. memcpy(p->conns, list, len * sizeof(hda_nid_t));
  235. list_add(&p->list, &codec->conn_list);
  236. return 0;
  237. }
  238. static void remove_conn_list(struct hda_codec *codec)
  239. {
  240. while (!list_empty(&codec->conn_list)) {
  241. struct hda_conn_list *p;
  242. p = list_first_entry(&codec->conn_list, typeof(*p), list);
  243. list_del(&p->list);
  244. kfree(p);
  245. }
  246. }
  247. /* read the connection and add to the cache */
  248. static int read_and_add_raw_conns(struct hda_codec *codec, hda_nid_t nid)
  249. {
  250. hda_nid_t list[32];
  251. hda_nid_t *result = list;
  252. int len;
  253. len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list));
  254. if (len == -ENOSPC) {
  255. len = snd_hda_get_num_raw_conns(codec, nid);
  256. result = kmalloc(sizeof(hda_nid_t) * len, GFP_KERNEL);
  257. if (!result)
  258. return -ENOMEM;
  259. len = snd_hda_get_raw_connections(codec, nid, result, len);
  260. }
  261. if (len >= 0)
  262. len = snd_hda_override_conn_list(codec, nid, len, result);
  263. if (result != list)
  264. kfree(result);
  265. return len;
  266. }
  267. /**
  268. * snd_hda_get_conn_list - get connection list
  269. * @codec: the HDA codec
  270. * @nid: NID to parse
  271. * @listp: the pointer to store NID list
  272. *
  273. * Parses the connection list of the given widget and stores the pointer
  274. * to the list of NIDs.
  275. *
  276. * Returns the number of connections, or a negative error code.
  277. *
  278. * Note that the returned pointer isn't protected against the list
  279. * modification. If snd_hda_override_conn_list() might be called
  280. * concurrently, protect with a mutex appropriately.
  281. */
  282. int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
  283. const hda_nid_t **listp)
  284. {
  285. bool added = false;
  286. for (;;) {
  287. int err;
  288. const struct hda_conn_list *p;
  289. /* if the connection-list is already cached, read it */
  290. p = lookup_conn_list(codec, nid);
  291. if (p) {
  292. if (listp)
  293. *listp = p->conns;
  294. return p->len;
  295. }
  296. if (snd_BUG_ON(added))
  297. return -EINVAL;
  298. err = read_and_add_raw_conns(codec, nid);
  299. if (err < 0)
  300. return err;
  301. added = true;
  302. }
  303. }
  304. EXPORT_SYMBOL_GPL(snd_hda_get_conn_list);
  305. /**
  306. * snd_hda_get_connections - copy connection list
  307. * @codec: the HDA codec
  308. * @nid: NID to parse
  309. * @conn_list: connection list array; when NULL, checks only the size
  310. * @max_conns: max. number of connections to store
  311. *
  312. * Parses the connection list of the given widget and stores the list
  313. * of NIDs.
  314. *
  315. * Returns the number of connections, or a negative error code.
  316. */
  317. int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
  318. hda_nid_t *conn_list, int max_conns)
  319. {
  320. const hda_nid_t *list;
  321. int len = snd_hda_get_conn_list(codec, nid, &list);
  322. if (len > 0 && conn_list) {
  323. if (len > max_conns) {
  324. codec_err(codec, "Too many connections %d for NID 0x%x\n",
  325. len, nid);
  326. return -EINVAL;
  327. }
  328. memcpy(conn_list, list, len * sizeof(hda_nid_t));
  329. }
  330. return len;
  331. }
  332. EXPORT_SYMBOL_GPL(snd_hda_get_connections);
  333. /**
  334. * snd_hda_override_conn_list - add/modify the connection-list to cache
  335. * @codec: the HDA codec
  336. * @nid: NID to parse
  337. * @len: number of connection list entries
  338. * @list: the list of connection entries
  339. *
  340. * Add or modify the given connection-list to the cache. If the corresponding
  341. * cache already exists, invalidate it and append a new one.
  342. *
  343. * Returns zero or a negative error code.
  344. */
  345. int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
  346. const hda_nid_t *list)
  347. {
  348. struct hda_conn_list *p;
  349. p = lookup_conn_list(codec, nid);
  350. if (p) {
  351. list_del(&p->list);
  352. kfree(p);
  353. }
  354. return add_conn_list(codec, nid, len, list);
  355. }
  356. EXPORT_SYMBOL_GPL(snd_hda_override_conn_list);
  357. /**
  358. * snd_hda_get_conn_index - get the connection index of the given NID
  359. * @codec: the HDA codec
  360. * @mux: NID containing the list
  361. * @nid: NID to select
  362. * @recursive: 1 when searching NID recursively, otherwise 0
  363. *
  364. * Parses the connection list of the widget @mux and checks whether the
  365. * widget @nid is present. If it is, return the connection index.
  366. * Otherwise it returns -1.
  367. */
  368. int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
  369. hda_nid_t nid, int recursive)
  370. {
  371. const hda_nid_t *conn;
  372. int i, nums;
  373. nums = snd_hda_get_conn_list(codec, mux, &conn);
  374. for (i = 0; i < nums; i++)
  375. if (conn[i] == nid)
  376. return i;
  377. if (!recursive)
  378. return -1;
  379. if (recursive > 10) {
  380. codec_dbg(codec, "too deep connection for 0x%x\n", nid);
  381. return -1;
  382. }
  383. recursive++;
  384. for (i = 0; i < nums; i++) {
  385. unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
  386. if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
  387. continue;
  388. if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
  389. return i;
  390. }
  391. return -1;
  392. }
  393. EXPORT_SYMBOL_GPL(snd_hda_get_conn_index);
  394. /* return DEVLIST_LEN parameter of the given widget */
  395. static unsigned int get_num_devices(struct hda_codec *codec, hda_nid_t nid)
  396. {
  397. unsigned int wcaps = get_wcaps(codec, nid);
  398. unsigned int parm;
  399. if (!codec->dp_mst || !(wcaps & AC_WCAP_DIGITAL) ||
  400. get_wcaps_type(wcaps) != AC_WID_PIN)
  401. return 0;
  402. parm = snd_hdac_read_parm_uncached(&codec->core, nid, AC_PAR_DEVLIST_LEN);
  403. if (parm == -1)
  404. parm = 0;
  405. return parm & AC_DEV_LIST_LEN_MASK;
  406. }
  407. /**
  408. * snd_hda_get_devices - copy device list without cache
  409. * @codec: the HDA codec
  410. * @nid: NID of the pin to parse
  411. * @dev_list: device list array
  412. * @max_devices: max. number of devices to store
  413. *
  414. * Copy the device list. This info is dynamic and so not cached.
  415. * Currently called only from hda_proc.c, so not exported.
  416. */
  417. int snd_hda_get_devices(struct hda_codec *codec, hda_nid_t nid,
  418. u8 *dev_list, int max_devices)
  419. {
  420. unsigned int parm;
  421. int i, dev_len, devices;
  422. parm = get_num_devices(codec, nid);
  423. if (!parm) /* not multi-stream capable */
  424. return 0;
  425. dev_len = parm + 1;
  426. dev_len = dev_len < max_devices ? dev_len : max_devices;
  427. devices = 0;
  428. while (devices < dev_len) {
  429. if (snd_hdac_read(&codec->core, nid,
  430. AC_VERB_GET_DEVICE_LIST, devices, &parm))
  431. break; /* error */
  432. for (i = 0; i < 8; i++) {
  433. dev_list[devices] = (u8)parm;
  434. parm >>= 4;
  435. devices++;
  436. if (devices >= dev_len)
  437. break;
  438. }
  439. }
  440. return devices;
  441. }
  442. /*
  443. * read widget caps for each widget and store in cache
  444. */
  445. static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
  446. {
  447. int i;
  448. hda_nid_t nid;
  449. codec->wcaps = kmalloc(codec->core.num_nodes * 4, GFP_KERNEL);
  450. if (!codec->wcaps)
  451. return -ENOMEM;
  452. nid = codec->core.start_nid;
  453. for (i = 0; i < codec->core.num_nodes; i++, nid++)
  454. codec->wcaps[i] = snd_hdac_read_parm_uncached(&codec->core,
  455. nid, AC_PAR_AUDIO_WIDGET_CAP);
  456. return 0;
  457. }
  458. /* read all pin default configurations and save codec->init_pins */
  459. static int read_pin_defaults(struct hda_codec *codec)
  460. {
  461. hda_nid_t nid;
  462. for_each_hda_codec_node(nid, codec) {
  463. struct hda_pincfg *pin;
  464. unsigned int wcaps = get_wcaps(codec, nid);
  465. unsigned int wid_type = get_wcaps_type(wcaps);
  466. if (wid_type != AC_WID_PIN)
  467. continue;
  468. pin = snd_array_new(&codec->init_pins);
  469. if (!pin)
  470. return -ENOMEM;
  471. pin->nid = nid;
  472. pin->cfg = snd_hda_codec_read(codec, nid, 0,
  473. AC_VERB_GET_CONFIG_DEFAULT, 0);
  474. pin->ctrl = snd_hda_codec_read(codec, nid, 0,
  475. AC_VERB_GET_PIN_WIDGET_CONTROL,
  476. 0);
  477. }
  478. return 0;
  479. }
  480. /* look up the given pin config list and return the item matching with NID */
  481. static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
  482. struct snd_array *array,
  483. hda_nid_t nid)
  484. {
  485. int i;
  486. for (i = 0; i < array->used; i++) {
  487. struct hda_pincfg *pin = snd_array_elem(array, i);
  488. if (pin->nid == nid)
  489. return pin;
  490. }
  491. return NULL;
  492. }
  493. /* set the current pin config value for the given NID.
  494. * the value is cached, and read via snd_hda_codec_get_pincfg()
  495. */
  496. int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
  497. hda_nid_t nid, unsigned int cfg)
  498. {
  499. struct hda_pincfg *pin;
  500. /* the check below may be invalid when pins are added by a fixup
  501. * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled
  502. * for now
  503. */
  504. /*
  505. if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
  506. return -EINVAL;
  507. */
  508. pin = look_up_pincfg(codec, list, nid);
  509. if (!pin) {
  510. pin = snd_array_new(list);
  511. if (!pin)
  512. return -ENOMEM;
  513. pin->nid = nid;
  514. }
  515. pin->cfg = cfg;
  516. return 0;
  517. }
  518. /**
  519. * snd_hda_codec_set_pincfg - Override a pin default configuration
  520. * @codec: the HDA codec
  521. * @nid: NID to set the pin config
  522. * @cfg: the pin default config value
  523. *
  524. * Override a pin default configuration value in the cache.
  525. * This value can be read by snd_hda_codec_get_pincfg() in a higher
  526. * priority than the real hardware value.
  527. */
  528. int snd_hda_codec_set_pincfg(struct hda_codec *codec,
  529. hda_nid_t nid, unsigned int cfg)
  530. {
  531. return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
  532. }
  533. EXPORT_SYMBOL_GPL(snd_hda_codec_set_pincfg);
  534. /**
  535. * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
  536. * @codec: the HDA codec
  537. * @nid: NID to get the pin config
  538. *
  539. * Get the current pin config value of the given pin NID.
  540. * If the pincfg value is cached or overridden via sysfs or driver,
  541. * returns the cached value.
  542. */
  543. unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
  544. {
  545. struct hda_pincfg *pin;
  546. #ifdef CONFIG_SND_HDA_RECONFIG
  547. {
  548. unsigned int cfg = 0;
  549. mutex_lock(&codec->user_mutex);
  550. pin = look_up_pincfg(codec, &codec->user_pins, nid);
  551. if (pin)
  552. cfg = pin->cfg;
  553. mutex_unlock(&codec->user_mutex);
  554. if (cfg)
  555. return cfg;
  556. }
  557. #endif
  558. pin = look_up_pincfg(codec, &codec->driver_pins, nid);
  559. if (pin)
  560. return pin->cfg;
  561. pin = look_up_pincfg(codec, &codec->init_pins, nid);
  562. if (pin)
  563. return pin->cfg;
  564. return 0;
  565. }
  566. EXPORT_SYMBOL_GPL(snd_hda_codec_get_pincfg);
  567. /**
  568. * snd_hda_codec_set_pin_target - remember the current pinctl target value
  569. * @codec: the HDA codec
  570. * @nid: pin NID
  571. * @val: assigned pinctl value
  572. *
  573. * This function stores the given value to a pinctl target value in the
  574. * pincfg table. This isn't always as same as the actually written value
  575. * but can be referred at any time via snd_hda_codec_get_pin_target().
  576. */
  577. int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid,
  578. unsigned int val)
  579. {
  580. struct hda_pincfg *pin;
  581. pin = look_up_pincfg(codec, &codec->init_pins, nid);
  582. if (!pin)
  583. return -EINVAL;
  584. pin->target = val;
  585. return 0;
  586. }
  587. EXPORT_SYMBOL_GPL(snd_hda_codec_set_pin_target);
  588. /**
  589. * snd_hda_codec_get_pin_target - return the current pinctl target value
  590. * @codec: the HDA codec
  591. * @nid: pin NID
  592. */
  593. int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid)
  594. {
  595. struct hda_pincfg *pin;
  596. pin = look_up_pincfg(codec, &codec->init_pins, nid);
  597. if (!pin)
  598. return 0;
  599. return pin->target;
  600. }
  601. EXPORT_SYMBOL_GPL(snd_hda_codec_get_pin_target);
  602. /**
  603. * snd_hda_shutup_pins - Shut up all pins
  604. * @codec: the HDA codec
  605. *
  606. * Clear all pin controls to shup up before suspend for avoiding click noise.
  607. * The controls aren't cached so that they can be resumed properly.
  608. */
  609. void snd_hda_shutup_pins(struct hda_codec *codec)
  610. {
  611. int i;
  612. /* don't shut up pins when unloading the driver; otherwise it breaks
  613. * the default pin setup at the next load of the driver
  614. */
  615. if (codec->bus->shutdown)
  616. return;
  617. for (i = 0; i < codec->init_pins.used; i++) {
  618. struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
  619. /* use read here for syncing after issuing each verb */
  620. snd_hda_codec_read(codec, pin->nid, 0,
  621. AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
  622. }
  623. codec->pins_shutup = 1;
  624. }
  625. EXPORT_SYMBOL_GPL(snd_hda_shutup_pins);
  626. #ifdef CONFIG_PM
  627. /* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
  628. static void restore_shutup_pins(struct hda_codec *codec)
  629. {
  630. int i;
  631. if (!codec->pins_shutup)
  632. return;
  633. if (codec->bus->shutdown)
  634. return;
  635. for (i = 0; i < codec->init_pins.used; i++) {
  636. struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
  637. snd_hda_codec_write(codec, pin->nid, 0,
  638. AC_VERB_SET_PIN_WIDGET_CONTROL,
  639. pin->ctrl);
  640. }
  641. codec->pins_shutup = 0;
  642. }
  643. #endif
  644. static void hda_jackpoll_work(struct work_struct *work)
  645. {
  646. struct hda_codec *codec =
  647. container_of(work, struct hda_codec, jackpoll_work.work);
  648. snd_hda_jack_set_dirty_all(codec);
  649. snd_hda_jack_poll_all(codec);
  650. if (!codec->jackpoll_interval)
  651. return;
  652. schedule_delayed_work(&codec->jackpoll_work,
  653. codec->jackpoll_interval);
  654. }
  655. /* release all pincfg lists */
  656. static void free_init_pincfgs(struct hda_codec *codec)
  657. {
  658. snd_array_free(&codec->driver_pins);
  659. #ifdef CONFIG_SND_HDA_RECONFIG
  660. snd_array_free(&codec->user_pins);
  661. #endif
  662. snd_array_free(&codec->init_pins);
  663. }
  664. /*
  665. * audio-converter setup caches
  666. */
  667. struct hda_cvt_setup {
  668. hda_nid_t nid;
  669. u8 stream_tag;
  670. u8 channel_id;
  671. u16 format_id;
  672. unsigned char active; /* cvt is currently used */
  673. unsigned char dirty; /* setups should be cleared */
  674. };
  675. /* get or create a cache entry for the given audio converter NID */
  676. static struct hda_cvt_setup *
  677. get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
  678. {
  679. struct hda_cvt_setup *p;
  680. int i;
  681. for (i = 0; i < codec->cvt_setups.used; i++) {
  682. p = snd_array_elem(&codec->cvt_setups, i);
  683. if (p->nid == nid)
  684. return p;
  685. }
  686. p = snd_array_new(&codec->cvt_setups);
  687. if (p)
  688. p->nid = nid;
  689. return p;
  690. }
  691. /*
  692. * PCM device
  693. */
  694. static void release_pcm(struct kref *kref)
  695. {
  696. struct hda_pcm *pcm = container_of(kref, struct hda_pcm, kref);
  697. if (pcm->pcm)
  698. snd_device_free(pcm->codec->card, pcm->pcm);
  699. clear_bit(pcm->device, pcm->codec->bus->pcm_dev_bits);
  700. kfree(pcm->name);
  701. kfree(pcm);
  702. }
  703. void snd_hda_codec_pcm_put(struct hda_pcm *pcm)
  704. {
  705. kref_put(&pcm->kref, release_pcm);
  706. }
  707. EXPORT_SYMBOL_GPL(snd_hda_codec_pcm_put);
  708. struct hda_pcm *snd_hda_codec_pcm_new(struct hda_codec *codec,
  709. const char *fmt, ...)
  710. {
  711. struct hda_pcm *pcm;
  712. va_list args;
  713. pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
  714. if (!pcm)
  715. return NULL;
  716. pcm->codec = codec;
  717. kref_init(&pcm->kref);
  718. va_start(args, fmt);
  719. pcm->name = kvasprintf(GFP_KERNEL, fmt, args);
  720. va_end(args);
  721. if (!pcm->name) {
  722. kfree(pcm);
  723. return NULL;
  724. }
  725. list_add_tail(&pcm->list, &codec->pcm_list_head);
  726. return pcm;
  727. }
  728. EXPORT_SYMBOL_GPL(snd_hda_codec_pcm_new);
  729. /*
  730. * codec destructor
  731. */
  732. static void codec_release_pcms(struct hda_codec *codec)
  733. {
  734. struct hda_pcm *pcm, *n;
  735. list_for_each_entry_safe(pcm, n, &codec->pcm_list_head, list) {
  736. list_del_init(&pcm->list);
  737. if (pcm->pcm)
  738. snd_device_disconnect(codec->card, pcm->pcm);
  739. snd_hda_codec_pcm_put(pcm);
  740. }
  741. }
  742. void snd_hda_codec_cleanup_for_unbind(struct hda_codec *codec)
  743. {
  744. if (codec->registered) {
  745. /* pm_runtime_put() is called in snd_hdac_device_exit() */
  746. pm_runtime_get_noresume(hda_codec_dev(codec));
  747. pm_runtime_disable(hda_codec_dev(codec));
  748. codec->registered = 0;
  749. }
  750. cancel_delayed_work_sync(&codec->jackpoll_work);
  751. if (!codec->in_freeing)
  752. snd_hda_ctls_clear(codec);
  753. codec_release_pcms(codec);
  754. snd_hda_detach_beep_device(codec);
  755. memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
  756. snd_hda_jack_tbl_clear(codec);
  757. codec->proc_widget_hook = NULL;
  758. codec->spec = NULL;
  759. /* free only driver_pins so that init_pins + user_pins are restored */
  760. snd_array_free(&codec->driver_pins);
  761. snd_array_free(&codec->cvt_setups);
  762. snd_array_free(&codec->spdif_out);
  763. snd_array_free(&codec->verbs);
  764. codec->preset = NULL;
  765. codec->slave_dig_outs = NULL;
  766. codec->spdif_status_reset = 0;
  767. snd_array_free(&codec->mixers);
  768. snd_array_free(&codec->nids);
  769. remove_conn_list(codec);
  770. snd_hdac_regmap_exit(&codec->core);
  771. }
  772. static unsigned int hda_set_power_state(struct hda_codec *codec,
  773. unsigned int power_state);
  774. /* also called from hda_bind.c */
  775. void snd_hda_codec_register(struct hda_codec *codec)
  776. {
  777. if (codec->registered)
  778. return;
  779. if (device_is_registered(hda_codec_dev(codec))) {
  780. snd_hda_register_beep_device(codec);
  781. snd_hdac_link_power(&codec->core, true);
  782. pm_runtime_enable(hda_codec_dev(codec));
  783. /* it was powered up in snd_hda_codec_new(), now all done */
  784. snd_hda_power_down(codec);
  785. codec->registered = 1;
  786. }
  787. }
  788. static int snd_hda_codec_dev_register(struct snd_device *device)
  789. {
  790. snd_hda_codec_register(device->device_data);
  791. return 0;
  792. }
  793. static int snd_hda_codec_dev_disconnect(struct snd_device *device)
  794. {
  795. struct hda_codec *codec = device->device_data;
  796. snd_hda_detach_beep_device(codec);
  797. return 0;
  798. }
  799. static int snd_hda_codec_dev_free(struct snd_device *device)
  800. {
  801. struct hda_codec *codec = device->device_data;
  802. codec->in_freeing = 1;
  803. snd_hdac_device_unregister(&codec->core);
  804. snd_hdac_link_power(&codec->core, false);
  805. put_device(hda_codec_dev(codec));
  806. return 0;
  807. }
  808. static void snd_hda_codec_dev_release(struct device *dev)
  809. {
  810. struct hda_codec *codec = dev_to_hda_codec(dev);
  811. free_init_pincfgs(codec);
  812. snd_hdac_device_exit(&codec->core);
  813. snd_hda_sysfs_clear(codec);
  814. kfree(codec->modelname);
  815. kfree(codec->wcaps);
  816. kfree(codec);
  817. }
  818. /**
  819. * snd_hda_codec_new - create a HDA codec
  820. * @bus: the bus to assign
  821. * @codec_addr: the codec address
  822. * @codecp: the pointer to store the generated codec
  823. *
  824. * Returns 0 if successful, or a negative error code.
  825. */
  826. int snd_hda_codec_new(struct hda_bus *bus, struct snd_card *card,
  827. unsigned int codec_addr, struct hda_codec **codecp)
  828. {
  829. struct hda_codec *codec;
  830. char component[31];
  831. hda_nid_t fg;
  832. int err;
  833. static struct snd_device_ops dev_ops = {
  834. .dev_register = snd_hda_codec_dev_register,
  835. .dev_disconnect = snd_hda_codec_dev_disconnect,
  836. .dev_free = snd_hda_codec_dev_free,
  837. };
  838. if (snd_BUG_ON(!bus))
  839. return -EINVAL;
  840. if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
  841. return -EINVAL;
  842. codec = kzalloc(sizeof(*codec), GFP_KERNEL);
  843. if (!codec)
  844. return -ENOMEM;
  845. sprintf(component, "hdaudioC%dD%d", card->number, codec_addr);
  846. err = snd_hdac_device_init(&codec->core, &bus->core, component,
  847. codec_addr);
  848. if (err < 0) {
  849. kfree(codec);
  850. return err;
  851. }
  852. codec->core.dev.release = snd_hda_codec_dev_release;
  853. codec->core.type = HDA_DEV_LEGACY;
  854. codec->core.exec_verb = codec_exec_verb;
  855. codec->bus = bus;
  856. codec->card = card;
  857. codec->addr = codec_addr;
  858. mutex_init(&codec->spdif_mutex);
  859. mutex_init(&codec->control_mutex);
  860. snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
  861. snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
  862. snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
  863. snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
  864. snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
  865. snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
  866. snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16);
  867. snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8);
  868. INIT_LIST_HEAD(&codec->conn_list);
  869. INIT_LIST_HEAD(&codec->pcm_list_head);
  870. INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
  871. codec->depop_delay = -1;
  872. codec->fixup_id = HDA_FIXUP_ID_NOT_SET;
  873. #ifdef CONFIG_PM
  874. codec->power_jiffies = jiffies;
  875. #endif
  876. snd_hda_sysfs_init(codec);
  877. if (codec->bus->modelname) {
  878. codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
  879. if (!codec->modelname) {
  880. err = -ENODEV;
  881. goto error;
  882. }
  883. }
  884. fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
  885. err = read_widget_caps(codec, fg);
  886. if (err < 0)
  887. goto error;
  888. err = read_pin_defaults(codec);
  889. if (err < 0)
  890. goto error;
  891. /* power-up all before initialization */
  892. hda_set_power_state(codec, AC_PWRST_D0);
  893. snd_hda_codec_proc_new(codec);
  894. snd_hda_create_hwdep(codec);
  895. sprintf(component, "HDA:%08x,%08x,%08x", codec->core.vendor_id,
  896. codec->core.subsystem_id, codec->core.revision_id);
  897. snd_component_add(card, component);
  898. err = snd_device_new(card, SNDRV_DEV_CODEC, codec, &dev_ops);
  899. if (err < 0)
  900. goto error;
  901. if (codecp)
  902. *codecp = codec;
  903. return 0;
  904. error:
  905. put_device(hda_codec_dev(codec));
  906. return err;
  907. }
  908. EXPORT_SYMBOL_GPL(snd_hda_codec_new);
  909. /**
  910. * snd_hda_codec_update_widgets - Refresh widget caps and pin defaults
  911. * @codec: the HDA codec
  912. *
  913. * Forcibly refresh the all widget caps and the init pin configurations of
  914. * the given codec.
  915. */
  916. int snd_hda_codec_update_widgets(struct hda_codec *codec)
  917. {
  918. hda_nid_t fg;
  919. int err;
  920. err = snd_hdac_refresh_widgets(&codec->core);
  921. if (err < 0)
  922. return err;
  923. /* Assume the function group node does not change,
  924. * only the widget nodes may change.
  925. */
  926. kfree(codec->wcaps);
  927. fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
  928. err = read_widget_caps(codec, fg);
  929. if (err < 0)
  930. return err;
  931. snd_array_free(&codec->init_pins);
  932. err = read_pin_defaults(codec);
  933. return err;
  934. }
  935. EXPORT_SYMBOL_GPL(snd_hda_codec_update_widgets);
  936. /* update the stream-id if changed */
  937. static void update_pcm_stream_id(struct hda_codec *codec,
  938. struct hda_cvt_setup *p, hda_nid_t nid,
  939. u32 stream_tag, int channel_id)
  940. {
  941. unsigned int oldval, newval;
  942. if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
  943. oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
  944. newval = (stream_tag << 4) | channel_id;
  945. if (oldval != newval)
  946. snd_hda_codec_write(codec, nid, 0,
  947. AC_VERB_SET_CHANNEL_STREAMID,
  948. newval);
  949. p->stream_tag = stream_tag;
  950. p->channel_id = channel_id;
  951. }
  952. }
  953. /* update the format-id if changed */
  954. static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
  955. hda_nid_t nid, int format)
  956. {
  957. unsigned int oldval;
  958. if (p->format_id != format) {
  959. oldval = snd_hda_codec_read(codec, nid, 0,
  960. AC_VERB_GET_STREAM_FORMAT, 0);
  961. if (oldval != format) {
  962. msleep(1);
  963. snd_hda_codec_write(codec, nid, 0,
  964. AC_VERB_SET_STREAM_FORMAT,
  965. format);
  966. }
  967. p->format_id = format;
  968. }
  969. }
  970. /**
  971. * snd_hda_codec_setup_stream - set up the codec for streaming
  972. * @codec: the CODEC to set up
  973. * @nid: the NID to set up
  974. * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
  975. * @channel_id: channel id to pass, zero based.
  976. * @format: stream format.
  977. */
  978. void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
  979. u32 stream_tag,
  980. int channel_id, int format)
  981. {
  982. struct hda_codec *c;
  983. struct hda_cvt_setup *p;
  984. int type;
  985. int i;
  986. if (!nid)
  987. return;
  988. codec_dbg(codec,
  989. "hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
  990. nid, stream_tag, channel_id, format);
  991. p = get_hda_cvt_setup(codec, nid);
  992. if (!p)
  993. return;
  994. if (codec->patch_ops.stream_pm)
  995. codec->patch_ops.stream_pm(codec, nid, true);
  996. if (codec->pcm_format_first)
  997. update_pcm_format(codec, p, nid, format);
  998. update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
  999. if (!codec->pcm_format_first)
  1000. update_pcm_format(codec, p, nid, format);
  1001. p->active = 1;
  1002. p->dirty = 0;
  1003. /* make other inactive cvts with the same stream-tag dirty */
  1004. type = get_wcaps_type(get_wcaps(codec, nid));
  1005. list_for_each_codec(c, codec->bus) {
  1006. for (i = 0; i < c->cvt_setups.used; i++) {
  1007. p = snd_array_elem(&c->cvt_setups, i);
  1008. if (!p->active && p->stream_tag == stream_tag &&
  1009. get_wcaps_type(get_wcaps(c, p->nid)) == type)
  1010. p->dirty = 1;
  1011. }
  1012. }
  1013. }
  1014. EXPORT_SYMBOL_GPL(snd_hda_codec_setup_stream);
  1015. static void really_cleanup_stream(struct hda_codec *codec,
  1016. struct hda_cvt_setup *q);
  1017. /**
  1018. * __snd_hda_codec_cleanup_stream - clean up the codec for closing
  1019. * @codec: the CODEC to clean up
  1020. * @nid: the NID to clean up
  1021. * @do_now: really clean up the stream instead of clearing the active flag
  1022. */
  1023. void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
  1024. int do_now)
  1025. {
  1026. struct hda_cvt_setup *p;
  1027. if (!nid)
  1028. return;
  1029. if (codec->no_sticky_stream)
  1030. do_now = 1;
  1031. codec_dbg(codec, "hda_codec_cleanup_stream: NID=0x%x\n", nid);
  1032. p = get_hda_cvt_setup(codec, nid);
  1033. if (p) {
  1034. /* here we just clear the active flag when do_now isn't set;
  1035. * actual clean-ups will be done later in
  1036. * purify_inactive_streams() called from snd_hda_codec_prpapre()
  1037. */
  1038. if (do_now)
  1039. really_cleanup_stream(codec, p);
  1040. else
  1041. p->active = 0;
  1042. }
  1043. }
  1044. EXPORT_SYMBOL_GPL(__snd_hda_codec_cleanup_stream);
  1045. static void really_cleanup_stream(struct hda_codec *codec,
  1046. struct hda_cvt_setup *q)
  1047. {
  1048. hda_nid_t nid = q->nid;
  1049. if (q->stream_tag || q->channel_id)
  1050. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
  1051. if (q->format_id)
  1052. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
  1053. );
  1054. memset(q, 0, sizeof(*q));
  1055. q->nid = nid;
  1056. if (codec->patch_ops.stream_pm)
  1057. codec->patch_ops.stream_pm(codec, nid, false);
  1058. }
  1059. /* clean up the all conflicting obsolete streams */
  1060. static void purify_inactive_streams(struct hda_codec *codec)
  1061. {
  1062. struct hda_codec *c;
  1063. int i;
  1064. list_for_each_codec(c, codec->bus) {
  1065. for (i = 0; i < c->cvt_setups.used; i++) {
  1066. struct hda_cvt_setup *p;
  1067. p = snd_array_elem(&c->cvt_setups, i);
  1068. if (p->dirty)
  1069. really_cleanup_stream(c, p);
  1070. }
  1071. }
  1072. }
  1073. #ifdef CONFIG_PM
  1074. /* clean up all streams; called from suspend */
  1075. static void hda_cleanup_all_streams(struct hda_codec *codec)
  1076. {
  1077. int i;
  1078. for (i = 0; i < codec->cvt_setups.used; i++) {
  1079. struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
  1080. if (p->stream_tag)
  1081. really_cleanup_stream(codec, p);
  1082. }
  1083. }
  1084. #endif
  1085. /*
  1086. * amp access functions
  1087. */
  1088. /**
  1089. * query_amp_caps - query AMP capabilities
  1090. * @codec: the HD-auio codec
  1091. * @nid: the NID to query
  1092. * @direction: either #HDA_INPUT or #HDA_OUTPUT
  1093. *
  1094. * Query AMP capabilities for the given widget and direction.
  1095. * Returns the obtained capability bits.
  1096. *
  1097. * When cap bits have been already read, this doesn't read again but
  1098. * returns the cached value.
  1099. */
  1100. u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
  1101. {
  1102. if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
  1103. nid = codec->core.afg;
  1104. return snd_hda_param_read(codec, nid,
  1105. direction == HDA_OUTPUT ?
  1106. AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
  1107. }
  1108. EXPORT_SYMBOL_GPL(query_amp_caps);
  1109. /**
  1110. * snd_hda_check_amp_caps - query AMP capabilities
  1111. * @codec: the HD-audio codec
  1112. * @nid: the NID to query
  1113. * @dir: either #HDA_INPUT or #HDA_OUTPUT
  1114. * @bits: bit mask to check the result
  1115. *
  1116. * Check whether the widget has the given amp capability for the direction.
  1117. */
  1118. bool snd_hda_check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
  1119. int dir, unsigned int bits)
  1120. {
  1121. if (!nid)
  1122. return false;
  1123. if (get_wcaps(codec, nid) & (1 << (dir + 1)))
  1124. if (query_amp_caps(codec, nid, dir) & bits)
  1125. return true;
  1126. return false;
  1127. }
  1128. EXPORT_SYMBOL_GPL(snd_hda_check_amp_caps);
  1129. /**
  1130. * snd_hda_override_amp_caps - Override the AMP capabilities
  1131. * @codec: the CODEC to clean up
  1132. * @nid: the NID to clean up
  1133. * @dir: either #HDA_INPUT or #HDA_OUTPUT
  1134. * @caps: the capability bits to set
  1135. *
  1136. * Override the cached AMP caps bits value by the given one.
  1137. * This function is useful if the driver needs to adjust the AMP ranges,
  1138. * e.g. limit to 0dB, etc.
  1139. *
  1140. * Returns zero if successful or a negative error code.
  1141. */
  1142. int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
  1143. unsigned int caps)
  1144. {
  1145. unsigned int parm;
  1146. snd_hda_override_wcaps(codec, nid,
  1147. get_wcaps(codec, nid) | AC_WCAP_AMP_OVRD);
  1148. parm = dir == HDA_OUTPUT ? AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP;
  1149. return snd_hdac_override_parm(&codec->core, nid, parm, caps);
  1150. }
  1151. EXPORT_SYMBOL_GPL(snd_hda_override_amp_caps);
  1152. /**
  1153. * snd_hda_codec_amp_update - update the AMP mono value
  1154. * @codec: HD-audio codec
  1155. * @nid: NID to read the AMP value
  1156. * @ch: channel to update (0 or 1)
  1157. * @dir: #HDA_INPUT or #HDA_OUTPUT
  1158. * @idx: the index value (only for input direction)
  1159. * @mask: bit mask to set
  1160. * @val: the bits value to set
  1161. *
  1162. * Update the AMP values for the given channel, direction and index.
  1163. */
  1164. int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid,
  1165. int ch, int dir, int idx, int mask, int val)
  1166. {
  1167. unsigned int cmd = snd_hdac_regmap_encode_amp(nid, ch, dir, idx);
  1168. /* enable fake mute if no h/w mute but min=mute */
  1169. if ((query_amp_caps(codec, nid, dir) &
  1170. (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) == AC_AMPCAP_MIN_MUTE)
  1171. cmd |= AC_AMP_FAKE_MUTE;
  1172. return snd_hdac_regmap_update_raw(&codec->core, cmd, mask, val);
  1173. }
  1174. EXPORT_SYMBOL_GPL(snd_hda_codec_amp_update);
  1175. /**
  1176. * snd_hda_codec_amp_stereo - update the AMP stereo values
  1177. * @codec: HD-audio codec
  1178. * @nid: NID to read the AMP value
  1179. * @direction: #HDA_INPUT or #HDA_OUTPUT
  1180. * @idx: the index value (only for input direction)
  1181. * @mask: bit mask to set
  1182. * @val: the bits value to set
  1183. *
  1184. * Update the AMP values like snd_hda_codec_amp_update(), but for a
  1185. * stereo widget with the same mask and value.
  1186. */
  1187. int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
  1188. int direction, int idx, int mask, int val)
  1189. {
  1190. int ch, ret = 0;
  1191. if (snd_BUG_ON(mask & ~0xff))
  1192. mask &= 0xff;
  1193. for (ch = 0; ch < 2; ch++)
  1194. ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
  1195. idx, mask, val);
  1196. return ret;
  1197. }
  1198. EXPORT_SYMBOL_GPL(snd_hda_codec_amp_stereo);
  1199. /**
  1200. * snd_hda_codec_amp_init - initialize the AMP value
  1201. * @codec: the HDA codec
  1202. * @nid: NID to read the AMP value
  1203. * @ch: channel (left=0 or right=1)
  1204. * @dir: #HDA_INPUT or #HDA_OUTPUT
  1205. * @idx: the index value (only for input direction)
  1206. * @mask: bit mask to set
  1207. * @val: the bits value to set
  1208. *
  1209. * Works like snd_hda_codec_amp_update() but it writes the value only at
  1210. * the first access. If the amp was already initialized / updated beforehand,
  1211. * this does nothing.
  1212. */
  1213. int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
  1214. int dir, int idx, int mask, int val)
  1215. {
  1216. int orig;
  1217. if (!codec->core.regmap)
  1218. return -EINVAL;
  1219. regcache_cache_only(codec->core.regmap, true);
  1220. orig = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
  1221. regcache_cache_only(codec->core.regmap, false);
  1222. if (orig >= 0)
  1223. return 0;
  1224. return snd_hda_codec_amp_update(codec, nid, ch, dir, idx, mask, val);
  1225. }
  1226. EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init);
  1227. /**
  1228. * snd_hda_codec_amp_init_stereo - initialize the stereo AMP value
  1229. * @codec: the HDA codec
  1230. * @nid: NID to read the AMP value
  1231. * @dir: #HDA_INPUT or #HDA_OUTPUT
  1232. * @idx: the index value (only for input direction)
  1233. * @mask: bit mask to set
  1234. * @val: the bits value to set
  1235. *
  1236. * Call snd_hda_codec_amp_init() for both stereo channels.
  1237. */
  1238. int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
  1239. int dir, int idx, int mask, int val)
  1240. {
  1241. int ch, ret = 0;
  1242. if (snd_BUG_ON(mask & ~0xff))
  1243. mask &= 0xff;
  1244. for (ch = 0; ch < 2; ch++)
  1245. ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
  1246. idx, mask, val);
  1247. return ret;
  1248. }
  1249. EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init_stereo);
  1250. static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
  1251. unsigned int ofs)
  1252. {
  1253. u32 caps = query_amp_caps(codec, nid, dir);
  1254. /* get num steps */
  1255. caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
  1256. if (ofs < caps)
  1257. caps -= ofs;
  1258. return caps;
  1259. }
  1260. /**
  1261. * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
  1262. * @kcontrol: referred ctl element
  1263. * @uinfo: pointer to get/store the data
  1264. *
  1265. * The control element is supposed to have the private_value field
  1266. * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
  1267. */
  1268. int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
  1269. struct snd_ctl_elem_info *uinfo)
  1270. {
  1271. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1272. u16 nid = get_amp_nid(kcontrol);
  1273. u8 chs = get_amp_channels(kcontrol);
  1274. int dir = get_amp_direction(kcontrol);
  1275. unsigned int ofs = get_amp_offset(kcontrol);
  1276. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1277. uinfo->count = chs == 3 ? 2 : 1;
  1278. uinfo->value.integer.min = 0;
  1279. uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
  1280. if (!uinfo->value.integer.max) {
  1281. codec_warn(codec,
  1282. "num_steps = 0 for NID=0x%x (ctl = %s)\n",
  1283. nid, kcontrol->id.name);
  1284. return -EINVAL;
  1285. }
  1286. return 0;
  1287. }
  1288. EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_info);
  1289. static inline unsigned int
  1290. read_amp_value(struct hda_codec *codec, hda_nid_t nid,
  1291. int ch, int dir, int idx, unsigned int ofs)
  1292. {
  1293. unsigned int val;
  1294. val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
  1295. val &= HDA_AMP_VOLMASK;
  1296. if (val >= ofs)
  1297. val -= ofs;
  1298. else
  1299. val = 0;
  1300. return val;
  1301. }
  1302. static inline int
  1303. update_amp_value(struct hda_codec *codec, hda_nid_t nid,
  1304. int ch, int dir, int idx, unsigned int ofs,
  1305. unsigned int val)
  1306. {
  1307. unsigned int maxval;
  1308. if (val > 0)
  1309. val += ofs;
  1310. /* ofs = 0: raw max value */
  1311. maxval = get_amp_max_value(codec, nid, dir, 0);
  1312. if (val > maxval)
  1313. val = maxval;
  1314. return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
  1315. HDA_AMP_VOLMASK, val);
  1316. }
  1317. /**
  1318. * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
  1319. * @kcontrol: ctl element
  1320. * @ucontrol: pointer to get/store the data
  1321. *
  1322. * The control element is supposed to have the private_value field
  1323. * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
  1324. */
  1325. int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
  1326. struct snd_ctl_elem_value *ucontrol)
  1327. {
  1328. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1329. hda_nid_t nid = get_amp_nid(kcontrol);
  1330. int chs = get_amp_channels(kcontrol);
  1331. int dir = get_amp_direction(kcontrol);
  1332. int idx = get_amp_index(kcontrol);
  1333. unsigned int ofs = get_amp_offset(kcontrol);
  1334. long *valp = ucontrol->value.integer.value;
  1335. if (chs & 1)
  1336. *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
  1337. if (chs & 2)
  1338. *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
  1339. return 0;
  1340. }
  1341. EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_get);
  1342. /**
  1343. * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
  1344. * @kcontrol: ctl element
  1345. * @ucontrol: pointer to get/store the data
  1346. *
  1347. * The control element is supposed to have the private_value field
  1348. * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
  1349. */
  1350. int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
  1351. struct snd_ctl_elem_value *ucontrol)
  1352. {
  1353. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1354. hda_nid_t nid = get_amp_nid(kcontrol);
  1355. int chs = get_amp_channels(kcontrol);
  1356. int dir = get_amp_direction(kcontrol);
  1357. int idx = get_amp_index(kcontrol);
  1358. unsigned int ofs = get_amp_offset(kcontrol);
  1359. long *valp = ucontrol->value.integer.value;
  1360. int change = 0;
  1361. if (chs & 1) {
  1362. change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
  1363. valp++;
  1364. }
  1365. if (chs & 2)
  1366. change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
  1367. return change;
  1368. }
  1369. EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_put);
  1370. /**
  1371. * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
  1372. * @kcontrol: ctl element
  1373. * @op_flag: operation flag
  1374. * @size: byte size of input TLV
  1375. * @_tlv: TLV data
  1376. *
  1377. * The control element is supposed to have the private_value field
  1378. * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
  1379. */
  1380. int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
  1381. unsigned int size, unsigned int __user *_tlv)
  1382. {
  1383. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1384. hda_nid_t nid = get_amp_nid(kcontrol);
  1385. int dir = get_amp_direction(kcontrol);
  1386. unsigned int ofs = get_amp_offset(kcontrol);
  1387. bool min_mute = get_amp_min_mute(kcontrol);
  1388. u32 caps, val1, val2;
  1389. if (size < 4 * sizeof(unsigned int))
  1390. return -ENOMEM;
  1391. caps = query_amp_caps(codec, nid, dir);
  1392. val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
  1393. val2 = (val2 + 1) * 25;
  1394. val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
  1395. val1 += ofs;
  1396. val1 = ((int)val1) * ((int)val2);
  1397. if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
  1398. val2 |= TLV_DB_SCALE_MUTE;
  1399. if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
  1400. return -EFAULT;
  1401. if (put_user(2 * sizeof(unsigned int), _tlv + 1))
  1402. return -EFAULT;
  1403. if (put_user(val1, _tlv + 2))
  1404. return -EFAULT;
  1405. if (put_user(val2, _tlv + 3))
  1406. return -EFAULT;
  1407. return 0;
  1408. }
  1409. EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_tlv);
  1410. /**
  1411. * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
  1412. * @codec: HD-audio codec
  1413. * @nid: NID of a reference widget
  1414. * @dir: #HDA_INPUT or #HDA_OUTPUT
  1415. * @tlv: TLV data to be stored, at least 4 elements
  1416. *
  1417. * Set (static) TLV data for a virtual master volume using the AMP caps
  1418. * obtained from the reference NID.
  1419. * The volume range is recalculated as if the max volume is 0dB.
  1420. */
  1421. void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
  1422. unsigned int *tlv)
  1423. {
  1424. u32 caps;
  1425. int nums, step;
  1426. caps = query_amp_caps(codec, nid, dir);
  1427. nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
  1428. step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
  1429. step = (step + 1) * 25;
  1430. tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
  1431. tlv[1] = 2 * sizeof(unsigned int);
  1432. tlv[2] = -nums * step;
  1433. tlv[3] = step;
  1434. }
  1435. EXPORT_SYMBOL_GPL(snd_hda_set_vmaster_tlv);
  1436. /* find a mixer control element with the given name */
  1437. static struct snd_kcontrol *
  1438. find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
  1439. {
  1440. struct snd_ctl_elem_id id;
  1441. memset(&id, 0, sizeof(id));
  1442. id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  1443. id.device = dev;
  1444. id.index = idx;
  1445. if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
  1446. return NULL;
  1447. strcpy(id.name, name);
  1448. return snd_ctl_find_id(codec->card, &id);
  1449. }
  1450. /**
  1451. * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
  1452. * @codec: HD-audio codec
  1453. * @name: ctl id name string
  1454. *
  1455. * Get the control element with the given id string and IFACE_MIXER.
  1456. */
  1457. struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
  1458. const char *name)
  1459. {
  1460. return find_mixer_ctl(codec, name, 0, 0);
  1461. }
  1462. EXPORT_SYMBOL_GPL(snd_hda_find_mixer_ctl);
  1463. static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
  1464. int start_idx)
  1465. {
  1466. int i, idx;
  1467. /* 16 ctlrs should be large enough */
  1468. for (i = 0, idx = start_idx; i < 16; i++, idx++) {
  1469. if (!find_mixer_ctl(codec, name, 0, idx))
  1470. return idx;
  1471. }
  1472. return -EBUSY;
  1473. }
  1474. /**
  1475. * snd_hda_ctl_add - Add a control element and assign to the codec
  1476. * @codec: HD-audio codec
  1477. * @nid: corresponding NID (optional)
  1478. * @kctl: the control element to assign
  1479. *
  1480. * Add the given control element to an array inside the codec instance.
  1481. * All control elements belonging to a codec are supposed to be added
  1482. * by this function so that a proper clean-up works at the free or
  1483. * reconfiguration time.
  1484. *
  1485. * If non-zero @nid is passed, the NID is assigned to the control element.
  1486. * The assignment is shown in the codec proc file.
  1487. *
  1488. * snd_hda_ctl_add() checks the control subdev id field whether
  1489. * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
  1490. * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
  1491. * specifies if kctl->private_value is a HDA amplifier value.
  1492. */
  1493. int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
  1494. struct snd_kcontrol *kctl)
  1495. {
  1496. int err;
  1497. unsigned short flags = 0;
  1498. struct hda_nid_item *item;
  1499. if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
  1500. flags |= HDA_NID_ITEM_AMP;
  1501. if (nid == 0)
  1502. nid = get_amp_nid_(kctl->private_value);
  1503. }
  1504. if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
  1505. nid = kctl->id.subdevice & 0xffff;
  1506. if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
  1507. kctl->id.subdevice = 0;
  1508. err = snd_ctl_add(codec->card, kctl);
  1509. if (err < 0)
  1510. return err;
  1511. item = snd_array_new(&codec->mixers);
  1512. if (!item)
  1513. return -ENOMEM;
  1514. item->kctl = kctl;
  1515. item->nid = nid;
  1516. item->flags = flags;
  1517. return 0;
  1518. }
  1519. EXPORT_SYMBOL_GPL(snd_hda_ctl_add);
  1520. /**
  1521. * snd_hda_add_nid - Assign a NID to a control element
  1522. * @codec: HD-audio codec
  1523. * @nid: corresponding NID (optional)
  1524. * @kctl: the control element to assign
  1525. * @index: index to kctl
  1526. *
  1527. * Add the given control element to an array inside the codec instance.
  1528. * This function is used when #snd_hda_ctl_add cannot be used for 1:1
  1529. * NID:KCTL mapping - for example "Capture Source" selector.
  1530. */
  1531. int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
  1532. unsigned int index, hda_nid_t nid)
  1533. {
  1534. struct hda_nid_item *item;
  1535. if (nid > 0) {
  1536. item = snd_array_new(&codec->nids);
  1537. if (!item)
  1538. return -ENOMEM;
  1539. item->kctl = kctl;
  1540. item->index = index;
  1541. item->nid = nid;
  1542. return 0;
  1543. }
  1544. codec_err(codec, "no NID for mapping control %s:%d:%d\n",
  1545. kctl->id.name, kctl->id.index, index);
  1546. return -EINVAL;
  1547. }
  1548. EXPORT_SYMBOL_GPL(snd_hda_add_nid);
  1549. /**
  1550. * snd_hda_ctls_clear - Clear all controls assigned to the given codec
  1551. * @codec: HD-audio codec
  1552. */
  1553. void snd_hda_ctls_clear(struct hda_codec *codec)
  1554. {
  1555. int i;
  1556. struct hda_nid_item *items = codec->mixers.list;
  1557. for (i = 0; i < codec->mixers.used; i++)
  1558. snd_ctl_remove(codec->card, items[i].kctl);
  1559. snd_array_free(&codec->mixers);
  1560. snd_array_free(&codec->nids);
  1561. }
  1562. /**
  1563. * snd_hda_lock_devices - pseudo device locking
  1564. * @bus: the BUS
  1565. *
  1566. * toggle card->shutdown to allow/disallow the device access (as a hack)
  1567. */
  1568. int snd_hda_lock_devices(struct hda_bus *bus)
  1569. {
  1570. struct snd_card *card = bus->card;
  1571. struct hda_codec *codec;
  1572. spin_lock(&card->files_lock);
  1573. if (card->shutdown)
  1574. goto err_unlock;
  1575. card->shutdown = 1;
  1576. if (!list_empty(&card->ctl_files))
  1577. goto err_clear;
  1578. list_for_each_codec(codec, bus) {
  1579. struct hda_pcm *cpcm;
  1580. list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
  1581. if (!cpcm->pcm)
  1582. continue;
  1583. if (cpcm->pcm->streams[0].substream_opened ||
  1584. cpcm->pcm->streams[1].substream_opened)
  1585. goto err_clear;
  1586. }
  1587. }
  1588. spin_unlock(&card->files_lock);
  1589. return 0;
  1590. err_clear:
  1591. card->shutdown = 0;
  1592. err_unlock:
  1593. spin_unlock(&card->files_lock);
  1594. return -EINVAL;
  1595. }
  1596. EXPORT_SYMBOL_GPL(snd_hda_lock_devices);
  1597. /**
  1598. * snd_hda_unlock_devices - pseudo device unlocking
  1599. * @bus: the BUS
  1600. */
  1601. void snd_hda_unlock_devices(struct hda_bus *bus)
  1602. {
  1603. struct snd_card *card = bus->card;
  1604. spin_lock(&card->files_lock);
  1605. card->shutdown = 0;
  1606. spin_unlock(&card->files_lock);
  1607. }
  1608. EXPORT_SYMBOL_GPL(snd_hda_unlock_devices);
  1609. /**
  1610. * snd_hda_codec_reset - Clear all objects assigned to the codec
  1611. * @codec: HD-audio codec
  1612. *
  1613. * This frees the all PCM and control elements assigned to the codec, and
  1614. * clears the caches and restores the pin default configurations.
  1615. *
  1616. * When a device is being used, it returns -EBSY. If successfully freed,
  1617. * returns zero.
  1618. */
  1619. int snd_hda_codec_reset(struct hda_codec *codec)
  1620. {
  1621. struct hda_bus *bus = codec->bus;
  1622. if (snd_hda_lock_devices(bus) < 0)
  1623. return -EBUSY;
  1624. /* OK, let it free */
  1625. snd_hdac_device_unregister(&codec->core);
  1626. /* allow device access again */
  1627. snd_hda_unlock_devices(bus);
  1628. return 0;
  1629. }
  1630. typedef int (*map_slave_func_t)(struct hda_codec *, void *, struct snd_kcontrol *);
  1631. /* apply the function to all matching slave ctls in the mixer list */
  1632. static int map_slaves(struct hda_codec *codec, const char * const *slaves,
  1633. const char *suffix, map_slave_func_t func, void *data)
  1634. {
  1635. struct hda_nid_item *items;
  1636. const char * const *s;
  1637. int i, err;
  1638. items = codec->mixers.list;
  1639. for (i = 0; i < codec->mixers.used; i++) {
  1640. struct snd_kcontrol *sctl = items[i].kctl;
  1641. if (!sctl || sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
  1642. continue;
  1643. for (s = slaves; *s; s++) {
  1644. char tmpname[sizeof(sctl->id.name)];
  1645. const char *name = *s;
  1646. if (suffix) {
  1647. snprintf(tmpname, sizeof(tmpname), "%s %s",
  1648. name, suffix);
  1649. name = tmpname;
  1650. }
  1651. if (!strcmp(sctl->id.name, name)) {
  1652. err = func(codec, data, sctl);
  1653. if (err)
  1654. return err;
  1655. break;
  1656. }
  1657. }
  1658. }
  1659. return 0;
  1660. }
  1661. static int check_slave_present(struct hda_codec *codec,
  1662. void *data, struct snd_kcontrol *sctl)
  1663. {
  1664. return 1;
  1665. }
  1666. /* guess the value corresponding to 0dB */
  1667. static int get_kctl_0dB_offset(struct hda_codec *codec,
  1668. struct snd_kcontrol *kctl, int *step_to_check)
  1669. {
  1670. int _tlv[4];
  1671. const int *tlv = NULL;
  1672. int val = -1;
  1673. if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
  1674. /* FIXME: set_fs() hack for obtaining user-space TLV data */
  1675. mm_segment_t fs = get_fs();
  1676. set_fs(get_ds());
  1677. if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
  1678. tlv = _tlv;
  1679. set_fs(fs);
  1680. } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
  1681. tlv = kctl->tlv.p;
  1682. if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE) {
  1683. int step = tlv[3];
  1684. step &= ~TLV_DB_SCALE_MUTE;
  1685. if (!step)
  1686. return -1;
  1687. if (*step_to_check && *step_to_check != step) {
  1688. codec_err(codec, "Mismatching dB step for vmaster slave (%d!=%d)\n",
  1689. - *step_to_check, step);
  1690. return -1;
  1691. }
  1692. *step_to_check = step;
  1693. val = -tlv[2] / step;
  1694. }
  1695. return val;
  1696. }
  1697. /* call kctl->put with the given value(s) */
  1698. static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
  1699. {
  1700. struct snd_ctl_elem_value *ucontrol;
  1701. ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
  1702. if (!ucontrol)
  1703. return -ENOMEM;
  1704. ucontrol->value.integer.value[0] = val;
  1705. ucontrol->value.integer.value[1] = val;
  1706. kctl->put(kctl, ucontrol);
  1707. kfree(ucontrol);
  1708. return 0;
  1709. }
  1710. /* initialize the slave volume with 0dB */
  1711. static int init_slave_0dB(struct hda_codec *codec,
  1712. void *data, struct snd_kcontrol *slave)
  1713. {
  1714. int offset = get_kctl_0dB_offset(codec, slave, data);
  1715. if (offset > 0)
  1716. put_kctl_with_value(slave, offset);
  1717. return 0;
  1718. }
  1719. /* unmute the slave */
  1720. static int init_slave_unmute(struct hda_codec *codec,
  1721. void *data, struct snd_kcontrol *slave)
  1722. {
  1723. return put_kctl_with_value(slave, 1);
  1724. }
  1725. static int add_slave(struct hda_codec *codec,
  1726. void *data, struct snd_kcontrol *slave)
  1727. {
  1728. return snd_ctl_add_slave(data, slave);
  1729. }
  1730. /**
  1731. * __snd_hda_add_vmaster - create a virtual master control and add slaves
  1732. * @codec: HD-audio codec
  1733. * @name: vmaster control name
  1734. * @tlv: TLV data (optional)
  1735. * @slaves: slave control names (optional)
  1736. * @suffix: suffix string to each slave name (optional)
  1737. * @init_slave_vol: initialize slaves to unmute/0dB
  1738. * @ctl_ret: store the vmaster kcontrol in return
  1739. *
  1740. * Create a virtual master control with the given name. The TLV data
  1741. * must be either NULL or a valid data.
  1742. *
  1743. * @slaves is a NULL-terminated array of strings, each of which is a
  1744. * slave control name. All controls with these names are assigned to
  1745. * the new virtual master control.
  1746. *
  1747. * This function returns zero if successful or a negative error code.
  1748. */
  1749. int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
  1750. unsigned int *tlv, const char * const *slaves,
  1751. const char *suffix, bool init_slave_vol,
  1752. struct snd_kcontrol **ctl_ret)
  1753. {
  1754. struct snd_kcontrol *kctl;
  1755. int err;
  1756. if (ctl_ret)
  1757. *ctl_ret = NULL;
  1758. err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
  1759. if (err != 1) {
  1760. codec_dbg(codec, "No slave found for %s\n", name);
  1761. return 0;
  1762. }
  1763. kctl = snd_ctl_make_virtual_master(name, tlv);
  1764. if (!kctl)
  1765. return -ENOMEM;
  1766. err = snd_hda_ctl_add(codec, 0, kctl);
  1767. if (err < 0)
  1768. return err;
  1769. err = map_slaves(codec, slaves, suffix, add_slave, kctl);
  1770. if (err < 0)
  1771. return err;
  1772. /* init with master mute & zero volume */
  1773. put_kctl_with_value(kctl, 0);
  1774. if (init_slave_vol) {
  1775. int step = 0;
  1776. map_slaves(codec, slaves, suffix,
  1777. tlv ? init_slave_0dB : init_slave_unmute, &step);
  1778. }
  1779. if (ctl_ret)
  1780. *ctl_ret = kctl;
  1781. return 0;
  1782. }
  1783. EXPORT_SYMBOL_GPL(__snd_hda_add_vmaster);
  1784. /*
  1785. * mute-LED control using vmaster
  1786. */
  1787. static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
  1788. struct snd_ctl_elem_info *uinfo)
  1789. {
  1790. static const char * const texts[] = {
  1791. "On", "Off", "Follow Master"
  1792. };
  1793. return snd_ctl_enum_info(uinfo, 1, 3, texts);
  1794. }
  1795. static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
  1796. struct snd_ctl_elem_value *ucontrol)
  1797. {
  1798. struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
  1799. ucontrol->value.enumerated.item[0] = hook->mute_mode;
  1800. return 0;
  1801. }
  1802. static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
  1803. struct snd_ctl_elem_value *ucontrol)
  1804. {
  1805. struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
  1806. unsigned int old_mode = hook->mute_mode;
  1807. hook->mute_mode = ucontrol->value.enumerated.item[0];
  1808. if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
  1809. hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
  1810. if (old_mode == hook->mute_mode)
  1811. return 0;
  1812. snd_hda_sync_vmaster_hook(hook);
  1813. return 1;
  1814. }
  1815. static struct snd_kcontrol_new vmaster_mute_mode = {
  1816. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1817. .name = "Mute-LED Mode",
  1818. .info = vmaster_mute_mode_info,
  1819. .get = vmaster_mute_mode_get,
  1820. .put = vmaster_mute_mode_put,
  1821. };
  1822. /* meta hook to call each driver's vmaster hook */
  1823. static void vmaster_hook(void *private_data, int enabled)
  1824. {
  1825. struct hda_vmaster_mute_hook *hook = private_data;
  1826. if (hook->mute_mode != HDA_VMUTE_FOLLOW_MASTER)
  1827. enabled = hook->mute_mode;
  1828. hook->hook(hook->codec, enabled);
  1829. }
  1830. /**
  1831. * snd_hda_add_vmaster_hook - Add a vmaster hook for mute-LED
  1832. * @codec: the HDA codec
  1833. * @hook: the vmaster hook object
  1834. * @expose_enum_ctl: flag to create an enum ctl
  1835. *
  1836. * Add a mute-LED hook with the given vmaster switch kctl.
  1837. * When @expose_enum_ctl is set, "Mute-LED Mode" control is automatically
  1838. * created and associated with the given hook.
  1839. */
  1840. int snd_hda_add_vmaster_hook(struct hda_codec *codec,
  1841. struct hda_vmaster_mute_hook *hook,
  1842. bool expose_enum_ctl)
  1843. {
  1844. struct snd_kcontrol *kctl;
  1845. if (!hook->hook || !hook->sw_kctl)
  1846. return 0;
  1847. hook->codec = codec;
  1848. hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
  1849. snd_ctl_add_vmaster_hook(hook->sw_kctl, vmaster_hook, hook);
  1850. if (!expose_enum_ctl)
  1851. return 0;
  1852. kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
  1853. if (!kctl)
  1854. return -ENOMEM;
  1855. return snd_hda_ctl_add(codec, 0, kctl);
  1856. }
  1857. EXPORT_SYMBOL_GPL(snd_hda_add_vmaster_hook);
  1858. /**
  1859. * snd_hda_sync_vmaster_hook - Sync vmaster hook
  1860. * @hook: the vmaster hook
  1861. *
  1862. * Call the hook with the current value for synchronization.
  1863. * Should be called in init callback.
  1864. */
  1865. void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
  1866. {
  1867. if (!hook->hook || !hook->codec)
  1868. return;
  1869. /* don't call vmaster hook in the destructor since it might have
  1870. * been already destroyed
  1871. */
  1872. if (hook->codec->bus->shutdown)
  1873. return;
  1874. snd_ctl_sync_vmaster_hook(hook->sw_kctl);
  1875. }
  1876. EXPORT_SYMBOL_GPL(snd_hda_sync_vmaster_hook);
  1877. /**
  1878. * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
  1879. * @kcontrol: referred ctl element
  1880. * @uinfo: pointer to get/store the data
  1881. *
  1882. * The control element is supposed to have the private_value field
  1883. * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
  1884. */
  1885. int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
  1886. struct snd_ctl_elem_info *uinfo)
  1887. {
  1888. int chs = get_amp_channels(kcontrol);
  1889. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  1890. uinfo->count = chs == 3 ? 2 : 1;
  1891. uinfo->value.integer.min = 0;
  1892. uinfo->value.integer.max = 1;
  1893. return 0;
  1894. }
  1895. EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_info);
  1896. /**
  1897. * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
  1898. * @kcontrol: ctl element
  1899. * @ucontrol: pointer to get/store the data
  1900. *
  1901. * The control element is supposed to have the private_value field
  1902. * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
  1903. */
  1904. int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
  1905. struct snd_ctl_elem_value *ucontrol)
  1906. {
  1907. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1908. hda_nid_t nid = get_amp_nid(kcontrol);
  1909. int chs = get_amp_channels(kcontrol);
  1910. int dir = get_amp_direction(kcontrol);
  1911. int idx = get_amp_index(kcontrol);
  1912. long *valp = ucontrol->value.integer.value;
  1913. if (chs & 1)
  1914. *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
  1915. HDA_AMP_MUTE) ? 0 : 1;
  1916. if (chs & 2)
  1917. *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
  1918. HDA_AMP_MUTE) ? 0 : 1;
  1919. return 0;
  1920. }
  1921. EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_get);
  1922. /**
  1923. * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
  1924. * @kcontrol: ctl element
  1925. * @ucontrol: pointer to get/store the data
  1926. *
  1927. * The control element is supposed to have the private_value field
  1928. * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
  1929. */
  1930. int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
  1931. struct snd_ctl_elem_value *ucontrol)
  1932. {
  1933. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1934. hda_nid_t nid = get_amp_nid(kcontrol);
  1935. int chs = get_amp_channels(kcontrol);
  1936. int dir = get_amp_direction(kcontrol);
  1937. int idx = get_amp_index(kcontrol);
  1938. long *valp = ucontrol->value.integer.value;
  1939. int change = 0;
  1940. if (chs & 1) {
  1941. change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
  1942. HDA_AMP_MUTE,
  1943. *valp ? 0 : HDA_AMP_MUTE);
  1944. valp++;
  1945. }
  1946. if (chs & 2)
  1947. change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
  1948. HDA_AMP_MUTE,
  1949. *valp ? 0 : HDA_AMP_MUTE);
  1950. hda_call_check_power_status(codec, nid);
  1951. return change;
  1952. }
  1953. EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_put);
  1954. /*
  1955. * bound volume controls
  1956. *
  1957. * bind multiple volumes (# indices, from 0)
  1958. */
  1959. #define AMP_VAL_IDX_SHIFT 19
  1960. #define AMP_VAL_IDX_MASK (0x0f<<19)
  1961. /**
  1962. * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
  1963. * @kcontrol: ctl element
  1964. * @ucontrol: pointer to get/store the data
  1965. *
  1966. * The control element is supposed to have the private_value field
  1967. * set up via HDA_BIND_MUTE*() macros.
  1968. */
  1969. int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
  1970. struct snd_ctl_elem_value *ucontrol)
  1971. {
  1972. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1973. unsigned long pval;
  1974. int err;
  1975. mutex_lock(&codec->control_mutex);
  1976. pval = kcontrol->private_value;
  1977. kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
  1978. err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
  1979. kcontrol->private_value = pval;
  1980. mutex_unlock(&codec->control_mutex);
  1981. return err;
  1982. }
  1983. EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_get);
  1984. /**
  1985. * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
  1986. * @kcontrol: ctl element
  1987. * @ucontrol: pointer to get/store the data
  1988. *
  1989. * The control element is supposed to have the private_value field
  1990. * set up via HDA_BIND_MUTE*() macros.
  1991. */
  1992. int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
  1993. struct snd_ctl_elem_value *ucontrol)
  1994. {
  1995. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1996. unsigned long pval;
  1997. int i, indices, err = 0, change = 0;
  1998. mutex_lock(&codec->control_mutex);
  1999. pval = kcontrol->private_value;
  2000. indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
  2001. for (i = 0; i < indices; i++) {
  2002. kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
  2003. (i << AMP_VAL_IDX_SHIFT);
  2004. err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
  2005. if (err < 0)
  2006. break;
  2007. change |= err;
  2008. }
  2009. kcontrol->private_value = pval;
  2010. mutex_unlock(&codec->control_mutex);
  2011. return err < 0 ? err : change;
  2012. }
  2013. EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_put);
  2014. /**
  2015. * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
  2016. * @kcontrol: referred ctl element
  2017. * @uinfo: pointer to get/store the data
  2018. *
  2019. * The control element is supposed to have the private_value field
  2020. * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
  2021. */
  2022. int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
  2023. struct snd_ctl_elem_info *uinfo)
  2024. {
  2025. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2026. struct hda_bind_ctls *c;
  2027. int err;
  2028. mutex_lock(&codec->control_mutex);
  2029. c = (struct hda_bind_ctls *)kcontrol->private_value;
  2030. kcontrol->private_value = *c->values;
  2031. err = c->ops->info(kcontrol, uinfo);
  2032. kcontrol->private_value = (long)c;
  2033. mutex_unlock(&codec->control_mutex);
  2034. return err;
  2035. }
  2036. EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_info);
  2037. /**
  2038. * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
  2039. * @kcontrol: ctl element
  2040. * @ucontrol: pointer to get/store the data
  2041. *
  2042. * The control element is supposed to have the private_value field
  2043. * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
  2044. */
  2045. int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
  2046. struct snd_ctl_elem_value *ucontrol)
  2047. {
  2048. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2049. struct hda_bind_ctls *c;
  2050. int err;
  2051. mutex_lock(&codec->control_mutex);
  2052. c = (struct hda_bind_ctls *)kcontrol->private_value;
  2053. kcontrol->private_value = *c->values;
  2054. err = c->ops->get(kcontrol, ucontrol);
  2055. kcontrol->private_value = (long)c;
  2056. mutex_unlock(&codec->control_mutex);
  2057. return err;
  2058. }
  2059. EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_get);
  2060. /**
  2061. * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
  2062. * @kcontrol: ctl element
  2063. * @ucontrol: pointer to get/store the data
  2064. *
  2065. * The control element is supposed to have the private_value field
  2066. * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
  2067. */
  2068. int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
  2069. struct snd_ctl_elem_value *ucontrol)
  2070. {
  2071. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2072. struct hda_bind_ctls *c;
  2073. unsigned long *vals;
  2074. int err = 0, change = 0;
  2075. mutex_lock(&codec->control_mutex);
  2076. c = (struct hda_bind_ctls *)kcontrol->private_value;
  2077. for (vals = c->values; *vals; vals++) {
  2078. kcontrol->private_value = *vals;
  2079. err = c->ops->put(kcontrol, ucontrol);
  2080. if (err < 0)
  2081. break;
  2082. change |= err;
  2083. }
  2084. kcontrol->private_value = (long)c;
  2085. mutex_unlock(&codec->control_mutex);
  2086. return err < 0 ? err : change;
  2087. }
  2088. EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_put);
  2089. /**
  2090. * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
  2091. * @kcontrol: ctl element
  2092. * @op_flag: operation flag
  2093. * @size: byte size of input TLV
  2094. * @tlv: TLV data
  2095. *
  2096. * The control element is supposed to have the private_value field
  2097. * set up via HDA_BIND_VOL() macro.
  2098. */
  2099. int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
  2100. unsigned int size, unsigned int __user *tlv)
  2101. {
  2102. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2103. struct hda_bind_ctls *c;
  2104. int err;
  2105. mutex_lock(&codec->control_mutex);
  2106. c = (struct hda_bind_ctls *)kcontrol->private_value;
  2107. kcontrol->private_value = *c->values;
  2108. err = c->ops->tlv(kcontrol, op_flag, size, tlv);
  2109. kcontrol->private_value = (long)c;
  2110. mutex_unlock(&codec->control_mutex);
  2111. return err;
  2112. }
  2113. EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_tlv);
  2114. struct hda_ctl_ops snd_hda_bind_vol = {
  2115. .info = snd_hda_mixer_amp_volume_info,
  2116. .get = snd_hda_mixer_amp_volume_get,
  2117. .put = snd_hda_mixer_amp_volume_put,
  2118. .tlv = snd_hda_mixer_amp_tlv
  2119. };
  2120. EXPORT_SYMBOL_GPL(snd_hda_bind_vol);
  2121. struct hda_ctl_ops snd_hda_bind_sw = {
  2122. .info = snd_hda_mixer_amp_switch_info,
  2123. .get = snd_hda_mixer_amp_switch_get,
  2124. .put = snd_hda_mixer_amp_switch_put,
  2125. .tlv = snd_hda_mixer_amp_tlv
  2126. };
  2127. EXPORT_SYMBOL_GPL(snd_hda_bind_sw);
  2128. /*
  2129. * SPDIF out controls
  2130. */
  2131. static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
  2132. struct snd_ctl_elem_info *uinfo)
  2133. {
  2134. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  2135. uinfo->count = 1;
  2136. return 0;
  2137. }
  2138. static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
  2139. struct snd_ctl_elem_value *ucontrol)
  2140. {
  2141. ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
  2142. IEC958_AES0_NONAUDIO |
  2143. IEC958_AES0_CON_EMPHASIS_5015 |
  2144. IEC958_AES0_CON_NOT_COPYRIGHT;
  2145. ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
  2146. IEC958_AES1_CON_ORIGINAL;
  2147. return 0;
  2148. }
  2149. static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
  2150. struct snd_ctl_elem_value *ucontrol)
  2151. {
  2152. ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
  2153. IEC958_AES0_NONAUDIO |
  2154. IEC958_AES0_PRO_EMPHASIS_5015;
  2155. return 0;
  2156. }
  2157. static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
  2158. struct snd_ctl_elem_value *ucontrol)
  2159. {
  2160. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2161. int idx = kcontrol->private_value;
  2162. struct hda_spdif_out *spdif;
  2163. mutex_lock(&codec->spdif_mutex);
  2164. spdif = snd_array_elem(&codec->spdif_out, idx);
  2165. ucontrol->value.iec958.status[0] = spdif->status & 0xff;
  2166. ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
  2167. ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
  2168. ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
  2169. mutex_unlock(&codec->spdif_mutex);
  2170. return 0;
  2171. }
  2172. /* convert from SPDIF status bits to HDA SPDIF bits
  2173. * bit 0 (DigEn) is always set zero (to be filled later)
  2174. */
  2175. static unsigned short convert_from_spdif_status(unsigned int sbits)
  2176. {
  2177. unsigned short val = 0;
  2178. if (sbits & IEC958_AES0_PROFESSIONAL)
  2179. val |= AC_DIG1_PROFESSIONAL;
  2180. if (sbits & IEC958_AES0_NONAUDIO)
  2181. val |= AC_DIG1_NONAUDIO;
  2182. if (sbits & IEC958_AES0_PROFESSIONAL) {
  2183. if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
  2184. IEC958_AES0_PRO_EMPHASIS_5015)
  2185. val |= AC_DIG1_EMPHASIS;
  2186. } else {
  2187. if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
  2188. IEC958_AES0_CON_EMPHASIS_5015)
  2189. val |= AC_DIG1_EMPHASIS;
  2190. if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
  2191. val |= AC_DIG1_COPYRIGHT;
  2192. if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
  2193. val |= AC_DIG1_LEVEL;
  2194. val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
  2195. }
  2196. return val;
  2197. }
  2198. /* convert to SPDIF status bits from HDA SPDIF bits
  2199. */
  2200. static unsigned int convert_to_spdif_status(unsigned short val)
  2201. {
  2202. unsigned int sbits = 0;
  2203. if (val & AC_DIG1_NONAUDIO)
  2204. sbits |= IEC958_AES0_NONAUDIO;
  2205. if (val & AC_DIG1_PROFESSIONAL)
  2206. sbits |= IEC958_AES0_PROFESSIONAL;
  2207. if (sbits & IEC958_AES0_PROFESSIONAL) {
  2208. if (val & AC_DIG1_EMPHASIS)
  2209. sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
  2210. } else {
  2211. if (val & AC_DIG1_EMPHASIS)
  2212. sbits |= IEC958_AES0_CON_EMPHASIS_5015;
  2213. if (!(val & AC_DIG1_COPYRIGHT))
  2214. sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
  2215. if (val & AC_DIG1_LEVEL)
  2216. sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
  2217. sbits |= val & (0x7f << 8);
  2218. }
  2219. return sbits;
  2220. }
  2221. /* set digital convert verbs both for the given NID and its slaves */
  2222. static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
  2223. int mask, int val)
  2224. {
  2225. const hda_nid_t *d;
  2226. snd_hdac_regmap_update(&codec->core, nid, AC_VERB_SET_DIGI_CONVERT_1,
  2227. mask, val);
  2228. d = codec->slave_dig_outs;
  2229. if (!d)
  2230. return;
  2231. for (; *d; d++)
  2232. snd_hdac_regmap_update(&codec->core, *d,
  2233. AC_VERB_SET_DIGI_CONVERT_1, mask, val);
  2234. }
  2235. static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
  2236. int dig1, int dig2)
  2237. {
  2238. unsigned int mask = 0;
  2239. unsigned int val = 0;
  2240. if (dig1 != -1) {
  2241. mask |= 0xff;
  2242. val = dig1;
  2243. }
  2244. if (dig2 != -1) {
  2245. mask |= 0xff00;
  2246. val |= dig2 << 8;
  2247. }
  2248. set_dig_out(codec, nid, mask, val);
  2249. }
  2250. static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
  2251. struct snd_ctl_elem_value *ucontrol)
  2252. {
  2253. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2254. int idx = kcontrol->private_value;
  2255. struct hda_spdif_out *spdif;
  2256. hda_nid_t nid;
  2257. unsigned short val;
  2258. int change;
  2259. mutex_lock(&codec->spdif_mutex);
  2260. spdif = snd_array_elem(&codec->spdif_out, idx);
  2261. nid = spdif->nid;
  2262. spdif->status = ucontrol->value.iec958.status[0] |
  2263. ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
  2264. ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
  2265. ((unsigned int)ucontrol->value.iec958.status[3] << 24);
  2266. val = convert_from_spdif_status(spdif->status);
  2267. val |= spdif->ctls & 1;
  2268. change = spdif->ctls != val;
  2269. spdif->ctls = val;
  2270. if (change && nid != (u16)-1)
  2271. set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
  2272. mutex_unlock(&codec->spdif_mutex);
  2273. return change;
  2274. }
  2275. #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
  2276. static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
  2277. struct snd_ctl_elem_value *ucontrol)
  2278. {
  2279. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2280. int idx = kcontrol->private_value;
  2281. struct hda_spdif_out *spdif;
  2282. mutex_lock(&codec->spdif_mutex);
  2283. spdif = snd_array_elem(&codec->spdif_out, idx);
  2284. ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
  2285. mutex_unlock(&codec->spdif_mutex);
  2286. return 0;
  2287. }
  2288. static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
  2289. int dig1, int dig2)
  2290. {
  2291. set_dig_out_convert(codec, nid, dig1, dig2);
  2292. /* unmute amp switch (if any) */
  2293. if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
  2294. (dig1 & AC_DIG1_ENABLE))
  2295. snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
  2296. HDA_AMP_MUTE, 0);
  2297. }
  2298. static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
  2299. struct snd_ctl_elem_value *ucontrol)
  2300. {
  2301. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2302. int idx = kcontrol->private_value;
  2303. struct hda_spdif_out *spdif;
  2304. hda_nid_t nid;
  2305. unsigned short val;
  2306. int change;
  2307. mutex_lock(&codec->spdif_mutex);
  2308. spdif = snd_array_elem(&codec->spdif_out, idx);
  2309. nid = spdif->nid;
  2310. val = spdif->ctls & ~AC_DIG1_ENABLE;
  2311. if (ucontrol->value.integer.value[0])
  2312. val |= AC_DIG1_ENABLE;
  2313. change = spdif->ctls != val;
  2314. spdif->ctls = val;
  2315. if (change && nid != (u16)-1)
  2316. set_spdif_ctls(codec, nid, val & 0xff, -1);
  2317. mutex_unlock(&codec->spdif_mutex);
  2318. return change;
  2319. }
  2320. static struct snd_kcontrol_new dig_mixes[] = {
  2321. {
  2322. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  2323. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2324. .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
  2325. .info = snd_hda_spdif_mask_info,
  2326. .get = snd_hda_spdif_cmask_get,
  2327. },
  2328. {
  2329. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  2330. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2331. .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
  2332. .info = snd_hda_spdif_mask_info,
  2333. .get = snd_hda_spdif_pmask_get,
  2334. },
  2335. {
  2336. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2337. .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
  2338. .info = snd_hda_spdif_mask_info,
  2339. .get = snd_hda_spdif_default_get,
  2340. .put = snd_hda_spdif_default_put,
  2341. },
  2342. {
  2343. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2344. .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
  2345. .info = snd_hda_spdif_out_switch_info,
  2346. .get = snd_hda_spdif_out_switch_get,
  2347. .put = snd_hda_spdif_out_switch_put,
  2348. },
  2349. { } /* end */
  2350. };
  2351. /**
  2352. * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
  2353. * @codec: the HDA codec
  2354. * @associated_nid: NID that new ctls associated with
  2355. * @cvt_nid: converter NID
  2356. * @type: HDA_PCM_TYPE_*
  2357. * Creates controls related with the digital output.
  2358. * Called from each patch supporting the digital out.
  2359. *
  2360. * Returns 0 if successful, or a negative error code.
  2361. */
  2362. int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
  2363. hda_nid_t associated_nid,
  2364. hda_nid_t cvt_nid,
  2365. int type)
  2366. {
  2367. int err;
  2368. struct snd_kcontrol *kctl;
  2369. struct snd_kcontrol_new *dig_mix;
  2370. int idx = 0;
  2371. int val = 0;
  2372. const int spdif_index = 16;
  2373. struct hda_spdif_out *spdif;
  2374. struct hda_bus *bus = codec->bus;
  2375. if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
  2376. type == HDA_PCM_TYPE_SPDIF) {
  2377. idx = spdif_index;
  2378. } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
  2379. type == HDA_PCM_TYPE_HDMI) {
  2380. /* suppose a single SPDIF device */
  2381. for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
  2382. kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0);
  2383. if (!kctl)
  2384. break;
  2385. kctl->id.index = spdif_index;
  2386. }
  2387. bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
  2388. }
  2389. if (!bus->primary_dig_out_type)
  2390. bus->primary_dig_out_type = type;
  2391. idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx);
  2392. if (idx < 0) {
  2393. codec_err(codec, "too many IEC958 outputs\n");
  2394. return -EBUSY;
  2395. }
  2396. spdif = snd_array_new(&codec->spdif_out);
  2397. if (!spdif)
  2398. return -ENOMEM;
  2399. for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
  2400. kctl = snd_ctl_new1(dig_mix, codec);
  2401. if (!kctl)
  2402. return -ENOMEM;
  2403. kctl->id.index = idx;
  2404. kctl->private_value = codec->spdif_out.used - 1;
  2405. err = snd_hda_ctl_add(codec, associated_nid, kctl);
  2406. if (err < 0)
  2407. return err;
  2408. }
  2409. spdif->nid = cvt_nid;
  2410. snd_hdac_regmap_read(&codec->core, cvt_nid,
  2411. AC_VERB_GET_DIGI_CONVERT_1, &val);
  2412. spdif->ctls = val;
  2413. spdif->status = convert_to_spdif_status(spdif->ctls);
  2414. return 0;
  2415. }
  2416. EXPORT_SYMBOL_GPL(snd_hda_create_dig_out_ctls);
  2417. /**
  2418. * snd_hda_spdif_out_of_nid - get the hda_spdif_out entry from the given NID
  2419. * @codec: the HDA codec
  2420. * @nid: widget NID
  2421. *
  2422. * call within spdif_mutex lock
  2423. */
  2424. struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
  2425. hda_nid_t nid)
  2426. {
  2427. int i;
  2428. for (i = 0; i < codec->spdif_out.used; i++) {
  2429. struct hda_spdif_out *spdif =
  2430. snd_array_elem(&codec->spdif_out, i);
  2431. if (spdif->nid == nid)
  2432. return spdif;
  2433. }
  2434. return NULL;
  2435. }
  2436. EXPORT_SYMBOL_GPL(snd_hda_spdif_out_of_nid);
  2437. /**
  2438. * snd_hda_spdif_ctls_unassign - Unassign the given SPDIF ctl
  2439. * @codec: the HDA codec
  2440. * @idx: the SPDIF ctl index
  2441. *
  2442. * Unassign the widget from the given SPDIF control.
  2443. */
  2444. void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
  2445. {
  2446. struct hda_spdif_out *spdif;
  2447. mutex_lock(&codec->spdif_mutex);
  2448. spdif = snd_array_elem(&codec->spdif_out, idx);
  2449. spdif->nid = (u16)-1;
  2450. mutex_unlock(&codec->spdif_mutex);
  2451. }
  2452. EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_unassign);
  2453. /**
  2454. * snd_hda_spdif_ctls_assign - Assign the SPDIF controls to the given NID
  2455. * @codec: the HDA codec
  2456. * @idx: the SPDIF ctl idx
  2457. * @nid: widget NID
  2458. *
  2459. * Assign the widget to the SPDIF control with the given index.
  2460. */
  2461. void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
  2462. {
  2463. struct hda_spdif_out *spdif;
  2464. unsigned short val;
  2465. mutex_lock(&codec->spdif_mutex);
  2466. spdif = snd_array_elem(&codec->spdif_out, idx);
  2467. if (spdif->nid != nid) {
  2468. spdif->nid = nid;
  2469. val = spdif->ctls;
  2470. set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
  2471. }
  2472. mutex_unlock(&codec->spdif_mutex);
  2473. }
  2474. EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_assign);
  2475. /*
  2476. * SPDIF sharing with analog output
  2477. */
  2478. static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
  2479. struct snd_ctl_elem_value *ucontrol)
  2480. {
  2481. struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
  2482. ucontrol->value.integer.value[0] = mout->share_spdif;
  2483. return 0;
  2484. }
  2485. static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
  2486. struct snd_ctl_elem_value *ucontrol)
  2487. {
  2488. struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
  2489. mout->share_spdif = !!ucontrol->value.integer.value[0];
  2490. return 0;
  2491. }
  2492. static struct snd_kcontrol_new spdif_share_sw = {
  2493. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2494. .name = "IEC958 Default PCM Playback Switch",
  2495. .info = snd_ctl_boolean_mono_info,
  2496. .get = spdif_share_sw_get,
  2497. .put = spdif_share_sw_put,
  2498. };
  2499. /**
  2500. * snd_hda_create_spdif_share_sw - create Default PCM switch
  2501. * @codec: the HDA codec
  2502. * @mout: multi-out instance
  2503. */
  2504. int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
  2505. struct hda_multi_out *mout)
  2506. {
  2507. struct snd_kcontrol *kctl;
  2508. if (!mout->dig_out_nid)
  2509. return 0;
  2510. kctl = snd_ctl_new1(&spdif_share_sw, mout);
  2511. if (!kctl)
  2512. return -ENOMEM;
  2513. /* ATTENTION: here mout is passed as private_data, instead of codec */
  2514. return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl);
  2515. }
  2516. EXPORT_SYMBOL_GPL(snd_hda_create_spdif_share_sw);
  2517. /*
  2518. * SPDIF input
  2519. */
  2520. #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
  2521. static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
  2522. struct snd_ctl_elem_value *ucontrol)
  2523. {
  2524. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2525. ucontrol->value.integer.value[0] = codec->spdif_in_enable;
  2526. return 0;
  2527. }
  2528. static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
  2529. struct snd_ctl_elem_value *ucontrol)
  2530. {
  2531. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2532. hda_nid_t nid = kcontrol->private_value;
  2533. unsigned int val = !!ucontrol->value.integer.value[0];
  2534. int change;
  2535. mutex_lock(&codec->spdif_mutex);
  2536. change = codec->spdif_in_enable != val;
  2537. if (change) {
  2538. codec->spdif_in_enable = val;
  2539. snd_hdac_regmap_write(&codec->core, nid,
  2540. AC_VERB_SET_DIGI_CONVERT_1, val);
  2541. }
  2542. mutex_unlock(&codec->spdif_mutex);
  2543. return change;
  2544. }
  2545. static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
  2546. struct snd_ctl_elem_value *ucontrol)
  2547. {
  2548. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2549. hda_nid_t nid = kcontrol->private_value;
  2550. unsigned int val;
  2551. unsigned int sbits;
  2552. snd_hdac_regmap_read(&codec->core, nid,
  2553. AC_VERB_GET_DIGI_CONVERT_1, &val);
  2554. sbits = convert_to_spdif_status(val);
  2555. ucontrol->value.iec958.status[0] = sbits;
  2556. ucontrol->value.iec958.status[1] = sbits >> 8;
  2557. ucontrol->value.iec958.status[2] = sbits >> 16;
  2558. ucontrol->value.iec958.status[3] = sbits >> 24;
  2559. return 0;
  2560. }
  2561. static struct snd_kcontrol_new dig_in_ctls[] = {
  2562. {
  2563. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2564. .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
  2565. .info = snd_hda_spdif_in_switch_info,
  2566. .get = snd_hda_spdif_in_switch_get,
  2567. .put = snd_hda_spdif_in_switch_put,
  2568. },
  2569. {
  2570. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  2571. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2572. .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
  2573. .info = snd_hda_spdif_mask_info,
  2574. .get = snd_hda_spdif_in_status_get,
  2575. },
  2576. { } /* end */
  2577. };
  2578. /**
  2579. * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
  2580. * @codec: the HDA codec
  2581. * @nid: audio in widget NID
  2582. *
  2583. * Creates controls related with the SPDIF input.
  2584. * Called from each patch supporting the SPDIF in.
  2585. *
  2586. * Returns 0 if successful, or a negative error code.
  2587. */
  2588. int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
  2589. {
  2590. int err;
  2591. struct snd_kcontrol *kctl;
  2592. struct snd_kcontrol_new *dig_mix;
  2593. int idx;
  2594. idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
  2595. if (idx < 0) {
  2596. codec_err(codec, "too many IEC958 inputs\n");
  2597. return -EBUSY;
  2598. }
  2599. for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
  2600. kctl = snd_ctl_new1(dig_mix, codec);
  2601. if (!kctl)
  2602. return -ENOMEM;
  2603. kctl->private_value = nid;
  2604. err = snd_hda_ctl_add(codec, nid, kctl);
  2605. if (err < 0)
  2606. return err;
  2607. }
  2608. codec->spdif_in_enable =
  2609. snd_hda_codec_read(codec, nid, 0,
  2610. AC_VERB_GET_DIGI_CONVERT_1, 0) &
  2611. AC_DIG1_ENABLE;
  2612. return 0;
  2613. }
  2614. EXPORT_SYMBOL_GPL(snd_hda_create_spdif_in_ctls);
  2615. /**
  2616. * snd_hda_codec_set_power_to_all - Set the power state to all widgets
  2617. * @codec: the HDA codec
  2618. * @fg: function group (not used now)
  2619. * @power_state: the power state to set (AC_PWRST_*)
  2620. *
  2621. * Set the given power state to all widgets that have the power control.
  2622. * If the codec has power_filter set, it evaluates the power state and
  2623. * filter out if it's unchanged as D3.
  2624. */
  2625. void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
  2626. unsigned int power_state)
  2627. {
  2628. hda_nid_t nid;
  2629. for_each_hda_codec_node(nid, codec) {
  2630. unsigned int wcaps = get_wcaps(codec, nid);
  2631. unsigned int state = power_state;
  2632. if (!(wcaps & AC_WCAP_POWER))
  2633. continue;
  2634. if (codec->power_filter) {
  2635. state = codec->power_filter(codec, nid, power_state);
  2636. if (state != power_state && power_state == AC_PWRST_D3)
  2637. continue;
  2638. }
  2639. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
  2640. state);
  2641. }
  2642. }
  2643. EXPORT_SYMBOL_GPL(snd_hda_codec_set_power_to_all);
  2644. /*
  2645. * wait until the state is reached, returns the current state
  2646. */
  2647. static unsigned int hda_sync_power_state(struct hda_codec *codec,
  2648. hda_nid_t fg,
  2649. unsigned int power_state)
  2650. {
  2651. unsigned long end_time = jiffies + msecs_to_jiffies(500);
  2652. unsigned int state, actual_state;
  2653. for (;;) {
  2654. state = snd_hda_codec_read(codec, fg, 0,
  2655. AC_VERB_GET_POWER_STATE, 0);
  2656. if (state & AC_PWRST_ERROR)
  2657. break;
  2658. actual_state = (state >> 4) & 0x0f;
  2659. if (actual_state == power_state)
  2660. break;
  2661. if (time_after_eq(jiffies, end_time))
  2662. break;
  2663. /* wait until the codec reachs to the target state */
  2664. msleep(1);
  2665. }
  2666. return state;
  2667. }
  2668. /**
  2669. * snd_hda_codec_eapd_power_filter - A power filter callback for EAPD
  2670. * @codec: the HDA codec
  2671. * @nid: widget NID
  2672. * @power_state: power state to evalue
  2673. *
  2674. * Don't power down the widget if it controls eapd and EAPD_BTLENABLE is set.
  2675. * This can be used a codec power_filter callback.
  2676. */
  2677. unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec,
  2678. hda_nid_t nid,
  2679. unsigned int power_state)
  2680. {
  2681. if (nid == codec->core.afg || nid == codec->core.mfg)
  2682. return power_state;
  2683. if (power_state == AC_PWRST_D3 &&
  2684. get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN &&
  2685. (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
  2686. int eapd = snd_hda_codec_read(codec, nid, 0,
  2687. AC_VERB_GET_EAPD_BTLENABLE, 0);
  2688. if (eapd & 0x02)
  2689. return AC_PWRST_D0;
  2690. }
  2691. return power_state;
  2692. }
  2693. EXPORT_SYMBOL_GPL(snd_hda_codec_eapd_power_filter);
  2694. /*
  2695. * set power state of the codec, and return the power state
  2696. */
  2697. static unsigned int hda_set_power_state(struct hda_codec *codec,
  2698. unsigned int power_state)
  2699. {
  2700. hda_nid_t fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
  2701. int count;
  2702. unsigned int state;
  2703. int flags = 0;
  2704. /* this delay seems necessary to avoid click noise at power-down */
  2705. if (power_state == AC_PWRST_D3) {
  2706. if (codec->depop_delay < 0)
  2707. msleep(codec_has_epss(codec) ? 10 : 100);
  2708. else if (codec->depop_delay > 0)
  2709. msleep(codec->depop_delay);
  2710. flags = HDA_RW_NO_RESPONSE_FALLBACK;
  2711. }
  2712. /* repeat power states setting at most 10 times*/
  2713. for (count = 0; count < 10; count++) {
  2714. if (codec->patch_ops.set_power_state)
  2715. codec->patch_ops.set_power_state(codec, fg,
  2716. power_state);
  2717. else {
  2718. state = power_state;
  2719. if (codec->power_filter)
  2720. state = codec->power_filter(codec, fg, state);
  2721. if (state == power_state || power_state != AC_PWRST_D3)
  2722. snd_hda_codec_read(codec, fg, flags,
  2723. AC_VERB_SET_POWER_STATE,
  2724. state);
  2725. snd_hda_codec_set_power_to_all(codec, fg, power_state);
  2726. }
  2727. state = hda_sync_power_state(codec, fg, power_state);
  2728. if (!(state & AC_PWRST_ERROR))
  2729. break;
  2730. }
  2731. return state;
  2732. }
  2733. /* sync power states of all widgets;
  2734. * this is called at the end of codec parsing
  2735. */
  2736. static void sync_power_up_states(struct hda_codec *codec)
  2737. {
  2738. hda_nid_t nid;
  2739. /* don't care if no filter is used */
  2740. if (!codec->power_filter)
  2741. return;
  2742. for_each_hda_codec_node(nid, codec) {
  2743. unsigned int wcaps = get_wcaps(codec, nid);
  2744. unsigned int target;
  2745. if (!(wcaps & AC_WCAP_POWER))
  2746. continue;
  2747. target = codec->power_filter(codec, nid, AC_PWRST_D0);
  2748. if (target == AC_PWRST_D0)
  2749. continue;
  2750. if (!snd_hda_check_power_state(codec, nid, target))
  2751. snd_hda_codec_write(codec, nid, 0,
  2752. AC_VERB_SET_POWER_STATE, target);
  2753. }
  2754. }
  2755. #ifdef CONFIG_SND_HDA_RECONFIG
  2756. /* execute additional init verbs */
  2757. static void hda_exec_init_verbs(struct hda_codec *codec)
  2758. {
  2759. if (codec->init_verbs.list)
  2760. snd_hda_sequence_write(codec, codec->init_verbs.list);
  2761. }
  2762. #else
  2763. static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
  2764. #endif
  2765. #ifdef CONFIG_PM
  2766. /* update the power on/off account with the current jiffies */
  2767. static void update_power_acct(struct hda_codec *codec, bool on)
  2768. {
  2769. unsigned long delta = jiffies - codec->power_jiffies;
  2770. if (on)
  2771. codec->power_on_acct += delta;
  2772. else
  2773. codec->power_off_acct += delta;
  2774. codec->power_jiffies += delta;
  2775. }
  2776. void snd_hda_update_power_acct(struct hda_codec *codec)
  2777. {
  2778. update_power_acct(codec, hda_codec_is_power_on(codec));
  2779. }
  2780. /*
  2781. * call suspend and power-down; used both from PM and power-save
  2782. * this function returns the power state in the end
  2783. */
  2784. static unsigned int hda_call_codec_suspend(struct hda_codec *codec)
  2785. {
  2786. unsigned int state;
  2787. atomic_inc(&codec->core.in_pm);
  2788. if (codec->patch_ops.suspend)
  2789. codec->patch_ops.suspend(codec);
  2790. hda_cleanup_all_streams(codec);
  2791. state = hda_set_power_state(codec, AC_PWRST_D3);
  2792. update_power_acct(codec, true);
  2793. atomic_dec(&codec->core.in_pm);
  2794. return state;
  2795. }
  2796. /*
  2797. * kick up codec; used both from PM and power-save
  2798. */
  2799. static void hda_call_codec_resume(struct hda_codec *codec)
  2800. {
  2801. atomic_inc(&codec->core.in_pm);
  2802. if (codec->core.regmap)
  2803. regcache_mark_dirty(codec->core.regmap);
  2804. codec->power_jiffies = jiffies;
  2805. hda_set_power_state(codec, AC_PWRST_D0);
  2806. restore_shutup_pins(codec);
  2807. hda_exec_init_verbs(codec);
  2808. snd_hda_jack_set_dirty_all(codec);
  2809. if (codec->patch_ops.resume)
  2810. codec->patch_ops.resume(codec);
  2811. else {
  2812. if (codec->patch_ops.init)
  2813. codec->patch_ops.init(codec);
  2814. if (codec->core.regmap)
  2815. regcache_sync(codec->core.regmap);
  2816. }
  2817. if (codec->jackpoll_interval)
  2818. hda_jackpoll_work(&codec->jackpoll_work.work);
  2819. else
  2820. snd_hda_jack_report_sync(codec);
  2821. atomic_dec(&codec->core.in_pm);
  2822. }
  2823. static int hda_codec_runtime_suspend(struct device *dev)
  2824. {
  2825. struct hda_codec *codec = dev_to_hda_codec(dev);
  2826. struct hda_pcm *pcm;
  2827. unsigned int state;
  2828. cancel_delayed_work_sync(&codec->jackpoll_work);
  2829. list_for_each_entry(pcm, &codec->pcm_list_head, list)
  2830. snd_pcm_suspend_all(pcm->pcm);
  2831. state = hda_call_codec_suspend(codec);
  2832. if (codec_has_clkstop(codec) && codec_has_epss(codec) &&
  2833. (state & AC_PWRST_CLK_STOP_OK))
  2834. snd_hdac_codec_link_down(&codec->core);
  2835. snd_hdac_link_power(&codec->core, false);
  2836. return 0;
  2837. }
  2838. static int hda_codec_runtime_resume(struct device *dev)
  2839. {
  2840. struct hda_codec *codec = dev_to_hda_codec(dev);
  2841. snd_hdac_link_power(&codec->core, true);
  2842. snd_hdac_codec_link_up(&codec->core);
  2843. hda_call_codec_resume(codec);
  2844. pm_runtime_mark_last_busy(dev);
  2845. return 0;
  2846. }
  2847. #endif /* CONFIG_PM */
  2848. /* referred in hda_bind.c */
  2849. const struct dev_pm_ops hda_codec_driver_pm = {
  2850. SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  2851. pm_runtime_force_resume)
  2852. SET_RUNTIME_PM_OPS(hda_codec_runtime_suspend, hda_codec_runtime_resume,
  2853. NULL)
  2854. };
  2855. /*
  2856. * add standard channel maps if not specified
  2857. */
  2858. static int add_std_chmaps(struct hda_codec *codec)
  2859. {
  2860. struct hda_pcm *pcm;
  2861. int str, err;
  2862. list_for_each_entry(pcm, &codec->pcm_list_head, list) {
  2863. for (str = 0; str < 2; str++) {
  2864. struct hda_pcm_stream *hinfo = &pcm->stream[str];
  2865. struct snd_pcm_chmap *chmap;
  2866. const struct snd_pcm_chmap_elem *elem;
  2867. if (!pcm || pcm->own_chmap ||
  2868. !hinfo->substreams)
  2869. continue;
  2870. elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
  2871. err = snd_pcm_add_chmap_ctls(pcm->pcm, str, elem,
  2872. hinfo->channels_max,
  2873. 0, &chmap);
  2874. if (err < 0)
  2875. return err;
  2876. chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
  2877. }
  2878. }
  2879. return 0;
  2880. }
  2881. /* default channel maps for 2.1 speakers;
  2882. * since HD-audio supports only stereo, odd number channels are omitted
  2883. */
  2884. const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
  2885. { .channels = 2,
  2886. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
  2887. { .channels = 4,
  2888. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
  2889. SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
  2890. { }
  2891. };
  2892. EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
  2893. int snd_hda_codec_build_controls(struct hda_codec *codec)
  2894. {
  2895. int err = 0;
  2896. hda_exec_init_verbs(codec);
  2897. /* continue to initialize... */
  2898. if (codec->patch_ops.init)
  2899. err = codec->patch_ops.init(codec);
  2900. if (!err && codec->patch_ops.build_controls)
  2901. err = codec->patch_ops.build_controls(codec);
  2902. if (err < 0)
  2903. return err;
  2904. /* we create chmaps here instead of build_pcms */
  2905. err = add_std_chmaps(codec);
  2906. if (err < 0)
  2907. return err;
  2908. if (codec->jackpoll_interval)
  2909. hda_jackpoll_work(&codec->jackpoll_work.work);
  2910. else
  2911. snd_hda_jack_report_sync(codec); /* call at the last init point */
  2912. sync_power_up_states(codec);
  2913. return 0;
  2914. }
  2915. /*
  2916. * PCM stuff
  2917. */
  2918. static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
  2919. struct hda_codec *codec,
  2920. struct snd_pcm_substream *substream)
  2921. {
  2922. return 0;
  2923. }
  2924. static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
  2925. struct hda_codec *codec,
  2926. unsigned int stream_tag,
  2927. unsigned int format,
  2928. struct snd_pcm_substream *substream)
  2929. {
  2930. snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
  2931. return 0;
  2932. }
  2933. static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
  2934. struct hda_codec *codec,
  2935. struct snd_pcm_substream *substream)
  2936. {
  2937. snd_hda_codec_cleanup_stream(codec, hinfo->nid);
  2938. return 0;
  2939. }
  2940. static int set_pcm_default_values(struct hda_codec *codec,
  2941. struct hda_pcm_stream *info)
  2942. {
  2943. int err;
  2944. /* query support PCM information from the given NID */
  2945. if (info->nid && (!info->rates || !info->formats)) {
  2946. err = snd_hda_query_supported_pcm(codec, info->nid,
  2947. info->rates ? NULL : &info->rates,
  2948. info->formats ? NULL : &info->formats,
  2949. info->maxbps ? NULL : &info->maxbps);
  2950. if (err < 0)
  2951. return err;
  2952. }
  2953. if (info->ops.open == NULL)
  2954. info->ops.open = hda_pcm_default_open_close;
  2955. if (info->ops.close == NULL)
  2956. info->ops.close = hda_pcm_default_open_close;
  2957. if (info->ops.prepare == NULL) {
  2958. if (snd_BUG_ON(!info->nid))
  2959. return -EINVAL;
  2960. info->ops.prepare = hda_pcm_default_prepare;
  2961. }
  2962. if (info->ops.cleanup == NULL) {
  2963. if (snd_BUG_ON(!info->nid))
  2964. return -EINVAL;
  2965. info->ops.cleanup = hda_pcm_default_cleanup;
  2966. }
  2967. return 0;
  2968. }
  2969. /*
  2970. * codec prepare/cleanup entries
  2971. */
  2972. /**
  2973. * snd_hda_codec_prepare - Prepare a stream
  2974. * @codec: the HDA codec
  2975. * @hinfo: PCM information
  2976. * @stream: stream tag to assign
  2977. * @format: format id to assign
  2978. * @substream: PCM substream to assign
  2979. *
  2980. * Calls the prepare callback set by the codec with the given arguments.
  2981. * Clean up the inactive streams when successful.
  2982. */
  2983. int snd_hda_codec_prepare(struct hda_codec *codec,
  2984. struct hda_pcm_stream *hinfo,
  2985. unsigned int stream,
  2986. unsigned int format,
  2987. struct snd_pcm_substream *substream)
  2988. {
  2989. int ret;
  2990. mutex_lock(&codec->bus->prepare_mutex);
  2991. if (hinfo->ops.prepare)
  2992. ret = hinfo->ops.prepare(hinfo, codec, stream, format,
  2993. substream);
  2994. else
  2995. ret = -ENODEV;
  2996. if (ret >= 0)
  2997. purify_inactive_streams(codec);
  2998. mutex_unlock(&codec->bus->prepare_mutex);
  2999. return ret;
  3000. }
  3001. EXPORT_SYMBOL_GPL(snd_hda_codec_prepare);
  3002. /**
  3003. * snd_hda_codec_cleanup - Prepare a stream
  3004. * @codec: the HDA codec
  3005. * @hinfo: PCM information
  3006. * @substream: PCM substream
  3007. *
  3008. * Calls the cleanup callback set by the codec with the given arguments.
  3009. */
  3010. void snd_hda_codec_cleanup(struct hda_codec *codec,
  3011. struct hda_pcm_stream *hinfo,
  3012. struct snd_pcm_substream *substream)
  3013. {
  3014. mutex_lock(&codec->bus->prepare_mutex);
  3015. if (hinfo->ops.cleanup)
  3016. hinfo->ops.cleanup(hinfo, codec, substream);
  3017. mutex_unlock(&codec->bus->prepare_mutex);
  3018. }
  3019. EXPORT_SYMBOL_GPL(snd_hda_codec_cleanup);
  3020. /* global */
  3021. const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
  3022. "Audio", "SPDIF", "HDMI", "Modem"
  3023. };
  3024. /*
  3025. * get the empty PCM device number to assign
  3026. */
  3027. static int get_empty_pcm_device(struct hda_bus *bus, unsigned int type)
  3028. {
  3029. /* audio device indices; not linear to keep compatibility */
  3030. /* assigned to static slots up to dev#10; if more needed, assign
  3031. * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y)
  3032. */
  3033. static int audio_idx[HDA_PCM_NTYPES][5] = {
  3034. [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
  3035. [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
  3036. [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
  3037. [HDA_PCM_TYPE_MODEM] = { 6, -1 },
  3038. };
  3039. int i;
  3040. if (type >= HDA_PCM_NTYPES) {
  3041. dev_err(bus->card->dev, "Invalid PCM type %d\n", type);
  3042. return -EINVAL;
  3043. }
  3044. for (i = 0; audio_idx[type][i] >= 0; i++) {
  3045. #ifndef CONFIG_SND_DYNAMIC_MINORS
  3046. if (audio_idx[type][i] >= 8)
  3047. break;
  3048. #endif
  3049. if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
  3050. return audio_idx[type][i];
  3051. }
  3052. #ifdef CONFIG_SND_DYNAMIC_MINORS
  3053. /* non-fixed slots starting from 10 */
  3054. for (i = 10; i < 32; i++) {
  3055. if (!test_and_set_bit(i, bus->pcm_dev_bits))
  3056. return i;
  3057. }
  3058. #endif
  3059. dev_warn(bus->card->dev, "Too many %s devices\n",
  3060. snd_hda_pcm_type_name[type]);
  3061. #ifndef CONFIG_SND_DYNAMIC_MINORS
  3062. dev_warn(bus->card->dev,
  3063. "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n");
  3064. #endif
  3065. return -EAGAIN;
  3066. }
  3067. /* call build_pcms ops of the given codec and set up the default parameters */
  3068. int snd_hda_codec_parse_pcms(struct hda_codec *codec)
  3069. {
  3070. struct hda_pcm *cpcm;
  3071. int err;
  3072. if (!list_empty(&codec->pcm_list_head))
  3073. return 0; /* already parsed */
  3074. if (!codec->patch_ops.build_pcms)
  3075. return 0;
  3076. err = codec->patch_ops.build_pcms(codec);
  3077. if (err < 0) {
  3078. codec_err(codec, "cannot build PCMs for #%d (error %d)\n",
  3079. codec->core.addr, err);
  3080. return err;
  3081. }
  3082. list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
  3083. int stream;
  3084. for (stream = 0; stream < 2; stream++) {
  3085. struct hda_pcm_stream *info = &cpcm->stream[stream];
  3086. if (!info->substreams)
  3087. continue;
  3088. err = set_pcm_default_values(codec, info);
  3089. if (err < 0) {
  3090. codec_warn(codec,
  3091. "fail to setup default for PCM %s\n",
  3092. cpcm->name);
  3093. return err;
  3094. }
  3095. }
  3096. }
  3097. return 0;
  3098. }
  3099. /* assign all PCMs of the given codec */
  3100. int snd_hda_codec_build_pcms(struct hda_codec *codec)
  3101. {
  3102. struct hda_bus *bus = codec->bus;
  3103. struct hda_pcm *cpcm;
  3104. int dev, err;
  3105. err = snd_hda_codec_parse_pcms(codec);
  3106. if (err < 0) {
  3107. snd_hda_codec_reset(codec);
  3108. return err;
  3109. }
  3110. /* attach a new PCM streams */
  3111. list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
  3112. if (cpcm->pcm)
  3113. continue; /* already attached */
  3114. if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
  3115. continue; /* no substreams assigned */
  3116. dev = get_empty_pcm_device(bus, cpcm->pcm_type);
  3117. if (dev < 0)
  3118. continue; /* no fatal error */
  3119. cpcm->device = dev;
  3120. err = snd_hda_attach_pcm_stream(bus, codec, cpcm);
  3121. if (err < 0) {
  3122. codec_err(codec,
  3123. "cannot attach PCM stream %d for codec #%d\n",
  3124. dev, codec->core.addr);
  3125. continue; /* no fatal error */
  3126. }
  3127. }
  3128. return 0;
  3129. }
  3130. /**
  3131. * snd_hda_add_new_ctls - create controls from the array
  3132. * @codec: the HDA codec
  3133. * @knew: the array of struct snd_kcontrol_new
  3134. *
  3135. * This helper function creates and add new controls in the given array.
  3136. * The array must be terminated with an empty entry as terminator.
  3137. *
  3138. * Returns 0 if successful, or a negative error code.
  3139. */
  3140. int snd_hda_add_new_ctls(struct hda_codec *codec,
  3141. const struct snd_kcontrol_new *knew)
  3142. {
  3143. int err;
  3144. for (; knew->name; knew++) {
  3145. struct snd_kcontrol *kctl;
  3146. int addr = 0, idx = 0;
  3147. if (knew->iface == -1) /* skip this codec private value */
  3148. continue;
  3149. for (;;) {
  3150. kctl = snd_ctl_new1(knew, codec);
  3151. if (!kctl)
  3152. return -ENOMEM;
  3153. if (addr > 0)
  3154. kctl->id.device = addr;
  3155. if (idx > 0)
  3156. kctl->id.index = idx;
  3157. err = snd_hda_ctl_add(codec, 0, kctl);
  3158. if (!err)
  3159. break;
  3160. /* try first with another device index corresponding to
  3161. * the codec addr; if it still fails (or it's the
  3162. * primary codec), then try another control index
  3163. */
  3164. if (!addr && codec->core.addr)
  3165. addr = codec->core.addr;
  3166. else if (!idx && !knew->index) {
  3167. idx = find_empty_mixer_ctl_idx(codec,
  3168. knew->name, 0);
  3169. if (idx <= 0)
  3170. return err;
  3171. } else
  3172. return err;
  3173. }
  3174. }
  3175. return 0;
  3176. }
  3177. EXPORT_SYMBOL_GPL(snd_hda_add_new_ctls);
  3178. #ifdef CONFIG_PM
  3179. static void codec_set_power_save(struct hda_codec *codec, int delay)
  3180. {
  3181. struct device *dev = hda_codec_dev(codec);
  3182. if (delay == 0 && codec->auto_runtime_pm)
  3183. delay = 3000;
  3184. if (delay > 0) {
  3185. pm_runtime_set_autosuspend_delay(dev, delay);
  3186. pm_runtime_use_autosuspend(dev);
  3187. pm_runtime_allow(dev);
  3188. if (!pm_runtime_suspended(dev))
  3189. pm_runtime_mark_last_busy(dev);
  3190. } else {
  3191. pm_runtime_dont_use_autosuspend(dev);
  3192. pm_runtime_forbid(dev);
  3193. }
  3194. }
  3195. /**
  3196. * snd_hda_set_power_save - reprogram autosuspend for the given delay
  3197. * @bus: HD-audio bus
  3198. * @delay: autosuspend delay in msec, 0 = off
  3199. *
  3200. * Synchronize the runtime PM autosuspend state from the power_save option.
  3201. */
  3202. void snd_hda_set_power_save(struct hda_bus *bus, int delay)
  3203. {
  3204. struct hda_codec *c;
  3205. list_for_each_codec(c, bus)
  3206. codec_set_power_save(c, delay);
  3207. }
  3208. EXPORT_SYMBOL_GPL(snd_hda_set_power_save);
  3209. /**
  3210. * snd_hda_check_amp_list_power - Check the amp list and update the power
  3211. * @codec: HD-audio codec
  3212. * @check: the object containing an AMP list and the status
  3213. * @nid: NID to check / update
  3214. *
  3215. * Check whether the given NID is in the amp list. If it's in the list,
  3216. * check the current AMP status, and update the the power-status according
  3217. * to the mute status.
  3218. *
  3219. * This function is supposed to be set or called from the check_power_status
  3220. * patch ops.
  3221. */
  3222. int snd_hda_check_amp_list_power(struct hda_codec *codec,
  3223. struct hda_loopback_check *check,
  3224. hda_nid_t nid)
  3225. {
  3226. const struct hda_amp_list *p;
  3227. int ch, v;
  3228. if (!check->amplist)
  3229. return 0;
  3230. for (p = check->amplist; p->nid; p++) {
  3231. if (p->nid == nid)
  3232. break;
  3233. }
  3234. if (!p->nid)
  3235. return 0; /* nothing changed */
  3236. for (p = check->amplist; p->nid; p++) {
  3237. for (ch = 0; ch < 2; ch++) {
  3238. v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
  3239. p->idx);
  3240. if (!(v & HDA_AMP_MUTE) && v > 0) {
  3241. if (!check->power_on) {
  3242. check->power_on = 1;
  3243. snd_hda_power_up_pm(codec);
  3244. }
  3245. return 1;
  3246. }
  3247. }
  3248. }
  3249. if (check->power_on) {
  3250. check->power_on = 0;
  3251. snd_hda_power_down_pm(codec);
  3252. }
  3253. return 0;
  3254. }
  3255. EXPORT_SYMBOL_GPL(snd_hda_check_amp_list_power);
  3256. #endif
  3257. /*
  3258. * input MUX helper
  3259. */
  3260. /**
  3261. * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
  3262. * @imux: imux helper object
  3263. * @uinfo: pointer to get/store the data
  3264. */
  3265. int snd_hda_input_mux_info(const struct hda_input_mux *imux,
  3266. struct snd_ctl_elem_info *uinfo)
  3267. {
  3268. unsigned int index;
  3269. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  3270. uinfo->count = 1;
  3271. uinfo->value.enumerated.items = imux->num_items;
  3272. if (!imux->num_items)
  3273. return 0;
  3274. index = uinfo->value.enumerated.item;
  3275. if (index >= imux->num_items)
  3276. index = imux->num_items - 1;
  3277. strcpy(uinfo->value.enumerated.name, imux->items[index].label);
  3278. return 0;
  3279. }
  3280. EXPORT_SYMBOL_GPL(snd_hda_input_mux_info);
  3281. /**
  3282. * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
  3283. * @codec: the HDA codec
  3284. * @imux: imux helper object
  3285. * @ucontrol: pointer to get/store the data
  3286. * @nid: input mux NID
  3287. * @cur_val: pointer to get/store the current imux value
  3288. */
  3289. int snd_hda_input_mux_put(struct hda_codec *codec,
  3290. const struct hda_input_mux *imux,
  3291. struct snd_ctl_elem_value *ucontrol,
  3292. hda_nid_t nid,
  3293. unsigned int *cur_val)
  3294. {
  3295. unsigned int idx;
  3296. if (!imux->num_items)
  3297. return 0;
  3298. idx = ucontrol->value.enumerated.item[0];
  3299. if (idx >= imux->num_items)
  3300. idx = imux->num_items - 1;
  3301. if (*cur_val == idx)
  3302. return 0;
  3303. snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
  3304. imux->items[idx].index);
  3305. *cur_val = idx;
  3306. return 1;
  3307. }
  3308. EXPORT_SYMBOL_GPL(snd_hda_input_mux_put);
  3309. /**
  3310. * snd_hda_enum_helper_info - Helper for simple enum ctls
  3311. * @kcontrol: ctl element
  3312. * @uinfo: pointer to get/store the data
  3313. * @num_items: number of enum items
  3314. * @texts: enum item string array
  3315. *
  3316. * process kcontrol info callback of a simple string enum array
  3317. * when @num_items is 0 or @texts is NULL, assume a boolean enum array
  3318. */
  3319. int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
  3320. struct snd_ctl_elem_info *uinfo,
  3321. int num_items, const char * const *texts)
  3322. {
  3323. static const char * const texts_default[] = {
  3324. "Disabled", "Enabled"
  3325. };
  3326. if (!texts || !num_items) {
  3327. num_items = 2;
  3328. texts = texts_default;
  3329. }
  3330. return snd_ctl_enum_info(uinfo, 1, num_items, texts);
  3331. }
  3332. EXPORT_SYMBOL_GPL(snd_hda_enum_helper_info);
  3333. /*
  3334. * Multi-channel / digital-out PCM helper functions
  3335. */
  3336. /* setup SPDIF output stream */
  3337. static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
  3338. unsigned int stream_tag, unsigned int format)
  3339. {
  3340. struct hda_spdif_out *spdif;
  3341. unsigned int curr_fmt;
  3342. bool reset;
  3343. spdif = snd_hda_spdif_out_of_nid(codec, nid);
  3344. curr_fmt = snd_hda_codec_read(codec, nid, 0,
  3345. AC_VERB_GET_STREAM_FORMAT, 0);
  3346. reset = codec->spdif_status_reset &&
  3347. (spdif->ctls & AC_DIG1_ENABLE) &&
  3348. curr_fmt != format;
  3349. /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
  3350. updated */
  3351. if (reset)
  3352. set_dig_out_convert(codec, nid,
  3353. spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
  3354. -1);
  3355. snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
  3356. if (codec->slave_dig_outs) {
  3357. const hda_nid_t *d;
  3358. for (d = codec->slave_dig_outs; *d; d++)
  3359. snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
  3360. format);
  3361. }
  3362. /* turn on again (if needed) */
  3363. if (reset)
  3364. set_dig_out_convert(codec, nid,
  3365. spdif->ctls & 0xff, -1);
  3366. }
  3367. static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
  3368. {
  3369. snd_hda_codec_cleanup_stream(codec, nid);
  3370. if (codec->slave_dig_outs) {
  3371. const hda_nid_t *d;
  3372. for (d = codec->slave_dig_outs; *d; d++)
  3373. snd_hda_codec_cleanup_stream(codec, *d);
  3374. }
  3375. }
  3376. /**
  3377. * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
  3378. * @codec: the HDA codec
  3379. * @mout: hda_multi_out object
  3380. */
  3381. int snd_hda_multi_out_dig_open(struct hda_codec *codec,
  3382. struct hda_multi_out *mout)
  3383. {
  3384. mutex_lock(&codec->spdif_mutex);
  3385. if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
  3386. /* already opened as analog dup; reset it once */
  3387. cleanup_dig_out_stream(codec, mout->dig_out_nid);
  3388. mout->dig_out_used = HDA_DIG_EXCLUSIVE;
  3389. mutex_unlock(&codec->spdif_mutex);
  3390. return 0;
  3391. }
  3392. EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_open);
  3393. /**
  3394. * snd_hda_multi_out_dig_prepare - prepare the digital out stream
  3395. * @codec: the HDA codec
  3396. * @mout: hda_multi_out object
  3397. * @stream_tag: stream tag to assign
  3398. * @format: format id to assign
  3399. * @substream: PCM substream to assign
  3400. */
  3401. int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
  3402. struct hda_multi_out *mout,
  3403. unsigned int stream_tag,
  3404. unsigned int format,
  3405. struct snd_pcm_substream *substream)
  3406. {
  3407. mutex_lock(&codec->spdif_mutex);
  3408. setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
  3409. mutex_unlock(&codec->spdif_mutex);
  3410. return 0;
  3411. }
  3412. EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_prepare);
  3413. /**
  3414. * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
  3415. * @codec: the HDA codec
  3416. * @mout: hda_multi_out object
  3417. */
  3418. int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
  3419. struct hda_multi_out *mout)
  3420. {
  3421. mutex_lock(&codec->spdif_mutex);
  3422. cleanup_dig_out_stream(codec, mout->dig_out_nid);
  3423. mutex_unlock(&codec->spdif_mutex);
  3424. return 0;
  3425. }
  3426. EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_cleanup);
  3427. /**
  3428. * snd_hda_multi_out_dig_close - release the digital out stream
  3429. * @codec: the HDA codec
  3430. * @mout: hda_multi_out object
  3431. */
  3432. int snd_hda_multi_out_dig_close(struct hda_codec *codec,
  3433. struct hda_multi_out *mout)
  3434. {
  3435. mutex_lock(&codec->spdif_mutex);
  3436. mout->dig_out_used = 0;
  3437. mutex_unlock(&codec->spdif_mutex);
  3438. return 0;
  3439. }
  3440. EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_close);
  3441. /**
  3442. * snd_hda_multi_out_analog_open - open analog outputs
  3443. * @codec: the HDA codec
  3444. * @mout: hda_multi_out object
  3445. * @substream: PCM substream to assign
  3446. * @hinfo: PCM information to assign
  3447. *
  3448. * Open analog outputs and set up the hw-constraints.
  3449. * If the digital outputs can be opened as slave, open the digital
  3450. * outputs, too.
  3451. */
  3452. int snd_hda_multi_out_analog_open(struct hda_codec *codec,
  3453. struct hda_multi_out *mout,
  3454. struct snd_pcm_substream *substream,
  3455. struct hda_pcm_stream *hinfo)
  3456. {
  3457. struct snd_pcm_runtime *runtime = substream->runtime;
  3458. runtime->hw.channels_max = mout->max_channels;
  3459. if (mout->dig_out_nid) {
  3460. if (!mout->analog_rates) {
  3461. mout->analog_rates = hinfo->rates;
  3462. mout->analog_formats = hinfo->formats;
  3463. mout->analog_maxbps = hinfo->maxbps;
  3464. } else {
  3465. runtime->hw.rates = mout->analog_rates;
  3466. runtime->hw.formats = mout->analog_formats;
  3467. hinfo->maxbps = mout->analog_maxbps;
  3468. }
  3469. if (!mout->spdif_rates) {
  3470. snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
  3471. &mout->spdif_rates,
  3472. &mout->spdif_formats,
  3473. &mout->spdif_maxbps);
  3474. }
  3475. mutex_lock(&codec->spdif_mutex);
  3476. if (mout->share_spdif) {
  3477. if ((runtime->hw.rates & mout->spdif_rates) &&
  3478. (runtime->hw.formats & mout->spdif_formats)) {
  3479. runtime->hw.rates &= mout->spdif_rates;
  3480. runtime->hw.formats &= mout->spdif_formats;
  3481. if (mout->spdif_maxbps < hinfo->maxbps)
  3482. hinfo->maxbps = mout->spdif_maxbps;
  3483. } else {
  3484. mout->share_spdif = 0;
  3485. /* FIXME: need notify? */
  3486. }
  3487. }
  3488. mutex_unlock(&codec->spdif_mutex);
  3489. }
  3490. return snd_pcm_hw_constraint_step(substream->runtime, 0,
  3491. SNDRV_PCM_HW_PARAM_CHANNELS, 2);
  3492. }
  3493. EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_open);
  3494. /**
  3495. * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
  3496. * @codec: the HDA codec
  3497. * @mout: hda_multi_out object
  3498. * @stream_tag: stream tag to assign
  3499. * @format: format id to assign
  3500. * @substream: PCM substream to assign
  3501. *
  3502. * Set up the i/o for analog out.
  3503. * When the digital out is available, copy the front out to digital out, too.
  3504. */
  3505. int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
  3506. struct hda_multi_out *mout,
  3507. unsigned int stream_tag,
  3508. unsigned int format,
  3509. struct snd_pcm_substream *substream)
  3510. {
  3511. const hda_nid_t *nids = mout->dac_nids;
  3512. int chs = substream->runtime->channels;
  3513. struct hda_spdif_out *spdif;
  3514. int i;
  3515. mutex_lock(&codec->spdif_mutex);
  3516. spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
  3517. if (mout->dig_out_nid && mout->share_spdif &&
  3518. mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
  3519. if (chs == 2 &&
  3520. snd_hda_is_supported_format(codec, mout->dig_out_nid,
  3521. format) &&
  3522. !(spdif->status & IEC958_AES0_NONAUDIO)) {
  3523. mout->dig_out_used = HDA_DIG_ANALOG_DUP;
  3524. setup_dig_out_stream(codec, mout->dig_out_nid,
  3525. stream_tag, format);
  3526. } else {
  3527. mout->dig_out_used = 0;
  3528. cleanup_dig_out_stream(codec, mout->dig_out_nid);
  3529. }
  3530. }
  3531. mutex_unlock(&codec->spdif_mutex);
  3532. /* front */
  3533. snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
  3534. 0, format);
  3535. if (!mout->no_share_stream &&
  3536. mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
  3537. /* headphone out will just decode front left/right (stereo) */
  3538. snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
  3539. 0, format);
  3540. /* extra outputs copied from front */
  3541. for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
  3542. if (!mout->no_share_stream && mout->hp_out_nid[i])
  3543. snd_hda_codec_setup_stream(codec,
  3544. mout->hp_out_nid[i],
  3545. stream_tag, 0, format);
  3546. /* surrounds */
  3547. for (i = 1; i < mout->num_dacs; i++) {
  3548. if (chs >= (i + 1) * 2) /* independent out */
  3549. snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
  3550. i * 2, format);
  3551. else if (!mout->no_share_stream) /* copy front */
  3552. snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
  3553. 0, format);
  3554. }
  3555. /* extra surrounds */
  3556. for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) {
  3557. int ch = 0;
  3558. if (!mout->extra_out_nid[i])
  3559. break;
  3560. if (chs >= (i + 1) * 2)
  3561. ch = i * 2;
  3562. else if (!mout->no_share_stream)
  3563. break;
  3564. snd_hda_codec_setup_stream(codec, mout->extra_out_nid[i],
  3565. stream_tag, ch, format);
  3566. }
  3567. return 0;
  3568. }
  3569. EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_prepare);
  3570. /**
  3571. * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
  3572. * @codec: the HDA codec
  3573. * @mout: hda_multi_out object
  3574. */
  3575. int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
  3576. struct hda_multi_out *mout)
  3577. {
  3578. const hda_nid_t *nids = mout->dac_nids;
  3579. int i;
  3580. for (i = 0; i < mout->num_dacs; i++)
  3581. snd_hda_codec_cleanup_stream(codec, nids[i]);
  3582. if (mout->hp_nid)
  3583. snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
  3584. for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
  3585. if (mout->hp_out_nid[i])
  3586. snd_hda_codec_cleanup_stream(codec,
  3587. mout->hp_out_nid[i]);
  3588. for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
  3589. if (mout->extra_out_nid[i])
  3590. snd_hda_codec_cleanup_stream(codec,
  3591. mout->extra_out_nid[i]);
  3592. mutex_lock(&codec->spdif_mutex);
  3593. if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
  3594. cleanup_dig_out_stream(codec, mout->dig_out_nid);
  3595. mout->dig_out_used = 0;
  3596. }
  3597. mutex_unlock(&codec->spdif_mutex);
  3598. return 0;
  3599. }
  3600. EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_cleanup);
  3601. /**
  3602. * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
  3603. * @codec: the HDA codec
  3604. * @pin: referred pin NID
  3605. *
  3606. * Guess the suitable VREF pin bits to be set as the pin-control value.
  3607. * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
  3608. */
  3609. unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
  3610. {
  3611. unsigned int pincap;
  3612. unsigned int oldval;
  3613. oldval = snd_hda_codec_read(codec, pin, 0,
  3614. AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
  3615. pincap = snd_hda_query_pin_caps(codec, pin);
  3616. pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
  3617. /* Exception: if the default pin setup is vref50, we give it priority */
  3618. if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
  3619. return AC_PINCTL_VREF_80;
  3620. else if (pincap & AC_PINCAP_VREF_50)
  3621. return AC_PINCTL_VREF_50;
  3622. else if (pincap & AC_PINCAP_VREF_100)
  3623. return AC_PINCTL_VREF_100;
  3624. else if (pincap & AC_PINCAP_VREF_GRD)
  3625. return AC_PINCTL_VREF_GRD;
  3626. return AC_PINCTL_VREF_HIZ;
  3627. }
  3628. EXPORT_SYMBOL_GPL(snd_hda_get_default_vref);
  3629. /**
  3630. * snd_hda_correct_pin_ctl - correct the pin ctl value for matching with the pin cap
  3631. * @codec: the HDA codec
  3632. * @pin: referred pin NID
  3633. * @val: pin ctl value to audit
  3634. */
  3635. unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
  3636. hda_nid_t pin, unsigned int val)
  3637. {
  3638. static unsigned int cap_lists[][2] = {
  3639. { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
  3640. { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
  3641. { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
  3642. { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
  3643. };
  3644. unsigned int cap;
  3645. if (!val)
  3646. return 0;
  3647. cap = snd_hda_query_pin_caps(codec, pin);
  3648. if (!cap)
  3649. return val; /* don't know what to do... */
  3650. if (val & AC_PINCTL_OUT_EN) {
  3651. if (!(cap & AC_PINCAP_OUT))
  3652. val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
  3653. else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
  3654. val &= ~AC_PINCTL_HP_EN;
  3655. }
  3656. if (val & AC_PINCTL_IN_EN) {
  3657. if (!(cap & AC_PINCAP_IN))
  3658. val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
  3659. else {
  3660. unsigned int vcap, vref;
  3661. int i;
  3662. vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
  3663. vref = val & AC_PINCTL_VREFEN;
  3664. for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
  3665. if (vref == cap_lists[i][0] &&
  3666. !(vcap & cap_lists[i][1])) {
  3667. if (i == ARRAY_SIZE(cap_lists) - 1)
  3668. vref = AC_PINCTL_VREF_HIZ;
  3669. else
  3670. vref = cap_lists[i + 1][0];
  3671. }
  3672. }
  3673. val &= ~AC_PINCTL_VREFEN;
  3674. val |= vref;
  3675. }
  3676. }
  3677. return val;
  3678. }
  3679. EXPORT_SYMBOL_GPL(snd_hda_correct_pin_ctl);
  3680. /**
  3681. * _snd_hda_pin_ctl - Helper to set pin ctl value
  3682. * @codec: the HDA codec
  3683. * @pin: referred pin NID
  3684. * @val: pin control value to set
  3685. * @cached: access over codec pinctl cache or direct write
  3686. *
  3687. * This function is a helper to set a pin ctl value more safely.
  3688. * It corrects the pin ctl value via snd_hda_correct_pin_ctl(), stores the
  3689. * value in pin target array via snd_hda_codec_set_pin_target(), then
  3690. * actually writes the value via either snd_hda_codec_update_cache() or
  3691. * snd_hda_codec_write() depending on @cached flag.
  3692. */
  3693. int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
  3694. unsigned int val, bool cached)
  3695. {
  3696. val = snd_hda_correct_pin_ctl(codec, pin, val);
  3697. snd_hda_codec_set_pin_target(codec, pin, val);
  3698. if (cached)
  3699. return snd_hda_codec_update_cache(codec, pin, 0,
  3700. AC_VERB_SET_PIN_WIDGET_CONTROL, val);
  3701. else
  3702. return snd_hda_codec_write(codec, pin, 0,
  3703. AC_VERB_SET_PIN_WIDGET_CONTROL, val);
  3704. }
  3705. EXPORT_SYMBOL_GPL(_snd_hda_set_pin_ctl);
  3706. /**
  3707. * snd_hda_add_imux_item - Add an item to input_mux
  3708. * @codec: the HDA codec
  3709. * @imux: imux helper object
  3710. * @label: the name of imux item to assign
  3711. * @index: index number of imux item to assign
  3712. * @type_idx: pointer to store the resultant label index
  3713. *
  3714. * When the same label is used already in the existing items, the number
  3715. * suffix is appended to the label. This label index number is stored
  3716. * to type_idx when non-NULL pointer is given.
  3717. */
  3718. int snd_hda_add_imux_item(struct hda_codec *codec,
  3719. struct hda_input_mux *imux, const char *label,
  3720. int index, int *type_idx)
  3721. {
  3722. int i, label_idx = 0;
  3723. if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
  3724. codec_err(codec, "hda_codec: Too many imux items!\n");
  3725. return -EINVAL;
  3726. }
  3727. for (i = 0; i < imux->num_items; i++) {
  3728. if (!strncmp(label, imux->items[i].label, strlen(label)))
  3729. label_idx++;
  3730. }
  3731. if (type_idx)
  3732. *type_idx = label_idx;
  3733. if (label_idx > 0)
  3734. snprintf(imux->items[imux->num_items].label,
  3735. sizeof(imux->items[imux->num_items].label),
  3736. "%s %d", label, label_idx);
  3737. else
  3738. strlcpy(imux->items[imux->num_items].label, label,
  3739. sizeof(imux->items[imux->num_items].label));
  3740. imux->items[imux->num_items].index = index;
  3741. imux->num_items++;
  3742. return 0;
  3743. }
  3744. EXPORT_SYMBOL_GPL(snd_hda_add_imux_item);
  3745. /**
  3746. * snd_hda_bus_reset_codecs - Reset the bus
  3747. * @bus: HD-audio bus
  3748. */
  3749. void snd_hda_bus_reset_codecs(struct hda_bus *bus)
  3750. {
  3751. struct hda_codec *codec;
  3752. list_for_each_codec(codec, bus) {
  3753. /* FIXME: maybe a better way needed for forced reset */
  3754. cancel_delayed_work_sync(&codec->jackpoll_work);
  3755. #ifdef CONFIG_PM
  3756. if (hda_codec_is_power_on(codec)) {
  3757. hda_call_codec_suspend(codec);
  3758. hda_call_codec_resume(codec);
  3759. }
  3760. #endif
  3761. }
  3762. }
  3763. /**
  3764. * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
  3765. * @pcm: PCM caps bits
  3766. * @buf: the string buffer to write
  3767. * @buflen: the max buffer length
  3768. *
  3769. * used by hda_proc.c and hda_eld.c
  3770. */
  3771. void snd_print_pcm_bits(int pcm, char *buf, int buflen)
  3772. {
  3773. static unsigned int bits[] = { 8, 16, 20, 24, 32 };
  3774. int i, j;
  3775. for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
  3776. if (pcm & (AC_SUPPCM_BITS_8 << i))
  3777. j += snprintf(buf + j, buflen - j, " %d", bits[i]);
  3778. buf[j] = '\0'; /* necessary when j == 0 */
  3779. }
  3780. EXPORT_SYMBOL_GPL(snd_print_pcm_bits);
  3781. MODULE_DESCRIPTION("HDA codec core");
  3782. MODULE_LICENSE("GPL");