blob: 696d77e575ec7765e963a10be20dc479347ec688 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Universal Interface for Intel High Definition Audio Codec
3 *
4 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
5 *
6 *
7 * This driver is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This driver is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/init.h>
23#include <linux/delay.h>
24#include <linux/slab.h>
25#include <linux/pci.h>
Ingo Molnar62932df2006-01-16 16:34:20 +010026#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <sound/core.h>
28#include "hda_codec.h"
29#include <sound/asoundef.h>
Jaroslav Kysela302e9c52006-07-05 17:39:49 +020030#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <sound/initval.h>
32#include "hda_local.h"
Takashi Iwai28073142007-07-27 18:58:06 +020033#include <sound/hda_hwdep.h>
Harvey Harrison3c9a3202008-02-29 11:59:26 +010034#include "hda_patch.h" /* codec presets */
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Takashi Iwaicb53c622007-08-10 17:21:45 +020036#ifdef CONFIG_SND_HDA_POWER_SAVE
37/* define this option here to hide as static */
Takashi Iwai7a5a27c2007-09-17 19:07:46 +020038static int power_save = CONFIG_SND_HDA_POWER_SAVE_DEFAULT;
Takashi Iwaicb53c622007-08-10 17:21:45 +020039module_param(power_save, int, 0644);
40MODULE_PARM_DESC(power_save, "Automatic power-saving timeout "
41 "(in second, 0 = disable).");
42#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Linus Torvalds1da177e2005-04-16 15:20:36 -070044/*
45 * vendor / preset table
46 */
47
48struct hda_vendor_id {
49 unsigned int id;
50 const char *name;
51};
52
53/* codec vendor labels */
54static struct hda_vendor_id hda_vendor_ids[] = {
Takashi Iwaic8cd1282008-02-13 16:59:29 +010055 { 0x1002, "ATI" },
Takashi Iwaia9226252006-09-17 22:05:54 +020056 { 0x1057, "Motorola" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010057 { 0x1095, "Silicon Image" },
58 { 0x10ec, "Realtek" },
Joseph Chanc577b8a2006-11-29 15:29:40 +010059 { 0x1106, "VIA" },
Matthew Ranostay7f168592007-10-18 17:38:17 +020060 { 0x111d, "IDT" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010061 { 0x11c1, "LSI" },
Takashi Iwai54b903e2005-05-15 14:30:10 +020062 { 0x11d4, "Analog Devices" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 { 0x13f6, "C-Media" },
Takashi Iwaia9226252006-09-17 22:05:54 +020064 { 0x14f1, "Conexant" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010065 { 0x17e8, "Chrontel" },
66 { 0x1854, "LG" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 { 0x434d, "C-Media" },
Matt2f2f4252005-04-13 14:45:30 +020068 { 0x8384, "SigmaTel" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 {} /* terminator */
70};
71
Harvey Harrison3c9a3202008-02-29 11:59:26 +010072static const struct hda_codec_preset *hda_preset_tables[] = {
73#ifdef CONFIG_SND_HDA_CODEC_REALTEK
74 snd_hda_preset_realtek,
75#endif
76#ifdef CONFIG_SND_HDA_CODEC_CMEDIA
77 snd_hda_preset_cmedia,
78#endif
79#ifdef CONFIG_SND_HDA_CODEC_ANALOG
80 snd_hda_preset_analog,
81#endif
82#ifdef CONFIG_SND_HDA_CODEC_SIGMATEL
83 snd_hda_preset_sigmatel,
84#endif
85#ifdef CONFIG_SND_HDA_CODEC_SI3054
86 snd_hda_preset_si3054,
87#endif
88#ifdef CONFIG_SND_HDA_CODEC_ATIHDMI
89 snd_hda_preset_atihdmi,
90#endif
91#ifdef CONFIG_SND_HDA_CODEC_CONEXANT
92 snd_hda_preset_conexant,
93#endif
94#ifdef CONFIG_SND_HDA_CODEC_VIA
95 snd_hda_preset_via,
96#endif
97 NULL
98};
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Takashi Iwaicb53c622007-08-10 17:21:45 +0200100#ifdef CONFIG_SND_HDA_POWER_SAVE
101static void hda_power_work(struct work_struct *work);
102static void hda_keep_power_on(struct hda_codec *codec);
103#else
104static inline void hda_keep_power_on(struct hda_codec *codec) {}
105#endif
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107/**
108 * snd_hda_codec_read - send a command and get the response
109 * @codec: the HDA codec
110 * @nid: NID to send the command
111 * @direct: direct flag
112 * @verb: the verb to send
113 * @parm: the parameter for the verb
114 *
115 * Send a single command and read the corresponding response.
116 *
117 * Returns the obtained response value, or -1 for an error.
118 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200119unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
120 int direct,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 unsigned int verb, unsigned int parm)
122{
123 unsigned int res;
Takashi Iwaicb53c622007-08-10 17:21:45 +0200124 snd_hda_power_up(codec);
Ingo Molnar62932df2006-01-16 16:34:20 +0100125 mutex_lock(&codec->bus->cmd_mutex);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200126 if (!codec->bus->ops.command(codec, nid, direct, verb, parm))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 res = codec->bus->ops.get_response(codec);
128 else
129 res = (unsigned int)-1;
Ingo Molnar62932df2006-01-16 16:34:20 +0100130 mutex_unlock(&codec->bus->cmd_mutex);
Takashi Iwaicb53c622007-08-10 17:21:45 +0200131 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 return res;
133}
134
135/**
136 * snd_hda_codec_write - send a single command without waiting for response
137 * @codec: the HDA codec
138 * @nid: NID to send the command
139 * @direct: direct flag
140 * @verb: the verb to send
141 * @parm: the parameter for the verb
142 *
143 * Send a single command without waiting for response.
144 *
145 * Returns 0 if successful, or a negative error code.
146 */
147int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
148 unsigned int verb, unsigned int parm)
149{
150 int err;
Takashi Iwaicb53c622007-08-10 17:21:45 +0200151 snd_hda_power_up(codec);
Ingo Molnar62932df2006-01-16 16:34:20 +0100152 mutex_lock(&codec->bus->cmd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 err = codec->bus->ops.command(codec, nid, direct, verb, parm);
Ingo Molnar62932df2006-01-16 16:34:20 +0100154 mutex_unlock(&codec->bus->cmd_mutex);
Takashi Iwaicb53c622007-08-10 17:21:45 +0200155 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 return err;
157}
158
159/**
160 * snd_hda_sequence_write - sequence writes
161 * @codec: the HDA codec
162 * @seq: VERB array to send
163 *
164 * Send the commands sequentially from the given array.
165 * The array must be terminated with NID=0.
166 */
167void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
168{
169 for (; seq->nid; seq++)
170 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
171}
172
173/**
174 * snd_hda_get_sub_nodes - get the range of sub nodes
175 * @codec: the HDA codec
176 * @nid: NID to parse
177 * @start_id: the pointer to store the start NID
178 *
179 * Parse the NID and store the start NID of its sub-nodes.
180 * Returns the number of sub-nodes.
181 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200182int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
183 hda_nid_t *start_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
185 unsigned int parm;
186
187 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
Danny Tholene8a7f132007-09-11 21:41:56 +0200188 if (parm == -1)
189 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 *start_id = (parm >> 16) & 0x7fff;
191 return (int)(parm & 0x7fff);
192}
193
194/**
195 * snd_hda_get_connections - get connection list
196 * @codec: the HDA codec
197 * @nid: NID to parse
198 * @conn_list: connection list array
199 * @max_conns: max. number of connections to store
200 *
201 * Parses the connection list of the given widget and stores the list
202 * of NIDs.
203 *
204 * Returns the number of connections, or a negative error code.
205 */
206int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
207 hda_nid_t *conn_list, int max_conns)
208{
209 unsigned int parm;
Takashi Iwai54d17402005-11-21 16:33:22 +0100210 int i, conn_len, conns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 unsigned int shift, num_elems, mask;
Takashi Iwai54d17402005-11-21 16:33:22 +0100212 hda_nid_t prev_nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Takashi Iwaida3cec32008-08-08 17:12:14 +0200214 if (snd_BUG_ON(!conn_list || max_conns <= 0))
215 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
218 if (parm & AC_CLIST_LONG) {
219 /* long form */
220 shift = 16;
221 num_elems = 2;
222 } else {
223 /* short form */
224 shift = 8;
225 num_elems = 4;
226 }
227 conn_len = parm & AC_CLIST_LENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 mask = (1 << (shift-1)) - 1;
229
Takashi Iwai0ba21762007-04-16 11:29:14 +0200230 if (!conn_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 return 0; /* no connection */
232
233 if (conn_len == 1) {
234 /* single connection */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200235 parm = snd_hda_codec_read(codec, nid, 0,
236 AC_VERB_GET_CONNECT_LIST, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 conn_list[0] = parm & mask;
238 return 1;
239 }
240
241 /* multi connection */
242 conns = 0;
Takashi Iwai54d17402005-11-21 16:33:22 +0100243 prev_nid = 0;
244 for (i = 0; i < conn_len; i++) {
245 int range_val;
246 hda_nid_t val, n;
247
248 if (i % num_elems == 0)
249 parm = snd_hda_codec_read(codec, nid, 0,
250 AC_VERB_GET_CONNECT_LIST, i);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200251 range_val = !!(parm & (1 << (shift-1))); /* ranges */
Takashi Iwai54d17402005-11-21 16:33:22 +0100252 val = parm & mask;
253 parm >>= shift;
254 if (range_val) {
255 /* ranges between the previous and this one */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200256 if (!prev_nid || prev_nid >= val) {
257 snd_printk(KERN_WARNING "hda_codec: "
258 "invalid dep_range_val %x:%x\n",
259 prev_nid, val);
Takashi Iwai54d17402005-11-21 16:33:22 +0100260 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100262 for (n = prev_nid + 1; n <= val; n++) {
263 if (conns >= max_conns) {
Takashi Iwai0ba21762007-04-16 11:29:14 +0200264 snd_printk(KERN_ERR
265 "Too many connections\n");
Takashi Iwai54d17402005-11-21 16:33:22 +0100266 return -EINVAL;
267 }
268 conn_list[conns++] = n;
269 }
270 } else {
271 if (conns >= max_conns) {
272 snd_printk(KERN_ERR "Too many connections\n");
273 return -EINVAL;
274 }
275 conn_list[conns++] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100277 prev_nid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 }
279 return conns;
280}
281
282
283/**
284 * snd_hda_queue_unsol_event - add an unsolicited event to queue
285 * @bus: the BUS
286 * @res: unsolicited event (lower 32bit of RIRB entry)
287 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
288 *
289 * Adds the given event to the queue. The events are processed in
290 * the workqueue asynchronously. Call this function in the interrupt
291 * hanlder when RIRB receives an unsolicited event.
292 *
293 * Returns 0 if successful, or a negative error code.
294 */
295int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
296{
297 struct hda_bus_unsolicited *unsol;
298 unsigned int wp;
299
Takashi Iwai0ba21762007-04-16 11:29:14 +0200300 unsol = bus->unsol;
301 if (!unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 return 0;
303
304 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
305 unsol->wp = wp;
306
307 wp <<= 1;
308 unsol->queue[wp] = res;
309 unsol->queue[wp + 1] = res_ex;
310
Takashi Iwaie250af22006-12-19 17:08:52 +0100311 schedule_work(&unsol->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
313 return 0;
314}
315
316/*
317 * process queueud unsolicited events
318 */
David Howellsc4028952006-11-22 14:57:56 +0000319static void process_unsol_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
David Howellsc4028952006-11-22 14:57:56 +0000321 struct hda_bus_unsolicited *unsol =
322 container_of(work, struct hda_bus_unsolicited, work);
323 struct hda_bus *bus = unsol->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 struct hda_codec *codec;
325 unsigned int rp, caddr, res;
326
327 while (unsol->rp != unsol->wp) {
328 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
329 unsol->rp = rp;
330 rp <<= 1;
331 res = unsol->queue[rp];
332 caddr = unsol->queue[rp + 1];
Takashi Iwai0ba21762007-04-16 11:29:14 +0200333 if (!(caddr & (1 << 4))) /* no unsolicited event? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 continue;
335 codec = bus->caddr_tbl[caddr & 0x0f];
336 if (codec && codec->patch_ops.unsol_event)
337 codec->patch_ops.unsol_event(codec, res);
338 }
339}
340
341/*
342 * initialize unsolicited queue
343 */
Takashi Iwai756e2b02007-04-16 11:27:07 +0200344static int __devinit init_unsol_queue(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
346 struct hda_bus_unsolicited *unsol;
347
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100348 if (bus->unsol) /* already initialized */
349 return 0;
350
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200351 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200352 if (!unsol) {
353 snd_printk(KERN_ERR "hda_codec: "
354 "can't allocate unsolicited queue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 return -ENOMEM;
356 }
David Howellsc4028952006-11-22 14:57:56 +0000357 INIT_WORK(&unsol->work, process_unsol_events);
358 unsol->bus = bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 bus->unsol = unsol;
360 return 0;
361}
362
363/*
364 * destructor
365 */
366static void snd_hda_codec_free(struct hda_codec *codec);
367
368static int snd_hda_bus_free(struct hda_bus *bus)
369{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200370 struct hda_codec *codec, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Takashi Iwai0ba21762007-04-16 11:29:14 +0200372 if (!bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 return 0;
374 if (bus->unsol) {
Takashi Iwaie250af22006-12-19 17:08:52 +0100375 flush_scheduled_work();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 kfree(bus->unsol);
377 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200378 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 snd_hda_codec_free(codec);
380 }
381 if (bus->ops.private_free)
382 bus->ops.private_free(bus);
383 kfree(bus);
384 return 0;
385}
386
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100387static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
389 struct hda_bus *bus = device->device_data;
390 return snd_hda_bus_free(bus);
391}
392
393/**
394 * snd_hda_bus_new - create a HDA bus
395 * @card: the card entry
396 * @temp: the template for hda_bus information
397 * @busp: the pointer to store the created bus instance
398 *
399 * Returns 0 if successful, or a negative error code.
400 */
Takashi Iwai756e2b02007-04-16 11:27:07 +0200401int __devinit snd_hda_bus_new(struct snd_card *card,
402 const struct hda_bus_template *temp,
403 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404{
405 struct hda_bus *bus;
406 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100407 static struct snd_device_ops dev_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 .dev_free = snd_hda_bus_dev_free,
409 };
410
Takashi Iwaida3cec32008-08-08 17:12:14 +0200411 if (snd_BUG_ON(!temp))
412 return -EINVAL;
413 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
414 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 if (busp)
417 *busp = NULL;
418
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200419 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 if (bus == NULL) {
421 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
422 return -ENOMEM;
423 }
424
425 bus->card = card;
426 bus->private_data = temp->private_data;
427 bus->pci = temp->pci;
428 bus->modelname = temp->modelname;
429 bus->ops = temp->ops;
430
Ingo Molnar62932df2006-01-16 16:34:20 +0100431 mutex_init(&bus->cmd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 INIT_LIST_HEAD(&bus->codec_list);
433
Takashi Iwai0ba21762007-04-16 11:29:14 +0200434 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
435 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 snd_hda_bus_free(bus);
437 return err;
438 }
439 if (busp)
440 *busp = bus;
441 return 0;
442}
443
Takashi Iwai82467612007-07-27 19:15:54 +0200444#ifdef CONFIG_SND_HDA_GENERIC
445#define is_generic_config(codec) \
446 (codec->bus->modelname && !strcmp(codec->bus->modelname, "generic"))
447#else
448#define is_generic_config(codec) 0
449#endif
450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451/*
452 * find a matching codec preset
453 */
Takashi Iwai756e2b02007-04-16 11:27:07 +0200454static const struct hda_codec_preset __devinit *
455find_codec_preset(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
457 const struct hda_codec_preset **tbl, *preset;
458
Takashi Iwai82467612007-07-27 19:15:54 +0200459 if (is_generic_config(codec))
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100460 return NULL; /* use the generic parser */
461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 for (tbl = hda_preset_tables; *tbl; tbl++) {
463 for (preset = *tbl; preset->id; preset++) {
464 u32 mask = preset->mask;
Marc Boucherca7cfae2008-01-22 15:32:25 +0100465 if (preset->afg && preset->afg != codec->afg)
466 continue;
467 if (preset->mfg && preset->mfg != codec->mfg)
468 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200469 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 mask = ~0;
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200471 if (preset->id == (codec->vendor_id & mask) &&
Takashi Iwai0ba21762007-04-16 11:29:14 +0200472 (!preset->rev ||
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200473 preset->rev == codec->revision_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 return preset;
475 }
476 }
477 return NULL;
478}
479
480/*
481 * snd_hda_get_codec_name - store the codec name
482 */
483void snd_hda_get_codec_name(struct hda_codec *codec,
484 char *name, int namelen)
485{
486 const struct hda_vendor_id *c;
487 const char *vendor = NULL;
488 u16 vendor_id = codec->vendor_id >> 16;
489 char tmp[16];
490
491 for (c = hda_vendor_ids; c->id; c++) {
492 if (c->id == vendor_id) {
493 vendor = c->name;
494 break;
495 }
496 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200497 if (!vendor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 sprintf(tmp, "Generic %04x", vendor_id);
499 vendor = tmp;
500 }
501 if (codec->preset && codec->preset->name)
502 snprintf(name, namelen, "%s %s", vendor, codec->preset->name);
503 else
Takashi Iwai0ba21762007-04-16 11:29:14 +0200504 snprintf(name, namelen, "%s ID %x", vendor,
505 codec->vendor_id & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506}
507
508/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200509 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 */
Takashi Iwai756e2b02007-04-16 11:27:07 +0200511static void __devinit setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
513 int i, total_nodes;
514 hda_nid_t nid;
515
516 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
517 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +0200518 unsigned int func;
519 func = snd_hda_param_read(codec, nid, AC_PAR_FUNCTION_TYPE);
520 switch (func & 0xff) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200521 case AC_GRP_AUDIO_FUNCTION:
522 codec->afg = nid;
523 break;
524 case AC_GRP_MODEM_FUNCTION:
525 codec->mfg = nid;
526 break;
527 default:
528 break;
529 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531}
532
533/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100534 * read widget caps for each widget and store in cache
535 */
536static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
537{
538 int i;
539 hda_nid_t nid;
540
541 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
542 &codec->start_nid);
543 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200544 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +0100545 return -ENOMEM;
546 nid = codec->start_nid;
547 for (i = 0; i < codec->num_nodes; i++, nid++)
548 codec->wcaps[i] = snd_hda_param_read(codec, nid,
549 AC_PAR_AUDIO_WIDGET_CAP);
550 return 0;
551}
552
553
Takashi Iwai01751f52007-08-10 16:59:39 +0200554static void init_hda_cache(struct hda_cache_rec *cache,
555 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +0200556static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +0200557
Takashi Iwai54d17402005-11-21 16:33:22 +0100558/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 * codec destructor
560 */
561static void snd_hda_codec_free(struct hda_codec *codec)
562{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200563 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 return;
Takashi Iwaicb53c622007-08-10 17:21:45 +0200565#ifdef CONFIG_SND_HDA_POWER_SAVE
566 cancel_delayed_work(&codec->power_work);
Takashi Iwai2525fdc2007-08-15 22:18:22 +0200567 flush_scheduled_work();
Takashi Iwaicb53c622007-08-10 17:21:45 +0200568#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 list_del(&codec->list);
570 codec->bus->caddr_tbl[codec->addr] = NULL;
571 if (codec->patch_ops.free)
572 codec->patch_ops.free(codec);
Takashi Iwai01751f52007-08-10 16:59:39 +0200573 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +0200574 free_hda_cache(&codec->cmd_cache);
Takashi Iwai54d17402005-11-21 16:33:22 +0100575 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 kfree(codec);
577}
578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579/**
580 * snd_hda_codec_new - create a HDA codec
581 * @bus: the bus to assign
582 * @codec_addr: the codec address
583 * @codecp: the pointer to store the generated codec
584 *
585 * Returns 0 if successful, or a negative error code.
586 */
Takashi Iwai756e2b02007-04-16 11:27:07 +0200587int __devinit snd_hda_codec_new(struct hda_bus *bus, unsigned int codec_addr,
588 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
590 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +0200591 char component[31];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 int err;
593
Takashi Iwaida3cec32008-08-08 17:12:14 +0200594 if (snd_BUG_ON(!bus))
595 return -EINVAL;
596 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
597 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
599 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +0200600 snd_printk(KERN_ERR "hda_codec: "
601 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 return -EBUSY;
603 }
604
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200605 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 if (codec == NULL) {
607 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
608 return -ENOMEM;
609 }
610
611 codec->bus = bus;
612 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +0100613 mutex_init(&codec->spdif_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +0200614 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +0200615 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Takashi Iwaicb53c622007-08-10 17:21:45 +0200617#ifdef CONFIG_SND_HDA_POWER_SAVE
618 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
619 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
620 * the caller has to power down appropriatley after initialization
621 * phase.
622 */
623 hda_keep_power_on(codec);
624#endif
625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 list_add_tail(&codec->list, &bus->codec_list);
627 bus->caddr_tbl[codec_addr] = codec;
628
Takashi Iwai0ba21762007-04-16 11:29:14 +0200629 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
630 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +0100631 if (codec->vendor_id == -1)
632 /* read again, hopefully the access method was corrected
633 * in the last read...
634 */
635 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
636 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200637 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
638 AC_PAR_SUBSYSTEM_ID);
639 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
640 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200642 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200643 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200644 snd_printdd("hda_codec: no AFG or MFG node found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 snd_hda_codec_free(codec);
646 return -ENODEV;
647 }
648
Takashi Iwai54d17402005-11-21 16:33:22 +0100649 if (read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg) < 0) {
650 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
651 snd_hda_codec_free(codec);
652 return -ENOMEM;
653 }
654
Takashi Iwai0ba21762007-04-16 11:29:14 +0200655 if (!codec->subsystem_id) {
Takashi Iwai86284e42005-10-11 15:05:54 +0200656 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200657 codec->subsystem_id =
658 snd_hda_codec_read(codec, nid, 0,
659 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +0200660 }
661
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100662 codec->preset = find_codec_preset(codec);
Takashi Iwai43ea1d42007-04-26 19:12:08 +0200663 /* audio codec should override the mixer name */
664 if (codec->afg || !*bus->card->mixername)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 snd_hda_get_codec_name(codec, bus->card->mixername,
666 sizeof(bus->card->mixername));
667
Takashi Iwai82467612007-07-27 19:15:54 +0200668 if (is_generic_config(codec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai82467612007-07-27 19:15:54 +0200670 goto patched;
671 }
Takashi Iwai82467612007-07-27 19:15:54 +0200672 if (codec->preset && codec->preset->patch) {
673 err = codec->preset->patch(codec);
674 goto patched;
675 }
676
677 /* call the default parser */
Takashi Iwai82467612007-07-27 19:15:54 +0200678 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai35a1e0c2007-10-19 08:13:40 +0200679 if (err < 0)
680 printk(KERN_ERR "hda-codec: No codec parser is available\n");
Takashi Iwai82467612007-07-27 19:15:54 +0200681
682 patched:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 if (err < 0) {
684 snd_hda_codec_free(codec);
685 return err;
686 }
687
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100688 if (codec->patch_ops.unsol_event)
689 init_unsol_queue(bus);
690
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 snd_hda_codec_proc_new(codec);
Takashi Iwai28073142007-07-27 18:58:06 +0200692#ifdef CONFIG_SND_HDA_HWDEP
693 snd_hda_create_hwdep(codec);
694#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Jaroslav Kyselaba443682008-08-13 20:55:32 +0200696 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id, codec->subsystem_id, codec->revision_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 snd_component_add(codec->bus->card, component);
698
699 if (codecp)
700 *codecp = codec;
701 return 0;
702}
703
704/**
705 * snd_hda_codec_setup_stream - set up the codec for streaming
706 * @codec: the CODEC to set up
707 * @nid: the NID to set up
708 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
709 * @channel_id: channel id to pass, zero based.
710 * @format: stream format.
711 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200712void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
713 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 int channel_id, int format)
715{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200716 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +0200717 return;
718
Takashi Iwai0ba21762007-04-16 11:29:14 +0200719 snd_printdd("hda_codec_setup_stream: "
720 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 nid, stream_tag, channel_id, format);
722 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID,
723 (stream_tag << 4) | channel_id);
724 msleep(1);
725 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, format);
726}
727
Takashi Iwai888afa12008-03-18 09:57:50 +0100728void snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid)
729{
730 if (!nid)
731 return;
732
733 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
734 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
735#if 0 /* keep the format */
736 msleep(1);
737 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0);
738#endif
739}
740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741/*
742 * amp access functions
743 */
744
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200745/* FIXME: more better hash key? */
746#define HDA_HASH_KEY(nid,dir,idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200748#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
750/* initialize the hash table */
Takashi Iwai01751f52007-08-10 16:59:39 +0200751static void __devinit init_hda_cache(struct hda_cache_rec *cache,
752 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
Takashi Iwai01751f52007-08-10 16:59:39 +0200754 memset(cache, 0, sizeof(*cache));
755 memset(cache->hash, 0xff, sizeof(cache->hash));
756 cache->record_size = record_size;
757}
758
Takashi Iwai1fcaee62007-08-23 00:01:09 +0200759static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +0200760{
761 kfree(cache->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762}
763
764/* query the hash. allocate an entry if not found. */
Takashi Iwai01751f52007-08-10 16:59:39 +0200765static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
766 u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767{
Takashi Iwai01751f52007-08-10 16:59:39 +0200768 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
769 u16 cur = cache->hash[idx];
770 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
772 while (cur != 0xffff) {
Takashi Iwai01751f52007-08-10 16:59:39 +0200773 info = (struct hda_cache_head *)(cache->buffer +
774 cur * cache->record_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 if (info->key == key)
776 return info;
777 cur = info->next;
778 }
779
780 /* add a new hash entry */
Takashi Iwai01751f52007-08-10 16:59:39 +0200781 if (cache->num_entries >= cache->size) {
Takashi Iwaid0311662005-11-07 14:38:44 +0100782 /* reallocate the array */
Takashi Iwai01751f52007-08-10 16:59:39 +0200783 unsigned int new_size = cache->size + 64;
784 void *new_buffer;
785 new_buffer = kcalloc(new_size, cache->record_size, GFP_KERNEL);
786 if (!new_buffer) {
Takashi Iwai0ba21762007-04-16 11:29:14 +0200787 snd_printk(KERN_ERR "hda_codec: "
788 "can't malloc amp_info\n");
Takashi Iwaid0311662005-11-07 14:38:44 +0100789 return NULL;
790 }
Takashi Iwai01751f52007-08-10 16:59:39 +0200791 if (cache->buffer) {
792 memcpy(new_buffer, cache->buffer,
793 cache->size * cache->record_size);
794 kfree(cache->buffer);
Takashi Iwaid0311662005-11-07 14:38:44 +0100795 }
Takashi Iwai01751f52007-08-10 16:59:39 +0200796 cache->size = new_size;
797 cache->buffer = new_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 }
Takashi Iwai01751f52007-08-10 16:59:39 +0200799 cur = cache->num_entries++;
800 info = (struct hda_cache_head *)(cache->buffer +
801 cur * cache->record_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 info->key = key;
Takashi Iwai01751f52007-08-10 16:59:39 +0200803 info->val = 0;
804 info->next = cache->hash[idx];
805 cache->hash[idx] = cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
807 return info;
808}
809
Takashi Iwai01751f52007-08-10 16:59:39 +0200810/* query and allocate an amp hash entry */
811static inline struct hda_amp_info *
812get_alloc_amp_hash(struct hda_codec *codec, u32 key)
813{
814 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
815}
816
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817/*
818 * query AMP capabilities for the given widget and direction
819 */
Matthew Ranostay09a99952008-01-24 11:49:21 +0100820u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200822 struct hda_amp_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Takashi Iwai0ba21762007-04-16 11:29:14 +0200824 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
825 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 return 0;
Takashi Iwai01751f52007-08-10 16:59:39 +0200827 if (!(info->head.val & INFO_AMP_CAPS)) {
Takashi Iwai0ba21762007-04-16 11:29:14 +0200828 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 nid = codec->afg;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200830 info->amp_caps = snd_hda_param_read(codec, nid,
831 direction == HDA_OUTPUT ?
832 AC_PAR_AMP_OUT_CAP :
833 AC_PAR_AMP_IN_CAP);
Takashi Iwaib75e53f2007-05-10 16:56:09 +0200834 if (info->amp_caps)
Takashi Iwai01751f52007-08-10 16:59:39 +0200835 info->head.val |= INFO_AMP_CAPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 }
837 return info->amp_caps;
838}
839
Takashi Iwai897cc182007-05-29 19:01:37 +0200840int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
841 unsigned int caps)
842{
843 struct hda_amp_info *info;
844
845 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
846 if (!info)
847 return -EINVAL;
848 info->amp_caps = caps;
Takashi Iwai01751f52007-08-10 16:59:39 +0200849 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai897cc182007-05-29 19:01:37 +0200850 return 0;
851}
852
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853/*
854 * read the current volume to info
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200855 * if the cache exists, read the cache value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200857static unsigned int get_vol_mute(struct hda_codec *codec,
858 struct hda_amp_info *info, hda_nid_t nid,
859 int ch, int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860{
861 u32 val, parm;
862
Takashi Iwai01751f52007-08-10 16:59:39 +0200863 if (info->head.val & INFO_AMP_VOL(ch))
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200864 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
866 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
867 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
868 parm |= index;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200869 val = snd_hda_codec_read(codec, nid, 0,
870 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 info->vol[ch] = val & 0xff;
Takashi Iwai01751f52007-08-10 16:59:39 +0200872 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200873 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874}
875
876/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200877 * write the current volume in info to the h/w and update the cache
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200879static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
Takashi Iwai0ba21762007-04-16 11:29:14 +0200880 hda_nid_t nid, int ch, int direction, int index,
881 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882{
883 u32 parm;
884
885 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
886 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
887 parm |= index << AC_AMP_SET_INDEX_SHIFT;
888 parm |= val;
889 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200890 info->vol[ch] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891}
892
893/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200894 * read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 */
Takashi Iwai834be882006-03-01 14:16:17 +0100896int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
897 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200899 struct hda_amp_info *info;
900 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
901 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200903 return get_vol_mute(codec, info, nid, ch, direction, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904}
905
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200906/*
907 * update the AMP value, mask = bit mask to set, val = the value
908 */
Takashi Iwai834be882006-03-01 14:16:17 +0100909int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
910 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200912 struct hda_amp_info *info;
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200913
Takashi Iwai0ba21762007-04-16 11:29:14 +0200914 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
915 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200917 val &= mask;
918 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
Takashi Iwai82beb8f2007-08-10 17:09:26 +0200919 if (info->vol[ch] == val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200921 put_vol_mute(codec, info, nid, ch, direction, idx, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 return 1;
923}
924
Takashi Iwai47fd8302007-08-10 17:11:07 +0200925/*
926 * update the AMP stereo with the same mask and value
927 */
928int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
929 int direction, int idx, int mask, int val)
930{
931 int ch, ret = 0;
932 for (ch = 0; ch < 2; ch++)
933 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
934 idx, mask, val);
935 return ret;
936}
937
Takashi Iwaicb53c622007-08-10 17:21:45 +0200938#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwaib3ac5632007-08-10 17:03:40 +0200939/* resume the all amp commands from the cache */
940void snd_hda_codec_resume_amp(struct hda_codec *codec)
941{
942 struct hda_amp_info *buffer = codec->amp_cache.buffer;
943 int i;
944
945 for (i = 0; i < codec->amp_cache.size; i++, buffer++) {
946 u32 key = buffer->head.key;
947 hda_nid_t nid;
948 unsigned int idx, dir, ch;
949 if (!key)
950 continue;
951 nid = key & 0xff;
952 idx = (key >> 16) & 0xff;
953 dir = (key >> 24) & 0xff;
954 for (ch = 0; ch < 2; ch++) {
955 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
956 continue;
957 put_vol_mute(codec, buffer, nid, ch, dir, idx,
958 buffer->vol[ch]);
959 }
960 }
961}
Takashi Iwaicb53c622007-08-10 17:21:45 +0200962#endif /* SND_HDA_NEEDS_RESUME */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
964/*
965 * AMP control callbacks
966 */
967/* retrieve parameters from private_value */
968#define get_amp_nid(kc) ((kc)->private_value & 0xffff)
969#define get_amp_channels(kc) (((kc)->private_value >> 16) & 0x3)
970#define get_amp_direction(kc) (((kc)->private_value >> 18) & 0x1)
971#define get_amp_index(kc) (((kc)->private_value >> 19) & 0xf)
972
973/* volume */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200974int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
975 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976{
977 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
978 u16 nid = get_amp_nid(kcontrol);
979 u8 chs = get_amp_channels(kcontrol);
980 int dir = get_amp_direction(kcontrol);
981 u32 caps;
982
983 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200984 /* num steps */
985 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
986 if (!caps) {
987 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +0100988 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
989 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 return -EINVAL;
991 }
992 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
993 uinfo->count = chs == 3 ? 2 : 1;
994 uinfo->value.integer.min = 0;
995 uinfo->value.integer.max = caps;
996 return 0;
997}
998
Takashi Iwai0ba21762007-04-16 11:29:14 +0200999int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1000 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001{
1002 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1003 hda_nid_t nid = get_amp_nid(kcontrol);
1004 int chs = get_amp_channels(kcontrol);
1005 int dir = get_amp_direction(kcontrol);
1006 int idx = get_amp_index(kcontrol);
1007 long *valp = ucontrol->value.integer.value;
1008
1009 if (chs & 1)
Takashi Iwai47fd8302007-08-10 17:11:07 +02001010 *valp++ = snd_hda_codec_amp_read(codec, nid, 0, dir, idx)
1011 & HDA_AMP_VOLMASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 if (chs & 2)
Takashi Iwai47fd8302007-08-10 17:11:07 +02001013 *valp = snd_hda_codec_amp_read(codec, nid, 1, dir, idx)
1014 & HDA_AMP_VOLMASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 return 0;
1016}
1017
Takashi Iwai0ba21762007-04-16 11:29:14 +02001018int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1019 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020{
1021 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1022 hda_nid_t nid = get_amp_nid(kcontrol);
1023 int chs = get_amp_channels(kcontrol);
1024 int dir = get_amp_direction(kcontrol);
1025 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 long *valp = ucontrol->value.integer.value;
1027 int change = 0;
1028
Takashi Iwaicb53c622007-08-10 17:21:45 +02001029 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001030 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001031 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
1032 0x7f, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001033 valp++;
1034 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001035 if (chs & 2)
1036 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001037 0x7f, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001038 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 return change;
1040}
1041
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001042int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1043 unsigned int size, unsigned int __user *_tlv)
1044{
1045 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1046 hda_nid_t nid = get_amp_nid(kcontrol);
1047 int dir = get_amp_direction(kcontrol);
1048 u32 caps, val1, val2;
1049
1050 if (size < 4 * sizeof(unsigned int))
1051 return -ENOMEM;
1052 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001053 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1054 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001055 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
1056 val1 = ((int)val1) * ((int)val2);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001057 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
1058 return -EFAULT;
1059 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
1060 return -EFAULT;
1061 if (put_user(val1, _tlv + 2))
1062 return -EFAULT;
1063 if (put_user(val2, _tlv + 3))
1064 return -EFAULT;
1065 return 0;
1066}
1067
Takashi Iwai2134ea42008-01-10 16:53:55 +01001068/*
1069 * set (static) TLV for virtual master volume; recalculated as max 0dB
1070 */
1071void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
1072 unsigned int *tlv)
1073{
1074 u32 caps;
1075 int nums, step;
1076
1077 caps = query_amp_caps(codec, nid, dir);
1078 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1079 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1080 step = (step + 1) * 25;
1081 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
1082 tlv[1] = 2 * sizeof(unsigned int);
1083 tlv[2] = -nums * step;
1084 tlv[3] = step;
1085}
1086
1087/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01001088static struct snd_kcontrol *
1089_snd_hda_find_mixer_ctl(struct hda_codec *codec,
1090 const char *name, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01001091{
1092 struct snd_ctl_elem_id id;
1093 memset(&id, 0, sizeof(id));
1094 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwai09f99702008-02-04 12:31:13 +01001095 id.index = idx;
Takashi Iwai2134ea42008-01-10 16:53:55 +01001096 strcpy(id.name, name);
1097 return snd_ctl_find_id(codec->bus->card, &id);
1098}
1099
Takashi Iwai09f99702008-02-04 12:31:13 +01001100struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
1101 const char *name)
1102{
1103 return _snd_hda_find_mixer_ctl(codec, name, 0);
1104}
1105
Takashi Iwai2134ea42008-01-10 16:53:55 +01001106/* create a virtual master control and add slaves */
1107int snd_hda_add_vmaster(struct hda_codec *codec, char *name,
1108 unsigned int *tlv, const char **slaves)
1109{
1110 struct snd_kcontrol *kctl;
1111 const char **s;
1112 int err;
1113
Takashi Iwai2f085542008-02-22 18:43:50 +01001114 for (s = slaves; *s && !snd_hda_find_mixer_ctl(codec, *s); s++)
1115 ;
1116 if (!*s) {
1117 snd_printdd("No slave found for %s\n", name);
1118 return 0;
1119 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01001120 kctl = snd_ctl_make_virtual_master(name, tlv);
1121 if (!kctl)
1122 return -ENOMEM;
1123 err = snd_ctl_add(codec->bus->card, kctl);
1124 if (err < 0)
1125 return err;
1126
1127 for (s = slaves; *s; s++) {
1128 struct snd_kcontrol *sctl;
1129
1130 sctl = snd_hda_find_mixer_ctl(codec, *s);
1131 if (!sctl) {
1132 snd_printdd("Cannot find slave %s, skipped\n", *s);
1133 continue;
1134 }
1135 err = snd_ctl_add_slave(kctl, sctl);
1136 if (err < 0)
1137 return err;
1138 }
1139 return 0;
1140}
1141
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142/* switch */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001143int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
1144 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145{
1146 int chs = get_amp_channels(kcontrol);
1147
1148 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1149 uinfo->count = chs == 3 ? 2 : 1;
1150 uinfo->value.integer.min = 0;
1151 uinfo->value.integer.max = 1;
1152 return 0;
1153}
1154
Takashi Iwai0ba21762007-04-16 11:29:14 +02001155int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
1156 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157{
1158 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1159 hda_nid_t nid = get_amp_nid(kcontrol);
1160 int chs = get_amp_channels(kcontrol);
1161 int dir = get_amp_direction(kcontrol);
1162 int idx = get_amp_index(kcontrol);
1163 long *valp = ucontrol->value.integer.value;
1164
1165 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02001166 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02001167 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02001169 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02001170 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 return 0;
1172}
1173
Takashi Iwai0ba21762007-04-16 11:29:14 +02001174int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
1175 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176{
1177 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1178 hda_nid_t nid = get_amp_nid(kcontrol);
1179 int chs = get_amp_channels(kcontrol);
1180 int dir = get_amp_direction(kcontrol);
1181 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 long *valp = ucontrol->value.integer.value;
1183 int change = 0;
1184
Takashi Iwaicb53c622007-08-10 17:21:45 +02001185 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001186 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001187 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02001188 HDA_AMP_MUTE,
1189 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001190 valp++;
1191 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001192 if (chs & 2)
1193 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02001194 HDA_AMP_MUTE,
1195 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001196#ifdef CONFIG_SND_HDA_POWER_SAVE
1197 if (codec->patch_ops.check_power_status)
1198 codec->patch_ops.check_power_status(codec, nid);
1199#endif
1200 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 return change;
1202}
1203
1204/*
Takashi Iwai985be542005-11-02 18:26:49 +01001205 * bound volume controls
1206 *
1207 * bind multiple volumes (# indices, from 0)
1208 */
1209
1210#define AMP_VAL_IDX_SHIFT 19
1211#define AMP_VAL_IDX_MASK (0x0f<<19)
1212
Takashi Iwai0ba21762007-04-16 11:29:14 +02001213int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
1214 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01001215{
1216 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1217 unsigned long pval;
1218 int err;
1219
Ingo Molnar62932df2006-01-16 16:34:20 +01001220 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Takashi Iwai985be542005-11-02 18:26:49 +01001221 pval = kcontrol->private_value;
1222 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
1223 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
1224 kcontrol->private_value = pval;
Ingo Molnar62932df2006-01-16 16:34:20 +01001225 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01001226 return err;
1227}
1228
Takashi Iwai0ba21762007-04-16 11:29:14 +02001229int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
1230 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01001231{
1232 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1233 unsigned long pval;
1234 int i, indices, err = 0, change = 0;
1235
Ingo Molnar62932df2006-01-16 16:34:20 +01001236 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Takashi Iwai985be542005-11-02 18:26:49 +01001237 pval = kcontrol->private_value;
1238 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
1239 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001240 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
1241 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01001242 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1243 if (err < 0)
1244 break;
1245 change |= err;
1246 }
1247 kcontrol->private_value = pval;
Ingo Molnar62932df2006-01-16 16:34:20 +01001248 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01001249 return err < 0 ? err : change;
1250}
1251
1252/*
Takashi Iwai532d5382007-07-27 19:02:40 +02001253 * generic bound volume/swtich controls
1254 */
1255int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
1256 struct snd_ctl_elem_info *uinfo)
1257{
1258 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1259 struct hda_bind_ctls *c;
1260 int err;
1261
Takashi Iwai532d5382007-07-27 19:02:40 +02001262 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01001263 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02001264 kcontrol->private_value = *c->values;
1265 err = c->ops->info(kcontrol, uinfo);
1266 kcontrol->private_value = (long)c;
1267 mutex_unlock(&codec->spdif_mutex);
1268 return err;
1269}
1270
1271int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
1272 struct snd_ctl_elem_value *ucontrol)
1273{
1274 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1275 struct hda_bind_ctls *c;
1276 int err;
1277
Takashi Iwai532d5382007-07-27 19:02:40 +02001278 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01001279 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02001280 kcontrol->private_value = *c->values;
1281 err = c->ops->get(kcontrol, ucontrol);
1282 kcontrol->private_value = (long)c;
1283 mutex_unlock(&codec->spdif_mutex);
1284 return err;
1285}
1286
1287int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
1288 struct snd_ctl_elem_value *ucontrol)
1289{
1290 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1291 struct hda_bind_ctls *c;
1292 unsigned long *vals;
1293 int err = 0, change = 0;
1294
Takashi Iwai532d5382007-07-27 19:02:40 +02001295 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01001296 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02001297 for (vals = c->values; *vals; vals++) {
1298 kcontrol->private_value = *vals;
1299 err = c->ops->put(kcontrol, ucontrol);
1300 if (err < 0)
1301 break;
1302 change |= err;
1303 }
1304 kcontrol->private_value = (long)c;
1305 mutex_unlock(&codec->spdif_mutex);
1306 return err < 0 ? err : change;
1307}
1308
1309int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1310 unsigned int size, unsigned int __user *tlv)
1311{
1312 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1313 struct hda_bind_ctls *c;
1314 int err;
1315
Takashi Iwai532d5382007-07-27 19:02:40 +02001316 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01001317 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02001318 kcontrol->private_value = *c->values;
1319 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
1320 kcontrol->private_value = (long)c;
1321 mutex_unlock(&codec->spdif_mutex);
1322 return err;
1323}
1324
1325struct hda_ctl_ops snd_hda_bind_vol = {
1326 .info = snd_hda_mixer_amp_volume_info,
1327 .get = snd_hda_mixer_amp_volume_get,
1328 .put = snd_hda_mixer_amp_volume_put,
1329 .tlv = snd_hda_mixer_amp_tlv
1330};
1331
1332struct hda_ctl_ops snd_hda_bind_sw = {
1333 .info = snd_hda_mixer_amp_switch_info,
1334 .get = snd_hda_mixer_amp_switch_get,
1335 .put = snd_hda_mixer_amp_switch_put,
1336 .tlv = snd_hda_mixer_amp_tlv
1337};
1338
1339/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 * SPDIF out controls
1341 */
1342
Takashi Iwai0ba21762007-04-16 11:29:14 +02001343static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
1344 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345{
1346 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1347 uinfo->count = 1;
1348 return 0;
1349}
1350
Takashi Iwai0ba21762007-04-16 11:29:14 +02001351static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
1352 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353{
1354 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
1355 IEC958_AES0_NONAUDIO |
1356 IEC958_AES0_CON_EMPHASIS_5015 |
1357 IEC958_AES0_CON_NOT_COPYRIGHT;
1358 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
1359 IEC958_AES1_CON_ORIGINAL;
1360 return 0;
1361}
1362
Takashi Iwai0ba21762007-04-16 11:29:14 +02001363static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
1364 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365{
1366 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
1367 IEC958_AES0_NONAUDIO |
1368 IEC958_AES0_PRO_EMPHASIS_5015;
1369 return 0;
1370}
1371
Takashi Iwai0ba21762007-04-16 11:29:14 +02001372static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
1373 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374{
1375 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1376
1377 ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
1378 ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
1379 ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
1380 ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
1381
1382 return 0;
1383}
1384
1385/* convert from SPDIF status bits to HDA SPDIF bits
1386 * bit 0 (DigEn) is always set zero (to be filled later)
1387 */
1388static unsigned short convert_from_spdif_status(unsigned int sbits)
1389{
1390 unsigned short val = 0;
1391
1392 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02001393 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02001395 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001397 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
1398 IEC958_AES0_PRO_EMPHASIS_5015)
1399 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001401 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
1402 IEC958_AES0_CON_EMPHASIS_5015)
1403 val |= AC_DIG1_EMPHASIS;
1404 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
1405 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02001407 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
1409 }
1410 return val;
1411}
1412
1413/* convert to SPDIF status bits from HDA SPDIF bits
1414 */
1415static unsigned int convert_to_spdif_status(unsigned short val)
1416{
1417 unsigned int sbits = 0;
1418
Takashi Iwai0ba21762007-04-16 11:29:14 +02001419 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001421 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 sbits |= IEC958_AES0_PROFESSIONAL;
1423 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001424 if (sbits & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
1426 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001427 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001429 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001431 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
1433 sbits |= val & (0x7f << 8);
1434 }
1435 return sbits;
1436}
1437
Takashi Iwai0ba21762007-04-16 11:29:14 +02001438static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
1439 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440{
1441 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1442 hda_nid_t nid = kcontrol->private_value;
1443 unsigned short val;
1444 int change;
1445
Ingo Molnar62932df2006-01-16 16:34:20 +01001446 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 codec->spdif_status = ucontrol->value.iec958.status[0] |
1448 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
1449 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
1450 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
1451 val = convert_from_spdif_status(codec->spdif_status);
1452 val |= codec->spdif_ctls & 1;
1453 change = codec->spdif_ctls != val;
1454 codec->spdif_ctls = val;
1455
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001456 if (change) {
Matthew Ranostayde51ca12008-09-07 14:31:40 -04001457 hda_nid_t *d;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001458 snd_hda_codec_write_cache(codec, nid, 0,
1459 AC_VERB_SET_DIGI_CONVERT_1,
1460 val & 0xff);
1461 snd_hda_codec_write_cache(codec, nid, 0,
1462 AC_VERB_SET_DIGI_CONVERT_2,
1463 val >> 8);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04001464
1465 for (d = codec->slave_dig_outs; *d; d++) {
1466 snd_hda_codec_write_cache(codec, *d, 0,
1467 AC_VERB_SET_DIGI_CONVERT_1,
1468 val & 0xff);
1469 snd_hda_codec_write_cache(codec, *d, 0,
1470 AC_VERB_SET_DIGI_CONVERT_2,
1471 val >> 8);
1472 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 }
1474
Ingo Molnar62932df2006-01-16 16:34:20 +01001475 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 return change;
1477}
1478
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001479#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
Takashi Iwai0ba21762007-04-16 11:29:14 +02001481static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
1482 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483{
1484 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1485
Takashi Iwai0ba21762007-04-16 11:29:14 +02001486 ucontrol->value.integer.value[0] = codec->spdif_ctls & AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 return 0;
1488}
1489
Takashi Iwai0ba21762007-04-16 11:29:14 +02001490static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
1491 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492{
1493 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1494 hda_nid_t nid = kcontrol->private_value;
1495 unsigned short val;
1496 int change;
1497
Ingo Molnar62932df2006-01-16 16:34:20 +01001498 mutex_lock(&codec->spdif_mutex);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001499 val = codec->spdif_ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02001501 val |= AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 change = codec->spdif_ctls != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001503 if (change) {
Matthew Ranostayde51ca12008-09-07 14:31:40 -04001504 hda_nid_t *d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 codec->spdif_ctls = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001506 snd_hda_codec_write_cache(codec, nid, 0,
1507 AC_VERB_SET_DIGI_CONVERT_1,
1508 val & 0xff);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04001509
1510 for (d = codec->slave_dig_outs; *d; d++)
1511 snd_hda_codec_write_cache(codec, *d, 0,
1512 AC_VERB_SET_DIGI_CONVERT_1,
1513 val & 0xff);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001514 /* unmute amp switch (if any) */
1515 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
Takashi Iwai47fd8302007-08-10 17:11:07 +02001516 (val & AC_DIG1_ENABLE))
1517 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
1518 HDA_AMP_MUTE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 }
Ingo Molnar62932df2006-01-16 16:34:20 +01001520 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 return change;
1522}
1523
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001524static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 {
1526 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1527 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1528 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
1529 .info = snd_hda_spdif_mask_info,
1530 .get = snd_hda_spdif_cmask_get,
1531 },
1532 {
1533 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1534 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1535 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
1536 .info = snd_hda_spdif_mask_info,
1537 .get = snd_hda_spdif_pmask_get,
1538 },
1539 {
1540 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1541 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1542 .info = snd_hda_spdif_mask_info,
1543 .get = snd_hda_spdif_default_get,
1544 .put = snd_hda_spdif_default_put,
1545 },
1546 {
1547 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1548 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
1549 .info = snd_hda_spdif_out_switch_info,
1550 .get = snd_hda_spdif_out_switch_get,
1551 .put = snd_hda_spdif_out_switch_put,
1552 },
1553 { } /* end */
1554};
1555
Takashi Iwai09f99702008-02-04 12:31:13 +01001556#define SPDIF_MAX_IDX 4 /* 4 instances should be enough to probe */
1557
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558/**
1559 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
1560 * @codec: the HDA codec
1561 * @nid: audio out widget NID
1562 *
1563 * Creates controls related with the SPDIF output.
1564 * Called from each patch supporting the SPDIF out.
1565 *
1566 * Returns 0 if successful, or a negative error code.
1567 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02001568int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569{
1570 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001571 struct snd_kcontrol *kctl;
1572 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01001573 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
Takashi Iwai09f99702008-02-04 12:31:13 +01001575 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
1576 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Playback Switch",
1577 idx))
1578 break;
1579 }
1580 if (idx >= SPDIF_MAX_IDX) {
1581 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
1582 return -EBUSY;
1583 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
1585 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwai09f99702008-02-04 12:31:13 +01001586 kctl->id.index = idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 kctl->private_value = nid;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001588 err = snd_ctl_add(codec->bus->card, kctl);
1589 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 return err;
1591 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02001592 codec->spdif_ctls =
Andrew Paprocki3982d172007-12-19 12:13:44 +01001593 snd_hda_codec_read(codec, nid, 0,
1594 AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
1596 return 0;
1597}
1598
1599/*
Takashi Iwai9a081602008-02-12 18:37:26 +01001600 * SPDIF sharing with analog output
1601 */
1602static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
1603 struct snd_ctl_elem_value *ucontrol)
1604{
1605 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
1606 ucontrol->value.integer.value[0] = mout->share_spdif;
1607 return 0;
1608}
1609
1610static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
1611 struct snd_ctl_elem_value *ucontrol)
1612{
1613 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
1614 mout->share_spdif = !!ucontrol->value.integer.value[0];
1615 return 0;
1616}
1617
1618static struct snd_kcontrol_new spdif_share_sw = {
1619 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1620 .name = "IEC958 Default PCM Playback Switch",
1621 .info = snd_ctl_boolean_mono_info,
1622 .get = spdif_share_sw_get,
1623 .put = spdif_share_sw_put,
1624};
1625
1626int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
1627 struct hda_multi_out *mout)
1628{
1629 if (!mout->dig_out_nid)
1630 return 0;
1631 /* ATTENTION: here mout is passed as private_data, instead of codec */
1632 return snd_ctl_add(codec->bus->card,
1633 snd_ctl_new1(&spdif_share_sw, mout));
1634}
1635
1636/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 * SPDIF input
1638 */
1639
1640#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
1641
Takashi Iwai0ba21762007-04-16 11:29:14 +02001642static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
1643 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644{
1645 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1646
1647 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
1648 return 0;
1649}
1650
Takashi Iwai0ba21762007-04-16 11:29:14 +02001651static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
1652 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653{
1654 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1655 hda_nid_t nid = kcontrol->private_value;
1656 unsigned int val = !!ucontrol->value.integer.value[0];
1657 int change;
1658
Ingo Molnar62932df2006-01-16 16:34:20 +01001659 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001661 if (change) {
Matthew Ranostayde51ca12008-09-07 14:31:40 -04001662 hda_nid_t *d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001664 snd_hda_codec_write_cache(codec, nid, 0,
1665 AC_VERB_SET_DIGI_CONVERT_1, val);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04001666
1667 for (d = codec->slave_dig_outs; *d; d++)
1668 snd_hda_codec_write_cache(codec, *d, 0,
1669 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 }
Ingo Molnar62932df2006-01-16 16:34:20 +01001671 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 return change;
1673}
1674
Takashi Iwai0ba21762007-04-16 11:29:14 +02001675static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
1676 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677{
1678 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1679 hda_nid_t nid = kcontrol->private_value;
1680 unsigned short val;
1681 unsigned int sbits;
1682
Andrew Paprocki3982d172007-12-19 12:13:44 +01001683 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 sbits = convert_to_spdif_status(val);
1685 ucontrol->value.iec958.status[0] = sbits;
1686 ucontrol->value.iec958.status[1] = sbits >> 8;
1687 ucontrol->value.iec958.status[2] = sbits >> 16;
1688 ucontrol->value.iec958.status[3] = sbits >> 24;
1689 return 0;
1690}
1691
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001692static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 {
1694 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1695 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH),
1696 .info = snd_hda_spdif_in_switch_info,
1697 .get = snd_hda_spdif_in_switch_get,
1698 .put = snd_hda_spdif_in_switch_put,
1699 },
1700 {
1701 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1702 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1703 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT),
1704 .info = snd_hda_spdif_mask_info,
1705 .get = snd_hda_spdif_in_status_get,
1706 },
1707 { } /* end */
1708};
1709
1710/**
1711 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
1712 * @codec: the HDA codec
1713 * @nid: audio in widget NID
1714 *
1715 * Creates controls related with the SPDIF input.
1716 * Called from each patch supporting the SPDIF in.
1717 *
1718 * Returns 0 if successful, or a negative error code.
1719 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02001720int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721{
1722 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001723 struct snd_kcontrol *kctl;
1724 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01001725 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
Takashi Iwai09f99702008-02-04 12:31:13 +01001727 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
1728 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Capture Switch",
1729 idx))
1730 break;
1731 }
1732 if (idx >= SPDIF_MAX_IDX) {
1733 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
1734 return -EBUSY;
1735 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
1737 kctl = snd_ctl_new1(dig_mix, codec);
1738 kctl->private_value = nid;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001739 err = snd_ctl_add(codec->bus->card, kctl);
1740 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 return err;
1742 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02001743 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01001744 snd_hda_codec_read(codec, nid, 0,
1745 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02001746 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 return 0;
1748}
1749
Takashi Iwaicb53c622007-08-10 17:21:45 +02001750#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001751/*
1752 * command cache
1753 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001755/* build a 32bit cache key with the widget id and the command parameter */
1756#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
1757#define get_cmd_cache_nid(key) ((key) & 0xff)
1758#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
1759
1760/**
1761 * snd_hda_codec_write_cache - send a single command with caching
1762 * @codec: the HDA codec
1763 * @nid: NID to send the command
1764 * @direct: direct flag
1765 * @verb: the verb to send
1766 * @parm: the parameter for the verb
1767 *
1768 * Send a single command without waiting for response.
1769 *
1770 * Returns 0 if successful, or a negative error code.
1771 */
1772int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
1773 int direct, unsigned int verb, unsigned int parm)
1774{
1775 int err;
Takashi Iwaicb53c622007-08-10 17:21:45 +02001776 snd_hda_power_up(codec);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001777 mutex_lock(&codec->bus->cmd_mutex);
1778 err = codec->bus->ops.command(codec, nid, direct, verb, parm);
1779 if (!err) {
1780 struct hda_cache_head *c;
1781 u32 key = build_cmd_cache_key(nid, verb);
1782 c = get_alloc_hash(&codec->cmd_cache, key);
1783 if (c)
1784 c->val = parm;
1785 }
1786 mutex_unlock(&codec->bus->cmd_mutex);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001787 snd_hda_power_down(codec);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001788 return err;
1789}
1790
1791/* resume the all commands from the cache */
1792void snd_hda_codec_resume_cache(struct hda_codec *codec)
1793{
1794 struct hda_cache_head *buffer = codec->cmd_cache.buffer;
1795 int i;
1796
1797 for (i = 0; i < codec->cmd_cache.size; i++, buffer++) {
1798 u32 key = buffer->key;
1799 if (!key)
1800 continue;
1801 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
1802 get_cmd_cache_cmd(key), buffer->val);
1803 }
1804}
1805
1806/**
1807 * snd_hda_sequence_write_cache - sequence writes with caching
1808 * @codec: the HDA codec
1809 * @seq: VERB array to send
1810 *
1811 * Send the commands sequentially from the given array.
1812 * Thte commands are recorded on cache for power-save and resume.
1813 * The array must be terminated with NID=0.
1814 */
1815void snd_hda_sequence_write_cache(struct hda_codec *codec,
1816 const struct hda_verb *seq)
1817{
1818 for (; seq->nid; seq++)
1819 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
1820 seq->param);
1821}
Takashi Iwaicb53c622007-08-10 17:21:45 +02001822#endif /* SND_HDA_NEEDS_RESUME */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001823
Takashi Iwai54d17402005-11-21 16:33:22 +01001824/*
1825 * set power state of the codec
1826 */
1827static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1828 unsigned int power_state)
1829{
Takashi Iwaicb53c622007-08-10 17:21:45 +02001830 hda_nid_t nid;
1831 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01001832
1833 snd_hda_codec_write(codec, fg, 0, AC_VERB_SET_POWER_STATE,
1834 power_state);
Marc Boucherd2595d82008-01-22 15:23:30 +01001835 msleep(10); /* partial workaround for "azx_get_response timeout" */
Takashi Iwai54d17402005-11-21 16:33:22 +01001836
Takashi Iwaicb53c622007-08-10 17:21:45 +02001837 nid = codec->start_nid;
1838 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01001839 unsigned int wcaps = get_wcaps(codec, nid);
1840 if (wcaps & AC_WCAP_POWER) {
1841 unsigned int wid_type = (wcaps & AC_WCAP_TYPE) >>
1842 AC_WCAP_TYPE_SHIFT;
1843 if (wid_type == AC_WID_PIN) {
1844 unsigned int pincap;
1845 /*
1846 * don't power down the widget if it controls
1847 * eapd and EAPD_BTLENABLE is set.
1848 */
1849 pincap = snd_hda_param_read(codec, nid,
1850 AC_PAR_PIN_CAP);
1851 if (pincap & AC_PINCAP_EAPD) {
1852 int eapd = snd_hda_codec_read(codec,
1853 nid, 0,
1854 AC_VERB_GET_EAPD_BTLENABLE, 0);
1855 eapd &= 0x02;
1856 if (power_state == AC_PWRST_D3 && eapd)
1857 continue;
1858 }
Takashi Iwai1194b5b2007-10-10 10:04:26 +02001859 }
Takashi Iwai54d17402005-11-21 16:33:22 +01001860 snd_hda_codec_write(codec, nid, 0,
1861 AC_VERB_SET_POWER_STATE,
1862 power_state);
Takashi Iwai1194b5b2007-10-10 10:04:26 +02001863 }
Takashi Iwai54d17402005-11-21 16:33:22 +01001864 }
1865
Takashi Iwaicb53c622007-08-10 17:21:45 +02001866 if (power_state == AC_PWRST_D0) {
1867 unsigned long end_time;
1868 int state;
Takashi Iwai54d17402005-11-21 16:33:22 +01001869 msleep(10);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001870 /* wait until the codec reachs to D0 */
1871 end_time = jiffies + msecs_to_jiffies(500);
1872 do {
1873 state = snd_hda_codec_read(codec, fg, 0,
1874 AC_VERB_GET_POWER_STATE, 0);
1875 if (state == power_state)
1876 break;
1877 msleep(1);
1878 } while (time_after_eq(end_time, jiffies));
1879 }
Takashi Iwai54d17402005-11-21 16:33:22 +01001880}
1881
Takashi Iwaicb53c622007-08-10 17:21:45 +02001882#ifdef SND_HDA_NEEDS_RESUME
1883/*
1884 * call suspend and power-down; used both from PM and power-save
1885 */
1886static void hda_call_codec_suspend(struct hda_codec *codec)
1887{
1888 if (codec->patch_ops.suspend)
1889 codec->patch_ops.suspend(codec, PMSG_SUSPEND);
1890 hda_set_power_state(codec,
1891 codec->afg ? codec->afg : codec->mfg,
1892 AC_PWRST_D3);
1893#ifdef CONFIG_SND_HDA_POWER_SAVE
1894 cancel_delayed_work(&codec->power_work);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02001895 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02001896 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02001897#endif
1898}
1899
1900/*
1901 * kick up codec; used both from PM and power-save
1902 */
1903static void hda_call_codec_resume(struct hda_codec *codec)
1904{
1905 hda_set_power_state(codec,
1906 codec->afg ? codec->afg : codec->mfg,
1907 AC_PWRST_D0);
1908 if (codec->patch_ops.resume)
1909 codec->patch_ops.resume(codec);
1910 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02001911 if (codec->patch_ops.init)
1912 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001913 snd_hda_codec_resume_amp(codec);
1914 snd_hda_codec_resume_cache(codec);
1915 }
1916}
1917#endif /* SND_HDA_NEEDS_RESUME */
1918
Takashi Iwai54d17402005-11-21 16:33:22 +01001919
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920/**
1921 * snd_hda_build_controls - build mixer controls
1922 * @bus: the BUS
1923 *
1924 * Creates mixer controls for each codec included in the bus.
1925 *
1926 * Returns 0 if successful, otherwise a negative error code.
1927 */
Takashi Iwai756e2b02007-04-16 11:27:07 +02001928int __devinit snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001930 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
Takashi Iwai0ba21762007-04-16 11:29:14 +02001932 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaicb53c622007-08-10 17:21:45 +02001933 int err = 0;
1934 /* fake as if already powered-on */
1935 hda_keep_power_on(codec);
1936 /* then fire up */
1937 hda_set_power_state(codec,
1938 codec->afg ? codec->afg : codec->mfg,
1939 AC_PWRST_D0);
1940 /* continue to initialize... */
1941 if (codec->patch_ops.init)
1942 err = codec->patch_ops.init(codec);
1943 if (!err && codec->patch_ops.build_controls)
1944 err = codec->patch_ops.build_controls(codec);
1945 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 if (err < 0)
1947 return err;
1948 }
1949
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 return 0;
1951}
1952
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953/*
1954 * stream formats
1955 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02001956struct hda_rate_tbl {
1957 unsigned int hz;
1958 unsigned int alsa_bits;
1959 unsigned int hda_fmt;
1960};
1961
1962static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02001964
1965 /* autodetected value used in snd_hda_query_supported_pcm */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 { 8000, SNDRV_PCM_RATE_8000, 0x0500 }, /* 1/6 x 48 */
1967 { 11025, SNDRV_PCM_RATE_11025, 0x4300 }, /* 1/4 x 44 */
1968 { 16000, SNDRV_PCM_RATE_16000, 0x0200 }, /* 1/3 x 48 */
1969 { 22050, SNDRV_PCM_RATE_22050, 0x4100 }, /* 1/2 x 44 */
1970 { 32000, SNDRV_PCM_RATE_32000, 0x0a00 }, /* 2/3 x 48 */
1971 { 44100, SNDRV_PCM_RATE_44100, 0x4000 }, /* 44 */
1972 { 48000, SNDRV_PCM_RATE_48000, 0x0000 }, /* 48 */
1973 { 88200, SNDRV_PCM_RATE_88200, 0x4800 }, /* 2 x 44 */
1974 { 96000, SNDRV_PCM_RATE_96000, 0x0800 }, /* 2 x 48 */
1975 { 176400, SNDRV_PCM_RATE_176400, 0x5800 },/* 4 x 44 */
1976 { 192000, SNDRV_PCM_RATE_192000, 0x1800 }, /* 4 x 48 */
Takashi Iwaia961f9f2007-04-12 13:08:09 +02001977#define AC_PAR_PCM_RATE_BITS 11
1978 /* up to bits 10, 384kHZ isn't supported properly */
1979
1980 /* not autodetected value */
1981 { 9600, SNDRV_PCM_RATE_KNOT, 0x0400 }, /* 1/5 x 48 */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02001982
Takashi Iwaibefdf312005-08-22 13:57:55 +02001983 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984};
1985
1986/**
1987 * snd_hda_calc_stream_format - calculate format bitset
1988 * @rate: the sample rate
1989 * @channels: the number of channels
1990 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
1991 * @maxbps: the max. bps
1992 *
1993 * Calculate the format bitset from the given rate, channels and th PCM format.
1994 *
1995 * Return zero if invalid.
1996 */
1997unsigned int snd_hda_calc_stream_format(unsigned int rate,
1998 unsigned int channels,
1999 unsigned int format,
2000 unsigned int maxbps)
2001{
2002 int i;
2003 unsigned int val = 0;
2004
Takashi Iwaibefdf312005-08-22 13:57:55 +02002005 for (i = 0; rate_bits[i].hz; i++)
2006 if (rate_bits[i].hz == rate) {
2007 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 break;
2009 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002010 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 snd_printdd("invalid rate %d\n", rate);
2012 return 0;
2013 }
2014
2015 if (channels == 0 || channels > 8) {
2016 snd_printdd("invalid channels %d\n", channels);
2017 return 0;
2018 }
2019 val |= channels - 1;
2020
2021 switch (snd_pcm_format_width(format)) {
2022 case 8: val |= 0x00; break;
2023 case 16: val |= 0x10; break;
2024 case 20:
2025 case 24:
2026 case 32:
2027 if (maxbps >= 32)
2028 val |= 0x40;
2029 else if (maxbps >= 24)
2030 val |= 0x30;
2031 else
2032 val |= 0x20;
2033 break;
2034 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002035 snd_printdd("invalid format width %d\n",
2036 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 return 0;
2038 }
2039
2040 return val;
2041}
2042
2043/**
2044 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
2045 * @codec: the HDA codec
2046 * @nid: NID to query
2047 * @ratesp: the pointer to store the detected rate bitflags
2048 * @formatsp: the pointer to store the detected formats
2049 * @bpsp: the pointer to store the detected format widths
2050 *
2051 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
2052 * or @bsps argument is ignored.
2053 *
2054 * Returns 0 if successful, otherwise a negative error code.
2055 */
2056int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
2057 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
2058{
2059 int i;
2060 unsigned int val, streams;
2061
2062 val = 0;
2063 if (nid != codec->afg &&
Takashi Iwai54d17402005-11-21 16:33:22 +01002064 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
2066 if (val == -1)
2067 return -EIO;
2068 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002069 if (!val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
2071
2072 if (ratesp) {
2073 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02002074 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02002076 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 }
2078 *ratesp = rates;
2079 }
2080
2081 if (formatsp || bpsp) {
2082 u64 formats = 0;
2083 unsigned int bps;
2084 unsigned int wcaps;
2085
Takashi Iwai54d17402005-11-21 16:33:22 +01002086 wcaps = get_wcaps(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
2088 if (streams == -1)
2089 return -EIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002090 if (!streams) {
2091 streams = snd_hda_param_read(codec, codec->afg,
2092 AC_PAR_STREAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 if (streams == -1)
2094 return -EIO;
2095 }
2096
2097 bps = 0;
2098 if (streams & AC_SUPFMT_PCM) {
2099 if (val & AC_SUPPCM_BITS_8) {
2100 formats |= SNDRV_PCM_FMTBIT_U8;
2101 bps = 8;
2102 }
2103 if (val & AC_SUPPCM_BITS_16) {
2104 formats |= SNDRV_PCM_FMTBIT_S16_LE;
2105 bps = 16;
2106 }
2107 if (wcaps & AC_WCAP_DIGITAL) {
2108 if (val & AC_SUPPCM_BITS_32)
2109 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
2110 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
2111 formats |= SNDRV_PCM_FMTBIT_S32_LE;
2112 if (val & AC_SUPPCM_BITS_24)
2113 bps = 24;
2114 else if (val & AC_SUPPCM_BITS_20)
2115 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002116 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
2117 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 formats |= SNDRV_PCM_FMTBIT_S32_LE;
2119 if (val & AC_SUPPCM_BITS_32)
2120 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 else if (val & AC_SUPPCM_BITS_24)
2122 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02002123 else if (val & AC_SUPPCM_BITS_20)
2124 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 }
2126 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002127 else if (streams == AC_SUPFMT_FLOAT32) {
2128 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
2130 bps = 32;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002131 } else if (streams == AC_SUPFMT_AC3) {
2132 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 /* temporary hack: we have still no proper support
2134 * for the direct AC3 stream...
2135 */
2136 formats |= SNDRV_PCM_FMTBIT_U8;
2137 bps = 8;
2138 }
2139 if (formatsp)
2140 *formatsp = formats;
2141 if (bpsp)
2142 *bpsp = bps;
2143 }
2144
2145 return 0;
2146}
2147
2148/**
Takashi Iwai0ba21762007-04-16 11:29:14 +02002149 * snd_hda_is_supported_format - check whether the given node supports
2150 * the format val
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 *
2152 * Returns 1 if supported, 0 if not.
2153 */
2154int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
2155 unsigned int format)
2156{
2157 int i;
2158 unsigned int val = 0, rate, stream;
2159
2160 if (nid != codec->afg &&
Takashi Iwai54d17402005-11-21 16:33:22 +01002161 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
2163 if (val == -1)
2164 return 0;
2165 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002166 if (!val) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
2168 if (val == -1)
2169 return 0;
2170 }
2171
2172 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02002173 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02002174 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 if (val & (1 << i))
2176 break;
2177 return 0;
2178 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02002179 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 return 0;
2181
2182 stream = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
2183 if (stream == -1)
2184 return 0;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002185 if (!stream && nid != codec->afg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 stream = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002187 if (!stream || stream == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 return 0;
2189
2190 if (stream & AC_SUPFMT_PCM) {
2191 switch (format & 0xf0) {
2192 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002193 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 return 0;
2195 break;
2196 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002197 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 return 0;
2199 break;
2200 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002201 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 return 0;
2203 break;
2204 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002205 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 return 0;
2207 break;
2208 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002209 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 return 0;
2211 break;
2212 default:
2213 return 0;
2214 }
2215 } else {
2216 /* FIXME: check for float32 and AC3? */
2217 }
2218
2219 return 1;
2220}
2221
2222/*
2223 * PCM stuff
2224 */
2225static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
2226 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002227 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228{
2229 return 0;
2230}
2231
2232static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
2233 struct hda_codec *codec,
2234 unsigned int stream_tag,
2235 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002236 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237{
2238 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
2239 return 0;
2240}
2241
2242static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
2243 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002244 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245{
Takashi Iwai888afa12008-03-18 09:57:50 +01002246 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 return 0;
2248}
2249
Takashi Iwai0ba21762007-04-16 11:29:14 +02002250static int __devinit set_pcm_default_values(struct hda_codec *codec,
2251 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252{
Takashi Iwai0ba21762007-04-16 11:29:14 +02002253 /* query support PCM information from the given NID */
2254 if (info->nid && (!info->rates || !info->formats)) {
2255 snd_hda_query_supported_pcm(codec, info->nid,
2256 info->rates ? NULL : &info->rates,
2257 info->formats ? NULL : &info->formats,
2258 info->maxbps ? NULL : &info->maxbps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 }
2260 if (info->ops.open == NULL)
2261 info->ops.open = hda_pcm_default_open_close;
2262 if (info->ops.close == NULL)
2263 info->ops.close = hda_pcm_default_open_close;
2264 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02002265 if (snd_BUG_ON(!info->nid))
2266 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 info->ops.prepare = hda_pcm_default_prepare;
2268 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02002270 if (snd_BUG_ON(!info->nid))
2271 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 info->ops.cleanup = hda_pcm_default_cleanup;
2273 }
2274 return 0;
2275}
2276
2277/**
2278 * snd_hda_build_pcms - build PCM information
2279 * @bus: the BUS
2280 *
2281 * Create PCM information for each codec included in the bus.
2282 *
2283 * The build_pcms codec patch is requested to set up codec->num_pcms and
2284 * codec->pcm_info properly. The array is referred by the top-level driver
2285 * to create its PCM instances.
2286 * The allocated codec->pcm_info should be released in codec->patch_ops.free
2287 * callback.
2288 *
2289 * At least, substreams, channels_min and channels_max must be filled for
2290 * each stream. substreams = 0 indicates that the stream doesn't exist.
2291 * When rates and/or formats are zero, the supported values are queried
2292 * from the given nid. The nid is used also by the default ops.prepare
2293 * and ops.cleanup callbacks.
2294 *
2295 * The driver needs to call ops.open in its open callback. Similarly,
2296 * ops.close is supposed to be called in the close callback.
2297 * ops.prepare should be called in the prepare or hw_params callback
2298 * with the proper parameters for set up.
2299 * ops.cleanup should be called in hw_free for clean up of streams.
2300 *
2301 * This function returns 0 if successfull, or a negative error code.
2302 */
Takashi Iwai756e2b02007-04-16 11:27:07 +02002303int __devinit snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304{
Takashi Iwai0ba21762007-04-16 11:29:14 +02002305 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306
Takashi Iwai0ba21762007-04-16 11:29:14 +02002307 list_for_each_entry(codec, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 unsigned int pcm, s;
2309 int err;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002310 if (!codec->patch_ops.build_pcms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 continue;
2312 err = codec->patch_ops.build_pcms(codec);
2313 if (err < 0)
2314 return err;
2315 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2316 for (s = 0; s < 2; s++) {
2317 struct hda_pcm_stream *info;
2318 info = &codec->pcm_info[pcm].stream[s];
Takashi Iwai0ba21762007-04-16 11:29:14 +02002319 if (!info->substreams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 continue;
2321 err = set_pcm_default_values(codec, info);
2322 if (err < 0)
2323 return err;
2324 }
2325 }
2326 }
2327 return 0;
2328}
2329
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330/**
2331 * snd_hda_check_board_config - compare the current codec with the config table
2332 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002333 * @num_configs: number of config enums
2334 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 * @tbl: configuration table, terminated by null entries
2336 *
2337 * Compares the modelname or PCI subsystem id of the current codec with the
2338 * given configuration table. If a matching entry is found, returns its
2339 * config value (supposed to be 0 or positive).
2340 *
2341 * If no entries are matching, the function returns a negative value.
2342 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002343int snd_hda_check_board_config(struct hda_codec *codec,
2344 int num_configs, const char **models,
2345 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346{
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002347 if (codec->bus->modelname && models) {
2348 int i;
2349 for (i = 0; i < num_configs; i++) {
2350 if (models[i] &&
2351 !strcmp(codec->bus->modelname, models[i])) {
2352 snd_printd(KERN_INFO "hda_codec: model '%s' is "
2353 "selected\n", models[i]);
2354 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 }
2356 }
2357 }
2358
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002359 if (!codec->bus->pci || !tbl)
2360 return -1;
2361
2362 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
2363 if (!tbl)
2364 return -1;
2365 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02002366#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002367 char tmp[10];
2368 const char *model = NULL;
2369 if (models)
2370 model = models[tbl->value];
2371 if (!model) {
2372 sprintf(tmp, "#%d", tbl->value);
2373 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002375 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
2376 "for config %x:%x (%s)\n",
2377 model, tbl->subvendor, tbl->subdevice,
2378 (tbl->name ? tbl->name : "Unknown device"));
2379#endif
2380 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 }
2382 return -1;
2383}
2384
2385/**
2386 * snd_hda_add_new_ctls - create controls from the array
2387 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002388 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 *
2390 * This helper function creates and add new controls in the given array.
2391 * The array must be terminated with an empty entry as terminator.
2392 *
2393 * Returns 0 if successful, or a negative error code.
2394 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002395int snd_hda_add_new_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396{
Takashi Iwaicb53c622007-08-10 17:21:45 +02002397 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398
2399 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01002400 struct snd_kcontrol *kctl;
2401 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002402 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01002403 return -ENOMEM;
2404 err = snd_ctl_add(codec->bus->card, kctl);
2405 if (err < 0) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002406 if (!codec->addr)
Takashi Iwai54d17402005-11-21 16:33:22 +01002407 return err;
2408 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002409 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01002410 return -ENOMEM;
2411 kctl->id.device = codec->addr;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002412 err = snd_ctl_add(codec->bus->card, kctl);
2413 if (err < 0)
Takashi Iwai54d17402005-11-21 16:33:22 +01002414 return err;
2415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 }
2417 return 0;
2418}
2419
Takashi Iwaicb53c622007-08-10 17:21:45 +02002420#ifdef CONFIG_SND_HDA_POWER_SAVE
2421static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2422 unsigned int power_state);
2423
2424static void hda_power_work(struct work_struct *work)
2425{
2426 struct hda_codec *codec =
2427 container_of(work, struct hda_codec, power_work.work);
2428
Maxim Levitsky2e492462007-09-03 15:26:57 +02002429 if (!codec->power_on || codec->power_count) {
2430 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002431 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02002432 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02002433
2434 hda_call_codec_suspend(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002435 if (codec->bus->ops.pm_notify)
2436 codec->bus->ops.pm_notify(codec);
2437}
2438
2439static void hda_keep_power_on(struct hda_codec *codec)
2440{
2441 codec->power_count++;
2442 codec->power_on = 1;
2443}
2444
2445void snd_hda_power_up(struct hda_codec *codec)
2446{
2447 codec->power_count++;
Takashi Iwaia221e282007-08-16 16:35:33 +02002448 if (codec->power_on || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02002449 return;
2450
2451 codec->power_on = 1;
2452 if (codec->bus->ops.pm_notify)
2453 codec->bus->ops.pm_notify(codec);
2454 hda_call_codec_resume(codec);
2455 cancel_delayed_work(&codec->power_work);
Takashi Iwaia221e282007-08-16 16:35:33 +02002456 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002457}
2458
2459void snd_hda_power_down(struct hda_codec *codec)
2460{
2461 --codec->power_count;
Takashi Iwaia221e282007-08-16 16:35:33 +02002462 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02002463 return;
Takashi Iwaia221e282007-08-16 16:35:33 +02002464 if (power_save) {
2465 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002466 schedule_delayed_work(&codec->power_work,
2467 msecs_to_jiffies(power_save * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02002468 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02002469}
2470
2471int snd_hda_check_amp_list_power(struct hda_codec *codec,
2472 struct hda_loopback_check *check,
2473 hda_nid_t nid)
2474{
2475 struct hda_amp_list *p;
2476 int ch, v;
2477
2478 if (!check->amplist)
2479 return 0;
2480 for (p = check->amplist; p->nid; p++) {
2481 if (p->nid == nid)
2482 break;
2483 }
2484 if (!p->nid)
2485 return 0; /* nothing changed */
2486
2487 for (p = check->amplist; p->nid; p++) {
2488 for (ch = 0; ch < 2; ch++) {
2489 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
2490 p->idx);
2491 if (!(v & HDA_AMP_MUTE) && v > 0) {
2492 if (!check->power_on) {
2493 check->power_on = 1;
2494 snd_hda_power_up(codec);
2495 }
2496 return 1;
2497 }
2498 }
2499 }
2500 if (check->power_on) {
2501 check->power_on = 0;
2502 snd_hda_power_down(codec);
2503 }
2504 return 0;
2505}
2506#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002508/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002509 * Channel mode helper
2510 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002511int snd_hda_ch_mode_info(struct hda_codec *codec,
2512 struct snd_ctl_elem_info *uinfo,
2513 const struct hda_channel_mode *chmode,
2514 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002515{
2516 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2517 uinfo->count = 1;
2518 uinfo->value.enumerated.items = num_chmodes;
2519 if (uinfo->value.enumerated.item >= num_chmodes)
2520 uinfo->value.enumerated.item = num_chmodes - 1;
2521 sprintf(uinfo->value.enumerated.name, "%dch",
2522 chmode[uinfo->value.enumerated.item].channels);
2523 return 0;
2524}
2525
Takashi Iwai0ba21762007-04-16 11:29:14 +02002526int snd_hda_ch_mode_get(struct hda_codec *codec,
2527 struct snd_ctl_elem_value *ucontrol,
2528 const struct hda_channel_mode *chmode,
2529 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002530 int max_channels)
2531{
2532 int i;
2533
2534 for (i = 0; i < num_chmodes; i++) {
2535 if (max_channels == chmode[i].channels) {
2536 ucontrol->value.enumerated.item[0] = i;
2537 break;
2538 }
2539 }
2540 return 0;
2541}
2542
Takashi Iwai0ba21762007-04-16 11:29:14 +02002543int snd_hda_ch_mode_put(struct hda_codec *codec,
2544 struct snd_ctl_elem_value *ucontrol,
2545 const struct hda_channel_mode *chmode,
2546 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002547 int *max_channelsp)
2548{
2549 unsigned int mode;
2550
2551 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01002552 if (mode >= num_chmodes)
2553 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002554 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002555 return 0;
2556 /* change the current channel setting */
2557 *max_channelsp = chmode[mode].channels;
2558 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002559 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002560 return 1;
2561}
2562
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563/*
2564 * input MUX helper
2565 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002566int snd_hda_input_mux_info(const struct hda_input_mux *imux,
2567 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568{
2569 unsigned int index;
2570
2571 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2572 uinfo->count = 1;
2573 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02002574 if (!imux->num_items)
2575 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 index = uinfo->value.enumerated.item;
2577 if (index >= imux->num_items)
2578 index = imux->num_items - 1;
2579 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
2580 return 0;
2581}
2582
Takashi Iwai0ba21762007-04-16 11:29:14 +02002583int snd_hda_input_mux_put(struct hda_codec *codec,
2584 const struct hda_input_mux *imux,
2585 struct snd_ctl_elem_value *ucontrol,
2586 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 unsigned int *cur_val)
2588{
2589 unsigned int idx;
2590
Takashi Iwai5513b0c2007-10-09 11:58:41 +02002591 if (!imux->num_items)
2592 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 idx = ucontrol->value.enumerated.item[0];
2594 if (idx >= imux->num_items)
2595 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002596 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002598 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
2599 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 *cur_val = idx;
2601 return 1;
2602}
2603
2604
2605/*
2606 * Multi-channel / digital-out PCM helper functions
2607 */
2608
Takashi Iwai6b97eb42007-04-05 14:51:48 +02002609/* setup SPDIF output stream */
2610static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
2611 unsigned int stream_tag, unsigned int format)
2612{
Matthew Ranostayde51ca12008-09-07 14:31:40 -04002613 hda_nid_t *d;
2614
Takashi Iwai6b97eb42007-04-05 14:51:48 +02002615 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Matthew Ranostayde51ca12008-09-07 14:31:40 -04002616 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE)) {
Takashi Iwai6b97eb42007-04-05 14:51:48 +02002617 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1,
Matthew Ranostayde51ca12008-09-07 14:31:40 -04002618 codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff);
2619
2620 for (d = codec->slave_dig_outs; *d; d++)
2621 snd_hda_codec_write(codec, *d, 0,
2622 AC_VERB_SET_DIGI_CONVERT_1,
Takashi Iwai6b97eb42007-04-05 14:51:48 +02002623 codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04002624 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02002625 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
2626 /* turn on again (if needed) */
Matthew Ranostayde51ca12008-09-07 14:31:40 -04002627 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE)) {
Takashi Iwai6b97eb42007-04-05 14:51:48 +02002628 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1,
2629 codec->spdif_ctls & 0xff);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04002630
2631 for (d = codec->slave_dig_outs; *d; d++)
2632 snd_hda_codec_write(codec, *d, 0,
2633 AC_VERB_SET_DIGI_CONVERT_1,
2634 codec->spdif_ctls & 0xff);
2635 }
2636
Takashi Iwai6b97eb42007-04-05 14:51:48 +02002637}
2638
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639/*
2640 * open the digital out in the exclusive mode
2641 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002642int snd_hda_multi_out_dig_open(struct hda_codec *codec,
2643 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644{
Ingo Molnar62932df2006-01-16 16:34:20 +01002645 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02002646 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
2647 /* already opened as analog dup; reset it once */
Takashi Iwai888afa12008-03-18 09:57:50 +01002648 snd_hda_codec_cleanup_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01002650 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 return 0;
2652}
2653
Takashi Iwai6b97eb42007-04-05 14:51:48 +02002654int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
2655 struct hda_multi_out *mout,
2656 unsigned int stream_tag,
2657 unsigned int format,
2658 struct snd_pcm_substream *substream)
2659{
Matthew Ranostayde51ca12008-09-07 14:31:40 -04002660 hda_nid_t *nid;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02002661 mutex_lock(&codec->spdif_mutex);
2662 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04002663 if (codec->slave_dig_outs)
2664 for (nid = codec->slave_dig_outs; *nid; nid++)
2665 setup_dig_out_stream(codec, *nid, stream_tag, format);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02002666 mutex_unlock(&codec->spdif_mutex);
2667 return 0;
2668}
2669
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670/*
2671 * release the digital out
2672 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002673int snd_hda_multi_out_dig_close(struct hda_codec *codec,
2674 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675{
Ingo Molnar62932df2006-01-16 16:34:20 +01002676 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01002678 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 return 0;
2680}
2681
2682/*
2683 * set up more restrictions for analog out
2684 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002685int snd_hda_multi_out_analog_open(struct hda_codec *codec,
2686 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01002687 struct snd_pcm_substream *substream,
2688 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689{
Takashi Iwai9a081602008-02-12 18:37:26 +01002690 struct snd_pcm_runtime *runtime = substream->runtime;
2691 runtime->hw.channels_max = mout->max_channels;
2692 if (mout->dig_out_nid) {
2693 if (!mout->analog_rates) {
2694 mout->analog_rates = hinfo->rates;
2695 mout->analog_formats = hinfo->formats;
2696 mout->analog_maxbps = hinfo->maxbps;
2697 } else {
2698 runtime->hw.rates = mout->analog_rates;
2699 runtime->hw.formats = mout->analog_formats;
2700 hinfo->maxbps = mout->analog_maxbps;
2701 }
2702 if (!mout->spdif_rates) {
2703 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
2704 &mout->spdif_rates,
2705 &mout->spdif_formats,
2706 &mout->spdif_maxbps);
2707 }
2708 mutex_lock(&codec->spdif_mutex);
2709 if (mout->share_spdif) {
2710 runtime->hw.rates &= mout->spdif_rates;
2711 runtime->hw.formats &= mout->spdif_formats;
2712 if (mout->spdif_maxbps < hinfo->maxbps)
2713 hinfo->maxbps = mout->spdif_maxbps;
2714 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02002715 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01002716 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 return snd_pcm_hw_constraint_step(substream->runtime, 0,
2718 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
2719}
2720
2721/*
2722 * set up the i/o for analog out
2723 * when the digital out is available, copy the front out to digital out, too.
2724 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002725int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
2726 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 unsigned int stream_tag,
2728 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002729 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730{
2731 hda_nid_t *nids = mout->dac_nids;
Matthew Ranostayde51ca12008-09-07 14:31:40 -04002732 hda_nid_t *d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 int chs = substream->runtime->channels;
2734 int i;
2735
Ingo Molnar62932df2006-01-16 16:34:20 +01002736 mutex_lock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01002737 if (mout->dig_out_nid && mout->share_spdif &&
2738 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02002740 snd_hda_is_supported_format(codec, mout->dig_out_nid,
2741 format) &&
2742 !(codec->spdif_status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02002744 setup_dig_out_stream(codec, mout->dig_out_nid,
2745 stream_tag, format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04002746 if (codec->slave_dig_outs)
2747 for (d = codec->slave_dig_outs; *d; d++)
2748 setup_dig_out_stream(codec, *d,
2749 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 } else {
2751 mout->dig_out_used = 0;
Takashi Iwai888afa12008-03-18 09:57:50 +01002752 snd_hda_codec_cleanup_stream(codec, mout->dig_out_nid);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04002753 if (codec->slave_dig_outs)
2754 for (d = codec->slave_dig_outs; *d; d++)
2755 snd_hda_codec_cleanup_stream(codec, *d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 }
2757 }
Ingo Molnar62932df2006-01-16 16:34:20 +01002758 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759
2760 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002761 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
2762 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02002763 if (!mout->no_share_stream &&
2764 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002766 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
2767 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01002768 /* extra outputs copied from front */
2769 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02002770 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01002771 snd_hda_codec_setup_stream(codec,
2772 mout->extra_out_nid[i],
2773 stream_tag, 0, format);
2774
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 /* surrounds */
2776 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02002777 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002778 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
2779 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02002780 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002781 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
2782 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 }
2784 return 0;
2785}
2786
2787/*
2788 * clean up the setting for analog out
2789 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002790int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
2791 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792{
2793 hda_nid_t *nids = mout->dac_nids;
2794 int i;
2795
2796 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01002797 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01002799 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwai82bc9552006-03-21 11:24:42 +01002800 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
2801 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01002802 snd_hda_codec_cleanup_stream(codec,
2803 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01002804 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai888afa12008-03-18 09:57:50 +01002806 snd_hda_codec_cleanup_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 mout->dig_out_used = 0;
2808 }
Ingo Molnar62932df2006-01-16 16:34:20 +01002809 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 return 0;
2811}
2812
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002813/*
2814 * Helper for automatic ping configuration
2815 */
Kailang Yangdf694da2005-12-05 19:42:22 +01002816
Takashi Iwai12f288b2007-08-02 15:51:59 +02002817static int is_in_nid_list(hda_nid_t nid, hda_nid_t *list)
Kailang Yangdf694da2005-12-05 19:42:22 +01002818{
2819 for (; *list; list++)
2820 if (*list == nid)
2821 return 1;
2822 return 0;
2823}
2824
Steve Longerbeam81937d32007-05-08 15:33:03 +02002825
2826/*
2827 * Sort an associated group of pins according to their sequence numbers.
2828 */
2829static void sort_pins_by_sequence(hda_nid_t * pins, short * sequences,
2830 int num_pins)
2831{
2832 int i, j;
2833 short seq;
2834 hda_nid_t nid;
2835
2836 for (i = 0; i < num_pins; i++) {
2837 for (j = i + 1; j < num_pins; j++) {
2838 if (sequences[i] > sequences[j]) {
2839 seq = sequences[i];
2840 sequences[i] = sequences[j];
2841 sequences[j] = seq;
2842 nid = pins[i];
2843 pins[i] = pins[j];
2844 pins[j] = nid;
2845 }
2846 }
2847 }
2848}
2849
2850
Takashi Iwai82bc9552006-03-21 11:24:42 +01002851/*
2852 * Parse all pin widgets and store the useful pin nids to cfg
2853 *
2854 * The number of line-outs or any primary output is stored in line_outs,
2855 * and the corresponding output pins are assigned to line_out_pins[],
2856 * in the order of front, rear, CLFE, side, ...
2857 *
2858 * If more extra outputs (speaker and headphone) are found, the pins are
Takashi Iwaieb06ed82006-09-20 17:10:27 +02002859 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
Takashi Iwai82bc9552006-03-21 11:24:42 +01002860 * is detected, one of speaker of HP pins is assigned as the primary
2861 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
2862 * if any analog output exists.
2863 *
2864 * The analog input pins are assigned to input_pins array.
2865 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
2866 * respectively.
2867 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002868int snd_hda_parse_pin_def_config(struct hda_codec *codec,
2869 struct auto_pin_cfg *cfg,
2870 hda_nid_t *ignore_nids)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002871{
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01002872 hda_nid_t nid, end_nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02002873 short seq, assoc_line_out, assoc_speaker;
2874 short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
2875 short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
Takashi Iwaif889fa92007-10-31 15:49:32 +01002876 short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002877
2878 memset(cfg, 0, sizeof(*cfg));
2879
Steve Longerbeam81937d32007-05-08 15:33:03 +02002880 memset(sequences_line_out, 0, sizeof(sequences_line_out));
2881 memset(sequences_speaker, 0, sizeof(sequences_speaker));
Takashi Iwaif889fa92007-10-31 15:49:32 +01002882 memset(sequences_hp, 0, sizeof(sequences_hp));
Steve Longerbeam81937d32007-05-08 15:33:03 +02002883 assoc_line_out = assoc_speaker = 0;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002884
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01002885 end_nid = codec->start_nid + codec->num_nodes;
2886 for (nid = codec->start_nid; nid < end_nid; nid++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01002887 unsigned int wid_caps = get_wcaps(codec, nid);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002888 unsigned int wid_type =
2889 (wid_caps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002890 unsigned int def_conf;
2891 short assoc, loc;
2892
2893 /* read all default configuration for pin complex */
2894 if (wid_type != AC_WID_PIN)
2895 continue;
Kailang Yangdf694da2005-12-05 19:42:22 +01002896 /* ignore the given nids (e.g. pc-beep returns error) */
2897 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
2898 continue;
2899
Takashi Iwai0ba21762007-04-16 11:29:14 +02002900 def_conf = snd_hda_codec_read(codec, nid, 0,
2901 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002902 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
2903 continue;
2904 loc = get_defcfg_location(def_conf);
2905 switch (get_defcfg_device(def_conf)) {
2906 case AC_JACK_LINE_OUT:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002907 seq = get_defcfg_sequence(def_conf);
2908 assoc = get_defcfg_association(def_conf);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01002909
2910 if (!(wid_caps & AC_WCAP_STEREO))
2911 if (!cfg->mono_out_pin)
2912 cfg->mono_out_pin = nid;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002913 if (!assoc)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002914 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002915 if (!assoc_line_out)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002916 assoc_line_out = assoc;
2917 else if (assoc_line_out != assoc)
2918 continue;
2919 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
2920 continue;
2921 cfg->line_out_pins[cfg->line_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02002922 sequences_line_out[cfg->line_outs] = seq;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002923 cfg->line_outs++;
2924 break;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01002925 case AC_JACK_SPEAKER:
Steve Longerbeam81937d32007-05-08 15:33:03 +02002926 seq = get_defcfg_sequence(def_conf);
2927 assoc = get_defcfg_association(def_conf);
2928 if (! assoc)
2929 continue;
2930 if (! assoc_speaker)
2931 assoc_speaker = assoc;
2932 else if (assoc_speaker != assoc)
2933 continue;
Takashi Iwai82bc9552006-03-21 11:24:42 +01002934 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
2935 continue;
2936 cfg->speaker_pins[cfg->speaker_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02002937 sequences_speaker[cfg->speaker_outs] = seq;
Takashi Iwai82bc9552006-03-21 11:24:42 +01002938 cfg->speaker_outs++;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01002939 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002940 case AC_JACK_HP_OUT:
Takashi Iwaif889fa92007-10-31 15:49:32 +01002941 seq = get_defcfg_sequence(def_conf);
2942 assoc = get_defcfg_association(def_conf);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02002943 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
2944 continue;
2945 cfg->hp_pins[cfg->hp_outs] = nid;
Takashi Iwaif889fa92007-10-31 15:49:32 +01002946 sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
Takashi Iwaieb06ed82006-09-20 17:10:27 +02002947 cfg->hp_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002948 break;
Takashi Iwai314634b2006-09-21 11:56:18 +02002949 case AC_JACK_MIC_IN: {
2950 int preferred, alt;
2951 if (loc == AC_JACK_LOC_FRONT) {
2952 preferred = AUTO_PIN_FRONT_MIC;
2953 alt = AUTO_PIN_MIC;
2954 } else {
2955 preferred = AUTO_PIN_MIC;
2956 alt = AUTO_PIN_FRONT_MIC;
2957 }
2958 if (!cfg->input_pins[preferred])
2959 cfg->input_pins[preferred] = nid;
2960 else if (!cfg->input_pins[alt])
2961 cfg->input_pins[alt] = nid;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002962 break;
Takashi Iwai314634b2006-09-21 11:56:18 +02002963 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002964 case AC_JACK_LINE_IN:
2965 if (loc == AC_JACK_LOC_FRONT)
2966 cfg->input_pins[AUTO_PIN_FRONT_LINE] = nid;
2967 else
2968 cfg->input_pins[AUTO_PIN_LINE] = nid;
2969 break;
2970 case AC_JACK_CD:
2971 cfg->input_pins[AUTO_PIN_CD] = nid;
2972 break;
2973 case AC_JACK_AUX:
2974 cfg->input_pins[AUTO_PIN_AUX] = nid;
2975 break;
2976 case AC_JACK_SPDIF_OUT:
2977 cfg->dig_out_pin = nid;
2978 break;
2979 case AC_JACK_SPDIF_IN:
2980 cfg->dig_in_pin = nid;
2981 break;
2982 }
2983 }
2984
Takashi Iwai5832fcf2008-02-12 18:30:12 +01002985 /* FIX-UP:
2986 * If no line-out is defined but multiple HPs are found,
2987 * some of them might be the real line-outs.
2988 */
2989 if (!cfg->line_outs && cfg->hp_outs > 1) {
2990 int i = 0;
2991 while (i < cfg->hp_outs) {
2992 /* The real HPs should have the sequence 0x0f */
2993 if ((sequences_hp[i] & 0x0f) == 0x0f) {
2994 i++;
2995 continue;
2996 }
2997 /* Move it to the line-out table */
2998 cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
2999 sequences_line_out[cfg->line_outs] = sequences_hp[i];
3000 cfg->line_outs++;
3001 cfg->hp_outs--;
3002 memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
3003 sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
3004 memmove(sequences_hp + i - 1, sequences_hp + i,
3005 sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
3006 }
3007 }
3008
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003009 /* sort by sequence */
Steve Longerbeam81937d32007-05-08 15:33:03 +02003010 sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
3011 cfg->line_outs);
3012 sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
3013 cfg->speaker_outs);
Takashi Iwaif889fa92007-10-31 15:49:32 +01003014 sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
3015 cfg->hp_outs);
Steve Longerbeam81937d32007-05-08 15:33:03 +02003016
Takashi Iwaif889fa92007-10-31 15:49:32 +01003017 /* if we have only one mic, make it AUTO_PIN_MIC */
3018 if (!cfg->input_pins[AUTO_PIN_MIC] &&
3019 cfg->input_pins[AUTO_PIN_FRONT_MIC]) {
3020 cfg->input_pins[AUTO_PIN_MIC] =
3021 cfg->input_pins[AUTO_PIN_FRONT_MIC];
3022 cfg->input_pins[AUTO_PIN_FRONT_MIC] = 0;
3023 }
3024 /* ditto for line-in */
3025 if (!cfg->input_pins[AUTO_PIN_LINE] &&
3026 cfg->input_pins[AUTO_PIN_FRONT_LINE]) {
3027 cfg->input_pins[AUTO_PIN_LINE] =
3028 cfg->input_pins[AUTO_PIN_FRONT_LINE];
3029 cfg->input_pins[AUTO_PIN_FRONT_LINE] = 0;
3030 }
3031
Steve Longerbeam81937d32007-05-08 15:33:03 +02003032 /*
3033 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
3034 * as a primary output
3035 */
3036 if (!cfg->line_outs) {
3037 if (cfg->speaker_outs) {
3038 cfg->line_outs = cfg->speaker_outs;
3039 memcpy(cfg->line_out_pins, cfg->speaker_pins,
3040 sizeof(cfg->speaker_pins));
3041 cfg->speaker_outs = 0;
3042 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
3043 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
3044 } else if (cfg->hp_outs) {
3045 cfg->line_outs = cfg->hp_outs;
3046 memcpy(cfg->line_out_pins, cfg->hp_pins,
3047 sizeof(cfg->hp_pins));
3048 cfg->hp_outs = 0;
3049 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
3050 cfg->line_out_type = AUTO_PIN_HP_OUT;
3051 }
3052 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003053
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02003054 /* Reorder the surround channels
3055 * ALSA sequence is front/surr/clfe/side
3056 * HDA sequence is:
3057 * 4-ch: front/surr => OK as it is
3058 * 6-ch: front/clfe/surr
Takashi Iwai9422db42007-04-20 16:11:43 +02003059 * 8-ch: front/clfe/rear/side|fc
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02003060 */
3061 switch (cfg->line_outs) {
3062 case 3:
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02003063 case 4:
3064 nid = cfg->line_out_pins[1];
Takashi Iwai9422db42007-04-20 16:11:43 +02003065 cfg->line_out_pins[1] = cfg->line_out_pins[2];
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02003066 cfg->line_out_pins[2] = nid;
3067 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003068 }
3069
Takashi Iwai82bc9552006-03-21 11:24:42 +01003070 /*
3071 * debug prints of the parsed results
3072 */
3073 snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3074 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
3075 cfg->line_out_pins[2], cfg->line_out_pins[3],
3076 cfg->line_out_pins[4]);
3077 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3078 cfg->speaker_outs, cfg->speaker_pins[0],
3079 cfg->speaker_pins[1], cfg->speaker_pins[2],
3080 cfg->speaker_pins[3], cfg->speaker_pins[4]);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02003081 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3082 cfg->hp_outs, cfg->hp_pins[0],
3083 cfg->hp_pins[1], cfg->hp_pins[2],
3084 cfg->hp_pins[3], cfg->hp_pins[4]);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01003085 snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
Takashi Iwai82bc9552006-03-21 11:24:42 +01003086 snd_printd(" inputs: mic=0x%x, fmic=0x%x, line=0x%x, fline=0x%x,"
3087 " cd=0x%x, aux=0x%x\n",
3088 cfg->input_pins[AUTO_PIN_MIC],
3089 cfg->input_pins[AUTO_PIN_FRONT_MIC],
3090 cfg->input_pins[AUTO_PIN_LINE],
3091 cfg->input_pins[AUTO_PIN_FRONT_LINE],
3092 cfg->input_pins[AUTO_PIN_CD],
3093 cfg->input_pins[AUTO_PIN_AUX]);
3094
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003095 return 0;
3096}
3097
Takashi Iwai4a471b72005-12-07 13:56:29 +01003098/* labels for input pins */
3099const char *auto_pin_cfg_labels[AUTO_PIN_LAST] = {
3100 "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux"
3101};
3102
3103
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104#ifdef CONFIG_PM
3105/*
3106 * power management
3107 */
3108
3109/**
3110 * snd_hda_suspend - suspend the codecs
3111 * @bus: the HDA bus
3112 * @state: suspsend state
3113 *
3114 * Returns 0 if successful.
3115 */
3116int snd_hda_suspend(struct hda_bus *bus, pm_message_t state)
3117{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003118 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119
Takashi Iwai0ba21762007-04-16 11:29:14 +02003120 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai0b7a2e92007-08-14 15:18:26 +02003121#ifdef CONFIG_SND_HDA_POWER_SAVE
3122 if (!codec->power_on)
3123 continue;
3124#endif
Takashi Iwaicb53c622007-08-10 17:21:45 +02003125 hda_call_codec_suspend(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 }
3127 return 0;
3128}
3129
3130/**
3131 * snd_hda_resume - resume the codecs
3132 * @bus: the HDA bus
3133 * @state: resume state
3134 *
3135 * Returns 0 if successful.
Takashi Iwaicb53c622007-08-10 17:21:45 +02003136 *
3137 * This fucntion is defined only when POWER_SAVE isn't set.
3138 * In the power-save mode, the codec is resumed dynamically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 */
3140int snd_hda_resume(struct hda_bus *bus)
3141{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003142 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143
Takashi Iwai0ba21762007-04-16 11:29:14 +02003144 list_for_each_entry(codec, &bus->codec_list, list) {
Maxim Levitskyd804ad92007-09-03 15:28:04 +02003145 if (snd_hda_codec_needs_resume(codec))
3146 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 return 0;
3149}
Maxim Levitskyd804ad92007-09-03 15:28:04 +02003150#ifdef CONFIG_SND_HDA_POWER_SAVE
3151int snd_hda_codecs_inuse(struct hda_bus *bus)
3152{
3153 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154
Maxim Levitskyd804ad92007-09-03 15:28:04 +02003155 list_for_each_entry(codec, &bus->codec_list, list) {
3156 if (snd_hda_codec_needs_resume(codec))
3157 return 1;
3158 }
3159 return 0;
3160}
3161#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162#endif