blob: e70303183c3c1f67b787a17e79ea3a931aaaf3c5 [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
Wei Ni9a10eb22008-09-26 13:45:46 +080097#ifdef CONFIG_SND_HDA_CODEC_NVHDMI
98 snd_hda_preset_nvhdmi,
99#endif
Harvey Harrison3c9a3202008-02-29 11:59:26 +0100100 NULL
101};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Takashi Iwaicb53c622007-08-10 17:21:45 +0200103#ifdef CONFIG_SND_HDA_POWER_SAVE
104static void hda_power_work(struct work_struct *work);
105static void hda_keep_power_on(struct hda_codec *codec);
106#else
107static inline void hda_keep_power_on(struct hda_codec *codec) {}
108#endif
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110/**
111 * snd_hda_codec_read - send a command and get the response
112 * @codec: the HDA codec
113 * @nid: NID to send the command
114 * @direct: direct flag
115 * @verb: the verb to send
116 * @parm: the parameter for the verb
117 *
118 * Send a single command and read the corresponding response.
119 *
120 * Returns the obtained response value, or -1 for an error.
121 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200122unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
123 int direct,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 unsigned int verb, unsigned int parm)
125{
126 unsigned int res;
Takashi Iwaicb53c622007-08-10 17:21:45 +0200127 snd_hda_power_up(codec);
Ingo Molnar62932df2006-01-16 16:34:20 +0100128 mutex_lock(&codec->bus->cmd_mutex);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200129 if (!codec->bus->ops.command(codec, nid, direct, verb, parm))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 res = codec->bus->ops.get_response(codec);
131 else
132 res = (unsigned int)-1;
Ingo Molnar62932df2006-01-16 16:34:20 +0100133 mutex_unlock(&codec->bus->cmd_mutex);
Takashi Iwaicb53c622007-08-10 17:21:45 +0200134 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 return res;
136}
137
138/**
139 * snd_hda_codec_write - send a single command without waiting for response
140 * @codec: the HDA codec
141 * @nid: NID to send the command
142 * @direct: direct flag
143 * @verb: the verb to send
144 * @parm: the parameter for the verb
145 *
146 * Send a single command without waiting for response.
147 *
148 * Returns 0 if successful, or a negative error code.
149 */
150int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
151 unsigned int verb, unsigned int parm)
152{
153 int err;
Takashi Iwaicb53c622007-08-10 17:21:45 +0200154 snd_hda_power_up(codec);
Ingo Molnar62932df2006-01-16 16:34:20 +0100155 mutex_lock(&codec->bus->cmd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 err = codec->bus->ops.command(codec, nid, direct, verb, parm);
Ingo Molnar62932df2006-01-16 16:34:20 +0100157 mutex_unlock(&codec->bus->cmd_mutex);
Takashi Iwaicb53c622007-08-10 17:21:45 +0200158 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 return err;
160}
161
162/**
163 * snd_hda_sequence_write - sequence writes
164 * @codec: the HDA codec
165 * @seq: VERB array to send
166 *
167 * Send the commands sequentially from the given array.
168 * The array must be terminated with NID=0.
169 */
170void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
171{
172 for (; seq->nid; seq++)
173 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
174}
175
176/**
177 * snd_hda_get_sub_nodes - get the range of sub nodes
178 * @codec: the HDA codec
179 * @nid: NID to parse
180 * @start_id: the pointer to store the start NID
181 *
182 * Parse the NID and store the start NID of its sub-nodes.
183 * Returns the number of sub-nodes.
184 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200185int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
186 hda_nid_t *start_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
188 unsigned int parm;
189
190 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
Danny Tholene8a7f132007-09-11 21:41:56 +0200191 if (parm == -1)
192 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 *start_id = (parm >> 16) & 0x7fff;
194 return (int)(parm & 0x7fff);
195}
196
197/**
198 * snd_hda_get_connections - get connection list
199 * @codec: the HDA codec
200 * @nid: NID to parse
201 * @conn_list: connection list array
202 * @max_conns: max. number of connections to store
203 *
204 * Parses the connection list of the given widget and stores the list
205 * of NIDs.
206 *
207 * Returns the number of connections, or a negative error code.
208 */
209int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
210 hda_nid_t *conn_list, int max_conns)
211{
212 unsigned int parm;
Takashi Iwai54d17402005-11-21 16:33:22 +0100213 int i, conn_len, conns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 unsigned int shift, num_elems, mask;
Takashi Iwai54d17402005-11-21 16:33:22 +0100215 hda_nid_t prev_nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Takashi Iwaida3cec32008-08-08 17:12:14 +0200217 if (snd_BUG_ON(!conn_list || max_conns <= 0))
218 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
221 if (parm & AC_CLIST_LONG) {
222 /* long form */
223 shift = 16;
224 num_elems = 2;
225 } else {
226 /* short form */
227 shift = 8;
228 num_elems = 4;
229 }
230 conn_len = parm & AC_CLIST_LENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 mask = (1 << (shift-1)) - 1;
232
Takashi Iwai0ba21762007-04-16 11:29:14 +0200233 if (!conn_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 return 0; /* no connection */
235
236 if (conn_len == 1) {
237 /* single connection */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200238 parm = snd_hda_codec_read(codec, nid, 0,
239 AC_VERB_GET_CONNECT_LIST, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 conn_list[0] = parm & mask;
241 return 1;
242 }
243
244 /* multi connection */
245 conns = 0;
Takashi Iwai54d17402005-11-21 16:33:22 +0100246 prev_nid = 0;
247 for (i = 0; i < conn_len; i++) {
248 int range_val;
249 hda_nid_t val, n;
250
251 if (i % num_elems == 0)
252 parm = snd_hda_codec_read(codec, nid, 0,
253 AC_VERB_GET_CONNECT_LIST, i);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200254 range_val = !!(parm & (1 << (shift-1))); /* ranges */
Takashi Iwai54d17402005-11-21 16:33:22 +0100255 val = parm & mask;
256 parm >>= shift;
257 if (range_val) {
258 /* ranges between the previous and this one */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200259 if (!prev_nid || prev_nid >= val) {
260 snd_printk(KERN_WARNING "hda_codec: "
261 "invalid dep_range_val %x:%x\n",
262 prev_nid, val);
Takashi Iwai54d17402005-11-21 16:33:22 +0100263 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100265 for (n = prev_nid + 1; n <= val; n++) {
266 if (conns >= max_conns) {
Takashi Iwai0ba21762007-04-16 11:29:14 +0200267 snd_printk(KERN_ERR
268 "Too many connections\n");
Takashi Iwai54d17402005-11-21 16:33:22 +0100269 return -EINVAL;
270 }
271 conn_list[conns++] = n;
272 }
273 } else {
274 if (conns >= max_conns) {
275 snd_printk(KERN_ERR "Too many connections\n");
276 return -EINVAL;
277 }
278 conn_list[conns++] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100280 prev_nid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 }
282 return conns;
283}
284
285
286/**
287 * snd_hda_queue_unsol_event - add an unsolicited event to queue
288 * @bus: the BUS
289 * @res: unsolicited event (lower 32bit of RIRB entry)
290 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
291 *
292 * Adds the given event to the queue. The events are processed in
293 * the workqueue asynchronously. Call this function in the interrupt
294 * hanlder when RIRB receives an unsolicited event.
295 *
296 * Returns 0 if successful, or a negative error code.
297 */
298int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
299{
300 struct hda_bus_unsolicited *unsol;
301 unsigned int wp;
302
Takashi Iwai0ba21762007-04-16 11:29:14 +0200303 unsol = bus->unsol;
304 if (!unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 return 0;
306
307 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
308 unsol->wp = wp;
309
310 wp <<= 1;
311 unsol->queue[wp] = res;
312 unsol->queue[wp + 1] = res_ex;
313
Takashi Iwaie250af22006-12-19 17:08:52 +0100314 schedule_work(&unsol->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
316 return 0;
317}
318
319/*
Wu Fengguang5c1d1a92008-10-07 14:17:53 +0800320 * process queued unsolicited events
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 */
David Howellsc4028952006-11-22 14:57:56 +0000322static void process_unsol_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
David Howellsc4028952006-11-22 14:57:56 +0000324 struct hda_bus_unsolicited *unsol =
325 container_of(work, struct hda_bus_unsolicited, work);
326 struct hda_bus *bus = unsol->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 struct hda_codec *codec;
328 unsigned int rp, caddr, res;
329
330 while (unsol->rp != unsol->wp) {
331 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
332 unsol->rp = rp;
333 rp <<= 1;
334 res = unsol->queue[rp];
335 caddr = unsol->queue[rp + 1];
Takashi Iwai0ba21762007-04-16 11:29:14 +0200336 if (!(caddr & (1 << 4))) /* no unsolicited event? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 continue;
338 codec = bus->caddr_tbl[caddr & 0x0f];
339 if (codec && codec->patch_ops.unsol_event)
340 codec->patch_ops.unsol_event(codec, res);
341 }
342}
343
344/*
345 * initialize unsolicited queue
346 */
Takashi Iwai756e2b02007-04-16 11:27:07 +0200347static int __devinit init_unsol_queue(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
349 struct hda_bus_unsolicited *unsol;
350
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100351 if (bus->unsol) /* already initialized */
352 return 0;
353
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200354 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200355 if (!unsol) {
356 snd_printk(KERN_ERR "hda_codec: "
357 "can't allocate unsolicited queue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 return -ENOMEM;
359 }
David Howellsc4028952006-11-22 14:57:56 +0000360 INIT_WORK(&unsol->work, process_unsol_events);
361 unsol->bus = bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 bus->unsol = unsol;
363 return 0;
364}
365
366/*
367 * destructor
368 */
369static void snd_hda_codec_free(struct hda_codec *codec);
370
371static int snd_hda_bus_free(struct hda_bus *bus)
372{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200373 struct hda_codec *codec, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Takashi Iwai0ba21762007-04-16 11:29:14 +0200375 if (!bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 return 0;
377 if (bus->unsol) {
Takashi Iwaie250af22006-12-19 17:08:52 +0100378 flush_scheduled_work();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 kfree(bus->unsol);
380 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200381 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 snd_hda_codec_free(codec);
383 }
384 if (bus->ops.private_free)
385 bus->ops.private_free(bus);
386 kfree(bus);
387 return 0;
388}
389
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100390static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
392 struct hda_bus *bus = device->device_data;
393 return snd_hda_bus_free(bus);
394}
395
396/**
397 * snd_hda_bus_new - create a HDA bus
398 * @card: the card entry
399 * @temp: the template for hda_bus information
400 * @busp: the pointer to store the created bus instance
401 *
402 * Returns 0 if successful, or a negative error code.
403 */
Takashi Iwai756e2b02007-04-16 11:27:07 +0200404int __devinit snd_hda_bus_new(struct snd_card *card,
405 const struct hda_bus_template *temp,
406 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{
408 struct hda_bus *bus;
409 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100410 static struct snd_device_ops dev_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 .dev_free = snd_hda_bus_dev_free,
412 };
413
Takashi Iwaida3cec32008-08-08 17:12:14 +0200414 if (snd_BUG_ON(!temp))
415 return -EINVAL;
416 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
417 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
419 if (busp)
420 *busp = NULL;
421
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200422 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 if (bus == NULL) {
424 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
425 return -ENOMEM;
426 }
427
428 bus->card = card;
429 bus->private_data = temp->private_data;
430 bus->pci = temp->pci;
431 bus->modelname = temp->modelname;
432 bus->ops = temp->ops;
433
Ingo Molnar62932df2006-01-16 16:34:20 +0100434 mutex_init(&bus->cmd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 INIT_LIST_HEAD(&bus->codec_list);
436
Takashi Iwai0ba21762007-04-16 11:29:14 +0200437 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
438 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 snd_hda_bus_free(bus);
440 return err;
441 }
442 if (busp)
443 *busp = bus;
444 return 0;
445}
446
Takashi Iwai82467612007-07-27 19:15:54 +0200447#ifdef CONFIG_SND_HDA_GENERIC
448#define is_generic_config(codec) \
449 (codec->bus->modelname && !strcmp(codec->bus->modelname, "generic"))
450#else
451#define is_generic_config(codec) 0
452#endif
453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454/*
455 * find a matching codec preset
456 */
Takashi Iwai756e2b02007-04-16 11:27:07 +0200457static const struct hda_codec_preset __devinit *
458find_codec_preset(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
460 const struct hda_codec_preset **tbl, *preset;
461
Takashi Iwai82467612007-07-27 19:15:54 +0200462 if (is_generic_config(codec))
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100463 return NULL; /* use the generic parser */
464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 for (tbl = hda_preset_tables; *tbl; tbl++) {
466 for (preset = *tbl; preset->id; preset++) {
467 u32 mask = preset->mask;
Marc Boucherca7cfae2008-01-22 15:32:25 +0100468 if (preset->afg && preset->afg != codec->afg)
469 continue;
470 if (preset->mfg && preset->mfg != codec->mfg)
471 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200472 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 mask = ~0;
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200474 if (preset->id == (codec->vendor_id & mask) &&
Takashi Iwai0ba21762007-04-16 11:29:14 +0200475 (!preset->rev ||
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200476 preset->rev == codec->revision_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 return preset;
478 }
479 }
480 return NULL;
481}
482
483/*
484 * snd_hda_get_codec_name - store the codec name
485 */
486void snd_hda_get_codec_name(struct hda_codec *codec,
487 char *name, int namelen)
488{
489 const struct hda_vendor_id *c;
490 const char *vendor = NULL;
491 u16 vendor_id = codec->vendor_id >> 16;
492 char tmp[16];
493
494 for (c = hda_vendor_ids; c->id; c++) {
495 if (c->id == vendor_id) {
496 vendor = c->name;
497 break;
498 }
499 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200500 if (!vendor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 sprintf(tmp, "Generic %04x", vendor_id);
502 vendor = tmp;
503 }
504 if (codec->preset && codec->preset->name)
505 snprintf(name, namelen, "%s %s", vendor, codec->preset->name);
506 else
Takashi Iwai0ba21762007-04-16 11:29:14 +0200507 snprintf(name, namelen, "%s ID %x", vendor,
508 codec->vendor_id & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509}
510
511/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200512 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 */
Takashi Iwai756e2b02007-04-16 11:27:07 +0200514static void __devinit setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
516 int i, total_nodes;
517 hda_nid_t nid;
518
519 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
520 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +0200521 unsigned int func;
522 func = snd_hda_param_read(codec, nid, AC_PAR_FUNCTION_TYPE);
523 switch (func & 0xff) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200524 case AC_GRP_AUDIO_FUNCTION:
525 codec->afg = nid;
526 break;
527 case AC_GRP_MODEM_FUNCTION:
528 codec->mfg = nid;
529 break;
530 default:
531 break;
532 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534}
535
536/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100537 * read widget caps for each widget and store in cache
538 */
539static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
540{
541 int i;
542 hda_nid_t nid;
543
544 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
545 &codec->start_nid);
546 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200547 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +0100548 return -ENOMEM;
549 nid = codec->start_nid;
550 for (i = 0; i < codec->num_nodes; i++, nid++)
551 codec->wcaps[i] = snd_hda_param_read(codec, nid,
552 AC_PAR_AUDIO_WIDGET_CAP);
553 return 0;
554}
555
556
Takashi Iwai01751f52007-08-10 16:59:39 +0200557static void init_hda_cache(struct hda_cache_rec *cache,
558 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +0200559static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +0200560
Takashi Iwai54d17402005-11-21 16:33:22 +0100561/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 * codec destructor
563 */
564static void snd_hda_codec_free(struct hda_codec *codec)
565{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200566 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 return;
Takashi Iwaicb53c622007-08-10 17:21:45 +0200568#ifdef CONFIG_SND_HDA_POWER_SAVE
569 cancel_delayed_work(&codec->power_work);
Takashi Iwai2525fdc2007-08-15 22:18:22 +0200570 flush_scheduled_work();
Takashi Iwaicb53c622007-08-10 17:21:45 +0200571#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 list_del(&codec->list);
573 codec->bus->caddr_tbl[codec->addr] = NULL;
574 if (codec->patch_ops.free)
575 codec->patch_ops.free(codec);
Takashi Iwai01751f52007-08-10 16:59:39 +0200576 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +0200577 free_hda_cache(&codec->cmd_cache);
Takashi Iwai54d17402005-11-21 16:33:22 +0100578 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 kfree(codec);
580}
581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582/**
583 * snd_hda_codec_new - create a HDA codec
584 * @bus: the bus to assign
585 * @codec_addr: the codec address
586 * @codecp: the pointer to store the generated codec
587 *
588 * Returns 0 if successful, or a negative error code.
589 */
Takashi Iwai756e2b02007-04-16 11:27:07 +0200590int __devinit snd_hda_codec_new(struct hda_bus *bus, unsigned int codec_addr,
591 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
593 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +0200594 char component[31];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 int err;
596
Takashi Iwaida3cec32008-08-08 17:12:14 +0200597 if (snd_BUG_ON(!bus))
598 return -EINVAL;
599 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
600 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
602 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +0200603 snd_printk(KERN_ERR "hda_codec: "
604 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 return -EBUSY;
606 }
607
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200608 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 if (codec == NULL) {
610 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
611 return -ENOMEM;
612 }
613
614 codec->bus = bus;
615 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +0100616 mutex_init(&codec->spdif_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +0200617 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +0200618 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Takashi Iwaicb53c622007-08-10 17:21:45 +0200620#ifdef CONFIG_SND_HDA_POWER_SAVE
621 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
622 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
623 * the caller has to power down appropriatley after initialization
624 * phase.
625 */
626 hda_keep_power_on(codec);
627#endif
628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 list_add_tail(&codec->list, &bus->codec_list);
630 bus->caddr_tbl[codec_addr] = codec;
631
Takashi Iwai0ba21762007-04-16 11:29:14 +0200632 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
633 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +0100634 if (codec->vendor_id == -1)
635 /* read again, hopefully the access method was corrected
636 * in the last read...
637 */
638 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
639 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200640 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
641 AC_PAR_SUBSYSTEM_ID);
642 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
643 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200645 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200646 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200647 snd_printdd("hda_codec: no AFG or MFG node found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 snd_hda_codec_free(codec);
649 return -ENODEV;
650 }
651
Takashi Iwai54d17402005-11-21 16:33:22 +0100652 if (read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg) < 0) {
653 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
654 snd_hda_codec_free(codec);
655 return -ENOMEM;
656 }
657
Takashi Iwai0ba21762007-04-16 11:29:14 +0200658 if (!codec->subsystem_id) {
Takashi Iwai86284e42005-10-11 15:05:54 +0200659 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200660 codec->subsystem_id =
661 snd_hda_codec_read(codec, nid, 0,
662 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +0200663 }
664
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100665 codec->preset = find_codec_preset(codec);
Takashi Iwai43ea1d42007-04-26 19:12:08 +0200666 /* audio codec should override the mixer name */
667 if (codec->afg || !*bus->card->mixername)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 snd_hda_get_codec_name(codec, bus->card->mixername,
669 sizeof(bus->card->mixername));
670
Takashi Iwai82467612007-07-27 19:15:54 +0200671 if (is_generic_config(codec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai82467612007-07-27 19:15:54 +0200673 goto patched;
674 }
Takashi Iwai82467612007-07-27 19:15:54 +0200675 if (codec->preset && codec->preset->patch) {
676 err = codec->preset->patch(codec);
677 goto patched;
678 }
679
680 /* call the default parser */
Takashi Iwai82467612007-07-27 19:15:54 +0200681 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai35a1e0c2007-10-19 08:13:40 +0200682 if (err < 0)
683 printk(KERN_ERR "hda-codec: No codec parser is available\n");
Takashi Iwai82467612007-07-27 19:15:54 +0200684
685 patched:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 if (err < 0) {
687 snd_hda_codec_free(codec);
688 return err;
689 }
690
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100691 if (codec->patch_ops.unsol_event)
692 init_unsol_queue(bus);
693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 snd_hda_codec_proc_new(codec);
Takashi Iwai28073142007-07-27 18:58:06 +0200695#ifdef CONFIG_SND_HDA_HWDEP
696 snd_hda_create_hwdep(codec);
697#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Jaroslav Kyselaba443682008-08-13 20:55:32 +0200699 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id, codec->subsystem_id, codec->revision_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 snd_component_add(codec->bus->card, component);
701
702 if (codecp)
703 *codecp = codec;
704 return 0;
705}
706
707/**
708 * snd_hda_codec_setup_stream - set up the codec for streaming
709 * @codec: the CODEC to set up
710 * @nid: the NID to set up
711 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
712 * @channel_id: channel id to pass, zero based.
713 * @format: stream format.
714 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200715void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
716 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 int channel_id, int format)
718{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200719 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +0200720 return;
721
Takashi Iwai0ba21762007-04-16 11:29:14 +0200722 snd_printdd("hda_codec_setup_stream: "
723 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 nid, stream_tag, channel_id, format);
725 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID,
726 (stream_tag << 4) | channel_id);
727 msleep(1);
728 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, format);
729}
730
Takashi Iwai888afa12008-03-18 09:57:50 +0100731void snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid)
732{
733 if (!nid)
734 return;
735
736 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
737 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
738#if 0 /* keep the format */
739 msleep(1);
740 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0);
741#endif
742}
743
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744/*
745 * amp access functions
746 */
747
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200748/* FIXME: more better hash key? */
749#define HDA_HASH_KEY(nid,dir,idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200751#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
753/* initialize the hash table */
Takashi Iwai01751f52007-08-10 16:59:39 +0200754static void __devinit init_hda_cache(struct hda_cache_rec *cache,
755 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756{
Takashi Iwai01751f52007-08-10 16:59:39 +0200757 memset(cache, 0, sizeof(*cache));
758 memset(cache->hash, 0xff, sizeof(cache->hash));
759 cache->record_size = record_size;
760}
761
Takashi Iwai1fcaee62007-08-23 00:01:09 +0200762static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +0200763{
764 kfree(cache->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765}
766
767/* query the hash. allocate an entry if not found. */
Takashi Iwai01751f52007-08-10 16:59:39 +0200768static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
769 u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770{
Takashi Iwai01751f52007-08-10 16:59:39 +0200771 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
772 u16 cur = cache->hash[idx];
773 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
775 while (cur != 0xffff) {
Takashi Iwai01751f52007-08-10 16:59:39 +0200776 info = (struct hda_cache_head *)(cache->buffer +
777 cur * cache->record_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 if (info->key == key)
779 return info;
780 cur = info->next;
781 }
782
783 /* add a new hash entry */
Takashi Iwai01751f52007-08-10 16:59:39 +0200784 if (cache->num_entries >= cache->size) {
Takashi Iwaid0311662005-11-07 14:38:44 +0100785 /* reallocate the array */
Takashi Iwai01751f52007-08-10 16:59:39 +0200786 unsigned int new_size = cache->size + 64;
787 void *new_buffer;
788 new_buffer = kcalloc(new_size, cache->record_size, GFP_KERNEL);
789 if (!new_buffer) {
Takashi Iwai0ba21762007-04-16 11:29:14 +0200790 snd_printk(KERN_ERR "hda_codec: "
791 "can't malloc amp_info\n");
Takashi Iwaid0311662005-11-07 14:38:44 +0100792 return NULL;
793 }
Takashi Iwai01751f52007-08-10 16:59:39 +0200794 if (cache->buffer) {
795 memcpy(new_buffer, cache->buffer,
796 cache->size * cache->record_size);
797 kfree(cache->buffer);
Takashi Iwaid0311662005-11-07 14:38:44 +0100798 }
Takashi Iwai01751f52007-08-10 16:59:39 +0200799 cache->size = new_size;
800 cache->buffer = new_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 }
Takashi Iwai01751f52007-08-10 16:59:39 +0200802 cur = cache->num_entries++;
803 info = (struct hda_cache_head *)(cache->buffer +
804 cur * cache->record_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 info->key = key;
Takashi Iwai01751f52007-08-10 16:59:39 +0200806 info->val = 0;
807 info->next = cache->hash[idx];
808 cache->hash[idx] = cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
810 return info;
811}
812
Takashi Iwai01751f52007-08-10 16:59:39 +0200813/* query and allocate an amp hash entry */
814static inline struct hda_amp_info *
815get_alloc_amp_hash(struct hda_codec *codec, u32 key)
816{
817 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
818}
819
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820/*
821 * query AMP capabilities for the given widget and direction
822 */
Matthew Ranostay09a99952008-01-24 11:49:21 +0100823u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200825 struct hda_amp_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Takashi Iwai0ba21762007-04-16 11:29:14 +0200827 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
828 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 return 0;
Takashi Iwai01751f52007-08-10 16:59:39 +0200830 if (!(info->head.val & INFO_AMP_CAPS)) {
Takashi Iwai0ba21762007-04-16 11:29:14 +0200831 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 nid = codec->afg;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200833 info->amp_caps = snd_hda_param_read(codec, nid,
834 direction == HDA_OUTPUT ?
835 AC_PAR_AMP_OUT_CAP :
836 AC_PAR_AMP_IN_CAP);
Takashi Iwaib75e53f2007-05-10 16:56:09 +0200837 if (info->amp_caps)
Takashi Iwai01751f52007-08-10 16:59:39 +0200838 info->head.val |= INFO_AMP_CAPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 }
840 return info->amp_caps;
841}
842
Takashi Iwai897cc182007-05-29 19:01:37 +0200843int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
844 unsigned int caps)
845{
846 struct hda_amp_info *info;
847
848 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
849 if (!info)
850 return -EINVAL;
851 info->amp_caps = caps;
Takashi Iwai01751f52007-08-10 16:59:39 +0200852 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai897cc182007-05-29 19:01:37 +0200853 return 0;
854}
855
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856/*
857 * read the current volume to info
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200858 * if the cache exists, read the cache value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200860static unsigned int get_vol_mute(struct hda_codec *codec,
861 struct hda_amp_info *info, hda_nid_t nid,
862 int ch, int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863{
864 u32 val, parm;
865
Takashi Iwai01751f52007-08-10 16:59:39 +0200866 if (info->head.val & INFO_AMP_VOL(ch))
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200867 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
869 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
870 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
871 parm |= index;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200872 val = snd_hda_codec_read(codec, nid, 0,
873 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 info->vol[ch] = val & 0xff;
Takashi Iwai01751f52007-08-10 16:59:39 +0200875 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200876 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877}
878
879/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200880 * write the current volume in info to the h/w and update the cache
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200882static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
Takashi Iwai0ba21762007-04-16 11:29:14 +0200883 hda_nid_t nid, int ch, int direction, int index,
884 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885{
886 u32 parm;
887
888 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
889 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
890 parm |= index << AC_AMP_SET_INDEX_SHIFT;
891 parm |= val;
892 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200893 info->vol[ch] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894}
895
896/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200897 * read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 */
Takashi Iwai834be882006-03-01 14:16:17 +0100899int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
900 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200902 struct hda_amp_info *info;
903 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
904 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200906 return get_vol_mute(codec, info, nid, ch, direction, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907}
908
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200909/*
910 * update the AMP value, mask = bit mask to set, val = the value
911 */
Takashi Iwai834be882006-03-01 14:16:17 +0100912int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
913 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200915 struct hda_amp_info *info;
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200916
Takashi Iwai0ba21762007-04-16 11:29:14 +0200917 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
918 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200920 val &= mask;
921 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
Takashi Iwai82beb8f2007-08-10 17:09:26 +0200922 if (info->vol[ch] == val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200924 put_vol_mute(codec, info, nid, ch, direction, idx, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 return 1;
926}
927
Takashi Iwai47fd8302007-08-10 17:11:07 +0200928/*
929 * update the AMP stereo with the same mask and value
930 */
931int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
932 int direction, int idx, int mask, int val)
933{
934 int ch, ret = 0;
935 for (ch = 0; ch < 2; ch++)
936 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
937 idx, mask, val);
938 return ret;
939}
940
Takashi Iwaicb53c622007-08-10 17:21:45 +0200941#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwaib3ac5632007-08-10 17:03:40 +0200942/* resume the all amp commands from the cache */
943void snd_hda_codec_resume_amp(struct hda_codec *codec)
944{
945 struct hda_amp_info *buffer = codec->amp_cache.buffer;
946 int i;
947
948 for (i = 0; i < codec->amp_cache.size; i++, buffer++) {
949 u32 key = buffer->head.key;
950 hda_nid_t nid;
951 unsigned int idx, dir, ch;
952 if (!key)
953 continue;
954 nid = key & 0xff;
955 idx = (key >> 16) & 0xff;
956 dir = (key >> 24) & 0xff;
957 for (ch = 0; ch < 2; ch++) {
958 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
959 continue;
960 put_vol_mute(codec, buffer, nid, ch, dir, idx,
961 buffer->vol[ch]);
962 }
963 }
964}
Takashi Iwaicb53c622007-08-10 17:21:45 +0200965#endif /* SND_HDA_NEEDS_RESUME */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967/* volume */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200968int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
969 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970{
971 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
972 u16 nid = get_amp_nid(kcontrol);
973 u8 chs = get_amp_channels(kcontrol);
974 int dir = get_amp_direction(kcontrol);
975 u32 caps;
976
977 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200978 /* num steps */
979 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
980 if (!caps) {
981 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +0100982 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
983 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 return -EINVAL;
985 }
986 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
987 uinfo->count = chs == 3 ? 2 : 1;
988 uinfo->value.integer.min = 0;
989 uinfo->value.integer.max = caps;
990 return 0;
991}
992
Takashi Iwai0ba21762007-04-16 11:29:14 +0200993int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
994 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995{
996 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
997 hda_nid_t nid = get_amp_nid(kcontrol);
998 int chs = get_amp_channels(kcontrol);
999 int dir = get_amp_direction(kcontrol);
1000 int idx = get_amp_index(kcontrol);
1001 long *valp = ucontrol->value.integer.value;
1002
1003 if (chs & 1)
Takashi Iwai47fd8302007-08-10 17:11:07 +02001004 *valp++ = snd_hda_codec_amp_read(codec, nid, 0, dir, idx)
1005 & HDA_AMP_VOLMASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 if (chs & 2)
Takashi Iwai47fd8302007-08-10 17:11:07 +02001007 *valp = snd_hda_codec_amp_read(codec, nid, 1, dir, idx)
1008 & HDA_AMP_VOLMASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 return 0;
1010}
1011
Takashi Iwai0ba21762007-04-16 11:29:14 +02001012int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1013 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014{
1015 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1016 hda_nid_t nid = get_amp_nid(kcontrol);
1017 int chs = get_amp_channels(kcontrol);
1018 int dir = get_amp_direction(kcontrol);
1019 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 long *valp = ucontrol->value.integer.value;
1021 int change = 0;
1022
Takashi Iwaicb53c622007-08-10 17:21:45 +02001023 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001024 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001025 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
1026 0x7f, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001027 valp++;
1028 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001029 if (chs & 2)
1030 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001031 0x7f, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001032 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 return change;
1034}
1035
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001036int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1037 unsigned int size, unsigned int __user *_tlv)
1038{
1039 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1040 hda_nid_t nid = get_amp_nid(kcontrol);
1041 int dir = get_amp_direction(kcontrol);
1042 u32 caps, val1, val2;
1043
1044 if (size < 4 * sizeof(unsigned int))
1045 return -ENOMEM;
1046 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001047 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1048 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001049 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
1050 val1 = ((int)val1) * ((int)val2);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001051 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
1052 return -EFAULT;
1053 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
1054 return -EFAULT;
1055 if (put_user(val1, _tlv + 2))
1056 return -EFAULT;
1057 if (put_user(val2, _tlv + 3))
1058 return -EFAULT;
1059 return 0;
1060}
1061
Takashi Iwai2134ea42008-01-10 16:53:55 +01001062/*
1063 * set (static) TLV for virtual master volume; recalculated as max 0dB
1064 */
1065void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
1066 unsigned int *tlv)
1067{
1068 u32 caps;
1069 int nums, step;
1070
1071 caps = query_amp_caps(codec, nid, dir);
1072 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1073 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1074 step = (step + 1) * 25;
1075 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
1076 tlv[1] = 2 * sizeof(unsigned int);
1077 tlv[2] = -nums * step;
1078 tlv[3] = step;
1079}
1080
1081/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01001082static struct snd_kcontrol *
1083_snd_hda_find_mixer_ctl(struct hda_codec *codec,
1084 const char *name, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01001085{
1086 struct snd_ctl_elem_id id;
1087 memset(&id, 0, sizeof(id));
1088 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwai09f99702008-02-04 12:31:13 +01001089 id.index = idx;
Takashi Iwai2134ea42008-01-10 16:53:55 +01001090 strcpy(id.name, name);
1091 return snd_ctl_find_id(codec->bus->card, &id);
1092}
1093
Takashi Iwai09f99702008-02-04 12:31:13 +01001094struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
1095 const char *name)
1096{
1097 return _snd_hda_find_mixer_ctl(codec, name, 0);
1098}
1099
Takashi Iwai2134ea42008-01-10 16:53:55 +01001100/* create a virtual master control and add slaves */
1101int snd_hda_add_vmaster(struct hda_codec *codec, char *name,
1102 unsigned int *tlv, const char **slaves)
1103{
1104 struct snd_kcontrol *kctl;
1105 const char **s;
1106 int err;
1107
Takashi Iwai2f085542008-02-22 18:43:50 +01001108 for (s = slaves; *s && !snd_hda_find_mixer_ctl(codec, *s); s++)
1109 ;
1110 if (!*s) {
1111 snd_printdd("No slave found for %s\n", name);
1112 return 0;
1113 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01001114 kctl = snd_ctl_make_virtual_master(name, tlv);
1115 if (!kctl)
1116 return -ENOMEM;
1117 err = snd_ctl_add(codec->bus->card, kctl);
1118 if (err < 0)
1119 return err;
1120
1121 for (s = slaves; *s; s++) {
1122 struct snd_kcontrol *sctl;
1123
1124 sctl = snd_hda_find_mixer_ctl(codec, *s);
1125 if (!sctl) {
1126 snd_printdd("Cannot find slave %s, skipped\n", *s);
1127 continue;
1128 }
1129 err = snd_ctl_add_slave(kctl, sctl);
1130 if (err < 0)
1131 return err;
1132 }
1133 return 0;
1134}
1135
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136/* switch */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001137int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
1138 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139{
1140 int chs = get_amp_channels(kcontrol);
1141
1142 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1143 uinfo->count = chs == 3 ? 2 : 1;
1144 uinfo->value.integer.min = 0;
1145 uinfo->value.integer.max = 1;
1146 return 0;
1147}
1148
Takashi Iwai0ba21762007-04-16 11:29:14 +02001149int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
1150 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151{
1152 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1153 hda_nid_t nid = get_amp_nid(kcontrol);
1154 int chs = get_amp_channels(kcontrol);
1155 int dir = get_amp_direction(kcontrol);
1156 int idx = get_amp_index(kcontrol);
1157 long *valp = ucontrol->value.integer.value;
1158
1159 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02001160 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02001161 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02001163 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02001164 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 return 0;
1166}
1167
Takashi Iwai0ba21762007-04-16 11:29:14 +02001168int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
1169 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170{
1171 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1172 hda_nid_t nid = get_amp_nid(kcontrol);
1173 int chs = get_amp_channels(kcontrol);
1174 int dir = get_amp_direction(kcontrol);
1175 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 long *valp = ucontrol->value.integer.value;
1177 int change = 0;
1178
Takashi Iwaicb53c622007-08-10 17:21:45 +02001179 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001180 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001181 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02001182 HDA_AMP_MUTE,
1183 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001184 valp++;
1185 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001186 if (chs & 2)
1187 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02001188 HDA_AMP_MUTE,
1189 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001190#ifdef CONFIG_SND_HDA_POWER_SAVE
1191 if (codec->patch_ops.check_power_status)
1192 codec->patch_ops.check_power_status(codec, nid);
1193#endif
1194 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 return change;
1196}
1197
1198/*
Takashi Iwai985be542005-11-02 18:26:49 +01001199 * bound volume controls
1200 *
1201 * bind multiple volumes (# indices, from 0)
1202 */
1203
1204#define AMP_VAL_IDX_SHIFT 19
1205#define AMP_VAL_IDX_MASK (0x0f<<19)
1206
Takashi Iwai0ba21762007-04-16 11:29:14 +02001207int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
1208 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01001209{
1210 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1211 unsigned long pval;
1212 int err;
1213
Ingo Molnar62932df2006-01-16 16:34:20 +01001214 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Takashi Iwai985be542005-11-02 18:26:49 +01001215 pval = kcontrol->private_value;
1216 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
1217 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
1218 kcontrol->private_value = pval;
Ingo Molnar62932df2006-01-16 16:34:20 +01001219 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01001220 return err;
1221}
1222
Takashi Iwai0ba21762007-04-16 11:29:14 +02001223int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
1224 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01001225{
1226 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1227 unsigned long pval;
1228 int i, indices, err = 0, change = 0;
1229
Ingo Molnar62932df2006-01-16 16:34:20 +01001230 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Takashi Iwai985be542005-11-02 18:26:49 +01001231 pval = kcontrol->private_value;
1232 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
1233 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001234 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
1235 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01001236 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1237 if (err < 0)
1238 break;
1239 change |= err;
1240 }
1241 kcontrol->private_value = pval;
Ingo Molnar62932df2006-01-16 16:34:20 +01001242 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01001243 return err < 0 ? err : change;
1244}
1245
1246/*
Takashi Iwai532d5382007-07-27 19:02:40 +02001247 * generic bound volume/swtich controls
1248 */
1249int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
1250 struct snd_ctl_elem_info *uinfo)
1251{
1252 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1253 struct hda_bind_ctls *c;
1254 int err;
1255
Takashi Iwai532d5382007-07-27 19:02:40 +02001256 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01001257 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02001258 kcontrol->private_value = *c->values;
1259 err = c->ops->info(kcontrol, uinfo);
1260 kcontrol->private_value = (long)c;
1261 mutex_unlock(&codec->spdif_mutex);
1262 return err;
1263}
1264
1265int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
1266 struct snd_ctl_elem_value *ucontrol)
1267{
1268 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1269 struct hda_bind_ctls *c;
1270 int err;
1271
Takashi Iwai532d5382007-07-27 19:02:40 +02001272 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01001273 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02001274 kcontrol->private_value = *c->values;
1275 err = c->ops->get(kcontrol, ucontrol);
1276 kcontrol->private_value = (long)c;
1277 mutex_unlock(&codec->spdif_mutex);
1278 return err;
1279}
1280
1281int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
1282 struct snd_ctl_elem_value *ucontrol)
1283{
1284 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1285 struct hda_bind_ctls *c;
1286 unsigned long *vals;
1287 int err = 0, change = 0;
1288
Takashi Iwai532d5382007-07-27 19:02:40 +02001289 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01001290 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02001291 for (vals = c->values; *vals; vals++) {
1292 kcontrol->private_value = *vals;
1293 err = c->ops->put(kcontrol, ucontrol);
1294 if (err < 0)
1295 break;
1296 change |= err;
1297 }
1298 kcontrol->private_value = (long)c;
1299 mutex_unlock(&codec->spdif_mutex);
1300 return err < 0 ? err : change;
1301}
1302
1303int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1304 unsigned int size, unsigned int __user *tlv)
1305{
1306 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1307 struct hda_bind_ctls *c;
1308 int err;
1309
Takashi Iwai532d5382007-07-27 19:02:40 +02001310 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01001311 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02001312 kcontrol->private_value = *c->values;
1313 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
1314 kcontrol->private_value = (long)c;
1315 mutex_unlock(&codec->spdif_mutex);
1316 return err;
1317}
1318
1319struct hda_ctl_ops snd_hda_bind_vol = {
1320 .info = snd_hda_mixer_amp_volume_info,
1321 .get = snd_hda_mixer_amp_volume_get,
1322 .put = snd_hda_mixer_amp_volume_put,
1323 .tlv = snd_hda_mixer_amp_tlv
1324};
1325
1326struct hda_ctl_ops snd_hda_bind_sw = {
1327 .info = snd_hda_mixer_amp_switch_info,
1328 .get = snd_hda_mixer_amp_switch_get,
1329 .put = snd_hda_mixer_amp_switch_put,
1330 .tlv = snd_hda_mixer_amp_tlv
1331};
1332
1333/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 * SPDIF out controls
1335 */
1336
Takashi Iwai0ba21762007-04-16 11:29:14 +02001337static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
1338 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339{
1340 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1341 uinfo->count = 1;
1342 return 0;
1343}
1344
Takashi Iwai0ba21762007-04-16 11:29:14 +02001345static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
1346 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347{
1348 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
1349 IEC958_AES0_NONAUDIO |
1350 IEC958_AES0_CON_EMPHASIS_5015 |
1351 IEC958_AES0_CON_NOT_COPYRIGHT;
1352 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
1353 IEC958_AES1_CON_ORIGINAL;
1354 return 0;
1355}
1356
Takashi Iwai0ba21762007-04-16 11:29:14 +02001357static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
1358 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359{
1360 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
1361 IEC958_AES0_NONAUDIO |
1362 IEC958_AES0_PRO_EMPHASIS_5015;
1363 return 0;
1364}
1365
Takashi Iwai0ba21762007-04-16 11:29:14 +02001366static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
1367 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368{
1369 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1370
1371 ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
1372 ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
1373 ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
1374 ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
1375
1376 return 0;
1377}
1378
1379/* convert from SPDIF status bits to HDA SPDIF bits
1380 * bit 0 (DigEn) is always set zero (to be filled later)
1381 */
1382static unsigned short convert_from_spdif_status(unsigned int sbits)
1383{
1384 unsigned short val = 0;
1385
1386 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02001387 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02001389 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001391 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
1392 IEC958_AES0_PRO_EMPHASIS_5015)
1393 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001395 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
1396 IEC958_AES0_CON_EMPHASIS_5015)
1397 val |= AC_DIG1_EMPHASIS;
1398 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
1399 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02001401 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
1403 }
1404 return val;
1405}
1406
1407/* convert to SPDIF status bits from HDA SPDIF bits
1408 */
1409static unsigned int convert_to_spdif_status(unsigned short val)
1410{
1411 unsigned int sbits = 0;
1412
Takashi Iwai0ba21762007-04-16 11:29:14 +02001413 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001415 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 sbits |= IEC958_AES0_PROFESSIONAL;
1417 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001418 if (sbits & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
1420 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001421 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001423 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001425 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
1427 sbits |= val & (0x7f << 8);
1428 }
1429 return sbits;
1430}
1431
Takashi Iwai2f728532008-09-25 16:32:41 +02001432/* set digital convert verbs both for the given NID and its slaves */
1433static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
1434 int verb, int val)
1435{
1436 hda_nid_t *d;
1437
1438 snd_hda_codec_write(codec, nid, 0, verb, val);
1439 d = codec->slave_dig_outs;
1440 if (!d)
1441 return;
1442 for (; *d; d++)
1443 snd_hda_codec_write(codec, *d, 0, verb, val);
1444}
1445
1446static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
1447 int dig1, int dig2)
1448{
1449 if (dig1 != -1)
1450 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
1451 if (dig2 != -1)
1452 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
1453}
1454
Takashi Iwai0ba21762007-04-16 11:29:14 +02001455static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
1456 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457{
1458 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1459 hda_nid_t nid = kcontrol->private_value;
1460 unsigned short val;
1461 int change;
1462
Ingo Molnar62932df2006-01-16 16:34:20 +01001463 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 codec->spdif_status = ucontrol->value.iec958.status[0] |
1465 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
1466 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
1467 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
1468 val = convert_from_spdif_status(codec->spdif_status);
1469 val |= codec->spdif_ctls & 1;
1470 change = codec->spdif_ctls != val;
1471 codec->spdif_ctls = val;
1472
Takashi Iwai2f728532008-09-25 16:32:41 +02001473 if (change)
1474 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
Ingo Molnar62932df2006-01-16 16:34:20 +01001476 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 return change;
1478}
1479
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001480#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
Takashi Iwai0ba21762007-04-16 11:29:14 +02001482static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
1483 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484{
1485 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1486
Takashi Iwai0ba21762007-04-16 11:29:14 +02001487 ucontrol->value.integer.value[0] = codec->spdif_ctls & AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 return 0;
1489}
1490
Takashi Iwai0ba21762007-04-16 11:29:14 +02001491static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
1492 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493{
1494 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1495 hda_nid_t nid = kcontrol->private_value;
1496 unsigned short val;
1497 int change;
1498
Ingo Molnar62932df2006-01-16 16:34:20 +01001499 mutex_lock(&codec->spdif_mutex);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001500 val = codec->spdif_ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02001502 val |= AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 change = codec->spdif_ctls != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001504 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 codec->spdif_ctls = val;
Takashi Iwai2f728532008-09-25 16:32:41 +02001506 set_dig_out_convert(codec, nid, val & 0xff, -1);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001507 /* unmute amp switch (if any) */
1508 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
Takashi Iwai47fd8302007-08-10 17:11:07 +02001509 (val & AC_DIG1_ENABLE))
1510 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
1511 HDA_AMP_MUTE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 }
Ingo Molnar62932df2006-01-16 16:34:20 +01001513 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 return change;
1515}
1516
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001517static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 {
1519 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1520 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1521 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
1522 .info = snd_hda_spdif_mask_info,
1523 .get = snd_hda_spdif_cmask_get,
1524 },
1525 {
1526 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1527 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1528 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
1529 .info = snd_hda_spdif_mask_info,
1530 .get = snd_hda_spdif_pmask_get,
1531 },
1532 {
1533 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1534 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1535 .info = snd_hda_spdif_mask_info,
1536 .get = snd_hda_spdif_default_get,
1537 .put = snd_hda_spdif_default_put,
1538 },
1539 {
1540 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1541 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
1542 .info = snd_hda_spdif_out_switch_info,
1543 .get = snd_hda_spdif_out_switch_get,
1544 .put = snd_hda_spdif_out_switch_put,
1545 },
1546 { } /* end */
1547};
1548
Takashi Iwai09f99702008-02-04 12:31:13 +01001549#define SPDIF_MAX_IDX 4 /* 4 instances should be enough to probe */
1550
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551/**
1552 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
1553 * @codec: the HDA codec
1554 * @nid: audio out widget NID
1555 *
1556 * Creates controls related with the SPDIF output.
1557 * Called from each patch supporting the SPDIF out.
1558 *
1559 * Returns 0 if successful, or a negative error code.
1560 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02001561int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562{
1563 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001564 struct snd_kcontrol *kctl;
1565 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01001566 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
Takashi Iwai09f99702008-02-04 12:31:13 +01001568 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
1569 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Playback Switch",
1570 idx))
1571 break;
1572 }
1573 if (idx >= SPDIF_MAX_IDX) {
1574 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
1575 return -EBUSY;
1576 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
1578 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwai09f99702008-02-04 12:31:13 +01001579 kctl->id.index = idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 kctl->private_value = nid;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001581 err = snd_ctl_add(codec->bus->card, kctl);
1582 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 return err;
1584 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02001585 codec->spdif_ctls =
Andrew Paprocki3982d172007-12-19 12:13:44 +01001586 snd_hda_codec_read(codec, nid, 0,
1587 AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
1589 return 0;
1590}
1591
1592/*
Takashi Iwai9a081602008-02-12 18:37:26 +01001593 * SPDIF sharing with analog output
1594 */
1595static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
1596 struct snd_ctl_elem_value *ucontrol)
1597{
1598 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
1599 ucontrol->value.integer.value[0] = mout->share_spdif;
1600 return 0;
1601}
1602
1603static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
1604 struct snd_ctl_elem_value *ucontrol)
1605{
1606 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
1607 mout->share_spdif = !!ucontrol->value.integer.value[0];
1608 return 0;
1609}
1610
1611static struct snd_kcontrol_new spdif_share_sw = {
1612 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1613 .name = "IEC958 Default PCM Playback Switch",
1614 .info = snd_ctl_boolean_mono_info,
1615 .get = spdif_share_sw_get,
1616 .put = spdif_share_sw_put,
1617};
1618
1619int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
1620 struct hda_multi_out *mout)
1621{
1622 if (!mout->dig_out_nid)
1623 return 0;
1624 /* ATTENTION: here mout is passed as private_data, instead of codec */
1625 return snd_ctl_add(codec->bus->card,
1626 snd_ctl_new1(&spdif_share_sw, mout));
1627}
1628
1629/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 * SPDIF input
1631 */
1632
1633#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
1634
Takashi Iwai0ba21762007-04-16 11:29:14 +02001635static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
1636 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637{
1638 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1639
1640 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
1641 return 0;
1642}
1643
Takashi Iwai0ba21762007-04-16 11:29:14 +02001644static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
1645 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646{
1647 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1648 hda_nid_t nid = kcontrol->private_value;
1649 unsigned int val = !!ucontrol->value.integer.value[0];
1650 int change;
1651
Ingo Molnar62932df2006-01-16 16:34:20 +01001652 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001654 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001656 snd_hda_codec_write_cache(codec, nid, 0,
1657 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 }
Ingo Molnar62932df2006-01-16 16:34:20 +01001659 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 return change;
1661}
1662
Takashi Iwai0ba21762007-04-16 11:29:14 +02001663static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
1664 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665{
1666 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1667 hda_nid_t nid = kcontrol->private_value;
1668 unsigned short val;
1669 unsigned int sbits;
1670
Andrew Paprocki3982d172007-12-19 12:13:44 +01001671 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 sbits = convert_to_spdif_status(val);
1673 ucontrol->value.iec958.status[0] = sbits;
1674 ucontrol->value.iec958.status[1] = sbits >> 8;
1675 ucontrol->value.iec958.status[2] = sbits >> 16;
1676 ucontrol->value.iec958.status[3] = sbits >> 24;
1677 return 0;
1678}
1679
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001680static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 {
1682 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1683 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH),
1684 .info = snd_hda_spdif_in_switch_info,
1685 .get = snd_hda_spdif_in_switch_get,
1686 .put = snd_hda_spdif_in_switch_put,
1687 },
1688 {
1689 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1690 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1691 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT),
1692 .info = snd_hda_spdif_mask_info,
1693 .get = snd_hda_spdif_in_status_get,
1694 },
1695 { } /* end */
1696};
1697
1698/**
1699 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
1700 * @codec: the HDA codec
1701 * @nid: audio in widget NID
1702 *
1703 * Creates controls related with the SPDIF input.
1704 * Called from each patch supporting the SPDIF in.
1705 *
1706 * Returns 0 if successful, or a negative error code.
1707 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02001708int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709{
1710 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001711 struct snd_kcontrol *kctl;
1712 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01001713 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
Takashi Iwai09f99702008-02-04 12:31:13 +01001715 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
1716 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Capture Switch",
1717 idx))
1718 break;
1719 }
1720 if (idx >= SPDIF_MAX_IDX) {
1721 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
1722 return -EBUSY;
1723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
1725 kctl = snd_ctl_new1(dig_mix, codec);
1726 kctl->private_value = nid;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001727 err = snd_ctl_add(codec->bus->card, kctl);
1728 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 return err;
1730 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02001731 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01001732 snd_hda_codec_read(codec, nid, 0,
1733 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02001734 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 return 0;
1736}
1737
Takashi Iwaicb53c622007-08-10 17:21:45 +02001738#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001739/*
1740 * command cache
1741 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001743/* build a 32bit cache key with the widget id and the command parameter */
1744#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
1745#define get_cmd_cache_nid(key) ((key) & 0xff)
1746#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
1747
1748/**
1749 * snd_hda_codec_write_cache - send a single command with caching
1750 * @codec: the HDA codec
1751 * @nid: NID to send the command
1752 * @direct: direct flag
1753 * @verb: the verb to send
1754 * @parm: the parameter for the verb
1755 *
1756 * Send a single command without waiting for response.
1757 *
1758 * Returns 0 if successful, or a negative error code.
1759 */
1760int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
1761 int direct, unsigned int verb, unsigned int parm)
1762{
1763 int err;
Takashi Iwaicb53c622007-08-10 17:21:45 +02001764 snd_hda_power_up(codec);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001765 mutex_lock(&codec->bus->cmd_mutex);
1766 err = codec->bus->ops.command(codec, nid, direct, verb, parm);
1767 if (!err) {
1768 struct hda_cache_head *c;
1769 u32 key = build_cmd_cache_key(nid, verb);
1770 c = get_alloc_hash(&codec->cmd_cache, key);
1771 if (c)
1772 c->val = parm;
1773 }
1774 mutex_unlock(&codec->bus->cmd_mutex);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001775 snd_hda_power_down(codec);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001776 return err;
1777}
1778
1779/* resume the all commands from the cache */
1780void snd_hda_codec_resume_cache(struct hda_codec *codec)
1781{
1782 struct hda_cache_head *buffer = codec->cmd_cache.buffer;
1783 int i;
1784
1785 for (i = 0; i < codec->cmd_cache.size; i++, buffer++) {
1786 u32 key = buffer->key;
1787 if (!key)
1788 continue;
1789 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
1790 get_cmd_cache_cmd(key), buffer->val);
1791 }
1792}
1793
1794/**
1795 * snd_hda_sequence_write_cache - sequence writes with caching
1796 * @codec: the HDA codec
1797 * @seq: VERB array to send
1798 *
1799 * Send the commands sequentially from the given array.
1800 * Thte commands are recorded on cache for power-save and resume.
1801 * The array must be terminated with NID=0.
1802 */
1803void snd_hda_sequence_write_cache(struct hda_codec *codec,
1804 const struct hda_verb *seq)
1805{
1806 for (; seq->nid; seq++)
1807 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
1808 seq->param);
1809}
Takashi Iwaicb53c622007-08-10 17:21:45 +02001810#endif /* SND_HDA_NEEDS_RESUME */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001811
Takashi Iwai54d17402005-11-21 16:33:22 +01001812/*
1813 * set power state of the codec
1814 */
1815static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1816 unsigned int power_state)
1817{
Takashi Iwaicb53c622007-08-10 17:21:45 +02001818 hda_nid_t nid;
1819 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01001820
1821 snd_hda_codec_write(codec, fg, 0, AC_VERB_SET_POWER_STATE,
1822 power_state);
Marc Boucherd2595d82008-01-22 15:23:30 +01001823 msleep(10); /* partial workaround for "azx_get_response timeout" */
Takashi Iwai54d17402005-11-21 16:33:22 +01001824
Takashi Iwaicb53c622007-08-10 17:21:45 +02001825 nid = codec->start_nid;
1826 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01001827 unsigned int wcaps = get_wcaps(codec, nid);
1828 if (wcaps & AC_WCAP_POWER) {
1829 unsigned int wid_type = (wcaps & AC_WCAP_TYPE) >>
1830 AC_WCAP_TYPE_SHIFT;
1831 if (wid_type == AC_WID_PIN) {
1832 unsigned int pincap;
1833 /*
1834 * don't power down the widget if it controls
1835 * eapd and EAPD_BTLENABLE is set.
1836 */
1837 pincap = snd_hda_param_read(codec, nid,
1838 AC_PAR_PIN_CAP);
1839 if (pincap & AC_PINCAP_EAPD) {
1840 int eapd = snd_hda_codec_read(codec,
1841 nid, 0,
1842 AC_VERB_GET_EAPD_BTLENABLE, 0);
1843 eapd &= 0x02;
1844 if (power_state == AC_PWRST_D3 && eapd)
1845 continue;
1846 }
Takashi Iwai1194b5b2007-10-10 10:04:26 +02001847 }
Takashi Iwai54d17402005-11-21 16:33:22 +01001848 snd_hda_codec_write(codec, nid, 0,
1849 AC_VERB_SET_POWER_STATE,
1850 power_state);
Takashi Iwai1194b5b2007-10-10 10:04:26 +02001851 }
Takashi Iwai54d17402005-11-21 16:33:22 +01001852 }
1853
Takashi Iwaicb53c622007-08-10 17:21:45 +02001854 if (power_state == AC_PWRST_D0) {
1855 unsigned long end_time;
1856 int state;
Takashi Iwai54d17402005-11-21 16:33:22 +01001857 msleep(10);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001858 /* wait until the codec reachs to D0 */
1859 end_time = jiffies + msecs_to_jiffies(500);
1860 do {
1861 state = snd_hda_codec_read(codec, fg, 0,
1862 AC_VERB_GET_POWER_STATE, 0);
1863 if (state == power_state)
1864 break;
1865 msleep(1);
1866 } while (time_after_eq(end_time, jiffies));
1867 }
Takashi Iwai54d17402005-11-21 16:33:22 +01001868}
1869
Takashi Iwaicb53c622007-08-10 17:21:45 +02001870#ifdef SND_HDA_NEEDS_RESUME
1871/*
1872 * call suspend and power-down; used both from PM and power-save
1873 */
1874static void hda_call_codec_suspend(struct hda_codec *codec)
1875{
1876 if (codec->patch_ops.suspend)
1877 codec->patch_ops.suspend(codec, PMSG_SUSPEND);
1878 hda_set_power_state(codec,
1879 codec->afg ? codec->afg : codec->mfg,
1880 AC_PWRST_D3);
1881#ifdef CONFIG_SND_HDA_POWER_SAVE
1882 cancel_delayed_work(&codec->power_work);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02001883 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02001884 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02001885#endif
1886}
1887
1888/*
1889 * kick up codec; used both from PM and power-save
1890 */
1891static void hda_call_codec_resume(struct hda_codec *codec)
1892{
1893 hda_set_power_state(codec,
1894 codec->afg ? codec->afg : codec->mfg,
1895 AC_PWRST_D0);
1896 if (codec->patch_ops.resume)
1897 codec->patch_ops.resume(codec);
1898 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02001899 if (codec->patch_ops.init)
1900 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001901 snd_hda_codec_resume_amp(codec);
1902 snd_hda_codec_resume_cache(codec);
1903 }
1904}
1905#endif /* SND_HDA_NEEDS_RESUME */
1906
Takashi Iwai54d17402005-11-21 16:33:22 +01001907
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908/**
1909 * snd_hda_build_controls - build mixer controls
1910 * @bus: the BUS
1911 *
1912 * Creates mixer controls for each codec included in the bus.
1913 *
1914 * Returns 0 if successful, otherwise a negative error code.
1915 */
Takashi Iwai756e2b02007-04-16 11:27:07 +02001916int __devinit snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001918 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
Takashi Iwai0ba21762007-04-16 11:29:14 +02001920 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaicb53c622007-08-10 17:21:45 +02001921 int err = 0;
1922 /* fake as if already powered-on */
1923 hda_keep_power_on(codec);
1924 /* then fire up */
1925 hda_set_power_state(codec,
1926 codec->afg ? codec->afg : codec->mfg,
1927 AC_PWRST_D0);
1928 /* continue to initialize... */
1929 if (codec->patch_ops.init)
1930 err = codec->patch_ops.init(codec);
1931 if (!err && codec->patch_ops.build_controls)
1932 err = codec->patch_ops.build_controls(codec);
1933 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 if (err < 0)
1935 return err;
1936 }
1937
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 return 0;
1939}
1940
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941/*
1942 * stream formats
1943 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02001944struct hda_rate_tbl {
1945 unsigned int hz;
1946 unsigned int alsa_bits;
1947 unsigned int hda_fmt;
1948};
1949
1950static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02001952
1953 /* autodetected value used in snd_hda_query_supported_pcm */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 { 8000, SNDRV_PCM_RATE_8000, 0x0500 }, /* 1/6 x 48 */
1955 { 11025, SNDRV_PCM_RATE_11025, 0x4300 }, /* 1/4 x 44 */
1956 { 16000, SNDRV_PCM_RATE_16000, 0x0200 }, /* 1/3 x 48 */
1957 { 22050, SNDRV_PCM_RATE_22050, 0x4100 }, /* 1/2 x 44 */
1958 { 32000, SNDRV_PCM_RATE_32000, 0x0a00 }, /* 2/3 x 48 */
1959 { 44100, SNDRV_PCM_RATE_44100, 0x4000 }, /* 44 */
1960 { 48000, SNDRV_PCM_RATE_48000, 0x0000 }, /* 48 */
1961 { 88200, SNDRV_PCM_RATE_88200, 0x4800 }, /* 2 x 44 */
1962 { 96000, SNDRV_PCM_RATE_96000, 0x0800 }, /* 2 x 48 */
1963 { 176400, SNDRV_PCM_RATE_176400, 0x5800 },/* 4 x 44 */
1964 { 192000, SNDRV_PCM_RATE_192000, 0x1800 }, /* 4 x 48 */
Takashi Iwaia961f9f2007-04-12 13:08:09 +02001965#define AC_PAR_PCM_RATE_BITS 11
1966 /* up to bits 10, 384kHZ isn't supported properly */
1967
1968 /* not autodetected value */
1969 { 9600, SNDRV_PCM_RATE_KNOT, 0x0400 }, /* 1/5 x 48 */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02001970
Takashi Iwaibefdf312005-08-22 13:57:55 +02001971 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972};
1973
1974/**
1975 * snd_hda_calc_stream_format - calculate format bitset
1976 * @rate: the sample rate
1977 * @channels: the number of channels
1978 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
1979 * @maxbps: the max. bps
1980 *
1981 * Calculate the format bitset from the given rate, channels and th PCM format.
1982 *
1983 * Return zero if invalid.
1984 */
1985unsigned int snd_hda_calc_stream_format(unsigned int rate,
1986 unsigned int channels,
1987 unsigned int format,
1988 unsigned int maxbps)
1989{
1990 int i;
1991 unsigned int val = 0;
1992
Takashi Iwaibefdf312005-08-22 13:57:55 +02001993 for (i = 0; rate_bits[i].hz; i++)
1994 if (rate_bits[i].hz == rate) {
1995 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 break;
1997 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02001998 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 snd_printdd("invalid rate %d\n", rate);
2000 return 0;
2001 }
2002
2003 if (channels == 0 || channels > 8) {
2004 snd_printdd("invalid channels %d\n", channels);
2005 return 0;
2006 }
2007 val |= channels - 1;
2008
2009 switch (snd_pcm_format_width(format)) {
2010 case 8: val |= 0x00; break;
2011 case 16: val |= 0x10; break;
2012 case 20:
2013 case 24:
2014 case 32:
2015 if (maxbps >= 32)
2016 val |= 0x40;
2017 else if (maxbps >= 24)
2018 val |= 0x30;
2019 else
2020 val |= 0x20;
2021 break;
2022 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002023 snd_printdd("invalid format width %d\n",
2024 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 return 0;
2026 }
2027
2028 return val;
2029}
2030
2031/**
2032 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
2033 * @codec: the HDA codec
2034 * @nid: NID to query
2035 * @ratesp: the pointer to store the detected rate bitflags
2036 * @formatsp: the pointer to store the detected formats
2037 * @bpsp: the pointer to store the detected format widths
2038 *
2039 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
2040 * or @bsps argument is ignored.
2041 *
2042 * Returns 0 if successful, otherwise a negative error code.
2043 */
2044int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
2045 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
2046{
2047 int i;
2048 unsigned int val, streams;
2049
2050 val = 0;
2051 if (nid != codec->afg &&
Takashi Iwai54d17402005-11-21 16:33:22 +01002052 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
2054 if (val == -1)
2055 return -EIO;
2056 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002057 if (!val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
2059
2060 if (ratesp) {
2061 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02002062 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02002064 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 }
2066 *ratesp = rates;
2067 }
2068
2069 if (formatsp || bpsp) {
2070 u64 formats = 0;
2071 unsigned int bps;
2072 unsigned int wcaps;
2073
Takashi Iwai54d17402005-11-21 16:33:22 +01002074 wcaps = get_wcaps(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
2076 if (streams == -1)
2077 return -EIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002078 if (!streams) {
2079 streams = snd_hda_param_read(codec, codec->afg,
2080 AC_PAR_STREAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 if (streams == -1)
2082 return -EIO;
2083 }
2084
2085 bps = 0;
2086 if (streams & AC_SUPFMT_PCM) {
2087 if (val & AC_SUPPCM_BITS_8) {
2088 formats |= SNDRV_PCM_FMTBIT_U8;
2089 bps = 8;
2090 }
2091 if (val & AC_SUPPCM_BITS_16) {
2092 formats |= SNDRV_PCM_FMTBIT_S16_LE;
2093 bps = 16;
2094 }
2095 if (wcaps & AC_WCAP_DIGITAL) {
2096 if (val & AC_SUPPCM_BITS_32)
2097 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
2098 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
2099 formats |= SNDRV_PCM_FMTBIT_S32_LE;
2100 if (val & AC_SUPPCM_BITS_24)
2101 bps = 24;
2102 else if (val & AC_SUPPCM_BITS_20)
2103 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002104 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
2105 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 formats |= SNDRV_PCM_FMTBIT_S32_LE;
2107 if (val & AC_SUPPCM_BITS_32)
2108 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 else if (val & AC_SUPPCM_BITS_24)
2110 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02002111 else if (val & AC_SUPPCM_BITS_20)
2112 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 }
2114 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002115 else if (streams == AC_SUPFMT_FLOAT32) {
2116 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
2118 bps = 32;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002119 } else if (streams == AC_SUPFMT_AC3) {
2120 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 /* temporary hack: we have still no proper support
2122 * for the direct AC3 stream...
2123 */
2124 formats |= SNDRV_PCM_FMTBIT_U8;
2125 bps = 8;
2126 }
2127 if (formatsp)
2128 *formatsp = formats;
2129 if (bpsp)
2130 *bpsp = bps;
2131 }
2132
2133 return 0;
2134}
2135
2136/**
Takashi Iwai0ba21762007-04-16 11:29:14 +02002137 * snd_hda_is_supported_format - check whether the given node supports
2138 * the format val
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 *
2140 * Returns 1 if supported, 0 if not.
2141 */
2142int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
2143 unsigned int format)
2144{
2145 int i;
2146 unsigned int val = 0, rate, stream;
2147
2148 if (nid != codec->afg &&
Takashi Iwai54d17402005-11-21 16:33:22 +01002149 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
2151 if (val == -1)
2152 return 0;
2153 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002154 if (!val) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
2156 if (val == -1)
2157 return 0;
2158 }
2159
2160 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02002161 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02002162 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 if (val & (1 << i))
2164 break;
2165 return 0;
2166 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02002167 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 return 0;
2169
2170 stream = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
2171 if (stream == -1)
2172 return 0;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002173 if (!stream && nid != codec->afg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 stream = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002175 if (!stream || stream == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 return 0;
2177
2178 if (stream & AC_SUPFMT_PCM) {
2179 switch (format & 0xf0) {
2180 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002181 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 return 0;
2183 break;
2184 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002185 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 return 0;
2187 break;
2188 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002189 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 return 0;
2191 break;
2192 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002193 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 return 0;
2195 break;
2196 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002197 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 return 0;
2199 break;
2200 default:
2201 return 0;
2202 }
2203 } else {
2204 /* FIXME: check for float32 and AC3? */
2205 }
2206
2207 return 1;
2208}
2209
2210/*
2211 * PCM stuff
2212 */
2213static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
2214 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002215 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216{
2217 return 0;
2218}
2219
2220static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
2221 struct hda_codec *codec,
2222 unsigned int stream_tag,
2223 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002224 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225{
2226 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
2227 return 0;
2228}
2229
2230static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
2231 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002232 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233{
Takashi Iwai888afa12008-03-18 09:57:50 +01002234 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 return 0;
2236}
2237
Takashi Iwai0ba21762007-04-16 11:29:14 +02002238static int __devinit set_pcm_default_values(struct hda_codec *codec,
2239 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240{
Takashi Iwai0ba21762007-04-16 11:29:14 +02002241 /* query support PCM information from the given NID */
2242 if (info->nid && (!info->rates || !info->formats)) {
2243 snd_hda_query_supported_pcm(codec, info->nid,
2244 info->rates ? NULL : &info->rates,
2245 info->formats ? NULL : &info->formats,
2246 info->maxbps ? NULL : &info->maxbps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 }
2248 if (info->ops.open == NULL)
2249 info->ops.open = hda_pcm_default_open_close;
2250 if (info->ops.close == NULL)
2251 info->ops.close = hda_pcm_default_open_close;
2252 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02002253 if (snd_BUG_ON(!info->nid))
2254 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 info->ops.prepare = hda_pcm_default_prepare;
2256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02002258 if (snd_BUG_ON(!info->nid))
2259 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 info->ops.cleanup = hda_pcm_default_cleanup;
2261 }
2262 return 0;
2263}
2264
Takashi Iwai176d5332008-07-30 15:01:44 +02002265/*
2266 * attach a new PCM stream
2267 */
2268static int __devinit
2269snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
2270{
2271 struct hda_pcm_stream *info;
2272 int stream, err;
2273
2274 if (!pcm->name)
2275 return -EINVAL;
2276 for (stream = 0; stream < 2; stream++) {
2277 info = &pcm->stream[stream];
2278 if (info->substreams) {
2279 err = set_pcm_default_values(codec, info);
2280 if (err < 0)
2281 return err;
2282 }
2283 }
2284 return codec->bus->ops.attach_pcm(codec, pcm);
2285}
2286
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287/**
2288 * snd_hda_build_pcms - build PCM information
2289 * @bus: the BUS
2290 *
2291 * Create PCM information for each codec included in the bus.
2292 *
2293 * The build_pcms codec patch is requested to set up codec->num_pcms and
2294 * codec->pcm_info properly. The array is referred by the top-level driver
2295 * to create its PCM instances.
2296 * The allocated codec->pcm_info should be released in codec->patch_ops.free
2297 * callback.
2298 *
2299 * At least, substreams, channels_min and channels_max must be filled for
2300 * each stream. substreams = 0 indicates that the stream doesn't exist.
2301 * When rates and/or formats are zero, the supported values are queried
2302 * from the given nid. The nid is used also by the default ops.prepare
2303 * and ops.cleanup callbacks.
2304 *
2305 * The driver needs to call ops.open in its open callback. Similarly,
2306 * ops.close is supposed to be called in the close callback.
2307 * ops.prepare should be called in the prepare or hw_params callback
2308 * with the proper parameters for set up.
2309 * ops.cleanup should be called in hw_free for clean up of streams.
2310 *
2311 * This function returns 0 if successfull, or a negative error code.
2312 */
Takashi Iwai756e2b02007-04-16 11:27:07 +02002313int __devinit snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314{
Takashi Iwai176d5332008-07-30 15:01:44 +02002315 static const char *dev_name[HDA_PCM_NTYPES] = {
2316 "Audio", "SPDIF", "HDMI", "Modem"
2317 };
2318 /* starting device index for each PCM type */
2319 static int dev_idx[HDA_PCM_NTYPES] = {
2320 [HDA_PCM_TYPE_AUDIO] = 0,
2321 [HDA_PCM_TYPE_SPDIF] = 1,
2322 [HDA_PCM_TYPE_HDMI] = 3,
2323 [HDA_PCM_TYPE_MODEM] = 6
2324 };
2325 /* normal audio device indices; not linear to keep compatibility */
2326 static int audio_idx[4] = { 0, 2, 4, 5 };
Takashi Iwai0ba21762007-04-16 11:29:14 +02002327 struct hda_codec *codec;
Takashi Iwai176d5332008-07-30 15:01:44 +02002328 int num_devs[HDA_PCM_NTYPES];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
Takashi Iwai176d5332008-07-30 15:01:44 +02002330 memset(num_devs, 0, sizeof(num_devs));
Takashi Iwai0ba21762007-04-16 11:29:14 +02002331 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai176d5332008-07-30 15:01:44 +02002332 unsigned int pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 int err;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002334 if (!codec->patch_ops.build_pcms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 continue;
2336 err = codec->patch_ops.build_pcms(codec);
2337 if (err < 0)
2338 return err;
2339 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
Takashi Iwai176d5332008-07-30 15:01:44 +02002340 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2341 int type = cpcm->pcm_type;
2342 switch (type) {
2343 case HDA_PCM_TYPE_AUDIO:
2344 if (num_devs[type] >= ARRAY_SIZE(audio_idx)) {
2345 snd_printk(KERN_WARNING
2346 "Too many audio devices\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 continue;
Takashi Iwai176d5332008-07-30 15:01:44 +02002348 }
2349 cpcm->device = audio_idx[num_devs[type]];
2350 break;
2351 case HDA_PCM_TYPE_SPDIF:
2352 case HDA_PCM_TYPE_HDMI:
2353 case HDA_PCM_TYPE_MODEM:
2354 if (num_devs[type]) {
2355 snd_printk(KERN_WARNING
2356 "%s already defined\n",
2357 dev_name[type]);
2358 continue;
2359 }
2360 cpcm->device = dev_idx[type];
2361 break;
2362 default:
2363 snd_printk(KERN_WARNING
2364 "Invalid PCM type %d\n", type);
2365 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 }
Takashi Iwai176d5332008-07-30 15:01:44 +02002367 num_devs[type]++;
2368 err = snd_hda_attach_pcm(codec, cpcm);
2369 if (err < 0)
2370 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 }
2372 }
2373 return 0;
2374}
2375
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376/**
2377 * snd_hda_check_board_config - compare the current codec with the config table
2378 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002379 * @num_configs: number of config enums
2380 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 * @tbl: configuration table, terminated by null entries
2382 *
2383 * Compares the modelname or PCI subsystem id of the current codec with the
2384 * given configuration table. If a matching entry is found, returns its
2385 * config value (supposed to be 0 or positive).
2386 *
2387 * If no entries are matching, the function returns a negative value.
2388 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002389int snd_hda_check_board_config(struct hda_codec *codec,
2390 int num_configs, const char **models,
2391 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392{
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002393 if (codec->bus->modelname && models) {
2394 int i;
2395 for (i = 0; i < num_configs; i++) {
2396 if (models[i] &&
2397 !strcmp(codec->bus->modelname, models[i])) {
2398 snd_printd(KERN_INFO "hda_codec: model '%s' is "
2399 "selected\n", models[i]);
2400 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 }
2402 }
2403 }
2404
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002405 if (!codec->bus->pci || !tbl)
2406 return -1;
2407
2408 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
2409 if (!tbl)
2410 return -1;
2411 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02002412#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002413 char tmp[10];
2414 const char *model = NULL;
2415 if (models)
2416 model = models[tbl->value];
2417 if (!model) {
2418 sprintf(tmp, "#%d", tbl->value);
2419 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002421 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
2422 "for config %x:%x (%s)\n",
2423 model, tbl->subvendor, tbl->subdevice,
2424 (tbl->name ? tbl->name : "Unknown device"));
2425#endif
2426 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 }
2428 return -1;
2429}
2430
2431/**
2432 * snd_hda_add_new_ctls - create controls from the array
2433 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002434 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 *
2436 * This helper function creates and add new controls in the given array.
2437 * The array must be terminated with an empty entry as terminator.
2438 *
2439 * Returns 0 if successful, or a negative error code.
2440 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002441int snd_hda_add_new_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442{
Takashi Iwaicb53c622007-08-10 17:21:45 +02002443 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444
2445 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01002446 struct snd_kcontrol *kctl;
2447 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002448 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01002449 return -ENOMEM;
2450 err = snd_ctl_add(codec->bus->card, kctl);
2451 if (err < 0) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002452 if (!codec->addr)
Takashi Iwai54d17402005-11-21 16:33:22 +01002453 return err;
2454 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002455 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01002456 return -ENOMEM;
2457 kctl->id.device = codec->addr;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002458 err = snd_ctl_add(codec->bus->card, kctl);
2459 if (err < 0)
Takashi Iwai54d17402005-11-21 16:33:22 +01002460 return err;
2461 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 }
2463 return 0;
2464}
2465
Takashi Iwaicb53c622007-08-10 17:21:45 +02002466#ifdef CONFIG_SND_HDA_POWER_SAVE
2467static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2468 unsigned int power_state);
2469
2470static void hda_power_work(struct work_struct *work)
2471{
2472 struct hda_codec *codec =
2473 container_of(work, struct hda_codec, power_work.work);
2474
Maxim Levitsky2e492462007-09-03 15:26:57 +02002475 if (!codec->power_on || codec->power_count) {
2476 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002477 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02002478 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02002479
2480 hda_call_codec_suspend(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002481 if (codec->bus->ops.pm_notify)
2482 codec->bus->ops.pm_notify(codec);
2483}
2484
2485static void hda_keep_power_on(struct hda_codec *codec)
2486{
2487 codec->power_count++;
2488 codec->power_on = 1;
2489}
2490
2491void snd_hda_power_up(struct hda_codec *codec)
2492{
2493 codec->power_count++;
Takashi Iwaia221e282007-08-16 16:35:33 +02002494 if (codec->power_on || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02002495 return;
2496
2497 codec->power_on = 1;
2498 if (codec->bus->ops.pm_notify)
2499 codec->bus->ops.pm_notify(codec);
2500 hda_call_codec_resume(codec);
2501 cancel_delayed_work(&codec->power_work);
Takashi Iwaia221e282007-08-16 16:35:33 +02002502 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002503}
2504
2505void snd_hda_power_down(struct hda_codec *codec)
2506{
2507 --codec->power_count;
Takashi Iwaia221e282007-08-16 16:35:33 +02002508 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02002509 return;
Takashi Iwaia221e282007-08-16 16:35:33 +02002510 if (power_save) {
2511 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002512 schedule_delayed_work(&codec->power_work,
2513 msecs_to_jiffies(power_save * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02002514 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02002515}
2516
2517int snd_hda_check_amp_list_power(struct hda_codec *codec,
2518 struct hda_loopback_check *check,
2519 hda_nid_t nid)
2520{
2521 struct hda_amp_list *p;
2522 int ch, v;
2523
2524 if (!check->amplist)
2525 return 0;
2526 for (p = check->amplist; p->nid; p++) {
2527 if (p->nid == nid)
2528 break;
2529 }
2530 if (!p->nid)
2531 return 0; /* nothing changed */
2532
2533 for (p = check->amplist; p->nid; p++) {
2534 for (ch = 0; ch < 2; ch++) {
2535 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
2536 p->idx);
2537 if (!(v & HDA_AMP_MUTE) && v > 0) {
2538 if (!check->power_on) {
2539 check->power_on = 1;
2540 snd_hda_power_up(codec);
2541 }
2542 return 1;
2543 }
2544 }
2545 }
2546 if (check->power_on) {
2547 check->power_on = 0;
2548 snd_hda_power_down(codec);
2549 }
2550 return 0;
2551}
2552#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002554/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002555 * Channel mode helper
2556 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002557int snd_hda_ch_mode_info(struct hda_codec *codec,
2558 struct snd_ctl_elem_info *uinfo,
2559 const struct hda_channel_mode *chmode,
2560 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002561{
2562 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2563 uinfo->count = 1;
2564 uinfo->value.enumerated.items = num_chmodes;
2565 if (uinfo->value.enumerated.item >= num_chmodes)
2566 uinfo->value.enumerated.item = num_chmodes - 1;
2567 sprintf(uinfo->value.enumerated.name, "%dch",
2568 chmode[uinfo->value.enumerated.item].channels);
2569 return 0;
2570}
2571
Takashi Iwai0ba21762007-04-16 11:29:14 +02002572int snd_hda_ch_mode_get(struct hda_codec *codec,
2573 struct snd_ctl_elem_value *ucontrol,
2574 const struct hda_channel_mode *chmode,
2575 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002576 int max_channels)
2577{
2578 int i;
2579
2580 for (i = 0; i < num_chmodes; i++) {
2581 if (max_channels == chmode[i].channels) {
2582 ucontrol->value.enumerated.item[0] = i;
2583 break;
2584 }
2585 }
2586 return 0;
2587}
2588
Takashi Iwai0ba21762007-04-16 11:29:14 +02002589int snd_hda_ch_mode_put(struct hda_codec *codec,
2590 struct snd_ctl_elem_value *ucontrol,
2591 const struct hda_channel_mode *chmode,
2592 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002593 int *max_channelsp)
2594{
2595 unsigned int mode;
2596
2597 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01002598 if (mode >= num_chmodes)
2599 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002600 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002601 return 0;
2602 /* change the current channel setting */
2603 *max_channelsp = chmode[mode].channels;
2604 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002605 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002606 return 1;
2607}
2608
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609/*
2610 * input MUX helper
2611 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002612int snd_hda_input_mux_info(const struct hda_input_mux *imux,
2613 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614{
2615 unsigned int index;
2616
2617 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2618 uinfo->count = 1;
2619 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02002620 if (!imux->num_items)
2621 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 index = uinfo->value.enumerated.item;
2623 if (index >= imux->num_items)
2624 index = imux->num_items - 1;
2625 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
2626 return 0;
2627}
2628
Takashi Iwai0ba21762007-04-16 11:29:14 +02002629int snd_hda_input_mux_put(struct hda_codec *codec,
2630 const struct hda_input_mux *imux,
2631 struct snd_ctl_elem_value *ucontrol,
2632 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 unsigned int *cur_val)
2634{
2635 unsigned int idx;
2636
Takashi Iwai5513b0c2007-10-09 11:58:41 +02002637 if (!imux->num_items)
2638 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 idx = ucontrol->value.enumerated.item[0];
2640 if (idx >= imux->num_items)
2641 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002642 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002644 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
2645 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 *cur_val = idx;
2647 return 1;
2648}
2649
2650
2651/*
2652 * Multi-channel / digital-out PCM helper functions
2653 */
2654
Takashi Iwai6b97eb42007-04-05 14:51:48 +02002655/* setup SPDIF output stream */
2656static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
2657 unsigned int stream_tag, unsigned int format)
2658{
2659 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Takashi Iwai2f728532008-09-25 16:32:41 +02002660 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
2661 set_dig_out_convert(codec, nid,
2662 codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff,
2663 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02002664 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02002665 if (codec->slave_dig_outs) {
2666 hda_nid_t *d;
2667 for (d = codec->slave_dig_outs; *d; d++)
2668 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
2669 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04002670 }
Takashi Iwai2f728532008-09-25 16:32:41 +02002671 /* turn on again (if needed) */
2672 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
2673 set_dig_out_convert(codec, nid,
2674 codec->spdif_ctls & 0xff, -1);
2675}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04002676
Takashi Iwai2f728532008-09-25 16:32:41 +02002677static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
2678{
2679 snd_hda_codec_cleanup_stream(codec, nid);
2680 if (codec->slave_dig_outs) {
2681 hda_nid_t *d;
2682 for (d = codec->slave_dig_outs; *d; d++)
2683 snd_hda_codec_cleanup_stream(codec, *d);
2684 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02002685}
2686
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687/*
2688 * open the digital out in the exclusive mode
2689 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002690int snd_hda_multi_out_dig_open(struct hda_codec *codec,
2691 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692{
Ingo Molnar62932df2006-01-16 16:34:20 +01002693 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02002694 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
2695 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02002696 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01002698 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 return 0;
2700}
2701
Takashi Iwai6b97eb42007-04-05 14:51:48 +02002702int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
2703 struct hda_multi_out *mout,
2704 unsigned int stream_tag,
2705 unsigned int format,
2706 struct snd_pcm_substream *substream)
2707{
2708 mutex_lock(&codec->spdif_mutex);
2709 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
2710 mutex_unlock(&codec->spdif_mutex);
2711 return 0;
2712}
2713
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714/*
2715 * release the digital out
2716 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002717int snd_hda_multi_out_dig_close(struct hda_codec *codec,
2718 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719{
Ingo Molnar62932df2006-01-16 16:34:20 +01002720 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01002722 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 return 0;
2724}
2725
2726/*
2727 * set up more restrictions for analog out
2728 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002729int snd_hda_multi_out_analog_open(struct hda_codec *codec,
2730 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01002731 struct snd_pcm_substream *substream,
2732 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733{
Takashi Iwai9a081602008-02-12 18:37:26 +01002734 struct snd_pcm_runtime *runtime = substream->runtime;
2735 runtime->hw.channels_max = mout->max_channels;
2736 if (mout->dig_out_nid) {
2737 if (!mout->analog_rates) {
2738 mout->analog_rates = hinfo->rates;
2739 mout->analog_formats = hinfo->formats;
2740 mout->analog_maxbps = hinfo->maxbps;
2741 } else {
2742 runtime->hw.rates = mout->analog_rates;
2743 runtime->hw.formats = mout->analog_formats;
2744 hinfo->maxbps = mout->analog_maxbps;
2745 }
2746 if (!mout->spdif_rates) {
2747 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
2748 &mout->spdif_rates,
2749 &mout->spdif_formats,
2750 &mout->spdif_maxbps);
2751 }
2752 mutex_lock(&codec->spdif_mutex);
2753 if (mout->share_spdif) {
2754 runtime->hw.rates &= mout->spdif_rates;
2755 runtime->hw.formats &= mout->spdif_formats;
2756 if (mout->spdif_maxbps < hinfo->maxbps)
2757 hinfo->maxbps = mout->spdif_maxbps;
2758 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02002759 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01002760 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 return snd_pcm_hw_constraint_step(substream->runtime, 0,
2762 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
2763}
2764
2765/*
2766 * set up the i/o for analog out
2767 * when the digital out is available, copy the front out to digital out, too.
2768 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002769int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
2770 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 unsigned int stream_tag,
2772 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002773 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774{
2775 hda_nid_t *nids = mout->dac_nids;
2776 int chs = substream->runtime->channels;
2777 int i;
2778
Ingo Molnar62932df2006-01-16 16:34:20 +01002779 mutex_lock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01002780 if (mout->dig_out_nid && mout->share_spdif &&
2781 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02002783 snd_hda_is_supported_format(codec, mout->dig_out_nid,
2784 format) &&
2785 !(codec->spdif_status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02002787 setup_dig_out_stream(codec, mout->dig_out_nid,
2788 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 } else {
2790 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02002791 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 }
2793 }
Ingo Molnar62932df2006-01-16 16:34:20 +01002794 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795
2796 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002797 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
2798 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02002799 if (!mout->no_share_stream &&
2800 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002802 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
2803 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01002804 /* extra outputs copied from front */
2805 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02002806 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01002807 snd_hda_codec_setup_stream(codec,
2808 mout->extra_out_nid[i],
2809 stream_tag, 0, format);
2810
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 /* surrounds */
2812 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02002813 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002814 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
2815 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02002816 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002817 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
2818 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 }
2820 return 0;
2821}
2822
2823/*
2824 * clean up the setting for analog out
2825 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002826int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
2827 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828{
2829 hda_nid_t *nids = mout->dac_nids;
2830 int i;
2831
2832 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01002833 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01002835 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwai82bc9552006-03-21 11:24:42 +01002836 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
2837 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01002838 snd_hda_codec_cleanup_stream(codec,
2839 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01002840 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02002842 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 mout->dig_out_used = 0;
2844 }
Ingo Molnar62932df2006-01-16 16:34:20 +01002845 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 return 0;
2847}
2848
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002849/*
Wu Fengguang6b345002008-10-07 14:21:41 +08002850 * Helper for automatic pin configuration
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002851 */
Kailang Yangdf694da2005-12-05 19:42:22 +01002852
Takashi Iwai12f288b2007-08-02 15:51:59 +02002853static int is_in_nid_list(hda_nid_t nid, hda_nid_t *list)
Kailang Yangdf694da2005-12-05 19:42:22 +01002854{
2855 for (; *list; list++)
2856 if (*list == nid)
2857 return 1;
2858 return 0;
2859}
2860
Steve Longerbeam81937d32007-05-08 15:33:03 +02002861
2862/*
2863 * Sort an associated group of pins according to their sequence numbers.
2864 */
2865static void sort_pins_by_sequence(hda_nid_t * pins, short * sequences,
2866 int num_pins)
2867{
2868 int i, j;
2869 short seq;
2870 hda_nid_t nid;
2871
2872 for (i = 0; i < num_pins; i++) {
2873 for (j = i + 1; j < num_pins; j++) {
2874 if (sequences[i] > sequences[j]) {
2875 seq = sequences[i];
2876 sequences[i] = sequences[j];
2877 sequences[j] = seq;
2878 nid = pins[i];
2879 pins[i] = pins[j];
2880 pins[j] = nid;
2881 }
2882 }
2883 }
2884}
2885
2886
Takashi Iwai82bc9552006-03-21 11:24:42 +01002887/*
2888 * Parse all pin widgets and store the useful pin nids to cfg
2889 *
2890 * The number of line-outs or any primary output is stored in line_outs,
2891 * and the corresponding output pins are assigned to line_out_pins[],
2892 * in the order of front, rear, CLFE, side, ...
2893 *
2894 * If more extra outputs (speaker and headphone) are found, the pins are
Takashi Iwaieb06ed82006-09-20 17:10:27 +02002895 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
Takashi Iwai82bc9552006-03-21 11:24:42 +01002896 * is detected, one of speaker of HP pins is assigned as the primary
2897 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
2898 * if any analog output exists.
2899 *
2900 * The analog input pins are assigned to input_pins array.
2901 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
2902 * respectively.
2903 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002904int snd_hda_parse_pin_def_config(struct hda_codec *codec,
2905 struct auto_pin_cfg *cfg,
2906 hda_nid_t *ignore_nids)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002907{
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01002908 hda_nid_t nid, end_nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02002909 short seq, assoc_line_out, assoc_speaker;
2910 short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
2911 short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
Takashi Iwaif889fa92007-10-31 15:49:32 +01002912 short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002913
2914 memset(cfg, 0, sizeof(*cfg));
2915
Steve Longerbeam81937d32007-05-08 15:33:03 +02002916 memset(sequences_line_out, 0, sizeof(sequences_line_out));
2917 memset(sequences_speaker, 0, sizeof(sequences_speaker));
Takashi Iwaif889fa92007-10-31 15:49:32 +01002918 memset(sequences_hp, 0, sizeof(sequences_hp));
Steve Longerbeam81937d32007-05-08 15:33:03 +02002919 assoc_line_out = assoc_speaker = 0;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002920
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01002921 end_nid = codec->start_nid + codec->num_nodes;
2922 for (nid = codec->start_nid; nid < end_nid; nid++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01002923 unsigned int wid_caps = get_wcaps(codec, nid);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002924 unsigned int wid_type =
2925 (wid_caps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002926 unsigned int def_conf;
2927 short assoc, loc;
2928
2929 /* read all default configuration for pin complex */
2930 if (wid_type != AC_WID_PIN)
2931 continue;
Kailang Yangdf694da2005-12-05 19:42:22 +01002932 /* ignore the given nids (e.g. pc-beep returns error) */
2933 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
2934 continue;
2935
Takashi Iwai0ba21762007-04-16 11:29:14 +02002936 def_conf = snd_hda_codec_read(codec, nid, 0,
2937 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002938 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
2939 continue;
2940 loc = get_defcfg_location(def_conf);
2941 switch (get_defcfg_device(def_conf)) {
2942 case AC_JACK_LINE_OUT:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002943 seq = get_defcfg_sequence(def_conf);
2944 assoc = get_defcfg_association(def_conf);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01002945
2946 if (!(wid_caps & AC_WCAP_STEREO))
2947 if (!cfg->mono_out_pin)
2948 cfg->mono_out_pin = nid;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002949 if (!assoc)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002950 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002951 if (!assoc_line_out)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002952 assoc_line_out = assoc;
2953 else if (assoc_line_out != assoc)
2954 continue;
2955 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
2956 continue;
2957 cfg->line_out_pins[cfg->line_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02002958 sequences_line_out[cfg->line_outs] = seq;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002959 cfg->line_outs++;
2960 break;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01002961 case AC_JACK_SPEAKER:
Steve Longerbeam81937d32007-05-08 15:33:03 +02002962 seq = get_defcfg_sequence(def_conf);
2963 assoc = get_defcfg_association(def_conf);
2964 if (! assoc)
2965 continue;
2966 if (! assoc_speaker)
2967 assoc_speaker = assoc;
2968 else if (assoc_speaker != assoc)
2969 continue;
Takashi Iwai82bc9552006-03-21 11:24:42 +01002970 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
2971 continue;
2972 cfg->speaker_pins[cfg->speaker_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02002973 sequences_speaker[cfg->speaker_outs] = seq;
Takashi Iwai82bc9552006-03-21 11:24:42 +01002974 cfg->speaker_outs++;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01002975 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002976 case AC_JACK_HP_OUT:
Takashi Iwaif889fa92007-10-31 15:49:32 +01002977 seq = get_defcfg_sequence(def_conf);
2978 assoc = get_defcfg_association(def_conf);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02002979 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
2980 continue;
2981 cfg->hp_pins[cfg->hp_outs] = nid;
Takashi Iwaif889fa92007-10-31 15:49:32 +01002982 sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
Takashi Iwaieb06ed82006-09-20 17:10:27 +02002983 cfg->hp_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002984 break;
Takashi Iwai314634b2006-09-21 11:56:18 +02002985 case AC_JACK_MIC_IN: {
2986 int preferred, alt;
2987 if (loc == AC_JACK_LOC_FRONT) {
2988 preferred = AUTO_PIN_FRONT_MIC;
2989 alt = AUTO_PIN_MIC;
2990 } else {
2991 preferred = AUTO_PIN_MIC;
2992 alt = AUTO_PIN_FRONT_MIC;
2993 }
2994 if (!cfg->input_pins[preferred])
2995 cfg->input_pins[preferred] = nid;
2996 else if (!cfg->input_pins[alt])
2997 cfg->input_pins[alt] = nid;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002998 break;
Takashi Iwai314634b2006-09-21 11:56:18 +02002999 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003000 case AC_JACK_LINE_IN:
3001 if (loc == AC_JACK_LOC_FRONT)
3002 cfg->input_pins[AUTO_PIN_FRONT_LINE] = nid;
3003 else
3004 cfg->input_pins[AUTO_PIN_LINE] = nid;
3005 break;
3006 case AC_JACK_CD:
3007 cfg->input_pins[AUTO_PIN_CD] = nid;
3008 break;
3009 case AC_JACK_AUX:
3010 cfg->input_pins[AUTO_PIN_AUX] = nid;
3011 break;
3012 case AC_JACK_SPDIF_OUT:
3013 cfg->dig_out_pin = nid;
3014 break;
3015 case AC_JACK_SPDIF_IN:
3016 cfg->dig_in_pin = nid;
3017 break;
3018 }
3019 }
3020
Takashi Iwai5832fcf2008-02-12 18:30:12 +01003021 /* FIX-UP:
3022 * If no line-out is defined but multiple HPs are found,
3023 * some of them might be the real line-outs.
3024 */
3025 if (!cfg->line_outs && cfg->hp_outs > 1) {
3026 int i = 0;
3027 while (i < cfg->hp_outs) {
3028 /* The real HPs should have the sequence 0x0f */
3029 if ((sequences_hp[i] & 0x0f) == 0x0f) {
3030 i++;
3031 continue;
3032 }
3033 /* Move it to the line-out table */
3034 cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
3035 sequences_line_out[cfg->line_outs] = sequences_hp[i];
3036 cfg->line_outs++;
3037 cfg->hp_outs--;
3038 memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
3039 sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
3040 memmove(sequences_hp + i - 1, sequences_hp + i,
3041 sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
3042 }
3043 }
3044
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003045 /* sort by sequence */
Steve Longerbeam81937d32007-05-08 15:33:03 +02003046 sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
3047 cfg->line_outs);
3048 sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
3049 cfg->speaker_outs);
Takashi Iwaif889fa92007-10-31 15:49:32 +01003050 sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
3051 cfg->hp_outs);
Steve Longerbeam81937d32007-05-08 15:33:03 +02003052
Takashi Iwaif889fa92007-10-31 15:49:32 +01003053 /* if we have only one mic, make it AUTO_PIN_MIC */
3054 if (!cfg->input_pins[AUTO_PIN_MIC] &&
3055 cfg->input_pins[AUTO_PIN_FRONT_MIC]) {
3056 cfg->input_pins[AUTO_PIN_MIC] =
3057 cfg->input_pins[AUTO_PIN_FRONT_MIC];
3058 cfg->input_pins[AUTO_PIN_FRONT_MIC] = 0;
3059 }
3060 /* ditto for line-in */
3061 if (!cfg->input_pins[AUTO_PIN_LINE] &&
3062 cfg->input_pins[AUTO_PIN_FRONT_LINE]) {
3063 cfg->input_pins[AUTO_PIN_LINE] =
3064 cfg->input_pins[AUTO_PIN_FRONT_LINE];
3065 cfg->input_pins[AUTO_PIN_FRONT_LINE] = 0;
3066 }
3067
Steve Longerbeam81937d32007-05-08 15:33:03 +02003068 /*
3069 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
3070 * as a primary output
3071 */
3072 if (!cfg->line_outs) {
3073 if (cfg->speaker_outs) {
3074 cfg->line_outs = cfg->speaker_outs;
3075 memcpy(cfg->line_out_pins, cfg->speaker_pins,
3076 sizeof(cfg->speaker_pins));
3077 cfg->speaker_outs = 0;
3078 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
3079 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
3080 } else if (cfg->hp_outs) {
3081 cfg->line_outs = cfg->hp_outs;
3082 memcpy(cfg->line_out_pins, cfg->hp_pins,
3083 sizeof(cfg->hp_pins));
3084 cfg->hp_outs = 0;
3085 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
3086 cfg->line_out_type = AUTO_PIN_HP_OUT;
3087 }
3088 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003089
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02003090 /* Reorder the surround channels
3091 * ALSA sequence is front/surr/clfe/side
3092 * HDA sequence is:
3093 * 4-ch: front/surr => OK as it is
3094 * 6-ch: front/clfe/surr
Takashi Iwai9422db42007-04-20 16:11:43 +02003095 * 8-ch: front/clfe/rear/side|fc
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02003096 */
3097 switch (cfg->line_outs) {
3098 case 3:
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02003099 case 4:
3100 nid = cfg->line_out_pins[1];
Takashi Iwai9422db42007-04-20 16:11:43 +02003101 cfg->line_out_pins[1] = cfg->line_out_pins[2];
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02003102 cfg->line_out_pins[2] = nid;
3103 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003104 }
3105
Takashi Iwai82bc9552006-03-21 11:24:42 +01003106 /*
3107 * debug prints of the parsed results
3108 */
3109 snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3110 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
3111 cfg->line_out_pins[2], cfg->line_out_pins[3],
3112 cfg->line_out_pins[4]);
3113 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3114 cfg->speaker_outs, cfg->speaker_pins[0],
3115 cfg->speaker_pins[1], cfg->speaker_pins[2],
3116 cfg->speaker_pins[3], cfg->speaker_pins[4]);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02003117 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3118 cfg->hp_outs, cfg->hp_pins[0],
3119 cfg->hp_pins[1], cfg->hp_pins[2],
3120 cfg->hp_pins[3], cfg->hp_pins[4]);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01003121 snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
Takashi Iwai82bc9552006-03-21 11:24:42 +01003122 snd_printd(" inputs: mic=0x%x, fmic=0x%x, line=0x%x, fline=0x%x,"
3123 " cd=0x%x, aux=0x%x\n",
3124 cfg->input_pins[AUTO_PIN_MIC],
3125 cfg->input_pins[AUTO_PIN_FRONT_MIC],
3126 cfg->input_pins[AUTO_PIN_LINE],
3127 cfg->input_pins[AUTO_PIN_FRONT_LINE],
3128 cfg->input_pins[AUTO_PIN_CD],
3129 cfg->input_pins[AUTO_PIN_AUX]);
3130
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003131 return 0;
3132}
3133
Takashi Iwai4a471b72005-12-07 13:56:29 +01003134/* labels for input pins */
3135const char *auto_pin_cfg_labels[AUTO_PIN_LAST] = {
3136 "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux"
3137};
3138
3139
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140#ifdef CONFIG_PM
3141/*
3142 * power management
3143 */
3144
3145/**
3146 * snd_hda_suspend - suspend the codecs
3147 * @bus: the HDA bus
3148 * @state: suspsend state
3149 *
3150 * Returns 0 if successful.
3151 */
3152int snd_hda_suspend(struct hda_bus *bus, pm_message_t state)
3153{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003154 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155
Takashi Iwai0ba21762007-04-16 11:29:14 +02003156 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai0b7a2e92007-08-14 15:18:26 +02003157#ifdef CONFIG_SND_HDA_POWER_SAVE
3158 if (!codec->power_on)
3159 continue;
3160#endif
Takashi Iwaicb53c622007-08-10 17:21:45 +02003161 hda_call_codec_suspend(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 }
3163 return 0;
3164}
3165
3166/**
3167 * snd_hda_resume - resume the codecs
3168 * @bus: the HDA bus
3169 * @state: resume state
3170 *
3171 * Returns 0 if successful.
Takashi Iwaicb53c622007-08-10 17:21:45 +02003172 *
3173 * This fucntion is defined only when POWER_SAVE isn't set.
3174 * In the power-save mode, the codec is resumed dynamically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 */
3176int snd_hda_resume(struct hda_bus *bus)
3177{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003178 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179
Takashi Iwai0ba21762007-04-16 11:29:14 +02003180 list_for_each_entry(codec, &bus->codec_list, list) {
Maxim Levitskyd804ad92007-09-03 15:28:04 +02003181 if (snd_hda_codec_needs_resume(codec))
3182 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 return 0;
3185}
Maxim Levitskyd804ad92007-09-03 15:28:04 +02003186#ifdef CONFIG_SND_HDA_POWER_SAVE
3187int snd_hda_codecs_inuse(struct hda_bus *bus)
3188{
3189 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190
Maxim Levitskyd804ad92007-09-03 15:28:04 +02003191 list_for_each_entry(codec, &bus->codec_list, list) {
3192 if (snd_hda_codec_needs_resume(codec))
3193 return 1;
3194 }
3195 return 0;
3196}
3197#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198#endif
Takashi Iwaib2e18592008-07-30 15:01:44 +02003199
3200/*
3201 * generic arrays
3202 */
3203
3204/* get a new element from the given array
3205 * if it exceeds the pre-allocated array size, re-allocate the array
3206 */
3207void *snd_array_new(struct snd_array *array)
3208{
3209 if (array->used >= array->alloced) {
3210 int num = array->alloced + array->alloc_align;
3211 void *nlist = kcalloc(num + 1, array->elem_size, GFP_KERNEL);
3212 if (!nlist)
3213 return NULL;
3214 if (array->list) {
3215 memcpy(nlist, array->list,
3216 array->elem_size * array->alloced);
3217 kfree(array->list);
3218 }
3219 array->list = nlist;
3220 array->alloced = num;
3221 }
3222 return array->list + (array->used++ * array->elem_size);
3223}
3224
3225/* free the given array elements */
3226void snd_array_free(struct snd_array *array)
3227{
3228 kfree(array->list);
3229 array->used = 0;
3230 array->alloced = 0;
3231 array->list = NULL;
3232}