hda_codec.c 81 KB

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