blob: 39a49d4a864a8ea7d1f7c192da742780c73d58e5 [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));
Takashi Iwai603c4012008-07-30 15:01:44 +0200759 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +0200760}
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{
Takashi Iwai603c4012008-07-30 15:01:44 +0200764 snd_array_free(&cache->buf);
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];
Takashi Iwai603c4012008-07-30 15:01:44 +0200773 struct hda_cache_head *info_head = cache->buf.list;
Takashi Iwai01751f52007-08-10 16:59:39 +0200774 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
776 while (cur != 0xffff) {
Takashi Iwai603c4012008-07-30 15:01:44 +0200777 info = &info_head[cur];
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 Iwai603c4012008-07-30 15:01:44 +0200784 info = snd_array_new(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 info->key = key;
Takashi Iwai01751f52007-08-10 16:59:39 +0200786 info->val = 0;
787 info->next = cache->hash[idx];
788 cache->hash[idx] = cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
790 return info;
791}
792
Takashi Iwai01751f52007-08-10 16:59:39 +0200793/* query and allocate an amp hash entry */
794static inline struct hda_amp_info *
795get_alloc_amp_hash(struct hda_codec *codec, u32 key)
796{
797 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
798}
799
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800/*
801 * query AMP capabilities for the given widget and direction
802 */
Matthew Ranostay09a99952008-01-24 11:49:21 +0100803u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200805 struct hda_amp_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
Takashi Iwai0ba21762007-04-16 11:29:14 +0200807 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
808 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 return 0;
Takashi Iwai01751f52007-08-10 16:59:39 +0200810 if (!(info->head.val & INFO_AMP_CAPS)) {
Takashi Iwai0ba21762007-04-16 11:29:14 +0200811 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 nid = codec->afg;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200813 info->amp_caps = snd_hda_param_read(codec, nid,
814 direction == HDA_OUTPUT ?
815 AC_PAR_AMP_OUT_CAP :
816 AC_PAR_AMP_IN_CAP);
Takashi Iwaib75e53f2007-05-10 16:56:09 +0200817 if (info->amp_caps)
Takashi Iwai01751f52007-08-10 16:59:39 +0200818 info->head.val |= INFO_AMP_CAPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 }
820 return info->amp_caps;
821}
822
Takashi Iwai897cc182007-05-29 19:01:37 +0200823int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
824 unsigned int caps)
825{
826 struct hda_amp_info *info;
827
828 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
829 if (!info)
830 return -EINVAL;
831 info->amp_caps = caps;
Takashi Iwai01751f52007-08-10 16:59:39 +0200832 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai897cc182007-05-29 19:01:37 +0200833 return 0;
834}
835
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836/*
837 * read the current volume to info
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200838 * if the cache exists, read the cache value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200840static unsigned int get_vol_mute(struct hda_codec *codec,
841 struct hda_amp_info *info, hda_nid_t nid,
842 int ch, int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843{
844 u32 val, parm;
845
Takashi Iwai01751f52007-08-10 16:59:39 +0200846 if (info->head.val & INFO_AMP_VOL(ch))
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200847 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
850 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
851 parm |= index;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200852 val = snd_hda_codec_read(codec, nid, 0,
853 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 info->vol[ch] = val & 0xff;
Takashi Iwai01751f52007-08-10 16:59:39 +0200855 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200856 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857}
858
859/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200860 * write the current volume in info to the h/w and update the cache
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200862static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
Takashi Iwai0ba21762007-04-16 11:29:14 +0200863 hda_nid_t nid, int ch, int direction, int index,
864 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865{
866 u32 parm;
867
868 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
869 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
870 parm |= index << AC_AMP_SET_INDEX_SHIFT;
871 parm |= val;
872 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200873 info->vol[ch] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874}
875
876/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200877 * read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 */
Takashi Iwai834be882006-03-01 14:16:17 +0100879int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
880 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200882 struct hda_amp_info *info;
883 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
884 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200886 return get_vol_mute(codec, info, nid, ch, direction, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887}
888
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200889/*
890 * update the AMP value, mask = bit mask to set, val = the value
891 */
Takashi Iwai834be882006-03-01 14:16:17 +0100892int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
893 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200895 struct hda_amp_info *info;
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200896
Takashi Iwai0ba21762007-04-16 11:29:14 +0200897 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
898 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200900 val &= mask;
901 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
Takashi Iwai82beb8f2007-08-10 17:09:26 +0200902 if (info->vol[ch] == val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200904 put_vol_mute(codec, info, nid, ch, direction, idx, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 return 1;
906}
907
Takashi Iwai47fd8302007-08-10 17:11:07 +0200908/*
909 * update the AMP stereo with the same mask and value
910 */
911int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
912 int direction, int idx, int mask, int val)
913{
914 int ch, ret = 0;
915 for (ch = 0; ch < 2; ch++)
916 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
917 idx, mask, val);
918 return ret;
919}
920
Takashi Iwaicb53c622007-08-10 17:21:45 +0200921#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwaib3ac5632007-08-10 17:03:40 +0200922/* resume the all amp commands from the cache */
923void snd_hda_codec_resume_amp(struct hda_codec *codec)
924{
Takashi Iwai603c4012008-07-30 15:01:44 +0200925 struct hda_amp_info *buffer = codec->amp_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +0200926 int i;
927
Takashi Iwai603c4012008-07-30 15:01:44 +0200928 for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +0200929 u32 key = buffer->head.key;
930 hda_nid_t nid;
931 unsigned int idx, dir, ch;
932 if (!key)
933 continue;
934 nid = key & 0xff;
935 idx = (key >> 16) & 0xff;
936 dir = (key >> 24) & 0xff;
937 for (ch = 0; ch < 2; ch++) {
938 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
939 continue;
940 put_vol_mute(codec, buffer, nid, ch, dir, idx,
941 buffer->vol[ch]);
942 }
943 }
944}
Takashi Iwaicb53c622007-08-10 17:21:45 +0200945#endif /* SND_HDA_NEEDS_RESUME */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947/* volume */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200948int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
949 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950{
951 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
952 u16 nid = get_amp_nid(kcontrol);
953 u8 chs = get_amp_channels(kcontrol);
954 int dir = get_amp_direction(kcontrol);
955 u32 caps;
956
957 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200958 /* num steps */
959 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
960 if (!caps) {
961 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +0100962 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
963 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 return -EINVAL;
965 }
966 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
967 uinfo->count = chs == 3 ? 2 : 1;
968 uinfo->value.integer.min = 0;
969 uinfo->value.integer.max = caps;
970 return 0;
971}
972
Takashi Iwai0ba21762007-04-16 11:29:14 +0200973int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
974 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975{
976 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
977 hda_nid_t nid = get_amp_nid(kcontrol);
978 int chs = get_amp_channels(kcontrol);
979 int dir = get_amp_direction(kcontrol);
980 int idx = get_amp_index(kcontrol);
981 long *valp = ucontrol->value.integer.value;
982
983 if (chs & 1)
Takashi Iwai47fd8302007-08-10 17:11:07 +0200984 *valp++ = snd_hda_codec_amp_read(codec, nid, 0, dir, idx)
985 & HDA_AMP_VOLMASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 if (chs & 2)
Takashi Iwai47fd8302007-08-10 17:11:07 +0200987 *valp = snd_hda_codec_amp_read(codec, nid, 1, dir, idx)
988 & HDA_AMP_VOLMASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 return 0;
990}
991
Takashi Iwai0ba21762007-04-16 11:29:14 +0200992int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
993 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994{
995 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
996 hda_nid_t nid = get_amp_nid(kcontrol);
997 int chs = get_amp_channels(kcontrol);
998 int dir = get_amp_direction(kcontrol);
999 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 long *valp = ucontrol->value.integer.value;
1001 int change = 0;
1002
Takashi Iwaicb53c622007-08-10 17:21:45 +02001003 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001004 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001005 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
1006 0x7f, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001007 valp++;
1008 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001009 if (chs & 2)
1010 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001011 0x7f, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001012 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 return change;
1014}
1015
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001016int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1017 unsigned int size, unsigned int __user *_tlv)
1018{
1019 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1020 hda_nid_t nid = get_amp_nid(kcontrol);
1021 int dir = get_amp_direction(kcontrol);
1022 u32 caps, val1, val2;
1023
1024 if (size < 4 * sizeof(unsigned int))
1025 return -ENOMEM;
1026 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001027 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1028 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001029 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
1030 val1 = ((int)val1) * ((int)val2);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001031 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
1032 return -EFAULT;
1033 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
1034 return -EFAULT;
1035 if (put_user(val1, _tlv + 2))
1036 return -EFAULT;
1037 if (put_user(val2, _tlv + 3))
1038 return -EFAULT;
1039 return 0;
1040}
1041
Takashi Iwai2134ea42008-01-10 16:53:55 +01001042/*
1043 * set (static) TLV for virtual master volume; recalculated as max 0dB
1044 */
1045void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
1046 unsigned int *tlv)
1047{
1048 u32 caps;
1049 int nums, step;
1050
1051 caps = query_amp_caps(codec, nid, dir);
1052 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1053 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1054 step = (step + 1) * 25;
1055 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
1056 tlv[1] = 2 * sizeof(unsigned int);
1057 tlv[2] = -nums * step;
1058 tlv[3] = step;
1059}
1060
1061/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01001062static struct snd_kcontrol *
1063_snd_hda_find_mixer_ctl(struct hda_codec *codec,
1064 const char *name, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01001065{
1066 struct snd_ctl_elem_id id;
1067 memset(&id, 0, sizeof(id));
1068 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwai09f99702008-02-04 12:31:13 +01001069 id.index = idx;
Takashi Iwai2134ea42008-01-10 16:53:55 +01001070 strcpy(id.name, name);
1071 return snd_ctl_find_id(codec->bus->card, &id);
1072}
1073
Takashi Iwai09f99702008-02-04 12:31:13 +01001074struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
1075 const char *name)
1076{
1077 return _snd_hda_find_mixer_ctl(codec, name, 0);
1078}
1079
Takashi Iwai2134ea42008-01-10 16:53:55 +01001080/* create a virtual master control and add slaves */
1081int snd_hda_add_vmaster(struct hda_codec *codec, char *name,
1082 unsigned int *tlv, const char **slaves)
1083{
1084 struct snd_kcontrol *kctl;
1085 const char **s;
1086 int err;
1087
Takashi Iwai2f085542008-02-22 18:43:50 +01001088 for (s = slaves; *s && !snd_hda_find_mixer_ctl(codec, *s); s++)
1089 ;
1090 if (!*s) {
1091 snd_printdd("No slave found for %s\n", name);
1092 return 0;
1093 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01001094 kctl = snd_ctl_make_virtual_master(name, tlv);
1095 if (!kctl)
1096 return -ENOMEM;
1097 err = snd_ctl_add(codec->bus->card, kctl);
1098 if (err < 0)
1099 return err;
1100
1101 for (s = slaves; *s; s++) {
1102 struct snd_kcontrol *sctl;
1103
1104 sctl = snd_hda_find_mixer_ctl(codec, *s);
1105 if (!sctl) {
1106 snd_printdd("Cannot find slave %s, skipped\n", *s);
1107 continue;
1108 }
1109 err = snd_ctl_add_slave(kctl, sctl);
1110 if (err < 0)
1111 return err;
1112 }
1113 return 0;
1114}
1115
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116/* switch */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001117int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
1118 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119{
1120 int chs = get_amp_channels(kcontrol);
1121
1122 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1123 uinfo->count = chs == 3 ? 2 : 1;
1124 uinfo->value.integer.min = 0;
1125 uinfo->value.integer.max = 1;
1126 return 0;
1127}
1128
Takashi Iwai0ba21762007-04-16 11:29:14 +02001129int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
1130 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131{
1132 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1133 hda_nid_t nid = get_amp_nid(kcontrol);
1134 int chs = get_amp_channels(kcontrol);
1135 int dir = get_amp_direction(kcontrol);
1136 int idx = get_amp_index(kcontrol);
1137 long *valp = ucontrol->value.integer.value;
1138
1139 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02001140 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02001141 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02001143 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02001144 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 return 0;
1146}
1147
Takashi Iwai0ba21762007-04-16 11:29:14 +02001148int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
1149 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150{
1151 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1152 hda_nid_t nid = get_amp_nid(kcontrol);
1153 int chs = get_amp_channels(kcontrol);
1154 int dir = get_amp_direction(kcontrol);
1155 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 long *valp = ucontrol->value.integer.value;
1157 int change = 0;
1158
Takashi Iwaicb53c622007-08-10 17:21:45 +02001159 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001160 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001161 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02001162 HDA_AMP_MUTE,
1163 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001164 valp++;
1165 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001166 if (chs & 2)
1167 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02001168 HDA_AMP_MUTE,
1169 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001170#ifdef CONFIG_SND_HDA_POWER_SAVE
1171 if (codec->patch_ops.check_power_status)
1172 codec->patch_ops.check_power_status(codec, nid);
1173#endif
1174 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 return change;
1176}
1177
1178/*
Takashi Iwai985be542005-11-02 18:26:49 +01001179 * bound volume controls
1180 *
1181 * bind multiple volumes (# indices, from 0)
1182 */
1183
1184#define AMP_VAL_IDX_SHIFT 19
1185#define AMP_VAL_IDX_MASK (0x0f<<19)
1186
Takashi Iwai0ba21762007-04-16 11:29:14 +02001187int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
1188 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01001189{
1190 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1191 unsigned long pval;
1192 int err;
1193
Ingo Molnar62932df2006-01-16 16:34:20 +01001194 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Takashi Iwai985be542005-11-02 18:26:49 +01001195 pval = kcontrol->private_value;
1196 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
1197 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
1198 kcontrol->private_value = pval;
Ingo Molnar62932df2006-01-16 16:34:20 +01001199 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01001200 return err;
1201}
1202
Takashi Iwai0ba21762007-04-16 11:29:14 +02001203int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
1204 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01001205{
1206 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1207 unsigned long pval;
1208 int i, indices, err = 0, change = 0;
1209
Ingo Molnar62932df2006-01-16 16:34:20 +01001210 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Takashi Iwai985be542005-11-02 18:26:49 +01001211 pval = kcontrol->private_value;
1212 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
1213 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001214 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
1215 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01001216 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1217 if (err < 0)
1218 break;
1219 change |= err;
1220 }
1221 kcontrol->private_value = pval;
Ingo Molnar62932df2006-01-16 16:34:20 +01001222 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01001223 return err < 0 ? err : change;
1224}
1225
1226/*
Takashi Iwai532d5382007-07-27 19:02:40 +02001227 * generic bound volume/swtich controls
1228 */
1229int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
1230 struct snd_ctl_elem_info *uinfo)
1231{
1232 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1233 struct hda_bind_ctls *c;
1234 int err;
1235
Takashi Iwai532d5382007-07-27 19:02:40 +02001236 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01001237 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02001238 kcontrol->private_value = *c->values;
1239 err = c->ops->info(kcontrol, uinfo);
1240 kcontrol->private_value = (long)c;
1241 mutex_unlock(&codec->spdif_mutex);
1242 return err;
1243}
1244
1245int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
1246 struct snd_ctl_elem_value *ucontrol)
1247{
1248 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1249 struct hda_bind_ctls *c;
1250 int err;
1251
Takashi Iwai532d5382007-07-27 19:02:40 +02001252 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01001253 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02001254 kcontrol->private_value = *c->values;
1255 err = c->ops->get(kcontrol, ucontrol);
1256 kcontrol->private_value = (long)c;
1257 mutex_unlock(&codec->spdif_mutex);
1258 return err;
1259}
1260
1261int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
1262 struct snd_ctl_elem_value *ucontrol)
1263{
1264 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1265 struct hda_bind_ctls *c;
1266 unsigned long *vals;
1267 int err = 0, change = 0;
1268
Takashi Iwai532d5382007-07-27 19:02:40 +02001269 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01001270 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02001271 for (vals = c->values; *vals; vals++) {
1272 kcontrol->private_value = *vals;
1273 err = c->ops->put(kcontrol, ucontrol);
1274 if (err < 0)
1275 break;
1276 change |= err;
1277 }
1278 kcontrol->private_value = (long)c;
1279 mutex_unlock(&codec->spdif_mutex);
1280 return err < 0 ? err : change;
1281}
1282
1283int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1284 unsigned int size, unsigned int __user *tlv)
1285{
1286 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1287 struct hda_bind_ctls *c;
1288 int err;
1289
Takashi Iwai532d5382007-07-27 19:02:40 +02001290 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01001291 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02001292 kcontrol->private_value = *c->values;
1293 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
1294 kcontrol->private_value = (long)c;
1295 mutex_unlock(&codec->spdif_mutex);
1296 return err;
1297}
1298
1299struct hda_ctl_ops snd_hda_bind_vol = {
1300 .info = snd_hda_mixer_amp_volume_info,
1301 .get = snd_hda_mixer_amp_volume_get,
1302 .put = snd_hda_mixer_amp_volume_put,
1303 .tlv = snd_hda_mixer_amp_tlv
1304};
1305
1306struct hda_ctl_ops snd_hda_bind_sw = {
1307 .info = snd_hda_mixer_amp_switch_info,
1308 .get = snd_hda_mixer_amp_switch_get,
1309 .put = snd_hda_mixer_amp_switch_put,
1310 .tlv = snd_hda_mixer_amp_tlv
1311};
1312
1313/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 * SPDIF out controls
1315 */
1316
Takashi Iwai0ba21762007-04-16 11:29:14 +02001317static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
1318 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319{
1320 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1321 uinfo->count = 1;
1322 return 0;
1323}
1324
Takashi Iwai0ba21762007-04-16 11:29:14 +02001325static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
1326 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327{
1328 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
1329 IEC958_AES0_NONAUDIO |
1330 IEC958_AES0_CON_EMPHASIS_5015 |
1331 IEC958_AES0_CON_NOT_COPYRIGHT;
1332 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
1333 IEC958_AES1_CON_ORIGINAL;
1334 return 0;
1335}
1336
Takashi Iwai0ba21762007-04-16 11:29:14 +02001337static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
1338 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339{
1340 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
1341 IEC958_AES0_NONAUDIO |
1342 IEC958_AES0_PRO_EMPHASIS_5015;
1343 return 0;
1344}
1345
Takashi Iwai0ba21762007-04-16 11:29:14 +02001346static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
1347 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348{
1349 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1350
1351 ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
1352 ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
1353 ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
1354 ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
1355
1356 return 0;
1357}
1358
1359/* convert from SPDIF status bits to HDA SPDIF bits
1360 * bit 0 (DigEn) is always set zero (to be filled later)
1361 */
1362static unsigned short convert_from_spdif_status(unsigned int sbits)
1363{
1364 unsigned short val = 0;
1365
1366 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02001367 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02001369 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001371 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
1372 IEC958_AES0_PRO_EMPHASIS_5015)
1373 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001375 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
1376 IEC958_AES0_CON_EMPHASIS_5015)
1377 val |= AC_DIG1_EMPHASIS;
1378 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
1379 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02001381 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
1383 }
1384 return val;
1385}
1386
1387/* convert to SPDIF status bits from HDA SPDIF bits
1388 */
1389static unsigned int convert_to_spdif_status(unsigned short val)
1390{
1391 unsigned int sbits = 0;
1392
Takashi Iwai0ba21762007-04-16 11:29:14 +02001393 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001395 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 sbits |= IEC958_AES0_PROFESSIONAL;
1397 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001398 if (sbits & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
1400 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001401 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001403 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001405 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
1407 sbits |= val & (0x7f << 8);
1408 }
1409 return sbits;
1410}
1411
Takashi Iwai2f728532008-09-25 16:32:41 +02001412/* set digital convert verbs both for the given NID and its slaves */
1413static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
1414 int verb, int val)
1415{
1416 hda_nid_t *d;
1417
1418 snd_hda_codec_write(codec, nid, 0, verb, val);
1419 d = codec->slave_dig_outs;
1420 if (!d)
1421 return;
1422 for (; *d; d++)
1423 snd_hda_codec_write(codec, *d, 0, verb, val);
1424}
1425
1426static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
1427 int dig1, int dig2)
1428{
1429 if (dig1 != -1)
1430 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
1431 if (dig2 != -1)
1432 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
1433}
1434
Takashi Iwai0ba21762007-04-16 11:29:14 +02001435static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
1436 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437{
1438 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1439 hda_nid_t nid = kcontrol->private_value;
1440 unsigned short val;
1441 int change;
1442
Ingo Molnar62932df2006-01-16 16:34:20 +01001443 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 codec->spdif_status = ucontrol->value.iec958.status[0] |
1445 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
1446 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
1447 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
1448 val = convert_from_spdif_status(codec->spdif_status);
1449 val |= codec->spdif_ctls & 1;
1450 change = codec->spdif_ctls != val;
1451 codec->spdif_ctls = val;
1452
Takashi Iwai2f728532008-09-25 16:32:41 +02001453 if (change)
1454 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
Ingo Molnar62932df2006-01-16 16:34:20 +01001456 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 return change;
1458}
1459
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001460#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
Takashi Iwai0ba21762007-04-16 11:29:14 +02001462static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
1463 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464{
1465 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1466
Takashi Iwai0ba21762007-04-16 11:29:14 +02001467 ucontrol->value.integer.value[0] = codec->spdif_ctls & AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 return 0;
1469}
1470
Takashi Iwai0ba21762007-04-16 11:29:14 +02001471static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
1472 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473{
1474 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1475 hda_nid_t nid = kcontrol->private_value;
1476 unsigned short val;
1477 int change;
1478
Ingo Molnar62932df2006-01-16 16:34:20 +01001479 mutex_lock(&codec->spdif_mutex);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001480 val = codec->spdif_ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02001482 val |= AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 change = codec->spdif_ctls != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001484 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 codec->spdif_ctls = val;
Takashi Iwai2f728532008-09-25 16:32:41 +02001486 set_dig_out_convert(codec, nid, val & 0xff, -1);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001487 /* unmute amp switch (if any) */
1488 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
Takashi Iwai47fd8302007-08-10 17:11:07 +02001489 (val & AC_DIG1_ENABLE))
1490 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
1491 HDA_AMP_MUTE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 }
Ingo Molnar62932df2006-01-16 16:34:20 +01001493 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 return change;
1495}
1496
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001497static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 {
1499 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1500 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1501 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
1502 .info = snd_hda_spdif_mask_info,
1503 .get = snd_hda_spdif_cmask_get,
1504 },
1505 {
1506 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1507 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1508 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
1509 .info = snd_hda_spdif_mask_info,
1510 .get = snd_hda_spdif_pmask_get,
1511 },
1512 {
1513 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1514 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1515 .info = snd_hda_spdif_mask_info,
1516 .get = snd_hda_spdif_default_get,
1517 .put = snd_hda_spdif_default_put,
1518 },
1519 {
1520 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1521 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
1522 .info = snd_hda_spdif_out_switch_info,
1523 .get = snd_hda_spdif_out_switch_get,
1524 .put = snd_hda_spdif_out_switch_put,
1525 },
1526 { } /* end */
1527};
1528
Takashi Iwai09f99702008-02-04 12:31:13 +01001529#define SPDIF_MAX_IDX 4 /* 4 instances should be enough to probe */
1530
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531/**
1532 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
1533 * @codec: the HDA codec
1534 * @nid: audio out widget NID
1535 *
1536 * Creates controls related with the SPDIF output.
1537 * Called from each patch supporting the SPDIF out.
1538 *
1539 * Returns 0 if successful, or a negative error code.
1540 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02001541int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542{
1543 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001544 struct snd_kcontrol *kctl;
1545 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01001546 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
Takashi Iwai09f99702008-02-04 12:31:13 +01001548 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
1549 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Playback Switch",
1550 idx))
1551 break;
1552 }
1553 if (idx >= SPDIF_MAX_IDX) {
1554 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
1555 return -EBUSY;
1556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
1558 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwai09f99702008-02-04 12:31:13 +01001559 kctl->id.index = idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 kctl->private_value = nid;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001561 err = snd_ctl_add(codec->bus->card, kctl);
1562 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 return err;
1564 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02001565 codec->spdif_ctls =
Andrew Paprocki3982d172007-12-19 12:13:44 +01001566 snd_hda_codec_read(codec, nid, 0,
1567 AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
1569 return 0;
1570}
1571
1572/*
Takashi Iwai9a081602008-02-12 18:37:26 +01001573 * SPDIF sharing with analog output
1574 */
1575static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
1576 struct snd_ctl_elem_value *ucontrol)
1577{
1578 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
1579 ucontrol->value.integer.value[0] = mout->share_spdif;
1580 return 0;
1581}
1582
1583static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
1584 struct snd_ctl_elem_value *ucontrol)
1585{
1586 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
1587 mout->share_spdif = !!ucontrol->value.integer.value[0];
1588 return 0;
1589}
1590
1591static struct snd_kcontrol_new spdif_share_sw = {
1592 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1593 .name = "IEC958 Default PCM Playback Switch",
1594 .info = snd_ctl_boolean_mono_info,
1595 .get = spdif_share_sw_get,
1596 .put = spdif_share_sw_put,
1597};
1598
1599int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
1600 struct hda_multi_out *mout)
1601{
1602 if (!mout->dig_out_nid)
1603 return 0;
1604 /* ATTENTION: here mout is passed as private_data, instead of codec */
1605 return snd_ctl_add(codec->bus->card,
1606 snd_ctl_new1(&spdif_share_sw, mout));
1607}
1608
1609/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 * SPDIF input
1611 */
1612
1613#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
1614
Takashi Iwai0ba21762007-04-16 11:29:14 +02001615static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
1616 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617{
1618 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1619
1620 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
1621 return 0;
1622}
1623
Takashi Iwai0ba21762007-04-16 11:29:14 +02001624static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
1625 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626{
1627 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1628 hda_nid_t nid = kcontrol->private_value;
1629 unsigned int val = !!ucontrol->value.integer.value[0];
1630 int change;
1631
Ingo Molnar62932df2006-01-16 16:34:20 +01001632 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001634 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001636 snd_hda_codec_write_cache(codec, nid, 0,
1637 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 }
Ingo Molnar62932df2006-01-16 16:34:20 +01001639 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 return change;
1641}
1642
Takashi Iwai0ba21762007-04-16 11:29:14 +02001643static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
1644 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645{
1646 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1647 hda_nid_t nid = kcontrol->private_value;
1648 unsigned short val;
1649 unsigned int sbits;
1650
Andrew Paprocki3982d172007-12-19 12:13:44 +01001651 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 sbits = convert_to_spdif_status(val);
1653 ucontrol->value.iec958.status[0] = sbits;
1654 ucontrol->value.iec958.status[1] = sbits >> 8;
1655 ucontrol->value.iec958.status[2] = sbits >> 16;
1656 ucontrol->value.iec958.status[3] = sbits >> 24;
1657 return 0;
1658}
1659
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001660static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 {
1662 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1663 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH),
1664 .info = snd_hda_spdif_in_switch_info,
1665 .get = snd_hda_spdif_in_switch_get,
1666 .put = snd_hda_spdif_in_switch_put,
1667 },
1668 {
1669 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1670 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1671 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT),
1672 .info = snd_hda_spdif_mask_info,
1673 .get = snd_hda_spdif_in_status_get,
1674 },
1675 { } /* end */
1676};
1677
1678/**
1679 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
1680 * @codec: the HDA codec
1681 * @nid: audio in widget NID
1682 *
1683 * Creates controls related with the SPDIF input.
1684 * Called from each patch supporting the SPDIF in.
1685 *
1686 * Returns 0 if successful, or a negative error code.
1687 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02001688int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689{
1690 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001691 struct snd_kcontrol *kctl;
1692 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01001693 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
Takashi Iwai09f99702008-02-04 12:31:13 +01001695 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
1696 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Capture Switch",
1697 idx))
1698 break;
1699 }
1700 if (idx >= SPDIF_MAX_IDX) {
1701 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
1702 return -EBUSY;
1703 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
1705 kctl = snd_ctl_new1(dig_mix, codec);
1706 kctl->private_value = nid;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001707 err = snd_ctl_add(codec->bus->card, kctl);
1708 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 return err;
1710 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02001711 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01001712 snd_hda_codec_read(codec, nid, 0,
1713 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02001714 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 return 0;
1716}
1717
Takashi Iwaicb53c622007-08-10 17:21:45 +02001718#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001719/*
1720 * command cache
1721 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001723/* build a 32bit cache key with the widget id and the command parameter */
1724#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
1725#define get_cmd_cache_nid(key) ((key) & 0xff)
1726#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
1727
1728/**
1729 * snd_hda_codec_write_cache - send a single command with caching
1730 * @codec: the HDA codec
1731 * @nid: NID to send the command
1732 * @direct: direct flag
1733 * @verb: the verb to send
1734 * @parm: the parameter for the verb
1735 *
1736 * Send a single command without waiting for response.
1737 *
1738 * Returns 0 if successful, or a negative error code.
1739 */
1740int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
1741 int direct, unsigned int verb, unsigned int parm)
1742{
1743 int err;
Takashi Iwaicb53c622007-08-10 17:21:45 +02001744 snd_hda_power_up(codec);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001745 mutex_lock(&codec->bus->cmd_mutex);
1746 err = codec->bus->ops.command(codec, nid, direct, verb, parm);
1747 if (!err) {
1748 struct hda_cache_head *c;
1749 u32 key = build_cmd_cache_key(nid, verb);
1750 c = get_alloc_hash(&codec->cmd_cache, key);
1751 if (c)
1752 c->val = parm;
1753 }
1754 mutex_unlock(&codec->bus->cmd_mutex);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001755 snd_hda_power_down(codec);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001756 return err;
1757}
1758
1759/* resume the all commands from the cache */
1760void snd_hda_codec_resume_cache(struct hda_codec *codec)
1761{
Takashi Iwai603c4012008-07-30 15:01:44 +02001762 struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001763 int i;
1764
Takashi Iwai603c4012008-07-30 15:01:44 +02001765 for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001766 u32 key = buffer->key;
1767 if (!key)
1768 continue;
1769 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
1770 get_cmd_cache_cmd(key), buffer->val);
1771 }
1772}
1773
1774/**
1775 * snd_hda_sequence_write_cache - sequence writes with caching
1776 * @codec: the HDA codec
1777 * @seq: VERB array to send
1778 *
1779 * Send the commands sequentially from the given array.
1780 * Thte commands are recorded on cache for power-save and resume.
1781 * The array must be terminated with NID=0.
1782 */
1783void snd_hda_sequence_write_cache(struct hda_codec *codec,
1784 const struct hda_verb *seq)
1785{
1786 for (; seq->nid; seq++)
1787 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
1788 seq->param);
1789}
Takashi Iwaicb53c622007-08-10 17:21:45 +02001790#endif /* SND_HDA_NEEDS_RESUME */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001791
Takashi Iwai54d17402005-11-21 16:33:22 +01001792/*
1793 * set power state of the codec
1794 */
1795static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1796 unsigned int power_state)
1797{
Takashi Iwaicb53c622007-08-10 17:21:45 +02001798 hda_nid_t nid;
1799 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01001800
1801 snd_hda_codec_write(codec, fg, 0, AC_VERB_SET_POWER_STATE,
1802 power_state);
Marc Boucherd2595d82008-01-22 15:23:30 +01001803 msleep(10); /* partial workaround for "azx_get_response timeout" */
Takashi Iwai54d17402005-11-21 16:33:22 +01001804
Takashi Iwaicb53c622007-08-10 17:21:45 +02001805 nid = codec->start_nid;
1806 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01001807 unsigned int wcaps = get_wcaps(codec, nid);
1808 if (wcaps & AC_WCAP_POWER) {
1809 unsigned int wid_type = (wcaps & AC_WCAP_TYPE) >>
1810 AC_WCAP_TYPE_SHIFT;
1811 if (wid_type == AC_WID_PIN) {
1812 unsigned int pincap;
1813 /*
1814 * don't power down the widget if it controls
1815 * eapd and EAPD_BTLENABLE is set.
1816 */
1817 pincap = snd_hda_param_read(codec, nid,
1818 AC_PAR_PIN_CAP);
1819 if (pincap & AC_PINCAP_EAPD) {
1820 int eapd = snd_hda_codec_read(codec,
1821 nid, 0,
1822 AC_VERB_GET_EAPD_BTLENABLE, 0);
1823 eapd &= 0x02;
1824 if (power_state == AC_PWRST_D3 && eapd)
1825 continue;
1826 }
Takashi Iwai1194b5b2007-10-10 10:04:26 +02001827 }
Takashi Iwai54d17402005-11-21 16:33:22 +01001828 snd_hda_codec_write(codec, nid, 0,
1829 AC_VERB_SET_POWER_STATE,
1830 power_state);
Takashi Iwai1194b5b2007-10-10 10:04:26 +02001831 }
Takashi Iwai54d17402005-11-21 16:33:22 +01001832 }
1833
Takashi Iwaicb53c622007-08-10 17:21:45 +02001834 if (power_state == AC_PWRST_D0) {
1835 unsigned long end_time;
1836 int state;
Takashi Iwai54d17402005-11-21 16:33:22 +01001837 msleep(10);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001838 /* wait until the codec reachs to D0 */
1839 end_time = jiffies + msecs_to_jiffies(500);
1840 do {
1841 state = snd_hda_codec_read(codec, fg, 0,
1842 AC_VERB_GET_POWER_STATE, 0);
1843 if (state == power_state)
1844 break;
1845 msleep(1);
1846 } while (time_after_eq(end_time, jiffies));
1847 }
Takashi Iwai54d17402005-11-21 16:33:22 +01001848}
1849
Takashi Iwaicb53c622007-08-10 17:21:45 +02001850#ifdef SND_HDA_NEEDS_RESUME
1851/*
1852 * call suspend and power-down; used both from PM and power-save
1853 */
1854static void hda_call_codec_suspend(struct hda_codec *codec)
1855{
1856 if (codec->patch_ops.suspend)
1857 codec->patch_ops.suspend(codec, PMSG_SUSPEND);
1858 hda_set_power_state(codec,
1859 codec->afg ? codec->afg : codec->mfg,
1860 AC_PWRST_D3);
1861#ifdef CONFIG_SND_HDA_POWER_SAVE
1862 cancel_delayed_work(&codec->power_work);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02001863 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02001864 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02001865#endif
1866}
1867
1868/*
1869 * kick up codec; used both from PM and power-save
1870 */
1871static void hda_call_codec_resume(struct hda_codec *codec)
1872{
1873 hda_set_power_state(codec,
1874 codec->afg ? codec->afg : codec->mfg,
1875 AC_PWRST_D0);
1876 if (codec->patch_ops.resume)
1877 codec->patch_ops.resume(codec);
1878 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02001879 if (codec->patch_ops.init)
1880 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001881 snd_hda_codec_resume_amp(codec);
1882 snd_hda_codec_resume_cache(codec);
1883 }
1884}
1885#endif /* SND_HDA_NEEDS_RESUME */
1886
Takashi Iwai54d17402005-11-21 16:33:22 +01001887
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888/**
1889 * snd_hda_build_controls - build mixer controls
1890 * @bus: the BUS
1891 *
1892 * Creates mixer controls for each codec included in the bus.
1893 *
1894 * Returns 0 if successful, otherwise a negative error code.
1895 */
Takashi Iwai756e2b02007-04-16 11:27:07 +02001896int __devinit snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001898 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
Takashi Iwai0ba21762007-04-16 11:29:14 +02001900 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaicb53c622007-08-10 17:21:45 +02001901 int err = 0;
1902 /* fake as if already powered-on */
1903 hda_keep_power_on(codec);
1904 /* then fire up */
1905 hda_set_power_state(codec,
1906 codec->afg ? codec->afg : codec->mfg,
1907 AC_PWRST_D0);
1908 /* continue to initialize... */
1909 if (codec->patch_ops.init)
1910 err = codec->patch_ops.init(codec);
1911 if (!err && codec->patch_ops.build_controls)
1912 err = codec->patch_ops.build_controls(codec);
1913 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 if (err < 0)
1915 return err;
1916 }
1917
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 return 0;
1919}
1920
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921/*
1922 * stream formats
1923 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02001924struct hda_rate_tbl {
1925 unsigned int hz;
1926 unsigned int alsa_bits;
1927 unsigned int hda_fmt;
1928};
1929
1930static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02001932
1933 /* autodetected value used in snd_hda_query_supported_pcm */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 { 8000, SNDRV_PCM_RATE_8000, 0x0500 }, /* 1/6 x 48 */
1935 { 11025, SNDRV_PCM_RATE_11025, 0x4300 }, /* 1/4 x 44 */
1936 { 16000, SNDRV_PCM_RATE_16000, 0x0200 }, /* 1/3 x 48 */
1937 { 22050, SNDRV_PCM_RATE_22050, 0x4100 }, /* 1/2 x 44 */
1938 { 32000, SNDRV_PCM_RATE_32000, 0x0a00 }, /* 2/3 x 48 */
1939 { 44100, SNDRV_PCM_RATE_44100, 0x4000 }, /* 44 */
1940 { 48000, SNDRV_PCM_RATE_48000, 0x0000 }, /* 48 */
1941 { 88200, SNDRV_PCM_RATE_88200, 0x4800 }, /* 2 x 44 */
1942 { 96000, SNDRV_PCM_RATE_96000, 0x0800 }, /* 2 x 48 */
1943 { 176400, SNDRV_PCM_RATE_176400, 0x5800 },/* 4 x 44 */
1944 { 192000, SNDRV_PCM_RATE_192000, 0x1800 }, /* 4 x 48 */
Takashi Iwaia961f9f2007-04-12 13:08:09 +02001945#define AC_PAR_PCM_RATE_BITS 11
1946 /* up to bits 10, 384kHZ isn't supported properly */
1947
1948 /* not autodetected value */
1949 { 9600, SNDRV_PCM_RATE_KNOT, 0x0400 }, /* 1/5 x 48 */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02001950
Takashi Iwaibefdf312005-08-22 13:57:55 +02001951 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952};
1953
1954/**
1955 * snd_hda_calc_stream_format - calculate format bitset
1956 * @rate: the sample rate
1957 * @channels: the number of channels
1958 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
1959 * @maxbps: the max. bps
1960 *
1961 * Calculate the format bitset from the given rate, channels and th PCM format.
1962 *
1963 * Return zero if invalid.
1964 */
1965unsigned int snd_hda_calc_stream_format(unsigned int rate,
1966 unsigned int channels,
1967 unsigned int format,
1968 unsigned int maxbps)
1969{
1970 int i;
1971 unsigned int val = 0;
1972
Takashi Iwaibefdf312005-08-22 13:57:55 +02001973 for (i = 0; rate_bits[i].hz; i++)
1974 if (rate_bits[i].hz == rate) {
1975 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 break;
1977 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02001978 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 snd_printdd("invalid rate %d\n", rate);
1980 return 0;
1981 }
1982
1983 if (channels == 0 || channels > 8) {
1984 snd_printdd("invalid channels %d\n", channels);
1985 return 0;
1986 }
1987 val |= channels - 1;
1988
1989 switch (snd_pcm_format_width(format)) {
1990 case 8: val |= 0x00; break;
1991 case 16: val |= 0x10; break;
1992 case 20:
1993 case 24:
1994 case 32:
1995 if (maxbps >= 32)
1996 val |= 0x40;
1997 else if (maxbps >= 24)
1998 val |= 0x30;
1999 else
2000 val |= 0x20;
2001 break;
2002 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002003 snd_printdd("invalid format width %d\n",
2004 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 return 0;
2006 }
2007
2008 return val;
2009}
2010
2011/**
2012 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
2013 * @codec: the HDA codec
2014 * @nid: NID to query
2015 * @ratesp: the pointer to store the detected rate bitflags
2016 * @formatsp: the pointer to store the detected formats
2017 * @bpsp: the pointer to store the detected format widths
2018 *
2019 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
2020 * or @bsps argument is ignored.
2021 *
2022 * Returns 0 if successful, otherwise a negative error code.
2023 */
2024int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
2025 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
2026{
2027 int i;
2028 unsigned int val, streams;
2029
2030 val = 0;
2031 if (nid != codec->afg &&
Takashi Iwai54d17402005-11-21 16:33:22 +01002032 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
2034 if (val == -1)
2035 return -EIO;
2036 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002037 if (!val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
2039
2040 if (ratesp) {
2041 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02002042 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02002044 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 }
2046 *ratesp = rates;
2047 }
2048
2049 if (formatsp || bpsp) {
2050 u64 formats = 0;
2051 unsigned int bps;
2052 unsigned int wcaps;
2053
Takashi Iwai54d17402005-11-21 16:33:22 +01002054 wcaps = get_wcaps(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
2056 if (streams == -1)
2057 return -EIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002058 if (!streams) {
2059 streams = snd_hda_param_read(codec, codec->afg,
2060 AC_PAR_STREAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 if (streams == -1)
2062 return -EIO;
2063 }
2064
2065 bps = 0;
2066 if (streams & AC_SUPFMT_PCM) {
2067 if (val & AC_SUPPCM_BITS_8) {
2068 formats |= SNDRV_PCM_FMTBIT_U8;
2069 bps = 8;
2070 }
2071 if (val & AC_SUPPCM_BITS_16) {
2072 formats |= SNDRV_PCM_FMTBIT_S16_LE;
2073 bps = 16;
2074 }
2075 if (wcaps & AC_WCAP_DIGITAL) {
2076 if (val & AC_SUPPCM_BITS_32)
2077 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
2078 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
2079 formats |= SNDRV_PCM_FMTBIT_S32_LE;
2080 if (val & AC_SUPPCM_BITS_24)
2081 bps = 24;
2082 else if (val & AC_SUPPCM_BITS_20)
2083 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002084 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
2085 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 formats |= SNDRV_PCM_FMTBIT_S32_LE;
2087 if (val & AC_SUPPCM_BITS_32)
2088 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 else if (val & AC_SUPPCM_BITS_24)
2090 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02002091 else if (val & AC_SUPPCM_BITS_20)
2092 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 }
2094 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002095 else if (streams == AC_SUPFMT_FLOAT32) {
2096 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
2098 bps = 32;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002099 } else if (streams == AC_SUPFMT_AC3) {
2100 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 /* temporary hack: we have still no proper support
2102 * for the direct AC3 stream...
2103 */
2104 formats |= SNDRV_PCM_FMTBIT_U8;
2105 bps = 8;
2106 }
2107 if (formatsp)
2108 *formatsp = formats;
2109 if (bpsp)
2110 *bpsp = bps;
2111 }
2112
2113 return 0;
2114}
2115
2116/**
Takashi Iwai0ba21762007-04-16 11:29:14 +02002117 * snd_hda_is_supported_format - check whether the given node supports
2118 * the format val
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 *
2120 * Returns 1 if supported, 0 if not.
2121 */
2122int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
2123 unsigned int format)
2124{
2125 int i;
2126 unsigned int val = 0, rate, stream;
2127
2128 if (nid != codec->afg &&
Takashi Iwai54d17402005-11-21 16:33:22 +01002129 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
2131 if (val == -1)
2132 return 0;
2133 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002134 if (!val) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
2136 if (val == -1)
2137 return 0;
2138 }
2139
2140 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02002141 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02002142 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 if (val & (1 << i))
2144 break;
2145 return 0;
2146 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02002147 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 return 0;
2149
2150 stream = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
2151 if (stream == -1)
2152 return 0;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002153 if (!stream && nid != codec->afg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 stream = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002155 if (!stream || stream == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 return 0;
2157
2158 if (stream & AC_SUPFMT_PCM) {
2159 switch (format & 0xf0) {
2160 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002161 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 return 0;
2163 break;
2164 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002165 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 return 0;
2167 break;
2168 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002169 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 return 0;
2171 break;
2172 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002173 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 return 0;
2175 break;
2176 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002177 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 return 0;
2179 break;
2180 default:
2181 return 0;
2182 }
2183 } else {
2184 /* FIXME: check for float32 and AC3? */
2185 }
2186
2187 return 1;
2188}
2189
2190/*
2191 * PCM stuff
2192 */
2193static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
2194 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002195 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196{
2197 return 0;
2198}
2199
2200static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
2201 struct hda_codec *codec,
2202 unsigned int stream_tag,
2203 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002204 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205{
2206 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
2207 return 0;
2208}
2209
2210static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
2211 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002212 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213{
Takashi Iwai888afa12008-03-18 09:57:50 +01002214 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 return 0;
2216}
2217
Takashi Iwai0ba21762007-04-16 11:29:14 +02002218static int __devinit set_pcm_default_values(struct hda_codec *codec,
2219 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220{
Takashi Iwai0ba21762007-04-16 11:29:14 +02002221 /* query support PCM information from the given NID */
2222 if (info->nid && (!info->rates || !info->formats)) {
2223 snd_hda_query_supported_pcm(codec, info->nid,
2224 info->rates ? NULL : &info->rates,
2225 info->formats ? NULL : &info->formats,
2226 info->maxbps ? NULL : &info->maxbps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 }
2228 if (info->ops.open == NULL)
2229 info->ops.open = hda_pcm_default_open_close;
2230 if (info->ops.close == NULL)
2231 info->ops.close = hda_pcm_default_open_close;
2232 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02002233 if (snd_BUG_ON(!info->nid))
2234 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 info->ops.prepare = hda_pcm_default_prepare;
2236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02002238 if (snd_BUG_ON(!info->nid))
2239 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 info->ops.cleanup = hda_pcm_default_cleanup;
2241 }
2242 return 0;
2243}
2244
Takashi Iwai176d5332008-07-30 15:01:44 +02002245/*
2246 * attach a new PCM stream
2247 */
2248static int __devinit
2249snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
2250{
2251 struct hda_pcm_stream *info;
2252 int stream, err;
2253
2254 if (!pcm->name)
2255 return -EINVAL;
2256 for (stream = 0; stream < 2; stream++) {
2257 info = &pcm->stream[stream];
2258 if (info->substreams) {
2259 err = set_pcm_default_values(codec, info);
2260 if (err < 0)
2261 return err;
2262 }
2263 }
2264 return codec->bus->ops.attach_pcm(codec, pcm);
2265}
2266
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267/**
2268 * snd_hda_build_pcms - build PCM information
2269 * @bus: the BUS
2270 *
2271 * Create PCM information for each codec included in the bus.
2272 *
2273 * The build_pcms codec patch is requested to set up codec->num_pcms and
2274 * codec->pcm_info properly. The array is referred by the top-level driver
2275 * to create its PCM instances.
2276 * The allocated codec->pcm_info should be released in codec->patch_ops.free
2277 * callback.
2278 *
2279 * At least, substreams, channels_min and channels_max must be filled for
2280 * each stream. substreams = 0 indicates that the stream doesn't exist.
2281 * When rates and/or formats are zero, the supported values are queried
2282 * from the given nid. The nid is used also by the default ops.prepare
2283 * and ops.cleanup callbacks.
2284 *
2285 * The driver needs to call ops.open in its open callback. Similarly,
2286 * ops.close is supposed to be called in the close callback.
2287 * ops.prepare should be called in the prepare or hw_params callback
2288 * with the proper parameters for set up.
2289 * ops.cleanup should be called in hw_free for clean up of streams.
2290 *
2291 * This function returns 0 if successfull, or a negative error code.
2292 */
Takashi Iwai756e2b02007-04-16 11:27:07 +02002293int __devinit snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294{
Takashi Iwai176d5332008-07-30 15:01:44 +02002295 static const char *dev_name[HDA_PCM_NTYPES] = {
2296 "Audio", "SPDIF", "HDMI", "Modem"
2297 };
2298 /* starting device index for each PCM type */
2299 static int dev_idx[HDA_PCM_NTYPES] = {
2300 [HDA_PCM_TYPE_AUDIO] = 0,
2301 [HDA_PCM_TYPE_SPDIF] = 1,
2302 [HDA_PCM_TYPE_HDMI] = 3,
2303 [HDA_PCM_TYPE_MODEM] = 6
2304 };
2305 /* normal audio device indices; not linear to keep compatibility */
2306 static int audio_idx[4] = { 0, 2, 4, 5 };
Takashi Iwai0ba21762007-04-16 11:29:14 +02002307 struct hda_codec *codec;
Takashi Iwai176d5332008-07-30 15:01:44 +02002308 int num_devs[HDA_PCM_NTYPES];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309
Takashi Iwai176d5332008-07-30 15:01:44 +02002310 memset(num_devs, 0, sizeof(num_devs));
Takashi Iwai0ba21762007-04-16 11:29:14 +02002311 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai176d5332008-07-30 15:01:44 +02002312 unsigned int pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 int err;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002314 if (!codec->patch_ops.build_pcms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 continue;
2316 err = codec->patch_ops.build_pcms(codec);
2317 if (err < 0)
2318 return err;
2319 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
Takashi Iwai176d5332008-07-30 15:01:44 +02002320 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2321 int type = cpcm->pcm_type;
2322 switch (type) {
2323 case HDA_PCM_TYPE_AUDIO:
2324 if (num_devs[type] >= ARRAY_SIZE(audio_idx)) {
2325 snd_printk(KERN_WARNING
2326 "Too many audio devices\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 continue;
Takashi Iwai176d5332008-07-30 15:01:44 +02002328 }
2329 cpcm->device = audio_idx[num_devs[type]];
2330 break;
2331 case HDA_PCM_TYPE_SPDIF:
2332 case HDA_PCM_TYPE_HDMI:
2333 case HDA_PCM_TYPE_MODEM:
2334 if (num_devs[type]) {
2335 snd_printk(KERN_WARNING
2336 "%s already defined\n",
2337 dev_name[type]);
2338 continue;
2339 }
2340 cpcm->device = dev_idx[type];
2341 break;
2342 default:
2343 snd_printk(KERN_WARNING
2344 "Invalid PCM type %d\n", type);
2345 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 }
Takashi Iwai176d5332008-07-30 15:01:44 +02002347 num_devs[type]++;
2348 err = snd_hda_attach_pcm(codec, cpcm);
2349 if (err < 0)
2350 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 }
2352 }
2353 return 0;
2354}
2355
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356/**
2357 * snd_hda_check_board_config - compare the current codec with the config table
2358 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002359 * @num_configs: number of config enums
2360 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 * @tbl: configuration table, terminated by null entries
2362 *
2363 * Compares the modelname or PCI subsystem id of the current codec with the
2364 * given configuration table. If a matching entry is found, returns its
2365 * config value (supposed to be 0 or positive).
2366 *
2367 * If no entries are matching, the function returns a negative value.
2368 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002369int snd_hda_check_board_config(struct hda_codec *codec,
2370 int num_configs, const char **models,
2371 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372{
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002373 if (codec->bus->modelname && models) {
2374 int i;
2375 for (i = 0; i < num_configs; i++) {
2376 if (models[i] &&
2377 !strcmp(codec->bus->modelname, models[i])) {
2378 snd_printd(KERN_INFO "hda_codec: model '%s' is "
2379 "selected\n", models[i]);
2380 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 }
2382 }
2383 }
2384
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002385 if (!codec->bus->pci || !tbl)
2386 return -1;
2387
2388 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
2389 if (!tbl)
2390 return -1;
2391 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02002392#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002393 char tmp[10];
2394 const char *model = NULL;
2395 if (models)
2396 model = models[tbl->value];
2397 if (!model) {
2398 sprintf(tmp, "#%d", tbl->value);
2399 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002401 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
2402 "for config %x:%x (%s)\n",
2403 model, tbl->subvendor, tbl->subdevice,
2404 (tbl->name ? tbl->name : "Unknown device"));
2405#endif
2406 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 }
2408 return -1;
2409}
2410
2411/**
2412 * snd_hda_add_new_ctls - create controls from the array
2413 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002414 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 *
2416 * This helper function creates and add new controls in the given array.
2417 * The array must be terminated with an empty entry as terminator.
2418 *
2419 * Returns 0 if successful, or a negative error code.
2420 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002421int snd_hda_add_new_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422{
Takashi Iwaicb53c622007-08-10 17:21:45 +02002423 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424
2425 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01002426 struct snd_kcontrol *kctl;
2427 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002428 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01002429 return -ENOMEM;
2430 err = snd_ctl_add(codec->bus->card, kctl);
2431 if (err < 0) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002432 if (!codec->addr)
Takashi Iwai54d17402005-11-21 16:33:22 +01002433 return err;
2434 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002435 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01002436 return -ENOMEM;
2437 kctl->id.device = codec->addr;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002438 err = snd_ctl_add(codec->bus->card, kctl);
2439 if (err < 0)
Takashi Iwai54d17402005-11-21 16:33:22 +01002440 return err;
2441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 }
2443 return 0;
2444}
2445
Takashi Iwaicb53c622007-08-10 17:21:45 +02002446#ifdef CONFIG_SND_HDA_POWER_SAVE
2447static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2448 unsigned int power_state);
2449
2450static void hda_power_work(struct work_struct *work)
2451{
2452 struct hda_codec *codec =
2453 container_of(work, struct hda_codec, power_work.work);
2454
Maxim Levitsky2e492462007-09-03 15:26:57 +02002455 if (!codec->power_on || codec->power_count) {
2456 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002457 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02002458 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02002459
2460 hda_call_codec_suspend(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002461 if (codec->bus->ops.pm_notify)
2462 codec->bus->ops.pm_notify(codec);
2463}
2464
2465static void hda_keep_power_on(struct hda_codec *codec)
2466{
2467 codec->power_count++;
2468 codec->power_on = 1;
2469}
2470
2471void snd_hda_power_up(struct hda_codec *codec)
2472{
2473 codec->power_count++;
Takashi Iwaia221e282007-08-16 16:35:33 +02002474 if (codec->power_on || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02002475 return;
2476
2477 codec->power_on = 1;
2478 if (codec->bus->ops.pm_notify)
2479 codec->bus->ops.pm_notify(codec);
2480 hda_call_codec_resume(codec);
2481 cancel_delayed_work(&codec->power_work);
Takashi Iwaia221e282007-08-16 16:35:33 +02002482 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002483}
2484
2485void snd_hda_power_down(struct hda_codec *codec)
2486{
2487 --codec->power_count;
Takashi Iwaia221e282007-08-16 16:35:33 +02002488 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02002489 return;
Takashi Iwaia221e282007-08-16 16:35:33 +02002490 if (power_save) {
2491 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002492 schedule_delayed_work(&codec->power_work,
2493 msecs_to_jiffies(power_save * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02002494 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02002495}
2496
2497int snd_hda_check_amp_list_power(struct hda_codec *codec,
2498 struct hda_loopback_check *check,
2499 hda_nid_t nid)
2500{
2501 struct hda_amp_list *p;
2502 int ch, v;
2503
2504 if (!check->amplist)
2505 return 0;
2506 for (p = check->amplist; p->nid; p++) {
2507 if (p->nid == nid)
2508 break;
2509 }
2510 if (!p->nid)
2511 return 0; /* nothing changed */
2512
2513 for (p = check->amplist; p->nid; p++) {
2514 for (ch = 0; ch < 2; ch++) {
2515 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
2516 p->idx);
2517 if (!(v & HDA_AMP_MUTE) && v > 0) {
2518 if (!check->power_on) {
2519 check->power_on = 1;
2520 snd_hda_power_up(codec);
2521 }
2522 return 1;
2523 }
2524 }
2525 }
2526 if (check->power_on) {
2527 check->power_on = 0;
2528 snd_hda_power_down(codec);
2529 }
2530 return 0;
2531}
2532#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002534/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002535 * Channel mode helper
2536 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002537int snd_hda_ch_mode_info(struct hda_codec *codec,
2538 struct snd_ctl_elem_info *uinfo,
2539 const struct hda_channel_mode *chmode,
2540 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002541{
2542 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2543 uinfo->count = 1;
2544 uinfo->value.enumerated.items = num_chmodes;
2545 if (uinfo->value.enumerated.item >= num_chmodes)
2546 uinfo->value.enumerated.item = num_chmodes - 1;
2547 sprintf(uinfo->value.enumerated.name, "%dch",
2548 chmode[uinfo->value.enumerated.item].channels);
2549 return 0;
2550}
2551
Takashi Iwai0ba21762007-04-16 11:29:14 +02002552int snd_hda_ch_mode_get(struct hda_codec *codec,
2553 struct snd_ctl_elem_value *ucontrol,
2554 const struct hda_channel_mode *chmode,
2555 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002556 int max_channels)
2557{
2558 int i;
2559
2560 for (i = 0; i < num_chmodes; i++) {
2561 if (max_channels == chmode[i].channels) {
2562 ucontrol->value.enumerated.item[0] = i;
2563 break;
2564 }
2565 }
2566 return 0;
2567}
2568
Takashi Iwai0ba21762007-04-16 11:29:14 +02002569int snd_hda_ch_mode_put(struct hda_codec *codec,
2570 struct snd_ctl_elem_value *ucontrol,
2571 const struct hda_channel_mode *chmode,
2572 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002573 int *max_channelsp)
2574{
2575 unsigned int mode;
2576
2577 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01002578 if (mode >= num_chmodes)
2579 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002580 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002581 return 0;
2582 /* change the current channel setting */
2583 *max_channelsp = chmode[mode].channels;
2584 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002585 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002586 return 1;
2587}
2588
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589/*
2590 * input MUX helper
2591 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002592int snd_hda_input_mux_info(const struct hda_input_mux *imux,
2593 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594{
2595 unsigned int index;
2596
2597 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2598 uinfo->count = 1;
2599 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02002600 if (!imux->num_items)
2601 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 index = uinfo->value.enumerated.item;
2603 if (index >= imux->num_items)
2604 index = imux->num_items - 1;
2605 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
2606 return 0;
2607}
2608
Takashi Iwai0ba21762007-04-16 11:29:14 +02002609int snd_hda_input_mux_put(struct hda_codec *codec,
2610 const struct hda_input_mux *imux,
2611 struct snd_ctl_elem_value *ucontrol,
2612 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 unsigned int *cur_val)
2614{
2615 unsigned int idx;
2616
Takashi Iwai5513b0c2007-10-09 11:58:41 +02002617 if (!imux->num_items)
2618 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 idx = ucontrol->value.enumerated.item[0];
2620 if (idx >= imux->num_items)
2621 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002622 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002624 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
2625 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 *cur_val = idx;
2627 return 1;
2628}
2629
2630
2631/*
2632 * Multi-channel / digital-out PCM helper functions
2633 */
2634
Takashi Iwai6b97eb42007-04-05 14:51:48 +02002635/* setup SPDIF output stream */
2636static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
2637 unsigned int stream_tag, unsigned int format)
2638{
2639 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Takashi Iwai2f728532008-09-25 16:32:41 +02002640 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
2641 set_dig_out_convert(codec, nid,
2642 codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff,
2643 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02002644 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02002645 if (codec->slave_dig_outs) {
2646 hda_nid_t *d;
2647 for (d = codec->slave_dig_outs; *d; d++)
2648 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
2649 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04002650 }
Takashi Iwai2f728532008-09-25 16:32:41 +02002651 /* turn on again (if needed) */
2652 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
2653 set_dig_out_convert(codec, nid,
2654 codec->spdif_ctls & 0xff, -1);
2655}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04002656
Takashi Iwai2f728532008-09-25 16:32:41 +02002657static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
2658{
2659 snd_hda_codec_cleanup_stream(codec, nid);
2660 if (codec->slave_dig_outs) {
2661 hda_nid_t *d;
2662 for (d = codec->slave_dig_outs; *d; d++)
2663 snd_hda_codec_cleanup_stream(codec, *d);
2664 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02002665}
2666
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667/*
2668 * open the digital out in the exclusive mode
2669 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002670int snd_hda_multi_out_dig_open(struct hda_codec *codec,
2671 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672{
Ingo Molnar62932df2006-01-16 16:34:20 +01002673 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02002674 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
2675 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02002676 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01002678 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 return 0;
2680}
2681
Takashi Iwai6b97eb42007-04-05 14:51:48 +02002682int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
2683 struct hda_multi_out *mout,
2684 unsigned int stream_tag,
2685 unsigned int format,
2686 struct snd_pcm_substream *substream)
2687{
2688 mutex_lock(&codec->spdif_mutex);
2689 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
2690 mutex_unlock(&codec->spdif_mutex);
2691 return 0;
2692}
2693
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694/*
2695 * release the digital out
2696 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002697int snd_hda_multi_out_dig_close(struct hda_codec *codec,
2698 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699{
Ingo Molnar62932df2006-01-16 16:34:20 +01002700 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01002702 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 return 0;
2704}
2705
2706/*
2707 * set up more restrictions for analog out
2708 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002709int snd_hda_multi_out_analog_open(struct hda_codec *codec,
2710 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01002711 struct snd_pcm_substream *substream,
2712 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713{
Takashi Iwai9a081602008-02-12 18:37:26 +01002714 struct snd_pcm_runtime *runtime = substream->runtime;
2715 runtime->hw.channels_max = mout->max_channels;
2716 if (mout->dig_out_nid) {
2717 if (!mout->analog_rates) {
2718 mout->analog_rates = hinfo->rates;
2719 mout->analog_formats = hinfo->formats;
2720 mout->analog_maxbps = hinfo->maxbps;
2721 } else {
2722 runtime->hw.rates = mout->analog_rates;
2723 runtime->hw.formats = mout->analog_formats;
2724 hinfo->maxbps = mout->analog_maxbps;
2725 }
2726 if (!mout->spdif_rates) {
2727 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
2728 &mout->spdif_rates,
2729 &mout->spdif_formats,
2730 &mout->spdif_maxbps);
2731 }
2732 mutex_lock(&codec->spdif_mutex);
2733 if (mout->share_spdif) {
2734 runtime->hw.rates &= mout->spdif_rates;
2735 runtime->hw.formats &= mout->spdif_formats;
2736 if (mout->spdif_maxbps < hinfo->maxbps)
2737 hinfo->maxbps = mout->spdif_maxbps;
2738 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02002739 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01002740 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 return snd_pcm_hw_constraint_step(substream->runtime, 0,
2742 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
2743}
2744
2745/*
2746 * set up the i/o for analog out
2747 * when the digital out is available, copy the front out to digital out, too.
2748 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002749int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
2750 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 unsigned int stream_tag,
2752 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002753 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754{
2755 hda_nid_t *nids = mout->dac_nids;
2756 int chs = substream->runtime->channels;
2757 int i;
2758
Ingo Molnar62932df2006-01-16 16:34:20 +01002759 mutex_lock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01002760 if (mout->dig_out_nid && mout->share_spdif &&
2761 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02002763 snd_hda_is_supported_format(codec, mout->dig_out_nid,
2764 format) &&
2765 !(codec->spdif_status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02002767 setup_dig_out_stream(codec, mout->dig_out_nid,
2768 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 } else {
2770 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02002771 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 }
2773 }
Ingo Molnar62932df2006-01-16 16:34:20 +01002774 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775
2776 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002777 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
2778 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02002779 if (!mout->no_share_stream &&
2780 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002782 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
2783 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01002784 /* extra outputs copied from front */
2785 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02002786 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01002787 snd_hda_codec_setup_stream(codec,
2788 mout->extra_out_nid[i],
2789 stream_tag, 0, format);
2790
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 /* surrounds */
2792 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02002793 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002794 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
2795 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02002796 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002797 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
2798 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 }
2800 return 0;
2801}
2802
2803/*
2804 * clean up the setting for analog out
2805 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002806int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
2807 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808{
2809 hda_nid_t *nids = mout->dac_nids;
2810 int i;
2811
2812 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01002813 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01002815 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwai82bc9552006-03-21 11:24:42 +01002816 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
2817 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01002818 snd_hda_codec_cleanup_stream(codec,
2819 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01002820 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02002822 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 mout->dig_out_used = 0;
2824 }
Ingo Molnar62932df2006-01-16 16:34:20 +01002825 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 return 0;
2827}
2828
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002829/*
Wu Fengguang6b345002008-10-07 14:21:41 +08002830 * Helper for automatic pin configuration
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002831 */
Kailang Yangdf694da2005-12-05 19:42:22 +01002832
Takashi Iwai12f288b2007-08-02 15:51:59 +02002833static int is_in_nid_list(hda_nid_t nid, hda_nid_t *list)
Kailang Yangdf694da2005-12-05 19:42:22 +01002834{
2835 for (; *list; list++)
2836 if (*list == nid)
2837 return 1;
2838 return 0;
2839}
2840
Steve Longerbeam81937d32007-05-08 15:33:03 +02002841
2842/*
2843 * Sort an associated group of pins according to their sequence numbers.
2844 */
2845static void sort_pins_by_sequence(hda_nid_t * pins, short * sequences,
2846 int num_pins)
2847{
2848 int i, j;
2849 short seq;
2850 hda_nid_t nid;
2851
2852 for (i = 0; i < num_pins; i++) {
2853 for (j = i + 1; j < num_pins; j++) {
2854 if (sequences[i] > sequences[j]) {
2855 seq = sequences[i];
2856 sequences[i] = sequences[j];
2857 sequences[j] = seq;
2858 nid = pins[i];
2859 pins[i] = pins[j];
2860 pins[j] = nid;
2861 }
2862 }
2863 }
2864}
2865
2866
Takashi Iwai82bc9552006-03-21 11:24:42 +01002867/*
2868 * Parse all pin widgets and store the useful pin nids to cfg
2869 *
2870 * The number of line-outs or any primary output is stored in line_outs,
2871 * and the corresponding output pins are assigned to line_out_pins[],
2872 * in the order of front, rear, CLFE, side, ...
2873 *
2874 * If more extra outputs (speaker and headphone) are found, the pins are
Takashi Iwaieb06ed82006-09-20 17:10:27 +02002875 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
Takashi Iwai82bc9552006-03-21 11:24:42 +01002876 * is detected, one of speaker of HP pins is assigned as the primary
2877 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
2878 * if any analog output exists.
2879 *
2880 * The analog input pins are assigned to input_pins array.
2881 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
2882 * respectively.
2883 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002884int snd_hda_parse_pin_def_config(struct hda_codec *codec,
2885 struct auto_pin_cfg *cfg,
2886 hda_nid_t *ignore_nids)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002887{
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01002888 hda_nid_t nid, end_nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02002889 short seq, assoc_line_out, assoc_speaker;
2890 short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
2891 short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
Takashi Iwaif889fa92007-10-31 15:49:32 +01002892 short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002893
2894 memset(cfg, 0, sizeof(*cfg));
2895
Steve Longerbeam81937d32007-05-08 15:33:03 +02002896 memset(sequences_line_out, 0, sizeof(sequences_line_out));
2897 memset(sequences_speaker, 0, sizeof(sequences_speaker));
Takashi Iwaif889fa92007-10-31 15:49:32 +01002898 memset(sequences_hp, 0, sizeof(sequences_hp));
Steve Longerbeam81937d32007-05-08 15:33:03 +02002899 assoc_line_out = assoc_speaker = 0;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002900
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01002901 end_nid = codec->start_nid + codec->num_nodes;
2902 for (nid = codec->start_nid; nid < end_nid; nid++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01002903 unsigned int wid_caps = get_wcaps(codec, nid);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002904 unsigned int wid_type =
2905 (wid_caps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002906 unsigned int def_conf;
2907 short assoc, loc;
2908
2909 /* read all default configuration for pin complex */
2910 if (wid_type != AC_WID_PIN)
2911 continue;
Kailang Yangdf694da2005-12-05 19:42:22 +01002912 /* ignore the given nids (e.g. pc-beep returns error) */
2913 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
2914 continue;
2915
Takashi Iwai0ba21762007-04-16 11:29:14 +02002916 def_conf = snd_hda_codec_read(codec, nid, 0,
2917 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002918 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
2919 continue;
2920 loc = get_defcfg_location(def_conf);
2921 switch (get_defcfg_device(def_conf)) {
2922 case AC_JACK_LINE_OUT:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002923 seq = get_defcfg_sequence(def_conf);
2924 assoc = get_defcfg_association(def_conf);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01002925
2926 if (!(wid_caps & AC_WCAP_STEREO))
2927 if (!cfg->mono_out_pin)
2928 cfg->mono_out_pin = nid;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002929 if (!assoc)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002930 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002931 if (!assoc_line_out)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002932 assoc_line_out = assoc;
2933 else if (assoc_line_out != assoc)
2934 continue;
2935 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
2936 continue;
2937 cfg->line_out_pins[cfg->line_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02002938 sequences_line_out[cfg->line_outs] = seq;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002939 cfg->line_outs++;
2940 break;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01002941 case AC_JACK_SPEAKER:
Steve Longerbeam81937d32007-05-08 15:33:03 +02002942 seq = get_defcfg_sequence(def_conf);
2943 assoc = get_defcfg_association(def_conf);
2944 if (! assoc)
2945 continue;
2946 if (! assoc_speaker)
2947 assoc_speaker = assoc;
2948 else if (assoc_speaker != assoc)
2949 continue;
Takashi Iwai82bc9552006-03-21 11:24:42 +01002950 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
2951 continue;
2952 cfg->speaker_pins[cfg->speaker_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02002953 sequences_speaker[cfg->speaker_outs] = seq;
Takashi Iwai82bc9552006-03-21 11:24:42 +01002954 cfg->speaker_outs++;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01002955 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002956 case AC_JACK_HP_OUT:
Takashi Iwaif889fa92007-10-31 15:49:32 +01002957 seq = get_defcfg_sequence(def_conf);
2958 assoc = get_defcfg_association(def_conf);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02002959 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
2960 continue;
2961 cfg->hp_pins[cfg->hp_outs] = nid;
Takashi Iwaif889fa92007-10-31 15:49:32 +01002962 sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
Takashi Iwaieb06ed82006-09-20 17:10:27 +02002963 cfg->hp_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002964 break;
Takashi Iwai314634b2006-09-21 11:56:18 +02002965 case AC_JACK_MIC_IN: {
2966 int preferred, alt;
2967 if (loc == AC_JACK_LOC_FRONT) {
2968 preferred = AUTO_PIN_FRONT_MIC;
2969 alt = AUTO_PIN_MIC;
2970 } else {
2971 preferred = AUTO_PIN_MIC;
2972 alt = AUTO_PIN_FRONT_MIC;
2973 }
2974 if (!cfg->input_pins[preferred])
2975 cfg->input_pins[preferred] = nid;
2976 else if (!cfg->input_pins[alt])
2977 cfg->input_pins[alt] = nid;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002978 break;
Takashi Iwai314634b2006-09-21 11:56:18 +02002979 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002980 case AC_JACK_LINE_IN:
2981 if (loc == AC_JACK_LOC_FRONT)
2982 cfg->input_pins[AUTO_PIN_FRONT_LINE] = nid;
2983 else
2984 cfg->input_pins[AUTO_PIN_LINE] = nid;
2985 break;
2986 case AC_JACK_CD:
2987 cfg->input_pins[AUTO_PIN_CD] = nid;
2988 break;
2989 case AC_JACK_AUX:
2990 cfg->input_pins[AUTO_PIN_AUX] = nid;
2991 break;
2992 case AC_JACK_SPDIF_OUT:
2993 cfg->dig_out_pin = nid;
2994 break;
2995 case AC_JACK_SPDIF_IN:
2996 cfg->dig_in_pin = nid;
2997 break;
2998 }
2999 }
3000
Takashi Iwai5832fcf2008-02-12 18:30:12 +01003001 /* FIX-UP:
3002 * If no line-out is defined but multiple HPs are found,
3003 * some of them might be the real line-outs.
3004 */
3005 if (!cfg->line_outs && cfg->hp_outs > 1) {
3006 int i = 0;
3007 while (i < cfg->hp_outs) {
3008 /* The real HPs should have the sequence 0x0f */
3009 if ((sequences_hp[i] & 0x0f) == 0x0f) {
3010 i++;
3011 continue;
3012 }
3013 /* Move it to the line-out table */
3014 cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
3015 sequences_line_out[cfg->line_outs] = sequences_hp[i];
3016 cfg->line_outs++;
3017 cfg->hp_outs--;
3018 memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
3019 sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
3020 memmove(sequences_hp + i - 1, sequences_hp + i,
3021 sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
3022 }
3023 }
3024
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003025 /* sort by sequence */
Steve Longerbeam81937d32007-05-08 15:33:03 +02003026 sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
3027 cfg->line_outs);
3028 sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
3029 cfg->speaker_outs);
Takashi Iwaif889fa92007-10-31 15:49:32 +01003030 sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
3031 cfg->hp_outs);
Steve Longerbeam81937d32007-05-08 15:33:03 +02003032
Takashi Iwaif889fa92007-10-31 15:49:32 +01003033 /* if we have only one mic, make it AUTO_PIN_MIC */
3034 if (!cfg->input_pins[AUTO_PIN_MIC] &&
3035 cfg->input_pins[AUTO_PIN_FRONT_MIC]) {
3036 cfg->input_pins[AUTO_PIN_MIC] =
3037 cfg->input_pins[AUTO_PIN_FRONT_MIC];
3038 cfg->input_pins[AUTO_PIN_FRONT_MIC] = 0;
3039 }
3040 /* ditto for line-in */
3041 if (!cfg->input_pins[AUTO_PIN_LINE] &&
3042 cfg->input_pins[AUTO_PIN_FRONT_LINE]) {
3043 cfg->input_pins[AUTO_PIN_LINE] =
3044 cfg->input_pins[AUTO_PIN_FRONT_LINE];
3045 cfg->input_pins[AUTO_PIN_FRONT_LINE] = 0;
3046 }
3047
Steve Longerbeam81937d32007-05-08 15:33:03 +02003048 /*
3049 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
3050 * as a primary output
3051 */
3052 if (!cfg->line_outs) {
3053 if (cfg->speaker_outs) {
3054 cfg->line_outs = cfg->speaker_outs;
3055 memcpy(cfg->line_out_pins, cfg->speaker_pins,
3056 sizeof(cfg->speaker_pins));
3057 cfg->speaker_outs = 0;
3058 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
3059 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
3060 } else if (cfg->hp_outs) {
3061 cfg->line_outs = cfg->hp_outs;
3062 memcpy(cfg->line_out_pins, cfg->hp_pins,
3063 sizeof(cfg->hp_pins));
3064 cfg->hp_outs = 0;
3065 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
3066 cfg->line_out_type = AUTO_PIN_HP_OUT;
3067 }
3068 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003069
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02003070 /* Reorder the surround channels
3071 * ALSA sequence is front/surr/clfe/side
3072 * HDA sequence is:
3073 * 4-ch: front/surr => OK as it is
3074 * 6-ch: front/clfe/surr
Takashi Iwai9422db42007-04-20 16:11:43 +02003075 * 8-ch: front/clfe/rear/side|fc
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02003076 */
3077 switch (cfg->line_outs) {
3078 case 3:
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02003079 case 4:
3080 nid = cfg->line_out_pins[1];
Takashi Iwai9422db42007-04-20 16:11:43 +02003081 cfg->line_out_pins[1] = cfg->line_out_pins[2];
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02003082 cfg->line_out_pins[2] = nid;
3083 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003084 }
3085
Takashi Iwai82bc9552006-03-21 11:24:42 +01003086 /*
3087 * debug prints of the parsed results
3088 */
3089 snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3090 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
3091 cfg->line_out_pins[2], cfg->line_out_pins[3],
3092 cfg->line_out_pins[4]);
3093 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3094 cfg->speaker_outs, cfg->speaker_pins[0],
3095 cfg->speaker_pins[1], cfg->speaker_pins[2],
3096 cfg->speaker_pins[3], cfg->speaker_pins[4]);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02003097 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3098 cfg->hp_outs, cfg->hp_pins[0],
3099 cfg->hp_pins[1], cfg->hp_pins[2],
3100 cfg->hp_pins[3], cfg->hp_pins[4]);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01003101 snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
Takashi Iwai82bc9552006-03-21 11:24:42 +01003102 snd_printd(" inputs: mic=0x%x, fmic=0x%x, line=0x%x, fline=0x%x,"
3103 " cd=0x%x, aux=0x%x\n",
3104 cfg->input_pins[AUTO_PIN_MIC],
3105 cfg->input_pins[AUTO_PIN_FRONT_MIC],
3106 cfg->input_pins[AUTO_PIN_LINE],
3107 cfg->input_pins[AUTO_PIN_FRONT_LINE],
3108 cfg->input_pins[AUTO_PIN_CD],
3109 cfg->input_pins[AUTO_PIN_AUX]);
3110
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003111 return 0;
3112}
3113
Takashi Iwai4a471b72005-12-07 13:56:29 +01003114/* labels for input pins */
3115const char *auto_pin_cfg_labels[AUTO_PIN_LAST] = {
3116 "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux"
3117};
3118
3119
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120#ifdef CONFIG_PM
3121/*
3122 * power management
3123 */
3124
3125/**
3126 * snd_hda_suspend - suspend the codecs
3127 * @bus: the HDA bus
3128 * @state: suspsend state
3129 *
3130 * Returns 0 if successful.
3131 */
3132int snd_hda_suspend(struct hda_bus *bus, pm_message_t state)
3133{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003134 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135
Takashi Iwai0ba21762007-04-16 11:29:14 +02003136 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai0b7a2e92007-08-14 15:18:26 +02003137#ifdef CONFIG_SND_HDA_POWER_SAVE
3138 if (!codec->power_on)
3139 continue;
3140#endif
Takashi Iwaicb53c622007-08-10 17:21:45 +02003141 hda_call_codec_suspend(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 }
3143 return 0;
3144}
3145
3146/**
3147 * snd_hda_resume - resume the codecs
3148 * @bus: the HDA bus
3149 * @state: resume state
3150 *
3151 * Returns 0 if successful.
Takashi Iwaicb53c622007-08-10 17:21:45 +02003152 *
3153 * This fucntion is defined only when POWER_SAVE isn't set.
3154 * In the power-save mode, the codec is resumed dynamically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 */
3156int snd_hda_resume(struct hda_bus *bus)
3157{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003158 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159
Takashi Iwai0ba21762007-04-16 11:29:14 +02003160 list_for_each_entry(codec, &bus->codec_list, list) {
Maxim Levitskyd804ad92007-09-03 15:28:04 +02003161 if (snd_hda_codec_needs_resume(codec))
3162 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 return 0;
3165}
Maxim Levitskyd804ad92007-09-03 15:28:04 +02003166#ifdef CONFIG_SND_HDA_POWER_SAVE
3167int snd_hda_codecs_inuse(struct hda_bus *bus)
3168{
3169 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170
Maxim Levitskyd804ad92007-09-03 15:28:04 +02003171 list_for_each_entry(codec, &bus->codec_list, list) {
3172 if (snd_hda_codec_needs_resume(codec))
3173 return 1;
3174 }
3175 return 0;
3176}
3177#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178#endif
Takashi Iwaib2e18592008-07-30 15:01:44 +02003179
3180/*
3181 * generic arrays
3182 */
3183
3184/* get a new element from the given array
3185 * if it exceeds the pre-allocated array size, re-allocate the array
3186 */
3187void *snd_array_new(struct snd_array *array)
3188{
3189 if (array->used >= array->alloced) {
3190 int num = array->alloced + array->alloc_align;
3191 void *nlist = kcalloc(num + 1, array->elem_size, GFP_KERNEL);
3192 if (!nlist)
3193 return NULL;
3194 if (array->list) {
3195 memcpy(nlist, array->list,
3196 array->elem_size * array->alloced);
3197 kfree(array->list);
3198 }
3199 array->list = nlist;
3200 array->alloced = num;
3201 }
3202 return array->list + (array->used++ * array->elem_size);
3203}
3204
3205/* free the given array elements */
3206void snd_array_free(struct snd_array *array)
3207{
3208 kfree(array->list);
3209 array->used = 0;
3210 array->alloced = 0;
3211 array->list = NULL;
3212}