blob: aa9cd142c30a62e22f1cb9481d890e6e714fb395 [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" },
Mark Brown8199de32008-10-28 14:50:13 +000067 { 0x1aec, "Wolfson Microelectronics" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 { 0x434d, "C-Media" },
Matt2f2f4252005-04-13 14:45:30 +020069 { 0x8384, "SigmaTel" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 {} /* terminator */
71};
72
Harvey Harrison3c9a3202008-02-29 11:59:26 +010073static const struct hda_codec_preset *hda_preset_tables[] = {
74#ifdef CONFIG_SND_HDA_CODEC_REALTEK
75 snd_hda_preset_realtek,
76#endif
77#ifdef CONFIG_SND_HDA_CODEC_CMEDIA
78 snd_hda_preset_cmedia,
79#endif
80#ifdef CONFIG_SND_HDA_CODEC_ANALOG
81 snd_hda_preset_analog,
82#endif
83#ifdef CONFIG_SND_HDA_CODEC_SIGMATEL
84 snd_hda_preset_sigmatel,
85#endif
86#ifdef CONFIG_SND_HDA_CODEC_SI3054
87 snd_hda_preset_si3054,
88#endif
89#ifdef CONFIG_SND_HDA_CODEC_ATIHDMI
90 snd_hda_preset_atihdmi,
91#endif
92#ifdef CONFIG_SND_HDA_CODEC_CONEXANT
93 snd_hda_preset_conexant,
94#endif
95#ifdef CONFIG_SND_HDA_CODEC_VIA
96 snd_hda_preset_via,
97#endif
Wei Ni9a10eb22008-09-26 13:45:46 +080098#ifdef CONFIG_SND_HDA_CODEC_NVHDMI
99 snd_hda_preset_nvhdmi,
100#endif
Harvey Harrison3c9a3202008-02-29 11:59:26 +0100101 NULL
102};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Takashi Iwaicb53c622007-08-10 17:21:45 +0200104#ifdef CONFIG_SND_HDA_POWER_SAVE
105static void hda_power_work(struct work_struct *work);
106static void hda_keep_power_on(struct hda_codec *codec);
107#else
108static inline void hda_keep_power_on(struct hda_codec *codec) {}
109#endif
110
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400111const char *snd_hda_get_jack_location(u32 cfg)
112{
113 static char *bases[7] = {
114 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
115 };
116 static unsigned char specials_idx[] = {
117 0x07, 0x08,
118 0x17, 0x18, 0x19,
119 0x37, 0x38
120 };
121 static char *specials[] = {
122 "Rear Panel", "Drive Bar",
123 "Riser", "HDMI", "ATAPI",
124 "Mobile-In", "Mobile-Out"
125 };
126 int i;
127 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
128 if ((cfg & 0x0f) < 7)
129 return bases[cfg & 0x0f];
130 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
131 if (cfg == specials_idx[i])
132 return specials[i];
133 }
134 return "UNKNOWN";
135}
136
137const char *snd_hda_get_jack_connectivity(u32 cfg)
138{
139 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
140
141 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
142}
143
144const char *snd_hda_get_jack_type(u32 cfg)
145{
146 static char *jack_types[16] = {
147 "Line Out", "Speaker", "HP Out", "CD",
148 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
149 "Line In", "Aux", "Mic", "Telephony",
150 "SPDIF In", "Digitial In", "Reserved", "Other"
151 };
152
153 return jack_types[(cfg & AC_DEFCFG_DEVICE)
154 >> AC_DEFCFG_DEVICE_SHIFT];
155}
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157/**
158 * snd_hda_codec_read - send a command and get the response
159 * @codec: the HDA codec
160 * @nid: NID to send the command
161 * @direct: direct flag
162 * @verb: the verb to send
163 * @parm: the parameter for the verb
164 *
165 * Send a single command and read the corresponding response.
166 *
167 * Returns the obtained response value, or -1 for an error.
168 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200169unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
170 int direct,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 unsigned int verb, unsigned int parm)
172{
173 unsigned int res;
Takashi Iwaicb53c622007-08-10 17:21:45 +0200174 snd_hda_power_up(codec);
Ingo Molnar62932df2006-01-16 16:34:20 +0100175 mutex_lock(&codec->bus->cmd_mutex);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200176 if (!codec->bus->ops.command(codec, nid, direct, verb, parm))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 res = codec->bus->ops.get_response(codec);
178 else
179 res = (unsigned int)-1;
Ingo Molnar62932df2006-01-16 16:34:20 +0100180 mutex_unlock(&codec->bus->cmd_mutex);
Takashi Iwaicb53c622007-08-10 17:21:45 +0200181 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 return res;
183}
184
185/**
186 * snd_hda_codec_write - send a single command without waiting for response
187 * @codec: the HDA codec
188 * @nid: NID to send the command
189 * @direct: direct flag
190 * @verb: the verb to send
191 * @parm: the parameter for the verb
192 *
193 * Send a single command without waiting for response.
194 *
195 * Returns 0 if successful, or a negative error code.
196 */
197int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
198 unsigned int verb, unsigned int parm)
199{
200 int err;
Takashi Iwaicb53c622007-08-10 17:21:45 +0200201 snd_hda_power_up(codec);
Ingo Molnar62932df2006-01-16 16:34:20 +0100202 mutex_lock(&codec->bus->cmd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 err = codec->bus->ops.command(codec, nid, direct, verb, parm);
Ingo Molnar62932df2006-01-16 16:34:20 +0100204 mutex_unlock(&codec->bus->cmd_mutex);
Takashi Iwaicb53c622007-08-10 17:21:45 +0200205 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 return err;
207}
208
209/**
210 * snd_hda_sequence_write - sequence writes
211 * @codec: the HDA codec
212 * @seq: VERB array to send
213 *
214 * Send the commands sequentially from the given array.
215 * The array must be terminated with NID=0.
216 */
217void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
218{
219 for (; seq->nid; seq++)
220 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
221}
222
223/**
224 * snd_hda_get_sub_nodes - get the range of sub nodes
225 * @codec: the HDA codec
226 * @nid: NID to parse
227 * @start_id: the pointer to store the start NID
228 *
229 * Parse the NID and store the start NID of its sub-nodes.
230 * Returns the number of sub-nodes.
231 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200232int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
233 hda_nid_t *start_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
235 unsigned int parm;
236
237 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
Danny Tholene8a7f132007-09-11 21:41:56 +0200238 if (parm == -1)
239 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 *start_id = (parm >> 16) & 0x7fff;
241 return (int)(parm & 0x7fff);
242}
243
244/**
245 * snd_hda_get_connections - get connection list
246 * @codec: the HDA codec
247 * @nid: NID to parse
248 * @conn_list: connection list array
249 * @max_conns: max. number of connections to store
250 *
251 * Parses the connection list of the given widget and stores the list
252 * of NIDs.
253 *
254 * Returns the number of connections, or a negative error code.
255 */
256int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
257 hda_nid_t *conn_list, int max_conns)
258{
259 unsigned int parm;
Takashi Iwai54d17402005-11-21 16:33:22 +0100260 int i, conn_len, conns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 unsigned int shift, num_elems, mask;
Takashi Iwai54d17402005-11-21 16:33:22 +0100262 hda_nid_t prev_nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Takashi Iwaida3cec32008-08-08 17:12:14 +0200264 if (snd_BUG_ON(!conn_list || max_conns <= 0))
265 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
267 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
268 if (parm & AC_CLIST_LONG) {
269 /* long form */
270 shift = 16;
271 num_elems = 2;
272 } else {
273 /* short form */
274 shift = 8;
275 num_elems = 4;
276 }
277 conn_len = parm & AC_CLIST_LENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 mask = (1 << (shift-1)) - 1;
279
Takashi Iwai0ba21762007-04-16 11:29:14 +0200280 if (!conn_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 return 0; /* no connection */
282
283 if (conn_len == 1) {
284 /* single connection */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200285 parm = snd_hda_codec_read(codec, nid, 0,
286 AC_VERB_GET_CONNECT_LIST, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 conn_list[0] = parm & mask;
288 return 1;
289 }
290
291 /* multi connection */
292 conns = 0;
Takashi Iwai54d17402005-11-21 16:33:22 +0100293 prev_nid = 0;
294 for (i = 0; i < conn_len; i++) {
295 int range_val;
296 hda_nid_t val, n;
297
298 if (i % num_elems == 0)
299 parm = snd_hda_codec_read(codec, nid, 0,
300 AC_VERB_GET_CONNECT_LIST, i);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200301 range_val = !!(parm & (1 << (shift-1))); /* ranges */
Takashi Iwai54d17402005-11-21 16:33:22 +0100302 val = parm & mask;
303 parm >>= shift;
304 if (range_val) {
305 /* ranges between the previous and this one */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200306 if (!prev_nid || prev_nid >= val) {
307 snd_printk(KERN_WARNING "hda_codec: "
308 "invalid dep_range_val %x:%x\n",
309 prev_nid, val);
Takashi Iwai54d17402005-11-21 16:33:22 +0100310 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100312 for (n = prev_nid + 1; n <= val; n++) {
313 if (conns >= max_conns) {
Takashi Iwai0ba21762007-04-16 11:29:14 +0200314 snd_printk(KERN_ERR
315 "Too many connections\n");
Takashi Iwai54d17402005-11-21 16:33:22 +0100316 return -EINVAL;
317 }
318 conn_list[conns++] = n;
319 }
320 } else {
321 if (conns >= max_conns) {
322 snd_printk(KERN_ERR "Too many connections\n");
323 return -EINVAL;
324 }
325 conn_list[conns++] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100327 prev_nid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 }
329 return conns;
330}
331
332
333/**
334 * snd_hda_queue_unsol_event - add an unsolicited event to queue
335 * @bus: the BUS
336 * @res: unsolicited event (lower 32bit of RIRB entry)
337 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
338 *
339 * Adds the given event to the queue. The events are processed in
340 * the workqueue asynchronously. Call this function in the interrupt
341 * hanlder when RIRB receives an unsolicited event.
342 *
343 * Returns 0 if successful, or a negative error code.
344 */
345int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
346{
347 struct hda_bus_unsolicited *unsol;
348 unsigned int wp;
349
Takashi Iwai0ba21762007-04-16 11:29:14 +0200350 unsol = bus->unsol;
351 if (!unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 return 0;
353
354 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
355 unsol->wp = wp;
356
357 wp <<= 1;
358 unsol->queue[wp] = res;
359 unsol->queue[wp + 1] = res_ex;
360
Takashi Iwaie250af22006-12-19 17:08:52 +0100361 schedule_work(&unsol->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 return 0;
364}
365
366/*
Wu Fengguang5c1d1a92008-10-07 14:17:53 +0800367 * process queued unsolicited events
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 */
David Howellsc4028952006-11-22 14:57:56 +0000369static void process_unsol_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370{
David Howellsc4028952006-11-22 14:57:56 +0000371 struct hda_bus_unsolicited *unsol =
372 container_of(work, struct hda_bus_unsolicited, work);
373 struct hda_bus *bus = unsol->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 struct hda_codec *codec;
375 unsigned int rp, caddr, res;
376
377 while (unsol->rp != unsol->wp) {
378 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
379 unsol->rp = rp;
380 rp <<= 1;
381 res = unsol->queue[rp];
382 caddr = unsol->queue[rp + 1];
Takashi Iwai0ba21762007-04-16 11:29:14 +0200383 if (!(caddr & (1 << 4))) /* no unsolicited event? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 continue;
385 codec = bus->caddr_tbl[caddr & 0x0f];
386 if (codec && codec->patch_ops.unsol_event)
387 codec->patch_ops.unsol_event(codec, res);
388 }
389}
390
391/*
392 * initialize unsolicited queue
393 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200394static int init_unsol_queue(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395{
396 struct hda_bus_unsolicited *unsol;
397
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100398 if (bus->unsol) /* already initialized */
399 return 0;
400
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200401 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200402 if (!unsol) {
403 snd_printk(KERN_ERR "hda_codec: "
404 "can't allocate unsolicited queue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 return -ENOMEM;
406 }
David Howellsc4028952006-11-22 14:57:56 +0000407 INIT_WORK(&unsol->work, process_unsol_events);
408 unsol->bus = bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 bus->unsol = unsol;
410 return 0;
411}
412
413/*
414 * destructor
415 */
416static void snd_hda_codec_free(struct hda_codec *codec);
417
418static int snd_hda_bus_free(struct hda_bus *bus)
419{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200420 struct hda_codec *codec, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Takashi Iwai0ba21762007-04-16 11:29:14 +0200422 if (!bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 return 0;
424 if (bus->unsol) {
Takashi Iwaie250af22006-12-19 17:08:52 +0100425 flush_scheduled_work();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 kfree(bus->unsol);
427 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200428 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 snd_hda_codec_free(codec);
430 }
431 if (bus->ops.private_free)
432 bus->ops.private_free(bus);
433 kfree(bus);
434 return 0;
435}
436
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100437static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
439 struct hda_bus *bus = device->device_data;
440 return snd_hda_bus_free(bus);
441}
442
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200443#ifdef CONFIG_SND_HDA_HWDEP
444static int snd_hda_bus_dev_register(struct snd_device *device)
445{
446 struct hda_bus *bus = device->device_data;
447 struct hda_codec *codec;
448 list_for_each_entry(codec, &bus->codec_list, list) {
449 snd_hda_hwdep_add_sysfs(codec);
450 }
451 return 0;
452}
453#else
454#define snd_hda_bus_dev_register NULL
455#endif
456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457/**
458 * snd_hda_bus_new - create a HDA bus
459 * @card: the card entry
460 * @temp: the template for hda_bus information
461 * @busp: the pointer to store the created bus instance
462 *
463 * Returns 0 if successful, or a negative error code.
464 */
Takashi Iwai756e2b02007-04-16 11:27:07 +0200465int __devinit snd_hda_bus_new(struct snd_card *card,
466 const struct hda_bus_template *temp,
467 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
469 struct hda_bus *bus;
470 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100471 static struct snd_device_ops dev_ops = {
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200472 .dev_register = snd_hda_bus_dev_register,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 .dev_free = snd_hda_bus_dev_free,
474 };
475
Takashi Iwaida3cec32008-08-08 17:12:14 +0200476 if (snd_BUG_ON(!temp))
477 return -EINVAL;
478 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
479 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481 if (busp)
482 *busp = NULL;
483
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200484 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 if (bus == NULL) {
486 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
487 return -ENOMEM;
488 }
489
490 bus->card = card;
491 bus->private_data = temp->private_data;
492 bus->pci = temp->pci;
493 bus->modelname = temp->modelname;
494 bus->ops = temp->ops;
495
Ingo Molnar62932df2006-01-16 16:34:20 +0100496 mutex_init(&bus->cmd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 INIT_LIST_HEAD(&bus->codec_list);
498
Takashi Iwai0ba21762007-04-16 11:29:14 +0200499 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
500 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 snd_hda_bus_free(bus);
502 return err;
503 }
504 if (busp)
505 *busp = bus;
506 return 0;
507}
508
Takashi Iwai82467612007-07-27 19:15:54 +0200509#ifdef CONFIG_SND_HDA_GENERIC
510#define is_generic_config(codec) \
Takashi Iwaif44ac832008-07-30 15:01:45 +0200511 (codec->modelname && !strcmp(codec->modelname, "generic"))
Takashi Iwai82467612007-07-27 19:15:54 +0200512#else
513#define is_generic_config(codec) 0
514#endif
515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516/*
517 * find a matching codec preset
518 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200519static const struct hda_codec_preset *
Takashi Iwai756e2b02007-04-16 11:27:07 +0200520find_codec_preset(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
522 const struct hda_codec_preset **tbl, *preset;
523
Takashi Iwai82467612007-07-27 19:15:54 +0200524 if (is_generic_config(codec))
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100525 return NULL; /* use the generic parser */
526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 for (tbl = hda_preset_tables; *tbl; tbl++) {
528 for (preset = *tbl; preset->id; preset++) {
529 u32 mask = preset->mask;
Marc Boucherca7cfae2008-01-22 15:32:25 +0100530 if (preset->afg && preset->afg != codec->afg)
531 continue;
532 if (preset->mfg && preset->mfg != codec->mfg)
533 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200534 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 mask = ~0;
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200536 if (preset->id == (codec->vendor_id & mask) &&
Takashi Iwai0ba21762007-04-16 11:29:14 +0200537 (!preset->rev ||
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200538 preset->rev == codec->revision_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 return preset;
540 }
541 }
542 return NULL;
543}
544
545/*
Takashi Iwaif44ac832008-07-30 15:01:45 +0200546 * get_codec_name - store the codec name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 */
Takashi Iwaif44ac832008-07-30 15:01:45 +0200548static int get_codec_name(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
550 const struct hda_vendor_id *c;
551 const char *vendor = NULL;
552 u16 vendor_id = codec->vendor_id >> 16;
Takashi Iwaif44ac832008-07-30 15:01:45 +0200553 char tmp[16], name[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555 for (c = hda_vendor_ids; c->id; c++) {
556 if (c->id == vendor_id) {
557 vendor = c->name;
558 break;
559 }
560 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200561 if (!vendor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 sprintf(tmp, "Generic %04x", vendor_id);
563 vendor = tmp;
564 }
565 if (codec->preset && codec->preset->name)
Takashi Iwaif44ac832008-07-30 15:01:45 +0200566 snprintf(name, sizeof(name), "%s %s", vendor,
567 codec->preset->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 else
Takashi Iwaif44ac832008-07-30 15:01:45 +0200569 snprintf(name, sizeof(name), "%s ID %x", vendor,
Takashi Iwai0ba21762007-04-16 11:29:14 +0200570 codec->vendor_id & 0xffff);
Takashi Iwaif44ac832008-07-30 15:01:45 +0200571 codec->name = kstrdup(name, GFP_KERNEL);
572 if (!codec->name)
573 return -ENOMEM;
574 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575}
576
577/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200578 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 */
Takashi Iwai756e2b02007-04-16 11:27:07 +0200580static void __devinit setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
582 int i, total_nodes;
583 hda_nid_t nid;
584
585 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
586 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +0200587 unsigned int func;
588 func = snd_hda_param_read(codec, nid, AC_PAR_FUNCTION_TYPE);
589 switch (func & 0xff) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200590 case AC_GRP_AUDIO_FUNCTION:
591 codec->afg = nid;
592 break;
593 case AC_GRP_MODEM_FUNCTION:
594 codec->mfg = nid;
595 break;
596 default:
597 break;
598 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600}
601
602/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100603 * read widget caps for each widget and store in cache
604 */
605static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
606{
607 int i;
608 hda_nid_t nid;
609
610 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
611 &codec->start_nid);
612 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200613 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +0100614 return -ENOMEM;
615 nid = codec->start_nid;
616 for (i = 0; i < codec->num_nodes; i++, nid++)
617 codec->wcaps[i] = snd_hda_param_read(codec, nid,
618 AC_PAR_AUDIO_WIDGET_CAP);
619 return 0;
620}
621
622
Takashi Iwai01751f52007-08-10 16:59:39 +0200623static void init_hda_cache(struct hda_cache_rec *cache,
624 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +0200625static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +0200626
Takashi Iwai54d17402005-11-21 16:33:22 +0100627/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 * codec destructor
629 */
630static void snd_hda_codec_free(struct hda_codec *codec)
631{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200632 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 return;
Takashi Iwaicb53c622007-08-10 17:21:45 +0200634#ifdef CONFIG_SND_HDA_POWER_SAVE
635 cancel_delayed_work(&codec->power_work);
Takashi Iwai2525fdc2007-08-15 22:18:22 +0200636 flush_scheduled_work();
Takashi Iwaicb53c622007-08-10 17:21:45 +0200637#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +0200639 snd_array_free(&codec->mixers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 codec->bus->caddr_tbl[codec->addr] = NULL;
641 if (codec->patch_ops.free)
642 codec->patch_ops.free(codec);
Takashi Iwai01751f52007-08-10 16:59:39 +0200643 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +0200644 free_hda_cache(&codec->cmd_cache);
Takashi Iwaif44ac832008-07-30 15:01:45 +0200645 kfree(codec->name);
646 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +0100647 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 kfree(codec);
649}
650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651/**
652 * snd_hda_codec_new - create a HDA codec
653 * @bus: the bus to assign
654 * @codec_addr: the codec address
655 * @codecp: the pointer to store the generated codec
656 *
657 * Returns 0 if successful, or a negative error code.
658 */
Takashi Iwai756e2b02007-04-16 11:27:07 +0200659int __devinit snd_hda_codec_new(struct hda_bus *bus, unsigned int codec_addr,
660 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
662 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +0200663 char component[31];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 int err;
665
Takashi Iwaida3cec32008-08-08 17:12:14 +0200666 if (snd_BUG_ON(!bus))
667 return -EINVAL;
668 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
669 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
671 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +0200672 snd_printk(KERN_ERR "hda_codec: "
673 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 return -EBUSY;
675 }
676
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200677 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 if (codec == NULL) {
679 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
680 return -ENOMEM;
681 }
682
683 codec->bus = bus;
684 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +0100685 mutex_init(&codec->spdif_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +0200686 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +0200687 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwaid13bd412008-07-30 15:01:45 +0200688 snd_array_init(&codec->mixers, sizeof(struct snd_kcontrol *), 32);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200689 if (codec->bus->modelname) {
690 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
691 if (!codec->modelname) {
692 snd_hda_codec_free(codec);
693 return -ENODEV;
694 }
695 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Takashi Iwaicb53c622007-08-10 17:21:45 +0200697#ifdef CONFIG_SND_HDA_POWER_SAVE
698 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
699 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
700 * the caller has to power down appropriatley after initialization
701 * phase.
702 */
703 hda_keep_power_on(codec);
704#endif
705
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 list_add_tail(&codec->list, &bus->codec_list);
707 bus->caddr_tbl[codec_addr] = codec;
708
Takashi Iwai0ba21762007-04-16 11:29:14 +0200709 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
710 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +0100711 if (codec->vendor_id == -1)
712 /* read again, hopefully the access method was corrected
713 * in the last read...
714 */
715 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
716 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200717 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
718 AC_PAR_SUBSYSTEM_ID);
719 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
720 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200722 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200723 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200724 snd_printdd("hda_codec: no AFG or MFG node found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 snd_hda_codec_free(codec);
726 return -ENODEV;
727 }
728
Takashi Iwai54d17402005-11-21 16:33:22 +0100729 if (read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg) < 0) {
730 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
731 snd_hda_codec_free(codec);
732 return -ENOMEM;
733 }
734
Takashi Iwai0ba21762007-04-16 11:29:14 +0200735 if (!codec->subsystem_id) {
Takashi Iwai86284e42005-10-11 15:05:54 +0200736 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200737 codec->subsystem_id =
738 snd_hda_codec_read(codec, nid, 0,
739 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +0200740 }
Takashi Iwaif44ac832008-07-30 15:01:45 +0200741 if (bus->modelname)
742 codec->modelname = kstrdup(bus->modelname, GFP_KERNEL);
Takashi Iwai86284e42005-10-11 15:05:54 +0200743
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200744 err = snd_hda_codec_configure(codec);
745 if (err < 0) {
746 snd_hda_codec_free(codec);
747 return err;
748 }
749 snd_hda_codec_proc_new(codec);
750
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200751 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200752
753 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
754 codec->subsystem_id, codec->revision_id);
755 snd_component_add(codec->bus->card, component);
756
757 if (codecp)
758 *codecp = codec;
759 return 0;
760}
761
762int snd_hda_codec_configure(struct hda_codec *codec)
763{
764 int err;
765
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100766 codec->preset = find_codec_preset(codec);
Takashi Iwaif44ac832008-07-30 15:01:45 +0200767 if (!codec->name) {
768 err = get_codec_name(codec);
769 if (err < 0)
770 return err;
771 }
Takashi Iwai43ea1d42007-04-26 19:12:08 +0200772 /* audio codec should override the mixer name */
Takashi Iwaif44ac832008-07-30 15:01:45 +0200773 if (codec->afg || !*codec->bus->card->mixername)
774 strlcpy(codec->bus->card->mixername, codec->name,
775 sizeof(codec->bus->card->mixername));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Takashi Iwai82467612007-07-27 19:15:54 +0200777 if (is_generic_config(codec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai82467612007-07-27 19:15:54 +0200779 goto patched;
780 }
Takashi Iwai82467612007-07-27 19:15:54 +0200781 if (codec->preset && codec->preset->patch) {
782 err = codec->preset->patch(codec);
783 goto patched;
784 }
785
786 /* call the default parser */
Takashi Iwai82467612007-07-27 19:15:54 +0200787 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai35a1e0c2007-10-19 08:13:40 +0200788 if (err < 0)
789 printk(KERN_ERR "hda-codec: No codec parser is available\n");
Takashi Iwai82467612007-07-27 19:15:54 +0200790
791 patched:
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200792 if (!err && codec->patch_ops.unsol_event)
793 err = init_unsol_queue(codec->bus);
794 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795}
796
797/**
798 * snd_hda_codec_setup_stream - set up the codec for streaming
799 * @codec: the CODEC to set up
800 * @nid: the NID to set up
801 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
802 * @channel_id: channel id to pass, zero based.
803 * @format: stream format.
804 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200805void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
806 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 int channel_id, int format)
808{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200809 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +0200810 return;
811
Takashi Iwai0ba21762007-04-16 11:29:14 +0200812 snd_printdd("hda_codec_setup_stream: "
813 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 nid, stream_tag, channel_id, format);
815 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID,
816 (stream_tag << 4) | channel_id);
817 msleep(1);
818 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, format);
819}
820
Takashi Iwai888afa12008-03-18 09:57:50 +0100821void snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid)
822{
823 if (!nid)
824 return;
825
826 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
827 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
828#if 0 /* keep the format */
829 msleep(1);
830 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0);
831#endif
832}
833
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834/*
835 * amp access functions
836 */
837
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200838/* FIXME: more better hash key? */
839#define HDA_HASH_KEY(nid,dir,idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200841#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
843/* initialize the hash table */
Takashi Iwai01751f52007-08-10 16:59:39 +0200844static void __devinit init_hda_cache(struct hda_cache_rec *cache,
845 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846{
Takashi Iwai01751f52007-08-10 16:59:39 +0200847 memset(cache, 0, sizeof(*cache));
848 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +0200849 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +0200850}
851
Takashi Iwai1fcaee62007-08-23 00:01:09 +0200852static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +0200853{
Takashi Iwai603c4012008-07-30 15:01:44 +0200854 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855}
856
857/* query the hash. allocate an entry if not found. */
Takashi Iwai01751f52007-08-10 16:59:39 +0200858static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
859 u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860{
Takashi Iwai01751f52007-08-10 16:59:39 +0200861 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
862 u16 cur = cache->hash[idx];
Takashi Iwai603c4012008-07-30 15:01:44 +0200863 struct hda_cache_head *info_head = cache->buf.list;
Takashi Iwai01751f52007-08-10 16:59:39 +0200864 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
866 while (cur != 0xffff) {
Takashi Iwai603c4012008-07-30 15:01:44 +0200867 info = &info_head[cur];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 if (info->key == key)
869 return info;
870 cur = info->next;
871 }
872
873 /* add a new hash entry */
Takashi Iwai603c4012008-07-30 15:01:44 +0200874 info = snd_array_new(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 info->key = key;
Takashi Iwai01751f52007-08-10 16:59:39 +0200876 info->val = 0;
877 info->next = cache->hash[idx];
878 cache->hash[idx] = cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
880 return info;
881}
882
Takashi Iwai01751f52007-08-10 16:59:39 +0200883/* query and allocate an amp hash entry */
884static inline struct hda_amp_info *
885get_alloc_amp_hash(struct hda_codec *codec, u32 key)
886{
887 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
888}
889
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890/*
891 * query AMP capabilities for the given widget and direction
892 */
Matthew Ranostay09a99952008-01-24 11:49:21 +0100893u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200895 struct hda_amp_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Takashi Iwai0ba21762007-04-16 11:29:14 +0200897 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
898 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 return 0;
Takashi Iwai01751f52007-08-10 16:59:39 +0200900 if (!(info->head.val & INFO_AMP_CAPS)) {
Takashi Iwai0ba21762007-04-16 11:29:14 +0200901 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 nid = codec->afg;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200903 info->amp_caps = snd_hda_param_read(codec, nid,
904 direction == HDA_OUTPUT ?
905 AC_PAR_AMP_OUT_CAP :
906 AC_PAR_AMP_IN_CAP);
Takashi Iwaib75e53f2007-05-10 16:56:09 +0200907 if (info->amp_caps)
Takashi Iwai01751f52007-08-10 16:59:39 +0200908 info->head.val |= INFO_AMP_CAPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 }
910 return info->amp_caps;
911}
912
Takashi Iwai897cc182007-05-29 19:01:37 +0200913int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
914 unsigned int caps)
915{
916 struct hda_amp_info *info;
917
918 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
919 if (!info)
920 return -EINVAL;
921 info->amp_caps = caps;
Takashi Iwai01751f52007-08-10 16:59:39 +0200922 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai897cc182007-05-29 19:01:37 +0200923 return 0;
924}
925
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926/*
927 * read the current volume to info
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200928 * if the cache exists, read the cache value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200930static unsigned int get_vol_mute(struct hda_codec *codec,
931 struct hda_amp_info *info, hda_nid_t nid,
932 int ch, int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933{
934 u32 val, parm;
935
Takashi Iwai01751f52007-08-10 16:59:39 +0200936 if (info->head.val & INFO_AMP_VOL(ch))
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200937 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
939 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
940 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
941 parm |= index;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200942 val = snd_hda_codec_read(codec, nid, 0,
943 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 info->vol[ch] = val & 0xff;
Takashi Iwai01751f52007-08-10 16:59:39 +0200945 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200946 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947}
948
949/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200950 * write the current volume in info to the h/w and update the cache
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200952static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
Takashi Iwai0ba21762007-04-16 11:29:14 +0200953 hda_nid_t nid, int ch, int direction, int index,
954 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955{
956 u32 parm;
957
958 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
959 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
960 parm |= index << AC_AMP_SET_INDEX_SHIFT;
961 parm |= val;
962 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200963 info->vol[ch] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964}
965
966/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200967 * read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 */
Takashi Iwai834be882006-03-01 14:16:17 +0100969int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
970 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200972 struct hda_amp_info *info;
973 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
974 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200976 return get_vol_mute(codec, info, nid, ch, direction, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977}
978
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200979/*
980 * update the AMP value, mask = bit mask to set, val = the value
981 */
Takashi Iwai834be882006-03-01 14:16:17 +0100982int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
983 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200985 struct hda_amp_info *info;
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200986
Takashi Iwai0ba21762007-04-16 11:29:14 +0200987 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
988 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200990 val &= mask;
991 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
Takashi Iwai82beb8f2007-08-10 17:09:26 +0200992 if (info->vol[ch] == val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200994 put_vol_mute(codec, info, nid, ch, direction, idx, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 return 1;
996}
997
Takashi Iwai47fd8302007-08-10 17:11:07 +0200998/*
999 * update the AMP stereo with the same mask and value
1000 */
1001int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1002 int direction, int idx, int mask, int val)
1003{
1004 int ch, ret = 0;
1005 for (ch = 0; ch < 2; ch++)
1006 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1007 idx, mask, val);
1008 return ret;
1009}
1010
Takashi Iwaicb53c622007-08-10 17:21:45 +02001011#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001012/* resume the all amp commands from the cache */
1013void snd_hda_codec_resume_amp(struct hda_codec *codec)
1014{
Takashi Iwai603c4012008-07-30 15:01:44 +02001015 struct hda_amp_info *buffer = codec->amp_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001016 int i;
1017
Takashi Iwai603c4012008-07-30 15:01:44 +02001018 for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001019 u32 key = buffer->head.key;
1020 hda_nid_t nid;
1021 unsigned int idx, dir, ch;
1022 if (!key)
1023 continue;
1024 nid = key & 0xff;
1025 idx = (key >> 16) & 0xff;
1026 dir = (key >> 24) & 0xff;
1027 for (ch = 0; ch < 2; ch++) {
1028 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
1029 continue;
1030 put_vol_mute(codec, buffer, nid, ch, dir, idx,
1031 buffer->vol[ch]);
1032 }
1033 }
1034}
Takashi Iwaicb53c622007-08-10 17:21:45 +02001035#endif /* SND_HDA_NEEDS_RESUME */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037/* volume */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001038int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1039 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040{
1041 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1042 u16 nid = get_amp_nid(kcontrol);
1043 u8 chs = get_amp_channels(kcontrol);
1044 int dir = get_amp_direction(kcontrol);
1045 u32 caps;
1046
1047 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001048 /* num steps */
1049 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1050 if (!caps) {
1051 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01001052 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
1053 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 return -EINVAL;
1055 }
1056 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1057 uinfo->count = chs == 3 ? 2 : 1;
1058 uinfo->value.integer.min = 0;
1059 uinfo->value.integer.max = caps;
1060 return 0;
1061}
1062
Takashi Iwai0ba21762007-04-16 11:29:14 +02001063int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1064 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065{
1066 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1067 hda_nid_t nid = get_amp_nid(kcontrol);
1068 int chs = get_amp_channels(kcontrol);
1069 int dir = get_amp_direction(kcontrol);
1070 int idx = get_amp_index(kcontrol);
1071 long *valp = ucontrol->value.integer.value;
1072
1073 if (chs & 1)
Takashi Iwai47fd8302007-08-10 17:11:07 +02001074 *valp++ = snd_hda_codec_amp_read(codec, nid, 0, dir, idx)
1075 & HDA_AMP_VOLMASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 if (chs & 2)
Takashi Iwai47fd8302007-08-10 17:11:07 +02001077 *valp = snd_hda_codec_amp_read(codec, nid, 1, dir, idx)
1078 & HDA_AMP_VOLMASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 return 0;
1080}
1081
Takashi Iwai0ba21762007-04-16 11:29:14 +02001082int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1083 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084{
1085 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1086 hda_nid_t nid = get_amp_nid(kcontrol);
1087 int chs = get_amp_channels(kcontrol);
1088 int dir = get_amp_direction(kcontrol);
1089 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 long *valp = ucontrol->value.integer.value;
1091 int change = 0;
1092
Takashi Iwaicb53c622007-08-10 17:21:45 +02001093 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001094 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001095 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
1096 0x7f, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001097 valp++;
1098 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001099 if (chs & 2)
1100 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001101 0x7f, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001102 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 return change;
1104}
1105
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001106int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1107 unsigned int size, unsigned int __user *_tlv)
1108{
1109 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1110 hda_nid_t nid = get_amp_nid(kcontrol);
1111 int dir = get_amp_direction(kcontrol);
1112 u32 caps, val1, val2;
1113
1114 if (size < 4 * sizeof(unsigned int))
1115 return -ENOMEM;
1116 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001117 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1118 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001119 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
1120 val1 = ((int)val1) * ((int)val2);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001121 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
1122 return -EFAULT;
1123 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
1124 return -EFAULT;
1125 if (put_user(val1, _tlv + 2))
1126 return -EFAULT;
1127 if (put_user(val2, _tlv + 3))
1128 return -EFAULT;
1129 return 0;
1130}
1131
Takashi Iwai2134ea42008-01-10 16:53:55 +01001132/*
1133 * set (static) TLV for virtual master volume; recalculated as max 0dB
1134 */
1135void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
1136 unsigned int *tlv)
1137{
1138 u32 caps;
1139 int nums, step;
1140
1141 caps = query_amp_caps(codec, nid, dir);
1142 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1143 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1144 step = (step + 1) * 25;
1145 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
1146 tlv[1] = 2 * sizeof(unsigned int);
1147 tlv[2] = -nums * step;
1148 tlv[3] = step;
1149}
1150
1151/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01001152static struct snd_kcontrol *
1153_snd_hda_find_mixer_ctl(struct hda_codec *codec,
1154 const char *name, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01001155{
1156 struct snd_ctl_elem_id id;
1157 memset(&id, 0, sizeof(id));
1158 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwai09f99702008-02-04 12:31:13 +01001159 id.index = idx;
Takashi Iwai2134ea42008-01-10 16:53:55 +01001160 strcpy(id.name, name);
1161 return snd_ctl_find_id(codec->bus->card, &id);
1162}
1163
Takashi Iwai09f99702008-02-04 12:31:13 +01001164struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
1165 const char *name)
1166{
1167 return _snd_hda_find_mixer_ctl(codec, name, 0);
1168}
1169
Takashi Iwaid13bd412008-07-30 15:01:45 +02001170/* Add a control element and assign to the codec */
1171int snd_hda_ctl_add(struct hda_codec *codec, struct snd_kcontrol *kctl)
1172{
1173 int err;
1174 struct snd_kcontrol **knewp;
1175
1176 err = snd_ctl_add(codec->bus->card, kctl);
1177 if (err < 0)
1178 return err;
1179 knewp = snd_array_new(&codec->mixers);
1180 if (!knewp)
1181 return -ENOMEM;
1182 *knewp = kctl;
1183 return 0;
1184}
1185
1186/* Clear all controls assigned to the given codec */
1187void snd_hda_ctls_clear(struct hda_codec *codec)
1188{
1189 int i;
1190 struct snd_kcontrol **kctls = codec->mixers.list;
1191 for (i = 0; i < codec->mixers.used; i++)
1192 snd_ctl_remove(codec->bus->card, kctls[i]);
1193 snd_array_free(&codec->mixers);
1194}
1195
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001196void snd_hda_codec_reset(struct hda_codec *codec)
1197{
1198 int i;
1199
1200#ifdef CONFIG_SND_HDA_POWER_SAVE
1201 cancel_delayed_work(&codec->power_work);
1202 flush_scheduled_work();
1203#endif
1204 snd_hda_ctls_clear(codec);
1205 /* relase PCMs */
1206 for (i = 0; i < codec->num_pcms; i++) {
1207 if (codec->pcm_info[i].pcm)
1208 snd_device_free(codec->bus->card,
1209 codec->pcm_info[i].pcm);
1210 }
1211 if (codec->patch_ops.free)
1212 codec->patch_ops.free(codec);
1213 codec->spec = NULL;
1214 free_hda_cache(&codec->amp_cache);
1215 free_hda_cache(&codec->cmd_cache);
1216 codec->num_pcms = 0;
1217 codec->pcm_info = NULL;
1218 codec->preset = NULL;
1219}
1220
Takashi Iwai2134ea42008-01-10 16:53:55 +01001221/* create a virtual master control and add slaves */
1222int snd_hda_add_vmaster(struct hda_codec *codec, char *name,
1223 unsigned int *tlv, const char **slaves)
1224{
1225 struct snd_kcontrol *kctl;
1226 const char **s;
1227 int err;
1228
Takashi Iwai2f085542008-02-22 18:43:50 +01001229 for (s = slaves; *s && !snd_hda_find_mixer_ctl(codec, *s); s++)
1230 ;
1231 if (!*s) {
1232 snd_printdd("No slave found for %s\n", name);
1233 return 0;
1234 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01001235 kctl = snd_ctl_make_virtual_master(name, tlv);
1236 if (!kctl)
1237 return -ENOMEM;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001238 err = snd_hda_ctl_add(codec, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001239 if (err < 0)
1240 return err;
1241
1242 for (s = slaves; *s; s++) {
1243 struct snd_kcontrol *sctl;
1244
1245 sctl = snd_hda_find_mixer_ctl(codec, *s);
1246 if (!sctl) {
1247 snd_printdd("Cannot find slave %s, skipped\n", *s);
1248 continue;
1249 }
1250 err = snd_ctl_add_slave(kctl, sctl);
1251 if (err < 0)
1252 return err;
1253 }
1254 return 0;
1255}
1256
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257/* switch */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001258int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
1259 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260{
1261 int chs = get_amp_channels(kcontrol);
1262
1263 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1264 uinfo->count = chs == 3 ? 2 : 1;
1265 uinfo->value.integer.min = 0;
1266 uinfo->value.integer.max = 1;
1267 return 0;
1268}
1269
Takashi Iwai0ba21762007-04-16 11:29:14 +02001270int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
1271 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272{
1273 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1274 hda_nid_t nid = get_amp_nid(kcontrol);
1275 int chs = get_amp_channels(kcontrol);
1276 int dir = get_amp_direction(kcontrol);
1277 int idx = get_amp_index(kcontrol);
1278 long *valp = ucontrol->value.integer.value;
1279
1280 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02001281 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02001282 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02001284 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02001285 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 return 0;
1287}
1288
Takashi Iwai0ba21762007-04-16 11:29:14 +02001289int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
1290 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291{
1292 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1293 hda_nid_t nid = get_amp_nid(kcontrol);
1294 int chs = get_amp_channels(kcontrol);
1295 int dir = get_amp_direction(kcontrol);
1296 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 long *valp = ucontrol->value.integer.value;
1298 int change = 0;
1299
Takashi Iwaicb53c622007-08-10 17:21:45 +02001300 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001301 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001302 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02001303 HDA_AMP_MUTE,
1304 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001305 valp++;
1306 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001307 if (chs & 2)
1308 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02001309 HDA_AMP_MUTE,
1310 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001311#ifdef CONFIG_SND_HDA_POWER_SAVE
1312 if (codec->patch_ops.check_power_status)
1313 codec->patch_ops.check_power_status(codec, nid);
1314#endif
1315 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 return change;
1317}
1318
1319/*
Takashi Iwai985be542005-11-02 18:26:49 +01001320 * bound volume controls
1321 *
1322 * bind multiple volumes (# indices, from 0)
1323 */
1324
1325#define AMP_VAL_IDX_SHIFT 19
1326#define AMP_VAL_IDX_MASK (0x0f<<19)
1327
Takashi Iwai0ba21762007-04-16 11:29:14 +02001328int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
1329 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01001330{
1331 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1332 unsigned long pval;
1333 int err;
1334
Ingo Molnar62932df2006-01-16 16:34:20 +01001335 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Takashi Iwai985be542005-11-02 18:26:49 +01001336 pval = kcontrol->private_value;
1337 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
1338 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
1339 kcontrol->private_value = pval;
Ingo Molnar62932df2006-01-16 16:34:20 +01001340 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01001341 return err;
1342}
1343
Takashi Iwai0ba21762007-04-16 11:29:14 +02001344int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
1345 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01001346{
1347 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1348 unsigned long pval;
1349 int i, indices, err = 0, change = 0;
1350
Ingo Molnar62932df2006-01-16 16:34:20 +01001351 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Takashi Iwai985be542005-11-02 18:26:49 +01001352 pval = kcontrol->private_value;
1353 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
1354 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001355 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
1356 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01001357 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1358 if (err < 0)
1359 break;
1360 change |= err;
1361 }
1362 kcontrol->private_value = pval;
Ingo Molnar62932df2006-01-16 16:34:20 +01001363 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01001364 return err < 0 ? err : change;
1365}
1366
1367/*
Takashi Iwai532d5382007-07-27 19:02:40 +02001368 * generic bound volume/swtich controls
1369 */
1370int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
1371 struct snd_ctl_elem_info *uinfo)
1372{
1373 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1374 struct hda_bind_ctls *c;
1375 int err;
1376
Takashi Iwai532d5382007-07-27 19:02:40 +02001377 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01001378 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02001379 kcontrol->private_value = *c->values;
1380 err = c->ops->info(kcontrol, uinfo);
1381 kcontrol->private_value = (long)c;
1382 mutex_unlock(&codec->spdif_mutex);
1383 return err;
1384}
1385
1386int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
1387 struct snd_ctl_elem_value *ucontrol)
1388{
1389 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1390 struct hda_bind_ctls *c;
1391 int err;
1392
Takashi Iwai532d5382007-07-27 19:02:40 +02001393 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01001394 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02001395 kcontrol->private_value = *c->values;
1396 err = c->ops->get(kcontrol, ucontrol);
1397 kcontrol->private_value = (long)c;
1398 mutex_unlock(&codec->spdif_mutex);
1399 return err;
1400}
1401
1402int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
1403 struct snd_ctl_elem_value *ucontrol)
1404{
1405 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1406 struct hda_bind_ctls *c;
1407 unsigned long *vals;
1408 int err = 0, change = 0;
1409
Takashi Iwai532d5382007-07-27 19:02:40 +02001410 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01001411 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02001412 for (vals = c->values; *vals; vals++) {
1413 kcontrol->private_value = *vals;
1414 err = c->ops->put(kcontrol, ucontrol);
1415 if (err < 0)
1416 break;
1417 change |= err;
1418 }
1419 kcontrol->private_value = (long)c;
1420 mutex_unlock(&codec->spdif_mutex);
1421 return err < 0 ? err : change;
1422}
1423
1424int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1425 unsigned int size, unsigned int __user *tlv)
1426{
1427 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1428 struct hda_bind_ctls *c;
1429 int err;
1430
Takashi Iwai532d5382007-07-27 19:02:40 +02001431 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01001432 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02001433 kcontrol->private_value = *c->values;
1434 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
1435 kcontrol->private_value = (long)c;
1436 mutex_unlock(&codec->spdif_mutex);
1437 return err;
1438}
1439
1440struct hda_ctl_ops snd_hda_bind_vol = {
1441 .info = snd_hda_mixer_amp_volume_info,
1442 .get = snd_hda_mixer_amp_volume_get,
1443 .put = snd_hda_mixer_amp_volume_put,
1444 .tlv = snd_hda_mixer_amp_tlv
1445};
1446
1447struct hda_ctl_ops snd_hda_bind_sw = {
1448 .info = snd_hda_mixer_amp_switch_info,
1449 .get = snd_hda_mixer_amp_switch_get,
1450 .put = snd_hda_mixer_amp_switch_put,
1451 .tlv = snd_hda_mixer_amp_tlv
1452};
1453
1454/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 * SPDIF out controls
1456 */
1457
Takashi Iwai0ba21762007-04-16 11:29:14 +02001458static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
1459 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460{
1461 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1462 uinfo->count = 1;
1463 return 0;
1464}
1465
Takashi Iwai0ba21762007-04-16 11:29:14 +02001466static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
1467 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468{
1469 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
1470 IEC958_AES0_NONAUDIO |
1471 IEC958_AES0_CON_EMPHASIS_5015 |
1472 IEC958_AES0_CON_NOT_COPYRIGHT;
1473 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
1474 IEC958_AES1_CON_ORIGINAL;
1475 return 0;
1476}
1477
Takashi Iwai0ba21762007-04-16 11:29:14 +02001478static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
1479 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480{
1481 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
1482 IEC958_AES0_NONAUDIO |
1483 IEC958_AES0_PRO_EMPHASIS_5015;
1484 return 0;
1485}
1486
Takashi Iwai0ba21762007-04-16 11:29:14 +02001487static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
1488 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489{
1490 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1491
1492 ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
1493 ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
1494 ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
1495 ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
1496
1497 return 0;
1498}
1499
1500/* convert from SPDIF status bits to HDA SPDIF bits
1501 * bit 0 (DigEn) is always set zero (to be filled later)
1502 */
1503static unsigned short convert_from_spdif_status(unsigned int sbits)
1504{
1505 unsigned short val = 0;
1506
1507 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02001508 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02001510 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001512 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
1513 IEC958_AES0_PRO_EMPHASIS_5015)
1514 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001516 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
1517 IEC958_AES0_CON_EMPHASIS_5015)
1518 val |= AC_DIG1_EMPHASIS;
1519 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
1520 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02001522 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
1524 }
1525 return val;
1526}
1527
1528/* convert to SPDIF status bits from HDA SPDIF bits
1529 */
1530static unsigned int convert_to_spdif_status(unsigned short val)
1531{
1532 unsigned int sbits = 0;
1533
Takashi Iwai0ba21762007-04-16 11:29:14 +02001534 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001536 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 sbits |= IEC958_AES0_PROFESSIONAL;
1538 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001539 if (sbits & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
1541 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001542 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001544 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001546 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
1548 sbits |= val & (0x7f << 8);
1549 }
1550 return sbits;
1551}
1552
Takashi Iwai2f728532008-09-25 16:32:41 +02001553/* set digital convert verbs both for the given NID and its slaves */
1554static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
1555 int verb, int val)
1556{
1557 hda_nid_t *d;
1558
1559 snd_hda_codec_write(codec, nid, 0, verb, val);
1560 d = codec->slave_dig_outs;
1561 if (!d)
1562 return;
1563 for (; *d; d++)
1564 snd_hda_codec_write(codec, *d, 0, verb, val);
1565}
1566
1567static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
1568 int dig1, int dig2)
1569{
1570 if (dig1 != -1)
1571 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
1572 if (dig2 != -1)
1573 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
1574}
1575
Takashi Iwai0ba21762007-04-16 11:29:14 +02001576static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
1577 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578{
1579 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1580 hda_nid_t nid = kcontrol->private_value;
1581 unsigned short val;
1582 int change;
1583
Ingo Molnar62932df2006-01-16 16:34:20 +01001584 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 codec->spdif_status = ucontrol->value.iec958.status[0] |
1586 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
1587 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
1588 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
1589 val = convert_from_spdif_status(codec->spdif_status);
1590 val |= codec->spdif_ctls & 1;
1591 change = codec->spdif_ctls != val;
1592 codec->spdif_ctls = val;
1593
Takashi Iwai2f728532008-09-25 16:32:41 +02001594 if (change)
1595 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
Ingo Molnar62932df2006-01-16 16:34:20 +01001597 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 return change;
1599}
1600
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001601#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
Takashi Iwai0ba21762007-04-16 11:29:14 +02001603static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
1604 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605{
1606 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1607
Takashi Iwai0ba21762007-04-16 11:29:14 +02001608 ucontrol->value.integer.value[0] = codec->spdif_ctls & AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 return 0;
1610}
1611
Takashi Iwai0ba21762007-04-16 11:29:14 +02001612static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
1613 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614{
1615 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1616 hda_nid_t nid = kcontrol->private_value;
1617 unsigned short val;
1618 int change;
1619
Ingo Molnar62932df2006-01-16 16:34:20 +01001620 mutex_lock(&codec->spdif_mutex);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001621 val = codec->spdif_ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02001623 val |= AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 change = codec->spdif_ctls != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001625 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 codec->spdif_ctls = val;
Takashi Iwai2f728532008-09-25 16:32:41 +02001627 set_dig_out_convert(codec, nid, val & 0xff, -1);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001628 /* unmute amp switch (if any) */
1629 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
Takashi Iwai47fd8302007-08-10 17:11:07 +02001630 (val & AC_DIG1_ENABLE))
1631 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
1632 HDA_AMP_MUTE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 }
Ingo Molnar62932df2006-01-16 16:34:20 +01001634 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 return change;
1636}
1637
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001638static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 {
1640 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1641 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1642 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
1643 .info = snd_hda_spdif_mask_info,
1644 .get = snd_hda_spdif_cmask_get,
1645 },
1646 {
1647 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1648 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1649 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
1650 .info = snd_hda_spdif_mask_info,
1651 .get = snd_hda_spdif_pmask_get,
1652 },
1653 {
1654 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1655 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1656 .info = snd_hda_spdif_mask_info,
1657 .get = snd_hda_spdif_default_get,
1658 .put = snd_hda_spdif_default_put,
1659 },
1660 {
1661 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1662 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
1663 .info = snd_hda_spdif_out_switch_info,
1664 .get = snd_hda_spdif_out_switch_get,
1665 .put = snd_hda_spdif_out_switch_put,
1666 },
1667 { } /* end */
1668};
1669
Takashi Iwai09f99702008-02-04 12:31:13 +01001670#define SPDIF_MAX_IDX 4 /* 4 instances should be enough to probe */
1671
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672/**
1673 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
1674 * @codec: the HDA codec
1675 * @nid: audio out widget NID
1676 *
1677 * Creates controls related with the SPDIF output.
1678 * Called from each patch supporting the SPDIF out.
1679 *
1680 * Returns 0 if successful, or a negative error code.
1681 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02001682int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683{
1684 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001685 struct snd_kcontrol *kctl;
1686 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01001687 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
Takashi Iwai09f99702008-02-04 12:31:13 +01001689 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
1690 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Playback Switch",
1691 idx))
1692 break;
1693 }
1694 if (idx >= SPDIF_MAX_IDX) {
1695 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
1696 return -EBUSY;
1697 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
1699 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01001700 if (!kctl)
1701 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01001702 kctl->id.index = idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 kctl->private_value = nid;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001704 err = snd_hda_ctl_add(codec, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001705 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 return err;
1707 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02001708 codec->spdif_ctls =
Andrew Paprocki3982d172007-12-19 12:13:44 +01001709 snd_hda_codec_read(codec, nid, 0,
1710 AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
1712 return 0;
1713}
1714
1715/*
Takashi Iwai9a081602008-02-12 18:37:26 +01001716 * SPDIF sharing with analog output
1717 */
1718static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
1719 struct snd_ctl_elem_value *ucontrol)
1720{
1721 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
1722 ucontrol->value.integer.value[0] = mout->share_spdif;
1723 return 0;
1724}
1725
1726static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
1727 struct snd_ctl_elem_value *ucontrol)
1728{
1729 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
1730 mout->share_spdif = !!ucontrol->value.integer.value[0];
1731 return 0;
1732}
1733
1734static struct snd_kcontrol_new spdif_share_sw = {
1735 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1736 .name = "IEC958 Default PCM Playback Switch",
1737 .info = snd_ctl_boolean_mono_info,
1738 .get = spdif_share_sw_get,
1739 .put = spdif_share_sw_put,
1740};
1741
1742int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
1743 struct hda_multi_out *mout)
1744{
1745 if (!mout->dig_out_nid)
1746 return 0;
1747 /* ATTENTION: here mout is passed as private_data, instead of codec */
Takashi Iwaid13bd412008-07-30 15:01:45 +02001748 return snd_hda_ctl_add(codec,
Takashi Iwai9a081602008-02-12 18:37:26 +01001749 snd_ctl_new1(&spdif_share_sw, mout));
1750}
1751
1752/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 * SPDIF input
1754 */
1755
1756#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
1757
Takashi Iwai0ba21762007-04-16 11:29:14 +02001758static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
1759 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760{
1761 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1762
1763 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
1764 return 0;
1765}
1766
Takashi Iwai0ba21762007-04-16 11:29:14 +02001767static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
1768 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769{
1770 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1771 hda_nid_t nid = kcontrol->private_value;
1772 unsigned int val = !!ucontrol->value.integer.value[0];
1773 int change;
1774
Ingo Molnar62932df2006-01-16 16:34:20 +01001775 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001777 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001779 snd_hda_codec_write_cache(codec, nid, 0,
1780 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 }
Ingo Molnar62932df2006-01-16 16:34:20 +01001782 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 return change;
1784}
1785
Takashi Iwai0ba21762007-04-16 11:29:14 +02001786static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
1787 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788{
1789 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1790 hda_nid_t nid = kcontrol->private_value;
1791 unsigned short val;
1792 unsigned int sbits;
1793
Andrew Paprocki3982d172007-12-19 12:13:44 +01001794 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 sbits = convert_to_spdif_status(val);
1796 ucontrol->value.iec958.status[0] = sbits;
1797 ucontrol->value.iec958.status[1] = sbits >> 8;
1798 ucontrol->value.iec958.status[2] = sbits >> 16;
1799 ucontrol->value.iec958.status[3] = sbits >> 24;
1800 return 0;
1801}
1802
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001803static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 {
1805 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1806 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH),
1807 .info = snd_hda_spdif_in_switch_info,
1808 .get = snd_hda_spdif_in_switch_get,
1809 .put = snd_hda_spdif_in_switch_put,
1810 },
1811 {
1812 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1813 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1814 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT),
1815 .info = snd_hda_spdif_mask_info,
1816 .get = snd_hda_spdif_in_status_get,
1817 },
1818 { } /* end */
1819};
1820
1821/**
1822 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
1823 * @codec: the HDA codec
1824 * @nid: audio in widget NID
1825 *
1826 * Creates controls related with the SPDIF input.
1827 * Called from each patch supporting the SPDIF in.
1828 *
1829 * Returns 0 if successful, or a negative error code.
1830 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02001831int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832{
1833 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001834 struct snd_kcontrol *kctl;
1835 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01001836 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
Takashi Iwai09f99702008-02-04 12:31:13 +01001838 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
1839 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Capture Switch",
1840 idx))
1841 break;
1842 }
1843 if (idx >= SPDIF_MAX_IDX) {
1844 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
1845 return -EBUSY;
1846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
1848 kctl = snd_ctl_new1(dig_mix, codec);
1849 kctl->private_value = nid;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001850 err = snd_hda_ctl_add(codec, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001851 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 return err;
1853 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02001854 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01001855 snd_hda_codec_read(codec, nid, 0,
1856 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02001857 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 return 0;
1859}
1860
Takashi Iwaicb53c622007-08-10 17:21:45 +02001861#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001862/*
1863 * command cache
1864 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001866/* build a 32bit cache key with the widget id and the command parameter */
1867#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
1868#define get_cmd_cache_nid(key) ((key) & 0xff)
1869#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
1870
1871/**
1872 * snd_hda_codec_write_cache - send a single command with caching
1873 * @codec: the HDA codec
1874 * @nid: NID to send the command
1875 * @direct: direct flag
1876 * @verb: the verb to send
1877 * @parm: the parameter for the verb
1878 *
1879 * Send a single command without waiting for response.
1880 *
1881 * Returns 0 if successful, or a negative error code.
1882 */
1883int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
1884 int direct, unsigned int verb, unsigned int parm)
1885{
1886 int err;
Takashi Iwaicb53c622007-08-10 17:21:45 +02001887 snd_hda_power_up(codec);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001888 mutex_lock(&codec->bus->cmd_mutex);
1889 err = codec->bus->ops.command(codec, nid, direct, verb, parm);
1890 if (!err) {
1891 struct hda_cache_head *c;
1892 u32 key = build_cmd_cache_key(nid, verb);
1893 c = get_alloc_hash(&codec->cmd_cache, key);
1894 if (c)
1895 c->val = parm;
1896 }
1897 mutex_unlock(&codec->bus->cmd_mutex);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001898 snd_hda_power_down(codec);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001899 return err;
1900}
1901
1902/* resume the all commands from the cache */
1903void snd_hda_codec_resume_cache(struct hda_codec *codec)
1904{
Takashi Iwai603c4012008-07-30 15:01:44 +02001905 struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001906 int i;
1907
Takashi Iwai603c4012008-07-30 15:01:44 +02001908 for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001909 u32 key = buffer->key;
1910 if (!key)
1911 continue;
1912 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
1913 get_cmd_cache_cmd(key), buffer->val);
1914 }
1915}
1916
1917/**
1918 * snd_hda_sequence_write_cache - sequence writes with caching
1919 * @codec: the HDA codec
1920 * @seq: VERB array to send
1921 *
1922 * Send the commands sequentially from the given array.
1923 * Thte commands are recorded on cache for power-save and resume.
1924 * The array must be terminated with NID=0.
1925 */
1926void snd_hda_sequence_write_cache(struct hda_codec *codec,
1927 const struct hda_verb *seq)
1928{
1929 for (; seq->nid; seq++)
1930 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
1931 seq->param);
1932}
Takashi Iwaicb53c622007-08-10 17:21:45 +02001933#endif /* SND_HDA_NEEDS_RESUME */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001934
Takashi Iwai54d17402005-11-21 16:33:22 +01001935/*
1936 * set power state of the codec
1937 */
1938static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1939 unsigned int power_state)
1940{
Takashi Iwaicb53c622007-08-10 17:21:45 +02001941 hda_nid_t nid;
1942 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01001943
1944 snd_hda_codec_write(codec, fg, 0, AC_VERB_SET_POWER_STATE,
1945 power_state);
Marc Boucherd2595d82008-01-22 15:23:30 +01001946 msleep(10); /* partial workaround for "azx_get_response timeout" */
Takashi Iwai54d17402005-11-21 16:33:22 +01001947
Takashi Iwaicb53c622007-08-10 17:21:45 +02001948 nid = codec->start_nid;
1949 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01001950 unsigned int wcaps = get_wcaps(codec, nid);
1951 if (wcaps & AC_WCAP_POWER) {
1952 unsigned int wid_type = (wcaps & AC_WCAP_TYPE) >>
1953 AC_WCAP_TYPE_SHIFT;
1954 if (wid_type == AC_WID_PIN) {
1955 unsigned int pincap;
1956 /*
1957 * don't power down the widget if it controls
1958 * eapd and EAPD_BTLENABLE is set.
1959 */
1960 pincap = snd_hda_param_read(codec, nid,
1961 AC_PAR_PIN_CAP);
1962 if (pincap & AC_PINCAP_EAPD) {
1963 int eapd = snd_hda_codec_read(codec,
1964 nid, 0,
1965 AC_VERB_GET_EAPD_BTLENABLE, 0);
1966 eapd &= 0x02;
1967 if (power_state == AC_PWRST_D3 && eapd)
1968 continue;
1969 }
Takashi Iwai1194b5b2007-10-10 10:04:26 +02001970 }
Takashi Iwai54d17402005-11-21 16:33:22 +01001971 snd_hda_codec_write(codec, nid, 0,
1972 AC_VERB_SET_POWER_STATE,
1973 power_state);
Takashi Iwai1194b5b2007-10-10 10:04:26 +02001974 }
Takashi Iwai54d17402005-11-21 16:33:22 +01001975 }
1976
Takashi Iwaicb53c622007-08-10 17:21:45 +02001977 if (power_state == AC_PWRST_D0) {
1978 unsigned long end_time;
1979 int state;
Takashi Iwai54d17402005-11-21 16:33:22 +01001980 msleep(10);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001981 /* wait until the codec reachs to D0 */
1982 end_time = jiffies + msecs_to_jiffies(500);
1983 do {
1984 state = snd_hda_codec_read(codec, fg, 0,
1985 AC_VERB_GET_POWER_STATE, 0);
1986 if (state == power_state)
1987 break;
1988 msleep(1);
1989 } while (time_after_eq(end_time, jiffies));
1990 }
Takashi Iwai54d17402005-11-21 16:33:22 +01001991}
1992
Takashi Iwai11aeff02008-07-30 15:01:46 +02001993#ifdef CONFIG_SND_HDA_HWDEP
1994/* execute additional init verbs */
1995static void hda_exec_init_verbs(struct hda_codec *codec)
1996{
1997 if (codec->init_verbs.list)
1998 snd_hda_sequence_write(codec, codec->init_verbs.list);
1999}
2000#else
2001static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
2002#endif
2003
Takashi Iwaicb53c622007-08-10 17:21:45 +02002004#ifdef SND_HDA_NEEDS_RESUME
2005/*
2006 * call suspend and power-down; used both from PM and power-save
2007 */
2008static void hda_call_codec_suspend(struct hda_codec *codec)
2009{
2010 if (codec->patch_ops.suspend)
2011 codec->patch_ops.suspend(codec, PMSG_SUSPEND);
2012 hda_set_power_state(codec,
2013 codec->afg ? codec->afg : codec->mfg,
2014 AC_PWRST_D3);
2015#ifdef CONFIG_SND_HDA_POWER_SAVE
2016 cancel_delayed_work(&codec->power_work);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02002017 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02002018 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002019#endif
2020}
2021
2022/*
2023 * kick up codec; used both from PM and power-save
2024 */
2025static void hda_call_codec_resume(struct hda_codec *codec)
2026{
2027 hda_set_power_state(codec,
2028 codec->afg ? codec->afg : codec->mfg,
2029 AC_PWRST_D0);
Takashi Iwai11aeff02008-07-30 15:01:46 +02002030 hda_exec_init_verbs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002031 if (codec->patch_ops.resume)
2032 codec->patch_ops.resume(codec);
2033 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02002034 if (codec->patch_ops.init)
2035 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002036 snd_hda_codec_resume_amp(codec);
2037 snd_hda_codec_resume_cache(codec);
2038 }
2039}
2040#endif /* SND_HDA_NEEDS_RESUME */
2041
Takashi Iwai54d17402005-11-21 16:33:22 +01002042
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043/**
2044 * snd_hda_build_controls - build mixer controls
2045 * @bus: the BUS
2046 *
2047 * Creates mixer controls for each codec included in the bus.
2048 *
2049 * Returns 0 if successful, otherwise a negative error code.
2050 */
Takashi Iwai756e2b02007-04-16 11:27:07 +02002051int __devinit snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052{
Takashi Iwai0ba21762007-04-16 11:29:14 +02002053 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
Takashi Iwai0ba21762007-04-16 11:29:14 +02002055 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002056 int err = snd_hda_codec_build_controls(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 if (err < 0)
2058 return err;
2059 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002060 return 0;
2061}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002063int snd_hda_codec_build_controls(struct hda_codec *codec)
2064{
2065 int err = 0;
2066 /* fake as if already powered-on */
2067 hda_keep_power_on(codec);
2068 /* then fire up */
2069 hda_set_power_state(codec,
2070 codec->afg ? codec->afg : codec->mfg,
2071 AC_PWRST_D0);
Takashi Iwai11aeff02008-07-30 15:01:46 +02002072 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002073 /* continue to initialize... */
2074 if (codec->patch_ops.init)
2075 err = codec->patch_ops.init(codec);
2076 if (!err && codec->patch_ops.build_controls)
2077 err = codec->patch_ops.build_controls(codec);
2078 snd_hda_power_down(codec);
2079 if (err < 0)
2080 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 return 0;
2082}
2083
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084/*
2085 * stream formats
2086 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02002087struct hda_rate_tbl {
2088 unsigned int hz;
2089 unsigned int alsa_bits;
2090 unsigned int hda_fmt;
2091};
2092
2093static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02002095
2096 /* autodetected value used in snd_hda_query_supported_pcm */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 { 8000, SNDRV_PCM_RATE_8000, 0x0500 }, /* 1/6 x 48 */
2098 { 11025, SNDRV_PCM_RATE_11025, 0x4300 }, /* 1/4 x 44 */
2099 { 16000, SNDRV_PCM_RATE_16000, 0x0200 }, /* 1/3 x 48 */
2100 { 22050, SNDRV_PCM_RATE_22050, 0x4100 }, /* 1/2 x 44 */
2101 { 32000, SNDRV_PCM_RATE_32000, 0x0a00 }, /* 2/3 x 48 */
2102 { 44100, SNDRV_PCM_RATE_44100, 0x4000 }, /* 44 */
2103 { 48000, SNDRV_PCM_RATE_48000, 0x0000 }, /* 48 */
2104 { 88200, SNDRV_PCM_RATE_88200, 0x4800 }, /* 2 x 44 */
2105 { 96000, SNDRV_PCM_RATE_96000, 0x0800 }, /* 2 x 48 */
2106 { 176400, SNDRV_PCM_RATE_176400, 0x5800 },/* 4 x 44 */
2107 { 192000, SNDRV_PCM_RATE_192000, 0x1800 }, /* 4 x 48 */
Takashi Iwaia961f9f2007-04-12 13:08:09 +02002108#define AC_PAR_PCM_RATE_BITS 11
2109 /* up to bits 10, 384kHZ isn't supported properly */
2110
2111 /* not autodetected value */
2112 { 9600, SNDRV_PCM_RATE_KNOT, 0x0400 }, /* 1/5 x 48 */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02002113
Takashi Iwaibefdf312005-08-22 13:57:55 +02002114 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115};
2116
2117/**
2118 * snd_hda_calc_stream_format - calculate format bitset
2119 * @rate: the sample rate
2120 * @channels: the number of channels
2121 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
2122 * @maxbps: the max. bps
2123 *
2124 * Calculate the format bitset from the given rate, channels and th PCM format.
2125 *
2126 * Return zero if invalid.
2127 */
2128unsigned int snd_hda_calc_stream_format(unsigned int rate,
2129 unsigned int channels,
2130 unsigned int format,
2131 unsigned int maxbps)
2132{
2133 int i;
2134 unsigned int val = 0;
2135
Takashi Iwaibefdf312005-08-22 13:57:55 +02002136 for (i = 0; rate_bits[i].hz; i++)
2137 if (rate_bits[i].hz == rate) {
2138 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 break;
2140 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002141 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 snd_printdd("invalid rate %d\n", rate);
2143 return 0;
2144 }
2145
2146 if (channels == 0 || channels > 8) {
2147 snd_printdd("invalid channels %d\n", channels);
2148 return 0;
2149 }
2150 val |= channels - 1;
2151
2152 switch (snd_pcm_format_width(format)) {
2153 case 8: val |= 0x00; break;
2154 case 16: val |= 0x10; break;
2155 case 20:
2156 case 24:
2157 case 32:
2158 if (maxbps >= 32)
2159 val |= 0x40;
2160 else if (maxbps >= 24)
2161 val |= 0x30;
2162 else
2163 val |= 0x20;
2164 break;
2165 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002166 snd_printdd("invalid format width %d\n",
2167 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 return 0;
2169 }
2170
2171 return val;
2172}
2173
2174/**
2175 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
2176 * @codec: the HDA codec
2177 * @nid: NID to query
2178 * @ratesp: the pointer to store the detected rate bitflags
2179 * @formatsp: the pointer to store the detected formats
2180 * @bpsp: the pointer to store the detected format widths
2181 *
2182 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
2183 * or @bsps argument is ignored.
2184 *
2185 * Returns 0 if successful, otherwise a negative error code.
2186 */
2187int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
2188 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
2189{
2190 int i;
2191 unsigned int val, streams;
2192
2193 val = 0;
2194 if (nid != codec->afg &&
Takashi Iwai54d17402005-11-21 16:33:22 +01002195 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
2197 if (val == -1)
2198 return -EIO;
2199 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002200 if (!val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
2202
2203 if (ratesp) {
2204 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02002205 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02002207 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 }
2209 *ratesp = rates;
2210 }
2211
2212 if (formatsp || bpsp) {
2213 u64 formats = 0;
2214 unsigned int bps;
2215 unsigned int wcaps;
2216
Takashi Iwai54d17402005-11-21 16:33:22 +01002217 wcaps = get_wcaps(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
2219 if (streams == -1)
2220 return -EIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002221 if (!streams) {
2222 streams = snd_hda_param_read(codec, codec->afg,
2223 AC_PAR_STREAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 if (streams == -1)
2225 return -EIO;
2226 }
2227
2228 bps = 0;
2229 if (streams & AC_SUPFMT_PCM) {
2230 if (val & AC_SUPPCM_BITS_8) {
2231 formats |= SNDRV_PCM_FMTBIT_U8;
2232 bps = 8;
2233 }
2234 if (val & AC_SUPPCM_BITS_16) {
2235 formats |= SNDRV_PCM_FMTBIT_S16_LE;
2236 bps = 16;
2237 }
2238 if (wcaps & AC_WCAP_DIGITAL) {
2239 if (val & AC_SUPPCM_BITS_32)
2240 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
2241 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
2242 formats |= SNDRV_PCM_FMTBIT_S32_LE;
2243 if (val & AC_SUPPCM_BITS_24)
2244 bps = 24;
2245 else if (val & AC_SUPPCM_BITS_20)
2246 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002247 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
2248 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 formats |= SNDRV_PCM_FMTBIT_S32_LE;
2250 if (val & AC_SUPPCM_BITS_32)
2251 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 else if (val & AC_SUPPCM_BITS_24)
2253 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02002254 else if (val & AC_SUPPCM_BITS_20)
2255 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 }
2257 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002258 else if (streams == AC_SUPFMT_FLOAT32) {
2259 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
2261 bps = 32;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002262 } else if (streams == AC_SUPFMT_AC3) {
2263 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 /* temporary hack: we have still no proper support
2265 * for the direct AC3 stream...
2266 */
2267 formats |= SNDRV_PCM_FMTBIT_U8;
2268 bps = 8;
2269 }
2270 if (formatsp)
2271 *formatsp = formats;
2272 if (bpsp)
2273 *bpsp = bps;
2274 }
2275
2276 return 0;
2277}
2278
2279/**
Takashi Iwai0ba21762007-04-16 11:29:14 +02002280 * snd_hda_is_supported_format - check whether the given node supports
2281 * the format val
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 *
2283 * Returns 1 if supported, 0 if not.
2284 */
2285int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
2286 unsigned int format)
2287{
2288 int i;
2289 unsigned int val = 0, rate, stream;
2290
2291 if (nid != codec->afg &&
Takashi Iwai54d17402005-11-21 16:33:22 +01002292 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
2294 if (val == -1)
2295 return 0;
2296 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002297 if (!val) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
2299 if (val == -1)
2300 return 0;
2301 }
2302
2303 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02002304 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02002305 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 if (val & (1 << i))
2307 break;
2308 return 0;
2309 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02002310 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 return 0;
2312
2313 stream = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
2314 if (stream == -1)
2315 return 0;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002316 if (!stream && nid != codec->afg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 stream = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002318 if (!stream || stream == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 return 0;
2320
2321 if (stream & AC_SUPFMT_PCM) {
2322 switch (format & 0xf0) {
2323 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002324 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 return 0;
2326 break;
2327 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002328 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 return 0;
2330 break;
2331 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002332 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 return 0;
2334 break;
2335 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002336 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 return 0;
2338 break;
2339 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002340 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 return 0;
2342 break;
2343 default:
2344 return 0;
2345 }
2346 } else {
2347 /* FIXME: check for float32 and AC3? */
2348 }
2349
2350 return 1;
2351}
2352
2353/*
2354 * PCM stuff
2355 */
2356static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
2357 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002358 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359{
2360 return 0;
2361}
2362
2363static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
2364 struct hda_codec *codec,
2365 unsigned int stream_tag,
2366 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002367 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368{
2369 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
2370 return 0;
2371}
2372
2373static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
2374 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002375 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376{
Takashi Iwai888afa12008-03-18 09:57:50 +01002377 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 return 0;
2379}
2380
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002381static int set_pcm_default_values(struct hda_codec *codec,
2382 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383{
Takashi Iwai0ba21762007-04-16 11:29:14 +02002384 /* query support PCM information from the given NID */
2385 if (info->nid && (!info->rates || !info->formats)) {
2386 snd_hda_query_supported_pcm(codec, info->nid,
2387 info->rates ? NULL : &info->rates,
2388 info->formats ? NULL : &info->formats,
2389 info->maxbps ? NULL : &info->maxbps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 }
2391 if (info->ops.open == NULL)
2392 info->ops.open = hda_pcm_default_open_close;
2393 if (info->ops.close == NULL)
2394 info->ops.close = hda_pcm_default_open_close;
2395 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02002396 if (snd_BUG_ON(!info->nid))
2397 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 info->ops.prepare = hda_pcm_default_prepare;
2399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02002401 if (snd_BUG_ON(!info->nid))
2402 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 info->ops.cleanup = hda_pcm_default_cleanup;
2404 }
2405 return 0;
2406}
2407
Takashi Iwai176d5332008-07-30 15:01:44 +02002408/*
2409 * attach a new PCM stream
2410 */
2411static int __devinit
2412snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
2413{
2414 struct hda_pcm_stream *info;
2415 int stream, err;
2416
Takashi Iwaib91f0802008-11-04 08:43:08 +01002417 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02002418 return -EINVAL;
2419 for (stream = 0; stream < 2; stream++) {
2420 info = &pcm->stream[stream];
2421 if (info->substreams) {
2422 err = set_pcm_default_values(codec, info);
2423 if (err < 0)
2424 return err;
2425 }
2426 }
2427 return codec->bus->ops.attach_pcm(codec, pcm);
2428}
2429
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430/**
2431 * snd_hda_build_pcms - build PCM information
2432 * @bus: the BUS
2433 *
2434 * Create PCM information for each codec included in the bus.
2435 *
2436 * The build_pcms codec patch is requested to set up codec->num_pcms and
2437 * codec->pcm_info properly. The array is referred by the top-level driver
2438 * to create its PCM instances.
2439 * The allocated codec->pcm_info should be released in codec->patch_ops.free
2440 * callback.
2441 *
2442 * At least, substreams, channels_min and channels_max must be filled for
2443 * each stream. substreams = 0 indicates that the stream doesn't exist.
2444 * When rates and/or formats are zero, the supported values are queried
2445 * from the given nid. The nid is used also by the default ops.prepare
2446 * and ops.cleanup callbacks.
2447 *
2448 * The driver needs to call ops.open in its open callback. Similarly,
2449 * ops.close is supposed to be called in the close callback.
2450 * ops.prepare should be called in the prepare or hw_params callback
2451 * with the proper parameters for set up.
2452 * ops.cleanup should be called in hw_free for clean up of streams.
2453 *
2454 * This function returns 0 if successfull, or a negative error code.
2455 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002456int snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457{
Takashi Iwai176d5332008-07-30 15:01:44 +02002458 static const char *dev_name[HDA_PCM_NTYPES] = {
2459 "Audio", "SPDIF", "HDMI", "Modem"
2460 };
2461 /* starting device index for each PCM type */
2462 static int dev_idx[HDA_PCM_NTYPES] = {
2463 [HDA_PCM_TYPE_AUDIO] = 0,
2464 [HDA_PCM_TYPE_SPDIF] = 1,
2465 [HDA_PCM_TYPE_HDMI] = 3,
2466 [HDA_PCM_TYPE_MODEM] = 6
2467 };
2468 /* normal audio device indices; not linear to keep compatibility */
2469 static int audio_idx[4] = { 0, 2, 4, 5 };
Takashi Iwai0ba21762007-04-16 11:29:14 +02002470 struct hda_codec *codec;
Takashi Iwai176d5332008-07-30 15:01:44 +02002471 int num_devs[HDA_PCM_NTYPES];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472
Takashi Iwai176d5332008-07-30 15:01:44 +02002473 memset(num_devs, 0, sizeof(num_devs));
Takashi Iwai0ba21762007-04-16 11:29:14 +02002474 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai176d5332008-07-30 15:01:44 +02002475 unsigned int pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 int err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002477 if (!codec->num_pcms) {
2478 if (!codec->patch_ops.build_pcms)
2479 continue;
2480 err = codec->patch_ops.build_pcms(codec);
2481 if (err < 0)
2482 return err;
2483 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
Takashi Iwai176d5332008-07-30 15:01:44 +02002485 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2486 int type = cpcm->pcm_type;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002487 int dev;
Takashi Iwai176d5332008-07-30 15:01:44 +02002488 switch (type) {
2489 case HDA_PCM_TYPE_AUDIO:
2490 if (num_devs[type] >= ARRAY_SIZE(audio_idx)) {
2491 snd_printk(KERN_WARNING
2492 "Too many audio devices\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 continue;
Takashi Iwai176d5332008-07-30 15:01:44 +02002494 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002495 dev = audio_idx[num_devs[type]];
Takashi Iwai176d5332008-07-30 15:01:44 +02002496 break;
2497 case HDA_PCM_TYPE_SPDIF:
2498 case HDA_PCM_TYPE_HDMI:
2499 case HDA_PCM_TYPE_MODEM:
2500 if (num_devs[type]) {
2501 snd_printk(KERN_WARNING
2502 "%s already defined\n",
2503 dev_name[type]);
2504 continue;
2505 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002506 dev = dev_idx[type];
Takashi Iwai176d5332008-07-30 15:01:44 +02002507 break;
2508 default:
2509 snd_printk(KERN_WARNING
2510 "Invalid PCM type %d\n", type);
2511 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 }
Takashi Iwai176d5332008-07-30 15:01:44 +02002513 num_devs[type]++;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002514 if (!cpcm->pcm) {
2515 cpcm->device = dev;
2516 err = snd_hda_attach_pcm(codec, cpcm);
2517 if (err < 0)
2518 return err;
2519 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 }
2521 }
2522 return 0;
2523}
2524
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525/**
2526 * snd_hda_check_board_config - compare the current codec with the config table
2527 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002528 * @num_configs: number of config enums
2529 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 * @tbl: configuration table, terminated by null entries
2531 *
2532 * Compares the modelname or PCI subsystem id of the current codec with the
2533 * given configuration table. If a matching entry is found, returns its
2534 * config value (supposed to be 0 or positive).
2535 *
2536 * If no entries are matching, the function returns a negative value.
2537 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002538int snd_hda_check_board_config(struct hda_codec *codec,
2539 int num_configs, const char **models,
2540 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541{
Takashi Iwaif44ac832008-07-30 15:01:45 +02002542 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002543 int i;
2544 for (i = 0; i < num_configs; i++) {
2545 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02002546 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002547 snd_printd(KERN_INFO "hda_codec: model '%s' is "
2548 "selected\n", models[i]);
2549 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 }
2551 }
2552 }
2553
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002554 if (!codec->bus->pci || !tbl)
2555 return -1;
2556
2557 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
2558 if (!tbl)
2559 return -1;
2560 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02002561#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002562 char tmp[10];
2563 const char *model = NULL;
2564 if (models)
2565 model = models[tbl->value];
2566 if (!model) {
2567 sprintf(tmp, "#%d", tbl->value);
2568 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002570 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
2571 "for config %x:%x (%s)\n",
2572 model, tbl->subvendor, tbl->subdevice,
2573 (tbl->name ? tbl->name : "Unknown device"));
2574#endif
2575 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 }
2577 return -1;
2578}
2579
2580/**
2581 * snd_hda_add_new_ctls - create controls from the array
2582 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002583 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 *
2585 * This helper function creates and add new controls in the given array.
2586 * The array must be terminated with an empty entry as terminator.
2587 *
2588 * Returns 0 if successful, or a negative error code.
2589 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002590int snd_hda_add_new_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591{
Takashi Iwaicb53c622007-08-10 17:21:45 +02002592 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593
2594 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01002595 struct snd_kcontrol *kctl;
2596 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002597 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01002598 return -ENOMEM;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002599 err = snd_hda_ctl_add(codec, kctl);
Takashi Iwai54d17402005-11-21 16:33:22 +01002600 if (err < 0) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002601 if (!codec->addr)
Takashi Iwai54d17402005-11-21 16:33:22 +01002602 return err;
2603 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002604 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01002605 return -ENOMEM;
2606 kctl->id.device = codec->addr;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002607 err = snd_hda_ctl_add(codec, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002608 if (err < 0)
Takashi Iwai54d17402005-11-21 16:33:22 +01002609 return err;
2610 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 }
2612 return 0;
2613}
2614
Takashi Iwaicb53c622007-08-10 17:21:45 +02002615#ifdef CONFIG_SND_HDA_POWER_SAVE
2616static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2617 unsigned int power_state);
2618
2619static void hda_power_work(struct work_struct *work)
2620{
2621 struct hda_codec *codec =
2622 container_of(work, struct hda_codec, power_work.work);
2623
Maxim Levitsky2e492462007-09-03 15:26:57 +02002624 if (!codec->power_on || codec->power_count) {
2625 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002626 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02002627 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02002628
2629 hda_call_codec_suspend(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002630 if (codec->bus->ops.pm_notify)
2631 codec->bus->ops.pm_notify(codec);
2632}
2633
2634static void hda_keep_power_on(struct hda_codec *codec)
2635{
2636 codec->power_count++;
2637 codec->power_on = 1;
2638}
2639
2640void snd_hda_power_up(struct hda_codec *codec)
2641{
2642 codec->power_count++;
Takashi Iwaia221e282007-08-16 16:35:33 +02002643 if (codec->power_on || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02002644 return;
2645
2646 codec->power_on = 1;
2647 if (codec->bus->ops.pm_notify)
2648 codec->bus->ops.pm_notify(codec);
2649 hda_call_codec_resume(codec);
2650 cancel_delayed_work(&codec->power_work);
Takashi Iwaia221e282007-08-16 16:35:33 +02002651 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002652}
2653
2654void snd_hda_power_down(struct hda_codec *codec)
2655{
2656 --codec->power_count;
Takashi Iwaia221e282007-08-16 16:35:33 +02002657 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02002658 return;
Takashi Iwaia221e282007-08-16 16:35:33 +02002659 if (power_save) {
2660 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002661 schedule_delayed_work(&codec->power_work,
2662 msecs_to_jiffies(power_save * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02002663 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02002664}
2665
2666int snd_hda_check_amp_list_power(struct hda_codec *codec,
2667 struct hda_loopback_check *check,
2668 hda_nid_t nid)
2669{
2670 struct hda_amp_list *p;
2671 int ch, v;
2672
2673 if (!check->amplist)
2674 return 0;
2675 for (p = check->amplist; p->nid; p++) {
2676 if (p->nid == nid)
2677 break;
2678 }
2679 if (!p->nid)
2680 return 0; /* nothing changed */
2681
2682 for (p = check->amplist; p->nid; p++) {
2683 for (ch = 0; ch < 2; ch++) {
2684 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
2685 p->idx);
2686 if (!(v & HDA_AMP_MUTE) && v > 0) {
2687 if (!check->power_on) {
2688 check->power_on = 1;
2689 snd_hda_power_up(codec);
2690 }
2691 return 1;
2692 }
2693 }
2694 }
2695 if (check->power_on) {
2696 check->power_on = 0;
2697 snd_hda_power_down(codec);
2698 }
2699 return 0;
2700}
2701#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002703/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002704 * Channel mode helper
2705 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002706int snd_hda_ch_mode_info(struct hda_codec *codec,
2707 struct snd_ctl_elem_info *uinfo,
2708 const struct hda_channel_mode *chmode,
2709 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002710{
2711 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2712 uinfo->count = 1;
2713 uinfo->value.enumerated.items = num_chmodes;
2714 if (uinfo->value.enumerated.item >= num_chmodes)
2715 uinfo->value.enumerated.item = num_chmodes - 1;
2716 sprintf(uinfo->value.enumerated.name, "%dch",
2717 chmode[uinfo->value.enumerated.item].channels);
2718 return 0;
2719}
2720
Takashi Iwai0ba21762007-04-16 11:29:14 +02002721int snd_hda_ch_mode_get(struct hda_codec *codec,
2722 struct snd_ctl_elem_value *ucontrol,
2723 const struct hda_channel_mode *chmode,
2724 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002725 int max_channels)
2726{
2727 int i;
2728
2729 for (i = 0; i < num_chmodes; i++) {
2730 if (max_channels == chmode[i].channels) {
2731 ucontrol->value.enumerated.item[0] = i;
2732 break;
2733 }
2734 }
2735 return 0;
2736}
2737
Takashi Iwai0ba21762007-04-16 11:29:14 +02002738int snd_hda_ch_mode_put(struct hda_codec *codec,
2739 struct snd_ctl_elem_value *ucontrol,
2740 const struct hda_channel_mode *chmode,
2741 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002742 int *max_channelsp)
2743{
2744 unsigned int mode;
2745
2746 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01002747 if (mode >= num_chmodes)
2748 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002749 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002750 return 0;
2751 /* change the current channel setting */
2752 *max_channelsp = chmode[mode].channels;
2753 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002754 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002755 return 1;
2756}
2757
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758/*
2759 * input MUX helper
2760 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002761int snd_hda_input_mux_info(const struct hda_input_mux *imux,
2762 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763{
2764 unsigned int index;
2765
2766 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2767 uinfo->count = 1;
2768 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02002769 if (!imux->num_items)
2770 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 index = uinfo->value.enumerated.item;
2772 if (index >= imux->num_items)
2773 index = imux->num_items - 1;
2774 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
2775 return 0;
2776}
2777
Takashi Iwai0ba21762007-04-16 11:29:14 +02002778int snd_hda_input_mux_put(struct hda_codec *codec,
2779 const struct hda_input_mux *imux,
2780 struct snd_ctl_elem_value *ucontrol,
2781 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 unsigned int *cur_val)
2783{
2784 unsigned int idx;
2785
Takashi Iwai5513b0c2007-10-09 11:58:41 +02002786 if (!imux->num_items)
2787 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 idx = ucontrol->value.enumerated.item[0];
2789 if (idx >= imux->num_items)
2790 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002791 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002793 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
2794 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 *cur_val = idx;
2796 return 1;
2797}
2798
2799
2800/*
2801 * Multi-channel / digital-out PCM helper functions
2802 */
2803
Takashi Iwai6b97eb42007-04-05 14:51:48 +02002804/* setup SPDIF output stream */
2805static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
2806 unsigned int stream_tag, unsigned int format)
2807{
2808 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Takashi Iwai2f728532008-09-25 16:32:41 +02002809 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
2810 set_dig_out_convert(codec, nid,
2811 codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff,
2812 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02002813 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02002814 if (codec->slave_dig_outs) {
2815 hda_nid_t *d;
2816 for (d = codec->slave_dig_outs; *d; d++)
2817 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
2818 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04002819 }
Takashi Iwai2f728532008-09-25 16:32:41 +02002820 /* turn on again (if needed) */
2821 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
2822 set_dig_out_convert(codec, nid,
2823 codec->spdif_ctls & 0xff, -1);
2824}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04002825
Takashi Iwai2f728532008-09-25 16:32:41 +02002826static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
2827{
2828 snd_hda_codec_cleanup_stream(codec, nid);
2829 if (codec->slave_dig_outs) {
2830 hda_nid_t *d;
2831 for (d = codec->slave_dig_outs; *d; d++)
2832 snd_hda_codec_cleanup_stream(codec, *d);
2833 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02002834}
2835
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836/*
2837 * open the digital out in the exclusive mode
2838 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002839int snd_hda_multi_out_dig_open(struct hda_codec *codec,
2840 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841{
Ingo Molnar62932df2006-01-16 16:34:20 +01002842 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02002843 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
2844 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02002845 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01002847 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 return 0;
2849}
2850
Takashi Iwai6b97eb42007-04-05 14:51:48 +02002851int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
2852 struct hda_multi_out *mout,
2853 unsigned int stream_tag,
2854 unsigned int format,
2855 struct snd_pcm_substream *substream)
2856{
2857 mutex_lock(&codec->spdif_mutex);
2858 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
2859 mutex_unlock(&codec->spdif_mutex);
2860 return 0;
2861}
2862
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863/*
2864 * release the digital out
2865 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002866int snd_hda_multi_out_dig_close(struct hda_codec *codec,
2867 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868{
Ingo Molnar62932df2006-01-16 16:34:20 +01002869 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01002871 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 return 0;
2873}
2874
2875/*
2876 * set up more restrictions for analog out
2877 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002878int snd_hda_multi_out_analog_open(struct hda_codec *codec,
2879 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01002880 struct snd_pcm_substream *substream,
2881 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882{
Takashi Iwai9a081602008-02-12 18:37:26 +01002883 struct snd_pcm_runtime *runtime = substream->runtime;
2884 runtime->hw.channels_max = mout->max_channels;
2885 if (mout->dig_out_nid) {
2886 if (!mout->analog_rates) {
2887 mout->analog_rates = hinfo->rates;
2888 mout->analog_formats = hinfo->formats;
2889 mout->analog_maxbps = hinfo->maxbps;
2890 } else {
2891 runtime->hw.rates = mout->analog_rates;
2892 runtime->hw.formats = mout->analog_formats;
2893 hinfo->maxbps = mout->analog_maxbps;
2894 }
2895 if (!mout->spdif_rates) {
2896 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
2897 &mout->spdif_rates,
2898 &mout->spdif_formats,
2899 &mout->spdif_maxbps);
2900 }
2901 mutex_lock(&codec->spdif_mutex);
2902 if (mout->share_spdif) {
2903 runtime->hw.rates &= mout->spdif_rates;
2904 runtime->hw.formats &= mout->spdif_formats;
2905 if (mout->spdif_maxbps < hinfo->maxbps)
2906 hinfo->maxbps = mout->spdif_maxbps;
2907 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02002908 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01002909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 return snd_pcm_hw_constraint_step(substream->runtime, 0,
2911 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
2912}
2913
2914/*
2915 * set up the i/o for analog out
2916 * when the digital out is available, copy the front out to digital out, too.
2917 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002918int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
2919 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 unsigned int stream_tag,
2921 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002922 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923{
2924 hda_nid_t *nids = mout->dac_nids;
2925 int chs = substream->runtime->channels;
2926 int i;
2927
Ingo Molnar62932df2006-01-16 16:34:20 +01002928 mutex_lock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01002929 if (mout->dig_out_nid && mout->share_spdif &&
2930 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02002932 snd_hda_is_supported_format(codec, mout->dig_out_nid,
2933 format) &&
2934 !(codec->spdif_status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02002936 setup_dig_out_stream(codec, mout->dig_out_nid,
2937 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 } else {
2939 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02002940 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 }
2942 }
Ingo Molnar62932df2006-01-16 16:34:20 +01002943 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944
2945 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002946 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
2947 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02002948 if (!mout->no_share_stream &&
2949 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002951 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
2952 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01002953 /* extra outputs copied from front */
2954 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02002955 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01002956 snd_hda_codec_setup_stream(codec,
2957 mout->extra_out_nid[i],
2958 stream_tag, 0, format);
2959
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 /* surrounds */
2961 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02002962 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002963 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
2964 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02002965 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002966 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
2967 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 }
2969 return 0;
2970}
2971
2972/*
2973 * clean up the setting for analog out
2974 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002975int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
2976 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977{
2978 hda_nid_t *nids = mout->dac_nids;
2979 int i;
2980
2981 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01002982 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01002984 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwai82bc9552006-03-21 11:24:42 +01002985 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
2986 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01002987 snd_hda_codec_cleanup_stream(codec,
2988 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01002989 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02002991 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 mout->dig_out_used = 0;
2993 }
Ingo Molnar62932df2006-01-16 16:34:20 +01002994 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 return 0;
2996}
2997
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002998/*
Wu Fengguang6b345002008-10-07 14:21:41 +08002999 * Helper for automatic pin configuration
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003000 */
Kailang Yangdf694da2005-12-05 19:42:22 +01003001
Takashi Iwai12f288b2007-08-02 15:51:59 +02003002static int is_in_nid_list(hda_nid_t nid, hda_nid_t *list)
Kailang Yangdf694da2005-12-05 19:42:22 +01003003{
3004 for (; *list; list++)
3005 if (*list == nid)
3006 return 1;
3007 return 0;
3008}
3009
Steve Longerbeam81937d32007-05-08 15:33:03 +02003010
3011/*
3012 * Sort an associated group of pins according to their sequence numbers.
3013 */
3014static void sort_pins_by_sequence(hda_nid_t * pins, short * sequences,
3015 int num_pins)
3016{
3017 int i, j;
3018 short seq;
3019 hda_nid_t nid;
3020
3021 for (i = 0; i < num_pins; i++) {
3022 for (j = i + 1; j < num_pins; j++) {
3023 if (sequences[i] > sequences[j]) {
3024 seq = sequences[i];
3025 sequences[i] = sequences[j];
3026 sequences[j] = seq;
3027 nid = pins[i];
3028 pins[i] = pins[j];
3029 pins[j] = nid;
3030 }
3031 }
3032 }
3033}
3034
3035
Takashi Iwai82bc9552006-03-21 11:24:42 +01003036/*
3037 * Parse all pin widgets and store the useful pin nids to cfg
3038 *
3039 * The number of line-outs or any primary output is stored in line_outs,
3040 * and the corresponding output pins are assigned to line_out_pins[],
3041 * in the order of front, rear, CLFE, side, ...
3042 *
3043 * If more extra outputs (speaker and headphone) are found, the pins are
Takashi Iwaieb06ed82006-09-20 17:10:27 +02003044 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
Takashi Iwai82bc9552006-03-21 11:24:42 +01003045 * is detected, one of speaker of HP pins is assigned as the primary
3046 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
3047 * if any analog output exists.
3048 *
3049 * The analog input pins are assigned to input_pins array.
3050 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
3051 * respectively.
3052 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003053int snd_hda_parse_pin_def_config(struct hda_codec *codec,
3054 struct auto_pin_cfg *cfg,
3055 hda_nid_t *ignore_nids)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003056{
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01003057 hda_nid_t nid, end_nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02003058 short seq, assoc_line_out, assoc_speaker;
3059 short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
3060 short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
Takashi Iwaif889fa92007-10-31 15:49:32 +01003061 short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003062
3063 memset(cfg, 0, sizeof(*cfg));
3064
Steve Longerbeam81937d32007-05-08 15:33:03 +02003065 memset(sequences_line_out, 0, sizeof(sequences_line_out));
3066 memset(sequences_speaker, 0, sizeof(sequences_speaker));
Takashi Iwaif889fa92007-10-31 15:49:32 +01003067 memset(sequences_hp, 0, sizeof(sequences_hp));
Steve Longerbeam81937d32007-05-08 15:33:03 +02003068 assoc_line_out = assoc_speaker = 0;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003069
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01003070 end_nid = codec->start_nid + codec->num_nodes;
3071 for (nid = codec->start_nid; nid < end_nid; nid++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01003072 unsigned int wid_caps = get_wcaps(codec, nid);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003073 unsigned int wid_type =
3074 (wid_caps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003075 unsigned int def_conf;
3076 short assoc, loc;
3077
3078 /* read all default configuration for pin complex */
3079 if (wid_type != AC_WID_PIN)
3080 continue;
Kailang Yangdf694da2005-12-05 19:42:22 +01003081 /* ignore the given nids (e.g. pc-beep returns error) */
3082 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
3083 continue;
3084
Takashi Iwai0ba21762007-04-16 11:29:14 +02003085 def_conf = snd_hda_codec_read(codec, nid, 0,
3086 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003087 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
3088 continue;
3089 loc = get_defcfg_location(def_conf);
3090 switch (get_defcfg_device(def_conf)) {
3091 case AC_JACK_LINE_OUT:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003092 seq = get_defcfg_sequence(def_conf);
3093 assoc = get_defcfg_association(def_conf);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01003094
3095 if (!(wid_caps & AC_WCAP_STEREO))
3096 if (!cfg->mono_out_pin)
3097 cfg->mono_out_pin = nid;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003098 if (!assoc)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003099 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003100 if (!assoc_line_out)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003101 assoc_line_out = assoc;
3102 else if (assoc_line_out != assoc)
3103 continue;
3104 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
3105 continue;
3106 cfg->line_out_pins[cfg->line_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02003107 sequences_line_out[cfg->line_outs] = seq;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003108 cfg->line_outs++;
3109 break;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01003110 case AC_JACK_SPEAKER:
Steve Longerbeam81937d32007-05-08 15:33:03 +02003111 seq = get_defcfg_sequence(def_conf);
3112 assoc = get_defcfg_association(def_conf);
3113 if (! assoc)
3114 continue;
3115 if (! assoc_speaker)
3116 assoc_speaker = assoc;
3117 else if (assoc_speaker != assoc)
3118 continue;
Takashi Iwai82bc9552006-03-21 11:24:42 +01003119 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
3120 continue;
3121 cfg->speaker_pins[cfg->speaker_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02003122 sequences_speaker[cfg->speaker_outs] = seq;
Takashi Iwai82bc9552006-03-21 11:24:42 +01003123 cfg->speaker_outs++;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01003124 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003125 case AC_JACK_HP_OUT:
Takashi Iwaif889fa92007-10-31 15:49:32 +01003126 seq = get_defcfg_sequence(def_conf);
3127 assoc = get_defcfg_association(def_conf);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02003128 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
3129 continue;
3130 cfg->hp_pins[cfg->hp_outs] = nid;
Takashi Iwaif889fa92007-10-31 15:49:32 +01003131 sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
Takashi Iwaieb06ed82006-09-20 17:10:27 +02003132 cfg->hp_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003133 break;
Takashi Iwai314634b2006-09-21 11:56:18 +02003134 case AC_JACK_MIC_IN: {
3135 int preferred, alt;
3136 if (loc == AC_JACK_LOC_FRONT) {
3137 preferred = AUTO_PIN_FRONT_MIC;
3138 alt = AUTO_PIN_MIC;
3139 } else {
3140 preferred = AUTO_PIN_MIC;
3141 alt = AUTO_PIN_FRONT_MIC;
3142 }
3143 if (!cfg->input_pins[preferred])
3144 cfg->input_pins[preferred] = nid;
3145 else if (!cfg->input_pins[alt])
3146 cfg->input_pins[alt] = nid;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003147 break;
Takashi Iwai314634b2006-09-21 11:56:18 +02003148 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003149 case AC_JACK_LINE_IN:
3150 if (loc == AC_JACK_LOC_FRONT)
3151 cfg->input_pins[AUTO_PIN_FRONT_LINE] = nid;
3152 else
3153 cfg->input_pins[AUTO_PIN_LINE] = nid;
3154 break;
3155 case AC_JACK_CD:
3156 cfg->input_pins[AUTO_PIN_CD] = nid;
3157 break;
3158 case AC_JACK_AUX:
3159 cfg->input_pins[AUTO_PIN_AUX] = nid;
3160 break;
3161 case AC_JACK_SPDIF_OUT:
3162 cfg->dig_out_pin = nid;
3163 break;
3164 case AC_JACK_SPDIF_IN:
3165 cfg->dig_in_pin = nid;
3166 break;
3167 }
3168 }
3169
Takashi Iwai5832fcf2008-02-12 18:30:12 +01003170 /* FIX-UP:
3171 * If no line-out is defined but multiple HPs are found,
3172 * some of them might be the real line-outs.
3173 */
3174 if (!cfg->line_outs && cfg->hp_outs > 1) {
3175 int i = 0;
3176 while (i < cfg->hp_outs) {
3177 /* The real HPs should have the sequence 0x0f */
3178 if ((sequences_hp[i] & 0x0f) == 0x0f) {
3179 i++;
3180 continue;
3181 }
3182 /* Move it to the line-out table */
3183 cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
3184 sequences_line_out[cfg->line_outs] = sequences_hp[i];
3185 cfg->line_outs++;
3186 cfg->hp_outs--;
3187 memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
3188 sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
3189 memmove(sequences_hp + i - 1, sequences_hp + i,
3190 sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
3191 }
3192 }
3193
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003194 /* sort by sequence */
Steve Longerbeam81937d32007-05-08 15:33:03 +02003195 sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
3196 cfg->line_outs);
3197 sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
3198 cfg->speaker_outs);
Takashi Iwaif889fa92007-10-31 15:49:32 +01003199 sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
3200 cfg->hp_outs);
Steve Longerbeam81937d32007-05-08 15:33:03 +02003201
Takashi Iwaif889fa92007-10-31 15:49:32 +01003202 /* if we have only one mic, make it AUTO_PIN_MIC */
3203 if (!cfg->input_pins[AUTO_PIN_MIC] &&
3204 cfg->input_pins[AUTO_PIN_FRONT_MIC]) {
3205 cfg->input_pins[AUTO_PIN_MIC] =
3206 cfg->input_pins[AUTO_PIN_FRONT_MIC];
3207 cfg->input_pins[AUTO_PIN_FRONT_MIC] = 0;
3208 }
3209 /* ditto for line-in */
3210 if (!cfg->input_pins[AUTO_PIN_LINE] &&
3211 cfg->input_pins[AUTO_PIN_FRONT_LINE]) {
3212 cfg->input_pins[AUTO_PIN_LINE] =
3213 cfg->input_pins[AUTO_PIN_FRONT_LINE];
3214 cfg->input_pins[AUTO_PIN_FRONT_LINE] = 0;
3215 }
3216
Steve Longerbeam81937d32007-05-08 15:33:03 +02003217 /*
3218 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
3219 * as a primary output
3220 */
3221 if (!cfg->line_outs) {
3222 if (cfg->speaker_outs) {
3223 cfg->line_outs = cfg->speaker_outs;
3224 memcpy(cfg->line_out_pins, cfg->speaker_pins,
3225 sizeof(cfg->speaker_pins));
3226 cfg->speaker_outs = 0;
3227 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
3228 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
3229 } else if (cfg->hp_outs) {
3230 cfg->line_outs = cfg->hp_outs;
3231 memcpy(cfg->line_out_pins, cfg->hp_pins,
3232 sizeof(cfg->hp_pins));
3233 cfg->hp_outs = 0;
3234 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
3235 cfg->line_out_type = AUTO_PIN_HP_OUT;
3236 }
3237 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003238
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02003239 /* Reorder the surround channels
3240 * ALSA sequence is front/surr/clfe/side
3241 * HDA sequence is:
3242 * 4-ch: front/surr => OK as it is
3243 * 6-ch: front/clfe/surr
Takashi Iwai9422db42007-04-20 16:11:43 +02003244 * 8-ch: front/clfe/rear/side|fc
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02003245 */
3246 switch (cfg->line_outs) {
3247 case 3:
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02003248 case 4:
3249 nid = cfg->line_out_pins[1];
Takashi Iwai9422db42007-04-20 16:11:43 +02003250 cfg->line_out_pins[1] = cfg->line_out_pins[2];
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02003251 cfg->line_out_pins[2] = nid;
3252 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003253 }
3254
Takashi Iwai82bc9552006-03-21 11:24:42 +01003255 /*
3256 * debug prints of the parsed results
3257 */
3258 snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3259 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
3260 cfg->line_out_pins[2], cfg->line_out_pins[3],
3261 cfg->line_out_pins[4]);
3262 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3263 cfg->speaker_outs, cfg->speaker_pins[0],
3264 cfg->speaker_pins[1], cfg->speaker_pins[2],
3265 cfg->speaker_pins[3], cfg->speaker_pins[4]);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02003266 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3267 cfg->hp_outs, cfg->hp_pins[0],
3268 cfg->hp_pins[1], cfg->hp_pins[2],
3269 cfg->hp_pins[3], cfg->hp_pins[4]);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01003270 snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
Takashi Iwai82bc9552006-03-21 11:24:42 +01003271 snd_printd(" inputs: mic=0x%x, fmic=0x%x, line=0x%x, fline=0x%x,"
3272 " cd=0x%x, aux=0x%x\n",
3273 cfg->input_pins[AUTO_PIN_MIC],
3274 cfg->input_pins[AUTO_PIN_FRONT_MIC],
3275 cfg->input_pins[AUTO_PIN_LINE],
3276 cfg->input_pins[AUTO_PIN_FRONT_LINE],
3277 cfg->input_pins[AUTO_PIN_CD],
3278 cfg->input_pins[AUTO_PIN_AUX]);
3279
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003280 return 0;
3281}
3282
Takashi Iwai4a471b72005-12-07 13:56:29 +01003283/* labels for input pins */
3284const char *auto_pin_cfg_labels[AUTO_PIN_LAST] = {
3285 "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux"
3286};
3287
3288
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289#ifdef CONFIG_PM
3290/*
3291 * power management
3292 */
3293
3294/**
3295 * snd_hda_suspend - suspend the codecs
3296 * @bus: the HDA bus
3297 * @state: suspsend state
3298 *
3299 * Returns 0 if successful.
3300 */
3301int snd_hda_suspend(struct hda_bus *bus, pm_message_t state)
3302{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003303 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304
Takashi Iwai0ba21762007-04-16 11:29:14 +02003305 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai0b7a2e92007-08-14 15:18:26 +02003306#ifdef CONFIG_SND_HDA_POWER_SAVE
3307 if (!codec->power_on)
3308 continue;
3309#endif
Takashi Iwaicb53c622007-08-10 17:21:45 +02003310 hda_call_codec_suspend(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 }
3312 return 0;
3313}
3314
3315/**
3316 * snd_hda_resume - resume the codecs
3317 * @bus: the HDA bus
3318 * @state: resume state
3319 *
3320 * Returns 0 if successful.
Takashi Iwaicb53c622007-08-10 17:21:45 +02003321 *
3322 * This fucntion is defined only when POWER_SAVE isn't set.
3323 * In the power-save mode, the codec is resumed dynamically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 */
3325int snd_hda_resume(struct hda_bus *bus)
3326{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003327 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328
Takashi Iwai0ba21762007-04-16 11:29:14 +02003329 list_for_each_entry(codec, &bus->codec_list, list) {
Maxim Levitskyd804ad92007-09-03 15:28:04 +02003330 if (snd_hda_codec_needs_resume(codec))
3331 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333 return 0;
3334}
Maxim Levitskyd804ad92007-09-03 15:28:04 +02003335#ifdef CONFIG_SND_HDA_POWER_SAVE
3336int snd_hda_codecs_inuse(struct hda_bus *bus)
3337{
3338 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339
Maxim Levitskyd804ad92007-09-03 15:28:04 +02003340 list_for_each_entry(codec, &bus->codec_list, list) {
3341 if (snd_hda_codec_needs_resume(codec))
3342 return 1;
3343 }
3344 return 0;
3345}
3346#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347#endif
Takashi Iwaib2e18592008-07-30 15:01:44 +02003348
3349/*
3350 * generic arrays
3351 */
3352
3353/* get a new element from the given array
3354 * if it exceeds the pre-allocated array size, re-allocate the array
3355 */
3356void *snd_array_new(struct snd_array *array)
3357{
3358 if (array->used >= array->alloced) {
3359 int num = array->alloced + array->alloc_align;
3360 void *nlist = kcalloc(num + 1, array->elem_size, GFP_KERNEL);
3361 if (!nlist)
3362 return NULL;
3363 if (array->list) {
3364 memcpy(nlist, array->list,
3365 array->elem_size * array->alloced);
3366 kfree(array->list);
3367 }
3368 array->list = nlist;
3369 array->alloced = num;
3370 }
3371 return array->list + (array->used++ * array->elem_size);
3372}
3373
3374/* free the given array elements */
3375void snd_array_free(struct snd_array *array)
3376{
3377 kfree(array->list);
3378 array->used = 0;
3379 array->alloced = 0;
3380 array->list = NULL;
3381}