hda_codec.c 106 KB

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