hda_codec.c 77 KB

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