hda_codec.c 82 KB

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