hda_codec.c 81 KB

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