blob: 625fe5984dd6b9852b8a515890b2ee9219891263 [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 Iwai756e2b02007-04-16 11:27:07 +0200712 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 Iwai6c1f45e2008-07-30 15:01:45 +0200796 err = snd_hda_codec_configure(codec);
797 if (err < 0) {
798 snd_hda_codec_free(codec);
799 return err;
800 }
801 snd_hda_codec_proc_new(codec);
802
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200803 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200804
805 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
806 codec->subsystem_id, codec->revision_id);
807 snd_component_add(codec->bus->card, component);
808
809 if (codecp)
810 *codecp = codec;
811 return 0;
812}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100813EXPORT_SYMBOL_HDA(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200814
815int snd_hda_codec_configure(struct hda_codec *codec)
816{
817 int err;
818
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100819 codec->preset = find_codec_preset(codec);
Takashi Iwaif44ac832008-07-30 15:01:45 +0200820 if (!codec->name) {
821 err = get_codec_name(codec);
822 if (err < 0)
823 return err;
824 }
Takashi Iwai43ea1d42007-04-26 19:12:08 +0200825 /* audio codec should override the mixer name */
Takashi Iwaif44ac832008-07-30 15:01:45 +0200826 if (codec->afg || !*codec->bus->card->mixername)
827 strlcpy(codec->bus->card->mixername, codec->name,
828 sizeof(codec->bus->card->mixername));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Takashi Iwai82467612007-07-27 19:15:54 +0200830 if (is_generic_config(codec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai82467612007-07-27 19:15:54 +0200832 goto patched;
833 }
Takashi Iwai82467612007-07-27 19:15:54 +0200834 if (codec->preset && codec->preset->patch) {
835 err = codec->preset->patch(codec);
836 goto patched;
837 }
838
839 /* call the default parser */
Takashi Iwai82467612007-07-27 19:15:54 +0200840 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai35a1e0c2007-10-19 08:13:40 +0200841 if (err < 0)
842 printk(KERN_ERR "hda-codec: No codec parser is available\n");
Takashi Iwai82467612007-07-27 19:15:54 +0200843
844 patched:
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200845 if (!err && codec->patch_ops.unsol_event)
846 err = init_unsol_queue(codec->bus);
847 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848}
849
850/**
851 * snd_hda_codec_setup_stream - set up the codec for streaming
852 * @codec: the CODEC to set up
853 * @nid: the NID to set up
854 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
855 * @channel_id: channel id to pass, zero based.
856 * @format: stream format.
857 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200858void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
859 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 int channel_id, int format)
861{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200862 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +0200863 return;
864
Takashi Iwai0ba21762007-04-16 11:29:14 +0200865 snd_printdd("hda_codec_setup_stream: "
866 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 nid, stream_tag, channel_id, format);
868 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID,
869 (stream_tag << 4) | channel_id);
870 msleep(1);
871 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, format);
872}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100873EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Takashi Iwai888afa12008-03-18 09:57:50 +0100875void snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid)
876{
877 if (!nid)
878 return;
879
880 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
881 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
882#if 0 /* keep the format */
883 msleep(1);
884 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0);
885#endif
886}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100887EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +0100888
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889/*
890 * amp access functions
891 */
892
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200893/* FIXME: more better hash key? */
894#define HDA_HASH_KEY(nid,dir,idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200896#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
898/* initialize the hash table */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100899static void /*__devinit*/ init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +0200900 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901{
Takashi Iwai01751f52007-08-10 16:59:39 +0200902 memset(cache, 0, sizeof(*cache));
903 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +0200904 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +0200905}
906
Takashi Iwai1fcaee62007-08-23 00:01:09 +0200907static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +0200908{
Takashi Iwai603c4012008-07-30 15:01:44 +0200909 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910}
911
912/* query the hash. allocate an entry if not found. */
Takashi Iwai01751f52007-08-10 16:59:39 +0200913static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
914 u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915{
Takashi Iwai01751f52007-08-10 16:59:39 +0200916 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
917 u16 cur = cache->hash[idx];
918 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
920 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +0100921 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 if (info->key == key)
923 return info;
924 cur = info->next;
925 }
926
927 /* add a new hash entry */
Takashi Iwai603c4012008-07-30 15:01:44 +0200928 info = snd_array_new(&cache->buf);
Takashi Iwaic2174292008-11-07 00:23:30 +0100929 if (!info)
930 return NULL;
Takashi Iwaif43aa022008-11-10 16:24:26 +0100931 cur = snd_array_index(&cache->buf, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 info->key = key;
Takashi Iwai01751f52007-08-10 16:59:39 +0200933 info->val = 0;
934 info->next = cache->hash[idx];
935 cache->hash[idx] = cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
937 return info;
938}
939
Takashi Iwai01751f52007-08-10 16:59:39 +0200940/* query and allocate an amp hash entry */
941static inline struct hda_amp_info *
942get_alloc_amp_hash(struct hda_codec *codec, u32 key)
943{
944 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
945}
946
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947/*
948 * query AMP capabilities for the given widget and direction
949 */
Matthew Ranostay09a99952008-01-24 11:49:21 +0100950u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200952 struct hda_amp_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
Takashi Iwai0ba21762007-04-16 11:29:14 +0200954 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
955 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 return 0;
Takashi Iwai01751f52007-08-10 16:59:39 +0200957 if (!(info->head.val & INFO_AMP_CAPS)) {
Takashi Iwai0ba21762007-04-16 11:29:14 +0200958 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 nid = codec->afg;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200960 info->amp_caps = snd_hda_param_read(codec, nid,
961 direction == HDA_OUTPUT ?
962 AC_PAR_AMP_OUT_CAP :
963 AC_PAR_AMP_IN_CAP);
Takashi Iwaib75e53f2007-05-10 16:56:09 +0200964 if (info->amp_caps)
Takashi Iwai01751f52007-08-10 16:59:39 +0200965 info->head.val |= INFO_AMP_CAPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 }
967 return info->amp_caps;
968}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100969EXPORT_SYMBOL_HDA(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Takashi Iwai897cc182007-05-29 19:01:37 +0200971int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
972 unsigned int caps)
973{
974 struct hda_amp_info *info;
975
976 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
977 if (!info)
978 return -EINVAL;
979 info->amp_caps = caps;
Takashi Iwai01751f52007-08-10 16:59:39 +0200980 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai897cc182007-05-29 19:01:37 +0200981 return 0;
982}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100983EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +0200984
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985/*
986 * read the current volume to info
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200987 * if the cache exists, read the cache value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200989static unsigned int get_vol_mute(struct hda_codec *codec,
990 struct hda_amp_info *info, hda_nid_t nid,
991 int ch, int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992{
993 u32 val, parm;
994
Takashi Iwai01751f52007-08-10 16:59:39 +0200995 if (info->head.val & INFO_AMP_VOL(ch))
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200996 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
998 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
999 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1000 parm |= index;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001001 val = snd_hda_codec_read(codec, nid, 0,
1002 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 info->vol[ch] = val & 0xff;
Takashi Iwai01751f52007-08-10 16:59:39 +02001004 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001005 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006}
1007
1008/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001009 * write the current volume in info to the h/w and update the cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001011static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001012 hda_nid_t nid, int ch, int direction, int index,
1013 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014{
1015 u32 parm;
1016
1017 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1018 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1019 parm |= index << AC_AMP_SET_INDEX_SHIFT;
1020 parm |= val;
1021 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001022 info->vol[ch] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023}
1024
1025/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001026 * read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 */
Takashi Iwai834be882006-03-01 14:16:17 +01001028int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1029 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001031 struct hda_amp_info *info;
1032 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1033 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001035 return get_vol_mute(codec, info, nid, ch, direction, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001037EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001039/*
1040 * update the AMP value, mask = bit mask to set, val = the value
1041 */
Takashi Iwai834be882006-03-01 14:16:17 +01001042int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1043 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001045 struct hda_amp_info *info;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001046
Takashi Iwai0ba21762007-04-16 11:29:14 +02001047 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
1048 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001050 val &= mask;
1051 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001052 if (info->vol[ch] == val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001054 put_vol_mute(codec, info, nid, ch, direction, idx, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 return 1;
1056}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001057EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
Takashi Iwai47fd8302007-08-10 17:11:07 +02001059/*
1060 * update the AMP stereo with the same mask and value
1061 */
1062int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1063 int direction, int idx, int mask, int val)
1064{
1065 int ch, ret = 0;
1066 for (ch = 0; ch < 2; ch++)
1067 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1068 idx, mask, val);
1069 return ret;
1070}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001071EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001072
Takashi Iwaicb53c622007-08-10 17:21:45 +02001073#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001074/* resume the all amp commands from the cache */
1075void snd_hda_codec_resume_amp(struct hda_codec *codec)
1076{
Takashi Iwai603c4012008-07-30 15:01:44 +02001077 struct hda_amp_info *buffer = codec->amp_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001078 int i;
1079
Takashi Iwai603c4012008-07-30 15:01:44 +02001080 for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001081 u32 key = buffer->head.key;
1082 hda_nid_t nid;
1083 unsigned int idx, dir, ch;
1084 if (!key)
1085 continue;
1086 nid = key & 0xff;
1087 idx = (key >> 16) & 0xff;
1088 dir = (key >> 24) & 0xff;
1089 for (ch = 0; ch < 2; ch++) {
1090 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
1091 continue;
1092 put_vol_mute(codec, buffer, nid, ch, dir, idx,
1093 buffer->vol[ch]);
1094 }
1095 }
1096}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001097EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001098#endif /* SND_HDA_NEEDS_RESUME */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100/* volume */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001101int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1102 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103{
1104 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1105 u16 nid = get_amp_nid(kcontrol);
1106 u8 chs = get_amp_channels(kcontrol);
1107 int dir = get_amp_direction(kcontrol);
1108 u32 caps;
1109
1110 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001111 /* num steps */
1112 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1113 if (!caps) {
1114 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01001115 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
1116 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 return -EINVAL;
1118 }
1119 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1120 uinfo->count = chs == 3 ? 2 : 1;
1121 uinfo->value.integer.min = 0;
1122 uinfo->value.integer.max = caps;
1123 return 0;
1124}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001125EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
Takashi Iwai0ba21762007-04-16 11:29:14 +02001127int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1128 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129{
1130 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1131 hda_nid_t nid = get_amp_nid(kcontrol);
1132 int chs = get_amp_channels(kcontrol);
1133 int dir = get_amp_direction(kcontrol);
1134 int idx = get_amp_index(kcontrol);
1135 long *valp = ucontrol->value.integer.value;
1136
1137 if (chs & 1)
Takashi Iwai47fd8302007-08-10 17:11:07 +02001138 *valp++ = snd_hda_codec_amp_read(codec, nid, 0, dir, idx)
1139 & HDA_AMP_VOLMASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 if (chs & 2)
Takashi Iwai47fd8302007-08-10 17:11:07 +02001141 *valp = snd_hda_codec_amp_read(codec, nid, 1, dir, idx)
1142 & HDA_AMP_VOLMASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 return 0;
1144}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001145EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
Takashi Iwai0ba21762007-04-16 11:29:14 +02001147int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1148 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149{
1150 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1151 hda_nid_t nid = get_amp_nid(kcontrol);
1152 int chs = get_amp_channels(kcontrol);
1153 int dir = get_amp_direction(kcontrol);
1154 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 long *valp = ucontrol->value.integer.value;
1156 int change = 0;
1157
Takashi Iwaicb53c622007-08-10 17:21:45 +02001158 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001159 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001160 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
1161 0x7f, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001162 valp++;
1163 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001164 if (chs & 2)
1165 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001166 0x7f, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001167 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 return change;
1169}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001170EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001172int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1173 unsigned int size, unsigned int __user *_tlv)
1174{
1175 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1176 hda_nid_t nid = get_amp_nid(kcontrol);
1177 int dir = get_amp_direction(kcontrol);
1178 u32 caps, val1, val2;
1179
1180 if (size < 4 * sizeof(unsigned int))
1181 return -ENOMEM;
1182 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001183 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1184 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001185 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
1186 val1 = ((int)val1) * ((int)val2);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001187 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
1188 return -EFAULT;
1189 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
1190 return -EFAULT;
1191 if (put_user(val1, _tlv + 2))
1192 return -EFAULT;
1193 if (put_user(val2, _tlv + 3))
1194 return -EFAULT;
1195 return 0;
1196}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001197EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001198
Takashi Iwai2134ea42008-01-10 16:53:55 +01001199/*
1200 * set (static) TLV for virtual master volume; recalculated as max 0dB
1201 */
1202void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
1203 unsigned int *tlv)
1204{
1205 u32 caps;
1206 int nums, step;
1207
1208 caps = query_amp_caps(codec, nid, dir);
1209 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1210 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1211 step = (step + 1) * 25;
1212 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
1213 tlv[1] = 2 * sizeof(unsigned int);
1214 tlv[2] = -nums * step;
1215 tlv[3] = step;
1216}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001217EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001218
1219/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01001220static struct snd_kcontrol *
1221_snd_hda_find_mixer_ctl(struct hda_codec *codec,
1222 const char *name, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01001223{
1224 struct snd_ctl_elem_id id;
1225 memset(&id, 0, sizeof(id));
1226 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwai09f99702008-02-04 12:31:13 +01001227 id.index = idx;
Takashi Iwai2134ea42008-01-10 16:53:55 +01001228 strcpy(id.name, name);
1229 return snd_ctl_find_id(codec->bus->card, &id);
1230}
1231
Takashi Iwai09f99702008-02-04 12:31:13 +01001232struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
1233 const char *name)
1234{
1235 return _snd_hda_find_mixer_ctl(codec, name, 0);
1236}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001237EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01001238
Takashi Iwaid13bd412008-07-30 15:01:45 +02001239/* Add a control element and assign to the codec */
1240int snd_hda_ctl_add(struct hda_codec *codec, struct snd_kcontrol *kctl)
1241{
1242 int err;
1243 struct snd_kcontrol **knewp;
1244
1245 err = snd_ctl_add(codec->bus->card, kctl);
1246 if (err < 0)
1247 return err;
1248 knewp = snd_array_new(&codec->mixers);
1249 if (!knewp)
1250 return -ENOMEM;
1251 *knewp = kctl;
1252 return 0;
1253}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001254EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001255
Takashi Iwai529bd6c2008-11-27 14:17:01 +01001256#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwaid13bd412008-07-30 15:01:45 +02001257/* Clear all controls assigned to the given codec */
1258void snd_hda_ctls_clear(struct hda_codec *codec)
1259{
1260 int i;
1261 struct snd_kcontrol **kctls = codec->mixers.list;
1262 for (i = 0; i < codec->mixers.used; i++)
1263 snd_ctl_remove(codec->bus->card, kctls[i]);
1264 snd_array_free(&codec->mixers);
1265}
1266
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001267void snd_hda_codec_reset(struct hda_codec *codec)
1268{
1269 int i;
1270
1271#ifdef CONFIG_SND_HDA_POWER_SAVE
1272 cancel_delayed_work(&codec->power_work);
1273 flush_scheduled_work();
1274#endif
1275 snd_hda_ctls_clear(codec);
1276 /* relase PCMs */
1277 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01001278 if (codec->pcm_info[i].pcm) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001279 snd_device_free(codec->bus->card,
1280 codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01001281 clear_bit(codec->pcm_info[i].device,
1282 codec->bus->pcm_dev_bits);
1283 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001284 }
1285 if (codec->patch_ops.free)
1286 codec->patch_ops.free(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01001287 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001288 codec->spec = NULL;
1289 free_hda_cache(&codec->amp_cache);
1290 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01001291 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
1292 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001293 codec->num_pcms = 0;
1294 codec->pcm_info = NULL;
1295 codec->preset = NULL;
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001296 module_put(codec->owner);
1297 codec->owner = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001298}
Takashi Iwai529bd6c2008-11-27 14:17:01 +01001299#endif /* CONFIG_SND_HDA_RECONFIG */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001300
Takashi Iwai2134ea42008-01-10 16:53:55 +01001301/* create a virtual master control and add slaves */
1302int snd_hda_add_vmaster(struct hda_codec *codec, char *name,
1303 unsigned int *tlv, const char **slaves)
1304{
1305 struct snd_kcontrol *kctl;
1306 const char **s;
1307 int err;
1308
Takashi Iwai2f085542008-02-22 18:43:50 +01001309 for (s = slaves; *s && !snd_hda_find_mixer_ctl(codec, *s); s++)
1310 ;
1311 if (!*s) {
1312 snd_printdd("No slave found for %s\n", name);
1313 return 0;
1314 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01001315 kctl = snd_ctl_make_virtual_master(name, tlv);
1316 if (!kctl)
1317 return -ENOMEM;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001318 err = snd_hda_ctl_add(codec, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001319 if (err < 0)
1320 return err;
1321
1322 for (s = slaves; *s; s++) {
1323 struct snd_kcontrol *sctl;
1324
1325 sctl = snd_hda_find_mixer_ctl(codec, *s);
1326 if (!sctl) {
1327 snd_printdd("Cannot find slave %s, skipped\n", *s);
1328 continue;
1329 }
1330 err = snd_ctl_add_slave(kctl, sctl);
1331 if (err < 0)
1332 return err;
1333 }
1334 return 0;
1335}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001336EXPORT_SYMBOL_HDA(snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001337
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338/* switch */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001339int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
1340 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341{
1342 int chs = get_amp_channels(kcontrol);
1343
1344 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1345 uinfo->count = chs == 3 ? 2 : 1;
1346 uinfo->value.integer.min = 0;
1347 uinfo->value.integer.max = 1;
1348 return 0;
1349}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001350EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
Takashi Iwai0ba21762007-04-16 11:29:14 +02001352int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
1353 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354{
1355 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1356 hda_nid_t nid = get_amp_nid(kcontrol);
1357 int chs = get_amp_channels(kcontrol);
1358 int dir = get_amp_direction(kcontrol);
1359 int idx = get_amp_index(kcontrol);
1360 long *valp = ucontrol->value.integer.value;
1361
1362 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02001363 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02001364 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02001366 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02001367 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 return 0;
1369}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001370EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
Takashi Iwai0ba21762007-04-16 11:29:14 +02001372int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
1373 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374{
1375 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1376 hda_nid_t nid = get_amp_nid(kcontrol);
1377 int chs = get_amp_channels(kcontrol);
1378 int dir = get_amp_direction(kcontrol);
1379 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 long *valp = ucontrol->value.integer.value;
1381 int change = 0;
1382
Takashi Iwaicb53c622007-08-10 17:21:45 +02001383 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001384 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001385 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02001386 HDA_AMP_MUTE,
1387 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001388 valp++;
1389 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001390 if (chs & 2)
1391 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02001392 HDA_AMP_MUTE,
1393 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001394#ifdef CONFIG_SND_HDA_POWER_SAVE
1395 if (codec->patch_ops.check_power_status)
1396 codec->patch_ops.check_power_status(codec, nid);
1397#endif
1398 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 return change;
1400}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001401EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
1403/*
Takashi Iwai985be542005-11-02 18:26:49 +01001404 * bound volume controls
1405 *
1406 * bind multiple volumes (# indices, from 0)
1407 */
1408
1409#define AMP_VAL_IDX_SHIFT 19
1410#define AMP_VAL_IDX_MASK (0x0f<<19)
1411
Takashi Iwai0ba21762007-04-16 11:29:14 +02001412int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
1413 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01001414{
1415 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1416 unsigned long pval;
1417 int err;
1418
Ingo Molnar62932df2006-01-16 16:34:20 +01001419 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Takashi Iwai985be542005-11-02 18:26:49 +01001420 pval = kcontrol->private_value;
1421 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
1422 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
1423 kcontrol->private_value = pval;
Ingo Molnar62932df2006-01-16 16:34:20 +01001424 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01001425 return err;
1426}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001427EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01001428
Takashi Iwai0ba21762007-04-16 11:29:14 +02001429int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
1430 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01001431{
1432 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1433 unsigned long pval;
1434 int i, indices, err = 0, change = 0;
1435
Ingo Molnar62932df2006-01-16 16:34:20 +01001436 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Takashi Iwai985be542005-11-02 18:26:49 +01001437 pval = kcontrol->private_value;
1438 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
1439 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001440 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
1441 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01001442 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1443 if (err < 0)
1444 break;
1445 change |= err;
1446 }
1447 kcontrol->private_value = pval;
Ingo Molnar62932df2006-01-16 16:34:20 +01001448 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01001449 return err < 0 ? err : change;
1450}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001451EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01001452
1453/*
Takashi Iwai532d5382007-07-27 19:02:40 +02001454 * generic bound volume/swtich controls
1455 */
1456int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
1457 struct snd_ctl_elem_info *uinfo)
1458{
1459 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1460 struct hda_bind_ctls *c;
1461 int err;
1462
Takashi Iwai532d5382007-07-27 19:02:40 +02001463 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01001464 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02001465 kcontrol->private_value = *c->values;
1466 err = c->ops->info(kcontrol, uinfo);
1467 kcontrol->private_value = (long)c;
1468 mutex_unlock(&codec->spdif_mutex);
1469 return err;
1470}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001471EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02001472
1473int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
1474 struct snd_ctl_elem_value *ucontrol)
1475{
1476 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1477 struct hda_bind_ctls *c;
1478 int err;
1479
Takashi Iwai532d5382007-07-27 19:02:40 +02001480 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01001481 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02001482 kcontrol->private_value = *c->values;
1483 err = c->ops->get(kcontrol, ucontrol);
1484 kcontrol->private_value = (long)c;
1485 mutex_unlock(&codec->spdif_mutex);
1486 return err;
1487}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001488EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02001489
1490int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
1491 struct snd_ctl_elem_value *ucontrol)
1492{
1493 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1494 struct hda_bind_ctls *c;
1495 unsigned long *vals;
1496 int err = 0, change = 0;
1497
Takashi Iwai532d5382007-07-27 19:02:40 +02001498 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01001499 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02001500 for (vals = c->values; *vals; vals++) {
1501 kcontrol->private_value = *vals;
1502 err = c->ops->put(kcontrol, ucontrol);
1503 if (err < 0)
1504 break;
1505 change |= err;
1506 }
1507 kcontrol->private_value = (long)c;
1508 mutex_unlock(&codec->spdif_mutex);
1509 return err < 0 ? err : change;
1510}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001511EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02001512
1513int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1514 unsigned int size, unsigned int __user *tlv)
1515{
1516 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1517 struct hda_bind_ctls *c;
1518 int err;
1519
Takashi Iwai532d5382007-07-27 19:02:40 +02001520 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01001521 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02001522 kcontrol->private_value = *c->values;
1523 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
1524 kcontrol->private_value = (long)c;
1525 mutex_unlock(&codec->spdif_mutex);
1526 return err;
1527}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001528EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02001529
1530struct hda_ctl_ops snd_hda_bind_vol = {
1531 .info = snd_hda_mixer_amp_volume_info,
1532 .get = snd_hda_mixer_amp_volume_get,
1533 .put = snd_hda_mixer_amp_volume_put,
1534 .tlv = snd_hda_mixer_amp_tlv
1535};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001536EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02001537
1538struct hda_ctl_ops snd_hda_bind_sw = {
1539 .info = snd_hda_mixer_amp_switch_info,
1540 .get = snd_hda_mixer_amp_switch_get,
1541 .put = snd_hda_mixer_amp_switch_put,
1542 .tlv = snd_hda_mixer_amp_tlv
1543};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001544EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02001545
1546/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 * SPDIF out controls
1548 */
1549
Takashi Iwai0ba21762007-04-16 11:29:14 +02001550static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
1551 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552{
1553 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1554 uinfo->count = 1;
1555 return 0;
1556}
1557
Takashi Iwai0ba21762007-04-16 11:29:14 +02001558static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
1559 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560{
1561 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
1562 IEC958_AES0_NONAUDIO |
1563 IEC958_AES0_CON_EMPHASIS_5015 |
1564 IEC958_AES0_CON_NOT_COPYRIGHT;
1565 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
1566 IEC958_AES1_CON_ORIGINAL;
1567 return 0;
1568}
1569
Takashi Iwai0ba21762007-04-16 11:29:14 +02001570static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
1571 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572{
1573 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
1574 IEC958_AES0_NONAUDIO |
1575 IEC958_AES0_PRO_EMPHASIS_5015;
1576 return 0;
1577}
1578
Takashi Iwai0ba21762007-04-16 11:29:14 +02001579static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
1580 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581{
1582 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1583
1584 ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
1585 ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
1586 ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
1587 ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
1588
1589 return 0;
1590}
1591
1592/* convert from SPDIF status bits to HDA SPDIF bits
1593 * bit 0 (DigEn) is always set zero (to be filled later)
1594 */
1595static unsigned short convert_from_spdif_status(unsigned int sbits)
1596{
1597 unsigned short val = 0;
1598
1599 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02001600 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02001602 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001604 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
1605 IEC958_AES0_PRO_EMPHASIS_5015)
1606 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001608 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
1609 IEC958_AES0_CON_EMPHASIS_5015)
1610 val |= AC_DIG1_EMPHASIS;
1611 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
1612 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02001614 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
1616 }
1617 return val;
1618}
1619
1620/* convert to SPDIF status bits from HDA SPDIF bits
1621 */
1622static unsigned int convert_to_spdif_status(unsigned short val)
1623{
1624 unsigned int sbits = 0;
1625
Takashi Iwai0ba21762007-04-16 11:29:14 +02001626 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001628 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 sbits |= IEC958_AES0_PROFESSIONAL;
1630 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001631 if (sbits & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
1633 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001634 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001636 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001638 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
1640 sbits |= val & (0x7f << 8);
1641 }
1642 return sbits;
1643}
1644
Takashi Iwai2f728532008-09-25 16:32:41 +02001645/* set digital convert verbs both for the given NID and its slaves */
1646static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
1647 int verb, int val)
1648{
1649 hda_nid_t *d;
1650
Takashi Iwai9e976972008-11-25 08:17:20 +01001651 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02001652 d = codec->slave_dig_outs;
1653 if (!d)
1654 return;
1655 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01001656 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02001657}
1658
1659static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
1660 int dig1, int dig2)
1661{
1662 if (dig1 != -1)
1663 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
1664 if (dig2 != -1)
1665 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
1666}
1667
Takashi Iwai0ba21762007-04-16 11:29:14 +02001668static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
1669 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670{
1671 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1672 hda_nid_t nid = kcontrol->private_value;
1673 unsigned short val;
1674 int change;
1675
Ingo Molnar62932df2006-01-16 16:34:20 +01001676 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 codec->spdif_status = ucontrol->value.iec958.status[0] |
1678 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
1679 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
1680 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
1681 val = convert_from_spdif_status(codec->spdif_status);
1682 val |= codec->spdif_ctls & 1;
1683 change = codec->spdif_ctls != val;
1684 codec->spdif_ctls = val;
1685
Takashi Iwai2f728532008-09-25 16:32:41 +02001686 if (change)
1687 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
Ingo Molnar62932df2006-01-16 16:34:20 +01001689 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 return change;
1691}
1692
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001693#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
Takashi Iwai0ba21762007-04-16 11:29:14 +02001695static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
1696 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697{
1698 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1699
Takashi Iwai0ba21762007-04-16 11:29:14 +02001700 ucontrol->value.integer.value[0] = codec->spdif_ctls & AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 return 0;
1702}
1703
Takashi Iwai0ba21762007-04-16 11:29:14 +02001704static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
1705 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706{
1707 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1708 hda_nid_t nid = kcontrol->private_value;
1709 unsigned short val;
1710 int change;
1711
Ingo Molnar62932df2006-01-16 16:34:20 +01001712 mutex_lock(&codec->spdif_mutex);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001713 val = codec->spdif_ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02001715 val |= AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 change = codec->spdif_ctls != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001717 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 codec->spdif_ctls = val;
Takashi Iwai2f728532008-09-25 16:32:41 +02001719 set_dig_out_convert(codec, nid, val & 0xff, -1);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001720 /* unmute amp switch (if any) */
1721 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
Takashi Iwai47fd8302007-08-10 17:11:07 +02001722 (val & AC_DIG1_ENABLE))
1723 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
1724 HDA_AMP_MUTE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 }
Ingo Molnar62932df2006-01-16 16:34:20 +01001726 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 return change;
1728}
1729
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001730static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 {
1732 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1733 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1734 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
1735 .info = snd_hda_spdif_mask_info,
1736 .get = snd_hda_spdif_cmask_get,
1737 },
1738 {
1739 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1740 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1741 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
1742 .info = snd_hda_spdif_mask_info,
1743 .get = snd_hda_spdif_pmask_get,
1744 },
1745 {
1746 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1747 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1748 .info = snd_hda_spdif_mask_info,
1749 .get = snd_hda_spdif_default_get,
1750 .put = snd_hda_spdif_default_put,
1751 },
1752 {
1753 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1754 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
1755 .info = snd_hda_spdif_out_switch_info,
1756 .get = snd_hda_spdif_out_switch_get,
1757 .put = snd_hda_spdif_out_switch_put,
1758 },
1759 { } /* end */
1760};
1761
Takashi Iwai09f99702008-02-04 12:31:13 +01001762#define SPDIF_MAX_IDX 4 /* 4 instances should be enough to probe */
1763
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764/**
1765 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
1766 * @codec: the HDA codec
1767 * @nid: audio out widget NID
1768 *
1769 * Creates controls related with the SPDIF output.
1770 * Called from each patch supporting the SPDIF out.
1771 *
1772 * Returns 0 if successful, or a negative error code.
1773 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02001774int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775{
1776 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001777 struct snd_kcontrol *kctl;
1778 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01001779 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
Takashi Iwai09f99702008-02-04 12:31:13 +01001781 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
1782 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Playback Switch",
1783 idx))
1784 break;
1785 }
1786 if (idx >= SPDIF_MAX_IDX) {
1787 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
1788 return -EBUSY;
1789 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
1791 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01001792 if (!kctl)
1793 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01001794 kctl->id.index = idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 kctl->private_value = nid;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001796 err = snd_hda_ctl_add(codec, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001797 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 return err;
1799 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02001800 codec->spdif_ctls =
Andrew Paprocki3982d172007-12-19 12:13:44 +01001801 snd_hda_codec_read(codec, nid, 0,
1802 AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
1804 return 0;
1805}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001806EXPORT_SYMBOL_HDA(snd_hda_create_spdif_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
1808/*
Takashi Iwai9a081602008-02-12 18:37:26 +01001809 * SPDIF sharing with analog output
1810 */
1811static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
1812 struct snd_ctl_elem_value *ucontrol)
1813{
1814 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
1815 ucontrol->value.integer.value[0] = mout->share_spdif;
1816 return 0;
1817}
1818
1819static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
1820 struct snd_ctl_elem_value *ucontrol)
1821{
1822 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
1823 mout->share_spdif = !!ucontrol->value.integer.value[0];
1824 return 0;
1825}
1826
1827static struct snd_kcontrol_new spdif_share_sw = {
1828 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1829 .name = "IEC958 Default PCM Playback Switch",
1830 .info = snd_ctl_boolean_mono_info,
1831 .get = spdif_share_sw_get,
1832 .put = spdif_share_sw_put,
1833};
1834
1835int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
1836 struct hda_multi_out *mout)
1837{
1838 if (!mout->dig_out_nid)
1839 return 0;
1840 /* ATTENTION: here mout is passed as private_data, instead of codec */
Takashi Iwaid13bd412008-07-30 15:01:45 +02001841 return snd_hda_ctl_add(codec,
Takashi Iwai9a081602008-02-12 18:37:26 +01001842 snd_ctl_new1(&spdif_share_sw, mout));
1843}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001844EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01001845
1846/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 * SPDIF input
1848 */
1849
1850#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
1851
Takashi Iwai0ba21762007-04-16 11:29:14 +02001852static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
1853 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854{
1855 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1856
1857 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
1858 return 0;
1859}
1860
Takashi Iwai0ba21762007-04-16 11:29:14 +02001861static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
1862 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863{
1864 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1865 hda_nid_t nid = kcontrol->private_value;
1866 unsigned int val = !!ucontrol->value.integer.value[0];
1867 int change;
1868
Ingo Molnar62932df2006-01-16 16:34:20 +01001869 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001871 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001873 snd_hda_codec_write_cache(codec, nid, 0,
1874 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 }
Ingo Molnar62932df2006-01-16 16:34:20 +01001876 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 return change;
1878}
1879
Takashi Iwai0ba21762007-04-16 11:29:14 +02001880static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
1881 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882{
1883 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1884 hda_nid_t nid = kcontrol->private_value;
1885 unsigned short val;
1886 unsigned int sbits;
1887
Andrew Paprocki3982d172007-12-19 12:13:44 +01001888 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 sbits = convert_to_spdif_status(val);
1890 ucontrol->value.iec958.status[0] = sbits;
1891 ucontrol->value.iec958.status[1] = sbits >> 8;
1892 ucontrol->value.iec958.status[2] = sbits >> 16;
1893 ucontrol->value.iec958.status[3] = sbits >> 24;
1894 return 0;
1895}
1896
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001897static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 {
1899 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1900 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH),
1901 .info = snd_hda_spdif_in_switch_info,
1902 .get = snd_hda_spdif_in_switch_get,
1903 .put = snd_hda_spdif_in_switch_put,
1904 },
1905 {
1906 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1907 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1908 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT),
1909 .info = snd_hda_spdif_mask_info,
1910 .get = snd_hda_spdif_in_status_get,
1911 },
1912 { } /* end */
1913};
1914
1915/**
1916 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
1917 * @codec: the HDA codec
1918 * @nid: audio in widget NID
1919 *
1920 * Creates controls related with the SPDIF input.
1921 * Called from each patch supporting the SPDIF in.
1922 *
1923 * Returns 0 if successful, or a negative error code.
1924 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02001925int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926{
1927 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001928 struct snd_kcontrol *kctl;
1929 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01001930 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
Takashi Iwai09f99702008-02-04 12:31:13 +01001932 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
1933 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Capture Switch",
1934 idx))
1935 break;
1936 }
1937 if (idx >= SPDIF_MAX_IDX) {
1938 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
1939 return -EBUSY;
1940 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
1942 kctl = snd_ctl_new1(dig_mix, codec);
1943 kctl->private_value = nid;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001944 err = snd_hda_ctl_add(codec, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001945 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 return err;
1947 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02001948 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01001949 snd_hda_codec_read(codec, nid, 0,
1950 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02001951 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 return 0;
1953}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001954EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955
Takashi Iwaicb53c622007-08-10 17:21:45 +02001956#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001957/*
1958 * command cache
1959 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001961/* build a 32bit cache key with the widget id and the command parameter */
1962#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
1963#define get_cmd_cache_nid(key) ((key) & 0xff)
1964#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
1965
1966/**
1967 * snd_hda_codec_write_cache - send a single command with caching
1968 * @codec: the HDA codec
1969 * @nid: NID to send the command
1970 * @direct: direct flag
1971 * @verb: the verb to send
1972 * @parm: the parameter for the verb
1973 *
1974 * Send a single command without waiting for response.
1975 *
1976 * Returns 0 if successful, or a negative error code.
1977 */
1978int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
1979 int direct, unsigned int verb, unsigned int parm)
1980{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01001981 struct hda_bus *bus = codec->bus;
1982 unsigned int res;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001983 int err;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01001984
1985 res = make_codec_cmd(codec, nid, direct, verb, parm);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001986 snd_hda_power_up(codec);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01001987 mutex_lock(&bus->cmd_mutex);
1988 err = bus->ops.command(bus, res);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001989 if (!err) {
1990 struct hda_cache_head *c;
1991 u32 key = build_cmd_cache_key(nid, verb);
1992 c = get_alloc_hash(&codec->cmd_cache, key);
1993 if (c)
1994 c->val = parm;
1995 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01001996 mutex_unlock(&bus->cmd_mutex);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001997 snd_hda_power_down(codec);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001998 return err;
1999}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002000EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002001
2002/* resume the all commands from the cache */
2003void snd_hda_codec_resume_cache(struct hda_codec *codec)
2004{
Takashi Iwai603c4012008-07-30 15:01:44 +02002005 struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002006 int i;
2007
Takashi Iwai603c4012008-07-30 15:01:44 +02002008 for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002009 u32 key = buffer->key;
2010 if (!key)
2011 continue;
2012 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
2013 get_cmd_cache_cmd(key), buffer->val);
2014 }
2015}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002016EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002017
2018/**
2019 * snd_hda_sequence_write_cache - sequence writes with caching
2020 * @codec: the HDA codec
2021 * @seq: VERB array to send
2022 *
2023 * Send the commands sequentially from the given array.
2024 * Thte commands are recorded on cache for power-save and resume.
2025 * The array must be terminated with NID=0.
2026 */
2027void snd_hda_sequence_write_cache(struct hda_codec *codec,
2028 const struct hda_verb *seq)
2029{
2030 for (; seq->nid; seq++)
2031 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
2032 seq->param);
2033}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002034EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002035#endif /* SND_HDA_NEEDS_RESUME */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002036
Takashi Iwai54d17402005-11-21 16:33:22 +01002037/*
2038 * set power state of the codec
2039 */
2040static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2041 unsigned int power_state)
2042{
Takashi Iwaicb53c622007-08-10 17:21:45 +02002043 hda_nid_t nid;
2044 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01002045
2046 snd_hda_codec_write(codec, fg, 0, AC_VERB_SET_POWER_STATE,
2047 power_state);
Marc Boucherd2595d82008-01-22 15:23:30 +01002048 msleep(10); /* partial workaround for "azx_get_response timeout" */
Takashi Iwai54d17402005-11-21 16:33:22 +01002049
Takashi Iwaicb53c622007-08-10 17:21:45 +02002050 nid = codec->start_nid;
2051 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01002052 unsigned int wcaps = get_wcaps(codec, nid);
2053 if (wcaps & AC_WCAP_POWER) {
2054 unsigned int wid_type = (wcaps & AC_WCAP_TYPE) >>
2055 AC_WCAP_TYPE_SHIFT;
2056 if (wid_type == AC_WID_PIN) {
2057 unsigned int pincap;
2058 /*
2059 * don't power down the widget if it controls
2060 * eapd and EAPD_BTLENABLE is set.
2061 */
2062 pincap = snd_hda_param_read(codec, nid,
2063 AC_PAR_PIN_CAP);
2064 if (pincap & AC_PINCAP_EAPD) {
2065 int eapd = snd_hda_codec_read(codec,
2066 nid, 0,
2067 AC_VERB_GET_EAPD_BTLENABLE, 0);
2068 eapd &= 0x02;
2069 if (power_state == AC_PWRST_D3 && eapd)
2070 continue;
2071 }
Takashi Iwai1194b5b2007-10-10 10:04:26 +02002072 }
Takashi Iwai54d17402005-11-21 16:33:22 +01002073 snd_hda_codec_write(codec, nid, 0,
2074 AC_VERB_SET_POWER_STATE,
2075 power_state);
Takashi Iwai1194b5b2007-10-10 10:04:26 +02002076 }
Takashi Iwai54d17402005-11-21 16:33:22 +01002077 }
2078
Takashi Iwaicb53c622007-08-10 17:21:45 +02002079 if (power_state == AC_PWRST_D0) {
2080 unsigned long end_time;
2081 int state;
Takashi Iwai54d17402005-11-21 16:33:22 +01002082 msleep(10);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002083 /* wait until the codec reachs to D0 */
2084 end_time = jiffies + msecs_to_jiffies(500);
2085 do {
2086 state = snd_hda_codec_read(codec, fg, 0,
2087 AC_VERB_GET_POWER_STATE, 0);
2088 if (state == power_state)
2089 break;
2090 msleep(1);
2091 } while (time_after_eq(end_time, jiffies));
2092 }
Takashi Iwai54d17402005-11-21 16:33:22 +01002093}
2094
Takashi Iwai11aeff02008-07-30 15:01:46 +02002095#ifdef CONFIG_SND_HDA_HWDEP
2096/* execute additional init verbs */
2097static void hda_exec_init_verbs(struct hda_codec *codec)
2098{
2099 if (codec->init_verbs.list)
2100 snd_hda_sequence_write(codec, codec->init_verbs.list);
2101}
2102#else
2103static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
2104#endif
2105
Takashi Iwaicb53c622007-08-10 17:21:45 +02002106#ifdef SND_HDA_NEEDS_RESUME
2107/*
2108 * call suspend and power-down; used both from PM and power-save
2109 */
2110static void hda_call_codec_suspend(struct hda_codec *codec)
2111{
2112 if (codec->patch_ops.suspend)
2113 codec->patch_ops.suspend(codec, PMSG_SUSPEND);
2114 hda_set_power_state(codec,
2115 codec->afg ? codec->afg : codec->mfg,
2116 AC_PWRST_D3);
2117#ifdef CONFIG_SND_HDA_POWER_SAVE
2118 cancel_delayed_work(&codec->power_work);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02002119 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02002120 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002121#endif
2122}
2123
2124/*
2125 * kick up codec; used both from PM and power-save
2126 */
2127static void hda_call_codec_resume(struct hda_codec *codec)
2128{
2129 hda_set_power_state(codec,
2130 codec->afg ? codec->afg : codec->mfg,
2131 AC_PWRST_D0);
Takashi Iwai11aeff02008-07-30 15:01:46 +02002132 hda_exec_init_verbs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002133 if (codec->patch_ops.resume)
2134 codec->patch_ops.resume(codec);
2135 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02002136 if (codec->patch_ops.init)
2137 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002138 snd_hda_codec_resume_amp(codec);
2139 snd_hda_codec_resume_cache(codec);
2140 }
2141}
2142#endif /* SND_HDA_NEEDS_RESUME */
2143
Takashi Iwai54d17402005-11-21 16:33:22 +01002144
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145/**
2146 * snd_hda_build_controls - build mixer controls
2147 * @bus: the BUS
2148 *
2149 * Creates mixer controls for each codec included in the bus.
2150 *
2151 * Returns 0 if successful, otherwise a negative error code.
2152 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002153int /*__devinit*/ snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154{
Takashi Iwai0ba21762007-04-16 11:29:14 +02002155 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156
Takashi Iwai0ba21762007-04-16 11:29:14 +02002157 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002158 int err = snd_hda_codec_build_controls(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 if (err < 0)
2160 return err;
2161 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002162 return 0;
2163}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002164EXPORT_SYMBOL_HDA(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002166int snd_hda_codec_build_controls(struct hda_codec *codec)
2167{
2168 int err = 0;
2169 /* fake as if already powered-on */
2170 hda_keep_power_on(codec);
2171 /* then fire up */
2172 hda_set_power_state(codec,
2173 codec->afg ? codec->afg : codec->mfg,
2174 AC_PWRST_D0);
Takashi Iwai11aeff02008-07-30 15:01:46 +02002175 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002176 /* continue to initialize... */
2177 if (codec->patch_ops.init)
2178 err = codec->patch_ops.init(codec);
2179 if (!err && codec->patch_ops.build_controls)
2180 err = codec->patch_ops.build_controls(codec);
2181 snd_hda_power_down(codec);
2182 if (err < 0)
2183 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 return 0;
2185}
2186
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187/*
2188 * stream formats
2189 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02002190struct hda_rate_tbl {
2191 unsigned int hz;
2192 unsigned int alsa_bits;
2193 unsigned int hda_fmt;
2194};
2195
2196static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02002198
2199 /* autodetected value used in snd_hda_query_supported_pcm */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 { 8000, SNDRV_PCM_RATE_8000, 0x0500 }, /* 1/6 x 48 */
2201 { 11025, SNDRV_PCM_RATE_11025, 0x4300 }, /* 1/4 x 44 */
2202 { 16000, SNDRV_PCM_RATE_16000, 0x0200 }, /* 1/3 x 48 */
2203 { 22050, SNDRV_PCM_RATE_22050, 0x4100 }, /* 1/2 x 44 */
2204 { 32000, SNDRV_PCM_RATE_32000, 0x0a00 }, /* 2/3 x 48 */
2205 { 44100, SNDRV_PCM_RATE_44100, 0x4000 }, /* 44 */
2206 { 48000, SNDRV_PCM_RATE_48000, 0x0000 }, /* 48 */
2207 { 88200, SNDRV_PCM_RATE_88200, 0x4800 }, /* 2 x 44 */
2208 { 96000, SNDRV_PCM_RATE_96000, 0x0800 }, /* 2 x 48 */
2209 { 176400, SNDRV_PCM_RATE_176400, 0x5800 },/* 4 x 44 */
2210 { 192000, SNDRV_PCM_RATE_192000, 0x1800 }, /* 4 x 48 */
Takashi Iwaia961f9f2007-04-12 13:08:09 +02002211#define AC_PAR_PCM_RATE_BITS 11
2212 /* up to bits 10, 384kHZ isn't supported properly */
2213
2214 /* not autodetected value */
2215 { 9600, SNDRV_PCM_RATE_KNOT, 0x0400 }, /* 1/5 x 48 */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02002216
Takashi Iwaibefdf312005-08-22 13:57:55 +02002217 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218};
2219
2220/**
2221 * snd_hda_calc_stream_format - calculate format bitset
2222 * @rate: the sample rate
2223 * @channels: the number of channels
2224 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
2225 * @maxbps: the max. bps
2226 *
2227 * Calculate the format bitset from the given rate, channels and th PCM format.
2228 *
2229 * Return zero if invalid.
2230 */
2231unsigned int snd_hda_calc_stream_format(unsigned int rate,
2232 unsigned int channels,
2233 unsigned int format,
2234 unsigned int maxbps)
2235{
2236 int i;
2237 unsigned int val = 0;
2238
Takashi Iwaibefdf312005-08-22 13:57:55 +02002239 for (i = 0; rate_bits[i].hz; i++)
2240 if (rate_bits[i].hz == rate) {
2241 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 break;
2243 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002244 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 snd_printdd("invalid rate %d\n", rate);
2246 return 0;
2247 }
2248
2249 if (channels == 0 || channels > 8) {
2250 snd_printdd("invalid channels %d\n", channels);
2251 return 0;
2252 }
2253 val |= channels - 1;
2254
2255 switch (snd_pcm_format_width(format)) {
2256 case 8: val |= 0x00; break;
2257 case 16: val |= 0x10; break;
2258 case 20:
2259 case 24:
2260 case 32:
2261 if (maxbps >= 32)
2262 val |= 0x40;
2263 else if (maxbps >= 24)
2264 val |= 0x30;
2265 else
2266 val |= 0x20;
2267 break;
2268 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002269 snd_printdd("invalid format width %d\n",
2270 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 return 0;
2272 }
2273
2274 return val;
2275}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002276EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
2278/**
2279 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
2280 * @codec: the HDA codec
2281 * @nid: NID to query
2282 * @ratesp: the pointer to store the detected rate bitflags
2283 * @formatsp: the pointer to store the detected formats
2284 * @bpsp: the pointer to store the detected format widths
2285 *
2286 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
2287 * or @bsps argument is ignored.
2288 *
2289 * Returns 0 if successful, otherwise a negative error code.
2290 */
Takashi Iwai986862bd2008-11-27 12:40:13 +01002291static int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
2293{
2294 int i;
2295 unsigned int val, streams;
2296
2297 val = 0;
2298 if (nid != codec->afg &&
Takashi Iwai54d17402005-11-21 16:33:22 +01002299 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
2301 if (val == -1)
2302 return -EIO;
2303 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002304 if (!val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
2306
2307 if (ratesp) {
2308 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02002309 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02002311 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 }
2313 *ratesp = rates;
2314 }
2315
2316 if (formatsp || bpsp) {
2317 u64 formats = 0;
2318 unsigned int bps;
2319 unsigned int wcaps;
2320
Takashi Iwai54d17402005-11-21 16:33:22 +01002321 wcaps = get_wcaps(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
2323 if (streams == -1)
2324 return -EIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002325 if (!streams) {
2326 streams = snd_hda_param_read(codec, codec->afg,
2327 AC_PAR_STREAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 if (streams == -1)
2329 return -EIO;
2330 }
2331
2332 bps = 0;
2333 if (streams & AC_SUPFMT_PCM) {
2334 if (val & AC_SUPPCM_BITS_8) {
2335 formats |= SNDRV_PCM_FMTBIT_U8;
2336 bps = 8;
2337 }
2338 if (val & AC_SUPPCM_BITS_16) {
2339 formats |= SNDRV_PCM_FMTBIT_S16_LE;
2340 bps = 16;
2341 }
2342 if (wcaps & AC_WCAP_DIGITAL) {
2343 if (val & AC_SUPPCM_BITS_32)
2344 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
2345 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
2346 formats |= SNDRV_PCM_FMTBIT_S32_LE;
2347 if (val & AC_SUPPCM_BITS_24)
2348 bps = 24;
2349 else if (val & AC_SUPPCM_BITS_20)
2350 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002351 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
2352 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 formats |= SNDRV_PCM_FMTBIT_S32_LE;
2354 if (val & AC_SUPPCM_BITS_32)
2355 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 else if (val & AC_SUPPCM_BITS_24)
2357 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02002358 else if (val & AC_SUPPCM_BITS_20)
2359 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 }
2361 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002362 else if (streams == AC_SUPFMT_FLOAT32) {
2363 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
2365 bps = 32;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002366 } else if (streams == AC_SUPFMT_AC3) {
2367 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 /* temporary hack: we have still no proper support
2369 * for the direct AC3 stream...
2370 */
2371 formats |= SNDRV_PCM_FMTBIT_U8;
2372 bps = 8;
2373 }
2374 if (formatsp)
2375 *formatsp = formats;
2376 if (bpsp)
2377 *bpsp = bps;
2378 }
2379
2380 return 0;
2381}
2382
2383/**
Takashi Iwai0ba21762007-04-16 11:29:14 +02002384 * snd_hda_is_supported_format - check whether the given node supports
2385 * the format val
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 *
2387 * Returns 1 if supported, 0 if not.
2388 */
2389int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
2390 unsigned int format)
2391{
2392 int i;
2393 unsigned int val = 0, rate, stream;
2394
2395 if (nid != codec->afg &&
Takashi Iwai54d17402005-11-21 16:33:22 +01002396 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
2398 if (val == -1)
2399 return 0;
2400 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002401 if (!val) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
2403 if (val == -1)
2404 return 0;
2405 }
2406
2407 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02002408 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02002409 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 if (val & (1 << i))
2411 break;
2412 return 0;
2413 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02002414 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 return 0;
2416
2417 stream = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
2418 if (stream == -1)
2419 return 0;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002420 if (!stream && nid != codec->afg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 stream = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002422 if (!stream || stream == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 return 0;
2424
2425 if (stream & AC_SUPFMT_PCM) {
2426 switch (format & 0xf0) {
2427 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002428 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 return 0;
2430 break;
2431 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002432 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 return 0;
2434 break;
2435 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002436 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 return 0;
2438 break;
2439 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002440 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 return 0;
2442 break;
2443 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002444 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 return 0;
2446 break;
2447 default:
2448 return 0;
2449 }
2450 } else {
2451 /* FIXME: check for float32 and AC3? */
2452 }
2453
2454 return 1;
2455}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002456EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457
2458/*
2459 * PCM stuff
2460 */
2461static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
2462 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002463 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464{
2465 return 0;
2466}
2467
2468static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
2469 struct hda_codec *codec,
2470 unsigned int stream_tag,
2471 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002472 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473{
2474 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
2475 return 0;
2476}
2477
2478static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
2479 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002480 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481{
Takashi Iwai888afa12008-03-18 09:57:50 +01002482 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 return 0;
2484}
2485
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002486static int set_pcm_default_values(struct hda_codec *codec,
2487 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488{
Takashi Iwai0ba21762007-04-16 11:29:14 +02002489 /* query support PCM information from the given NID */
2490 if (info->nid && (!info->rates || !info->formats)) {
2491 snd_hda_query_supported_pcm(codec, info->nid,
2492 info->rates ? NULL : &info->rates,
2493 info->formats ? NULL : &info->formats,
2494 info->maxbps ? NULL : &info->maxbps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 }
2496 if (info->ops.open == NULL)
2497 info->ops.open = hda_pcm_default_open_close;
2498 if (info->ops.close == NULL)
2499 info->ops.close = hda_pcm_default_open_close;
2500 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02002501 if (snd_BUG_ON(!info->nid))
2502 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 info->ops.prepare = hda_pcm_default_prepare;
2504 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02002506 if (snd_BUG_ON(!info->nid))
2507 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 info->ops.cleanup = hda_pcm_default_cleanup;
2509 }
2510 return 0;
2511}
2512
Takashi Iwai176d5332008-07-30 15:01:44 +02002513/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002514 * get the empty PCM device number to assign
2515 */
2516static int get_empty_pcm_device(struct hda_bus *bus, int type)
2517{
2518 static const char *dev_name[HDA_PCM_NTYPES] = {
2519 "Audio", "SPDIF", "HDMI", "Modem"
2520 };
2521 /* starting device index for each PCM type */
2522 static int dev_idx[HDA_PCM_NTYPES] = {
2523 [HDA_PCM_TYPE_AUDIO] = 0,
2524 [HDA_PCM_TYPE_SPDIF] = 1,
2525 [HDA_PCM_TYPE_HDMI] = 3,
2526 [HDA_PCM_TYPE_MODEM] = 6
2527 };
2528 /* normal audio device indices; not linear to keep compatibility */
2529 static int audio_idx[4] = { 0, 2, 4, 5 };
2530 int i, dev;
2531
2532 switch (type) {
2533 case HDA_PCM_TYPE_AUDIO:
2534 for (i = 0; i < ARRAY_SIZE(audio_idx); i++) {
2535 dev = audio_idx[i];
2536 if (!test_bit(dev, bus->pcm_dev_bits))
2537 break;
2538 }
2539 if (i >= ARRAY_SIZE(audio_idx)) {
2540 snd_printk(KERN_WARNING "Too many audio devices\n");
2541 return -EAGAIN;
2542 }
2543 break;
2544 case HDA_PCM_TYPE_SPDIF:
2545 case HDA_PCM_TYPE_HDMI:
2546 case HDA_PCM_TYPE_MODEM:
2547 dev = dev_idx[type];
2548 if (test_bit(dev, bus->pcm_dev_bits)) {
2549 snd_printk(KERN_WARNING "%s already defined\n",
2550 dev_name[type]);
2551 return -EAGAIN;
2552 }
2553 break;
2554 default:
2555 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
2556 return -EINVAL;
2557 }
2558 set_bit(dev, bus->pcm_dev_bits);
2559 return dev;
2560}
2561
2562/*
Takashi Iwai176d5332008-07-30 15:01:44 +02002563 * attach a new PCM stream
2564 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002565static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02002566{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01002567 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02002568 struct hda_pcm_stream *info;
2569 int stream, err;
2570
Takashi Iwaib91f0802008-11-04 08:43:08 +01002571 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02002572 return -EINVAL;
2573 for (stream = 0; stream < 2; stream++) {
2574 info = &pcm->stream[stream];
2575 if (info->substreams) {
2576 err = set_pcm_default_values(codec, info);
2577 if (err < 0)
2578 return err;
2579 }
2580 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01002581 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02002582}
2583
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002584/* assign all PCMs of the given codec */
2585int snd_hda_codec_build_pcms(struct hda_codec *codec)
2586{
2587 unsigned int pcm;
2588 int err;
2589
2590 if (!codec->num_pcms) {
2591 if (!codec->patch_ops.build_pcms)
2592 return 0;
2593 err = codec->patch_ops.build_pcms(codec);
2594 if (err < 0)
2595 return err;
2596 }
2597 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2598 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2599 int dev;
2600
2601 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
2602 return 0; /* no substreams assigned */
2603
2604 if (!cpcm->pcm) {
2605 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
2606 if (dev < 0)
2607 return 0;
2608 cpcm->device = dev;
2609 err = snd_hda_attach_pcm(codec, cpcm);
2610 if (err < 0)
2611 return err;
2612 }
2613 }
2614 return 0;
2615}
2616
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617/**
2618 * snd_hda_build_pcms - build PCM information
2619 * @bus: the BUS
2620 *
2621 * Create PCM information for each codec included in the bus.
2622 *
2623 * The build_pcms codec patch is requested to set up codec->num_pcms and
2624 * codec->pcm_info properly. The array is referred by the top-level driver
2625 * to create its PCM instances.
2626 * The allocated codec->pcm_info should be released in codec->patch_ops.free
2627 * callback.
2628 *
2629 * At least, substreams, channels_min and channels_max must be filled for
2630 * each stream. substreams = 0 indicates that the stream doesn't exist.
2631 * When rates and/or formats are zero, the supported values are queried
2632 * from the given nid. The nid is used also by the default ops.prepare
2633 * and ops.cleanup callbacks.
2634 *
2635 * The driver needs to call ops.open in its open callback. Similarly,
2636 * ops.close is supposed to be called in the close callback.
2637 * ops.prepare should be called in the prepare or hw_params callback
2638 * with the proper parameters for set up.
2639 * ops.cleanup should be called in hw_free for clean up of streams.
2640 *
2641 * This function returns 0 if successfull, or a negative error code.
2642 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002643int __devinit snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644{
Takashi Iwai0ba21762007-04-16 11:29:14 +02002645 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646
Takashi Iwai0ba21762007-04-16 11:29:14 +02002647 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002648 int err = snd_hda_codec_build_pcms(codec);
2649 if (err < 0)
2650 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 }
2652 return 0;
2653}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002654EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656/**
2657 * snd_hda_check_board_config - compare the current codec with the config table
2658 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002659 * @num_configs: number of config enums
2660 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 * @tbl: configuration table, terminated by null entries
2662 *
2663 * Compares the modelname or PCI subsystem id of the current codec with the
2664 * given configuration table. If a matching entry is found, returns its
2665 * config value (supposed to be 0 or positive).
2666 *
2667 * If no entries are matching, the function returns a negative value.
2668 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002669int snd_hda_check_board_config(struct hda_codec *codec,
2670 int num_configs, const char **models,
2671 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672{
Takashi Iwaif44ac832008-07-30 15:01:45 +02002673 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002674 int i;
2675 for (i = 0; i < num_configs; i++) {
2676 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02002677 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002678 snd_printd(KERN_INFO "hda_codec: model '%s' is "
2679 "selected\n", models[i]);
2680 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 }
2682 }
2683 }
2684
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002685 if (!codec->bus->pci || !tbl)
2686 return -1;
2687
2688 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
2689 if (!tbl)
2690 return -1;
2691 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02002692#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002693 char tmp[10];
2694 const char *model = NULL;
2695 if (models)
2696 model = models[tbl->value];
2697 if (!model) {
2698 sprintf(tmp, "#%d", tbl->value);
2699 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002701 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
2702 "for config %x:%x (%s)\n",
2703 model, tbl->subvendor, tbl->subdevice,
2704 (tbl->name ? tbl->name : "Unknown device"));
2705#endif
2706 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 }
2708 return -1;
2709}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002710EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711
2712/**
2713 * snd_hda_add_new_ctls - create controls from the array
2714 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002715 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 *
2717 * This helper function creates and add new controls in the given array.
2718 * The array must be terminated with an empty entry as terminator.
2719 *
2720 * Returns 0 if successful, or a negative error code.
2721 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002722int snd_hda_add_new_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723{
Takashi Iwaicb53c622007-08-10 17:21:45 +02002724 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725
2726 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01002727 struct snd_kcontrol *kctl;
2728 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002729 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01002730 return -ENOMEM;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002731 err = snd_hda_ctl_add(codec, kctl);
Takashi Iwai54d17402005-11-21 16:33:22 +01002732 if (err < 0) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002733 if (!codec->addr)
Takashi Iwai54d17402005-11-21 16:33:22 +01002734 return err;
2735 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002736 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01002737 return -ENOMEM;
2738 kctl->id.device = codec->addr;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002739 err = snd_hda_ctl_add(codec, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002740 if (err < 0)
Takashi Iwai54d17402005-11-21 16:33:22 +01002741 return err;
2742 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 }
2744 return 0;
2745}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002746EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747
Takashi Iwaicb53c622007-08-10 17:21:45 +02002748#ifdef CONFIG_SND_HDA_POWER_SAVE
2749static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2750 unsigned int power_state);
2751
2752static void hda_power_work(struct work_struct *work)
2753{
2754 struct hda_codec *codec =
2755 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01002756 struct hda_bus *bus = codec->bus;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002757
Maxim Levitsky2e492462007-09-03 15:26:57 +02002758 if (!codec->power_on || codec->power_count) {
2759 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002760 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02002761 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02002762
2763 hda_call_codec_suspend(codec);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01002764 if (bus->ops.pm_notify)
2765 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002766}
2767
2768static void hda_keep_power_on(struct hda_codec *codec)
2769{
2770 codec->power_count++;
2771 codec->power_on = 1;
2772}
2773
2774void snd_hda_power_up(struct hda_codec *codec)
2775{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01002776 struct hda_bus *bus = codec->bus;
2777
Takashi Iwaicb53c622007-08-10 17:21:45 +02002778 codec->power_count++;
Takashi Iwaia221e282007-08-16 16:35:33 +02002779 if (codec->power_on || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02002780 return;
2781
2782 codec->power_on = 1;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01002783 if (bus->ops.pm_notify)
2784 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002785 hda_call_codec_resume(codec);
2786 cancel_delayed_work(&codec->power_work);
Takashi Iwaia221e282007-08-16 16:35:33 +02002787 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002788}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002789EXPORT_SYMBOL_HDA(snd_hda_power_up);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002790
2791#define power_save(codec) \
2792 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02002793
Takashi Iwaifee2fba2008-11-27 12:43:28 +01002794#define power_save(codec) \
2795 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
2796
Takashi Iwaicb53c622007-08-10 17:21:45 +02002797void snd_hda_power_down(struct hda_codec *codec)
2798{
2799 --codec->power_count;
Takashi Iwaia221e282007-08-16 16:35:33 +02002800 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02002801 return;
Takashi Iwaifee2fba2008-11-27 12:43:28 +01002802 if (power_save(codec)) {
Takashi Iwaia221e282007-08-16 16:35:33 +02002803 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002804 schedule_delayed_work(&codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01002805 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02002806 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02002807}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002808EXPORT_SYMBOL_HDA(snd_hda_power_down);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002809
2810int snd_hda_check_amp_list_power(struct hda_codec *codec,
2811 struct hda_loopback_check *check,
2812 hda_nid_t nid)
2813{
2814 struct hda_amp_list *p;
2815 int ch, v;
2816
2817 if (!check->amplist)
2818 return 0;
2819 for (p = check->amplist; p->nid; p++) {
2820 if (p->nid == nid)
2821 break;
2822 }
2823 if (!p->nid)
2824 return 0; /* nothing changed */
2825
2826 for (p = check->amplist; p->nid; p++) {
2827 for (ch = 0; ch < 2; ch++) {
2828 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
2829 p->idx);
2830 if (!(v & HDA_AMP_MUTE) && v > 0) {
2831 if (!check->power_on) {
2832 check->power_on = 1;
2833 snd_hda_power_up(codec);
2834 }
2835 return 1;
2836 }
2837 }
2838 }
2839 if (check->power_on) {
2840 check->power_on = 0;
2841 snd_hda_power_down(codec);
2842 }
2843 return 0;
2844}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002845EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002846#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002848/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002849 * Channel mode helper
2850 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002851int snd_hda_ch_mode_info(struct hda_codec *codec,
2852 struct snd_ctl_elem_info *uinfo,
2853 const struct hda_channel_mode *chmode,
2854 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002855{
2856 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2857 uinfo->count = 1;
2858 uinfo->value.enumerated.items = num_chmodes;
2859 if (uinfo->value.enumerated.item >= num_chmodes)
2860 uinfo->value.enumerated.item = num_chmodes - 1;
2861 sprintf(uinfo->value.enumerated.name, "%dch",
2862 chmode[uinfo->value.enumerated.item].channels);
2863 return 0;
2864}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002865EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002866
Takashi Iwai0ba21762007-04-16 11:29:14 +02002867int snd_hda_ch_mode_get(struct hda_codec *codec,
2868 struct snd_ctl_elem_value *ucontrol,
2869 const struct hda_channel_mode *chmode,
2870 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002871 int max_channels)
2872{
2873 int i;
2874
2875 for (i = 0; i < num_chmodes; i++) {
2876 if (max_channels == chmode[i].channels) {
2877 ucontrol->value.enumerated.item[0] = i;
2878 break;
2879 }
2880 }
2881 return 0;
2882}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002883EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002884
Takashi Iwai0ba21762007-04-16 11:29:14 +02002885int snd_hda_ch_mode_put(struct hda_codec *codec,
2886 struct snd_ctl_elem_value *ucontrol,
2887 const struct hda_channel_mode *chmode,
2888 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002889 int *max_channelsp)
2890{
2891 unsigned int mode;
2892
2893 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01002894 if (mode >= num_chmodes)
2895 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002896 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002897 return 0;
2898 /* change the current channel setting */
2899 *max_channelsp = chmode[mode].channels;
2900 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002901 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002902 return 1;
2903}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002904EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002905
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906/*
2907 * input MUX helper
2908 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002909int snd_hda_input_mux_info(const struct hda_input_mux *imux,
2910 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911{
2912 unsigned int index;
2913
2914 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2915 uinfo->count = 1;
2916 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02002917 if (!imux->num_items)
2918 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 index = uinfo->value.enumerated.item;
2920 if (index >= imux->num_items)
2921 index = imux->num_items - 1;
2922 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
2923 return 0;
2924}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002925EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926
Takashi Iwai0ba21762007-04-16 11:29:14 +02002927int snd_hda_input_mux_put(struct hda_codec *codec,
2928 const struct hda_input_mux *imux,
2929 struct snd_ctl_elem_value *ucontrol,
2930 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 unsigned int *cur_val)
2932{
2933 unsigned int idx;
2934
Takashi Iwai5513b0c2007-10-09 11:58:41 +02002935 if (!imux->num_items)
2936 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 idx = ucontrol->value.enumerated.item[0];
2938 if (idx >= imux->num_items)
2939 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002940 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002942 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
2943 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 *cur_val = idx;
2945 return 1;
2946}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002947EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948
2949
2950/*
2951 * Multi-channel / digital-out PCM helper functions
2952 */
2953
Takashi Iwai6b97eb42007-04-05 14:51:48 +02002954/* setup SPDIF output stream */
2955static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
2956 unsigned int stream_tag, unsigned int format)
2957{
2958 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Takashi Iwai2f728532008-09-25 16:32:41 +02002959 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
2960 set_dig_out_convert(codec, nid,
2961 codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff,
2962 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02002963 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02002964 if (codec->slave_dig_outs) {
2965 hda_nid_t *d;
2966 for (d = codec->slave_dig_outs; *d; d++)
2967 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
2968 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04002969 }
Takashi Iwai2f728532008-09-25 16:32:41 +02002970 /* turn on again (if needed) */
2971 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
2972 set_dig_out_convert(codec, nid,
2973 codec->spdif_ctls & 0xff, -1);
2974}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04002975
Takashi Iwai2f728532008-09-25 16:32:41 +02002976static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
2977{
2978 snd_hda_codec_cleanup_stream(codec, nid);
2979 if (codec->slave_dig_outs) {
2980 hda_nid_t *d;
2981 for (d = codec->slave_dig_outs; *d; d++)
2982 snd_hda_codec_cleanup_stream(codec, *d);
2983 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02002984}
2985
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986/*
2987 * open the digital out in the exclusive mode
2988 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002989int snd_hda_multi_out_dig_open(struct hda_codec *codec,
2990 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991{
Ingo Molnar62932df2006-01-16 16:34:20 +01002992 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02002993 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
2994 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02002995 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01002997 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 return 0;
2999}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003000EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003002int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
3003 struct hda_multi_out *mout,
3004 unsigned int stream_tag,
3005 unsigned int format,
3006 struct snd_pcm_substream *substream)
3007{
3008 mutex_lock(&codec->spdif_mutex);
3009 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
3010 mutex_unlock(&codec->spdif_mutex);
3011 return 0;
3012}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003013EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003014
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015/*
3016 * release the digital out
3017 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003018int snd_hda_multi_out_dig_close(struct hda_codec *codec,
3019 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020{
Ingo Molnar62932df2006-01-16 16:34:20 +01003021 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01003023 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 return 0;
3025}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003026EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027
3028/*
3029 * set up more restrictions for analog out
3030 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003031int snd_hda_multi_out_analog_open(struct hda_codec *codec,
3032 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01003033 struct snd_pcm_substream *substream,
3034 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035{
Takashi Iwai9a081602008-02-12 18:37:26 +01003036 struct snd_pcm_runtime *runtime = substream->runtime;
3037 runtime->hw.channels_max = mout->max_channels;
3038 if (mout->dig_out_nid) {
3039 if (!mout->analog_rates) {
3040 mout->analog_rates = hinfo->rates;
3041 mout->analog_formats = hinfo->formats;
3042 mout->analog_maxbps = hinfo->maxbps;
3043 } else {
3044 runtime->hw.rates = mout->analog_rates;
3045 runtime->hw.formats = mout->analog_formats;
3046 hinfo->maxbps = mout->analog_maxbps;
3047 }
3048 if (!mout->spdif_rates) {
3049 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
3050 &mout->spdif_rates,
3051 &mout->spdif_formats,
3052 &mout->spdif_maxbps);
3053 }
3054 mutex_lock(&codec->spdif_mutex);
3055 if (mout->share_spdif) {
3056 runtime->hw.rates &= mout->spdif_rates;
3057 runtime->hw.formats &= mout->spdif_formats;
3058 if (mout->spdif_maxbps < hinfo->maxbps)
3059 hinfo->maxbps = mout->spdif_maxbps;
3060 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02003061 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01003062 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 return snd_pcm_hw_constraint_step(substream->runtime, 0,
3064 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
3065}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003066EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067
3068/*
3069 * set up the i/o for analog out
3070 * when the digital out is available, copy the front out to digital out, too.
3071 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003072int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
3073 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 unsigned int stream_tag,
3075 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003076 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077{
3078 hda_nid_t *nids = mout->dac_nids;
3079 int chs = substream->runtime->channels;
3080 int i;
3081
Ingo Molnar62932df2006-01-16 16:34:20 +01003082 mutex_lock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01003083 if (mout->dig_out_nid && mout->share_spdif &&
3084 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02003086 snd_hda_is_supported_format(codec, mout->dig_out_nid,
3087 format) &&
3088 !(codec->spdif_status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003090 setup_dig_out_stream(codec, mout->dig_out_nid,
3091 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 } else {
3093 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02003094 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 }
3096 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003097 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098
3099 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003100 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
3101 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02003102 if (!mout->no_share_stream &&
3103 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003105 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
3106 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01003107 /* extra outputs copied from front */
3108 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02003109 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01003110 snd_hda_codec_setup_stream(codec,
3111 mout->extra_out_nid[i],
3112 stream_tag, 0, format);
3113
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 /* surrounds */
3115 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02003116 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003117 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
3118 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02003119 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003120 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
3121 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 }
3123 return 0;
3124}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003125EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126
3127/*
3128 * clean up the setting for analog out
3129 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003130int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
3131 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132{
3133 hda_nid_t *nids = mout->dac_nids;
3134 int i;
3135
3136 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01003137 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01003139 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwai82bc9552006-03-21 11:24:42 +01003140 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
3141 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01003142 snd_hda_codec_cleanup_stream(codec,
3143 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01003144 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02003146 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 mout->dig_out_used = 0;
3148 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003149 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 return 0;
3151}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003152EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003154/*
Wu Fengguang6b345002008-10-07 14:21:41 +08003155 * Helper for automatic pin configuration
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003156 */
Kailang Yangdf694da2005-12-05 19:42:22 +01003157
Takashi Iwai12f288b2007-08-02 15:51:59 +02003158static int is_in_nid_list(hda_nid_t nid, hda_nid_t *list)
Kailang Yangdf694da2005-12-05 19:42:22 +01003159{
3160 for (; *list; list++)
3161 if (*list == nid)
3162 return 1;
3163 return 0;
3164}
3165
Steve Longerbeam81937d32007-05-08 15:33:03 +02003166
3167/*
3168 * Sort an associated group of pins according to their sequence numbers.
3169 */
3170static void sort_pins_by_sequence(hda_nid_t * pins, short * sequences,
3171 int num_pins)
3172{
3173 int i, j;
3174 short seq;
3175 hda_nid_t nid;
3176
3177 for (i = 0; i < num_pins; i++) {
3178 for (j = i + 1; j < num_pins; j++) {
3179 if (sequences[i] > sequences[j]) {
3180 seq = sequences[i];
3181 sequences[i] = sequences[j];
3182 sequences[j] = seq;
3183 nid = pins[i];
3184 pins[i] = pins[j];
3185 pins[j] = nid;
3186 }
3187 }
3188 }
3189}
3190
3191
Takashi Iwai82bc9552006-03-21 11:24:42 +01003192/*
3193 * Parse all pin widgets and store the useful pin nids to cfg
3194 *
3195 * The number of line-outs or any primary output is stored in line_outs,
3196 * and the corresponding output pins are assigned to line_out_pins[],
3197 * in the order of front, rear, CLFE, side, ...
3198 *
3199 * If more extra outputs (speaker and headphone) are found, the pins are
Takashi Iwaieb06ed82006-09-20 17:10:27 +02003200 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
Takashi Iwai82bc9552006-03-21 11:24:42 +01003201 * is detected, one of speaker of HP pins is assigned as the primary
3202 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
3203 * if any analog output exists.
3204 *
3205 * The analog input pins are assigned to input_pins array.
3206 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
3207 * respectively.
3208 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003209int snd_hda_parse_pin_def_config(struct hda_codec *codec,
3210 struct auto_pin_cfg *cfg,
3211 hda_nid_t *ignore_nids)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003212{
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01003213 hda_nid_t nid, end_nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02003214 short seq, assoc_line_out, assoc_speaker;
3215 short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
3216 short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
Takashi Iwaif889fa92007-10-31 15:49:32 +01003217 short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003218
3219 memset(cfg, 0, sizeof(*cfg));
3220
Steve Longerbeam81937d32007-05-08 15:33:03 +02003221 memset(sequences_line_out, 0, sizeof(sequences_line_out));
3222 memset(sequences_speaker, 0, sizeof(sequences_speaker));
Takashi Iwaif889fa92007-10-31 15:49:32 +01003223 memset(sequences_hp, 0, sizeof(sequences_hp));
Steve Longerbeam81937d32007-05-08 15:33:03 +02003224 assoc_line_out = assoc_speaker = 0;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003225
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01003226 end_nid = codec->start_nid + codec->num_nodes;
3227 for (nid = codec->start_nid; nid < end_nid; nid++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01003228 unsigned int wid_caps = get_wcaps(codec, nid);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003229 unsigned int wid_type =
3230 (wid_caps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003231 unsigned int def_conf;
3232 short assoc, loc;
3233
3234 /* read all default configuration for pin complex */
3235 if (wid_type != AC_WID_PIN)
3236 continue;
Kailang Yangdf694da2005-12-05 19:42:22 +01003237 /* ignore the given nids (e.g. pc-beep returns error) */
3238 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
3239 continue;
3240
Takashi Iwai0ba21762007-04-16 11:29:14 +02003241 def_conf = snd_hda_codec_read(codec, nid, 0,
3242 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003243 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
3244 continue;
3245 loc = get_defcfg_location(def_conf);
3246 switch (get_defcfg_device(def_conf)) {
3247 case AC_JACK_LINE_OUT:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003248 seq = get_defcfg_sequence(def_conf);
3249 assoc = get_defcfg_association(def_conf);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01003250
3251 if (!(wid_caps & AC_WCAP_STEREO))
3252 if (!cfg->mono_out_pin)
3253 cfg->mono_out_pin = nid;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003254 if (!assoc)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003255 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003256 if (!assoc_line_out)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003257 assoc_line_out = assoc;
3258 else if (assoc_line_out != assoc)
3259 continue;
3260 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
3261 continue;
3262 cfg->line_out_pins[cfg->line_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02003263 sequences_line_out[cfg->line_outs] = seq;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003264 cfg->line_outs++;
3265 break;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01003266 case AC_JACK_SPEAKER:
Steve Longerbeam81937d32007-05-08 15:33:03 +02003267 seq = get_defcfg_sequence(def_conf);
3268 assoc = get_defcfg_association(def_conf);
3269 if (! assoc)
3270 continue;
3271 if (! assoc_speaker)
3272 assoc_speaker = assoc;
3273 else if (assoc_speaker != assoc)
3274 continue;
Takashi Iwai82bc9552006-03-21 11:24:42 +01003275 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
3276 continue;
3277 cfg->speaker_pins[cfg->speaker_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02003278 sequences_speaker[cfg->speaker_outs] = seq;
Takashi Iwai82bc9552006-03-21 11:24:42 +01003279 cfg->speaker_outs++;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01003280 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003281 case AC_JACK_HP_OUT:
Takashi Iwaif889fa92007-10-31 15:49:32 +01003282 seq = get_defcfg_sequence(def_conf);
3283 assoc = get_defcfg_association(def_conf);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02003284 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
3285 continue;
3286 cfg->hp_pins[cfg->hp_outs] = nid;
Takashi Iwaif889fa92007-10-31 15:49:32 +01003287 sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
Takashi Iwaieb06ed82006-09-20 17:10:27 +02003288 cfg->hp_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003289 break;
Takashi Iwai314634b2006-09-21 11:56:18 +02003290 case AC_JACK_MIC_IN: {
3291 int preferred, alt;
3292 if (loc == AC_JACK_LOC_FRONT) {
3293 preferred = AUTO_PIN_FRONT_MIC;
3294 alt = AUTO_PIN_MIC;
3295 } else {
3296 preferred = AUTO_PIN_MIC;
3297 alt = AUTO_PIN_FRONT_MIC;
3298 }
3299 if (!cfg->input_pins[preferred])
3300 cfg->input_pins[preferred] = nid;
3301 else if (!cfg->input_pins[alt])
3302 cfg->input_pins[alt] = nid;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003303 break;
Takashi Iwai314634b2006-09-21 11:56:18 +02003304 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003305 case AC_JACK_LINE_IN:
3306 if (loc == AC_JACK_LOC_FRONT)
3307 cfg->input_pins[AUTO_PIN_FRONT_LINE] = nid;
3308 else
3309 cfg->input_pins[AUTO_PIN_LINE] = nid;
3310 break;
3311 case AC_JACK_CD:
3312 cfg->input_pins[AUTO_PIN_CD] = nid;
3313 break;
3314 case AC_JACK_AUX:
3315 cfg->input_pins[AUTO_PIN_AUX] = nid;
3316 break;
3317 case AC_JACK_SPDIF_OUT:
3318 cfg->dig_out_pin = nid;
3319 break;
3320 case AC_JACK_SPDIF_IN:
3321 cfg->dig_in_pin = nid;
3322 break;
3323 }
3324 }
3325
Takashi Iwai5832fcf2008-02-12 18:30:12 +01003326 /* FIX-UP:
3327 * If no line-out is defined but multiple HPs are found,
3328 * some of them might be the real line-outs.
3329 */
3330 if (!cfg->line_outs && cfg->hp_outs > 1) {
3331 int i = 0;
3332 while (i < cfg->hp_outs) {
3333 /* The real HPs should have the sequence 0x0f */
3334 if ((sequences_hp[i] & 0x0f) == 0x0f) {
3335 i++;
3336 continue;
3337 }
3338 /* Move it to the line-out table */
3339 cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
3340 sequences_line_out[cfg->line_outs] = sequences_hp[i];
3341 cfg->line_outs++;
3342 cfg->hp_outs--;
3343 memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
3344 sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
3345 memmove(sequences_hp + i - 1, sequences_hp + i,
3346 sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
3347 }
3348 }
3349
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003350 /* sort by sequence */
Steve Longerbeam81937d32007-05-08 15:33:03 +02003351 sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
3352 cfg->line_outs);
3353 sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
3354 cfg->speaker_outs);
Takashi Iwaif889fa92007-10-31 15:49:32 +01003355 sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
3356 cfg->hp_outs);
Steve Longerbeam81937d32007-05-08 15:33:03 +02003357
Takashi Iwaif889fa92007-10-31 15:49:32 +01003358 /* if we have only one mic, make it AUTO_PIN_MIC */
3359 if (!cfg->input_pins[AUTO_PIN_MIC] &&
3360 cfg->input_pins[AUTO_PIN_FRONT_MIC]) {
3361 cfg->input_pins[AUTO_PIN_MIC] =
3362 cfg->input_pins[AUTO_PIN_FRONT_MIC];
3363 cfg->input_pins[AUTO_PIN_FRONT_MIC] = 0;
3364 }
3365 /* ditto for line-in */
3366 if (!cfg->input_pins[AUTO_PIN_LINE] &&
3367 cfg->input_pins[AUTO_PIN_FRONT_LINE]) {
3368 cfg->input_pins[AUTO_PIN_LINE] =
3369 cfg->input_pins[AUTO_PIN_FRONT_LINE];
3370 cfg->input_pins[AUTO_PIN_FRONT_LINE] = 0;
3371 }
3372
Steve Longerbeam81937d32007-05-08 15:33:03 +02003373 /*
3374 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
3375 * as a primary output
3376 */
3377 if (!cfg->line_outs) {
3378 if (cfg->speaker_outs) {
3379 cfg->line_outs = cfg->speaker_outs;
3380 memcpy(cfg->line_out_pins, cfg->speaker_pins,
3381 sizeof(cfg->speaker_pins));
3382 cfg->speaker_outs = 0;
3383 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
3384 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
3385 } else if (cfg->hp_outs) {
3386 cfg->line_outs = cfg->hp_outs;
3387 memcpy(cfg->line_out_pins, cfg->hp_pins,
3388 sizeof(cfg->hp_pins));
3389 cfg->hp_outs = 0;
3390 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
3391 cfg->line_out_type = AUTO_PIN_HP_OUT;
3392 }
3393 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003394
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02003395 /* Reorder the surround channels
3396 * ALSA sequence is front/surr/clfe/side
3397 * HDA sequence is:
3398 * 4-ch: front/surr => OK as it is
3399 * 6-ch: front/clfe/surr
Takashi Iwai9422db42007-04-20 16:11:43 +02003400 * 8-ch: front/clfe/rear/side|fc
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02003401 */
3402 switch (cfg->line_outs) {
3403 case 3:
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02003404 case 4:
3405 nid = cfg->line_out_pins[1];
Takashi Iwai9422db42007-04-20 16:11:43 +02003406 cfg->line_out_pins[1] = cfg->line_out_pins[2];
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02003407 cfg->line_out_pins[2] = nid;
3408 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003409 }
3410
Takashi Iwai82bc9552006-03-21 11:24:42 +01003411 /*
3412 * debug prints of the parsed results
3413 */
3414 snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3415 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
3416 cfg->line_out_pins[2], cfg->line_out_pins[3],
3417 cfg->line_out_pins[4]);
3418 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3419 cfg->speaker_outs, cfg->speaker_pins[0],
3420 cfg->speaker_pins[1], cfg->speaker_pins[2],
3421 cfg->speaker_pins[3], cfg->speaker_pins[4]);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02003422 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3423 cfg->hp_outs, cfg->hp_pins[0],
3424 cfg->hp_pins[1], cfg->hp_pins[2],
3425 cfg->hp_pins[3], cfg->hp_pins[4]);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01003426 snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
Takashi Iwai82bc9552006-03-21 11:24:42 +01003427 snd_printd(" inputs: mic=0x%x, fmic=0x%x, line=0x%x, fline=0x%x,"
3428 " cd=0x%x, aux=0x%x\n",
3429 cfg->input_pins[AUTO_PIN_MIC],
3430 cfg->input_pins[AUTO_PIN_FRONT_MIC],
3431 cfg->input_pins[AUTO_PIN_LINE],
3432 cfg->input_pins[AUTO_PIN_FRONT_LINE],
3433 cfg->input_pins[AUTO_PIN_CD],
3434 cfg->input_pins[AUTO_PIN_AUX]);
3435
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003436 return 0;
3437}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003438EXPORT_SYMBOL_HDA(snd_hda_parse_pin_def_config);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003439
Takashi Iwai4a471b72005-12-07 13:56:29 +01003440/* labels for input pins */
3441const char *auto_pin_cfg_labels[AUTO_PIN_LAST] = {
3442 "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux"
3443};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003444EXPORT_SYMBOL_HDA(auto_pin_cfg_labels);
Takashi Iwai4a471b72005-12-07 13:56:29 +01003445
3446
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447#ifdef CONFIG_PM
3448/*
3449 * power management
3450 */
3451
3452/**
3453 * snd_hda_suspend - suspend the codecs
3454 * @bus: the HDA bus
3455 * @state: suspsend state
3456 *
3457 * Returns 0 if successful.
3458 */
3459int snd_hda_suspend(struct hda_bus *bus, pm_message_t state)
3460{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003461 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462
Takashi Iwai0ba21762007-04-16 11:29:14 +02003463 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai0b7a2e92007-08-14 15:18:26 +02003464#ifdef CONFIG_SND_HDA_POWER_SAVE
3465 if (!codec->power_on)
3466 continue;
3467#endif
Takashi Iwaicb53c622007-08-10 17:21:45 +02003468 hda_call_codec_suspend(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 }
3470 return 0;
3471}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003472EXPORT_SYMBOL_HDA(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473
3474/**
3475 * snd_hda_resume - resume the codecs
3476 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477 *
3478 * Returns 0 if successful.
Takashi Iwaicb53c622007-08-10 17:21:45 +02003479 *
3480 * This fucntion is defined only when POWER_SAVE isn't set.
3481 * In the power-save mode, the codec is resumed dynamically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482 */
3483int snd_hda_resume(struct hda_bus *bus)
3484{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003485 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486
Takashi Iwai0ba21762007-04-16 11:29:14 +02003487 list_for_each_entry(codec, &bus->codec_list, list) {
Maxim Levitskyd804ad92007-09-03 15:28:04 +02003488 if (snd_hda_codec_needs_resume(codec))
3489 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491 return 0;
3492}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003493EXPORT_SYMBOL_HDA(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003494#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02003495
3496/*
3497 * generic arrays
3498 */
3499
3500/* get a new element from the given array
3501 * if it exceeds the pre-allocated array size, re-allocate the array
3502 */
3503void *snd_array_new(struct snd_array *array)
3504{
3505 if (array->used >= array->alloced) {
3506 int num = array->alloced + array->alloc_align;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01003507 void *nlist;
3508 if (snd_BUG_ON(num >= 4096))
3509 return NULL;
3510 nlist = kcalloc(num + 1, array->elem_size, GFP_KERNEL);
Takashi Iwaib2e18592008-07-30 15:01:44 +02003511 if (!nlist)
3512 return NULL;
3513 if (array->list) {
3514 memcpy(nlist, array->list,
3515 array->elem_size * array->alloced);
3516 kfree(array->list);
3517 }
3518 array->list = nlist;
3519 array->alloced = num;
3520 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01003521 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02003522}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003523EXPORT_SYMBOL_HDA(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02003524
3525/* free the given array elements */
3526void snd_array_free(struct snd_array *array)
3527{
3528 kfree(array->list);
3529 array->used = 0;
3530 array->alloced = 0;
3531 array->list = NULL;
3532}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003533EXPORT_SYMBOL_HDA(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01003534
3535/*
3536 * used by hda_proc.c and hda_eld.c
3537 */
3538void snd_print_pcm_rates(int pcm, char *buf, int buflen)
3539{
3540 static unsigned int rates[] = {
3541 8000, 11025, 16000, 22050, 32000, 44100, 48000, 88200,
3542 96000, 176400, 192000, 384000
3543 };
3544 int i, j;
3545
3546 for (i = 0, j = 0; i < ARRAY_SIZE(rates); i++)
3547 if (pcm & (1 << i))
3548 j += snprintf(buf + j, buflen - j, " %d", rates[i]);
3549
3550 buf[j] = '\0'; /* necessary when j == 0 */
3551}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003552EXPORT_SYMBOL_HDA(snd_print_pcm_rates);
Takashi Iwaib2022262008-11-21 21:24:03 +01003553
3554void snd_print_pcm_bits(int pcm, char *buf, int buflen)
3555{
3556 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
3557 int i, j;
3558
3559 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
3560 if (pcm & (AC_SUPPCM_BITS_8 << i))
3561 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
3562
3563 buf[j] = '\0'; /* necessary when j == 0 */
3564}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003565EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003566
3567MODULE_DESCRIPTION("HDA codec core");
3568MODULE_LICENSE("GPL");