hda_codec.c 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569
  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 __devinit snd_hda_create_spdif_out_ctls(struct hda_codec *codec,
  1180. hda_nid_t nid)
  1181. {
  1182. int err;
  1183. struct snd_kcontrol *kctl;
  1184. struct snd_kcontrol_new *dig_mix;
  1185. for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
  1186. kctl = snd_ctl_new1(dig_mix, codec);
  1187. kctl->private_value = nid;
  1188. err = snd_ctl_add(codec->bus->card, kctl);
  1189. if (err < 0)
  1190. return err;
  1191. }
  1192. codec->spdif_ctls =
  1193. snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0);
  1194. codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
  1195. return 0;
  1196. }
  1197. /*
  1198. * SPDIF input
  1199. */
  1200. #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
  1201. static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
  1202. struct snd_ctl_elem_value *ucontrol)
  1203. {
  1204. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1205. ucontrol->value.integer.value[0] = codec->spdif_in_enable;
  1206. return 0;
  1207. }
  1208. static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
  1209. struct snd_ctl_elem_value *ucontrol)
  1210. {
  1211. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1212. hda_nid_t nid = kcontrol->private_value;
  1213. unsigned int val = !!ucontrol->value.integer.value[0];
  1214. int change;
  1215. mutex_lock(&codec->spdif_mutex);
  1216. change = codec->spdif_in_enable != val;
  1217. if (change || codec->in_resume) {
  1218. codec->spdif_in_enable = val;
  1219. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1,
  1220. val);
  1221. }
  1222. mutex_unlock(&codec->spdif_mutex);
  1223. return change;
  1224. }
  1225. static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
  1226. struct snd_ctl_elem_value *ucontrol)
  1227. {
  1228. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1229. hda_nid_t nid = kcontrol->private_value;
  1230. unsigned short val;
  1231. unsigned int sbits;
  1232. val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0);
  1233. sbits = convert_to_spdif_status(val);
  1234. ucontrol->value.iec958.status[0] = sbits;
  1235. ucontrol->value.iec958.status[1] = sbits >> 8;
  1236. ucontrol->value.iec958.status[2] = sbits >> 16;
  1237. ucontrol->value.iec958.status[3] = sbits >> 24;
  1238. return 0;
  1239. }
  1240. static struct snd_kcontrol_new dig_in_ctls[] = {
  1241. {
  1242. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1243. .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH),
  1244. .info = snd_hda_spdif_in_switch_info,
  1245. .get = snd_hda_spdif_in_switch_get,
  1246. .put = snd_hda_spdif_in_switch_put,
  1247. },
  1248. {
  1249. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  1250. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1251. .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT),
  1252. .info = snd_hda_spdif_mask_info,
  1253. .get = snd_hda_spdif_in_status_get,
  1254. },
  1255. { } /* end */
  1256. };
  1257. /**
  1258. * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
  1259. * @codec: the HDA codec
  1260. * @nid: audio in widget NID
  1261. *
  1262. * Creates controls related with the SPDIF input.
  1263. * Called from each patch supporting the SPDIF in.
  1264. *
  1265. * Returns 0 if successful, or a negative error code.
  1266. */
  1267. int __devinit snd_hda_create_spdif_in_ctls(struct hda_codec *codec,
  1268. hda_nid_t nid)
  1269. {
  1270. int err;
  1271. struct snd_kcontrol *kctl;
  1272. struct snd_kcontrol_new *dig_mix;
  1273. for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
  1274. kctl = snd_ctl_new1(dig_mix, codec);
  1275. kctl->private_value = nid;
  1276. err = snd_ctl_add(codec->bus->card, kctl);
  1277. if (err < 0)
  1278. return err;
  1279. }
  1280. codec->spdif_in_enable =
  1281. snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0) &
  1282. AC_DIG1_ENABLE;
  1283. return 0;
  1284. }
  1285. /*
  1286. * set power state of the codec
  1287. */
  1288. static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
  1289. unsigned int power_state)
  1290. {
  1291. hda_nid_t nid, nid_start;
  1292. int nodes;
  1293. snd_hda_codec_write(codec, fg, 0, AC_VERB_SET_POWER_STATE,
  1294. power_state);
  1295. nodes = snd_hda_get_sub_nodes(codec, fg, &nid_start);
  1296. for (nid = nid_start; nid < nodes + nid_start; nid++) {
  1297. if (get_wcaps(codec, nid) & AC_WCAP_POWER)
  1298. snd_hda_codec_write(codec, nid, 0,
  1299. AC_VERB_SET_POWER_STATE,
  1300. power_state);
  1301. }
  1302. if (power_state == AC_PWRST_D0)
  1303. msleep(10);
  1304. }
  1305. /**
  1306. * snd_hda_build_controls - build mixer controls
  1307. * @bus: the BUS
  1308. *
  1309. * Creates mixer controls for each codec included in the bus.
  1310. *
  1311. * Returns 0 if successful, otherwise a negative error code.
  1312. */
  1313. int __devinit snd_hda_build_controls(struct hda_bus *bus)
  1314. {
  1315. struct hda_codec *codec;
  1316. /* build controls */
  1317. list_for_each_entry(codec, &bus->codec_list, list) {
  1318. int err;
  1319. if (!codec->patch_ops.build_controls)
  1320. continue;
  1321. err = codec->patch_ops.build_controls(codec);
  1322. if (err < 0)
  1323. return err;
  1324. }
  1325. /* initialize */
  1326. list_for_each_entry(codec, &bus->codec_list, list) {
  1327. int err;
  1328. hda_set_power_state(codec,
  1329. codec->afg ? codec->afg : codec->mfg,
  1330. AC_PWRST_D0);
  1331. if (!codec->patch_ops.init)
  1332. continue;
  1333. err = codec->patch_ops.init(codec);
  1334. if (err < 0)
  1335. return err;
  1336. }
  1337. return 0;
  1338. }
  1339. /*
  1340. * stream formats
  1341. */
  1342. struct hda_rate_tbl {
  1343. unsigned int hz;
  1344. unsigned int alsa_bits;
  1345. unsigned int hda_fmt;
  1346. };
  1347. static struct hda_rate_tbl rate_bits[] = {
  1348. /* rate in Hz, ALSA rate bitmask, HDA format value */
  1349. /* autodetected value used in snd_hda_query_supported_pcm */
  1350. { 8000, SNDRV_PCM_RATE_8000, 0x0500 }, /* 1/6 x 48 */
  1351. { 11025, SNDRV_PCM_RATE_11025, 0x4300 }, /* 1/4 x 44 */
  1352. { 16000, SNDRV_PCM_RATE_16000, 0x0200 }, /* 1/3 x 48 */
  1353. { 22050, SNDRV_PCM_RATE_22050, 0x4100 }, /* 1/2 x 44 */
  1354. { 32000, SNDRV_PCM_RATE_32000, 0x0a00 }, /* 2/3 x 48 */
  1355. { 44100, SNDRV_PCM_RATE_44100, 0x4000 }, /* 44 */
  1356. { 48000, SNDRV_PCM_RATE_48000, 0x0000 }, /* 48 */
  1357. { 88200, SNDRV_PCM_RATE_88200, 0x4800 }, /* 2 x 44 */
  1358. { 96000, SNDRV_PCM_RATE_96000, 0x0800 }, /* 2 x 48 */
  1359. { 176400, SNDRV_PCM_RATE_176400, 0x5800 },/* 4 x 44 */
  1360. { 192000, SNDRV_PCM_RATE_192000, 0x1800 }, /* 4 x 48 */
  1361. #define AC_PAR_PCM_RATE_BITS 11
  1362. /* up to bits 10, 384kHZ isn't supported properly */
  1363. /* not autodetected value */
  1364. { 9600, SNDRV_PCM_RATE_KNOT, 0x0400 }, /* 1/5 x 48 */
  1365. { 0 } /* terminator */
  1366. };
  1367. /**
  1368. * snd_hda_calc_stream_format - calculate format bitset
  1369. * @rate: the sample rate
  1370. * @channels: the number of channels
  1371. * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
  1372. * @maxbps: the max. bps
  1373. *
  1374. * Calculate the format bitset from the given rate, channels and th PCM format.
  1375. *
  1376. * Return zero if invalid.
  1377. */
  1378. unsigned int snd_hda_calc_stream_format(unsigned int rate,
  1379. unsigned int channels,
  1380. unsigned int format,
  1381. unsigned int maxbps)
  1382. {
  1383. int i;
  1384. unsigned int val = 0;
  1385. for (i = 0; rate_bits[i].hz; i++)
  1386. if (rate_bits[i].hz == rate) {
  1387. val = rate_bits[i].hda_fmt;
  1388. break;
  1389. }
  1390. if (!rate_bits[i].hz) {
  1391. snd_printdd("invalid rate %d\n", rate);
  1392. return 0;
  1393. }
  1394. if (channels == 0 || channels > 8) {
  1395. snd_printdd("invalid channels %d\n", channels);
  1396. return 0;
  1397. }
  1398. val |= channels - 1;
  1399. switch (snd_pcm_format_width(format)) {
  1400. case 8: val |= 0x00; break;
  1401. case 16: val |= 0x10; break;
  1402. case 20:
  1403. case 24:
  1404. case 32:
  1405. if (maxbps >= 32)
  1406. val |= 0x40;
  1407. else if (maxbps >= 24)
  1408. val |= 0x30;
  1409. else
  1410. val |= 0x20;
  1411. break;
  1412. default:
  1413. snd_printdd("invalid format width %d\n",
  1414. snd_pcm_format_width(format));
  1415. return 0;
  1416. }
  1417. return val;
  1418. }
  1419. /**
  1420. * snd_hda_query_supported_pcm - query the supported PCM rates and formats
  1421. * @codec: the HDA codec
  1422. * @nid: NID to query
  1423. * @ratesp: the pointer to store the detected rate bitflags
  1424. * @formatsp: the pointer to store the detected formats
  1425. * @bpsp: the pointer to store the detected format widths
  1426. *
  1427. * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
  1428. * or @bsps argument is ignored.
  1429. *
  1430. * Returns 0 if successful, otherwise a negative error code.
  1431. */
  1432. int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
  1433. u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
  1434. {
  1435. int i;
  1436. unsigned int val, streams;
  1437. val = 0;
  1438. if (nid != codec->afg &&
  1439. (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
  1440. val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
  1441. if (val == -1)
  1442. return -EIO;
  1443. }
  1444. if (!val)
  1445. val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
  1446. if (ratesp) {
  1447. u32 rates = 0;
  1448. for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
  1449. if (val & (1 << i))
  1450. rates |= rate_bits[i].alsa_bits;
  1451. }
  1452. *ratesp = rates;
  1453. }
  1454. if (formatsp || bpsp) {
  1455. u64 formats = 0;
  1456. unsigned int bps;
  1457. unsigned int wcaps;
  1458. wcaps = get_wcaps(codec, nid);
  1459. streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
  1460. if (streams == -1)
  1461. return -EIO;
  1462. if (!streams) {
  1463. streams = snd_hda_param_read(codec, codec->afg,
  1464. AC_PAR_STREAM);
  1465. if (streams == -1)
  1466. return -EIO;
  1467. }
  1468. bps = 0;
  1469. if (streams & AC_SUPFMT_PCM) {
  1470. if (val & AC_SUPPCM_BITS_8) {
  1471. formats |= SNDRV_PCM_FMTBIT_U8;
  1472. bps = 8;
  1473. }
  1474. if (val & AC_SUPPCM_BITS_16) {
  1475. formats |= SNDRV_PCM_FMTBIT_S16_LE;
  1476. bps = 16;
  1477. }
  1478. if (wcaps & AC_WCAP_DIGITAL) {
  1479. if (val & AC_SUPPCM_BITS_32)
  1480. formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
  1481. if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
  1482. formats |= SNDRV_PCM_FMTBIT_S32_LE;
  1483. if (val & AC_SUPPCM_BITS_24)
  1484. bps = 24;
  1485. else if (val & AC_SUPPCM_BITS_20)
  1486. bps = 20;
  1487. } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
  1488. AC_SUPPCM_BITS_32)) {
  1489. formats |= SNDRV_PCM_FMTBIT_S32_LE;
  1490. if (val & AC_SUPPCM_BITS_32)
  1491. bps = 32;
  1492. else if (val & AC_SUPPCM_BITS_24)
  1493. bps = 24;
  1494. else if (val & AC_SUPPCM_BITS_20)
  1495. bps = 20;
  1496. }
  1497. }
  1498. else if (streams == AC_SUPFMT_FLOAT32) {
  1499. /* should be exclusive */
  1500. formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
  1501. bps = 32;
  1502. } else if (streams == AC_SUPFMT_AC3) {
  1503. /* should be exclusive */
  1504. /* temporary hack: we have still no proper support
  1505. * for the direct AC3 stream...
  1506. */
  1507. formats |= SNDRV_PCM_FMTBIT_U8;
  1508. bps = 8;
  1509. }
  1510. if (formatsp)
  1511. *formatsp = formats;
  1512. if (bpsp)
  1513. *bpsp = bps;
  1514. }
  1515. return 0;
  1516. }
  1517. /**
  1518. * snd_hda_is_supported_format - check whether the given node supports
  1519. * the format val
  1520. *
  1521. * Returns 1 if supported, 0 if not.
  1522. */
  1523. int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
  1524. unsigned int format)
  1525. {
  1526. int i;
  1527. unsigned int val = 0, rate, stream;
  1528. if (nid != codec->afg &&
  1529. (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
  1530. val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
  1531. if (val == -1)
  1532. return 0;
  1533. }
  1534. if (!val) {
  1535. val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
  1536. if (val == -1)
  1537. return 0;
  1538. }
  1539. rate = format & 0xff00;
  1540. for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
  1541. if (rate_bits[i].hda_fmt == rate) {
  1542. if (val & (1 << i))
  1543. break;
  1544. return 0;
  1545. }
  1546. if (i >= AC_PAR_PCM_RATE_BITS)
  1547. return 0;
  1548. stream = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
  1549. if (stream == -1)
  1550. return 0;
  1551. if (!stream && nid != codec->afg)
  1552. stream = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
  1553. if (!stream || stream == -1)
  1554. return 0;
  1555. if (stream & AC_SUPFMT_PCM) {
  1556. switch (format & 0xf0) {
  1557. case 0x00:
  1558. if (!(val & AC_SUPPCM_BITS_8))
  1559. return 0;
  1560. break;
  1561. case 0x10:
  1562. if (!(val & AC_SUPPCM_BITS_16))
  1563. return 0;
  1564. break;
  1565. case 0x20:
  1566. if (!(val & AC_SUPPCM_BITS_20))
  1567. return 0;
  1568. break;
  1569. case 0x30:
  1570. if (!(val & AC_SUPPCM_BITS_24))
  1571. return 0;
  1572. break;
  1573. case 0x40:
  1574. if (!(val & AC_SUPPCM_BITS_32))
  1575. return 0;
  1576. break;
  1577. default:
  1578. return 0;
  1579. }
  1580. } else {
  1581. /* FIXME: check for float32 and AC3? */
  1582. }
  1583. return 1;
  1584. }
  1585. /*
  1586. * PCM stuff
  1587. */
  1588. static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
  1589. struct hda_codec *codec,
  1590. struct snd_pcm_substream *substream)
  1591. {
  1592. return 0;
  1593. }
  1594. static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
  1595. struct hda_codec *codec,
  1596. unsigned int stream_tag,
  1597. unsigned int format,
  1598. struct snd_pcm_substream *substream)
  1599. {
  1600. snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
  1601. return 0;
  1602. }
  1603. static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
  1604. struct hda_codec *codec,
  1605. struct snd_pcm_substream *substream)
  1606. {
  1607. snd_hda_codec_setup_stream(codec, hinfo->nid, 0, 0, 0);
  1608. return 0;
  1609. }
  1610. static int __devinit set_pcm_default_values(struct hda_codec *codec,
  1611. struct hda_pcm_stream *info)
  1612. {
  1613. /* query support PCM information from the given NID */
  1614. if (info->nid && (!info->rates || !info->formats)) {
  1615. snd_hda_query_supported_pcm(codec, info->nid,
  1616. info->rates ? NULL : &info->rates,
  1617. info->formats ? NULL : &info->formats,
  1618. info->maxbps ? NULL : &info->maxbps);
  1619. }
  1620. if (info->ops.open == NULL)
  1621. info->ops.open = hda_pcm_default_open_close;
  1622. if (info->ops.close == NULL)
  1623. info->ops.close = hda_pcm_default_open_close;
  1624. if (info->ops.prepare == NULL) {
  1625. snd_assert(info->nid, return -EINVAL);
  1626. info->ops.prepare = hda_pcm_default_prepare;
  1627. }
  1628. if (info->ops.cleanup == NULL) {
  1629. snd_assert(info->nid, return -EINVAL);
  1630. info->ops.cleanup = hda_pcm_default_cleanup;
  1631. }
  1632. return 0;
  1633. }
  1634. /**
  1635. * snd_hda_build_pcms - build PCM information
  1636. * @bus: the BUS
  1637. *
  1638. * Create PCM information for each codec included in the bus.
  1639. *
  1640. * The build_pcms codec patch is requested to set up codec->num_pcms and
  1641. * codec->pcm_info properly. The array is referred by the top-level driver
  1642. * to create its PCM instances.
  1643. * The allocated codec->pcm_info should be released in codec->patch_ops.free
  1644. * callback.
  1645. *
  1646. * At least, substreams, channels_min and channels_max must be filled for
  1647. * each stream. substreams = 0 indicates that the stream doesn't exist.
  1648. * When rates and/or formats are zero, the supported values are queried
  1649. * from the given nid. The nid is used also by the default ops.prepare
  1650. * and ops.cleanup callbacks.
  1651. *
  1652. * The driver needs to call ops.open in its open callback. Similarly,
  1653. * ops.close is supposed to be called in the close callback.
  1654. * ops.prepare should be called in the prepare or hw_params callback
  1655. * with the proper parameters for set up.
  1656. * ops.cleanup should be called in hw_free for clean up of streams.
  1657. *
  1658. * This function returns 0 if successfull, or a negative error code.
  1659. */
  1660. int __devinit snd_hda_build_pcms(struct hda_bus *bus)
  1661. {
  1662. struct hda_codec *codec;
  1663. list_for_each_entry(codec, &bus->codec_list, list) {
  1664. unsigned int pcm, s;
  1665. int err;
  1666. if (!codec->patch_ops.build_pcms)
  1667. continue;
  1668. err = codec->patch_ops.build_pcms(codec);
  1669. if (err < 0)
  1670. return err;
  1671. for (pcm = 0; pcm < codec->num_pcms; pcm++) {
  1672. for (s = 0; s < 2; s++) {
  1673. struct hda_pcm_stream *info;
  1674. info = &codec->pcm_info[pcm].stream[s];
  1675. if (!info->substreams)
  1676. continue;
  1677. err = set_pcm_default_values(codec, info);
  1678. if (err < 0)
  1679. return err;
  1680. }
  1681. }
  1682. }
  1683. return 0;
  1684. }
  1685. /**
  1686. * snd_hda_check_board_config - compare the current codec with the config table
  1687. * @codec: the HDA codec
  1688. * @num_configs: number of config enums
  1689. * @models: array of model name strings
  1690. * @tbl: configuration table, terminated by null entries
  1691. *
  1692. * Compares the modelname or PCI subsystem id of the current codec with the
  1693. * given configuration table. If a matching entry is found, returns its
  1694. * config value (supposed to be 0 or positive).
  1695. *
  1696. * If no entries are matching, the function returns a negative value.
  1697. */
  1698. int __devinit snd_hda_check_board_config(struct hda_codec *codec,
  1699. int num_configs, const char **models,
  1700. const struct snd_pci_quirk *tbl)
  1701. {
  1702. if (codec->bus->modelname && models) {
  1703. int i;
  1704. for (i = 0; i < num_configs; i++) {
  1705. if (models[i] &&
  1706. !strcmp(codec->bus->modelname, models[i])) {
  1707. snd_printd(KERN_INFO "hda_codec: model '%s' is "
  1708. "selected\n", models[i]);
  1709. return i;
  1710. }
  1711. }
  1712. }
  1713. if (!codec->bus->pci || !tbl)
  1714. return -1;
  1715. tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
  1716. if (!tbl)
  1717. return -1;
  1718. if (tbl->value >= 0 && tbl->value < num_configs) {
  1719. #ifdef CONFIG_SND_DEBUG_DETECT
  1720. char tmp[10];
  1721. const char *model = NULL;
  1722. if (models)
  1723. model = models[tbl->value];
  1724. if (!model) {
  1725. sprintf(tmp, "#%d", tbl->value);
  1726. model = tmp;
  1727. }
  1728. snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
  1729. "for config %x:%x (%s)\n",
  1730. model, tbl->subvendor, tbl->subdevice,
  1731. (tbl->name ? tbl->name : "Unknown device"));
  1732. #endif
  1733. return tbl->value;
  1734. }
  1735. return -1;
  1736. }
  1737. /**
  1738. * snd_hda_add_new_ctls - create controls from the array
  1739. * @codec: the HDA codec
  1740. * @knew: the array of struct snd_kcontrol_new
  1741. *
  1742. * This helper function creates and add new controls in the given array.
  1743. * The array must be terminated with an empty entry as terminator.
  1744. *
  1745. * Returns 0 if successful, or a negative error code.
  1746. */
  1747. int __devinit snd_hda_add_new_ctls(struct hda_codec *codec,
  1748. struct snd_kcontrol_new *knew)
  1749. {
  1750. int err;
  1751. for (; knew->name; knew++) {
  1752. struct snd_kcontrol *kctl;
  1753. kctl = snd_ctl_new1(knew, codec);
  1754. if (!kctl)
  1755. return -ENOMEM;
  1756. err = snd_ctl_add(codec->bus->card, kctl);
  1757. if (err < 0) {
  1758. if (!codec->addr)
  1759. return err;
  1760. kctl = snd_ctl_new1(knew, codec);
  1761. if (!kctl)
  1762. return -ENOMEM;
  1763. kctl->id.device = codec->addr;
  1764. err = snd_ctl_add(codec->bus->card, kctl);
  1765. if (err < 0)
  1766. return err;
  1767. }
  1768. }
  1769. return 0;
  1770. }
  1771. /*
  1772. * Channel mode helper
  1773. */
  1774. int snd_hda_ch_mode_info(struct hda_codec *codec,
  1775. struct snd_ctl_elem_info *uinfo,
  1776. const struct hda_channel_mode *chmode,
  1777. int num_chmodes)
  1778. {
  1779. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  1780. uinfo->count = 1;
  1781. uinfo->value.enumerated.items = num_chmodes;
  1782. if (uinfo->value.enumerated.item >= num_chmodes)
  1783. uinfo->value.enumerated.item = num_chmodes - 1;
  1784. sprintf(uinfo->value.enumerated.name, "%dch",
  1785. chmode[uinfo->value.enumerated.item].channels);
  1786. return 0;
  1787. }
  1788. int snd_hda_ch_mode_get(struct hda_codec *codec,
  1789. struct snd_ctl_elem_value *ucontrol,
  1790. const struct hda_channel_mode *chmode,
  1791. int num_chmodes,
  1792. int max_channels)
  1793. {
  1794. int i;
  1795. for (i = 0; i < num_chmodes; i++) {
  1796. if (max_channels == chmode[i].channels) {
  1797. ucontrol->value.enumerated.item[0] = i;
  1798. break;
  1799. }
  1800. }
  1801. return 0;
  1802. }
  1803. int snd_hda_ch_mode_put(struct hda_codec *codec,
  1804. struct snd_ctl_elem_value *ucontrol,
  1805. const struct hda_channel_mode *chmode,
  1806. int num_chmodes,
  1807. int *max_channelsp)
  1808. {
  1809. unsigned int mode;
  1810. mode = ucontrol->value.enumerated.item[0];
  1811. snd_assert(mode < num_chmodes, return -EINVAL);
  1812. if (*max_channelsp == chmode[mode].channels && !codec->in_resume)
  1813. return 0;
  1814. /* change the current channel setting */
  1815. *max_channelsp = chmode[mode].channels;
  1816. if (chmode[mode].sequence)
  1817. snd_hda_sequence_write(codec, chmode[mode].sequence);
  1818. return 1;
  1819. }
  1820. /*
  1821. * input MUX helper
  1822. */
  1823. int snd_hda_input_mux_info(const struct hda_input_mux *imux,
  1824. struct snd_ctl_elem_info *uinfo)
  1825. {
  1826. unsigned int index;
  1827. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  1828. uinfo->count = 1;
  1829. uinfo->value.enumerated.items = imux->num_items;
  1830. index = uinfo->value.enumerated.item;
  1831. if (index >= imux->num_items)
  1832. index = imux->num_items - 1;
  1833. strcpy(uinfo->value.enumerated.name, imux->items[index].label);
  1834. return 0;
  1835. }
  1836. int snd_hda_input_mux_put(struct hda_codec *codec,
  1837. const struct hda_input_mux *imux,
  1838. struct snd_ctl_elem_value *ucontrol,
  1839. hda_nid_t nid,
  1840. unsigned int *cur_val)
  1841. {
  1842. unsigned int idx;
  1843. idx = ucontrol->value.enumerated.item[0];
  1844. if (idx >= imux->num_items)
  1845. idx = imux->num_items - 1;
  1846. if (*cur_val == idx && !codec->in_resume)
  1847. return 0;
  1848. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
  1849. imux->items[idx].index);
  1850. *cur_val = idx;
  1851. return 1;
  1852. }
  1853. /*
  1854. * Multi-channel / digital-out PCM helper functions
  1855. */
  1856. /* setup SPDIF output stream */
  1857. static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
  1858. unsigned int stream_tag, unsigned int format)
  1859. {
  1860. /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
  1861. if (codec->spdif_ctls & AC_DIG1_ENABLE)
  1862. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1,
  1863. codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff);
  1864. snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
  1865. /* turn on again (if needed) */
  1866. if (codec->spdif_ctls & AC_DIG1_ENABLE)
  1867. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1,
  1868. codec->spdif_ctls & 0xff);
  1869. }
  1870. /*
  1871. * open the digital out in the exclusive mode
  1872. */
  1873. int snd_hda_multi_out_dig_open(struct hda_codec *codec,
  1874. struct hda_multi_out *mout)
  1875. {
  1876. mutex_lock(&codec->spdif_mutex);
  1877. if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
  1878. /* already opened as analog dup; reset it once */
  1879. snd_hda_codec_setup_stream(codec, mout->dig_out_nid, 0, 0, 0);
  1880. mout->dig_out_used = HDA_DIG_EXCLUSIVE;
  1881. mutex_unlock(&codec->spdif_mutex);
  1882. return 0;
  1883. }
  1884. int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
  1885. struct hda_multi_out *mout,
  1886. unsigned int stream_tag,
  1887. unsigned int format,
  1888. struct snd_pcm_substream *substream)
  1889. {
  1890. mutex_lock(&codec->spdif_mutex);
  1891. setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
  1892. mutex_unlock(&codec->spdif_mutex);
  1893. return 0;
  1894. }
  1895. /*
  1896. * release the digital out
  1897. */
  1898. int snd_hda_multi_out_dig_close(struct hda_codec *codec,
  1899. struct hda_multi_out *mout)
  1900. {
  1901. mutex_lock(&codec->spdif_mutex);
  1902. mout->dig_out_used = 0;
  1903. mutex_unlock(&codec->spdif_mutex);
  1904. return 0;
  1905. }
  1906. /*
  1907. * set up more restrictions for analog out
  1908. */
  1909. int snd_hda_multi_out_analog_open(struct hda_codec *codec,
  1910. struct hda_multi_out *mout,
  1911. struct snd_pcm_substream *substream)
  1912. {
  1913. substream->runtime->hw.channels_max = mout->max_channels;
  1914. return snd_pcm_hw_constraint_step(substream->runtime, 0,
  1915. SNDRV_PCM_HW_PARAM_CHANNELS, 2);
  1916. }
  1917. /*
  1918. * set up the i/o for analog out
  1919. * when the digital out is available, copy the front out to digital out, too.
  1920. */
  1921. int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
  1922. struct hda_multi_out *mout,
  1923. unsigned int stream_tag,
  1924. unsigned int format,
  1925. struct snd_pcm_substream *substream)
  1926. {
  1927. hda_nid_t *nids = mout->dac_nids;
  1928. int chs = substream->runtime->channels;
  1929. int i;
  1930. mutex_lock(&codec->spdif_mutex);
  1931. if (mout->dig_out_nid && mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
  1932. if (chs == 2 &&
  1933. snd_hda_is_supported_format(codec, mout->dig_out_nid,
  1934. format) &&
  1935. !(codec->spdif_status & IEC958_AES0_NONAUDIO)) {
  1936. mout->dig_out_used = HDA_DIG_ANALOG_DUP;
  1937. setup_dig_out_stream(codec, mout->dig_out_nid,
  1938. stream_tag, format);
  1939. } else {
  1940. mout->dig_out_used = 0;
  1941. snd_hda_codec_setup_stream(codec, mout->dig_out_nid,
  1942. 0, 0, 0);
  1943. }
  1944. }
  1945. mutex_unlock(&codec->spdif_mutex);
  1946. /* front */
  1947. snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
  1948. 0, format);
  1949. if (mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
  1950. /* headphone out will just decode front left/right (stereo) */
  1951. snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
  1952. 0, format);
  1953. /* extra outputs copied from front */
  1954. for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
  1955. if (mout->extra_out_nid[i])
  1956. snd_hda_codec_setup_stream(codec,
  1957. mout->extra_out_nid[i],
  1958. stream_tag, 0, format);
  1959. /* surrounds */
  1960. for (i = 1; i < mout->num_dacs; i++) {
  1961. if (chs >= (i + 1) * 2) /* independent out */
  1962. snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
  1963. i * 2, format);
  1964. else /* copy front */
  1965. snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
  1966. 0, format);
  1967. }
  1968. return 0;
  1969. }
  1970. /*
  1971. * clean up the setting for analog out
  1972. */
  1973. int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
  1974. struct hda_multi_out *mout)
  1975. {
  1976. hda_nid_t *nids = mout->dac_nids;
  1977. int i;
  1978. for (i = 0; i < mout->num_dacs; i++)
  1979. snd_hda_codec_setup_stream(codec, nids[i], 0, 0, 0);
  1980. if (mout->hp_nid)
  1981. snd_hda_codec_setup_stream(codec, mout->hp_nid, 0, 0, 0);
  1982. for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
  1983. if (mout->extra_out_nid[i])
  1984. snd_hda_codec_setup_stream(codec,
  1985. mout->extra_out_nid[i],
  1986. 0, 0, 0);
  1987. mutex_lock(&codec->spdif_mutex);
  1988. if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
  1989. snd_hda_codec_setup_stream(codec, mout->dig_out_nid, 0, 0, 0);
  1990. mout->dig_out_used = 0;
  1991. }
  1992. mutex_unlock(&codec->spdif_mutex);
  1993. return 0;
  1994. }
  1995. /*
  1996. * Helper for automatic ping configuration
  1997. */
  1998. static int __devinit is_in_nid_list(hda_nid_t nid, hda_nid_t *list)
  1999. {
  2000. for (; *list; list++)
  2001. if (*list == nid)
  2002. return 1;
  2003. return 0;
  2004. }
  2005. /*
  2006. * Sort an associated group of pins according to their sequence numbers.
  2007. */
  2008. static void sort_pins_by_sequence(hda_nid_t * pins, short * sequences,
  2009. int num_pins)
  2010. {
  2011. int i, j;
  2012. short seq;
  2013. hda_nid_t nid;
  2014. for (i = 0; i < num_pins; i++) {
  2015. for (j = i + 1; j < num_pins; j++) {
  2016. if (sequences[i] > sequences[j]) {
  2017. seq = sequences[i];
  2018. sequences[i] = sequences[j];
  2019. sequences[j] = seq;
  2020. nid = pins[i];
  2021. pins[i] = pins[j];
  2022. pins[j] = nid;
  2023. }
  2024. }
  2025. }
  2026. }
  2027. /*
  2028. * Parse all pin widgets and store the useful pin nids to cfg
  2029. *
  2030. * The number of line-outs or any primary output is stored in line_outs,
  2031. * and the corresponding output pins are assigned to line_out_pins[],
  2032. * in the order of front, rear, CLFE, side, ...
  2033. *
  2034. * If more extra outputs (speaker and headphone) are found, the pins are
  2035. * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
  2036. * is detected, one of speaker of HP pins is assigned as the primary
  2037. * output, i.e. to line_out_pins[0]. So, line_outs is always positive
  2038. * if any analog output exists.
  2039. *
  2040. * The analog input pins are assigned to input_pins array.
  2041. * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
  2042. * respectively.
  2043. */
  2044. int __devinit snd_hda_parse_pin_def_config(struct hda_codec *codec,
  2045. struct auto_pin_cfg *cfg,
  2046. hda_nid_t *ignore_nids)
  2047. {
  2048. hda_nid_t nid, nid_start;
  2049. int nodes;
  2050. short seq, assoc_line_out, assoc_speaker;
  2051. short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
  2052. short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
  2053. memset(cfg, 0, sizeof(*cfg));
  2054. memset(sequences_line_out, 0, sizeof(sequences_line_out));
  2055. memset(sequences_speaker, 0, sizeof(sequences_speaker));
  2056. assoc_line_out = assoc_speaker = 0;
  2057. nodes = snd_hda_get_sub_nodes(codec, codec->afg, &nid_start);
  2058. for (nid = nid_start; nid < nodes + nid_start; nid++) {
  2059. unsigned int wid_caps = get_wcaps(codec, nid);
  2060. unsigned int wid_type =
  2061. (wid_caps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
  2062. unsigned int def_conf;
  2063. short assoc, loc;
  2064. /* read all default configuration for pin complex */
  2065. if (wid_type != AC_WID_PIN)
  2066. continue;
  2067. /* ignore the given nids (e.g. pc-beep returns error) */
  2068. if (ignore_nids && is_in_nid_list(nid, ignore_nids))
  2069. continue;
  2070. def_conf = snd_hda_codec_read(codec, nid, 0,
  2071. AC_VERB_GET_CONFIG_DEFAULT, 0);
  2072. if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
  2073. continue;
  2074. loc = get_defcfg_location(def_conf);
  2075. switch (get_defcfg_device(def_conf)) {
  2076. case AC_JACK_LINE_OUT:
  2077. seq = get_defcfg_sequence(def_conf);
  2078. assoc = get_defcfg_association(def_conf);
  2079. if (!assoc)
  2080. continue;
  2081. if (!assoc_line_out)
  2082. assoc_line_out = assoc;
  2083. else if (assoc_line_out != assoc)
  2084. continue;
  2085. if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
  2086. continue;
  2087. cfg->line_out_pins[cfg->line_outs] = nid;
  2088. sequences_line_out[cfg->line_outs] = seq;
  2089. cfg->line_outs++;
  2090. break;
  2091. case AC_JACK_SPEAKER:
  2092. seq = get_defcfg_sequence(def_conf);
  2093. assoc = get_defcfg_association(def_conf);
  2094. if (! assoc)
  2095. continue;
  2096. if (! assoc_speaker)
  2097. assoc_speaker = assoc;
  2098. else if (assoc_speaker != assoc)
  2099. continue;
  2100. if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
  2101. continue;
  2102. cfg->speaker_pins[cfg->speaker_outs] = nid;
  2103. sequences_speaker[cfg->speaker_outs] = seq;
  2104. cfg->speaker_outs++;
  2105. break;
  2106. case AC_JACK_HP_OUT:
  2107. if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
  2108. continue;
  2109. cfg->hp_pins[cfg->hp_outs] = nid;
  2110. cfg->hp_outs++;
  2111. break;
  2112. case AC_JACK_MIC_IN: {
  2113. int preferred, alt;
  2114. if (loc == AC_JACK_LOC_FRONT) {
  2115. preferred = AUTO_PIN_FRONT_MIC;
  2116. alt = AUTO_PIN_MIC;
  2117. } else {
  2118. preferred = AUTO_PIN_MIC;
  2119. alt = AUTO_PIN_FRONT_MIC;
  2120. }
  2121. if (!cfg->input_pins[preferred])
  2122. cfg->input_pins[preferred] = nid;
  2123. else if (!cfg->input_pins[alt])
  2124. cfg->input_pins[alt] = nid;
  2125. break;
  2126. }
  2127. case AC_JACK_LINE_IN:
  2128. if (loc == AC_JACK_LOC_FRONT)
  2129. cfg->input_pins[AUTO_PIN_FRONT_LINE] = nid;
  2130. else
  2131. cfg->input_pins[AUTO_PIN_LINE] = nid;
  2132. break;
  2133. case AC_JACK_CD:
  2134. cfg->input_pins[AUTO_PIN_CD] = nid;
  2135. break;
  2136. case AC_JACK_AUX:
  2137. cfg->input_pins[AUTO_PIN_AUX] = nid;
  2138. break;
  2139. case AC_JACK_SPDIF_OUT:
  2140. cfg->dig_out_pin = nid;
  2141. break;
  2142. case AC_JACK_SPDIF_IN:
  2143. cfg->dig_in_pin = nid;
  2144. break;
  2145. }
  2146. }
  2147. /* sort by sequence */
  2148. sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
  2149. cfg->line_outs);
  2150. sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
  2151. cfg->speaker_outs);
  2152. /*
  2153. * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
  2154. * as a primary output
  2155. */
  2156. if (!cfg->line_outs) {
  2157. if (cfg->speaker_outs) {
  2158. cfg->line_outs = cfg->speaker_outs;
  2159. memcpy(cfg->line_out_pins, cfg->speaker_pins,
  2160. sizeof(cfg->speaker_pins));
  2161. cfg->speaker_outs = 0;
  2162. memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
  2163. cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
  2164. } else if (cfg->hp_outs) {
  2165. cfg->line_outs = cfg->hp_outs;
  2166. memcpy(cfg->line_out_pins, cfg->hp_pins,
  2167. sizeof(cfg->hp_pins));
  2168. cfg->hp_outs = 0;
  2169. memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
  2170. cfg->line_out_type = AUTO_PIN_HP_OUT;
  2171. }
  2172. }
  2173. /* Reorder the surround channels
  2174. * ALSA sequence is front/surr/clfe/side
  2175. * HDA sequence is:
  2176. * 4-ch: front/surr => OK as it is
  2177. * 6-ch: front/clfe/surr
  2178. * 8-ch: front/clfe/rear/side|fc
  2179. */
  2180. switch (cfg->line_outs) {
  2181. case 3:
  2182. case 4:
  2183. nid = cfg->line_out_pins[1];
  2184. cfg->line_out_pins[1] = cfg->line_out_pins[2];
  2185. cfg->line_out_pins[2] = nid;
  2186. break;
  2187. }
  2188. /*
  2189. * debug prints of the parsed results
  2190. */
  2191. snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
  2192. cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
  2193. cfg->line_out_pins[2], cfg->line_out_pins[3],
  2194. cfg->line_out_pins[4]);
  2195. snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
  2196. cfg->speaker_outs, cfg->speaker_pins[0],
  2197. cfg->speaker_pins[1], cfg->speaker_pins[2],
  2198. cfg->speaker_pins[3], cfg->speaker_pins[4]);
  2199. snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
  2200. cfg->hp_outs, cfg->hp_pins[0],
  2201. cfg->hp_pins[1], cfg->hp_pins[2],
  2202. cfg->hp_pins[3], cfg->hp_pins[4]);
  2203. snd_printd(" inputs: mic=0x%x, fmic=0x%x, line=0x%x, fline=0x%x,"
  2204. " cd=0x%x, aux=0x%x\n",
  2205. cfg->input_pins[AUTO_PIN_MIC],
  2206. cfg->input_pins[AUTO_PIN_FRONT_MIC],
  2207. cfg->input_pins[AUTO_PIN_LINE],
  2208. cfg->input_pins[AUTO_PIN_FRONT_LINE],
  2209. cfg->input_pins[AUTO_PIN_CD],
  2210. cfg->input_pins[AUTO_PIN_AUX]);
  2211. return 0;
  2212. }
  2213. /* labels for input pins */
  2214. const char *auto_pin_cfg_labels[AUTO_PIN_LAST] = {
  2215. "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux"
  2216. };
  2217. #ifdef CONFIG_PM
  2218. /*
  2219. * power management
  2220. */
  2221. /**
  2222. * snd_hda_suspend - suspend the codecs
  2223. * @bus: the HDA bus
  2224. * @state: suspsend state
  2225. *
  2226. * Returns 0 if successful.
  2227. */
  2228. int snd_hda_suspend(struct hda_bus *bus, pm_message_t state)
  2229. {
  2230. struct hda_codec *codec;
  2231. /* FIXME: should handle power widget capabilities */
  2232. list_for_each_entry(codec, &bus->codec_list, list) {
  2233. if (codec->patch_ops.suspend)
  2234. codec->patch_ops.suspend(codec, state);
  2235. hda_set_power_state(codec,
  2236. codec->afg ? codec->afg : codec->mfg,
  2237. AC_PWRST_D3);
  2238. }
  2239. return 0;
  2240. }
  2241. /**
  2242. * snd_hda_resume - resume the codecs
  2243. * @bus: the HDA bus
  2244. * @state: resume state
  2245. *
  2246. * Returns 0 if successful.
  2247. */
  2248. int snd_hda_resume(struct hda_bus *bus)
  2249. {
  2250. struct hda_codec *codec;
  2251. list_for_each_entry(codec, &bus->codec_list, list) {
  2252. hda_set_power_state(codec,
  2253. codec->afg ? codec->afg : codec->mfg,
  2254. AC_PWRST_D0);
  2255. if (codec->patch_ops.resume)
  2256. codec->patch_ops.resume(codec);
  2257. }
  2258. return 0;
  2259. }
  2260. /**
  2261. * snd_hda_resume_ctls - resume controls in the new control list
  2262. * @codec: the HDA codec
  2263. * @knew: the array of struct snd_kcontrol_new
  2264. *
  2265. * This function resumes the mixer controls in the struct snd_kcontrol_new array,
  2266. * originally for snd_hda_add_new_ctls().
  2267. * The array must be terminated with an empty entry as terminator.
  2268. */
  2269. int snd_hda_resume_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
  2270. {
  2271. struct snd_ctl_elem_value *val;
  2272. val = kmalloc(sizeof(*val), GFP_KERNEL);
  2273. if (!val)
  2274. return -ENOMEM;
  2275. codec->in_resume = 1;
  2276. for (; knew->name; knew++) {
  2277. int i, count;
  2278. count = knew->count ? knew->count : 1;
  2279. for (i = 0; i < count; i++) {
  2280. memset(val, 0, sizeof(*val));
  2281. val->id.iface = knew->iface;
  2282. val->id.device = knew->device;
  2283. val->id.subdevice = knew->subdevice;
  2284. strcpy(val->id.name, knew->name);
  2285. val->id.index = knew->index ? knew->index : i;
  2286. /* Assume that get callback reads only from cache,
  2287. * not accessing to the real hardware
  2288. */
  2289. if (snd_ctl_elem_read(codec->bus->card, val) < 0)
  2290. continue;
  2291. snd_ctl_elem_write(codec->bus->card, NULL, val);
  2292. }
  2293. }
  2294. codec->in_resume = 0;
  2295. kfree(val);
  2296. return 0;
  2297. }
  2298. /**
  2299. * snd_hda_resume_spdif_out - resume the digital out
  2300. * @codec: the HDA codec
  2301. */
  2302. int snd_hda_resume_spdif_out(struct hda_codec *codec)
  2303. {
  2304. return snd_hda_resume_ctls(codec, dig_mixes);
  2305. }
  2306. /**
  2307. * snd_hda_resume_spdif_in - resume the digital in
  2308. * @codec: the HDA codec
  2309. */
  2310. int snd_hda_resume_spdif_in(struct hda_codec *codec)
  2311. {
  2312. return snd_hda_resume_ctls(codec, dig_in_ctls);
  2313. }
  2314. #endif