hda_codec.c 67 KB

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