blob: e16cf63821aeab8e46edf1921d5ae094f7f55ff9 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Linus Torvalds1da177e2005-04-16 15:20:36 -070035/*
36 * vendor / preset table
37 */
38
39struct hda_vendor_id {
40 unsigned int id;
41 const char *name;
42};
43
44/* codec vendor labels */
45static struct hda_vendor_id hda_vendor_ids[] = {
Takashi Iwaic8cd1282008-02-13 16:59:29 +010046 { 0x1002, "ATI" },
Takashi Iwaia9226252006-09-17 22:05:54 +020047 { 0x1057, "Motorola" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010048 { 0x1095, "Silicon Image" },
Takashi Iwai31117b72008-12-16 14:43:21 +010049 { 0x10de, "Nvidia" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010050 { 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" },
Takashi Iwai74c61132008-12-18 09:11:33 +010061 { 0x8086, "Intel" },
Matt2f2f4252005-04-13 14:45:30 +020062 { 0x8384, "SigmaTel" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 {} /* terminator */
64};
65
Takashi Iwai1289e9e2008-11-27 15:47:11 +010066static DEFINE_MUTEX(preset_mutex);
67static LIST_HEAD(hda_preset_tables);
68
69int snd_hda_add_codec_preset(struct hda_codec_preset_list *preset)
70{
71 mutex_lock(&preset_mutex);
72 list_add_tail(&preset->list, &hda_preset_tables);
73 mutex_unlock(&preset_mutex);
74 return 0;
75}
Takashi Iwaiff7a3262008-11-28 15:17:06 +010076EXPORT_SYMBOL_HDA(snd_hda_add_codec_preset);
Takashi Iwai1289e9e2008-11-27 15:47:11 +010077
78int snd_hda_delete_codec_preset(struct hda_codec_preset_list *preset)
79{
80 mutex_lock(&preset_mutex);
81 list_del(&preset->list);
82 mutex_unlock(&preset_mutex);
83 return 0;
84}
Takashi Iwaiff7a3262008-11-28 15:17:06 +010085EXPORT_SYMBOL_HDA(snd_hda_delete_codec_preset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Takashi Iwaicb53c622007-08-10 17:21:45 +020087#ifdef CONFIG_SND_HDA_POWER_SAVE
88static void hda_power_work(struct work_struct *work);
89static void hda_keep_power_on(struct hda_codec *codec);
90#else
91static inline void hda_keep_power_on(struct hda_codec *codec) {}
92#endif
93
Matthew Ranostay50a9f792008-10-25 01:05:45 -040094const char *snd_hda_get_jack_location(u32 cfg)
95{
96 static char *bases[7] = {
97 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
98 };
99 static unsigned char specials_idx[] = {
100 0x07, 0x08,
101 0x17, 0x18, 0x19,
102 0x37, 0x38
103 };
104 static char *specials[] = {
105 "Rear Panel", "Drive Bar",
106 "Riser", "HDMI", "ATAPI",
107 "Mobile-In", "Mobile-Out"
108 };
109 int i;
110 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
111 if ((cfg & 0x0f) < 7)
112 return bases[cfg & 0x0f];
113 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
114 if (cfg == specials_idx[i])
115 return specials[i];
116 }
117 return "UNKNOWN";
118}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100119EXPORT_SYMBOL_HDA(snd_hda_get_jack_location);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400120
121const char *snd_hda_get_jack_connectivity(u32 cfg)
122{
123 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
124
125 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
126}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100127EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400128
129const char *snd_hda_get_jack_type(u32 cfg)
130{
131 static char *jack_types[16] = {
132 "Line Out", "Speaker", "HP Out", "CD",
133 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
134 "Line In", "Aux", "Mic", "Telephony",
135 "SPDIF In", "Digitial In", "Reserved", "Other"
136 };
137
138 return jack_types[(cfg & AC_DEFCFG_DEVICE)
139 >> AC_DEFCFG_DEVICE_SHIFT];
140}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100141EXPORT_SYMBOL_HDA(snd_hda_get_jack_type);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400142
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100143/*
144 * Compose a 32bit command word to be sent to the HD-audio controller
145 */
146static inline unsigned int
147make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int direct,
148 unsigned int verb, unsigned int parm)
149{
150 u32 val;
151
152 val = (u32)(codec->addr & 0x0f) << 28;
153 val |= (u32)direct << 27;
154 val |= (u32)nid << 20;
155 val |= verb << 8;
156 val |= parm;
157 return val;
158}
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160/**
161 * snd_hda_codec_read - send a command and get the response
162 * @codec: the HDA codec
163 * @nid: NID to send the command
164 * @direct: direct flag
165 * @verb: the verb to send
166 * @parm: the parameter for the verb
167 *
168 * Send a single command and read the corresponding response.
169 *
170 * Returns the obtained response value, or -1 for an error.
171 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200172unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
173 int direct,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 unsigned int verb, unsigned int parm)
175{
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100176 struct hda_bus *bus = codec->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 unsigned int res;
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100178
179 res = make_codec_cmd(codec, nid, direct, verb, parm);
Takashi Iwaicb53c622007-08-10 17:21:45 +0200180 snd_hda_power_up(codec);
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100181 mutex_lock(&bus->cmd_mutex);
182 if (!bus->ops.command(bus, res))
183 res = bus->ops.get_response(bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 else
185 res = (unsigned int)-1;
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100186 mutex_unlock(&bus->cmd_mutex);
Takashi Iwaicb53c622007-08-10 17:21:45 +0200187 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 return res;
189}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100190EXPORT_SYMBOL_HDA(snd_hda_codec_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192/**
193 * snd_hda_codec_write - send a single command without waiting for response
194 * @codec: the HDA codec
195 * @nid: NID to send the command
196 * @direct: direct flag
197 * @verb: the verb to send
198 * @parm: the parameter for the verb
199 *
200 * Send a single command without waiting for response.
201 *
202 * Returns 0 if successful, or a negative error code.
203 */
204int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
205 unsigned int verb, unsigned int parm)
206{
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100207 struct hda_bus *bus = codec->bus;
208 unsigned int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 int err;
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100210
211 res = make_codec_cmd(codec, nid, direct, verb, parm);
Takashi Iwaicb53c622007-08-10 17:21:45 +0200212 snd_hda_power_up(codec);
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100213 mutex_lock(&bus->cmd_mutex);
214 err = bus->ops.command(bus, res);
215 mutex_unlock(&bus->cmd_mutex);
Takashi Iwaicb53c622007-08-10 17:21:45 +0200216 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 return err;
218}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100219EXPORT_SYMBOL_HDA(snd_hda_codec_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221/**
222 * snd_hda_sequence_write - sequence writes
223 * @codec: the HDA codec
224 * @seq: VERB array to send
225 *
226 * Send the commands sequentially from the given array.
227 * The array must be terminated with NID=0.
228 */
229void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
230{
231 for (; seq->nid; seq++)
232 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
233}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100234EXPORT_SYMBOL_HDA(snd_hda_sequence_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236/**
237 * snd_hda_get_sub_nodes - get the range of sub nodes
238 * @codec: the HDA codec
239 * @nid: NID to parse
240 * @start_id: the pointer to store the start NID
241 *
242 * Parse the NID and store the start NID of its sub-nodes.
243 * Returns the number of sub-nodes.
244 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200245int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
246 hda_nid_t *start_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
248 unsigned int parm;
249
250 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
Danny Tholene8a7f132007-09-11 21:41:56 +0200251 if (parm == -1)
252 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 *start_id = (parm >> 16) & 0x7fff;
254 return (int)(parm & 0x7fff);
255}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100256EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258/**
259 * snd_hda_get_connections - get connection list
260 * @codec: the HDA codec
261 * @nid: NID to parse
262 * @conn_list: connection list array
263 * @max_conns: max. number of connections to store
264 *
265 * Parses the connection list of the given widget and stores the list
266 * of NIDs.
267 *
268 * Returns the number of connections, or a negative error code.
269 */
270int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
271 hda_nid_t *conn_list, int max_conns)
272{
273 unsigned int parm;
Takashi Iwai54d17402005-11-21 16:33:22 +0100274 int i, conn_len, conns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 unsigned int shift, num_elems, mask;
Takashi Iwai54d17402005-11-21 16:33:22 +0100276 hda_nid_t prev_nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Takashi Iwaida3cec32008-08-08 17:12:14 +0200278 if (snd_BUG_ON(!conn_list || max_conns <= 0))
279 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
282 if (parm & AC_CLIST_LONG) {
283 /* long form */
284 shift = 16;
285 num_elems = 2;
286 } else {
287 /* short form */
288 shift = 8;
289 num_elems = 4;
290 }
291 conn_len = parm & AC_CLIST_LENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 mask = (1 << (shift-1)) - 1;
293
Takashi Iwai0ba21762007-04-16 11:29:14 +0200294 if (!conn_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 return 0; /* no connection */
296
297 if (conn_len == 1) {
298 /* single connection */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200299 parm = snd_hda_codec_read(codec, nid, 0,
300 AC_VERB_GET_CONNECT_LIST, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 conn_list[0] = parm & mask;
302 return 1;
303 }
304
305 /* multi connection */
306 conns = 0;
Takashi Iwai54d17402005-11-21 16:33:22 +0100307 prev_nid = 0;
308 for (i = 0; i < conn_len; i++) {
309 int range_val;
310 hda_nid_t val, n;
311
312 if (i % num_elems == 0)
313 parm = snd_hda_codec_read(codec, nid, 0,
314 AC_VERB_GET_CONNECT_LIST, i);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200315 range_val = !!(parm & (1 << (shift-1))); /* ranges */
Takashi Iwai54d17402005-11-21 16:33:22 +0100316 val = parm & mask;
317 parm >>= shift;
318 if (range_val) {
319 /* ranges between the previous and this one */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200320 if (!prev_nid || prev_nid >= val) {
321 snd_printk(KERN_WARNING "hda_codec: "
322 "invalid dep_range_val %x:%x\n",
323 prev_nid, val);
Takashi Iwai54d17402005-11-21 16:33:22 +0100324 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100326 for (n = prev_nid + 1; n <= val; n++) {
327 if (conns >= max_conns) {
Takashi Iwai0ba21762007-04-16 11:29:14 +0200328 snd_printk(KERN_ERR
329 "Too many connections\n");
Takashi Iwai54d17402005-11-21 16:33:22 +0100330 return -EINVAL;
331 }
332 conn_list[conns++] = n;
333 }
334 } else {
335 if (conns >= max_conns) {
336 snd_printk(KERN_ERR "Too many connections\n");
337 return -EINVAL;
338 }
339 conn_list[conns++] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100341 prev_nid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 }
343 return conns;
344}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100345EXPORT_SYMBOL_HDA(snd_hda_get_connections);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347
348/**
349 * snd_hda_queue_unsol_event - add an unsolicited event to queue
350 * @bus: the BUS
351 * @res: unsolicited event (lower 32bit of RIRB entry)
352 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
353 *
354 * Adds the given event to the queue. The events are processed in
355 * the workqueue asynchronously. Call this function in the interrupt
356 * hanlder when RIRB receives an unsolicited event.
357 *
358 * Returns 0 if successful, or a negative error code.
359 */
360int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
361{
362 struct hda_bus_unsolicited *unsol;
363 unsigned int wp;
364
Takashi Iwai0ba21762007-04-16 11:29:14 +0200365 unsol = bus->unsol;
366 if (!unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 return 0;
368
369 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
370 unsol->wp = wp;
371
372 wp <<= 1;
373 unsol->queue[wp] = res;
374 unsol->queue[wp + 1] = res_ex;
375
Takashi Iwaie250af22006-12-19 17:08:52 +0100376 schedule_work(&unsol->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
378 return 0;
379}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100380EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
382/*
Wu Fengguang5c1d1a92008-10-07 14:17:53 +0800383 * process queued unsolicited events
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 */
David Howellsc4028952006-11-22 14:57:56 +0000385static void process_unsol_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
David Howellsc4028952006-11-22 14:57:56 +0000387 struct hda_bus_unsolicited *unsol =
388 container_of(work, struct hda_bus_unsolicited, work);
389 struct hda_bus *bus = unsol->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 struct hda_codec *codec;
391 unsigned int rp, caddr, res;
392
393 while (unsol->rp != unsol->wp) {
394 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
395 unsol->rp = rp;
396 rp <<= 1;
397 res = unsol->queue[rp];
398 caddr = unsol->queue[rp + 1];
Takashi Iwai0ba21762007-04-16 11:29:14 +0200399 if (!(caddr & (1 << 4))) /* no unsolicited event? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 continue;
401 codec = bus->caddr_tbl[caddr & 0x0f];
402 if (codec && codec->patch_ops.unsol_event)
403 codec->patch_ops.unsol_event(codec, res);
404 }
405}
406
407/*
408 * initialize unsolicited queue
409 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200410static int init_unsol_queue(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411{
412 struct hda_bus_unsolicited *unsol;
413
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100414 if (bus->unsol) /* already initialized */
415 return 0;
416
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200417 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200418 if (!unsol) {
419 snd_printk(KERN_ERR "hda_codec: "
420 "can't allocate unsolicited queue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 return -ENOMEM;
422 }
David Howellsc4028952006-11-22 14:57:56 +0000423 INIT_WORK(&unsol->work, process_unsol_events);
424 unsol->bus = bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 bus->unsol = unsol;
426 return 0;
427}
428
429/*
430 * destructor
431 */
432static void snd_hda_codec_free(struct hda_codec *codec);
433
434static int snd_hda_bus_free(struct hda_bus *bus)
435{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200436 struct hda_codec *codec, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Takashi Iwai0ba21762007-04-16 11:29:14 +0200438 if (!bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 return 0;
440 if (bus->unsol) {
Takashi Iwaie250af22006-12-19 17:08:52 +0100441 flush_scheduled_work();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 kfree(bus->unsol);
443 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200444 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 snd_hda_codec_free(codec);
446 }
447 if (bus->ops.private_free)
448 bus->ops.private_free(bus);
449 kfree(bus);
450 return 0;
451}
452
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100453static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
455 struct hda_bus *bus = device->device_data;
Takashi Iwaib94d35392008-11-21 09:08:06 +0100456 bus->shutdown = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 return snd_hda_bus_free(bus);
458}
459
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200460#ifdef CONFIG_SND_HDA_HWDEP
461static int snd_hda_bus_dev_register(struct snd_device *device)
462{
463 struct hda_bus *bus = device->device_data;
464 struct hda_codec *codec;
465 list_for_each_entry(codec, &bus->codec_list, list) {
466 snd_hda_hwdep_add_sysfs(codec);
467 }
468 return 0;
469}
470#else
471#define snd_hda_bus_dev_register NULL
472#endif
473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474/**
475 * snd_hda_bus_new - create a HDA bus
476 * @card: the card entry
477 * @temp: the template for hda_bus information
478 * @busp: the pointer to store the created bus instance
479 *
480 * Returns 0 if successful, or a negative error code.
481 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100482int /*__devinit*/ snd_hda_bus_new(struct snd_card *card,
Takashi Iwai756e2b02007-04-16 11:27:07 +0200483 const struct hda_bus_template *temp,
484 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
486 struct hda_bus *bus;
487 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100488 static struct snd_device_ops dev_ops = {
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200489 .dev_register = snd_hda_bus_dev_register,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 .dev_free = snd_hda_bus_dev_free,
491 };
492
Takashi Iwaida3cec32008-08-08 17:12:14 +0200493 if (snd_BUG_ON(!temp))
494 return -EINVAL;
495 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
496 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498 if (busp)
499 *busp = NULL;
500
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200501 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 if (bus == NULL) {
503 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
504 return -ENOMEM;
505 }
506
507 bus->card = card;
508 bus->private_data = temp->private_data;
509 bus->pci = temp->pci;
510 bus->modelname = temp->modelname;
Takashi Iwaifee2fba2008-11-27 12:43:28 +0100511 bus->power_save = temp->power_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 bus->ops = temp->ops;
513
Ingo Molnar62932df2006-01-16 16:34:20 +0100514 mutex_init(&bus->cmd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 INIT_LIST_HEAD(&bus->codec_list);
516
Takashi Iwai0ba21762007-04-16 11:29:14 +0200517 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
518 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 snd_hda_bus_free(bus);
520 return err;
521 }
522 if (busp)
523 *busp = bus;
524 return 0;
525}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100526EXPORT_SYMBOL_HDA(snd_hda_bus_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Takashi Iwai82467612007-07-27 19:15:54 +0200528#ifdef CONFIG_SND_HDA_GENERIC
529#define is_generic_config(codec) \
Takashi Iwaif44ac832008-07-30 15:01:45 +0200530 (codec->modelname && !strcmp(codec->modelname, "generic"))
Takashi Iwai82467612007-07-27 19:15:54 +0200531#else
532#define is_generic_config(codec) 0
533#endif
534
Takashi Iwai645f10c2008-11-28 15:07:37 +0100535#ifdef MODULE
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100536#define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
537#else
Takashi Iwai645f10c2008-11-28 15:07:37 +0100538#define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100539#endif
540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541/*
542 * find a matching codec preset
543 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200544static const struct hda_codec_preset *
Takashi Iwai756e2b02007-04-16 11:27:07 +0200545find_codec_preset(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100547 struct hda_codec_preset_list *tbl;
548 const struct hda_codec_preset *preset;
549 int mod_requested = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Takashi Iwai82467612007-07-27 19:15:54 +0200551 if (is_generic_config(codec))
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100552 return NULL; /* use the generic parser */
553
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100554 again:
555 mutex_lock(&preset_mutex);
556 list_for_each_entry(tbl, &hda_preset_tables, list) {
557 if (!try_module_get(tbl->owner)) {
558 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
559 continue;
560 }
561 for (preset = tbl->preset; preset->id; preset++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 u32 mask = preset->mask;
Marc Boucherca7cfae2008-01-22 15:32:25 +0100563 if (preset->afg && preset->afg != codec->afg)
564 continue;
565 if (preset->mfg && preset->mfg != codec->mfg)
566 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200567 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 mask = ~0;
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200569 if (preset->id == (codec->vendor_id & mask) &&
Takashi Iwai0ba21762007-04-16 11:29:14 +0200570 (!preset->rev ||
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100571 preset->rev == codec->revision_id)) {
572 mutex_unlock(&preset_mutex);
573 codec->owner = tbl->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 return preset;
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100575 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 }
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100577 module_put(tbl->owner);
578 }
579 mutex_unlock(&preset_mutex);
580
581 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
582 char name[32];
583 if (!mod_requested)
584 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
585 codec->vendor_id);
586 else
587 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
588 (codec->vendor_id >> 16) & 0xffff);
589 request_module(name);
590 mod_requested++;
591 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 }
593 return NULL;
594}
595
596/*
Takashi Iwaif44ac832008-07-30 15:01:45 +0200597 * get_codec_name - store the codec name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 */
Takashi Iwaif44ac832008-07-30 15:01:45 +0200599static int get_codec_name(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600{
601 const struct hda_vendor_id *c;
602 const char *vendor = NULL;
603 u16 vendor_id = codec->vendor_id >> 16;
Takashi Iwaif44ac832008-07-30 15:01:45 +0200604 char tmp[16], name[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606 for (c = hda_vendor_ids; c->id; c++) {
607 if (c->id == vendor_id) {
608 vendor = c->name;
609 break;
610 }
611 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200612 if (!vendor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 sprintf(tmp, "Generic %04x", vendor_id);
614 vendor = tmp;
615 }
616 if (codec->preset && codec->preset->name)
Takashi Iwaif44ac832008-07-30 15:01:45 +0200617 snprintf(name, sizeof(name), "%s %s", vendor,
618 codec->preset->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 else
Takashi Iwaif44ac832008-07-30 15:01:45 +0200620 snprintf(name, sizeof(name), "%s ID %x", vendor,
Takashi Iwai0ba21762007-04-16 11:29:14 +0200621 codec->vendor_id & 0xffff);
Takashi Iwaif44ac832008-07-30 15:01:45 +0200622 codec->name = kstrdup(name, GFP_KERNEL);
623 if (!codec->name)
624 return -ENOMEM;
625 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626}
627
628/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200629 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100631static void /*__devinit*/ setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
633 int i, total_nodes;
634 hda_nid_t nid;
635
636 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
637 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +0200638 unsigned int func;
639 func = snd_hda_param_read(codec, nid, AC_PAR_FUNCTION_TYPE);
640 switch (func & 0xff) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200641 case AC_GRP_AUDIO_FUNCTION:
642 codec->afg = nid;
643 break;
644 case AC_GRP_MODEM_FUNCTION:
645 codec->mfg = nid;
646 break;
647 default:
648 break;
649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651}
652
653/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100654 * read widget caps for each widget and store in cache
655 */
656static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
657{
658 int i;
659 hda_nid_t nid;
660
661 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
662 &codec->start_nid);
663 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200664 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +0100665 return -ENOMEM;
666 nid = codec->start_nid;
667 for (i = 0; i < codec->num_nodes; i++, nid++)
668 codec->wcaps[i] = snd_hda_param_read(codec, nid,
669 AC_PAR_AUDIO_WIDGET_CAP);
670 return 0;
671}
672
673
Takashi Iwai01751f52007-08-10 16:59:39 +0200674static void init_hda_cache(struct hda_cache_rec *cache,
675 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +0200676static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +0200677
Takashi Iwai54d17402005-11-21 16:33:22 +0100678/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 * codec destructor
680 */
681static void snd_hda_codec_free(struct hda_codec *codec)
682{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200683 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 return;
Takashi Iwaicb53c622007-08-10 17:21:45 +0200685#ifdef CONFIG_SND_HDA_POWER_SAVE
686 cancel_delayed_work(&codec->power_work);
Takashi Iwai2525fdc2007-08-15 22:18:22 +0200687 flush_scheduled_work();
Takashi Iwaicb53c622007-08-10 17:21:45 +0200688#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +0200690 snd_array_free(&codec->mixers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 codec->bus->caddr_tbl[codec->addr] = NULL;
692 if (codec->patch_ops.free)
693 codec->patch_ops.free(codec);
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100694 module_put(codec->owner);
Takashi Iwai01751f52007-08-10 16:59:39 +0200695 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +0200696 free_hda_cache(&codec->cmd_cache);
Takashi Iwaif44ac832008-07-30 15:01:45 +0200697 kfree(codec->name);
698 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +0100699 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 kfree(codec);
701}
702
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703/**
704 * snd_hda_codec_new - create a HDA codec
705 * @bus: the bus to assign
706 * @codec_addr: the codec address
707 * @codecp: the pointer to store the generated codec
708 *
709 * Returns 0 if successful, or a negative error code.
710 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100711int /*__devinit*/ snd_hda_codec_new(struct hda_bus *bus, unsigned int codec_addr,
Takashi Iwaid4d9cd032008-12-19 15:19:11 +0100712 int do_init, struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
714 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +0200715 char component[31];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 int err;
717
Takashi Iwaida3cec32008-08-08 17:12:14 +0200718 if (snd_BUG_ON(!bus))
719 return -EINVAL;
720 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
721 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +0200724 snd_printk(KERN_ERR "hda_codec: "
725 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 return -EBUSY;
727 }
728
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200729 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 if (codec == NULL) {
731 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
732 return -ENOMEM;
733 }
734
735 codec->bus = bus;
736 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +0100737 mutex_init(&codec->spdif_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +0200738 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +0200739 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwaid13bd412008-07-30 15:01:45 +0200740 snd_array_init(&codec->mixers, sizeof(struct snd_kcontrol *), 32);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200741 if (codec->bus->modelname) {
742 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
743 if (!codec->modelname) {
744 snd_hda_codec_free(codec);
745 return -ENODEV;
746 }
747 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
Takashi Iwaicb53c622007-08-10 17:21:45 +0200749#ifdef CONFIG_SND_HDA_POWER_SAVE
750 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
751 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
752 * the caller has to power down appropriatley after initialization
753 * phase.
754 */
755 hda_keep_power_on(codec);
756#endif
757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 list_add_tail(&codec->list, &bus->codec_list);
759 bus->caddr_tbl[codec_addr] = codec;
760
Takashi Iwai0ba21762007-04-16 11:29:14 +0200761 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
762 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +0100763 if (codec->vendor_id == -1)
764 /* read again, hopefully the access method was corrected
765 * in the last read...
766 */
767 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
768 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200769 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
770 AC_PAR_SUBSYSTEM_ID);
771 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
772 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200774 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200775 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200776 snd_printdd("hda_codec: no AFG or MFG node found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 snd_hda_codec_free(codec);
778 return -ENODEV;
779 }
780
Takashi Iwai54d17402005-11-21 16:33:22 +0100781 if (read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg) < 0) {
782 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
783 snd_hda_codec_free(codec);
784 return -ENOMEM;
785 }
786
Takashi Iwai0ba21762007-04-16 11:29:14 +0200787 if (!codec->subsystem_id) {
Takashi Iwai86284e42005-10-11 15:05:54 +0200788 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200789 codec->subsystem_id =
790 snd_hda_codec_read(codec, nid, 0,
791 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +0200792 }
Takashi Iwaif44ac832008-07-30 15:01:45 +0200793 if (bus->modelname)
794 codec->modelname = kstrdup(bus->modelname, GFP_KERNEL);
Takashi Iwai86284e42005-10-11 15:05:54 +0200795
Takashi Iwaid4d9cd032008-12-19 15:19:11 +0100796 if (do_init) {
797 err = snd_hda_codec_configure(codec);
798 if (err < 0) {
799 snd_hda_codec_free(codec);
800 return err;
801 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200802 }
803 snd_hda_codec_proc_new(codec);
804
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200805 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200806
807 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
808 codec->subsystem_id, codec->revision_id);
809 snd_component_add(codec->bus->card, component);
810
811 if (codecp)
812 *codecp = codec;
813 return 0;
814}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100815EXPORT_SYMBOL_HDA(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200816
817int snd_hda_codec_configure(struct hda_codec *codec)
818{
819 int err;
820
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100821 codec->preset = find_codec_preset(codec);
Takashi Iwaif44ac832008-07-30 15:01:45 +0200822 if (!codec->name) {
823 err = get_codec_name(codec);
824 if (err < 0)
825 return err;
826 }
Takashi Iwai43ea1d42007-04-26 19:12:08 +0200827 /* audio codec should override the mixer name */
Takashi Iwaif44ac832008-07-30 15:01:45 +0200828 if (codec->afg || !*codec->bus->card->mixername)
829 strlcpy(codec->bus->card->mixername, codec->name,
830 sizeof(codec->bus->card->mixername));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
Takashi Iwai82467612007-07-27 19:15:54 +0200832 if (is_generic_config(codec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai82467612007-07-27 19:15:54 +0200834 goto patched;
835 }
Takashi Iwai82467612007-07-27 19:15:54 +0200836 if (codec->preset && codec->preset->patch) {
837 err = codec->preset->patch(codec);
838 goto patched;
839 }
840
841 /* call the default parser */
Takashi Iwai82467612007-07-27 19:15:54 +0200842 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai35a1e0c2007-10-19 08:13:40 +0200843 if (err < 0)
844 printk(KERN_ERR "hda-codec: No codec parser is available\n");
Takashi Iwai82467612007-07-27 19:15:54 +0200845
846 patched:
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200847 if (!err && codec->patch_ops.unsol_event)
848 err = init_unsol_queue(codec->bus);
849 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850}
851
852/**
853 * snd_hda_codec_setup_stream - set up the codec for streaming
854 * @codec: the CODEC to set up
855 * @nid: the NID to set up
856 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
857 * @channel_id: channel id to pass, zero based.
858 * @format: stream format.
859 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200860void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
861 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 int channel_id, int format)
863{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200864 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +0200865 return;
866
Takashi Iwai0ba21762007-04-16 11:29:14 +0200867 snd_printdd("hda_codec_setup_stream: "
868 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 nid, stream_tag, channel_id, format);
870 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID,
871 (stream_tag << 4) | channel_id);
872 msleep(1);
873 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, format);
874}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100875EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
Takashi Iwai888afa12008-03-18 09:57:50 +0100877void snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid)
878{
879 if (!nid)
880 return;
881
882 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
883 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
884#if 0 /* keep the format */
885 msleep(1);
886 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0);
887#endif
888}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100889EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +0100890
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891/*
892 * amp access functions
893 */
894
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200895/* FIXME: more better hash key? */
896#define HDA_HASH_KEY(nid,dir,idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200898#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
900/* initialize the hash table */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100901static void /*__devinit*/ init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +0200902 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903{
Takashi Iwai01751f52007-08-10 16:59:39 +0200904 memset(cache, 0, sizeof(*cache));
905 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +0200906 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +0200907}
908
Takashi Iwai1fcaee62007-08-23 00:01:09 +0200909static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +0200910{
Takashi Iwai603c4012008-07-30 15:01:44 +0200911 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912}
913
914/* query the hash. allocate an entry if not found. */
Takashi Iwai01751f52007-08-10 16:59:39 +0200915static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
916 u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
Takashi Iwai01751f52007-08-10 16:59:39 +0200918 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
919 u16 cur = cache->hash[idx];
920 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
922 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +0100923 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 if (info->key == key)
925 return info;
926 cur = info->next;
927 }
928
929 /* add a new hash entry */
Takashi Iwai603c4012008-07-30 15:01:44 +0200930 info = snd_array_new(&cache->buf);
Takashi Iwaic2174292008-11-07 00:23:30 +0100931 if (!info)
932 return NULL;
Takashi Iwaif43aa022008-11-10 16:24:26 +0100933 cur = snd_array_index(&cache->buf, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 info->key = key;
Takashi Iwai01751f52007-08-10 16:59:39 +0200935 info->val = 0;
936 info->next = cache->hash[idx];
937 cache->hash[idx] = cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
939 return info;
940}
941
Takashi Iwai01751f52007-08-10 16:59:39 +0200942/* query and allocate an amp hash entry */
943static inline struct hda_amp_info *
944get_alloc_amp_hash(struct hda_codec *codec, u32 key)
945{
946 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
947}
948
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949/*
950 * query AMP capabilities for the given widget and direction
951 */
Matthew Ranostay09a99952008-01-24 11:49:21 +0100952u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200954 struct hda_amp_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
Takashi Iwai0ba21762007-04-16 11:29:14 +0200956 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
957 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 return 0;
Takashi Iwai01751f52007-08-10 16:59:39 +0200959 if (!(info->head.val & INFO_AMP_CAPS)) {
Takashi Iwai0ba21762007-04-16 11:29:14 +0200960 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 nid = codec->afg;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200962 info->amp_caps = snd_hda_param_read(codec, nid,
963 direction == HDA_OUTPUT ?
964 AC_PAR_AMP_OUT_CAP :
965 AC_PAR_AMP_IN_CAP);
Takashi Iwaib75e53f2007-05-10 16:56:09 +0200966 if (info->amp_caps)
Takashi Iwai01751f52007-08-10 16:59:39 +0200967 info->head.val |= INFO_AMP_CAPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 }
969 return info->amp_caps;
970}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100971EXPORT_SYMBOL_HDA(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
Takashi Iwai897cc182007-05-29 19:01:37 +0200973int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
974 unsigned int caps)
975{
976 struct hda_amp_info *info;
977
978 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
979 if (!info)
980 return -EINVAL;
981 info->amp_caps = caps;
Takashi Iwai01751f52007-08-10 16:59:39 +0200982 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai897cc182007-05-29 19:01:37 +0200983 return 0;
984}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100985EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +0200986
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987/*
988 * read the current volume to info
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200989 * if the cache exists, read the cache value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200991static unsigned int get_vol_mute(struct hda_codec *codec,
992 struct hda_amp_info *info, hda_nid_t nid,
993 int ch, int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994{
995 u32 val, parm;
996
Takashi Iwai01751f52007-08-10 16:59:39 +0200997 if (info->head.val & INFO_AMP_VOL(ch))
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200998 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
1000 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1001 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1002 parm |= index;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001003 val = snd_hda_codec_read(codec, nid, 0,
1004 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 info->vol[ch] = val & 0xff;
Takashi Iwai01751f52007-08-10 16:59:39 +02001006 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001007 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008}
1009
1010/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001011 * write the current volume in info to the h/w and update the cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001013static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001014 hda_nid_t nid, int ch, int direction, int index,
1015 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016{
1017 u32 parm;
1018
1019 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1020 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1021 parm |= index << AC_AMP_SET_INDEX_SHIFT;
1022 parm |= val;
1023 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001024 info->vol[ch] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025}
1026
1027/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001028 * read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 */
Takashi Iwai834be882006-03-01 14:16:17 +01001030int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1031 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001033 struct hda_amp_info *info;
1034 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1035 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001037 return get_vol_mute(codec, info, nid, ch, direction, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001039EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001041/*
1042 * update the AMP value, mask = bit mask to set, val = the value
1043 */
Takashi Iwai834be882006-03-01 14:16:17 +01001044int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1045 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001047 struct hda_amp_info *info;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001048
Takashi Iwai0ba21762007-04-16 11:29:14 +02001049 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
1050 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001052 val &= mask;
1053 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001054 if (info->vol[ch] == val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001056 put_vol_mute(codec, info, nid, ch, direction, idx, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 return 1;
1058}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001059EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
Takashi Iwai47fd8302007-08-10 17:11:07 +02001061/*
1062 * update the AMP stereo with the same mask and value
1063 */
1064int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1065 int direction, int idx, int mask, int val)
1066{
1067 int ch, ret = 0;
1068 for (ch = 0; ch < 2; ch++)
1069 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1070 idx, mask, val);
1071 return ret;
1072}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001073EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001074
Takashi Iwaicb53c622007-08-10 17:21:45 +02001075#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001076/* resume the all amp commands from the cache */
1077void snd_hda_codec_resume_amp(struct hda_codec *codec)
1078{
Takashi Iwai603c4012008-07-30 15:01:44 +02001079 struct hda_amp_info *buffer = codec->amp_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001080 int i;
1081
Takashi Iwai603c4012008-07-30 15:01:44 +02001082 for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001083 u32 key = buffer->head.key;
1084 hda_nid_t nid;
1085 unsigned int idx, dir, ch;
1086 if (!key)
1087 continue;
1088 nid = key & 0xff;
1089 idx = (key >> 16) & 0xff;
1090 dir = (key >> 24) & 0xff;
1091 for (ch = 0; ch < 2; ch++) {
1092 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
1093 continue;
1094 put_vol_mute(codec, buffer, nid, ch, dir, idx,
1095 buffer->vol[ch]);
1096 }
1097 }
1098}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001099EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001100#endif /* SND_HDA_NEEDS_RESUME */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102/* volume */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001103int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1104 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105{
1106 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1107 u16 nid = get_amp_nid(kcontrol);
1108 u8 chs = get_amp_channels(kcontrol);
1109 int dir = get_amp_direction(kcontrol);
1110 u32 caps;
1111
1112 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001113 /* num steps */
1114 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1115 if (!caps) {
1116 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01001117 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
1118 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 return -EINVAL;
1120 }
1121 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1122 uinfo->count = chs == 3 ? 2 : 1;
1123 uinfo->value.integer.min = 0;
1124 uinfo->value.integer.max = caps;
1125 return 0;
1126}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001127EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
Takashi Iwai0ba21762007-04-16 11:29:14 +02001129int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1130 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131{
1132 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1133 hda_nid_t nid = get_amp_nid(kcontrol);
1134 int chs = get_amp_channels(kcontrol);
1135 int dir = get_amp_direction(kcontrol);
1136 int idx = get_amp_index(kcontrol);
1137 long *valp = ucontrol->value.integer.value;
1138
1139 if (chs & 1)
Takashi Iwai47fd8302007-08-10 17:11:07 +02001140 *valp++ = snd_hda_codec_amp_read(codec, nid, 0, dir, idx)
1141 & HDA_AMP_VOLMASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 if (chs & 2)
Takashi Iwai47fd8302007-08-10 17:11:07 +02001143 *valp = snd_hda_codec_amp_read(codec, nid, 1, dir, idx)
1144 & HDA_AMP_VOLMASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 return 0;
1146}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001147EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
Takashi Iwai0ba21762007-04-16 11:29:14 +02001149int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1150 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151{
1152 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1153 hda_nid_t nid = get_amp_nid(kcontrol);
1154 int chs = get_amp_channels(kcontrol);
1155 int dir = get_amp_direction(kcontrol);
1156 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 long *valp = ucontrol->value.integer.value;
1158 int change = 0;
1159
Takashi Iwaicb53c622007-08-10 17:21:45 +02001160 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001161 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001162 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
1163 0x7f, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001164 valp++;
1165 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001166 if (chs & 2)
1167 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001168 0x7f, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001169 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 return change;
1171}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001172EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001174int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1175 unsigned int size, unsigned int __user *_tlv)
1176{
1177 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1178 hda_nid_t nid = get_amp_nid(kcontrol);
1179 int dir = get_amp_direction(kcontrol);
1180 u32 caps, val1, val2;
1181
1182 if (size < 4 * sizeof(unsigned int))
1183 return -ENOMEM;
1184 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001185 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1186 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001187 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
1188 val1 = ((int)val1) * ((int)val2);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001189 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
1190 return -EFAULT;
1191 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
1192 return -EFAULT;
1193 if (put_user(val1, _tlv + 2))
1194 return -EFAULT;
1195 if (put_user(val2, _tlv + 3))
1196 return -EFAULT;
1197 return 0;
1198}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001199EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001200
Takashi Iwai2134ea42008-01-10 16:53:55 +01001201/*
1202 * set (static) TLV for virtual master volume; recalculated as max 0dB
1203 */
1204void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
1205 unsigned int *tlv)
1206{
1207 u32 caps;
1208 int nums, step;
1209
1210 caps = query_amp_caps(codec, nid, dir);
1211 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1212 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1213 step = (step + 1) * 25;
1214 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
1215 tlv[1] = 2 * sizeof(unsigned int);
1216 tlv[2] = -nums * step;
1217 tlv[3] = step;
1218}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001219EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001220
1221/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01001222static struct snd_kcontrol *
1223_snd_hda_find_mixer_ctl(struct hda_codec *codec,
1224 const char *name, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01001225{
1226 struct snd_ctl_elem_id id;
1227 memset(&id, 0, sizeof(id));
1228 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwai09f99702008-02-04 12:31:13 +01001229 id.index = idx;
Takashi Iwai2134ea42008-01-10 16:53:55 +01001230 strcpy(id.name, name);
1231 return snd_ctl_find_id(codec->bus->card, &id);
1232}
1233
Takashi Iwai09f99702008-02-04 12:31:13 +01001234struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
1235 const char *name)
1236{
1237 return _snd_hda_find_mixer_ctl(codec, name, 0);
1238}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001239EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01001240
Takashi Iwaid13bd412008-07-30 15:01:45 +02001241/* Add a control element and assign to the codec */
1242int snd_hda_ctl_add(struct hda_codec *codec, struct snd_kcontrol *kctl)
1243{
1244 int err;
1245 struct snd_kcontrol **knewp;
1246
1247 err = snd_ctl_add(codec->bus->card, kctl);
1248 if (err < 0)
1249 return err;
1250 knewp = snd_array_new(&codec->mixers);
1251 if (!knewp)
1252 return -ENOMEM;
1253 *knewp = kctl;
1254 return 0;
1255}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001256EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001257
Takashi Iwai529bd6c2008-11-27 14:17:01 +01001258#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwaid13bd412008-07-30 15:01:45 +02001259/* Clear all controls assigned to the given codec */
1260void snd_hda_ctls_clear(struct hda_codec *codec)
1261{
1262 int i;
1263 struct snd_kcontrol **kctls = codec->mixers.list;
1264 for (i = 0; i < codec->mixers.used; i++)
1265 snd_ctl_remove(codec->bus->card, kctls[i]);
1266 snd_array_free(&codec->mixers);
1267}
1268
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001269void snd_hda_codec_reset(struct hda_codec *codec)
1270{
1271 int i;
1272
1273#ifdef CONFIG_SND_HDA_POWER_SAVE
1274 cancel_delayed_work(&codec->power_work);
1275 flush_scheduled_work();
1276#endif
1277 snd_hda_ctls_clear(codec);
1278 /* relase PCMs */
1279 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01001280 if (codec->pcm_info[i].pcm) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001281 snd_device_free(codec->bus->card,
1282 codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01001283 clear_bit(codec->pcm_info[i].device,
1284 codec->bus->pcm_dev_bits);
1285 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001286 }
1287 if (codec->patch_ops.free)
1288 codec->patch_ops.free(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01001289 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001290 codec->spec = NULL;
1291 free_hda_cache(&codec->amp_cache);
1292 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01001293 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
1294 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001295 codec->num_pcms = 0;
1296 codec->pcm_info = NULL;
1297 codec->preset = NULL;
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001298 module_put(codec->owner);
1299 codec->owner = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001300}
Takashi Iwai529bd6c2008-11-27 14:17:01 +01001301#endif /* CONFIG_SND_HDA_RECONFIG */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001302
Takashi Iwai2134ea42008-01-10 16:53:55 +01001303/* create a virtual master control and add slaves */
1304int snd_hda_add_vmaster(struct hda_codec *codec, char *name,
1305 unsigned int *tlv, const char **slaves)
1306{
1307 struct snd_kcontrol *kctl;
1308 const char **s;
1309 int err;
1310
Takashi Iwai2f085542008-02-22 18:43:50 +01001311 for (s = slaves; *s && !snd_hda_find_mixer_ctl(codec, *s); s++)
1312 ;
1313 if (!*s) {
1314 snd_printdd("No slave found for %s\n", name);
1315 return 0;
1316 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01001317 kctl = snd_ctl_make_virtual_master(name, tlv);
1318 if (!kctl)
1319 return -ENOMEM;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001320 err = snd_hda_ctl_add(codec, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001321 if (err < 0)
1322 return err;
1323
1324 for (s = slaves; *s; s++) {
1325 struct snd_kcontrol *sctl;
1326
1327 sctl = snd_hda_find_mixer_ctl(codec, *s);
1328 if (!sctl) {
1329 snd_printdd("Cannot find slave %s, skipped\n", *s);
1330 continue;
1331 }
1332 err = snd_ctl_add_slave(kctl, sctl);
1333 if (err < 0)
1334 return err;
1335 }
1336 return 0;
1337}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001338EXPORT_SYMBOL_HDA(snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001339
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340/* switch */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001341int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
1342 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343{
1344 int chs = get_amp_channels(kcontrol);
1345
1346 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1347 uinfo->count = chs == 3 ? 2 : 1;
1348 uinfo->value.integer.min = 0;
1349 uinfo->value.integer.max = 1;
1350 return 0;
1351}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001352EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
Takashi Iwai0ba21762007-04-16 11:29:14 +02001354int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
1355 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356{
1357 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1358 hda_nid_t nid = get_amp_nid(kcontrol);
1359 int chs = get_amp_channels(kcontrol);
1360 int dir = get_amp_direction(kcontrol);
1361 int idx = get_amp_index(kcontrol);
1362 long *valp = ucontrol->value.integer.value;
1363
1364 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02001365 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02001366 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02001368 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02001369 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 return 0;
1371}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001372EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
Takashi Iwai0ba21762007-04-16 11:29:14 +02001374int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
1375 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376{
1377 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1378 hda_nid_t nid = get_amp_nid(kcontrol);
1379 int chs = get_amp_channels(kcontrol);
1380 int dir = get_amp_direction(kcontrol);
1381 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 long *valp = ucontrol->value.integer.value;
1383 int change = 0;
1384
Takashi Iwaicb53c622007-08-10 17:21:45 +02001385 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001386 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001387 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02001388 HDA_AMP_MUTE,
1389 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001390 valp++;
1391 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001392 if (chs & 2)
1393 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02001394 HDA_AMP_MUTE,
1395 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001396#ifdef CONFIG_SND_HDA_POWER_SAVE
1397 if (codec->patch_ops.check_power_status)
1398 codec->patch_ops.check_power_status(codec, nid);
1399#endif
1400 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 return change;
1402}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001403EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
1405/*
Takashi Iwai985be542005-11-02 18:26:49 +01001406 * bound volume controls
1407 *
1408 * bind multiple volumes (# indices, from 0)
1409 */
1410
1411#define AMP_VAL_IDX_SHIFT 19
1412#define AMP_VAL_IDX_MASK (0x0f<<19)
1413
Takashi Iwai0ba21762007-04-16 11:29:14 +02001414int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
1415 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01001416{
1417 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1418 unsigned long pval;
1419 int err;
1420
Ingo Molnar62932df2006-01-16 16:34:20 +01001421 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Takashi Iwai985be542005-11-02 18:26:49 +01001422 pval = kcontrol->private_value;
1423 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
1424 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
1425 kcontrol->private_value = pval;
Ingo Molnar62932df2006-01-16 16:34:20 +01001426 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01001427 return err;
1428}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001429EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01001430
Takashi Iwai0ba21762007-04-16 11:29:14 +02001431int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
1432 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01001433{
1434 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1435 unsigned long pval;
1436 int i, indices, err = 0, change = 0;
1437
Ingo Molnar62932df2006-01-16 16:34:20 +01001438 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Takashi Iwai985be542005-11-02 18:26:49 +01001439 pval = kcontrol->private_value;
1440 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
1441 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001442 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
1443 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01001444 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1445 if (err < 0)
1446 break;
1447 change |= err;
1448 }
1449 kcontrol->private_value = pval;
Ingo Molnar62932df2006-01-16 16:34:20 +01001450 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01001451 return err < 0 ? err : change;
1452}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001453EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01001454
1455/*
Takashi Iwai532d5382007-07-27 19:02:40 +02001456 * generic bound volume/swtich controls
1457 */
1458int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
1459 struct snd_ctl_elem_info *uinfo)
1460{
1461 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1462 struct hda_bind_ctls *c;
1463 int err;
1464
Takashi Iwai532d5382007-07-27 19:02:40 +02001465 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01001466 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02001467 kcontrol->private_value = *c->values;
1468 err = c->ops->info(kcontrol, uinfo);
1469 kcontrol->private_value = (long)c;
1470 mutex_unlock(&codec->spdif_mutex);
1471 return err;
1472}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001473EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02001474
1475int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
1476 struct snd_ctl_elem_value *ucontrol)
1477{
1478 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1479 struct hda_bind_ctls *c;
1480 int err;
1481
Takashi Iwai532d5382007-07-27 19:02:40 +02001482 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01001483 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02001484 kcontrol->private_value = *c->values;
1485 err = c->ops->get(kcontrol, ucontrol);
1486 kcontrol->private_value = (long)c;
1487 mutex_unlock(&codec->spdif_mutex);
1488 return err;
1489}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001490EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02001491
1492int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
1493 struct snd_ctl_elem_value *ucontrol)
1494{
1495 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1496 struct hda_bind_ctls *c;
1497 unsigned long *vals;
1498 int err = 0, change = 0;
1499
Takashi Iwai532d5382007-07-27 19:02:40 +02001500 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01001501 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02001502 for (vals = c->values; *vals; vals++) {
1503 kcontrol->private_value = *vals;
1504 err = c->ops->put(kcontrol, ucontrol);
1505 if (err < 0)
1506 break;
1507 change |= err;
1508 }
1509 kcontrol->private_value = (long)c;
1510 mutex_unlock(&codec->spdif_mutex);
1511 return err < 0 ? err : change;
1512}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001513EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02001514
1515int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1516 unsigned int size, unsigned int __user *tlv)
1517{
1518 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1519 struct hda_bind_ctls *c;
1520 int err;
1521
Takashi Iwai532d5382007-07-27 19:02:40 +02001522 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01001523 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02001524 kcontrol->private_value = *c->values;
1525 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
1526 kcontrol->private_value = (long)c;
1527 mutex_unlock(&codec->spdif_mutex);
1528 return err;
1529}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001530EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02001531
1532struct hda_ctl_ops snd_hda_bind_vol = {
1533 .info = snd_hda_mixer_amp_volume_info,
1534 .get = snd_hda_mixer_amp_volume_get,
1535 .put = snd_hda_mixer_amp_volume_put,
1536 .tlv = snd_hda_mixer_amp_tlv
1537};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001538EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02001539
1540struct hda_ctl_ops snd_hda_bind_sw = {
1541 .info = snd_hda_mixer_amp_switch_info,
1542 .get = snd_hda_mixer_amp_switch_get,
1543 .put = snd_hda_mixer_amp_switch_put,
1544 .tlv = snd_hda_mixer_amp_tlv
1545};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001546EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02001547
1548/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 * SPDIF out controls
1550 */
1551
Takashi Iwai0ba21762007-04-16 11:29:14 +02001552static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
1553 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554{
1555 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1556 uinfo->count = 1;
1557 return 0;
1558}
1559
Takashi Iwai0ba21762007-04-16 11:29:14 +02001560static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
1561 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562{
1563 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
1564 IEC958_AES0_NONAUDIO |
1565 IEC958_AES0_CON_EMPHASIS_5015 |
1566 IEC958_AES0_CON_NOT_COPYRIGHT;
1567 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
1568 IEC958_AES1_CON_ORIGINAL;
1569 return 0;
1570}
1571
Takashi Iwai0ba21762007-04-16 11:29:14 +02001572static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
1573 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574{
1575 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
1576 IEC958_AES0_NONAUDIO |
1577 IEC958_AES0_PRO_EMPHASIS_5015;
1578 return 0;
1579}
1580
Takashi Iwai0ba21762007-04-16 11:29:14 +02001581static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
1582 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583{
1584 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1585
1586 ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
1587 ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
1588 ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
1589 ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
1590
1591 return 0;
1592}
1593
1594/* convert from SPDIF status bits to HDA SPDIF bits
1595 * bit 0 (DigEn) is always set zero (to be filled later)
1596 */
1597static unsigned short convert_from_spdif_status(unsigned int sbits)
1598{
1599 unsigned short val = 0;
1600
1601 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02001602 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02001604 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001606 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
1607 IEC958_AES0_PRO_EMPHASIS_5015)
1608 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001610 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
1611 IEC958_AES0_CON_EMPHASIS_5015)
1612 val |= AC_DIG1_EMPHASIS;
1613 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
1614 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02001616 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
1618 }
1619 return val;
1620}
1621
1622/* convert to SPDIF status bits from HDA SPDIF bits
1623 */
1624static unsigned int convert_to_spdif_status(unsigned short val)
1625{
1626 unsigned int sbits = 0;
1627
Takashi Iwai0ba21762007-04-16 11:29:14 +02001628 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001630 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 sbits |= IEC958_AES0_PROFESSIONAL;
1632 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001633 if (sbits & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
1635 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001636 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001638 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001640 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
1642 sbits |= val & (0x7f << 8);
1643 }
1644 return sbits;
1645}
1646
Takashi Iwai2f728532008-09-25 16:32:41 +02001647/* set digital convert verbs both for the given NID and its slaves */
1648static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
1649 int verb, int val)
1650{
1651 hda_nid_t *d;
1652
Takashi Iwai9e976972008-11-25 08:17:20 +01001653 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02001654 d = codec->slave_dig_outs;
1655 if (!d)
1656 return;
1657 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01001658 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02001659}
1660
1661static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
1662 int dig1, int dig2)
1663{
1664 if (dig1 != -1)
1665 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
1666 if (dig2 != -1)
1667 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
1668}
1669
Takashi Iwai0ba21762007-04-16 11:29:14 +02001670static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
1671 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672{
1673 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1674 hda_nid_t nid = kcontrol->private_value;
1675 unsigned short val;
1676 int change;
1677
Ingo Molnar62932df2006-01-16 16:34:20 +01001678 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 codec->spdif_status = ucontrol->value.iec958.status[0] |
1680 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
1681 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
1682 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
1683 val = convert_from_spdif_status(codec->spdif_status);
1684 val |= codec->spdif_ctls & 1;
1685 change = codec->spdif_ctls != val;
1686 codec->spdif_ctls = val;
1687
Takashi Iwai2f728532008-09-25 16:32:41 +02001688 if (change)
1689 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
Ingo Molnar62932df2006-01-16 16:34:20 +01001691 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 return change;
1693}
1694
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001695#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
Takashi Iwai0ba21762007-04-16 11:29:14 +02001697static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
1698 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699{
1700 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1701
Takashi Iwai0ba21762007-04-16 11:29:14 +02001702 ucontrol->value.integer.value[0] = codec->spdif_ctls & AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 return 0;
1704}
1705
Takashi Iwai0ba21762007-04-16 11:29:14 +02001706static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
1707 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708{
1709 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1710 hda_nid_t nid = kcontrol->private_value;
1711 unsigned short val;
1712 int change;
1713
Ingo Molnar62932df2006-01-16 16:34:20 +01001714 mutex_lock(&codec->spdif_mutex);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001715 val = codec->spdif_ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02001717 val |= AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 change = codec->spdif_ctls != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001719 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 codec->spdif_ctls = val;
Takashi Iwai2f728532008-09-25 16:32:41 +02001721 set_dig_out_convert(codec, nid, val & 0xff, -1);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001722 /* unmute amp switch (if any) */
1723 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
Takashi Iwai47fd8302007-08-10 17:11:07 +02001724 (val & AC_DIG1_ENABLE))
1725 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
1726 HDA_AMP_MUTE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 }
Ingo Molnar62932df2006-01-16 16:34:20 +01001728 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 return change;
1730}
1731
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001732static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 {
1734 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1735 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1736 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
1737 .info = snd_hda_spdif_mask_info,
1738 .get = snd_hda_spdif_cmask_get,
1739 },
1740 {
1741 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1742 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1743 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
1744 .info = snd_hda_spdif_mask_info,
1745 .get = snd_hda_spdif_pmask_get,
1746 },
1747 {
1748 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1749 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1750 .info = snd_hda_spdif_mask_info,
1751 .get = snd_hda_spdif_default_get,
1752 .put = snd_hda_spdif_default_put,
1753 },
1754 {
1755 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1756 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
1757 .info = snd_hda_spdif_out_switch_info,
1758 .get = snd_hda_spdif_out_switch_get,
1759 .put = snd_hda_spdif_out_switch_put,
1760 },
1761 { } /* end */
1762};
1763
Takashi Iwai09f99702008-02-04 12:31:13 +01001764#define SPDIF_MAX_IDX 4 /* 4 instances should be enough to probe */
1765
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766/**
1767 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
1768 * @codec: the HDA codec
1769 * @nid: audio out widget NID
1770 *
1771 * Creates controls related with the SPDIF output.
1772 * Called from each patch supporting the SPDIF out.
1773 *
1774 * Returns 0 if successful, or a negative error code.
1775 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02001776int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777{
1778 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001779 struct snd_kcontrol *kctl;
1780 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01001781 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
Takashi Iwai09f99702008-02-04 12:31:13 +01001783 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
1784 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Playback Switch",
1785 idx))
1786 break;
1787 }
1788 if (idx >= SPDIF_MAX_IDX) {
1789 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
1790 return -EBUSY;
1791 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
1793 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01001794 if (!kctl)
1795 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01001796 kctl->id.index = idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 kctl->private_value = nid;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001798 err = snd_hda_ctl_add(codec, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001799 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 return err;
1801 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02001802 codec->spdif_ctls =
Andrew Paprocki3982d172007-12-19 12:13:44 +01001803 snd_hda_codec_read(codec, nid, 0,
1804 AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
1806 return 0;
1807}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001808EXPORT_SYMBOL_HDA(snd_hda_create_spdif_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
1810/*
Takashi Iwai9a081602008-02-12 18:37:26 +01001811 * SPDIF sharing with analog output
1812 */
1813static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
1814 struct snd_ctl_elem_value *ucontrol)
1815{
1816 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
1817 ucontrol->value.integer.value[0] = mout->share_spdif;
1818 return 0;
1819}
1820
1821static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
1822 struct snd_ctl_elem_value *ucontrol)
1823{
1824 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
1825 mout->share_spdif = !!ucontrol->value.integer.value[0];
1826 return 0;
1827}
1828
1829static struct snd_kcontrol_new spdif_share_sw = {
1830 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1831 .name = "IEC958 Default PCM Playback Switch",
1832 .info = snd_ctl_boolean_mono_info,
1833 .get = spdif_share_sw_get,
1834 .put = spdif_share_sw_put,
1835};
1836
1837int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
1838 struct hda_multi_out *mout)
1839{
1840 if (!mout->dig_out_nid)
1841 return 0;
1842 /* ATTENTION: here mout is passed as private_data, instead of codec */
Takashi Iwaid13bd412008-07-30 15:01:45 +02001843 return snd_hda_ctl_add(codec,
Takashi Iwai9a081602008-02-12 18:37:26 +01001844 snd_ctl_new1(&spdif_share_sw, mout));
1845}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001846EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01001847
1848/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 * SPDIF input
1850 */
1851
1852#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
1853
Takashi Iwai0ba21762007-04-16 11:29:14 +02001854static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
1855 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856{
1857 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1858
1859 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
1860 return 0;
1861}
1862
Takashi Iwai0ba21762007-04-16 11:29:14 +02001863static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
1864 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865{
1866 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1867 hda_nid_t nid = kcontrol->private_value;
1868 unsigned int val = !!ucontrol->value.integer.value[0];
1869 int change;
1870
Ingo Molnar62932df2006-01-16 16:34:20 +01001871 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001873 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001875 snd_hda_codec_write_cache(codec, nid, 0,
1876 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 }
Ingo Molnar62932df2006-01-16 16:34:20 +01001878 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 return change;
1880}
1881
Takashi Iwai0ba21762007-04-16 11:29:14 +02001882static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
1883 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884{
1885 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1886 hda_nid_t nid = kcontrol->private_value;
1887 unsigned short val;
1888 unsigned int sbits;
1889
Andrew Paprocki3982d172007-12-19 12:13:44 +01001890 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 sbits = convert_to_spdif_status(val);
1892 ucontrol->value.iec958.status[0] = sbits;
1893 ucontrol->value.iec958.status[1] = sbits >> 8;
1894 ucontrol->value.iec958.status[2] = sbits >> 16;
1895 ucontrol->value.iec958.status[3] = sbits >> 24;
1896 return 0;
1897}
1898
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001899static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 {
1901 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1902 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH),
1903 .info = snd_hda_spdif_in_switch_info,
1904 .get = snd_hda_spdif_in_switch_get,
1905 .put = snd_hda_spdif_in_switch_put,
1906 },
1907 {
1908 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1909 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1910 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT),
1911 .info = snd_hda_spdif_mask_info,
1912 .get = snd_hda_spdif_in_status_get,
1913 },
1914 { } /* end */
1915};
1916
1917/**
1918 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
1919 * @codec: the HDA codec
1920 * @nid: audio in widget NID
1921 *
1922 * Creates controls related with the SPDIF input.
1923 * Called from each patch supporting the SPDIF in.
1924 *
1925 * Returns 0 if successful, or a negative error code.
1926 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02001927int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928{
1929 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001930 struct snd_kcontrol *kctl;
1931 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01001932 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
Takashi Iwai09f99702008-02-04 12:31:13 +01001934 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
1935 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Capture Switch",
1936 idx))
1937 break;
1938 }
1939 if (idx >= SPDIF_MAX_IDX) {
1940 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
1941 return -EBUSY;
1942 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
1944 kctl = snd_ctl_new1(dig_mix, codec);
1945 kctl->private_value = nid;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001946 err = snd_hda_ctl_add(codec, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001947 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 return err;
1949 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02001950 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01001951 snd_hda_codec_read(codec, nid, 0,
1952 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02001953 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 return 0;
1955}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001956EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
Takashi Iwaicb53c622007-08-10 17:21:45 +02001958#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001959/*
1960 * command cache
1961 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001963/* build a 32bit cache key with the widget id and the command parameter */
1964#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
1965#define get_cmd_cache_nid(key) ((key) & 0xff)
1966#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
1967
1968/**
1969 * snd_hda_codec_write_cache - send a single command with caching
1970 * @codec: the HDA codec
1971 * @nid: NID to send the command
1972 * @direct: direct flag
1973 * @verb: the verb to send
1974 * @parm: the parameter for the verb
1975 *
1976 * Send a single command without waiting for response.
1977 *
1978 * Returns 0 if successful, or a negative error code.
1979 */
1980int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
1981 int direct, unsigned int verb, unsigned int parm)
1982{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01001983 struct hda_bus *bus = codec->bus;
1984 unsigned int res;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001985 int err;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01001986
1987 res = make_codec_cmd(codec, nid, direct, verb, parm);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001988 snd_hda_power_up(codec);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01001989 mutex_lock(&bus->cmd_mutex);
1990 err = bus->ops.command(bus, res);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001991 if (!err) {
1992 struct hda_cache_head *c;
1993 u32 key = build_cmd_cache_key(nid, verb);
1994 c = get_alloc_hash(&codec->cmd_cache, key);
1995 if (c)
1996 c->val = parm;
1997 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01001998 mutex_unlock(&bus->cmd_mutex);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001999 snd_hda_power_down(codec);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002000 return err;
2001}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002002EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002003
2004/* resume the all commands from the cache */
2005void snd_hda_codec_resume_cache(struct hda_codec *codec)
2006{
Takashi Iwai603c4012008-07-30 15:01:44 +02002007 struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002008 int i;
2009
Takashi Iwai603c4012008-07-30 15:01:44 +02002010 for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002011 u32 key = buffer->key;
2012 if (!key)
2013 continue;
2014 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
2015 get_cmd_cache_cmd(key), buffer->val);
2016 }
2017}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002018EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002019
2020/**
2021 * snd_hda_sequence_write_cache - sequence writes with caching
2022 * @codec: the HDA codec
2023 * @seq: VERB array to send
2024 *
2025 * Send the commands sequentially from the given array.
2026 * Thte commands are recorded on cache for power-save and resume.
2027 * The array must be terminated with NID=0.
2028 */
2029void snd_hda_sequence_write_cache(struct hda_codec *codec,
2030 const struct hda_verb *seq)
2031{
2032 for (; seq->nid; seq++)
2033 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
2034 seq->param);
2035}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002036EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002037#endif /* SND_HDA_NEEDS_RESUME */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002038
Takashi Iwai54d17402005-11-21 16:33:22 +01002039/*
2040 * set power state of the codec
2041 */
2042static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2043 unsigned int power_state)
2044{
Takashi Iwaicb53c622007-08-10 17:21:45 +02002045 hda_nid_t nid;
2046 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01002047
2048 snd_hda_codec_write(codec, fg, 0, AC_VERB_SET_POWER_STATE,
2049 power_state);
Marc Boucherd2595d82008-01-22 15:23:30 +01002050 msleep(10); /* partial workaround for "azx_get_response timeout" */
Takashi Iwai54d17402005-11-21 16:33:22 +01002051
Takashi Iwaicb53c622007-08-10 17:21:45 +02002052 nid = codec->start_nid;
2053 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01002054 unsigned int wcaps = get_wcaps(codec, nid);
2055 if (wcaps & AC_WCAP_POWER) {
2056 unsigned int wid_type = (wcaps & AC_WCAP_TYPE) >>
2057 AC_WCAP_TYPE_SHIFT;
2058 if (wid_type == AC_WID_PIN) {
2059 unsigned int pincap;
2060 /*
2061 * don't power down the widget if it controls
2062 * eapd and EAPD_BTLENABLE is set.
2063 */
2064 pincap = snd_hda_param_read(codec, nid,
2065 AC_PAR_PIN_CAP);
2066 if (pincap & AC_PINCAP_EAPD) {
2067 int eapd = snd_hda_codec_read(codec,
2068 nid, 0,
2069 AC_VERB_GET_EAPD_BTLENABLE, 0);
2070 eapd &= 0x02;
2071 if (power_state == AC_PWRST_D3 && eapd)
2072 continue;
2073 }
Takashi Iwai1194b5b2007-10-10 10:04:26 +02002074 }
Takashi Iwai54d17402005-11-21 16:33:22 +01002075 snd_hda_codec_write(codec, nid, 0,
2076 AC_VERB_SET_POWER_STATE,
2077 power_state);
Takashi Iwai1194b5b2007-10-10 10:04:26 +02002078 }
Takashi Iwai54d17402005-11-21 16:33:22 +01002079 }
2080
Takashi Iwaicb53c622007-08-10 17:21:45 +02002081 if (power_state == AC_PWRST_D0) {
2082 unsigned long end_time;
2083 int state;
Takashi Iwai54d17402005-11-21 16:33:22 +01002084 msleep(10);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002085 /* wait until the codec reachs to D0 */
2086 end_time = jiffies + msecs_to_jiffies(500);
2087 do {
2088 state = snd_hda_codec_read(codec, fg, 0,
2089 AC_VERB_GET_POWER_STATE, 0);
2090 if (state == power_state)
2091 break;
2092 msleep(1);
2093 } while (time_after_eq(end_time, jiffies));
2094 }
Takashi Iwai54d17402005-11-21 16:33:22 +01002095}
2096
Takashi Iwai11aeff02008-07-30 15:01:46 +02002097#ifdef CONFIG_SND_HDA_HWDEP
2098/* execute additional init verbs */
2099static void hda_exec_init_verbs(struct hda_codec *codec)
2100{
2101 if (codec->init_verbs.list)
2102 snd_hda_sequence_write(codec, codec->init_verbs.list);
2103}
2104#else
2105static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
2106#endif
2107
Takashi Iwaicb53c622007-08-10 17:21:45 +02002108#ifdef SND_HDA_NEEDS_RESUME
2109/*
2110 * call suspend and power-down; used both from PM and power-save
2111 */
2112static void hda_call_codec_suspend(struct hda_codec *codec)
2113{
2114 if (codec->patch_ops.suspend)
2115 codec->patch_ops.suspend(codec, PMSG_SUSPEND);
2116 hda_set_power_state(codec,
2117 codec->afg ? codec->afg : codec->mfg,
2118 AC_PWRST_D3);
2119#ifdef CONFIG_SND_HDA_POWER_SAVE
2120 cancel_delayed_work(&codec->power_work);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02002121 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02002122 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002123#endif
2124}
2125
2126/*
2127 * kick up codec; used both from PM and power-save
2128 */
2129static void hda_call_codec_resume(struct hda_codec *codec)
2130{
2131 hda_set_power_state(codec,
2132 codec->afg ? codec->afg : codec->mfg,
2133 AC_PWRST_D0);
Takashi Iwai11aeff02008-07-30 15:01:46 +02002134 hda_exec_init_verbs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002135 if (codec->patch_ops.resume)
2136 codec->patch_ops.resume(codec);
2137 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02002138 if (codec->patch_ops.init)
2139 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002140 snd_hda_codec_resume_amp(codec);
2141 snd_hda_codec_resume_cache(codec);
2142 }
2143}
2144#endif /* SND_HDA_NEEDS_RESUME */
2145
Takashi Iwai54d17402005-11-21 16:33:22 +01002146
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147/**
2148 * snd_hda_build_controls - build mixer controls
2149 * @bus: the BUS
2150 *
2151 * Creates mixer controls for each codec included in the bus.
2152 *
2153 * Returns 0 if successful, otherwise a negative error code.
2154 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002155int /*__devinit*/ snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156{
Takashi Iwai0ba21762007-04-16 11:29:14 +02002157 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158
Takashi Iwai0ba21762007-04-16 11:29:14 +02002159 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002160 int err = snd_hda_codec_build_controls(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 if (err < 0)
2162 return err;
2163 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002164 return 0;
2165}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002166EXPORT_SYMBOL_HDA(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002168int snd_hda_codec_build_controls(struct hda_codec *codec)
2169{
2170 int err = 0;
2171 /* fake as if already powered-on */
2172 hda_keep_power_on(codec);
2173 /* then fire up */
2174 hda_set_power_state(codec,
2175 codec->afg ? codec->afg : codec->mfg,
2176 AC_PWRST_D0);
Takashi Iwai11aeff02008-07-30 15:01:46 +02002177 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002178 /* continue to initialize... */
2179 if (codec->patch_ops.init)
2180 err = codec->patch_ops.init(codec);
2181 if (!err && codec->patch_ops.build_controls)
2182 err = codec->patch_ops.build_controls(codec);
2183 snd_hda_power_down(codec);
2184 if (err < 0)
2185 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 return 0;
2187}
2188
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189/*
2190 * stream formats
2191 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02002192struct hda_rate_tbl {
2193 unsigned int hz;
2194 unsigned int alsa_bits;
2195 unsigned int hda_fmt;
2196};
2197
2198static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02002200
2201 /* autodetected value used in snd_hda_query_supported_pcm */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 { 8000, SNDRV_PCM_RATE_8000, 0x0500 }, /* 1/6 x 48 */
2203 { 11025, SNDRV_PCM_RATE_11025, 0x4300 }, /* 1/4 x 44 */
2204 { 16000, SNDRV_PCM_RATE_16000, 0x0200 }, /* 1/3 x 48 */
2205 { 22050, SNDRV_PCM_RATE_22050, 0x4100 }, /* 1/2 x 44 */
2206 { 32000, SNDRV_PCM_RATE_32000, 0x0a00 }, /* 2/3 x 48 */
2207 { 44100, SNDRV_PCM_RATE_44100, 0x4000 }, /* 44 */
2208 { 48000, SNDRV_PCM_RATE_48000, 0x0000 }, /* 48 */
2209 { 88200, SNDRV_PCM_RATE_88200, 0x4800 }, /* 2 x 44 */
2210 { 96000, SNDRV_PCM_RATE_96000, 0x0800 }, /* 2 x 48 */
2211 { 176400, SNDRV_PCM_RATE_176400, 0x5800 },/* 4 x 44 */
2212 { 192000, SNDRV_PCM_RATE_192000, 0x1800 }, /* 4 x 48 */
Takashi Iwaia961f9f2007-04-12 13:08:09 +02002213#define AC_PAR_PCM_RATE_BITS 11
2214 /* up to bits 10, 384kHZ isn't supported properly */
2215
2216 /* not autodetected value */
2217 { 9600, SNDRV_PCM_RATE_KNOT, 0x0400 }, /* 1/5 x 48 */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02002218
Takashi Iwaibefdf312005-08-22 13:57:55 +02002219 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220};
2221
2222/**
2223 * snd_hda_calc_stream_format - calculate format bitset
2224 * @rate: the sample rate
2225 * @channels: the number of channels
2226 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
2227 * @maxbps: the max. bps
2228 *
2229 * Calculate the format bitset from the given rate, channels and th PCM format.
2230 *
2231 * Return zero if invalid.
2232 */
2233unsigned int snd_hda_calc_stream_format(unsigned int rate,
2234 unsigned int channels,
2235 unsigned int format,
2236 unsigned int maxbps)
2237{
2238 int i;
2239 unsigned int val = 0;
2240
Takashi Iwaibefdf312005-08-22 13:57:55 +02002241 for (i = 0; rate_bits[i].hz; i++)
2242 if (rate_bits[i].hz == rate) {
2243 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 break;
2245 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002246 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 snd_printdd("invalid rate %d\n", rate);
2248 return 0;
2249 }
2250
2251 if (channels == 0 || channels > 8) {
2252 snd_printdd("invalid channels %d\n", channels);
2253 return 0;
2254 }
2255 val |= channels - 1;
2256
2257 switch (snd_pcm_format_width(format)) {
2258 case 8: val |= 0x00; break;
2259 case 16: val |= 0x10; break;
2260 case 20:
2261 case 24:
2262 case 32:
2263 if (maxbps >= 32)
2264 val |= 0x40;
2265 else if (maxbps >= 24)
2266 val |= 0x30;
2267 else
2268 val |= 0x20;
2269 break;
2270 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002271 snd_printdd("invalid format width %d\n",
2272 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 return 0;
2274 }
2275
2276 return val;
2277}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002278EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279
2280/**
2281 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
2282 * @codec: the HDA codec
2283 * @nid: NID to query
2284 * @ratesp: the pointer to store the detected rate bitflags
2285 * @formatsp: the pointer to store the detected formats
2286 * @bpsp: the pointer to store the detected format widths
2287 *
2288 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
2289 * or @bsps argument is ignored.
2290 *
2291 * Returns 0 if successful, otherwise a negative error code.
2292 */
Takashi Iwai986862bd2008-11-27 12:40:13 +01002293static int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
2295{
2296 int i;
2297 unsigned int val, streams;
2298
2299 val = 0;
2300 if (nid != codec->afg &&
Takashi Iwai54d17402005-11-21 16:33:22 +01002301 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
2303 if (val == -1)
2304 return -EIO;
2305 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002306 if (!val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
2308
2309 if (ratesp) {
2310 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02002311 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02002313 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 }
2315 *ratesp = rates;
2316 }
2317
2318 if (formatsp || bpsp) {
2319 u64 formats = 0;
2320 unsigned int bps;
2321 unsigned int wcaps;
2322
Takashi Iwai54d17402005-11-21 16:33:22 +01002323 wcaps = get_wcaps(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
2325 if (streams == -1)
2326 return -EIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002327 if (!streams) {
2328 streams = snd_hda_param_read(codec, codec->afg,
2329 AC_PAR_STREAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 if (streams == -1)
2331 return -EIO;
2332 }
2333
2334 bps = 0;
2335 if (streams & AC_SUPFMT_PCM) {
2336 if (val & AC_SUPPCM_BITS_8) {
2337 formats |= SNDRV_PCM_FMTBIT_U8;
2338 bps = 8;
2339 }
2340 if (val & AC_SUPPCM_BITS_16) {
2341 formats |= SNDRV_PCM_FMTBIT_S16_LE;
2342 bps = 16;
2343 }
2344 if (wcaps & AC_WCAP_DIGITAL) {
2345 if (val & AC_SUPPCM_BITS_32)
2346 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
2347 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
2348 formats |= SNDRV_PCM_FMTBIT_S32_LE;
2349 if (val & AC_SUPPCM_BITS_24)
2350 bps = 24;
2351 else if (val & AC_SUPPCM_BITS_20)
2352 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002353 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
2354 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 formats |= SNDRV_PCM_FMTBIT_S32_LE;
2356 if (val & AC_SUPPCM_BITS_32)
2357 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 else if (val & AC_SUPPCM_BITS_24)
2359 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02002360 else if (val & AC_SUPPCM_BITS_20)
2361 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 }
2363 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002364 else if (streams == AC_SUPFMT_FLOAT32) {
2365 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
2367 bps = 32;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002368 } else if (streams == AC_SUPFMT_AC3) {
2369 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 /* temporary hack: we have still no proper support
2371 * for the direct AC3 stream...
2372 */
2373 formats |= SNDRV_PCM_FMTBIT_U8;
2374 bps = 8;
2375 }
2376 if (formatsp)
2377 *formatsp = formats;
2378 if (bpsp)
2379 *bpsp = bps;
2380 }
2381
2382 return 0;
2383}
2384
2385/**
Takashi Iwai0ba21762007-04-16 11:29:14 +02002386 * snd_hda_is_supported_format - check whether the given node supports
2387 * the format val
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 *
2389 * Returns 1 if supported, 0 if not.
2390 */
2391int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
2392 unsigned int format)
2393{
2394 int i;
2395 unsigned int val = 0, rate, stream;
2396
2397 if (nid != codec->afg &&
Takashi Iwai54d17402005-11-21 16:33:22 +01002398 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
2400 if (val == -1)
2401 return 0;
2402 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002403 if (!val) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
2405 if (val == -1)
2406 return 0;
2407 }
2408
2409 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02002410 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02002411 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 if (val & (1 << i))
2413 break;
2414 return 0;
2415 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02002416 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 return 0;
2418
2419 stream = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
2420 if (stream == -1)
2421 return 0;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002422 if (!stream && nid != codec->afg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 stream = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002424 if (!stream || stream == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 return 0;
2426
2427 if (stream & AC_SUPFMT_PCM) {
2428 switch (format & 0xf0) {
2429 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002430 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 return 0;
2432 break;
2433 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002434 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 return 0;
2436 break;
2437 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002438 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 return 0;
2440 break;
2441 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002442 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 return 0;
2444 break;
2445 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002446 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 return 0;
2448 break;
2449 default:
2450 return 0;
2451 }
2452 } else {
2453 /* FIXME: check for float32 and AC3? */
2454 }
2455
2456 return 1;
2457}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002458EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459
2460/*
2461 * PCM stuff
2462 */
2463static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
2464 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002465 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466{
2467 return 0;
2468}
2469
2470static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
2471 struct hda_codec *codec,
2472 unsigned int stream_tag,
2473 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002474 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475{
2476 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
2477 return 0;
2478}
2479
2480static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
2481 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002482 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483{
Takashi Iwai888afa12008-03-18 09:57:50 +01002484 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 return 0;
2486}
2487
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002488static int set_pcm_default_values(struct hda_codec *codec,
2489 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490{
Takashi Iwai0ba21762007-04-16 11:29:14 +02002491 /* query support PCM information from the given NID */
2492 if (info->nid && (!info->rates || !info->formats)) {
2493 snd_hda_query_supported_pcm(codec, info->nid,
2494 info->rates ? NULL : &info->rates,
2495 info->formats ? NULL : &info->formats,
2496 info->maxbps ? NULL : &info->maxbps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 }
2498 if (info->ops.open == NULL)
2499 info->ops.open = hda_pcm_default_open_close;
2500 if (info->ops.close == NULL)
2501 info->ops.close = hda_pcm_default_open_close;
2502 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02002503 if (snd_BUG_ON(!info->nid))
2504 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 info->ops.prepare = hda_pcm_default_prepare;
2506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02002508 if (snd_BUG_ON(!info->nid))
2509 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 info->ops.cleanup = hda_pcm_default_cleanup;
2511 }
2512 return 0;
2513}
2514
Takashi Iwai176d5332008-07-30 15:01:44 +02002515/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002516 * get the empty PCM device number to assign
2517 */
2518static int get_empty_pcm_device(struct hda_bus *bus, int type)
2519{
2520 static const char *dev_name[HDA_PCM_NTYPES] = {
2521 "Audio", "SPDIF", "HDMI", "Modem"
2522 };
2523 /* starting device index for each PCM type */
2524 static int dev_idx[HDA_PCM_NTYPES] = {
2525 [HDA_PCM_TYPE_AUDIO] = 0,
2526 [HDA_PCM_TYPE_SPDIF] = 1,
2527 [HDA_PCM_TYPE_HDMI] = 3,
2528 [HDA_PCM_TYPE_MODEM] = 6
2529 };
2530 /* normal audio device indices; not linear to keep compatibility */
2531 static int audio_idx[4] = { 0, 2, 4, 5 };
2532 int i, dev;
2533
2534 switch (type) {
2535 case HDA_PCM_TYPE_AUDIO:
2536 for (i = 0; i < ARRAY_SIZE(audio_idx); i++) {
2537 dev = audio_idx[i];
2538 if (!test_bit(dev, bus->pcm_dev_bits))
2539 break;
2540 }
2541 if (i >= ARRAY_SIZE(audio_idx)) {
2542 snd_printk(KERN_WARNING "Too many audio devices\n");
2543 return -EAGAIN;
2544 }
2545 break;
2546 case HDA_PCM_TYPE_SPDIF:
2547 case HDA_PCM_TYPE_HDMI:
2548 case HDA_PCM_TYPE_MODEM:
2549 dev = dev_idx[type];
2550 if (test_bit(dev, bus->pcm_dev_bits)) {
2551 snd_printk(KERN_WARNING "%s already defined\n",
2552 dev_name[type]);
2553 return -EAGAIN;
2554 }
2555 break;
2556 default:
2557 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
2558 return -EINVAL;
2559 }
2560 set_bit(dev, bus->pcm_dev_bits);
2561 return dev;
2562}
2563
2564/*
Takashi Iwai176d5332008-07-30 15:01:44 +02002565 * attach a new PCM stream
2566 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002567static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02002568{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01002569 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02002570 struct hda_pcm_stream *info;
2571 int stream, err;
2572
Takashi Iwaib91f0802008-11-04 08:43:08 +01002573 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02002574 return -EINVAL;
2575 for (stream = 0; stream < 2; stream++) {
2576 info = &pcm->stream[stream];
2577 if (info->substreams) {
2578 err = set_pcm_default_values(codec, info);
2579 if (err < 0)
2580 return err;
2581 }
2582 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01002583 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02002584}
2585
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002586/* assign all PCMs of the given codec */
2587int snd_hda_codec_build_pcms(struct hda_codec *codec)
2588{
2589 unsigned int pcm;
2590 int err;
2591
2592 if (!codec->num_pcms) {
2593 if (!codec->patch_ops.build_pcms)
2594 return 0;
2595 err = codec->patch_ops.build_pcms(codec);
2596 if (err < 0)
2597 return err;
2598 }
2599 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2600 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2601 int dev;
2602
2603 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
2604 return 0; /* no substreams assigned */
2605
2606 if (!cpcm->pcm) {
2607 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
2608 if (dev < 0)
2609 return 0;
2610 cpcm->device = dev;
2611 err = snd_hda_attach_pcm(codec, cpcm);
2612 if (err < 0)
2613 return err;
2614 }
2615 }
2616 return 0;
2617}
2618
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619/**
2620 * snd_hda_build_pcms - build PCM information
2621 * @bus: the BUS
2622 *
2623 * Create PCM information for each codec included in the bus.
2624 *
2625 * The build_pcms codec patch is requested to set up codec->num_pcms and
2626 * codec->pcm_info properly. The array is referred by the top-level driver
2627 * to create its PCM instances.
2628 * The allocated codec->pcm_info should be released in codec->patch_ops.free
2629 * callback.
2630 *
2631 * At least, substreams, channels_min and channels_max must be filled for
2632 * each stream. substreams = 0 indicates that the stream doesn't exist.
2633 * When rates and/or formats are zero, the supported values are queried
2634 * from the given nid. The nid is used also by the default ops.prepare
2635 * and ops.cleanup callbacks.
2636 *
2637 * The driver needs to call ops.open in its open callback. Similarly,
2638 * ops.close is supposed to be called in the close callback.
2639 * ops.prepare should be called in the prepare or hw_params callback
2640 * with the proper parameters for set up.
2641 * ops.cleanup should be called in hw_free for clean up of streams.
2642 *
2643 * This function returns 0 if successfull, or a negative error code.
2644 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002645int __devinit snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646{
Takashi Iwai0ba21762007-04-16 11:29:14 +02002647 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648
Takashi Iwai0ba21762007-04-16 11:29:14 +02002649 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002650 int err = snd_hda_codec_build_pcms(codec);
2651 if (err < 0)
2652 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 }
2654 return 0;
2655}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002656EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658/**
2659 * snd_hda_check_board_config - compare the current codec with the config table
2660 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002661 * @num_configs: number of config enums
2662 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 * @tbl: configuration table, terminated by null entries
2664 *
2665 * Compares the modelname or PCI subsystem id of the current codec with the
2666 * given configuration table. If a matching entry is found, returns its
2667 * config value (supposed to be 0 or positive).
2668 *
2669 * If no entries are matching, the function returns a negative value.
2670 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002671int snd_hda_check_board_config(struct hda_codec *codec,
2672 int num_configs, const char **models,
2673 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674{
Takashi Iwaif44ac832008-07-30 15:01:45 +02002675 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002676 int i;
2677 for (i = 0; i < num_configs; i++) {
2678 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02002679 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002680 snd_printd(KERN_INFO "hda_codec: model '%s' is "
2681 "selected\n", models[i]);
2682 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 }
2684 }
2685 }
2686
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002687 if (!codec->bus->pci || !tbl)
2688 return -1;
2689
2690 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
2691 if (!tbl)
2692 return -1;
2693 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02002694#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002695 char tmp[10];
2696 const char *model = NULL;
2697 if (models)
2698 model = models[tbl->value];
2699 if (!model) {
2700 sprintf(tmp, "#%d", tbl->value);
2701 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002703 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
2704 "for config %x:%x (%s)\n",
2705 model, tbl->subvendor, tbl->subdevice,
2706 (tbl->name ? tbl->name : "Unknown device"));
2707#endif
2708 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 }
2710 return -1;
2711}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002712EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713
2714/**
2715 * snd_hda_add_new_ctls - create controls from the array
2716 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002717 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 *
2719 * This helper function creates and add new controls in the given array.
2720 * The array must be terminated with an empty entry as terminator.
2721 *
2722 * Returns 0 if successful, or a negative error code.
2723 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002724int snd_hda_add_new_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725{
Takashi Iwaicb53c622007-08-10 17:21:45 +02002726 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727
2728 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01002729 struct snd_kcontrol *kctl;
2730 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002731 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01002732 return -ENOMEM;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002733 err = snd_hda_ctl_add(codec, kctl);
Takashi Iwai54d17402005-11-21 16:33:22 +01002734 if (err < 0) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002735 if (!codec->addr)
Takashi Iwai54d17402005-11-21 16:33:22 +01002736 return err;
2737 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002738 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01002739 return -ENOMEM;
2740 kctl->id.device = codec->addr;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002741 err = snd_hda_ctl_add(codec, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002742 if (err < 0)
Takashi Iwai54d17402005-11-21 16:33:22 +01002743 return err;
2744 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 }
2746 return 0;
2747}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002748EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749
Takashi Iwaicb53c622007-08-10 17:21:45 +02002750#ifdef CONFIG_SND_HDA_POWER_SAVE
2751static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2752 unsigned int power_state);
2753
2754static void hda_power_work(struct work_struct *work)
2755{
2756 struct hda_codec *codec =
2757 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01002758 struct hda_bus *bus = codec->bus;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002759
Maxim Levitsky2e492462007-09-03 15:26:57 +02002760 if (!codec->power_on || codec->power_count) {
2761 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002762 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02002763 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02002764
2765 hda_call_codec_suspend(codec);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01002766 if (bus->ops.pm_notify)
2767 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002768}
2769
2770static void hda_keep_power_on(struct hda_codec *codec)
2771{
2772 codec->power_count++;
2773 codec->power_on = 1;
2774}
2775
2776void snd_hda_power_up(struct hda_codec *codec)
2777{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01002778 struct hda_bus *bus = codec->bus;
2779
Takashi Iwaicb53c622007-08-10 17:21:45 +02002780 codec->power_count++;
Takashi Iwaia221e282007-08-16 16:35:33 +02002781 if (codec->power_on || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02002782 return;
2783
2784 codec->power_on = 1;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01002785 if (bus->ops.pm_notify)
2786 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002787 hda_call_codec_resume(codec);
2788 cancel_delayed_work(&codec->power_work);
Takashi Iwaia221e282007-08-16 16:35:33 +02002789 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002790}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002791EXPORT_SYMBOL_HDA(snd_hda_power_up);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002792
2793#define power_save(codec) \
2794 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02002795
Takashi Iwaifee2fba2008-11-27 12:43:28 +01002796#define power_save(codec) \
2797 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
2798
Takashi Iwaicb53c622007-08-10 17:21:45 +02002799void snd_hda_power_down(struct hda_codec *codec)
2800{
2801 --codec->power_count;
Takashi Iwaia221e282007-08-16 16:35:33 +02002802 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02002803 return;
Takashi Iwaifee2fba2008-11-27 12:43:28 +01002804 if (power_save(codec)) {
Takashi Iwaia221e282007-08-16 16:35:33 +02002805 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002806 schedule_delayed_work(&codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01002807 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02002808 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02002809}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002810EXPORT_SYMBOL_HDA(snd_hda_power_down);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002811
2812int snd_hda_check_amp_list_power(struct hda_codec *codec,
2813 struct hda_loopback_check *check,
2814 hda_nid_t nid)
2815{
2816 struct hda_amp_list *p;
2817 int ch, v;
2818
2819 if (!check->amplist)
2820 return 0;
2821 for (p = check->amplist; p->nid; p++) {
2822 if (p->nid == nid)
2823 break;
2824 }
2825 if (!p->nid)
2826 return 0; /* nothing changed */
2827
2828 for (p = check->amplist; p->nid; p++) {
2829 for (ch = 0; ch < 2; ch++) {
2830 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
2831 p->idx);
2832 if (!(v & HDA_AMP_MUTE) && v > 0) {
2833 if (!check->power_on) {
2834 check->power_on = 1;
2835 snd_hda_power_up(codec);
2836 }
2837 return 1;
2838 }
2839 }
2840 }
2841 if (check->power_on) {
2842 check->power_on = 0;
2843 snd_hda_power_down(codec);
2844 }
2845 return 0;
2846}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002847EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002848#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002850/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002851 * Channel mode helper
2852 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002853int snd_hda_ch_mode_info(struct hda_codec *codec,
2854 struct snd_ctl_elem_info *uinfo,
2855 const struct hda_channel_mode *chmode,
2856 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002857{
2858 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2859 uinfo->count = 1;
2860 uinfo->value.enumerated.items = num_chmodes;
2861 if (uinfo->value.enumerated.item >= num_chmodes)
2862 uinfo->value.enumerated.item = num_chmodes - 1;
2863 sprintf(uinfo->value.enumerated.name, "%dch",
2864 chmode[uinfo->value.enumerated.item].channels);
2865 return 0;
2866}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002867EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002868
Takashi Iwai0ba21762007-04-16 11:29:14 +02002869int snd_hda_ch_mode_get(struct hda_codec *codec,
2870 struct snd_ctl_elem_value *ucontrol,
2871 const struct hda_channel_mode *chmode,
2872 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002873 int max_channels)
2874{
2875 int i;
2876
2877 for (i = 0; i < num_chmodes; i++) {
2878 if (max_channels == chmode[i].channels) {
2879 ucontrol->value.enumerated.item[0] = i;
2880 break;
2881 }
2882 }
2883 return 0;
2884}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002885EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002886
Takashi Iwai0ba21762007-04-16 11:29:14 +02002887int snd_hda_ch_mode_put(struct hda_codec *codec,
2888 struct snd_ctl_elem_value *ucontrol,
2889 const struct hda_channel_mode *chmode,
2890 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002891 int *max_channelsp)
2892{
2893 unsigned int mode;
2894
2895 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01002896 if (mode >= num_chmodes)
2897 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002898 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002899 return 0;
2900 /* change the current channel setting */
2901 *max_channelsp = chmode[mode].channels;
2902 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002903 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002904 return 1;
2905}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002906EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002907
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908/*
2909 * input MUX helper
2910 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002911int snd_hda_input_mux_info(const struct hda_input_mux *imux,
2912 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913{
2914 unsigned int index;
2915
2916 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2917 uinfo->count = 1;
2918 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02002919 if (!imux->num_items)
2920 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 index = uinfo->value.enumerated.item;
2922 if (index >= imux->num_items)
2923 index = imux->num_items - 1;
2924 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
2925 return 0;
2926}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002927EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928
Takashi Iwai0ba21762007-04-16 11:29:14 +02002929int snd_hda_input_mux_put(struct hda_codec *codec,
2930 const struct hda_input_mux *imux,
2931 struct snd_ctl_elem_value *ucontrol,
2932 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 unsigned int *cur_val)
2934{
2935 unsigned int idx;
2936
Takashi Iwai5513b0c2007-10-09 11:58:41 +02002937 if (!imux->num_items)
2938 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 idx = ucontrol->value.enumerated.item[0];
2940 if (idx >= imux->num_items)
2941 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002942 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002944 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
2945 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 *cur_val = idx;
2947 return 1;
2948}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002949EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950
2951
2952/*
2953 * Multi-channel / digital-out PCM helper functions
2954 */
2955
Takashi Iwai6b97eb42007-04-05 14:51:48 +02002956/* setup SPDIF output stream */
2957static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
2958 unsigned int stream_tag, unsigned int format)
2959{
2960 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Takashi Iwai2f728532008-09-25 16:32:41 +02002961 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
2962 set_dig_out_convert(codec, nid,
2963 codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff,
2964 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02002965 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02002966 if (codec->slave_dig_outs) {
2967 hda_nid_t *d;
2968 for (d = codec->slave_dig_outs; *d; d++)
2969 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
2970 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04002971 }
Takashi Iwai2f728532008-09-25 16:32:41 +02002972 /* turn on again (if needed) */
2973 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
2974 set_dig_out_convert(codec, nid,
2975 codec->spdif_ctls & 0xff, -1);
2976}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04002977
Takashi Iwai2f728532008-09-25 16:32:41 +02002978static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
2979{
2980 snd_hda_codec_cleanup_stream(codec, nid);
2981 if (codec->slave_dig_outs) {
2982 hda_nid_t *d;
2983 for (d = codec->slave_dig_outs; *d; d++)
2984 snd_hda_codec_cleanup_stream(codec, *d);
2985 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02002986}
2987
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988/*
2989 * open the digital out in the exclusive mode
2990 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002991int snd_hda_multi_out_dig_open(struct hda_codec *codec,
2992 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993{
Ingo Molnar62932df2006-01-16 16:34:20 +01002994 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02002995 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
2996 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02002997 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01002999 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 return 0;
3001}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003002EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003004int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
3005 struct hda_multi_out *mout,
3006 unsigned int stream_tag,
3007 unsigned int format,
3008 struct snd_pcm_substream *substream)
3009{
3010 mutex_lock(&codec->spdif_mutex);
3011 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
3012 mutex_unlock(&codec->spdif_mutex);
3013 return 0;
3014}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003015EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003016
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017/*
3018 * release the digital out
3019 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003020int snd_hda_multi_out_dig_close(struct hda_codec *codec,
3021 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022{
Ingo Molnar62932df2006-01-16 16:34:20 +01003023 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01003025 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 return 0;
3027}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003028EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029
3030/*
3031 * set up more restrictions for analog out
3032 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003033int snd_hda_multi_out_analog_open(struct hda_codec *codec,
3034 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01003035 struct snd_pcm_substream *substream,
3036 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037{
Takashi Iwai9a081602008-02-12 18:37:26 +01003038 struct snd_pcm_runtime *runtime = substream->runtime;
3039 runtime->hw.channels_max = mout->max_channels;
3040 if (mout->dig_out_nid) {
3041 if (!mout->analog_rates) {
3042 mout->analog_rates = hinfo->rates;
3043 mout->analog_formats = hinfo->formats;
3044 mout->analog_maxbps = hinfo->maxbps;
3045 } else {
3046 runtime->hw.rates = mout->analog_rates;
3047 runtime->hw.formats = mout->analog_formats;
3048 hinfo->maxbps = mout->analog_maxbps;
3049 }
3050 if (!mout->spdif_rates) {
3051 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
3052 &mout->spdif_rates,
3053 &mout->spdif_formats,
3054 &mout->spdif_maxbps);
3055 }
3056 mutex_lock(&codec->spdif_mutex);
3057 if (mout->share_spdif) {
3058 runtime->hw.rates &= mout->spdif_rates;
3059 runtime->hw.formats &= mout->spdif_formats;
3060 if (mout->spdif_maxbps < hinfo->maxbps)
3061 hinfo->maxbps = mout->spdif_maxbps;
3062 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02003063 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01003064 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 return snd_pcm_hw_constraint_step(substream->runtime, 0,
3066 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
3067}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003068EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069
3070/*
3071 * set up the i/o for analog out
3072 * when the digital out is available, copy the front out to digital out, too.
3073 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003074int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
3075 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 unsigned int stream_tag,
3077 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003078 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079{
3080 hda_nid_t *nids = mout->dac_nids;
3081 int chs = substream->runtime->channels;
3082 int i;
3083
Ingo Molnar62932df2006-01-16 16:34:20 +01003084 mutex_lock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01003085 if (mout->dig_out_nid && mout->share_spdif &&
3086 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02003088 snd_hda_is_supported_format(codec, mout->dig_out_nid,
3089 format) &&
3090 !(codec->spdif_status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003092 setup_dig_out_stream(codec, mout->dig_out_nid,
3093 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 } else {
3095 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02003096 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 }
3098 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003099 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100
3101 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003102 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
3103 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02003104 if (!mout->no_share_stream &&
3105 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003107 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
3108 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01003109 /* extra outputs copied from front */
3110 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02003111 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01003112 snd_hda_codec_setup_stream(codec,
3113 mout->extra_out_nid[i],
3114 stream_tag, 0, format);
3115
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 /* surrounds */
3117 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02003118 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003119 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
3120 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02003121 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003122 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
3123 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 }
3125 return 0;
3126}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003127EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128
3129/*
3130 * clean up the setting for analog out
3131 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003132int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
3133 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134{
3135 hda_nid_t *nids = mout->dac_nids;
3136 int i;
3137
3138 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01003139 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01003141 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwai82bc9552006-03-21 11:24:42 +01003142 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
3143 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01003144 snd_hda_codec_cleanup_stream(codec,
3145 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01003146 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02003148 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 mout->dig_out_used = 0;
3150 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003151 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 return 0;
3153}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003154EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003156/*
Wu Fengguang6b345002008-10-07 14:21:41 +08003157 * Helper for automatic pin configuration
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003158 */
Kailang Yangdf694da2005-12-05 19:42:22 +01003159
Takashi Iwai12f288b2007-08-02 15:51:59 +02003160static int is_in_nid_list(hda_nid_t nid, hda_nid_t *list)
Kailang Yangdf694da2005-12-05 19:42:22 +01003161{
3162 for (; *list; list++)
3163 if (*list == nid)
3164 return 1;
3165 return 0;
3166}
3167
Steve Longerbeam81937d32007-05-08 15:33:03 +02003168
3169/*
3170 * Sort an associated group of pins according to their sequence numbers.
3171 */
3172static void sort_pins_by_sequence(hda_nid_t * pins, short * sequences,
3173 int num_pins)
3174{
3175 int i, j;
3176 short seq;
3177 hda_nid_t nid;
3178
3179 for (i = 0; i < num_pins; i++) {
3180 for (j = i + 1; j < num_pins; j++) {
3181 if (sequences[i] > sequences[j]) {
3182 seq = sequences[i];
3183 sequences[i] = sequences[j];
3184 sequences[j] = seq;
3185 nid = pins[i];
3186 pins[i] = pins[j];
3187 pins[j] = nid;
3188 }
3189 }
3190 }
3191}
3192
3193
Takashi Iwai82bc9552006-03-21 11:24:42 +01003194/*
3195 * Parse all pin widgets and store the useful pin nids to cfg
3196 *
3197 * The number of line-outs or any primary output is stored in line_outs,
3198 * and the corresponding output pins are assigned to line_out_pins[],
3199 * in the order of front, rear, CLFE, side, ...
3200 *
3201 * If more extra outputs (speaker and headphone) are found, the pins are
Takashi Iwaieb06ed82006-09-20 17:10:27 +02003202 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
Takashi Iwai82bc9552006-03-21 11:24:42 +01003203 * is detected, one of speaker of HP pins is assigned as the primary
3204 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
3205 * if any analog output exists.
3206 *
3207 * The analog input pins are assigned to input_pins array.
3208 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
3209 * respectively.
3210 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003211int snd_hda_parse_pin_def_config(struct hda_codec *codec,
3212 struct auto_pin_cfg *cfg,
3213 hda_nid_t *ignore_nids)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003214{
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01003215 hda_nid_t nid, end_nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02003216 short seq, assoc_line_out, assoc_speaker;
3217 short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
3218 short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
Takashi Iwaif889fa92007-10-31 15:49:32 +01003219 short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003220
3221 memset(cfg, 0, sizeof(*cfg));
3222
Steve Longerbeam81937d32007-05-08 15:33:03 +02003223 memset(sequences_line_out, 0, sizeof(sequences_line_out));
3224 memset(sequences_speaker, 0, sizeof(sequences_speaker));
Takashi Iwaif889fa92007-10-31 15:49:32 +01003225 memset(sequences_hp, 0, sizeof(sequences_hp));
Steve Longerbeam81937d32007-05-08 15:33:03 +02003226 assoc_line_out = assoc_speaker = 0;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003227
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01003228 end_nid = codec->start_nid + codec->num_nodes;
3229 for (nid = codec->start_nid; nid < end_nid; nid++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01003230 unsigned int wid_caps = get_wcaps(codec, nid);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003231 unsigned int wid_type =
3232 (wid_caps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003233 unsigned int def_conf;
3234 short assoc, loc;
3235
3236 /* read all default configuration for pin complex */
3237 if (wid_type != AC_WID_PIN)
3238 continue;
Kailang Yangdf694da2005-12-05 19:42:22 +01003239 /* ignore the given nids (e.g. pc-beep returns error) */
3240 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
3241 continue;
3242
Takashi Iwai0ba21762007-04-16 11:29:14 +02003243 def_conf = snd_hda_codec_read(codec, nid, 0,
3244 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003245 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
3246 continue;
3247 loc = get_defcfg_location(def_conf);
3248 switch (get_defcfg_device(def_conf)) {
3249 case AC_JACK_LINE_OUT:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003250 seq = get_defcfg_sequence(def_conf);
3251 assoc = get_defcfg_association(def_conf);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01003252
3253 if (!(wid_caps & AC_WCAP_STEREO))
3254 if (!cfg->mono_out_pin)
3255 cfg->mono_out_pin = nid;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003256 if (!assoc)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003257 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003258 if (!assoc_line_out)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003259 assoc_line_out = assoc;
3260 else if (assoc_line_out != assoc)
3261 continue;
3262 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
3263 continue;
3264 cfg->line_out_pins[cfg->line_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02003265 sequences_line_out[cfg->line_outs] = seq;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003266 cfg->line_outs++;
3267 break;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01003268 case AC_JACK_SPEAKER:
Steve Longerbeam81937d32007-05-08 15:33:03 +02003269 seq = get_defcfg_sequence(def_conf);
3270 assoc = get_defcfg_association(def_conf);
3271 if (! assoc)
3272 continue;
3273 if (! assoc_speaker)
3274 assoc_speaker = assoc;
3275 else if (assoc_speaker != assoc)
3276 continue;
Takashi Iwai82bc9552006-03-21 11:24:42 +01003277 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
3278 continue;
3279 cfg->speaker_pins[cfg->speaker_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02003280 sequences_speaker[cfg->speaker_outs] = seq;
Takashi Iwai82bc9552006-03-21 11:24:42 +01003281 cfg->speaker_outs++;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01003282 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003283 case AC_JACK_HP_OUT:
Takashi Iwaif889fa92007-10-31 15:49:32 +01003284 seq = get_defcfg_sequence(def_conf);
3285 assoc = get_defcfg_association(def_conf);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02003286 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
3287 continue;
3288 cfg->hp_pins[cfg->hp_outs] = nid;
Takashi Iwaif889fa92007-10-31 15:49:32 +01003289 sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
Takashi Iwaieb06ed82006-09-20 17:10:27 +02003290 cfg->hp_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003291 break;
Takashi Iwai314634b2006-09-21 11:56:18 +02003292 case AC_JACK_MIC_IN: {
3293 int preferred, alt;
3294 if (loc == AC_JACK_LOC_FRONT) {
3295 preferred = AUTO_PIN_FRONT_MIC;
3296 alt = AUTO_PIN_MIC;
3297 } else {
3298 preferred = AUTO_PIN_MIC;
3299 alt = AUTO_PIN_FRONT_MIC;
3300 }
3301 if (!cfg->input_pins[preferred])
3302 cfg->input_pins[preferred] = nid;
3303 else if (!cfg->input_pins[alt])
3304 cfg->input_pins[alt] = nid;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003305 break;
Takashi Iwai314634b2006-09-21 11:56:18 +02003306 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003307 case AC_JACK_LINE_IN:
3308 if (loc == AC_JACK_LOC_FRONT)
3309 cfg->input_pins[AUTO_PIN_FRONT_LINE] = nid;
3310 else
3311 cfg->input_pins[AUTO_PIN_LINE] = nid;
3312 break;
3313 case AC_JACK_CD:
3314 cfg->input_pins[AUTO_PIN_CD] = nid;
3315 break;
3316 case AC_JACK_AUX:
3317 cfg->input_pins[AUTO_PIN_AUX] = nid;
3318 break;
3319 case AC_JACK_SPDIF_OUT:
3320 cfg->dig_out_pin = nid;
3321 break;
3322 case AC_JACK_SPDIF_IN:
3323 cfg->dig_in_pin = nid;
3324 break;
3325 }
3326 }
3327
Takashi Iwai5832fcf2008-02-12 18:30:12 +01003328 /* FIX-UP:
3329 * If no line-out is defined but multiple HPs are found,
3330 * some of them might be the real line-outs.
3331 */
3332 if (!cfg->line_outs && cfg->hp_outs > 1) {
3333 int i = 0;
3334 while (i < cfg->hp_outs) {
3335 /* The real HPs should have the sequence 0x0f */
3336 if ((sequences_hp[i] & 0x0f) == 0x0f) {
3337 i++;
3338 continue;
3339 }
3340 /* Move it to the line-out table */
3341 cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
3342 sequences_line_out[cfg->line_outs] = sequences_hp[i];
3343 cfg->line_outs++;
3344 cfg->hp_outs--;
3345 memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
3346 sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
3347 memmove(sequences_hp + i - 1, sequences_hp + i,
3348 sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
3349 }
3350 }
3351
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003352 /* sort by sequence */
Steve Longerbeam81937d32007-05-08 15:33:03 +02003353 sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
3354 cfg->line_outs);
3355 sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
3356 cfg->speaker_outs);
Takashi Iwaif889fa92007-10-31 15:49:32 +01003357 sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
3358 cfg->hp_outs);
Steve Longerbeam81937d32007-05-08 15:33:03 +02003359
Takashi Iwaif889fa92007-10-31 15:49:32 +01003360 /* if we have only one mic, make it AUTO_PIN_MIC */
3361 if (!cfg->input_pins[AUTO_PIN_MIC] &&
3362 cfg->input_pins[AUTO_PIN_FRONT_MIC]) {
3363 cfg->input_pins[AUTO_PIN_MIC] =
3364 cfg->input_pins[AUTO_PIN_FRONT_MIC];
3365 cfg->input_pins[AUTO_PIN_FRONT_MIC] = 0;
3366 }
3367 /* ditto for line-in */
3368 if (!cfg->input_pins[AUTO_PIN_LINE] &&
3369 cfg->input_pins[AUTO_PIN_FRONT_LINE]) {
3370 cfg->input_pins[AUTO_PIN_LINE] =
3371 cfg->input_pins[AUTO_PIN_FRONT_LINE];
3372 cfg->input_pins[AUTO_PIN_FRONT_LINE] = 0;
3373 }
3374
Steve Longerbeam81937d32007-05-08 15:33:03 +02003375 /*
3376 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
3377 * as a primary output
3378 */
3379 if (!cfg->line_outs) {
3380 if (cfg->speaker_outs) {
3381 cfg->line_outs = cfg->speaker_outs;
3382 memcpy(cfg->line_out_pins, cfg->speaker_pins,
3383 sizeof(cfg->speaker_pins));
3384 cfg->speaker_outs = 0;
3385 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
3386 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
3387 } else if (cfg->hp_outs) {
3388 cfg->line_outs = cfg->hp_outs;
3389 memcpy(cfg->line_out_pins, cfg->hp_pins,
3390 sizeof(cfg->hp_pins));
3391 cfg->hp_outs = 0;
3392 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
3393 cfg->line_out_type = AUTO_PIN_HP_OUT;
3394 }
3395 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003396
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02003397 /* Reorder the surround channels
3398 * ALSA sequence is front/surr/clfe/side
3399 * HDA sequence is:
3400 * 4-ch: front/surr => OK as it is
3401 * 6-ch: front/clfe/surr
Takashi Iwai9422db42007-04-20 16:11:43 +02003402 * 8-ch: front/clfe/rear/side|fc
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02003403 */
3404 switch (cfg->line_outs) {
3405 case 3:
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02003406 case 4:
3407 nid = cfg->line_out_pins[1];
Takashi Iwai9422db42007-04-20 16:11:43 +02003408 cfg->line_out_pins[1] = cfg->line_out_pins[2];
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02003409 cfg->line_out_pins[2] = nid;
3410 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003411 }
3412
Takashi Iwai82bc9552006-03-21 11:24:42 +01003413 /*
3414 * debug prints of the parsed results
3415 */
3416 snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3417 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
3418 cfg->line_out_pins[2], cfg->line_out_pins[3],
3419 cfg->line_out_pins[4]);
3420 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3421 cfg->speaker_outs, cfg->speaker_pins[0],
3422 cfg->speaker_pins[1], cfg->speaker_pins[2],
3423 cfg->speaker_pins[3], cfg->speaker_pins[4]);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02003424 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3425 cfg->hp_outs, cfg->hp_pins[0],
3426 cfg->hp_pins[1], cfg->hp_pins[2],
3427 cfg->hp_pins[3], cfg->hp_pins[4]);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01003428 snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
Takashi Iwai82bc9552006-03-21 11:24:42 +01003429 snd_printd(" inputs: mic=0x%x, fmic=0x%x, line=0x%x, fline=0x%x,"
3430 " cd=0x%x, aux=0x%x\n",
3431 cfg->input_pins[AUTO_PIN_MIC],
3432 cfg->input_pins[AUTO_PIN_FRONT_MIC],
3433 cfg->input_pins[AUTO_PIN_LINE],
3434 cfg->input_pins[AUTO_PIN_FRONT_LINE],
3435 cfg->input_pins[AUTO_PIN_CD],
3436 cfg->input_pins[AUTO_PIN_AUX]);
3437
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003438 return 0;
3439}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003440EXPORT_SYMBOL_HDA(snd_hda_parse_pin_def_config);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003441
Takashi Iwai4a471b72005-12-07 13:56:29 +01003442/* labels for input pins */
3443const char *auto_pin_cfg_labels[AUTO_PIN_LAST] = {
3444 "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux"
3445};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003446EXPORT_SYMBOL_HDA(auto_pin_cfg_labels);
Takashi Iwai4a471b72005-12-07 13:56:29 +01003447
3448
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449#ifdef CONFIG_PM
3450/*
3451 * power management
3452 */
3453
3454/**
3455 * snd_hda_suspend - suspend the codecs
3456 * @bus: the HDA bus
3457 * @state: suspsend state
3458 *
3459 * Returns 0 if successful.
3460 */
3461int snd_hda_suspend(struct hda_bus *bus, pm_message_t state)
3462{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003463 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464
Takashi Iwai0ba21762007-04-16 11:29:14 +02003465 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai0b7a2e92007-08-14 15:18:26 +02003466#ifdef CONFIG_SND_HDA_POWER_SAVE
3467 if (!codec->power_on)
3468 continue;
3469#endif
Takashi Iwaicb53c622007-08-10 17:21:45 +02003470 hda_call_codec_suspend(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 }
3472 return 0;
3473}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003474EXPORT_SYMBOL_HDA(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475
3476/**
3477 * snd_hda_resume - resume the codecs
3478 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479 *
3480 * Returns 0 if successful.
Takashi Iwaicb53c622007-08-10 17:21:45 +02003481 *
3482 * This fucntion is defined only when POWER_SAVE isn't set.
3483 * In the power-save mode, the codec is resumed dynamically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 */
3485int snd_hda_resume(struct hda_bus *bus)
3486{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003487 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488
Takashi Iwai0ba21762007-04-16 11:29:14 +02003489 list_for_each_entry(codec, &bus->codec_list, list) {
Maxim Levitskyd804ad92007-09-03 15:28:04 +02003490 if (snd_hda_codec_needs_resume(codec))
3491 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 return 0;
3494}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003495EXPORT_SYMBOL_HDA(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003496#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02003497
3498/*
3499 * generic arrays
3500 */
3501
3502/* get a new element from the given array
3503 * if it exceeds the pre-allocated array size, re-allocate the array
3504 */
3505void *snd_array_new(struct snd_array *array)
3506{
3507 if (array->used >= array->alloced) {
3508 int num = array->alloced + array->alloc_align;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01003509 void *nlist;
3510 if (snd_BUG_ON(num >= 4096))
3511 return NULL;
3512 nlist = kcalloc(num + 1, array->elem_size, GFP_KERNEL);
Takashi Iwaib2e18592008-07-30 15:01:44 +02003513 if (!nlist)
3514 return NULL;
3515 if (array->list) {
3516 memcpy(nlist, array->list,
3517 array->elem_size * array->alloced);
3518 kfree(array->list);
3519 }
3520 array->list = nlist;
3521 array->alloced = num;
3522 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01003523 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02003524}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003525EXPORT_SYMBOL_HDA(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02003526
3527/* free the given array elements */
3528void snd_array_free(struct snd_array *array)
3529{
3530 kfree(array->list);
3531 array->used = 0;
3532 array->alloced = 0;
3533 array->list = NULL;
3534}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003535EXPORT_SYMBOL_HDA(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01003536
3537/*
3538 * used by hda_proc.c and hda_eld.c
3539 */
3540void snd_print_pcm_rates(int pcm, char *buf, int buflen)
3541{
3542 static unsigned int rates[] = {
3543 8000, 11025, 16000, 22050, 32000, 44100, 48000, 88200,
3544 96000, 176400, 192000, 384000
3545 };
3546 int i, j;
3547
3548 for (i = 0, j = 0; i < ARRAY_SIZE(rates); i++)
3549 if (pcm & (1 << i))
3550 j += snprintf(buf + j, buflen - j, " %d", rates[i]);
3551
3552 buf[j] = '\0'; /* necessary when j == 0 */
3553}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003554EXPORT_SYMBOL_HDA(snd_print_pcm_rates);
Takashi Iwaib2022262008-11-21 21:24:03 +01003555
3556void snd_print_pcm_bits(int pcm, char *buf, int buflen)
3557{
3558 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
3559 int i, j;
3560
3561 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
3562 if (pcm & (AC_SUPPCM_BITS_8 << i))
3563 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
3564
3565 buf[j] = '\0'; /* necessary when j == 0 */
3566}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003567EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003568
3569MODULE_DESCRIPTION("HDA codec core");
3570MODULE_LICENSE("GPL");