blob: 53e36495fae52815ec4b29e44058d7b26f87cb5d [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) \
Takashi Iwaif44ac832008-07-30 15:01:45 +0200449 (codec->modelname && !strcmp(codec->modelname, "generic"))
Takashi Iwai82467612007-07-27 19:15:54 +0200450#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/*
Takashi Iwaif44ac832008-07-30 15:01:45 +0200484 * get_codec_name - store the codec name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 */
Takashi Iwaif44ac832008-07-30 15:01:45 +0200486static int get_codec_name(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
488 const struct hda_vendor_id *c;
489 const char *vendor = NULL;
490 u16 vendor_id = codec->vendor_id >> 16;
Takashi Iwaif44ac832008-07-30 15:01:45 +0200491 char tmp[16], name[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493 for (c = hda_vendor_ids; c->id; c++) {
494 if (c->id == vendor_id) {
495 vendor = c->name;
496 break;
497 }
498 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200499 if (!vendor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 sprintf(tmp, "Generic %04x", vendor_id);
501 vendor = tmp;
502 }
503 if (codec->preset && codec->preset->name)
Takashi Iwaif44ac832008-07-30 15:01:45 +0200504 snprintf(name, sizeof(name), "%s %s", vendor,
505 codec->preset->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 else
Takashi Iwaif44ac832008-07-30 15:01:45 +0200507 snprintf(name, sizeof(name), "%s ID %x", vendor,
Takashi Iwai0ba21762007-04-16 11:29:14 +0200508 codec->vendor_id & 0xffff);
Takashi Iwaif44ac832008-07-30 15:01:45 +0200509 codec->name = kstrdup(name, GFP_KERNEL);
510 if (!codec->name)
511 return -ENOMEM;
512 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513}
514
515/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200516 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 */
Takashi Iwai756e2b02007-04-16 11:27:07 +0200518static void __devinit setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
520 int i, total_nodes;
521 hda_nid_t nid;
522
523 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
524 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +0200525 unsigned int func;
526 func = snd_hda_param_read(codec, nid, AC_PAR_FUNCTION_TYPE);
527 switch (func & 0xff) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200528 case AC_GRP_AUDIO_FUNCTION:
529 codec->afg = nid;
530 break;
531 case AC_GRP_MODEM_FUNCTION:
532 codec->mfg = nid;
533 break;
534 default:
535 break;
536 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538}
539
540/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100541 * read widget caps for each widget and store in cache
542 */
543static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
544{
545 int i;
546 hda_nid_t nid;
547
548 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
549 &codec->start_nid);
550 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200551 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +0100552 return -ENOMEM;
553 nid = codec->start_nid;
554 for (i = 0; i < codec->num_nodes; i++, nid++)
555 codec->wcaps[i] = snd_hda_param_read(codec, nid,
556 AC_PAR_AUDIO_WIDGET_CAP);
557 return 0;
558}
559
560
Takashi Iwai01751f52007-08-10 16:59:39 +0200561static void init_hda_cache(struct hda_cache_rec *cache,
562 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +0200563static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +0200564
Takashi Iwai54d17402005-11-21 16:33:22 +0100565/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 * codec destructor
567 */
568static void snd_hda_codec_free(struct hda_codec *codec)
569{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200570 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 return;
Takashi Iwaicb53c622007-08-10 17:21:45 +0200572#ifdef CONFIG_SND_HDA_POWER_SAVE
573 cancel_delayed_work(&codec->power_work);
Takashi Iwai2525fdc2007-08-15 22:18:22 +0200574 flush_scheduled_work();
Takashi Iwaicb53c622007-08-10 17:21:45 +0200575#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 list_del(&codec->list);
577 codec->bus->caddr_tbl[codec->addr] = NULL;
578 if (codec->patch_ops.free)
579 codec->patch_ops.free(codec);
Takashi Iwai01751f52007-08-10 16:59:39 +0200580 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +0200581 free_hda_cache(&codec->cmd_cache);
Takashi Iwaif44ac832008-07-30 15:01:45 +0200582 kfree(codec->name);
583 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +0100584 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 kfree(codec);
586}
587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588/**
589 * snd_hda_codec_new - create a HDA codec
590 * @bus: the bus to assign
591 * @codec_addr: the codec address
592 * @codecp: the pointer to store the generated codec
593 *
594 * Returns 0 if successful, or a negative error code.
595 */
Takashi Iwai756e2b02007-04-16 11:27:07 +0200596int __devinit snd_hda_codec_new(struct hda_bus *bus, unsigned int codec_addr,
597 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
599 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +0200600 char component[31];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 int err;
602
Takashi Iwaida3cec32008-08-08 17:12:14 +0200603 if (snd_BUG_ON(!bus))
604 return -EINVAL;
605 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
606 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
608 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +0200609 snd_printk(KERN_ERR "hda_codec: "
610 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 return -EBUSY;
612 }
613
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200614 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 if (codec == NULL) {
616 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
617 return -ENOMEM;
618 }
619
620 codec->bus = bus;
621 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +0100622 mutex_init(&codec->spdif_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +0200623 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +0200624 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Takashi Iwaicb53c622007-08-10 17:21:45 +0200626#ifdef CONFIG_SND_HDA_POWER_SAVE
627 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
628 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
629 * the caller has to power down appropriatley after initialization
630 * phase.
631 */
632 hda_keep_power_on(codec);
633#endif
634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 list_add_tail(&codec->list, &bus->codec_list);
636 bus->caddr_tbl[codec_addr] = codec;
637
Takashi Iwai0ba21762007-04-16 11:29:14 +0200638 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
639 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +0100640 if (codec->vendor_id == -1)
641 /* read again, hopefully the access method was corrected
642 * in the last read...
643 */
644 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
645 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200646 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
647 AC_PAR_SUBSYSTEM_ID);
648 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
649 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200651 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200652 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200653 snd_printdd("hda_codec: no AFG or MFG node found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 snd_hda_codec_free(codec);
655 return -ENODEV;
656 }
657
Takashi Iwai54d17402005-11-21 16:33:22 +0100658 if (read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg) < 0) {
659 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
660 snd_hda_codec_free(codec);
661 return -ENOMEM;
662 }
663
Takashi Iwai0ba21762007-04-16 11:29:14 +0200664 if (!codec->subsystem_id) {
Takashi Iwai86284e42005-10-11 15:05:54 +0200665 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200666 codec->subsystem_id =
667 snd_hda_codec_read(codec, nid, 0,
668 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +0200669 }
Takashi Iwaif44ac832008-07-30 15:01:45 +0200670 if (bus->modelname)
671 codec->modelname = kstrdup(bus->modelname, GFP_KERNEL);
Takashi Iwai86284e42005-10-11 15:05:54 +0200672
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100673 codec->preset = find_codec_preset(codec);
Takashi Iwaif44ac832008-07-30 15:01:45 +0200674 if (!codec->name) {
675 err = get_codec_name(codec);
676 if (err < 0)
677 return err;
678 }
Takashi Iwai43ea1d42007-04-26 19:12:08 +0200679 /* audio codec should override the mixer name */
Takashi Iwaif44ac832008-07-30 15:01:45 +0200680 if (codec->afg || !*codec->bus->card->mixername)
681 strlcpy(codec->bus->card->mixername, codec->name,
682 sizeof(codec->bus->card->mixername));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Takashi Iwai82467612007-07-27 19:15:54 +0200684 if (is_generic_config(codec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai82467612007-07-27 19:15:54 +0200686 goto patched;
687 }
Takashi Iwai82467612007-07-27 19:15:54 +0200688 if (codec->preset && codec->preset->patch) {
689 err = codec->preset->patch(codec);
690 goto patched;
691 }
692
693 /* call the default parser */
Takashi Iwai82467612007-07-27 19:15:54 +0200694 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai35a1e0c2007-10-19 08:13:40 +0200695 if (err < 0)
696 printk(KERN_ERR "hda-codec: No codec parser is available\n");
Takashi Iwai82467612007-07-27 19:15:54 +0200697
698 patched:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 if (err < 0) {
700 snd_hda_codec_free(codec);
701 return err;
702 }
703
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100704 if (codec->patch_ops.unsol_event)
705 init_unsol_queue(bus);
706
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 snd_hda_codec_proc_new(codec);
Takashi Iwai28073142007-07-27 18:58:06 +0200708#ifdef CONFIG_SND_HDA_HWDEP
709 snd_hda_create_hwdep(codec);
710#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Jaroslav Kyselaba443682008-08-13 20:55:32 +0200712 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id, codec->subsystem_id, codec->revision_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 snd_component_add(codec->bus->card, component);
714
715 if (codecp)
716 *codecp = codec;
717 return 0;
718}
719
720/**
721 * snd_hda_codec_setup_stream - set up the codec for streaming
722 * @codec: the CODEC to set up
723 * @nid: the NID to set up
724 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
725 * @channel_id: channel id to pass, zero based.
726 * @format: stream format.
727 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200728void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
729 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 int channel_id, int format)
731{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200732 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +0200733 return;
734
Takashi Iwai0ba21762007-04-16 11:29:14 +0200735 snd_printdd("hda_codec_setup_stream: "
736 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 nid, stream_tag, channel_id, format);
738 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID,
739 (stream_tag << 4) | channel_id);
740 msleep(1);
741 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, format);
742}
743
Takashi Iwai888afa12008-03-18 09:57:50 +0100744void snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid)
745{
746 if (!nid)
747 return;
748
749 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
750 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
751#if 0 /* keep the format */
752 msleep(1);
753 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0);
754#endif
755}
756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757/*
758 * amp access functions
759 */
760
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200761/* FIXME: more better hash key? */
762#define HDA_HASH_KEY(nid,dir,idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200764#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
766/* initialize the hash table */
Takashi Iwai01751f52007-08-10 16:59:39 +0200767static void __devinit init_hda_cache(struct hda_cache_rec *cache,
768 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769{
Takashi Iwai01751f52007-08-10 16:59:39 +0200770 memset(cache, 0, sizeof(*cache));
771 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +0200772 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +0200773}
774
Takashi Iwai1fcaee62007-08-23 00:01:09 +0200775static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +0200776{
Takashi Iwai603c4012008-07-30 15:01:44 +0200777 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778}
779
780/* query the hash. allocate an entry if not found. */
Takashi Iwai01751f52007-08-10 16:59:39 +0200781static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
782 u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783{
Takashi Iwai01751f52007-08-10 16:59:39 +0200784 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
785 u16 cur = cache->hash[idx];
Takashi Iwai603c4012008-07-30 15:01:44 +0200786 struct hda_cache_head *info_head = cache->buf.list;
Takashi Iwai01751f52007-08-10 16:59:39 +0200787 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
789 while (cur != 0xffff) {
Takashi Iwai603c4012008-07-30 15:01:44 +0200790 info = &info_head[cur];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 if (info->key == key)
792 return info;
793 cur = info->next;
794 }
795
796 /* add a new hash entry */
Takashi Iwai603c4012008-07-30 15:01:44 +0200797 info = snd_array_new(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 info->key = key;
Takashi Iwai01751f52007-08-10 16:59:39 +0200799 info->val = 0;
800 info->next = cache->hash[idx];
801 cache->hash[idx] = cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
803 return info;
804}
805
Takashi Iwai01751f52007-08-10 16:59:39 +0200806/* query and allocate an amp hash entry */
807static inline struct hda_amp_info *
808get_alloc_amp_hash(struct hda_codec *codec, u32 key)
809{
810 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
811}
812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813/*
814 * query AMP capabilities for the given widget and direction
815 */
Matthew Ranostay09a99952008-01-24 11:49:21 +0100816u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200818 struct hda_amp_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Takashi Iwai0ba21762007-04-16 11:29:14 +0200820 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
821 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 return 0;
Takashi Iwai01751f52007-08-10 16:59:39 +0200823 if (!(info->head.val & INFO_AMP_CAPS)) {
Takashi Iwai0ba21762007-04-16 11:29:14 +0200824 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 nid = codec->afg;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200826 info->amp_caps = snd_hda_param_read(codec, nid,
827 direction == HDA_OUTPUT ?
828 AC_PAR_AMP_OUT_CAP :
829 AC_PAR_AMP_IN_CAP);
Takashi Iwaib75e53f2007-05-10 16:56:09 +0200830 if (info->amp_caps)
Takashi Iwai01751f52007-08-10 16:59:39 +0200831 info->head.val |= INFO_AMP_CAPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 }
833 return info->amp_caps;
834}
835
Takashi Iwai897cc182007-05-29 19:01:37 +0200836int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
837 unsigned int caps)
838{
839 struct hda_amp_info *info;
840
841 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
842 if (!info)
843 return -EINVAL;
844 info->amp_caps = caps;
Takashi Iwai01751f52007-08-10 16:59:39 +0200845 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai897cc182007-05-29 19:01:37 +0200846 return 0;
847}
848
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849/*
850 * read the current volume to info
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200851 * if the cache exists, read the cache value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200853static unsigned int get_vol_mute(struct hda_codec *codec,
854 struct hda_amp_info *info, hda_nid_t nid,
855 int ch, int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856{
857 u32 val, parm;
858
Takashi Iwai01751f52007-08-10 16:59:39 +0200859 if (info->head.val & INFO_AMP_VOL(ch))
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200860 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
862 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
863 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
864 parm |= index;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200865 val = snd_hda_codec_read(codec, nid, 0,
866 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 info->vol[ch] = val & 0xff;
Takashi Iwai01751f52007-08-10 16:59:39 +0200868 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200869 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870}
871
872/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200873 * write the current volume in info to the h/w and update the cache
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200875static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
Takashi Iwai0ba21762007-04-16 11:29:14 +0200876 hda_nid_t nid, int ch, int direction, int index,
877 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878{
879 u32 parm;
880
881 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
882 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
883 parm |= index << AC_AMP_SET_INDEX_SHIFT;
884 parm |= val;
885 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200886 info->vol[ch] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887}
888
889/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200890 * read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 */
Takashi Iwai834be882006-03-01 14:16:17 +0100892int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
893 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200895 struct hda_amp_info *info;
896 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
897 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200899 return get_vol_mute(codec, info, nid, ch, direction, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900}
901
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200902/*
903 * update the AMP value, mask = bit mask to set, val = the value
904 */
Takashi Iwai834be882006-03-01 14:16:17 +0100905int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
906 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200908 struct hda_amp_info *info;
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200909
Takashi Iwai0ba21762007-04-16 11:29:14 +0200910 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
911 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200913 val &= mask;
914 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
Takashi Iwai82beb8f2007-08-10 17:09:26 +0200915 if (info->vol[ch] == val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200917 put_vol_mute(codec, info, nid, ch, direction, idx, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 return 1;
919}
920
Takashi Iwai47fd8302007-08-10 17:11:07 +0200921/*
922 * update the AMP stereo with the same mask and value
923 */
924int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
925 int direction, int idx, int mask, int val)
926{
927 int ch, ret = 0;
928 for (ch = 0; ch < 2; ch++)
929 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
930 idx, mask, val);
931 return ret;
932}
933
Takashi Iwaicb53c622007-08-10 17:21:45 +0200934#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwaib3ac5632007-08-10 17:03:40 +0200935/* resume the all amp commands from the cache */
936void snd_hda_codec_resume_amp(struct hda_codec *codec)
937{
Takashi Iwai603c4012008-07-30 15:01:44 +0200938 struct hda_amp_info *buffer = codec->amp_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +0200939 int i;
940
Takashi Iwai603c4012008-07-30 15:01:44 +0200941 for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +0200942 u32 key = buffer->head.key;
943 hda_nid_t nid;
944 unsigned int idx, dir, ch;
945 if (!key)
946 continue;
947 nid = key & 0xff;
948 idx = (key >> 16) & 0xff;
949 dir = (key >> 24) & 0xff;
950 for (ch = 0; ch < 2; ch++) {
951 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
952 continue;
953 put_vol_mute(codec, buffer, nid, ch, dir, idx,
954 buffer->vol[ch]);
955 }
956 }
957}
Takashi Iwaicb53c622007-08-10 17:21:45 +0200958#endif /* SND_HDA_NEEDS_RESUME */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960/* volume */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200961int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
962 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963{
964 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
965 u16 nid = get_amp_nid(kcontrol);
966 u8 chs = get_amp_channels(kcontrol);
967 int dir = get_amp_direction(kcontrol);
968 u32 caps;
969
970 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200971 /* num steps */
972 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
973 if (!caps) {
974 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +0100975 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
976 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 return -EINVAL;
978 }
979 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
980 uinfo->count = chs == 3 ? 2 : 1;
981 uinfo->value.integer.min = 0;
982 uinfo->value.integer.max = caps;
983 return 0;
984}
985
Takashi Iwai0ba21762007-04-16 11:29:14 +0200986int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
987 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988{
989 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
990 hda_nid_t nid = get_amp_nid(kcontrol);
991 int chs = get_amp_channels(kcontrol);
992 int dir = get_amp_direction(kcontrol);
993 int idx = get_amp_index(kcontrol);
994 long *valp = ucontrol->value.integer.value;
995
996 if (chs & 1)
Takashi Iwai47fd8302007-08-10 17:11:07 +0200997 *valp++ = snd_hda_codec_amp_read(codec, nid, 0, dir, idx)
998 & HDA_AMP_VOLMASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 if (chs & 2)
Takashi Iwai47fd8302007-08-10 17:11:07 +02001000 *valp = snd_hda_codec_amp_read(codec, nid, 1, dir, idx)
1001 & HDA_AMP_VOLMASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 return 0;
1003}
1004
Takashi Iwai0ba21762007-04-16 11:29:14 +02001005int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1006 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007{
1008 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1009 hda_nid_t nid = get_amp_nid(kcontrol);
1010 int chs = get_amp_channels(kcontrol);
1011 int dir = get_amp_direction(kcontrol);
1012 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 long *valp = ucontrol->value.integer.value;
1014 int change = 0;
1015
Takashi Iwaicb53c622007-08-10 17:21:45 +02001016 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001017 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001018 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
1019 0x7f, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001020 valp++;
1021 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001022 if (chs & 2)
1023 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001024 0x7f, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001025 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 return change;
1027}
1028
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001029int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1030 unsigned int size, unsigned int __user *_tlv)
1031{
1032 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1033 hda_nid_t nid = get_amp_nid(kcontrol);
1034 int dir = get_amp_direction(kcontrol);
1035 u32 caps, val1, val2;
1036
1037 if (size < 4 * sizeof(unsigned int))
1038 return -ENOMEM;
1039 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001040 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1041 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001042 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
1043 val1 = ((int)val1) * ((int)val2);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001044 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
1045 return -EFAULT;
1046 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
1047 return -EFAULT;
1048 if (put_user(val1, _tlv + 2))
1049 return -EFAULT;
1050 if (put_user(val2, _tlv + 3))
1051 return -EFAULT;
1052 return 0;
1053}
1054
Takashi Iwai2134ea42008-01-10 16:53:55 +01001055/*
1056 * set (static) TLV for virtual master volume; recalculated as max 0dB
1057 */
1058void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
1059 unsigned int *tlv)
1060{
1061 u32 caps;
1062 int nums, step;
1063
1064 caps = query_amp_caps(codec, nid, dir);
1065 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1066 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1067 step = (step + 1) * 25;
1068 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
1069 tlv[1] = 2 * sizeof(unsigned int);
1070 tlv[2] = -nums * step;
1071 tlv[3] = step;
1072}
1073
1074/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01001075static struct snd_kcontrol *
1076_snd_hda_find_mixer_ctl(struct hda_codec *codec,
1077 const char *name, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01001078{
1079 struct snd_ctl_elem_id id;
1080 memset(&id, 0, sizeof(id));
1081 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwai09f99702008-02-04 12:31:13 +01001082 id.index = idx;
Takashi Iwai2134ea42008-01-10 16:53:55 +01001083 strcpy(id.name, name);
1084 return snd_ctl_find_id(codec->bus->card, &id);
1085}
1086
Takashi Iwai09f99702008-02-04 12:31:13 +01001087struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
1088 const char *name)
1089{
1090 return _snd_hda_find_mixer_ctl(codec, name, 0);
1091}
1092
Takashi Iwai2134ea42008-01-10 16:53:55 +01001093/* create a virtual master control and add slaves */
1094int snd_hda_add_vmaster(struct hda_codec *codec, char *name,
1095 unsigned int *tlv, const char **slaves)
1096{
1097 struct snd_kcontrol *kctl;
1098 const char **s;
1099 int err;
1100
Takashi Iwai2f085542008-02-22 18:43:50 +01001101 for (s = slaves; *s && !snd_hda_find_mixer_ctl(codec, *s); s++)
1102 ;
1103 if (!*s) {
1104 snd_printdd("No slave found for %s\n", name);
1105 return 0;
1106 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01001107 kctl = snd_ctl_make_virtual_master(name, tlv);
1108 if (!kctl)
1109 return -ENOMEM;
1110 err = snd_ctl_add(codec->bus->card, kctl);
1111 if (err < 0)
1112 return err;
1113
1114 for (s = slaves; *s; s++) {
1115 struct snd_kcontrol *sctl;
1116
1117 sctl = snd_hda_find_mixer_ctl(codec, *s);
1118 if (!sctl) {
1119 snd_printdd("Cannot find slave %s, skipped\n", *s);
1120 continue;
1121 }
1122 err = snd_ctl_add_slave(kctl, sctl);
1123 if (err < 0)
1124 return err;
1125 }
1126 return 0;
1127}
1128
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129/* switch */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001130int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
1131 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132{
1133 int chs = get_amp_channels(kcontrol);
1134
1135 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1136 uinfo->count = chs == 3 ? 2 : 1;
1137 uinfo->value.integer.min = 0;
1138 uinfo->value.integer.max = 1;
1139 return 0;
1140}
1141
Takashi Iwai0ba21762007-04-16 11:29:14 +02001142int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
1143 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144{
1145 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1146 hda_nid_t nid = get_amp_nid(kcontrol);
1147 int chs = get_amp_channels(kcontrol);
1148 int dir = get_amp_direction(kcontrol);
1149 int idx = get_amp_index(kcontrol);
1150 long *valp = ucontrol->value.integer.value;
1151
1152 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02001153 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02001154 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02001156 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02001157 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 return 0;
1159}
1160
Takashi Iwai0ba21762007-04-16 11:29:14 +02001161int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
1162 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163{
1164 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1165 hda_nid_t nid = get_amp_nid(kcontrol);
1166 int chs = get_amp_channels(kcontrol);
1167 int dir = get_amp_direction(kcontrol);
1168 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 long *valp = ucontrol->value.integer.value;
1170 int change = 0;
1171
Takashi Iwaicb53c622007-08-10 17:21:45 +02001172 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001173 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001174 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02001175 HDA_AMP_MUTE,
1176 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001177 valp++;
1178 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001179 if (chs & 2)
1180 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02001181 HDA_AMP_MUTE,
1182 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001183#ifdef CONFIG_SND_HDA_POWER_SAVE
1184 if (codec->patch_ops.check_power_status)
1185 codec->patch_ops.check_power_status(codec, nid);
1186#endif
1187 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 return change;
1189}
1190
1191/*
Takashi Iwai985be542005-11-02 18:26:49 +01001192 * bound volume controls
1193 *
1194 * bind multiple volumes (# indices, from 0)
1195 */
1196
1197#define AMP_VAL_IDX_SHIFT 19
1198#define AMP_VAL_IDX_MASK (0x0f<<19)
1199
Takashi Iwai0ba21762007-04-16 11:29:14 +02001200int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
1201 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01001202{
1203 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1204 unsigned long pval;
1205 int err;
1206
Ingo Molnar62932df2006-01-16 16:34:20 +01001207 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Takashi Iwai985be542005-11-02 18:26:49 +01001208 pval = kcontrol->private_value;
1209 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
1210 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
1211 kcontrol->private_value = pval;
Ingo Molnar62932df2006-01-16 16:34:20 +01001212 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01001213 return err;
1214}
1215
Takashi Iwai0ba21762007-04-16 11:29:14 +02001216int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
1217 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01001218{
1219 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1220 unsigned long pval;
1221 int i, indices, err = 0, change = 0;
1222
Ingo Molnar62932df2006-01-16 16:34:20 +01001223 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Takashi Iwai985be542005-11-02 18:26:49 +01001224 pval = kcontrol->private_value;
1225 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
1226 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001227 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
1228 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01001229 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1230 if (err < 0)
1231 break;
1232 change |= err;
1233 }
1234 kcontrol->private_value = pval;
Ingo Molnar62932df2006-01-16 16:34:20 +01001235 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01001236 return err < 0 ? err : change;
1237}
1238
1239/*
Takashi Iwai532d5382007-07-27 19:02:40 +02001240 * generic bound volume/swtich controls
1241 */
1242int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
1243 struct snd_ctl_elem_info *uinfo)
1244{
1245 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1246 struct hda_bind_ctls *c;
1247 int err;
1248
Takashi Iwai532d5382007-07-27 19:02:40 +02001249 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01001250 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02001251 kcontrol->private_value = *c->values;
1252 err = c->ops->info(kcontrol, uinfo);
1253 kcontrol->private_value = (long)c;
1254 mutex_unlock(&codec->spdif_mutex);
1255 return err;
1256}
1257
1258int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
1259 struct snd_ctl_elem_value *ucontrol)
1260{
1261 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1262 struct hda_bind_ctls *c;
1263 int err;
1264
Takashi Iwai532d5382007-07-27 19:02:40 +02001265 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01001266 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02001267 kcontrol->private_value = *c->values;
1268 err = c->ops->get(kcontrol, ucontrol);
1269 kcontrol->private_value = (long)c;
1270 mutex_unlock(&codec->spdif_mutex);
1271 return err;
1272}
1273
1274int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
1275 struct snd_ctl_elem_value *ucontrol)
1276{
1277 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1278 struct hda_bind_ctls *c;
1279 unsigned long *vals;
1280 int err = 0, change = 0;
1281
Takashi Iwai532d5382007-07-27 19:02:40 +02001282 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01001283 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02001284 for (vals = c->values; *vals; vals++) {
1285 kcontrol->private_value = *vals;
1286 err = c->ops->put(kcontrol, ucontrol);
1287 if (err < 0)
1288 break;
1289 change |= err;
1290 }
1291 kcontrol->private_value = (long)c;
1292 mutex_unlock(&codec->spdif_mutex);
1293 return err < 0 ? err : change;
1294}
1295
1296int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1297 unsigned int size, unsigned int __user *tlv)
1298{
1299 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1300 struct hda_bind_ctls *c;
1301 int err;
1302
Takashi Iwai532d5382007-07-27 19:02:40 +02001303 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01001304 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02001305 kcontrol->private_value = *c->values;
1306 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
1307 kcontrol->private_value = (long)c;
1308 mutex_unlock(&codec->spdif_mutex);
1309 return err;
1310}
1311
1312struct hda_ctl_ops snd_hda_bind_vol = {
1313 .info = snd_hda_mixer_amp_volume_info,
1314 .get = snd_hda_mixer_amp_volume_get,
1315 .put = snd_hda_mixer_amp_volume_put,
1316 .tlv = snd_hda_mixer_amp_tlv
1317};
1318
1319struct hda_ctl_ops snd_hda_bind_sw = {
1320 .info = snd_hda_mixer_amp_switch_info,
1321 .get = snd_hda_mixer_amp_switch_get,
1322 .put = snd_hda_mixer_amp_switch_put,
1323 .tlv = snd_hda_mixer_amp_tlv
1324};
1325
1326/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 * SPDIF out controls
1328 */
1329
Takashi Iwai0ba21762007-04-16 11:29:14 +02001330static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
1331 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332{
1333 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1334 uinfo->count = 1;
1335 return 0;
1336}
1337
Takashi Iwai0ba21762007-04-16 11:29:14 +02001338static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
1339 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340{
1341 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
1342 IEC958_AES0_NONAUDIO |
1343 IEC958_AES0_CON_EMPHASIS_5015 |
1344 IEC958_AES0_CON_NOT_COPYRIGHT;
1345 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
1346 IEC958_AES1_CON_ORIGINAL;
1347 return 0;
1348}
1349
Takashi Iwai0ba21762007-04-16 11:29:14 +02001350static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
1351 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352{
1353 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
1354 IEC958_AES0_NONAUDIO |
1355 IEC958_AES0_PRO_EMPHASIS_5015;
1356 return 0;
1357}
1358
Takashi Iwai0ba21762007-04-16 11:29:14 +02001359static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
1360 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361{
1362 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1363
1364 ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
1365 ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
1366 ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
1367 ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
1368
1369 return 0;
1370}
1371
1372/* convert from SPDIF status bits to HDA SPDIF bits
1373 * bit 0 (DigEn) is always set zero (to be filled later)
1374 */
1375static unsigned short convert_from_spdif_status(unsigned int sbits)
1376{
1377 unsigned short val = 0;
1378
1379 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02001380 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02001382 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001384 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
1385 IEC958_AES0_PRO_EMPHASIS_5015)
1386 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001388 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
1389 IEC958_AES0_CON_EMPHASIS_5015)
1390 val |= AC_DIG1_EMPHASIS;
1391 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
1392 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02001394 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
1396 }
1397 return val;
1398}
1399
1400/* convert to SPDIF status bits from HDA SPDIF bits
1401 */
1402static unsigned int convert_to_spdif_status(unsigned short val)
1403{
1404 unsigned int sbits = 0;
1405
Takashi Iwai0ba21762007-04-16 11:29:14 +02001406 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001408 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 sbits |= IEC958_AES0_PROFESSIONAL;
1410 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001411 if (sbits & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
1413 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001414 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001416 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001418 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
1420 sbits |= val & (0x7f << 8);
1421 }
1422 return sbits;
1423}
1424
Takashi Iwai2f728532008-09-25 16:32:41 +02001425/* set digital convert verbs both for the given NID and its slaves */
1426static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
1427 int verb, int val)
1428{
1429 hda_nid_t *d;
1430
1431 snd_hda_codec_write(codec, nid, 0, verb, val);
1432 d = codec->slave_dig_outs;
1433 if (!d)
1434 return;
1435 for (; *d; d++)
1436 snd_hda_codec_write(codec, *d, 0, verb, val);
1437}
1438
1439static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
1440 int dig1, int dig2)
1441{
1442 if (dig1 != -1)
1443 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
1444 if (dig2 != -1)
1445 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
1446}
1447
Takashi Iwai0ba21762007-04-16 11:29:14 +02001448static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
1449 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450{
1451 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1452 hda_nid_t nid = kcontrol->private_value;
1453 unsigned short val;
1454 int change;
1455
Ingo Molnar62932df2006-01-16 16:34:20 +01001456 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 codec->spdif_status = ucontrol->value.iec958.status[0] |
1458 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
1459 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
1460 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
1461 val = convert_from_spdif_status(codec->spdif_status);
1462 val |= codec->spdif_ctls & 1;
1463 change = codec->spdif_ctls != val;
1464 codec->spdif_ctls = val;
1465
Takashi Iwai2f728532008-09-25 16:32:41 +02001466 if (change)
1467 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
Ingo Molnar62932df2006-01-16 16:34:20 +01001469 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 return change;
1471}
1472
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001473#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
Takashi Iwai0ba21762007-04-16 11:29:14 +02001475static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
1476 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477{
1478 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1479
Takashi Iwai0ba21762007-04-16 11:29:14 +02001480 ucontrol->value.integer.value[0] = codec->spdif_ctls & AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 return 0;
1482}
1483
Takashi Iwai0ba21762007-04-16 11:29:14 +02001484static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
1485 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486{
1487 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1488 hda_nid_t nid = kcontrol->private_value;
1489 unsigned short val;
1490 int change;
1491
Ingo Molnar62932df2006-01-16 16:34:20 +01001492 mutex_lock(&codec->spdif_mutex);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001493 val = codec->spdif_ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02001495 val |= AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 change = codec->spdif_ctls != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001497 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 codec->spdif_ctls = val;
Takashi Iwai2f728532008-09-25 16:32:41 +02001499 set_dig_out_convert(codec, nid, val & 0xff, -1);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001500 /* unmute amp switch (if any) */
1501 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
Takashi Iwai47fd8302007-08-10 17:11:07 +02001502 (val & AC_DIG1_ENABLE))
1503 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
1504 HDA_AMP_MUTE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 }
Ingo Molnar62932df2006-01-16 16:34:20 +01001506 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 return change;
1508}
1509
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001510static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 {
1512 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1513 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1514 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
1515 .info = snd_hda_spdif_mask_info,
1516 .get = snd_hda_spdif_cmask_get,
1517 },
1518 {
1519 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1520 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1521 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
1522 .info = snd_hda_spdif_mask_info,
1523 .get = snd_hda_spdif_pmask_get,
1524 },
1525 {
1526 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1527 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1528 .info = snd_hda_spdif_mask_info,
1529 .get = snd_hda_spdif_default_get,
1530 .put = snd_hda_spdif_default_put,
1531 },
1532 {
1533 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1534 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
1535 .info = snd_hda_spdif_out_switch_info,
1536 .get = snd_hda_spdif_out_switch_get,
1537 .put = snd_hda_spdif_out_switch_put,
1538 },
1539 { } /* end */
1540};
1541
Takashi Iwai09f99702008-02-04 12:31:13 +01001542#define SPDIF_MAX_IDX 4 /* 4 instances should be enough to probe */
1543
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544/**
1545 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
1546 * @codec: the HDA codec
1547 * @nid: audio out widget NID
1548 *
1549 * Creates controls related with the SPDIF output.
1550 * Called from each patch supporting the SPDIF out.
1551 *
1552 * Returns 0 if successful, or a negative error code.
1553 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02001554int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555{
1556 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001557 struct snd_kcontrol *kctl;
1558 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01001559 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
Takashi Iwai09f99702008-02-04 12:31:13 +01001561 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
1562 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Playback Switch",
1563 idx))
1564 break;
1565 }
1566 if (idx >= SPDIF_MAX_IDX) {
1567 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
1568 return -EBUSY;
1569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
1571 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwai09f99702008-02-04 12:31:13 +01001572 kctl->id.index = idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 kctl->private_value = nid;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001574 err = snd_ctl_add(codec->bus->card, kctl);
1575 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 return err;
1577 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02001578 codec->spdif_ctls =
Andrew Paprocki3982d172007-12-19 12:13:44 +01001579 snd_hda_codec_read(codec, nid, 0,
1580 AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
1582 return 0;
1583}
1584
1585/*
Takashi Iwai9a081602008-02-12 18:37:26 +01001586 * SPDIF sharing with analog output
1587 */
1588static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
1589 struct snd_ctl_elem_value *ucontrol)
1590{
1591 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
1592 ucontrol->value.integer.value[0] = mout->share_spdif;
1593 return 0;
1594}
1595
1596static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
1597 struct snd_ctl_elem_value *ucontrol)
1598{
1599 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
1600 mout->share_spdif = !!ucontrol->value.integer.value[0];
1601 return 0;
1602}
1603
1604static struct snd_kcontrol_new spdif_share_sw = {
1605 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1606 .name = "IEC958 Default PCM Playback Switch",
1607 .info = snd_ctl_boolean_mono_info,
1608 .get = spdif_share_sw_get,
1609 .put = spdif_share_sw_put,
1610};
1611
1612int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
1613 struct hda_multi_out *mout)
1614{
1615 if (!mout->dig_out_nid)
1616 return 0;
1617 /* ATTENTION: here mout is passed as private_data, instead of codec */
1618 return snd_ctl_add(codec->bus->card,
1619 snd_ctl_new1(&spdif_share_sw, mout));
1620}
1621
1622/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 * SPDIF input
1624 */
1625
1626#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
1627
Takashi Iwai0ba21762007-04-16 11:29:14 +02001628static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
1629 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630{
1631 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1632
1633 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
1634 return 0;
1635}
1636
Takashi Iwai0ba21762007-04-16 11:29:14 +02001637static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
1638 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639{
1640 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1641 hda_nid_t nid = kcontrol->private_value;
1642 unsigned int val = !!ucontrol->value.integer.value[0];
1643 int change;
1644
Ingo Molnar62932df2006-01-16 16:34:20 +01001645 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001647 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001649 snd_hda_codec_write_cache(codec, nid, 0,
1650 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 }
Ingo Molnar62932df2006-01-16 16:34:20 +01001652 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 return change;
1654}
1655
Takashi Iwai0ba21762007-04-16 11:29:14 +02001656static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
1657 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658{
1659 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1660 hda_nid_t nid = kcontrol->private_value;
1661 unsigned short val;
1662 unsigned int sbits;
1663
Andrew Paprocki3982d172007-12-19 12:13:44 +01001664 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 sbits = convert_to_spdif_status(val);
1666 ucontrol->value.iec958.status[0] = sbits;
1667 ucontrol->value.iec958.status[1] = sbits >> 8;
1668 ucontrol->value.iec958.status[2] = sbits >> 16;
1669 ucontrol->value.iec958.status[3] = sbits >> 24;
1670 return 0;
1671}
1672
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001673static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 {
1675 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1676 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH),
1677 .info = snd_hda_spdif_in_switch_info,
1678 .get = snd_hda_spdif_in_switch_get,
1679 .put = snd_hda_spdif_in_switch_put,
1680 },
1681 {
1682 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1683 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1684 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT),
1685 .info = snd_hda_spdif_mask_info,
1686 .get = snd_hda_spdif_in_status_get,
1687 },
1688 { } /* end */
1689};
1690
1691/**
1692 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
1693 * @codec: the HDA codec
1694 * @nid: audio in widget NID
1695 *
1696 * Creates controls related with the SPDIF input.
1697 * Called from each patch supporting the SPDIF in.
1698 *
1699 * Returns 0 if successful, or a negative error code.
1700 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02001701int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702{
1703 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001704 struct snd_kcontrol *kctl;
1705 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01001706 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707
Takashi Iwai09f99702008-02-04 12:31:13 +01001708 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
1709 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Capture Switch",
1710 idx))
1711 break;
1712 }
1713 if (idx >= SPDIF_MAX_IDX) {
1714 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
1715 return -EBUSY;
1716 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
1718 kctl = snd_ctl_new1(dig_mix, codec);
1719 kctl->private_value = nid;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001720 err = snd_ctl_add(codec->bus->card, kctl);
1721 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 return err;
1723 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02001724 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01001725 snd_hda_codec_read(codec, nid, 0,
1726 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02001727 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 return 0;
1729}
1730
Takashi Iwaicb53c622007-08-10 17:21:45 +02001731#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001732/*
1733 * command cache
1734 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001736/* build a 32bit cache key with the widget id and the command parameter */
1737#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
1738#define get_cmd_cache_nid(key) ((key) & 0xff)
1739#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
1740
1741/**
1742 * snd_hda_codec_write_cache - send a single command with caching
1743 * @codec: the HDA codec
1744 * @nid: NID to send the command
1745 * @direct: direct flag
1746 * @verb: the verb to send
1747 * @parm: the parameter for the verb
1748 *
1749 * Send a single command without waiting for response.
1750 *
1751 * Returns 0 if successful, or a negative error code.
1752 */
1753int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
1754 int direct, unsigned int verb, unsigned int parm)
1755{
1756 int err;
Takashi Iwaicb53c622007-08-10 17:21:45 +02001757 snd_hda_power_up(codec);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001758 mutex_lock(&codec->bus->cmd_mutex);
1759 err = codec->bus->ops.command(codec, nid, direct, verb, parm);
1760 if (!err) {
1761 struct hda_cache_head *c;
1762 u32 key = build_cmd_cache_key(nid, verb);
1763 c = get_alloc_hash(&codec->cmd_cache, key);
1764 if (c)
1765 c->val = parm;
1766 }
1767 mutex_unlock(&codec->bus->cmd_mutex);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001768 snd_hda_power_down(codec);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001769 return err;
1770}
1771
1772/* resume the all commands from the cache */
1773void snd_hda_codec_resume_cache(struct hda_codec *codec)
1774{
Takashi Iwai603c4012008-07-30 15:01:44 +02001775 struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001776 int i;
1777
Takashi Iwai603c4012008-07-30 15:01:44 +02001778 for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001779 u32 key = buffer->key;
1780 if (!key)
1781 continue;
1782 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
1783 get_cmd_cache_cmd(key), buffer->val);
1784 }
1785}
1786
1787/**
1788 * snd_hda_sequence_write_cache - sequence writes with caching
1789 * @codec: the HDA codec
1790 * @seq: VERB array to send
1791 *
1792 * Send the commands sequentially from the given array.
1793 * Thte commands are recorded on cache for power-save and resume.
1794 * The array must be terminated with NID=0.
1795 */
1796void snd_hda_sequence_write_cache(struct hda_codec *codec,
1797 const struct hda_verb *seq)
1798{
1799 for (; seq->nid; seq++)
1800 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
1801 seq->param);
1802}
Takashi Iwaicb53c622007-08-10 17:21:45 +02001803#endif /* SND_HDA_NEEDS_RESUME */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001804
Takashi Iwai54d17402005-11-21 16:33:22 +01001805/*
1806 * set power state of the codec
1807 */
1808static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1809 unsigned int power_state)
1810{
Takashi Iwaicb53c622007-08-10 17:21:45 +02001811 hda_nid_t nid;
1812 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01001813
1814 snd_hda_codec_write(codec, fg, 0, AC_VERB_SET_POWER_STATE,
1815 power_state);
Marc Boucherd2595d82008-01-22 15:23:30 +01001816 msleep(10); /* partial workaround for "azx_get_response timeout" */
Takashi Iwai54d17402005-11-21 16:33:22 +01001817
Takashi Iwaicb53c622007-08-10 17:21:45 +02001818 nid = codec->start_nid;
1819 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01001820 unsigned int wcaps = get_wcaps(codec, nid);
1821 if (wcaps & AC_WCAP_POWER) {
1822 unsigned int wid_type = (wcaps & AC_WCAP_TYPE) >>
1823 AC_WCAP_TYPE_SHIFT;
1824 if (wid_type == AC_WID_PIN) {
1825 unsigned int pincap;
1826 /*
1827 * don't power down the widget if it controls
1828 * eapd and EAPD_BTLENABLE is set.
1829 */
1830 pincap = snd_hda_param_read(codec, nid,
1831 AC_PAR_PIN_CAP);
1832 if (pincap & AC_PINCAP_EAPD) {
1833 int eapd = snd_hda_codec_read(codec,
1834 nid, 0,
1835 AC_VERB_GET_EAPD_BTLENABLE, 0);
1836 eapd &= 0x02;
1837 if (power_state == AC_PWRST_D3 && eapd)
1838 continue;
1839 }
Takashi Iwai1194b5b2007-10-10 10:04:26 +02001840 }
Takashi Iwai54d17402005-11-21 16:33:22 +01001841 snd_hda_codec_write(codec, nid, 0,
1842 AC_VERB_SET_POWER_STATE,
1843 power_state);
Takashi Iwai1194b5b2007-10-10 10:04:26 +02001844 }
Takashi Iwai54d17402005-11-21 16:33:22 +01001845 }
1846
Takashi Iwaicb53c622007-08-10 17:21:45 +02001847 if (power_state == AC_PWRST_D0) {
1848 unsigned long end_time;
1849 int state;
Takashi Iwai54d17402005-11-21 16:33:22 +01001850 msleep(10);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001851 /* wait until the codec reachs to D0 */
1852 end_time = jiffies + msecs_to_jiffies(500);
1853 do {
1854 state = snd_hda_codec_read(codec, fg, 0,
1855 AC_VERB_GET_POWER_STATE, 0);
1856 if (state == power_state)
1857 break;
1858 msleep(1);
1859 } while (time_after_eq(end_time, jiffies));
1860 }
Takashi Iwai54d17402005-11-21 16:33:22 +01001861}
1862
Takashi Iwaicb53c622007-08-10 17:21:45 +02001863#ifdef SND_HDA_NEEDS_RESUME
1864/*
1865 * call suspend and power-down; used both from PM and power-save
1866 */
1867static void hda_call_codec_suspend(struct hda_codec *codec)
1868{
1869 if (codec->patch_ops.suspend)
1870 codec->patch_ops.suspend(codec, PMSG_SUSPEND);
1871 hda_set_power_state(codec,
1872 codec->afg ? codec->afg : codec->mfg,
1873 AC_PWRST_D3);
1874#ifdef CONFIG_SND_HDA_POWER_SAVE
1875 cancel_delayed_work(&codec->power_work);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02001876 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02001877 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02001878#endif
1879}
1880
1881/*
1882 * kick up codec; used both from PM and power-save
1883 */
1884static void hda_call_codec_resume(struct hda_codec *codec)
1885{
1886 hda_set_power_state(codec,
1887 codec->afg ? codec->afg : codec->mfg,
1888 AC_PWRST_D0);
1889 if (codec->patch_ops.resume)
1890 codec->patch_ops.resume(codec);
1891 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02001892 if (codec->patch_ops.init)
1893 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001894 snd_hda_codec_resume_amp(codec);
1895 snd_hda_codec_resume_cache(codec);
1896 }
1897}
1898#endif /* SND_HDA_NEEDS_RESUME */
1899
Takashi Iwai54d17402005-11-21 16:33:22 +01001900
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901/**
1902 * snd_hda_build_controls - build mixer controls
1903 * @bus: the BUS
1904 *
1905 * Creates mixer controls for each codec included in the bus.
1906 *
1907 * Returns 0 if successful, otherwise a negative error code.
1908 */
Takashi Iwai756e2b02007-04-16 11:27:07 +02001909int __devinit snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001911 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912
Takashi Iwai0ba21762007-04-16 11:29:14 +02001913 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaicb53c622007-08-10 17:21:45 +02001914 int err = 0;
1915 /* fake as if already powered-on */
1916 hda_keep_power_on(codec);
1917 /* then fire up */
1918 hda_set_power_state(codec,
1919 codec->afg ? codec->afg : codec->mfg,
1920 AC_PWRST_D0);
1921 /* continue to initialize... */
1922 if (codec->patch_ops.init)
1923 err = codec->patch_ops.init(codec);
1924 if (!err && codec->patch_ops.build_controls)
1925 err = codec->patch_ops.build_controls(codec);
1926 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 if (err < 0)
1928 return err;
1929 }
1930
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 return 0;
1932}
1933
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934/*
1935 * stream formats
1936 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02001937struct hda_rate_tbl {
1938 unsigned int hz;
1939 unsigned int alsa_bits;
1940 unsigned int hda_fmt;
1941};
1942
1943static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02001945
1946 /* autodetected value used in snd_hda_query_supported_pcm */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 { 8000, SNDRV_PCM_RATE_8000, 0x0500 }, /* 1/6 x 48 */
1948 { 11025, SNDRV_PCM_RATE_11025, 0x4300 }, /* 1/4 x 44 */
1949 { 16000, SNDRV_PCM_RATE_16000, 0x0200 }, /* 1/3 x 48 */
1950 { 22050, SNDRV_PCM_RATE_22050, 0x4100 }, /* 1/2 x 44 */
1951 { 32000, SNDRV_PCM_RATE_32000, 0x0a00 }, /* 2/3 x 48 */
1952 { 44100, SNDRV_PCM_RATE_44100, 0x4000 }, /* 44 */
1953 { 48000, SNDRV_PCM_RATE_48000, 0x0000 }, /* 48 */
1954 { 88200, SNDRV_PCM_RATE_88200, 0x4800 }, /* 2 x 44 */
1955 { 96000, SNDRV_PCM_RATE_96000, 0x0800 }, /* 2 x 48 */
1956 { 176400, SNDRV_PCM_RATE_176400, 0x5800 },/* 4 x 44 */
1957 { 192000, SNDRV_PCM_RATE_192000, 0x1800 }, /* 4 x 48 */
Takashi Iwaia961f9f2007-04-12 13:08:09 +02001958#define AC_PAR_PCM_RATE_BITS 11
1959 /* up to bits 10, 384kHZ isn't supported properly */
1960
1961 /* not autodetected value */
1962 { 9600, SNDRV_PCM_RATE_KNOT, 0x0400 }, /* 1/5 x 48 */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02001963
Takashi Iwaibefdf312005-08-22 13:57:55 +02001964 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965};
1966
1967/**
1968 * snd_hda_calc_stream_format - calculate format bitset
1969 * @rate: the sample rate
1970 * @channels: the number of channels
1971 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
1972 * @maxbps: the max. bps
1973 *
1974 * Calculate the format bitset from the given rate, channels and th PCM format.
1975 *
1976 * Return zero if invalid.
1977 */
1978unsigned int snd_hda_calc_stream_format(unsigned int rate,
1979 unsigned int channels,
1980 unsigned int format,
1981 unsigned int maxbps)
1982{
1983 int i;
1984 unsigned int val = 0;
1985
Takashi Iwaibefdf312005-08-22 13:57:55 +02001986 for (i = 0; rate_bits[i].hz; i++)
1987 if (rate_bits[i].hz == rate) {
1988 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 break;
1990 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02001991 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 snd_printdd("invalid rate %d\n", rate);
1993 return 0;
1994 }
1995
1996 if (channels == 0 || channels > 8) {
1997 snd_printdd("invalid channels %d\n", channels);
1998 return 0;
1999 }
2000 val |= channels - 1;
2001
2002 switch (snd_pcm_format_width(format)) {
2003 case 8: val |= 0x00; break;
2004 case 16: val |= 0x10; break;
2005 case 20:
2006 case 24:
2007 case 32:
2008 if (maxbps >= 32)
2009 val |= 0x40;
2010 else if (maxbps >= 24)
2011 val |= 0x30;
2012 else
2013 val |= 0x20;
2014 break;
2015 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002016 snd_printdd("invalid format width %d\n",
2017 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 return 0;
2019 }
2020
2021 return val;
2022}
2023
2024/**
2025 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
2026 * @codec: the HDA codec
2027 * @nid: NID to query
2028 * @ratesp: the pointer to store the detected rate bitflags
2029 * @formatsp: the pointer to store the detected formats
2030 * @bpsp: the pointer to store the detected format widths
2031 *
2032 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
2033 * or @bsps argument is ignored.
2034 *
2035 * Returns 0 if successful, otherwise a negative error code.
2036 */
2037int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
2038 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
2039{
2040 int i;
2041 unsigned int val, streams;
2042
2043 val = 0;
2044 if (nid != codec->afg &&
Takashi Iwai54d17402005-11-21 16:33:22 +01002045 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
2047 if (val == -1)
2048 return -EIO;
2049 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002050 if (!val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
2052
2053 if (ratesp) {
2054 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02002055 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02002057 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 }
2059 *ratesp = rates;
2060 }
2061
2062 if (formatsp || bpsp) {
2063 u64 formats = 0;
2064 unsigned int bps;
2065 unsigned int wcaps;
2066
Takashi Iwai54d17402005-11-21 16:33:22 +01002067 wcaps = get_wcaps(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
2069 if (streams == -1)
2070 return -EIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002071 if (!streams) {
2072 streams = snd_hda_param_read(codec, codec->afg,
2073 AC_PAR_STREAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 if (streams == -1)
2075 return -EIO;
2076 }
2077
2078 bps = 0;
2079 if (streams & AC_SUPFMT_PCM) {
2080 if (val & AC_SUPPCM_BITS_8) {
2081 formats |= SNDRV_PCM_FMTBIT_U8;
2082 bps = 8;
2083 }
2084 if (val & AC_SUPPCM_BITS_16) {
2085 formats |= SNDRV_PCM_FMTBIT_S16_LE;
2086 bps = 16;
2087 }
2088 if (wcaps & AC_WCAP_DIGITAL) {
2089 if (val & AC_SUPPCM_BITS_32)
2090 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
2091 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
2092 formats |= SNDRV_PCM_FMTBIT_S32_LE;
2093 if (val & AC_SUPPCM_BITS_24)
2094 bps = 24;
2095 else if (val & AC_SUPPCM_BITS_20)
2096 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002097 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
2098 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 formats |= SNDRV_PCM_FMTBIT_S32_LE;
2100 if (val & AC_SUPPCM_BITS_32)
2101 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 else if (val & AC_SUPPCM_BITS_24)
2103 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02002104 else if (val & AC_SUPPCM_BITS_20)
2105 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 }
2107 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002108 else if (streams == AC_SUPFMT_FLOAT32) {
2109 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
2111 bps = 32;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002112 } else if (streams == AC_SUPFMT_AC3) {
2113 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 /* temporary hack: we have still no proper support
2115 * for the direct AC3 stream...
2116 */
2117 formats |= SNDRV_PCM_FMTBIT_U8;
2118 bps = 8;
2119 }
2120 if (formatsp)
2121 *formatsp = formats;
2122 if (bpsp)
2123 *bpsp = bps;
2124 }
2125
2126 return 0;
2127}
2128
2129/**
Takashi Iwai0ba21762007-04-16 11:29:14 +02002130 * snd_hda_is_supported_format - check whether the given node supports
2131 * the format val
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 *
2133 * Returns 1 if supported, 0 if not.
2134 */
2135int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
2136 unsigned int format)
2137{
2138 int i;
2139 unsigned int val = 0, rate, stream;
2140
2141 if (nid != codec->afg &&
Takashi Iwai54d17402005-11-21 16:33:22 +01002142 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
2144 if (val == -1)
2145 return 0;
2146 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002147 if (!val) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
2149 if (val == -1)
2150 return 0;
2151 }
2152
2153 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02002154 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02002155 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 if (val & (1 << i))
2157 break;
2158 return 0;
2159 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02002160 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 return 0;
2162
2163 stream = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
2164 if (stream == -1)
2165 return 0;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002166 if (!stream && nid != codec->afg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 stream = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002168 if (!stream || stream == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 return 0;
2170
2171 if (stream & AC_SUPFMT_PCM) {
2172 switch (format & 0xf0) {
2173 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002174 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 return 0;
2176 break;
2177 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002178 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 return 0;
2180 break;
2181 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002182 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 return 0;
2184 break;
2185 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002186 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 return 0;
2188 break;
2189 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002190 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 return 0;
2192 break;
2193 default:
2194 return 0;
2195 }
2196 } else {
2197 /* FIXME: check for float32 and AC3? */
2198 }
2199
2200 return 1;
2201}
2202
2203/*
2204 * PCM stuff
2205 */
2206static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
2207 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002208 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209{
2210 return 0;
2211}
2212
2213static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
2214 struct hda_codec *codec,
2215 unsigned int stream_tag,
2216 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002217 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218{
2219 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
2220 return 0;
2221}
2222
2223static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
2224 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002225 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226{
Takashi Iwai888afa12008-03-18 09:57:50 +01002227 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 return 0;
2229}
2230
Takashi Iwai0ba21762007-04-16 11:29:14 +02002231static int __devinit set_pcm_default_values(struct hda_codec *codec,
2232 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233{
Takashi Iwai0ba21762007-04-16 11:29:14 +02002234 /* query support PCM information from the given NID */
2235 if (info->nid && (!info->rates || !info->formats)) {
2236 snd_hda_query_supported_pcm(codec, info->nid,
2237 info->rates ? NULL : &info->rates,
2238 info->formats ? NULL : &info->formats,
2239 info->maxbps ? NULL : &info->maxbps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 }
2241 if (info->ops.open == NULL)
2242 info->ops.open = hda_pcm_default_open_close;
2243 if (info->ops.close == NULL)
2244 info->ops.close = hda_pcm_default_open_close;
2245 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02002246 if (snd_BUG_ON(!info->nid))
2247 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 info->ops.prepare = hda_pcm_default_prepare;
2249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02002251 if (snd_BUG_ON(!info->nid))
2252 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 info->ops.cleanup = hda_pcm_default_cleanup;
2254 }
2255 return 0;
2256}
2257
Takashi Iwai176d5332008-07-30 15:01:44 +02002258/*
2259 * attach a new PCM stream
2260 */
2261static int __devinit
2262snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
2263{
2264 struct hda_pcm_stream *info;
2265 int stream, err;
2266
2267 if (!pcm->name)
2268 return -EINVAL;
2269 for (stream = 0; stream < 2; stream++) {
2270 info = &pcm->stream[stream];
2271 if (info->substreams) {
2272 err = set_pcm_default_values(codec, info);
2273 if (err < 0)
2274 return err;
2275 }
2276 }
2277 return codec->bus->ops.attach_pcm(codec, pcm);
2278}
2279
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280/**
2281 * snd_hda_build_pcms - build PCM information
2282 * @bus: the BUS
2283 *
2284 * Create PCM information for each codec included in the bus.
2285 *
2286 * The build_pcms codec patch is requested to set up codec->num_pcms and
2287 * codec->pcm_info properly. The array is referred by the top-level driver
2288 * to create its PCM instances.
2289 * The allocated codec->pcm_info should be released in codec->patch_ops.free
2290 * callback.
2291 *
2292 * At least, substreams, channels_min and channels_max must be filled for
2293 * each stream. substreams = 0 indicates that the stream doesn't exist.
2294 * When rates and/or formats are zero, the supported values are queried
2295 * from the given nid. The nid is used also by the default ops.prepare
2296 * and ops.cleanup callbacks.
2297 *
2298 * The driver needs to call ops.open in its open callback. Similarly,
2299 * ops.close is supposed to be called in the close callback.
2300 * ops.prepare should be called in the prepare or hw_params callback
2301 * with the proper parameters for set up.
2302 * ops.cleanup should be called in hw_free for clean up of streams.
2303 *
2304 * This function returns 0 if successfull, or a negative error code.
2305 */
Takashi Iwai756e2b02007-04-16 11:27:07 +02002306int __devinit snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307{
Takashi Iwai176d5332008-07-30 15:01:44 +02002308 static const char *dev_name[HDA_PCM_NTYPES] = {
2309 "Audio", "SPDIF", "HDMI", "Modem"
2310 };
2311 /* starting device index for each PCM type */
2312 static int dev_idx[HDA_PCM_NTYPES] = {
2313 [HDA_PCM_TYPE_AUDIO] = 0,
2314 [HDA_PCM_TYPE_SPDIF] = 1,
2315 [HDA_PCM_TYPE_HDMI] = 3,
2316 [HDA_PCM_TYPE_MODEM] = 6
2317 };
2318 /* normal audio device indices; not linear to keep compatibility */
2319 static int audio_idx[4] = { 0, 2, 4, 5 };
Takashi Iwai0ba21762007-04-16 11:29:14 +02002320 struct hda_codec *codec;
Takashi Iwai176d5332008-07-30 15:01:44 +02002321 int num_devs[HDA_PCM_NTYPES];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
Takashi Iwai176d5332008-07-30 15:01:44 +02002323 memset(num_devs, 0, sizeof(num_devs));
Takashi Iwai0ba21762007-04-16 11:29:14 +02002324 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai176d5332008-07-30 15:01:44 +02002325 unsigned int pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 int err;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002327 if (!codec->patch_ops.build_pcms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 continue;
2329 err = codec->patch_ops.build_pcms(codec);
2330 if (err < 0)
2331 return err;
2332 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
Takashi Iwai176d5332008-07-30 15:01:44 +02002333 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2334 int type = cpcm->pcm_type;
2335 switch (type) {
2336 case HDA_PCM_TYPE_AUDIO:
2337 if (num_devs[type] >= ARRAY_SIZE(audio_idx)) {
2338 snd_printk(KERN_WARNING
2339 "Too many audio devices\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 continue;
Takashi Iwai176d5332008-07-30 15:01:44 +02002341 }
2342 cpcm->device = audio_idx[num_devs[type]];
2343 break;
2344 case HDA_PCM_TYPE_SPDIF:
2345 case HDA_PCM_TYPE_HDMI:
2346 case HDA_PCM_TYPE_MODEM:
2347 if (num_devs[type]) {
2348 snd_printk(KERN_WARNING
2349 "%s already defined\n",
2350 dev_name[type]);
2351 continue;
2352 }
2353 cpcm->device = dev_idx[type];
2354 break;
2355 default:
2356 snd_printk(KERN_WARNING
2357 "Invalid PCM type %d\n", type);
2358 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 }
Takashi Iwai176d5332008-07-30 15:01:44 +02002360 num_devs[type]++;
2361 err = snd_hda_attach_pcm(codec, cpcm);
2362 if (err < 0)
2363 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 }
2365 }
2366 return 0;
2367}
2368
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369/**
2370 * snd_hda_check_board_config - compare the current codec with the config table
2371 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002372 * @num_configs: number of config enums
2373 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 * @tbl: configuration table, terminated by null entries
2375 *
2376 * Compares the modelname or PCI subsystem id of the current codec with the
2377 * given configuration table. If a matching entry is found, returns its
2378 * config value (supposed to be 0 or positive).
2379 *
2380 * If no entries are matching, the function returns a negative value.
2381 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002382int snd_hda_check_board_config(struct hda_codec *codec,
2383 int num_configs, const char **models,
2384 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385{
Takashi Iwaif44ac832008-07-30 15:01:45 +02002386 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002387 int i;
2388 for (i = 0; i < num_configs; i++) {
2389 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02002390 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002391 snd_printd(KERN_INFO "hda_codec: model '%s' is "
2392 "selected\n", models[i]);
2393 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 }
2395 }
2396 }
2397
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002398 if (!codec->bus->pci || !tbl)
2399 return -1;
2400
2401 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
2402 if (!tbl)
2403 return -1;
2404 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02002405#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002406 char tmp[10];
2407 const char *model = NULL;
2408 if (models)
2409 model = models[tbl->value];
2410 if (!model) {
2411 sprintf(tmp, "#%d", tbl->value);
2412 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002414 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
2415 "for config %x:%x (%s)\n",
2416 model, tbl->subvendor, tbl->subdevice,
2417 (tbl->name ? tbl->name : "Unknown device"));
2418#endif
2419 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 }
2421 return -1;
2422}
2423
2424/**
2425 * snd_hda_add_new_ctls - create controls from the array
2426 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002427 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 *
2429 * This helper function creates and add new controls in the given array.
2430 * The array must be terminated with an empty entry as terminator.
2431 *
2432 * Returns 0 if successful, or a negative error code.
2433 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002434int snd_hda_add_new_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435{
Takashi Iwaicb53c622007-08-10 17:21:45 +02002436 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437
2438 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01002439 struct snd_kcontrol *kctl;
2440 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002441 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01002442 return -ENOMEM;
2443 err = snd_ctl_add(codec->bus->card, kctl);
2444 if (err < 0) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002445 if (!codec->addr)
Takashi Iwai54d17402005-11-21 16:33:22 +01002446 return err;
2447 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002448 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01002449 return -ENOMEM;
2450 kctl->id.device = codec->addr;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002451 err = snd_ctl_add(codec->bus->card, kctl);
2452 if (err < 0)
Takashi Iwai54d17402005-11-21 16:33:22 +01002453 return err;
2454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 }
2456 return 0;
2457}
2458
Takashi Iwaicb53c622007-08-10 17:21:45 +02002459#ifdef CONFIG_SND_HDA_POWER_SAVE
2460static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2461 unsigned int power_state);
2462
2463static void hda_power_work(struct work_struct *work)
2464{
2465 struct hda_codec *codec =
2466 container_of(work, struct hda_codec, power_work.work);
2467
Maxim Levitsky2e492462007-09-03 15:26:57 +02002468 if (!codec->power_on || codec->power_count) {
2469 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002470 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02002471 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02002472
2473 hda_call_codec_suspend(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002474 if (codec->bus->ops.pm_notify)
2475 codec->bus->ops.pm_notify(codec);
2476}
2477
2478static void hda_keep_power_on(struct hda_codec *codec)
2479{
2480 codec->power_count++;
2481 codec->power_on = 1;
2482}
2483
2484void snd_hda_power_up(struct hda_codec *codec)
2485{
2486 codec->power_count++;
Takashi Iwaia221e282007-08-16 16:35:33 +02002487 if (codec->power_on || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02002488 return;
2489
2490 codec->power_on = 1;
2491 if (codec->bus->ops.pm_notify)
2492 codec->bus->ops.pm_notify(codec);
2493 hda_call_codec_resume(codec);
2494 cancel_delayed_work(&codec->power_work);
Takashi Iwaia221e282007-08-16 16:35:33 +02002495 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002496}
2497
2498void snd_hda_power_down(struct hda_codec *codec)
2499{
2500 --codec->power_count;
Takashi Iwaia221e282007-08-16 16:35:33 +02002501 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02002502 return;
Takashi Iwaia221e282007-08-16 16:35:33 +02002503 if (power_save) {
2504 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002505 schedule_delayed_work(&codec->power_work,
2506 msecs_to_jiffies(power_save * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02002507 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02002508}
2509
2510int snd_hda_check_amp_list_power(struct hda_codec *codec,
2511 struct hda_loopback_check *check,
2512 hda_nid_t nid)
2513{
2514 struct hda_amp_list *p;
2515 int ch, v;
2516
2517 if (!check->amplist)
2518 return 0;
2519 for (p = check->amplist; p->nid; p++) {
2520 if (p->nid == nid)
2521 break;
2522 }
2523 if (!p->nid)
2524 return 0; /* nothing changed */
2525
2526 for (p = check->amplist; p->nid; p++) {
2527 for (ch = 0; ch < 2; ch++) {
2528 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
2529 p->idx);
2530 if (!(v & HDA_AMP_MUTE) && v > 0) {
2531 if (!check->power_on) {
2532 check->power_on = 1;
2533 snd_hda_power_up(codec);
2534 }
2535 return 1;
2536 }
2537 }
2538 }
2539 if (check->power_on) {
2540 check->power_on = 0;
2541 snd_hda_power_down(codec);
2542 }
2543 return 0;
2544}
2545#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002547/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002548 * Channel mode helper
2549 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002550int snd_hda_ch_mode_info(struct hda_codec *codec,
2551 struct snd_ctl_elem_info *uinfo,
2552 const struct hda_channel_mode *chmode,
2553 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002554{
2555 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2556 uinfo->count = 1;
2557 uinfo->value.enumerated.items = num_chmodes;
2558 if (uinfo->value.enumerated.item >= num_chmodes)
2559 uinfo->value.enumerated.item = num_chmodes - 1;
2560 sprintf(uinfo->value.enumerated.name, "%dch",
2561 chmode[uinfo->value.enumerated.item].channels);
2562 return 0;
2563}
2564
Takashi Iwai0ba21762007-04-16 11:29:14 +02002565int snd_hda_ch_mode_get(struct hda_codec *codec,
2566 struct snd_ctl_elem_value *ucontrol,
2567 const struct hda_channel_mode *chmode,
2568 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002569 int max_channels)
2570{
2571 int i;
2572
2573 for (i = 0; i < num_chmodes; i++) {
2574 if (max_channels == chmode[i].channels) {
2575 ucontrol->value.enumerated.item[0] = i;
2576 break;
2577 }
2578 }
2579 return 0;
2580}
2581
Takashi Iwai0ba21762007-04-16 11:29:14 +02002582int snd_hda_ch_mode_put(struct hda_codec *codec,
2583 struct snd_ctl_elem_value *ucontrol,
2584 const struct hda_channel_mode *chmode,
2585 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002586 int *max_channelsp)
2587{
2588 unsigned int mode;
2589
2590 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01002591 if (mode >= num_chmodes)
2592 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002593 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002594 return 0;
2595 /* change the current channel setting */
2596 *max_channelsp = chmode[mode].channels;
2597 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002598 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002599 return 1;
2600}
2601
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602/*
2603 * input MUX helper
2604 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002605int snd_hda_input_mux_info(const struct hda_input_mux *imux,
2606 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607{
2608 unsigned int index;
2609
2610 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2611 uinfo->count = 1;
2612 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02002613 if (!imux->num_items)
2614 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 index = uinfo->value.enumerated.item;
2616 if (index >= imux->num_items)
2617 index = imux->num_items - 1;
2618 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
2619 return 0;
2620}
2621
Takashi Iwai0ba21762007-04-16 11:29:14 +02002622int snd_hda_input_mux_put(struct hda_codec *codec,
2623 const struct hda_input_mux *imux,
2624 struct snd_ctl_elem_value *ucontrol,
2625 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 unsigned int *cur_val)
2627{
2628 unsigned int idx;
2629
Takashi Iwai5513b0c2007-10-09 11:58:41 +02002630 if (!imux->num_items)
2631 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 idx = ucontrol->value.enumerated.item[0];
2633 if (idx >= imux->num_items)
2634 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002635 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002637 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
2638 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 *cur_val = idx;
2640 return 1;
2641}
2642
2643
2644/*
2645 * Multi-channel / digital-out PCM helper functions
2646 */
2647
Takashi Iwai6b97eb42007-04-05 14:51:48 +02002648/* setup SPDIF output stream */
2649static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
2650 unsigned int stream_tag, unsigned int format)
2651{
2652 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Takashi Iwai2f728532008-09-25 16:32:41 +02002653 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
2654 set_dig_out_convert(codec, nid,
2655 codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff,
2656 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02002657 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02002658 if (codec->slave_dig_outs) {
2659 hda_nid_t *d;
2660 for (d = codec->slave_dig_outs; *d; d++)
2661 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
2662 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04002663 }
Takashi Iwai2f728532008-09-25 16:32:41 +02002664 /* turn on again (if needed) */
2665 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
2666 set_dig_out_convert(codec, nid,
2667 codec->spdif_ctls & 0xff, -1);
2668}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04002669
Takashi Iwai2f728532008-09-25 16:32:41 +02002670static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
2671{
2672 snd_hda_codec_cleanup_stream(codec, nid);
2673 if (codec->slave_dig_outs) {
2674 hda_nid_t *d;
2675 for (d = codec->slave_dig_outs; *d; d++)
2676 snd_hda_codec_cleanup_stream(codec, *d);
2677 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02002678}
2679
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680/*
2681 * open the digital out in the exclusive mode
2682 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002683int snd_hda_multi_out_dig_open(struct hda_codec *codec,
2684 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685{
Ingo Molnar62932df2006-01-16 16:34:20 +01002686 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02002687 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
2688 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02002689 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01002691 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 return 0;
2693}
2694
Takashi Iwai6b97eb42007-04-05 14:51:48 +02002695int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
2696 struct hda_multi_out *mout,
2697 unsigned int stream_tag,
2698 unsigned int format,
2699 struct snd_pcm_substream *substream)
2700{
2701 mutex_lock(&codec->spdif_mutex);
2702 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
2703 mutex_unlock(&codec->spdif_mutex);
2704 return 0;
2705}
2706
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707/*
2708 * release the digital out
2709 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002710int snd_hda_multi_out_dig_close(struct hda_codec *codec,
2711 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712{
Ingo Molnar62932df2006-01-16 16:34:20 +01002713 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01002715 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 return 0;
2717}
2718
2719/*
2720 * set up more restrictions for analog out
2721 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002722int snd_hda_multi_out_analog_open(struct hda_codec *codec,
2723 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01002724 struct snd_pcm_substream *substream,
2725 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726{
Takashi Iwai9a081602008-02-12 18:37:26 +01002727 struct snd_pcm_runtime *runtime = substream->runtime;
2728 runtime->hw.channels_max = mout->max_channels;
2729 if (mout->dig_out_nid) {
2730 if (!mout->analog_rates) {
2731 mout->analog_rates = hinfo->rates;
2732 mout->analog_formats = hinfo->formats;
2733 mout->analog_maxbps = hinfo->maxbps;
2734 } else {
2735 runtime->hw.rates = mout->analog_rates;
2736 runtime->hw.formats = mout->analog_formats;
2737 hinfo->maxbps = mout->analog_maxbps;
2738 }
2739 if (!mout->spdif_rates) {
2740 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
2741 &mout->spdif_rates,
2742 &mout->spdif_formats,
2743 &mout->spdif_maxbps);
2744 }
2745 mutex_lock(&codec->spdif_mutex);
2746 if (mout->share_spdif) {
2747 runtime->hw.rates &= mout->spdif_rates;
2748 runtime->hw.formats &= mout->spdif_formats;
2749 if (mout->spdif_maxbps < hinfo->maxbps)
2750 hinfo->maxbps = mout->spdif_maxbps;
2751 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02002752 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01002753 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 return snd_pcm_hw_constraint_step(substream->runtime, 0,
2755 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
2756}
2757
2758/*
2759 * set up the i/o for analog out
2760 * when the digital out is available, copy the front out to digital out, too.
2761 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002762int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
2763 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 unsigned int stream_tag,
2765 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002766 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767{
2768 hda_nid_t *nids = mout->dac_nids;
2769 int chs = substream->runtime->channels;
2770 int i;
2771
Ingo Molnar62932df2006-01-16 16:34:20 +01002772 mutex_lock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01002773 if (mout->dig_out_nid && mout->share_spdif &&
2774 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02002776 snd_hda_is_supported_format(codec, mout->dig_out_nid,
2777 format) &&
2778 !(codec->spdif_status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02002780 setup_dig_out_stream(codec, mout->dig_out_nid,
2781 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 } else {
2783 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02002784 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 }
2786 }
Ingo Molnar62932df2006-01-16 16:34:20 +01002787 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788
2789 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002790 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
2791 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02002792 if (!mout->no_share_stream &&
2793 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002795 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
2796 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01002797 /* extra outputs copied from front */
2798 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02002799 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01002800 snd_hda_codec_setup_stream(codec,
2801 mout->extra_out_nid[i],
2802 stream_tag, 0, format);
2803
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 /* surrounds */
2805 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02002806 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002807 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
2808 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02002809 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002810 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
2811 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 }
2813 return 0;
2814}
2815
2816/*
2817 * clean up the setting for analog out
2818 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002819int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
2820 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821{
2822 hda_nid_t *nids = mout->dac_nids;
2823 int i;
2824
2825 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01002826 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01002828 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwai82bc9552006-03-21 11:24:42 +01002829 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
2830 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01002831 snd_hda_codec_cleanup_stream(codec,
2832 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01002833 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02002835 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 mout->dig_out_used = 0;
2837 }
Ingo Molnar62932df2006-01-16 16:34:20 +01002838 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 return 0;
2840}
2841
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002842/*
Wu Fengguang6b345002008-10-07 14:21:41 +08002843 * Helper for automatic pin configuration
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002844 */
Kailang Yangdf694da2005-12-05 19:42:22 +01002845
Takashi Iwai12f288b2007-08-02 15:51:59 +02002846static int is_in_nid_list(hda_nid_t nid, hda_nid_t *list)
Kailang Yangdf694da2005-12-05 19:42:22 +01002847{
2848 for (; *list; list++)
2849 if (*list == nid)
2850 return 1;
2851 return 0;
2852}
2853
Steve Longerbeam81937d32007-05-08 15:33:03 +02002854
2855/*
2856 * Sort an associated group of pins according to their sequence numbers.
2857 */
2858static void sort_pins_by_sequence(hda_nid_t * pins, short * sequences,
2859 int num_pins)
2860{
2861 int i, j;
2862 short seq;
2863 hda_nid_t nid;
2864
2865 for (i = 0; i < num_pins; i++) {
2866 for (j = i + 1; j < num_pins; j++) {
2867 if (sequences[i] > sequences[j]) {
2868 seq = sequences[i];
2869 sequences[i] = sequences[j];
2870 sequences[j] = seq;
2871 nid = pins[i];
2872 pins[i] = pins[j];
2873 pins[j] = nid;
2874 }
2875 }
2876 }
2877}
2878
2879
Takashi Iwai82bc9552006-03-21 11:24:42 +01002880/*
2881 * Parse all pin widgets and store the useful pin nids to cfg
2882 *
2883 * The number of line-outs or any primary output is stored in line_outs,
2884 * and the corresponding output pins are assigned to line_out_pins[],
2885 * in the order of front, rear, CLFE, side, ...
2886 *
2887 * If more extra outputs (speaker and headphone) are found, the pins are
Takashi Iwaieb06ed82006-09-20 17:10:27 +02002888 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
Takashi Iwai82bc9552006-03-21 11:24:42 +01002889 * is detected, one of speaker of HP pins is assigned as the primary
2890 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
2891 * if any analog output exists.
2892 *
2893 * The analog input pins are assigned to input_pins array.
2894 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
2895 * respectively.
2896 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002897int snd_hda_parse_pin_def_config(struct hda_codec *codec,
2898 struct auto_pin_cfg *cfg,
2899 hda_nid_t *ignore_nids)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002900{
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01002901 hda_nid_t nid, end_nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02002902 short seq, assoc_line_out, assoc_speaker;
2903 short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
2904 short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
Takashi Iwaif889fa92007-10-31 15:49:32 +01002905 short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002906
2907 memset(cfg, 0, sizeof(*cfg));
2908
Steve Longerbeam81937d32007-05-08 15:33:03 +02002909 memset(sequences_line_out, 0, sizeof(sequences_line_out));
2910 memset(sequences_speaker, 0, sizeof(sequences_speaker));
Takashi Iwaif889fa92007-10-31 15:49:32 +01002911 memset(sequences_hp, 0, sizeof(sequences_hp));
Steve Longerbeam81937d32007-05-08 15:33:03 +02002912 assoc_line_out = assoc_speaker = 0;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002913
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01002914 end_nid = codec->start_nid + codec->num_nodes;
2915 for (nid = codec->start_nid; nid < end_nid; nid++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01002916 unsigned int wid_caps = get_wcaps(codec, nid);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002917 unsigned int wid_type =
2918 (wid_caps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002919 unsigned int def_conf;
2920 short assoc, loc;
2921
2922 /* read all default configuration for pin complex */
2923 if (wid_type != AC_WID_PIN)
2924 continue;
Kailang Yangdf694da2005-12-05 19:42:22 +01002925 /* ignore the given nids (e.g. pc-beep returns error) */
2926 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
2927 continue;
2928
Takashi Iwai0ba21762007-04-16 11:29:14 +02002929 def_conf = snd_hda_codec_read(codec, nid, 0,
2930 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002931 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
2932 continue;
2933 loc = get_defcfg_location(def_conf);
2934 switch (get_defcfg_device(def_conf)) {
2935 case AC_JACK_LINE_OUT:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002936 seq = get_defcfg_sequence(def_conf);
2937 assoc = get_defcfg_association(def_conf);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01002938
2939 if (!(wid_caps & AC_WCAP_STEREO))
2940 if (!cfg->mono_out_pin)
2941 cfg->mono_out_pin = nid;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002942 if (!assoc)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002943 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002944 if (!assoc_line_out)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002945 assoc_line_out = assoc;
2946 else if (assoc_line_out != assoc)
2947 continue;
2948 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
2949 continue;
2950 cfg->line_out_pins[cfg->line_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02002951 sequences_line_out[cfg->line_outs] = seq;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002952 cfg->line_outs++;
2953 break;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01002954 case AC_JACK_SPEAKER:
Steve Longerbeam81937d32007-05-08 15:33:03 +02002955 seq = get_defcfg_sequence(def_conf);
2956 assoc = get_defcfg_association(def_conf);
2957 if (! assoc)
2958 continue;
2959 if (! assoc_speaker)
2960 assoc_speaker = assoc;
2961 else if (assoc_speaker != assoc)
2962 continue;
Takashi Iwai82bc9552006-03-21 11:24:42 +01002963 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
2964 continue;
2965 cfg->speaker_pins[cfg->speaker_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02002966 sequences_speaker[cfg->speaker_outs] = seq;
Takashi Iwai82bc9552006-03-21 11:24:42 +01002967 cfg->speaker_outs++;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01002968 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002969 case AC_JACK_HP_OUT:
Takashi Iwaif889fa92007-10-31 15:49:32 +01002970 seq = get_defcfg_sequence(def_conf);
2971 assoc = get_defcfg_association(def_conf);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02002972 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
2973 continue;
2974 cfg->hp_pins[cfg->hp_outs] = nid;
Takashi Iwaif889fa92007-10-31 15:49:32 +01002975 sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
Takashi Iwaieb06ed82006-09-20 17:10:27 +02002976 cfg->hp_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002977 break;
Takashi Iwai314634b2006-09-21 11:56:18 +02002978 case AC_JACK_MIC_IN: {
2979 int preferred, alt;
2980 if (loc == AC_JACK_LOC_FRONT) {
2981 preferred = AUTO_PIN_FRONT_MIC;
2982 alt = AUTO_PIN_MIC;
2983 } else {
2984 preferred = AUTO_PIN_MIC;
2985 alt = AUTO_PIN_FRONT_MIC;
2986 }
2987 if (!cfg->input_pins[preferred])
2988 cfg->input_pins[preferred] = nid;
2989 else if (!cfg->input_pins[alt])
2990 cfg->input_pins[alt] = nid;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002991 break;
Takashi Iwai314634b2006-09-21 11:56:18 +02002992 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002993 case AC_JACK_LINE_IN:
2994 if (loc == AC_JACK_LOC_FRONT)
2995 cfg->input_pins[AUTO_PIN_FRONT_LINE] = nid;
2996 else
2997 cfg->input_pins[AUTO_PIN_LINE] = nid;
2998 break;
2999 case AC_JACK_CD:
3000 cfg->input_pins[AUTO_PIN_CD] = nid;
3001 break;
3002 case AC_JACK_AUX:
3003 cfg->input_pins[AUTO_PIN_AUX] = nid;
3004 break;
3005 case AC_JACK_SPDIF_OUT:
3006 cfg->dig_out_pin = nid;
3007 break;
3008 case AC_JACK_SPDIF_IN:
3009 cfg->dig_in_pin = nid;
3010 break;
3011 }
3012 }
3013
Takashi Iwai5832fcf2008-02-12 18:30:12 +01003014 /* FIX-UP:
3015 * If no line-out is defined but multiple HPs are found,
3016 * some of them might be the real line-outs.
3017 */
3018 if (!cfg->line_outs && cfg->hp_outs > 1) {
3019 int i = 0;
3020 while (i < cfg->hp_outs) {
3021 /* The real HPs should have the sequence 0x0f */
3022 if ((sequences_hp[i] & 0x0f) == 0x0f) {
3023 i++;
3024 continue;
3025 }
3026 /* Move it to the line-out table */
3027 cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
3028 sequences_line_out[cfg->line_outs] = sequences_hp[i];
3029 cfg->line_outs++;
3030 cfg->hp_outs--;
3031 memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
3032 sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
3033 memmove(sequences_hp + i - 1, sequences_hp + i,
3034 sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
3035 }
3036 }
3037
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003038 /* sort by sequence */
Steve Longerbeam81937d32007-05-08 15:33:03 +02003039 sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
3040 cfg->line_outs);
3041 sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
3042 cfg->speaker_outs);
Takashi Iwaif889fa92007-10-31 15:49:32 +01003043 sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
3044 cfg->hp_outs);
Steve Longerbeam81937d32007-05-08 15:33:03 +02003045
Takashi Iwaif889fa92007-10-31 15:49:32 +01003046 /* if we have only one mic, make it AUTO_PIN_MIC */
3047 if (!cfg->input_pins[AUTO_PIN_MIC] &&
3048 cfg->input_pins[AUTO_PIN_FRONT_MIC]) {
3049 cfg->input_pins[AUTO_PIN_MIC] =
3050 cfg->input_pins[AUTO_PIN_FRONT_MIC];
3051 cfg->input_pins[AUTO_PIN_FRONT_MIC] = 0;
3052 }
3053 /* ditto for line-in */
3054 if (!cfg->input_pins[AUTO_PIN_LINE] &&
3055 cfg->input_pins[AUTO_PIN_FRONT_LINE]) {
3056 cfg->input_pins[AUTO_PIN_LINE] =
3057 cfg->input_pins[AUTO_PIN_FRONT_LINE];
3058 cfg->input_pins[AUTO_PIN_FRONT_LINE] = 0;
3059 }
3060
Steve Longerbeam81937d32007-05-08 15:33:03 +02003061 /*
3062 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
3063 * as a primary output
3064 */
3065 if (!cfg->line_outs) {
3066 if (cfg->speaker_outs) {
3067 cfg->line_outs = cfg->speaker_outs;
3068 memcpy(cfg->line_out_pins, cfg->speaker_pins,
3069 sizeof(cfg->speaker_pins));
3070 cfg->speaker_outs = 0;
3071 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
3072 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
3073 } else if (cfg->hp_outs) {
3074 cfg->line_outs = cfg->hp_outs;
3075 memcpy(cfg->line_out_pins, cfg->hp_pins,
3076 sizeof(cfg->hp_pins));
3077 cfg->hp_outs = 0;
3078 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
3079 cfg->line_out_type = AUTO_PIN_HP_OUT;
3080 }
3081 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003082
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02003083 /* Reorder the surround channels
3084 * ALSA sequence is front/surr/clfe/side
3085 * HDA sequence is:
3086 * 4-ch: front/surr => OK as it is
3087 * 6-ch: front/clfe/surr
Takashi Iwai9422db42007-04-20 16:11:43 +02003088 * 8-ch: front/clfe/rear/side|fc
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02003089 */
3090 switch (cfg->line_outs) {
3091 case 3:
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02003092 case 4:
3093 nid = cfg->line_out_pins[1];
Takashi Iwai9422db42007-04-20 16:11:43 +02003094 cfg->line_out_pins[1] = cfg->line_out_pins[2];
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02003095 cfg->line_out_pins[2] = nid;
3096 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003097 }
3098
Takashi Iwai82bc9552006-03-21 11:24:42 +01003099 /*
3100 * debug prints of the parsed results
3101 */
3102 snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3103 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
3104 cfg->line_out_pins[2], cfg->line_out_pins[3],
3105 cfg->line_out_pins[4]);
3106 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3107 cfg->speaker_outs, cfg->speaker_pins[0],
3108 cfg->speaker_pins[1], cfg->speaker_pins[2],
3109 cfg->speaker_pins[3], cfg->speaker_pins[4]);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02003110 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3111 cfg->hp_outs, cfg->hp_pins[0],
3112 cfg->hp_pins[1], cfg->hp_pins[2],
3113 cfg->hp_pins[3], cfg->hp_pins[4]);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01003114 snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
Takashi Iwai82bc9552006-03-21 11:24:42 +01003115 snd_printd(" inputs: mic=0x%x, fmic=0x%x, line=0x%x, fline=0x%x,"
3116 " cd=0x%x, aux=0x%x\n",
3117 cfg->input_pins[AUTO_PIN_MIC],
3118 cfg->input_pins[AUTO_PIN_FRONT_MIC],
3119 cfg->input_pins[AUTO_PIN_LINE],
3120 cfg->input_pins[AUTO_PIN_FRONT_LINE],
3121 cfg->input_pins[AUTO_PIN_CD],
3122 cfg->input_pins[AUTO_PIN_AUX]);
3123
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003124 return 0;
3125}
3126
Takashi Iwai4a471b72005-12-07 13:56:29 +01003127/* labels for input pins */
3128const char *auto_pin_cfg_labels[AUTO_PIN_LAST] = {
3129 "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux"
3130};
3131
3132
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133#ifdef CONFIG_PM
3134/*
3135 * power management
3136 */
3137
3138/**
3139 * snd_hda_suspend - suspend the codecs
3140 * @bus: the HDA bus
3141 * @state: suspsend state
3142 *
3143 * Returns 0 if successful.
3144 */
3145int snd_hda_suspend(struct hda_bus *bus, pm_message_t state)
3146{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003147 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148
Takashi Iwai0ba21762007-04-16 11:29:14 +02003149 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai0b7a2e92007-08-14 15:18:26 +02003150#ifdef CONFIG_SND_HDA_POWER_SAVE
3151 if (!codec->power_on)
3152 continue;
3153#endif
Takashi Iwaicb53c622007-08-10 17:21:45 +02003154 hda_call_codec_suspend(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 }
3156 return 0;
3157}
3158
3159/**
3160 * snd_hda_resume - resume the codecs
3161 * @bus: the HDA bus
3162 * @state: resume state
3163 *
3164 * Returns 0 if successful.
Takashi Iwaicb53c622007-08-10 17:21:45 +02003165 *
3166 * This fucntion is defined only when POWER_SAVE isn't set.
3167 * In the power-save mode, the codec is resumed dynamically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 */
3169int snd_hda_resume(struct hda_bus *bus)
3170{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003171 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172
Takashi Iwai0ba21762007-04-16 11:29:14 +02003173 list_for_each_entry(codec, &bus->codec_list, list) {
Maxim Levitskyd804ad92007-09-03 15:28:04 +02003174 if (snd_hda_codec_needs_resume(codec))
3175 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 return 0;
3178}
Maxim Levitskyd804ad92007-09-03 15:28:04 +02003179#ifdef CONFIG_SND_HDA_POWER_SAVE
3180int snd_hda_codecs_inuse(struct hda_bus *bus)
3181{
3182 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183
Maxim Levitskyd804ad92007-09-03 15:28:04 +02003184 list_for_each_entry(codec, &bus->codec_list, list) {
3185 if (snd_hda_codec_needs_resume(codec))
3186 return 1;
3187 }
3188 return 0;
3189}
3190#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191#endif
Takashi Iwaib2e18592008-07-30 15:01:44 +02003192
3193/*
3194 * generic arrays
3195 */
3196
3197/* get a new element from the given array
3198 * if it exceeds the pre-allocated array size, re-allocate the array
3199 */
3200void *snd_array_new(struct snd_array *array)
3201{
3202 if (array->used >= array->alloced) {
3203 int num = array->alloced + array->alloc_align;
3204 void *nlist = kcalloc(num + 1, array->elem_size, GFP_KERNEL);
3205 if (!nlist)
3206 return NULL;
3207 if (array->list) {
3208 memcpy(nlist, array->list,
3209 array->elem_size * array->alloced);
3210 kfree(array->list);
3211 }
3212 array->list = nlist;
3213 array->alloced = num;
3214 }
3215 return array->list + (array->used++ * array->elem_size);
3216}
3217
3218/* free the given array elements */
3219void snd_array_free(struct snd_array *array)
3220{
3221 kfree(array->list);
3222 array->used = 0;
3223 array->alloced = 0;
3224 array->list = NULL;
3225}