blob: f84874445b5091dd36833cba1c8ad8d3d4a2149f [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
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100160/*
161 * Compose a 32bit command word to be sent to the HD-audio controller
162 */
163static inline unsigned int
164make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int direct,
165 unsigned int verb, unsigned int parm)
166{
167 u32 val;
168
169 val = (u32)(codec->addr & 0x0f) << 28;
170 val |= (u32)direct << 27;
171 val |= (u32)nid << 20;
172 val |= verb << 8;
173 val |= parm;
174 return val;
175}
176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177/**
178 * snd_hda_codec_read - send a command and get the response
179 * @codec: the HDA codec
180 * @nid: NID to send the command
181 * @direct: direct flag
182 * @verb: the verb to send
183 * @parm: the parameter for the verb
184 *
185 * Send a single command and read the corresponding response.
186 *
187 * Returns the obtained response value, or -1 for an error.
188 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200189unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
190 int direct,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 unsigned int verb, unsigned int parm)
192{
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100193 struct hda_bus *bus = codec->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 unsigned int res;
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100195
196 res = make_codec_cmd(codec, nid, direct, verb, parm);
Takashi Iwaicb53c622007-08-10 17:21:45 +0200197 snd_hda_power_up(codec);
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100198 mutex_lock(&bus->cmd_mutex);
199 if (!bus->ops.command(bus, res))
200 res = bus->ops.get_response(bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 else
202 res = (unsigned int)-1;
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100203 mutex_unlock(&bus->cmd_mutex);
Takashi Iwaicb53c622007-08-10 17:21:45 +0200204 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 return res;
206}
207
208/**
209 * snd_hda_codec_write - send a single command without waiting for response
210 * @codec: the HDA codec
211 * @nid: NID to send the command
212 * @direct: direct flag
213 * @verb: the verb to send
214 * @parm: the parameter for the verb
215 *
216 * Send a single command without waiting for response.
217 *
218 * Returns 0 if successful, or a negative error code.
219 */
220int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
221 unsigned int verb, unsigned int parm)
222{
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100223 struct hda_bus *bus = codec->bus;
224 unsigned int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 int err;
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100226
227 res = make_codec_cmd(codec, nid, direct, verb, parm);
Takashi Iwaicb53c622007-08-10 17:21:45 +0200228 snd_hda_power_up(codec);
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100229 mutex_lock(&bus->cmd_mutex);
230 err = bus->ops.command(bus, res);
231 mutex_unlock(&bus->cmd_mutex);
Takashi Iwaicb53c622007-08-10 17:21:45 +0200232 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 return err;
234}
235
236/**
237 * snd_hda_sequence_write - sequence writes
238 * @codec: the HDA codec
239 * @seq: VERB array to send
240 *
241 * Send the commands sequentially from the given array.
242 * The array must be terminated with NID=0.
243 */
244void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
245{
246 for (; seq->nid; seq++)
247 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
248}
249
250/**
251 * snd_hda_get_sub_nodes - get the range of sub nodes
252 * @codec: the HDA codec
253 * @nid: NID to parse
254 * @start_id: the pointer to store the start NID
255 *
256 * Parse the NID and store the start NID of its sub-nodes.
257 * Returns the number of sub-nodes.
258 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200259int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
260 hda_nid_t *start_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
262 unsigned int parm;
263
264 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
Danny Tholene8a7f132007-09-11 21:41:56 +0200265 if (parm == -1)
266 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 *start_id = (parm >> 16) & 0x7fff;
268 return (int)(parm & 0x7fff);
269}
270
271/**
272 * snd_hda_get_connections - get connection list
273 * @codec: the HDA codec
274 * @nid: NID to parse
275 * @conn_list: connection list array
276 * @max_conns: max. number of connections to store
277 *
278 * Parses the connection list of the given widget and stores the list
279 * of NIDs.
280 *
281 * Returns the number of connections, or a negative error code.
282 */
283int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
284 hda_nid_t *conn_list, int max_conns)
285{
286 unsigned int parm;
Takashi Iwai54d17402005-11-21 16:33:22 +0100287 int i, conn_len, conns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 unsigned int shift, num_elems, mask;
Takashi Iwai54d17402005-11-21 16:33:22 +0100289 hda_nid_t prev_nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Takashi Iwaida3cec32008-08-08 17:12:14 +0200291 if (snd_BUG_ON(!conn_list || max_conns <= 0))
292 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
294 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
295 if (parm & AC_CLIST_LONG) {
296 /* long form */
297 shift = 16;
298 num_elems = 2;
299 } else {
300 /* short form */
301 shift = 8;
302 num_elems = 4;
303 }
304 conn_len = parm & AC_CLIST_LENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 mask = (1 << (shift-1)) - 1;
306
Takashi Iwai0ba21762007-04-16 11:29:14 +0200307 if (!conn_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 return 0; /* no connection */
309
310 if (conn_len == 1) {
311 /* single connection */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200312 parm = snd_hda_codec_read(codec, nid, 0,
313 AC_VERB_GET_CONNECT_LIST, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 conn_list[0] = parm & mask;
315 return 1;
316 }
317
318 /* multi connection */
319 conns = 0;
Takashi Iwai54d17402005-11-21 16:33:22 +0100320 prev_nid = 0;
321 for (i = 0; i < conn_len; i++) {
322 int range_val;
323 hda_nid_t val, n;
324
325 if (i % num_elems == 0)
326 parm = snd_hda_codec_read(codec, nid, 0,
327 AC_VERB_GET_CONNECT_LIST, i);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200328 range_val = !!(parm & (1 << (shift-1))); /* ranges */
Takashi Iwai54d17402005-11-21 16:33:22 +0100329 val = parm & mask;
330 parm >>= shift;
331 if (range_val) {
332 /* ranges between the previous and this one */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200333 if (!prev_nid || prev_nid >= val) {
334 snd_printk(KERN_WARNING "hda_codec: "
335 "invalid dep_range_val %x:%x\n",
336 prev_nid, val);
Takashi Iwai54d17402005-11-21 16:33:22 +0100337 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100339 for (n = prev_nid + 1; n <= val; n++) {
340 if (conns >= max_conns) {
Takashi Iwai0ba21762007-04-16 11:29:14 +0200341 snd_printk(KERN_ERR
342 "Too many connections\n");
Takashi Iwai54d17402005-11-21 16:33:22 +0100343 return -EINVAL;
344 }
345 conn_list[conns++] = n;
346 }
347 } else {
348 if (conns >= max_conns) {
349 snd_printk(KERN_ERR "Too many connections\n");
350 return -EINVAL;
351 }
352 conn_list[conns++] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100354 prev_nid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 }
356 return conns;
357}
358
359
360/**
361 * snd_hda_queue_unsol_event - add an unsolicited event to queue
362 * @bus: the BUS
363 * @res: unsolicited event (lower 32bit of RIRB entry)
364 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
365 *
366 * Adds the given event to the queue. The events are processed in
367 * the workqueue asynchronously. Call this function in the interrupt
368 * hanlder when RIRB receives an unsolicited event.
369 *
370 * Returns 0 if successful, or a negative error code.
371 */
372int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
373{
374 struct hda_bus_unsolicited *unsol;
375 unsigned int wp;
376
Takashi Iwai0ba21762007-04-16 11:29:14 +0200377 unsol = bus->unsol;
378 if (!unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 return 0;
380
381 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
382 unsol->wp = wp;
383
384 wp <<= 1;
385 unsol->queue[wp] = res;
386 unsol->queue[wp + 1] = res_ex;
387
Takashi Iwaie250af22006-12-19 17:08:52 +0100388 schedule_work(&unsol->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
390 return 0;
391}
392
393/*
Wu Fengguang5c1d1a92008-10-07 14:17:53 +0800394 * process queued unsolicited events
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 */
David Howellsc4028952006-11-22 14:57:56 +0000396static void process_unsol_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
David Howellsc4028952006-11-22 14:57:56 +0000398 struct hda_bus_unsolicited *unsol =
399 container_of(work, struct hda_bus_unsolicited, work);
400 struct hda_bus *bus = unsol->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 struct hda_codec *codec;
402 unsigned int rp, caddr, res;
403
404 while (unsol->rp != unsol->wp) {
405 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
406 unsol->rp = rp;
407 rp <<= 1;
408 res = unsol->queue[rp];
409 caddr = unsol->queue[rp + 1];
Takashi Iwai0ba21762007-04-16 11:29:14 +0200410 if (!(caddr & (1 << 4))) /* no unsolicited event? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 continue;
412 codec = bus->caddr_tbl[caddr & 0x0f];
413 if (codec && codec->patch_ops.unsol_event)
414 codec->patch_ops.unsol_event(codec, res);
415 }
416}
417
418/*
419 * initialize unsolicited queue
420 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200421static int init_unsol_queue(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
423 struct hda_bus_unsolicited *unsol;
424
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100425 if (bus->unsol) /* already initialized */
426 return 0;
427
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200428 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200429 if (!unsol) {
430 snd_printk(KERN_ERR "hda_codec: "
431 "can't allocate unsolicited queue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 return -ENOMEM;
433 }
David Howellsc4028952006-11-22 14:57:56 +0000434 INIT_WORK(&unsol->work, process_unsol_events);
435 unsol->bus = bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 bus->unsol = unsol;
437 return 0;
438}
439
440/*
441 * destructor
442 */
443static void snd_hda_codec_free(struct hda_codec *codec);
444
445static int snd_hda_bus_free(struct hda_bus *bus)
446{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200447 struct hda_codec *codec, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Takashi Iwai0ba21762007-04-16 11:29:14 +0200449 if (!bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 return 0;
451 if (bus->unsol) {
Takashi Iwaie250af22006-12-19 17:08:52 +0100452 flush_scheduled_work();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 kfree(bus->unsol);
454 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200455 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 snd_hda_codec_free(codec);
457 }
458 if (bus->ops.private_free)
459 bus->ops.private_free(bus);
460 kfree(bus);
461 return 0;
462}
463
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100464static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
466 struct hda_bus *bus = device->device_data;
Takashi Iwaib94d35392008-11-21 09:08:06 +0100467 bus->shutdown = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 return snd_hda_bus_free(bus);
469}
470
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200471#ifdef CONFIG_SND_HDA_HWDEP
472static int snd_hda_bus_dev_register(struct snd_device *device)
473{
474 struct hda_bus *bus = device->device_data;
475 struct hda_codec *codec;
476 list_for_each_entry(codec, &bus->codec_list, list) {
477 snd_hda_hwdep_add_sysfs(codec);
478 }
479 return 0;
480}
481#else
482#define snd_hda_bus_dev_register NULL
483#endif
484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485/**
486 * snd_hda_bus_new - create a HDA bus
487 * @card: the card entry
488 * @temp: the template for hda_bus information
489 * @busp: the pointer to store the created bus instance
490 *
491 * Returns 0 if successful, or a negative error code.
492 */
Takashi Iwai756e2b02007-04-16 11:27:07 +0200493int __devinit snd_hda_bus_new(struct snd_card *card,
494 const struct hda_bus_template *temp,
495 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
497 struct hda_bus *bus;
498 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100499 static struct snd_device_ops dev_ops = {
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200500 .dev_register = snd_hda_bus_dev_register,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 .dev_free = snd_hda_bus_dev_free,
502 };
503
Takashi Iwaida3cec32008-08-08 17:12:14 +0200504 if (snd_BUG_ON(!temp))
505 return -EINVAL;
506 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
507 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
509 if (busp)
510 *busp = NULL;
511
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200512 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 if (bus == NULL) {
514 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
515 return -ENOMEM;
516 }
517
518 bus->card = card;
519 bus->private_data = temp->private_data;
520 bus->pci = temp->pci;
521 bus->modelname = temp->modelname;
522 bus->ops = temp->ops;
523
Ingo Molnar62932df2006-01-16 16:34:20 +0100524 mutex_init(&bus->cmd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 INIT_LIST_HEAD(&bus->codec_list);
526
Takashi Iwai0ba21762007-04-16 11:29:14 +0200527 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
528 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 snd_hda_bus_free(bus);
530 return err;
531 }
532 if (busp)
533 *busp = bus;
534 return 0;
535}
536
Takashi Iwai82467612007-07-27 19:15:54 +0200537#ifdef CONFIG_SND_HDA_GENERIC
538#define is_generic_config(codec) \
Takashi Iwaif44ac832008-07-30 15:01:45 +0200539 (codec->modelname && !strcmp(codec->modelname, "generic"))
Takashi Iwai82467612007-07-27 19:15:54 +0200540#else
541#define is_generic_config(codec) 0
542#endif
543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544/*
545 * find a matching codec preset
546 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200547static const struct hda_codec_preset *
Takashi Iwai756e2b02007-04-16 11:27:07 +0200548find_codec_preset(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
550 const struct hda_codec_preset **tbl, *preset;
551
Takashi Iwai82467612007-07-27 19:15:54 +0200552 if (is_generic_config(codec))
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100553 return NULL; /* use the generic parser */
554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 for (tbl = hda_preset_tables; *tbl; tbl++) {
556 for (preset = *tbl; preset->id; preset++) {
557 u32 mask = preset->mask;
Marc Boucherca7cfae2008-01-22 15:32:25 +0100558 if (preset->afg && preset->afg != codec->afg)
559 continue;
560 if (preset->mfg && preset->mfg != codec->mfg)
561 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200562 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 mask = ~0;
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200564 if (preset->id == (codec->vendor_id & mask) &&
Takashi Iwai0ba21762007-04-16 11:29:14 +0200565 (!preset->rev ||
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200566 preset->rev == codec->revision_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 return preset;
568 }
569 }
570 return NULL;
571}
572
573/*
Takashi Iwaif44ac832008-07-30 15:01:45 +0200574 * get_codec_name - store the codec name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 */
Takashi Iwaif44ac832008-07-30 15:01:45 +0200576static int get_codec_name(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577{
578 const struct hda_vendor_id *c;
579 const char *vendor = NULL;
580 u16 vendor_id = codec->vendor_id >> 16;
Takashi Iwaif44ac832008-07-30 15:01:45 +0200581 char tmp[16], name[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
583 for (c = hda_vendor_ids; c->id; c++) {
584 if (c->id == vendor_id) {
585 vendor = c->name;
586 break;
587 }
588 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200589 if (!vendor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 sprintf(tmp, "Generic %04x", vendor_id);
591 vendor = tmp;
592 }
593 if (codec->preset && codec->preset->name)
Takashi Iwaif44ac832008-07-30 15:01:45 +0200594 snprintf(name, sizeof(name), "%s %s", vendor,
595 codec->preset->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 else
Takashi Iwaif44ac832008-07-30 15:01:45 +0200597 snprintf(name, sizeof(name), "%s ID %x", vendor,
Takashi Iwai0ba21762007-04-16 11:29:14 +0200598 codec->vendor_id & 0xffff);
Takashi Iwaif44ac832008-07-30 15:01:45 +0200599 codec->name = kstrdup(name, GFP_KERNEL);
600 if (!codec->name)
601 return -ENOMEM;
602 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603}
604
605/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200606 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 */
Takashi Iwai756e2b02007-04-16 11:27:07 +0200608static void __devinit setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
610 int i, total_nodes;
611 hda_nid_t nid;
612
613 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
614 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +0200615 unsigned int func;
616 func = snd_hda_param_read(codec, nid, AC_PAR_FUNCTION_TYPE);
617 switch (func & 0xff) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200618 case AC_GRP_AUDIO_FUNCTION:
619 codec->afg = nid;
620 break;
621 case AC_GRP_MODEM_FUNCTION:
622 codec->mfg = nid;
623 break;
624 default:
625 break;
626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628}
629
630/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100631 * read widget caps for each widget and store in cache
632 */
633static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
634{
635 int i;
636 hda_nid_t nid;
637
638 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
639 &codec->start_nid);
640 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200641 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +0100642 return -ENOMEM;
643 nid = codec->start_nid;
644 for (i = 0; i < codec->num_nodes; i++, nid++)
645 codec->wcaps[i] = snd_hda_param_read(codec, nid,
646 AC_PAR_AUDIO_WIDGET_CAP);
647 return 0;
648}
649
650
Takashi Iwai01751f52007-08-10 16:59:39 +0200651static void init_hda_cache(struct hda_cache_rec *cache,
652 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +0200653static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +0200654
Takashi Iwai54d17402005-11-21 16:33:22 +0100655/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 * codec destructor
657 */
658static void snd_hda_codec_free(struct hda_codec *codec)
659{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200660 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 return;
Takashi Iwaicb53c622007-08-10 17:21:45 +0200662#ifdef CONFIG_SND_HDA_POWER_SAVE
663 cancel_delayed_work(&codec->power_work);
Takashi Iwai2525fdc42007-08-15 22:18:22 +0200664 flush_scheduled_work();
Takashi Iwaicb53c622007-08-10 17:21:45 +0200665#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +0200667 snd_array_free(&codec->mixers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 codec->bus->caddr_tbl[codec->addr] = NULL;
669 if (codec->patch_ops.free)
670 codec->patch_ops.free(codec);
Takashi Iwai01751f52007-08-10 16:59:39 +0200671 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +0200672 free_hda_cache(&codec->cmd_cache);
Takashi Iwaif44ac832008-07-30 15:01:45 +0200673 kfree(codec->name);
674 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +0100675 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 kfree(codec);
677}
678
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679/**
680 * snd_hda_codec_new - create a HDA codec
681 * @bus: the bus to assign
682 * @codec_addr: the codec address
683 * @codecp: the pointer to store the generated codec
684 *
685 * Returns 0 if successful, or a negative error code.
686 */
Takashi Iwai756e2b02007-04-16 11:27:07 +0200687int __devinit snd_hda_codec_new(struct hda_bus *bus, unsigned int codec_addr,
688 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
690 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +0200691 char component[31];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 int err;
693
Takashi Iwaida3cec32008-08-08 17:12:14 +0200694 if (snd_BUG_ON(!bus))
695 return -EINVAL;
696 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
697 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
699 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +0200700 snd_printk(KERN_ERR "hda_codec: "
701 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 return -EBUSY;
703 }
704
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200705 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 if (codec == NULL) {
707 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
708 return -ENOMEM;
709 }
710
711 codec->bus = bus;
712 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +0100713 mutex_init(&codec->spdif_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +0200714 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +0200715 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwaid13bd412008-07-30 15:01:45 +0200716 snd_array_init(&codec->mixers, sizeof(struct snd_kcontrol *), 32);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200717 if (codec->bus->modelname) {
718 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
719 if (!codec->modelname) {
720 snd_hda_codec_free(codec);
721 return -ENODEV;
722 }
723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Takashi Iwaicb53c622007-08-10 17:21:45 +0200725#ifdef CONFIG_SND_HDA_POWER_SAVE
726 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
727 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
728 * the caller has to power down appropriatley after initialization
729 * phase.
730 */
731 hda_keep_power_on(codec);
732#endif
733
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 list_add_tail(&codec->list, &bus->codec_list);
735 bus->caddr_tbl[codec_addr] = codec;
736
Takashi Iwai0ba21762007-04-16 11:29:14 +0200737 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
738 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +0100739 if (codec->vendor_id == -1)
740 /* read again, hopefully the access method was corrected
741 * in the last read...
742 */
743 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
744 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200745 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
746 AC_PAR_SUBSYSTEM_ID);
747 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
748 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200750 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200751 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200752 snd_printdd("hda_codec: no AFG or MFG node found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 snd_hda_codec_free(codec);
754 return -ENODEV;
755 }
756
Takashi Iwai54d17402005-11-21 16:33:22 +0100757 if (read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg) < 0) {
758 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
759 snd_hda_codec_free(codec);
760 return -ENOMEM;
761 }
762
Takashi Iwai0ba21762007-04-16 11:29:14 +0200763 if (!codec->subsystem_id) {
Takashi Iwai86284e42005-10-11 15:05:54 +0200764 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200765 codec->subsystem_id =
766 snd_hda_codec_read(codec, nid, 0,
767 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +0200768 }
Takashi Iwaif44ac832008-07-30 15:01:45 +0200769 if (bus->modelname)
770 codec->modelname = kstrdup(bus->modelname, GFP_KERNEL);
Takashi Iwai86284e42005-10-11 15:05:54 +0200771
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200772 err = snd_hda_codec_configure(codec);
773 if (err < 0) {
774 snd_hda_codec_free(codec);
775 return err;
776 }
777 snd_hda_codec_proc_new(codec);
778
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200779 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200780
781 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
782 codec->subsystem_id, codec->revision_id);
783 snd_component_add(codec->bus->card, component);
784
785 if (codecp)
786 *codecp = codec;
787 return 0;
788}
789
790int snd_hda_codec_configure(struct hda_codec *codec)
791{
792 int err;
793
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100794 codec->preset = find_codec_preset(codec);
Takashi Iwaif44ac832008-07-30 15:01:45 +0200795 if (!codec->name) {
796 err = get_codec_name(codec);
797 if (err < 0)
798 return err;
799 }
Takashi Iwai43ea1d42007-04-26 19:12:08 +0200800 /* audio codec should override the mixer name */
Takashi Iwaif44ac832008-07-30 15:01:45 +0200801 if (codec->afg || !*codec->bus->card->mixername)
802 strlcpy(codec->bus->card->mixername, codec->name,
803 sizeof(codec->bus->card->mixername));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Takashi Iwai82467612007-07-27 19:15:54 +0200805 if (is_generic_config(codec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai82467612007-07-27 19:15:54 +0200807 goto patched;
808 }
Takashi Iwai82467612007-07-27 19:15:54 +0200809 if (codec->preset && codec->preset->patch) {
810 err = codec->preset->patch(codec);
811 goto patched;
812 }
813
814 /* call the default parser */
Takashi Iwai82467612007-07-27 19:15:54 +0200815 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai35a1e0c2007-10-19 08:13:40 +0200816 if (err < 0)
817 printk(KERN_ERR "hda-codec: No codec parser is available\n");
Takashi Iwai82467612007-07-27 19:15:54 +0200818
819 patched:
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200820 if (!err && codec->patch_ops.unsol_event)
821 err = init_unsol_queue(codec->bus);
822 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823}
824
825/**
826 * snd_hda_codec_setup_stream - set up the codec for streaming
827 * @codec: the CODEC to set up
828 * @nid: the NID to set up
829 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
830 * @channel_id: channel id to pass, zero based.
831 * @format: stream format.
832 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200833void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
834 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 int channel_id, int format)
836{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200837 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +0200838 return;
839
Takashi Iwai0ba21762007-04-16 11:29:14 +0200840 snd_printdd("hda_codec_setup_stream: "
841 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 nid, stream_tag, channel_id, format);
843 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID,
844 (stream_tag << 4) | channel_id);
845 msleep(1);
846 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, format);
847}
848
Takashi Iwai888afa12008-03-18 09:57:50 +0100849void snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid)
850{
851 if (!nid)
852 return;
853
854 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
855 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
856#if 0 /* keep the format */
857 msleep(1);
858 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0);
859#endif
860}
861
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862/*
863 * amp access functions
864 */
865
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200866/* FIXME: more better hash key? */
867#define HDA_HASH_KEY(nid,dir,idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200869#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
871/* initialize the hash table */
Takashi Iwai01751f52007-08-10 16:59:39 +0200872static void __devinit init_hda_cache(struct hda_cache_rec *cache,
873 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
Takashi Iwai01751f52007-08-10 16:59:39 +0200875 memset(cache, 0, sizeof(*cache));
876 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +0200877 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +0200878}
879
Takashi Iwai1fcaee62007-08-23 00:01:09 +0200880static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +0200881{
Takashi Iwai603c4012008-07-30 15:01:44 +0200882 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883}
884
885/* query the hash. allocate an entry if not found. */
Takashi Iwai01751f52007-08-10 16:59:39 +0200886static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
887 u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888{
Takashi Iwai01751f52007-08-10 16:59:39 +0200889 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
890 u16 cur = cache->hash[idx];
891 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
893 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +0100894 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 if (info->key == key)
896 return info;
897 cur = info->next;
898 }
899
900 /* add a new hash entry */
Takashi Iwai603c4012008-07-30 15:01:44 +0200901 info = snd_array_new(&cache->buf);
Takashi Iwaic2174292008-11-07 00:23:30 +0100902 if (!info)
903 return NULL;
Takashi Iwaif43aa022008-11-10 16:24:26 +0100904 cur = snd_array_index(&cache->buf, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 info->key = key;
Takashi Iwai01751f52007-08-10 16:59:39 +0200906 info->val = 0;
907 info->next = cache->hash[idx];
908 cache->hash[idx] = cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
910 return info;
911}
912
Takashi Iwai01751f52007-08-10 16:59:39 +0200913/* query and allocate an amp hash entry */
914static inline struct hda_amp_info *
915get_alloc_amp_hash(struct hda_codec *codec, u32 key)
916{
917 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
918}
919
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920/*
921 * query AMP capabilities for the given widget and direction
922 */
Matthew Ranostay09a99952008-01-24 11:49:21 +0100923u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200925 struct hda_amp_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Takashi Iwai0ba21762007-04-16 11:29:14 +0200927 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
928 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 return 0;
Takashi Iwai01751f52007-08-10 16:59:39 +0200930 if (!(info->head.val & INFO_AMP_CAPS)) {
Takashi Iwai0ba21762007-04-16 11:29:14 +0200931 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 nid = codec->afg;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200933 info->amp_caps = snd_hda_param_read(codec, nid,
934 direction == HDA_OUTPUT ?
935 AC_PAR_AMP_OUT_CAP :
936 AC_PAR_AMP_IN_CAP);
Takashi Iwaib75e53f2007-05-10 16:56:09 +0200937 if (info->amp_caps)
Takashi Iwai01751f52007-08-10 16:59:39 +0200938 info->head.val |= INFO_AMP_CAPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 }
940 return info->amp_caps;
941}
942
Takashi Iwai897cc182007-05-29 19:01:37 +0200943int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
944 unsigned int caps)
945{
946 struct hda_amp_info *info;
947
948 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
949 if (!info)
950 return -EINVAL;
951 info->amp_caps = caps;
Takashi Iwai01751f52007-08-10 16:59:39 +0200952 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai897cc182007-05-29 19:01:37 +0200953 return 0;
954}
955
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956/*
957 * read the current volume to info
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200958 * if the cache exists, read the cache value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200960static unsigned int get_vol_mute(struct hda_codec *codec,
961 struct hda_amp_info *info, hda_nid_t nid,
962 int ch, int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963{
964 u32 val, parm;
965
Takashi Iwai01751f52007-08-10 16:59:39 +0200966 if (info->head.val & INFO_AMP_VOL(ch))
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200967 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
969 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
970 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
971 parm |= index;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200972 val = snd_hda_codec_read(codec, nid, 0,
973 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 info->vol[ch] = val & 0xff;
Takashi Iwai01751f52007-08-10 16:59:39 +0200975 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200976 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977}
978
979/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200980 * write the current volume in info to the h/w and update the cache
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200982static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
Takashi Iwai0ba21762007-04-16 11:29:14 +0200983 hda_nid_t nid, int ch, int direction, int index,
984 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985{
986 u32 parm;
987
988 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
989 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
990 parm |= index << AC_AMP_SET_INDEX_SHIFT;
991 parm |= val;
992 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200993 info->vol[ch] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994}
995
996/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200997 * read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 */
Takashi Iwai834be882006-03-01 14:16:17 +0100999int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1000 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001002 struct hda_amp_info *info;
1003 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1004 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001006 return get_vol_mute(codec, info, nid, ch, direction, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007}
1008
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001009/*
1010 * update the AMP value, mask = bit mask to set, val = the value
1011 */
Takashi Iwai834be882006-03-01 14:16:17 +01001012int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1013 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001015 struct hda_amp_info *info;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001016
Takashi Iwai0ba21762007-04-16 11:29:14 +02001017 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
1018 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001020 val &= mask;
1021 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001022 if (info->vol[ch] == val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001024 put_vol_mute(codec, info, nid, ch, direction, idx, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 return 1;
1026}
1027
Takashi Iwai47fd8302007-08-10 17:11:07 +02001028/*
1029 * update the AMP stereo with the same mask and value
1030 */
1031int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1032 int direction, int idx, int mask, int val)
1033{
1034 int ch, ret = 0;
1035 for (ch = 0; ch < 2; ch++)
1036 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1037 idx, mask, val);
1038 return ret;
1039}
1040
Takashi Iwaicb53c622007-08-10 17:21:45 +02001041#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001042/* resume the all amp commands from the cache */
1043void snd_hda_codec_resume_amp(struct hda_codec *codec)
1044{
Takashi Iwai603c4012008-07-30 15:01:44 +02001045 struct hda_amp_info *buffer = codec->amp_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001046 int i;
1047
Takashi Iwai603c4012008-07-30 15:01:44 +02001048 for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001049 u32 key = buffer->head.key;
1050 hda_nid_t nid;
1051 unsigned int idx, dir, ch;
1052 if (!key)
1053 continue;
1054 nid = key & 0xff;
1055 idx = (key >> 16) & 0xff;
1056 dir = (key >> 24) & 0xff;
1057 for (ch = 0; ch < 2; ch++) {
1058 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
1059 continue;
1060 put_vol_mute(codec, buffer, nid, ch, dir, idx,
1061 buffer->vol[ch]);
1062 }
1063 }
1064}
Takashi Iwaicb53c622007-08-10 17:21:45 +02001065#endif /* SND_HDA_NEEDS_RESUME */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067/* volume */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001068int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1069 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070{
1071 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1072 u16 nid = get_amp_nid(kcontrol);
1073 u8 chs = get_amp_channels(kcontrol);
1074 int dir = get_amp_direction(kcontrol);
1075 u32 caps;
1076
1077 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001078 /* num steps */
1079 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1080 if (!caps) {
1081 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01001082 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
1083 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 return -EINVAL;
1085 }
1086 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1087 uinfo->count = chs == 3 ? 2 : 1;
1088 uinfo->value.integer.min = 0;
1089 uinfo->value.integer.max = caps;
1090 return 0;
1091}
1092
Takashi Iwai0ba21762007-04-16 11:29:14 +02001093int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1094 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095{
1096 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1097 hda_nid_t nid = get_amp_nid(kcontrol);
1098 int chs = get_amp_channels(kcontrol);
1099 int dir = get_amp_direction(kcontrol);
1100 int idx = get_amp_index(kcontrol);
1101 long *valp = ucontrol->value.integer.value;
1102
1103 if (chs & 1)
Takashi Iwai47fd8302007-08-10 17:11:07 +02001104 *valp++ = snd_hda_codec_amp_read(codec, nid, 0, dir, idx)
1105 & HDA_AMP_VOLMASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 if (chs & 2)
Takashi Iwai47fd8302007-08-10 17:11:07 +02001107 *valp = snd_hda_codec_amp_read(codec, nid, 1, dir, idx)
1108 & HDA_AMP_VOLMASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 return 0;
1110}
1111
Takashi Iwai0ba21762007-04-16 11:29:14 +02001112int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1113 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114{
1115 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1116 hda_nid_t nid = get_amp_nid(kcontrol);
1117 int chs = get_amp_channels(kcontrol);
1118 int dir = get_amp_direction(kcontrol);
1119 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 long *valp = ucontrol->value.integer.value;
1121 int change = 0;
1122
Takashi Iwaicb53c622007-08-10 17:21:45 +02001123 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001124 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001125 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
1126 0x7f, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001127 valp++;
1128 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001129 if (chs & 2)
1130 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001131 0x7f, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001132 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 return change;
1134}
1135
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001136int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1137 unsigned int size, unsigned int __user *_tlv)
1138{
1139 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1140 hda_nid_t nid = get_amp_nid(kcontrol);
1141 int dir = get_amp_direction(kcontrol);
1142 u32 caps, val1, val2;
1143
1144 if (size < 4 * sizeof(unsigned int))
1145 return -ENOMEM;
1146 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001147 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1148 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001149 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
1150 val1 = ((int)val1) * ((int)val2);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001151 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
1152 return -EFAULT;
1153 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
1154 return -EFAULT;
1155 if (put_user(val1, _tlv + 2))
1156 return -EFAULT;
1157 if (put_user(val2, _tlv + 3))
1158 return -EFAULT;
1159 return 0;
1160}
1161
Takashi Iwai2134ea42008-01-10 16:53:55 +01001162/*
1163 * set (static) TLV for virtual master volume; recalculated as max 0dB
1164 */
1165void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
1166 unsigned int *tlv)
1167{
1168 u32 caps;
1169 int nums, step;
1170
1171 caps = query_amp_caps(codec, nid, dir);
1172 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1173 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1174 step = (step + 1) * 25;
1175 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
1176 tlv[1] = 2 * sizeof(unsigned int);
1177 tlv[2] = -nums * step;
1178 tlv[3] = step;
1179}
1180
1181/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01001182static struct snd_kcontrol *
1183_snd_hda_find_mixer_ctl(struct hda_codec *codec,
1184 const char *name, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01001185{
1186 struct snd_ctl_elem_id id;
1187 memset(&id, 0, sizeof(id));
1188 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwai09f99702008-02-04 12:31:13 +01001189 id.index = idx;
Takashi Iwai2134ea42008-01-10 16:53:55 +01001190 strcpy(id.name, name);
1191 return snd_ctl_find_id(codec->bus->card, &id);
1192}
1193
Takashi Iwai09f99702008-02-04 12:31:13 +01001194struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
1195 const char *name)
1196{
1197 return _snd_hda_find_mixer_ctl(codec, name, 0);
1198}
1199
Takashi Iwaid13bd412008-07-30 15:01:45 +02001200/* Add a control element and assign to the codec */
1201int snd_hda_ctl_add(struct hda_codec *codec, struct snd_kcontrol *kctl)
1202{
1203 int err;
1204 struct snd_kcontrol **knewp;
1205
1206 err = snd_ctl_add(codec->bus->card, kctl);
1207 if (err < 0)
1208 return err;
1209 knewp = snd_array_new(&codec->mixers);
1210 if (!knewp)
1211 return -ENOMEM;
1212 *knewp = kctl;
1213 return 0;
1214}
1215
1216/* Clear all controls assigned to the given codec */
1217void snd_hda_ctls_clear(struct hda_codec *codec)
1218{
1219 int i;
1220 struct snd_kcontrol **kctls = codec->mixers.list;
1221 for (i = 0; i < codec->mixers.used; i++)
1222 snd_ctl_remove(codec->bus->card, kctls[i]);
1223 snd_array_free(&codec->mixers);
1224}
1225
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001226void snd_hda_codec_reset(struct hda_codec *codec)
1227{
1228 int i;
1229
1230#ifdef CONFIG_SND_HDA_POWER_SAVE
1231 cancel_delayed_work(&codec->power_work);
1232 flush_scheduled_work();
1233#endif
1234 snd_hda_ctls_clear(codec);
1235 /* relase PCMs */
1236 for (i = 0; i < codec->num_pcms; i++) {
1237 if (codec->pcm_info[i].pcm)
1238 snd_device_free(codec->bus->card,
1239 codec->pcm_info[i].pcm);
1240 }
1241 if (codec->patch_ops.free)
1242 codec->patch_ops.free(codec);
1243 codec->spec = NULL;
1244 free_hda_cache(&codec->amp_cache);
1245 free_hda_cache(&codec->cmd_cache);
1246 codec->num_pcms = 0;
1247 codec->pcm_info = NULL;
1248 codec->preset = NULL;
1249}
1250
Takashi Iwai2134ea42008-01-10 16:53:55 +01001251/* create a virtual master control and add slaves */
1252int snd_hda_add_vmaster(struct hda_codec *codec, char *name,
1253 unsigned int *tlv, const char **slaves)
1254{
1255 struct snd_kcontrol *kctl;
1256 const char **s;
1257 int err;
1258
Takashi Iwai2f085542008-02-22 18:43:50 +01001259 for (s = slaves; *s && !snd_hda_find_mixer_ctl(codec, *s); s++)
1260 ;
1261 if (!*s) {
1262 snd_printdd("No slave found for %s\n", name);
1263 return 0;
1264 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01001265 kctl = snd_ctl_make_virtual_master(name, tlv);
1266 if (!kctl)
1267 return -ENOMEM;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001268 err = snd_hda_ctl_add(codec, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001269 if (err < 0)
1270 return err;
1271
1272 for (s = slaves; *s; s++) {
1273 struct snd_kcontrol *sctl;
1274
1275 sctl = snd_hda_find_mixer_ctl(codec, *s);
1276 if (!sctl) {
1277 snd_printdd("Cannot find slave %s, skipped\n", *s);
1278 continue;
1279 }
1280 err = snd_ctl_add_slave(kctl, sctl);
1281 if (err < 0)
1282 return err;
1283 }
1284 return 0;
1285}
1286
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287/* switch */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001288int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
1289 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290{
1291 int chs = get_amp_channels(kcontrol);
1292
1293 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1294 uinfo->count = chs == 3 ? 2 : 1;
1295 uinfo->value.integer.min = 0;
1296 uinfo->value.integer.max = 1;
1297 return 0;
1298}
1299
Takashi Iwai0ba21762007-04-16 11:29:14 +02001300int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
1301 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302{
1303 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1304 hda_nid_t nid = get_amp_nid(kcontrol);
1305 int chs = get_amp_channels(kcontrol);
1306 int dir = get_amp_direction(kcontrol);
1307 int idx = get_amp_index(kcontrol);
1308 long *valp = ucontrol->value.integer.value;
1309
1310 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02001311 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02001312 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02001314 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02001315 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 return 0;
1317}
1318
Takashi Iwai0ba21762007-04-16 11:29:14 +02001319int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
1320 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321{
1322 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1323 hda_nid_t nid = get_amp_nid(kcontrol);
1324 int chs = get_amp_channels(kcontrol);
1325 int dir = get_amp_direction(kcontrol);
1326 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 long *valp = ucontrol->value.integer.value;
1328 int change = 0;
1329
Takashi Iwaicb53c622007-08-10 17:21:45 +02001330 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001331 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001332 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02001333 HDA_AMP_MUTE,
1334 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001335 valp++;
1336 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001337 if (chs & 2)
1338 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02001339 HDA_AMP_MUTE,
1340 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001341#ifdef CONFIG_SND_HDA_POWER_SAVE
1342 if (codec->patch_ops.check_power_status)
1343 codec->patch_ops.check_power_status(codec, nid);
1344#endif
1345 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 return change;
1347}
1348
1349/*
Takashi Iwai985be542005-11-02 18:26:49 +01001350 * bound volume controls
1351 *
1352 * bind multiple volumes (# indices, from 0)
1353 */
1354
1355#define AMP_VAL_IDX_SHIFT 19
1356#define AMP_VAL_IDX_MASK (0x0f<<19)
1357
Takashi Iwai0ba21762007-04-16 11:29:14 +02001358int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
1359 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01001360{
1361 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1362 unsigned long pval;
1363 int err;
1364
Ingo Molnar62932df2006-01-16 16:34:20 +01001365 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Takashi Iwai985be542005-11-02 18:26:49 +01001366 pval = kcontrol->private_value;
1367 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
1368 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
1369 kcontrol->private_value = pval;
Ingo Molnar62932df2006-01-16 16:34:20 +01001370 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01001371 return err;
1372}
1373
Takashi Iwai0ba21762007-04-16 11:29:14 +02001374int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
1375 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01001376{
1377 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1378 unsigned long pval;
1379 int i, indices, err = 0, change = 0;
1380
Ingo Molnar62932df2006-01-16 16:34:20 +01001381 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Takashi Iwai985be542005-11-02 18:26:49 +01001382 pval = kcontrol->private_value;
1383 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
1384 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001385 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
1386 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01001387 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1388 if (err < 0)
1389 break;
1390 change |= err;
1391 }
1392 kcontrol->private_value = pval;
Ingo Molnar62932df2006-01-16 16:34:20 +01001393 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01001394 return err < 0 ? err : change;
1395}
1396
1397/*
Takashi Iwai532d5382007-07-27 19:02:40 +02001398 * generic bound volume/swtich controls
1399 */
1400int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
1401 struct snd_ctl_elem_info *uinfo)
1402{
1403 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1404 struct hda_bind_ctls *c;
1405 int err;
1406
Takashi Iwai532d5382007-07-27 19:02:40 +02001407 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01001408 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02001409 kcontrol->private_value = *c->values;
1410 err = c->ops->info(kcontrol, uinfo);
1411 kcontrol->private_value = (long)c;
1412 mutex_unlock(&codec->spdif_mutex);
1413 return err;
1414}
1415
1416int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
1417 struct snd_ctl_elem_value *ucontrol)
1418{
1419 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1420 struct hda_bind_ctls *c;
1421 int err;
1422
Takashi Iwai532d5382007-07-27 19:02:40 +02001423 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01001424 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02001425 kcontrol->private_value = *c->values;
1426 err = c->ops->get(kcontrol, ucontrol);
1427 kcontrol->private_value = (long)c;
1428 mutex_unlock(&codec->spdif_mutex);
1429 return err;
1430}
1431
1432int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
1433 struct snd_ctl_elem_value *ucontrol)
1434{
1435 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1436 struct hda_bind_ctls *c;
1437 unsigned long *vals;
1438 int err = 0, change = 0;
1439
Takashi Iwai532d5382007-07-27 19:02:40 +02001440 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01001441 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02001442 for (vals = c->values; *vals; vals++) {
1443 kcontrol->private_value = *vals;
1444 err = c->ops->put(kcontrol, ucontrol);
1445 if (err < 0)
1446 break;
1447 change |= err;
1448 }
1449 kcontrol->private_value = (long)c;
1450 mutex_unlock(&codec->spdif_mutex);
1451 return err < 0 ? err : change;
1452}
1453
1454int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1455 unsigned int size, unsigned int __user *tlv)
1456{
1457 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1458 struct hda_bind_ctls *c;
1459 int err;
1460
Takashi Iwai532d5382007-07-27 19:02:40 +02001461 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01001462 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02001463 kcontrol->private_value = *c->values;
1464 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
1465 kcontrol->private_value = (long)c;
1466 mutex_unlock(&codec->spdif_mutex);
1467 return err;
1468}
1469
1470struct hda_ctl_ops snd_hda_bind_vol = {
1471 .info = snd_hda_mixer_amp_volume_info,
1472 .get = snd_hda_mixer_amp_volume_get,
1473 .put = snd_hda_mixer_amp_volume_put,
1474 .tlv = snd_hda_mixer_amp_tlv
1475};
1476
1477struct hda_ctl_ops snd_hda_bind_sw = {
1478 .info = snd_hda_mixer_amp_switch_info,
1479 .get = snd_hda_mixer_amp_switch_get,
1480 .put = snd_hda_mixer_amp_switch_put,
1481 .tlv = snd_hda_mixer_amp_tlv
1482};
1483
1484/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 * SPDIF out controls
1486 */
1487
Takashi Iwai0ba21762007-04-16 11:29:14 +02001488static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
1489 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490{
1491 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1492 uinfo->count = 1;
1493 return 0;
1494}
1495
Takashi Iwai0ba21762007-04-16 11:29:14 +02001496static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
1497 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498{
1499 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
1500 IEC958_AES0_NONAUDIO |
1501 IEC958_AES0_CON_EMPHASIS_5015 |
1502 IEC958_AES0_CON_NOT_COPYRIGHT;
1503 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
1504 IEC958_AES1_CON_ORIGINAL;
1505 return 0;
1506}
1507
Takashi Iwai0ba21762007-04-16 11:29:14 +02001508static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
1509 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510{
1511 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
1512 IEC958_AES0_NONAUDIO |
1513 IEC958_AES0_PRO_EMPHASIS_5015;
1514 return 0;
1515}
1516
Takashi Iwai0ba21762007-04-16 11:29:14 +02001517static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
1518 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519{
1520 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1521
1522 ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
1523 ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
1524 ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
1525 ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
1526
1527 return 0;
1528}
1529
1530/* convert from SPDIF status bits to HDA SPDIF bits
1531 * bit 0 (DigEn) is always set zero (to be filled later)
1532 */
1533static unsigned short convert_from_spdif_status(unsigned int sbits)
1534{
1535 unsigned short val = 0;
1536
1537 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02001538 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02001540 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001542 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
1543 IEC958_AES0_PRO_EMPHASIS_5015)
1544 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001546 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
1547 IEC958_AES0_CON_EMPHASIS_5015)
1548 val |= AC_DIG1_EMPHASIS;
1549 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
1550 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02001552 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
1554 }
1555 return val;
1556}
1557
1558/* convert to SPDIF status bits from HDA SPDIF bits
1559 */
1560static unsigned int convert_to_spdif_status(unsigned short val)
1561{
1562 unsigned int sbits = 0;
1563
Takashi Iwai0ba21762007-04-16 11:29:14 +02001564 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001566 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 sbits |= IEC958_AES0_PROFESSIONAL;
1568 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001569 if (sbits & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
1571 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001572 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001574 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001576 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
1578 sbits |= val & (0x7f << 8);
1579 }
1580 return sbits;
1581}
1582
Takashi Iwai2f728532008-09-25 16:32:41 +02001583/* set digital convert verbs both for the given NID and its slaves */
1584static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
1585 int verb, int val)
1586{
1587 hda_nid_t *d;
1588
Takashi Iwai9e976972008-11-25 08:17:20 +01001589 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02001590 d = codec->slave_dig_outs;
1591 if (!d)
1592 return;
1593 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01001594 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02001595}
1596
1597static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
1598 int dig1, int dig2)
1599{
1600 if (dig1 != -1)
1601 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
1602 if (dig2 != -1)
1603 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
1604}
1605
Takashi Iwai0ba21762007-04-16 11:29:14 +02001606static int snd_hda_spdif_default_put(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 hda_nid_t nid = kcontrol->private_value;
1611 unsigned short val;
1612 int change;
1613
Ingo Molnar62932df2006-01-16 16:34:20 +01001614 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 codec->spdif_status = ucontrol->value.iec958.status[0] |
1616 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
1617 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
1618 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
1619 val = convert_from_spdif_status(codec->spdif_status);
1620 val |= codec->spdif_ctls & 1;
1621 change = codec->spdif_ctls != val;
1622 codec->spdif_ctls = val;
1623
Takashi Iwai2f728532008-09-25 16:32:41 +02001624 if (change)
1625 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626
Ingo Molnar62932df2006-01-16 16:34:20 +01001627 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 return change;
1629}
1630
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001631#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
Takashi Iwai0ba21762007-04-16 11:29:14 +02001633static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
1634 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635{
1636 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1637
Takashi Iwai0ba21762007-04-16 11:29:14 +02001638 ucontrol->value.integer.value[0] = codec->spdif_ctls & AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 return 0;
1640}
1641
Takashi Iwai0ba21762007-04-16 11:29:14 +02001642static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
1643 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644{
1645 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1646 hda_nid_t nid = kcontrol->private_value;
1647 unsigned short val;
1648 int change;
1649
Ingo Molnar62932df2006-01-16 16:34:20 +01001650 mutex_lock(&codec->spdif_mutex);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001651 val = codec->spdif_ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02001653 val |= AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 change = codec->spdif_ctls != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001655 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 codec->spdif_ctls = val;
Takashi Iwai2f728532008-09-25 16:32:41 +02001657 set_dig_out_convert(codec, nid, val & 0xff, -1);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001658 /* unmute amp switch (if any) */
1659 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
Takashi Iwai47fd8302007-08-10 17:11:07 +02001660 (val & AC_DIG1_ENABLE))
1661 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
1662 HDA_AMP_MUTE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 }
Ingo Molnar62932df2006-01-16 16:34:20 +01001664 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 return change;
1666}
1667
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001668static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 {
1670 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1671 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1672 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
1673 .info = snd_hda_spdif_mask_info,
1674 .get = snd_hda_spdif_cmask_get,
1675 },
1676 {
1677 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1678 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1679 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
1680 .info = snd_hda_spdif_mask_info,
1681 .get = snd_hda_spdif_pmask_get,
1682 },
1683 {
1684 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1685 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1686 .info = snd_hda_spdif_mask_info,
1687 .get = snd_hda_spdif_default_get,
1688 .put = snd_hda_spdif_default_put,
1689 },
1690 {
1691 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1692 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
1693 .info = snd_hda_spdif_out_switch_info,
1694 .get = snd_hda_spdif_out_switch_get,
1695 .put = snd_hda_spdif_out_switch_put,
1696 },
1697 { } /* end */
1698};
1699
Takashi Iwai09f99702008-02-04 12:31:13 +01001700#define SPDIF_MAX_IDX 4 /* 4 instances should be enough to probe */
1701
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702/**
1703 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
1704 * @codec: the HDA codec
1705 * @nid: audio out widget NID
1706 *
1707 * Creates controls related with the SPDIF output.
1708 * Called from each patch supporting the SPDIF out.
1709 *
1710 * Returns 0 if successful, or a negative error code.
1711 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02001712int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713{
1714 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001715 struct snd_kcontrol *kctl;
1716 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01001717 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
Takashi Iwai09f99702008-02-04 12:31:13 +01001719 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
1720 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Playback Switch",
1721 idx))
1722 break;
1723 }
1724 if (idx >= SPDIF_MAX_IDX) {
1725 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
1726 return -EBUSY;
1727 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
1729 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01001730 if (!kctl)
1731 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01001732 kctl->id.index = idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 kctl->private_value = nid;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001734 err = snd_hda_ctl_add(codec, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001735 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 return err;
1737 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02001738 codec->spdif_ctls =
Andrew Paprocki3982d172007-12-19 12:13:44 +01001739 snd_hda_codec_read(codec, nid, 0,
1740 AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
1742 return 0;
1743}
1744
1745/*
Takashi Iwai9a081602008-02-12 18:37:26 +01001746 * SPDIF sharing with analog output
1747 */
1748static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
1749 struct snd_ctl_elem_value *ucontrol)
1750{
1751 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
1752 ucontrol->value.integer.value[0] = mout->share_spdif;
1753 return 0;
1754}
1755
1756static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
1757 struct snd_ctl_elem_value *ucontrol)
1758{
1759 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
1760 mout->share_spdif = !!ucontrol->value.integer.value[0];
1761 return 0;
1762}
1763
1764static struct snd_kcontrol_new spdif_share_sw = {
1765 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1766 .name = "IEC958 Default PCM Playback Switch",
1767 .info = snd_ctl_boolean_mono_info,
1768 .get = spdif_share_sw_get,
1769 .put = spdif_share_sw_put,
1770};
1771
1772int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
1773 struct hda_multi_out *mout)
1774{
1775 if (!mout->dig_out_nid)
1776 return 0;
1777 /* ATTENTION: here mout is passed as private_data, instead of codec */
Takashi Iwaid13bd412008-07-30 15:01:45 +02001778 return snd_hda_ctl_add(codec,
Takashi Iwai9a081602008-02-12 18:37:26 +01001779 snd_ctl_new1(&spdif_share_sw, mout));
1780}
1781
1782/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 * SPDIF input
1784 */
1785
1786#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
1787
Takashi Iwai0ba21762007-04-16 11:29:14 +02001788static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
1789 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790{
1791 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1792
1793 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
1794 return 0;
1795}
1796
Takashi Iwai0ba21762007-04-16 11:29:14 +02001797static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
1798 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799{
1800 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1801 hda_nid_t nid = kcontrol->private_value;
1802 unsigned int val = !!ucontrol->value.integer.value[0];
1803 int change;
1804
Ingo Molnar62932df2006-01-16 16:34:20 +01001805 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001807 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001809 snd_hda_codec_write_cache(codec, nid, 0,
1810 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 }
Ingo Molnar62932df2006-01-16 16:34:20 +01001812 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 return change;
1814}
1815
Takashi Iwai0ba21762007-04-16 11:29:14 +02001816static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
1817 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818{
1819 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1820 hda_nid_t nid = kcontrol->private_value;
1821 unsigned short val;
1822 unsigned int sbits;
1823
Andrew Paprocki3982d172007-12-19 12:13:44 +01001824 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 sbits = convert_to_spdif_status(val);
1826 ucontrol->value.iec958.status[0] = sbits;
1827 ucontrol->value.iec958.status[1] = sbits >> 8;
1828 ucontrol->value.iec958.status[2] = sbits >> 16;
1829 ucontrol->value.iec958.status[3] = sbits >> 24;
1830 return 0;
1831}
1832
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001833static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 {
1835 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1836 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH),
1837 .info = snd_hda_spdif_in_switch_info,
1838 .get = snd_hda_spdif_in_switch_get,
1839 .put = snd_hda_spdif_in_switch_put,
1840 },
1841 {
1842 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1843 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1844 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT),
1845 .info = snd_hda_spdif_mask_info,
1846 .get = snd_hda_spdif_in_status_get,
1847 },
1848 { } /* end */
1849};
1850
1851/**
1852 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
1853 * @codec: the HDA codec
1854 * @nid: audio in widget NID
1855 *
1856 * Creates controls related with the SPDIF input.
1857 * Called from each patch supporting the SPDIF in.
1858 *
1859 * Returns 0 if successful, or a negative error code.
1860 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02001861int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862{
1863 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001864 struct snd_kcontrol *kctl;
1865 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01001866 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
Takashi Iwai09f99702008-02-04 12:31:13 +01001868 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
1869 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Capture Switch",
1870 idx))
1871 break;
1872 }
1873 if (idx >= SPDIF_MAX_IDX) {
1874 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
1875 return -EBUSY;
1876 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
1878 kctl = snd_ctl_new1(dig_mix, codec);
1879 kctl->private_value = nid;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001880 err = snd_hda_ctl_add(codec, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001881 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 return err;
1883 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02001884 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01001885 snd_hda_codec_read(codec, nid, 0,
1886 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02001887 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 return 0;
1889}
1890
Takashi Iwaicb53c622007-08-10 17:21:45 +02001891#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001892/*
1893 * command cache
1894 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001896/* build a 32bit cache key with the widget id and the command parameter */
1897#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
1898#define get_cmd_cache_nid(key) ((key) & 0xff)
1899#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
1900
1901/**
1902 * snd_hda_codec_write_cache - send a single command with caching
1903 * @codec: the HDA codec
1904 * @nid: NID to send the command
1905 * @direct: direct flag
1906 * @verb: the verb to send
1907 * @parm: the parameter for the verb
1908 *
1909 * Send a single command without waiting for response.
1910 *
1911 * Returns 0 if successful, or a negative error code.
1912 */
1913int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
1914 int direct, unsigned int verb, unsigned int parm)
1915{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01001916 struct hda_bus *bus = codec->bus;
1917 unsigned int res;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001918 int err;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01001919
1920 res = make_codec_cmd(codec, nid, direct, verb, parm);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001921 snd_hda_power_up(codec);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01001922 mutex_lock(&bus->cmd_mutex);
1923 err = bus->ops.command(bus, res);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001924 if (!err) {
1925 struct hda_cache_head *c;
1926 u32 key = build_cmd_cache_key(nid, verb);
1927 c = get_alloc_hash(&codec->cmd_cache, key);
1928 if (c)
1929 c->val = parm;
1930 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01001931 mutex_unlock(&bus->cmd_mutex);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001932 snd_hda_power_down(codec);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001933 return err;
1934}
1935
1936/* resume the all commands from the cache */
1937void snd_hda_codec_resume_cache(struct hda_codec *codec)
1938{
Takashi Iwai603c4012008-07-30 15:01:44 +02001939 struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001940 int i;
1941
Takashi Iwai603c4012008-07-30 15:01:44 +02001942 for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001943 u32 key = buffer->key;
1944 if (!key)
1945 continue;
1946 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
1947 get_cmd_cache_cmd(key), buffer->val);
1948 }
1949}
1950
1951/**
1952 * snd_hda_sequence_write_cache - sequence writes with caching
1953 * @codec: the HDA codec
1954 * @seq: VERB array to send
1955 *
1956 * Send the commands sequentially from the given array.
1957 * Thte commands are recorded on cache for power-save and resume.
1958 * The array must be terminated with NID=0.
1959 */
1960void snd_hda_sequence_write_cache(struct hda_codec *codec,
1961 const struct hda_verb *seq)
1962{
1963 for (; seq->nid; seq++)
1964 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
1965 seq->param);
1966}
Takashi Iwaicb53c622007-08-10 17:21:45 +02001967#endif /* SND_HDA_NEEDS_RESUME */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001968
Takashi Iwai54d17402005-11-21 16:33:22 +01001969/*
1970 * set power state of the codec
1971 */
1972static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1973 unsigned int power_state)
1974{
Takashi Iwaicb53c622007-08-10 17:21:45 +02001975 hda_nid_t nid;
1976 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01001977
1978 snd_hda_codec_write(codec, fg, 0, AC_VERB_SET_POWER_STATE,
1979 power_state);
Marc Boucherd2595d82008-01-22 15:23:30 +01001980 msleep(10); /* partial workaround for "azx_get_response timeout" */
Takashi Iwai54d17402005-11-21 16:33:22 +01001981
Takashi Iwaicb53c622007-08-10 17:21:45 +02001982 nid = codec->start_nid;
1983 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01001984 unsigned int wcaps = get_wcaps(codec, nid);
1985 if (wcaps & AC_WCAP_POWER) {
1986 unsigned int wid_type = (wcaps & AC_WCAP_TYPE) >>
1987 AC_WCAP_TYPE_SHIFT;
1988 if (wid_type == AC_WID_PIN) {
1989 unsigned int pincap;
1990 /*
1991 * don't power down the widget if it controls
1992 * eapd and EAPD_BTLENABLE is set.
1993 */
1994 pincap = snd_hda_param_read(codec, nid,
1995 AC_PAR_PIN_CAP);
1996 if (pincap & AC_PINCAP_EAPD) {
1997 int eapd = snd_hda_codec_read(codec,
1998 nid, 0,
1999 AC_VERB_GET_EAPD_BTLENABLE, 0);
2000 eapd &= 0x02;
2001 if (power_state == AC_PWRST_D3 && eapd)
2002 continue;
2003 }
Takashi Iwai1194b5b2007-10-10 10:04:26 +02002004 }
Takashi Iwai54d17402005-11-21 16:33:22 +01002005 snd_hda_codec_write(codec, nid, 0,
2006 AC_VERB_SET_POWER_STATE,
2007 power_state);
Takashi Iwai1194b5b2007-10-10 10:04:26 +02002008 }
Takashi Iwai54d17402005-11-21 16:33:22 +01002009 }
2010
Takashi Iwaicb53c622007-08-10 17:21:45 +02002011 if (power_state == AC_PWRST_D0) {
2012 unsigned long end_time;
2013 int state;
Takashi Iwai54d17402005-11-21 16:33:22 +01002014 msleep(10);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002015 /* wait until the codec reachs to D0 */
2016 end_time = jiffies + msecs_to_jiffies(500);
2017 do {
2018 state = snd_hda_codec_read(codec, fg, 0,
2019 AC_VERB_GET_POWER_STATE, 0);
2020 if (state == power_state)
2021 break;
2022 msleep(1);
2023 } while (time_after_eq(end_time, jiffies));
2024 }
Takashi Iwai54d17402005-11-21 16:33:22 +01002025}
2026
Takashi Iwai11aeff02008-07-30 15:01:46 +02002027#ifdef CONFIG_SND_HDA_HWDEP
2028/* execute additional init verbs */
2029static void hda_exec_init_verbs(struct hda_codec *codec)
2030{
2031 if (codec->init_verbs.list)
2032 snd_hda_sequence_write(codec, codec->init_verbs.list);
2033}
2034#else
2035static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
2036#endif
2037
Takashi Iwaicb53c622007-08-10 17:21:45 +02002038#ifdef SND_HDA_NEEDS_RESUME
2039/*
2040 * call suspend and power-down; used both from PM and power-save
2041 */
2042static void hda_call_codec_suspend(struct hda_codec *codec)
2043{
2044 if (codec->patch_ops.suspend)
2045 codec->patch_ops.suspend(codec, PMSG_SUSPEND);
2046 hda_set_power_state(codec,
2047 codec->afg ? codec->afg : codec->mfg,
2048 AC_PWRST_D3);
2049#ifdef CONFIG_SND_HDA_POWER_SAVE
2050 cancel_delayed_work(&codec->power_work);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02002051 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02002052 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002053#endif
2054}
2055
2056/*
2057 * kick up codec; used both from PM and power-save
2058 */
2059static void hda_call_codec_resume(struct hda_codec *codec)
2060{
2061 hda_set_power_state(codec,
2062 codec->afg ? codec->afg : codec->mfg,
2063 AC_PWRST_D0);
Takashi Iwai11aeff02008-07-30 15:01:46 +02002064 hda_exec_init_verbs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002065 if (codec->patch_ops.resume)
2066 codec->patch_ops.resume(codec);
2067 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02002068 if (codec->patch_ops.init)
2069 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002070 snd_hda_codec_resume_amp(codec);
2071 snd_hda_codec_resume_cache(codec);
2072 }
2073}
2074#endif /* SND_HDA_NEEDS_RESUME */
2075
Takashi Iwai54d17402005-11-21 16:33:22 +01002076
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077/**
2078 * snd_hda_build_controls - build mixer controls
2079 * @bus: the BUS
2080 *
2081 * Creates mixer controls for each codec included in the bus.
2082 *
2083 * Returns 0 if successful, otherwise a negative error code.
2084 */
Takashi Iwai756e2b02007-04-16 11:27:07 +02002085int __devinit snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086{
Takashi Iwai0ba21762007-04-16 11:29:14 +02002087 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088
Takashi Iwai0ba21762007-04-16 11:29:14 +02002089 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002090 int err = snd_hda_codec_build_controls(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 if (err < 0)
2092 return err;
2093 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002094 return 0;
2095}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002097int snd_hda_codec_build_controls(struct hda_codec *codec)
2098{
2099 int err = 0;
2100 /* fake as if already powered-on */
2101 hda_keep_power_on(codec);
2102 /* then fire up */
2103 hda_set_power_state(codec,
2104 codec->afg ? codec->afg : codec->mfg,
2105 AC_PWRST_D0);
Takashi Iwai11aeff02008-07-30 15:01:46 +02002106 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002107 /* continue to initialize... */
2108 if (codec->patch_ops.init)
2109 err = codec->patch_ops.init(codec);
2110 if (!err && codec->patch_ops.build_controls)
2111 err = codec->patch_ops.build_controls(codec);
2112 snd_hda_power_down(codec);
2113 if (err < 0)
2114 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 return 0;
2116}
2117
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118/*
2119 * stream formats
2120 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02002121struct hda_rate_tbl {
2122 unsigned int hz;
2123 unsigned int alsa_bits;
2124 unsigned int hda_fmt;
2125};
2126
2127static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02002129
2130 /* autodetected value used in snd_hda_query_supported_pcm */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 { 8000, SNDRV_PCM_RATE_8000, 0x0500 }, /* 1/6 x 48 */
2132 { 11025, SNDRV_PCM_RATE_11025, 0x4300 }, /* 1/4 x 44 */
2133 { 16000, SNDRV_PCM_RATE_16000, 0x0200 }, /* 1/3 x 48 */
2134 { 22050, SNDRV_PCM_RATE_22050, 0x4100 }, /* 1/2 x 44 */
2135 { 32000, SNDRV_PCM_RATE_32000, 0x0a00 }, /* 2/3 x 48 */
2136 { 44100, SNDRV_PCM_RATE_44100, 0x4000 }, /* 44 */
2137 { 48000, SNDRV_PCM_RATE_48000, 0x0000 }, /* 48 */
2138 { 88200, SNDRV_PCM_RATE_88200, 0x4800 }, /* 2 x 44 */
2139 { 96000, SNDRV_PCM_RATE_96000, 0x0800 }, /* 2 x 48 */
2140 { 176400, SNDRV_PCM_RATE_176400, 0x5800 },/* 4 x 44 */
2141 { 192000, SNDRV_PCM_RATE_192000, 0x1800 }, /* 4 x 48 */
Takashi Iwaia961f9f2007-04-12 13:08:09 +02002142#define AC_PAR_PCM_RATE_BITS 11
2143 /* up to bits 10, 384kHZ isn't supported properly */
2144
2145 /* not autodetected value */
2146 { 9600, SNDRV_PCM_RATE_KNOT, 0x0400 }, /* 1/5 x 48 */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02002147
Takashi Iwaibefdf312005-08-22 13:57:55 +02002148 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149};
2150
2151/**
2152 * snd_hda_calc_stream_format - calculate format bitset
2153 * @rate: the sample rate
2154 * @channels: the number of channels
2155 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
2156 * @maxbps: the max. bps
2157 *
2158 * Calculate the format bitset from the given rate, channels and th PCM format.
2159 *
2160 * Return zero if invalid.
2161 */
2162unsigned int snd_hda_calc_stream_format(unsigned int rate,
2163 unsigned int channels,
2164 unsigned int format,
2165 unsigned int maxbps)
2166{
2167 int i;
2168 unsigned int val = 0;
2169
Takashi Iwaibefdf312005-08-22 13:57:55 +02002170 for (i = 0; rate_bits[i].hz; i++)
2171 if (rate_bits[i].hz == rate) {
2172 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 break;
2174 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002175 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 snd_printdd("invalid rate %d\n", rate);
2177 return 0;
2178 }
2179
2180 if (channels == 0 || channels > 8) {
2181 snd_printdd("invalid channels %d\n", channels);
2182 return 0;
2183 }
2184 val |= channels - 1;
2185
2186 switch (snd_pcm_format_width(format)) {
2187 case 8: val |= 0x00; break;
2188 case 16: val |= 0x10; break;
2189 case 20:
2190 case 24:
2191 case 32:
2192 if (maxbps >= 32)
2193 val |= 0x40;
2194 else if (maxbps >= 24)
2195 val |= 0x30;
2196 else
2197 val |= 0x20;
2198 break;
2199 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002200 snd_printdd("invalid format width %d\n",
2201 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 return 0;
2203 }
2204
2205 return val;
2206}
2207
2208/**
2209 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
2210 * @codec: the HDA codec
2211 * @nid: NID to query
2212 * @ratesp: the pointer to store the detected rate bitflags
2213 * @formatsp: the pointer to store the detected formats
2214 * @bpsp: the pointer to store the detected format widths
2215 *
2216 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
2217 * or @bsps argument is ignored.
2218 *
2219 * Returns 0 if successful, otherwise a negative error code.
2220 */
Takashi Iwai986862bd2008-11-27 12:40:13 +01002221static int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
2223{
2224 int i;
2225 unsigned int val, streams;
2226
2227 val = 0;
2228 if (nid != codec->afg &&
Takashi Iwai54d17402005-11-21 16:33:22 +01002229 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
2231 if (val == -1)
2232 return -EIO;
2233 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002234 if (!val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
2236
2237 if (ratesp) {
2238 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02002239 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02002241 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 }
2243 *ratesp = rates;
2244 }
2245
2246 if (formatsp || bpsp) {
2247 u64 formats = 0;
2248 unsigned int bps;
2249 unsigned int wcaps;
2250
Takashi Iwai54d17402005-11-21 16:33:22 +01002251 wcaps = get_wcaps(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
2253 if (streams == -1)
2254 return -EIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002255 if (!streams) {
2256 streams = snd_hda_param_read(codec, codec->afg,
2257 AC_PAR_STREAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 if (streams == -1)
2259 return -EIO;
2260 }
2261
2262 bps = 0;
2263 if (streams & AC_SUPFMT_PCM) {
2264 if (val & AC_SUPPCM_BITS_8) {
2265 formats |= SNDRV_PCM_FMTBIT_U8;
2266 bps = 8;
2267 }
2268 if (val & AC_SUPPCM_BITS_16) {
2269 formats |= SNDRV_PCM_FMTBIT_S16_LE;
2270 bps = 16;
2271 }
2272 if (wcaps & AC_WCAP_DIGITAL) {
2273 if (val & AC_SUPPCM_BITS_32)
2274 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
2275 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
2276 formats |= SNDRV_PCM_FMTBIT_S32_LE;
2277 if (val & AC_SUPPCM_BITS_24)
2278 bps = 24;
2279 else if (val & AC_SUPPCM_BITS_20)
2280 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002281 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
2282 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 formats |= SNDRV_PCM_FMTBIT_S32_LE;
2284 if (val & AC_SUPPCM_BITS_32)
2285 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 else if (val & AC_SUPPCM_BITS_24)
2287 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02002288 else if (val & AC_SUPPCM_BITS_20)
2289 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 }
2291 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002292 else if (streams == AC_SUPFMT_FLOAT32) {
2293 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
2295 bps = 32;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002296 } else if (streams == AC_SUPFMT_AC3) {
2297 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 /* temporary hack: we have still no proper support
2299 * for the direct AC3 stream...
2300 */
2301 formats |= SNDRV_PCM_FMTBIT_U8;
2302 bps = 8;
2303 }
2304 if (formatsp)
2305 *formatsp = formats;
2306 if (bpsp)
2307 *bpsp = bps;
2308 }
2309
2310 return 0;
2311}
2312
2313/**
Takashi Iwai0ba21762007-04-16 11:29:14 +02002314 * snd_hda_is_supported_format - check whether the given node supports
2315 * the format val
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 *
2317 * Returns 1 if supported, 0 if not.
2318 */
2319int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
2320 unsigned int format)
2321{
2322 int i;
2323 unsigned int val = 0, rate, stream;
2324
2325 if (nid != codec->afg &&
Takashi Iwai54d17402005-11-21 16:33:22 +01002326 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
2328 if (val == -1)
2329 return 0;
2330 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002331 if (!val) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
2333 if (val == -1)
2334 return 0;
2335 }
2336
2337 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02002338 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02002339 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 if (val & (1 << i))
2341 break;
2342 return 0;
2343 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02002344 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 return 0;
2346
2347 stream = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
2348 if (stream == -1)
2349 return 0;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002350 if (!stream && nid != codec->afg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 stream = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002352 if (!stream || stream == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 return 0;
2354
2355 if (stream & AC_SUPFMT_PCM) {
2356 switch (format & 0xf0) {
2357 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002358 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 return 0;
2360 break;
2361 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002362 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 return 0;
2364 break;
2365 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002366 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 return 0;
2368 break;
2369 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002370 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 return 0;
2372 break;
2373 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002374 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 return 0;
2376 break;
2377 default:
2378 return 0;
2379 }
2380 } else {
2381 /* FIXME: check for float32 and AC3? */
2382 }
2383
2384 return 1;
2385}
2386
2387/*
2388 * PCM stuff
2389 */
2390static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
2391 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002392 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393{
2394 return 0;
2395}
2396
2397static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
2398 struct hda_codec *codec,
2399 unsigned int stream_tag,
2400 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002401 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402{
2403 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
2404 return 0;
2405}
2406
2407static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
2408 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002409 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410{
Takashi Iwai888afa12008-03-18 09:57:50 +01002411 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 return 0;
2413}
2414
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002415static int set_pcm_default_values(struct hda_codec *codec,
2416 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417{
Takashi Iwai0ba21762007-04-16 11:29:14 +02002418 /* query support PCM information from the given NID */
2419 if (info->nid && (!info->rates || !info->formats)) {
2420 snd_hda_query_supported_pcm(codec, info->nid,
2421 info->rates ? NULL : &info->rates,
2422 info->formats ? NULL : &info->formats,
2423 info->maxbps ? NULL : &info->maxbps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 }
2425 if (info->ops.open == NULL)
2426 info->ops.open = hda_pcm_default_open_close;
2427 if (info->ops.close == NULL)
2428 info->ops.close = hda_pcm_default_open_close;
2429 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02002430 if (snd_BUG_ON(!info->nid))
2431 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 info->ops.prepare = hda_pcm_default_prepare;
2433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02002435 if (snd_BUG_ON(!info->nid))
2436 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 info->ops.cleanup = hda_pcm_default_cleanup;
2438 }
2439 return 0;
2440}
2441
Takashi Iwai176d5332008-07-30 15:01:44 +02002442/*
2443 * attach a new PCM stream
2444 */
2445static int __devinit
2446snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
2447{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01002448 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02002449 struct hda_pcm_stream *info;
2450 int stream, err;
2451
Takashi Iwaib91f0802008-11-04 08:43:08 +01002452 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02002453 return -EINVAL;
2454 for (stream = 0; stream < 2; stream++) {
2455 info = &pcm->stream[stream];
2456 if (info->substreams) {
2457 err = set_pcm_default_values(codec, info);
2458 if (err < 0)
2459 return err;
2460 }
2461 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01002462 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02002463}
2464
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465/**
2466 * snd_hda_build_pcms - build PCM information
2467 * @bus: the BUS
2468 *
2469 * Create PCM information for each codec included in the bus.
2470 *
2471 * The build_pcms codec patch is requested to set up codec->num_pcms and
2472 * codec->pcm_info properly. The array is referred by the top-level driver
2473 * to create its PCM instances.
2474 * The allocated codec->pcm_info should be released in codec->patch_ops.free
2475 * callback.
2476 *
2477 * At least, substreams, channels_min and channels_max must be filled for
2478 * each stream. substreams = 0 indicates that the stream doesn't exist.
2479 * When rates and/or formats are zero, the supported values are queried
2480 * from the given nid. The nid is used also by the default ops.prepare
2481 * and ops.cleanup callbacks.
2482 *
2483 * The driver needs to call ops.open in its open callback. Similarly,
2484 * ops.close is supposed to be called in the close callback.
2485 * ops.prepare should be called in the prepare or hw_params callback
2486 * with the proper parameters for set up.
2487 * ops.cleanup should be called in hw_free for clean up of streams.
2488 *
2489 * This function returns 0 if successfull, or a negative error code.
2490 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002491int snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492{
Takashi Iwai176d5332008-07-30 15:01:44 +02002493 static const char *dev_name[HDA_PCM_NTYPES] = {
2494 "Audio", "SPDIF", "HDMI", "Modem"
2495 };
2496 /* starting device index for each PCM type */
2497 static int dev_idx[HDA_PCM_NTYPES] = {
2498 [HDA_PCM_TYPE_AUDIO] = 0,
2499 [HDA_PCM_TYPE_SPDIF] = 1,
2500 [HDA_PCM_TYPE_HDMI] = 3,
2501 [HDA_PCM_TYPE_MODEM] = 6
2502 };
2503 /* normal audio device indices; not linear to keep compatibility */
2504 static int audio_idx[4] = { 0, 2, 4, 5 };
Takashi Iwai0ba21762007-04-16 11:29:14 +02002505 struct hda_codec *codec;
Takashi Iwai176d5332008-07-30 15:01:44 +02002506 int num_devs[HDA_PCM_NTYPES];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507
Takashi Iwai176d5332008-07-30 15:01:44 +02002508 memset(num_devs, 0, sizeof(num_devs));
Takashi Iwai0ba21762007-04-16 11:29:14 +02002509 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai176d5332008-07-30 15:01:44 +02002510 unsigned int pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 int err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002512 if (!codec->num_pcms) {
2513 if (!codec->patch_ops.build_pcms)
2514 continue;
2515 err = codec->patch_ops.build_pcms(codec);
2516 if (err < 0)
2517 return err;
2518 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
Takashi Iwai176d5332008-07-30 15:01:44 +02002520 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2521 int type = cpcm->pcm_type;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002522 int dev;
Takashi Iwaibfc5c262008-11-04 17:48:39 +01002523
2524 if (!cpcm->stream[0].substreams &&
2525 !cpcm->stream[1].substreams)
2526 continue; /* no substreams assigned */
2527
Takashi Iwai176d5332008-07-30 15:01:44 +02002528 switch (type) {
2529 case HDA_PCM_TYPE_AUDIO:
2530 if (num_devs[type] >= ARRAY_SIZE(audio_idx)) {
2531 snd_printk(KERN_WARNING
2532 "Too many audio devices\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 continue;
Takashi Iwai176d5332008-07-30 15:01:44 +02002534 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002535 dev = audio_idx[num_devs[type]];
Takashi Iwai176d5332008-07-30 15:01:44 +02002536 break;
2537 case HDA_PCM_TYPE_SPDIF:
2538 case HDA_PCM_TYPE_HDMI:
2539 case HDA_PCM_TYPE_MODEM:
2540 if (num_devs[type]) {
2541 snd_printk(KERN_WARNING
2542 "%s already defined\n",
2543 dev_name[type]);
2544 continue;
2545 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002546 dev = dev_idx[type];
Takashi Iwai176d5332008-07-30 15:01:44 +02002547 break;
2548 default:
2549 snd_printk(KERN_WARNING
2550 "Invalid PCM type %d\n", type);
2551 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 }
Takashi Iwai176d5332008-07-30 15:01:44 +02002553 num_devs[type]++;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002554 if (!cpcm->pcm) {
2555 cpcm->device = dev;
2556 err = snd_hda_attach_pcm(codec, cpcm);
2557 if (err < 0)
2558 return err;
2559 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 }
2561 }
2562 return 0;
2563}
2564
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565/**
2566 * snd_hda_check_board_config - compare the current codec with the config table
2567 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002568 * @num_configs: number of config enums
2569 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 * @tbl: configuration table, terminated by null entries
2571 *
2572 * Compares the modelname or PCI subsystem id of the current codec with the
2573 * given configuration table. If a matching entry is found, returns its
2574 * config value (supposed to be 0 or positive).
2575 *
2576 * If no entries are matching, the function returns a negative value.
2577 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002578int snd_hda_check_board_config(struct hda_codec *codec,
2579 int num_configs, const char **models,
2580 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581{
Takashi Iwaif44ac832008-07-30 15:01:45 +02002582 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002583 int i;
2584 for (i = 0; i < num_configs; i++) {
2585 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02002586 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002587 snd_printd(KERN_INFO "hda_codec: model '%s' is "
2588 "selected\n", models[i]);
2589 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 }
2591 }
2592 }
2593
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002594 if (!codec->bus->pci || !tbl)
2595 return -1;
2596
2597 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
2598 if (!tbl)
2599 return -1;
2600 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02002601#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002602 char tmp[10];
2603 const char *model = NULL;
2604 if (models)
2605 model = models[tbl->value];
2606 if (!model) {
2607 sprintf(tmp, "#%d", tbl->value);
2608 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002610 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
2611 "for config %x:%x (%s)\n",
2612 model, tbl->subvendor, tbl->subdevice,
2613 (tbl->name ? tbl->name : "Unknown device"));
2614#endif
2615 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 }
2617 return -1;
2618}
2619
2620/**
2621 * snd_hda_add_new_ctls - create controls from the array
2622 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002623 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 *
2625 * This helper function creates and add new controls in the given array.
2626 * The array must be terminated with an empty entry as terminator.
2627 *
2628 * Returns 0 if successful, or a negative error code.
2629 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002630int snd_hda_add_new_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631{
Takashi Iwaicb53c622007-08-10 17:21:45 +02002632 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633
2634 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01002635 struct snd_kcontrol *kctl;
2636 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002637 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01002638 return -ENOMEM;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002639 err = snd_hda_ctl_add(codec, kctl);
Takashi Iwai54d17402005-11-21 16:33:22 +01002640 if (err < 0) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002641 if (!codec->addr)
Takashi Iwai54d17402005-11-21 16:33:22 +01002642 return err;
2643 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002644 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01002645 return -ENOMEM;
2646 kctl->id.device = codec->addr;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002647 err = snd_hda_ctl_add(codec, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002648 if (err < 0)
Takashi Iwai54d17402005-11-21 16:33:22 +01002649 return err;
2650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 }
2652 return 0;
2653}
2654
Takashi Iwaicb53c622007-08-10 17:21:45 +02002655#ifdef CONFIG_SND_HDA_POWER_SAVE
2656static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2657 unsigned int power_state);
2658
2659static void hda_power_work(struct work_struct *work)
2660{
2661 struct hda_codec *codec =
2662 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01002663 struct hda_bus *bus = codec->bus;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002664
Maxim Levitsky2e492462007-09-03 15:26:57 +02002665 if (!codec->power_on || codec->power_count) {
2666 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002667 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02002668 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02002669
2670 hda_call_codec_suspend(codec);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01002671 if (bus->ops.pm_notify)
2672 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002673}
2674
2675static void hda_keep_power_on(struct hda_codec *codec)
2676{
2677 codec->power_count++;
2678 codec->power_on = 1;
2679}
2680
2681void snd_hda_power_up(struct hda_codec *codec)
2682{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01002683 struct hda_bus *bus = codec->bus;
2684
Takashi Iwaicb53c622007-08-10 17:21:45 +02002685 codec->power_count++;
Takashi Iwaia221e282007-08-16 16:35:33 +02002686 if (codec->power_on || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02002687 return;
2688
2689 codec->power_on = 1;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01002690 if (bus->ops.pm_notify)
2691 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002692 hda_call_codec_resume(codec);
2693 cancel_delayed_work(&codec->power_work);
Takashi Iwaia221e282007-08-16 16:35:33 +02002694 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002695}
2696
2697void snd_hda_power_down(struct hda_codec *codec)
2698{
2699 --codec->power_count;
Takashi Iwaia221e282007-08-16 16:35:33 +02002700 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02002701 return;
Takashi Iwaia221e282007-08-16 16:35:33 +02002702 if (power_save) {
2703 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002704 schedule_delayed_work(&codec->power_work,
2705 msecs_to_jiffies(power_save * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02002706 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02002707}
2708
2709int snd_hda_check_amp_list_power(struct hda_codec *codec,
2710 struct hda_loopback_check *check,
2711 hda_nid_t nid)
2712{
2713 struct hda_amp_list *p;
2714 int ch, v;
2715
2716 if (!check->amplist)
2717 return 0;
2718 for (p = check->amplist; p->nid; p++) {
2719 if (p->nid == nid)
2720 break;
2721 }
2722 if (!p->nid)
2723 return 0; /* nothing changed */
2724
2725 for (p = check->amplist; p->nid; p++) {
2726 for (ch = 0; ch < 2; ch++) {
2727 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
2728 p->idx);
2729 if (!(v & HDA_AMP_MUTE) && v > 0) {
2730 if (!check->power_on) {
2731 check->power_on = 1;
2732 snd_hda_power_up(codec);
2733 }
2734 return 1;
2735 }
2736 }
2737 }
2738 if (check->power_on) {
2739 check->power_on = 0;
2740 snd_hda_power_down(codec);
2741 }
2742 return 0;
2743}
2744#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002746/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002747 * Channel mode helper
2748 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002749int snd_hda_ch_mode_info(struct hda_codec *codec,
2750 struct snd_ctl_elem_info *uinfo,
2751 const struct hda_channel_mode *chmode,
2752 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002753{
2754 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2755 uinfo->count = 1;
2756 uinfo->value.enumerated.items = num_chmodes;
2757 if (uinfo->value.enumerated.item >= num_chmodes)
2758 uinfo->value.enumerated.item = num_chmodes - 1;
2759 sprintf(uinfo->value.enumerated.name, "%dch",
2760 chmode[uinfo->value.enumerated.item].channels);
2761 return 0;
2762}
2763
Takashi Iwai0ba21762007-04-16 11:29:14 +02002764int snd_hda_ch_mode_get(struct hda_codec *codec,
2765 struct snd_ctl_elem_value *ucontrol,
2766 const struct hda_channel_mode *chmode,
2767 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002768 int max_channels)
2769{
2770 int i;
2771
2772 for (i = 0; i < num_chmodes; i++) {
2773 if (max_channels == chmode[i].channels) {
2774 ucontrol->value.enumerated.item[0] = i;
2775 break;
2776 }
2777 }
2778 return 0;
2779}
2780
Takashi Iwai0ba21762007-04-16 11:29:14 +02002781int snd_hda_ch_mode_put(struct hda_codec *codec,
2782 struct snd_ctl_elem_value *ucontrol,
2783 const struct hda_channel_mode *chmode,
2784 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002785 int *max_channelsp)
2786{
2787 unsigned int mode;
2788
2789 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01002790 if (mode >= num_chmodes)
2791 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002792 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002793 return 0;
2794 /* change the current channel setting */
2795 *max_channelsp = chmode[mode].channels;
2796 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002797 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002798 return 1;
2799}
2800
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801/*
2802 * input MUX helper
2803 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002804int snd_hda_input_mux_info(const struct hda_input_mux *imux,
2805 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806{
2807 unsigned int index;
2808
2809 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2810 uinfo->count = 1;
2811 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02002812 if (!imux->num_items)
2813 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 index = uinfo->value.enumerated.item;
2815 if (index >= imux->num_items)
2816 index = imux->num_items - 1;
2817 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
2818 return 0;
2819}
2820
Takashi Iwai0ba21762007-04-16 11:29:14 +02002821int snd_hda_input_mux_put(struct hda_codec *codec,
2822 const struct hda_input_mux *imux,
2823 struct snd_ctl_elem_value *ucontrol,
2824 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 unsigned int *cur_val)
2826{
2827 unsigned int idx;
2828
Takashi Iwai5513b0c2007-10-09 11:58:41 +02002829 if (!imux->num_items)
2830 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 idx = ucontrol->value.enumerated.item[0];
2832 if (idx >= imux->num_items)
2833 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002834 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002836 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
2837 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 *cur_val = idx;
2839 return 1;
2840}
2841
2842
2843/*
2844 * Multi-channel / digital-out PCM helper functions
2845 */
2846
Takashi Iwai6b97eb42007-04-05 14:51:48 +02002847/* setup SPDIF output stream */
2848static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
2849 unsigned int stream_tag, unsigned int format)
2850{
2851 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Takashi Iwai2f728532008-09-25 16:32:41 +02002852 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
2853 set_dig_out_convert(codec, nid,
2854 codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff,
2855 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02002856 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02002857 if (codec->slave_dig_outs) {
2858 hda_nid_t *d;
2859 for (d = codec->slave_dig_outs; *d; d++)
2860 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
2861 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04002862 }
Takashi Iwai2f728532008-09-25 16:32:41 +02002863 /* turn on again (if needed) */
2864 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
2865 set_dig_out_convert(codec, nid,
2866 codec->spdif_ctls & 0xff, -1);
2867}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04002868
Takashi Iwai2f728532008-09-25 16:32:41 +02002869static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
2870{
2871 snd_hda_codec_cleanup_stream(codec, nid);
2872 if (codec->slave_dig_outs) {
2873 hda_nid_t *d;
2874 for (d = codec->slave_dig_outs; *d; d++)
2875 snd_hda_codec_cleanup_stream(codec, *d);
2876 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02002877}
2878
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879/*
2880 * open the digital out in the exclusive mode
2881 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002882int snd_hda_multi_out_dig_open(struct hda_codec *codec,
2883 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884{
Ingo Molnar62932df2006-01-16 16:34:20 +01002885 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02002886 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
2887 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02002888 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01002890 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 return 0;
2892}
2893
Takashi Iwai6b97eb42007-04-05 14:51:48 +02002894int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
2895 struct hda_multi_out *mout,
2896 unsigned int stream_tag,
2897 unsigned int format,
2898 struct snd_pcm_substream *substream)
2899{
2900 mutex_lock(&codec->spdif_mutex);
2901 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
2902 mutex_unlock(&codec->spdif_mutex);
2903 return 0;
2904}
2905
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906/*
2907 * release the digital out
2908 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002909int snd_hda_multi_out_dig_close(struct hda_codec *codec,
2910 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911{
Ingo Molnar62932df2006-01-16 16:34:20 +01002912 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01002914 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 return 0;
2916}
2917
2918/*
2919 * set up more restrictions for analog out
2920 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002921int snd_hda_multi_out_analog_open(struct hda_codec *codec,
2922 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01002923 struct snd_pcm_substream *substream,
2924 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925{
Takashi Iwai9a081602008-02-12 18:37:26 +01002926 struct snd_pcm_runtime *runtime = substream->runtime;
2927 runtime->hw.channels_max = mout->max_channels;
2928 if (mout->dig_out_nid) {
2929 if (!mout->analog_rates) {
2930 mout->analog_rates = hinfo->rates;
2931 mout->analog_formats = hinfo->formats;
2932 mout->analog_maxbps = hinfo->maxbps;
2933 } else {
2934 runtime->hw.rates = mout->analog_rates;
2935 runtime->hw.formats = mout->analog_formats;
2936 hinfo->maxbps = mout->analog_maxbps;
2937 }
2938 if (!mout->spdif_rates) {
2939 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
2940 &mout->spdif_rates,
2941 &mout->spdif_formats,
2942 &mout->spdif_maxbps);
2943 }
2944 mutex_lock(&codec->spdif_mutex);
2945 if (mout->share_spdif) {
2946 runtime->hw.rates &= mout->spdif_rates;
2947 runtime->hw.formats &= mout->spdif_formats;
2948 if (mout->spdif_maxbps < hinfo->maxbps)
2949 hinfo->maxbps = mout->spdif_maxbps;
2950 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02002951 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01002952 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 return snd_pcm_hw_constraint_step(substream->runtime, 0,
2954 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
2955}
2956
2957/*
2958 * set up the i/o for analog out
2959 * when the digital out is available, copy the front out to digital out, too.
2960 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002961int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
2962 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 unsigned int stream_tag,
2964 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002965 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966{
2967 hda_nid_t *nids = mout->dac_nids;
2968 int chs = substream->runtime->channels;
2969 int i;
2970
Ingo Molnar62932df2006-01-16 16:34:20 +01002971 mutex_lock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01002972 if (mout->dig_out_nid && mout->share_spdif &&
2973 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02002975 snd_hda_is_supported_format(codec, mout->dig_out_nid,
2976 format) &&
2977 !(codec->spdif_status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02002979 setup_dig_out_stream(codec, mout->dig_out_nid,
2980 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 } else {
2982 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02002983 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 }
2985 }
Ingo Molnar62932df2006-01-16 16:34:20 +01002986 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987
2988 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002989 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
2990 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02002991 if (!mout->no_share_stream &&
2992 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002994 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
2995 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01002996 /* extra outputs copied from front */
2997 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02002998 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01002999 snd_hda_codec_setup_stream(codec,
3000 mout->extra_out_nid[i],
3001 stream_tag, 0, format);
3002
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 /* surrounds */
3004 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02003005 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003006 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
3007 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02003008 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003009 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
3010 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 }
3012 return 0;
3013}
3014
3015/*
3016 * clean up the setting for analog out
3017 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003018int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
3019 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020{
3021 hda_nid_t *nids = mout->dac_nids;
3022 int i;
3023
3024 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01003025 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01003027 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwai82bc9552006-03-21 11:24:42 +01003028 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
3029 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01003030 snd_hda_codec_cleanup_stream(codec,
3031 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01003032 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02003034 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 mout->dig_out_used = 0;
3036 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003037 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 return 0;
3039}
3040
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003041/*
Wu Fengguang6b345002008-10-07 14:21:41 +08003042 * Helper for automatic pin configuration
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003043 */
Kailang Yangdf694da2005-12-05 19:42:22 +01003044
Takashi Iwai12f288b2007-08-02 15:51:59 +02003045static int is_in_nid_list(hda_nid_t nid, hda_nid_t *list)
Kailang Yangdf694da2005-12-05 19:42:22 +01003046{
3047 for (; *list; list++)
3048 if (*list == nid)
3049 return 1;
3050 return 0;
3051}
3052
Steve Longerbeam81937d32007-05-08 15:33:03 +02003053
3054/*
3055 * Sort an associated group of pins according to their sequence numbers.
3056 */
3057static void sort_pins_by_sequence(hda_nid_t * pins, short * sequences,
3058 int num_pins)
3059{
3060 int i, j;
3061 short seq;
3062 hda_nid_t nid;
3063
3064 for (i = 0; i < num_pins; i++) {
3065 for (j = i + 1; j < num_pins; j++) {
3066 if (sequences[i] > sequences[j]) {
3067 seq = sequences[i];
3068 sequences[i] = sequences[j];
3069 sequences[j] = seq;
3070 nid = pins[i];
3071 pins[i] = pins[j];
3072 pins[j] = nid;
3073 }
3074 }
3075 }
3076}
3077
3078
Takashi Iwai82bc9552006-03-21 11:24:42 +01003079/*
3080 * Parse all pin widgets and store the useful pin nids to cfg
3081 *
3082 * The number of line-outs or any primary output is stored in line_outs,
3083 * and the corresponding output pins are assigned to line_out_pins[],
3084 * in the order of front, rear, CLFE, side, ...
3085 *
3086 * If more extra outputs (speaker and headphone) are found, the pins are
Takashi Iwaieb06ed82006-09-20 17:10:27 +02003087 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
Takashi Iwai82bc9552006-03-21 11:24:42 +01003088 * is detected, one of speaker of HP pins is assigned as the primary
3089 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
3090 * if any analog output exists.
3091 *
3092 * The analog input pins are assigned to input_pins array.
3093 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
3094 * respectively.
3095 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003096int snd_hda_parse_pin_def_config(struct hda_codec *codec,
3097 struct auto_pin_cfg *cfg,
3098 hda_nid_t *ignore_nids)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003099{
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01003100 hda_nid_t nid, end_nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02003101 short seq, assoc_line_out, assoc_speaker;
3102 short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
3103 short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
Takashi Iwaif889fa92007-10-31 15:49:32 +01003104 short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003105
3106 memset(cfg, 0, sizeof(*cfg));
3107
Steve Longerbeam81937d32007-05-08 15:33:03 +02003108 memset(sequences_line_out, 0, sizeof(sequences_line_out));
3109 memset(sequences_speaker, 0, sizeof(sequences_speaker));
Takashi Iwaif889fa92007-10-31 15:49:32 +01003110 memset(sequences_hp, 0, sizeof(sequences_hp));
Steve Longerbeam81937d32007-05-08 15:33:03 +02003111 assoc_line_out = assoc_speaker = 0;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003112
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01003113 end_nid = codec->start_nid + codec->num_nodes;
3114 for (nid = codec->start_nid; nid < end_nid; nid++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01003115 unsigned int wid_caps = get_wcaps(codec, nid);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003116 unsigned int wid_type =
3117 (wid_caps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003118 unsigned int def_conf;
3119 short assoc, loc;
3120
3121 /* read all default configuration for pin complex */
3122 if (wid_type != AC_WID_PIN)
3123 continue;
Kailang Yangdf694da2005-12-05 19:42:22 +01003124 /* ignore the given nids (e.g. pc-beep returns error) */
3125 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
3126 continue;
3127
Takashi Iwai0ba21762007-04-16 11:29:14 +02003128 def_conf = snd_hda_codec_read(codec, nid, 0,
3129 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003130 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
3131 continue;
3132 loc = get_defcfg_location(def_conf);
3133 switch (get_defcfg_device(def_conf)) {
3134 case AC_JACK_LINE_OUT:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003135 seq = get_defcfg_sequence(def_conf);
3136 assoc = get_defcfg_association(def_conf);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01003137
3138 if (!(wid_caps & AC_WCAP_STEREO))
3139 if (!cfg->mono_out_pin)
3140 cfg->mono_out_pin = nid;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003141 if (!assoc)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003142 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003143 if (!assoc_line_out)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003144 assoc_line_out = assoc;
3145 else if (assoc_line_out != assoc)
3146 continue;
3147 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
3148 continue;
3149 cfg->line_out_pins[cfg->line_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02003150 sequences_line_out[cfg->line_outs] = seq;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003151 cfg->line_outs++;
3152 break;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01003153 case AC_JACK_SPEAKER:
Steve Longerbeam81937d32007-05-08 15:33:03 +02003154 seq = get_defcfg_sequence(def_conf);
3155 assoc = get_defcfg_association(def_conf);
3156 if (! assoc)
3157 continue;
3158 if (! assoc_speaker)
3159 assoc_speaker = assoc;
3160 else if (assoc_speaker != assoc)
3161 continue;
Takashi Iwai82bc9552006-03-21 11:24:42 +01003162 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
3163 continue;
3164 cfg->speaker_pins[cfg->speaker_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02003165 sequences_speaker[cfg->speaker_outs] = seq;
Takashi Iwai82bc9552006-03-21 11:24:42 +01003166 cfg->speaker_outs++;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01003167 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003168 case AC_JACK_HP_OUT:
Takashi Iwaif889fa92007-10-31 15:49:32 +01003169 seq = get_defcfg_sequence(def_conf);
3170 assoc = get_defcfg_association(def_conf);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02003171 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
3172 continue;
3173 cfg->hp_pins[cfg->hp_outs] = nid;
Takashi Iwaif889fa92007-10-31 15:49:32 +01003174 sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
Takashi Iwaieb06ed82006-09-20 17:10:27 +02003175 cfg->hp_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003176 break;
Takashi Iwai314634b2006-09-21 11:56:18 +02003177 case AC_JACK_MIC_IN: {
3178 int preferred, alt;
3179 if (loc == AC_JACK_LOC_FRONT) {
3180 preferred = AUTO_PIN_FRONT_MIC;
3181 alt = AUTO_PIN_MIC;
3182 } else {
3183 preferred = AUTO_PIN_MIC;
3184 alt = AUTO_PIN_FRONT_MIC;
3185 }
3186 if (!cfg->input_pins[preferred])
3187 cfg->input_pins[preferred] = nid;
3188 else if (!cfg->input_pins[alt])
3189 cfg->input_pins[alt] = nid;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003190 break;
Takashi Iwai314634b2006-09-21 11:56:18 +02003191 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003192 case AC_JACK_LINE_IN:
3193 if (loc == AC_JACK_LOC_FRONT)
3194 cfg->input_pins[AUTO_PIN_FRONT_LINE] = nid;
3195 else
3196 cfg->input_pins[AUTO_PIN_LINE] = nid;
3197 break;
3198 case AC_JACK_CD:
3199 cfg->input_pins[AUTO_PIN_CD] = nid;
3200 break;
3201 case AC_JACK_AUX:
3202 cfg->input_pins[AUTO_PIN_AUX] = nid;
3203 break;
3204 case AC_JACK_SPDIF_OUT:
3205 cfg->dig_out_pin = nid;
3206 break;
3207 case AC_JACK_SPDIF_IN:
3208 cfg->dig_in_pin = nid;
3209 break;
3210 }
3211 }
3212
Takashi Iwai5832fcf82008-02-12 18:30:12 +01003213 /* FIX-UP:
3214 * If no line-out is defined but multiple HPs are found,
3215 * some of them might be the real line-outs.
3216 */
3217 if (!cfg->line_outs && cfg->hp_outs > 1) {
3218 int i = 0;
3219 while (i < cfg->hp_outs) {
3220 /* The real HPs should have the sequence 0x0f */
3221 if ((sequences_hp[i] & 0x0f) == 0x0f) {
3222 i++;
3223 continue;
3224 }
3225 /* Move it to the line-out table */
3226 cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
3227 sequences_line_out[cfg->line_outs] = sequences_hp[i];
3228 cfg->line_outs++;
3229 cfg->hp_outs--;
3230 memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
3231 sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
3232 memmove(sequences_hp + i - 1, sequences_hp + i,
3233 sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
3234 }
3235 }
3236
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003237 /* sort by sequence */
Steve Longerbeam81937d32007-05-08 15:33:03 +02003238 sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
3239 cfg->line_outs);
3240 sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
3241 cfg->speaker_outs);
Takashi Iwaif889fa92007-10-31 15:49:32 +01003242 sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
3243 cfg->hp_outs);
Steve Longerbeam81937d32007-05-08 15:33:03 +02003244
Takashi Iwaif889fa92007-10-31 15:49:32 +01003245 /* if we have only one mic, make it AUTO_PIN_MIC */
3246 if (!cfg->input_pins[AUTO_PIN_MIC] &&
3247 cfg->input_pins[AUTO_PIN_FRONT_MIC]) {
3248 cfg->input_pins[AUTO_PIN_MIC] =
3249 cfg->input_pins[AUTO_PIN_FRONT_MIC];
3250 cfg->input_pins[AUTO_PIN_FRONT_MIC] = 0;
3251 }
3252 /* ditto for line-in */
3253 if (!cfg->input_pins[AUTO_PIN_LINE] &&
3254 cfg->input_pins[AUTO_PIN_FRONT_LINE]) {
3255 cfg->input_pins[AUTO_PIN_LINE] =
3256 cfg->input_pins[AUTO_PIN_FRONT_LINE];
3257 cfg->input_pins[AUTO_PIN_FRONT_LINE] = 0;
3258 }
3259
Steve Longerbeam81937d32007-05-08 15:33:03 +02003260 /*
3261 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
3262 * as a primary output
3263 */
3264 if (!cfg->line_outs) {
3265 if (cfg->speaker_outs) {
3266 cfg->line_outs = cfg->speaker_outs;
3267 memcpy(cfg->line_out_pins, cfg->speaker_pins,
3268 sizeof(cfg->speaker_pins));
3269 cfg->speaker_outs = 0;
3270 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
3271 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
3272 } else if (cfg->hp_outs) {
3273 cfg->line_outs = cfg->hp_outs;
3274 memcpy(cfg->line_out_pins, cfg->hp_pins,
3275 sizeof(cfg->hp_pins));
3276 cfg->hp_outs = 0;
3277 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
3278 cfg->line_out_type = AUTO_PIN_HP_OUT;
3279 }
3280 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003281
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02003282 /* Reorder the surround channels
3283 * ALSA sequence is front/surr/clfe/side
3284 * HDA sequence is:
3285 * 4-ch: front/surr => OK as it is
3286 * 6-ch: front/clfe/surr
Takashi Iwai9422db42007-04-20 16:11:43 +02003287 * 8-ch: front/clfe/rear/side|fc
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02003288 */
3289 switch (cfg->line_outs) {
3290 case 3:
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02003291 case 4:
3292 nid = cfg->line_out_pins[1];
Takashi Iwai9422db42007-04-20 16:11:43 +02003293 cfg->line_out_pins[1] = cfg->line_out_pins[2];
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02003294 cfg->line_out_pins[2] = nid;
3295 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003296 }
3297
Takashi Iwai82bc9552006-03-21 11:24:42 +01003298 /*
3299 * debug prints of the parsed results
3300 */
3301 snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3302 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
3303 cfg->line_out_pins[2], cfg->line_out_pins[3],
3304 cfg->line_out_pins[4]);
3305 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3306 cfg->speaker_outs, cfg->speaker_pins[0],
3307 cfg->speaker_pins[1], cfg->speaker_pins[2],
3308 cfg->speaker_pins[3], cfg->speaker_pins[4]);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02003309 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3310 cfg->hp_outs, cfg->hp_pins[0],
3311 cfg->hp_pins[1], cfg->hp_pins[2],
3312 cfg->hp_pins[3], cfg->hp_pins[4]);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01003313 snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
Takashi Iwai82bc9552006-03-21 11:24:42 +01003314 snd_printd(" inputs: mic=0x%x, fmic=0x%x, line=0x%x, fline=0x%x,"
3315 " cd=0x%x, aux=0x%x\n",
3316 cfg->input_pins[AUTO_PIN_MIC],
3317 cfg->input_pins[AUTO_PIN_FRONT_MIC],
3318 cfg->input_pins[AUTO_PIN_LINE],
3319 cfg->input_pins[AUTO_PIN_FRONT_LINE],
3320 cfg->input_pins[AUTO_PIN_CD],
3321 cfg->input_pins[AUTO_PIN_AUX]);
3322
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003323 return 0;
3324}
3325
Takashi Iwai4a471b72005-12-07 13:56:29 +01003326/* labels for input pins */
3327const char *auto_pin_cfg_labels[AUTO_PIN_LAST] = {
3328 "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux"
3329};
3330
3331
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332#ifdef CONFIG_PM
3333/*
3334 * power management
3335 */
3336
3337/**
3338 * snd_hda_suspend - suspend the codecs
3339 * @bus: the HDA bus
3340 * @state: suspsend state
3341 *
3342 * Returns 0 if successful.
3343 */
3344int snd_hda_suspend(struct hda_bus *bus, pm_message_t state)
3345{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003346 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347
Takashi Iwai0ba21762007-04-16 11:29:14 +02003348 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai0b7a2e92007-08-14 15:18:26 +02003349#ifdef CONFIG_SND_HDA_POWER_SAVE
3350 if (!codec->power_on)
3351 continue;
3352#endif
Takashi Iwaicb53c622007-08-10 17:21:45 +02003353 hda_call_codec_suspend(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 }
3355 return 0;
3356}
3357
3358/**
3359 * snd_hda_resume - resume the codecs
3360 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 *
3362 * Returns 0 if successful.
Takashi Iwaicb53c622007-08-10 17:21:45 +02003363 *
3364 * This fucntion is defined only when POWER_SAVE isn't set.
3365 * In the power-save mode, the codec is resumed dynamically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 */
3367int snd_hda_resume(struct hda_bus *bus)
3368{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003369 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370
Takashi Iwai0ba21762007-04-16 11:29:14 +02003371 list_for_each_entry(codec, &bus->codec_list, list) {
Maxim Levitskyd804ad92007-09-03 15:28:04 +02003372 if (snd_hda_codec_needs_resume(codec))
3373 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 return 0;
3376}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377#endif
Takashi Iwaib2e18592008-07-30 15:01:44 +02003378
3379/*
3380 * generic arrays
3381 */
3382
3383/* get a new element from the given array
3384 * if it exceeds the pre-allocated array size, re-allocate the array
3385 */
3386void *snd_array_new(struct snd_array *array)
3387{
3388 if (array->used >= array->alloced) {
3389 int num = array->alloced + array->alloc_align;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01003390 void *nlist;
3391 if (snd_BUG_ON(num >= 4096))
3392 return NULL;
3393 nlist = kcalloc(num + 1, array->elem_size, GFP_KERNEL);
Takashi Iwaib2e18592008-07-30 15:01:44 +02003394 if (!nlist)
3395 return NULL;
3396 if (array->list) {
3397 memcpy(nlist, array->list,
3398 array->elem_size * array->alloced);
3399 kfree(array->list);
3400 }
3401 array->list = nlist;
3402 array->alloced = num;
3403 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01003404 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02003405}
3406
3407/* free the given array elements */
3408void snd_array_free(struct snd_array *array)
3409{
3410 kfree(array->list);
3411 array->used = 0;
3412 array->alloced = 0;
3413 array->list = NULL;
3414}
Takashi Iwaib2022262008-11-21 21:24:03 +01003415
3416/*
3417 * used by hda_proc.c and hda_eld.c
3418 */
3419void snd_print_pcm_rates(int pcm, char *buf, int buflen)
3420{
3421 static unsigned int rates[] = {
3422 8000, 11025, 16000, 22050, 32000, 44100, 48000, 88200,
3423 96000, 176400, 192000, 384000
3424 };
3425 int i, j;
3426
3427 for (i = 0, j = 0; i < ARRAY_SIZE(rates); i++)
3428 if (pcm & (1 << i))
3429 j += snprintf(buf + j, buflen - j, " %d", rates[i]);
3430
3431 buf[j] = '\0'; /* necessary when j == 0 */
3432}
3433
3434void snd_print_pcm_bits(int pcm, char *buf, int buflen)
3435{
3436 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
3437 int i, j;
3438
3439 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
3440 if (pcm & (AC_SUPPCM_BITS_8 << i))
3441 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
3442
3443 buf[j] = '\0'; /* necessary when j == 0 */
3444}