hda_codec.c 93 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563
  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/pci.h>
  25. #include <linux/mutex.h>
  26. #include <sound/core.h>
  27. #include "hda_codec.h"
  28. #include <sound/asoundef.h>
  29. #include <sound/tlv.h>
  30. #include <sound/initval.h>
  31. #include "hda_local.h"
  32. #include <sound/hda_hwdep.h>
  33. /*
  34. * vendor / preset table
  35. */
  36. struct hda_vendor_id {
  37. unsigned int id;
  38. const char *name;
  39. };
  40. /* codec vendor labels */
  41. static struct hda_vendor_id hda_vendor_ids[] = {
  42. { 0x1002, "ATI" },
  43. { 0x1057, "Motorola" },
  44. { 0x1095, "Silicon Image" },
  45. { 0x10ec, "Realtek" },
  46. { 0x1106, "VIA" },
  47. { 0x111d, "IDT" },
  48. { 0x11c1, "LSI" },
  49. { 0x11d4, "Analog Devices" },
  50. { 0x13f6, "C-Media" },
  51. { 0x14f1, "Conexant" },
  52. { 0x17e8, "Chrontel" },
  53. { 0x1854, "LG" },
  54. { 0x1aec, "Wolfson Microelectronics" },
  55. { 0x434d, "C-Media" },
  56. { 0x8384, "SigmaTel" },
  57. {} /* terminator */
  58. };
  59. static DEFINE_MUTEX(preset_mutex);
  60. static LIST_HEAD(hda_preset_tables);
  61. int snd_hda_add_codec_preset(struct hda_codec_preset_list *preset)
  62. {
  63. mutex_lock(&preset_mutex);
  64. list_add_tail(&preset->list, &hda_preset_tables);
  65. mutex_unlock(&preset_mutex);
  66. return 0;
  67. }
  68. EXPORT_SYMBOL_GPL(snd_hda_add_codec_preset);
  69. int snd_hda_delete_codec_preset(struct hda_codec_preset_list *preset)
  70. {
  71. mutex_lock(&preset_mutex);
  72. list_del(&preset->list);
  73. mutex_unlock(&preset_mutex);
  74. return 0;
  75. }
  76. EXPORT_SYMBOL_GPL(snd_hda_delete_codec_preset);
  77. #ifdef CONFIG_SND_HDA_POWER_SAVE
  78. static void hda_power_work(struct work_struct *work);
  79. static void hda_keep_power_on(struct hda_codec *codec);
  80. #else
  81. static inline void hda_keep_power_on(struct hda_codec *codec) {}
  82. #endif
  83. const char *snd_hda_get_jack_location(u32 cfg)
  84. {
  85. static char *bases[7] = {
  86. "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
  87. };
  88. static unsigned char specials_idx[] = {
  89. 0x07, 0x08,
  90. 0x17, 0x18, 0x19,
  91. 0x37, 0x38
  92. };
  93. static char *specials[] = {
  94. "Rear Panel", "Drive Bar",
  95. "Riser", "HDMI", "ATAPI",
  96. "Mobile-In", "Mobile-Out"
  97. };
  98. int i;
  99. cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
  100. if ((cfg & 0x0f) < 7)
  101. return bases[cfg & 0x0f];
  102. for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
  103. if (cfg == specials_idx[i])
  104. return specials[i];
  105. }
  106. return "UNKNOWN";
  107. }
  108. EXPORT_SYMBOL_GPL(snd_hda_get_jack_location);
  109. const char *snd_hda_get_jack_connectivity(u32 cfg)
  110. {
  111. static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
  112. return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
  113. }
  114. EXPORT_SYMBOL_GPL(snd_hda_get_jack_connectivity);
  115. const char *snd_hda_get_jack_type(u32 cfg)
  116. {
  117. static char *jack_types[16] = {
  118. "Line Out", "Speaker", "HP Out", "CD",
  119. "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
  120. "Line In", "Aux", "Mic", "Telephony",
  121. "SPDIF In", "Digitial In", "Reserved", "Other"
  122. };
  123. return jack_types[(cfg & AC_DEFCFG_DEVICE)
  124. >> AC_DEFCFG_DEVICE_SHIFT];
  125. }
  126. EXPORT_SYMBOL_GPL(snd_hda_get_jack_type);
  127. /*
  128. * Compose a 32bit command word to be sent to the HD-audio controller
  129. */
  130. static inline unsigned int
  131. make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int direct,
  132. unsigned int verb, unsigned int parm)
  133. {
  134. u32 val;
  135. val = (u32)(codec->addr & 0x0f) << 28;
  136. val |= (u32)direct << 27;
  137. val |= (u32)nid << 20;
  138. val |= verb << 8;
  139. val |= parm;
  140. return val;
  141. }
  142. /**
  143. * snd_hda_codec_read - send a command and get the response
  144. * @codec: the HDA codec
  145. * @nid: NID to send the command
  146. * @direct: direct flag
  147. * @verb: the verb to send
  148. * @parm: the parameter for the verb
  149. *
  150. * Send a single command and read the corresponding response.
  151. *
  152. * Returns the obtained response value, or -1 for an error.
  153. */
  154. unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
  155. int direct,
  156. unsigned int verb, unsigned int parm)
  157. {
  158. struct hda_bus *bus = codec->bus;
  159. unsigned int res;
  160. res = make_codec_cmd(codec, nid, direct, verb, parm);
  161. snd_hda_power_up(codec);
  162. mutex_lock(&bus->cmd_mutex);
  163. if (!bus->ops.command(bus, res))
  164. res = bus->ops.get_response(bus);
  165. else
  166. res = (unsigned int)-1;
  167. mutex_unlock(&bus->cmd_mutex);
  168. snd_hda_power_down(codec);
  169. return res;
  170. }
  171. EXPORT_SYMBOL_GPL(snd_hda_codec_read);
  172. /**
  173. * snd_hda_codec_write - send a single command without waiting for response
  174. * @codec: the HDA codec
  175. * @nid: NID to send the command
  176. * @direct: direct flag
  177. * @verb: the verb to send
  178. * @parm: the parameter for the verb
  179. *
  180. * Send a single command without waiting for response.
  181. *
  182. * Returns 0 if successful, or a negative error code.
  183. */
  184. int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
  185. unsigned int verb, unsigned int parm)
  186. {
  187. struct hda_bus *bus = codec->bus;
  188. unsigned int res;
  189. int err;
  190. res = make_codec_cmd(codec, nid, direct, verb, parm);
  191. snd_hda_power_up(codec);
  192. mutex_lock(&bus->cmd_mutex);
  193. err = bus->ops.command(bus, res);
  194. mutex_unlock(&bus->cmd_mutex);
  195. snd_hda_power_down(codec);
  196. return err;
  197. }
  198. EXPORT_SYMBOL_GPL(snd_hda_codec_write);
  199. /**
  200. * snd_hda_sequence_write - sequence writes
  201. * @codec: the HDA codec
  202. * @seq: VERB array to send
  203. *
  204. * Send the commands sequentially from the given array.
  205. * The array must be terminated with NID=0.
  206. */
  207. void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
  208. {
  209. for (; seq->nid; seq++)
  210. snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
  211. }
  212. EXPORT_SYMBOL_GPL(snd_hda_sequence_write);
  213. /**
  214. * snd_hda_get_sub_nodes - get the range of sub nodes
  215. * @codec: the HDA codec
  216. * @nid: NID to parse
  217. * @start_id: the pointer to store the start NID
  218. *
  219. * Parse the NID and store the start NID of its sub-nodes.
  220. * Returns the number of sub-nodes.
  221. */
  222. int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
  223. hda_nid_t *start_id)
  224. {
  225. unsigned int parm;
  226. parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
  227. if (parm == -1)
  228. return 0;
  229. *start_id = (parm >> 16) & 0x7fff;
  230. return (int)(parm & 0x7fff);
  231. }
  232. EXPORT_SYMBOL_GPL(snd_hda_get_sub_nodes);
  233. /**
  234. * snd_hda_get_connections - get connection list
  235. * @codec: the HDA codec
  236. * @nid: NID to parse
  237. * @conn_list: connection list array
  238. * @max_conns: max. number of connections to store
  239. *
  240. * Parses the connection list of the given widget and stores the list
  241. * of NIDs.
  242. *
  243. * Returns the number of connections, or a negative error code.
  244. */
  245. int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
  246. hda_nid_t *conn_list, int max_conns)
  247. {
  248. unsigned int parm;
  249. int i, conn_len, conns;
  250. unsigned int shift, num_elems, mask;
  251. hda_nid_t prev_nid;
  252. if (snd_BUG_ON(!conn_list || max_conns <= 0))
  253. return -EINVAL;
  254. parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
  255. if (parm & AC_CLIST_LONG) {
  256. /* long form */
  257. shift = 16;
  258. num_elems = 2;
  259. } else {
  260. /* short form */
  261. shift = 8;
  262. num_elems = 4;
  263. }
  264. conn_len = parm & AC_CLIST_LENGTH;
  265. mask = (1 << (shift-1)) - 1;
  266. if (!conn_len)
  267. return 0; /* no connection */
  268. if (conn_len == 1) {
  269. /* single connection */
  270. parm = snd_hda_codec_read(codec, nid, 0,
  271. AC_VERB_GET_CONNECT_LIST, 0);
  272. conn_list[0] = parm & mask;
  273. return 1;
  274. }
  275. /* multi connection */
  276. conns = 0;
  277. prev_nid = 0;
  278. for (i = 0; i < conn_len; i++) {
  279. int range_val;
  280. hda_nid_t val, n;
  281. if (i % num_elems == 0)
  282. parm = snd_hda_codec_read(codec, nid, 0,
  283. AC_VERB_GET_CONNECT_LIST, i);
  284. range_val = !!(parm & (1 << (shift-1))); /* ranges */
  285. val = parm & mask;
  286. parm >>= shift;
  287. if (range_val) {
  288. /* ranges between the previous and this one */
  289. if (!prev_nid || prev_nid >= val) {
  290. snd_printk(KERN_WARNING "hda_codec: "
  291. "invalid dep_range_val %x:%x\n",
  292. prev_nid, val);
  293. continue;
  294. }
  295. for (n = prev_nid + 1; n <= val; n++) {
  296. if (conns >= max_conns) {
  297. snd_printk(KERN_ERR
  298. "Too many connections\n");
  299. return -EINVAL;
  300. }
  301. conn_list[conns++] = n;
  302. }
  303. } else {
  304. if (conns >= max_conns) {
  305. snd_printk(KERN_ERR "Too many connections\n");
  306. return -EINVAL;
  307. }
  308. conn_list[conns++] = val;
  309. }
  310. prev_nid = val;
  311. }
  312. return conns;
  313. }
  314. EXPORT_SYMBOL_GPL(snd_hda_get_connections);
  315. /**
  316. * snd_hda_queue_unsol_event - add an unsolicited event to queue
  317. * @bus: the BUS
  318. * @res: unsolicited event (lower 32bit of RIRB entry)
  319. * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
  320. *
  321. * Adds the given event to the queue. The events are processed in
  322. * the workqueue asynchronously. Call this function in the interrupt
  323. * hanlder when RIRB receives an unsolicited event.
  324. *
  325. * Returns 0 if successful, or a negative error code.
  326. */
  327. int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
  328. {
  329. struct hda_bus_unsolicited *unsol;
  330. unsigned int wp;
  331. unsol = bus->unsol;
  332. if (!unsol)
  333. return 0;
  334. wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
  335. unsol->wp = wp;
  336. wp <<= 1;
  337. unsol->queue[wp] = res;
  338. unsol->queue[wp + 1] = res_ex;
  339. schedule_work(&unsol->work);
  340. return 0;
  341. }
  342. EXPORT_SYMBOL_GPL(snd_hda_queue_unsol_event);
  343. /*
  344. * process queued unsolicited events
  345. */
  346. static void process_unsol_events(struct work_struct *work)
  347. {
  348. struct hda_bus_unsolicited *unsol =
  349. container_of(work, struct hda_bus_unsolicited, work);
  350. struct hda_bus *bus = unsol->bus;
  351. struct hda_codec *codec;
  352. unsigned int rp, caddr, res;
  353. while (unsol->rp != unsol->wp) {
  354. rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
  355. unsol->rp = rp;
  356. rp <<= 1;
  357. res = unsol->queue[rp];
  358. caddr = unsol->queue[rp + 1];
  359. if (!(caddr & (1 << 4))) /* no unsolicited event? */
  360. continue;
  361. codec = bus->caddr_tbl[caddr & 0x0f];
  362. if (codec && codec->patch_ops.unsol_event)
  363. codec->patch_ops.unsol_event(codec, res);
  364. }
  365. }
  366. /*
  367. * initialize unsolicited queue
  368. */
  369. static int init_unsol_queue(struct hda_bus *bus)
  370. {
  371. struct hda_bus_unsolicited *unsol;
  372. if (bus->unsol) /* already initialized */
  373. return 0;
  374. unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
  375. if (!unsol) {
  376. snd_printk(KERN_ERR "hda_codec: "
  377. "can't allocate unsolicited queue\n");
  378. return -ENOMEM;
  379. }
  380. INIT_WORK(&unsol->work, process_unsol_events);
  381. unsol->bus = bus;
  382. bus->unsol = unsol;
  383. return 0;
  384. }
  385. /*
  386. * destructor
  387. */
  388. static void snd_hda_codec_free(struct hda_codec *codec);
  389. static int snd_hda_bus_free(struct hda_bus *bus)
  390. {
  391. struct hda_codec *codec, *n;
  392. if (!bus)
  393. return 0;
  394. if (bus->unsol) {
  395. flush_scheduled_work();
  396. kfree(bus->unsol);
  397. }
  398. list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
  399. snd_hda_codec_free(codec);
  400. }
  401. if (bus->ops.private_free)
  402. bus->ops.private_free(bus);
  403. kfree(bus);
  404. return 0;
  405. }
  406. static int snd_hda_bus_dev_free(struct snd_device *device)
  407. {
  408. struct hda_bus *bus = device->device_data;
  409. bus->shutdown = 1;
  410. return snd_hda_bus_free(bus);
  411. }
  412. #ifdef CONFIG_SND_HDA_HWDEP
  413. static int snd_hda_bus_dev_register(struct snd_device *device)
  414. {
  415. struct hda_bus *bus = device->device_data;
  416. struct hda_codec *codec;
  417. list_for_each_entry(codec, &bus->codec_list, list) {
  418. snd_hda_hwdep_add_sysfs(codec);
  419. }
  420. return 0;
  421. }
  422. #else
  423. #define snd_hda_bus_dev_register NULL
  424. #endif
  425. /**
  426. * snd_hda_bus_new - create a HDA bus
  427. * @card: the card entry
  428. * @temp: the template for hda_bus information
  429. * @busp: the pointer to store the created bus instance
  430. *
  431. * Returns 0 if successful, or a negative error code.
  432. */
  433. int /*__devinit*/ snd_hda_bus_new(struct snd_card *card,
  434. const struct hda_bus_template *temp,
  435. struct hda_bus **busp)
  436. {
  437. struct hda_bus *bus;
  438. int err;
  439. static struct snd_device_ops dev_ops = {
  440. .dev_register = snd_hda_bus_dev_register,
  441. .dev_free = snd_hda_bus_dev_free,
  442. };
  443. if (snd_BUG_ON(!temp))
  444. return -EINVAL;
  445. if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
  446. return -EINVAL;
  447. if (busp)
  448. *busp = NULL;
  449. bus = kzalloc(sizeof(*bus), GFP_KERNEL);
  450. if (bus == NULL) {
  451. snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
  452. return -ENOMEM;
  453. }
  454. bus->card = card;
  455. bus->private_data = temp->private_data;
  456. bus->pci = temp->pci;
  457. bus->modelname = temp->modelname;
  458. bus->power_save = temp->power_save;
  459. bus->ops = temp->ops;
  460. mutex_init(&bus->cmd_mutex);
  461. INIT_LIST_HEAD(&bus->codec_list);
  462. err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
  463. if (err < 0) {
  464. snd_hda_bus_free(bus);
  465. return err;
  466. }
  467. if (busp)
  468. *busp = bus;
  469. return 0;
  470. }
  471. EXPORT_SYMBOL_GPL(snd_hda_bus_new);
  472. #ifdef CONFIG_SND_HDA_GENERIC
  473. #define is_generic_config(codec) \
  474. (codec->modelname && !strcmp(codec->modelname, "generic"))
  475. #else
  476. #define is_generic_config(codec) 0
  477. #endif
  478. #ifdef CONFIG_SND_HDA_INTEL_MODULE
  479. #define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
  480. #else
  481. #define HDA_MODREQ_MAX_COUNT 0
  482. #endif
  483. /*
  484. * find a matching codec preset
  485. */
  486. static const struct hda_codec_preset *
  487. find_codec_preset(struct hda_codec *codec)
  488. {
  489. struct hda_codec_preset_list *tbl;
  490. const struct hda_codec_preset *preset;
  491. int mod_requested = 0;
  492. if (is_generic_config(codec))
  493. return NULL; /* use the generic parser */
  494. again:
  495. mutex_lock(&preset_mutex);
  496. list_for_each_entry(tbl, &hda_preset_tables, list) {
  497. if (!try_module_get(tbl->owner)) {
  498. snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
  499. continue;
  500. }
  501. for (preset = tbl->preset; preset->id; preset++) {
  502. u32 mask = preset->mask;
  503. if (preset->afg && preset->afg != codec->afg)
  504. continue;
  505. if (preset->mfg && preset->mfg != codec->mfg)
  506. continue;
  507. if (!mask)
  508. mask = ~0;
  509. if (preset->id == (codec->vendor_id & mask) &&
  510. (!preset->rev ||
  511. preset->rev == codec->revision_id)) {
  512. mutex_unlock(&preset_mutex);
  513. codec->owner = tbl->owner;
  514. return preset;
  515. }
  516. }
  517. module_put(tbl->owner);
  518. }
  519. mutex_unlock(&preset_mutex);
  520. if (mod_requested < HDA_MODREQ_MAX_COUNT) {
  521. char name[32];
  522. if (!mod_requested)
  523. snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
  524. codec->vendor_id);
  525. else
  526. snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
  527. (codec->vendor_id >> 16) & 0xffff);
  528. request_module(name);
  529. mod_requested++;
  530. goto again;
  531. }
  532. return NULL;
  533. }
  534. /*
  535. * get_codec_name - store the codec name
  536. */
  537. static int get_codec_name(struct hda_codec *codec)
  538. {
  539. const struct hda_vendor_id *c;
  540. const char *vendor = NULL;
  541. u16 vendor_id = codec->vendor_id >> 16;
  542. char tmp[16], name[32];
  543. for (c = hda_vendor_ids; c->id; c++) {
  544. if (c->id == vendor_id) {
  545. vendor = c->name;
  546. break;
  547. }
  548. }
  549. if (!vendor) {
  550. sprintf(tmp, "Generic %04x", vendor_id);
  551. vendor = tmp;
  552. }
  553. if (codec->preset && codec->preset->name)
  554. snprintf(name, sizeof(name), "%s %s", vendor,
  555. codec->preset->name);
  556. else
  557. snprintf(name, sizeof(name), "%s ID %x", vendor,
  558. codec->vendor_id & 0xffff);
  559. codec->name = kstrdup(name, GFP_KERNEL);
  560. if (!codec->name)
  561. return -ENOMEM;
  562. return 0;
  563. }
  564. /*
  565. * look for an AFG and MFG nodes
  566. */
  567. static void /*__devinit*/ setup_fg_nodes(struct hda_codec *codec)
  568. {
  569. int i, total_nodes;
  570. hda_nid_t nid;
  571. total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
  572. for (i = 0; i < total_nodes; i++, nid++) {
  573. unsigned int func;
  574. func = snd_hda_param_read(codec, nid, AC_PAR_FUNCTION_TYPE);
  575. switch (func & 0xff) {
  576. case AC_GRP_AUDIO_FUNCTION:
  577. codec->afg = nid;
  578. break;
  579. case AC_GRP_MODEM_FUNCTION:
  580. codec->mfg = nid;
  581. break;
  582. default:
  583. break;
  584. }
  585. }
  586. }
  587. /*
  588. * read widget caps for each widget and store in cache
  589. */
  590. static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
  591. {
  592. int i;
  593. hda_nid_t nid;
  594. codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
  595. &codec->start_nid);
  596. codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
  597. if (!codec->wcaps)
  598. return -ENOMEM;
  599. nid = codec->start_nid;
  600. for (i = 0; i < codec->num_nodes; i++, nid++)
  601. codec->wcaps[i] = snd_hda_param_read(codec, nid,
  602. AC_PAR_AUDIO_WIDGET_CAP);
  603. return 0;
  604. }
  605. static void init_hda_cache(struct hda_cache_rec *cache,
  606. unsigned int record_size);
  607. static void free_hda_cache(struct hda_cache_rec *cache);
  608. /*
  609. * codec destructor
  610. */
  611. static void snd_hda_codec_free(struct hda_codec *codec)
  612. {
  613. if (!codec)
  614. return;
  615. #ifdef CONFIG_SND_HDA_POWER_SAVE
  616. cancel_delayed_work(&codec->power_work);
  617. flush_scheduled_work();
  618. #endif
  619. list_del(&codec->list);
  620. snd_array_free(&codec->mixers);
  621. codec->bus->caddr_tbl[codec->addr] = NULL;
  622. if (codec->patch_ops.free)
  623. codec->patch_ops.free(codec);
  624. module_put(codec->owner);
  625. free_hda_cache(&codec->amp_cache);
  626. free_hda_cache(&codec->cmd_cache);
  627. kfree(codec->name);
  628. kfree(codec->modelname);
  629. kfree(codec->wcaps);
  630. kfree(codec);
  631. }
  632. /**
  633. * snd_hda_codec_new - create a HDA codec
  634. * @bus: the bus to assign
  635. * @codec_addr: the codec address
  636. * @codecp: the pointer to store the generated codec
  637. *
  638. * Returns 0 if successful, or a negative error code.
  639. */
  640. int /*__devinit*/ snd_hda_codec_new(struct hda_bus *bus, unsigned int codec_addr,
  641. struct hda_codec **codecp)
  642. {
  643. struct hda_codec *codec;
  644. char component[31];
  645. int err;
  646. if (snd_BUG_ON(!bus))
  647. return -EINVAL;
  648. if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
  649. return -EINVAL;
  650. if (bus->caddr_tbl[codec_addr]) {
  651. snd_printk(KERN_ERR "hda_codec: "
  652. "address 0x%x is already occupied\n", codec_addr);
  653. return -EBUSY;
  654. }
  655. codec = kzalloc(sizeof(*codec), GFP_KERNEL);
  656. if (codec == NULL) {
  657. snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
  658. return -ENOMEM;
  659. }
  660. codec->bus = bus;
  661. codec->addr = codec_addr;
  662. mutex_init(&codec->spdif_mutex);
  663. init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
  664. init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
  665. snd_array_init(&codec->mixers, sizeof(struct snd_kcontrol *), 32);
  666. if (codec->bus->modelname) {
  667. codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
  668. if (!codec->modelname) {
  669. snd_hda_codec_free(codec);
  670. return -ENODEV;
  671. }
  672. }
  673. #ifdef CONFIG_SND_HDA_POWER_SAVE
  674. INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
  675. /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
  676. * the caller has to power down appropriatley after initialization
  677. * phase.
  678. */
  679. hda_keep_power_on(codec);
  680. #endif
  681. list_add_tail(&codec->list, &bus->codec_list);
  682. bus->caddr_tbl[codec_addr] = codec;
  683. codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
  684. AC_PAR_VENDOR_ID);
  685. if (codec->vendor_id == -1)
  686. /* read again, hopefully the access method was corrected
  687. * in the last read...
  688. */
  689. codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
  690. AC_PAR_VENDOR_ID);
  691. codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
  692. AC_PAR_SUBSYSTEM_ID);
  693. codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
  694. AC_PAR_REV_ID);
  695. setup_fg_nodes(codec);
  696. if (!codec->afg && !codec->mfg) {
  697. snd_printdd("hda_codec: no AFG or MFG node found\n");
  698. snd_hda_codec_free(codec);
  699. return -ENODEV;
  700. }
  701. if (read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg) < 0) {
  702. snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
  703. snd_hda_codec_free(codec);
  704. return -ENOMEM;
  705. }
  706. if (!codec->subsystem_id) {
  707. hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
  708. codec->subsystem_id =
  709. snd_hda_codec_read(codec, nid, 0,
  710. AC_VERB_GET_SUBSYSTEM_ID, 0);
  711. }
  712. if (bus->modelname)
  713. codec->modelname = kstrdup(bus->modelname, GFP_KERNEL);
  714. err = snd_hda_codec_configure(codec);
  715. if (err < 0) {
  716. snd_hda_codec_free(codec);
  717. return err;
  718. }
  719. snd_hda_codec_proc_new(codec);
  720. snd_hda_create_hwdep(codec);
  721. sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
  722. codec->subsystem_id, codec->revision_id);
  723. snd_component_add(codec->bus->card, component);
  724. if (codecp)
  725. *codecp = codec;
  726. return 0;
  727. }
  728. EXPORT_SYMBOL_GPL(snd_hda_codec_new);
  729. int snd_hda_codec_configure(struct hda_codec *codec)
  730. {
  731. int err;
  732. codec->preset = find_codec_preset(codec);
  733. if (!codec->name) {
  734. err = get_codec_name(codec);
  735. if (err < 0)
  736. return err;
  737. }
  738. /* audio codec should override the mixer name */
  739. if (codec->afg || !*codec->bus->card->mixername)
  740. strlcpy(codec->bus->card->mixername, codec->name,
  741. sizeof(codec->bus->card->mixername));
  742. if (is_generic_config(codec)) {
  743. err = snd_hda_parse_generic_codec(codec);
  744. goto patched;
  745. }
  746. if (codec->preset && codec->preset->patch) {
  747. err = codec->preset->patch(codec);
  748. goto patched;
  749. }
  750. /* call the default parser */
  751. err = snd_hda_parse_generic_codec(codec);
  752. if (err < 0)
  753. printk(KERN_ERR "hda-codec: No codec parser is available\n");
  754. patched:
  755. if (!err && codec->patch_ops.unsol_event)
  756. err = init_unsol_queue(codec->bus);
  757. return err;
  758. }
  759. /**
  760. * snd_hda_codec_setup_stream - set up the codec for streaming
  761. * @codec: the CODEC to set up
  762. * @nid: the NID to set up
  763. * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
  764. * @channel_id: channel id to pass, zero based.
  765. * @format: stream format.
  766. */
  767. void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
  768. u32 stream_tag,
  769. int channel_id, int format)
  770. {
  771. if (!nid)
  772. return;
  773. snd_printdd("hda_codec_setup_stream: "
  774. "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
  775. nid, stream_tag, channel_id, format);
  776. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID,
  777. (stream_tag << 4) | channel_id);
  778. msleep(1);
  779. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, format);
  780. }
  781. EXPORT_SYMBOL_GPL(snd_hda_codec_setup_stream);
  782. void snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid)
  783. {
  784. if (!nid)
  785. return;
  786. snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
  787. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
  788. #if 0 /* keep the format */
  789. msleep(1);
  790. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0);
  791. #endif
  792. }
  793. EXPORT_SYMBOL_GPL(snd_hda_codec_cleanup_stream);
  794. /*
  795. * amp access functions
  796. */
  797. /* FIXME: more better hash key? */
  798. #define HDA_HASH_KEY(nid,dir,idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
  799. #define INFO_AMP_CAPS (1<<0)
  800. #define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
  801. /* initialize the hash table */
  802. static void /*__devinit*/ init_hda_cache(struct hda_cache_rec *cache,
  803. unsigned int record_size)
  804. {
  805. memset(cache, 0, sizeof(*cache));
  806. memset(cache->hash, 0xff, sizeof(cache->hash));
  807. snd_array_init(&cache->buf, record_size, 64);
  808. }
  809. static void free_hda_cache(struct hda_cache_rec *cache)
  810. {
  811. snd_array_free(&cache->buf);
  812. }
  813. /* query the hash. allocate an entry if not found. */
  814. static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
  815. u32 key)
  816. {
  817. u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
  818. u16 cur = cache->hash[idx];
  819. struct hda_cache_head *info;
  820. while (cur != 0xffff) {
  821. info = snd_array_elem(&cache->buf, cur);
  822. if (info->key == key)
  823. return info;
  824. cur = info->next;
  825. }
  826. /* add a new hash entry */
  827. info = snd_array_new(&cache->buf);
  828. if (!info)
  829. return NULL;
  830. cur = snd_array_index(&cache->buf, info);
  831. info->key = key;
  832. info->val = 0;
  833. info->next = cache->hash[idx];
  834. cache->hash[idx] = cur;
  835. return info;
  836. }
  837. /* query and allocate an amp hash entry */
  838. static inline struct hda_amp_info *
  839. get_alloc_amp_hash(struct hda_codec *codec, u32 key)
  840. {
  841. return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
  842. }
  843. /*
  844. * query AMP capabilities for the given widget and direction
  845. */
  846. u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
  847. {
  848. struct hda_amp_info *info;
  849. info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
  850. if (!info)
  851. return 0;
  852. if (!(info->head.val & INFO_AMP_CAPS)) {
  853. if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
  854. nid = codec->afg;
  855. info->amp_caps = snd_hda_param_read(codec, nid,
  856. direction == HDA_OUTPUT ?
  857. AC_PAR_AMP_OUT_CAP :
  858. AC_PAR_AMP_IN_CAP);
  859. if (info->amp_caps)
  860. info->head.val |= INFO_AMP_CAPS;
  861. }
  862. return info->amp_caps;
  863. }
  864. EXPORT_SYMBOL_GPL(query_amp_caps);
  865. int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
  866. unsigned int caps)
  867. {
  868. struct hda_amp_info *info;
  869. info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
  870. if (!info)
  871. return -EINVAL;
  872. info->amp_caps = caps;
  873. info->head.val |= INFO_AMP_CAPS;
  874. return 0;
  875. }
  876. EXPORT_SYMBOL_GPL(snd_hda_override_amp_caps);
  877. /*
  878. * read the current volume to info
  879. * if the cache exists, read the cache value.
  880. */
  881. static unsigned int get_vol_mute(struct hda_codec *codec,
  882. struct hda_amp_info *info, hda_nid_t nid,
  883. int ch, int direction, int index)
  884. {
  885. u32 val, parm;
  886. if (info->head.val & INFO_AMP_VOL(ch))
  887. return info->vol[ch];
  888. parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
  889. parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
  890. parm |= index;
  891. val = snd_hda_codec_read(codec, nid, 0,
  892. AC_VERB_GET_AMP_GAIN_MUTE, parm);
  893. info->vol[ch] = val & 0xff;
  894. info->head.val |= INFO_AMP_VOL(ch);
  895. return info->vol[ch];
  896. }
  897. /*
  898. * write the current volume in info to the h/w and update the cache
  899. */
  900. static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
  901. hda_nid_t nid, int ch, int direction, int index,
  902. int val)
  903. {
  904. u32 parm;
  905. parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
  906. parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
  907. parm |= index << AC_AMP_SET_INDEX_SHIFT;
  908. parm |= val;
  909. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
  910. info->vol[ch] = val;
  911. }
  912. /*
  913. * read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
  914. */
  915. int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
  916. int direction, int index)
  917. {
  918. struct hda_amp_info *info;
  919. info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
  920. if (!info)
  921. return 0;
  922. return get_vol_mute(codec, info, nid, ch, direction, index);
  923. }
  924. EXPORT_SYMBOL_GPL(snd_hda_codec_amp_read);
  925. /*
  926. * update the AMP value, mask = bit mask to set, val = the value
  927. */
  928. int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
  929. int direction, int idx, int mask, int val)
  930. {
  931. struct hda_amp_info *info;
  932. info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
  933. if (!info)
  934. return 0;
  935. val &= mask;
  936. val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
  937. if (info->vol[ch] == val)
  938. return 0;
  939. put_vol_mute(codec, info, nid, ch, direction, idx, val);
  940. return 1;
  941. }
  942. EXPORT_SYMBOL_GPL(snd_hda_codec_amp_update);
  943. /*
  944. * update the AMP stereo with the same mask and value
  945. */
  946. int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
  947. int direction, int idx, int mask, int val)
  948. {
  949. int ch, ret = 0;
  950. for (ch = 0; ch < 2; ch++)
  951. ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
  952. idx, mask, val);
  953. return ret;
  954. }
  955. EXPORT_SYMBOL_GPL(snd_hda_codec_amp_stereo);
  956. #ifdef SND_HDA_NEEDS_RESUME
  957. /* resume the all amp commands from the cache */
  958. void snd_hda_codec_resume_amp(struct hda_codec *codec)
  959. {
  960. struct hda_amp_info *buffer = codec->amp_cache.buf.list;
  961. int i;
  962. for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
  963. u32 key = buffer->head.key;
  964. hda_nid_t nid;
  965. unsigned int idx, dir, ch;
  966. if (!key)
  967. continue;
  968. nid = key & 0xff;
  969. idx = (key >> 16) & 0xff;
  970. dir = (key >> 24) & 0xff;
  971. for (ch = 0; ch < 2; ch++) {
  972. if (!(buffer->head.val & INFO_AMP_VOL(ch)))
  973. continue;
  974. put_vol_mute(codec, buffer, nid, ch, dir, idx,
  975. buffer->vol[ch]);
  976. }
  977. }
  978. }
  979. EXPORT_SYMBOL_GPL(snd_hda_codec_resume_amp);
  980. #endif /* SND_HDA_NEEDS_RESUME */
  981. /* volume */
  982. int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
  983. struct snd_ctl_elem_info *uinfo)
  984. {
  985. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  986. u16 nid = get_amp_nid(kcontrol);
  987. u8 chs = get_amp_channels(kcontrol);
  988. int dir = get_amp_direction(kcontrol);
  989. u32 caps;
  990. caps = query_amp_caps(codec, nid, dir);
  991. /* num steps */
  992. caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
  993. if (!caps) {
  994. printk(KERN_WARNING "hda_codec: "
  995. "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
  996. kcontrol->id.name);
  997. return -EINVAL;
  998. }
  999. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1000. uinfo->count = chs == 3 ? 2 : 1;
  1001. uinfo->value.integer.min = 0;
  1002. uinfo->value.integer.max = caps;
  1003. return 0;
  1004. }
  1005. EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_info);
  1006. int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
  1007. struct snd_ctl_elem_value *ucontrol)
  1008. {
  1009. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1010. hda_nid_t nid = get_amp_nid(kcontrol);
  1011. int chs = get_amp_channels(kcontrol);
  1012. int dir = get_amp_direction(kcontrol);
  1013. int idx = get_amp_index(kcontrol);
  1014. long *valp = ucontrol->value.integer.value;
  1015. if (chs & 1)
  1016. *valp++ = snd_hda_codec_amp_read(codec, nid, 0, dir, idx)
  1017. & HDA_AMP_VOLMASK;
  1018. if (chs & 2)
  1019. *valp = snd_hda_codec_amp_read(codec, nid, 1, dir, idx)
  1020. & HDA_AMP_VOLMASK;
  1021. return 0;
  1022. }
  1023. EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_get);
  1024. int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
  1025. struct snd_ctl_elem_value *ucontrol)
  1026. {
  1027. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1028. hda_nid_t nid = get_amp_nid(kcontrol);
  1029. int chs = get_amp_channels(kcontrol);
  1030. int dir = get_amp_direction(kcontrol);
  1031. int idx = get_amp_index(kcontrol);
  1032. long *valp = ucontrol->value.integer.value;
  1033. int change = 0;
  1034. snd_hda_power_up(codec);
  1035. if (chs & 1) {
  1036. change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
  1037. 0x7f, *valp);
  1038. valp++;
  1039. }
  1040. if (chs & 2)
  1041. change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
  1042. 0x7f, *valp);
  1043. snd_hda_power_down(codec);
  1044. return change;
  1045. }
  1046. EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_put);
  1047. int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
  1048. unsigned int size, unsigned int __user *_tlv)
  1049. {
  1050. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1051. hda_nid_t nid = get_amp_nid(kcontrol);
  1052. int dir = get_amp_direction(kcontrol);
  1053. u32 caps, val1, val2;
  1054. if (size < 4 * sizeof(unsigned int))
  1055. return -ENOMEM;
  1056. caps = query_amp_caps(codec, nid, dir);
  1057. val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
  1058. val2 = (val2 + 1) * 25;
  1059. val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
  1060. val1 = ((int)val1) * ((int)val2);
  1061. if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
  1062. return -EFAULT;
  1063. if (put_user(2 * sizeof(unsigned int), _tlv + 1))
  1064. return -EFAULT;
  1065. if (put_user(val1, _tlv + 2))
  1066. return -EFAULT;
  1067. if (put_user(val2, _tlv + 3))
  1068. return -EFAULT;
  1069. return 0;
  1070. }
  1071. EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_tlv);
  1072. /*
  1073. * set (static) TLV for virtual master volume; recalculated as max 0dB
  1074. */
  1075. void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
  1076. unsigned int *tlv)
  1077. {
  1078. u32 caps;
  1079. int nums, step;
  1080. caps = query_amp_caps(codec, nid, dir);
  1081. nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
  1082. step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
  1083. step = (step + 1) * 25;
  1084. tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
  1085. tlv[1] = 2 * sizeof(unsigned int);
  1086. tlv[2] = -nums * step;
  1087. tlv[3] = step;
  1088. }
  1089. EXPORT_SYMBOL_GPL(snd_hda_set_vmaster_tlv);
  1090. /* find a mixer control element with the given name */
  1091. static struct snd_kcontrol *
  1092. _snd_hda_find_mixer_ctl(struct hda_codec *codec,
  1093. const char *name, int idx)
  1094. {
  1095. struct snd_ctl_elem_id id;
  1096. memset(&id, 0, sizeof(id));
  1097. id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  1098. id.index = idx;
  1099. strcpy(id.name, name);
  1100. return snd_ctl_find_id(codec->bus->card, &id);
  1101. }
  1102. struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
  1103. const char *name)
  1104. {
  1105. return _snd_hda_find_mixer_ctl(codec, name, 0);
  1106. }
  1107. EXPORT_SYMBOL_GPL(snd_hda_find_mixer_ctl);
  1108. /* Add a control element and assign to the codec */
  1109. int snd_hda_ctl_add(struct hda_codec *codec, struct snd_kcontrol *kctl)
  1110. {
  1111. int err;
  1112. struct snd_kcontrol **knewp;
  1113. err = snd_ctl_add(codec->bus->card, kctl);
  1114. if (err < 0)
  1115. return err;
  1116. knewp = snd_array_new(&codec->mixers);
  1117. if (!knewp)
  1118. return -ENOMEM;
  1119. *knewp = kctl;
  1120. return 0;
  1121. }
  1122. EXPORT_SYMBOL_GPL(snd_hda_ctl_add);
  1123. #ifdef CONFIG_SND_HDA_RECONFIG
  1124. /* Clear all controls assigned to the given codec */
  1125. void snd_hda_ctls_clear(struct hda_codec *codec)
  1126. {
  1127. int i;
  1128. struct snd_kcontrol **kctls = codec->mixers.list;
  1129. for (i = 0; i < codec->mixers.used; i++)
  1130. snd_ctl_remove(codec->bus->card, kctls[i]);
  1131. snd_array_free(&codec->mixers);
  1132. }
  1133. void snd_hda_codec_reset(struct hda_codec *codec)
  1134. {
  1135. int i;
  1136. #ifdef CONFIG_SND_HDA_POWER_SAVE
  1137. cancel_delayed_work(&codec->power_work);
  1138. flush_scheduled_work();
  1139. #endif
  1140. snd_hda_ctls_clear(codec);
  1141. /* relase PCMs */
  1142. for (i = 0; i < codec->num_pcms; i++) {
  1143. if (codec->pcm_info[i].pcm) {
  1144. snd_device_free(codec->bus->card,
  1145. codec->pcm_info[i].pcm);
  1146. clear_bit(codec->pcm_info[i].device,
  1147. codec->bus->pcm_dev_bits);
  1148. }
  1149. }
  1150. if (codec->patch_ops.free)
  1151. codec->patch_ops.free(codec);
  1152. codec->spec = NULL;
  1153. free_hda_cache(&codec->amp_cache);
  1154. free_hda_cache(&codec->cmd_cache);
  1155. codec->num_pcms = 0;
  1156. codec->pcm_info = NULL;
  1157. codec->preset = NULL;
  1158. module_put(codec->owner);
  1159. codec->owner = NULL;
  1160. }
  1161. #endif /* CONFIG_SND_HDA_RECONFIG */
  1162. /* create a virtual master control and add slaves */
  1163. int snd_hda_add_vmaster(struct hda_codec *codec, char *name,
  1164. unsigned int *tlv, const char **slaves)
  1165. {
  1166. struct snd_kcontrol *kctl;
  1167. const char **s;
  1168. int err;
  1169. for (s = slaves; *s && !snd_hda_find_mixer_ctl(codec, *s); s++)
  1170. ;
  1171. if (!*s) {
  1172. snd_printdd("No slave found for %s\n", name);
  1173. return 0;
  1174. }
  1175. kctl = snd_ctl_make_virtual_master(name, tlv);
  1176. if (!kctl)
  1177. return -ENOMEM;
  1178. err = snd_hda_ctl_add(codec, kctl);
  1179. if (err < 0)
  1180. return err;
  1181. for (s = slaves; *s; s++) {
  1182. struct snd_kcontrol *sctl;
  1183. sctl = snd_hda_find_mixer_ctl(codec, *s);
  1184. if (!sctl) {
  1185. snd_printdd("Cannot find slave %s, skipped\n", *s);
  1186. continue;
  1187. }
  1188. err = snd_ctl_add_slave(kctl, sctl);
  1189. if (err < 0)
  1190. return err;
  1191. }
  1192. return 0;
  1193. }
  1194. EXPORT_SYMBOL_GPL(snd_hda_add_vmaster);
  1195. /* switch */
  1196. int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
  1197. struct snd_ctl_elem_info *uinfo)
  1198. {
  1199. int chs = get_amp_channels(kcontrol);
  1200. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  1201. uinfo->count = chs == 3 ? 2 : 1;
  1202. uinfo->value.integer.min = 0;
  1203. uinfo->value.integer.max = 1;
  1204. return 0;
  1205. }
  1206. EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_info);
  1207. int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
  1208. struct snd_ctl_elem_value *ucontrol)
  1209. {
  1210. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1211. hda_nid_t nid = get_amp_nid(kcontrol);
  1212. int chs = get_amp_channels(kcontrol);
  1213. int dir = get_amp_direction(kcontrol);
  1214. int idx = get_amp_index(kcontrol);
  1215. long *valp = ucontrol->value.integer.value;
  1216. if (chs & 1)
  1217. *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
  1218. HDA_AMP_MUTE) ? 0 : 1;
  1219. if (chs & 2)
  1220. *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
  1221. HDA_AMP_MUTE) ? 0 : 1;
  1222. return 0;
  1223. }
  1224. EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_get);
  1225. int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
  1226. struct snd_ctl_elem_value *ucontrol)
  1227. {
  1228. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1229. hda_nid_t nid = get_amp_nid(kcontrol);
  1230. int chs = get_amp_channels(kcontrol);
  1231. int dir = get_amp_direction(kcontrol);
  1232. int idx = get_amp_index(kcontrol);
  1233. long *valp = ucontrol->value.integer.value;
  1234. int change = 0;
  1235. snd_hda_power_up(codec);
  1236. if (chs & 1) {
  1237. change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
  1238. HDA_AMP_MUTE,
  1239. *valp ? 0 : HDA_AMP_MUTE);
  1240. valp++;
  1241. }
  1242. if (chs & 2)
  1243. change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
  1244. HDA_AMP_MUTE,
  1245. *valp ? 0 : HDA_AMP_MUTE);
  1246. #ifdef CONFIG_SND_HDA_POWER_SAVE
  1247. if (codec->patch_ops.check_power_status)
  1248. codec->patch_ops.check_power_status(codec, nid);
  1249. #endif
  1250. snd_hda_power_down(codec);
  1251. return change;
  1252. }
  1253. EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_put);
  1254. /*
  1255. * bound volume controls
  1256. *
  1257. * bind multiple volumes (# indices, from 0)
  1258. */
  1259. #define AMP_VAL_IDX_SHIFT 19
  1260. #define AMP_VAL_IDX_MASK (0x0f<<19)
  1261. int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
  1262. struct snd_ctl_elem_value *ucontrol)
  1263. {
  1264. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1265. unsigned long pval;
  1266. int err;
  1267. mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
  1268. pval = kcontrol->private_value;
  1269. kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
  1270. err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
  1271. kcontrol->private_value = pval;
  1272. mutex_unlock(&codec->spdif_mutex);
  1273. return err;
  1274. }
  1275. EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_get);
  1276. int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
  1277. struct snd_ctl_elem_value *ucontrol)
  1278. {
  1279. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1280. unsigned long pval;
  1281. int i, indices, err = 0, change = 0;
  1282. mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
  1283. pval = kcontrol->private_value;
  1284. indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
  1285. for (i = 0; i < indices; i++) {
  1286. kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
  1287. (i << AMP_VAL_IDX_SHIFT);
  1288. err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
  1289. if (err < 0)
  1290. break;
  1291. change |= err;
  1292. }
  1293. kcontrol->private_value = pval;
  1294. mutex_unlock(&codec->spdif_mutex);
  1295. return err < 0 ? err : change;
  1296. }
  1297. EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_put);
  1298. /*
  1299. * generic bound volume/swtich controls
  1300. */
  1301. int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
  1302. struct snd_ctl_elem_info *uinfo)
  1303. {
  1304. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1305. struct hda_bind_ctls *c;
  1306. int err;
  1307. mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
  1308. c = (struct hda_bind_ctls *)kcontrol->private_value;
  1309. kcontrol->private_value = *c->values;
  1310. err = c->ops->info(kcontrol, uinfo);
  1311. kcontrol->private_value = (long)c;
  1312. mutex_unlock(&codec->spdif_mutex);
  1313. return err;
  1314. }
  1315. EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_info);
  1316. int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
  1317. struct snd_ctl_elem_value *ucontrol)
  1318. {
  1319. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1320. struct hda_bind_ctls *c;
  1321. int err;
  1322. mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
  1323. c = (struct hda_bind_ctls *)kcontrol->private_value;
  1324. kcontrol->private_value = *c->values;
  1325. err = c->ops->get(kcontrol, ucontrol);
  1326. kcontrol->private_value = (long)c;
  1327. mutex_unlock(&codec->spdif_mutex);
  1328. return err;
  1329. }
  1330. EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_get);
  1331. int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
  1332. struct snd_ctl_elem_value *ucontrol)
  1333. {
  1334. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1335. struct hda_bind_ctls *c;
  1336. unsigned long *vals;
  1337. int err = 0, change = 0;
  1338. mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
  1339. c = (struct hda_bind_ctls *)kcontrol->private_value;
  1340. for (vals = c->values; *vals; vals++) {
  1341. kcontrol->private_value = *vals;
  1342. err = c->ops->put(kcontrol, ucontrol);
  1343. if (err < 0)
  1344. break;
  1345. change |= err;
  1346. }
  1347. kcontrol->private_value = (long)c;
  1348. mutex_unlock(&codec->spdif_mutex);
  1349. return err < 0 ? err : change;
  1350. }
  1351. EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_put);
  1352. int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
  1353. unsigned int size, unsigned int __user *tlv)
  1354. {
  1355. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1356. struct hda_bind_ctls *c;
  1357. int err;
  1358. mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
  1359. c = (struct hda_bind_ctls *)kcontrol->private_value;
  1360. kcontrol->private_value = *c->values;
  1361. err = c->ops->tlv(kcontrol, op_flag, size, tlv);
  1362. kcontrol->private_value = (long)c;
  1363. mutex_unlock(&codec->spdif_mutex);
  1364. return err;
  1365. }
  1366. EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_tlv);
  1367. struct hda_ctl_ops snd_hda_bind_vol = {
  1368. .info = snd_hda_mixer_amp_volume_info,
  1369. .get = snd_hda_mixer_amp_volume_get,
  1370. .put = snd_hda_mixer_amp_volume_put,
  1371. .tlv = snd_hda_mixer_amp_tlv
  1372. };
  1373. EXPORT_SYMBOL_GPL(snd_hda_bind_vol);
  1374. struct hda_ctl_ops snd_hda_bind_sw = {
  1375. .info = snd_hda_mixer_amp_switch_info,
  1376. .get = snd_hda_mixer_amp_switch_get,
  1377. .put = snd_hda_mixer_amp_switch_put,
  1378. .tlv = snd_hda_mixer_amp_tlv
  1379. };
  1380. EXPORT_SYMBOL_GPL(snd_hda_bind_sw);
  1381. /*
  1382. * SPDIF out controls
  1383. */
  1384. static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
  1385. struct snd_ctl_elem_info *uinfo)
  1386. {
  1387. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  1388. uinfo->count = 1;
  1389. return 0;
  1390. }
  1391. static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
  1392. struct snd_ctl_elem_value *ucontrol)
  1393. {
  1394. ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
  1395. IEC958_AES0_NONAUDIO |
  1396. IEC958_AES0_CON_EMPHASIS_5015 |
  1397. IEC958_AES0_CON_NOT_COPYRIGHT;
  1398. ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
  1399. IEC958_AES1_CON_ORIGINAL;
  1400. return 0;
  1401. }
  1402. static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
  1403. struct snd_ctl_elem_value *ucontrol)
  1404. {
  1405. ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
  1406. IEC958_AES0_NONAUDIO |
  1407. IEC958_AES0_PRO_EMPHASIS_5015;
  1408. return 0;
  1409. }
  1410. static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
  1411. struct snd_ctl_elem_value *ucontrol)
  1412. {
  1413. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1414. ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
  1415. ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
  1416. ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
  1417. ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
  1418. return 0;
  1419. }
  1420. /* convert from SPDIF status bits to HDA SPDIF bits
  1421. * bit 0 (DigEn) is always set zero (to be filled later)
  1422. */
  1423. static unsigned short convert_from_spdif_status(unsigned int sbits)
  1424. {
  1425. unsigned short val = 0;
  1426. if (sbits & IEC958_AES0_PROFESSIONAL)
  1427. val |= AC_DIG1_PROFESSIONAL;
  1428. if (sbits & IEC958_AES0_NONAUDIO)
  1429. val |= AC_DIG1_NONAUDIO;
  1430. if (sbits & IEC958_AES0_PROFESSIONAL) {
  1431. if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
  1432. IEC958_AES0_PRO_EMPHASIS_5015)
  1433. val |= AC_DIG1_EMPHASIS;
  1434. } else {
  1435. if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
  1436. IEC958_AES0_CON_EMPHASIS_5015)
  1437. val |= AC_DIG1_EMPHASIS;
  1438. if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
  1439. val |= AC_DIG1_COPYRIGHT;
  1440. if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
  1441. val |= AC_DIG1_LEVEL;
  1442. val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
  1443. }
  1444. return val;
  1445. }
  1446. /* convert to SPDIF status bits from HDA SPDIF bits
  1447. */
  1448. static unsigned int convert_to_spdif_status(unsigned short val)
  1449. {
  1450. unsigned int sbits = 0;
  1451. if (val & AC_DIG1_NONAUDIO)
  1452. sbits |= IEC958_AES0_NONAUDIO;
  1453. if (val & AC_DIG1_PROFESSIONAL)
  1454. sbits |= IEC958_AES0_PROFESSIONAL;
  1455. if (sbits & IEC958_AES0_PROFESSIONAL) {
  1456. if (sbits & AC_DIG1_EMPHASIS)
  1457. sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
  1458. } else {
  1459. if (val & AC_DIG1_EMPHASIS)
  1460. sbits |= IEC958_AES0_CON_EMPHASIS_5015;
  1461. if (!(val & AC_DIG1_COPYRIGHT))
  1462. sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
  1463. if (val & AC_DIG1_LEVEL)
  1464. sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
  1465. sbits |= val & (0x7f << 8);
  1466. }
  1467. return sbits;
  1468. }
  1469. /* set digital convert verbs both for the given NID and its slaves */
  1470. static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
  1471. int verb, int val)
  1472. {
  1473. hda_nid_t *d;
  1474. snd_hda_codec_write_cache(codec, nid, 0, verb, val);
  1475. d = codec->slave_dig_outs;
  1476. if (!d)
  1477. return;
  1478. for (; *d; d++)
  1479. snd_hda_codec_write_cache(codec, *d, 0, verb, val);
  1480. }
  1481. static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
  1482. int dig1, int dig2)
  1483. {
  1484. if (dig1 != -1)
  1485. set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
  1486. if (dig2 != -1)
  1487. set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
  1488. }
  1489. static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
  1490. struct snd_ctl_elem_value *ucontrol)
  1491. {
  1492. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1493. hda_nid_t nid = kcontrol->private_value;
  1494. unsigned short val;
  1495. int change;
  1496. mutex_lock(&codec->spdif_mutex);
  1497. codec->spdif_status = ucontrol->value.iec958.status[0] |
  1498. ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
  1499. ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
  1500. ((unsigned int)ucontrol->value.iec958.status[3] << 24);
  1501. val = convert_from_spdif_status(codec->spdif_status);
  1502. val |= codec->spdif_ctls & 1;
  1503. change = codec->spdif_ctls != val;
  1504. codec->spdif_ctls = val;
  1505. if (change)
  1506. set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
  1507. mutex_unlock(&codec->spdif_mutex);
  1508. return change;
  1509. }
  1510. #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
  1511. static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
  1512. struct snd_ctl_elem_value *ucontrol)
  1513. {
  1514. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1515. ucontrol->value.integer.value[0] = codec->spdif_ctls & AC_DIG1_ENABLE;
  1516. return 0;
  1517. }
  1518. static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
  1519. struct snd_ctl_elem_value *ucontrol)
  1520. {
  1521. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1522. hda_nid_t nid = kcontrol->private_value;
  1523. unsigned short val;
  1524. int change;
  1525. mutex_lock(&codec->spdif_mutex);
  1526. val = codec->spdif_ctls & ~AC_DIG1_ENABLE;
  1527. if (ucontrol->value.integer.value[0])
  1528. val |= AC_DIG1_ENABLE;
  1529. change = codec->spdif_ctls != val;
  1530. if (change) {
  1531. codec->spdif_ctls = val;
  1532. set_dig_out_convert(codec, nid, val & 0xff, -1);
  1533. /* unmute amp switch (if any) */
  1534. if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
  1535. (val & AC_DIG1_ENABLE))
  1536. snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
  1537. HDA_AMP_MUTE, 0);
  1538. }
  1539. mutex_unlock(&codec->spdif_mutex);
  1540. return change;
  1541. }
  1542. static struct snd_kcontrol_new dig_mixes[] = {
  1543. {
  1544. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  1545. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1546. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
  1547. .info = snd_hda_spdif_mask_info,
  1548. .get = snd_hda_spdif_cmask_get,
  1549. },
  1550. {
  1551. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  1552. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1553. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
  1554. .info = snd_hda_spdif_mask_info,
  1555. .get = snd_hda_spdif_pmask_get,
  1556. },
  1557. {
  1558. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1559. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
  1560. .info = snd_hda_spdif_mask_info,
  1561. .get = snd_hda_spdif_default_get,
  1562. .put = snd_hda_spdif_default_put,
  1563. },
  1564. {
  1565. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1566. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
  1567. .info = snd_hda_spdif_out_switch_info,
  1568. .get = snd_hda_spdif_out_switch_get,
  1569. .put = snd_hda_spdif_out_switch_put,
  1570. },
  1571. { } /* end */
  1572. };
  1573. #define SPDIF_MAX_IDX 4 /* 4 instances should be enough to probe */
  1574. /**
  1575. * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
  1576. * @codec: the HDA codec
  1577. * @nid: audio out widget NID
  1578. *
  1579. * Creates controls related with the SPDIF output.
  1580. * Called from each patch supporting the SPDIF out.
  1581. *
  1582. * Returns 0 if successful, or a negative error code.
  1583. */
  1584. int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
  1585. {
  1586. int err;
  1587. struct snd_kcontrol *kctl;
  1588. struct snd_kcontrol_new *dig_mix;
  1589. int idx;
  1590. for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
  1591. if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Playback Switch",
  1592. idx))
  1593. break;
  1594. }
  1595. if (idx >= SPDIF_MAX_IDX) {
  1596. printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
  1597. return -EBUSY;
  1598. }
  1599. for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
  1600. kctl = snd_ctl_new1(dig_mix, codec);
  1601. if (!kctl)
  1602. return -ENOMEM;
  1603. kctl->id.index = idx;
  1604. kctl->private_value = nid;
  1605. err = snd_hda_ctl_add(codec, kctl);
  1606. if (err < 0)
  1607. return err;
  1608. }
  1609. codec->spdif_ctls =
  1610. snd_hda_codec_read(codec, nid, 0,
  1611. AC_VERB_GET_DIGI_CONVERT_1, 0);
  1612. codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
  1613. return 0;
  1614. }
  1615. EXPORT_SYMBOL_GPL(snd_hda_create_spdif_out_ctls);
  1616. /*
  1617. * SPDIF sharing with analog output
  1618. */
  1619. static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
  1620. struct snd_ctl_elem_value *ucontrol)
  1621. {
  1622. struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
  1623. ucontrol->value.integer.value[0] = mout->share_spdif;
  1624. return 0;
  1625. }
  1626. static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
  1627. struct snd_ctl_elem_value *ucontrol)
  1628. {
  1629. struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
  1630. mout->share_spdif = !!ucontrol->value.integer.value[0];
  1631. return 0;
  1632. }
  1633. static struct snd_kcontrol_new spdif_share_sw = {
  1634. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1635. .name = "IEC958 Default PCM Playback Switch",
  1636. .info = snd_ctl_boolean_mono_info,
  1637. .get = spdif_share_sw_get,
  1638. .put = spdif_share_sw_put,
  1639. };
  1640. int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
  1641. struct hda_multi_out *mout)
  1642. {
  1643. if (!mout->dig_out_nid)
  1644. return 0;
  1645. /* ATTENTION: here mout is passed as private_data, instead of codec */
  1646. return snd_hda_ctl_add(codec,
  1647. snd_ctl_new1(&spdif_share_sw, mout));
  1648. }
  1649. EXPORT_SYMBOL_GPL(snd_hda_create_spdif_share_sw);
  1650. /*
  1651. * SPDIF input
  1652. */
  1653. #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
  1654. static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
  1655. struct snd_ctl_elem_value *ucontrol)
  1656. {
  1657. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1658. ucontrol->value.integer.value[0] = codec->spdif_in_enable;
  1659. return 0;
  1660. }
  1661. static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
  1662. struct snd_ctl_elem_value *ucontrol)
  1663. {
  1664. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1665. hda_nid_t nid = kcontrol->private_value;
  1666. unsigned int val = !!ucontrol->value.integer.value[0];
  1667. int change;
  1668. mutex_lock(&codec->spdif_mutex);
  1669. change = codec->spdif_in_enable != val;
  1670. if (change) {
  1671. codec->spdif_in_enable = val;
  1672. snd_hda_codec_write_cache(codec, nid, 0,
  1673. AC_VERB_SET_DIGI_CONVERT_1, val);
  1674. }
  1675. mutex_unlock(&codec->spdif_mutex);
  1676. return change;
  1677. }
  1678. static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
  1679. struct snd_ctl_elem_value *ucontrol)
  1680. {
  1681. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1682. hda_nid_t nid = kcontrol->private_value;
  1683. unsigned short val;
  1684. unsigned int sbits;
  1685. val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
  1686. sbits = convert_to_spdif_status(val);
  1687. ucontrol->value.iec958.status[0] = sbits;
  1688. ucontrol->value.iec958.status[1] = sbits >> 8;
  1689. ucontrol->value.iec958.status[2] = sbits >> 16;
  1690. ucontrol->value.iec958.status[3] = sbits >> 24;
  1691. return 0;
  1692. }
  1693. static struct snd_kcontrol_new dig_in_ctls[] = {
  1694. {
  1695. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1696. .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH),
  1697. .info = snd_hda_spdif_in_switch_info,
  1698. .get = snd_hda_spdif_in_switch_get,
  1699. .put = snd_hda_spdif_in_switch_put,
  1700. },
  1701. {
  1702. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  1703. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1704. .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT),
  1705. .info = snd_hda_spdif_mask_info,
  1706. .get = snd_hda_spdif_in_status_get,
  1707. },
  1708. { } /* end */
  1709. };
  1710. /**
  1711. * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
  1712. * @codec: the HDA codec
  1713. * @nid: audio in widget NID
  1714. *
  1715. * Creates controls related with the SPDIF input.
  1716. * Called from each patch supporting the SPDIF in.
  1717. *
  1718. * Returns 0 if successful, or a negative error code.
  1719. */
  1720. int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
  1721. {
  1722. int err;
  1723. struct snd_kcontrol *kctl;
  1724. struct snd_kcontrol_new *dig_mix;
  1725. int idx;
  1726. for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
  1727. if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Capture Switch",
  1728. idx))
  1729. break;
  1730. }
  1731. if (idx >= SPDIF_MAX_IDX) {
  1732. printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
  1733. return -EBUSY;
  1734. }
  1735. for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
  1736. kctl = snd_ctl_new1(dig_mix, codec);
  1737. kctl->private_value = nid;
  1738. err = snd_hda_ctl_add(codec, kctl);
  1739. if (err < 0)
  1740. return err;
  1741. }
  1742. codec->spdif_in_enable =
  1743. snd_hda_codec_read(codec, nid, 0,
  1744. AC_VERB_GET_DIGI_CONVERT_1, 0) &
  1745. AC_DIG1_ENABLE;
  1746. return 0;
  1747. }
  1748. EXPORT_SYMBOL_GPL(snd_hda_create_spdif_in_ctls);
  1749. #ifdef SND_HDA_NEEDS_RESUME
  1750. /*
  1751. * command cache
  1752. */
  1753. /* build a 32bit cache key with the widget id and the command parameter */
  1754. #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
  1755. #define get_cmd_cache_nid(key) ((key) & 0xff)
  1756. #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
  1757. /**
  1758. * snd_hda_codec_write_cache - send a single command with caching
  1759. * @codec: the HDA codec
  1760. * @nid: NID to send the command
  1761. * @direct: direct flag
  1762. * @verb: the verb to send
  1763. * @parm: the parameter for the verb
  1764. *
  1765. * Send a single command without waiting for response.
  1766. *
  1767. * Returns 0 if successful, or a negative error code.
  1768. */
  1769. int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
  1770. int direct, unsigned int verb, unsigned int parm)
  1771. {
  1772. struct hda_bus *bus = codec->bus;
  1773. unsigned int res;
  1774. int err;
  1775. res = make_codec_cmd(codec, nid, direct, verb, parm);
  1776. snd_hda_power_up(codec);
  1777. mutex_lock(&bus->cmd_mutex);
  1778. err = bus->ops.command(bus, res);
  1779. if (!err) {
  1780. struct hda_cache_head *c;
  1781. u32 key = build_cmd_cache_key(nid, verb);
  1782. c = get_alloc_hash(&codec->cmd_cache, key);
  1783. if (c)
  1784. c->val = parm;
  1785. }
  1786. mutex_unlock(&bus->cmd_mutex);
  1787. snd_hda_power_down(codec);
  1788. return err;
  1789. }
  1790. EXPORT_SYMBOL_GPL(snd_hda_codec_write_cache);
  1791. /* resume the all commands from the cache */
  1792. void snd_hda_codec_resume_cache(struct hda_codec *codec)
  1793. {
  1794. struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
  1795. int i;
  1796. for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
  1797. u32 key = buffer->key;
  1798. if (!key)
  1799. continue;
  1800. snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
  1801. get_cmd_cache_cmd(key), buffer->val);
  1802. }
  1803. }
  1804. EXPORT_SYMBOL_GPL(snd_hda_codec_resume_cache);
  1805. /**
  1806. * snd_hda_sequence_write_cache - sequence writes with caching
  1807. * @codec: the HDA codec
  1808. * @seq: VERB array to send
  1809. *
  1810. * Send the commands sequentially from the given array.
  1811. * Thte commands are recorded on cache for power-save and resume.
  1812. * The array must be terminated with NID=0.
  1813. */
  1814. void snd_hda_sequence_write_cache(struct hda_codec *codec,
  1815. const struct hda_verb *seq)
  1816. {
  1817. for (; seq->nid; seq++)
  1818. snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
  1819. seq->param);
  1820. }
  1821. EXPORT_SYMBOL_GPL(snd_hda_sequence_write_cache);
  1822. #endif /* SND_HDA_NEEDS_RESUME */
  1823. /*
  1824. * set power state of the codec
  1825. */
  1826. static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
  1827. unsigned int power_state)
  1828. {
  1829. hda_nid_t nid;
  1830. int i;
  1831. snd_hda_codec_write(codec, fg, 0, AC_VERB_SET_POWER_STATE,
  1832. power_state);
  1833. msleep(10); /* partial workaround for "azx_get_response timeout" */
  1834. nid = codec->start_nid;
  1835. for (i = 0; i < codec->num_nodes; i++, nid++) {
  1836. unsigned int wcaps = get_wcaps(codec, nid);
  1837. if (wcaps & AC_WCAP_POWER) {
  1838. unsigned int wid_type = (wcaps & AC_WCAP_TYPE) >>
  1839. AC_WCAP_TYPE_SHIFT;
  1840. if (wid_type == AC_WID_PIN) {
  1841. unsigned int pincap;
  1842. /*
  1843. * don't power down the widget if it controls
  1844. * eapd and EAPD_BTLENABLE is set.
  1845. */
  1846. pincap = snd_hda_param_read(codec, nid,
  1847. AC_PAR_PIN_CAP);
  1848. if (pincap & AC_PINCAP_EAPD) {
  1849. int eapd = snd_hda_codec_read(codec,
  1850. nid, 0,
  1851. AC_VERB_GET_EAPD_BTLENABLE, 0);
  1852. eapd &= 0x02;
  1853. if (power_state == AC_PWRST_D3 && eapd)
  1854. continue;
  1855. }
  1856. }
  1857. snd_hda_codec_write(codec, nid, 0,
  1858. AC_VERB_SET_POWER_STATE,
  1859. power_state);
  1860. }
  1861. }
  1862. if (power_state == AC_PWRST_D0) {
  1863. unsigned long end_time;
  1864. int state;
  1865. msleep(10);
  1866. /* wait until the codec reachs to D0 */
  1867. end_time = jiffies + msecs_to_jiffies(500);
  1868. do {
  1869. state = snd_hda_codec_read(codec, fg, 0,
  1870. AC_VERB_GET_POWER_STATE, 0);
  1871. if (state == power_state)
  1872. break;
  1873. msleep(1);
  1874. } while (time_after_eq(end_time, jiffies));
  1875. }
  1876. }
  1877. #ifdef CONFIG_SND_HDA_HWDEP
  1878. /* execute additional init verbs */
  1879. static void hda_exec_init_verbs(struct hda_codec *codec)
  1880. {
  1881. if (codec->init_verbs.list)
  1882. snd_hda_sequence_write(codec, codec->init_verbs.list);
  1883. }
  1884. #else
  1885. static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
  1886. #endif
  1887. #ifdef SND_HDA_NEEDS_RESUME
  1888. /*
  1889. * call suspend and power-down; used both from PM and power-save
  1890. */
  1891. static void hda_call_codec_suspend(struct hda_codec *codec)
  1892. {
  1893. if (codec->patch_ops.suspend)
  1894. codec->patch_ops.suspend(codec, PMSG_SUSPEND);
  1895. hda_set_power_state(codec,
  1896. codec->afg ? codec->afg : codec->mfg,
  1897. AC_PWRST_D3);
  1898. #ifdef CONFIG_SND_HDA_POWER_SAVE
  1899. cancel_delayed_work(&codec->power_work);
  1900. codec->power_on = 0;
  1901. codec->power_transition = 0;
  1902. #endif
  1903. }
  1904. /*
  1905. * kick up codec; used both from PM and power-save
  1906. */
  1907. static void hda_call_codec_resume(struct hda_codec *codec)
  1908. {
  1909. hda_set_power_state(codec,
  1910. codec->afg ? codec->afg : codec->mfg,
  1911. AC_PWRST_D0);
  1912. hda_exec_init_verbs(codec);
  1913. if (codec->patch_ops.resume)
  1914. codec->patch_ops.resume(codec);
  1915. else {
  1916. if (codec->patch_ops.init)
  1917. codec->patch_ops.init(codec);
  1918. snd_hda_codec_resume_amp(codec);
  1919. snd_hda_codec_resume_cache(codec);
  1920. }
  1921. }
  1922. #endif /* SND_HDA_NEEDS_RESUME */
  1923. /**
  1924. * snd_hda_build_controls - build mixer controls
  1925. * @bus: the BUS
  1926. *
  1927. * Creates mixer controls for each codec included in the bus.
  1928. *
  1929. * Returns 0 if successful, otherwise a negative error code.
  1930. */
  1931. int /*__devinit*/ snd_hda_build_controls(struct hda_bus *bus)
  1932. {
  1933. struct hda_codec *codec;
  1934. list_for_each_entry(codec, &bus->codec_list, list) {
  1935. int err = snd_hda_codec_build_controls(codec);
  1936. if (err < 0)
  1937. return err;
  1938. }
  1939. return 0;
  1940. }
  1941. EXPORT_SYMBOL_GPL(snd_hda_build_controls);
  1942. int snd_hda_codec_build_controls(struct hda_codec *codec)
  1943. {
  1944. int err = 0;
  1945. /* fake as if already powered-on */
  1946. hda_keep_power_on(codec);
  1947. /* then fire up */
  1948. hda_set_power_state(codec,
  1949. codec->afg ? codec->afg : codec->mfg,
  1950. AC_PWRST_D0);
  1951. hda_exec_init_verbs(codec);
  1952. /* continue to initialize... */
  1953. if (codec->patch_ops.init)
  1954. err = codec->patch_ops.init(codec);
  1955. if (!err && codec->patch_ops.build_controls)
  1956. err = codec->patch_ops.build_controls(codec);
  1957. snd_hda_power_down(codec);
  1958. if (err < 0)
  1959. return err;
  1960. return 0;
  1961. }
  1962. /*
  1963. * stream formats
  1964. */
  1965. struct hda_rate_tbl {
  1966. unsigned int hz;
  1967. unsigned int alsa_bits;
  1968. unsigned int hda_fmt;
  1969. };
  1970. static struct hda_rate_tbl rate_bits[] = {
  1971. /* rate in Hz, ALSA rate bitmask, HDA format value */
  1972. /* autodetected value used in snd_hda_query_supported_pcm */
  1973. { 8000, SNDRV_PCM_RATE_8000, 0x0500 }, /* 1/6 x 48 */
  1974. { 11025, SNDRV_PCM_RATE_11025, 0x4300 }, /* 1/4 x 44 */
  1975. { 16000, SNDRV_PCM_RATE_16000, 0x0200 }, /* 1/3 x 48 */
  1976. { 22050, SNDRV_PCM_RATE_22050, 0x4100 }, /* 1/2 x 44 */
  1977. { 32000, SNDRV_PCM_RATE_32000, 0x0a00 }, /* 2/3 x 48 */
  1978. { 44100, SNDRV_PCM_RATE_44100, 0x4000 }, /* 44 */
  1979. { 48000, SNDRV_PCM_RATE_48000, 0x0000 }, /* 48 */
  1980. { 88200, SNDRV_PCM_RATE_88200, 0x4800 }, /* 2 x 44 */
  1981. { 96000, SNDRV_PCM_RATE_96000, 0x0800 }, /* 2 x 48 */
  1982. { 176400, SNDRV_PCM_RATE_176400, 0x5800 },/* 4 x 44 */
  1983. { 192000, SNDRV_PCM_RATE_192000, 0x1800 }, /* 4 x 48 */
  1984. #define AC_PAR_PCM_RATE_BITS 11
  1985. /* up to bits 10, 384kHZ isn't supported properly */
  1986. /* not autodetected value */
  1987. { 9600, SNDRV_PCM_RATE_KNOT, 0x0400 }, /* 1/5 x 48 */
  1988. { 0 } /* terminator */
  1989. };
  1990. /**
  1991. * snd_hda_calc_stream_format - calculate format bitset
  1992. * @rate: the sample rate
  1993. * @channels: the number of channels
  1994. * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
  1995. * @maxbps: the max. bps
  1996. *
  1997. * Calculate the format bitset from the given rate, channels and th PCM format.
  1998. *
  1999. * Return zero if invalid.
  2000. */
  2001. unsigned int snd_hda_calc_stream_format(unsigned int rate,
  2002. unsigned int channels,
  2003. unsigned int format,
  2004. unsigned int maxbps)
  2005. {
  2006. int i;
  2007. unsigned int val = 0;
  2008. for (i = 0; rate_bits[i].hz; i++)
  2009. if (rate_bits[i].hz == rate) {
  2010. val = rate_bits[i].hda_fmt;
  2011. break;
  2012. }
  2013. if (!rate_bits[i].hz) {
  2014. snd_printdd("invalid rate %d\n", rate);
  2015. return 0;
  2016. }
  2017. if (channels == 0 || channels > 8) {
  2018. snd_printdd("invalid channels %d\n", channels);
  2019. return 0;
  2020. }
  2021. val |= channels - 1;
  2022. switch (snd_pcm_format_width(format)) {
  2023. case 8: val |= 0x00; break;
  2024. case 16: val |= 0x10; break;
  2025. case 20:
  2026. case 24:
  2027. case 32:
  2028. if (maxbps >= 32)
  2029. val |= 0x40;
  2030. else if (maxbps >= 24)
  2031. val |= 0x30;
  2032. else
  2033. val |= 0x20;
  2034. break;
  2035. default:
  2036. snd_printdd("invalid format width %d\n",
  2037. snd_pcm_format_width(format));
  2038. return 0;
  2039. }
  2040. return val;
  2041. }
  2042. EXPORT_SYMBOL_GPL(snd_hda_calc_stream_format);
  2043. /**
  2044. * snd_hda_query_supported_pcm - query the supported PCM rates and formats
  2045. * @codec: the HDA codec
  2046. * @nid: NID to query
  2047. * @ratesp: the pointer to store the detected rate bitflags
  2048. * @formatsp: the pointer to store the detected formats
  2049. * @bpsp: the pointer to store the detected format widths
  2050. *
  2051. * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
  2052. * or @bsps argument is ignored.
  2053. *
  2054. * Returns 0 if successful, otherwise a negative error code.
  2055. */
  2056. static int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
  2057. u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
  2058. {
  2059. int i;
  2060. unsigned int val, streams;
  2061. val = 0;
  2062. if (nid != codec->afg &&
  2063. (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
  2064. val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
  2065. if (val == -1)
  2066. return -EIO;
  2067. }
  2068. if (!val)
  2069. val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
  2070. if (ratesp) {
  2071. u32 rates = 0;
  2072. for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
  2073. if (val & (1 << i))
  2074. rates |= rate_bits[i].alsa_bits;
  2075. }
  2076. *ratesp = rates;
  2077. }
  2078. if (formatsp || bpsp) {
  2079. u64 formats = 0;
  2080. unsigned int bps;
  2081. unsigned int wcaps;
  2082. wcaps = get_wcaps(codec, nid);
  2083. streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
  2084. if (streams == -1)
  2085. return -EIO;
  2086. if (!streams) {
  2087. streams = snd_hda_param_read(codec, codec->afg,
  2088. AC_PAR_STREAM);
  2089. if (streams == -1)
  2090. return -EIO;
  2091. }
  2092. bps = 0;
  2093. if (streams & AC_SUPFMT_PCM) {
  2094. if (val & AC_SUPPCM_BITS_8) {
  2095. formats |= SNDRV_PCM_FMTBIT_U8;
  2096. bps = 8;
  2097. }
  2098. if (val & AC_SUPPCM_BITS_16) {
  2099. formats |= SNDRV_PCM_FMTBIT_S16_LE;
  2100. bps = 16;
  2101. }
  2102. if (wcaps & AC_WCAP_DIGITAL) {
  2103. if (val & AC_SUPPCM_BITS_32)
  2104. formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
  2105. if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
  2106. formats |= SNDRV_PCM_FMTBIT_S32_LE;
  2107. if (val & AC_SUPPCM_BITS_24)
  2108. bps = 24;
  2109. else if (val & AC_SUPPCM_BITS_20)
  2110. bps = 20;
  2111. } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
  2112. AC_SUPPCM_BITS_32)) {
  2113. formats |= SNDRV_PCM_FMTBIT_S32_LE;
  2114. if (val & AC_SUPPCM_BITS_32)
  2115. bps = 32;
  2116. else if (val & AC_SUPPCM_BITS_24)
  2117. bps = 24;
  2118. else if (val & AC_SUPPCM_BITS_20)
  2119. bps = 20;
  2120. }
  2121. }
  2122. else if (streams == AC_SUPFMT_FLOAT32) {
  2123. /* should be exclusive */
  2124. formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
  2125. bps = 32;
  2126. } else if (streams == AC_SUPFMT_AC3) {
  2127. /* should be exclusive */
  2128. /* temporary hack: we have still no proper support
  2129. * for the direct AC3 stream...
  2130. */
  2131. formats |= SNDRV_PCM_FMTBIT_U8;
  2132. bps = 8;
  2133. }
  2134. if (formatsp)
  2135. *formatsp = formats;
  2136. if (bpsp)
  2137. *bpsp = bps;
  2138. }
  2139. return 0;
  2140. }
  2141. /**
  2142. * snd_hda_is_supported_format - check whether the given node supports
  2143. * the format val
  2144. *
  2145. * Returns 1 if supported, 0 if not.
  2146. */
  2147. int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
  2148. unsigned int format)
  2149. {
  2150. int i;
  2151. unsigned int val = 0, rate, stream;
  2152. if (nid != codec->afg &&
  2153. (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
  2154. val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
  2155. if (val == -1)
  2156. return 0;
  2157. }
  2158. if (!val) {
  2159. val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
  2160. if (val == -1)
  2161. return 0;
  2162. }
  2163. rate = format & 0xff00;
  2164. for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
  2165. if (rate_bits[i].hda_fmt == rate) {
  2166. if (val & (1 << i))
  2167. break;
  2168. return 0;
  2169. }
  2170. if (i >= AC_PAR_PCM_RATE_BITS)
  2171. return 0;
  2172. stream = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
  2173. if (stream == -1)
  2174. return 0;
  2175. if (!stream && nid != codec->afg)
  2176. stream = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
  2177. if (!stream || stream == -1)
  2178. return 0;
  2179. if (stream & AC_SUPFMT_PCM) {
  2180. switch (format & 0xf0) {
  2181. case 0x00:
  2182. if (!(val & AC_SUPPCM_BITS_8))
  2183. return 0;
  2184. break;
  2185. case 0x10:
  2186. if (!(val & AC_SUPPCM_BITS_16))
  2187. return 0;
  2188. break;
  2189. case 0x20:
  2190. if (!(val & AC_SUPPCM_BITS_20))
  2191. return 0;
  2192. break;
  2193. case 0x30:
  2194. if (!(val & AC_SUPPCM_BITS_24))
  2195. return 0;
  2196. break;
  2197. case 0x40:
  2198. if (!(val & AC_SUPPCM_BITS_32))
  2199. return 0;
  2200. break;
  2201. default:
  2202. return 0;
  2203. }
  2204. } else {
  2205. /* FIXME: check for float32 and AC3? */
  2206. }
  2207. return 1;
  2208. }
  2209. EXPORT_SYMBOL_GPL(snd_hda_is_supported_format);
  2210. /*
  2211. * PCM stuff
  2212. */
  2213. static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
  2214. struct hda_codec *codec,
  2215. struct snd_pcm_substream *substream)
  2216. {
  2217. return 0;
  2218. }
  2219. static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
  2220. struct hda_codec *codec,
  2221. unsigned int stream_tag,
  2222. unsigned int format,
  2223. struct snd_pcm_substream *substream)
  2224. {
  2225. snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
  2226. return 0;
  2227. }
  2228. static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
  2229. struct hda_codec *codec,
  2230. struct snd_pcm_substream *substream)
  2231. {
  2232. snd_hda_codec_cleanup_stream(codec, hinfo->nid);
  2233. return 0;
  2234. }
  2235. static int set_pcm_default_values(struct hda_codec *codec,
  2236. struct hda_pcm_stream *info)
  2237. {
  2238. /* query support PCM information from the given NID */
  2239. if (info->nid && (!info->rates || !info->formats)) {
  2240. snd_hda_query_supported_pcm(codec, info->nid,
  2241. info->rates ? NULL : &info->rates,
  2242. info->formats ? NULL : &info->formats,
  2243. info->maxbps ? NULL : &info->maxbps);
  2244. }
  2245. if (info->ops.open == NULL)
  2246. info->ops.open = hda_pcm_default_open_close;
  2247. if (info->ops.close == NULL)
  2248. info->ops.close = hda_pcm_default_open_close;
  2249. if (info->ops.prepare == NULL) {
  2250. if (snd_BUG_ON(!info->nid))
  2251. return -EINVAL;
  2252. info->ops.prepare = hda_pcm_default_prepare;
  2253. }
  2254. if (info->ops.cleanup == NULL) {
  2255. if (snd_BUG_ON(!info->nid))
  2256. return -EINVAL;
  2257. info->ops.cleanup = hda_pcm_default_cleanup;
  2258. }
  2259. return 0;
  2260. }
  2261. /*
  2262. * get the empty PCM device number to assign
  2263. */
  2264. static int get_empty_pcm_device(struct hda_bus *bus, int type)
  2265. {
  2266. static const char *dev_name[HDA_PCM_NTYPES] = {
  2267. "Audio", "SPDIF", "HDMI", "Modem"
  2268. };
  2269. /* starting device index for each PCM type */
  2270. static int dev_idx[HDA_PCM_NTYPES] = {
  2271. [HDA_PCM_TYPE_AUDIO] = 0,
  2272. [HDA_PCM_TYPE_SPDIF] = 1,
  2273. [HDA_PCM_TYPE_HDMI] = 3,
  2274. [HDA_PCM_TYPE_MODEM] = 6
  2275. };
  2276. /* normal audio device indices; not linear to keep compatibility */
  2277. static int audio_idx[4] = { 0, 2, 4, 5 };
  2278. int i, dev;
  2279. switch (type) {
  2280. case HDA_PCM_TYPE_AUDIO:
  2281. for (i = 0; i < ARRAY_SIZE(audio_idx); i++) {
  2282. dev = audio_idx[i];
  2283. if (!test_bit(dev, bus->pcm_dev_bits))
  2284. break;
  2285. }
  2286. if (i >= ARRAY_SIZE(audio_idx)) {
  2287. snd_printk(KERN_WARNING "Too many audio devices\n");
  2288. return -EAGAIN;
  2289. }
  2290. break;
  2291. case HDA_PCM_TYPE_SPDIF:
  2292. case HDA_PCM_TYPE_HDMI:
  2293. case HDA_PCM_TYPE_MODEM:
  2294. dev = dev_idx[type];
  2295. if (test_bit(dev, bus->pcm_dev_bits)) {
  2296. snd_printk(KERN_WARNING "%s already defined\n",
  2297. dev_name[type]);
  2298. return -EAGAIN;
  2299. }
  2300. break;
  2301. default:
  2302. snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
  2303. return -EINVAL;
  2304. }
  2305. set_bit(dev, bus->pcm_dev_bits);
  2306. return dev;
  2307. }
  2308. /*
  2309. * attach a new PCM stream
  2310. */
  2311. static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
  2312. {
  2313. struct hda_bus *bus = codec->bus;
  2314. struct hda_pcm_stream *info;
  2315. int stream, err;
  2316. if (snd_BUG_ON(!pcm->name))
  2317. return -EINVAL;
  2318. for (stream = 0; stream < 2; stream++) {
  2319. info = &pcm->stream[stream];
  2320. if (info->substreams) {
  2321. err = set_pcm_default_values(codec, info);
  2322. if (err < 0)
  2323. return err;
  2324. }
  2325. }
  2326. return bus->ops.attach_pcm(bus, codec, pcm);
  2327. }
  2328. /* assign all PCMs of the given codec */
  2329. int snd_hda_codec_build_pcms(struct hda_codec *codec)
  2330. {
  2331. unsigned int pcm;
  2332. int err;
  2333. if (!codec->num_pcms) {
  2334. if (!codec->patch_ops.build_pcms)
  2335. return 0;
  2336. err = codec->patch_ops.build_pcms(codec);
  2337. if (err < 0)
  2338. return err;
  2339. }
  2340. for (pcm = 0; pcm < codec->num_pcms; pcm++) {
  2341. struct hda_pcm *cpcm = &codec->pcm_info[pcm];
  2342. int dev;
  2343. if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
  2344. return 0; /* no substreams assigned */
  2345. if (!cpcm->pcm) {
  2346. dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
  2347. if (dev < 0)
  2348. return 0;
  2349. cpcm->device = dev;
  2350. err = snd_hda_attach_pcm(codec, cpcm);
  2351. if (err < 0)
  2352. return err;
  2353. }
  2354. }
  2355. return 0;
  2356. }
  2357. /**
  2358. * snd_hda_build_pcms - build PCM information
  2359. * @bus: the BUS
  2360. *
  2361. * Create PCM information for each codec included in the bus.
  2362. *
  2363. * The build_pcms codec patch is requested to set up codec->num_pcms and
  2364. * codec->pcm_info properly. The array is referred by the top-level driver
  2365. * to create its PCM instances.
  2366. * The allocated codec->pcm_info should be released in codec->patch_ops.free
  2367. * callback.
  2368. *
  2369. * At least, substreams, channels_min and channels_max must be filled for
  2370. * each stream. substreams = 0 indicates that the stream doesn't exist.
  2371. * When rates and/or formats are zero, the supported values are queried
  2372. * from the given nid. The nid is used also by the default ops.prepare
  2373. * and ops.cleanup callbacks.
  2374. *
  2375. * The driver needs to call ops.open in its open callback. Similarly,
  2376. * ops.close is supposed to be called in the close callback.
  2377. * ops.prepare should be called in the prepare or hw_params callback
  2378. * with the proper parameters for set up.
  2379. * ops.cleanup should be called in hw_free for clean up of streams.
  2380. *
  2381. * This function returns 0 if successfull, or a negative error code.
  2382. */
  2383. int __devinit snd_hda_build_pcms(struct hda_bus *bus)
  2384. {
  2385. struct hda_codec *codec;
  2386. list_for_each_entry(codec, &bus->codec_list, list) {
  2387. int err = snd_hda_codec_build_pcms(codec);
  2388. if (err < 0)
  2389. return err;
  2390. }
  2391. return 0;
  2392. }
  2393. EXPORT_SYMBOL_GPL(snd_hda_build_pcms);
  2394. /**
  2395. * snd_hda_check_board_config - compare the current codec with the config table
  2396. * @codec: the HDA codec
  2397. * @num_configs: number of config enums
  2398. * @models: array of model name strings
  2399. * @tbl: configuration table, terminated by null entries
  2400. *
  2401. * Compares the modelname or PCI subsystem id of the current codec with the
  2402. * given configuration table. If a matching entry is found, returns its
  2403. * config value (supposed to be 0 or positive).
  2404. *
  2405. * If no entries are matching, the function returns a negative value.
  2406. */
  2407. int snd_hda_check_board_config(struct hda_codec *codec,
  2408. int num_configs, const char **models,
  2409. const struct snd_pci_quirk *tbl)
  2410. {
  2411. if (codec->modelname && models) {
  2412. int i;
  2413. for (i = 0; i < num_configs; i++) {
  2414. if (models[i] &&
  2415. !strcmp(codec->modelname, models[i])) {
  2416. snd_printd(KERN_INFO "hda_codec: model '%s' is "
  2417. "selected\n", models[i]);
  2418. return i;
  2419. }
  2420. }
  2421. }
  2422. if (!codec->bus->pci || !tbl)
  2423. return -1;
  2424. tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
  2425. if (!tbl)
  2426. return -1;
  2427. if (tbl->value >= 0 && tbl->value < num_configs) {
  2428. #ifdef CONFIG_SND_DEBUG_VERBOSE
  2429. char tmp[10];
  2430. const char *model = NULL;
  2431. if (models)
  2432. model = models[tbl->value];
  2433. if (!model) {
  2434. sprintf(tmp, "#%d", tbl->value);
  2435. model = tmp;
  2436. }
  2437. snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
  2438. "for config %x:%x (%s)\n",
  2439. model, tbl->subvendor, tbl->subdevice,
  2440. (tbl->name ? tbl->name : "Unknown device"));
  2441. #endif
  2442. return tbl->value;
  2443. }
  2444. return -1;
  2445. }
  2446. EXPORT_SYMBOL_GPL(snd_hda_check_board_config);
  2447. /**
  2448. * snd_hda_add_new_ctls - create controls from the array
  2449. * @codec: the HDA codec
  2450. * @knew: the array of struct snd_kcontrol_new
  2451. *
  2452. * This helper function creates and add new controls in the given array.
  2453. * The array must be terminated with an empty entry as terminator.
  2454. *
  2455. * Returns 0 if successful, or a negative error code.
  2456. */
  2457. int snd_hda_add_new_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
  2458. {
  2459. int err;
  2460. for (; knew->name; knew++) {
  2461. struct snd_kcontrol *kctl;
  2462. kctl = snd_ctl_new1(knew, codec);
  2463. if (!kctl)
  2464. return -ENOMEM;
  2465. err = snd_hda_ctl_add(codec, kctl);
  2466. if (err < 0) {
  2467. if (!codec->addr)
  2468. return err;
  2469. kctl = snd_ctl_new1(knew, codec);
  2470. if (!kctl)
  2471. return -ENOMEM;
  2472. kctl->id.device = codec->addr;
  2473. err = snd_hda_ctl_add(codec, kctl);
  2474. if (err < 0)
  2475. return err;
  2476. }
  2477. }
  2478. return 0;
  2479. }
  2480. EXPORT_SYMBOL_GPL(snd_hda_add_new_ctls);
  2481. #ifdef CONFIG_SND_HDA_POWER_SAVE
  2482. static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
  2483. unsigned int power_state);
  2484. static void hda_power_work(struct work_struct *work)
  2485. {
  2486. struct hda_codec *codec =
  2487. container_of(work, struct hda_codec, power_work.work);
  2488. struct hda_bus *bus = codec->bus;
  2489. if (!codec->power_on || codec->power_count) {
  2490. codec->power_transition = 0;
  2491. return;
  2492. }
  2493. hda_call_codec_suspend(codec);
  2494. if (bus->ops.pm_notify)
  2495. bus->ops.pm_notify(bus);
  2496. }
  2497. static void hda_keep_power_on(struct hda_codec *codec)
  2498. {
  2499. codec->power_count++;
  2500. codec->power_on = 1;
  2501. }
  2502. void snd_hda_power_up(struct hda_codec *codec)
  2503. {
  2504. struct hda_bus *bus = codec->bus;
  2505. codec->power_count++;
  2506. if (codec->power_on || codec->power_transition)
  2507. return;
  2508. codec->power_on = 1;
  2509. if (bus->ops.pm_notify)
  2510. bus->ops.pm_notify(bus);
  2511. hda_call_codec_resume(codec);
  2512. cancel_delayed_work(&codec->power_work);
  2513. codec->power_transition = 0;
  2514. }
  2515. EXPORT_SYMBOL_GPL(snd_hda_power_up);
  2516. #define power_save(codec) \
  2517. ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
  2518. #define power_save(codec) \
  2519. ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
  2520. void snd_hda_power_down(struct hda_codec *codec)
  2521. {
  2522. --codec->power_count;
  2523. if (!codec->power_on || codec->power_count || codec->power_transition)
  2524. return;
  2525. if (power_save(codec)) {
  2526. codec->power_transition = 1; /* avoid reentrance */
  2527. schedule_delayed_work(&codec->power_work,
  2528. msecs_to_jiffies(power_save(codec) * 1000));
  2529. }
  2530. }
  2531. EXPORT_SYMBOL_GPL(snd_hda_power_down);
  2532. int snd_hda_check_amp_list_power(struct hda_codec *codec,
  2533. struct hda_loopback_check *check,
  2534. hda_nid_t nid)
  2535. {
  2536. struct hda_amp_list *p;
  2537. int ch, v;
  2538. if (!check->amplist)
  2539. return 0;
  2540. for (p = check->amplist; p->nid; p++) {
  2541. if (p->nid == nid)
  2542. break;
  2543. }
  2544. if (!p->nid)
  2545. return 0; /* nothing changed */
  2546. for (p = check->amplist; p->nid; p++) {
  2547. for (ch = 0; ch < 2; ch++) {
  2548. v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
  2549. p->idx);
  2550. if (!(v & HDA_AMP_MUTE) && v > 0) {
  2551. if (!check->power_on) {
  2552. check->power_on = 1;
  2553. snd_hda_power_up(codec);
  2554. }
  2555. return 1;
  2556. }
  2557. }
  2558. }
  2559. if (check->power_on) {
  2560. check->power_on = 0;
  2561. snd_hda_power_down(codec);
  2562. }
  2563. return 0;
  2564. }
  2565. EXPORT_SYMBOL_GPL(snd_hda_check_amp_list_power);
  2566. #endif
  2567. /*
  2568. * Channel mode helper
  2569. */
  2570. int snd_hda_ch_mode_info(struct hda_codec *codec,
  2571. struct snd_ctl_elem_info *uinfo,
  2572. const struct hda_channel_mode *chmode,
  2573. int num_chmodes)
  2574. {
  2575. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  2576. uinfo->count = 1;
  2577. uinfo->value.enumerated.items = num_chmodes;
  2578. if (uinfo->value.enumerated.item >= num_chmodes)
  2579. uinfo->value.enumerated.item = num_chmodes - 1;
  2580. sprintf(uinfo->value.enumerated.name, "%dch",
  2581. chmode[uinfo->value.enumerated.item].channels);
  2582. return 0;
  2583. }
  2584. EXPORT_SYMBOL_GPL(snd_hda_ch_mode_info);
  2585. int snd_hda_ch_mode_get(struct hda_codec *codec,
  2586. struct snd_ctl_elem_value *ucontrol,
  2587. const struct hda_channel_mode *chmode,
  2588. int num_chmodes,
  2589. int max_channels)
  2590. {
  2591. int i;
  2592. for (i = 0; i < num_chmodes; i++) {
  2593. if (max_channels == chmode[i].channels) {
  2594. ucontrol->value.enumerated.item[0] = i;
  2595. break;
  2596. }
  2597. }
  2598. return 0;
  2599. }
  2600. EXPORT_SYMBOL_GPL(snd_hda_ch_mode_get);
  2601. int snd_hda_ch_mode_put(struct hda_codec *codec,
  2602. struct snd_ctl_elem_value *ucontrol,
  2603. const struct hda_channel_mode *chmode,
  2604. int num_chmodes,
  2605. int *max_channelsp)
  2606. {
  2607. unsigned int mode;
  2608. mode = ucontrol->value.enumerated.item[0];
  2609. if (mode >= num_chmodes)
  2610. return -EINVAL;
  2611. if (*max_channelsp == chmode[mode].channels)
  2612. return 0;
  2613. /* change the current channel setting */
  2614. *max_channelsp = chmode[mode].channels;
  2615. if (chmode[mode].sequence)
  2616. snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
  2617. return 1;
  2618. }
  2619. EXPORT_SYMBOL_GPL(snd_hda_ch_mode_put);
  2620. /*
  2621. * input MUX helper
  2622. */
  2623. int snd_hda_input_mux_info(const struct hda_input_mux *imux,
  2624. struct snd_ctl_elem_info *uinfo)
  2625. {
  2626. unsigned int index;
  2627. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  2628. uinfo->count = 1;
  2629. uinfo->value.enumerated.items = imux->num_items;
  2630. if (!imux->num_items)
  2631. return 0;
  2632. index = uinfo->value.enumerated.item;
  2633. if (index >= imux->num_items)
  2634. index = imux->num_items - 1;
  2635. strcpy(uinfo->value.enumerated.name, imux->items[index].label);
  2636. return 0;
  2637. }
  2638. EXPORT_SYMBOL_GPL(snd_hda_input_mux_info);
  2639. int snd_hda_input_mux_put(struct hda_codec *codec,
  2640. const struct hda_input_mux *imux,
  2641. struct snd_ctl_elem_value *ucontrol,
  2642. hda_nid_t nid,
  2643. unsigned int *cur_val)
  2644. {
  2645. unsigned int idx;
  2646. if (!imux->num_items)
  2647. return 0;
  2648. idx = ucontrol->value.enumerated.item[0];
  2649. if (idx >= imux->num_items)
  2650. idx = imux->num_items - 1;
  2651. if (*cur_val == idx)
  2652. return 0;
  2653. snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
  2654. imux->items[idx].index);
  2655. *cur_val = idx;
  2656. return 1;
  2657. }
  2658. EXPORT_SYMBOL_GPL(snd_hda_input_mux_put);
  2659. /*
  2660. * Multi-channel / digital-out PCM helper functions
  2661. */
  2662. /* setup SPDIF output stream */
  2663. static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
  2664. unsigned int stream_tag, unsigned int format)
  2665. {
  2666. /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
  2667. if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
  2668. set_dig_out_convert(codec, nid,
  2669. codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff,
  2670. -1);
  2671. snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
  2672. if (codec->slave_dig_outs) {
  2673. hda_nid_t *d;
  2674. for (d = codec->slave_dig_outs; *d; d++)
  2675. snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
  2676. format);
  2677. }
  2678. /* turn on again (if needed) */
  2679. if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
  2680. set_dig_out_convert(codec, nid,
  2681. codec->spdif_ctls & 0xff, -1);
  2682. }
  2683. static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
  2684. {
  2685. snd_hda_codec_cleanup_stream(codec, nid);
  2686. if (codec->slave_dig_outs) {
  2687. hda_nid_t *d;
  2688. for (d = codec->slave_dig_outs; *d; d++)
  2689. snd_hda_codec_cleanup_stream(codec, *d);
  2690. }
  2691. }
  2692. /*
  2693. * open the digital out in the exclusive mode
  2694. */
  2695. int snd_hda_multi_out_dig_open(struct hda_codec *codec,
  2696. struct hda_multi_out *mout)
  2697. {
  2698. mutex_lock(&codec->spdif_mutex);
  2699. if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
  2700. /* already opened as analog dup; reset it once */
  2701. cleanup_dig_out_stream(codec, mout->dig_out_nid);
  2702. mout->dig_out_used = HDA_DIG_EXCLUSIVE;
  2703. mutex_unlock(&codec->spdif_mutex);
  2704. return 0;
  2705. }
  2706. EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_open);
  2707. int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
  2708. struct hda_multi_out *mout,
  2709. unsigned int stream_tag,
  2710. unsigned int format,
  2711. struct snd_pcm_substream *substream)
  2712. {
  2713. mutex_lock(&codec->spdif_mutex);
  2714. setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
  2715. mutex_unlock(&codec->spdif_mutex);
  2716. return 0;
  2717. }
  2718. EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_prepare);
  2719. /*
  2720. * release the digital out
  2721. */
  2722. int snd_hda_multi_out_dig_close(struct hda_codec *codec,
  2723. struct hda_multi_out *mout)
  2724. {
  2725. mutex_lock(&codec->spdif_mutex);
  2726. mout->dig_out_used = 0;
  2727. mutex_unlock(&codec->spdif_mutex);
  2728. return 0;
  2729. }
  2730. EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_close);
  2731. /*
  2732. * set up more restrictions for analog out
  2733. */
  2734. int snd_hda_multi_out_analog_open(struct hda_codec *codec,
  2735. struct hda_multi_out *mout,
  2736. struct snd_pcm_substream *substream,
  2737. struct hda_pcm_stream *hinfo)
  2738. {
  2739. struct snd_pcm_runtime *runtime = substream->runtime;
  2740. runtime->hw.channels_max = mout->max_channels;
  2741. if (mout->dig_out_nid) {
  2742. if (!mout->analog_rates) {
  2743. mout->analog_rates = hinfo->rates;
  2744. mout->analog_formats = hinfo->formats;
  2745. mout->analog_maxbps = hinfo->maxbps;
  2746. } else {
  2747. runtime->hw.rates = mout->analog_rates;
  2748. runtime->hw.formats = mout->analog_formats;
  2749. hinfo->maxbps = mout->analog_maxbps;
  2750. }
  2751. if (!mout->spdif_rates) {
  2752. snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
  2753. &mout->spdif_rates,
  2754. &mout->spdif_formats,
  2755. &mout->spdif_maxbps);
  2756. }
  2757. mutex_lock(&codec->spdif_mutex);
  2758. if (mout->share_spdif) {
  2759. runtime->hw.rates &= mout->spdif_rates;
  2760. runtime->hw.formats &= mout->spdif_formats;
  2761. if (mout->spdif_maxbps < hinfo->maxbps)
  2762. hinfo->maxbps = mout->spdif_maxbps;
  2763. }
  2764. mutex_unlock(&codec->spdif_mutex);
  2765. }
  2766. return snd_pcm_hw_constraint_step(substream->runtime, 0,
  2767. SNDRV_PCM_HW_PARAM_CHANNELS, 2);
  2768. }
  2769. EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_open);
  2770. /*
  2771. * set up the i/o for analog out
  2772. * when the digital out is available, copy the front out to digital out, too.
  2773. */
  2774. int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
  2775. struct hda_multi_out *mout,
  2776. unsigned int stream_tag,
  2777. unsigned int format,
  2778. struct snd_pcm_substream *substream)
  2779. {
  2780. hda_nid_t *nids = mout->dac_nids;
  2781. int chs = substream->runtime->channels;
  2782. int i;
  2783. mutex_lock(&codec->spdif_mutex);
  2784. if (mout->dig_out_nid && mout->share_spdif &&
  2785. mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
  2786. if (chs == 2 &&
  2787. snd_hda_is_supported_format(codec, mout->dig_out_nid,
  2788. format) &&
  2789. !(codec->spdif_status & IEC958_AES0_NONAUDIO)) {
  2790. mout->dig_out_used = HDA_DIG_ANALOG_DUP;
  2791. setup_dig_out_stream(codec, mout->dig_out_nid,
  2792. stream_tag, format);
  2793. } else {
  2794. mout->dig_out_used = 0;
  2795. cleanup_dig_out_stream(codec, mout->dig_out_nid);
  2796. }
  2797. }
  2798. mutex_unlock(&codec->spdif_mutex);
  2799. /* front */
  2800. snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
  2801. 0, format);
  2802. if (!mout->no_share_stream &&
  2803. mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
  2804. /* headphone out will just decode front left/right (stereo) */
  2805. snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
  2806. 0, format);
  2807. /* extra outputs copied from front */
  2808. for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
  2809. if (!mout->no_share_stream && mout->extra_out_nid[i])
  2810. snd_hda_codec_setup_stream(codec,
  2811. mout->extra_out_nid[i],
  2812. stream_tag, 0, format);
  2813. /* surrounds */
  2814. for (i = 1; i < mout->num_dacs; i++) {
  2815. if (chs >= (i + 1) * 2) /* independent out */
  2816. snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
  2817. i * 2, format);
  2818. else if (!mout->no_share_stream) /* copy front */
  2819. snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
  2820. 0, format);
  2821. }
  2822. return 0;
  2823. }
  2824. EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_prepare);
  2825. /*
  2826. * clean up the setting for analog out
  2827. */
  2828. int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
  2829. struct hda_multi_out *mout)
  2830. {
  2831. hda_nid_t *nids = mout->dac_nids;
  2832. int i;
  2833. for (i = 0; i < mout->num_dacs; i++)
  2834. snd_hda_codec_cleanup_stream(codec, nids[i]);
  2835. if (mout->hp_nid)
  2836. snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
  2837. for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
  2838. if (mout->extra_out_nid[i])
  2839. snd_hda_codec_cleanup_stream(codec,
  2840. mout->extra_out_nid[i]);
  2841. mutex_lock(&codec->spdif_mutex);
  2842. if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
  2843. cleanup_dig_out_stream(codec, mout->dig_out_nid);
  2844. mout->dig_out_used = 0;
  2845. }
  2846. mutex_unlock(&codec->spdif_mutex);
  2847. return 0;
  2848. }
  2849. EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_cleanup);
  2850. /*
  2851. * Helper for automatic pin configuration
  2852. */
  2853. static int is_in_nid_list(hda_nid_t nid, hda_nid_t *list)
  2854. {
  2855. for (; *list; list++)
  2856. if (*list == nid)
  2857. return 1;
  2858. return 0;
  2859. }
  2860. /*
  2861. * Sort an associated group of pins according to their sequence numbers.
  2862. */
  2863. static void sort_pins_by_sequence(hda_nid_t * pins, short * sequences,
  2864. int num_pins)
  2865. {
  2866. int i, j;
  2867. short seq;
  2868. hda_nid_t nid;
  2869. for (i = 0; i < num_pins; i++) {
  2870. for (j = i + 1; j < num_pins; j++) {
  2871. if (sequences[i] > sequences[j]) {
  2872. seq = sequences[i];
  2873. sequences[i] = sequences[j];
  2874. sequences[j] = seq;
  2875. nid = pins[i];
  2876. pins[i] = pins[j];
  2877. pins[j] = nid;
  2878. }
  2879. }
  2880. }
  2881. }
  2882. /*
  2883. * Parse all pin widgets and store the useful pin nids to cfg
  2884. *
  2885. * The number of line-outs or any primary output is stored in line_outs,
  2886. * and the corresponding output pins are assigned to line_out_pins[],
  2887. * in the order of front, rear, CLFE, side, ...
  2888. *
  2889. * If more extra outputs (speaker and headphone) are found, the pins are
  2890. * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
  2891. * is detected, one of speaker of HP pins is assigned as the primary
  2892. * output, i.e. to line_out_pins[0]. So, line_outs is always positive
  2893. * if any analog output exists.
  2894. *
  2895. * The analog input pins are assigned to input_pins array.
  2896. * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
  2897. * respectively.
  2898. */
  2899. int snd_hda_parse_pin_def_config(struct hda_codec *codec,
  2900. struct auto_pin_cfg *cfg,
  2901. hda_nid_t *ignore_nids)
  2902. {
  2903. hda_nid_t nid, end_nid;
  2904. short seq, assoc_line_out, assoc_speaker;
  2905. short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
  2906. short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
  2907. short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
  2908. memset(cfg, 0, sizeof(*cfg));
  2909. memset(sequences_line_out, 0, sizeof(sequences_line_out));
  2910. memset(sequences_speaker, 0, sizeof(sequences_speaker));
  2911. memset(sequences_hp, 0, sizeof(sequences_hp));
  2912. assoc_line_out = assoc_speaker = 0;
  2913. end_nid = codec->start_nid + codec->num_nodes;
  2914. for (nid = codec->start_nid; nid < end_nid; nid++) {
  2915. unsigned int wid_caps = get_wcaps(codec, nid);
  2916. unsigned int wid_type =
  2917. (wid_caps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
  2918. unsigned int def_conf;
  2919. short assoc, loc;
  2920. /* read all default configuration for pin complex */
  2921. if (wid_type != AC_WID_PIN)
  2922. continue;
  2923. /* ignore the given nids (e.g. pc-beep returns error) */
  2924. if (ignore_nids && is_in_nid_list(nid, ignore_nids))
  2925. continue;
  2926. def_conf = snd_hda_codec_read(codec, nid, 0,
  2927. AC_VERB_GET_CONFIG_DEFAULT, 0);
  2928. if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
  2929. continue;
  2930. loc = get_defcfg_location(def_conf);
  2931. switch (get_defcfg_device(def_conf)) {
  2932. case AC_JACK_LINE_OUT:
  2933. seq = get_defcfg_sequence(def_conf);
  2934. assoc = get_defcfg_association(def_conf);
  2935. if (!(wid_caps & AC_WCAP_STEREO))
  2936. if (!cfg->mono_out_pin)
  2937. cfg->mono_out_pin = nid;
  2938. if (!assoc)
  2939. continue;
  2940. if (!assoc_line_out)
  2941. assoc_line_out = assoc;
  2942. else if (assoc_line_out != assoc)
  2943. continue;
  2944. if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
  2945. continue;
  2946. cfg->line_out_pins[cfg->line_outs] = nid;
  2947. sequences_line_out[cfg->line_outs] = seq;
  2948. cfg->line_outs++;
  2949. break;
  2950. case AC_JACK_SPEAKER:
  2951. seq = get_defcfg_sequence(def_conf);
  2952. assoc = get_defcfg_association(def_conf);
  2953. if (! assoc)
  2954. continue;
  2955. if (! assoc_speaker)
  2956. assoc_speaker = assoc;
  2957. else if (assoc_speaker != assoc)
  2958. continue;
  2959. if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
  2960. continue;
  2961. cfg->speaker_pins[cfg->speaker_outs] = nid;
  2962. sequences_speaker[cfg->speaker_outs] = seq;
  2963. cfg->speaker_outs++;
  2964. break;
  2965. case AC_JACK_HP_OUT:
  2966. seq = get_defcfg_sequence(def_conf);
  2967. assoc = get_defcfg_association(def_conf);
  2968. if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
  2969. continue;
  2970. cfg->hp_pins[cfg->hp_outs] = nid;
  2971. sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
  2972. cfg->hp_outs++;
  2973. break;
  2974. case AC_JACK_MIC_IN: {
  2975. int preferred, alt;
  2976. if (loc == AC_JACK_LOC_FRONT) {
  2977. preferred = AUTO_PIN_FRONT_MIC;
  2978. alt = AUTO_PIN_MIC;
  2979. } else {
  2980. preferred = AUTO_PIN_MIC;
  2981. alt = AUTO_PIN_FRONT_MIC;
  2982. }
  2983. if (!cfg->input_pins[preferred])
  2984. cfg->input_pins[preferred] = nid;
  2985. else if (!cfg->input_pins[alt])
  2986. cfg->input_pins[alt] = nid;
  2987. break;
  2988. }
  2989. case AC_JACK_LINE_IN:
  2990. if (loc == AC_JACK_LOC_FRONT)
  2991. cfg->input_pins[AUTO_PIN_FRONT_LINE] = nid;
  2992. else
  2993. cfg->input_pins[AUTO_PIN_LINE] = nid;
  2994. break;
  2995. case AC_JACK_CD:
  2996. cfg->input_pins[AUTO_PIN_CD] = nid;
  2997. break;
  2998. case AC_JACK_AUX:
  2999. cfg->input_pins[AUTO_PIN_AUX] = nid;
  3000. break;
  3001. case AC_JACK_SPDIF_OUT:
  3002. cfg->dig_out_pin = nid;
  3003. break;
  3004. case AC_JACK_SPDIF_IN:
  3005. cfg->dig_in_pin = nid;
  3006. break;
  3007. }
  3008. }
  3009. /* FIX-UP:
  3010. * If no line-out is defined but multiple HPs are found,
  3011. * some of them might be the real line-outs.
  3012. */
  3013. if (!cfg->line_outs && cfg->hp_outs > 1) {
  3014. int i = 0;
  3015. while (i < cfg->hp_outs) {
  3016. /* The real HPs should have the sequence 0x0f */
  3017. if ((sequences_hp[i] & 0x0f) == 0x0f) {
  3018. i++;
  3019. continue;
  3020. }
  3021. /* Move it to the line-out table */
  3022. cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
  3023. sequences_line_out[cfg->line_outs] = sequences_hp[i];
  3024. cfg->line_outs++;
  3025. cfg->hp_outs--;
  3026. memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
  3027. sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
  3028. memmove(sequences_hp + i - 1, sequences_hp + i,
  3029. sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
  3030. }
  3031. }
  3032. /* sort by sequence */
  3033. sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
  3034. cfg->line_outs);
  3035. sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
  3036. cfg->speaker_outs);
  3037. sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
  3038. cfg->hp_outs);
  3039. /* if we have only one mic, make it AUTO_PIN_MIC */
  3040. if (!cfg->input_pins[AUTO_PIN_MIC] &&
  3041. cfg->input_pins[AUTO_PIN_FRONT_MIC]) {
  3042. cfg->input_pins[AUTO_PIN_MIC] =
  3043. cfg->input_pins[AUTO_PIN_FRONT_MIC];
  3044. cfg->input_pins[AUTO_PIN_FRONT_MIC] = 0;
  3045. }
  3046. /* ditto for line-in */
  3047. if (!cfg->input_pins[AUTO_PIN_LINE] &&
  3048. cfg->input_pins[AUTO_PIN_FRONT_LINE]) {
  3049. cfg->input_pins[AUTO_PIN_LINE] =
  3050. cfg->input_pins[AUTO_PIN_FRONT_LINE];
  3051. cfg->input_pins[AUTO_PIN_FRONT_LINE] = 0;
  3052. }
  3053. /*
  3054. * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
  3055. * as a primary output
  3056. */
  3057. if (!cfg->line_outs) {
  3058. if (cfg->speaker_outs) {
  3059. cfg->line_outs = cfg->speaker_outs;
  3060. memcpy(cfg->line_out_pins, cfg->speaker_pins,
  3061. sizeof(cfg->speaker_pins));
  3062. cfg->speaker_outs = 0;
  3063. memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
  3064. cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
  3065. } else if (cfg->hp_outs) {
  3066. cfg->line_outs = cfg->hp_outs;
  3067. memcpy(cfg->line_out_pins, cfg->hp_pins,
  3068. sizeof(cfg->hp_pins));
  3069. cfg->hp_outs = 0;
  3070. memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
  3071. cfg->line_out_type = AUTO_PIN_HP_OUT;
  3072. }
  3073. }
  3074. /* Reorder the surround channels
  3075. * ALSA sequence is front/surr/clfe/side
  3076. * HDA sequence is:
  3077. * 4-ch: front/surr => OK as it is
  3078. * 6-ch: front/clfe/surr
  3079. * 8-ch: front/clfe/rear/side|fc
  3080. */
  3081. switch (cfg->line_outs) {
  3082. case 3:
  3083. case 4:
  3084. nid = cfg->line_out_pins[1];
  3085. cfg->line_out_pins[1] = cfg->line_out_pins[2];
  3086. cfg->line_out_pins[2] = nid;
  3087. break;
  3088. }
  3089. /*
  3090. * debug prints of the parsed results
  3091. */
  3092. snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
  3093. cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
  3094. cfg->line_out_pins[2], cfg->line_out_pins[3],
  3095. cfg->line_out_pins[4]);
  3096. snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
  3097. cfg->speaker_outs, cfg->speaker_pins[0],
  3098. cfg->speaker_pins[1], cfg->speaker_pins[2],
  3099. cfg->speaker_pins[3], cfg->speaker_pins[4]);
  3100. snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
  3101. cfg->hp_outs, cfg->hp_pins[0],
  3102. cfg->hp_pins[1], cfg->hp_pins[2],
  3103. cfg->hp_pins[3], cfg->hp_pins[4]);
  3104. snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
  3105. snd_printd(" inputs: mic=0x%x, fmic=0x%x, line=0x%x, fline=0x%x,"
  3106. " cd=0x%x, aux=0x%x\n",
  3107. cfg->input_pins[AUTO_PIN_MIC],
  3108. cfg->input_pins[AUTO_PIN_FRONT_MIC],
  3109. cfg->input_pins[AUTO_PIN_LINE],
  3110. cfg->input_pins[AUTO_PIN_FRONT_LINE],
  3111. cfg->input_pins[AUTO_PIN_CD],
  3112. cfg->input_pins[AUTO_PIN_AUX]);
  3113. return 0;
  3114. }
  3115. EXPORT_SYMBOL_GPL(snd_hda_parse_pin_def_config);
  3116. /* labels for input pins */
  3117. const char *auto_pin_cfg_labels[AUTO_PIN_LAST] = {
  3118. "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux"
  3119. };
  3120. EXPORT_SYMBOL_GPL(auto_pin_cfg_labels);
  3121. #ifdef CONFIG_PM
  3122. /*
  3123. * power management
  3124. */
  3125. /**
  3126. * snd_hda_suspend - suspend the codecs
  3127. * @bus: the HDA bus
  3128. * @state: suspsend state
  3129. *
  3130. * Returns 0 if successful.
  3131. */
  3132. int snd_hda_suspend(struct hda_bus *bus, pm_message_t state)
  3133. {
  3134. struct hda_codec *codec;
  3135. list_for_each_entry(codec, &bus->codec_list, list) {
  3136. #ifdef CONFIG_SND_HDA_POWER_SAVE
  3137. if (!codec->power_on)
  3138. continue;
  3139. #endif
  3140. hda_call_codec_suspend(codec);
  3141. }
  3142. return 0;
  3143. }
  3144. EXPORT_SYMBOL_GPL(snd_hda_suspend);
  3145. /**
  3146. * snd_hda_resume - resume the codecs
  3147. * @bus: the HDA bus
  3148. *
  3149. * Returns 0 if successful.
  3150. *
  3151. * This fucntion is defined only when POWER_SAVE isn't set.
  3152. * In the power-save mode, the codec is resumed dynamically.
  3153. */
  3154. int snd_hda_resume(struct hda_bus *bus)
  3155. {
  3156. struct hda_codec *codec;
  3157. list_for_each_entry(codec, &bus->codec_list, list) {
  3158. if (snd_hda_codec_needs_resume(codec))
  3159. hda_call_codec_resume(codec);
  3160. }
  3161. return 0;
  3162. }
  3163. EXPORT_SYMBOL_GPL(snd_hda_resume);
  3164. #endif /* CONFIG_PM */
  3165. /*
  3166. * generic arrays
  3167. */
  3168. /* get a new element from the given array
  3169. * if it exceeds the pre-allocated array size, re-allocate the array
  3170. */
  3171. void *snd_array_new(struct snd_array *array)
  3172. {
  3173. if (array->used >= array->alloced) {
  3174. int num = array->alloced + array->alloc_align;
  3175. void *nlist;
  3176. if (snd_BUG_ON(num >= 4096))
  3177. return NULL;
  3178. nlist = kcalloc(num + 1, array->elem_size, GFP_KERNEL);
  3179. if (!nlist)
  3180. return NULL;
  3181. if (array->list) {
  3182. memcpy(nlist, array->list,
  3183. array->elem_size * array->alloced);
  3184. kfree(array->list);
  3185. }
  3186. array->list = nlist;
  3187. array->alloced = num;
  3188. }
  3189. return snd_array_elem(array, array->used++);
  3190. }
  3191. EXPORT_SYMBOL_GPL(snd_array_new);
  3192. /* free the given array elements */
  3193. void snd_array_free(struct snd_array *array)
  3194. {
  3195. kfree(array->list);
  3196. array->used = 0;
  3197. array->alloced = 0;
  3198. array->list = NULL;
  3199. }
  3200. EXPORT_SYMBOL_GPL(snd_array_free);
  3201. /*
  3202. * used by hda_proc.c and hda_eld.c
  3203. */
  3204. void snd_print_pcm_rates(int pcm, char *buf, int buflen)
  3205. {
  3206. static unsigned int rates[] = {
  3207. 8000, 11025, 16000, 22050, 32000, 44100, 48000, 88200,
  3208. 96000, 176400, 192000, 384000
  3209. };
  3210. int i, j;
  3211. for (i = 0, j = 0; i < ARRAY_SIZE(rates); i++)
  3212. if (pcm & (1 << i))
  3213. j += snprintf(buf + j, buflen - j, " %d", rates[i]);
  3214. buf[j] = '\0'; /* necessary when j == 0 */
  3215. }
  3216. EXPORT_SYMBOL_GPL(snd_print_pcm_rates);
  3217. void snd_print_pcm_bits(int pcm, char *buf, int buflen)
  3218. {
  3219. static unsigned int bits[] = { 8, 16, 20, 24, 32 };
  3220. int i, j;
  3221. for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
  3222. if (pcm & (AC_SUPPCM_BITS_8 << i))
  3223. j += snprintf(buf + j, buflen - j, " %d", bits[i]);
  3224. buf[j] = '\0'; /* necessary when j == 0 */
  3225. }
  3226. EXPORT_SYMBOL_GPL(snd_print_pcm_bits);
  3227. MODULE_DESCRIPTION("HDA codec core");
  3228. MODULE_LICENSE("GPL");