blob: 45695d608c7648203606697c802f604ac3c37d41 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/init.h>
23#include <linux/delay.h>
24#include <linux/slab.h>
25#include <linux/pci.h>
Ingo Molnar62932df2006-01-16 16:34:20 +010026#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <sound/core.h>
28#include "hda_codec.h"
29#include <sound/asoundef.h>
Jaroslav Kysela302e9c52006-07-05 17:39:49 +020030#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <sound/initval.h>
32#include "hda_local.h"
Takashi Iwai28073142007-07-27 18:58:06 +020033#include <sound/hda_hwdep.h>
Harvey Harrison3c9a3202008-02-29 11:59:26 +010034#include "hda_patch.h" /* codec presets */
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Takashi Iwaicb53c622007-08-10 17:21:45 +020036#ifdef CONFIG_SND_HDA_POWER_SAVE
37/* define this option here to hide as static */
Takashi Iwai7a5a27c2007-09-17 19:07:46 +020038static int power_save = CONFIG_SND_HDA_POWER_SAVE_DEFAULT;
Takashi Iwaicb53c622007-08-10 17:21:45 +020039module_param(power_save, int, 0644);
40MODULE_PARM_DESC(power_save, "Automatic power-saving timeout "
41 "(in second, 0 = disable).");
42#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Linus Torvalds1da177e2005-04-16 15:20:36 -070044/*
45 * vendor / preset table
46 */
47
48struct hda_vendor_id {
49 unsigned int id;
50 const char *name;
51};
52
53/* codec vendor labels */
54static struct hda_vendor_id hda_vendor_ids[] = {
Takashi Iwaic8cd1282008-02-13 16:59:29 +010055 { 0x1002, "ATI" },
Takashi Iwaia9226252006-09-17 22:05:54 +020056 { 0x1057, "Motorola" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010057 { 0x1095, "Silicon Image" },
58 { 0x10ec, "Realtek" },
Joseph Chanc577b8a2006-11-29 15:29:40 +010059 { 0x1106, "VIA" },
Matthew Ranostay7f168592007-10-18 17:38:17 +020060 { 0x111d, "IDT" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010061 { 0x11c1, "LSI" },
Takashi Iwai54b903e2005-05-15 14:30:10 +020062 { 0x11d4, "Analog Devices" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 { 0x13f6, "C-Media" },
Takashi Iwaia9226252006-09-17 22:05:54 +020064 { 0x14f1, "Conexant" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010065 { 0x17e8, "Chrontel" },
66 { 0x1854, "LG" },
Mark Brown8199de32008-10-28 14:50:13 +000067 { 0x1aec, "Wolfson Microelectronics" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 { 0x434d, "C-Media" },
Matt2f2f4252005-04-13 14:45:30 +020069 { 0x8384, "SigmaTel" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 {} /* terminator */
71};
72
Harvey Harrison3c9a3202008-02-29 11:59:26 +010073static const struct hda_codec_preset *hda_preset_tables[] = {
74#ifdef CONFIG_SND_HDA_CODEC_REALTEK
75 snd_hda_preset_realtek,
76#endif
77#ifdef CONFIG_SND_HDA_CODEC_CMEDIA
78 snd_hda_preset_cmedia,
79#endif
80#ifdef CONFIG_SND_HDA_CODEC_ANALOG
81 snd_hda_preset_analog,
82#endif
83#ifdef CONFIG_SND_HDA_CODEC_SIGMATEL
84 snd_hda_preset_sigmatel,
85#endif
86#ifdef CONFIG_SND_HDA_CODEC_SI3054
87 snd_hda_preset_si3054,
88#endif
89#ifdef CONFIG_SND_HDA_CODEC_ATIHDMI
90 snd_hda_preset_atihdmi,
91#endif
92#ifdef CONFIG_SND_HDA_CODEC_CONEXANT
93 snd_hda_preset_conexant,
94#endif
95#ifdef CONFIG_SND_HDA_CODEC_VIA
96 snd_hda_preset_via,
97#endif
Wei Ni9a10eb22008-09-26 13:45:46 +080098#ifdef CONFIG_SND_HDA_CODEC_NVHDMI
99 snd_hda_preset_nvhdmi,
100#endif
Wu, Fengguang91504872008-11-05 11:16:56 +0800101#ifdef CONFIG_SND_HDA_CODEC_INTELHDMI
102 snd_hda_preset_intelhdmi,
103#endif
Harvey Harrison3c9a3202008-02-29 11:59:26 +0100104 NULL
105};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Takashi Iwaicb53c622007-08-10 17:21:45 +0200107#ifdef CONFIG_SND_HDA_POWER_SAVE
108static void hda_power_work(struct work_struct *work);
109static void hda_keep_power_on(struct hda_codec *codec);
110#else
111static inline void hda_keep_power_on(struct hda_codec *codec) {}
112#endif
113
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400114const char *snd_hda_get_jack_location(u32 cfg)
115{
116 static char *bases[7] = {
117 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
118 };
119 static unsigned char specials_idx[] = {
120 0x07, 0x08,
121 0x17, 0x18, 0x19,
122 0x37, 0x38
123 };
124 static char *specials[] = {
125 "Rear Panel", "Drive Bar",
126 "Riser", "HDMI", "ATAPI",
127 "Mobile-In", "Mobile-Out"
128 };
129 int i;
130 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
131 if ((cfg & 0x0f) < 7)
132 return bases[cfg & 0x0f];
133 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
134 if (cfg == specials_idx[i])
135 return specials[i];
136 }
137 return "UNKNOWN";
138}
139
140const char *snd_hda_get_jack_connectivity(u32 cfg)
141{
142 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
143
144 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
145}
146
147const char *snd_hda_get_jack_type(u32 cfg)
148{
149 static char *jack_types[16] = {
150 "Line Out", "Speaker", "HP Out", "CD",
151 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
152 "Line In", "Aux", "Mic", "Telephony",
153 "SPDIF In", "Digitial In", "Reserved", "Other"
154 };
155
156 return jack_types[(cfg & AC_DEFCFG_DEVICE)
157 >> AC_DEFCFG_DEVICE_SHIFT];
158}
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160/**
161 * snd_hda_codec_read - send a command and get the response
162 * @codec: the HDA codec
163 * @nid: NID to send the command
164 * @direct: direct flag
165 * @verb: the verb to send
166 * @parm: the parameter for the verb
167 *
168 * Send a single command and read the corresponding response.
169 *
170 * Returns the obtained response value, or -1 for an error.
171 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200172unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
173 int direct,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 unsigned int verb, unsigned int parm)
175{
176 unsigned int res;
Takashi Iwaicb53c622007-08-10 17:21:45 +0200177 snd_hda_power_up(codec);
Ingo Molnar62932df2006-01-16 16:34:20 +0100178 mutex_lock(&codec->bus->cmd_mutex);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200179 if (!codec->bus->ops.command(codec, nid, direct, verb, parm))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 res = codec->bus->ops.get_response(codec);
181 else
182 res = (unsigned int)-1;
Ingo Molnar62932df2006-01-16 16:34:20 +0100183 mutex_unlock(&codec->bus->cmd_mutex);
Takashi Iwaicb53c622007-08-10 17:21:45 +0200184 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 return res;
186}
187
188/**
189 * snd_hda_codec_write - send a single command without waiting for response
190 * @codec: the HDA codec
191 * @nid: NID to send the command
192 * @direct: direct flag
193 * @verb: the verb to send
194 * @parm: the parameter for the verb
195 *
196 * Send a single command without waiting for response.
197 *
198 * Returns 0 if successful, or a negative error code.
199 */
200int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
201 unsigned int verb, unsigned int parm)
202{
203 int err;
Takashi Iwaicb53c622007-08-10 17:21:45 +0200204 snd_hda_power_up(codec);
Ingo Molnar62932df2006-01-16 16:34:20 +0100205 mutex_lock(&codec->bus->cmd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 err = codec->bus->ops.command(codec, nid, direct, verb, parm);
Ingo Molnar62932df2006-01-16 16:34:20 +0100207 mutex_unlock(&codec->bus->cmd_mutex);
Takashi Iwaicb53c622007-08-10 17:21:45 +0200208 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 return err;
210}
211
212/**
213 * snd_hda_sequence_write - sequence writes
214 * @codec: the HDA codec
215 * @seq: VERB array to send
216 *
217 * Send the commands sequentially from the given array.
218 * The array must be terminated with NID=0.
219 */
220void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
221{
222 for (; seq->nid; seq++)
223 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
224}
225
226/**
227 * snd_hda_get_sub_nodes - get the range of sub nodes
228 * @codec: the HDA codec
229 * @nid: NID to parse
230 * @start_id: the pointer to store the start NID
231 *
232 * Parse the NID and store the start NID of its sub-nodes.
233 * Returns the number of sub-nodes.
234 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200235int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
236 hda_nid_t *start_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
238 unsigned int parm;
239
240 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
Danny Tholene8a7f132007-09-11 21:41:56 +0200241 if (parm == -1)
242 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 *start_id = (parm >> 16) & 0x7fff;
244 return (int)(parm & 0x7fff);
245}
246
247/**
248 * snd_hda_get_connections - get connection list
249 * @codec: the HDA codec
250 * @nid: NID to parse
251 * @conn_list: connection list array
252 * @max_conns: max. number of connections to store
253 *
254 * Parses the connection list of the given widget and stores the list
255 * of NIDs.
256 *
257 * Returns the number of connections, or a negative error code.
258 */
259int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
260 hda_nid_t *conn_list, int max_conns)
261{
262 unsigned int parm;
Takashi Iwai54d17402005-11-21 16:33:22 +0100263 int i, conn_len, conns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 unsigned int shift, num_elems, mask;
Takashi Iwai54d17402005-11-21 16:33:22 +0100265 hda_nid_t prev_nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Takashi Iwaida3cec32008-08-08 17:12:14 +0200267 if (snd_BUG_ON(!conn_list || max_conns <= 0))
268 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
271 if (parm & AC_CLIST_LONG) {
272 /* long form */
273 shift = 16;
274 num_elems = 2;
275 } else {
276 /* short form */
277 shift = 8;
278 num_elems = 4;
279 }
280 conn_len = parm & AC_CLIST_LENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 mask = (1 << (shift-1)) - 1;
282
Takashi Iwai0ba21762007-04-16 11:29:14 +0200283 if (!conn_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 return 0; /* no connection */
285
286 if (conn_len == 1) {
287 /* single connection */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200288 parm = snd_hda_codec_read(codec, nid, 0,
289 AC_VERB_GET_CONNECT_LIST, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 conn_list[0] = parm & mask;
291 return 1;
292 }
293
294 /* multi connection */
295 conns = 0;
Takashi Iwai54d17402005-11-21 16:33:22 +0100296 prev_nid = 0;
297 for (i = 0; i < conn_len; i++) {
298 int range_val;
299 hda_nid_t val, n;
300
301 if (i % num_elems == 0)
302 parm = snd_hda_codec_read(codec, nid, 0,
303 AC_VERB_GET_CONNECT_LIST, i);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200304 range_val = !!(parm & (1 << (shift-1))); /* ranges */
Takashi Iwai54d17402005-11-21 16:33:22 +0100305 val = parm & mask;
306 parm >>= shift;
307 if (range_val) {
308 /* ranges between the previous and this one */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200309 if (!prev_nid || prev_nid >= val) {
310 snd_printk(KERN_WARNING "hda_codec: "
311 "invalid dep_range_val %x:%x\n",
312 prev_nid, val);
Takashi Iwai54d17402005-11-21 16:33:22 +0100313 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100315 for (n = prev_nid + 1; n <= val; n++) {
316 if (conns >= max_conns) {
Takashi Iwai0ba21762007-04-16 11:29:14 +0200317 snd_printk(KERN_ERR
318 "Too many connections\n");
Takashi Iwai54d17402005-11-21 16:33:22 +0100319 return -EINVAL;
320 }
321 conn_list[conns++] = n;
322 }
323 } else {
324 if (conns >= max_conns) {
325 snd_printk(KERN_ERR "Too many connections\n");
326 return -EINVAL;
327 }
328 conn_list[conns++] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100330 prev_nid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 }
332 return conns;
333}
334
335
336/**
337 * snd_hda_queue_unsol_event - add an unsolicited event to queue
338 * @bus: the BUS
339 * @res: unsolicited event (lower 32bit of RIRB entry)
340 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
341 *
342 * Adds the given event to the queue. The events are processed in
343 * the workqueue asynchronously. Call this function in the interrupt
344 * hanlder when RIRB receives an unsolicited event.
345 *
346 * Returns 0 if successful, or a negative error code.
347 */
348int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
349{
350 struct hda_bus_unsolicited *unsol;
351 unsigned int wp;
352
Takashi Iwai0ba21762007-04-16 11:29:14 +0200353 unsol = bus->unsol;
354 if (!unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 return 0;
356
357 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
358 unsol->wp = wp;
359
360 wp <<= 1;
361 unsol->queue[wp] = res;
362 unsol->queue[wp + 1] = res_ex;
363
Takashi Iwaie250af22006-12-19 17:08:52 +0100364 schedule_work(&unsol->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
366 return 0;
367}
368
369/*
Wu Fengguang5c1d1a92008-10-07 14:17:53 +0800370 * process queued unsolicited events
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 */
David Howellsc4028952006-11-22 14:57:56 +0000372static void process_unsol_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
David Howellsc4028952006-11-22 14:57:56 +0000374 struct hda_bus_unsolicited *unsol =
375 container_of(work, struct hda_bus_unsolicited, work);
376 struct hda_bus *bus = unsol->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 struct hda_codec *codec;
378 unsigned int rp, caddr, res;
379
380 while (unsol->rp != unsol->wp) {
381 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
382 unsol->rp = rp;
383 rp <<= 1;
384 res = unsol->queue[rp];
385 caddr = unsol->queue[rp + 1];
Takashi Iwai0ba21762007-04-16 11:29:14 +0200386 if (!(caddr & (1 << 4))) /* no unsolicited event? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 continue;
388 codec = bus->caddr_tbl[caddr & 0x0f];
389 if (codec && codec->patch_ops.unsol_event)
390 codec->patch_ops.unsol_event(codec, res);
391 }
392}
393
394/*
395 * initialize unsolicited queue
396 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200397static int init_unsol_queue(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
399 struct hda_bus_unsolicited *unsol;
400
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100401 if (bus->unsol) /* already initialized */
402 return 0;
403
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200404 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200405 if (!unsol) {
406 snd_printk(KERN_ERR "hda_codec: "
407 "can't allocate unsolicited queue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 return -ENOMEM;
409 }
David Howellsc4028952006-11-22 14:57:56 +0000410 INIT_WORK(&unsol->work, process_unsol_events);
411 unsol->bus = bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 bus->unsol = unsol;
413 return 0;
414}
415
416/*
417 * destructor
418 */
419static void snd_hda_codec_free(struct hda_codec *codec);
420
421static int snd_hda_bus_free(struct hda_bus *bus)
422{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200423 struct hda_codec *codec, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Takashi Iwai0ba21762007-04-16 11:29:14 +0200425 if (!bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 return 0;
427 if (bus->unsol) {
Takashi Iwaie250af22006-12-19 17:08:52 +0100428 flush_scheduled_work();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 kfree(bus->unsol);
430 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200431 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 snd_hda_codec_free(codec);
433 }
434 if (bus->ops.private_free)
435 bus->ops.private_free(bus);
436 kfree(bus);
437 return 0;
438}
439
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100440static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
442 struct hda_bus *bus = device->device_data;
443 return snd_hda_bus_free(bus);
444}
445
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200446#ifdef CONFIG_SND_HDA_HWDEP
447static int snd_hda_bus_dev_register(struct snd_device *device)
448{
449 struct hda_bus *bus = device->device_data;
450 struct hda_codec *codec;
451 list_for_each_entry(codec, &bus->codec_list, list) {
452 snd_hda_hwdep_add_sysfs(codec);
453 }
454 return 0;
455}
456#else
457#define snd_hda_bus_dev_register NULL
458#endif
459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460/**
461 * snd_hda_bus_new - create a HDA bus
462 * @card: the card entry
463 * @temp: the template for hda_bus information
464 * @busp: the pointer to store the created bus instance
465 *
466 * Returns 0 if successful, or a negative error code.
467 */
Takashi Iwai756e2b02007-04-16 11:27:07 +0200468int __devinit snd_hda_bus_new(struct snd_card *card,
469 const struct hda_bus_template *temp,
470 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
472 struct hda_bus *bus;
473 int err;
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100474 static struct snd_device_ops dev_ops = {
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200475 .dev_register = snd_hda_bus_dev_register,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 .dev_free = snd_hda_bus_dev_free,
477 };
478
Takashi Iwaida3cec32008-08-08 17:12:14 +0200479 if (snd_BUG_ON(!temp))
480 return -EINVAL;
481 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
482 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
484 if (busp)
485 *busp = NULL;
486
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200487 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 if (bus == NULL) {
489 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
490 return -ENOMEM;
491 }
492
493 bus->card = card;
494 bus->private_data = temp->private_data;
495 bus->pci = temp->pci;
496 bus->modelname = temp->modelname;
497 bus->ops = temp->ops;
498
Ingo Molnar62932df2006-01-16 16:34:20 +0100499 mutex_init(&bus->cmd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 INIT_LIST_HEAD(&bus->codec_list);
501
Takashi Iwai0ba21762007-04-16 11:29:14 +0200502 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
503 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 snd_hda_bus_free(bus);
505 return err;
506 }
507 if (busp)
508 *busp = bus;
509 return 0;
510}
511
Takashi Iwai82467612007-07-27 19:15:54 +0200512#ifdef CONFIG_SND_HDA_GENERIC
513#define is_generic_config(codec) \
Takashi Iwaif44ac832008-07-30 15:01:45 +0200514 (codec->modelname && !strcmp(codec->modelname, "generic"))
Takashi Iwai82467612007-07-27 19:15:54 +0200515#else
516#define is_generic_config(codec) 0
517#endif
518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519/*
520 * find a matching codec preset
521 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200522static const struct hda_codec_preset *
Takashi Iwai756e2b02007-04-16 11:27:07 +0200523find_codec_preset(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524{
525 const struct hda_codec_preset **tbl, *preset;
526
Takashi Iwai82467612007-07-27 19:15:54 +0200527 if (is_generic_config(codec))
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100528 return NULL; /* use the generic parser */
529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 for (tbl = hda_preset_tables; *tbl; tbl++) {
531 for (preset = *tbl; preset->id; preset++) {
532 u32 mask = preset->mask;
Marc Boucherca7cfae2008-01-22 15:32:25 +0100533 if (preset->afg && preset->afg != codec->afg)
534 continue;
535 if (preset->mfg && preset->mfg != codec->mfg)
536 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200537 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 mask = ~0;
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200539 if (preset->id == (codec->vendor_id & mask) &&
Takashi Iwai0ba21762007-04-16 11:29:14 +0200540 (!preset->rev ||
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200541 preset->rev == codec->revision_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 return preset;
543 }
544 }
545 return NULL;
546}
547
548/*
Takashi Iwaif44ac832008-07-30 15:01:45 +0200549 * get_codec_name - store the codec name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 */
Takashi Iwaif44ac832008-07-30 15:01:45 +0200551static int get_codec_name(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
553 const struct hda_vendor_id *c;
554 const char *vendor = NULL;
555 u16 vendor_id = codec->vendor_id >> 16;
Takashi Iwaif44ac832008-07-30 15:01:45 +0200556 char tmp[16], name[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
558 for (c = hda_vendor_ids; c->id; c++) {
559 if (c->id == vendor_id) {
560 vendor = c->name;
561 break;
562 }
563 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200564 if (!vendor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 sprintf(tmp, "Generic %04x", vendor_id);
566 vendor = tmp;
567 }
568 if (codec->preset && codec->preset->name)
Takashi Iwaif44ac832008-07-30 15:01:45 +0200569 snprintf(name, sizeof(name), "%s %s", vendor,
570 codec->preset->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 else
Takashi Iwaif44ac832008-07-30 15:01:45 +0200572 snprintf(name, sizeof(name), "%s ID %x", vendor,
Takashi Iwai0ba21762007-04-16 11:29:14 +0200573 codec->vendor_id & 0xffff);
Takashi Iwaif44ac832008-07-30 15:01:45 +0200574 codec->name = kstrdup(name, GFP_KERNEL);
575 if (!codec->name)
576 return -ENOMEM;
577 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578}
579
580/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200581 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 */
Takashi Iwai756e2b02007-04-16 11:27:07 +0200583static void __devinit setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
585 int i, total_nodes;
586 hda_nid_t nid;
587
588 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
589 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +0200590 unsigned int func;
591 func = snd_hda_param_read(codec, nid, AC_PAR_FUNCTION_TYPE);
592 switch (func & 0xff) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200593 case AC_GRP_AUDIO_FUNCTION:
594 codec->afg = nid;
595 break;
596 case AC_GRP_MODEM_FUNCTION:
597 codec->mfg = nid;
598 break;
599 default:
600 break;
601 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603}
604
605/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100606 * read widget caps for each widget and store in cache
607 */
608static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
609{
610 int i;
611 hda_nid_t nid;
612
613 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
614 &codec->start_nid);
615 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200616 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +0100617 return -ENOMEM;
618 nid = codec->start_nid;
619 for (i = 0; i < codec->num_nodes; i++, nid++)
620 codec->wcaps[i] = snd_hda_param_read(codec, nid,
621 AC_PAR_AUDIO_WIDGET_CAP);
622 return 0;
623}
624
625
Takashi Iwai01751f52007-08-10 16:59:39 +0200626static void init_hda_cache(struct hda_cache_rec *cache,
627 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +0200628static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +0200629
Takashi Iwai54d17402005-11-21 16:33:22 +0100630/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 * codec destructor
632 */
633static void snd_hda_codec_free(struct hda_codec *codec)
634{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200635 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 return;
Takashi Iwaicb53c622007-08-10 17:21:45 +0200637#ifdef CONFIG_SND_HDA_POWER_SAVE
638 cancel_delayed_work(&codec->power_work);
Takashi Iwai2525fdc2007-08-15 22:18:22 +0200639 flush_scheduled_work();
Takashi Iwaicb53c622007-08-10 17:21:45 +0200640#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +0200642 snd_array_free(&codec->mixers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 codec->bus->caddr_tbl[codec->addr] = NULL;
644 if (codec->patch_ops.free)
645 codec->patch_ops.free(codec);
Takashi Iwai01751f52007-08-10 16:59:39 +0200646 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +0200647 free_hda_cache(&codec->cmd_cache);
Takashi Iwaif44ac832008-07-30 15:01:45 +0200648 kfree(codec->name);
649 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +0100650 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 kfree(codec);
652}
653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654/**
655 * snd_hda_codec_new - create a HDA codec
656 * @bus: the bus to assign
657 * @codec_addr: the codec address
658 * @codecp: the pointer to store the generated codec
659 *
660 * Returns 0 if successful, or a negative error code.
661 */
Takashi Iwai756e2b02007-04-16 11:27:07 +0200662int __devinit snd_hda_codec_new(struct hda_bus *bus, unsigned int codec_addr,
663 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664{
665 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +0200666 char component[31];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 int err;
668
Takashi Iwaida3cec32008-08-08 17:12:14 +0200669 if (snd_BUG_ON(!bus))
670 return -EINVAL;
671 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
672 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
674 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +0200675 snd_printk(KERN_ERR "hda_codec: "
676 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 return -EBUSY;
678 }
679
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200680 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 if (codec == NULL) {
682 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
683 return -ENOMEM;
684 }
685
686 codec->bus = bus;
687 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +0100688 mutex_init(&codec->spdif_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +0200689 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +0200690 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwaid13bd412008-07-30 15:01:45 +0200691 snd_array_init(&codec->mixers, sizeof(struct snd_kcontrol *), 32);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200692 if (codec->bus->modelname) {
693 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
694 if (!codec->modelname) {
695 snd_hda_codec_free(codec);
696 return -ENODEV;
697 }
698 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Takashi Iwaicb53c622007-08-10 17:21:45 +0200700#ifdef CONFIG_SND_HDA_POWER_SAVE
701 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
702 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
703 * the caller has to power down appropriatley after initialization
704 * phase.
705 */
706 hda_keep_power_on(codec);
707#endif
708
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 list_add_tail(&codec->list, &bus->codec_list);
710 bus->caddr_tbl[codec_addr] = codec;
711
Takashi Iwai0ba21762007-04-16 11:29:14 +0200712 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
713 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +0100714 if (codec->vendor_id == -1)
715 /* read again, hopefully the access method was corrected
716 * in the last read...
717 */
718 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
719 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200720 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
721 AC_PAR_SUBSYSTEM_ID);
722 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
723 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200725 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200726 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200727 snd_printdd("hda_codec: no AFG or MFG node found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 snd_hda_codec_free(codec);
729 return -ENODEV;
730 }
731
Takashi Iwai54d17402005-11-21 16:33:22 +0100732 if (read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg) < 0) {
733 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
734 snd_hda_codec_free(codec);
735 return -ENOMEM;
736 }
737
Takashi Iwai0ba21762007-04-16 11:29:14 +0200738 if (!codec->subsystem_id) {
Takashi Iwai86284e42005-10-11 15:05:54 +0200739 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200740 codec->subsystem_id =
741 snd_hda_codec_read(codec, nid, 0,
742 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +0200743 }
Takashi Iwaif44ac832008-07-30 15:01:45 +0200744 if (bus->modelname)
745 codec->modelname = kstrdup(bus->modelname, GFP_KERNEL);
Takashi Iwai86284e42005-10-11 15:05:54 +0200746
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200747 err = snd_hda_codec_configure(codec);
748 if (err < 0) {
749 snd_hda_codec_free(codec);
750 return err;
751 }
752 snd_hda_codec_proc_new(codec);
753
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200754 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200755
756 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
757 codec->subsystem_id, codec->revision_id);
758 snd_component_add(codec->bus->card, component);
759
760 if (codecp)
761 *codecp = codec;
762 return 0;
763}
764
765int snd_hda_codec_configure(struct hda_codec *codec)
766{
767 int err;
768
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100769 codec->preset = find_codec_preset(codec);
Takashi Iwaif44ac832008-07-30 15:01:45 +0200770 if (!codec->name) {
771 err = get_codec_name(codec);
772 if (err < 0)
773 return err;
774 }
Takashi Iwai43ea1d42007-04-26 19:12:08 +0200775 /* audio codec should override the mixer name */
Takashi Iwaif44ac832008-07-30 15:01:45 +0200776 if (codec->afg || !*codec->bus->card->mixername)
777 strlcpy(codec->bus->card->mixername, codec->name,
778 sizeof(codec->bus->card->mixername));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
Takashi Iwai82467612007-07-27 19:15:54 +0200780 if (is_generic_config(codec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai82467612007-07-27 19:15:54 +0200782 goto patched;
783 }
Takashi Iwai82467612007-07-27 19:15:54 +0200784 if (codec->preset && codec->preset->patch) {
785 err = codec->preset->patch(codec);
786 goto patched;
787 }
788
789 /* call the default parser */
Takashi Iwai82467612007-07-27 19:15:54 +0200790 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai35a1e0c2007-10-19 08:13:40 +0200791 if (err < 0)
792 printk(KERN_ERR "hda-codec: No codec parser is available\n");
Takashi Iwai82467612007-07-27 19:15:54 +0200793
794 patched:
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200795 if (!err && codec->patch_ops.unsol_event)
796 err = init_unsol_queue(codec->bus);
797 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798}
799
800/**
801 * snd_hda_codec_setup_stream - set up the codec for streaming
802 * @codec: the CODEC to set up
803 * @nid: the NID to set up
804 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
805 * @channel_id: channel id to pass, zero based.
806 * @format: stream format.
807 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200808void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
809 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 int channel_id, int format)
811{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200812 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +0200813 return;
814
Takashi Iwai0ba21762007-04-16 11:29:14 +0200815 snd_printdd("hda_codec_setup_stream: "
816 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 nid, stream_tag, channel_id, format);
818 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID,
819 (stream_tag << 4) | channel_id);
820 msleep(1);
821 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, format);
822}
823
Takashi Iwai888afa12008-03-18 09:57:50 +0100824void snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid)
825{
826 if (!nid)
827 return;
828
829 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
830 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
831#if 0 /* keep the format */
832 msleep(1);
833 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0);
834#endif
835}
836
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837/*
838 * amp access functions
839 */
840
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200841/* FIXME: more better hash key? */
842#define HDA_HASH_KEY(nid,dir,idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200844#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
846/* initialize the hash table */
Takashi Iwai01751f52007-08-10 16:59:39 +0200847static void __devinit init_hda_cache(struct hda_cache_rec *cache,
848 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849{
Takashi Iwai01751f52007-08-10 16:59:39 +0200850 memset(cache, 0, sizeof(*cache));
851 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +0200852 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +0200853}
854
Takashi Iwai1fcaee62007-08-23 00:01:09 +0200855static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +0200856{
Takashi Iwai603c4012008-07-30 15:01:44 +0200857 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858}
859
860/* query the hash. allocate an entry if not found. */
Takashi Iwai01751f52007-08-10 16:59:39 +0200861static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
862 u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863{
Takashi Iwai01751f52007-08-10 16:59:39 +0200864 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
865 u16 cur = cache->hash[idx];
Takashi Iwai603c4012008-07-30 15:01:44 +0200866 struct hda_cache_head *info_head = cache->buf.list;
Takashi Iwai01751f52007-08-10 16:59:39 +0200867 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
869 while (cur != 0xffff) {
Takashi Iwai603c4012008-07-30 15:01:44 +0200870 info = &info_head[cur];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 if (info->key == key)
872 return info;
873 cur = info->next;
874 }
875
876 /* add a new hash entry */
Takashi Iwai603c4012008-07-30 15:01:44 +0200877 info = snd_array_new(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 info->key = key;
Takashi Iwai01751f52007-08-10 16:59:39 +0200879 info->val = 0;
880 info->next = cache->hash[idx];
881 cache->hash[idx] = cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
883 return info;
884}
885
Takashi Iwai01751f52007-08-10 16:59:39 +0200886/* query and allocate an amp hash entry */
887static inline struct hda_amp_info *
888get_alloc_amp_hash(struct hda_codec *codec, u32 key)
889{
890 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
891}
892
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893/*
894 * query AMP capabilities for the given widget and direction
895 */
Matthew Ranostay09a99952008-01-24 11:49:21 +0100896u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200898 struct hda_amp_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Takashi Iwai0ba21762007-04-16 11:29:14 +0200900 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
901 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 return 0;
Takashi Iwai01751f52007-08-10 16:59:39 +0200903 if (!(info->head.val & INFO_AMP_CAPS)) {
Takashi Iwai0ba21762007-04-16 11:29:14 +0200904 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 nid = codec->afg;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200906 info->amp_caps = snd_hda_param_read(codec, nid,
907 direction == HDA_OUTPUT ?
908 AC_PAR_AMP_OUT_CAP :
909 AC_PAR_AMP_IN_CAP);
Takashi Iwaib75e53f2007-05-10 16:56:09 +0200910 if (info->amp_caps)
Takashi Iwai01751f52007-08-10 16:59:39 +0200911 info->head.val |= INFO_AMP_CAPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 }
913 return info->amp_caps;
914}
915
Takashi Iwai897cc182007-05-29 19:01:37 +0200916int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
917 unsigned int caps)
918{
919 struct hda_amp_info *info;
920
921 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
922 if (!info)
923 return -EINVAL;
924 info->amp_caps = caps;
Takashi Iwai01751f52007-08-10 16:59:39 +0200925 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai897cc182007-05-29 19:01:37 +0200926 return 0;
927}
928
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929/*
930 * read the current volume to info
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200931 * if the cache exists, read the cache value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200933static unsigned int get_vol_mute(struct hda_codec *codec,
934 struct hda_amp_info *info, hda_nid_t nid,
935 int ch, int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936{
937 u32 val, parm;
938
Takashi Iwai01751f52007-08-10 16:59:39 +0200939 if (info->head.val & INFO_AMP_VOL(ch))
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200940 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
942 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
943 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
944 parm |= index;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200945 val = snd_hda_codec_read(codec, nid, 0,
946 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 info->vol[ch] = val & 0xff;
Takashi Iwai01751f52007-08-10 16:59:39 +0200948 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200949 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950}
951
952/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200953 * write the current volume in info to the h/w and update the cache
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200955static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
Takashi Iwai0ba21762007-04-16 11:29:14 +0200956 hda_nid_t nid, int ch, int direction, int index,
957 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958{
959 u32 parm;
960
961 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
962 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
963 parm |= index << AC_AMP_SET_INDEX_SHIFT;
964 parm |= val;
965 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200966 info->vol[ch] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967}
968
969/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200970 * read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 */
Takashi Iwai834be882006-03-01 14:16:17 +0100972int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
973 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200975 struct hda_amp_info *info;
976 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
977 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200979 return get_vol_mute(codec, info, nid, ch, direction, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980}
981
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200982/*
983 * update the AMP value, mask = bit mask to set, val = the value
984 */
Takashi Iwai834be882006-03-01 14:16:17 +0100985int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
986 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200988 struct hda_amp_info *info;
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200989
Takashi Iwai0ba21762007-04-16 11:29:14 +0200990 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
991 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200993 val &= mask;
994 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
Takashi Iwai82beb8f2007-08-10 17:09:26 +0200995 if (info->vol[ch] == val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200997 put_vol_mute(codec, info, nid, ch, direction, idx, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 return 1;
999}
1000
Takashi Iwai47fd8302007-08-10 17:11:07 +02001001/*
1002 * update the AMP stereo with the same mask and value
1003 */
1004int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1005 int direction, int idx, int mask, int val)
1006{
1007 int ch, ret = 0;
1008 for (ch = 0; ch < 2; ch++)
1009 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1010 idx, mask, val);
1011 return ret;
1012}
1013
Takashi Iwaicb53c622007-08-10 17:21:45 +02001014#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001015/* resume the all amp commands from the cache */
1016void snd_hda_codec_resume_amp(struct hda_codec *codec)
1017{
Takashi Iwai603c4012008-07-30 15:01:44 +02001018 struct hda_amp_info *buffer = codec->amp_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001019 int i;
1020
Takashi Iwai603c4012008-07-30 15:01:44 +02001021 for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001022 u32 key = buffer->head.key;
1023 hda_nid_t nid;
1024 unsigned int idx, dir, ch;
1025 if (!key)
1026 continue;
1027 nid = key & 0xff;
1028 idx = (key >> 16) & 0xff;
1029 dir = (key >> 24) & 0xff;
1030 for (ch = 0; ch < 2; ch++) {
1031 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
1032 continue;
1033 put_vol_mute(codec, buffer, nid, ch, dir, idx,
1034 buffer->vol[ch]);
1035 }
1036 }
1037}
Takashi Iwaicb53c622007-08-10 17:21:45 +02001038#endif /* SND_HDA_NEEDS_RESUME */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040/* volume */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001041int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1042 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043{
1044 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1045 u16 nid = get_amp_nid(kcontrol);
1046 u8 chs = get_amp_channels(kcontrol);
1047 int dir = get_amp_direction(kcontrol);
1048 u32 caps;
1049
1050 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001051 /* num steps */
1052 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1053 if (!caps) {
1054 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01001055 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
1056 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 return -EINVAL;
1058 }
1059 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1060 uinfo->count = chs == 3 ? 2 : 1;
1061 uinfo->value.integer.min = 0;
1062 uinfo->value.integer.max = caps;
1063 return 0;
1064}
1065
Takashi Iwai0ba21762007-04-16 11:29:14 +02001066int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1067 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068{
1069 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1070 hda_nid_t nid = get_amp_nid(kcontrol);
1071 int chs = get_amp_channels(kcontrol);
1072 int dir = get_amp_direction(kcontrol);
1073 int idx = get_amp_index(kcontrol);
1074 long *valp = ucontrol->value.integer.value;
1075
1076 if (chs & 1)
Takashi Iwai47fd8302007-08-10 17:11:07 +02001077 *valp++ = snd_hda_codec_amp_read(codec, nid, 0, dir, idx)
1078 & HDA_AMP_VOLMASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 if (chs & 2)
Takashi Iwai47fd8302007-08-10 17:11:07 +02001080 *valp = snd_hda_codec_amp_read(codec, nid, 1, dir, idx)
1081 & HDA_AMP_VOLMASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 return 0;
1083}
1084
Takashi Iwai0ba21762007-04-16 11:29:14 +02001085int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1086 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087{
1088 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1089 hda_nid_t nid = get_amp_nid(kcontrol);
1090 int chs = get_amp_channels(kcontrol);
1091 int dir = get_amp_direction(kcontrol);
1092 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 long *valp = ucontrol->value.integer.value;
1094 int change = 0;
1095
Takashi Iwaicb53c622007-08-10 17:21:45 +02001096 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001097 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001098 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
1099 0x7f, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001100 valp++;
1101 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001102 if (chs & 2)
1103 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001104 0x7f, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001105 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 return change;
1107}
1108
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001109int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1110 unsigned int size, unsigned int __user *_tlv)
1111{
1112 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1113 hda_nid_t nid = get_amp_nid(kcontrol);
1114 int dir = get_amp_direction(kcontrol);
1115 u32 caps, val1, val2;
1116
1117 if (size < 4 * sizeof(unsigned int))
1118 return -ENOMEM;
1119 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001120 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1121 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001122 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
1123 val1 = ((int)val1) * ((int)val2);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001124 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
1125 return -EFAULT;
1126 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
1127 return -EFAULT;
1128 if (put_user(val1, _tlv + 2))
1129 return -EFAULT;
1130 if (put_user(val2, _tlv + 3))
1131 return -EFAULT;
1132 return 0;
1133}
1134
Takashi Iwai2134ea42008-01-10 16:53:55 +01001135/*
1136 * set (static) TLV for virtual master volume; recalculated as max 0dB
1137 */
1138void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
1139 unsigned int *tlv)
1140{
1141 u32 caps;
1142 int nums, step;
1143
1144 caps = query_amp_caps(codec, nid, dir);
1145 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1146 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1147 step = (step + 1) * 25;
1148 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
1149 tlv[1] = 2 * sizeof(unsigned int);
1150 tlv[2] = -nums * step;
1151 tlv[3] = step;
1152}
1153
1154/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01001155static struct snd_kcontrol *
1156_snd_hda_find_mixer_ctl(struct hda_codec *codec,
1157 const char *name, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01001158{
1159 struct snd_ctl_elem_id id;
1160 memset(&id, 0, sizeof(id));
1161 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwai09f99702008-02-04 12:31:13 +01001162 id.index = idx;
Takashi Iwai2134ea42008-01-10 16:53:55 +01001163 strcpy(id.name, name);
1164 return snd_ctl_find_id(codec->bus->card, &id);
1165}
1166
Takashi Iwai09f99702008-02-04 12:31:13 +01001167struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
1168 const char *name)
1169{
1170 return _snd_hda_find_mixer_ctl(codec, name, 0);
1171}
1172
Takashi Iwaid13bd412008-07-30 15:01:45 +02001173/* Add a control element and assign to the codec */
1174int snd_hda_ctl_add(struct hda_codec *codec, struct snd_kcontrol *kctl)
1175{
1176 int err;
1177 struct snd_kcontrol **knewp;
1178
1179 err = snd_ctl_add(codec->bus->card, kctl);
1180 if (err < 0)
1181 return err;
1182 knewp = snd_array_new(&codec->mixers);
1183 if (!knewp)
1184 return -ENOMEM;
1185 *knewp = kctl;
1186 return 0;
1187}
1188
1189/* Clear all controls assigned to the given codec */
1190void snd_hda_ctls_clear(struct hda_codec *codec)
1191{
1192 int i;
1193 struct snd_kcontrol **kctls = codec->mixers.list;
1194 for (i = 0; i < codec->mixers.used; i++)
1195 snd_ctl_remove(codec->bus->card, kctls[i]);
1196 snd_array_free(&codec->mixers);
1197}
1198
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001199void snd_hda_codec_reset(struct hda_codec *codec)
1200{
1201 int i;
1202
1203#ifdef CONFIG_SND_HDA_POWER_SAVE
1204 cancel_delayed_work(&codec->power_work);
1205 flush_scheduled_work();
1206#endif
1207 snd_hda_ctls_clear(codec);
1208 /* relase PCMs */
1209 for (i = 0; i < codec->num_pcms; i++) {
1210 if (codec->pcm_info[i].pcm)
1211 snd_device_free(codec->bus->card,
1212 codec->pcm_info[i].pcm);
1213 }
1214 if (codec->patch_ops.free)
1215 codec->patch_ops.free(codec);
1216 codec->spec = NULL;
1217 free_hda_cache(&codec->amp_cache);
1218 free_hda_cache(&codec->cmd_cache);
1219 codec->num_pcms = 0;
1220 codec->pcm_info = NULL;
1221 codec->preset = NULL;
1222}
1223
Takashi Iwai2134ea42008-01-10 16:53:55 +01001224/* create a virtual master control and add slaves */
1225int snd_hda_add_vmaster(struct hda_codec *codec, char *name,
1226 unsigned int *tlv, const char **slaves)
1227{
1228 struct snd_kcontrol *kctl;
1229 const char **s;
1230 int err;
1231
Takashi Iwai2f085542008-02-22 18:43:50 +01001232 for (s = slaves; *s && !snd_hda_find_mixer_ctl(codec, *s); s++)
1233 ;
1234 if (!*s) {
1235 snd_printdd("No slave found for %s\n", name);
1236 return 0;
1237 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01001238 kctl = snd_ctl_make_virtual_master(name, tlv);
1239 if (!kctl)
1240 return -ENOMEM;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001241 err = snd_hda_ctl_add(codec, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001242 if (err < 0)
1243 return err;
1244
1245 for (s = slaves; *s; s++) {
1246 struct snd_kcontrol *sctl;
1247
1248 sctl = snd_hda_find_mixer_ctl(codec, *s);
1249 if (!sctl) {
1250 snd_printdd("Cannot find slave %s, skipped\n", *s);
1251 continue;
1252 }
1253 err = snd_ctl_add_slave(kctl, sctl);
1254 if (err < 0)
1255 return err;
1256 }
1257 return 0;
1258}
1259
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260/* switch */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001261int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
1262 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263{
1264 int chs = get_amp_channels(kcontrol);
1265
1266 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1267 uinfo->count = chs == 3 ? 2 : 1;
1268 uinfo->value.integer.min = 0;
1269 uinfo->value.integer.max = 1;
1270 return 0;
1271}
1272
Takashi Iwai0ba21762007-04-16 11:29:14 +02001273int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
1274 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275{
1276 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1277 hda_nid_t nid = get_amp_nid(kcontrol);
1278 int chs = get_amp_channels(kcontrol);
1279 int dir = get_amp_direction(kcontrol);
1280 int idx = get_amp_index(kcontrol);
1281 long *valp = ucontrol->value.integer.value;
1282
1283 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02001284 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02001285 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02001287 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02001288 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 return 0;
1290}
1291
Takashi Iwai0ba21762007-04-16 11:29:14 +02001292int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
1293 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294{
1295 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1296 hda_nid_t nid = get_amp_nid(kcontrol);
1297 int chs = get_amp_channels(kcontrol);
1298 int dir = get_amp_direction(kcontrol);
1299 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 long *valp = ucontrol->value.integer.value;
1301 int change = 0;
1302
Takashi Iwaicb53c622007-08-10 17:21:45 +02001303 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001304 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001305 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02001306 HDA_AMP_MUTE,
1307 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001308 valp++;
1309 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001310 if (chs & 2)
1311 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02001312 HDA_AMP_MUTE,
1313 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001314#ifdef CONFIG_SND_HDA_POWER_SAVE
1315 if (codec->patch_ops.check_power_status)
1316 codec->patch_ops.check_power_status(codec, nid);
1317#endif
1318 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 return change;
1320}
1321
1322/*
Takashi Iwai985be542005-11-02 18:26:49 +01001323 * bound volume controls
1324 *
1325 * bind multiple volumes (# indices, from 0)
1326 */
1327
1328#define AMP_VAL_IDX_SHIFT 19
1329#define AMP_VAL_IDX_MASK (0x0f<<19)
1330
Takashi Iwai0ba21762007-04-16 11:29:14 +02001331int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
1332 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01001333{
1334 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1335 unsigned long pval;
1336 int err;
1337
Ingo Molnar62932df2006-01-16 16:34:20 +01001338 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Takashi Iwai985be542005-11-02 18:26:49 +01001339 pval = kcontrol->private_value;
1340 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
1341 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
1342 kcontrol->private_value = pval;
Ingo Molnar62932df2006-01-16 16:34:20 +01001343 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01001344 return err;
1345}
1346
Takashi Iwai0ba21762007-04-16 11:29:14 +02001347int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
1348 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01001349{
1350 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1351 unsigned long pval;
1352 int i, indices, err = 0, change = 0;
1353
Ingo Molnar62932df2006-01-16 16:34:20 +01001354 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Takashi Iwai985be542005-11-02 18:26:49 +01001355 pval = kcontrol->private_value;
1356 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
1357 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001358 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
1359 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01001360 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1361 if (err < 0)
1362 break;
1363 change |= err;
1364 }
1365 kcontrol->private_value = pval;
Ingo Molnar62932df2006-01-16 16:34:20 +01001366 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01001367 return err < 0 ? err : change;
1368}
1369
1370/*
Takashi Iwai532d5382007-07-27 19:02:40 +02001371 * generic bound volume/swtich controls
1372 */
1373int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
1374 struct snd_ctl_elem_info *uinfo)
1375{
1376 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1377 struct hda_bind_ctls *c;
1378 int err;
1379
Takashi Iwai532d5382007-07-27 19:02:40 +02001380 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01001381 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02001382 kcontrol->private_value = *c->values;
1383 err = c->ops->info(kcontrol, uinfo);
1384 kcontrol->private_value = (long)c;
1385 mutex_unlock(&codec->spdif_mutex);
1386 return err;
1387}
1388
1389int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
1390 struct snd_ctl_elem_value *ucontrol)
1391{
1392 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1393 struct hda_bind_ctls *c;
1394 int err;
1395
Takashi Iwai532d5382007-07-27 19:02:40 +02001396 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01001397 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02001398 kcontrol->private_value = *c->values;
1399 err = c->ops->get(kcontrol, ucontrol);
1400 kcontrol->private_value = (long)c;
1401 mutex_unlock(&codec->spdif_mutex);
1402 return err;
1403}
1404
1405int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
1406 struct snd_ctl_elem_value *ucontrol)
1407{
1408 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1409 struct hda_bind_ctls *c;
1410 unsigned long *vals;
1411 int err = 0, change = 0;
1412
Takashi Iwai532d5382007-07-27 19:02:40 +02001413 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01001414 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02001415 for (vals = c->values; *vals; vals++) {
1416 kcontrol->private_value = *vals;
1417 err = c->ops->put(kcontrol, ucontrol);
1418 if (err < 0)
1419 break;
1420 change |= err;
1421 }
1422 kcontrol->private_value = (long)c;
1423 mutex_unlock(&codec->spdif_mutex);
1424 return err < 0 ? err : change;
1425}
1426
1427int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1428 unsigned int size, unsigned int __user *tlv)
1429{
1430 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1431 struct hda_bind_ctls *c;
1432 int err;
1433
Takashi Iwai532d5382007-07-27 19:02:40 +02001434 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01001435 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02001436 kcontrol->private_value = *c->values;
1437 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
1438 kcontrol->private_value = (long)c;
1439 mutex_unlock(&codec->spdif_mutex);
1440 return err;
1441}
1442
1443struct hda_ctl_ops snd_hda_bind_vol = {
1444 .info = snd_hda_mixer_amp_volume_info,
1445 .get = snd_hda_mixer_amp_volume_get,
1446 .put = snd_hda_mixer_amp_volume_put,
1447 .tlv = snd_hda_mixer_amp_tlv
1448};
1449
1450struct hda_ctl_ops snd_hda_bind_sw = {
1451 .info = snd_hda_mixer_amp_switch_info,
1452 .get = snd_hda_mixer_amp_switch_get,
1453 .put = snd_hda_mixer_amp_switch_put,
1454 .tlv = snd_hda_mixer_amp_tlv
1455};
1456
1457/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 * SPDIF out controls
1459 */
1460
Takashi Iwai0ba21762007-04-16 11:29:14 +02001461static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
1462 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463{
1464 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1465 uinfo->count = 1;
1466 return 0;
1467}
1468
Takashi Iwai0ba21762007-04-16 11:29:14 +02001469static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
1470 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471{
1472 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
1473 IEC958_AES0_NONAUDIO |
1474 IEC958_AES0_CON_EMPHASIS_5015 |
1475 IEC958_AES0_CON_NOT_COPYRIGHT;
1476 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
1477 IEC958_AES1_CON_ORIGINAL;
1478 return 0;
1479}
1480
Takashi Iwai0ba21762007-04-16 11:29:14 +02001481static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
1482 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483{
1484 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
1485 IEC958_AES0_NONAUDIO |
1486 IEC958_AES0_PRO_EMPHASIS_5015;
1487 return 0;
1488}
1489
Takashi Iwai0ba21762007-04-16 11:29:14 +02001490static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
1491 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492{
1493 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1494
1495 ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
1496 ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
1497 ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
1498 ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
1499
1500 return 0;
1501}
1502
1503/* convert from SPDIF status bits to HDA SPDIF bits
1504 * bit 0 (DigEn) is always set zero (to be filled later)
1505 */
1506static unsigned short convert_from_spdif_status(unsigned int sbits)
1507{
1508 unsigned short val = 0;
1509
1510 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02001511 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02001513 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001515 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
1516 IEC958_AES0_PRO_EMPHASIS_5015)
1517 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001519 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
1520 IEC958_AES0_CON_EMPHASIS_5015)
1521 val |= AC_DIG1_EMPHASIS;
1522 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
1523 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02001525 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
1527 }
1528 return val;
1529}
1530
1531/* convert to SPDIF status bits from HDA SPDIF bits
1532 */
1533static unsigned int convert_to_spdif_status(unsigned short val)
1534{
1535 unsigned int sbits = 0;
1536
Takashi Iwai0ba21762007-04-16 11:29:14 +02001537 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001539 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 sbits |= IEC958_AES0_PROFESSIONAL;
1541 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001542 if (sbits & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
1544 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001545 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001547 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001549 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
1551 sbits |= val & (0x7f << 8);
1552 }
1553 return sbits;
1554}
1555
Takashi Iwai2f728532008-09-25 16:32:41 +02001556/* set digital convert verbs both for the given NID and its slaves */
1557static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
1558 int verb, int val)
1559{
1560 hda_nid_t *d;
1561
1562 snd_hda_codec_write(codec, nid, 0, verb, val);
1563 d = codec->slave_dig_outs;
1564 if (!d)
1565 return;
1566 for (; *d; d++)
1567 snd_hda_codec_write(codec, *d, 0, verb, val);
1568}
1569
1570static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
1571 int dig1, int dig2)
1572{
1573 if (dig1 != -1)
1574 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
1575 if (dig2 != -1)
1576 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
1577}
1578
Takashi Iwai0ba21762007-04-16 11:29:14 +02001579static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
1580 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581{
1582 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1583 hda_nid_t nid = kcontrol->private_value;
1584 unsigned short val;
1585 int change;
1586
Ingo Molnar62932df2006-01-16 16:34:20 +01001587 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 codec->spdif_status = ucontrol->value.iec958.status[0] |
1589 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
1590 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
1591 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
1592 val = convert_from_spdif_status(codec->spdif_status);
1593 val |= codec->spdif_ctls & 1;
1594 change = codec->spdif_ctls != val;
1595 codec->spdif_ctls = val;
1596
Takashi Iwai2f728532008-09-25 16:32:41 +02001597 if (change)
1598 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
Ingo Molnar62932df2006-01-16 16:34:20 +01001600 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 return change;
1602}
1603
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001604#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
Takashi Iwai0ba21762007-04-16 11:29:14 +02001606static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
1607 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608{
1609 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1610
Takashi Iwai0ba21762007-04-16 11:29:14 +02001611 ucontrol->value.integer.value[0] = codec->spdif_ctls & AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 return 0;
1613}
1614
Takashi Iwai0ba21762007-04-16 11:29:14 +02001615static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
1616 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617{
1618 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1619 hda_nid_t nid = kcontrol->private_value;
1620 unsigned short val;
1621 int change;
1622
Ingo Molnar62932df2006-01-16 16:34:20 +01001623 mutex_lock(&codec->spdif_mutex);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001624 val = codec->spdif_ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02001626 val |= AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 change = codec->spdif_ctls != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001628 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 codec->spdif_ctls = val;
Takashi Iwai2f728532008-09-25 16:32:41 +02001630 set_dig_out_convert(codec, nid, val & 0xff, -1);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001631 /* unmute amp switch (if any) */
1632 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
Takashi Iwai47fd8302007-08-10 17:11:07 +02001633 (val & AC_DIG1_ENABLE))
1634 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
1635 HDA_AMP_MUTE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 }
Ingo Molnar62932df2006-01-16 16:34:20 +01001637 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 return change;
1639}
1640
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01001641static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 {
1643 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1644 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1645 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
1646 .info = snd_hda_spdif_mask_info,
1647 .get = snd_hda_spdif_cmask_get,
1648 },
1649 {
1650 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1651 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1652 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
1653 .info = snd_hda_spdif_mask_info,
1654 .get = snd_hda_spdif_pmask_get,
1655 },
1656 {
1657 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1658 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1659 .info = snd_hda_spdif_mask_info,
1660 .get = snd_hda_spdif_default_get,
1661 .put = snd_hda_spdif_default_put,
1662 },
1663 {
1664 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1665 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
1666 .info = snd_hda_spdif_out_switch_info,
1667 .get = snd_hda_spdif_out_switch_get,
1668 .put = snd_hda_spdif_out_switch_put,
1669 },
1670 { } /* end */
1671};
1672
Takashi Iwai09f99702008-02-04 12:31:13 +01001673#define SPDIF_MAX_IDX 4 /* 4 instances should be enough to probe */
1674
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675/**
1676 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
1677 * @codec: the HDA codec
1678 * @nid: audio out widget NID
1679 *
1680 * Creates controls related with the SPDIF output.
1681 * Called from each patch supporting the SPDIF out.
1682 *
1683 * Returns 0 if successful, or a negative error code.
1684 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02001685int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686{
1687 int err;
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01001688 struct snd_kcontrol *kctl;
1689 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01001690 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
Takashi Iwai09f99702008-02-04 12:31:13 +01001692 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
1693 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Playback Switch",
1694 idx))
1695 break;
1696 }
1697 if (idx >= SPDIF_MAX_IDX) {
1698 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
1699 return -EBUSY;
1700 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
1702 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01001703 if (!kctl)
1704 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01001705 kctl->id.index = idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 kctl->private_value = nid;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001707 err = snd_hda_ctl_add(codec, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001708 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 return err;
1710 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02001711 codec->spdif_ctls =
Andrew Paprocki3982d172007-12-19 12:13:44 +01001712 snd_hda_codec_read(codec, nid, 0,
1713 AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
1715 return 0;
1716}
1717
1718/*
Takashi Iwai9a081602008-02-12 18:37:26 +01001719 * SPDIF sharing with analog output
1720 */
1721static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
1722 struct snd_ctl_elem_value *ucontrol)
1723{
1724 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
1725 ucontrol->value.integer.value[0] = mout->share_spdif;
1726 return 0;
1727}
1728
1729static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
1730 struct snd_ctl_elem_value *ucontrol)
1731{
1732 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
1733 mout->share_spdif = !!ucontrol->value.integer.value[0];
1734 return 0;
1735}
1736
1737static struct snd_kcontrol_new spdif_share_sw = {
1738 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1739 .name = "IEC958 Default PCM Playback Switch",
1740 .info = snd_ctl_boolean_mono_info,
1741 .get = spdif_share_sw_get,
1742 .put = spdif_share_sw_put,
1743};
1744
1745int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
1746 struct hda_multi_out *mout)
1747{
1748 if (!mout->dig_out_nid)
1749 return 0;
1750 /* ATTENTION: here mout is passed as private_data, instead of codec */
Takashi Iwaid13bd412008-07-30 15:01:45 +02001751 return snd_hda_ctl_add(codec,
Takashi Iwai9a081602008-02-12 18:37:26 +01001752 snd_ctl_new1(&spdif_share_sw, mout));
1753}
1754
1755/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 * SPDIF input
1757 */
1758
1759#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
1760
Takashi Iwai0ba21762007-04-16 11:29:14 +02001761static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
1762 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763{
1764 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1765
1766 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
1767 return 0;
1768}
1769
Takashi Iwai0ba21762007-04-16 11:29:14 +02001770static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
1771 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772{
1773 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1774 hda_nid_t nid = kcontrol->private_value;
1775 unsigned int val = !!ucontrol->value.integer.value[0];
1776 int change;
1777
Ingo Molnar62932df2006-01-16 16:34:20 +01001778 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001780 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001782 snd_hda_codec_write_cache(codec, nid, 0,
1783 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 }
Ingo Molnar62932df2006-01-16 16:34:20 +01001785 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 return change;
1787}
1788
Takashi Iwai0ba21762007-04-16 11:29:14 +02001789static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
1790 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791{
1792 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1793 hda_nid_t nid = kcontrol->private_value;
1794 unsigned short val;
1795 unsigned int sbits;
1796
Andrew Paprocki3982d172007-12-19 12:13:44 +01001797 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 sbits = convert_to_spdif_status(val);
1799 ucontrol->value.iec958.status[0] = sbits;
1800 ucontrol->value.iec958.status[1] = sbits >> 8;
1801 ucontrol->value.iec958.status[2] = sbits >> 16;
1802 ucontrol->value.iec958.status[3] = sbits >> 24;
1803 return 0;
1804}
1805
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01001806static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 {
1808 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1809 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH),
1810 .info = snd_hda_spdif_in_switch_info,
1811 .get = snd_hda_spdif_in_switch_get,
1812 .put = snd_hda_spdif_in_switch_put,
1813 },
1814 {
1815 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1816 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1817 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT),
1818 .info = snd_hda_spdif_mask_info,
1819 .get = snd_hda_spdif_in_status_get,
1820 },
1821 { } /* end */
1822};
1823
1824/**
1825 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
1826 * @codec: the HDA codec
1827 * @nid: audio in widget NID
1828 *
1829 * Creates controls related with the SPDIF input.
1830 * Called from each patch supporting the SPDIF in.
1831 *
1832 * Returns 0 if successful, or a negative error code.
1833 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02001834int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835{
1836 int err;
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01001837 struct snd_kcontrol *kctl;
1838 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01001839 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
Takashi Iwai09f99702008-02-04 12:31:13 +01001841 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
1842 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Capture Switch",
1843 idx))
1844 break;
1845 }
1846 if (idx >= SPDIF_MAX_IDX) {
1847 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
1848 return -EBUSY;
1849 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
1851 kctl = snd_ctl_new1(dig_mix, codec);
1852 kctl->private_value = nid;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001853 err = snd_hda_ctl_add(codec, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001854 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 return err;
1856 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02001857 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01001858 snd_hda_codec_read(codec, nid, 0,
1859 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02001860 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 return 0;
1862}
1863
Takashi Iwaicb53c622007-08-10 17:21:45 +02001864#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001865/*
1866 * command cache
1867 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001869/* build a 32bit cache key with the widget id and the command parameter */
1870#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
1871#define get_cmd_cache_nid(key) ((key) & 0xff)
1872#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
1873
1874/**
1875 * snd_hda_codec_write_cache - send a single command with caching
1876 * @codec: the HDA codec
1877 * @nid: NID to send the command
1878 * @direct: direct flag
1879 * @verb: the verb to send
1880 * @parm: the parameter for the verb
1881 *
1882 * Send a single command without waiting for response.
1883 *
1884 * Returns 0 if successful, or a negative error code.
1885 */
1886int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
1887 int direct, unsigned int verb, unsigned int parm)
1888{
1889 int err;
Takashi Iwaicb53c622007-08-10 17:21:45 +02001890 snd_hda_power_up(codec);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001891 mutex_lock(&codec->bus->cmd_mutex);
1892 err = codec->bus->ops.command(codec, nid, direct, verb, parm);
1893 if (!err) {
1894 struct hda_cache_head *c;
1895 u32 key = build_cmd_cache_key(nid, verb);
1896 c = get_alloc_hash(&codec->cmd_cache, key);
1897 if (c)
1898 c->val = parm;
1899 }
1900 mutex_unlock(&codec->bus->cmd_mutex);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001901 snd_hda_power_down(codec);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001902 return err;
1903}
1904
1905/* resume the all commands from the cache */
1906void snd_hda_codec_resume_cache(struct hda_codec *codec)
1907{
Takashi Iwai603c4012008-07-30 15:01:44 +02001908 struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001909 int i;
1910
Takashi Iwai603c4012008-07-30 15:01:44 +02001911 for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001912 u32 key = buffer->key;
1913 if (!key)
1914 continue;
1915 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
1916 get_cmd_cache_cmd(key), buffer->val);
1917 }
1918}
1919
1920/**
1921 * snd_hda_sequence_write_cache - sequence writes with caching
1922 * @codec: the HDA codec
1923 * @seq: VERB array to send
1924 *
1925 * Send the commands sequentially from the given array.
1926 * Thte commands are recorded on cache for power-save and resume.
1927 * The array must be terminated with NID=0.
1928 */
1929void snd_hda_sequence_write_cache(struct hda_codec *codec,
1930 const struct hda_verb *seq)
1931{
1932 for (; seq->nid; seq++)
1933 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
1934 seq->param);
1935}
Takashi Iwaicb53c622007-08-10 17:21:45 +02001936#endif /* SND_HDA_NEEDS_RESUME */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001937
Takashi Iwai54d17402005-11-21 16:33:22 +01001938/*
1939 * set power state of the codec
1940 */
1941static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1942 unsigned int power_state)
1943{
Takashi Iwaicb53c622007-08-10 17:21:45 +02001944 hda_nid_t nid;
1945 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01001946
1947 snd_hda_codec_write(codec, fg, 0, AC_VERB_SET_POWER_STATE,
1948 power_state);
Marc Boucherd2595d82008-01-22 15:23:30 +01001949 msleep(10); /* partial workaround for "azx_get_response timeout" */
Takashi Iwai54d17402005-11-21 16:33:22 +01001950
Takashi Iwaicb53c622007-08-10 17:21:45 +02001951 nid = codec->start_nid;
1952 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01001953 unsigned int wcaps = get_wcaps(codec, nid);
1954 if (wcaps & AC_WCAP_POWER) {
1955 unsigned int wid_type = (wcaps & AC_WCAP_TYPE) >>
1956 AC_WCAP_TYPE_SHIFT;
1957 if (wid_type == AC_WID_PIN) {
1958 unsigned int pincap;
1959 /*
1960 * don't power down the widget if it controls
1961 * eapd and EAPD_BTLENABLE is set.
1962 */
1963 pincap = snd_hda_param_read(codec, nid,
1964 AC_PAR_PIN_CAP);
1965 if (pincap & AC_PINCAP_EAPD) {
1966 int eapd = snd_hda_codec_read(codec,
1967 nid, 0,
1968 AC_VERB_GET_EAPD_BTLENABLE, 0);
1969 eapd &= 0x02;
1970 if (power_state == AC_PWRST_D3 && eapd)
1971 continue;
1972 }
Takashi Iwai1194b5b2007-10-10 10:04:26 +02001973 }
Takashi Iwai54d17402005-11-21 16:33:22 +01001974 snd_hda_codec_write(codec, nid, 0,
1975 AC_VERB_SET_POWER_STATE,
1976 power_state);
Takashi Iwai1194b5b2007-10-10 10:04:26 +02001977 }
Takashi Iwai54d17402005-11-21 16:33:22 +01001978 }
1979
Takashi Iwaicb53c622007-08-10 17:21:45 +02001980 if (power_state == AC_PWRST_D0) {
1981 unsigned long end_time;
1982 int state;
Takashi Iwai54d17402005-11-21 16:33:22 +01001983 msleep(10);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001984 /* wait until the codec reachs to D0 */
1985 end_time = jiffies + msecs_to_jiffies(500);
1986 do {
1987 state = snd_hda_codec_read(codec, fg, 0,
1988 AC_VERB_GET_POWER_STATE, 0);
1989 if (state == power_state)
1990 break;
1991 msleep(1);
1992 } while (time_after_eq(end_time, jiffies));
1993 }
Takashi Iwai54d17402005-11-21 16:33:22 +01001994}
1995
Takashi Iwai11aeff02008-07-30 15:01:46 +02001996#ifdef CONFIG_SND_HDA_HWDEP
1997/* execute additional init verbs */
1998static void hda_exec_init_verbs(struct hda_codec *codec)
1999{
2000 if (codec->init_verbs.list)
2001 snd_hda_sequence_write(codec, codec->init_verbs.list);
2002}
2003#else
2004static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
2005#endif
2006
Takashi Iwaicb53c622007-08-10 17:21:45 +02002007#ifdef SND_HDA_NEEDS_RESUME
2008/*
2009 * call suspend and power-down; used both from PM and power-save
2010 */
2011static void hda_call_codec_suspend(struct hda_codec *codec)
2012{
2013 if (codec->patch_ops.suspend)
2014 codec->patch_ops.suspend(codec, PMSG_SUSPEND);
2015 hda_set_power_state(codec,
2016 codec->afg ? codec->afg : codec->mfg,
2017 AC_PWRST_D3);
2018#ifdef CONFIG_SND_HDA_POWER_SAVE
2019 cancel_delayed_work(&codec->power_work);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02002020 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02002021 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002022#endif
2023}
2024
2025/*
2026 * kick up codec; used both from PM and power-save
2027 */
2028static void hda_call_codec_resume(struct hda_codec *codec)
2029{
2030 hda_set_power_state(codec,
2031 codec->afg ? codec->afg : codec->mfg,
2032 AC_PWRST_D0);
Takashi Iwai11aeff02008-07-30 15:01:46 +02002033 hda_exec_init_verbs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002034 if (codec->patch_ops.resume)
2035 codec->patch_ops.resume(codec);
2036 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02002037 if (codec->patch_ops.init)
2038 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002039 snd_hda_codec_resume_amp(codec);
2040 snd_hda_codec_resume_cache(codec);
2041 }
2042}
2043#endif /* SND_HDA_NEEDS_RESUME */
2044
Takashi Iwai54d17402005-11-21 16:33:22 +01002045
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046/**
2047 * snd_hda_build_controls - build mixer controls
2048 * @bus: the BUS
2049 *
2050 * Creates mixer controls for each codec included in the bus.
2051 *
2052 * Returns 0 if successful, otherwise a negative error code.
2053 */
Takashi Iwai756e2b02007-04-16 11:27:07 +02002054int __devinit snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055{
Takashi Iwai0ba21762007-04-16 11:29:14 +02002056 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057
Takashi Iwai0ba21762007-04-16 11:29:14 +02002058 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002059 int err = snd_hda_codec_build_controls(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 if (err < 0)
2061 return err;
2062 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002063 return 0;
2064}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002066int snd_hda_codec_build_controls(struct hda_codec *codec)
2067{
2068 int err = 0;
2069 /* fake as if already powered-on */
2070 hda_keep_power_on(codec);
2071 /* then fire up */
2072 hda_set_power_state(codec,
2073 codec->afg ? codec->afg : codec->mfg,
2074 AC_PWRST_D0);
Takashi Iwai11aeff02008-07-30 15:01:46 +02002075 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002076 /* continue to initialize... */
2077 if (codec->patch_ops.init)
2078 err = codec->patch_ops.init(codec);
2079 if (!err && codec->patch_ops.build_controls)
2080 err = codec->patch_ops.build_controls(codec);
2081 snd_hda_power_down(codec);
2082 if (err < 0)
2083 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 return 0;
2085}
2086
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087/*
2088 * stream formats
2089 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02002090struct hda_rate_tbl {
2091 unsigned int hz;
2092 unsigned int alsa_bits;
2093 unsigned int hda_fmt;
2094};
2095
2096static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02002098
2099 /* autodetected value used in snd_hda_query_supported_pcm */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 { 8000, SNDRV_PCM_RATE_8000, 0x0500 }, /* 1/6 x 48 */
2101 { 11025, SNDRV_PCM_RATE_11025, 0x4300 }, /* 1/4 x 44 */
2102 { 16000, SNDRV_PCM_RATE_16000, 0x0200 }, /* 1/3 x 48 */
2103 { 22050, SNDRV_PCM_RATE_22050, 0x4100 }, /* 1/2 x 44 */
2104 { 32000, SNDRV_PCM_RATE_32000, 0x0a00 }, /* 2/3 x 48 */
2105 { 44100, SNDRV_PCM_RATE_44100, 0x4000 }, /* 44 */
2106 { 48000, SNDRV_PCM_RATE_48000, 0x0000 }, /* 48 */
2107 { 88200, SNDRV_PCM_RATE_88200, 0x4800 }, /* 2 x 44 */
2108 { 96000, SNDRV_PCM_RATE_96000, 0x0800 }, /* 2 x 48 */
2109 { 176400, SNDRV_PCM_RATE_176400, 0x5800 },/* 4 x 44 */
2110 { 192000, SNDRV_PCM_RATE_192000, 0x1800 }, /* 4 x 48 */
Takashi Iwaia961f9f2007-04-12 13:08:09 +02002111#define AC_PAR_PCM_RATE_BITS 11
2112 /* up to bits 10, 384kHZ isn't supported properly */
2113
2114 /* not autodetected value */
2115 { 9600, SNDRV_PCM_RATE_KNOT, 0x0400 }, /* 1/5 x 48 */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02002116
Takashi Iwaibefdf312005-08-22 13:57:55 +02002117 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118};
2119
2120/**
2121 * snd_hda_calc_stream_format - calculate format bitset
2122 * @rate: the sample rate
2123 * @channels: the number of channels
2124 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
2125 * @maxbps: the max. bps
2126 *
2127 * Calculate the format bitset from the given rate, channels and th PCM format.
2128 *
2129 * Return zero if invalid.
2130 */
2131unsigned int snd_hda_calc_stream_format(unsigned int rate,
2132 unsigned int channels,
2133 unsigned int format,
2134 unsigned int maxbps)
2135{
2136 int i;
2137 unsigned int val = 0;
2138
Takashi Iwaibefdf312005-08-22 13:57:55 +02002139 for (i = 0; rate_bits[i].hz; i++)
2140 if (rate_bits[i].hz == rate) {
2141 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 break;
2143 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002144 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 snd_printdd("invalid rate %d\n", rate);
2146 return 0;
2147 }
2148
2149 if (channels == 0 || channels > 8) {
2150 snd_printdd("invalid channels %d\n", channels);
2151 return 0;
2152 }
2153 val |= channels - 1;
2154
2155 switch (snd_pcm_format_width(format)) {
2156 case 8: val |= 0x00; break;
2157 case 16: val |= 0x10; break;
2158 case 20:
2159 case 24:
2160 case 32:
2161 if (maxbps >= 32)
2162 val |= 0x40;
2163 else if (maxbps >= 24)
2164 val |= 0x30;
2165 else
2166 val |= 0x20;
2167 break;
2168 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002169 snd_printdd("invalid format width %d\n",
2170 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 return 0;
2172 }
2173
2174 return val;
2175}
2176
2177/**
2178 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
2179 * @codec: the HDA codec
2180 * @nid: NID to query
2181 * @ratesp: the pointer to store the detected rate bitflags
2182 * @formatsp: the pointer to store the detected formats
2183 * @bpsp: the pointer to store the detected format widths
2184 *
2185 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
2186 * or @bsps argument is ignored.
2187 *
2188 * Returns 0 if successful, otherwise a negative error code.
2189 */
2190int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
2191 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
2192{
2193 int i;
2194 unsigned int val, streams;
2195
2196 val = 0;
2197 if (nid != codec->afg &&
Takashi Iwai54d17402005-11-21 16:33:22 +01002198 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
2200 if (val == -1)
2201 return -EIO;
2202 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002203 if (!val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
2205
2206 if (ratesp) {
2207 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02002208 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02002210 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 }
2212 *ratesp = rates;
2213 }
2214
2215 if (formatsp || bpsp) {
2216 u64 formats = 0;
2217 unsigned int bps;
2218 unsigned int wcaps;
2219
Takashi Iwai54d17402005-11-21 16:33:22 +01002220 wcaps = get_wcaps(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
2222 if (streams == -1)
2223 return -EIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002224 if (!streams) {
2225 streams = snd_hda_param_read(codec, codec->afg,
2226 AC_PAR_STREAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 if (streams == -1)
2228 return -EIO;
2229 }
2230
2231 bps = 0;
2232 if (streams & AC_SUPFMT_PCM) {
2233 if (val & AC_SUPPCM_BITS_8) {
2234 formats |= SNDRV_PCM_FMTBIT_U8;
2235 bps = 8;
2236 }
2237 if (val & AC_SUPPCM_BITS_16) {
2238 formats |= SNDRV_PCM_FMTBIT_S16_LE;
2239 bps = 16;
2240 }
2241 if (wcaps & AC_WCAP_DIGITAL) {
2242 if (val & AC_SUPPCM_BITS_32)
2243 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
2244 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
2245 formats |= SNDRV_PCM_FMTBIT_S32_LE;
2246 if (val & AC_SUPPCM_BITS_24)
2247 bps = 24;
2248 else if (val & AC_SUPPCM_BITS_20)
2249 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002250 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
2251 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 formats |= SNDRV_PCM_FMTBIT_S32_LE;
2253 if (val & AC_SUPPCM_BITS_32)
2254 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 else if (val & AC_SUPPCM_BITS_24)
2256 bps = 24;
Nicolas Graziano33ef7652006-09-19 14:23:14 +02002257 else if (val & AC_SUPPCM_BITS_20)
2258 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 }
2260 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002261 else if (streams == AC_SUPFMT_FLOAT32) {
2262 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
2264 bps = 32;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002265 } else if (streams == AC_SUPFMT_AC3) {
2266 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 /* temporary hack: we have still no proper support
2268 * for the direct AC3 stream...
2269 */
2270 formats |= SNDRV_PCM_FMTBIT_U8;
2271 bps = 8;
2272 }
2273 if (formatsp)
2274 *formatsp = formats;
2275 if (bpsp)
2276 *bpsp = bps;
2277 }
2278
2279 return 0;
2280}
2281
2282/**
Takashi Iwai0ba21762007-04-16 11:29:14 +02002283 * snd_hda_is_supported_format - check whether the given node supports
2284 * the format val
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 *
2286 * Returns 1 if supported, 0 if not.
2287 */
2288int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
2289 unsigned int format)
2290{
2291 int i;
2292 unsigned int val = 0, rate, stream;
2293
2294 if (nid != codec->afg &&
Takashi Iwai54d17402005-11-21 16:33:22 +01002295 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
2297 if (val == -1)
2298 return 0;
2299 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002300 if (!val) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
2302 if (val == -1)
2303 return 0;
2304 }
2305
2306 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02002307 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02002308 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 if (val & (1 << i))
2310 break;
2311 return 0;
2312 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02002313 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 return 0;
2315
2316 stream = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
2317 if (stream == -1)
2318 return 0;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002319 if (!stream && nid != codec->afg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 stream = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002321 if (!stream || stream == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 return 0;
2323
2324 if (stream & AC_SUPFMT_PCM) {
2325 switch (format & 0xf0) {
2326 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002327 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 return 0;
2329 break;
2330 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002331 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 return 0;
2333 break;
2334 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002335 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 return 0;
2337 break;
2338 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002339 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 return 0;
2341 break;
2342 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002343 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 return 0;
2345 break;
2346 default:
2347 return 0;
2348 }
2349 } else {
2350 /* FIXME: check for float32 and AC3? */
2351 }
2352
2353 return 1;
2354}
2355
2356/*
2357 * PCM stuff
2358 */
2359static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
2360 struct hda_codec *codec,
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01002361 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362{
2363 return 0;
2364}
2365
2366static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
2367 struct hda_codec *codec,
2368 unsigned int stream_tag,
2369 unsigned int format,
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01002370 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371{
2372 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
2373 return 0;
2374}
2375
2376static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
2377 struct hda_codec *codec,
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01002378 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379{
Takashi Iwai888afa12008-03-18 09:57:50 +01002380 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 return 0;
2382}
2383
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002384static int set_pcm_default_values(struct hda_codec *codec,
2385 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386{
Takashi Iwai0ba21762007-04-16 11:29:14 +02002387 /* query support PCM information from the given NID */
2388 if (info->nid && (!info->rates || !info->formats)) {
2389 snd_hda_query_supported_pcm(codec, info->nid,
2390 info->rates ? NULL : &info->rates,
2391 info->formats ? NULL : &info->formats,
2392 info->maxbps ? NULL : &info->maxbps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 }
2394 if (info->ops.open == NULL)
2395 info->ops.open = hda_pcm_default_open_close;
2396 if (info->ops.close == NULL)
2397 info->ops.close = hda_pcm_default_open_close;
2398 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02002399 if (snd_BUG_ON(!info->nid))
2400 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 info->ops.prepare = hda_pcm_default_prepare;
2402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02002404 if (snd_BUG_ON(!info->nid))
2405 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 info->ops.cleanup = hda_pcm_default_cleanup;
2407 }
2408 return 0;
2409}
2410
Takashi Iwai176d5332008-07-30 15:01:44 +02002411/*
2412 * attach a new PCM stream
2413 */
2414static int __devinit
2415snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
2416{
2417 struct hda_pcm_stream *info;
2418 int stream, err;
2419
Takashi Iwaib91f0802008-11-04 08:43:08 +01002420 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02002421 return -EINVAL;
2422 for (stream = 0; stream < 2; stream++) {
2423 info = &pcm->stream[stream];
2424 if (info->substreams) {
2425 err = set_pcm_default_values(codec, info);
2426 if (err < 0)
2427 return err;
2428 }
2429 }
2430 return codec->bus->ops.attach_pcm(codec, pcm);
2431}
2432
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433/**
2434 * snd_hda_build_pcms - build PCM information
2435 * @bus: the BUS
2436 *
2437 * Create PCM information for each codec included in the bus.
2438 *
2439 * The build_pcms codec patch is requested to set up codec->num_pcms and
2440 * codec->pcm_info properly. The array is referred by the top-level driver
2441 * to create its PCM instances.
2442 * The allocated codec->pcm_info should be released in codec->patch_ops.free
2443 * callback.
2444 *
2445 * At least, substreams, channels_min and channels_max must be filled for
2446 * each stream. substreams = 0 indicates that the stream doesn't exist.
2447 * When rates and/or formats are zero, the supported values are queried
2448 * from the given nid. The nid is used also by the default ops.prepare
2449 * and ops.cleanup callbacks.
2450 *
2451 * The driver needs to call ops.open in its open callback. Similarly,
2452 * ops.close is supposed to be called in the close callback.
2453 * ops.prepare should be called in the prepare or hw_params callback
2454 * with the proper parameters for set up.
2455 * ops.cleanup should be called in hw_free for clean up of streams.
2456 *
2457 * This function returns 0 if successfull, or a negative error code.
2458 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002459int snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460{
Takashi Iwai176d5332008-07-30 15:01:44 +02002461 static const char *dev_name[HDA_PCM_NTYPES] = {
2462 "Audio", "SPDIF", "HDMI", "Modem"
2463 };
2464 /* starting device index for each PCM type */
2465 static int dev_idx[HDA_PCM_NTYPES] = {
2466 [HDA_PCM_TYPE_AUDIO] = 0,
2467 [HDA_PCM_TYPE_SPDIF] = 1,
2468 [HDA_PCM_TYPE_HDMI] = 3,
2469 [HDA_PCM_TYPE_MODEM] = 6
2470 };
2471 /* normal audio device indices; not linear to keep compatibility */
2472 static int audio_idx[4] = { 0, 2, 4, 5 };
Takashi Iwai0ba21762007-04-16 11:29:14 +02002473 struct hda_codec *codec;
Takashi Iwai176d5332008-07-30 15:01:44 +02002474 int num_devs[HDA_PCM_NTYPES];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475
Takashi Iwai176d5332008-07-30 15:01:44 +02002476 memset(num_devs, 0, sizeof(num_devs));
Takashi Iwai0ba21762007-04-16 11:29:14 +02002477 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai176d5332008-07-30 15:01:44 +02002478 unsigned int pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 int err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002480 if (!codec->num_pcms) {
2481 if (!codec->patch_ops.build_pcms)
2482 continue;
2483 err = codec->patch_ops.build_pcms(codec);
2484 if (err < 0)
2485 return err;
2486 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
Takashi Iwai176d5332008-07-30 15:01:44 +02002488 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2489 int type = cpcm->pcm_type;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002490 int dev;
Takashi Iwaibfc5c262008-11-04 17:48:39 +01002491
2492 if (!cpcm->stream[0].substreams &&
2493 !cpcm->stream[1].substreams)
2494 continue; /* no substreams assigned */
2495
Takashi Iwai176d5332008-07-30 15:01:44 +02002496 switch (type) {
2497 case HDA_PCM_TYPE_AUDIO:
2498 if (num_devs[type] >= ARRAY_SIZE(audio_idx)) {
2499 snd_printk(KERN_WARNING
2500 "Too many audio devices\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 continue;
Takashi Iwai176d5332008-07-30 15:01:44 +02002502 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002503 dev = audio_idx[num_devs[type]];
Takashi Iwai176d5332008-07-30 15:01:44 +02002504 break;
2505 case HDA_PCM_TYPE_SPDIF:
2506 case HDA_PCM_TYPE_HDMI:
2507 case HDA_PCM_TYPE_MODEM:
2508 if (num_devs[type]) {
2509 snd_printk(KERN_WARNING
2510 "%s already defined\n",
2511 dev_name[type]);
2512 continue;
2513 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002514 dev = dev_idx[type];
Takashi Iwai176d5332008-07-30 15:01:44 +02002515 break;
2516 default:
2517 snd_printk(KERN_WARNING
2518 "Invalid PCM type %d\n", type);
2519 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 }
Takashi Iwai176d5332008-07-30 15:01:44 +02002521 num_devs[type]++;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002522 if (!cpcm->pcm) {
2523 cpcm->device = dev;
2524 err = snd_hda_attach_pcm(codec, cpcm);
2525 if (err < 0)
2526 return err;
2527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 }
2529 }
2530 return 0;
2531}
2532
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533/**
2534 * snd_hda_check_board_config - compare the current codec with the config table
2535 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002536 * @num_configs: number of config enums
2537 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 * @tbl: configuration table, terminated by null entries
2539 *
2540 * Compares the modelname or PCI subsystem id of the current codec with the
2541 * given configuration table. If a matching entry is found, returns its
2542 * config value (supposed to be 0 or positive).
2543 *
2544 * If no entries are matching, the function returns a negative value.
2545 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002546int snd_hda_check_board_config(struct hda_codec *codec,
2547 int num_configs, const char **models,
2548 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549{
Takashi Iwaif44ac832008-07-30 15:01:45 +02002550 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002551 int i;
2552 for (i = 0; i < num_configs; i++) {
2553 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02002554 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002555 snd_printd(KERN_INFO "hda_codec: model '%s' is "
2556 "selected\n", models[i]);
2557 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 }
2559 }
2560 }
2561
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002562 if (!codec->bus->pci || !tbl)
2563 return -1;
2564
2565 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
2566 if (!tbl)
2567 return -1;
2568 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02002569#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002570 char tmp[10];
2571 const char *model = NULL;
2572 if (models)
2573 model = models[tbl->value];
2574 if (!model) {
2575 sprintf(tmp, "#%d", tbl->value);
2576 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002578 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
2579 "for config %x:%x (%s)\n",
2580 model, tbl->subvendor, tbl->subdevice,
2581 (tbl->name ? tbl->name : "Unknown device"));
2582#endif
2583 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 }
2585 return -1;
2586}
2587
2588/**
2589 * snd_hda_add_new_ctls - create controls from the array
2590 * @codec: the HDA codec
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01002591 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 *
2593 * This helper function creates and add new controls in the given array.
2594 * The array must be terminated with an empty entry as terminator.
2595 *
2596 * Returns 0 if successful, or a negative error code.
2597 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002598int snd_hda_add_new_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599{
Takashi Iwaicb53c622007-08-10 17:21:45 +02002600 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601
2602 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01002603 struct snd_kcontrol *kctl;
2604 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002605 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01002606 return -ENOMEM;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002607 err = snd_hda_ctl_add(codec, kctl);
Takashi Iwai54d17402005-11-21 16:33:22 +01002608 if (err < 0) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002609 if (!codec->addr)
Takashi Iwai54d17402005-11-21 16:33:22 +01002610 return err;
2611 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002612 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01002613 return -ENOMEM;
2614 kctl->id.device = codec->addr;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002615 err = snd_hda_ctl_add(codec, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002616 if (err < 0)
Takashi Iwai54d17402005-11-21 16:33:22 +01002617 return err;
2618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 }
2620 return 0;
2621}
2622
Takashi Iwaicb53c622007-08-10 17:21:45 +02002623#ifdef CONFIG_SND_HDA_POWER_SAVE
2624static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2625 unsigned int power_state);
2626
2627static void hda_power_work(struct work_struct *work)
2628{
2629 struct hda_codec *codec =
2630 container_of(work, struct hda_codec, power_work.work);
2631
Maxim Levitsky2e492462007-09-03 15:26:57 +02002632 if (!codec->power_on || codec->power_count) {
2633 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002634 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02002635 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02002636
2637 hda_call_codec_suspend(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002638 if (codec->bus->ops.pm_notify)
2639 codec->bus->ops.pm_notify(codec);
2640}
2641
2642static void hda_keep_power_on(struct hda_codec *codec)
2643{
2644 codec->power_count++;
2645 codec->power_on = 1;
2646}
2647
2648void snd_hda_power_up(struct hda_codec *codec)
2649{
2650 codec->power_count++;
Takashi Iwaia221e282007-08-16 16:35:33 +02002651 if (codec->power_on || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02002652 return;
2653
2654 codec->power_on = 1;
2655 if (codec->bus->ops.pm_notify)
2656 codec->bus->ops.pm_notify(codec);
2657 hda_call_codec_resume(codec);
2658 cancel_delayed_work(&codec->power_work);
Takashi Iwaia221e282007-08-16 16:35:33 +02002659 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002660}
2661
2662void snd_hda_power_down(struct hda_codec *codec)
2663{
2664 --codec->power_count;
Takashi Iwaia221e282007-08-16 16:35:33 +02002665 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02002666 return;
Takashi Iwaia221e282007-08-16 16:35:33 +02002667 if (power_save) {
2668 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002669 schedule_delayed_work(&codec->power_work,
2670 msecs_to_jiffies(power_save * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02002671 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02002672}
2673
2674int snd_hda_check_amp_list_power(struct hda_codec *codec,
2675 struct hda_loopback_check *check,
2676 hda_nid_t nid)
2677{
2678 struct hda_amp_list *p;
2679 int ch, v;
2680
2681 if (!check->amplist)
2682 return 0;
2683 for (p = check->amplist; p->nid; p++) {
2684 if (p->nid == nid)
2685 break;
2686 }
2687 if (!p->nid)
2688 return 0; /* nothing changed */
2689
2690 for (p = check->amplist; p->nid; p++) {
2691 for (ch = 0; ch < 2; ch++) {
2692 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
2693 p->idx);
2694 if (!(v & HDA_AMP_MUTE) && v > 0) {
2695 if (!check->power_on) {
2696 check->power_on = 1;
2697 snd_hda_power_up(codec);
2698 }
2699 return 1;
2700 }
2701 }
2702 }
2703 if (check->power_on) {
2704 check->power_on = 0;
2705 snd_hda_power_down(codec);
2706 }
2707 return 0;
2708}
2709#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01002711/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002712 * Channel mode helper
2713 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002714int snd_hda_ch_mode_info(struct hda_codec *codec,
2715 struct snd_ctl_elem_info *uinfo,
2716 const struct hda_channel_mode *chmode,
2717 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002718{
2719 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2720 uinfo->count = 1;
2721 uinfo->value.enumerated.items = num_chmodes;
2722 if (uinfo->value.enumerated.item >= num_chmodes)
2723 uinfo->value.enumerated.item = num_chmodes - 1;
2724 sprintf(uinfo->value.enumerated.name, "%dch",
2725 chmode[uinfo->value.enumerated.item].channels);
2726 return 0;
2727}
2728
Takashi Iwai0ba21762007-04-16 11:29:14 +02002729int snd_hda_ch_mode_get(struct hda_codec *codec,
2730 struct snd_ctl_elem_value *ucontrol,
2731 const struct hda_channel_mode *chmode,
2732 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002733 int max_channels)
2734{
2735 int i;
2736
2737 for (i = 0; i < num_chmodes; i++) {
2738 if (max_channels == chmode[i].channels) {
2739 ucontrol->value.enumerated.item[0] = i;
2740 break;
2741 }
2742 }
2743 return 0;
2744}
2745
Takashi Iwai0ba21762007-04-16 11:29:14 +02002746int snd_hda_ch_mode_put(struct hda_codec *codec,
2747 struct snd_ctl_elem_value *ucontrol,
2748 const struct hda_channel_mode *chmode,
2749 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002750 int *max_channelsp)
2751{
2752 unsigned int mode;
2753
2754 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01002755 if (mode >= num_chmodes)
2756 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002757 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002758 return 0;
2759 /* change the current channel setting */
2760 *max_channelsp = chmode[mode].channels;
2761 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002762 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002763 return 1;
2764}
2765
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766/*
2767 * input MUX helper
2768 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002769int snd_hda_input_mux_info(const struct hda_input_mux *imux,
2770 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771{
2772 unsigned int index;
2773
2774 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2775 uinfo->count = 1;
2776 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02002777 if (!imux->num_items)
2778 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 index = uinfo->value.enumerated.item;
2780 if (index >= imux->num_items)
2781 index = imux->num_items - 1;
2782 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
2783 return 0;
2784}
2785
Takashi Iwai0ba21762007-04-16 11:29:14 +02002786int snd_hda_input_mux_put(struct hda_codec *codec,
2787 const struct hda_input_mux *imux,
2788 struct snd_ctl_elem_value *ucontrol,
2789 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 unsigned int *cur_val)
2791{
2792 unsigned int idx;
2793
Takashi Iwai5513b0c2007-10-09 11:58:41 +02002794 if (!imux->num_items)
2795 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 idx = ucontrol->value.enumerated.item[0];
2797 if (idx >= imux->num_items)
2798 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002799 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002801 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
2802 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 *cur_val = idx;
2804 return 1;
2805}
2806
2807
2808/*
2809 * Multi-channel / digital-out PCM helper functions
2810 */
2811
Takashi Iwai6b97eb42007-04-05 14:51:48 +02002812/* setup SPDIF output stream */
2813static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
2814 unsigned int stream_tag, unsigned int format)
2815{
2816 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Takashi Iwai2f728532008-09-25 16:32:41 +02002817 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
2818 set_dig_out_convert(codec, nid,
2819 codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff,
2820 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02002821 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02002822 if (codec->slave_dig_outs) {
2823 hda_nid_t *d;
2824 for (d = codec->slave_dig_outs; *d; d++)
2825 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
2826 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04002827 }
Takashi Iwai2f728532008-09-25 16:32:41 +02002828 /* turn on again (if needed) */
2829 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
2830 set_dig_out_convert(codec, nid,
2831 codec->spdif_ctls & 0xff, -1);
2832}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04002833
Takashi Iwai2f728532008-09-25 16:32:41 +02002834static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
2835{
2836 snd_hda_codec_cleanup_stream(codec, nid);
2837 if (codec->slave_dig_outs) {
2838 hda_nid_t *d;
2839 for (d = codec->slave_dig_outs; *d; d++)
2840 snd_hda_codec_cleanup_stream(codec, *d);
2841 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02002842}
2843
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844/*
2845 * open the digital out in the exclusive mode
2846 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002847int snd_hda_multi_out_dig_open(struct hda_codec *codec,
2848 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849{
Ingo Molnar62932df2006-01-16 16:34:20 +01002850 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02002851 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
2852 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02002853 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01002855 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 return 0;
2857}
2858
Takashi Iwai6b97eb42007-04-05 14:51:48 +02002859int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
2860 struct hda_multi_out *mout,
2861 unsigned int stream_tag,
2862 unsigned int format,
2863 struct snd_pcm_substream *substream)
2864{
2865 mutex_lock(&codec->spdif_mutex);
2866 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
2867 mutex_unlock(&codec->spdif_mutex);
2868 return 0;
2869}
2870
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871/*
2872 * release the digital out
2873 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002874int snd_hda_multi_out_dig_close(struct hda_codec *codec,
2875 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876{
Ingo Molnar62932df2006-01-16 16:34:20 +01002877 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01002879 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 return 0;
2881}
2882
2883/*
2884 * set up more restrictions for analog out
2885 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002886int snd_hda_multi_out_analog_open(struct hda_codec *codec,
2887 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01002888 struct snd_pcm_substream *substream,
2889 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890{
Takashi Iwai9a081602008-02-12 18:37:26 +01002891 struct snd_pcm_runtime *runtime = substream->runtime;
2892 runtime->hw.channels_max = mout->max_channels;
2893 if (mout->dig_out_nid) {
2894 if (!mout->analog_rates) {
2895 mout->analog_rates = hinfo->rates;
2896 mout->analog_formats = hinfo->formats;
2897 mout->analog_maxbps = hinfo->maxbps;
2898 } else {
2899 runtime->hw.rates = mout->analog_rates;
2900 runtime->hw.formats = mout->analog_formats;
2901 hinfo->maxbps = mout->analog_maxbps;
2902 }
2903 if (!mout->spdif_rates) {
2904 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
2905 &mout->spdif_rates,
2906 &mout->spdif_formats,
2907 &mout->spdif_maxbps);
2908 }
2909 mutex_lock(&codec->spdif_mutex);
2910 if (mout->share_spdif) {
2911 runtime->hw.rates &= mout->spdif_rates;
2912 runtime->hw.formats &= mout->spdif_formats;
2913 if (mout->spdif_maxbps < hinfo->maxbps)
2914 hinfo->maxbps = mout->spdif_maxbps;
2915 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02002916 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01002917 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 return snd_pcm_hw_constraint_step(substream->runtime, 0,
2919 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
2920}
2921
2922/*
2923 * set up the i/o for analog out
2924 * when the digital out is available, copy the front out to digital out, too.
2925 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002926int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
2927 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 unsigned int stream_tag,
2929 unsigned int format,
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01002930 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931{
2932 hda_nid_t *nids = mout->dac_nids;
2933 int chs = substream->runtime->channels;
2934 int i;
2935
Ingo Molnar62932df2006-01-16 16:34:20 +01002936 mutex_lock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01002937 if (mout->dig_out_nid && mout->share_spdif &&
2938 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02002940 snd_hda_is_supported_format(codec, mout->dig_out_nid,
2941 format) &&
2942 !(codec->spdif_status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02002944 setup_dig_out_stream(codec, mout->dig_out_nid,
2945 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 } else {
2947 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02002948 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 }
2950 }
Ingo Molnar62932df2006-01-16 16:34:20 +01002951 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952
2953 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002954 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
2955 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02002956 if (!mout->no_share_stream &&
2957 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002959 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
2960 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01002961 /* extra outputs copied from front */
2962 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02002963 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01002964 snd_hda_codec_setup_stream(codec,
2965 mout->extra_out_nid[i],
2966 stream_tag, 0, format);
2967
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 /* surrounds */
2969 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02002970 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002971 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
2972 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02002973 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002974 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
2975 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 }
2977 return 0;
2978}
2979
2980/*
2981 * clean up the setting for analog out
2982 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002983int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
2984 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985{
2986 hda_nid_t *nids = mout->dac_nids;
2987 int i;
2988
2989 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01002990 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01002992 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwai82bc9552006-03-21 11:24:42 +01002993 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
2994 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01002995 snd_hda_codec_cleanup_stream(codec,
2996 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01002997 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02002999 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 mout->dig_out_used = 0;
3001 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003002 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 return 0;
3004}
3005
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003006/*
Wu Fengguang6b345002008-10-07 14:21:41 +08003007 * Helper for automatic pin configuration
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003008 */
Kailang Yangdf694da2005-12-05 19:42:22 +01003009
Takashi Iwai12f288b2007-08-02 15:51:59 +02003010static int is_in_nid_list(hda_nid_t nid, hda_nid_t *list)
Kailang Yangdf694da2005-12-05 19:42:22 +01003011{
3012 for (; *list; list++)
3013 if (*list == nid)
3014 return 1;
3015 return 0;
3016}
3017
Steve Longerbeam81937d32007-05-08 15:33:03 +02003018
3019/*
3020 * Sort an associated group of pins according to their sequence numbers.
3021 */
3022static void sort_pins_by_sequence(hda_nid_t * pins, short * sequences,
3023 int num_pins)
3024{
3025 int i, j;
3026 short seq;
3027 hda_nid_t nid;
3028
3029 for (i = 0; i < num_pins; i++) {
3030 for (j = i + 1; j < num_pins; j++) {
3031 if (sequences[i] > sequences[j]) {
3032 seq = sequences[i];
3033 sequences[i] = sequences[j];
3034 sequences[j] = seq;
3035 nid = pins[i];
3036 pins[i] = pins[j];
3037 pins[j] = nid;
3038 }
3039 }
3040 }
3041}
3042
3043
Takashi Iwai82bc9552006-03-21 11:24:42 +01003044/*
3045 * Parse all pin widgets and store the useful pin nids to cfg
3046 *
3047 * The number of line-outs or any primary output is stored in line_outs,
3048 * and the corresponding output pins are assigned to line_out_pins[],
3049 * in the order of front, rear, CLFE, side, ...
3050 *
3051 * If more extra outputs (speaker and headphone) are found, the pins are
Takashi Iwaieb06ed82006-09-20 17:10:27 +02003052 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
Takashi Iwai82bc9552006-03-21 11:24:42 +01003053 * is detected, one of speaker of HP pins is assigned as the primary
3054 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
3055 * if any analog output exists.
3056 *
3057 * The analog input pins are assigned to input_pins array.
3058 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
3059 * respectively.
3060 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003061int snd_hda_parse_pin_def_config(struct hda_codec *codec,
3062 struct auto_pin_cfg *cfg,
3063 hda_nid_t *ignore_nids)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003064{
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01003065 hda_nid_t nid, end_nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02003066 short seq, assoc_line_out, assoc_speaker;
3067 short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
3068 short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
Takashi Iwaif889fa92007-10-31 15:49:32 +01003069 short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003070
3071 memset(cfg, 0, sizeof(*cfg));
3072
Steve Longerbeam81937d32007-05-08 15:33:03 +02003073 memset(sequences_line_out, 0, sizeof(sequences_line_out));
3074 memset(sequences_speaker, 0, sizeof(sequences_speaker));
Takashi Iwaif889fa92007-10-31 15:49:32 +01003075 memset(sequences_hp, 0, sizeof(sequences_hp));
Steve Longerbeam81937d32007-05-08 15:33:03 +02003076 assoc_line_out = assoc_speaker = 0;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003077
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01003078 end_nid = codec->start_nid + codec->num_nodes;
3079 for (nid = codec->start_nid; nid < end_nid; nid++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01003080 unsigned int wid_caps = get_wcaps(codec, nid);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003081 unsigned int wid_type =
3082 (wid_caps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003083 unsigned int def_conf;
3084 short assoc, loc;
3085
3086 /* read all default configuration for pin complex */
3087 if (wid_type != AC_WID_PIN)
3088 continue;
Kailang Yangdf694da2005-12-05 19:42:22 +01003089 /* ignore the given nids (e.g. pc-beep returns error) */
3090 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
3091 continue;
3092
Takashi Iwai0ba21762007-04-16 11:29:14 +02003093 def_conf = snd_hda_codec_read(codec, nid, 0,
3094 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003095 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
3096 continue;
3097 loc = get_defcfg_location(def_conf);
3098 switch (get_defcfg_device(def_conf)) {
3099 case AC_JACK_LINE_OUT:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003100 seq = get_defcfg_sequence(def_conf);
3101 assoc = get_defcfg_association(def_conf);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01003102
3103 if (!(wid_caps & AC_WCAP_STEREO))
3104 if (!cfg->mono_out_pin)
3105 cfg->mono_out_pin = nid;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003106 if (!assoc)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003107 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003108 if (!assoc_line_out)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003109 assoc_line_out = assoc;
3110 else if (assoc_line_out != assoc)
3111 continue;
3112 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
3113 continue;
3114 cfg->line_out_pins[cfg->line_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02003115 sequences_line_out[cfg->line_outs] = seq;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003116 cfg->line_outs++;
3117 break;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01003118 case AC_JACK_SPEAKER:
Steve Longerbeam81937d32007-05-08 15:33:03 +02003119 seq = get_defcfg_sequence(def_conf);
3120 assoc = get_defcfg_association(def_conf);
3121 if (! assoc)
3122 continue;
3123 if (! assoc_speaker)
3124 assoc_speaker = assoc;
3125 else if (assoc_speaker != assoc)
3126 continue;
Takashi Iwai82bc9552006-03-21 11:24:42 +01003127 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
3128 continue;
3129 cfg->speaker_pins[cfg->speaker_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02003130 sequences_speaker[cfg->speaker_outs] = seq;
Takashi Iwai82bc9552006-03-21 11:24:42 +01003131 cfg->speaker_outs++;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01003132 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003133 case AC_JACK_HP_OUT:
Takashi Iwaif889fa92007-10-31 15:49:32 +01003134 seq = get_defcfg_sequence(def_conf);
3135 assoc = get_defcfg_association(def_conf);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02003136 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
3137 continue;
3138 cfg->hp_pins[cfg->hp_outs] = nid;
Takashi Iwaif889fa92007-10-31 15:49:32 +01003139 sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
Takashi Iwaieb06ed82006-09-20 17:10:27 +02003140 cfg->hp_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003141 break;
Takashi Iwai314634b2006-09-21 11:56:18 +02003142 case AC_JACK_MIC_IN: {
3143 int preferred, alt;
3144 if (loc == AC_JACK_LOC_FRONT) {
3145 preferred = AUTO_PIN_FRONT_MIC;
3146 alt = AUTO_PIN_MIC;
3147 } else {
3148 preferred = AUTO_PIN_MIC;
3149 alt = AUTO_PIN_FRONT_MIC;
3150 }
3151 if (!cfg->input_pins[preferred])
3152 cfg->input_pins[preferred] = nid;
3153 else if (!cfg->input_pins[alt])
3154 cfg->input_pins[alt] = nid;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003155 break;
Takashi Iwai314634b2006-09-21 11:56:18 +02003156 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003157 case AC_JACK_LINE_IN:
3158 if (loc == AC_JACK_LOC_FRONT)
3159 cfg->input_pins[AUTO_PIN_FRONT_LINE] = nid;
3160 else
3161 cfg->input_pins[AUTO_PIN_LINE] = nid;
3162 break;
3163 case AC_JACK_CD:
3164 cfg->input_pins[AUTO_PIN_CD] = nid;
3165 break;
3166 case AC_JACK_AUX:
3167 cfg->input_pins[AUTO_PIN_AUX] = nid;
3168 break;
3169 case AC_JACK_SPDIF_OUT:
3170 cfg->dig_out_pin = nid;
3171 break;
3172 case AC_JACK_SPDIF_IN:
3173 cfg->dig_in_pin = nid;
3174 break;
3175 }
3176 }
3177
Takashi Iwai5832fcf2008-02-12 18:30:12 +01003178 /* FIX-UP:
3179 * If no line-out is defined but multiple HPs are found,
3180 * some of them might be the real line-outs.
3181 */
3182 if (!cfg->line_outs && cfg->hp_outs > 1) {
3183 int i = 0;
3184 while (i < cfg->hp_outs) {
3185 /* The real HPs should have the sequence 0x0f */
3186 if ((sequences_hp[i] & 0x0f) == 0x0f) {
3187 i++;
3188 continue;
3189 }
3190 /* Move it to the line-out table */
3191 cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
3192 sequences_line_out[cfg->line_outs] = sequences_hp[i];
3193 cfg->line_outs++;
3194 cfg->hp_outs--;
3195 memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
3196 sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
3197 memmove(sequences_hp + i - 1, sequences_hp + i,
3198 sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
3199 }
3200 }
3201
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003202 /* sort by sequence */
Steve Longerbeam81937d32007-05-08 15:33:03 +02003203 sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
3204 cfg->line_outs);
3205 sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
3206 cfg->speaker_outs);
Takashi Iwaif889fa92007-10-31 15:49:32 +01003207 sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
3208 cfg->hp_outs);
Steve Longerbeam81937d32007-05-08 15:33:03 +02003209
Takashi Iwaif889fa92007-10-31 15:49:32 +01003210 /* if we have only one mic, make it AUTO_PIN_MIC */
3211 if (!cfg->input_pins[AUTO_PIN_MIC] &&
3212 cfg->input_pins[AUTO_PIN_FRONT_MIC]) {
3213 cfg->input_pins[AUTO_PIN_MIC] =
3214 cfg->input_pins[AUTO_PIN_FRONT_MIC];
3215 cfg->input_pins[AUTO_PIN_FRONT_MIC] = 0;
3216 }
3217 /* ditto for line-in */
3218 if (!cfg->input_pins[AUTO_PIN_LINE] &&
3219 cfg->input_pins[AUTO_PIN_FRONT_LINE]) {
3220 cfg->input_pins[AUTO_PIN_LINE] =
3221 cfg->input_pins[AUTO_PIN_FRONT_LINE];
3222 cfg->input_pins[AUTO_PIN_FRONT_LINE] = 0;
3223 }
3224
Steve Longerbeam81937d32007-05-08 15:33:03 +02003225 /*
3226 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
3227 * as a primary output
3228 */
3229 if (!cfg->line_outs) {
3230 if (cfg->speaker_outs) {
3231 cfg->line_outs = cfg->speaker_outs;
3232 memcpy(cfg->line_out_pins, cfg->speaker_pins,
3233 sizeof(cfg->speaker_pins));
3234 cfg->speaker_outs = 0;
3235 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
3236 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
3237 } else if (cfg->hp_outs) {
3238 cfg->line_outs = cfg->hp_outs;
3239 memcpy(cfg->line_out_pins, cfg->hp_pins,
3240 sizeof(cfg->hp_pins));
3241 cfg->hp_outs = 0;
3242 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
3243 cfg->line_out_type = AUTO_PIN_HP_OUT;
3244 }
3245 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003246
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02003247 /* Reorder the surround channels
3248 * ALSA sequence is front/surr/clfe/side
3249 * HDA sequence is:
3250 * 4-ch: front/surr => OK as it is
3251 * 6-ch: front/clfe/surr
Takashi Iwai9422db42007-04-20 16:11:43 +02003252 * 8-ch: front/clfe/rear/side|fc
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02003253 */
3254 switch (cfg->line_outs) {
3255 case 3:
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02003256 case 4:
3257 nid = cfg->line_out_pins[1];
Takashi Iwai9422db42007-04-20 16:11:43 +02003258 cfg->line_out_pins[1] = cfg->line_out_pins[2];
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02003259 cfg->line_out_pins[2] = nid;
3260 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003261 }
3262
Takashi Iwai82bc9552006-03-21 11:24:42 +01003263 /*
3264 * debug prints of the parsed results
3265 */
3266 snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3267 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
3268 cfg->line_out_pins[2], cfg->line_out_pins[3],
3269 cfg->line_out_pins[4]);
3270 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3271 cfg->speaker_outs, cfg->speaker_pins[0],
3272 cfg->speaker_pins[1], cfg->speaker_pins[2],
3273 cfg->speaker_pins[3], cfg->speaker_pins[4]);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02003274 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3275 cfg->hp_outs, cfg->hp_pins[0],
3276 cfg->hp_pins[1], cfg->hp_pins[2],
3277 cfg->hp_pins[3], cfg->hp_pins[4]);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01003278 snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
Takashi Iwai82bc9552006-03-21 11:24:42 +01003279 snd_printd(" inputs: mic=0x%x, fmic=0x%x, line=0x%x, fline=0x%x,"
3280 " cd=0x%x, aux=0x%x\n",
3281 cfg->input_pins[AUTO_PIN_MIC],
3282 cfg->input_pins[AUTO_PIN_FRONT_MIC],
3283 cfg->input_pins[AUTO_PIN_LINE],
3284 cfg->input_pins[AUTO_PIN_FRONT_LINE],
3285 cfg->input_pins[AUTO_PIN_CD],
3286 cfg->input_pins[AUTO_PIN_AUX]);
3287
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003288 return 0;
3289}
3290
Takashi Iwai4a471b72005-12-07 13:56:29 +01003291/* labels for input pins */
3292const char *auto_pin_cfg_labels[AUTO_PIN_LAST] = {
3293 "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux"
3294};
3295
3296
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297#ifdef CONFIG_PM
3298/*
3299 * power management
3300 */
3301
3302/**
3303 * snd_hda_suspend - suspend the codecs
3304 * @bus: the HDA bus
3305 * @state: suspsend state
3306 *
3307 * Returns 0 if successful.
3308 */
3309int snd_hda_suspend(struct hda_bus *bus, pm_message_t state)
3310{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003311 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312
Takashi Iwai0ba21762007-04-16 11:29:14 +02003313 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai0b7a2e92007-08-14 15:18:26 +02003314#ifdef CONFIG_SND_HDA_POWER_SAVE
3315 if (!codec->power_on)
3316 continue;
3317#endif
Takashi Iwaicb53c622007-08-10 17:21:45 +02003318 hda_call_codec_suspend(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 }
3320 return 0;
3321}
3322
3323/**
3324 * snd_hda_resume - resume the codecs
3325 * @bus: the HDA bus
3326 * @state: resume state
3327 *
3328 * Returns 0 if successful.
Takashi Iwaicb53c622007-08-10 17:21:45 +02003329 *
3330 * This fucntion is defined only when POWER_SAVE isn't set.
3331 * In the power-save mode, the codec is resumed dynamically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 */
3333int snd_hda_resume(struct hda_bus *bus)
3334{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003335 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336
Takashi Iwai0ba21762007-04-16 11:29:14 +02003337 list_for_each_entry(codec, &bus->codec_list, list) {
Maxim Levitskyd804ad92007-09-03 15:28:04 +02003338 if (snd_hda_codec_needs_resume(codec))
3339 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 return 0;
3342}
Maxim Levitskyd804ad92007-09-03 15:28:04 +02003343#ifdef CONFIG_SND_HDA_POWER_SAVE
3344int snd_hda_codecs_inuse(struct hda_bus *bus)
3345{
3346 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347
Maxim Levitskyd804ad92007-09-03 15:28:04 +02003348 list_for_each_entry(codec, &bus->codec_list, list) {
3349 if (snd_hda_codec_needs_resume(codec))
3350 return 1;
3351 }
3352 return 0;
3353}
3354#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355#endif
Takashi Iwaib2e18592008-07-30 15:01:44 +02003356
3357/*
3358 * generic arrays
3359 */
3360
3361/* get a new element from the given array
3362 * if it exceeds the pre-allocated array size, re-allocate the array
3363 */
3364void *snd_array_new(struct snd_array *array)
3365{
3366 if (array->used >= array->alloced) {
3367 int num = array->alloced + array->alloc_align;
3368 void *nlist = kcalloc(num + 1, array->elem_size, GFP_KERNEL);
3369 if (!nlist)
3370 return NULL;
3371 if (array->list) {
3372 memcpy(nlist, array->list,
3373 array->elem_size * array->alloced);
3374 kfree(array->list);
3375 }
3376 array->list = nlist;
3377 array->alloced = num;
3378 }
3379 return array->list + (array->used++ * array->elem_size);
3380}
3381
3382/* free the given array elements */
3383void snd_array_free(struct snd_array *array)
3384{
3385 kfree(array->list);
3386 array->used = 0;
3387 array->alloced = 0;
3388 array->list = NULL;
3389}