hda_codec.c 74 KB

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