blob: 5b54ac07fcbd4b8cf803771eee83e7271c4ec275 [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 Iwai6c1f45e2008-07-30 15:01:45 +0200347static int 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 Iwai6c1f45e2008-07-30 15:01:45 +0200457static const struct hda_codec_preset *
Takashi Iwai756e2b02007-04-16 11:27:07 +0200458find_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);
Takashi Iwaid13bd412008-07-30 15:01:45 +0200577 snd_array_free(&codec->mixers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 codec->bus->caddr_tbl[codec->addr] = NULL;
579 if (codec->patch_ops.free)
580 codec->patch_ops.free(codec);
Takashi Iwai01751f52007-08-10 16:59:39 +0200581 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +0200582 free_hda_cache(&codec->cmd_cache);
Takashi Iwaif44ac832008-07-30 15:01:45 +0200583 kfree(codec->name);
584 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +0100585 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 kfree(codec);
587}
588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589/**
590 * snd_hda_codec_new - create a HDA codec
591 * @bus: the bus to assign
592 * @codec_addr: the codec address
593 * @codecp: the pointer to store the generated codec
594 *
595 * Returns 0 if successful, or a negative error code.
596 */
Takashi Iwai756e2b02007-04-16 11:27:07 +0200597int __devinit snd_hda_codec_new(struct hda_bus *bus, unsigned int codec_addr,
598 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
600 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +0200601 char component[31];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 int err;
603
Takashi Iwaida3cec32008-08-08 17:12:14 +0200604 if (snd_BUG_ON(!bus))
605 return -EINVAL;
606 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
607 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +0200610 snd_printk(KERN_ERR "hda_codec: "
611 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 return -EBUSY;
613 }
614
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200615 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 if (codec == NULL) {
617 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
618 return -ENOMEM;
619 }
620
621 codec->bus = bus;
622 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +0100623 mutex_init(&codec->spdif_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +0200624 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +0200625 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwaid13bd412008-07-30 15:01:45 +0200626 snd_array_init(&codec->mixers, sizeof(struct snd_kcontrol *), 32);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200627 if (codec->bus->modelname) {
628 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
629 if (!codec->modelname) {
630 snd_hda_codec_free(codec);
631 return -ENODEV;
632 }
633 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Takashi Iwaicb53c622007-08-10 17:21:45 +0200635#ifdef CONFIG_SND_HDA_POWER_SAVE
636 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
637 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
638 * the caller has to power down appropriatley after initialization
639 * phase.
640 */
641 hda_keep_power_on(codec);
642#endif
643
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 list_add_tail(&codec->list, &bus->codec_list);
645 bus->caddr_tbl[codec_addr] = codec;
646
Takashi Iwai0ba21762007-04-16 11:29:14 +0200647 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
648 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +0100649 if (codec->vendor_id == -1)
650 /* read again, hopefully the access method was corrected
651 * in the last read...
652 */
653 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
654 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200655 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
656 AC_PAR_SUBSYSTEM_ID);
657 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
658 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200660 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200661 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200662 snd_printdd("hda_codec: no AFG or MFG node found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 snd_hda_codec_free(codec);
664 return -ENODEV;
665 }
666
Takashi Iwai54d17402005-11-21 16:33:22 +0100667 if (read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg) < 0) {
668 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
669 snd_hda_codec_free(codec);
670 return -ENOMEM;
671 }
672
Takashi Iwai0ba21762007-04-16 11:29:14 +0200673 if (!codec->subsystem_id) {
Takashi Iwai86284e42005-10-11 15:05:54 +0200674 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200675 codec->subsystem_id =
676 snd_hda_codec_read(codec, nid, 0,
677 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +0200678 }
Takashi Iwaif44ac832008-07-30 15:01:45 +0200679 if (bus->modelname)
680 codec->modelname = kstrdup(bus->modelname, GFP_KERNEL);
Takashi Iwai86284e42005-10-11 15:05:54 +0200681
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200682 err = snd_hda_codec_configure(codec);
683 if (err < 0) {
684 snd_hda_codec_free(codec);
685 return err;
686 }
687 snd_hda_codec_proc_new(codec);
688
689#ifdef CONFIG_SND_HDA_HWDEP
690 snd_hda_create_hwdep(codec);
691#endif
692
693 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
694 codec->subsystem_id, codec->revision_id);
695 snd_component_add(codec->bus->card, component);
696
697 if (codecp)
698 *codecp = codec;
699 return 0;
700}
701
702int snd_hda_codec_configure(struct hda_codec *codec)
703{
704 int err;
705
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100706 codec->preset = find_codec_preset(codec);
Takashi Iwaif44ac832008-07-30 15:01:45 +0200707 if (!codec->name) {
708 err = get_codec_name(codec);
709 if (err < 0)
710 return err;
711 }
Takashi Iwai43ea1d42007-04-26 19:12:08 +0200712 /* audio codec should override the mixer name */
Takashi Iwaif44ac832008-07-30 15:01:45 +0200713 if (codec->afg || !*codec->bus->card->mixername)
714 strlcpy(codec->bus->card->mixername, codec->name,
715 sizeof(codec->bus->card->mixername));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Takashi Iwai82467612007-07-27 19:15:54 +0200717 if (is_generic_config(codec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai82467612007-07-27 19:15:54 +0200719 goto patched;
720 }
Takashi Iwai82467612007-07-27 19:15:54 +0200721 if (codec->preset && codec->preset->patch) {
722 err = codec->preset->patch(codec);
723 goto patched;
724 }
725
726 /* call the default parser */
Takashi Iwai82467612007-07-27 19:15:54 +0200727 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai35a1e0c2007-10-19 08:13:40 +0200728 if (err < 0)
729 printk(KERN_ERR "hda-codec: No codec parser is available\n");
Takashi Iwai82467612007-07-27 19:15:54 +0200730
731 patched:
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200732 if (!err && codec->patch_ops.unsol_event)
733 err = init_unsol_queue(codec->bus);
734 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735}
736
737/**
738 * snd_hda_codec_setup_stream - set up the codec for streaming
739 * @codec: the CODEC to set up
740 * @nid: the NID to set up
741 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
742 * @channel_id: channel id to pass, zero based.
743 * @format: stream format.
744 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200745void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
746 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 int channel_id, int format)
748{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200749 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +0200750 return;
751
Takashi Iwai0ba21762007-04-16 11:29:14 +0200752 snd_printdd("hda_codec_setup_stream: "
753 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 nid, stream_tag, channel_id, format);
755 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID,
756 (stream_tag << 4) | channel_id);
757 msleep(1);
758 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, format);
759}
760
Takashi Iwai888afa12008-03-18 09:57:50 +0100761void snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid)
762{
763 if (!nid)
764 return;
765
766 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
767 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
768#if 0 /* keep the format */
769 msleep(1);
770 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0);
771#endif
772}
773
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774/*
775 * amp access functions
776 */
777
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200778/* FIXME: more better hash key? */
779#define HDA_HASH_KEY(nid,dir,idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200781#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
783/* initialize the hash table */
Takashi Iwai01751f52007-08-10 16:59:39 +0200784static void __devinit init_hda_cache(struct hda_cache_rec *cache,
785 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
Takashi Iwai01751f52007-08-10 16:59:39 +0200787 memset(cache, 0, sizeof(*cache));
788 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +0200789 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +0200790}
791
Takashi Iwai1fcaee62007-08-23 00:01:09 +0200792static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +0200793{
Takashi Iwai603c4012008-07-30 15:01:44 +0200794 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795}
796
797/* query the hash. allocate an entry if not found. */
Takashi Iwai01751f52007-08-10 16:59:39 +0200798static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
799 u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
Takashi Iwai01751f52007-08-10 16:59:39 +0200801 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
802 u16 cur = cache->hash[idx];
Takashi Iwai603c4012008-07-30 15:01:44 +0200803 struct hda_cache_head *info_head = cache->buf.list;
Takashi Iwai01751f52007-08-10 16:59:39 +0200804 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
806 while (cur != 0xffff) {
Takashi Iwai603c4012008-07-30 15:01:44 +0200807 info = &info_head[cur];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 if (info->key == key)
809 return info;
810 cur = info->next;
811 }
812
813 /* add a new hash entry */
Takashi Iwai603c4012008-07-30 15:01:44 +0200814 info = snd_array_new(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 info->key = key;
Takashi Iwai01751f52007-08-10 16:59:39 +0200816 info->val = 0;
817 info->next = cache->hash[idx];
818 cache->hash[idx] = cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
820 return info;
821}
822
Takashi Iwai01751f52007-08-10 16:59:39 +0200823/* query and allocate an amp hash entry */
824static inline struct hda_amp_info *
825get_alloc_amp_hash(struct hda_codec *codec, u32 key)
826{
827 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
828}
829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830/*
831 * query AMP capabilities for the given widget and direction
832 */
Matthew Ranostay09a99952008-01-24 11:49:21 +0100833u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200835 struct hda_amp_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Takashi Iwai0ba21762007-04-16 11:29:14 +0200837 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
838 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 return 0;
Takashi Iwai01751f52007-08-10 16:59:39 +0200840 if (!(info->head.val & INFO_AMP_CAPS)) {
Takashi Iwai0ba21762007-04-16 11:29:14 +0200841 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 nid = codec->afg;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200843 info->amp_caps = snd_hda_param_read(codec, nid,
844 direction == HDA_OUTPUT ?
845 AC_PAR_AMP_OUT_CAP :
846 AC_PAR_AMP_IN_CAP);
Takashi Iwaib75e53f2007-05-10 16:56:09 +0200847 if (info->amp_caps)
Takashi Iwai01751f52007-08-10 16:59:39 +0200848 info->head.val |= INFO_AMP_CAPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 }
850 return info->amp_caps;
851}
852
Takashi Iwai897cc182007-05-29 19:01:37 +0200853int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
854 unsigned int caps)
855{
856 struct hda_amp_info *info;
857
858 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
859 if (!info)
860 return -EINVAL;
861 info->amp_caps = caps;
Takashi Iwai01751f52007-08-10 16:59:39 +0200862 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai897cc182007-05-29 19:01:37 +0200863 return 0;
864}
865
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866/*
867 * read the current volume to info
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200868 * if the cache exists, read the cache value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200870static unsigned int get_vol_mute(struct hda_codec *codec,
871 struct hda_amp_info *info, hda_nid_t nid,
872 int ch, int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873{
874 u32 val, parm;
875
Takashi Iwai01751f52007-08-10 16:59:39 +0200876 if (info->head.val & INFO_AMP_VOL(ch))
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200877 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
879 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
880 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
881 parm |= index;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200882 val = snd_hda_codec_read(codec, nid, 0,
883 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 info->vol[ch] = val & 0xff;
Takashi Iwai01751f52007-08-10 16:59:39 +0200885 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200886 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887}
888
889/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200890 * write the current volume in info to the h/w and update the cache
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200892static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
Takashi Iwai0ba21762007-04-16 11:29:14 +0200893 hda_nid_t nid, int ch, int direction, int index,
894 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895{
896 u32 parm;
897
898 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
899 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
900 parm |= index << AC_AMP_SET_INDEX_SHIFT;
901 parm |= val;
902 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200903 info->vol[ch] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904}
905
906/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200907 * read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 */
Takashi Iwai834be882006-03-01 14:16:17 +0100909int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
910 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200912 struct hda_amp_info *info;
913 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
914 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200916 return get_vol_mute(codec, info, nid, ch, direction, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917}
918
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200919/*
920 * update the AMP value, mask = bit mask to set, val = the value
921 */
Takashi Iwai834be882006-03-01 14:16:17 +0100922int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
923 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200925 struct hda_amp_info *info;
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200926
Takashi Iwai0ba21762007-04-16 11:29:14 +0200927 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
928 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200930 val &= mask;
931 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
Takashi Iwai82beb8f2007-08-10 17:09:26 +0200932 if (info->vol[ch] == val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200934 put_vol_mute(codec, info, nid, ch, direction, idx, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 return 1;
936}
937
Takashi Iwai47fd8302007-08-10 17:11:07 +0200938/*
939 * update the AMP stereo with the same mask and value
940 */
941int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
942 int direction, int idx, int mask, int val)
943{
944 int ch, ret = 0;
945 for (ch = 0; ch < 2; ch++)
946 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
947 idx, mask, val);
948 return ret;
949}
950
Takashi Iwaicb53c622007-08-10 17:21:45 +0200951#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwaib3ac5632007-08-10 17:03:40 +0200952/* resume the all amp commands from the cache */
953void snd_hda_codec_resume_amp(struct hda_codec *codec)
954{
Takashi Iwai603c4012008-07-30 15:01:44 +0200955 struct hda_amp_info *buffer = codec->amp_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +0200956 int i;
957
Takashi Iwai603c4012008-07-30 15:01:44 +0200958 for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +0200959 u32 key = buffer->head.key;
960 hda_nid_t nid;
961 unsigned int idx, dir, ch;
962 if (!key)
963 continue;
964 nid = key & 0xff;
965 idx = (key >> 16) & 0xff;
966 dir = (key >> 24) & 0xff;
967 for (ch = 0; ch < 2; ch++) {
968 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
969 continue;
970 put_vol_mute(codec, buffer, nid, ch, dir, idx,
971 buffer->vol[ch]);
972 }
973 }
974}
Takashi Iwaicb53c622007-08-10 17:21:45 +0200975#endif /* SND_HDA_NEEDS_RESUME */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977/* volume */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200978int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
979 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980{
981 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
982 u16 nid = get_amp_nid(kcontrol);
983 u8 chs = get_amp_channels(kcontrol);
984 int dir = get_amp_direction(kcontrol);
985 u32 caps;
986
987 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200988 /* num steps */
989 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
990 if (!caps) {
991 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +0100992 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
993 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 return -EINVAL;
995 }
996 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
997 uinfo->count = chs == 3 ? 2 : 1;
998 uinfo->value.integer.min = 0;
999 uinfo->value.integer.max = caps;
1000 return 0;
1001}
1002
Takashi Iwai0ba21762007-04-16 11:29:14 +02001003int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1004 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005{
1006 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1007 hda_nid_t nid = get_amp_nid(kcontrol);
1008 int chs = get_amp_channels(kcontrol);
1009 int dir = get_amp_direction(kcontrol);
1010 int idx = get_amp_index(kcontrol);
1011 long *valp = ucontrol->value.integer.value;
1012
1013 if (chs & 1)
Takashi Iwai47fd8302007-08-10 17:11:07 +02001014 *valp++ = snd_hda_codec_amp_read(codec, nid, 0, dir, idx)
1015 & HDA_AMP_VOLMASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 if (chs & 2)
Takashi Iwai47fd8302007-08-10 17:11:07 +02001017 *valp = snd_hda_codec_amp_read(codec, nid, 1, dir, idx)
1018 & HDA_AMP_VOLMASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 return 0;
1020}
1021
Takashi Iwai0ba21762007-04-16 11:29:14 +02001022int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1023 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024{
1025 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1026 hda_nid_t nid = get_amp_nid(kcontrol);
1027 int chs = get_amp_channels(kcontrol);
1028 int dir = get_amp_direction(kcontrol);
1029 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 long *valp = ucontrol->value.integer.value;
1031 int change = 0;
1032
Takashi Iwaicb53c622007-08-10 17:21:45 +02001033 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001034 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001035 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
1036 0x7f, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001037 valp++;
1038 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001039 if (chs & 2)
1040 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001041 0x7f, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001042 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 return change;
1044}
1045
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001046int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1047 unsigned int size, unsigned int __user *_tlv)
1048{
1049 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1050 hda_nid_t nid = get_amp_nid(kcontrol);
1051 int dir = get_amp_direction(kcontrol);
1052 u32 caps, val1, val2;
1053
1054 if (size < 4 * sizeof(unsigned int))
1055 return -ENOMEM;
1056 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001057 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1058 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001059 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
1060 val1 = ((int)val1) * ((int)val2);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001061 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
1062 return -EFAULT;
1063 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
1064 return -EFAULT;
1065 if (put_user(val1, _tlv + 2))
1066 return -EFAULT;
1067 if (put_user(val2, _tlv + 3))
1068 return -EFAULT;
1069 return 0;
1070}
1071
Takashi Iwai2134ea42008-01-10 16:53:55 +01001072/*
1073 * set (static) TLV for virtual master volume; recalculated as max 0dB
1074 */
1075void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
1076 unsigned int *tlv)
1077{
1078 u32 caps;
1079 int nums, step;
1080
1081 caps = query_amp_caps(codec, nid, dir);
1082 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1083 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1084 step = (step + 1) * 25;
1085 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
1086 tlv[1] = 2 * sizeof(unsigned int);
1087 tlv[2] = -nums * step;
1088 tlv[3] = step;
1089}
1090
1091/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01001092static struct snd_kcontrol *
1093_snd_hda_find_mixer_ctl(struct hda_codec *codec,
1094 const char *name, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01001095{
1096 struct snd_ctl_elem_id id;
1097 memset(&id, 0, sizeof(id));
1098 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwai09f99702008-02-04 12:31:13 +01001099 id.index = idx;
Takashi Iwai2134ea42008-01-10 16:53:55 +01001100 strcpy(id.name, name);
1101 return snd_ctl_find_id(codec->bus->card, &id);
1102}
1103
Takashi Iwai09f99702008-02-04 12:31:13 +01001104struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
1105 const char *name)
1106{
1107 return _snd_hda_find_mixer_ctl(codec, name, 0);
1108}
1109
Takashi Iwaid13bd412008-07-30 15:01:45 +02001110/* Add a control element and assign to the codec */
1111int snd_hda_ctl_add(struct hda_codec *codec, struct snd_kcontrol *kctl)
1112{
1113 int err;
1114 struct snd_kcontrol **knewp;
1115
1116 err = snd_ctl_add(codec->bus->card, kctl);
1117 if (err < 0)
1118 return err;
1119 knewp = snd_array_new(&codec->mixers);
1120 if (!knewp)
1121 return -ENOMEM;
1122 *knewp = kctl;
1123 return 0;
1124}
1125
1126/* Clear all controls assigned to the given codec */
1127void snd_hda_ctls_clear(struct hda_codec *codec)
1128{
1129 int i;
1130 struct snd_kcontrol **kctls = codec->mixers.list;
1131 for (i = 0; i < codec->mixers.used; i++)
1132 snd_ctl_remove(codec->bus->card, kctls[i]);
1133 snd_array_free(&codec->mixers);
1134}
1135
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001136void snd_hda_codec_reset(struct hda_codec *codec)
1137{
1138 int i;
1139
1140#ifdef CONFIG_SND_HDA_POWER_SAVE
1141 cancel_delayed_work(&codec->power_work);
1142 flush_scheduled_work();
1143#endif
1144 snd_hda_ctls_clear(codec);
1145 /* relase PCMs */
1146 for (i = 0; i < codec->num_pcms; i++) {
1147 if (codec->pcm_info[i].pcm)
1148 snd_device_free(codec->bus->card,
1149 codec->pcm_info[i].pcm);
1150 }
1151 if (codec->patch_ops.free)
1152 codec->patch_ops.free(codec);
1153 codec->spec = NULL;
1154 free_hda_cache(&codec->amp_cache);
1155 free_hda_cache(&codec->cmd_cache);
1156 codec->num_pcms = 0;
1157 codec->pcm_info = NULL;
1158 codec->preset = NULL;
1159}
1160
Takashi Iwai2134ea42008-01-10 16:53:55 +01001161/* create a virtual master control and add slaves */
1162int snd_hda_add_vmaster(struct hda_codec *codec, char *name,
1163 unsigned int *tlv, const char **slaves)
1164{
1165 struct snd_kcontrol *kctl;
1166 const char **s;
1167 int err;
1168
Takashi Iwai2f085542008-02-22 18:43:50 +01001169 for (s = slaves; *s && !snd_hda_find_mixer_ctl(codec, *s); s++)
1170 ;
1171 if (!*s) {
1172 snd_printdd("No slave found for %s\n", name);
1173 return 0;
1174 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01001175 kctl = snd_ctl_make_virtual_master(name, tlv);
1176 if (!kctl)
1177 return -ENOMEM;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001178 err = snd_hda_ctl_add(codec, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001179 if (err < 0)
1180 return err;
1181
1182 for (s = slaves; *s; s++) {
1183 struct snd_kcontrol *sctl;
1184
1185 sctl = snd_hda_find_mixer_ctl(codec, *s);
1186 if (!sctl) {
1187 snd_printdd("Cannot find slave %s, skipped\n", *s);
1188 continue;
1189 }
1190 err = snd_ctl_add_slave(kctl, sctl);
1191 if (err < 0)
1192 return err;
1193 }
1194 return 0;
1195}
1196
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197/* switch */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001198int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
1199 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200{
1201 int chs = get_amp_channels(kcontrol);
1202
1203 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1204 uinfo->count = chs == 3 ? 2 : 1;
1205 uinfo->value.integer.min = 0;
1206 uinfo->value.integer.max = 1;
1207 return 0;
1208}
1209
Takashi Iwai0ba21762007-04-16 11:29:14 +02001210int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
1211 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212{
1213 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1214 hda_nid_t nid = get_amp_nid(kcontrol);
1215 int chs = get_amp_channels(kcontrol);
1216 int dir = get_amp_direction(kcontrol);
1217 int idx = get_amp_index(kcontrol);
1218 long *valp = ucontrol->value.integer.value;
1219
1220 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02001221 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02001222 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02001224 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02001225 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 return 0;
1227}
1228
Takashi Iwai0ba21762007-04-16 11:29:14 +02001229int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
1230 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231{
1232 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1233 hda_nid_t nid = get_amp_nid(kcontrol);
1234 int chs = get_amp_channels(kcontrol);
1235 int dir = get_amp_direction(kcontrol);
1236 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 long *valp = ucontrol->value.integer.value;
1238 int change = 0;
1239
Takashi Iwaicb53c622007-08-10 17:21:45 +02001240 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001241 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001242 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02001243 HDA_AMP_MUTE,
1244 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001245 valp++;
1246 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001247 if (chs & 2)
1248 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02001249 HDA_AMP_MUTE,
1250 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001251#ifdef CONFIG_SND_HDA_POWER_SAVE
1252 if (codec->patch_ops.check_power_status)
1253 codec->patch_ops.check_power_status(codec, nid);
1254#endif
1255 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 return change;
1257}
1258
1259/*
Takashi Iwai985be542005-11-02 18:26:49 +01001260 * bound volume controls
1261 *
1262 * bind multiple volumes (# indices, from 0)
1263 */
1264
1265#define AMP_VAL_IDX_SHIFT 19
1266#define AMP_VAL_IDX_MASK (0x0f<<19)
1267
Takashi Iwai0ba21762007-04-16 11:29:14 +02001268int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
1269 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01001270{
1271 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1272 unsigned long pval;
1273 int err;
1274
Ingo Molnar62932df2006-01-16 16:34:20 +01001275 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Takashi Iwai985be542005-11-02 18:26:49 +01001276 pval = kcontrol->private_value;
1277 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
1278 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
1279 kcontrol->private_value = pval;
Ingo Molnar62932df2006-01-16 16:34:20 +01001280 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01001281 return err;
1282}
1283
Takashi Iwai0ba21762007-04-16 11:29:14 +02001284int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
1285 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01001286{
1287 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1288 unsigned long pval;
1289 int i, indices, err = 0, change = 0;
1290
Ingo Molnar62932df2006-01-16 16:34:20 +01001291 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Takashi Iwai985be542005-11-02 18:26:49 +01001292 pval = kcontrol->private_value;
1293 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
1294 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001295 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
1296 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01001297 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1298 if (err < 0)
1299 break;
1300 change |= err;
1301 }
1302 kcontrol->private_value = pval;
Ingo Molnar62932df2006-01-16 16:34:20 +01001303 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01001304 return err < 0 ? err : change;
1305}
1306
1307/*
Takashi Iwai532d5382007-07-27 19:02:40 +02001308 * generic bound volume/swtich controls
1309 */
1310int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
1311 struct snd_ctl_elem_info *uinfo)
1312{
1313 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1314 struct hda_bind_ctls *c;
1315 int err;
1316
Takashi Iwai532d5382007-07-27 19:02:40 +02001317 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01001318 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02001319 kcontrol->private_value = *c->values;
1320 err = c->ops->info(kcontrol, uinfo);
1321 kcontrol->private_value = (long)c;
1322 mutex_unlock(&codec->spdif_mutex);
1323 return err;
1324}
1325
1326int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
1327 struct snd_ctl_elem_value *ucontrol)
1328{
1329 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1330 struct hda_bind_ctls *c;
1331 int err;
1332
Takashi Iwai532d5382007-07-27 19:02:40 +02001333 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01001334 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02001335 kcontrol->private_value = *c->values;
1336 err = c->ops->get(kcontrol, ucontrol);
1337 kcontrol->private_value = (long)c;
1338 mutex_unlock(&codec->spdif_mutex);
1339 return err;
1340}
1341
1342int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
1343 struct snd_ctl_elem_value *ucontrol)
1344{
1345 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1346 struct hda_bind_ctls *c;
1347 unsigned long *vals;
1348 int err = 0, change = 0;
1349
Takashi Iwai532d5382007-07-27 19:02:40 +02001350 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01001351 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02001352 for (vals = c->values; *vals; vals++) {
1353 kcontrol->private_value = *vals;
1354 err = c->ops->put(kcontrol, ucontrol);
1355 if (err < 0)
1356 break;
1357 change |= err;
1358 }
1359 kcontrol->private_value = (long)c;
1360 mutex_unlock(&codec->spdif_mutex);
1361 return err < 0 ? err : change;
1362}
1363
1364int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1365 unsigned int size, unsigned int __user *tlv)
1366{
1367 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1368 struct hda_bind_ctls *c;
1369 int err;
1370
Takashi Iwai532d5382007-07-27 19:02:40 +02001371 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01001372 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02001373 kcontrol->private_value = *c->values;
1374 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
1375 kcontrol->private_value = (long)c;
1376 mutex_unlock(&codec->spdif_mutex);
1377 return err;
1378}
1379
1380struct hda_ctl_ops snd_hda_bind_vol = {
1381 .info = snd_hda_mixer_amp_volume_info,
1382 .get = snd_hda_mixer_amp_volume_get,
1383 .put = snd_hda_mixer_amp_volume_put,
1384 .tlv = snd_hda_mixer_amp_tlv
1385};
1386
1387struct hda_ctl_ops snd_hda_bind_sw = {
1388 .info = snd_hda_mixer_amp_switch_info,
1389 .get = snd_hda_mixer_amp_switch_get,
1390 .put = snd_hda_mixer_amp_switch_put,
1391 .tlv = snd_hda_mixer_amp_tlv
1392};
1393
1394/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 * SPDIF out controls
1396 */
1397
Takashi Iwai0ba21762007-04-16 11:29:14 +02001398static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
1399 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400{
1401 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1402 uinfo->count = 1;
1403 return 0;
1404}
1405
Takashi Iwai0ba21762007-04-16 11:29:14 +02001406static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
1407 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408{
1409 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
1410 IEC958_AES0_NONAUDIO |
1411 IEC958_AES0_CON_EMPHASIS_5015 |
1412 IEC958_AES0_CON_NOT_COPYRIGHT;
1413 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
1414 IEC958_AES1_CON_ORIGINAL;
1415 return 0;
1416}
1417
Takashi Iwai0ba21762007-04-16 11:29:14 +02001418static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
1419 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420{
1421 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
1422 IEC958_AES0_NONAUDIO |
1423 IEC958_AES0_PRO_EMPHASIS_5015;
1424 return 0;
1425}
1426
Takashi Iwai0ba21762007-04-16 11:29:14 +02001427static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
1428 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429{
1430 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1431
1432 ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
1433 ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
1434 ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
1435 ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
1436
1437 return 0;
1438}
1439
1440/* convert from SPDIF status bits to HDA SPDIF bits
1441 * bit 0 (DigEn) is always set zero (to be filled later)
1442 */
1443static unsigned short convert_from_spdif_status(unsigned int sbits)
1444{
1445 unsigned short val = 0;
1446
1447 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02001448 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02001450 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001452 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
1453 IEC958_AES0_PRO_EMPHASIS_5015)
1454 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001456 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
1457 IEC958_AES0_CON_EMPHASIS_5015)
1458 val |= AC_DIG1_EMPHASIS;
1459 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
1460 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02001462 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
1464 }
1465 return val;
1466}
1467
1468/* convert to SPDIF status bits from HDA SPDIF bits
1469 */
1470static unsigned int convert_to_spdif_status(unsigned short val)
1471{
1472 unsigned int sbits = 0;
1473
Takashi Iwai0ba21762007-04-16 11:29:14 +02001474 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001476 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 sbits |= IEC958_AES0_PROFESSIONAL;
1478 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001479 if (sbits & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
1481 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001482 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001484 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001486 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
1488 sbits |= val & (0x7f << 8);
1489 }
1490 return sbits;
1491}
1492
Takashi Iwai2f728532008-09-25 16:32:41 +02001493/* set digital convert verbs both for the given NID and its slaves */
1494static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
1495 int verb, int val)
1496{
1497 hda_nid_t *d;
1498
1499 snd_hda_codec_write(codec, nid, 0, verb, val);
1500 d = codec->slave_dig_outs;
1501 if (!d)
1502 return;
1503 for (; *d; d++)
1504 snd_hda_codec_write(codec, *d, 0, verb, val);
1505}
1506
1507static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
1508 int dig1, int dig2)
1509{
1510 if (dig1 != -1)
1511 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
1512 if (dig2 != -1)
1513 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
1514}
1515
Takashi Iwai0ba21762007-04-16 11:29:14 +02001516static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
1517 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518{
1519 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1520 hda_nid_t nid = kcontrol->private_value;
1521 unsigned short val;
1522 int change;
1523
Ingo Molnar62932df2006-01-16 16:34:20 +01001524 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 codec->spdif_status = ucontrol->value.iec958.status[0] |
1526 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
1527 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
1528 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
1529 val = convert_from_spdif_status(codec->spdif_status);
1530 val |= codec->spdif_ctls & 1;
1531 change = codec->spdif_ctls != val;
1532 codec->spdif_ctls = val;
1533
Takashi Iwai2f728532008-09-25 16:32:41 +02001534 if (change)
1535 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
Ingo Molnar62932df2006-01-16 16:34:20 +01001537 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 return change;
1539}
1540
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001541#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
Takashi Iwai0ba21762007-04-16 11:29:14 +02001543static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
1544 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545{
1546 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1547
Takashi Iwai0ba21762007-04-16 11:29:14 +02001548 ucontrol->value.integer.value[0] = codec->spdif_ctls & AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 return 0;
1550}
1551
Takashi Iwai0ba21762007-04-16 11:29:14 +02001552static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
1553 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554{
1555 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1556 hda_nid_t nid = kcontrol->private_value;
1557 unsigned short val;
1558 int change;
1559
Ingo Molnar62932df2006-01-16 16:34:20 +01001560 mutex_lock(&codec->spdif_mutex);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001561 val = codec->spdif_ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02001563 val |= AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 change = codec->spdif_ctls != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001565 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 codec->spdif_ctls = val;
Takashi Iwai2f728532008-09-25 16:32:41 +02001567 set_dig_out_convert(codec, nid, val & 0xff, -1);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001568 /* unmute amp switch (if any) */
1569 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
Takashi Iwai47fd8302007-08-10 17:11:07 +02001570 (val & AC_DIG1_ENABLE))
1571 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
1572 HDA_AMP_MUTE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 }
Ingo Molnar62932df2006-01-16 16:34:20 +01001574 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 return change;
1576}
1577
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001578static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 {
1580 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1581 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1582 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
1583 .info = snd_hda_spdif_mask_info,
1584 .get = snd_hda_spdif_cmask_get,
1585 },
1586 {
1587 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1588 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1589 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
1590 .info = snd_hda_spdif_mask_info,
1591 .get = snd_hda_spdif_pmask_get,
1592 },
1593 {
1594 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1595 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1596 .info = snd_hda_spdif_mask_info,
1597 .get = snd_hda_spdif_default_get,
1598 .put = snd_hda_spdif_default_put,
1599 },
1600 {
1601 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1602 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
1603 .info = snd_hda_spdif_out_switch_info,
1604 .get = snd_hda_spdif_out_switch_get,
1605 .put = snd_hda_spdif_out_switch_put,
1606 },
1607 { } /* end */
1608};
1609
Takashi Iwai09f99702008-02-04 12:31:13 +01001610#define SPDIF_MAX_IDX 4 /* 4 instances should be enough to probe */
1611
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612/**
1613 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
1614 * @codec: the HDA codec
1615 * @nid: audio out widget NID
1616 *
1617 * Creates controls related with the SPDIF output.
1618 * Called from each patch supporting the SPDIF out.
1619 *
1620 * Returns 0 if successful, or a negative error code.
1621 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02001622int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623{
1624 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001625 struct snd_kcontrol *kctl;
1626 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01001627 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
Takashi Iwai09f99702008-02-04 12:31:13 +01001629 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
1630 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Playback Switch",
1631 idx))
1632 break;
1633 }
1634 if (idx >= SPDIF_MAX_IDX) {
1635 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
1636 return -EBUSY;
1637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
1639 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwai09f99702008-02-04 12:31:13 +01001640 kctl->id.index = idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 kctl->private_value = nid;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001642 err = snd_hda_ctl_add(codec, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001643 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 return err;
1645 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02001646 codec->spdif_ctls =
Andrew Paprocki3982d172007-12-19 12:13:44 +01001647 snd_hda_codec_read(codec, nid, 0,
1648 AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
1650 return 0;
1651}
1652
1653/*
Takashi Iwai9a081602008-02-12 18:37:26 +01001654 * SPDIF sharing with analog output
1655 */
1656static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
1657 struct snd_ctl_elem_value *ucontrol)
1658{
1659 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
1660 ucontrol->value.integer.value[0] = mout->share_spdif;
1661 return 0;
1662}
1663
1664static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
1665 struct snd_ctl_elem_value *ucontrol)
1666{
1667 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
1668 mout->share_spdif = !!ucontrol->value.integer.value[0];
1669 return 0;
1670}
1671
1672static struct snd_kcontrol_new spdif_share_sw = {
1673 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1674 .name = "IEC958 Default PCM Playback Switch",
1675 .info = snd_ctl_boolean_mono_info,
1676 .get = spdif_share_sw_get,
1677 .put = spdif_share_sw_put,
1678};
1679
1680int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
1681 struct hda_multi_out *mout)
1682{
1683 if (!mout->dig_out_nid)
1684 return 0;
1685 /* ATTENTION: here mout is passed as private_data, instead of codec */
Takashi Iwaid13bd412008-07-30 15:01:45 +02001686 return snd_hda_ctl_add(codec,
Takashi Iwai9a081602008-02-12 18:37:26 +01001687 snd_ctl_new1(&spdif_share_sw, mout));
1688}
1689
1690/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 * SPDIF input
1692 */
1693
1694#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
1695
Takashi Iwai0ba21762007-04-16 11:29:14 +02001696static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
1697 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698{
1699 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1700
1701 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
1702 return 0;
1703}
1704
Takashi Iwai0ba21762007-04-16 11:29:14 +02001705static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
1706 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707{
1708 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1709 hda_nid_t nid = kcontrol->private_value;
1710 unsigned int val = !!ucontrol->value.integer.value[0];
1711 int change;
1712
Ingo Molnar62932df2006-01-16 16:34:20 +01001713 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001715 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001717 snd_hda_codec_write_cache(codec, nid, 0,
1718 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 }
Ingo Molnar62932df2006-01-16 16:34:20 +01001720 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 return change;
1722}
1723
Takashi Iwai0ba21762007-04-16 11:29:14 +02001724static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
1725 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726{
1727 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1728 hda_nid_t nid = kcontrol->private_value;
1729 unsigned short val;
1730 unsigned int sbits;
1731
Andrew Paprocki3982d172007-12-19 12:13:44 +01001732 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 sbits = convert_to_spdif_status(val);
1734 ucontrol->value.iec958.status[0] = sbits;
1735 ucontrol->value.iec958.status[1] = sbits >> 8;
1736 ucontrol->value.iec958.status[2] = sbits >> 16;
1737 ucontrol->value.iec958.status[3] = sbits >> 24;
1738 return 0;
1739}
1740
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001741static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 {
1743 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1744 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH),
1745 .info = snd_hda_spdif_in_switch_info,
1746 .get = snd_hda_spdif_in_switch_get,
1747 .put = snd_hda_spdif_in_switch_put,
1748 },
1749 {
1750 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1751 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1752 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT),
1753 .info = snd_hda_spdif_mask_info,
1754 .get = snd_hda_spdif_in_status_get,
1755 },
1756 { } /* end */
1757};
1758
1759/**
1760 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
1761 * @codec: the HDA codec
1762 * @nid: audio in widget NID
1763 *
1764 * Creates controls related with the SPDIF input.
1765 * Called from each patch supporting the SPDIF in.
1766 *
1767 * Returns 0 if successful, or a negative error code.
1768 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02001769int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770{
1771 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001772 struct snd_kcontrol *kctl;
1773 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01001774 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775
Takashi Iwai09f99702008-02-04 12:31:13 +01001776 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
1777 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Capture Switch",
1778 idx))
1779 break;
1780 }
1781 if (idx >= SPDIF_MAX_IDX) {
1782 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
1783 return -EBUSY;
1784 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
1786 kctl = snd_ctl_new1(dig_mix, codec);
1787 kctl->private_value = nid;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001788 err = snd_hda_ctl_add(codec, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001789 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 return err;
1791 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02001792 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01001793 snd_hda_codec_read(codec, nid, 0,
1794 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02001795 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 return 0;
1797}
1798
Takashi Iwaicb53c622007-08-10 17:21:45 +02001799#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001800/*
1801 * command cache
1802 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001804/* build a 32bit cache key with the widget id and the command parameter */
1805#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
1806#define get_cmd_cache_nid(key) ((key) & 0xff)
1807#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
1808
1809/**
1810 * snd_hda_codec_write_cache - send a single command with caching
1811 * @codec: the HDA codec
1812 * @nid: NID to send the command
1813 * @direct: direct flag
1814 * @verb: the verb to send
1815 * @parm: the parameter for the verb
1816 *
1817 * Send a single command without waiting for response.
1818 *
1819 * Returns 0 if successful, or a negative error code.
1820 */
1821int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
1822 int direct, unsigned int verb, unsigned int parm)
1823{
1824 int err;
Takashi Iwaicb53c622007-08-10 17:21:45 +02001825 snd_hda_power_up(codec);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001826 mutex_lock(&codec->bus->cmd_mutex);
1827 err = codec->bus->ops.command(codec, nid, direct, verb, parm);
1828 if (!err) {
1829 struct hda_cache_head *c;
1830 u32 key = build_cmd_cache_key(nid, verb);
1831 c = get_alloc_hash(&codec->cmd_cache, key);
1832 if (c)
1833 c->val = parm;
1834 }
1835 mutex_unlock(&codec->bus->cmd_mutex);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001836 snd_hda_power_down(codec);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001837 return err;
1838}
1839
1840/* resume the all commands from the cache */
1841void snd_hda_codec_resume_cache(struct hda_codec *codec)
1842{
Takashi Iwai603c4012008-07-30 15:01:44 +02001843 struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001844 int i;
1845
Takashi Iwai603c4012008-07-30 15:01:44 +02001846 for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001847 u32 key = buffer->key;
1848 if (!key)
1849 continue;
1850 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
1851 get_cmd_cache_cmd(key), buffer->val);
1852 }
1853}
1854
1855/**
1856 * snd_hda_sequence_write_cache - sequence writes with caching
1857 * @codec: the HDA codec
1858 * @seq: VERB array to send
1859 *
1860 * Send the commands sequentially from the given array.
1861 * Thte commands are recorded on cache for power-save and resume.
1862 * The array must be terminated with NID=0.
1863 */
1864void snd_hda_sequence_write_cache(struct hda_codec *codec,
1865 const struct hda_verb *seq)
1866{
1867 for (; seq->nid; seq++)
1868 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
1869 seq->param);
1870}
Takashi Iwaicb53c622007-08-10 17:21:45 +02001871#endif /* SND_HDA_NEEDS_RESUME */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001872
Takashi Iwai54d17402005-11-21 16:33:22 +01001873/*
1874 * set power state of the codec
1875 */
1876static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1877 unsigned int power_state)
1878{
Takashi Iwaicb53c622007-08-10 17:21:45 +02001879 hda_nid_t nid;
1880 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01001881
1882 snd_hda_codec_write(codec, fg, 0, AC_VERB_SET_POWER_STATE,
1883 power_state);
Marc Boucherd2595d82008-01-22 15:23:30 +01001884 msleep(10); /* partial workaround for "azx_get_response timeout" */
Takashi Iwai54d17402005-11-21 16:33:22 +01001885
Takashi Iwaicb53c622007-08-10 17:21:45 +02001886 nid = codec->start_nid;
1887 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01001888 unsigned int wcaps = get_wcaps(codec, nid);
1889 if (wcaps & AC_WCAP_POWER) {
1890 unsigned int wid_type = (wcaps & AC_WCAP_TYPE) >>
1891 AC_WCAP_TYPE_SHIFT;
1892 if (wid_type == AC_WID_PIN) {
1893 unsigned int pincap;
1894 /*
1895 * don't power down the widget if it controls
1896 * eapd and EAPD_BTLENABLE is set.
1897 */
1898 pincap = snd_hda_param_read(codec, nid,
1899 AC_PAR_PIN_CAP);
1900 if (pincap & AC_PINCAP_EAPD) {
1901 int eapd = snd_hda_codec_read(codec,
1902 nid, 0,
1903 AC_VERB_GET_EAPD_BTLENABLE, 0);
1904 eapd &= 0x02;
1905 if (power_state == AC_PWRST_D3 && eapd)
1906 continue;
1907 }
Takashi Iwai1194b5b2007-10-10 10:04:26 +02001908 }
Takashi Iwai54d17402005-11-21 16:33:22 +01001909 snd_hda_codec_write(codec, nid, 0,
1910 AC_VERB_SET_POWER_STATE,
1911 power_state);
Takashi Iwai1194b5b2007-10-10 10:04:26 +02001912 }
Takashi Iwai54d17402005-11-21 16:33:22 +01001913 }
1914
Takashi Iwaicb53c622007-08-10 17:21:45 +02001915 if (power_state == AC_PWRST_D0) {
1916 unsigned long end_time;
1917 int state;
Takashi Iwai54d17402005-11-21 16:33:22 +01001918 msleep(10);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001919 /* wait until the codec reachs to D0 */
1920 end_time = jiffies + msecs_to_jiffies(500);
1921 do {
1922 state = snd_hda_codec_read(codec, fg, 0,
1923 AC_VERB_GET_POWER_STATE, 0);
1924 if (state == power_state)
1925 break;
1926 msleep(1);
1927 } while (time_after_eq(end_time, jiffies));
1928 }
Takashi Iwai54d17402005-11-21 16:33:22 +01001929}
1930
Takashi Iwaicb53c622007-08-10 17:21:45 +02001931#ifdef SND_HDA_NEEDS_RESUME
1932/*
1933 * call suspend and power-down; used both from PM and power-save
1934 */
1935static void hda_call_codec_suspend(struct hda_codec *codec)
1936{
1937 if (codec->patch_ops.suspend)
1938 codec->patch_ops.suspend(codec, PMSG_SUSPEND);
1939 hda_set_power_state(codec,
1940 codec->afg ? codec->afg : codec->mfg,
1941 AC_PWRST_D3);
1942#ifdef CONFIG_SND_HDA_POWER_SAVE
1943 cancel_delayed_work(&codec->power_work);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02001944 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02001945 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02001946#endif
1947}
1948
1949/*
1950 * kick up codec; used both from PM and power-save
1951 */
1952static void hda_call_codec_resume(struct hda_codec *codec)
1953{
1954 hda_set_power_state(codec,
1955 codec->afg ? codec->afg : codec->mfg,
1956 AC_PWRST_D0);
1957 if (codec->patch_ops.resume)
1958 codec->patch_ops.resume(codec);
1959 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02001960 if (codec->patch_ops.init)
1961 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001962 snd_hda_codec_resume_amp(codec);
1963 snd_hda_codec_resume_cache(codec);
1964 }
1965}
1966#endif /* SND_HDA_NEEDS_RESUME */
1967
Takashi Iwai54d17402005-11-21 16:33:22 +01001968
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969/**
1970 * snd_hda_build_controls - build mixer controls
1971 * @bus: the BUS
1972 *
1973 * Creates mixer controls for each codec included in the bus.
1974 *
1975 * Returns 0 if successful, otherwise a negative error code.
1976 */
Takashi Iwai756e2b02007-04-16 11:27:07 +02001977int __devinit snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001979 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980
Takashi Iwai0ba21762007-04-16 11:29:14 +02001981 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001982 int err = snd_hda_codec_build_controls(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 if (err < 0)
1984 return err;
1985 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001986 return 0;
1987}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001989int snd_hda_codec_build_controls(struct hda_codec *codec)
1990{
1991 int err = 0;
1992 /* fake as if already powered-on */
1993 hda_keep_power_on(codec);
1994 /* then fire up */
1995 hda_set_power_state(codec,
1996 codec->afg ? codec->afg : codec->mfg,
1997 AC_PWRST_D0);
1998 /* continue to initialize... */
1999 if (codec->patch_ops.init)
2000 err = codec->patch_ops.init(codec);
2001 if (!err && codec->patch_ops.build_controls)
2002 err = codec->patch_ops.build_controls(codec);
2003 snd_hda_power_down(codec);
2004 if (err < 0)
2005 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 return 0;
2007}
2008
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009/*
2010 * stream formats
2011 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02002012struct hda_rate_tbl {
2013 unsigned int hz;
2014 unsigned int alsa_bits;
2015 unsigned int hda_fmt;
2016};
2017
2018static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02002020
2021 /* autodetected value used in snd_hda_query_supported_pcm */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 { 8000, SNDRV_PCM_RATE_8000, 0x0500 }, /* 1/6 x 48 */
2023 { 11025, SNDRV_PCM_RATE_11025, 0x4300 }, /* 1/4 x 44 */
2024 { 16000, SNDRV_PCM_RATE_16000, 0x0200 }, /* 1/3 x 48 */
2025 { 22050, SNDRV_PCM_RATE_22050, 0x4100 }, /* 1/2 x 44 */
2026 { 32000, SNDRV_PCM_RATE_32000, 0x0a00 }, /* 2/3 x 48 */
2027 { 44100, SNDRV_PCM_RATE_44100, 0x4000 }, /* 44 */
2028 { 48000, SNDRV_PCM_RATE_48000, 0x0000 }, /* 48 */
2029 { 88200, SNDRV_PCM_RATE_88200, 0x4800 }, /* 2 x 44 */
2030 { 96000, SNDRV_PCM_RATE_96000, 0x0800 }, /* 2 x 48 */
2031 { 176400, SNDRV_PCM_RATE_176400, 0x5800 },/* 4 x 44 */
2032 { 192000, SNDRV_PCM_RATE_192000, 0x1800 }, /* 4 x 48 */
Takashi Iwaia961f9f2007-04-12 13:08:09 +02002033#define AC_PAR_PCM_RATE_BITS 11
2034 /* up to bits 10, 384kHZ isn't supported properly */
2035
2036 /* not autodetected value */
2037 { 9600, SNDRV_PCM_RATE_KNOT, 0x0400 }, /* 1/5 x 48 */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02002038
Takashi Iwaibefdf312005-08-22 13:57:55 +02002039 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040};
2041
2042/**
2043 * snd_hda_calc_stream_format - calculate format bitset
2044 * @rate: the sample rate
2045 * @channels: the number of channels
2046 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
2047 * @maxbps: the max. bps
2048 *
2049 * Calculate the format bitset from the given rate, channels and th PCM format.
2050 *
2051 * Return zero if invalid.
2052 */
2053unsigned int snd_hda_calc_stream_format(unsigned int rate,
2054 unsigned int channels,
2055 unsigned int format,
2056 unsigned int maxbps)
2057{
2058 int i;
2059 unsigned int val = 0;
2060
Takashi Iwaibefdf312005-08-22 13:57:55 +02002061 for (i = 0; rate_bits[i].hz; i++)
2062 if (rate_bits[i].hz == rate) {
2063 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 break;
2065 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002066 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 snd_printdd("invalid rate %d\n", rate);
2068 return 0;
2069 }
2070
2071 if (channels == 0 || channels > 8) {
2072 snd_printdd("invalid channels %d\n", channels);
2073 return 0;
2074 }
2075 val |= channels - 1;
2076
2077 switch (snd_pcm_format_width(format)) {
2078 case 8: val |= 0x00; break;
2079 case 16: val |= 0x10; break;
2080 case 20:
2081 case 24:
2082 case 32:
2083 if (maxbps >= 32)
2084 val |= 0x40;
2085 else if (maxbps >= 24)
2086 val |= 0x30;
2087 else
2088 val |= 0x20;
2089 break;
2090 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002091 snd_printdd("invalid format width %d\n",
2092 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 return 0;
2094 }
2095
2096 return val;
2097}
2098
2099/**
2100 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
2101 * @codec: the HDA codec
2102 * @nid: NID to query
2103 * @ratesp: the pointer to store the detected rate bitflags
2104 * @formatsp: the pointer to store the detected formats
2105 * @bpsp: the pointer to store the detected format widths
2106 *
2107 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
2108 * or @bsps argument is ignored.
2109 *
2110 * Returns 0 if successful, otherwise a negative error code.
2111 */
2112int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
2113 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
2114{
2115 int i;
2116 unsigned int val, streams;
2117
2118 val = 0;
2119 if (nid != codec->afg &&
Takashi Iwai54d17402005-11-21 16:33:22 +01002120 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
2122 if (val == -1)
2123 return -EIO;
2124 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002125 if (!val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
2127
2128 if (ratesp) {
2129 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02002130 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02002132 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 }
2134 *ratesp = rates;
2135 }
2136
2137 if (formatsp || bpsp) {
2138 u64 formats = 0;
2139 unsigned int bps;
2140 unsigned int wcaps;
2141
Takashi Iwai54d17402005-11-21 16:33:22 +01002142 wcaps = get_wcaps(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
2144 if (streams == -1)
2145 return -EIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002146 if (!streams) {
2147 streams = snd_hda_param_read(codec, codec->afg,
2148 AC_PAR_STREAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 if (streams == -1)
2150 return -EIO;
2151 }
2152
2153 bps = 0;
2154 if (streams & AC_SUPFMT_PCM) {
2155 if (val & AC_SUPPCM_BITS_8) {
2156 formats |= SNDRV_PCM_FMTBIT_U8;
2157 bps = 8;
2158 }
2159 if (val & AC_SUPPCM_BITS_16) {
2160 formats |= SNDRV_PCM_FMTBIT_S16_LE;
2161 bps = 16;
2162 }
2163 if (wcaps & AC_WCAP_DIGITAL) {
2164 if (val & AC_SUPPCM_BITS_32)
2165 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
2166 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
2167 formats |= SNDRV_PCM_FMTBIT_S32_LE;
2168 if (val & AC_SUPPCM_BITS_24)
2169 bps = 24;
2170 else if (val & AC_SUPPCM_BITS_20)
2171 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002172 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
2173 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 formats |= SNDRV_PCM_FMTBIT_S32_LE;
2175 if (val & AC_SUPPCM_BITS_32)
2176 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 else if (val & AC_SUPPCM_BITS_24)
2178 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02002179 else if (val & AC_SUPPCM_BITS_20)
2180 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 }
2182 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002183 else if (streams == AC_SUPFMT_FLOAT32) {
2184 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
2186 bps = 32;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002187 } else if (streams == AC_SUPFMT_AC3) {
2188 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 /* temporary hack: we have still no proper support
2190 * for the direct AC3 stream...
2191 */
2192 formats |= SNDRV_PCM_FMTBIT_U8;
2193 bps = 8;
2194 }
2195 if (formatsp)
2196 *formatsp = formats;
2197 if (bpsp)
2198 *bpsp = bps;
2199 }
2200
2201 return 0;
2202}
2203
2204/**
Takashi Iwai0ba21762007-04-16 11:29:14 +02002205 * snd_hda_is_supported_format - check whether the given node supports
2206 * the format val
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 *
2208 * Returns 1 if supported, 0 if not.
2209 */
2210int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
2211 unsigned int format)
2212{
2213 int i;
2214 unsigned int val = 0, rate, stream;
2215
2216 if (nid != codec->afg &&
Takashi Iwai54d17402005-11-21 16:33:22 +01002217 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
2219 if (val == -1)
2220 return 0;
2221 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002222 if (!val) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
2224 if (val == -1)
2225 return 0;
2226 }
2227
2228 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02002229 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02002230 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 if (val & (1 << i))
2232 break;
2233 return 0;
2234 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02002235 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 return 0;
2237
2238 stream = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
2239 if (stream == -1)
2240 return 0;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002241 if (!stream && nid != codec->afg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 stream = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002243 if (!stream || stream == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 return 0;
2245
2246 if (stream & AC_SUPFMT_PCM) {
2247 switch (format & 0xf0) {
2248 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002249 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 return 0;
2251 break;
2252 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002253 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 return 0;
2255 break;
2256 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002257 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 return 0;
2259 break;
2260 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002261 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 return 0;
2263 break;
2264 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002265 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 return 0;
2267 break;
2268 default:
2269 return 0;
2270 }
2271 } else {
2272 /* FIXME: check for float32 and AC3? */
2273 }
2274
2275 return 1;
2276}
2277
2278/*
2279 * PCM stuff
2280 */
2281static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
2282 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002283 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284{
2285 return 0;
2286}
2287
2288static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
2289 struct hda_codec *codec,
2290 unsigned int stream_tag,
2291 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002292 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293{
2294 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
2295 return 0;
2296}
2297
2298static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
2299 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002300 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301{
Takashi Iwai888afa12008-03-18 09:57:50 +01002302 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 return 0;
2304}
2305
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002306static int set_pcm_default_values(struct hda_codec *codec,
2307 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308{
Takashi Iwai0ba21762007-04-16 11:29:14 +02002309 /* query support PCM information from the given NID */
2310 if (info->nid && (!info->rates || !info->formats)) {
2311 snd_hda_query_supported_pcm(codec, info->nid,
2312 info->rates ? NULL : &info->rates,
2313 info->formats ? NULL : &info->formats,
2314 info->maxbps ? NULL : &info->maxbps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 }
2316 if (info->ops.open == NULL)
2317 info->ops.open = hda_pcm_default_open_close;
2318 if (info->ops.close == NULL)
2319 info->ops.close = hda_pcm_default_open_close;
2320 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02002321 if (snd_BUG_ON(!info->nid))
2322 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 info->ops.prepare = hda_pcm_default_prepare;
2324 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02002326 if (snd_BUG_ON(!info->nid))
2327 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 info->ops.cleanup = hda_pcm_default_cleanup;
2329 }
2330 return 0;
2331}
2332
Takashi Iwai176d5332008-07-30 15:01:44 +02002333/*
2334 * attach a new PCM stream
2335 */
2336static int __devinit
2337snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
2338{
2339 struct hda_pcm_stream *info;
2340 int stream, err;
2341
2342 if (!pcm->name)
2343 return -EINVAL;
2344 for (stream = 0; stream < 2; stream++) {
2345 info = &pcm->stream[stream];
2346 if (info->substreams) {
2347 err = set_pcm_default_values(codec, info);
2348 if (err < 0)
2349 return err;
2350 }
2351 }
2352 return codec->bus->ops.attach_pcm(codec, pcm);
2353}
2354
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355/**
2356 * snd_hda_build_pcms - build PCM information
2357 * @bus: the BUS
2358 *
2359 * Create PCM information for each codec included in the bus.
2360 *
2361 * The build_pcms codec patch is requested to set up codec->num_pcms and
2362 * codec->pcm_info properly. The array is referred by the top-level driver
2363 * to create its PCM instances.
2364 * The allocated codec->pcm_info should be released in codec->patch_ops.free
2365 * callback.
2366 *
2367 * At least, substreams, channels_min and channels_max must be filled for
2368 * each stream. substreams = 0 indicates that the stream doesn't exist.
2369 * When rates and/or formats are zero, the supported values are queried
2370 * from the given nid. The nid is used also by the default ops.prepare
2371 * and ops.cleanup callbacks.
2372 *
2373 * The driver needs to call ops.open in its open callback. Similarly,
2374 * ops.close is supposed to be called in the close callback.
2375 * ops.prepare should be called in the prepare or hw_params callback
2376 * with the proper parameters for set up.
2377 * ops.cleanup should be called in hw_free for clean up of streams.
2378 *
2379 * This function returns 0 if successfull, or a negative error code.
2380 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002381int snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382{
Takashi Iwai176d5332008-07-30 15:01:44 +02002383 static const char *dev_name[HDA_PCM_NTYPES] = {
2384 "Audio", "SPDIF", "HDMI", "Modem"
2385 };
2386 /* starting device index for each PCM type */
2387 static int dev_idx[HDA_PCM_NTYPES] = {
2388 [HDA_PCM_TYPE_AUDIO] = 0,
2389 [HDA_PCM_TYPE_SPDIF] = 1,
2390 [HDA_PCM_TYPE_HDMI] = 3,
2391 [HDA_PCM_TYPE_MODEM] = 6
2392 };
2393 /* normal audio device indices; not linear to keep compatibility */
2394 static int audio_idx[4] = { 0, 2, 4, 5 };
Takashi Iwai0ba21762007-04-16 11:29:14 +02002395 struct hda_codec *codec;
Takashi Iwai176d5332008-07-30 15:01:44 +02002396 int num_devs[HDA_PCM_NTYPES];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397
Takashi Iwai176d5332008-07-30 15:01:44 +02002398 memset(num_devs, 0, sizeof(num_devs));
Takashi Iwai0ba21762007-04-16 11:29:14 +02002399 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai176d5332008-07-30 15:01:44 +02002400 unsigned int pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 int err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002402 if (!codec->num_pcms) {
2403 if (!codec->patch_ops.build_pcms)
2404 continue;
2405 err = codec->patch_ops.build_pcms(codec);
2406 if (err < 0)
2407 return err;
2408 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
Takashi Iwai176d5332008-07-30 15:01:44 +02002410 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2411 int type = cpcm->pcm_type;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002412 int dev;
Takashi Iwai176d5332008-07-30 15:01:44 +02002413 switch (type) {
2414 case HDA_PCM_TYPE_AUDIO:
2415 if (num_devs[type] >= ARRAY_SIZE(audio_idx)) {
2416 snd_printk(KERN_WARNING
2417 "Too many audio devices\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 continue;
Takashi Iwai176d5332008-07-30 15:01:44 +02002419 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002420 dev = audio_idx[num_devs[type]];
Takashi Iwai176d5332008-07-30 15:01:44 +02002421 break;
2422 case HDA_PCM_TYPE_SPDIF:
2423 case HDA_PCM_TYPE_HDMI:
2424 case HDA_PCM_TYPE_MODEM:
2425 if (num_devs[type]) {
2426 snd_printk(KERN_WARNING
2427 "%s already defined\n",
2428 dev_name[type]);
2429 continue;
2430 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002431 dev = dev_idx[type];
Takashi Iwai176d5332008-07-30 15:01:44 +02002432 break;
2433 default:
2434 snd_printk(KERN_WARNING
2435 "Invalid PCM type %d\n", type);
2436 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 }
Takashi Iwai176d5332008-07-30 15:01:44 +02002438 num_devs[type]++;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002439 if (!cpcm->pcm) {
2440 cpcm->device = dev;
2441 err = snd_hda_attach_pcm(codec, cpcm);
2442 if (err < 0)
2443 return err;
2444 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 }
2446 }
2447 return 0;
2448}
2449
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450/**
2451 * snd_hda_check_board_config - compare the current codec with the config table
2452 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002453 * @num_configs: number of config enums
2454 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 * @tbl: configuration table, terminated by null entries
2456 *
2457 * Compares the modelname or PCI subsystem id of the current codec with the
2458 * given configuration table. If a matching entry is found, returns its
2459 * config value (supposed to be 0 or positive).
2460 *
2461 * If no entries are matching, the function returns a negative value.
2462 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002463int snd_hda_check_board_config(struct hda_codec *codec,
2464 int num_configs, const char **models,
2465 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466{
Takashi Iwaif44ac832008-07-30 15:01:45 +02002467 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002468 int i;
2469 for (i = 0; i < num_configs; i++) {
2470 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02002471 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002472 snd_printd(KERN_INFO "hda_codec: model '%s' is "
2473 "selected\n", models[i]);
2474 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 }
2476 }
2477 }
2478
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002479 if (!codec->bus->pci || !tbl)
2480 return -1;
2481
2482 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
2483 if (!tbl)
2484 return -1;
2485 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02002486#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002487 char tmp[10];
2488 const char *model = NULL;
2489 if (models)
2490 model = models[tbl->value];
2491 if (!model) {
2492 sprintf(tmp, "#%d", tbl->value);
2493 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002495 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
2496 "for config %x:%x (%s)\n",
2497 model, tbl->subvendor, tbl->subdevice,
2498 (tbl->name ? tbl->name : "Unknown device"));
2499#endif
2500 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 }
2502 return -1;
2503}
2504
2505/**
2506 * snd_hda_add_new_ctls - create controls from the array
2507 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002508 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 *
2510 * This helper function creates and add new controls in the given array.
2511 * The array must be terminated with an empty entry as terminator.
2512 *
2513 * Returns 0 if successful, or a negative error code.
2514 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002515int snd_hda_add_new_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516{
Takashi Iwaicb53c622007-08-10 17:21:45 +02002517 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518
2519 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01002520 struct snd_kcontrol *kctl;
2521 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002522 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01002523 return -ENOMEM;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002524 err = snd_hda_ctl_add(codec, kctl);
Takashi Iwai54d17402005-11-21 16:33:22 +01002525 if (err < 0) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002526 if (!codec->addr)
Takashi Iwai54d17402005-11-21 16:33:22 +01002527 return err;
2528 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002529 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01002530 return -ENOMEM;
2531 kctl->id.device = codec->addr;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002532 err = snd_hda_ctl_add(codec, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002533 if (err < 0)
Takashi Iwai54d17402005-11-21 16:33:22 +01002534 return err;
2535 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 }
2537 return 0;
2538}
2539
Takashi Iwaicb53c622007-08-10 17:21:45 +02002540#ifdef CONFIG_SND_HDA_POWER_SAVE
2541static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2542 unsigned int power_state);
2543
2544static void hda_power_work(struct work_struct *work)
2545{
2546 struct hda_codec *codec =
2547 container_of(work, struct hda_codec, power_work.work);
2548
Maxim Levitsky2e492462007-09-03 15:26:57 +02002549 if (!codec->power_on || codec->power_count) {
2550 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002551 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02002552 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02002553
2554 hda_call_codec_suspend(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002555 if (codec->bus->ops.pm_notify)
2556 codec->bus->ops.pm_notify(codec);
2557}
2558
2559static void hda_keep_power_on(struct hda_codec *codec)
2560{
2561 codec->power_count++;
2562 codec->power_on = 1;
2563}
2564
2565void snd_hda_power_up(struct hda_codec *codec)
2566{
2567 codec->power_count++;
Takashi Iwaia221e282007-08-16 16:35:33 +02002568 if (codec->power_on || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02002569 return;
2570
2571 codec->power_on = 1;
2572 if (codec->bus->ops.pm_notify)
2573 codec->bus->ops.pm_notify(codec);
2574 hda_call_codec_resume(codec);
2575 cancel_delayed_work(&codec->power_work);
Takashi Iwaia221e282007-08-16 16:35:33 +02002576 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002577}
2578
2579void snd_hda_power_down(struct hda_codec *codec)
2580{
2581 --codec->power_count;
Takashi Iwaia221e282007-08-16 16:35:33 +02002582 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02002583 return;
Takashi Iwaia221e282007-08-16 16:35:33 +02002584 if (power_save) {
2585 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002586 schedule_delayed_work(&codec->power_work,
2587 msecs_to_jiffies(power_save * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02002588 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02002589}
2590
2591int snd_hda_check_amp_list_power(struct hda_codec *codec,
2592 struct hda_loopback_check *check,
2593 hda_nid_t nid)
2594{
2595 struct hda_amp_list *p;
2596 int ch, v;
2597
2598 if (!check->amplist)
2599 return 0;
2600 for (p = check->amplist; p->nid; p++) {
2601 if (p->nid == nid)
2602 break;
2603 }
2604 if (!p->nid)
2605 return 0; /* nothing changed */
2606
2607 for (p = check->amplist; p->nid; p++) {
2608 for (ch = 0; ch < 2; ch++) {
2609 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
2610 p->idx);
2611 if (!(v & HDA_AMP_MUTE) && v > 0) {
2612 if (!check->power_on) {
2613 check->power_on = 1;
2614 snd_hda_power_up(codec);
2615 }
2616 return 1;
2617 }
2618 }
2619 }
2620 if (check->power_on) {
2621 check->power_on = 0;
2622 snd_hda_power_down(codec);
2623 }
2624 return 0;
2625}
2626#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002628/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002629 * Channel mode helper
2630 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002631int snd_hda_ch_mode_info(struct hda_codec *codec,
2632 struct snd_ctl_elem_info *uinfo,
2633 const struct hda_channel_mode *chmode,
2634 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002635{
2636 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2637 uinfo->count = 1;
2638 uinfo->value.enumerated.items = num_chmodes;
2639 if (uinfo->value.enumerated.item >= num_chmodes)
2640 uinfo->value.enumerated.item = num_chmodes - 1;
2641 sprintf(uinfo->value.enumerated.name, "%dch",
2642 chmode[uinfo->value.enumerated.item].channels);
2643 return 0;
2644}
2645
Takashi Iwai0ba21762007-04-16 11:29:14 +02002646int snd_hda_ch_mode_get(struct hda_codec *codec,
2647 struct snd_ctl_elem_value *ucontrol,
2648 const struct hda_channel_mode *chmode,
2649 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002650 int max_channels)
2651{
2652 int i;
2653
2654 for (i = 0; i < num_chmodes; i++) {
2655 if (max_channels == chmode[i].channels) {
2656 ucontrol->value.enumerated.item[0] = i;
2657 break;
2658 }
2659 }
2660 return 0;
2661}
2662
Takashi Iwai0ba21762007-04-16 11:29:14 +02002663int snd_hda_ch_mode_put(struct hda_codec *codec,
2664 struct snd_ctl_elem_value *ucontrol,
2665 const struct hda_channel_mode *chmode,
2666 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002667 int *max_channelsp)
2668{
2669 unsigned int mode;
2670
2671 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01002672 if (mode >= num_chmodes)
2673 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002674 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002675 return 0;
2676 /* change the current channel setting */
2677 *max_channelsp = chmode[mode].channels;
2678 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002679 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002680 return 1;
2681}
2682
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683/*
2684 * input MUX helper
2685 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002686int snd_hda_input_mux_info(const struct hda_input_mux *imux,
2687 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688{
2689 unsigned int index;
2690
2691 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2692 uinfo->count = 1;
2693 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02002694 if (!imux->num_items)
2695 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 index = uinfo->value.enumerated.item;
2697 if (index >= imux->num_items)
2698 index = imux->num_items - 1;
2699 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
2700 return 0;
2701}
2702
Takashi Iwai0ba21762007-04-16 11:29:14 +02002703int snd_hda_input_mux_put(struct hda_codec *codec,
2704 const struct hda_input_mux *imux,
2705 struct snd_ctl_elem_value *ucontrol,
2706 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 unsigned int *cur_val)
2708{
2709 unsigned int idx;
2710
Takashi Iwai5513b0c2007-10-09 11:58:41 +02002711 if (!imux->num_items)
2712 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 idx = ucontrol->value.enumerated.item[0];
2714 if (idx >= imux->num_items)
2715 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002716 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002718 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
2719 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 *cur_val = idx;
2721 return 1;
2722}
2723
2724
2725/*
2726 * Multi-channel / digital-out PCM helper functions
2727 */
2728
Takashi Iwai6b97eb42007-04-05 14:51:48 +02002729/* setup SPDIF output stream */
2730static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
2731 unsigned int stream_tag, unsigned int format)
2732{
2733 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Takashi Iwai2f728532008-09-25 16:32:41 +02002734 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
2735 set_dig_out_convert(codec, nid,
2736 codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff,
2737 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02002738 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02002739 if (codec->slave_dig_outs) {
2740 hda_nid_t *d;
2741 for (d = codec->slave_dig_outs; *d; d++)
2742 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
2743 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04002744 }
Takashi Iwai2f728532008-09-25 16:32:41 +02002745 /* turn on again (if needed) */
2746 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
2747 set_dig_out_convert(codec, nid,
2748 codec->spdif_ctls & 0xff, -1);
2749}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04002750
Takashi Iwai2f728532008-09-25 16:32:41 +02002751static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
2752{
2753 snd_hda_codec_cleanup_stream(codec, nid);
2754 if (codec->slave_dig_outs) {
2755 hda_nid_t *d;
2756 for (d = codec->slave_dig_outs; *d; d++)
2757 snd_hda_codec_cleanup_stream(codec, *d);
2758 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02002759}
2760
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761/*
2762 * open the digital out in the exclusive mode
2763 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002764int snd_hda_multi_out_dig_open(struct hda_codec *codec,
2765 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766{
Ingo Molnar62932df2006-01-16 16:34:20 +01002767 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02002768 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
2769 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02002770 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01002772 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 return 0;
2774}
2775
Takashi Iwai6b97eb42007-04-05 14:51:48 +02002776int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
2777 struct hda_multi_out *mout,
2778 unsigned int stream_tag,
2779 unsigned int format,
2780 struct snd_pcm_substream *substream)
2781{
2782 mutex_lock(&codec->spdif_mutex);
2783 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
2784 mutex_unlock(&codec->spdif_mutex);
2785 return 0;
2786}
2787
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788/*
2789 * release the digital out
2790 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002791int snd_hda_multi_out_dig_close(struct hda_codec *codec,
2792 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793{
Ingo Molnar62932df2006-01-16 16:34:20 +01002794 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01002796 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 return 0;
2798}
2799
2800/*
2801 * set up more restrictions for analog out
2802 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002803int snd_hda_multi_out_analog_open(struct hda_codec *codec,
2804 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01002805 struct snd_pcm_substream *substream,
2806 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807{
Takashi Iwai9a081602008-02-12 18:37:26 +01002808 struct snd_pcm_runtime *runtime = substream->runtime;
2809 runtime->hw.channels_max = mout->max_channels;
2810 if (mout->dig_out_nid) {
2811 if (!mout->analog_rates) {
2812 mout->analog_rates = hinfo->rates;
2813 mout->analog_formats = hinfo->formats;
2814 mout->analog_maxbps = hinfo->maxbps;
2815 } else {
2816 runtime->hw.rates = mout->analog_rates;
2817 runtime->hw.formats = mout->analog_formats;
2818 hinfo->maxbps = mout->analog_maxbps;
2819 }
2820 if (!mout->spdif_rates) {
2821 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
2822 &mout->spdif_rates,
2823 &mout->spdif_formats,
2824 &mout->spdif_maxbps);
2825 }
2826 mutex_lock(&codec->spdif_mutex);
2827 if (mout->share_spdif) {
2828 runtime->hw.rates &= mout->spdif_rates;
2829 runtime->hw.formats &= mout->spdif_formats;
2830 if (mout->spdif_maxbps < hinfo->maxbps)
2831 hinfo->maxbps = mout->spdif_maxbps;
2832 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02002833 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01002834 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 return snd_pcm_hw_constraint_step(substream->runtime, 0,
2836 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
2837}
2838
2839/*
2840 * set up the i/o for analog out
2841 * when the digital out is available, copy the front out to digital out, too.
2842 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002843int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
2844 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 unsigned int stream_tag,
2846 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002847 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848{
2849 hda_nid_t *nids = mout->dac_nids;
2850 int chs = substream->runtime->channels;
2851 int i;
2852
Ingo Molnar62932df2006-01-16 16:34:20 +01002853 mutex_lock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01002854 if (mout->dig_out_nid && mout->share_spdif &&
2855 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02002857 snd_hda_is_supported_format(codec, mout->dig_out_nid,
2858 format) &&
2859 !(codec->spdif_status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02002861 setup_dig_out_stream(codec, mout->dig_out_nid,
2862 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 } else {
2864 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02002865 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 }
2867 }
Ingo Molnar62932df2006-01-16 16:34:20 +01002868 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869
2870 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002871 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
2872 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02002873 if (!mout->no_share_stream &&
2874 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002876 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
2877 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01002878 /* extra outputs copied from front */
2879 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02002880 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01002881 snd_hda_codec_setup_stream(codec,
2882 mout->extra_out_nid[i],
2883 stream_tag, 0, format);
2884
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 /* surrounds */
2886 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02002887 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002888 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
2889 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02002890 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002891 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
2892 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 }
2894 return 0;
2895}
2896
2897/*
2898 * clean up the setting for analog out
2899 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002900int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
2901 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902{
2903 hda_nid_t *nids = mout->dac_nids;
2904 int i;
2905
2906 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01002907 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01002909 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwai82bc9552006-03-21 11:24:42 +01002910 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
2911 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01002912 snd_hda_codec_cleanup_stream(codec,
2913 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01002914 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02002916 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 mout->dig_out_used = 0;
2918 }
Ingo Molnar62932df2006-01-16 16:34:20 +01002919 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 return 0;
2921}
2922
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002923/*
Wu Fengguang6b345002008-10-07 14:21:41 +08002924 * Helper for automatic pin configuration
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002925 */
Kailang Yangdf694da2005-12-05 19:42:22 +01002926
Takashi Iwai12f288b2007-08-02 15:51:59 +02002927static int is_in_nid_list(hda_nid_t nid, hda_nid_t *list)
Kailang Yangdf694da2005-12-05 19:42:22 +01002928{
2929 for (; *list; list++)
2930 if (*list == nid)
2931 return 1;
2932 return 0;
2933}
2934
Steve Longerbeam81937d32007-05-08 15:33:03 +02002935
2936/*
2937 * Sort an associated group of pins according to their sequence numbers.
2938 */
2939static void sort_pins_by_sequence(hda_nid_t * pins, short * sequences,
2940 int num_pins)
2941{
2942 int i, j;
2943 short seq;
2944 hda_nid_t nid;
2945
2946 for (i = 0; i < num_pins; i++) {
2947 for (j = i + 1; j < num_pins; j++) {
2948 if (sequences[i] > sequences[j]) {
2949 seq = sequences[i];
2950 sequences[i] = sequences[j];
2951 sequences[j] = seq;
2952 nid = pins[i];
2953 pins[i] = pins[j];
2954 pins[j] = nid;
2955 }
2956 }
2957 }
2958}
2959
2960
Takashi Iwai82bc9552006-03-21 11:24:42 +01002961/*
2962 * Parse all pin widgets and store the useful pin nids to cfg
2963 *
2964 * The number of line-outs or any primary output is stored in line_outs,
2965 * and the corresponding output pins are assigned to line_out_pins[],
2966 * in the order of front, rear, CLFE, side, ...
2967 *
2968 * If more extra outputs (speaker and headphone) are found, the pins are
Takashi Iwaieb06ed82006-09-20 17:10:27 +02002969 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
Takashi Iwai82bc9552006-03-21 11:24:42 +01002970 * is detected, one of speaker of HP pins is assigned as the primary
2971 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
2972 * if any analog output exists.
2973 *
2974 * The analog input pins are assigned to input_pins array.
2975 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
2976 * respectively.
2977 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002978int snd_hda_parse_pin_def_config(struct hda_codec *codec,
2979 struct auto_pin_cfg *cfg,
2980 hda_nid_t *ignore_nids)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002981{
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01002982 hda_nid_t nid, end_nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02002983 short seq, assoc_line_out, assoc_speaker;
2984 short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
2985 short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
Takashi Iwaif889fa92007-10-31 15:49:32 +01002986 short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002987
2988 memset(cfg, 0, sizeof(*cfg));
2989
Steve Longerbeam81937d32007-05-08 15:33:03 +02002990 memset(sequences_line_out, 0, sizeof(sequences_line_out));
2991 memset(sequences_speaker, 0, sizeof(sequences_speaker));
Takashi Iwaif889fa92007-10-31 15:49:32 +01002992 memset(sequences_hp, 0, sizeof(sequences_hp));
Steve Longerbeam81937d32007-05-08 15:33:03 +02002993 assoc_line_out = assoc_speaker = 0;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002994
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01002995 end_nid = codec->start_nid + codec->num_nodes;
2996 for (nid = codec->start_nid; nid < end_nid; nid++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01002997 unsigned int wid_caps = get_wcaps(codec, nid);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002998 unsigned int wid_type =
2999 (wid_caps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003000 unsigned int def_conf;
3001 short assoc, loc;
3002
3003 /* read all default configuration for pin complex */
3004 if (wid_type != AC_WID_PIN)
3005 continue;
Kailang Yangdf694da2005-12-05 19:42:22 +01003006 /* ignore the given nids (e.g. pc-beep returns error) */
3007 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
3008 continue;
3009
Takashi Iwai0ba21762007-04-16 11:29:14 +02003010 def_conf = snd_hda_codec_read(codec, nid, 0,
3011 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003012 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
3013 continue;
3014 loc = get_defcfg_location(def_conf);
3015 switch (get_defcfg_device(def_conf)) {
3016 case AC_JACK_LINE_OUT:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003017 seq = get_defcfg_sequence(def_conf);
3018 assoc = get_defcfg_association(def_conf);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01003019
3020 if (!(wid_caps & AC_WCAP_STEREO))
3021 if (!cfg->mono_out_pin)
3022 cfg->mono_out_pin = nid;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003023 if (!assoc)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003024 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003025 if (!assoc_line_out)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003026 assoc_line_out = assoc;
3027 else if (assoc_line_out != assoc)
3028 continue;
3029 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
3030 continue;
3031 cfg->line_out_pins[cfg->line_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02003032 sequences_line_out[cfg->line_outs] = seq;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003033 cfg->line_outs++;
3034 break;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01003035 case AC_JACK_SPEAKER:
Steve Longerbeam81937d32007-05-08 15:33:03 +02003036 seq = get_defcfg_sequence(def_conf);
3037 assoc = get_defcfg_association(def_conf);
3038 if (! assoc)
3039 continue;
3040 if (! assoc_speaker)
3041 assoc_speaker = assoc;
3042 else if (assoc_speaker != assoc)
3043 continue;
Takashi Iwai82bc9552006-03-21 11:24:42 +01003044 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
3045 continue;
3046 cfg->speaker_pins[cfg->speaker_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02003047 sequences_speaker[cfg->speaker_outs] = seq;
Takashi Iwai82bc9552006-03-21 11:24:42 +01003048 cfg->speaker_outs++;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01003049 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003050 case AC_JACK_HP_OUT:
Takashi Iwaif889fa92007-10-31 15:49:32 +01003051 seq = get_defcfg_sequence(def_conf);
3052 assoc = get_defcfg_association(def_conf);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02003053 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
3054 continue;
3055 cfg->hp_pins[cfg->hp_outs] = nid;
Takashi Iwaif889fa92007-10-31 15:49:32 +01003056 sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
Takashi Iwaieb06ed82006-09-20 17:10:27 +02003057 cfg->hp_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003058 break;
Takashi Iwai314634b2006-09-21 11:56:18 +02003059 case AC_JACK_MIC_IN: {
3060 int preferred, alt;
3061 if (loc == AC_JACK_LOC_FRONT) {
3062 preferred = AUTO_PIN_FRONT_MIC;
3063 alt = AUTO_PIN_MIC;
3064 } else {
3065 preferred = AUTO_PIN_MIC;
3066 alt = AUTO_PIN_FRONT_MIC;
3067 }
3068 if (!cfg->input_pins[preferred])
3069 cfg->input_pins[preferred] = nid;
3070 else if (!cfg->input_pins[alt])
3071 cfg->input_pins[alt] = nid;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003072 break;
Takashi Iwai314634b2006-09-21 11:56:18 +02003073 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003074 case AC_JACK_LINE_IN:
3075 if (loc == AC_JACK_LOC_FRONT)
3076 cfg->input_pins[AUTO_PIN_FRONT_LINE] = nid;
3077 else
3078 cfg->input_pins[AUTO_PIN_LINE] = nid;
3079 break;
3080 case AC_JACK_CD:
3081 cfg->input_pins[AUTO_PIN_CD] = nid;
3082 break;
3083 case AC_JACK_AUX:
3084 cfg->input_pins[AUTO_PIN_AUX] = nid;
3085 break;
3086 case AC_JACK_SPDIF_OUT:
3087 cfg->dig_out_pin = nid;
3088 break;
3089 case AC_JACK_SPDIF_IN:
3090 cfg->dig_in_pin = nid;
3091 break;
3092 }
3093 }
3094
Takashi Iwai5832fcf2008-02-12 18:30:12 +01003095 /* FIX-UP:
3096 * If no line-out is defined but multiple HPs are found,
3097 * some of them might be the real line-outs.
3098 */
3099 if (!cfg->line_outs && cfg->hp_outs > 1) {
3100 int i = 0;
3101 while (i < cfg->hp_outs) {
3102 /* The real HPs should have the sequence 0x0f */
3103 if ((sequences_hp[i] & 0x0f) == 0x0f) {
3104 i++;
3105 continue;
3106 }
3107 /* Move it to the line-out table */
3108 cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
3109 sequences_line_out[cfg->line_outs] = sequences_hp[i];
3110 cfg->line_outs++;
3111 cfg->hp_outs--;
3112 memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
3113 sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
3114 memmove(sequences_hp + i - 1, sequences_hp + i,
3115 sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
3116 }
3117 }
3118
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003119 /* sort by sequence */
Steve Longerbeam81937d32007-05-08 15:33:03 +02003120 sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
3121 cfg->line_outs);
3122 sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
3123 cfg->speaker_outs);
Takashi Iwaif889fa92007-10-31 15:49:32 +01003124 sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
3125 cfg->hp_outs);
Steve Longerbeam81937d32007-05-08 15:33:03 +02003126
Takashi Iwaif889fa92007-10-31 15:49:32 +01003127 /* if we have only one mic, make it AUTO_PIN_MIC */
3128 if (!cfg->input_pins[AUTO_PIN_MIC] &&
3129 cfg->input_pins[AUTO_PIN_FRONT_MIC]) {
3130 cfg->input_pins[AUTO_PIN_MIC] =
3131 cfg->input_pins[AUTO_PIN_FRONT_MIC];
3132 cfg->input_pins[AUTO_PIN_FRONT_MIC] = 0;
3133 }
3134 /* ditto for line-in */
3135 if (!cfg->input_pins[AUTO_PIN_LINE] &&
3136 cfg->input_pins[AUTO_PIN_FRONT_LINE]) {
3137 cfg->input_pins[AUTO_PIN_LINE] =
3138 cfg->input_pins[AUTO_PIN_FRONT_LINE];
3139 cfg->input_pins[AUTO_PIN_FRONT_LINE] = 0;
3140 }
3141
Steve Longerbeam81937d32007-05-08 15:33:03 +02003142 /*
3143 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
3144 * as a primary output
3145 */
3146 if (!cfg->line_outs) {
3147 if (cfg->speaker_outs) {
3148 cfg->line_outs = cfg->speaker_outs;
3149 memcpy(cfg->line_out_pins, cfg->speaker_pins,
3150 sizeof(cfg->speaker_pins));
3151 cfg->speaker_outs = 0;
3152 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
3153 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
3154 } else if (cfg->hp_outs) {
3155 cfg->line_outs = cfg->hp_outs;
3156 memcpy(cfg->line_out_pins, cfg->hp_pins,
3157 sizeof(cfg->hp_pins));
3158 cfg->hp_outs = 0;
3159 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
3160 cfg->line_out_type = AUTO_PIN_HP_OUT;
3161 }
3162 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003163
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02003164 /* Reorder the surround channels
3165 * ALSA sequence is front/surr/clfe/side
3166 * HDA sequence is:
3167 * 4-ch: front/surr => OK as it is
3168 * 6-ch: front/clfe/surr
Takashi Iwai9422db42007-04-20 16:11:43 +02003169 * 8-ch: front/clfe/rear/side|fc
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02003170 */
3171 switch (cfg->line_outs) {
3172 case 3:
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02003173 case 4:
3174 nid = cfg->line_out_pins[1];
Takashi Iwai9422db42007-04-20 16:11:43 +02003175 cfg->line_out_pins[1] = cfg->line_out_pins[2];
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02003176 cfg->line_out_pins[2] = nid;
3177 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003178 }
3179
Takashi Iwai82bc9552006-03-21 11:24:42 +01003180 /*
3181 * debug prints of the parsed results
3182 */
3183 snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3184 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
3185 cfg->line_out_pins[2], cfg->line_out_pins[3],
3186 cfg->line_out_pins[4]);
3187 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3188 cfg->speaker_outs, cfg->speaker_pins[0],
3189 cfg->speaker_pins[1], cfg->speaker_pins[2],
3190 cfg->speaker_pins[3], cfg->speaker_pins[4]);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02003191 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3192 cfg->hp_outs, cfg->hp_pins[0],
3193 cfg->hp_pins[1], cfg->hp_pins[2],
3194 cfg->hp_pins[3], cfg->hp_pins[4]);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01003195 snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
Takashi Iwai82bc9552006-03-21 11:24:42 +01003196 snd_printd(" inputs: mic=0x%x, fmic=0x%x, line=0x%x, fline=0x%x,"
3197 " cd=0x%x, aux=0x%x\n",
3198 cfg->input_pins[AUTO_PIN_MIC],
3199 cfg->input_pins[AUTO_PIN_FRONT_MIC],
3200 cfg->input_pins[AUTO_PIN_LINE],
3201 cfg->input_pins[AUTO_PIN_FRONT_LINE],
3202 cfg->input_pins[AUTO_PIN_CD],
3203 cfg->input_pins[AUTO_PIN_AUX]);
3204
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003205 return 0;
3206}
3207
Takashi Iwai4a471b72005-12-07 13:56:29 +01003208/* labels for input pins */
3209const char *auto_pin_cfg_labels[AUTO_PIN_LAST] = {
3210 "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux"
3211};
3212
3213
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214#ifdef CONFIG_PM
3215/*
3216 * power management
3217 */
3218
3219/**
3220 * snd_hda_suspend - suspend the codecs
3221 * @bus: the HDA bus
3222 * @state: suspsend state
3223 *
3224 * Returns 0 if successful.
3225 */
3226int snd_hda_suspend(struct hda_bus *bus, pm_message_t state)
3227{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003228 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229
Takashi Iwai0ba21762007-04-16 11:29:14 +02003230 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai0b7a2e92007-08-14 15:18:26 +02003231#ifdef CONFIG_SND_HDA_POWER_SAVE
3232 if (!codec->power_on)
3233 continue;
3234#endif
Takashi Iwaicb53c622007-08-10 17:21:45 +02003235 hda_call_codec_suspend(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 }
3237 return 0;
3238}
3239
3240/**
3241 * snd_hda_resume - resume the codecs
3242 * @bus: the HDA bus
3243 * @state: resume state
3244 *
3245 * Returns 0 if successful.
Takashi Iwaicb53c622007-08-10 17:21:45 +02003246 *
3247 * This fucntion is defined only when POWER_SAVE isn't set.
3248 * In the power-save mode, the codec is resumed dynamically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 */
3250int snd_hda_resume(struct hda_bus *bus)
3251{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003252 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253
Takashi Iwai0ba21762007-04-16 11:29:14 +02003254 list_for_each_entry(codec, &bus->codec_list, list) {
Maxim Levitskyd804ad92007-09-03 15:28:04 +02003255 if (snd_hda_codec_needs_resume(codec))
3256 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258 return 0;
3259}
Maxim Levitskyd804ad92007-09-03 15:28:04 +02003260#ifdef CONFIG_SND_HDA_POWER_SAVE
3261int snd_hda_codecs_inuse(struct hda_bus *bus)
3262{
3263 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264
Maxim Levitskyd804ad92007-09-03 15:28:04 +02003265 list_for_each_entry(codec, &bus->codec_list, list) {
3266 if (snd_hda_codec_needs_resume(codec))
3267 return 1;
3268 }
3269 return 0;
3270}
3271#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272#endif
Takashi Iwaib2e18592008-07-30 15:01:44 +02003273
3274/*
3275 * generic arrays
3276 */
3277
3278/* get a new element from the given array
3279 * if it exceeds the pre-allocated array size, re-allocate the array
3280 */
3281void *snd_array_new(struct snd_array *array)
3282{
3283 if (array->used >= array->alloced) {
3284 int num = array->alloced + array->alloc_align;
3285 void *nlist = kcalloc(num + 1, array->elem_size, GFP_KERNEL);
3286 if (!nlist)
3287 return NULL;
3288 if (array->list) {
3289 memcpy(nlist, array->list,
3290 array->elem_size * array->alloced);
3291 kfree(array->list);
3292 }
3293 array->list = nlist;
3294 array->alloced = num;
3295 }
3296 return array->list + (array->used++ * array->elem_size);
3297}
3298
3299/* free the given array elements */
3300void snd_array_free(struct snd_array *array)
3301{
3302 kfree(array->list);
3303 array->used = 0;
3304 array->alloced = 0;
3305 array->list = NULL;
3306}