blob: a6be6e3e8716026b8a8e129f5e7bc9d9ec31b37f [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" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 { 0x434d, "C-Media" },
Matt2f2f4252005-04-13 14:45:30 +020068 { 0x8384, "SigmaTel" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 {} /* terminator */
70};
71
Harvey Harrison3c9a3202008-02-29 11:59:26 +010072static const struct hda_codec_preset *hda_preset_tables[] = {
73#ifdef CONFIG_SND_HDA_CODEC_REALTEK
74 snd_hda_preset_realtek,
75#endif
76#ifdef CONFIG_SND_HDA_CODEC_CMEDIA
77 snd_hda_preset_cmedia,
78#endif
79#ifdef CONFIG_SND_HDA_CODEC_ANALOG
80 snd_hda_preset_analog,
81#endif
82#ifdef CONFIG_SND_HDA_CODEC_SIGMATEL
83 snd_hda_preset_sigmatel,
84#endif
85#ifdef CONFIG_SND_HDA_CODEC_SI3054
86 snd_hda_preset_si3054,
87#endif
88#ifdef CONFIG_SND_HDA_CODEC_ATIHDMI
89 snd_hda_preset_atihdmi,
90#endif
91#ifdef CONFIG_SND_HDA_CODEC_CONEXANT
92 snd_hda_preset_conexant,
93#endif
94#ifdef CONFIG_SND_HDA_CODEC_VIA
95 snd_hda_preset_via,
96#endif
97 NULL
98};
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Takashi Iwaicb53c622007-08-10 17:21:45 +0200100#ifdef CONFIG_SND_HDA_POWER_SAVE
101static void hda_power_work(struct work_struct *work);
102static void hda_keep_power_on(struct hda_codec *codec);
103#else
104static inline void hda_keep_power_on(struct hda_codec *codec) {}
105#endif
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107/**
108 * snd_hda_codec_read - send a command and get the response
109 * @codec: the HDA codec
110 * @nid: NID to send the command
111 * @direct: direct flag
112 * @verb: the verb to send
113 * @parm: the parameter for the verb
114 *
115 * Send a single command and read the corresponding response.
116 *
117 * Returns the obtained response value, or -1 for an error.
118 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200119unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
120 int direct,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 unsigned int verb, unsigned int parm)
122{
123 unsigned int res;
Takashi Iwaicb53c622007-08-10 17:21:45 +0200124 snd_hda_power_up(codec);
Ingo Molnar62932df2006-01-16 16:34:20 +0100125 mutex_lock(&codec->bus->cmd_mutex);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200126 if (!codec->bus->ops.command(codec, nid, direct, verb, parm))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 res = codec->bus->ops.get_response(codec);
128 else
129 res = (unsigned int)-1;
Ingo Molnar62932df2006-01-16 16:34:20 +0100130 mutex_unlock(&codec->bus->cmd_mutex);
Takashi Iwaicb53c622007-08-10 17:21:45 +0200131 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 return res;
133}
134
135/**
136 * snd_hda_codec_write - send a single command without waiting for response
137 * @codec: the HDA codec
138 * @nid: NID to send the command
139 * @direct: direct flag
140 * @verb: the verb to send
141 * @parm: the parameter for the verb
142 *
143 * Send a single command without waiting for response.
144 *
145 * Returns 0 if successful, or a negative error code.
146 */
147int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
148 unsigned int verb, unsigned int parm)
149{
150 int err;
Takashi Iwaicb53c622007-08-10 17:21:45 +0200151 snd_hda_power_up(codec);
Ingo Molnar62932df2006-01-16 16:34:20 +0100152 mutex_lock(&codec->bus->cmd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 err = codec->bus->ops.command(codec, nid, direct, verb, parm);
Ingo Molnar62932df2006-01-16 16:34:20 +0100154 mutex_unlock(&codec->bus->cmd_mutex);
Takashi Iwaicb53c622007-08-10 17:21:45 +0200155 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 return err;
157}
158
159/**
160 * snd_hda_sequence_write - sequence writes
161 * @codec: the HDA codec
162 * @seq: VERB array to send
163 *
164 * Send the commands sequentially from the given array.
165 * The array must be terminated with NID=0.
166 */
167void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
168{
169 for (; seq->nid; seq++)
170 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
171}
172
173/**
174 * snd_hda_get_sub_nodes - get the range of sub nodes
175 * @codec: the HDA codec
176 * @nid: NID to parse
177 * @start_id: the pointer to store the start NID
178 *
179 * Parse the NID and store the start NID of its sub-nodes.
180 * Returns the number of sub-nodes.
181 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200182int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
183 hda_nid_t *start_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
185 unsigned int parm;
186
187 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
Danny Tholene8a7f132007-09-11 21:41:56 +0200188 if (parm == -1)
189 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 *start_id = (parm >> 16) & 0x7fff;
191 return (int)(parm & 0x7fff);
192}
193
194/**
195 * snd_hda_get_connections - get connection list
196 * @codec: the HDA codec
197 * @nid: NID to parse
198 * @conn_list: connection list array
199 * @max_conns: max. number of connections to store
200 *
201 * Parses the connection list of the given widget and stores the list
202 * of NIDs.
203 *
204 * Returns the number of connections, or a negative error code.
205 */
206int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
207 hda_nid_t *conn_list, int max_conns)
208{
209 unsigned int parm;
Takashi Iwai54d17402005-11-21 16:33:22 +0100210 int i, conn_len, conns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 unsigned int shift, num_elems, mask;
Takashi Iwai54d17402005-11-21 16:33:22 +0100212 hda_nid_t prev_nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214 snd_assert(conn_list && max_conns > 0, return -EINVAL);
215
216 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
217 if (parm & AC_CLIST_LONG) {
218 /* long form */
219 shift = 16;
220 num_elems = 2;
221 } else {
222 /* short form */
223 shift = 8;
224 num_elems = 4;
225 }
226 conn_len = parm & AC_CLIST_LENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 mask = (1 << (shift-1)) - 1;
228
Takashi Iwai0ba21762007-04-16 11:29:14 +0200229 if (!conn_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 return 0; /* no connection */
231
232 if (conn_len == 1) {
233 /* single connection */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200234 parm = snd_hda_codec_read(codec, nid, 0,
235 AC_VERB_GET_CONNECT_LIST, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 conn_list[0] = parm & mask;
237 return 1;
238 }
239
240 /* multi connection */
241 conns = 0;
Takashi Iwai54d17402005-11-21 16:33:22 +0100242 prev_nid = 0;
243 for (i = 0; i < conn_len; i++) {
244 int range_val;
245 hda_nid_t val, n;
246
247 if (i % num_elems == 0)
248 parm = snd_hda_codec_read(codec, nid, 0,
249 AC_VERB_GET_CONNECT_LIST, i);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200250 range_val = !!(parm & (1 << (shift-1))); /* ranges */
Takashi Iwai54d17402005-11-21 16:33:22 +0100251 val = parm & mask;
252 parm >>= shift;
253 if (range_val) {
254 /* ranges between the previous and this one */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200255 if (!prev_nid || prev_nid >= val) {
256 snd_printk(KERN_WARNING "hda_codec: "
257 "invalid dep_range_val %x:%x\n",
258 prev_nid, val);
Takashi Iwai54d17402005-11-21 16:33:22 +0100259 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100261 for (n = prev_nid + 1; n <= val; n++) {
262 if (conns >= max_conns) {
Takashi Iwai0ba21762007-04-16 11:29:14 +0200263 snd_printk(KERN_ERR
264 "Too many connections\n");
Takashi Iwai54d17402005-11-21 16:33:22 +0100265 return -EINVAL;
266 }
267 conn_list[conns++] = n;
268 }
269 } else {
270 if (conns >= max_conns) {
271 snd_printk(KERN_ERR "Too many connections\n");
272 return -EINVAL;
273 }
274 conn_list[conns++] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100276 prev_nid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 }
278 return conns;
279}
280
281
282/**
283 * snd_hda_queue_unsol_event - add an unsolicited event to queue
284 * @bus: the BUS
285 * @res: unsolicited event (lower 32bit of RIRB entry)
286 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
287 *
288 * Adds the given event to the queue. The events are processed in
289 * the workqueue asynchronously. Call this function in the interrupt
290 * hanlder when RIRB receives an unsolicited event.
291 *
292 * Returns 0 if successful, or a negative error code.
293 */
294int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
295{
296 struct hda_bus_unsolicited *unsol;
297 unsigned int wp;
298
Takashi Iwai0ba21762007-04-16 11:29:14 +0200299 unsol = bus->unsol;
300 if (!unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 return 0;
302
303 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
304 unsol->wp = wp;
305
306 wp <<= 1;
307 unsol->queue[wp] = res;
308 unsol->queue[wp + 1] = res_ex;
309
Takashi Iwaie250af22006-12-19 17:08:52 +0100310 schedule_work(&unsol->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312 return 0;
313}
314
315/*
316 * process queueud unsolicited events
317 */
David Howellsc4028952006-11-22 14:57:56 +0000318static void process_unsol_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{
David Howellsc4028952006-11-22 14:57:56 +0000320 struct hda_bus_unsolicited *unsol =
321 container_of(work, struct hda_bus_unsolicited, work);
322 struct hda_bus *bus = unsol->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 struct hda_codec *codec;
324 unsigned int rp, caddr, res;
325
326 while (unsol->rp != unsol->wp) {
327 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
328 unsol->rp = rp;
329 rp <<= 1;
330 res = unsol->queue[rp];
331 caddr = unsol->queue[rp + 1];
Takashi Iwai0ba21762007-04-16 11:29:14 +0200332 if (!(caddr & (1 << 4))) /* no unsolicited event? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 continue;
334 codec = bus->caddr_tbl[caddr & 0x0f];
335 if (codec && codec->patch_ops.unsol_event)
336 codec->patch_ops.unsol_event(codec, res);
337 }
338}
339
340/*
341 * initialize unsolicited queue
342 */
Takashi Iwai756e2b02007-04-16 11:27:07 +0200343static int __devinit init_unsol_queue(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
345 struct hda_bus_unsolicited *unsol;
346
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100347 if (bus->unsol) /* already initialized */
348 return 0;
349
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200350 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200351 if (!unsol) {
352 snd_printk(KERN_ERR "hda_codec: "
353 "can't allocate unsolicited queue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 return -ENOMEM;
355 }
David Howellsc4028952006-11-22 14:57:56 +0000356 INIT_WORK(&unsol->work, process_unsol_events);
357 unsol->bus = bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 bus->unsol = unsol;
359 return 0;
360}
361
362/*
363 * destructor
364 */
365static void snd_hda_codec_free(struct hda_codec *codec);
366
367static int snd_hda_bus_free(struct hda_bus *bus)
368{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200369 struct hda_codec *codec, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Takashi Iwai0ba21762007-04-16 11:29:14 +0200371 if (!bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 return 0;
373 if (bus->unsol) {
Takashi Iwaie250af22006-12-19 17:08:52 +0100374 flush_scheduled_work();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 kfree(bus->unsol);
376 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200377 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 snd_hda_codec_free(codec);
379 }
380 if (bus->ops.private_free)
381 bus->ops.private_free(bus);
382 kfree(bus);
383 return 0;
384}
385
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100386static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
388 struct hda_bus *bus = device->device_data;
389 return snd_hda_bus_free(bus);
390}
391
392/**
393 * snd_hda_bus_new - create a HDA bus
394 * @card: the card entry
395 * @temp: the template for hda_bus information
396 * @busp: the pointer to store the created bus instance
397 *
398 * Returns 0 if successful, or a negative error code.
399 */
Takashi Iwai756e2b02007-04-16 11:27:07 +0200400int __devinit snd_hda_bus_new(struct snd_card *card,
401 const struct hda_bus_template *temp,
402 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
404 struct hda_bus *bus;
405 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100406 static struct snd_device_ops dev_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 .dev_free = snd_hda_bus_dev_free,
408 };
409
410 snd_assert(temp, return -EINVAL);
411 snd_assert(temp->ops.command && temp->ops.get_response, return -EINVAL);
412
413 if (busp)
414 *busp = NULL;
415
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200416 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 if (bus == NULL) {
418 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
419 return -ENOMEM;
420 }
421
422 bus->card = card;
423 bus->private_data = temp->private_data;
424 bus->pci = temp->pci;
425 bus->modelname = temp->modelname;
426 bus->ops = temp->ops;
427
Ingo Molnar62932df2006-01-16 16:34:20 +0100428 mutex_init(&bus->cmd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 INIT_LIST_HEAD(&bus->codec_list);
430
Takashi Iwai0ba21762007-04-16 11:29:14 +0200431 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
432 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 snd_hda_bus_free(bus);
434 return err;
435 }
436 if (busp)
437 *busp = bus;
438 return 0;
439}
440
Takashi Iwai82467612007-07-27 19:15:54 +0200441#ifdef CONFIG_SND_HDA_GENERIC
442#define is_generic_config(codec) \
443 (codec->bus->modelname && !strcmp(codec->bus->modelname, "generic"))
444#else
445#define is_generic_config(codec) 0
446#endif
447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448/*
449 * find a matching codec preset
450 */
Takashi Iwai756e2b02007-04-16 11:27:07 +0200451static const struct hda_codec_preset __devinit *
452find_codec_preset(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
454 const struct hda_codec_preset **tbl, *preset;
455
Takashi Iwai82467612007-07-27 19:15:54 +0200456 if (is_generic_config(codec))
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100457 return NULL; /* use the generic parser */
458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 for (tbl = hda_preset_tables; *tbl; tbl++) {
460 for (preset = *tbl; preset->id; preset++) {
461 u32 mask = preset->mask;
Marc Boucherca7cfae2008-01-22 15:32:25 +0100462 if (preset->afg && preset->afg != codec->afg)
463 continue;
464 if (preset->mfg && preset->mfg != codec->mfg)
465 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200466 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 mask = ~0;
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200468 if (preset->id == (codec->vendor_id & mask) &&
Takashi Iwai0ba21762007-04-16 11:29:14 +0200469 (!preset->rev ||
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200470 preset->rev == codec->revision_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 return preset;
472 }
473 }
474 return NULL;
475}
476
477/*
478 * snd_hda_get_codec_name - store the codec name
479 */
480void snd_hda_get_codec_name(struct hda_codec *codec,
481 char *name, int namelen)
482{
483 const struct hda_vendor_id *c;
484 const char *vendor = NULL;
485 u16 vendor_id = codec->vendor_id >> 16;
486 char tmp[16];
487
488 for (c = hda_vendor_ids; c->id; c++) {
489 if (c->id == vendor_id) {
490 vendor = c->name;
491 break;
492 }
493 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200494 if (!vendor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 sprintf(tmp, "Generic %04x", vendor_id);
496 vendor = tmp;
497 }
498 if (codec->preset && codec->preset->name)
499 snprintf(name, namelen, "%s %s", vendor, codec->preset->name);
500 else
Takashi Iwai0ba21762007-04-16 11:29:14 +0200501 snprintf(name, namelen, "%s ID %x", vendor,
502 codec->vendor_id & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503}
504
505/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200506 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 */
Takashi Iwai756e2b02007-04-16 11:27:07 +0200508static void __devinit setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
510 int i, total_nodes;
511 hda_nid_t nid;
512
513 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
514 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +0200515 unsigned int func;
516 func = snd_hda_param_read(codec, nid, AC_PAR_FUNCTION_TYPE);
517 switch (func & 0xff) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200518 case AC_GRP_AUDIO_FUNCTION:
519 codec->afg = nid;
520 break;
521 case AC_GRP_MODEM_FUNCTION:
522 codec->mfg = nid;
523 break;
524 default:
525 break;
526 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528}
529
530/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100531 * read widget caps for each widget and store in cache
532 */
533static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
534{
535 int i;
536 hda_nid_t nid;
537
538 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
539 &codec->start_nid);
540 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200541 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +0100542 return -ENOMEM;
543 nid = codec->start_nid;
544 for (i = 0; i < codec->num_nodes; i++, nid++)
545 codec->wcaps[i] = snd_hda_param_read(codec, nid,
546 AC_PAR_AUDIO_WIDGET_CAP);
547 return 0;
548}
549
550
Takashi Iwai01751f52007-08-10 16:59:39 +0200551static void init_hda_cache(struct hda_cache_rec *cache,
552 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +0200553static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +0200554
Takashi Iwai54d17402005-11-21 16:33:22 +0100555/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 * codec destructor
557 */
558static void snd_hda_codec_free(struct hda_codec *codec)
559{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200560 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 return;
Takashi Iwaicb53c622007-08-10 17:21:45 +0200562#ifdef CONFIG_SND_HDA_POWER_SAVE
563 cancel_delayed_work(&codec->power_work);
Takashi Iwai2525fdc2007-08-15 22:18:22 +0200564 flush_scheduled_work();
Takashi Iwaicb53c622007-08-10 17:21:45 +0200565#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 list_del(&codec->list);
567 codec->bus->caddr_tbl[codec->addr] = NULL;
568 if (codec->patch_ops.free)
569 codec->patch_ops.free(codec);
Takashi Iwai01751f52007-08-10 16:59:39 +0200570 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +0200571 free_hda_cache(&codec->cmd_cache);
Takashi Iwai54d17402005-11-21 16:33:22 +0100572 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 kfree(codec);
574}
575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576/**
577 * snd_hda_codec_new - create a HDA codec
578 * @bus: the bus to assign
579 * @codec_addr: the codec address
580 * @codecp: the pointer to store the generated codec
581 *
582 * Returns 0 if successful, or a negative error code.
583 */
Takashi Iwai756e2b02007-04-16 11:27:07 +0200584int __devinit snd_hda_codec_new(struct hda_bus *bus, unsigned int codec_addr,
585 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
587 struct hda_codec *codec;
588 char component[13];
589 int err;
590
591 snd_assert(bus, return -EINVAL);
592 snd_assert(codec_addr <= HDA_MAX_CODEC_ADDRESS, return -EINVAL);
593
594 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +0200595 snd_printk(KERN_ERR "hda_codec: "
596 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 return -EBUSY;
598 }
599
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200600 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 if (codec == NULL) {
602 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
603 return -ENOMEM;
604 }
605
606 codec->bus = bus;
607 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +0100608 mutex_init(&codec->spdif_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +0200609 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +0200610 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Takashi Iwaicb53c622007-08-10 17:21:45 +0200612#ifdef CONFIG_SND_HDA_POWER_SAVE
613 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
614 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
615 * the caller has to power down appropriatley after initialization
616 * phase.
617 */
618 hda_keep_power_on(codec);
619#endif
620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 list_add_tail(&codec->list, &bus->codec_list);
622 bus->caddr_tbl[codec_addr] = codec;
623
Takashi Iwai0ba21762007-04-16 11:29:14 +0200624 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
625 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +0100626 if (codec->vendor_id == -1)
627 /* read again, hopefully the access method was corrected
628 * in the last read...
629 */
630 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
631 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200632 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
633 AC_PAR_SUBSYSTEM_ID);
634 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
635 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200637 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200638 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200639 snd_printdd("hda_codec: no AFG or MFG node found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 snd_hda_codec_free(codec);
641 return -ENODEV;
642 }
643
Takashi Iwai54d17402005-11-21 16:33:22 +0100644 if (read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg) < 0) {
645 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
646 snd_hda_codec_free(codec);
647 return -ENOMEM;
648 }
649
Takashi Iwai0ba21762007-04-16 11:29:14 +0200650 if (!codec->subsystem_id) {
Takashi Iwai86284e42005-10-11 15:05:54 +0200651 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200652 codec->subsystem_id =
653 snd_hda_codec_read(codec, nid, 0,
654 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +0200655 }
656
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100657 codec->preset = find_codec_preset(codec);
Takashi Iwai43ea1d42007-04-26 19:12:08 +0200658 /* audio codec should override the mixer name */
659 if (codec->afg || !*bus->card->mixername)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 snd_hda_get_codec_name(codec, bus->card->mixername,
661 sizeof(bus->card->mixername));
662
Takashi Iwai82467612007-07-27 19:15:54 +0200663 if (is_generic_config(codec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai82467612007-07-27 19:15:54 +0200665 goto patched;
666 }
Takashi Iwai82467612007-07-27 19:15:54 +0200667 if (codec->preset && codec->preset->patch) {
668 err = codec->preset->patch(codec);
669 goto patched;
670 }
671
672 /* call the default parser */
Takashi Iwai82467612007-07-27 19:15:54 +0200673 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai35a1e0c2007-10-19 08:13:40 +0200674 if (err < 0)
675 printk(KERN_ERR "hda-codec: No codec parser is available\n");
Takashi Iwai82467612007-07-27 19:15:54 +0200676
677 patched:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 if (err < 0) {
679 snd_hda_codec_free(codec);
680 return err;
681 }
682
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100683 if (codec->patch_ops.unsol_event)
684 init_unsol_queue(bus);
685
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 snd_hda_codec_proc_new(codec);
Takashi Iwai28073142007-07-27 18:58:06 +0200687#ifdef CONFIG_SND_HDA_HWDEP
688 snd_hda_create_hwdep(codec);
689#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
691 sprintf(component, "HDA:%08x", codec->vendor_id);
692 snd_component_add(codec->bus->card, component);
693
694 if (codecp)
695 *codecp = codec;
696 return 0;
697}
698
699/**
700 * snd_hda_codec_setup_stream - set up the codec for streaming
701 * @codec: the CODEC to set up
702 * @nid: the NID to set up
703 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
704 * @channel_id: channel id to pass, zero based.
705 * @format: stream format.
706 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200707void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
708 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 int channel_id, int format)
710{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200711 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +0200712 return;
713
Takashi Iwai0ba21762007-04-16 11:29:14 +0200714 snd_printdd("hda_codec_setup_stream: "
715 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 nid, stream_tag, channel_id, format);
717 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID,
718 (stream_tag << 4) | channel_id);
719 msleep(1);
720 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, format);
721}
722
Takashi Iwai888afa12008-03-18 09:57:50 +0100723void snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid)
724{
725 if (!nid)
726 return;
727
728 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
729 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
730#if 0 /* keep the format */
731 msleep(1);
732 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0);
733#endif
734}
735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736/*
737 * amp access functions
738 */
739
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200740/* FIXME: more better hash key? */
741#define HDA_HASH_KEY(nid,dir,idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200743#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
745/* initialize the hash table */
Takashi Iwai01751f52007-08-10 16:59:39 +0200746static void __devinit init_hda_cache(struct hda_cache_rec *cache,
747 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748{
Takashi Iwai01751f52007-08-10 16:59:39 +0200749 memset(cache, 0, sizeof(*cache));
750 memset(cache->hash, 0xff, sizeof(cache->hash));
751 cache->record_size = record_size;
752}
753
Takashi Iwai1fcaee62007-08-23 00:01:09 +0200754static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +0200755{
756 kfree(cache->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757}
758
759/* query the hash. allocate an entry if not found. */
Takashi Iwai01751f52007-08-10 16:59:39 +0200760static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
761 u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762{
Takashi Iwai01751f52007-08-10 16:59:39 +0200763 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
764 u16 cur = cache->hash[idx];
765 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
767 while (cur != 0xffff) {
Takashi Iwai01751f52007-08-10 16:59:39 +0200768 info = (struct hda_cache_head *)(cache->buffer +
769 cur * cache->record_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 if (info->key == key)
771 return info;
772 cur = info->next;
773 }
774
775 /* add a new hash entry */
Takashi Iwai01751f52007-08-10 16:59:39 +0200776 if (cache->num_entries >= cache->size) {
Takashi Iwaid0311662005-11-07 14:38:44 +0100777 /* reallocate the array */
Takashi Iwai01751f52007-08-10 16:59:39 +0200778 unsigned int new_size = cache->size + 64;
779 void *new_buffer;
780 new_buffer = kcalloc(new_size, cache->record_size, GFP_KERNEL);
781 if (!new_buffer) {
Takashi Iwai0ba21762007-04-16 11:29:14 +0200782 snd_printk(KERN_ERR "hda_codec: "
783 "can't malloc amp_info\n");
Takashi Iwaid0311662005-11-07 14:38:44 +0100784 return NULL;
785 }
Takashi Iwai01751f52007-08-10 16:59:39 +0200786 if (cache->buffer) {
787 memcpy(new_buffer, cache->buffer,
788 cache->size * cache->record_size);
789 kfree(cache->buffer);
Takashi Iwaid0311662005-11-07 14:38:44 +0100790 }
Takashi Iwai01751f52007-08-10 16:59:39 +0200791 cache->size = new_size;
792 cache->buffer = new_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 }
Takashi Iwai01751f52007-08-10 16:59:39 +0200794 cur = cache->num_entries++;
795 info = (struct hda_cache_head *)(cache->buffer +
796 cur * cache->record_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 info->key = key;
Takashi Iwai01751f52007-08-10 16:59:39 +0200798 info->val = 0;
799 info->next = cache->hash[idx];
800 cache->hash[idx] = cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
802 return info;
803}
804
Takashi Iwai01751f52007-08-10 16:59:39 +0200805/* query and allocate an amp hash entry */
806static inline struct hda_amp_info *
807get_alloc_amp_hash(struct hda_codec *codec, u32 key)
808{
809 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
810}
811
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812/*
813 * query AMP capabilities for the given widget and direction
814 */
Matthew Ranostay09a99952008-01-24 11:49:21 +0100815u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200817 struct hda_amp_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Takashi Iwai0ba21762007-04-16 11:29:14 +0200819 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
820 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 return 0;
Takashi Iwai01751f52007-08-10 16:59:39 +0200822 if (!(info->head.val & INFO_AMP_CAPS)) {
Takashi Iwai0ba21762007-04-16 11:29:14 +0200823 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 nid = codec->afg;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200825 info->amp_caps = snd_hda_param_read(codec, nid,
826 direction == HDA_OUTPUT ?
827 AC_PAR_AMP_OUT_CAP :
828 AC_PAR_AMP_IN_CAP);
Takashi Iwaib75e53f2007-05-10 16:56:09 +0200829 if (info->amp_caps)
Takashi Iwai01751f52007-08-10 16:59:39 +0200830 info->head.val |= INFO_AMP_CAPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 }
832 return info->amp_caps;
833}
834
Takashi Iwai897cc182007-05-29 19:01:37 +0200835int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
836 unsigned int caps)
837{
838 struct hda_amp_info *info;
839
840 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
841 if (!info)
842 return -EINVAL;
843 info->amp_caps = caps;
Takashi Iwai01751f52007-08-10 16:59:39 +0200844 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai897cc182007-05-29 19:01:37 +0200845 return 0;
846}
847
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848/*
849 * read the current volume to info
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200850 * if the cache exists, read the cache value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200852static unsigned int get_vol_mute(struct hda_codec *codec,
853 struct hda_amp_info *info, hda_nid_t nid,
854 int ch, int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855{
856 u32 val, parm;
857
Takashi Iwai01751f52007-08-10 16:59:39 +0200858 if (info->head.val & INFO_AMP_VOL(ch))
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200859 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
861 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
862 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
863 parm |= index;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200864 val = snd_hda_codec_read(codec, nid, 0,
865 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 info->vol[ch] = val & 0xff;
Takashi Iwai01751f52007-08-10 16:59:39 +0200867 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200868 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869}
870
871/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200872 * write the current volume in info to the h/w and update the cache
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200874static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
Takashi Iwai0ba21762007-04-16 11:29:14 +0200875 hda_nid_t nid, int ch, int direction, int index,
876 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
878 u32 parm;
879
880 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
881 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
882 parm |= index << AC_AMP_SET_INDEX_SHIFT;
883 parm |= val;
884 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200885 info->vol[ch] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886}
887
888/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200889 * read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 */
Takashi Iwai834be882006-03-01 14:16:17 +0100891int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
892 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200894 struct hda_amp_info *info;
895 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
896 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200898 return get_vol_mute(codec, info, nid, ch, direction, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899}
900
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200901/*
902 * update the AMP value, mask = bit mask to set, val = the value
903 */
Takashi Iwai834be882006-03-01 14:16:17 +0100904int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
905 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200907 struct hda_amp_info *info;
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200908
Takashi Iwai0ba21762007-04-16 11:29:14 +0200909 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
910 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200912 val &= mask;
913 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
Takashi Iwai82beb8f2007-08-10 17:09:26 +0200914 if (info->vol[ch] == val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200916 put_vol_mute(codec, info, nid, ch, direction, idx, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 return 1;
918}
919
Takashi Iwai47fd8302007-08-10 17:11:07 +0200920/*
921 * update the AMP stereo with the same mask and value
922 */
923int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
924 int direction, int idx, int mask, int val)
925{
926 int ch, ret = 0;
927 for (ch = 0; ch < 2; ch++)
928 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
929 idx, mask, val);
930 return ret;
931}
932
Takashi Iwaicb53c622007-08-10 17:21:45 +0200933#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwaib3ac5632007-08-10 17:03:40 +0200934/* resume the all amp commands from the cache */
935void snd_hda_codec_resume_amp(struct hda_codec *codec)
936{
937 struct hda_amp_info *buffer = codec->amp_cache.buffer;
938 int i;
939
940 for (i = 0; i < codec->amp_cache.size; i++, buffer++) {
941 u32 key = buffer->head.key;
942 hda_nid_t nid;
943 unsigned int idx, dir, ch;
944 if (!key)
945 continue;
946 nid = key & 0xff;
947 idx = (key >> 16) & 0xff;
948 dir = (key >> 24) & 0xff;
949 for (ch = 0; ch < 2; ch++) {
950 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
951 continue;
952 put_vol_mute(codec, buffer, nid, ch, dir, idx,
953 buffer->vol[ch]);
954 }
955 }
956}
Takashi Iwaicb53c622007-08-10 17:21:45 +0200957#endif /* SND_HDA_NEEDS_RESUME */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
959/*
960 * AMP control callbacks
961 */
962/* retrieve parameters from private_value */
963#define get_amp_nid(kc) ((kc)->private_value & 0xffff)
964#define get_amp_channels(kc) (((kc)->private_value >> 16) & 0x3)
965#define get_amp_direction(kc) (((kc)->private_value >> 18) & 0x1)
966#define get_amp_index(kc) (((kc)->private_value >> 19) & 0xf)
967
968/* volume */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200969int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
970 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971{
972 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
973 u16 nid = get_amp_nid(kcontrol);
974 u8 chs = get_amp_channels(kcontrol);
975 int dir = get_amp_direction(kcontrol);
976 u32 caps;
977
978 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200979 /* num steps */
980 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
981 if (!caps) {
982 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +0100983 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
984 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 return -EINVAL;
986 }
987 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
988 uinfo->count = chs == 3 ? 2 : 1;
989 uinfo->value.integer.min = 0;
990 uinfo->value.integer.max = caps;
991 return 0;
992}
993
Takashi Iwai0ba21762007-04-16 11:29:14 +0200994int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
995 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996{
997 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
998 hda_nid_t nid = get_amp_nid(kcontrol);
999 int chs = get_amp_channels(kcontrol);
1000 int dir = get_amp_direction(kcontrol);
1001 int idx = get_amp_index(kcontrol);
1002 long *valp = ucontrol->value.integer.value;
1003
1004 if (chs & 1)
Takashi Iwai47fd8302007-08-10 17:11:07 +02001005 *valp++ = snd_hda_codec_amp_read(codec, nid, 0, dir, idx)
1006 & HDA_AMP_VOLMASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 if (chs & 2)
Takashi Iwai47fd8302007-08-10 17:11:07 +02001008 *valp = snd_hda_codec_amp_read(codec, nid, 1, dir, idx)
1009 & HDA_AMP_VOLMASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 return 0;
1011}
1012
Takashi Iwai0ba21762007-04-16 11:29:14 +02001013int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1014 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015{
1016 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1017 hda_nid_t nid = get_amp_nid(kcontrol);
1018 int chs = get_amp_channels(kcontrol);
1019 int dir = get_amp_direction(kcontrol);
1020 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 long *valp = ucontrol->value.integer.value;
1022 int change = 0;
1023
Takashi Iwaicb53c622007-08-10 17:21:45 +02001024 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001025 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001026 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
1027 0x7f, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001028 valp++;
1029 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001030 if (chs & 2)
1031 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001032 0x7f, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001033 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 return change;
1035}
1036
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001037int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1038 unsigned int size, unsigned int __user *_tlv)
1039{
1040 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1041 hda_nid_t nid = get_amp_nid(kcontrol);
1042 int dir = get_amp_direction(kcontrol);
1043 u32 caps, val1, val2;
1044
1045 if (size < 4 * sizeof(unsigned int))
1046 return -ENOMEM;
1047 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001048 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1049 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001050 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
1051 val1 = ((int)val1) * ((int)val2);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001052 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
1053 return -EFAULT;
1054 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
1055 return -EFAULT;
1056 if (put_user(val1, _tlv + 2))
1057 return -EFAULT;
1058 if (put_user(val2, _tlv + 3))
1059 return -EFAULT;
1060 return 0;
1061}
1062
Takashi Iwai2134ea42008-01-10 16:53:55 +01001063/*
1064 * set (static) TLV for virtual master volume; recalculated as max 0dB
1065 */
1066void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
1067 unsigned int *tlv)
1068{
1069 u32 caps;
1070 int nums, step;
1071
1072 caps = query_amp_caps(codec, nid, dir);
1073 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1074 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1075 step = (step + 1) * 25;
1076 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
1077 tlv[1] = 2 * sizeof(unsigned int);
1078 tlv[2] = -nums * step;
1079 tlv[3] = step;
1080}
1081
1082/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01001083static struct snd_kcontrol *
1084_snd_hda_find_mixer_ctl(struct hda_codec *codec,
1085 const char *name, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01001086{
1087 struct snd_ctl_elem_id id;
1088 memset(&id, 0, sizeof(id));
1089 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwai09f99702008-02-04 12:31:13 +01001090 id.index = idx;
Takashi Iwai2134ea42008-01-10 16:53:55 +01001091 strcpy(id.name, name);
1092 return snd_ctl_find_id(codec->bus->card, &id);
1093}
1094
Takashi Iwai09f99702008-02-04 12:31:13 +01001095struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
1096 const char *name)
1097{
1098 return _snd_hda_find_mixer_ctl(codec, name, 0);
1099}
1100
Takashi Iwai2134ea42008-01-10 16:53:55 +01001101/* create a virtual master control and add slaves */
1102int snd_hda_add_vmaster(struct hda_codec *codec, char *name,
1103 unsigned int *tlv, const char **slaves)
1104{
1105 struct snd_kcontrol *kctl;
1106 const char **s;
1107 int err;
1108
Takashi Iwai2f085542008-02-22 18:43:50 +01001109 for (s = slaves; *s && !snd_hda_find_mixer_ctl(codec, *s); s++)
1110 ;
1111 if (!*s) {
1112 snd_printdd("No slave found for %s\n", name);
1113 return 0;
1114 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01001115 kctl = snd_ctl_make_virtual_master(name, tlv);
1116 if (!kctl)
1117 return -ENOMEM;
1118 err = snd_ctl_add(codec->bus->card, kctl);
1119 if (err < 0)
1120 return err;
1121
1122 for (s = slaves; *s; s++) {
1123 struct snd_kcontrol *sctl;
1124
1125 sctl = snd_hda_find_mixer_ctl(codec, *s);
1126 if (!sctl) {
1127 snd_printdd("Cannot find slave %s, skipped\n", *s);
1128 continue;
1129 }
1130 err = snd_ctl_add_slave(kctl, sctl);
1131 if (err < 0)
1132 return err;
1133 }
1134 return 0;
1135}
1136
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137/* switch */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001138int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
1139 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140{
1141 int chs = get_amp_channels(kcontrol);
1142
1143 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1144 uinfo->count = chs == 3 ? 2 : 1;
1145 uinfo->value.integer.min = 0;
1146 uinfo->value.integer.max = 1;
1147 return 0;
1148}
1149
Takashi Iwai0ba21762007-04-16 11:29:14 +02001150int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
1151 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152{
1153 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1154 hda_nid_t nid = get_amp_nid(kcontrol);
1155 int chs = get_amp_channels(kcontrol);
1156 int dir = get_amp_direction(kcontrol);
1157 int idx = get_amp_index(kcontrol);
1158 long *valp = ucontrol->value.integer.value;
1159
1160 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02001161 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02001162 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02001164 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02001165 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 return 0;
1167}
1168
Takashi Iwai0ba21762007-04-16 11:29:14 +02001169int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
1170 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171{
1172 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1173 hda_nid_t nid = get_amp_nid(kcontrol);
1174 int chs = get_amp_channels(kcontrol);
1175 int dir = get_amp_direction(kcontrol);
1176 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 long *valp = ucontrol->value.integer.value;
1178 int change = 0;
1179
Takashi Iwaicb53c622007-08-10 17:21:45 +02001180 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001181 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001182 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02001183 HDA_AMP_MUTE,
1184 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001185 valp++;
1186 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001187 if (chs & 2)
1188 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02001189 HDA_AMP_MUTE,
1190 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001191#ifdef CONFIG_SND_HDA_POWER_SAVE
1192 if (codec->patch_ops.check_power_status)
1193 codec->patch_ops.check_power_status(codec, nid);
1194#endif
1195 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 return change;
1197}
1198
1199/*
Takashi Iwai985be542005-11-02 18:26:49 +01001200 * bound volume controls
1201 *
1202 * bind multiple volumes (# indices, from 0)
1203 */
1204
1205#define AMP_VAL_IDX_SHIFT 19
1206#define AMP_VAL_IDX_MASK (0x0f<<19)
1207
Takashi Iwai0ba21762007-04-16 11:29:14 +02001208int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
1209 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01001210{
1211 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1212 unsigned long pval;
1213 int err;
1214
Ingo Molnar62932df2006-01-16 16:34:20 +01001215 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Takashi Iwai985be542005-11-02 18:26:49 +01001216 pval = kcontrol->private_value;
1217 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
1218 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
1219 kcontrol->private_value = pval;
Ingo Molnar62932df2006-01-16 16:34:20 +01001220 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01001221 return err;
1222}
1223
Takashi Iwai0ba21762007-04-16 11:29:14 +02001224int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
1225 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01001226{
1227 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1228 unsigned long pval;
1229 int i, indices, err = 0, change = 0;
1230
Ingo Molnar62932df2006-01-16 16:34:20 +01001231 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Takashi Iwai985be542005-11-02 18:26:49 +01001232 pval = kcontrol->private_value;
1233 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
1234 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001235 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
1236 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01001237 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1238 if (err < 0)
1239 break;
1240 change |= err;
1241 }
1242 kcontrol->private_value = pval;
Ingo Molnar62932df2006-01-16 16:34:20 +01001243 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01001244 return err < 0 ? err : change;
1245}
1246
1247/*
Takashi Iwai532d5382007-07-27 19:02:40 +02001248 * generic bound volume/swtich controls
1249 */
1250int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
1251 struct snd_ctl_elem_info *uinfo)
1252{
1253 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1254 struct hda_bind_ctls *c;
1255 int err;
1256
Takashi Iwai532d5382007-07-27 19:02:40 +02001257 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01001258 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02001259 kcontrol->private_value = *c->values;
1260 err = c->ops->info(kcontrol, uinfo);
1261 kcontrol->private_value = (long)c;
1262 mutex_unlock(&codec->spdif_mutex);
1263 return err;
1264}
1265
1266int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
1267 struct snd_ctl_elem_value *ucontrol)
1268{
1269 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1270 struct hda_bind_ctls *c;
1271 int err;
1272
Takashi Iwai532d5382007-07-27 19:02:40 +02001273 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01001274 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02001275 kcontrol->private_value = *c->values;
1276 err = c->ops->get(kcontrol, ucontrol);
1277 kcontrol->private_value = (long)c;
1278 mutex_unlock(&codec->spdif_mutex);
1279 return err;
1280}
1281
1282int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
1283 struct snd_ctl_elem_value *ucontrol)
1284{
1285 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1286 struct hda_bind_ctls *c;
1287 unsigned long *vals;
1288 int err = 0, change = 0;
1289
Takashi Iwai532d5382007-07-27 19:02:40 +02001290 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01001291 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02001292 for (vals = c->values; *vals; vals++) {
1293 kcontrol->private_value = *vals;
1294 err = c->ops->put(kcontrol, ucontrol);
1295 if (err < 0)
1296 break;
1297 change |= err;
1298 }
1299 kcontrol->private_value = (long)c;
1300 mutex_unlock(&codec->spdif_mutex);
1301 return err < 0 ? err : change;
1302}
1303
1304int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1305 unsigned int size, unsigned int __user *tlv)
1306{
1307 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1308 struct hda_bind_ctls *c;
1309 int err;
1310
Takashi Iwai532d5382007-07-27 19:02:40 +02001311 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01001312 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02001313 kcontrol->private_value = *c->values;
1314 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
1315 kcontrol->private_value = (long)c;
1316 mutex_unlock(&codec->spdif_mutex);
1317 return err;
1318}
1319
1320struct hda_ctl_ops snd_hda_bind_vol = {
1321 .info = snd_hda_mixer_amp_volume_info,
1322 .get = snd_hda_mixer_amp_volume_get,
1323 .put = snd_hda_mixer_amp_volume_put,
1324 .tlv = snd_hda_mixer_amp_tlv
1325};
1326
1327struct hda_ctl_ops snd_hda_bind_sw = {
1328 .info = snd_hda_mixer_amp_switch_info,
1329 .get = snd_hda_mixer_amp_switch_get,
1330 .put = snd_hda_mixer_amp_switch_put,
1331 .tlv = snd_hda_mixer_amp_tlv
1332};
1333
1334/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 * SPDIF out controls
1336 */
1337
Takashi Iwai0ba21762007-04-16 11:29:14 +02001338static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
1339 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340{
1341 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1342 uinfo->count = 1;
1343 return 0;
1344}
1345
Takashi Iwai0ba21762007-04-16 11:29:14 +02001346static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
1347 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348{
1349 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
1350 IEC958_AES0_NONAUDIO |
1351 IEC958_AES0_CON_EMPHASIS_5015 |
1352 IEC958_AES0_CON_NOT_COPYRIGHT;
1353 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
1354 IEC958_AES1_CON_ORIGINAL;
1355 return 0;
1356}
1357
Takashi Iwai0ba21762007-04-16 11:29:14 +02001358static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
1359 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360{
1361 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
1362 IEC958_AES0_NONAUDIO |
1363 IEC958_AES0_PRO_EMPHASIS_5015;
1364 return 0;
1365}
1366
Takashi Iwai0ba21762007-04-16 11:29:14 +02001367static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
1368 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369{
1370 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1371
1372 ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
1373 ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
1374 ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
1375 ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
1376
1377 return 0;
1378}
1379
1380/* convert from SPDIF status bits to HDA SPDIF bits
1381 * bit 0 (DigEn) is always set zero (to be filled later)
1382 */
1383static unsigned short convert_from_spdif_status(unsigned int sbits)
1384{
1385 unsigned short val = 0;
1386
1387 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02001388 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02001390 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001392 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
1393 IEC958_AES0_PRO_EMPHASIS_5015)
1394 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001396 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
1397 IEC958_AES0_CON_EMPHASIS_5015)
1398 val |= AC_DIG1_EMPHASIS;
1399 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
1400 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02001402 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
1404 }
1405 return val;
1406}
1407
1408/* convert to SPDIF status bits from HDA SPDIF bits
1409 */
1410static unsigned int convert_to_spdif_status(unsigned short val)
1411{
1412 unsigned int sbits = 0;
1413
Takashi Iwai0ba21762007-04-16 11:29:14 +02001414 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001416 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 sbits |= IEC958_AES0_PROFESSIONAL;
1418 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001419 if (sbits & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
1421 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001422 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001424 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001426 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
1428 sbits |= val & (0x7f << 8);
1429 }
1430 return sbits;
1431}
1432
Takashi Iwai0ba21762007-04-16 11:29:14 +02001433static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
1434 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435{
1436 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1437 hda_nid_t nid = kcontrol->private_value;
1438 unsigned short val;
1439 int change;
1440
Ingo Molnar62932df2006-01-16 16:34:20 +01001441 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 codec->spdif_status = ucontrol->value.iec958.status[0] |
1443 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
1444 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
1445 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
1446 val = convert_from_spdif_status(codec->spdif_status);
1447 val |= codec->spdif_ctls & 1;
1448 change = codec->spdif_ctls != val;
1449 codec->spdif_ctls = val;
1450
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001451 if (change) {
1452 snd_hda_codec_write_cache(codec, nid, 0,
1453 AC_VERB_SET_DIGI_CONVERT_1,
1454 val & 0xff);
1455 snd_hda_codec_write_cache(codec, nid, 0,
1456 AC_VERB_SET_DIGI_CONVERT_2,
1457 val >> 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 }
1459
Ingo Molnar62932df2006-01-16 16:34:20 +01001460 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 return change;
1462}
1463
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001464#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
Takashi Iwai0ba21762007-04-16 11:29:14 +02001466static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
1467 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468{
1469 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1470
Takashi Iwai0ba21762007-04-16 11:29:14 +02001471 ucontrol->value.integer.value[0] = codec->spdif_ctls & AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 return 0;
1473}
1474
Takashi Iwai0ba21762007-04-16 11:29:14 +02001475static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
1476 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477{
1478 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1479 hda_nid_t nid = kcontrol->private_value;
1480 unsigned short val;
1481 int change;
1482
Ingo Molnar62932df2006-01-16 16:34:20 +01001483 mutex_lock(&codec->spdif_mutex);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001484 val = codec->spdif_ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02001486 val |= AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 change = codec->spdif_ctls != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001488 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 codec->spdif_ctls = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001490 snd_hda_codec_write_cache(codec, nid, 0,
1491 AC_VERB_SET_DIGI_CONVERT_1,
1492 val & 0xff);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001493 /* unmute amp switch (if any) */
1494 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
Takashi Iwai47fd8302007-08-10 17:11:07 +02001495 (val & AC_DIG1_ENABLE))
1496 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
1497 HDA_AMP_MUTE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 }
Ingo Molnar62932df2006-01-16 16:34:20 +01001499 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 return change;
1501}
1502
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001503static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 {
1505 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1506 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1507 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
1508 .info = snd_hda_spdif_mask_info,
1509 .get = snd_hda_spdif_cmask_get,
1510 },
1511 {
1512 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1513 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1514 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
1515 .info = snd_hda_spdif_mask_info,
1516 .get = snd_hda_spdif_pmask_get,
1517 },
1518 {
1519 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1520 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1521 .info = snd_hda_spdif_mask_info,
1522 .get = snd_hda_spdif_default_get,
1523 .put = snd_hda_spdif_default_put,
1524 },
1525 {
1526 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1527 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
1528 .info = snd_hda_spdif_out_switch_info,
1529 .get = snd_hda_spdif_out_switch_get,
1530 .put = snd_hda_spdif_out_switch_put,
1531 },
1532 { } /* end */
1533};
1534
Takashi Iwai09f99702008-02-04 12:31:13 +01001535#define SPDIF_MAX_IDX 4 /* 4 instances should be enough to probe */
1536
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537/**
1538 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
1539 * @codec: the HDA codec
1540 * @nid: audio out widget NID
1541 *
1542 * Creates controls related with the SPDIF output.
1543 * Called from each patch supporting the SPDIF out.
1544 *
1545 * Returns 0 if successful, or a negative error code.
1546 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02001547int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548{
1549 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001550 struct snd_kcontrol *kctl;
1551 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01001552 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
Takashi Iwai09f99702008-02-04 12:31:13 +01001554 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
1555 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Playback Switch",
1556 idx))
1557 break;
1558 }
1559 if (idx >= SPDIF_MAX_IDX) {
1560 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
1561 return -EBUSY;
1562 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
1564 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwai09f99702008-02-04 12:31:13 +01001565 kctl->id.index = idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 kctl->private_value = nid;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001567 err = snd_ctl_add(codec->bus->card, kctl);
1568 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 return err;
1570 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02001571 codec->spdif_ctls =
Andrew Paprocki3982d172007-12-19 12:13:44 +01001572 snd_hda_codec_read(codec, nid, 0,
1573 AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
1575 return 0;
1576}
1577
1578/*
Takashi Iwai9a081602008-02-12 18:37:26 +01001579 * SPDIF sharing with analog output
1580 */
1581static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
1582 struct snd_ctl_elem_value *ucontrol)
1583{
1584 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
1585 ucontrol->value.integer.value[0] = mout->share_spdif;
1586 return 0;
1587}
1588
1589static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
1590 struct snd_ctl_elem_value *ucontrol)
1591{
1592 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
1593 mout->share_spdif = !!ucontrol->value.integer.value[0];
1594 return 0;
1595}
1596
1597static struct snd_kcontrol_new spdif_share_sw = {
1598 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1599 .name = "IEC958 Default PCM Playback Switch",
1600 .info = snd_ctl_boolean_mono_info,
1601 .get = spdif_share_sw_get,
1602 .put = spdif_share_sw_put,
1603};
1604
1605int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
1606 struct hda_multi_out *mout)
1607{
1608 if (!mout->dig_out_nid)
1609 return 0;
1610 /* ATTENTION: here mout is passed as private_data, instead of codec */
1611 return snd_ctl_add(codec->bus->card,
1612 snd_ctl_new1(&spdif_share_sw, mout));
1613}
1614
1615/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 * SPDIF input
1617 */
1618
1619#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
1620
Takashi Iwai0ba21762007-04-16 11:29:14 +02001621static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
1622 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623{
1624 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1625
1626 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
1627 return 0;
1628}
1629
Takashi Iwai0ba21762007-04-16 11:29:14 +02001630static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
1631 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632{
1633 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1634 hda_nid_t nid = kcontrol->private_value;
1635 unsigned int val = !!ucontrol->value.integer.value[0];
1636 int change;
1637
Ingo Molnar62932df2006-01-16 16:34:20 +01001638 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001640 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001642 snd_hda_codec_write_cache(codec, nid, 0,
1643 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 }
Ingo Molnar62932df2006-01-16 16:34:20 +01001645 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 return change;
1647}
1648
Takashi Iwai0ba21762007-04-16 11:29:14 +02001649static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
1650 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651{
1652 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1653 hda_nid_t nid = kcontrol->private_value;
1654 unsigned short val;
1655 unsigned int sbits;
1656
Andrew Paprocki3982d172007-12-19 12:13:44 +01001657 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 sbits = convert_to_spdif_status(val);
1659 ucontrol->value.iec958.status[0] = sbits;
1660 ucontrol->value.iec958.status[1] = sbits >> 8;
1661 ucontrol->value.iec958.status[2] = sbits >> 16;
1662 ucontrol->value.iec958.status[3] = sbits >> 24;
1663 return 0;
1664}
1665
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001666static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 {
1668 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1669 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH),
1670 .info = snd_hda_spdif_in_switch_info,
1671 .get = snd_hda_spdif_in_switch_get,
1672 .put = snd_hda_spdif_in_switch_put,
1673 },
1674 {
1675 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1676 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1677 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT),
1678 .info = snd_hda_spdif_mask_info,
1679 .get = snd_hda_spdif_in_status_get,
1680 },
1681 { } /* end */
1682};
1683
1684/**
1685 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
1686 * @codec: the HDA codec
1687 * @nid: audio in widget NID
1688 *
1689 * Creates controls related with the SPDIF input.
1690 * Called from each patch supporting the SPDIF in.
1691 *
1692 * Returns 0 if successful, or a negative error code.
1693 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02001694int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695{
1696 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001697 struct snd_kcontrol *kctl;
1698 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01001699 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
Takashi Iwai09f99702008-02-04 12:31:13 +01001701 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
1702 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Capture Switch",
1703 idx))
1704 break;
1705 }
1706 if (idx >= SPDIF_MAX_IDX) {
1707 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
1708 return -EBUSY;
1709 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
1711 kctl = snd_ctl_new1(dig_mix, codec);
1712 kctl->private_value = nid;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001713 err = snd_ctl_add(codec->bus->card, kctl);
1714 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 return err;
1716 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02001717 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01001718 snd_hda_codec_read(codec, nid, 0,
1719 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02001720 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 return 0;
1722}
1723
Takashi Iwaicb53c622007-08-10 17:21:45 +02001724#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001725/*
1726 * command cache
1727 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001729/* build a 32bit cache key with the widget id and the command parameter */
1730#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
1731#define get_cmd_cache_nid(key) ((key) & 0xff)
1732#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
1733
1734/**
1735 * snd_hda_codec_write_cache - send a single command with caching
1736 * @codec: the HDA codec
1737 * @nid: NID to send the command
1738 * @direct: direct flag
1739 * @verb: the verb to send
1740 * @parm: the parameter for the verb
1741 *
1742 * Send a single command without waiting for response.
1743 *
1744 * Returns 0 if successful, or a negative error code.
1745 */
1746int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
1747 int direct, unsigned int verb, unsigned int parm)
1748{
1749 int err;
Takashi Iwaicb53c622007-08-10 17:21:45 +02001750 snd_hda_power_up(codec);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001751 mutex_lock(&codec->bus->cmd_mutex);
1752 err = codec->bus->ops.command(codec, nid, direct, verb, parm);
1753 if (!err) {
1754 struct hda_cache_head *c;
1755 u32 key = build_cmd_cache_key(nid, verb);
1756 c = get_alloc_hash(&codec->cmd_cache, key);
1757 if (c)
1758 c->val = parm;
1759 }
1760 mutex_unlock(&codec->bus->cmd_mutex);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001761 snd_hda_power_down(codec);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001762 return err;
1763}
1764
1765/* resume the all commands from the cache */
1766void snd_hda_codec_resume_cache(struct hda_codec *codec)
1767{
1768 struct hda_cache_head *buffer = codec->cmd_cache.buffer;
1769 int i;
1770
1771 for (i = 0; i < codec->cmd_cache.size; i++, buffer++) {
1772 u32 key = buffer->key;
1773 if (!key)
1774 continue;
1775 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
1776 get_cmd_cache_cmd(key), buffer->val);
1777 }
1778}
1779
1780/**
1781 * snd_hda_sequence_write_cache - sequence writes with caching
1782 * @codec: the HDA codec
1783 * @seq: VERB array to send
1784 *
1785 * Send the commands sequentially from the given array.
1786 * Thte commands are recorded on cache for power-save and resume.
1787 * The array must be terminated with NID=0.
1788 */
1789void snd_hda_sequence_write_cache(struct hda_codec *codec,
1790 const struct hda_verb *seq)
1791{
1792 for (; seq->nid; seq++)
1793 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
1794 seq->param);
1795}
Takashi Iwaicb53c622007-08-10 17:21:45 +02001796#endif /* SND_HDA_NEEDS_RESUME */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001797
Takashi Iwai54d17402005-11-21 16:33:22 +01001798/*
1799 * set power state of the codec
1800 */
1801static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1802 unsigned int power_state)
1803{
Takashi Iwaicb53c622007-08-10 17:21:45 +02001804 hda_nid_t nid;
1805 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01001806
1807 snd_hda_codec_write(codec, fg, 0, AC_VERB_SET_POWER_STATE,
1808 power_state);
Marc Boucherd2595d82008-01-22 15:23:30 +01001809 msleep(10); /* partial workaround for "azx_get_response timeout" */
Takashi Iwai54d17402005-11-21 16:33:22 +01001810
Takashi Iwaicb53c622007-08-10 17:21:45 +02001811 nid = codec->start_nid;
1812 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01001813 unsigned int wcaps = get_wcaps(codec, nid);
1814 if (wcaps & AC_WCAP_POWER) {
1815 unsigned int wid_type = (wcaps & AC_WCAP_TYPE) >>
1816 AC_WCAP_TYPE_SHIFT;
1817 if (wid_type == AC_WID_PIN) {
1818 unsigned int pincap;
1819 /*
1820 * don't power down the widget if it controls
1821 * eapd and EAPD_BTLENABLE is set.
1822 */
1823 pincap = snd_hda_param_read(codec, nid,
1824 AC_PAR_PIN_CAP);
1825 if (pincap & AC_PINCAP_EAPD) {
1826 int eapd = snd_hda_codec_read(codec,
1827 nid, 0,
1828 AC_VERB_GET_EAPD_BTLENABLE, 0);
1829 eapd &= 0x02;
1830 if (power_state == AC_PWRST_D3 && eapd)
1831 continue;
1832 }
Takashi Iwai1194b5b2007-10-10 10:04:26 +02001833 }
Takashi Iwai54d17402005-11-21 16:33:22 +01001834 snd_hda_codec_write(codec, nid, 0,
1835 AC_VERB_SET_POWER_STATE,
1836 power_state);
Takashi Iwai1194b5b2007-10-10 10:04:26 +02001837 }
Takashi Iwai54d17402005-11-21 16:33:22 +01001838 }
1839
Takashi Iwaicb53c622007-08-10 17:21:45 +02001840 if (power_state == AC_PWRST_D0) {
1841 unsigned long end_time;
1842 int state;
Takashi Iwai54d17402005-11-21 16:33:22 +01001843 msleep(10);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001844 /* wait until the codec reachs to D0 */
1845 end_time = jiffies + msecs_to_jiffies(500);
1846 do {
1847 state = snd_hda_codec_read(codec, fg, 0,
1848 AC_VERB_GET_POWER_STATE, 0);
1849 if (state == power_state)
1850 break;
1851 msleep(1);
1852 } while (time_after_eq(end_time, jiffies));
1853 }
Takashi Iwai54d17402005-11-21 16:33:22 +01001854}
1855
Takashi Iwaicb53c622007-08-10 17:21:45 +02001856#ifdef SND_HDA_NEEDS_RESUME
1857/*
1858 * call suspend and power-down; used both from PM and power-save
1859 */
1860static void hda_call_codec_suspend(struct hda_codec *codec)
1861{
1862 if (codec->patch_ops.suspend)
1863 codec->patch_ops.suspend(codec, PMSG_SUSPEND);
1864 hda_set_power_state(codec,
1865 codec->afg ? codec->afg : codec->mfg,
1866 AC_PWRST_D3);
1867#ifdef CONFIG_SND_HDA_POWER_SAVE
1868 cancel_delayed_work(&codec->power_work);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02001869 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02001870 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02001871#endif
1872}
1873
1874/*
1875 * kick up codec; used both from PM and power-save
1876 */
1877static void hda_call_codec_resume(struct hda_codec *codec)
1878{
1879 hda_set_power_state(codec,
1880 codec->afg ? codec->afg : codec->mfg,
1881 AC_PWRST_D0);
1882 if (codec->patch_ops.resume)
1883 codec->patch_ops.resume(codec);
1884 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02001885 if (codec->patch_ops.init)
1886 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001887 snd_hda_codec_resume_amp(codec);
1888 snd_hda_codec_resume_cache(codec);
1889 }
1890}
1891#endif /* SND_HDA_NEEDS_RESUME */
1892
Takashi Iwai54d17402005-11-21 16:33:22 +01001893
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894/**
1895 * snd_hda_build_controls - build mixer controls
1896 * @bus: the BUS
1897 *
1898 * Creates mixer controls for each codec included in the bus.
1899 *
1900 * Returns 0 if successful, otherwise a negative error code.
1901 */
Takashi Iwai756e2b02007-04-16 11:27:07 +02001902int __devinit snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001904 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
Takashi Iwai0ba21762007-04-16 11:29:14 +02001906 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaicb53c622007-08-10 17:21:45 +02001907 int err = 0;
1908 /* fake as if already powered-on */
1909 hda_keep_power_on(codec);
1910 /* then fire up */
1911 hda_set_power_state(codec,
1912 codec->afg ? codec->afg : codec->mfg,
1913 AC_PWRST_D0);
1914 /* continue to initialize... */
1915 if (codec->patch_ops.init)
1916 err = codec->patch_ops.init(codec);
1917 if (!err && codec->patch_ops.build_controls)
1918 err = codec->patch_ops.build_controls(codec);
1919 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 if (err < 0)
1921 return err;
1922 }
1923
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 return 0;
1925}
1926
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927/*
1928 * stream formats
1929 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02001930struct hda_rate_tbl {
1931 unsigned int hz;
1932 unsigned int alsa_bits;
1933 unsigned int hda_fmt;
1934};
1935
1936static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02001938
1939 /* autodetected value used in snd_hda_query_supported_pcm */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 { 8000, SNDRV_PCM_RATE_8000, 0x0500 }, /* 1/6 x 48 */
1941 { 11025, SNDRV_PCM_RATE_11025, 0x4300 }, /* 1/4 x 44 */
1942 { 16000, SNDRV_PCM_RATE_16000, 0x0200 }, /* 1/3 x 48 */
1943 { 22050, SNDRV_PCM_RATE_22050, 0x4100 }, /* 1/2 x 44 */
1944 { 32000, SNDRV_PCM_RATE_32000, 0x0a00 }, /* 2/3 x 48 */
1945 { 44100, SNDRV_PCM_RATE_44100, 0x4000 }, /* 44 */
1946 { 48000, SNDRV_PCM_RATE_48000, 0x0000 }, /* 48 */
1947 { 88200, SNDRV_PCM_RATE_88200, 0x4800 }, /* 2 x 44 */
1948 { 96000, SNDRV_PCM_RATE_96000, 0x0800 }, /* 2 x 48 */
1949 { 176400, SNDRV_PCM_RATE_176400, 0x5800 },/* 4 x 44 */
1950 { 192000, SNDRV_PCM_RATE_192000, 0x1800 }, /* 4 x 48 */
Takashi Iwaia961f9f2007-04-12 13:08:09 +02001951#define AC_PAR_PCM_RATE_BITS 11
1952 /* up to bits 10, 384kHZ isn't supported properly */
1953
1954 /* not autodetected value */
1955 { 9600, SNDRV_PCM_RATE_KNOT, 0x0400 }, /* 1/5 x 48 */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02001956
Takashi Iwaibefdf312005-08-22 13:57:55 +02001957 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958};
1959
1960/**
1961 * snd_hda_calc_stream_format - calculate format bitset
1962 * @rate: the sample rate
1963 * @channels: the number of channels
1964 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
1965 * @maxbps: the max. bps
1966 *
1967 * Calculate the format bitset from the given rate, channels and th PCM format.
1968 *
1969 * Return zero if invalid.
1970 */
1971unsigned int snd_hda_calc_stream_format(unsigned int rate,
1972 unsigned int channels,
1973 unsigned int format,
1974 unsigned int maxbps)
1975{
1976 int i;
1977 unsigned int val = 0;
1978
Takashi Iwaibefdf312005-08-22 13:57:55 +02001979 for (i = 0; rate_bits[i].hz; i++)
1980 if (rate_bits[i].hz == rate) {
1981 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 break;
1983 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02001984 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 snd_printdd("invalid rate %d\n", rate);
1986 return 0;
1987 }
1988
1989 if (channels == 0 || channels > 8) {
1990 snd_printdd("invalid channels %d\n", channels);
1991 return 0;
1992 }
1993 val |= channels - 1;
1994
1995 switch (snd_pcm_format_width(format)) {
1996 case 8: val |= 0x00; break;
1997 case 16: val |= 0x10; break;
1998 case 20:
1999 case 24:
2000 case 32:
2001 if (maxbps >= 32)
2002 val |= 0x40;
2003 else if (maxbps >= 24)
2004 val |= 0x30;
2005 else
2006 val |= 0x20;
2007 break;
2008 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002009 snd_printdd("invalid format width %d\n",
2010 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 return 0;
2012 }
2013
2014 return val;
2015}
2016
2017/**
2018 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
2019 * @codec: the HDA codec
2020 * @nid: NID to query
2021 * @ratesp: the pointer to store the detected rate bitflags
2022 * @formatsp: the pointer to store the detected formats
2023 * @bpsp: the pointer to store the detected format widths
2024 *
2025 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
2026 * or @bsps argument is ignored.
2027 *
2028 * Returns 0 if successful, otherwise a negative error code.
2029 */
2030int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
2031 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
2032{
2033 int i;
2034 unsigned int val, streams;
2035
2036 val = 0;
2037 if (nid != codec->afg &&
Takashi Iwai54d17402005-11-21 16:33:22 +01002038 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
2040 if (val == -1)
2041 return -EIO;
2042 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002043 if (!val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
2045
2046 if (ratesp) {
2047 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02002048 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02002050 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 }
2052 *ratesp = rates;
2053 }
2054
2055 if (formatsp || bpsp) {
2056 u64 formats = 0;
2057 unsigned int bps;
2058 unsigned int wcaps;
2059
Takashi Iwai54d17402005-11-21 16:33:22 +01002060 wcaps = get_wcaps(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
2062 if (streams == -1)
2063 return -EIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002064 if (!streams) {
2065 streams = snd_hda_param_read(codec, codec->afg,
2066 AC_PAR_STREAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 if (streams == -1)
2068 return -EIO;
2069 }
2070
2071 bps = 0;
2072 if (streams & AC_SUPFMT_PCM) {
2073 if (val & AC_SUPPCM_BITS_8) {
2074 formats |= SNDRV_PCM_FMTBIT_U8;
2075 bps = 8;
2076 }
2077 if (val & AC_SUPPCM_BITS_16) {
2078 formats |= SNDRV_PCM_FMTBIT_S16_LE;
2079 bps = 16;
2080 }
2081 if (wcaps & AC_WCAP_DIGITAL) {
2082 if (val & AC_SUPPCM_BITS_32)
2083 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
2084 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
2085 formats |= SNDRV_PCM_FMTBIT_S32_LE;
2086 if (val & AC_SUPPCM_BITS_24)
2087 bps = 24;
2088 else if (val & AC_SUPPCM_BITS_20)
2089 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002090 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
2091 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 formats |= SNDRV_PCM_FMTBIT_S32_LE;
2093 if (val & AC_SUPPCM_BITS_32)
2094 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 else if (val & AC_SUPPCM_BITS_24)
2096 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02002097 else if (val & AC_SUPPCM_BITS_20)
2098 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 }
2100 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002101 else if (streams == AC_SUPFMT_FLOAT32) {
2102 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
2104 bps = 32;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002105 } else if (streams == AC_SUPFMT_AC3) {
2106 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 /* temporary hack: we have still no proper support
2108 * for the direct AC3 stream...
2109 */
2110 formats |= SNDRV_PCM_FMTBIT_U8;
2111 bps = 8;
2112 }
2113 if (formatsp)
2114 *formatsp = formats;
2115 if (bpsp)
2116 *bpsp = bps;
2117 }
2118
2119 return 0;
2120}
2121
2122/**
Takashi Iwai0ba21762007-04-16 11:29:14 +02002123 * snd_hda_is_supported_format - check whether the given node supports
2124 * the format val
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 *
2126 * Returns 1 if supported, 0 if not.
2127 */
2128int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
2129 unsigned int format)
2130{
2131 int i;
2132 unsigned int val = 0, rate, stream;
2133
2134 if (nid != codec->afg &&
Takashi Iwai54d17402005-11-21 16:33:22 +01002135 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
2137 if (val == -1)
2138 return 0;
2139 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002140 if (!val) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
2142 if (val == -1)
2143 return 0;
2144 }
2145
2146 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02002147 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02002148 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 if (val & (1 << i))
2150 break;
2151 return 0;
2152 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02002153 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 return 0;
2155
2156 stream = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
2157 if (stream == -1)
2158 return 0;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002159 if (!stream && nid != codec->afg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 stream = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002161 if (!stream || stream == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 return 0;
2163
2164 if (stream & AC_SUPFMT_PCM) {
2165 switch (format & 0xf0) {
2166 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002167 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 return 0;
2169 break;
2170 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002171 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 return 0;
2173 break;
2174 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002175 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 return 0;
2177 break;
2178 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002179 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 return 0;
2181 break;
2182 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002183 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 return 0;
2185 break;
2186 default:
2187 return 0;
2188 }
2189 } else {
2190 /* FIXME: check for float32 and AC3? */
2191 }
2192
2193 return 1;
2194}
2195
2196/*
2197 * PCM stuff
2198 */
2199static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
2200 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002201 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202{
2203 return 0;
2204}
2205
2206static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
2207 struct hda_codec *codec,
2208 unsigned int stream_tag,
2209 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002210 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211{
2212 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
2213 return 0;
2214}
2215
2216static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
2217 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002218 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219{
Takashi Iwai888afa12008-03-18 09:57:50 +01002220 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 return 0;
2222}
2223
Takashi Iwai0ba21762007-04-16 11:29:14 +02002224static int __devinit set_pcm_default_values(struct hda_codec *codec,
2225 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226{
Takashi Iwai0ba21762007-04-16 11:29:14 +02002227 /* query support PCM information from the given NID */
2228 if (info->nid && (!info->rates || !info->formats)) {
2229 snd_hda_query_supported_pcm(codec, info->nid,
2230 info->rates ? NULL : &info->rates,
2231 info->formats ? NULL : &info->formats,
2232 info->maxbps ? NULL : &info->maxbps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 }
2234 if (info->ops.open == NULL)
2235 info->ops.open = hda_pcm_default_open_close;
2236 if (info->ops.close == NULL)
2237 info->ops.close = hda_pcm_default_open_close;
2238 if (info->ops.prepare == NULL) {
2239 snd_assert(info->nid, return -EINVAL);
2240 info->ops.prepare = hda_pcm_default_prepare;
2241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 if (info->ops.cleanup == NULL) {
2243 snd_assert(info->nid, return -EINVAL);
2244 info->ops.cleanup = hda_pcm_default_cleanup;
2245 }
2246 return 0;
2247}
2248
2249/**
2250 * snd_hda_build_pcms - build PCM information
2251 * @bus: the BUS
2252 *
2253 * Create PCM information for each codec included in the bus.
2254 *
2255 * The build_pcms codec patch is requested to set up codec->num_pcms and
2256 * codec->pcm_info properly. The array is referred by the top-level driver
2257 * to create its PCM instances.
2258 * The allocated codec->pcm_info should be released in codec->patch_ops.free
2259 * callback.
2260 *
2261 * At least, substreams, channels_min and channels_max must be filled for
2262 * each stream. substreams = 0 indicates that the stream doesn't exist.
2263 * When rates and/or formats are zero, the supported values are queried
2264 * from the given nid. The nid is used also by the default ops.prepare
2265 * and ops.cleanup callbacks.
2266 *
2267 * The driver needs to call ops.open in its open callback. Similarly,
2268 * ops.close is supposed to be called in the close callback.
2269 * ops.prepare should be called in the prepare or hw_params callback
2270 * with the proper parameters for set up.
2271 * ops.cleanup should be called in hw_free for clean up of streams.
2272 *
2273 * This function returns 0 if successfull, or a negative error code.
2274 */
Takashi Iwai756e2b02007-04-16 11:27:07 +02002275int __devinit snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276{
Takashi Iwai0ba21762007-04-16 11:29:14 +02002277 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278
Takashi Iwai0ba21762007-04-16 11:29:14 +02002279 list_for_each_entry(codec, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 unsigned int pcm, s;
2281 int err;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002282 if (!codec->patch_ops.build_pcms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 continue;
2284 err = codec->patch_ops.build_pcms(codec);
2285 if (err < 0)
2286 return err;
2287 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2288 for (s = 0; s < 2; s++) {
2289 struct hda_pcm_stream *info;
2290 info = &codec->pcm_info[pcm].stream[s];
Takashi Iwai0ba21762007-04-16 11:29:14 +02002291 if (!info->substreams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 continue;
2293 err = set_pcm_default_values(codec, info);
2294 if (err < 0)
2295 return err;
2296 }
2297 }
2298 }
2299 return 0;
2300}
2301
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302/**
2303 * snd_hda_check_board_config - compare the current codec with the config table
2304 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002305 * @num_configs: number of config enums
2306 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 * @tbl: configuration table, terminated by null entries
2308 *
2309 * Compares the modelname or PCI subsystem id of the current codec with the
2310 * given configuration table. If a matching entry is found, returns its
2311 * config value (supposed to be 0 or positive).
2312 *
2313 * If no entries are matching, the function returns a negative value.
2314 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002315int snd_hda_check_board_config(struct hda_codec *codec,
2316 int num_configs, const char **models,
2317 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318{
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002319 if (codec->bus->modelname && models) {
2320 int i;
2321 for (i = 0; i < num_configs; i++) {
2322 if (models[i] &&
2323 !strcmp(codec->bus->modelname, models[i])) {
2324 snd_printd(KERN_INFO "hda_codec: model '%s' is "
2325 "selected\n", models[i]);
2326 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 }
2328 }
2329 }
2330
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002331 if (!codec->bus->pci || !tbl)
2332 return -1;
2333
2334 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
2335 if (!tbl)
2336 return -1;
2337 if (tbl->value >= 0 && tbl->value < num_configs) {
2338#ifdef CONFIG_SND_DEBUG_DETECT
2339 char tmp[10];
2340 const char *model = NULL;
2341 if (models)
2342 model = models[tbl->value];
2343 if (!model) {
2344 sprintf(tmp, "#%d", tbl->value);
2345 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002347 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
2348 "for config %x:%x (%s)\n",
2349 model, tbl->subvendor, tbl->subdevice,
2350 (tbl->name ? tbl->name : "Unknown device"));
2351#endif
2352 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 }
2354 return -1;
2355}
2356
2357/**
2358 * snd_hda_add_new_ctls - create controls from the array
2359 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002360 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 *
2362 * This helper function creates and add new controls in the given array.
2363 * The array must be terminated with an empty entry as terminator.
2364 *
2365 * Returns 0 if successful, or a negative error code.
2366 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002367int snd_hda_add_new_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368{
Takashi Iwaicb53c622007-08-10 17:21:45 +02002369 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370
2371 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01002372 struct snd_kcontrol *kctl;
2373 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002374 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01002375 return -ENOMEM;
2376 err = snd_ctl_add(codec->bus->card, kctl);
2377 if (err < 0) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002378 if (!codec->addr)
Takashi Iwai54d17402005-11-21 16:33:22 +01002379 return err;
2380 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002381 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01002382 return -ENOMEM;
2383 kctl->id.device = codec->addr;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002384 err = snd_ctl_add(codec->bus->card, kctl);
2385 if (err < 0)
Takashi Iwai54d17402005-11-21 16:33:22 +01002386 return err;
2387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 }
2389 return 0;
2390}
2391
Takashi Iwaicb53c622007-08-10 17:21:45 +02002392#ifdef CONFIG_SND_HDA_POWER_SAVE
2393static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2394 unsigned int power_state);
2395
2396static void hda_power_work(struct work_struct *work)
2397{
2398 struct hda_codec *codec =
2399 container_of(work, struct hda_codec, power_work.work);
2400
Maxim Levitsky2e492462007-09-03 15:26:57 +02002401 if (!codec->power_on || codec->power_count) {
2402 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002403 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02002404 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02002405
2406 hda_call_codec_suspend(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002407 if (codec->bus->ops.pm_notify)
2408 codec->bus->ops.pm_notify(codec);
2409}
2410
2411static void hda_keep_power_on(struct hda_codec *codec)
2412{
2413 codec->power_count++;
2414 codec->power_on = 1;
2415}
2416
2417void snd_hda_power_up(struct hda_codec *codec)
2418{
2419 codec->power_count++;
Takashi Iwaia221e282007-08-16 16:35:33 +02002420 if (codec->power_on || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02002421 return;
2422
2423 codec->power_on = 1;
2424 if (codec->bus->ops.pm_notify)
2425 codec->bus->ops.pm_notify(codec);
2426 hda_call_codec_resume(codec);
2427 cancel_delayed_work(&codec->power_work);
Takashi Iwaia221e282007-08-16 16:35:33 +02002428 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002429}
2430
2431void snd_hda_power_down(struct hda_codec *codec)
2432{
2433 --codec->power_count;
Takashi Iwaia221e282007-08-16 16:35:33 +02002434 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02002435 return;
Takashi Iwaia221e282007-08-16 16:35:33 +02002436 if (power_save) {
2437 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002438 schedule_delayed_work(&codec->power_work,
2439 msecs_to_jiffies(power_save * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02002440 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02002441}
2442
2443int snd_hda_check_amp_list_power(struct hda_codec *codec,
2444 struct hda_loopback_check *check,
2445 hda_nid_t nid)
2446{
2447 struct hda_amp_list *p;
2448 int ch, v;
2449
2450 if (!check->amplist)
2451 return 0;
2452 for (p = check->amplist; p->nid; p++) {
2453 if (p->nid == nid)
2454 break;
2455 }
2456 if (!p->nid)
2457 return 0; /* nothing changed */
2458
2459 for (p = check->amplist; p->nid; p++) {
2460 for (ch = 0; ch < 2; ch++) {
2461 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
2462 p->idx);
2463 if (!(v & HDA_AMP_MUTE) && v > 0) {
2464 if (!check->power_on) {
2465 check->power_on = 1;
2466 snd_hda_power_up(codec);
2467 }
2468 return 1;
2469 }
2470 }
2471 }
2472 if (check->power_on) {
2473 check->power_on = 0;
2474 snd_hda_power_down(codec);
2475 }
2476 return 0;
2477}
2478#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002480/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002481 * Channel mode helper
2482 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002483int snd_hda_ch_mode_info(struct hda_codec *codec,
2484 struct snd_ctl_elem_info *uinfo,
2485 const struct hda_channel_mode *chmode,
2486 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002487{
2488 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2489 uinfo->count = 1;
2490 uinfo->value.enumerated.items = num_chmodes;
2491 if (uinfo->value.enumerated.item >= num_chmodes)
2492 uinfo->value.enumerated.item = num_chmodes - 1;
2493 sprintf(uinfo->value.enumerated.name, "%dch",
2494 chmode[uinfo->value.enumerated.item].channels);
2495 return 0;
2496}
2497
Takashi Iwai0ba21762007-04-16 11:29:14 +02002498int snd_hda_ch_mode_get(struct hda_codec *codec,
2499 struct snd_ctl_elem_value *ucontrol,
2500 const struct hda_channel_mode *chmode,
2501 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002502 int max_channels)
2503{
2504 int i;
2505
2506 for (i = 0; i < num_chmodes; i++) {
2507 if (max_channels == chmode[i].channels) {
2508 ucontrol->value.enumerated.item[0] = i;
2509 break;
2510 }
2511 }
2512 return 0;
2513}
2514
Takashi Iwai0ba21762007-04-16 11:29:14 +02002515int snd_hda_ch_mode_put(struct hda_codec *codec,
2516 struct snd_ctl_elem_value *ucontrol,
2517 const struct hda_channel_mode *chmode,
2518 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002519 int *max_channelsp)
2520{
2521 unsigned int mode;
2522
2523 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01002524 if (mode >= num_chmodes)
2525 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002526 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002527 return 0;
2528 /* change the current channel setting */
2529 *max_channelsp = chmode[mode].channels;
2530 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002531 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002532 return 1;
2533}
2534
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535/*
2536 * input MUX helper
2537 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002538int snd_hda_input_mux_info(const struct hda_input_mux *imux,
2539 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540{
2541 unsigned int index;
2542
2543 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2544 uinfo->count = 1;
2545 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02002546 if (!imux->num_items)
2547 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 index = uinfo->value.enumerated.item;
2549 if (index >= imux->num_items)
2550 index = imux->num_items - 1;
2551 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
2552 return 0;
2553}
2554
Takashi Iwai0ba21762007-04-16 11:29:14 +02002555int snd_hda_input_mux_put(struct hda_codec *codec,
2556 const struct hda_input_mux *imux,
2557 struct snd_ctl_elem_value *ucontrol,
2558 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 unsigned int *cur_val)
2560{
2561 unsigned int idx;
2562
Takashi Iwai5513b0c2007-10-09 11:58:41 +02002563 if (!imux->num_items)
2564 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 idx = ucontrol->value.enumerated.item[0];
2566 if (idx >= imux->num_items)
2567 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002568 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002570 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
2571 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 *cur_val = idx;
2573 return 1;
2574}
2575
2576
2577/*
2578 * Multi-channel / digital-out PCM helper functions
2579 */
2580
Takashi Iwai6b97eb42007-04-05 14:51:48 +02002581/* setup SPDIF output stream */
2582static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
2583 unsigned int stream_tag, unsigned int format)
2584{
2585 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
2586 if (codec->spdif_ctls & AC_DIG1_ENABLE)
2587 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1,
2588 codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff);
2589 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
2590 /* turn on again (if needed) */
2591 if (codec->spdif_ctls & AC_DIG1_ENABLE)
2592 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1,
2593 codec->spdif_ctls & 0xff);
2594}
2595
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596/*
2597 * open the digital out in the exclusive mode
2598 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002599int snd_hda_multi_out_dig_open(struct hda_codec *codec,
2600 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601{
Ingo Molnar62932df2006-01-16 16:34:20 +01002602 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02002603 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
2604 /* already opened as analog dup; reset it once */
Takashi Iwai888afa12008-03-18 09:57:50 +01002605 snd_hda_codec_cleanup_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01002607 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 return 0;
2609}
2610
Takashi Iwai6b97eb42007-04-05 14:51:48 +02002611int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
2612 struct hda_multi_out *mout,
2613 unsigned int stream_tag,
2614 unsigned int format,
2615 struct snd_pcm_substream *substream)
2616{
2617 mutex_lock(&codec->spdif_mutex);
2618 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
2619 mutex_unlock(&codec->spdif_mutex);
2620 return 0;
2621}
2622
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623/*
2624 * release the digital out
2625 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002626int snd_hda_multi_out_dig_close(struct hda_codec *codec,
2627 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628{
Ingo Molnar62932df2006-01-16 16:34:20 +01002629 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01002631 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 return 0;
2633}
2634
2635/*
2636 * set up more restrictions for analog out
2637 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002638int snd_hda_multi_out_analog_open(struct hda_codec *codec,
2639 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01002640 struct snd_pcm_substream *substream,
2641 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642{
Takashi Iwai9a081602008-02-12 18:37:26 +01002643 struct snd_pcm_runtime *runtime = substream->runtime;
2644 runtime->hw.channels_max = mout->max_channels;
2645 if (mout->dig_out_nid) {
2646 if (!mout->analog_rates) {
2647 mout->analog_rates = hinfo->rates;
2648 mout->analog_formats = hinfo->formats;
2649 mout->analog_maxbps = hinfo->maxbps;
2650 } else {
2651 runtime->hw.rates = mout->analog_rates;
2652 runtime->hw.formats = mout->analog_formats;
2653 hinfo->maxbps = mout->analog_maxbps;
2654 }
2655 if (!mout->spdif_rates) {
2656 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
2657 &mout->spdif_rates,
2658 &mout->spdif_formats,
2659 &mout->spdif_maxbps);
2660 }
2661 mutex_lock(&codec->spdif_mutex);
2662 if (mout->share_spdif) {
2663 runtime->hw.rates &= mout->spdif_rates;
2664 runtime->hw.formats &= mout->spdif_formats;
2665 if (mout->spdif_maxbps < hinfo->maxbps)
2666 hinfo->maxbps = mout->spdif_maxbps;
2667 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02002668 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01002669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 return snd_pcm_hw_constraint_step(substream->runtime, 0,
2671 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
2672}
2673
2674/*
2675 * set up the i/o for analog out
2676 * when the digital out is available, copy the front out to digital out, too.
2677 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002678int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
2679 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 unsigned int stream_tag,
2681 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002682 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683{
2684 hda_nid_t *nids = mout->dac_nids;
2685 int chs = substream->runtime->channels;
2686 int i;
2687
Ingo Molnar62932df2006-01-16 16:34:20 +01002688 mutex_lock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01002689 if (mout->dig_out_nid && mout->share_spdif &&
2690 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02002692 snd_hda_is_supported_format(codec, mout->dig_out_nid,
2693 format) &&
2694 !(codec->spdif_status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02002696 setup_dig_out_stream(codec, mout->dig_out_nid,
2697 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 } else {
2699 mout->dig_out_used = 0;
Takashi Iwai888afa12008-03-18 09:57:50 +01002700 snd_hda_codec_cleanup_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 }
2702 }
Ingo Molnar62932df2006-01-16 16:34:20 +01002703 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704
2705 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002706 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
2707 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02002708 if (!mout->no_share_stream &&
2709 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002711 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
2712 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01002713 /* extra outputs copied from front */
2714 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02002715 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01002716 snd_hda_codec_setup_stream(codec,
2717 mout->extra_out_nid[i],
2718 stream_tag, 0, format);
2719
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 /* surrounds */
2721 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02002722 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002723 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
2724 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02002725 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002726 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
2727 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 }
2729 return 0;
2730}
2731
2732/*
2733 * clean up the setting for analog out
2734 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002735int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
2736 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737{
2738 hda_nid_t *nids = mout->dac_nids;
2739 int i;
2740
2741 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01002742 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01002744 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwai82bc9552006-03-21 11:24:42 +01002745 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
2746 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01002747 snd_hda_codec_cleanup_stream(codec,
2748 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01002749 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai888afa12008-03-18 09:57:50 +01002751 snd_hda_codec_cleanup_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 mout->dig_out_used = 0;
2753 }
Ingo Molnar62932df2006-01-16 16:34:20 +01002754 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 return 0;
2756}
2757
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002758/*
2759 * Helper for automatic ping configuration
2760 */
Kailang Yangdf694da2005-12-05 19:42:22 +01002761
Takashi Iwai12f288b2007-08-02 15:51:59 +02002762static int is_in_nid_list(hda_nid_t nid, hda_nid_t *list)
Kailang Yangdf694da2005-12-05 19:42:22 +01002763{
2764 for (; *list; list++)
2765 if (*list == nid)
2766 return 1;
2767 return 0;
2768}
2769
Steve Longerbeam81937d32007-05-08 15:33:03 +02002770
2771/*
2772 * Sort an associated group of pins according to their sequence numbers.
2773 */
2774static void sort_pins_by_sequence(hda_nid_t * pins, short * sequences,
2775 int num_pins)
2776{
2777 int i, j;
2778 short seq;
2779 hda_nid_t nid;
2780
2781 for (i = 0; i < num_pins; i++) {
2782 for (j = i + 1; j < num_pins; j++) {
2783 if (sequences[i] > sequences[j]) {
2784 seq = sequences[i];
2785 sequences[i] = sequences[j];
2786 sequences[j] = seq;
2787 nid = pins[i];
2788 pins[i] = pins[j];
2789 pins[j] = nid;
2790 }
2791 }
2792 }
2793}
2794
2795
Takashi Iwai82bc9552006-03-21 11:24:42 +01002796/*
2797 * Parse all pin widgets and store the useful pin nids to cfg
2798 *
2799 * The number of line-outs or any primary output is stored in line_outs,
2800 * and the corresponding output pins are assigned to line_out_pins[],
2801 * in the order of front, rear, CLFE, side, ...
2802 *
2803 * If more extra outputs (speaker and headphone) are found, the pins are
Takashi Iwaieb06ed82006-09-20 17:10:27 +02002804 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
Takashi Iwai82bc9552006-03-21 11:24:42 +01002805 * is detected, one of speaker of HP pins is assigned as the primary
2806 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
2807 * if any analog output exists.
2808 *
2809 * The analog input pins are assigned to input_pins array.
2810 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
2811 * respectively.
2812 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002813int snd_hda_parse_pin_def_config(struct hda_codec *codec,
2814 struct auto_pin_cfg *cfg,
2815 hda_nid_t *ignore_nids)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002816{
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01002817 hda_nid_t nid, end_nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02002818 short seq, assoc_line_out, assoc_speaker;
2819 short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
2820 short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
Takashi Iwaif889fa92007-10-31 15:49:32 +01002821 short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002822
2823 memset(cfg, 0, sizeof(*cfg));
2824
Steve Longerbeam81937d32007-05-08 15:33:03 +02002825 memset(sequences_line_out, 0, sizeof(sequences_line_out));
2826 memset(sequences_speaker, 0, sizeof(sequences_speaker));
Takashi Iwaif889fa92007-10-31 15:49:32 +01002827 memset(sequences_hp, 0, sizeof(sequences_hp));
Steve Longerbeam81937d32007-05-08 15:33:03 +02002828 assoc_line_out = assoc_speaker = 0;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002829
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01002830 end_nid = codec->start_nid + codec->num_nodes;
2831 for (nid = codec->start_nid; nid < end_nid; nid++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01002832 unsigned int wid_caps = get_wcaps(codec, nid);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002833 unsigned int wid_type =
2834 (wid_caps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002835 unsigned int def_conf;
2836 short assoc, loc;
2837
2838 /* read all default configuration for pin complex */
2839 if (wid_type != AC_WID_PIN)
2840 continue;
Kailang Yangdf694da2005-12-05 19:42:22 +01002841 /* ignore the given nids (e.g. pc-beep returns error) */
2842 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
2843 continue;
2844
Takashi Iwai0ba21762007-04-16 11:29:14 +02002845 def_conf = snd_hda_codec_read(codec, nid, 0,
2846 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002847 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
2848 continue;
2849 loc = get_defcfg_location(def_conf);
2850 switch (get_defcfg_device(def_conf)) {
2851 case AC_JACK_LINE_OUT:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002852 seq = get_defcfg_sequence(def_conf);
2853 assoc = get_defcfg_association(def_conf);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01002854
2855 if (!(wid_caps & AC_WCAP_STEREO))
2856 if (!cfg->mono_out_pin)
2857 cfg->mono_out_pin = nid;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002858 if (!assoc)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002859 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002860 if (!assoc_line_out)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002861 assoc_line_out = assoc;
2862 else if (assoc_line_out != assoc)
2863 continue;
2864 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
2865 continue;
2866 cfg->line_out_pins[cfg->line_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02002867 sequences_line_out[cfg->line_outs] = seq;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002868 cfg->line_outs++;
2869 break;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01002870 case AC_JACK_SPEAKER:
Steve Longerbeam81937d32007-05-08 15:33:03 +02002871 seq = get_defcfg_sequence(def_conf);
2872 assoc = get_defcfg_association(def_conf);
2873 if (! assoc)
2874 continue;
2875 if (! assoc_speaker)
2876 assoc_speaker = assoc;
2877 else if (assoc_speaker != assoc)
2878 continue;
Takashi Iwai82bc9552006-03-21 11:24:42 +01002879 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
2880 continue;
2881 cfg->speaker_pins[cfg->speaker_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02002882 sequences_speaker[cfg->speaker_outs] = seq;
Takashi Iwai82bc9552006-03-21 11:24:42 +01002883 cfg->speaker_outs++;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01002884 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002885 case AC_JACK_HP_OUT:
Takashi Iwaif889fa92007-10-31 15:49:32 +01002886 seq = get_defcfg_sequence(def_conf);
2887 assoc = get_defcfg_association(def_conf);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02002888 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
2889 continue;
2890 cfg->hp_pins[cfg->hp_outs] = nid;
Takashi Iwaif889fa92007-10-31 15:49:32 +01002891 sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
Takashi Iwaieb06ed82006-09-20 17:10:27 +02002892 cfg->hp_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002893 break;
Takashi Iwai314634b2006-09-21 11:56:18 +02002894 case AC_JACK_MIC_IN: {
2895 int preferred, alt;
2896 if (loc == AC_JACK_LOC_FRONT) {
2897 preferred = AUTO_PIN_FRONT_MIC;
2898 alt = AUTO_PIN_MIC;
2899 } else {
2900 preferred = AUTO_PIN_MIC;
2901 alt = AUTO_PIN_FRONT_MIC;
2902 }
2903 if (!cfg->input_pins[preferred])
2904 cfg->input_pins[preferred] = nid;
2905 else if (!cfg->input_pins[alt])
2906 cfg->input_pins[alt] = nid;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002907 break;
Takashi Iwai314634b2006-09-21 11:56:18 +02002908 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002909 case AC_JACK_LINE_IN:
2910 if (loc == AC_JACK_LOC_FRONT)
2911 cfg->input_pins[AUTO_PIN_FRONT_LINE] = nid;
2912 else
2913 cfg->input_pins[AUTO_PIN_LINE] = nid;
2914 break;
2915 case AC_JACK_CD:
2916 cfg->input_pins[AUTO_PIN_CD] = nid;
2917 break;
2918 case AC_JACK_AUX:
2919 cfg->input_pins[AUTO_PIN_AUX] = nid;
2920 break;
2921 case AC_JACK_SPDIF_OUT:
2922 cfg->dig_out_pin = nid;
2923 break;
2924 case AC_JACK_SPDIF_IN:
2925 cfg->dig_in_pin = nid;
2926 break;
2927 }
2928 }
2929
Takashi Iwai5832fcf2008-02-12 18:30:12 +01002930 /* FIX-UP:
2931 * If no line-out is defined but multiple HPs are found,
2932 * some of them might be the real line-outs.
2933 */
2934 if (!cfg->line_outs && cfg->hp_outs > 1) {
2935 int i = 0;
2936 while (i < cfg->hp_outs) {
2937 /* The real HPs should have the sequence 0x0f */
2938 if ((sequences_hp[i] & 0x0f) == 0x0f) {
2939 i++;
2940 continue;
2941 }
2942 /* Move it to the line-out table */
2943 cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
2944 sequences_line_out[cfg->line_outs] = sequences_hp[i];
2945 cfg->line_outs++;
2946 cfg->hp_outs--;
2947 memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
2948 sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
2949 memmove(sequences_hp + i - 1, sequences_hp + i,
2950 sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
2951 }
2952 }
2953
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002954 /* sort by sequence */
Steve Longerbeam81937d32007-05-08 15:33:03 +02002955 sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
2956 cfg->line_outs);
2957 sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
2958 cfg->speaker_outs);
Takashi Iwaif889fa92007-10-31 15:49:32 +01002959 sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
2960 cfg->hp_outs);
Steve Longerbeam81937d32007-05-08 15:33:03 +02002961
Takashi Iwaif889fa92007-10-31 15:49:32 +01002962 /* if we have only one mic, make it AUTO_PIN_MIC */
2963 if (!cfg->input_pins[AUTO_PIN_MIC] &&
2964 cfg->input_pins[AUTO_PIN_FRONT_MIC]) {
2965 cfg->input_pins[AUTO_PIN_MIC] =
2966 cfg->input_pins[AUTO_PIN_FRONT_MIC];
2967 cfg->input_pins[AUTO_PIN_FRONT_MIC] = 0;
2968 }
2969 /* ditto for line-in */
2970 if (!cfg->input_pins[AUTO_PIN_LINE] &&
2971 cfg->input_pins[AUTO_PIN_FRONT_LINE]) {
2972 cfg->input_pins[AUTO_PIN_LINE] =
2973 cfg->input_pins[AUTO_PIN_FRONT_LINE];
2974 cfg->input_pins[AUTO_PIN_FRONT_LINE] = 0;
2975 }
2976
Steve Longerbeam81937d32007-05-08 15:33:03 +02002977 /*
2978 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
2979 * as a primary output
2980 */
2981 if (!cfg->line_outs) {
2982 if (cfg->speaker_outs) {
2983 cfg->line_outs = cfg->speaker_outs;
2984 memcpy(cfg->line_out_pins, cfg->speaker_pins,
2985 sizeof(cfg->speaker_pins));
2986 cfg->speaker_outs = 0;
2987 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
2988 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
2989 } else if (cfg->hp_outs) {
2990 cfg->line_outs = cfg->hp_outs;
2991 memcpy(cfg->line_out_pins, cfg->hp_pins,
2992 sizeof(cfg->hp_pins));
2993 cfg->hp_outs = 0;
2994 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
2995 cfg->line_out_type = AUTO_PIN_HP_OUT;
2996 }
2997 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002998
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02002999 /* Reorder the surround channels
3000 * ALSA sequence is front/surr/clfe/side
3001 * HDA sequence is:
3002 * 4-ch: front/surr => OK as it is
3003 * 6-ch: front/clfe/surr
Takashi Iwai9422db42007-04-20 16:11:43 +02003004 * 8-ch: front/clfe/rear/side|fc
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02003005 */
3006 switch (cfg->line_outs) {
3007 case 3:
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02003008 case 4:
3009 nid = cfg->line_out_pins[1];
Takashi Iwai9422db42007-04-20 16:11:43 +02003010 cfg->line_out_pins[1] = cfg->line_out_pins[2];
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02003011 cfg->line_out_pins[2] = nid;
3012 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003013 }
3014
Takashi Iwai82bc9552006-03-21 11:24:42 +01003015 /*
3016 * debug prints of the parsed results
3017 */
3018 snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3019 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
3020 cfg->line_out_pins[2], cfg->line_out_pins[3],
3021 cfg->line_out_pins[4]);
3022 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3023 cfg->speaker_outs, cfg->speaker_pins[0],
3024 cfg->speaker_pins[1], cfg->speaker_pins[2],
3025 cfg->speaker_pins[3], cfg->speaker_pins[4]);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02003026 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3027 cfg->hp_outs, cfg->hp_pins[0],
3028 cfg->hp_pins[1], cfg->hp_pins[2],
3029 cfg->hp_pins[3], cfg->hp_pins[4]);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01003030 snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
Takashi Iwai82bc9552006-03-21 11:24:42 +01003031 snd_printd(" inputs: mic=0x%x, fmic=0x%x, line=0x%x, fline=0x%x,"
3032 " cd=0x%x, aux=0x%x\n",
3033 cfg->input_pins[AUTO_PIN_MIC],
3034 cfg->input_pins[AUTO_PIN_FRONT_MIC],
3035 cfg->input_pins[AUTO_PIN_LINE],
3036 cfg->input_pins[AUTO_PIN_FRONT_LINE],
3037 cfg->input_pins[AUTO_PIN_CD],
3038 cfg->input_pins[AUTO_PIN_AUX]);
3039
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003040 return 0;
3041}
3042
Takashi Iwai4a471b72005-12-07 13:56:29 +01003043/* labels for input pins */
3044const char *auto_pin_cfg_labels[AUTO_PIN_LAST] = {
3045 "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux"
3046};
3047
3048
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049#ifdef CONFIG_PM
3050/*
3051 * power management
3052 */
3053
3054/**
3055 * snd_hda_suspend - suspend the codecs
3056 * @bus: the HDA bus
3057 * @state: suspsend state
3058 *
3059 * Returns 0 if successful.
3060 */
3061int snd_hda_suspend(struct hda_bus *bus, pm_message_t state)
3062{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003063 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064
Takashi Iwai0ba21762007-04-16 11:29:14 +02003065 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai0b7a2e92007-08-14 15:18:26 +02003066#ifdef CONFIG_SND_HDA_POWER_SAVE
3067 if (!codec->power_on)
3068 continue;
3069#endif
Takashi Iwaicb53c622007-08-10 17:21:45 +02003070 hda_call_codec_suspend(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 }
3072 return 0;
3073}
3074
3075/**
3076 * snd_hda_resume - resume the codecs
3077 * @bus: the HDA bus
3078 * @state: resume state
3079 *
3080 * Returns 0 if successful.
Takashi Iwaicb53c622007-08-10 17:21:45 +02003081 *
3082 * This fucntion is defined only when POWER_SAVE isn't set.
3083 * In the power-save mode, the codec is resumed dynamically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 */
3085int snd_hda_resume(struct hda_bus *bus)
3086{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003087 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088
Takashi Iwai0ba21762007-04-16 11:29:14 +02003089 list_for_each_entry(codec, &bus->codec_list, list) {
Maxim Levitskyd804ad92007-09-03 15:28:04 +02003090 if (snd_hda_codec_needs_resume(codec))
3091 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 return 0;
3094}
Maxim Levitskyd804ad92007-09-03 15:28:04 +02003095#ifdef CONFIG_SND_HDA_POWER_SAVE
3096int snd_hda_codecs_inuse(struct hda_bus *bus)
3097{
3098 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099
Maxim Levitskyd804ad92007-09-03 15:28:04 +02003100 list_for_each_entry(codec, &bus->codec_list, list) {
3101 if (snd_hda_codec_needs_resume(codec))
3102 return 1;
3103 }
3104 return 0;
3105}
3106#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107#endif