blob: a867e1e8658a4769b31abb34f07965cd17f73a75 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070036/*
37 * vendor / preset table
38 */
39
40struct hda_vendor_id {
41 unsigned int id;
42 const char *name;
43};
44
45/* codec vendor labels */
46static struct hda_vendor_id hda_vendor_ids[] = {
Takashi Iwaic8cd1282008-02-13 16:59:29 +010047 { 0x1002, "ATI" },
Takashi Iwaia9226252006-09-17 22:05:54 +020048 { 0x1057, "Motorola" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010049 { 0x1095, "Silicon Image" },
50 { 0x10ec, "Realtek" },
Joseph Chanc577b8a2006-11-29 15:29:40 +010051 { 0x1106, "VIA" },
Matthew Ranostay7f168592007-10-18 17:38:17 +020052 { 0x111d, "IDT" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010053 { 0x11c1, "LSI" },
Takashi Iwai54b903e2005-05-15 14:30:10 +020054 { 0x11d4, "Analog Devices" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 { 0x13f6, "C-Media" },
Takashi Iwaia9226252006-09-17 22:05:54 +020056 { 0x14f1, "Conexant" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010057 { 0x17e8, "Chrontel" },
58 { 0x1854, "LG" },
Mark Brown8199de32008-10-28 14:50:13 +000059 { 0x1aec, "Wolfson Microelectronics" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 { 0x434d, "C-Media" },
Matt2f2f4252005-04-13 14:45:30 +020061 { 0x8384, "SigmaTel" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 {} /* terminator */
63};
64
Harvey Harrison3c9a3202008-02-29 11:59:26 +010065static const struct hda_codec_preset *hda_preset_tables[] = {
66#ifdef CONFIG_SND_HDA_CODEC_REALTEK
67 snd_hda_preset_realtek,
68#endif
69#ifdef CONFIG_SND_HDA_CODEC_CMEDIA
70 snd_hda_preset_cmedia,
71#endif
72#ifdef CONFIG_SND_HDA_CODEC_ANALOG
73 snd_hda_preset_analog,
74#endif
75#ifdef CONFIG_SND_HDA_CODEC_SIGMATEL
76 snd_hda_preset_sigmatel,
77#endif
78#ifdef CONFIG_SND_HDA_CODEC_SI3054
79 snd_hda_preset_si3054,
80#endif
81#ifdef CONFIG_SND_HDA_CODEC_ATIHDMI
82 snd_hda_preset_atihdmi,
83#endif
84#ifdef CONFIG_SND_HDA_CODEC_CONEXANT
85 snd_hda_preset_conexant,
86#endif
87#ifdef CONFIG_SND_HDA_CODEC_VIA
88 snd_hda_preset_via,
89#endif
Wei Ni9a10eb22008-09-26 13:45:46 +080090#ifdef CONFIG_SND_HDA_CODEC_NVHDMI
91 snd_hda_preset_nvhdmi,
92#endif
Wu, Fengguang91504872008-11-05 11:16:56 +080093#ifdef CONFIG_SND_HDA_CODEC_INTELHDMI
94 snd_hda_preset_intelhdmi,
95#endif
Harvey Harrison3c9a3202008-02-29 11:59:26 +010096 NULL
97};
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Takashi Iwaicb53c622007-08-10 17:21:45 +020099#ifdef CONFIG_SND_HDA_POWER_SAVE
100static void hda_power_work(struct work_struct *work);
101static void hda_keep_power_on(struct hda_codec *codec);
102#else
103static inline void hda_keep_power_on(struct hda_codec *codec) {}
104#endif
105
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400106const char *snd_hda_get_jack_location(u32 cfg)
107{
108 static char *bases[7] = {
109 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
110 };
111 static unsigned char specials_idx[] = {
112 0x07, 0x08,
113 0x17, 0x18, 0x19,
114 0x37, 0x38
115 };
116 static char *specials[] = {
117 "Rear Panel", "Drive Bar",
118 "Riser", "HDMI", "ATAPI",
119 "Mobile-In", "Mobile-Out"
120 };
121 int i;
122 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
123 if ((cfg & 0x0f) < 7)
124 return bases[cfg & 0x0f];
125 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
126 if (cfg == specials_idx[i])
127 return specials[i];
128 }
129 return "UNKNOWN";
130}
131
132const char *snd_hda_get_jack_connectivity(u32 cfg)
133{
134 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
135
136 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
137}
138
139const char *snd_hda_get_jack_type(u32 cfg)
140{
141 static char *jack_types[16] = {
142 "Line Out", "Speaker", "HP Out", "CD",
143 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
144 "Line In", "Aux", "Mic", "Telephony",
145 "SPDIF In", "Digitial In", "Reserved", "Other"
146 };
147
148 return jack_types[(cfg & AC_DEFCFG_DEVICE)
149 >> AC_DEFCFG_DEVICE_SHIFT];
150}
151
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100152/*
153 * Compose a 32bit command word to be sent to the HD-audio controller
154 */
155static inline unsigned int
156make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int direct,
157 unsigned int verb, unsigned int parm)
158{
159 u32 val;
160
161 val = (u32)(codec->addr & 0x0f) << 28;
162 val |= (u32)direct << 27;
163 val |= (u32)nid << 20;
164 val |= verb << 8;
165 val |= parm;
166 return val;
167}
168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169/**
170 * snd_hda_codec_read - send a command and get the response
171 * @codec: the HDA codec
172 * @nid: NID to send the command
173 * @direct: direct flag
174 * @verb: the verb to send
175 * @parm: the parameter for the verb
176 *
177 * Send a single command and read the corresponding response.
178 *
179 * Returns the obtained response value, or -1 for an error.
180 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200181unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
182 int direct,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 unsigned int verb, unsigned int parm)
184{
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100185 struct hda_bus *bus = codec->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 unsigned int res;
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100187
188 res = make_codec_cmd(codec, nid, direct, verb, parm);
Takashi Iwaicb53c622007-08-10 17:21:45 +0200189 snd_hda_power_up(codec);
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100190 mutex_lock(&bus->cmd_mutex);
191 if (!bus->ops.command(bus, res))
192 res = bus->ops.get_response(bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 else
194 res = (unsigned int)-1;
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100195 mutex_unlock(&bus->cmd_mutex);
Takashi Iwaicb53c622007-08-10 17:21:45 +0200196 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 return res;
198}
199
200/**
201 * snd_hda_codec_write - send a single command without waiting for response
202 * @codec: the HDA codec
203 * @nid: NID to send the command
204 * @direct: direct flag
205 * @verb: the verb to send
206 * @parm: the parameter for the verb
207 *
208 * Send a single command without waiting for response.
209 *
210 * Returns 0 if successful, or a negative error code.
211 */
212int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
213 unsigned int verb, unsigned int parm)
214{
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100215 struct hda_bus *bus = codec->bus;
216 unsigned int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 int err;
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100218
219 res = make_codec_cmd(codec, nid, direct, verb, parm);
Takashi Iwaicb53c622007-08-10 17:21:45 +0200220 snd_hda_power_up(codec);
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100221 mutex_lock(&bus->cmd_mutex);
222 err = bus->ops.command(bus, res);
223 mutex_unlock(&bus->cmd_mutex);
Takashi Iwaicb53c622007-08-10 17:21:45 +0200224 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 return err;
226}
227
228/**
229 * snd_hda_sequence_write - sequence writes
230 * @codec: the HDA codec
231 * @seq: VERB array to send
232 *
233 * Send the commands sequentially from the given array.
234 * The array must be terminated with NID=0.
235 */
236void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
237{
238 for (; seq->nid; seq++)
239 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
240}
241
242/**
243 * snd_hda_get_sub_nodes - get the range of sub nodes
244 * @codec: the HDA codec
245 * @nid: NID to parse
246 * @start_id: the pointer to store the start NID
247 *
248 * Parse the NID and store the start NID of its sub-nodes.
249 * Returns the number of sub-nodes.
250 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200251int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
252 hda_nid_t *start_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
254 unsigned int parm;
255
256 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
Danny Tholene8a7f132007-09-11 21:41:56 +0200257 if (parm == -1)
258 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 *start_id = (parm >> 16) & 0x7fff;
260 return (int)(parm & 0x7fff);
261}
262
263/**
264 * snd_hda_get_connections - get connection list
265 * @codec: the HDA codec
266 * @nid: NID to parse
267 * @conn_list: connection list array
268 * @max_conns: max. number of connections to store
269 *
270 * Parses the connection list of the given widget and stores the list
271 * of NIDs.
272 *
273 * Returns the number of connections, or a negative error code.
274 */
275int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
276 hda_nid_t *conn_list, int max_conns)
277{
278 unsigned int parm;
Takashi Iwai54d17402005-11-21 16:33:22 +0100279 int i, conn_len, conns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 unsigned int shift, num_elems, mask;
Takashi Iwai54d17402005-11-21 16:33:22 +0100281 hda_nid_t prev_nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Takashi Iwaida3cec32008-08-08 17:12:14 +0200283 if (snd_BUG_ON(!conn_list || max_conns <= 0))
284 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
287 if (parm & AC_CLIST_LONG) {
288 /* long form */
289 shift = 16;
290 num_elems = 2;
291 } else {
292 /* short form */
293 shift = 8;
294 num_elems = 4;
295 }
296 conn_len = parm & AC_CLIST_LENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 mask = (1 << (shift-1)) - 1;
298
Takashi Iwai0ba21762007-04-16 11:29:14 +0200299 if (!conn_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 return 0; /* no connection */
301
302 if (conn_len == 1) {
303 /* single connection */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200304 parm = snd_hda_codec_read(codec, nid, 0,
305 AC_VERB_GET_CONNECT_LIST, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 conn_list[0] = parm & mask;
307 return 1;
308 }
309
310 /* multi connection */
311 conns = 0;
Takashi Iwai54d17402005-11-21 16:33:22 +0100312 prev_nid = 0;
313 for (i = 0; i < conn_len; i++) {
314 int range_val;
315 hda_nid_t val, n;
316
317 if (i % num_elems == 0)
318 parm = snd_hda_codec_read(codec, nid, 0,
319 AC_VERB_GET_CONNECT_LIST, i);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200320 range_val = !!(parm & (1 << (shift-1))); /* ranges */
Takashi Iwai54d17402005-11-21 16:33:22 +0100321 val = parm & mask;
322 parm >>= shift;
323 if (range_val) {
324 /* ranges between the previous and this one */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200325 if (!prev_nid || prev_nid >= val) {
326 snd_printk(KERN_WARNING "hda_codec: "
327 "invalid dep_range_val %x:%x\n",
328 prev_nid, val);
Takashi Iwai54d17402005-11-21 16:33:22 +0100329 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100331 for (n = prev_nid + 1; n <= val; n++) {
332 if (conns >= max_conns) {
Takashi Iwai0ba21762007-04-16 11:29:14 +0200333 snd_printk(KERN_ERR
334 "Too many connections\n");
Takashi Iwai54d17402005-11-21 16:33:22 +0100335 return -EINVAL;
336 }
337 conn_list[conns++] = n;
338 }
339 } else {
340 if (conns >= max_conns) {
341 snd_printk(KERN_ERR "Too many connections\n");
342 return -EINVAL;
343 }
344 conn_list[conns++] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100346 prev_nid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 }
348 return conns;
349}
350
351
352/**
353 * snd_hda_queue_unsol_event - add an unsolicited event to queue
354 * @bus: the BUS
355 * @res: unsolicited event (lower 32bit of RIRB entry)
356 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
357 *
358 * Adds the given event to the queue. The events are processed in
359 * the workqueue asynchronously. Call this function in the interrupt
360 * hanlder when RIRB receives an unsolicited event.
361 *
362 * Returns 0 if successful, or a negative error code.
363 */
364int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
365{
366 struct hda_bus_unsolicited *unsol;
367 unsigned int wp;
368
Takashi Iwai0ba21762007-04-16 11:29:14 +0200369 unsol = bus->unsol;
370 if (!unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 return 0;
372
373 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
374 unsol->wp = wp;
375
376 wp <<= 1;
377 unsol->queue[wp] = res;
378 unsol->queue[wp + 1] = res_ex;
379
Takashi Iwaie250af22006-12-19 17:08:52 +0100380 schedule_work(&unsol->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
382 return 0;
383}
384
385/*
Wu Fengguang5c1d1a92008-10-07 14:17:53 +0800386 * process queued unsolicited events
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 */
David Howellsc4028952006-11-22 14:57:56 +0000388static void process_unsol_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
David Howellsc4028952006-11-22 14:57:56 +0000390 struct hda_bus_unsolicited *unsol =
391 container_of(work, struct hda_bus_unsolicited, work);
392 struct hda_bus *bus = unsol->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 struct hda_codec *codec;
394 unsigned int rp, caddr, res;
395
396 while (unsol->rp != unsol->wp) {
397 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
398 unsol->rp = rp;
399 rp <<= 1;
400 res = unsol->queue[rp];
401 caddr = unsol->queue[rp + 1];
Takashi Iwai0ba21762007-04-16 11:29:14 +0200402 if (!(caddr & (1 << 4))) /* no unsolicited event? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 continue;
404 codec = bus->caddr_tbl[caddr & 0x0f];
405 if (codec && codec->patch_ops.unsol_event)
406 codec->patch_ops.unsol_event(codec, res);
407 }
408}
409
410/*
411 * initialize unsolicited queue
412 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200413static int init_unsol_queue(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
415 struct hda_bus_unsolicited *unsol;
416
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100417 if (bus->unsol) /* already initialized */
418 return 0;
419
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200420 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200421 if (!unsol) {
422 snd_printk(KERN_ERR "hda_codec: "
423 "can't allocate unsolicited queue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 return -ENOMEM;
425 }
David Howellsc4028952006-11-22 14:57:56 +0000426 INIT_WORK(&unsol->work, process_unsol_events);
427 unsol->bus = bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 bus->unsol = unsol;
429 return 0;
430}
431
432/*
433 * destructor
434 */
435static void snd_hda_codec_free(struct hda_codec *codec);
436
437static int snd_hda_bus_free(struct hda_bus *bus)
438{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200439 struct hda_codec *codec, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Takashi Iwai0ba21762007-04-16 11:29:14 +0200441 if (!bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 return 0;
443 if (bus->unsol) {
Takashi Iwaie250af22006-12-19 17:08:52 +0100444 flush_scheduled_work();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 kfree(bus->unsol);
446 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200447 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 snd_hda_codec_free(codec);
449 }
450 if (bus->ops.private_free)
451 bus->ops.private_free(bus);
452 kfree(bus);
453 return 0;
454}
455
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100456static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
458 struct hda_bus *bus = device->device_data;
Takashi Iwaib94d35392008-11-21 09:08:06 +0100459 bus->shutdown = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 return snd_hda_bus_free(bus);
461}
462
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200463#ifdef CONFIG_SND_HDA_HWDEP
464static int snd_hda_bus_dev_register(struct snd_device *device)
465{
466 struct hda_bus *bus = device->device_data;
467 struct hda_codec *codec;
468 list_for_each_entry(codec, &bus->codec_list, list) {
469 snd_hda_hwdep_add_sysfs(codec);
470 }
471 return 0;
472}
473#else
474#define snd_hda_bus_dev_register NULL
475#endif
476
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477/**
478 * snd_hda_bus_new - create a HDA bus
479 * @card: the card entry
480 * @temp: the template for hda_bus information
481 * @busp: the pointer to store the created bus instance
482 *
483 * Returns 0 if successful, or a negative error code.
484 */
Takashi Iwai756e2b02007-04-16 11:27:07 +0200485int __devinit snd_hda_bus_new(struct snd_card *card,
486 const struct hda_bus_template *temp,
487 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
489 struct hda_bus *bus;
490 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100491 static struct snd_device_ops dev_ops = {
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200492 .dev_register = snd_hda_bus_dev_register,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 .dev_free = snd_hda_bus_dev_free,
494 };
495
Takashi Iwaida3cec32008-08-08 17:12:14 +0200496 if (snd_BUG_ON(!temp))
497 return -EINVAL;
498 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
499 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501 if (busp)
502 *busp = NULL;
503
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200504 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 if (bus == NULL) {
506 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
507 return -ENOMEM;
508 }
509
510 bus->card = card;
511 bus->private_data = temp->private_data;
512 bus->pci = temp->pci;
513 bus->modelname = temp->modelname;
Takashi Iwaifee2fba2008-11-27 12:43:28 +0100514 bus->power_save = temp->power_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 bus->ops = temp->ops;
516
Ingo Molnar62932df2006-01-16 16:34:20 +0100517 mutex_init(&bus->cmd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 INIT_LIST_HEAD(&bus->codec_list);
519
Takashi Iwai0ba21762007-04-16 11:29:14 +0200520 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
521 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 snd_hda_bus_free(bus);
523 return err;
524 }
525 if (busp)
526 *busp = bus;
527 return 0;
528}
529
Takashi Iwai82467612007-07-27 19:15:54 +0200530#ifdef CONFIG_SND_HDA_GENERIC
531#define is_generic_config(codec) \
Takashi Iwaif44ac832008-07-30 15:01:45 +0200532 (codec->modelname && !strcmp(codec->modelname, "generic"))
Takashi Iwai82467612007-07-27 19:15:54 +0200533#else
534#define is_generic_config(codec) 0
535#endif
536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537/*
538 * find a matching codec preset
539 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200540static const struct hda_codec_preset *
Takashi Iwai756e2b02007-04-16 11:27:07 +0200541find_codec_preset(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
543 const struct hda_codec_preset **tbl, *preset;
544
Takashi Iwai82467612007-07-27 19:15:54 +0200545 if (is_generic_config(codec))
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100546 return NULL; /* use the generic parser */
547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 for (tbl = hda_preset_tables; *tbl; tbl++) {
549 for (preset = *tbl; preset->id; preset++) {
550 u32 mask = preset->mask;
Marc Boucherca7cfae2008-01-22 15:32:25 +0100551 if (preset->afg && preset->afg != codec->afg)
552 continue;
553 if (preset->mfg && preset->mfg != codec->mfg)
554 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200555 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 mask = ~0;
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200557 if (preset->id == (codec->vendor_id & mask) &&
Takashi Iwai0ba21762007-04-16 11:29:14 +0200558 (!preset->rev ||
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200559 preset->rev == codec->revision_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 return preset;
561 }
562 }
563 return NULL;
564}
565
566/*
Takashi Iwaif44ac832008-07-30 15:01:45 +0200567 * get_codec_name - store the codec name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 */
Takashi Iwaif44ac832008-07-30 15:01:45 +0200569static int get_codec_name(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
571 const struct hda_vendor_id *c;
572 const char *vendor = NULL;
573 u16 vendor_id = codec->vendor_id >> 16;
Takashi Iwaif44ac832008-07-30 15:01:45 +0200574 char tmp[16], name[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
576 for (c = hda_vendor_ids; c->id; c++) {
577 if (c->id == vendor_id) {
578 vendor = c->name;
579 break;
580 }
581 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200582 if (!vendor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 sprintf(tmp, "Generic %04x", vendor_id);
584 vendor = tmp;
585 }
586 if (codec->preset && codec->preset->name)
Takashi Iwaif44ac832008-07-30 15:01:45 +0200587 snprintf(name, sizeof(name), "%s %s", vendor,
588 codec->preset->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 else
Takashi Iwaif44ac832008-07-30 15:01:45 +0200590 snprintf(name, sizeof(name), "%s ID %x", vendor,
Takashi Iwai0ba21762007-04-16 11:29:14 +0200591 codec->vendor_id & 0xffff);
Takashi Iwaif44ac832008-07-30 15:01:45 +0200592 codec->name = kstrdup(name, GFP_KERNEL);
593 if (!codec->name)
594 return -ENOMEM;
595 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596}
597
598/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200599 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 */
Takashi Iwai756e2b02007-04-16 11:27:07 +0200601static void __devinit setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
603 int i, total_nodes;
604 hda_nid_t nid;
605
606 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
607 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +0200608 unsigned int func;
609 func = snd_hda_param_read(codec, nid, AC_PAR_FUNCTION_TYPE);
610 switch (func & 0xff) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200611 case AC_GRP_AUDIO_FUNCTION:
612 codec->afg = nid;
613 break;
614 case AC_GRP_MODEM_FUNCTION:
615 codec->mfg = nid;
616 break;
617 default:
618 break;
619 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621}
622
623/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100624 * read widget caps for each widget and store in cache
625 */
626static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
627{
628 int i;
629 hda_nid_t nid;
630
631 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
632 &codec->start_nid);
633 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200634 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +0100635 return -ENOMEM;
636 nid = codec->start_nid;
637 for (i = 0; i < codec->num_nodes; i++, nid++)
638 codec->wcaps[i] = snd_hda_param_read(codec, nid,
639 AC_PAR_AUDIO_WIDGET_CAP);
640 return 0;
641}
642
643
Takashi Iwai01751f52007-08-10 16:59:39 +0200644static void init_hda_cache(struct hda_cache_rec *cache,
645 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +0200646static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +0200647
Takashi Iwai54d17402005-11-21 16:33:22 +0100648/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 * codec destructor
650 */
651static void snd_hda_codec_free(struct hda_codec *codec)
652{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200653 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 return;
Takashi Iwaicb53c622007-08-10 17:21:45 +0200655#ifdef CONFIG_SND_HDA_POWER_SAVE
656 cancel_delayed_work(&codec->power_work);
Takashi Iwai2525fdc2007-08-15 22:18:22 +0200657 flush_scheduled_work();
Takashi Iwaicb53c622007-08-10 17:21:45 +0200658#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +0200660 snd_array_free(&codec->mixers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 codec->bus->caddr_tbl[codec->addr] = NULL;
662 if (codec->patch_ops.free)
663 codec->patch_ops.free(codec);
Takashi Iwai01751f52007-08-10 16:59:39 +0200664 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +0200665 free_hda_cache(&codec->cmd_cache);
Takashi Iwaif44ac832008-07-30 15:01:45 +0200666 kfree(codec->name);
667 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +0100668 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 kfree(codec);
670}
671
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672/**
673 * snd_hda_codec_new - create a HDA codec
674 * @bus: the bus to assign
675 * @codec_addr: the codec address
676 * @codecp: the pointer to store the generated codec
677 *
678 * Returns 0 if successful, or a negative error code.
679 */
Takashi Iwai756e2b02007-04-16 11:27:07 +0200680int __devinit snd_hda_codec_new(struct hda_bus *bus, unsigned int codec_addr,
681 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
683 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +0200684 char component[31];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 int err;
686
Takashi Iwaida3cec32008-08-08 17:12:14 +0200687 if (snd_BUG_ON(!bus))
688 return -EINVAL;
689 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
690 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
692 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +0200693 snd_printk(KERN_ERR "hda_codec: "
694 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 return -EBUSY;
696 }
697
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200698 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 if (codec == NULL) {
700 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
701 return -ENOMEM;
702 }
703
704 codec->bus = bus;
705 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +0100706 mutex_init(&codec->spdif_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +0200707 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +0200708 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwaid13bd412008-07-30 15:01:45 +0200709 snd_array_init(&codec->mixers, sizeof(struct snd_kcontrol *), 32);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200710 if (codec->bus->modelname) {
711 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
712 if (!codec->modelname) {
713 snd_hda_codec_free(codec);
714 return -ENODEV;
715 }
716 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Takashi Iwaicb53c622007-08-10 17:21:45 +0200718#ifdef CONFIG_SND_HDA_POWER_SAVE
719 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
720 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
721 * the caller has to power down appropriatley after initialization
722 * phase.
723 */
724 hda_keep_power_on(codec);
725#endif
726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 list_add_tail(&codec->list, &bus->codec_list);
728 bus->caddr_tbl[codec_addr] = codec;
729
Takashi Iwai0ba21762007-04-16 11:29:14 +0200730 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
731 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +0100732 if (codec->vendor_id == -1)
733 /* read again, hopefully the access method was corrected
734 * in the last read...
735 */
736 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
737 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200738 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
739 AC_PAR_SUBSYSTEM_ID);
740 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
741 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200743 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200744 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200745 snd_printdd("hda_codec: no AFG or MFG node found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 snd_hda_codec_free(codec);
747 return -ENODEV;
748 }
749
Takashi Iwai54d17402005-11-21 16:33:22 +0100750 if (read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg) < 0) {
751 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
752 snd_hda_codec_free(codec);
753 return -ENOMEM;
754 }
755
Takashi Iwai0ba21762007-04-16 11:29:14 +0200756 if (!codec->subsystem_id) {
Takashi Iwai86284e42005-10-11 15:05:54 +0200757 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200758 codec->subsystem_id =
759 snd_hda_codec_read(codec, nid, 0,
760 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +0200761 }
Takashi Iwaif44ac832008-07-30 15:01:45 +0200762 if (bus->modelname)
763 codec->modelname = kstrdup(bus->modelname, GFP_KERNEL);
Takashi Iwai86284e42005-10-11 15:05:54 +0200764
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200765 err = snd_hda_codec_configure(codec);
766 if (err < 0) {
767 snd_hda_codec_free(codec);
768 return err;
769 }
770 snd_hda_codec_proc_new(codec);
771
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200772 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200773
774 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
775 codec->subsystem_id, codec->revision_id);
776 snd_component_add(codec->bus->card, component);
777
778 if (codecp)
779 *codecp = codec;
780 return 0;
781}
782
783int snd_hda_codec_configure(struct hda_codec *codec)
784{
785 int err;
786
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100787 codec->preset = find_codec_preset(codec);
Takashi Iwaif44ac832008-07-30 15:01:45 +0200788 if (!codec->name) {
789 err = get_codec_name(codec);
790 if (err < 0)
791 return err;
792 }
Takashi Iwai43ea1d42007-04-26 19:12:08 +0200793 /* audio codec should override the mixer name */
Takashi Iwaif44ac832008-07-30 15:01:45 +0200794 if (codec->afg || !*codec->bus->card->mixername)
795 strlcpy(codec->bus->card->mixername, codec->name,
796 sizeof(codec->bus->card->mixername));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Takashi Iwai82467612007-07-27 19:15:54 +0200798 if (is_generic_config(codec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai82467612007-07-27 19:15:54 +0200800 goto patched;
801 }
Takashi Iwai82467612007-07-27 19:15:54 +0200802 if (codec->preset && codec->preset->patch) {
803 err = codec->preset->patch(codec);
804 goto patched;
805 }
806
807 /* call the default parser */
Takashi Iwai82467612007-07-27 19:15:54 +0200808 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai35a1e0c2007-10-19 08:13:40 +0200809 if (err < 0)
810 printk(KERN_ERR "hda-codec: No codec parser is available\n");
Takashi Iwai82467612007-07-27 19:15:54 +0200811
812 patched:
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200813 if (!err && codec->patch_ops.unsol_event)
814 err = init_unsol_queue(codec->bus);
815 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816}
817
818/**
819 * snd_hda_codec_setup_stream - set up the codec for streaming
820 * @codec: the CODEC to set up
821 * @nid: the NID to set up
822 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
823 * @channel_id: channel id to pass, zero based.
824 * @format: stream format.
825 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200826void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
827 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 int channel_id, int format)
829{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200830 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +0200831 return;
832
Takashi Iwai0ba21762007-04-16 11:29:14 +0200833 snd_printdd("hda_codec_setup_stream: "
834 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 nid, stream_tag, channel_id, format);
836 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID,
837 (stream_tag << 4) | channel_id);
838 msleep(1);
839 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, format);
840}
841
Takashi Iwai888afa12008-03-18 09:57:50 +0100842void snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid)
843{
844 if (!nid)
845 return;
846
847 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
848 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
849#if 0 /* keep the format */
850 msleep(1);
851 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0);
852#endif
853}
854
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855/*
856 * amp access functions
857 */
858
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200859/* FIXME: more better hash key? */
860#define HDA_HASH_KEY(nid,dir,idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200862#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
864/* initialize the hash table */
Takashi Iwai01751f52007-08-10 16:59:39 +0200865static void __devinit init_hda_cache(struct hda_cache_rec *cache,
866 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867{
Takashi Iwai01751f52007-08-10 16:59:39 +0200868 memset(cache, 0, sizeof(*cache));
869 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +0200870 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +0200871}
872
Takashi Iwai1fcaee62007-08-23 00:01:09 +0200873static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +0200874{
Takashi Iwai603c4012008-07-30 15:01:44 +0200875 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876}
877
878/* query the hash. allocate an entry if not found. */
Takashi Iwai01751f52007-08-10 16:59:39 +0200879static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
880 u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
Takashi Iwai01751f52007-08-10 16:59:39 +0200882 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
883 u16 cur = cache->hash[idx];
884 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
886 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +0100887 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 if (info->key == key)
889 return info;
890 cur = info->next;
891 }
892
893 /* add a new hash entry */
Takashi Iwai603c4012008-07-30 15:01:44 +0200894 info = snd_array_new(&cache->buf);
Takashi Iwaic2174292008-11-07 00:23:30 +0100895 if (!info)
896 return NULL;
Takashi Iwaif43aa022008-11-10 16:24:26 +0100897 cur = snd_array_index(&cache->buf, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 info->key = key;
Takashi Iwai01751f52007-08-10 16:59:39 +0200899 info->val = 0;
900 info->next = cache->hash[idx];
901 cache->hash[idx] = cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
903 return info;
904}
905
Takashi Iwai01751f52007-08-10 16:59:39 +0200906/* query and allocate an amp hash entry */
907static inline struct hda_amp_info *
908get_alloc_amp_hash(struct hda_codec *codec, u32 key)
909{
910 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
911}
912
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913/*
914 * query AMP capabilities for the given widget and direction
915 */
Matthew Ranostay09a99952008-01-24 11:49:21 +0100916u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200918 struct hda_amp_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
Takashi Iwai0ba21762007-04-16 11:29:14 +0200920 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
921 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 return 0;
Takashi Iwai01751f52007-08-10 16:59:39 +0200923 if (!(info->head.val & INFO_AMP_CAPS)) {
Takashi Iwai0ba21762007-04-16 11:29:14 +0200924 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 nid = codec->afg;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200926 info->amp_caps = snd_hda_param_read(codec, nid,
927 direction == HDA_OUTPUT ?
928 AC_PAR_AMP_OUT_CAP :
929 AC_PAR_AMP_IN_CAP);
Takashi Iwaib75e53f2007-05-10 16:56:09 +0200930 if (info->amp_caps)
Takashi Iwai01751f52007-08-10 16:59:39 +0200931 info->head.val |= INFO_AMP_CAPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 }
933 return info->amp_caps;
934}
935
Takashi Iwai897cc182007-05-29 19:01:37 +0200936int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
937 unsigned int caps)
938{
939 struct hda_amp_info *info;
940
941 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
942 if (!info)
943 return -EINVAL;
944 info->amp_caps = caps;
Takashi Iwai01751f52007-08-10 16:59:39 +0200945 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai897cc182007-05-29 19:01:37 +0200946 return 0;
947}
948
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949/*
950 * read the current volume to info
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200951 * if the cache exists, read the cache value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200953static unsigned int get_vol_mute(struct hda_codec *codec,
954 struct hda_amp_info *info, hda_nid_t nid,
955 int ch, int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956{
957 u32 val, parm;
958
Takashi Iwai01751f52007-08-10 16:59:39 +0200959 if (info->head.val & INFO_AMP_VOL(ch))
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200960 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
962 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
963 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
964 parm |= index;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200965 val = snd_hda_codec_read(codec, nid, 0,
966 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 info->vol[ch] = val & 0xff;
Takashi Iwai01751f52007-08-10 16:59:39 +0200968 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200969 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970}
971
972/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200973 * write the current volume in info to the h/w and update the cache
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200975static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
Takashi Iwai0ba21762007-04-16 11:29:14 +0200976 hda_nid_t nid, int ch, int direction, int index,
977 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978{
979 u32 parm;
980
981 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
982 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
983 parm |= index << AC_AMP_SET_INDEX_SHIFT;
984 parm |= val;
985 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200986 info->vol[ch] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987}
988
989/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200990 * read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 */
Takashi Iwai834be882006-03-01 14:16:17 +0100992int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
993 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200995 struct hda_amp_info *info;
996 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
997 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200999 return get_vol_mute(codec, info, nid, ch, direction, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000}
1001
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001002/*
1003 * update the AMP value, mask = bit mask to set, val = the value
1004 */
Takashi Iwai834be882006-03-01 14:16:17 +01001005int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1006 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001008 struct hda_amp_info *info;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001009
Takashi Iwai0ba21762007-04-16 11:29:14 +02001010 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
1011 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001013 val &= mask;
1014 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001015 if (info->vol[ch] == val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001017 put_vol_mute(codec, info, nid, ch, direction, idx, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 return 1;
1019}
1020
Takashi Iwai47fd8302007-08-10 17:11:07 +02001021/*
1022 * update the AMP stereo with the same mask and value
1023 */
1024int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1025 int direction, int idx, int mask, int val)
1026{
1027 int ch, ret = 0;
1028 for (ch = 0; ch < 2; ch++)
1029 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1030 idx, mask, val);
1031 return ret;
1032}
1033
Takashi Iwaicb53c622007-08-10 17:21:45 +02001034#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001035/* resume the all amp commands from the cache */
1036void snd_hda_codec_resume_amp(struct hda_codec *codec)
1037{
Takashi Iwai603c4012008-07-30 15:01:44 +02001038 struct hda_amp_info *buffer = codec->amp_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001039 int i;
1040
Takashi Iwai603c4012008-07-30 15:01:44 +02001041 for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001042 u32 key = buffer->head.key;
1043 hda_nid_t nid;
1044 unsigned int idx, dir, ch;
1045 if (!key)
1046 continue;
1047 nid = key & 0xff;
1048 idx = (key >> 16) & 0xff;
1049 dir = (key >> 24) & 0xff;
1050 for (ch = 0; ch < 2; ch++) {
1051 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
1052 continue;
1053 put_vol_mute(codec, buffer, nid, ch, dir, idx,
1054 buffer->vol[ch]);
1055 }
1056 }
1057}
Takashi Iwaicb53c622007-08-10 17:21:45 +02001058#endif /* SND_HDA_NEEDS_RESUME */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060/* volume */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001061int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1062 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063{
1064 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1065 u16 nid = get_amp_nid(kcontrol);
1066 u8 chs = get_amp_channels(kcontrol);
1067 int dir = get_amp_direction(kcontrol);
1068 u32 caps;
1069
1070 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001071 /* num steps */
1072 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1073 if (!caps) {
1074 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01001075 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
1076 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 return -EINVAL;
1078 }
1079 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1080 uinfo->count = chs == 3 ? 2 : 1;
1081 uinfo->value.integer.min = 0;
1082 uinfo->value.integer.max = caps;
1083 return 0;
1084}
1085
Takashi Iwai0ba21762007-04-16 11:29:14 +02001086int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1087 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088{
1089 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1090 hda_nid_t nid = get_amp_nid(kcontrol);
1091 int chs = get_amp_channels(kcontrol);
1092 int dir = get_amp_direction(kcontrol);
1093 int idx = get_amp_index(kcontrol);
1094 long *valp = ucontrol->value.integer.value;
1095
1096 if (chs & 1)
Takashi Iwai47fd8302007-08-10 17:11:07 +02001097 *valp++ = snd_hda_codec_amp_read(codec, nid, 0, dir, idx)
1098 & HDA_AMP_VOLMASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 if (chs & 2)
Takashi Iwai47fd8302007-08-10 17:11:07 +02001100 *valp = snd_hda_codec_amp_read(codec, nid, 1, dir, idx)
1101 & HDA_AMP_VOLMASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 return 0;
1103}
1104
Takashi Iwai0ba21762007-04-16 11:29:14 +02001105int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1106 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107{
1108 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1109 hda_nid_t nid = get_amp_nid(kcontrol);
1110 int chs = get_amp_channels(kcontrol);
1111 int dir = get_amp_direction(kcontrol);
1112 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 long *valp = ucontrol->value.integer.value;
1114 int change = 0;
1115
Takashi Iwaicb53c622007-08-10 17:21:45 +02001116 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001117 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001118 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
1119 0x7f, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001120 valp++;
1121 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001122 if (chs & 2)
1123 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001124 0x7f, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001125 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 return change;
1127}
1128
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001129int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1130 unsigned int size, unsigned int __user *_tlv)
1131{
1132 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1133 hda_nid_t nid = get_amp_nid(kcontrol);
1134 int dir = get_amp_direction(kcontrol);
1135 u32 caps, val1, val2;
1136
1137 if (size < 4 * sizeof(unsigned int))
1138 return -ENOMEM;
1139 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001140 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1141 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001142 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
1143 val1 = ((int)val1) * ((int)val2);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001144 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
1145 return -EFAULT;
1146 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
1147 return -EFAULT;
1148 if (put_user(val1, _tlv + 2))
1149 return -EFAULT;
1150 if (put_user(val2, _tlv + 3))
1151 return -EFAULT;
1152 return 0;
1153}
1154
Takashi Iwai2134ea42008-01-10 16:53:55 +01001155/*
1156 * set (static) TLV for virtual master volume; recalculated as max 0dB
1157 */
1158void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
1159 unsigned int *tlv)
1160{
1161 u32 caps;
1162 int nums, step;
1163
1164 caps = query_amp_caps(codec, nid, dir);
1165 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1166 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1167 step = (step + 1) * 25;
1168 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
1169 tlv[1] = 2 * sizeof(unsigned int);
1170 tlv[2] = -nums * step;
1171 tlv[3] = step;
1172}
1173
1174/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01001175static struct snd_kcontrol *
1176_snd_hda_find_mixer_ctl(struct hda_codec *codec,
1177 const char *name, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01001178{
1179 struct snd_ctl_elem_id id;
1180 memset(&id, 0, sizeof(id));
1181 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwai09f99702008-02-04 12:31:13 +01001182 id.index = idx;
Takashi Iwai2134ea42008-01-10 16:53:55 +01001183 strcpy(id.name, name);
1184 return snd_ctl_find_id(codec->bus->card, &id);
1185}
1186
Takashi Iwai09f99702008-02-04 12:31:13 +01001187struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
1188 const char *name)
1189{
1190 return _snd_hda_find_mixer_ctl(codec, name, 0);
1191}
1192
Takashi Iwaid13bd412008-07-30 15:01:45 +02001193/* Add a control element and assign to the codec */
1194int snd_hda_ctl_add(struct hda_codec *codec, struct snd_kcontrol *kctl)
1195{
1196 int err;
1197 struct snd_kcontrol **knewp;
1198
1199 err = snd_ctl_add(codec->bus->card, kctl);
1200 if (err < 0)
1201 return err;
1202 knewp = snd_array_new(&codec->mixers);
1203 if (!knewp)
1204 return -ENOMEM;
1205 *knewp = kctl;
1206 return 0;
1207}
1208
Takashi Iwai529bd6c2008-11-27 14:17:01 +01001209#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwaid13bd412008-07-30 15:01:45 +02001210/* Clear all controls assigned to the given codec */
1211void snd_hda_ctls_clear(struct hda_codec *codec)
1212{
1213 int i;
1214 struct snd_kcontrol **kctls = codec->mixers.list;
1215 for (i = 0; i < codec->mixers.used; i++)
1216 snd_ctl_remove(codec->bus->card, kctls[i]);
1217 snd_array_free(&codec->mixers);
1218}
1219
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001220void snd_hda_codec_reset(struct hda_codec *codec)
1221{
1222 int i;
1223
1224#ifdef CONFIG_SND_HDA_POWER_SAVE
1225 cancel_delayed_work(&codec->power_work);
1226 flush_scheduled_work();
1227#endif
1228 snd_hda_ctls_clear(codec);
1229 /* relase PCMs */
1230 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01001231 if (codec->pcm_info[i].pcm) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001232 snd_device_free(codec->bus->card,
1233 codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01001234 clear_bit(codec->pcm_info[i].device,
1235 codec->bus->pcm_dev_bits);
1236 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001237 }
1238 if (codec->patch_ops.free)
1239 codec->patch_ops.free(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01001240 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001241 codec->spec = NULL;
1242 free_hda_cache(&codec->amp_cache);
1243 free_hda_cache(&codec->cmd_cache);
1244 codec->num_pcms = 0;
1245 codec->pcm_info = NULL;
1246 codec->preset = NULL;
1247}
Takashi Iwai529bd6c2008-11-27 14:17:01 +01001248#endif /* CONFIG_SND_HDA_RECONFIG */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001249
Takashi Iwai2134ea42008-01-10 16:53:55 +01001250/* create a virtual master control and add slaves */
1251int snd_hda_add_vmaster(struct hda_codec *codec, char *name,
1252 unsigned int *tlv, const char **slaves)
1253{
1254 struct snd_kcontrol *kctl;
1255 const char **s;
1256 int err;
1257
Takashi Iwai2f085542008-02-22 18:43:50 +01001258 for (s = slaves; *s && !snd_hda_find_mixer_ctl(codec, *s); s++)
1259 ;
1260 if (!*s) {
1261 snd_printdd("No slave found for %s\n", name);
1262 return 0;
1263 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01001264 kctl = snd_ctl_make_virtual_master(name, tlv);
1265 if (!kctl)
1266 return -ENOMEM;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001267 err = snd_hda_ctl_add(codec, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001268 if (err < 0)
1269 return err;
1270
1271 for (s = slaves; *s; s++) {
1272 struct snd_kcontrol *sctl;
1273
1274 sctl = snd_hda_find_mixer_ctl(codec, *s);
1275 if (!sctl) {
1276 snd_printdd("Cannot find slave %s, skipped\n", *s);
1277 continue;
1278 }
1279 err = snd_ctl_add_slave(kctl, sctl);
1280 if (err < 0)
1281 return err;
1282 }
1283 return 0;
1284}
1285
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286/* switch */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001287int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
1288 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289{
1290 int chs = get_amp_channels(kcontrol);
1291
1292 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1293 uinfo->count = chs == 3 ? 2 : 1;
1294 uinfo->value.integer.min = 0;
1295 uinfo->value.integer.max = 1;
1296 return 0;
1297}
1298
Takashi Iwai0ba21762007-04-16 11:29:14 +02001299int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
1300 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301{
1302 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1303 hda_nid_t nid = get_amp_nid(kcontrol);
1304 int chs = get_amp_channels(kcontrol);
1305 int dir = get_amp_direction(kcontrol);
1306 int idx = get_amp_index(kcontrol);
1307 long *valp = ucontrol->value.integer.value;
1308
1309 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02001310 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02001311 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02001313 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02001314 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 return 0;
1316}
1317
Takashi Iwai0ba21762007-04-16 11:29:14 +02001318int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
1319 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320{
1321 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1322 hda_nid_t nid = get_amp_nid(kcontrol);
1323 int chs = get_amp_channels(kcontrol);
1324 int dir = get_amp_direction(kcontrol);
1325 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 long *valp = ucontrol->value.integer.value;
1327 int change = 0;
1328
Takashi Iwaicb53c622007-08-10 17:21:45 +02001329 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001330 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001331 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02001332 HDA_AMP_MUTE,
1333 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001334 valp++;
1335 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001336 if (chs & 2)
1337 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02001338 HDA_AMP_MUTE,
1339 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001340#ifdef CONFIG_SND_HDA_POWER_SAVE
1341 if (codec->patch_ops.check_power_status)
1342 codec->patch_ops.check_power_status(codec, nid);
1343#endif
1344 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 return change;
1346}
1347
1348/*
Takashi Iwai985be542005-11-02 18:26:49 +01001349 * bound volume controls
1350 *
1351 * bind multiple volumes (# indices, from 0)
1352 */
1353
1354#define AMP_VAL_IDX_SHIFT 19
1355#define AMP_VAL_IDX_MASK (0x0f<<19)
1356
Takashi Iwai0ba21762007-04-16 11:29:14 +02001357int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
1358 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01001359{
1360 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1361 unsigned long pval;
1362 int err;
1363
Ingo Molnar62932df2006-01-16 16:34:20 +01001364 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Takashi Iwai985be542005-11-02 18:26:49 +01001365 pval = kcontrol->private_value;
1366 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
1367 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
1368 kcontrol->private_value = pval;
Ingo Molnar62932df2006-01-16 16:34:20 +01001369 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01001370 return err;
1371}
1372
Takashi Iwai0ba21762007-04-16 11:29:14 +02001373int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
1374 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01001375{
1376 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1377 unsigned long pval;
1378 int i, indices, err = 0, change = 0;
1379
Ingo Molnar62932df2006-01-16 16:34:20 +01001380 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Takashi Iwai985be542005-11-02 18:26:49 +01001381 pval = kcontrol->private_value;
1382 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
1383 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001384 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
1385 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01001386 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1387 if (err < 0)
1388 break;
1389 change |= err;
1390 }
1391 kcontrol->private_value = pval;
Ingo Molnar62932df2006-01-16 16:34:20 +01001392 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01001393 return err < 0 ? err : change;
1394}
1395
1396/*
Takashi Iwai532d5382007-07-27 19:02:40 +02001397 * generic bound volume/swtich controls
1398 */
1399int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
1400 struct snd_ctl_elem_info *uinfo)
1401{
1402 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1403 struct hda_bind_ctls *c;
1404 int err;
1405
Takashi Iwai532d5382007-07-27 19:02:40 +02001406 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01001407 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02001408 kcontrol->private_value = *c->values;
1409 err = c->ops->info(kcontrol, uinfo);
1410 kcontrol->private_value = (long)c;
1411 mutex_unlock(&codec->spdif_mutex);
1412 return err;
1413}
1414
1415int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
1416 struct snd_ctl_elem_value *ucontrol)
1417{
1418 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1419 struct hda_bind_ctls *c;
1420 int err;
1421
Takashi Iwai532d5382007-07-27 19:02:40 +02001422 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01001423 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02001424 kcontrol->private_value = *c->values;
1425 err = c->ops->get(kcontrol, ucontrol);
1426 kcontrol->private_value = (long)c;
1427 mutex_unlock(&codec->spdif_mutex);
1428 return err;
1429}
1430
1431int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
1432 struct snd_ctl_elem_value *ucontrol)
1433{
1434 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1435 struct hda_bind_ctls *c;
1436 unsigned long *vals;
1437 int err = 0, change = 0;
1438
Takashi Iwai532d5382007-07-27 19:02:40 +02001439 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01001440 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02001441 for (vals = c->values; *vals; vals++) {
1442 kcontrol->private_value = *vals;
1443 err = c->ops->put(kcontrol, ucontrol);
1444 if (err < 0)
1445 break;
1446 change |= err;
1447 }
1448 kcontrol->private_value = (long)c;
1449 mutex_unlock(&codec->spdif_mutex);
1450 return err < 0 ? err : change;
1451}
1452
1453int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1454 unsigned int size, unsigned int __user *tlv)
1455{
1456 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1457 struct hda_bind_ctls *c;
1458 int err;
1459
Takashi Iwai532d5382007-07-27 19:02:40 +02001460 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01001461 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02001462 kcontrol->private_value = *c->values;
1463 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
1464 kcontrol->private_value = (long)c;
1465 mutex_unlock(&codec->spdif_mutex);
1466 return err;
1467}
1468
1469struct hda_ctl_ops snd_hda_bind_vol = {
1470 .info = snd_hda_mixer_amp_volume_info,
1471 .get = snd_hda_mixer_amp_volume_get,
1472 .put = snd_hda_mixer_amp_volume_put,
1473 .tlv = snd_hda_mixer_amp_tlv
1474};
1475
1476struct hda_ctl_ops snd_hda_bind_sw = {
1477 .info = snd_hda_mixer_amp_switch_info,
1478 .get = snd_hda_mixer_amp_switch_get,
1479 .put = snd_hda_mixer_amp_switch_put,
1480 .tlv = snd_hda_mixer_amp_tlv
1481};
1482
1483/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 * SPDIF out controls
1485 */
1486
Takashi Iwai0ba21762007-04-16 11:29:14 +02001487static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
1488 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489{
1490 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1491 uinfo->count = 1;
1492 return 0;
1493}
1494
Takashi Iwai0ba21762007-04-16 11:29:14 +02001495static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
1496 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497{
1498 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
1499 IEC958_AES0_NONAUDIO |
1500 IEC958_AES0_CON_EMPHASIS_5015 |
1501 IEC958_AES0_CON_NOT_COPYRIGHT;
1502 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
1503 IEC958_AES1_CON_ORIGINAL;
1504 return 0;
1505}
1506
Takashi Iwai0ba21762007-04-16 11:29:14 +02001507static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
1508 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509{
1510 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
1511 IEC958_AES0_NONAUDIO |
1512 IEC958_AES0_PRO_EMPHASIS_5015;
1513 return 0;
1514}
1515
Takashi Iwai0ba21762007-04-16 11:29:14 +02001516static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
1517 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518{
1519 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1520
1521 ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
1522 ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
1523 ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
1524 ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
1525
1526 return 0;
1527}
1528
1529/* convert from SPDIF status bits to HDA SPDIF bits
1530 * bit 0 (DigEn) is always set zero (to be filled later)
1531 */
1532static unsigned short convert_from_spdif_status(unsigned int sbits)
1533{
1534 unsigned short val = 0;
1535
1536 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02001537 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02001539 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001541 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
1542 IEC958_AES0_PRO_EMPHASIS_5015)
1543 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001545 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
1546 IEC958_AES0_CON_EMPHASIS_5015)
1547 val |= AC_DIG1_EMPHASIS;
1548 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
1549 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02001551 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
1553 }
1554 return val;
1555}
1556
1557/* convert to SPDIF status bits from HDA SPDIF bits
1558 */
1559static unsigned int convert_to_spdif_status(unsigned short val)
1560{
1561 unsigned int sbits = 0;
1562
Takashi Iwai0ba21762007-04-16 11:29:14 +02001563 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001565 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 sbits |= IEC958_AES0_PROFESSIONAL;
1567 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001568 if (sbits & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
1570 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001571 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001573 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001575 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
1577 sbits |= val & (0x7f << 8);
1578 }
1579 return sbits;
1580}
1581
Takashi Iwai2f728532008-09-25 16:32:41 +02001582/* set digital convert verbs both for the given NID and its slaves */
1583static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
1584 int verb, int val)
1585{
1586 hda_nid_t *d;
1587
Takashi Iwai9e976972008-11-25 08:17:20 +01001588 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02001589 d = codec->slave_dig_outs;
1590 if (!d)
1591 return;
1592 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01001593 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02001594}
1595
1596static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
1597 int dig1, int dig2)
1598{
1599 if (dig1 != -1)
1600 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
1601 if (dig2 != -1)
1602 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
1603}
1604
Takashi Iwai0ba21762007-04-16 11:29:14 +02001605static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
1606 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607{
1608 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1609 hda_nid_t nid = kcontrol->private_value;
1610 unsigned short val;
1611 int change;
1612
Ingo Molnar62932df2006-01-16 16:34:20 +01001613 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 codec->spdif_status = ucontrol->value.iec958.status[0] |
1615 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
1616 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
1617 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
1618 val = convert_from_spdif_status(codec->spdif_status);
1619 val |= codec->spdif_ctls & 1;
1620 change = codec->spdif_ctls != val;
1621 codec->spdif_ctls = val;
1622
Takashi Iwai2f728532008-09-25 16:32:41 +02001623 if (change)
1624 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
Ingo Molnar62932df2006-01-16 16:34:20 +01001626 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 return change;
1628}
1629
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001630#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
Takashi Iwai0ba21762007-04-16 11:29:14 +02001632static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
1633 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634{
1635 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1636
Takashi Iwai0ba21762007-04-16 11:29:14 +02001637 ucontrol->value.integer.value[0] = codec->spdif_ctls & AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 return 0;
1639}
1640
Takashi Iwai0ba21762007-04-16 11:29:14 +02001641static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
1642 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643{
1644 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1645 hda_nid_t nid = kcontrol->private_value;
1646 unsigned short val;
1647 int change;
1648
Ingo Molnar62932df2006-01-16 16:34:20 +01001649 mutex_lock(&codec->spdif_mutex);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001650 val = codec->spdif_ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02001652 val |= AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 change = codec->spdif_ctls != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001654 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 codec->spdif_ctls = val;
Takashi Iwai2f728532008-09-25 16:32:41 +02001656 set_dig_out_convert(codec, nid, val & 0xff, -1);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001657 /* unmute amp switch (if any) */
1658 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
Takashi Iwai47fd8302007-08-10 17:11:07 +02001659 (val & AC_DIG1_ENABLE))
1660 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
1661 HDA_AMP_MUTE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 }
Ingo Molnar62932df2006-01-16 16:34:20 +01001663 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 return change;
1665}
1666
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001667static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 {
1669 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1670 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1671 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
1672 .info = snd_hda_spdif_mask_info,
1673 .get = snd_hda_spdif_cmask_get,
1674 },
1675 {
1676 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1677 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1678 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
1679 .info = snd_hda_spdif_mask_info,
1680 .get = snd_hda_spdif_pmask_get,
1681 },
1682 {
1683 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1684 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1685 .info = snd_hda_spdif_mask_info,
1686 .get = snd_hda_spdif_default_get,
1687 .put = snd_hda_spdif_default_put,
1688 },
1689 {
1690 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1691 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
1692 .info = snd_hda_spdif_out_switch_info,
1693 .get = snd_hda_spdif_out_switch_get,
1694 .put = snd_hda_spdif_out_switch_put,
1695 },
1696 { } /* end */
1697};
1698
Takashi Iwai09f99702008-02-04 12:31:13 +01001699#define SPDIF_MAX_IDX 4 /* 4 instances should be enough to probe */
1700
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701/**
1702 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
1703 * @codec: the HDA codec
1704 * @nid: audio out widget NID
1705 *
1706 * Creates controls related with the SPDIF output.
1707 * Called from each patch supporting the SPDIF out.
1708 *
1709 * Returns 0 if successful, or a negative error code.
1710 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02001711int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712{
1713 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001714 struct snd_kcontrol *kctl;
1715 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01001716 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
Takashi Iwai09f99702008-02-04 12:31:13 +01001718 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
1719 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Playback Switch",
1720 idx))
1721 break;
1722 }
1723 if (idx >= SPDIF_MAX_IDX) {
1724 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
1725 return -EBUSY;
1726 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
1728 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01001729 if (!kctl)
1730 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01001731 kctl->id.index = idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 kctl->private_value = nid;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001733 err = snd_hda_ctl_add(codec, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001734 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 return err;
1736 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02001737 codec->spdif_ctls =
Andrew Paprocki3982d172007-12-19 12:13:44 +01001738 snd_hda_codec_read(codec, nid, 0,
1739 AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
1741 return 0;
1742}
1743
1744/*
Takashi Iwai9a081602008-02-12 18:37:26 +01001745 * SPDIF sharing with analog output
1746 */
1747static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
1748 struct snd_ctl_elem_value *ucontrol)
1749{
1750 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
1751 ucontrol->value.integer.value[0] = mout->share_spdif;
1752 return 0;
1753}
1754
1755static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
1756 struct snd_ctl_elem_value *ucontrol)
1757{
1758 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
1759 mout->share_spdif = !!ucontrol->value.integer.value[0];
1760 return 0;
1761}
1762
1763static struct snd_kcontrol_new spdif_share_sw = {
1764 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1765 .name = "IEC958 Default PCM Playback Switch",
1766 .info = snd_ctl_boolean_mono_info,
1767 .get = spdif_share_sw_get,
1768 .put = spdif_share_sw_put,
1769};
1770
1771int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
1772 struct hda_multi_out *mout)
1773{
1774 if (!mout->dig_out_nid)
1775 return 0;
1776 /* ATTENTION: here mout is passed as private_data, instead of codec */
Takashi Iwaid13bd412008-07-30 15:01:45 +02001777 return snd_hda_ctl_add(codec,
Takashi Iwai9a081602008-02-12 18:37:26 +01001778 snd_ctl_new1(&spdif_share_sw, mout));
1779}
1780
1781/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 * SPDIF input
1783 */
1784
1785#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
1786
Takashi Iwai0ba21762007-04-16 11:29:14 +02001787static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
1788 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789{
1790 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1791
1792 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
1793 return 0;
1794}
1795
Takashi Iwai0ba21762007-04-16 11:29:14 +02001796static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
1797 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798{
1799 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1800 hda_nid_t nid = kcontrol->private_value;
1801 unsigned int val = !!ucontrol->value.integer.value[0];
1802 int change;
1803
Ingo Molnar62932df2006-01-16 16:34:20 +01001804 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001806 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001808 snd_hda_codec_write_cache(codec, nid, 0,
1809 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 }
Ingo Molnar62932df2006-01-16 16:34:20 +01001811 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 return change;
1813}
1814
Takashi Iwai0ba21762007-04-16 11:29:14 +02001815static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
1816 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817{
1818 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1819 hda_nid_t nid = kcontrol->private_value;
1820 unsigned short val;
1821 unsigned int sbits;
1822
Andrew Paprocki3982d172007-12-19 12:13:44 +01001823 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 sbits = convert_to_spdif_status(val);
1825 ucontrol->value.iec958.status[0] = sbits;
1826 ucontrol->value.iec958.status[1] = sbits >> 8;
1827 ucontrol->value.iec958.status[2] = sbits >> 16;
1828 ucontrol->value.iec958.status[3] = sbits >> 24;
1829 return 0;
1830}
1831
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001832static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 {
1834 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1835 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH),
1836 .info = snd_hda_spdif_in_switch_info,
1837 .get = snd_hda_spdif_in_switch_get,
1838 .put = snd_hda_spdif_in_switch_put,
1839 },
1840 {
1841 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1842 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1843 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT),
1844 .info = snd_hda_spdif_mask_info,
1845 .get = snd_hda_spdif_in_status_get,
1846 },
1847 { } /* end */
1848};
1849
1850/**
1851 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
1852 * @codec: the HDA codec
1853 * @nid: audio in widget NID
1854 *
1855 * Creates controls related with the SPDIF input.
1856 * Called from each patch supporting the SPDIF in.
1857 *
1858 * Returns 0 if successful, or a negative error code.
1859 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02001860int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861{
1862 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001863 struct snd_kcontrol *kctl;
1864 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01001865 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
Takashi Iwai09f99702008-02-04 12:31:13 +01001867 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
1868 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Capture Switch",
1869 idx))
1870 break;
1871 }
1872 if (idx >= SPDIF_MAX_IDX) {
1873 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
1874 return -EBUSY;
1875 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
1877 kctl = snd_ctl_new1(dig_mix, codec);
1878 kctl->private_value = nid;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001879 err = snd_hda_ctl_add(codec, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001880 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 return err;
1882 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02001883 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01001884 snd_hda_codec_read(codec, nid, 0,
1885 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02001886 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 return 0;
1888}
1889
Takashi Iwaicb53c622007-08-10 17:21:45 +02001890#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001891/*
1892 * command cache
1893 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001895/* build a 32bit cache key with the widget id and the command parameter */
1896#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
1897#define get_cmd_cache_nid(key) ((key) & 0xff)
1898#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
1899
1900/**
1901 * snd_hda_codec_write_cache - send a single command with caching
1902 * @codec: the HDA codec
1903 * @nid: NID to send the command
1904 * @direct: direct flag
1905 * @verb: the verb to send
1906 * @parm: the parameter for the verb
1907 *
1908 * Send a single command without waiting for response.
1909 *
1910 * Returns 0 if successful, or a negative error code.
1911 */
1912int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
1913 int direct, unsigned int verb, unsigned int parm)
1914{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01001915 struct hda_bus *bus = codec->bus;
1916 unsigned int res;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001917 int err;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01001918
1919 res = make_codec_cmd(codec, nid, direct, verb, parm);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001920 snd_hda_power_up(codec);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01001921 mutex_lock(&bus->cmd_mutex);
1922 err = bus->ops.command(bus, res);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001923 if (!err) {
1924 struct hda_cache_head *c;
1925 u32 key = build_cmd_cache_key(nid, verb);
1926 c = get_alloc_hash(&codec->cmd_cache, key);
1927 if (c)
1928 c->val = parm;
1929 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01001930 mutex_unlock(&bus->cmd_mutex);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001931 snd_hda_power_down(codec);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001932 return err;
1933}
1934
1935/* resume the all commands from the cache */
1936void snd_hda_codec_resume_cache(struct hda_codec *codec)
1937{
Takashi Iwai603c4012008-07-30 15:01:44 +02001938 struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001939 int i;
1940
Takashi Iwai603c4012008-07-30 15:01:44 +02001941 for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001942 u32 key = buffer->key;
1943 if (!key)
1944 continue;
1945 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
1946 get_cmd_cache_cmd(key), buffer->val);
1947 }
1948}
1949
1950/**
1951 * snd_hda_sequence_write_cache - sequence writes with caching
1952 * @codec: the HDA codec
1953 * @seq: VERB array to send
1954 *
1955 * Send the commands sequentially from the given array.
1956 * Thte commands are recorded on cache for power-save and resume.
1957 * The array must be terminated with NID=0.
1958 */
1959void snd_hda_sequence_write_cache(struct hda_codec *codec,
1960 const struct hda_verb *seq)
1961{
1962 for (; seq->nid; seq++)
1963 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
1964 seq->param);
1965}
Takashi Iwaicb53c622007-08-10 17:21:45 +02001966#endif /* SND_HDA_NEEDS_RESUME */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001967
Takashi Iwai54d17402005-11-21 16:33:22 +01001968/*
1969 * set power state of the codec
1970 */
1971static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1972 unsigned int power_state)
1973{
Takashi Iwaicb53c622007-08-10 17:21:45 +02001974 hda_nid_t nid;
1975 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01001976
1977 snd_hda_codec_write(codec, fg, 0, AC_VERB_SET_POWER_STATE,
1978 power_state);
Marc Boucherd2595d82008-01-22 15:23:30 +01001979 msleep(10); /* partial workaround for "azx_get_response timeout" */
Takashi Iwai54d17402005-11-21 16:33:22 +01001980
Takashi Iwaicb53c622007-08-10 17:21:45 +02001981 nid = codec->start_nid;
1982 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01001983 unsigned int wcaps = get_wcaps(codec, nid);
1984 if (wcaps & AC_WCAP_POWER) {
1985 unsigned int wid_type = (wcaps & AC_WCAP_TYPE) >>
1986 AC_WCAP_TYPE_SHIFT;
1987 if (wid_type == AC_WID_PIN) {
1988 unsigned int pincap;
1989 /*
1990 * don't power down the widget if it controls
1991 * eapd and EAPD_BTLENABLE is set.
1992 */
1993 pincap = snd_hda_param_read(codec, nid,
1994 AC_PAR_PIN_CAP);
1995 if (pincap & AC_PINCAP_EAPD) {
1996 int eapd = snd_hda_codec_read(codec,
1997 nid, 0,
1998 AC_VERB_GET_EAPD_BTLENABLE, 0);
1999 eapd &= 0x02;
2000 if (power_state == AC_PWRST_D3 && eapd)
2001 continue;
2002 }
Takashi Iwai1194b5b2007-10-10 10:04:26 +02002003 }
Takashi Iwai54d17402005-11-21 16:33:22 +01002004 snd_hda_codec_write(codec, nid, 0,
2005 AC_VERB_SET_POWER_STATE,
2006 power_state);
Takashi Iwai1194b5b2007-10-10 10:04:26 +02002007 }
Takashi Iwai54d17402005-11-21 16:33:22 +01002008 }
2009
Takashi Iwaicb53c622007-08-10 17:21:45 +02002010 if (power_state == AC_PWRST_D0) {
2011 unsigned long end_time;
2012 int state;
Takashi Iwai54d17402005-11-21 16:33:22 +01002013 msleep(10);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002014 /* wait until the codec reachs to D0 */
2015 end_time = jiffies + msecs_to_jiffies(500);
2016 do {
2017 state = snd_hda_codec_read(codec, fg, 0,
2018 AC_VERB_GET_POWER_STATE, 0);
2019 if (state == power_state)
2020 break;
2021 msleep(1);
2022 } while (time_after_eq(end_time, jiffies));
2023 }
Takashi Iwai54d17402005-11-21 16:33:22 +01002024}
2025
Takashi Iwai11aeff02008-07-30 15:01:46 +02002026#ifdef CONFIG_SND_HDA_HWDEP
2027/* execute additional init verbs */
2028static void hda_exec_init_verbs(struct hda_codec *codec)
2029{
2030 if (codec->init_verbs.list)
2031 snd_hda_sequence_write(codec, codec->init_verbs.list);
2032}
2033#else
2034static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
2035#endif
2036
Takashi Iwaicb53c622007-08-10 17:21:45 +02002037#ifdef SND_HDA_NEEDS_RESUME
2038/*
2039 * call suspend and power-down; used both from PM and power-save
2040 */
2041static void hda_call_codec_suspend(struct hda_codec *codec)
2042{
2043 if (codec->patch_ops.suspend)
2044 codec->patch_ops.suspend(codec, PMSG_SUSPEND);
2045 hda_set_power_state(codec,
2046 codec->afg ? codec->afg : codec->mfg,
2047 AC_PWRST_D3);
2048#ifdef CONFIG_SND_HDA_POWER_SAVE
2049 cancel_delayed_work(&codec->power_work);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02002050 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02002051 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002052#endif
2053}
2054
2055/*
2056 * kick up codec; used both from PM and power-save
2057 */
2058static void hda_call_codec_resume(struct hda_codec *codec)
2059{
2060 hda_set_power_state(codec,
2061 codec->afg ? codec->afg : codec->mfg,
2062 AC_PWRST_D0);
Takashi Iwai11aeff02008-07-30 15:01:46 +02002063 hda_exec_init_verbs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002064 if (codec->patch_ops.resume)
2065 codec->patch_ops.resume(codec);
2066 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02002067 if (codec->patch_ops.init)
2068 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002069 snd_hda_codec_resume_amp(codec);
2070 snd_hda_codec_resume_cache(codec);
2071 }
2072}
2073#endif /* SND_HDA_NEEDS_RESUME */
2074
Takashi Iwai54d17402005-11-21 16:33:22 +01002075
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076/**
2077 * snd_hda_build_controls - build mixer controls
2078 * @bus: the BUS
2079 *
2080 * Creates mixer controls for each codec included in the bus.
2081 *
2082 * Returns 0 if successful, otherwise a negative error code.
2083 */
Takashi Iwai756e2b02007-04-16 11:27:07 +02002084int __devinit snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085{
Takashi Iwai0ba21762007-04-16 11:29:14 +02002086 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
Takashi Iwai0ba21762007-04-16 11:29:14 +02002088 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002089 int err = snd_hda_codec_build_controls(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 if (err < 0)
2091 return err;
2092 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002093 return 0;
2094}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002096int snd_hda_codec_build_controls(struct hda_codec *codec)
2097{
2098 int err = 0;
2099 /* fake as if already powered-on */
2100 hda_keep_power_on(codec);
2101 /* then fire up */
2102 hda_set_power_state(codec,
2103 codec->afg ? codec->afg : codec->mfg,
2104 AC_PWRST_D0);
Takashi Iwai11aeff02008-07-30 15:01:46 +02002105 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002106 /* continue to initialize... */
2107 if (codec->patch_ops.init)
2108 err = codec->patch_ops.init(codec);
2109 if (!err && codec->patch_ops.build_controls)
2110 err = codec->patch_ops.build_controls(codec);
2111 snd_hda_power_down(codec);
2112 if (err < 0)
2113 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 return 0;
2115}
2116
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117/*
2118 * stream formats
2119 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02002120struct hda_rate_tbl {
2121 unsigned int hz;
2122 unsigned int alsa_bits;
2123 unsigned int hda_fmt;
2124};
2125
2126static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02002128
2129 /* autodetected value used in snd_hda_query_supported_pcm */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 { 8000, SNDRV_PCM_RATE_8000, 0x0500 }, /* 1/6 x 48 */
2131 { 11025, SNDRV_PCM_RATE_11025, 0x4300 }, /* 1/4 x 44 */
2132 { 16000, SNDRV_PCM_RATE_16000, 0x0200 }, /* 1/3 x 48 */
2133 { 22050, SNDRV_PCM_RATE_22050, 0x4100 }, /* 1/2 x 44 */
2134 { 32000, SNDRV_PCM_RATE_32000, 0x0a00 }, /* 2/3 x 48 */
2135 { 44100, SNDRV_PCM_RATE_44100, 0x4000 }, /* 44 */
2136 { 48000, SNDRV_PCM_RATE_48000, 0x0000 }, /* 48 */
2137 { 88200, SNDRV_PCM_RATE_88200, 0x4800 }, /* 2 x 44 */
2138 { 96000, SNDRV_PCM_RATE_96000, 0x0800 }, /* 2 x 48 */
2139 { 176400, SNDRV_PCM_RATE_176400, 0x5800 },/* 4 x 44 */
2140 { 192000, SNDRV_PCM_RATE_192000, 0x1800 }, /* 4 x 48 */
Takashi Iwaia961f9f2007-04-12 13:08:09 +02002141#define AC_PAR_PCM_RATE_BITS 11
2142 /* up to bits 10, 384kHZ isn't supported properly */
2143
2144 /* not autodetected value */
2145 { 9600, SNDRV_PCM_RATE_KNOT, 0x0400 }, /* 1/5 x 48 */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02002146
Takashi Iwaibefdf312005-08-22 13:57:55 +02002147 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148};
2149
2150/**
2151 * snd_hda_calc_stream_format - calculate format bitset
2152 * @rate: the sample rate
2153 * @channels: the number of channels
2154 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
2155 * @maxbps: the max. bps
2156 *
2157 * Calculate the format bitset from the given rate, channels and th PCM format.
2158 *
2159 * Return zero if invalid.
2160 */
2161unsigned int snd_hda_calc_stream_format(unsigned int rate,
2162 unsigned int channels,
2163 unsigned int format,
2164 unsigned int maxbps)
2165{
2166 int i;
2167 unsigned int val = 0;
2168
Takashi Iwaibefdf312005-08-22 13:57:55 +02002169 for (i = 0; rate_bits[i].hz; i++)
2170 if (rate_bits[i].hz == rate) {
2171 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 break;
2173 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002174 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 snd_printdd("invalid rate %d\n", rate);
2176 return 0;
2177 }
2178
2179 if (channels == 0 || channels > 8) {
2180 snd_printdd("invalid channels %d\n", channels);
2181 return 0;
2182 }
2183 val |= channels - 1;
2184
2185 switch (snd_pcm_format_width(format)) {
2186 case 8: val |= 0x00; break;
2187 case 16: val |= 0x10; break;
2188 case 20:
2189 case 24:
2190 case 32:
2191 if (maxbps >= 32)
2192 val |= 0x40;
2193 else if (maxbps >= 24)
2194 val |= 0x30;
2195 else
2196 val |= 0x20;
2197 break;
2198 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002199 snd_printdd("invalid format width %d\n",
2200 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 return 0;
2202 }
2203
2204 return val;
2205}
2206
2207/**
2208 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
2209 * @codec: the HDA codec
2210 * @nid: NID to query
2211 * @ratesp: the pointer to store the detected rate bitflags
2212 * @formatsp: the pointer to store the detected formats
2213 * @bpsp: the pointer to store the detected format widths
2214 *
2215 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
2216 * or @bsps argument is ignored.
2217 *
2218 * Returns 0 if successful, otherwise a negative error code.
2219 */
Takashi Iwai986862bd2008-11-27 12:40:13 +01002220static int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
2222{
2223 int i;
2224 unsigned int val, streams;
2225
2226 val = 0;
2227 if (nid != codec->afg &&
Takashi Iwai54d17402005-11-21 16:33:22 +01002228 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
2230 if (val == -1)
2231 return -EIO;
2232 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002233 if (!val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
2235
2236 if (ratesp) {
2237 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02002238 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02002240 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 }
2242 *ratesp = rates;
2243 }
2244
2245 if (formatsp || bpsp) {
2246 u64 formats = 0;
2247 unsigned int bps;
2248 unsigned int wcaps;
2249
Takashi Iwai54d17402005-11-21 16:33:22 +01002250 wcaps = get_wcaps(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
2252 if (streams == -1)
2253 return -EIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002254 if (!streams) {
2255 streams = snd_hda_param_read(codec, codec->afg,
2256 AC_PAR_STREAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 if (streams == -1)
2258 return -EIO;
2259 }
2260
2261 bps = 0;
2262 if (streams & AC_SUPFMT_PCM) {
2263 if (val & AC_SUPPCM_BITS_8) {
2264 formats |= SNDRV_PCM_FMTBIT_U8;
2265 bps = 8;
2266 }
2267 if (val & AC_SUPPCM_BITS_16) {
2268 formats |= SNDRV_PCM_FMTBIT_S16_LE;
2269 bps = 16;
2270 }
2271 if (wcaps & AC_WCAP_DIGITAL) {
2272 if (val & AC_SUPPCM_BITS_32)
2273 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
2274 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
2275 formats |= SNDRV_PCM_FMTBIT_S32_LE;
2276 if (val & AC_SUPPCM_BITS_24)
2277 bps = 24;
2278 else if (val & AC_SUPPCM_BITS_20)
2279 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002280 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
2281 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 formats |= SNDRV_PCM_FMTBIT_S32_LE;
2283 if (val & AC_SUPPCM_BITS_32)
2284 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 else if (val & AC_SUPPCM_BITS_24)
2286 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02002287 else if (val & AC_SUPPCM_BITS_20)
2288 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 }
2290 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002291 else if (streams == AC_SUPFMT_FLOAT32) {
2292 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
2294 bps = 32;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002295 } else if (streams == AC_SUPFMT_AC3) {
2296 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 /* temporary hack: we have still no proper support
2298 * for the direct AC3 stream...
2299 */
2300 formats |= SNDRV_PCM_FMTBIT_U8;
2301 bps = 8;
2302 }
2303 if (formatsp)
2304 *formatsp = formats;
2305 if (bpsp)
2306 *bpsp = bps;
2307 }
2308
2309 return 0;
2310}
2311
2312/**
Takashi Iwai0ba21762007-04-16 11:29:14 +02002313 * snd_hda_is_supported_format - check whether the given node supports
2314 * the format val
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 *
2316 * Returns 1 if supported, 0 if not.
2317 */
2318int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
2319 unsigned int format)
2320{
2321 int i;
2322 unsigned int val = 0, rate, stream;
2323
2324 if (nid != codec->afg &&
Takashi Iwai54d17402005-11-21 16:33:22 +01002325 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
2327 if (val == -1)
2328 return 0;
2329 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002330 if (!val) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
2332 if (val == -1)
2333 return 0;
2334 }
2335
2336 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02002337 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02002338 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 if (val & (1 << i))
2340 break;
2341 return 0;
2342 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02002343 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 return 0;
2345
2346 stream = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
2347 if (stream == -1)
2348 return 0;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002349 if (!stream && nid != codec->afg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 stream = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002351 if (!stream || stream == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 return 0;
2353
2354 if (stream & AC_SUPFMT_PCM) {
2355 switch (format & 0xf0) {
2356 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002357 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 return 0;
2359 break;
2360 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002361 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 return 0;
2363 break;
2364 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002365 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 return 0;
2367 break;
2368 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002369 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 return 0;
2371 break;
2372 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002373 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 return 0;
2375 break;
2376 default:
2377 return 0;
2378 }
2379 } else {
2380 /* FIXME: check for float32 and AC3? */
2381 }
2382
2383 return 1;
2384}
2385
2386/*
2387 * PCM stuff
2388 */
2389static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
2390 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002391 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392{
2393 return 0;
2394}
2395
2396static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
2397 struct hda_codec *codec,
2398 unsigned int stream_tag,
2399 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002400 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401{
2402 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
2403 return 0;
2404}
2405
2406static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
2407 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002408 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409{
Takashi Iwai888afa12008-03-18 09:57:50 +01002410 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 return 0;
2412}
2413
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002414static int set_pcm_default_values(struct hda_codec *codec,
2415 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416{
Takashi Iwai0ba21762007-04-16 11:29:14 +02002417 /* query support PCM information from the given NID */
2418 if (info->nid && (!info->rates || !info->formats)) {
2419 snd_hda_query_supported_pcm(codec, info->nid,
2420 info->rates ? NULL : &info->rates,
2421 info->formats ? NULL : &info->formats,
2422 info->maxbps ? NULL : &info->maxbps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 }
2424 if (info->ops.open == NULL)
2425 info->ops.open = hda_pcm_default_open_close;
2426 if (info->ops.close == NULL)
2427 info->ops.close = hda_pcm_default_open_close;
2428 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02002429 if (snd_BUG_ON(!info->nid))
2430 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 info->ops.prepare = hda_pcm_default_prepare;
2432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02002434 if (snd_BUG_ON(!info->nid))
2435 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 info->ops.cleanup = hda_pcm_default_cleanup;
2437 }
2438 return 0;
2439}
2440
Takashi Iwai176d5332008-07-30 15:01:44 +02002441/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002442 * get the empty PCM device number to assign
2443 */
2444static int get_empty_pcm_device(struct hda_bus *bus, int type)
2445{
2446 static const char *dev_name[HDA_PCM_NTYPES] = {
2447 "Audio", "SPDIF", "HDMI", "Modem"
2448 };
2449 /* starting device index for each PCM type */
2450 static int dev_idx[HDA_PCM_NTYPES] = {
2451 [HDA_PCM_TYPE_AUDIO] = 0,
2452 [HDA_PCM_TYPE_SPDIF] = 1,
2453 [HDA_PCM_TYPE_HDMI] = 3,
2454 [HDA_PCM_TYPE_MODEM] = 6
2455 };
2456 /* normal audio device indices; not linear to keep compatibility */
2457 static int audio_idx[4] = { 0, 2, 4, 5 };
2458 int i, dev;
2459
2460 switch (type) {
2461 case HDA_PCM_TYPE_AUDIO:
2462 for (i = 0; i < ARRAY_SIZE(audio_idx); i++) {
2463 dev = audio_idx[i];
2464 if (!test_bit(dev, bus->pcm_dev_bits))
2465 break;
2466 }
2467 if (i >= ARRAY_SIZE(audio_idx)) {
2468 snd_printk(KERN_WARNING "Too many audio devices\n");
2469 return -EAGAIN;
2470 }
2471 break;
2472 case HDA_PCM_TYPE_SPDIF:
2473 case HDA_PCM_TYPE_HDMI:
2474 case HDA_PCM_TYPE_MODEM:
2475 dev = dev_idx[type];
2476 if (test_bit(dev, bus->pcm_dev_bits)) {
2477 snd_printk(KERN_WARNING "%s already defined\n",
2478 dev_name[type]);
2479 return -EAGAIN;
2480 }
2481 break;
2482 default:
2483 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
2484 return -EINVAL;
2485 }
2486 set_bit(dev, bus->pcm_dev_bits);
2487 return dev;
2488}
2489
2490/*
Takashi Iwai176d5332008-07-30 15:01:44 +02002491 * attach a new PCM stream
2492 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002493static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02002494{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01002495 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02002496 struct hda_pcm_stream *info;
2497 int stream, err;
2498
Takashi Iwaib91f0802008-11-04 08:43:08 +01002499 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02002500 return -EINVAL;
2501 for (stream = 0; stream < 2; stream++) {
2502 info = &pcm->stream[stream];
2503 if (info->substreams) {
2504 err = set_pcm_default_values(codec, info);
2505 if (err < 0)
2506 return err;
2507 }
2508 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01002509 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02002510}
2511
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002512/* assign all PCMs of the given codec */
2513int snd_hda_codec_build_pcms(struct hda_codec *codec)
2514{
2515 unsigned int pcm;
2516 int err;
2517
2518 if (!codec->num_pcms) {
2519 if (!codec->patch_ops.build_pcms)
2520 return 0;
2521 err = codec->patch_ops.build_pcms(codec);
2522 if (err < 0)
2523 return err;
2524 }
2525 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2526 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2527 int dev;
2528
2529 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
2530 return 0; /* no substreams assigned */
2531
2532 if (!cpcm->pcm) {
2533 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
2534 if (dev < 0)
2535 return 0;
2536 cpcm->device = dev;
2537 err = snd_hda_attach_pcm(codec, cpcm);
2538 if (err < 0)
2539 return err;
2540 }
2541 }
2542 return 0;
2543}
2544
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545/**
2546 * snd_hda_build_pcms - build PCM information
2547 * @bus: the BUS
2548 *
2549 * Create PCM information for each codec included in the bus.
2550 *
2551 * The build_pcms codec patch is requested to set up codec->num_pcms and
2552 * codec->pcm_info properly. The array is referred by the top-level driver
2553 * to create its PCM instances.
2554 * The allocated codec->pcm_info should be released in codec->patch_ops.free
2555 * callback.
2556 *
2557 * At least, substreams, channels_min and channels_max must be filled for
2558 * each stream. substreams = 0 indicates that the stream doesn't exist.
2559 * When rates and/or formats are zero, the supported values are queried
2560 * from the given nid. The nid is used also by the default ops.prepare
2561 * and ops.cleanup callbacks.
2562 *
2563 * The driver needs to call ops.open in its open callback. Similarly,
2564 * ops.close is supposed to be called in the close callback.
2565 * ops.prepare should be called in the prepare or hw_params callback
2566 * with the proper parameters for set up.
2567 * ops.cleanup should be called in hw_free for clean up of streams.
2568 *
2569 * This function returns 0 if successfull, or a negative error code.
2570 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002571int __devinit snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572{
Takashi Iwai0ba21762007-04-16 11:29:14 +02002573 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574
Takashi Iwai0ba21762007-04-16 11:29:14 +02002575 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002576 int err = snd_hda_codec_build_pcms(codec);
2577 if (err < 0)
2578 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 }
2580 return 0;
2581}
2582
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583/**
2584 * snd_hda_check_board_config - compare the current codec with the config table
2585 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002586 * @num_configs: number of config enums
2587 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 * @tbl: configuration table, terminated by null entries
2589 *
2590 * Compares the modelname or PCI subsystem id of the current codec with the
2591 * given configuration table. If a matching entry is found, returns its
2592 * config value (supposed to be 0 or positive).
2593 *
2594 * If no entries are matching, the function returns a negative value.
2595 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002596int snd_hda_check_board_config(struct hda_codec *codec,
2597 int num_configs, const char **models,
2598 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599{
Takashi Iwaif44ac832008-07-30 15:01:45 +02002600 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002601 int i;
2602 for (i = 0; i < num_configs; i++) {
2603 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02002604 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002605 snd_printd(KERN_INFO "hda_codec: model '%s' is "
2606 "selected\n", models[i]);
2607 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 }
2609 }
2610 }
2611
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002612 if (!codec->bus->pci || !tbl)
2613 return -1;
2614
2615 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
2616 if (!tbl)
2617 return -1;
2618 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02002619#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002620 char tmp[10];
2621 const char *model = NULL;
2622 if (models)
2623 model = models[tbl->value];
2624 if (!model) {
2625 sprintf(tmp, "#%d", tbl->value);
2626 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002628 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
2629 "for config %x:%x (%s)\n",
2630 model, tbl->subvendor, tbl->subdevice,
2631 (tbl->name ? tbl->name : "Unknown device"));
2632#endif
2633 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 }
2635 return -1;
2636}
2637
2638/**
2639 * snd_hda_add_new_ctls - create controls from the array
2640 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002641 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 *
2643 * This helper function creates and add new controls in the given array.
2644 * The array must be terminated with an empty entry as terminator.
2645 *
2646 * Returns 0 if successful, or a negative error code.
2647 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002648int snd_hda_add_new_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649{
Takashi Iwaicb53c622007-08-10 17:21:45 +02002650 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651
2652 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01002653 struct snd_kcontrol *kctl;
2654 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002655 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01002656 return -ENOMEM;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002657 err = snd_hda_ctl_add(codec, kctl);
Takashi Iwai54d17402005-11-21 16:33:22 +01002658 if (err < 0) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002659 if (!codec->addr)
Takashi Iwai54d17402005-11-21 16:33:22 +01002660 return err;
2661 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002662 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01002663 return -ENOMEM;
2664 kctl->id.device = codec->addr;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002665 err = snd_hda_ctl_add(codec, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002666 if (err < 0)
Takashi Iwai54d17402005-11-21 16:33:22 +01002667 return err;
2668 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 }
2670 return 0;
2671}
2672
Takashi Iwaicb53c622007-08-10 17:21:45 +02002673#ifdef CONFIG_SND_HDA_POWER_SAVE
2674static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2675 unsigned int power_state);
2676
2677static void hda_power_work(struct work_struct *work)
2678{
2679 struct hda_codec *codec =
2680 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01002681 struct hda_bus *bus = codec->bus;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002682
Maxim Levitsky2e492462007-09-03 15:26:57 +02002683 if (!codec->power_on || codec->power_count) {
2684 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002685 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02002686 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02002687
2688 hda_call_codec_suspend(codec);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01002689 if (bus->ops.pm_notify)
2690 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002691}
2692
2693static void hda_keep_power_on(struct hda_codec *codec)
2694{
2695 codec->power_count++;
2696 codec->power_on = 1;
2697}
2698
2699void snd_hda_power_up(struct hda_codec *codec)
2700{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01002701 struct hda_bus *bus = codec->bus;
2702
Takashi Iwaicb53c622007-08-10 17:21:45 +02002703 codec->power_count++;
Takashi Iwaia221e282007-08-16 16:35:33 +02002704 if (codec->power_on || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02002705 return;
2706
2707 codec->power_on = 1;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01002708 if (bus->ops.pm_notify)
2709 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002710 hda_call_codec_resume(codec);
2711 cancel_delayed_work(&codec->power_work);
Takashi Iwaia221e282007-08-16 16:35:33 +02002712 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002713}
2714
Takashi Iwaifee2fba2008-11-27 12:43:28 +01002715#define power_save(codec) \
2716 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
2717
Takashi Iwaicb53c622007-08-10 17:21:45 +02002718void snd_hda_power_down(struct hda_codec *codec)
2719{
2720 --codec->power_count;
Takashi Iwaia221e282007-08-16 16:35:33 +02002721 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02002722 return;
Takashi Iwaifee2fba2008-11-27 12:43:28 +01002723 if (power_save(codec)) {
Takashi Iwaia221e282007-08-16 16:35:33 +02002724 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002725 schedule_delayed_work(&codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01002726 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02002727 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02002728}
2729
2730int snd_hda_check_amp_list_power(struct hda_codec *codec,
2731 struct hda_loopback_check *check,
2732 hda_nid_t nid)
2733{
2734 struct hda_amp_list *p;
2735 int ch, v;
2736
2737 if (!check->amplist)
2738 return 0;
2739 for (p = check->amplist; p->nid; p++) {
2740 if (p->nid == nid)
2741 break;
2742 }
2743 if (!p->nid)
2744 return 0; /* nothing changed */
2745
2746 for (p = check->amplist; p->nid; p++) {
2747 for (ch = 0; ch < 2; ch++) {
2748 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
2749 p->idx);
2750 if (!(v & HDA_AMP_MUTE) && v > 0) {
2751 if (!check->power_on) {
2752 check->power_on = 1;
2753 snd_hda_power_up(codec);
2754 }
2755 return 1;
2756 }
2757 }
2758 }
2759 if (check->power_on) {
2760 check->power_on = 0;
2761 snd_hda_power_down(codec);
2762 }
2763 return 0;
2764}
2765#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002767/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002768 * Channel mode helper
2769 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002770int snd_hda_ch_mode_info(struct hda_codec *codec,
2771 struct snd_ctl_elem_info *uinfo,
2772 const struct hda_channel_mode *chmode,
2773 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002774{
2775 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2776 uinfo->count = 1;
2777 uinfo->value.enumerated.items = num_chmodes;
2778 if (uinfo->value.enumerated.item >= num_chmodes)
2779 uinfo->value.enumerated.item = num_chmodes - 1;
2780 sprintf(uinfo->value.enumerated.name, "%dch",
2781 chmode[uinfo->value.enumerated.item].channels);
2782 return 0;
2783}
2784
Takashi Iwai0ba21762007-04-16 11:29:14 +02002785int snd_hda_ch_mode_get(struct hda_codec *codec,
2786 struct snd_ctl_elem_value *ucontrol,
2787 const struct hda_channel_mode *chmode,
2788 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002789 int max_channels)
2790{
2791 int i;
2792
2793 for (i = 0; i < num_chmodes; i++) {
2794 if (max_channels == chmode[i].channels) {
2795 ucontrol->value.enumerated.item[0] = i;
2796 break;
2797 }
2798 }
2799 return 0;
2800}
2801
Takashi Iwai0ba21762007-04-16 11:29:14 +02002802int snd_hda_ch_mode_put(struct hda_codec *codec,
2803 struct snd_ctl_elem_value *ucontrol,
2804 const struct hda_channel_mode *chmode,
2805 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002806 int *max_channelsp)
2807{
2808 unsigned int mode;
2809
2810 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01002811 if (mode >= num_chmodes)
2812 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002813 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002814 return 0;
2815 /* change the current channel setting */
2816 *max_channelsp = chmode[mode].channels;
2817 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002818 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002819 return 1;
2820}
2821
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822/*
2823 * input MUX helper
2824 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002825int snd_hda_input_mux_info(const struct hda_input_mux *imux,
2826 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827{
2828 unsigned int index;
2829
2830 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2831 uinfo->count = 1;
2832 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02002833 if (!imux->num_items)
2834 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 index = uinfo->value.enumerated.item;
2836 if (index >= imux->num_items)
2837 index = imux->num_items - 1;
2838 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
2839 return 0;
2840}
2841
Takashi Iwai0ba21762007-04-16 11:29:14 +02002842int snd_hda_input_mux_put(struct hda_codec *codec,
2843 const struct hda_input_mux *imux,
2844 struct snd_ctl_elem_value *ucontrol,
2845 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 unsigned int *cur_val)
2847{
2848 unsigned int idx;
2849
Takashi Iwai5513b0c2007-10-09 11:58:41 +02002850 if (!imux->num_items)
2851 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 idx = ucontrol->value.enumerated.item[0];
2853 if (idx >= imux->num_items)
2854 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002855 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002857 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
2858 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 *cur_val = idx;
2860 return 1;
2861}
2862
2863
2864/*
2865 * Multi-channel / digital-out PCM helper functions
2866 */
2867
Takashi Iwai6b97eb42007-04-05 14:51:48 +02002868/* setup SPDIF output stream */
2869static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
2870 unsigned int stream_tag, unsigned int format)
2871{
2872 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Takashi Iwai2f728532008-09-25 16:32:41 +02002873 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
2874 set_dig_out_convert(codec, nid,
2875 codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff,
2876 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02002877 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02002878 if (codec->slave_dig_outs) {
2879 hda_nid_t *d;
2880 for (d = codec->slave_dig_outs; *d; d++)
2881 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
2882 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04002883 }
Takashi Iwai2f728532008-09-25 16:32:41 +02002884 /* turn on again (if needed) */
2885 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
2886 set_dig_out_convert(codec, nid,
2887 codec->spdif_ctls & 0xff, -1);
2888}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04002889
Takashi Iwai2f728532008-09-25 16:32:41 +02002890static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
2891{
2892 snd_hda_codec_cleanup_stream(codec, nid);
2893 if (codec->slave_dig_outs) {
2894 hda_nid_t *d;
2895 for (d = codec->slave_dig_outs; *d; d++)
2896 snd_hda_codec_cleanup_stream(codec, *d);
2897 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02002898}
2899
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900/*
2901 * open the digital out in the exclusive mode
2902 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002903int snd_hda_multi_out_dig_open(struct hda_codec *codec,
2904 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905{
Ingo Molnar62932df2006-01-16 16:34:20 +01002906 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02002907 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
2908 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02002909 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01002911 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 return 0;
2913}
2914
Takashi Iwai6b97eb42007-04-05 14:51:48 +02002915int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
2916 struct hda_multi_out *mout,
2917 unsigned int stream_tag,
2918 unsigned int format,
2919 struct snd_pcm_substream *substream)
2920{
2921 mutex_lock(&codec->spdif_mutex);
2922 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
2923 mutex_unlock(&codec->spdif_mutex);
2924 return 0;
2925}
2926
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927/*
2928 * release the digital out
2929 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002930int snd_hda_multi_out_dig_close(struct hda_codec *codec,
2931 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932{
Ingo Molnar62932df2006-01-16 16:34:20 +01002933 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01002935 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 return 0;
2937}
2938
2939/*
2940 * set up more restrictions for analog out
2941 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002942int snd_hda_multi_out_analog_open(struct hda_codec *codec,
2943 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01002944 struct snd_pcm_substream *substream,
2945 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946{
Takashi Iwai9a081602008-02-12 18:37:26 +01002947 struct snd_pcm_runtime *runtime = substream->runtime;
2948 runtime->hw.channels_max = mout->max_channels;
2949 if (mout->dig_out_nid) {
2950 if (!mout->analog_rates) {
2951 mout->analog_rates = hinfo->rates;
2952 mout->analog_formats = hinfo->formats;
2953 mout->analog_maxbps = hinfo->maxbps;
2954 } else {
2955 runtime->hw.rates = mout->analog_rates;
2956 runtime->hw.formats = mout->analog_formats;
2957 hinfo->maxbps = mout->analog_maxbps;
2958 }
2959 if (!mout->spdif_rates) {
2960 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
2961 &mout->spdif_rates,
2962 &mout->spdif_formats,
2963 &mout->spdif_maxbps);
2964 }
2965 mutex_lock(&codec->spdif_mutex);
2966 if (mout->share_spdif) {
2967 runtime->hw.rates &= mout->spdif_rates;
2968 runtime->hw.formats &= mout->spdif_formats;
2969 if (mout->spdif_maxbps < hinfo->maxbps)
2970 hinfo->maxbps = mout->spdif_maxbps;
2971 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02002972 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01002973 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 return snd_pcm_hw_constraint_step(substream->runtime, 0,
2975 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
2976}
2977
2978/*
2979 * set up the i/o for analog out
2980 * when the digital out is available, copy the front out to digital out, too.
2981 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002982int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
2983 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 unsigned int stream_tag,
2985 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002986 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987{
2988 hda_nid_t *nids = mout->dac_nids;
2989 int chs = substream->runtime->channels;
2990 int i;
2991
Ingo Molnar62932df2006-01-16 16:34:20 +01002992 mutex_lock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01002993 if (mout->dig_out_nid && mout->share_spdif &&
2994 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02002996 snd_hda_is_supported_format(codec, mout->dig_out_nid,
2997 format) &&
2998 !(codec->spdif_status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003000 setup_dig_out_stream(codec, mout->dig_out_nid,
3001 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 } else {
3003 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02003004 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 }
3006 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003007 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008
3009 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003010 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
3011 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02003012 if (!mout->no_share_stream &&
3013 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003015 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
3016 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01003017 /* extra outputs copied from front */
3018 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02003019 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01003020 snd_hda_codec_setup_stream(codec,
3021 mout->extra_out_nid[i],
3022 stream_tag, 0, format);
3023
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 /* surrounds */
3025 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02003026 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003027 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
3028 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02003029 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003030 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
3031 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 }
3033 return 0;
3034}
3035
3036/*
3037 * clean up the setting for analog out
3038 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003039int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
3040 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041{
3042 hda_nid_t *nids = mout->dac_nids;
3043 int i;
3044
3045 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01003046 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01003048 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwai82bc9552006-03-21 11:24:42 +01003049 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
3050 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01003051 snd_hda_codec_cleanup_stream(codec,
3052 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01003053 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02003055 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 mout->dig_out_used = 0;
3057 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003058 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 return 0;
3060}
3061
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003062/*
Wu Fengguang6b345002008-10-07 14:21:41 +08003063 * Helper for automatic pin configuration
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003064 */
Kailang Yangdf694da2005-12-05 19:42:22 +01003065
Takashi Iwai12f288b2007-08-02 15:51:59 +02003066static int is_in_nid_list(hda_nid_t nid, hda_nid_t *list)
Kailang Yangdf694da2005-12-05 19:42:22 +01003067{
3068 for (; *list; list++)
3069 if (*list == nid)
3070 return 1;
3071 return 0;
3072}
3073
Steve Longerbeam81937d32007-05-08 15:33:03 +02003074
3075/*
3076 * Sort an associated group of pins according to their sequence numbers.
3077 */
3078static void sort_pins_by_sequence(hda_nid_t * pins, short * sequences,
3079 int num_pins)
3080{
3081 int i, j;
3082 short seq;
3083 hda_nid_t nid;
3084
3085 for (i = 0; i < num_pins; i++) {
3086 for (j = i + 1; j < num_pins; j++) {
3087 if (sequences[i] > sequences[j]) {
3088 seq = sequences[i];
3089 sequences[i] = sequences[j];
3090 sequences[j] = seq;
3091 nid = pins[i];
3092 pins[i] = pins[j];
3093 pins[j] = nid;
3094 }
3095 }
3096 }
3097}
3098
3099
Takashi Iwai82bc9552006-03-21 11:24:42 +01003100/*
3101 * Parse all pin widgets and store the useful pin nids to cfg
3102 *
3103 * The number of line-outs or any primary output is stored in line_outs,
3104 * and the corresponding output pins are assigned to line_out_pins[],
3105 * in the order of front, rear, CLFE, side, ...
3106 *
3107 * If more extra outputs (speaker and headphone) are found, the pins are
Takashi Iwaieb06ed82006-09-20 17:10:27 +02003108 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
Takashi Iwai82bc9552006-03-21 11:24:42 +01003109 * is detected, one of speaker of HP pins is assigned as the primary
3110 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
3111 * if any analog output exists.
3112 *
3113 * The analog input pins are assigned to input_pins array.
3114 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
3115 * respectively.
3116 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003117int snd_hda_parse_pin_def_config(struct hda_codec *codec,
3118 struct auto_pin_cfg *cfg,
3119 hda_nid_t *ignore_nids)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003120{
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01003121 hda_nid_t nid, end_nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02003122 short seq, assoc_line_out, assoc_speaker;
3123 short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
3124 short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
Takashi Iwaif889fa92007-10-31 15:49:32 +01003125 short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003126
3127 memset(cfg, 0, sizeof(*cfg));
3128
Steve Longerbeam81937d32007-05-08 15:33:03 +02003129 memset(sequences_line_out, 0, sizeof(sequences_line_out));
3130 memset(sequences_speaker, 0, sizeof(sequences_speaker));
Takashi Iwaif889fa92007-10-31 15:49:32 +01003131 memset(sequences_hp, 0, sizeof(sequences_hp));
Steve Longerbeam81937d32007-05-08 15:33:03 +02003132 assoc_line_out = assoc_speaker = 0;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003133
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01003134 end_nid = codec->start_nid + codec->num_nodes;
3135 for (nid = codec->start_nid; nid < end_nid; nid++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01003136 unsigned int wid_caps = get_wcaps(codec, nid);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003137 unsigned int wid_type =
3138 (wid_caps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003139 unsigned int def_conf;
3140 short assoc, loc;
3141
3142 /* read all default configuration for pin complex */
3143 if (wid_type != AC_WID_PIN)
3144 continue;
Kailang Yangdf694da2005-12-05 19:42:22 +01003145 /* ignore the given nids (e.g. pc-beep returns error) */
3146 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
3147 continue;
3148
Takashi Iwai0ba21762007-04-16 11:29:14 +02003149 def_conf = snd_hda_codec_read(codec, nid, 0,
3150 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003151 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
3152 continue;
3153 loc = get_defcfg_location(def_conf);
3154 switch (get_defcfg_device(def_conf)) {
3155 case AC_JACK_LINE_OUT:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003156 seq = get_defcfg_sequence(def_conf);
3157 assoc = get_defcfg_association(def_conf);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01003158
3159 if (!(wid_caps & AC_WCAP_STEREO))
3160 if (!cfg->mono_out_pin)
3161 cfg->mono_out_pin = nid;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003162 if (!assoc)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003163 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003164 if (!assoc_line_out)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003165 assoc_line_out = assoc;
3166 else if (assoc_line_out != assoc)
3167 continue;
3168 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
3169 continue;
3170 cfg->line_out_pins[cfg->line_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02003171 sequences_line_out[cfg->line_outs] = seq;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003172 cfg->line_outs++;
3173 break;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01003174 case AC_JACK_SPEAKER:
Steve Longerbeam81937d32007-05-08 15:33:03 +02003175 seq = get_defcfg_sequence(def_conf);
3176 assoc = get_defcfg_association(def_conf);
3177 if (! assoc)
3178 continue;
3179 if (! assoc_speaker)
3180 assoc_speaker = assoc;
3181 else if (assoc_speaker != assoc)
3182 continue;
Takashi Iwai82bc9552006-03-21 11:24:42 +01003183 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
3184 continue;
3185 cfg->speaker_pins[cfg->speaker_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02003186 sequences_speaker[cfg->speaker_outs] = seq;
Takashi Iwai82bc9552006-03-21 11:24:42 +01003187 cfg->speaker_outs++;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01003188 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003189 case AC_JACK_HP_OUT:
Takashi Iwaif889fa92007-10-31 15:49:32 +01003190 seq = get_defcfg_sequence(def_conf);
3191 assoc = get_defcfg_association(def_conf);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02003192 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
3193 continue;
3194 cfg->hp_pins[cfg->hp_outs] = nid;
Takashi Iwaif889fa92007-10-31 15:49:32 +01003195 sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
Takashi Iwaieb06ed82006-09-20 17:10:27 +02003196 cfg->hp_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003197 break;
Takashi Iwai314634b2006-09-21 11:56:18 +02003198 case AC_JACK_MIC_IN: {
3199 int preferred, alt;
3200 if (loc == AC_JACK_LOC_FRONT) {
3201 preferred = AUTO_PIN_FRONT_MIC;
3202 alt = AUTO_PIN_MIC;
3203 } else {
3204 preferred = AUTO_PIN_MIC;
3205 alt = AUTO_PIN_FRONT_MIC;
3206 }
3207 if (!cfg->input_pins[preferred])
3208 cfg->input_pins[preferred] = nid;
3209 else if (!cfg->input_pins[alt])
3210 cfg->input_pins[alt] = nid;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003211 break;
Takashi Iwai314634b2006-09-21 11:56:18 +02003212 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003213 case AC_JACK_LINE_IN:
3214 if (loc == AC_JACK_LOC_FRONT)
3215 cfg->input_pins[AUTO_PIN_FRONT_LINE] = nid;
3216 else
3217 cfg->input_pins[AUTO_PIN_LINE] = nid;
3218 break;
3219 case AC_JACK_CD:
3220 cfg->input_pins[AUTO_PIN_CD] = nid;
3221 break;
3222 case AC_JACK_AUX:
3223 cfg->input_pins[AUTO_PIN_AUX] = nid;
3224 break;
3225 case AC_JACK_SPDIF_OUT:
3226 cfg->dig_out_pin = nid;
3227 break;
3228 case AC_JACK_SPDIF_IN:
3229 cfg->dig_in_pin = nid;
3230 break;
3231 }
3232 }
3233
Takashi Iwai5832fcf2008-02-12 18:30:12 +01003234 /* FIX-UP:
3235 * If no line-out is defined but multiple HPs are found,
3236 * some of them might be the real line-outs.
3237 */
3238 if (!cfg->line_outs && cfg->hp_outs > 1) {
3239 int i = 0;
3240 while (i < cfg->hp_outs) {
3241 /* The real HPs should have the sequence 0x0f */
3242 if ((sequences_hp[i] & 0x0f) == 0x0f) {
3243 i++;
3244 continue;
3245 }
3246 /* Move it to the line-out table */
3247 cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
3248 sequences_line_out[cfg->line_outs] = sequences_hp[i];
3249 cfg->line_outs++;
3250 cfg->hp_outs--;
3251 memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
3252 sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
3253 memmove(sequences_hp + i - 1, sequences_hp + i,
3254 sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
3255 }
3256 }
3257
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003258 /* sort by sequence */
Steve Longerbeam81937d32007-05-08 15:33:03 +02003259 sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
3260 cfg->line_outs);
3261 sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
3262 cfg->speaker_outs);
Takashi Iwaif889fa92007-10-31 15:49:32 +01003263 sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
3264 cfg->hp_outs);
Steve Longerbeam81937d32007-05-08 15:33:03 +02003265
Takashi Iwaif889fa92007-10-31 15:49:32 +01003266 /* if we have only one mic, make it AUTO_PIN_MIC */
3267 if (!cfg->input_pins[AUTO_PIN_MIC] &&
3268 cfg->input_pins[AUTO_PIN_FRONT_MIC]) {
3269 cfg->input_pins[AUTO_PIN_MIC] =
3270 cfg->input_pins[AUTO_PIN_FRONT_MIC];
3271 cfg->input_pins[AUTO_PIN_FRONT_MIC] = 0;
3272 }
3273 /* ditto for line-in */
3274 if (!cfg->input_pins[AUTO_PIN_LINE] &&
3275 cfg->input_pins[AUTO_PIN_FRONT_LINE]) {
3276 cfg->input_pins[AUTO_PIN_LINE] =
3277 cfg->input_pins[AUTO_PIN_FRONT_LINE];
3278 cfg->input_pins[AUTO_PIN_FRONT_LINE] = 0;
3279 }
3280
Steve Longerbeam81937d32007-05-08 15:33:03 +02003281 /*
3282 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
3283 * as a primary output
3284 */
3285 if (!cfg->line_outs) {
3286 if (cfg->speaker_outs) {
3287 cfg->line_outs = cfg->speaker_outs;
3288 memcpy(cfg->line_out_pins, cfg->speaker_pins,
3289 sizeof(cfg->speaker_pins));
3290 cfg->speaker_outs = 0;
3291 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
3292 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
3293 } else if (cfg->hp_outs) {
3294 cfg->line_outs = cfg->hp_outs;
3295 memcpy(cfg->line_out_pins, cfg->hp_pins,
3296 sizeof(cfg->hp_pins));
3297 cfg->hp_outs = 0;
3298 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
3299 cfg->line_out_type = AUTO_PIN_HP_OUT;
3300 }
3301 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003302
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02003303 /* Reorder the surround channels
3304 * ALSA sequence is front/surr/clfe/side
3305 * HDA sequence is:
3306 * 4-ch: front/surr => OK as it is
3307 * 6-ch: front/clfe/surr
Takashi Iwai9422db42007-04-20 16:11:43 +02003308 * 8-ch: front/clfe/rear/side|fc
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02003309 */
3310 switch (cfg->line_outs) {
3311 case 3:
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02003312 case 4:
3313 nid = cfg->line_out_pins[1];
Takashi Iwai9422db42007-04-20 16:11:43 +02003314 cfg->line_out_pins[1] = cfg->line_out_pins[2];
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02003315 cfg->line_out_pins[2] = nid;
3316 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003317 }
3318
Takashi Iwai82bc9552006-03-21 11:24:42 +01003319 /*
3320 * debug prints of the parsed results
3321 */
3322 snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3323 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
3324 cfg->line_out_pins[2], cfg->line_out_pins[3],
3325 cfg->line_out_pins[4]);
3326 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3327 cfg->speaker_outs, cfg->speaker_pins[0],
3328 cfg->speaker_pins[1], cfg->speaker_pins[2],
3329 cfg->speaker_pins[3], cfg->speaker_pins[4]);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02003330 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3331 cfg->hp_outs, cfg->hp_pins[0],
3332 cfg->hp_pins[1], cfg->hp_pins[2],
3333 cfg->hp_pins[3], cfg->hp_pins[4]);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01003334 snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
Takashi Iwai82bc9552006-03-21 11:24:42 +01003335 snd_printd(" inputs: mic=0x%x, fmic=0x%x, line=0x%x, fline=0x%x,"
3336 " cd=0x%x, aux=0x%x\n",
3337 cfg->input_pins[AUTO_PIN_MIC],
3338 cfg->input_pins[AUTO_PIN_FRONT_MIC],
3339 cfg->input_pins[AUTO_PIN_LINE],
3340 cfg->input_pins[AUTO_PIN_FRONT_LINE],
3341 cfg->input_pins[AUTO_PIN_CD],
3342 cfg->input_pins[AUTO_PIN_AUX]);
3343
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003344 return 0;
3345}
3346
Takashi Iwai4a471b72005-12-07 13:56:29 +01003347/* labels for input pins */
3348const char *auto_pin_cfg_labels[AUTO_PIN_LAST] = {
3349 "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux"
3350};
3351
3352
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353#ifdef CONFIG_PM
3354/*
3355 * power management
3356 */
3357
3358/**
3359 * snd_hda_suspend - suspend the codecs
3360 * @bus: the HDA bus
3361 * @state: suspsend state
3362 *
3363 * Returns 0 if successful.
3364 */
3365int snd_hda_suspend(struct hda_bus *bus, pm_message_t state)
3366{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003367 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368
Takashi Iwai0ba21762007-04-16 11:29:14 +02003369 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai0b7a2e92007-08-14 15:18:26 +02003370#ifdef CONFIG_SND_HDA_POWER_SAVE
3371 if (!codec->power_on)
3372 continue;
3373#endif
Takashi Iwaicb53c622007-08-10 17:21:45 +02003374 hda_call_codec_suspend(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 }
3376 return 0;
3377}
3378
3379/**
3380 * snd_hda_resume - resume the codecs
3381 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 *
3383 * Returns 0 if successful.
Takashi Iwaicb53c622007-08-10 17:21:45 +02003384 *
3385 * This fucntion is defined only when POWER_SAVE isn't set.
3386 * In the power-save mode, the codec is resumed dynamically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 */
3388int snd_hda_resume(struct hda_bus *bus)
3389{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003390 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391
Takashi Iwai0ba21762007-04-16 11:29:14 +02003392 list_for_each_entry(codec, &bus->codec_list, list) {
Maxim Levitskyd804ad92007-09-03 15:28:04 +02003393 if (snd_hda_codec_needs_resume(codec))
3394 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 return 0;
3397}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398#endif
Takashi Iwaib2e18592008-07-30 15:01:44 +02003399
3400/*
3401 * generic arrays
3402 */
3403
3404/* get a new element from the given array
3405 * if it exceeds the pre-allocated array size, re-allocate the array
3406 */
3407void *snd_array_new(struct snd_array *array)
3408{
3409 if (array->used >= array->alloced) {
3410 int num = array->alloced + array->alloc_align;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01003411 void *nlist;
3412 if (snd_BUG_ON(num >= 4096))
3413 return NULL;
3414 nlist = kcalloc(num + 1, array->elem_size, GFP_KERNEL);
Takashi Iwaib2e18592008-07-30 15:01:44 +02003415 if (!nlist)
3416 return NULL;
3417 if (array->list) {
3418 memcpy(nlist, array->list,
3419 array->elem_size * array->alloced);
3420 kfree(array->list);
3421 }
3422 array->list = nlist;
3423 array->alloced = num;
3424 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01003425 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02003426}
3427
3428/* free the given array elements */
3429void snd_array_free(struct snd_array *array)
3430{
3431 kfree(array->list);
3432 array->used = 0;
3433 array->alloced = 0;
3434 array->list = NULL;
3435}
Takashi Iwaib2022262008-11-21 21:24:03 +01003436
3437/*
3438 * used by hda_proc.c and hda_eld.c
3439 */
3440void snd_print_pcm_rates(int pcm, char *buf, int buflen)
3441{
3442 static unsigned int rates[] = {
3443 8000, 11025, 16000, 22050, 32000, 44100, 48000, 88200,
3444 96000, 176400, 192000, 384000
3445 };
3446 int i, j;
3447
3448 for (i = 0, j = 0; i < ARRAY_SIZE(rates); i++)
3449 if (pcm & (1 << i))
3450 j += snprintf(buf + j, buflen - j, " %d", rates[i]);
3451
3452 buf[j] = '\0'; /* necessary when j == 0 */
3453}
3454
3455void snd_print_pcm_bits(int pcm, char *buf, int buflen)
3456{
3457 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
3458 int i, j;
3459
3460 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
3461 if (pcm & (AC_SUPPCM_BITS_8 << i))
3462 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
3463
3464 buf[j] = '\0'; /* necessary when j == 0 */
3465}