hda_codec.c 122 KB

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