hda_codec.c 73 KB

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