hda_codec.c 110 KB

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