blob: f80e5f3b97dc76c7ac44d63062d700e71bd905fe [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Universal Interface for Intel High Definition Audio Codec
3 *
4 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
5 *
6 *
7 * This driver is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This driver is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/init.h>
23#include <linux/delay.h>
24#include <linux/slab.h>
25#include <linux/pci.h>
Ingo Molnar62932df2006-01-16 16:34:20 +010026#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <sound/core.h>
28#include "hda_codec.h"
29#include <sound/asoundef.h>
Jaroslav Kysela302e9c52006-07-05 17:39:49 +020030#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <sound/initval.h>
32#include "hda_local.h"
Takashi Iwai28073142007-07-27 18:58:06 +020033#include <sound/hda_hwdep.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Linus Torvalds1da177e2005-04-16 15:20:36 -070035/*
36 * vendor / preset table
37 */
38
39struct hda_vendor_id {
40 unsigned int id;
41 const char *name;
42};
43
44/* codec vendor labels */
45static struct hda_vendor_id hda_vendor_ids[] = {
Takashi Iwaic8cd1282008-02-13 16:59:29 +010046 { 0x1002, "ATI" },
Takashi Iwaia9226252006-09-17 22:05:54 +020047 { 0x1057, "Motorola" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010048 { 0x1095, "Silicon Image" },
Takashi Iwai31117b72008-12-16 14:43:21 +010049 { 0x10de, "Nvidia" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010050 { 0x10ec, "Realtek" },
Joseph Chanc577b8a2006-11-29 15:29:40 +010051 { 0x1106, "VIA" },
Matthew Ranostay7f168592007-10-18 17:38:17 +020052 { 0x111d, "IDT" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010053 { 0x11c1, "LSI" },
Takashi Iwai54b903e2005-05-15 14:30:10 +020054 { 0x11d4, "Analog Devices" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 { 0x13f6, "C-Media" },
Takashi Iwaia9226252006-09-17 22:05:54 +020056 { 0x14f1, "Conexant" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010057 { 0x17e8, "Chrontel" },
58 { 0x1854, "LG" },
Mark Brown8199de32008-10-28 14:50:13 +000059 { 0x1aec, "Wolfson Microelectronics" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 { 0x434d, "C-Media" },
Takashi Iwai74c61132008-12-18 09:11:33 +010061 { 0x8086, "Intel" },
Matt2f2f4252005-04-13 14:45:30 +020062 { 0x8384, "SigmaTel" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 {} /* terminator */
64};
65
Takashi Iwai1289e9e2008-11-27 15:47:11 +010066static DEFINE_MUTEX(preset_mutex);
67static LIST_HEAD(hda_preset_tables);
68
69int snd_hda_add_codec_preset(struct hda_codec_preset_list *preset)
70{
71 mutex_lock(&preset_mutex);
72 list_add_tail(&preset->list, &hda_preset_tables);
73 mutex_unlock(&preset_mutex);
74 return 0;
75}
Takashi Iwaiff7a3262008-11-28 15:17:06 +010076EXPORT_SYMBOL_HDA(snd_hda_add_codec_preset);
Takashi Iwai1289e9e2008-11-27 15:47:11 +010077
78int snd_hda_delete_codec_preset(struct hda_codec_preset_list *preset)
79{
80 mutex_lock(&preset_mutex);
81 list_del(&preset->list);
82 mutex_unlock(&preset_mutex);
83 return 0;
84}
Takashi Iwaiff7a3262008-11-28 15:17:06 +010085EXPORT_SYMBOL_HDA(snd_hda_delete_codec_preset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Takashi Iwaicb53c622007-08-10 17:21:45 +020087#ifdef CONFIG_SND_HDA_POWER_SAVE
88static void hda_power_work(struct work_struct *work);
89static void hda_keep_power_on(struct hda_codec *codec);
90#else
91static inline void hda_keep_power_on(struct hda_codec *codec) {}
92#endif
93
Matthew Ranostay50a9f792008-10-25 01:05:45 -040094const char *snd_hda_get_jack_location(u32 cfg)
95{
96 static char *bases[7] = {
97 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
98 };
99 static unsigned char specials_idx[] = {
100 0x07, 0x08,
101 0x17, 0x18, 0x19,
102 0x37, 0x38
103 };
104 static char *specials[] = {
105 "Rear Panel", "Drive Bar",
106 "Riser", "HDMI", "ATAPI",
107 "Mobile-In", "Mobile-Out"
108 };
109 int i;
110 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
111 if ((cfg & 0x0f) < 7)
112 return bases[cfg & 0x0f];
113 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
114 if (cfg == specials_idx[i])
115 return specials[i];
116 }
117 return "UNKNOWN";
118}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100119EXPORT_SYMBOL_HDA(snd_hda_get_jack_location);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400120
121const char *snd_hda_get_jack_connectivity(u32 cfg)
122{
123 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
124
125 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
126}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100127EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400128
129const char *snd_hda_get_jack_type(u32 cfg)
130{
131 static char *jack_types[16] = {
132 "Line Out", "Speaker", "HP Out", "CD",
133 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
134 "Line In", "Aux", "Mic", "Telephony",
135 "SPDIF In", "Digitial In", "Reserved", "Other"
136 };
137
138 return jack_types[(cfg & AC_DEFCFG_DEVICE)
139 >> AC_DEFCFG_DEVICE_SHIFT];
140}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100141EXPORT_SYMBOL_HDA(snd_hda_get_jack_type);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400142
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100143/*
144 * Compose a 32bit command word to be sent to the HD-audio controller
145 */
146static inline unsigned int
147make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int direct,
148 unsigned int verb, unsigned int parm)
149{
150 u32 val;
151
152 val = (u32)(codec->addr & 0x0f) << 28;
153 val |= (u32)direct << 27;
154 val |= (u32)nid << 20;
155 val |= verb << 8;
156 val |= parm;
157 return val;
158}
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160/**
161 * snd_hda_codec_read - send a command and get the response
162 * @codec: the HDA codec
163 * @nid: NID to send the command
164 * @direct: direct flag
165 * @verb: the verb to send
166 * @parm: the parameter for the verb
167 *
168 * Send a single command and read the corresponding response.
169 *
170 * Returns the obtained response value, or -1 for an error.
171 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200172unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
173 int direct,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 unsigned int verb, unsigned int parm)
175{
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100176 struct hda_bus *bus = codec->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 unsigned int res;
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100178
179 res = make_codec_cmd(codec, nid, direct, verb, parm);
Takashi Iwaicb53c622007-08-10 17:21:45 +0200180 snd_hda_power_up(codec);
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100181 mutex_lock(&bus->cmd_mutex);
182 if (!bus->ops.command(bus, res))
183 res = bus->ops.get_response(bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 else
185 res = (unsigned int)-1;
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100186 mutex_unlock(&bus->cmd_mutex);
Takashi Iwaicb53c622007-08-10 17:21:45 +0200187 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 return res;
189}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100190EXPORT_SYMBOL_HDA(snd_hda_codec_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192/**
193 * snd_hda_codec_write - send a single command without waiting for response
194 * @codec: the HDA codec
195 * @nid: NID to send the command
196 * @direct: direct flag
197 * @verb: the verb to send
198 * @parm: the parameter for the verb
199 *
200 * Send a single command without waiting for response.
201 *
202 * Returns 0 if successful, or a negative error code.
203 */
204int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
205 unsigned int verb, unsigned int parm)
206{
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100207 struct hda_bus *bus = codec->bus;
208 unsigned int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 int err;
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100210
211 res = make_codec_cmd(codec, nid, direct, verb, parm);
Takashi Iwaicb53c622007-08-10 17:21:45 +0200212 snd_hda_power_up(codec);
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100213 mutex_lock(&bus->cmd_mutex);
214 err = bus->ops.command(bus, res);
215 mutex_unlock(&bus->cmd_mutex);
Takashi Iwaicb53c622007-08-10 17:21:45 +0200216 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 return err;
218}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100219EXPORT_SYMBOL_HDA(snd_hda_codec_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221/**
222 * snd_hda_sequence_write - sequence writes
223 * @codec: the HDA codec
224 * @seq: VERB array to send
225 *
226 * Send the commands sequentially from the given array.
227 * The array must be terminated with NID=0.
228 */
229void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
230{
231 for (; seq->nid; seq++)
232 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
233}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100234EXPORT_SYMBOL_HDA(snd_hda_sequence_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236/**
237 * snd_hda_get_sub_nodes - get the range of sub nodes
238 * @codec: the HDA codec
239 * @nid: NID to parse
240 * @start_id: the pointer to store the start NID
241 *
242 * Parse the NID and store the start NID of its sub-nodes.
243 * Returns the number of sub-nodes.
244 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200245int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
246 hda_nid_t *start_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
248 unsigned int parm;
249
250 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
Danny Tholene8a7f132007-09-11 21:41:56 +0200251 if (parm == -1)
252 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 *start_id = (parm >> 16) & 0x7fff;
254 return (int)(parm & 0x7fff);
255}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100256EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258/**
259 * snd_hda_get_connections - get connection list
260 * @codec: the HDA codec
261 * @nid: NID to parse
262 * @conn_list: connection list array
263 * @max_conns: max. number of connections to store
264 *
265 * Parses the connection list of the given widget and stores the list
266 * of NIDs.
267 *
268 * Returns the number of connections, or a negative error code.
269 */
270int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
271 hda_nid_t *conn_list, int max_conns)
272{
273 unsigned int parm;
Takashi Iwai54d17402005-11-21 16:33:22 +0100274 int i, conn_len, conns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 unsigned int shift, num_elems, mask;
Takashi Iwai54d17402005-11-21 16:33:22 +0100276 hda_nid_t prev_nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Takashi Iwaida3cec32008-08-08 17:12:14 +0200278 if (snd_BUG_ON(!conn_list || max_conns <= 0))
279 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
282 if (parm & AC_CLIST_LONG) {
283 /* long form */
284 shift = 16;
285 num_elems = 2;
286 } else {
287 /* short form */
288 shift = 8;
289 num_elems = 4;
290 }
291 conn_len = parm & AC_CLIST_LENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 mask = (1 << (shift-1)) - 1;
293
Takashi Iwai0ba21762007-04-16 11:29:14 +0200294 if (!conn_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 return 0; /* no connection */
296
297 if (conn_len == 1) {
298 /* single connection */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200299 parm = snd_hda_codec_read(codec, nid, 0,
300 AC_VERB_GET_CONNECT_LIST, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 conn_list[0] = parm & mask;
302 return 1;
303 }
304
305 /* multi connection */
306 conns = 0;
Takashi Iwai54d17402005-11-21 16:33:22 +0100307 prev_nid = 0;
308 for (i = 0; i < conn_len; i++) {
309 int range_val;
310 hda_nid_t val, n;
311
312 if (i % num_elems == 0)
313 parm = snd_hda_codec_read(codec, nid, 0,
314 AC_VERB_GET_CONNECT_LIST, i);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200315 range_val = !!(parm & (1 << (shift-1))); /* ranges */
Takashi Iwai54d17402005-11-21 16:33:22 +0100316 val = parm & mask;
317 parm >>= shift;
318 if (range_val) {
319 /* ranges between the previous and this one */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200320 if (!prev_nid || prev_nid >= val) {
321 snd_printk(KERN_WARNING "hda_codec: "
322 "invalid dep_range_val %x:%x\n",
323 prev_nid, val);
Takashi Iwai54d17402005-11-21 16:33:22 +0100324 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100326 for (n = prev_nid + 1; n <= val; n++) {
327 if (conns >= max_conns) {
Takashi Iwai0ba21762007-04-16 11:29:14 +0200328 snd_printk(KERN_ERR
329 "Too many connections\n");
Takashi Iwai54d17402005-11-21 16:33:22 +0100330 return -EINVAL;
331 }
332 conn_list[conns++] = n;
333 }
334 } else {
335 if (conns >= max_conns) {
336 snd_printk(KERN_ERR "Too many connections\n");
337 return -EINVAL;
338 }
339 conn_list[conns++] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100341 prev_nid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 }
343 return conns;
344}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100345EXPORT_SYMBOL_HDA(snd_hda_get_connections);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347
348/**
349 * snd_hda_queue_unsol_event - add an unsolicited event to queue
350 * @bus: the BUS
351 * @res: unsolicited event (lower 32bit of RIRB entry)
352 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
353 *
354 * Adds the given event to the queue. The events are processed in
355 * the workqueue asynchronously. Call this function in the interrupt
356 * hanlder when RIRB receives an unsolicited event.
357 *
358 * Returns 0 if successful, or a negative error code.
359 */
360int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
361{
362 struct hda_bus_unsolicited *unsol;
363 unsigned int wp;
364
Takashi Iwai0ba21762007-04-16 11:29:14 +0200365 unsol = bus->unsol;
366 if (!unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 return 0;
368
369 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
370 unsol->wp = wp;
371
372 wp <<= 1;
373 unsol->queue[wp] = res;
374 unsol->queue[wp + 1] = res_ex;
375
Takashi Iwaie250af22006-12-19 17:08:52 +0100376 schedule_work(&unsol->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
378 return 0;
379}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100380EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
382/*
Wu Fengguang5c1d1a92008-10-07 14:17:53 +0800383 * process queued unsolicited events
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 */
David Howellsc4028952006-11-22 14:57:56 +0000385static void process_unsol_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
David Howellsc4028952006-11-22 14:57:56 +0000387 struct hda_bus_unsolicited *unsol =
388 container_of(work, struct hda_bus_unsolicited, work);
389 struct hda_bus *bus = unsol->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 struct hda_codec *codec;
391 unsigned int rp, caddr, res;
392
393 while (unsol->rp != unsol->wp) {
394 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
395 unsol->rp = rp;
396 rp <<= 1;
397 res = unsol->queue[rp];
398 caddr = unsol->queue[rp + 1];
Takashi Iwai0ba21762007-04-16 11:29:14 +0200399 if (!(caddr & (1 << 4))) /* no unsolicited event? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 continue;
401 codec = bus->caddr_tbl[caddr & 0x0f];
402 if (codec && codec->patch_ops.unsol_event)
403 codec->patch_ops.unsol_event(codec, res);
404 }
405}
406
407/*
408 * initialize unsolicited queue
409 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200410static int init_unsol_queue(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411{
412 struct hda_bus_unsolicited *unsol;
413
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100414 if (bus->unsol) /* already initialized */
415 return 0;
416
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200417 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200418 if (!unsol) {
419 snd_printk(KERN_ERR "hda_codec: "
420 "can't allocate unsolicited queue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 return -ENOMEM;
422 }
David Howellsc4028952006-11-22 14:57:56 +0000423 INIT_WORK(&unsol->work, process_unsol_events);
424 unsol->bus = bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 bus->unsol = unsol;
426 return 0;
427}
428
429/*
430 * destructor
431 */
432static void snd_hda_codec_free(struct hda_codec *codec);
433
434static int snd_hda_bus_free(struct hda_bus *bus)
435{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200436 struct hda_codec *codec, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Takashi Iwai0ba21762007-04-16 11:29:14 +0200438 if (!bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 return 0;
440 if (bus->unsol) {
Takashi Iwaie250af22006-12-19 17:08:52 +0100441 flush_scheduled_work();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 kfree(bus->unsol);
443 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200444 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 snd_hda_codec_free(codec);
446 }
447 if (bus->ops.private_free)
448 bus->ops.private_free(bus);
449 kfree(bus);
450 return 0;
451}
452
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100453static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
455 struct hda_bus *bus = device->device_data;
Takashi Iwaib94d35392008-11-21 09:08:06 +0100456 bus->shutdown = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 return snd_hda_bus_free(bus);
458}
459
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200460#ifdef CONFIG_SND_HDA_HWDEP
461static int snd_hda_bus_dev_register(struct snd_device *device)
462{
463 struct hda_bus *bus = device->device_data;
464 struct hda_codec *codec;
465 list_for_each_entry(codec, &bus->codec_list, list) {
466 snd_hda_hwdep_add_sysfs(codec);
467 }
468 return 0;
469}
470#else
471#define snd_hda_bus_dev_register NULL
472#endif
473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474/**
475 * snd_hda_bus_new - create a HDA bus
476 * @card: the card entry
477 * @temp: the template for hda_bus information
478 * @busp: the pointer to store the created bus instance
479 *
480 * Returns 0 if successful, or a negative error code.
481 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100482int /*__devinit*/ snd_hda_bus_new(struct snd_card *card,
Takashi Iwai756e2b02007-04-16 11:27:07 +0200483 const struct hda_bus_template *temp,
484 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
486 struct hda_bus *bus;
487 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100488 static struct snd_device_ops dev_ops = {
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200489 .dev_register = snd_hda_bus_dev_register,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 .dev_free = snd_hda_bus_dev_free,
491 };
492
Takashi Iwaida3cec32008-08-08 17:12:14 +0200493 if (snd_BUG_ON(!temp))
494 return -EINVAL;
495 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
496 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498 if (busp)
499 *busp = NULL;
500
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200501 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 if (bus == NULL) {
503 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
504 return -ENOMEM;
505 }
506
507 bus->card = card;
508 bus->private_data = temp->private_data;
509 bus->pci = temp->pci;
510 bus->modelname = temp->modelname;
Takashi Iwaifee2fba2008-11-27 12:43:28 +0100511 bus->power_save = temp->power_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 bus->ops = temp->ops;
513
Ingo Molnar62932df2006-01-16 16:34:20 +0100514 mutex_init(&bus->cmd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 INIT_LIST_HEAD(&bus->codec_list);
516
Takashi Iwai0ba21762007-04-16 11:29:14 +0200517 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
518 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 snd_hda_bus_free(bus);
520 return err;
521 }
522 if (busp)
523 *busp = bus;
524 return 0;
525}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100526EXPORT_SYMBOL_HDA(snd_hda_bus_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Takashi Iwai82467612007-07-27 19:15:54 +0200528#ifdef CONFIG_SND_HDA_GENERIC
529#define is_generic_config(codec) \
Takashi Iwaif44ac832008-07-30 15:01:45 +0200530 (codec->modelname && !strcmp(codec->modelname, "generic"))
Takashi Iwai82467612007-07-27 19:15:54 +0200531#else
532#define is_generic_config(codec) 0
533#endif
534
Takashi Iwai645f10c2008-11-28 15:07:37 +0100535#ifdef MODULE
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100536#define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
537#else
Takashi Iwai645f10c2008-11-28 15:07:37 +0100538#define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100539#endif
540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541/*
542 * find a matching codec preset
543 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200544static const struct hda_codec_preset *
Takashi Iwai756e2b02007-04-16 11:27:07 +0200545find_codec_preset(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100547 struct hda_codec_preset_list *tbl;
548 const struct hda_codec_preset *preset;
549 int mod_requested = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Takashi Iwai82467612007-07-27 19:15:54 +0200551 if (is_generic_config(codec))
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100552 return NULL; /* use the generic parser */
553
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100554 again:
555 mutex_lock(&preset_mutex);
556 list_for_each_entry(tbl, &hda_preset_tables, list) {
557 if (!try_module_get(tbl->owner)) {
558 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
559 continue;
560 }
561 for (preset = tbl->preset; preset->id; preset++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 u32 mask = preset->mask;
Marc Boucherca7cfae2008-01-22 15:32:25 +0100563 if (preset->afg && preset->afg != codec->afg)
564 continue;
565 if (preset->mfg && preset->mfg != codec->mfg)
566 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200567 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 mask = ~0;
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200569 if (preset->id == (codec->vendor_id & mask) &&
Takashi Iwai0ba21762007-04-16 11:29:14 +0200570 (!preset->rev ||
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100571 preset->rev == codec->revision_id)) {
572 mutex_unlock(&preset_mutex);
573 codec->owner = tbl->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 return preset;
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100575 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 }
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100577 module_put(tbl->owner);
578 }
579 mutex_unlock(&preset_mutex);
580
581 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
582 char name[32];
583 if (!mod_requested)
584 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
585 codec->vendor_id);
586 else
587 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
588 (codec->vendor_id >> 16) & 0xffff);
589 request_module(name);
590 mod_requested++;
591 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 }
593 return NULL;
594}
595
596/*
Takashi Iwaif44ac832008-07-30 15:01:45 +0200597 * get_codec_name - store the codec name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 */
Takashi Iwaif44ac832008-07-30 15:01:45 +0200599static int get_codec_name(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600{
601 const struct hda_vendor_id *c;
602 const char *vendor = NULL;
603 u16 vendor_id = codec->vendor_id >> 16;
Takashi Iwaif44ac832008-07-30 15:01:45 +0200604 char tmp[16], name[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606 for (c = hda_vendor_ids; c->id; c++) {
607 if (c->id == vendor_id) {
608 vendor = c->name;
609 break;
610 }
611 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200612 if (!vendor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 sprintf(tmp, "Generic %04x", vendor_id);
614 vendor = tmp;
615 }
616 if (codec->preset && codec->preset->name)
Takashi Iwaif44ac832008-07-30 15:01:45 +0200617 snprintf(name, sizeof(name), "%s %s", vendor,
618 codec->preset->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 else
Takashi Iwaif44ac832008-07-30 15:01:45 +0200620 snprintf(name, sizeof(name), "%s ID %x", vendor,
Takashi Iwai0ba21762007-04-16 11:29:14 +0200621 codec->vendor_id & 0xffff);
Takashi Iwaif44ac832008-07-30 15:01:45 +0200622 codec->name = kstrdup(name, GFP_KERNEL);
623 if (!codec->name)
624 return -ENOMEM;
625 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626}
627
628/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200629 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100631static void /*__devinit*/ setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
633 int i, total_nodes;
634 hda_nid_t nid;
635
636 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
637 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +0200638 unsigned int func;
639 func = snd_hda_param_read(codec, nid, AC_PAR_FUNCTION_TYPE);
640 switch (func & 0xff) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200641 case AC_GRP_AUDIO_FUNCTION:
642 codec->afg = nid;
643 break;
644 case AC_GRP_MODEM_FUNCTION:
645 codec->mfg = nid;
646 break;
647 default:
648 break;
649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651}
652
653/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100654 * read widget caps for each widget and store in cache
655 */
656static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
657{
658 int i;
659 hda_nid_t nid;
660
661 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
662 &codec->start_nid);
663 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200664 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +0100665 return -ENOMEM;
666 nid = codec->start_nid;
667 for (i = 0; i < codec->num_nodes; i++, nid++)
668 codec->wcaps[i] = snd_hda_param_read(codec, nid,
669 AC_PAR_AUDIO_WIDGET_CAP);
670 return 0;
671}
672
673
Takashi Iwai01751f52007-08-10 16:59:39 +0200674static void init_hda_cache(struct hda_cache_rec *cache,
675 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +0200676static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +0200677
Takashi Iwai54d17402005-11-21 16:33:22 +0100678/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 * codec destructor
680 */
681static void snd_hda_codec_free(struct hda_codec *codec)
682{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200683 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 return;
Takashi Iwaicb53c622007-08-10 17:21:45 +0200685#ifdef CONFIG_SND_HDA_POWER_SAVE
686 cancel_delayed_work(&codec->power_work);
Takashi Iwai2525fdc2007-08-15 22:18:22 +0200687 flush_scheduled_work();
Takashi Iwaicb53c622007-08-10 17:21:45 +0200688#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +0200690 snd_array_free(&codec->mixers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 codec->bus->caddr_tbl[codec->addr] = NULL;
692 if (codec->patch_ops.free)
693 codec->patch_ops.free(codec);
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100694 module_put(codec->owner);
Takashi Iwai01751f52007-08-10 16:59:39 +0200695 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +0200696 free_hda_cache(&codec->cmd_cache);
Takashi Iwaif44ac832008-07-30 15:01:45 +0200697 kfree(codec->name);
698 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +0100699 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 kfree(codec);
701}
702
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703/**
704 * snd_hda_codec_new - create a HDA codec
705 * @bus: the bus to assign
706 * @codec_addr: the codec address
707 * @codecp: the pointer to store the generated codec
708 *
709 * Returns 0 if successful, or a negative error code.
710 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100711int /*__devinit*/ snd_hda_codec_new(struct hda_bus *bus, unsigned int codec_addr,
Takashi Iwaid4d9cd032008-12-19 15:19:11 +0100712 int do_init, struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
714 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +0200715 char component[31];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 int err;
717
Takashi Iwaida3cec32008-08-08 17:12:14 +0200718 if (snd_BUG_ON(!bus))
719 return -EINVAL;
720 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
721 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +0200724 snd_printk(KERN_ERR "hda_codec: "
725 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 return -EBUSY;
727 }
728
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200729 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 if (codec == NULL) {
731 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
732 return -ENOMEM;
733 }
734
735 codec->bus = bus;
736 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +0100737 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +0800738 mutex_init(&codec->control_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +0200739 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +0200740 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwaid13bd412008-07-30 15:01:45 +0200741 snd_array_init(&codec->mixers, sizeof(struct snd_kcontrol *), 32);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200742 if (codec->bus->modelname) {
743 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
744 if (!codec->modelname) {
745 snd_hda_codec_free(codec);
746 return -ENODEV;
747 }
748 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Takashi Iwaicb53c622007-08-10 17:21:45 +0200750#ifdef CONFIG_SND_HDA_POWER_SAVE
751 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
752 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
753 * the caller has to power down appropriatley after initialization
754 * phase.
755 */
756 hda_keep_power_on(codec);
757#endif
758
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 list_add_tail(&codec->list, &bus->codec_list);
760 bus->caddr_tbl[codec_addr] = codec;
761
Takashi Iwai0ba21762007-04-16 11:29:14 +0200762 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
763 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +0100764 if (codec->vendor_id == -1)
765 /* read again, hopefully the access method was corrected
766 * in the last read...
767 */
768 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
769 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200770 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
771 AC_PAR_SUBSYSTEM_ID);
772 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
773 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200775 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200776 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200777 snd_printdd("hda_codec: no AFG or MFG node found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 snd_hda_codec_free(codec);
779 return -ENODEV;
780 }
781
Takashi Iwai54d17402005-11-21 16:33:22 +0100782 if (read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg) < 0) {
783 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
784 snd_hda_codec_free(codec);
785 return -ENOMEM;
786 }
787
Takashi Iwai0ba21762007-04-16 11:29:14 +0200788 if (!codec->subsystem_id) {
Takashi Iwai86284e42005-10-11 15:05:54 +0200789 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200790 codec->subsystem_id =
791 snd_hda_codec_read(codec, nid, 0,
792 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +0200793 }
Takashi Iwaif44ac832008-07-30 15:01:45 +0200794 if (bus->modelname)
795 codec->modelname = kstrdup(bus->modelname, GFP_KERNEL);
Takashi Iwai86284e42005-10-11 15:05:54 +0200796
Takashi Iwaid4d9cd032008-12-19 15:19:11 +0100797 if (do_init) {
798 err = snd_hda_codec_configure(codec);
799 if (err < 0) {
800 snd_hda_codec_free(codec);
801 return err;
802 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200803 }
804 snd_hda_codec_proc_new(codec);
805
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200806 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200807
808 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
809 codec->subsystem_id, codec->revision_id);
810 snd_component_add(codec->bus->card, component);
811
812 if (codecp)
813 *codecp = codec;
814 return 0;
815}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100816EXPORT_SYMBOL_HDA(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200817
818int snd_hda_codec_configure(struct hda_codec *codec)
819{
820 int err;
821
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100822 codec->preset = find_codec_preset(codec);
Takashi Iwaif44ac832008-07-30 15:01:45 +0200823 if (!codec->name) {
824 err = get_codec_name(codec);
825 if (err < 0)
826 return err;
827 }
Takashi Iwai43ea1d42007-04-26 19:12:08 +0200828 /* audio codec should override the mixer name */
Takashi Iwaif44ac832008-07-30 15:01:45 +0200829 if (codec->afg || !*codec->bus->card->mixername)
830 strlcpy(codec->bus->card->mixername, codec->name,
831 sizeof(codec->bus->card->mixername));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
Takashi Iwai82467612007-07-27 19:15:54 +0200833 if (is_generic_config(codec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai82467612007-07-27 19:15:54 +0200835 goto patched;
836 }
Takashi Iwai82467612007-07-27 19:15:54 +0200837 if (codec->preset && codec->preset->patch) {
838 err = codec->preset->patch(codec);
839 goto patched;
840 }
841
842 /* call the default parser */
Takashi Iwai82467612007-07-27 19:15:54 +0200843 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai35a1e0c2007-10-19 08:13:40 +0200844 if (err < 0)
845 printk(KERN_ERR "hda-codec: No codec parser is available\n");
Takashi Iwai82467612007-07-27 19:15:54 +0200846
847 patched:
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200848 if (!err && codec->patch_ops.unsol_event)
849 err = init_unsol_queue(codec->bus);
850 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851}
852
853/**
854 * snd_hda_codec_setup_stream - set up the codec for streaming
855 * @codec: the CODEC to set up
856 * @nid: the NID to set up
857 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
858 * @channel_id: channel id to pass, zero based.
859 * @format: stream format.
860 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200861void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
862 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 int channel_id, int format)
864{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200865 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +0200866 return;
867
Takashi Iwai0ba21762007-04-16 11:29:14 +0200868 snd_printdd("hda_codec_setup_stream: "
869 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 nid, stream_tag, channel_id, format);
871 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID,
872 (stream_tag << 4) | channel_id);
873 msleep(1);
874 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, format);
875}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100876EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Takashi Iwai888afa12008-03-18 09:57:50 +0100878void snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid)
879{
880 if (!nid)
881 return;
882
883 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
884 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
885#if 0 /* keep the format */
886 msleep(1);
887 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0);
888#endif
889}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100890EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +0100891
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892/*
893 * amp access functions
894 */
895
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200896/* FIXME: more better hash key? */
897#define HDA_HASH_KEY(nid,dir,idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200899#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
901/* initialize the hash table */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100902static void /*__devinit*/ init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +0200903 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904{
Takashi Iwai01751f52007-08-10 16:59:39 +0200905 memset(cache, 0, sizeof(*cache));
906 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +0200907 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +0200908}
909
Takashi Iwai1fcaee62007-08-23 00:01:09 +0200910static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +0200911{
Takashi Iwai603c4012008-07-30 15:01:44 +0200912 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913}
914
915/* query the hash. allocate an entry if not found. */
Takashi Iwai01751f52007-08-10 16:59:39 +0200916static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
917 u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918{
Takashi Iwai01751f52007-08-10 16:59:39 +0200919 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
920 u16 cur = cache->hash[idx];
921 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
923 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +0100924 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 if (info->key == key)
926 return info;
927 cur = info->next;
928 }
929
930 /* add a new hash entry */
Takashi Iwai603c4012008-07-30 15:01:44 +0200931 info = snd_array_new(&cache->buf);
Takashi Iwaic2174292008-11-07 00:23:30 +0100932 if (!info)
933 return NULL;
Takashi Iwaif43aa022008-11-10 16:24:26 +0100934 cur = snd_array_index(&cache->buf, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 info->key = key;
Takashi Iwai01751f52007-08-10 16:59:39 +0200936 info->val = 0;
937 info->next = cache->hash[idx];
938 cache->hash[idx] = cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
940 return info;
941}
942
Takashi Iwai01751f52007-08-10 16:59:39 +0200943/* query and allocate an amp hash entry */
944static inline struct hda_amp_info *
945get_alloc_amp_hash(struct hda_codec *codec, u32 key)
946{
947 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
948}
949
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950/*
951 * query AMP capabilities for the given widget and direction
952 */
Matthew Ranostay09a99952008-01-24 11:49:21 +0100953u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200955 struct hda_amp_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
Takashi Iwai0ba21762007-04-16 11:29:14 +0200957 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
958 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 return 0;
Takashi Iwai01751f52007-08-10 16:59:39 +0200960 if (!(info->head.val & INFO_AMP_CAPS)) {
Takashi Iwai0ba21762007-04-16 11:29:14 +0200961 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 nid = codec->afg;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200963 info->amp_caps = snd_hda_param_read(codec, nid,
964 direction == HDA_OUTPUT ?
965 AC_PAR_AMP_OUT_CAP :
966 AC_PAR_AMP_IN_CAP);
Takashi Iwaib75e53f2007-05-10 16:56:09 +0200967 if (info->amp_caps)
Takashi Iwai01751f52007-08-10 16:59:39 +0200968 info->head.val |= INFO_AMP_CAPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 }
970 return info->amp_caps;
971}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100972EXPORT_SYMBOL_HDA(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Takashi Iwai897cc182007-05-29 19:01:37 +0200974int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
975 unsigned int caps)
976{
977 struct hda_amp_info *info;
978
979 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
980 if (!info)
981 return -EINVAL;
982 info->amp_caps = caps;
Takashi Iwai01751f52007-08-10 16:59:39 +0200983 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai897cc182007-05-29 19:01:37 +0200984 return 0;
985}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100986EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +0200987
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988/*
989 * read the current volume to info
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200990 * if the cache exists, read the cache value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200992static unsigned int get_vol_mute(struct hda_codec *codec,
993 struct hda_amp_info *info, hda_nid_t nid,
994 int ch, int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995{
996 u32 val, parm;
997
Takashi Iwai01751f52007-08-10 16:59:39 +0200998 if (info->head.val & INFO_AMP_VOL(ch))
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200999 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
1001 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1002 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1003 parm |= index;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001004 val = snd_hda_codec_read(codec, nid, 0,
1005 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 info->vol[ch] = val & 0xff;
Takashi Iwai01751f52007-08-10 16:59:39 +02001007 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001008 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009}
1010
1011/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001012 * write the current volume in info to the h/w and update the cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001014static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001015 hda_nid_t nid, int ch, int direction, int index,
1016 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017{
1018 u32 parm;
1019
1020 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1021 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1022 parm |= index << AC_AMP_SET_INDEX_SHIFT;
1023 parm |= val;
1024 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001025 info->vol[ch] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026}
1027
1028/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001029 * read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 */
Takashi Iwai834be882006-03-01 14:16:17 +01001031int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1032 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001034 struct hda_amp_info *info;
1035 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1036 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001038 return get_vol_mute(codec, info, nid, ch, direction, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001040EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001042/*
1043 * update the AMP value, mask = bit mask to set, val = the value
1044 */
Takashi Iwai834be882006-03-01 14:16:17 +01001045int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1046 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001048 struct hda_amp_info *info;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001049
Takashi Iwai0ba21762007-04-16 11:29:14 +02001050 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
1051 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001053 val &= mask;
1054 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001055 if (info->vol[ch] == val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001057 put_vol_mute(codec, info, nid, ch, direction, idx, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 return 1;
1059}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001060EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
Takashi Iwai47fd8302007-08-10 17:11:07 +02001062/*
1063 * update the AMP stereo with the same mask and value
1064 */
1065int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1066 int direction, int idx, int mask, int val)
1067{
1068 int ch, ret = 0;
1069 for (ch = 0; ch < 2; ch++)
1070 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1071 idx, mask, val);
1072 return ret;
1073}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001074EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001075
Takashi Iwaicb53c622007-08-10 17:21:45 +02001076#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001077/* resume the all amp commands from the cache */
1078void snd_hda_codec_resume_amp(struct hda_codec *codec)
1079{
Takashi Iwai603c4012008-07-30 15:01:44 +02001080 struct hda_amp_info *buffer = codec->amp_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001081 int i;
1082
Takashi Iwai603c4012008-07-30 15:01:44 +02001083 for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001084 u32 key = buffer->head.key;
1085 hda_nid_t nid;
1086 unsigned int idx, dir, ch;
1087 if (!key)
1088 continue;
1089 nid = key & 0xff;
1090 idx = (key >> 16) & 0xff;
1091 dir = (key >> 24) & 0xff;
1092 for (ch = 0; ch < 2; ch++) {
1093 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
1094 continue;
1095 put_vol_mute(codec, buffer, nid, ch, dir, idx,
1096 buffer->vol[ch]);
1097 }
1098 }
1099}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001100EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001101#endif /* SND_HDA_NEEDS_RESUME */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103/* volume */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001104int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1105 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106{
1107 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1108 u16 nid = get_amp_nid(kcontrol);
1109 u8 chs = get_amp_channels(kcontrol);
1110 int dir = get_amp_direction(kcontrol);
1111 u32 caps;
1112
1113 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001114 /* num steps */
1115 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1116 if (!caps) {
1117 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01001118 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
1119 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 return -EINVAL;
1121 }
1122 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1123 uinfo->count = chs == 3 ? 2 : 1;
1124 uinfo->value.integer.min = 0;
1125 uinfo->value.integer.max = caps;
1126 return 0;
1127}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001128EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
Takashi Iwai0ba21762007-04-16 11:29:14 +02001130int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1131 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132{
1133 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1134 hda_nid_t nid = get_amp_nid(kcontrol);
1135 int chs = get_amp_channels(kcontrol);
1136 int dir = get_amp_direction(kcontrol);
1137 int idx = get_amp_index(kcontrol);
1138 long *valp = ucontrol->value.integer.value;
1139
1140 if (chs & 1)
Takashi Iwai47fd8302007-08-10 17:11:07 +02001141 *valp++ = snd_hda_codec_amp_read(codec, nid, 0, dir, idx)
1142 & HDA_AMP_VOLMASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 if (chs & 2)
Takashi Iwai47fd8302007-08-10 17:11:07 +02001144 *valp = snd_hda_codec_amp_read(codec, nid, 1, dir, idx)
1145 & HDA_AMP_VOLMASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 return 0;
1147}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001148EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
Takashi Iwai0ba21762007-04-16 11:29:14 +02001150int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1151 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152{
1153 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1154 hda_nid_t nid = get_amp_nid(kcontrol);
1155 int chs = get_amp_channels(kcontrol);
1156 int dir = get_amp_direction(kcontrol);
1157 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 long *valp = ucontrol->value.integer.value;
1159 int change = 0;
1160
Takashi Iwaicb53c622007-08-10 17:21:45 +02001161 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001162 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001163 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
1164 0x7f, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001165 valp++;
1166 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001167 if (chs & 2)
1168 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001169 0x7f, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001170 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 return change;
1172}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001173EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001175int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1176 unsigned int size, unsigned int __user *_tlv)
1177{
1178 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1179 hda_nid_t nid = get_amp_nid(kcontrol);
1180 int dir = get_amp_direction(kcontrol);
1181 u32 caps, val1, val2;
1182
1183 if (size < 4 * sizeof(unsigned int))
1184 return -ENOMEM;
1185 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001186 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1187 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001188 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
1189 val1 = ((int)val1) * ((int)val2);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001190 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
1191 return -EFAULT;
1192 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
1193 return -EFAULT;
1194 if (put_user(val1, _tlv + 2))
1195 return -EFAULT;
1196 if (put_user(val2, _tlv + 3))
1197 return -EFAULT;
1198 return 0;
1199}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001200EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001201
Takashi Iwai2134ea42008-01-10 16:53:55 +01001202/*
1203 * set (static) TLV for virtual master volume; recalculated as max 0dB
1204 */
1205void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
1206 unsigned int *tlv)
1207{
1208 u32 caps;
1209 int nums, step;
1210
1211 caps = query_amp_caps(codec, nid, dir);
1212 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1213 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1214 step = (step + 1) * 25;
1215 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
1216 tlv[1] = 2 * sizeof(unsigned int);
1217 tlv[2] = -nums * step;
1218 tlv[3] = step;
1219}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001220EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001221
1222/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01001223static struct snd_kcontrol *
1224_snd_hda_find_mixer_ctl(struct hda_codec *codec,
1225 const char *name, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01001226{
1227 struct snd_ctl_elem_id id;
1228 memset(&id, 0, sizeof(id));
1229 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwai09f99702008-02-04 12:31:13 +01001230 id.index = idx;
Takashi Iwai2134ea42008-01-10 16:53:55 +01001231 strcpy(id.name, name);
1232 return snd_ctl_find_id(codec->bus->card, &id);
1233}
1234
Takashi Iwai09f99702008-02-04 12:31:13 +01001235struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
1236 const char *name)
1237{
1238 return _snd_hda_find_mixer_ctl(codec, name, 0);
1239}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001240EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01001241
Takashi Iwaid13bd412008-07-30 15:01:45 +02001242/* Add a control element and assign to the codec */
1243int snd_hda_ctl_add(struct hda_codec *codec, struct snd_kcontrol *kctl)
1244{
1245 int err;
1246 struct snd_kcontrol **knewp;
1247
1248 err = snd_ctl_add(codec->bus->card, kctl);
1249 if (err < 0)
1250 return err;
1251 knewp = snd_array_new(&codec->mixers);
1252 if (!knewp)
1253 return -ENOMEM;
1254 *knewp = kctl;
1255 return 0;
1256}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001257EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001258
Takashi Iwai529bd6c2008-11-27 14:17:01 +01001259#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwaid13bd412008-07-30 15:01:45 +02001260/* Clear all controls assigned to the given codec */
1261void snd_hda_ctls_clear(struct hda_codec *codec)
1262{
1263 int i;
1264 struct snd_kcontrol **kctls = codec->mixers.list;
1265 for (i = 0; i < codec->mixers.used; i++)
1266 snd_ctl_remove(codec->bus->card, kctls[i]);
1267 snd_array_free(&codec->mixers);
1268}
1269
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001270void snd_hda_codec_reset(struct hda_codec *codec)
1271{
1272 int i;
1273
1274#ifdef CONFIG_SND_HDA_POWER_SAVE
1275 cancel_delayed_work(&codec->power_work);
1276 flush_scheduled_work();
1277#endif
1278 snd_hda_ctls_clear(codec);
1279 /* relase PCMs */
1280 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01001281 if (codec->pcm_info[i].pcm) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001282 snd_device_free(codec->bus->card,
1283 codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01001284 clear_bit(codec->pcm_info[i].device,
1285 codec->bus->pcm_dev_bits);
1286 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001287 }
1288 if (codec->patch_ops.free)
1289 codec->patch_ops.free(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01001290 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001291 codec->spec = NULL;
1292 free_hda_cache(&codec->amp_cache);
1293 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01001294 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
1295 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001296 codec->num_pcms = 0;
1297 codec->pcm_info = NULL;
1298 codec->preset = NULL;
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001299 module_put(codec->owner);
1300 codec->owner = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001301}
Takashi Iwai529bd6c2008-11-27 14:17:01 +01001302#endif /* CONFIG_SND_HDA_RECONFIG */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001303
Takashi Iwai2134ea42008-01-10 16:53:55 +01001304/* create a virtual master control and add slaves */
1305int snd_hda_add_vmaster(struct hda_codec *codec, char *name,
1306 unsigned int *tlv, const char **slaves)
1307{
1308 struct snd_kcontrol *kctl;
1309 const char **s;
1310 int err;
1311
Takashi Iwai2f085542008-02-22 18:43:50 +01001312 for (s = slaves; *s && !snd_hda_find_mixer_ctl(codec, *s); s++)
1313 ;
1314 if (!*s) {
1315 snd_printdd("No slave found for %s\n", name);
1316 return 0;
1317 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01001318 kctl = snd_ctl_make_virtual_master(name, tlv);
1319 if (!kctl)
1320 return -ENOMEM;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001321 err = snd_hda_ctl_add(codec, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001322 if (err < 0)
1323 return err;
1324
1325 for (s = slaves; *s; s++) {
1326 struct snd_kcontrol *sctl;
1327
1328 sctl = snd_hda_find_mixer_ctl(codec, *s);
1329 if (!sctl) {
1330 snd_printdd("Cannot find slave %s, skipped\n", *s);
1331 continue;
1332 }
1333 err = snd_ctl_add_slave(kctl, sctl);
1334 if (err < 0)
1335 return err;
1336 }
1337 return 0;
1338}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001339EXPORT_SYMBOL_HDA(snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001340
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341/* switch */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001342int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
1343 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344{
1345 int chs = get_amp_channels(kcontrol);
1346
1347 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1348 uinfo->count = chs == 3 ? 2 : 1;
1349 uinfo->value.integer.min = 0;
1350 uinfo->value.integer.max = 1;
1351 return 0;
1352}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001353EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
Takashi Iwai0ba21762007-04-16 11:29:14 +02001355int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
1356 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357{
1358 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1359 hda_nid_t nid = get_amp_nid(kcontrol);
1360 int chs = get_amp_channels(kcontrol);
1361 int dir = get_amp_direction(kcontrol);
1362 int idx = get_amp_index(kcontrol);
1363 long *valp = ucontrol->value.integer.value;
1364
1365 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02001366 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02001367 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02001369 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02001370 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 return 0;
1372}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001373EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
Takashi Iwai0ba21762007-04-16 11:29:14 +02001375int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
1376 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377{
1378 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1379 hda_nid_t nid = get_amp_nid(kcontrol);
1380 int chs = get_amp_channels(kcontrol);
1381 int dir = get_amp_direction(kcontrol);
1382 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 long *valp = ucontrol->value.integer.value;
1384 int change = 0;
1385
Takashi Iwaicb53c622007-08-10 17:21:45 +02001386 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001387 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001388 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02001389 HDA_AMP_MUTE,
1390 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001391 valp++;
1392 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001393 if (chs & 2)
1394 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02001395 HDA_AMP_MUTE,
1396 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001397#ifdef CONFIG_SND_HDA_POWER_SAVE
1398 if (codec->patch_ops.check_power_status)
1399 codec->patch_ops.check_power_status(codec, nid);
1400#endif
1401 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 return change;
1403}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001404EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
1406/*
Takashi Iwai985be542005-11-02 18:26:49 +01001407 * bound volume controls
1408 *
1409 * bind multiple volumes (# indices, from 0)
1410 */
1411
1412#define AMP_VAL_IDX_SHIFT 19
1413#define AMP_VAL_IDX_MASK (0x0f<<19)
1414
Takashi Iwai0ba21762007-04-16 11:29:14 +02001415int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
1416 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01001417{
1418 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1419 unsigned long pval;
1420 int err;
1421
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001422 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01001423 pval = kcontrol->private_value;
1424 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
1425 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
1426 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001427 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01001428 return err;
1429}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001430EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01001431
Takashi Iwai0ba21762007-04-16 11:29:14 +02001432int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
1433 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01001434{
1435 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1436 unsigned long pval;
1437 int i, indices, err = 0, change = 0;
1438
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001439 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01001440 pval = kcontrol->private_value;
1441 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
1442 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001443 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
1444 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01001445 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1446 if (err < 0)
1447 break;
1448 change |= err;
1449 }
1450 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001451 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01001452 return err < 0 ? err : change;
1453}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001454EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01001455
1456/*
Takashi Iwai532d5382007-07-27 19:02:40 +02001457 * generic bound volume/swtich controls
1458 */
1459int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
1460 struct snd_ctl_elem_info *uinfo)
1461{
1462 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1463 struct hda_bind_ctls *c;
1464 int err;
1465
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001466 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01001467 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02001468 kcontrol->private_value = *c->values;
1469 err = c->ops->info(kcontrol, uinfo);
1470 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001471 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02001472 return err;
1473}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001474EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02001475
1476int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
1477 struct snd_ctl_elem_value *ucontrol)
1478{
1479 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1480 struct hda_bind_ctls *c;
1481 int err;
1482
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001483 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01001484 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02001485 kcontrol->private_value = *c->values;
1486 err = c->ops->get(kcontrol, ucontrol);
1487 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001488 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02001489 return err;
1490}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001491EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02001492
1493int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
1494 struct snd_ctl_elem_value *ucontrol)
1495{
1496 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1497 struct hda_bind_ctls *c;
1498 unsigned long *vals;
1499 int err = 0, change = 0;
1500
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001501 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01001502 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02001503 for (vals = c->values; *vals; vals++) {
1504 kcontrol->private_value = *vals;
1505 err = c->ops->put(kcontrol, ucontrol);
1506 if (err < 0)
1507 break;
1508 change |= err;
1509 }
1510 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001511 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02001512 return err < 0 ? err : change;
1513}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001514EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02001515
1516int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1517 unsigned int size, unsigned int __user *tlv)
1518{
1519 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1520 struct hda_bind_ctls *c;
1521 int err;
1522
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001523 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01001524 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02001525 kcontrol->private_value = *c->values;
1526 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
1527 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001528 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02001529 return err;
1530}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001531EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02001532
1533struct hda_ctl_ops snd_hda_bind_vol = {
1534 .info = snd_hda_mixer_amp_volume_info,
1535 .get = snd_hda_mixer_amp_volume_get,
1536 .put = snd_hda_mixer_amp_volume_put,
1537 .tlv = snd_hda_mixer_amp_tlv
1538};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001539EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02001540
1541struct hda_ctl_ops snd_hda_bind_sw = {
1542 .info = snd_hda_mixer_amp_switch_info,
1543 .get = snd_hda_mixer_amp_switch_get,
1544 .put = snd_hda_mixer_amp_switch_put,
1545 .tlv = snd_hda_mixer_amp_tlv
1546};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001547EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02001548
1549/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 * SPDIF out controls
1551 */
1552
Takashi Iwai0ba21762007-04-16 11:29:14 +02001553static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
1554 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555{
1556 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1557 uinfo->count = 1;
1558 return 0;
1559}
1560
Takashi Iwai0ba21762007-04-16 11:29:14 +02001561static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
1562 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563{
1564 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
1565 IEC958_AES0_NONAUDIO |
1566 IEC958_AES0_CON_EMPHASIS_5015 |
1567 IEC958_AES0_CON_NOT_COPYRIGHT;
1568 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
1569 IEC958_AES1_CON_ORIGINAL;
1570 return 0;
1571}
1572
Takashi Iwai0ba21762007-04-16 11:29:14 +02001573static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
1574 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575{
1576 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
1577 IEC958_AES0_NONAUDIO |
1578 IEC958_AES0_PRO_EMPHASIS_5015;
1579 return 0;
1580}
1581
Takashi Iwai0ba21762007-04-16 11:29:14 +02001582static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
1583 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584{
1585 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1586
1587 ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
1588 ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
1589 ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
1590 ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
1591
1592 return 0;
1593}
1594
1595/* convert from SPDIF status bits to HDA SPDIF bits
1596 * bit 0 (DigEn) is always set zero (to be filled later)
1597 */
1598static unsigned short convert_from_spdif_status(unsigned int sbits)
1599{
1600 unsigned short val = 0;
1601
1602 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02001603 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02001605 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001607 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
1608 IEC958_AES0_PRO_EMPHASIS_5015)
1609 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001611 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
1612 IEC958_AES0_CON_EMPHASIS_5015)
1613 val |= AC_DIG1_EMPHASIS;
1614 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
1615 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02001617 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
1619 }
1620 return val;
1621}
1622
1623/* convert to SPDIF status bits from HDA SPDIF bits
1624 */
1625static unsigned int convert_to_spdif_status(unsigned short val)
1626{
1627 unsigned int sbits = 0;
1628
Takashi Iwai0ba21762007-04-16 11:29:14 +02001629 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001631 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 sbits |= IEC958_AES0_PROFESSIONAL;
1633 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001634 if (sbits & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
1636 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001637 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001639 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001641 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
1643 sbits |= val & (0x7f << 8);
1644 }
1645 return sbits;
1646}
1647
Takashi Iwai2f728532008-09-25 16:32:41 +02001648/* set digital convert verbs both for the given NID and its slaves */
1649static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
1650 int verb, int val)
1651{
1652 hda_nid_t *d;
1653
Takashi Iwai9e976972008-11-25 08:17:20 +01001654 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02001655 d = codec->slave_dig_outs;
1656 if (!d)
1657 return;
1658 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01001659 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02001660}
1661
1662static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
1663 int dig1, int dig2)
1664{
1665 if (dig1 != -1)
1666 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
1667 if (dig2 != -1)
1668 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
1669}
1670
Takashi Iwai0ba21762007-04-16 11:29:14 +02001671static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
1672 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673{
1674 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1675 hda_nid_t nid = kcontrol->private_value;
1676 unsigned short val;
1677 int change;
1678
Ingo Molnar62932df2006-01-16 16:34:20 +01001679 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 codec->spdif_status = ucontrol->value.iec958.status[0] |
1681 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
1682 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
1683 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
1684 val = convert_from_spdif_status(codec->spdif_status);
1685 val |= codec->spdif_ctls & 1;
1686 change = codec->spdif_ctls != val;
1687 codec->spdif_ctls = val;
1688
Takashi Iwai2f728532008-09-25 16:32:41 +02001689 if (change)
1690 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
Ingo Molnar62932df2006-01-16 16:34:20 +01001692 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 return change;
1694}
1695
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001696#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
Takashi Iwai0ba21762007-04-16 11:29:14 +02001698static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
1699 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700{
1701 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1702
Takashi Iwai0ba21762007-04-16 11:29:14 +02001703 ucontrol->value.integer.value[0] = codec->spdif_ctls & AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 return 0;
1705}
1706
Takashi Iwai0ba21762007-04-16 11:29:14 +02001707static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
1708 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709{
1710 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1711 hda_nid_t nid = kcontrol->private_value;
1712 unsigned short val;
1713 int change;
1714
Ingo Molnar62932df2006-01-16 16:34:20 +01001715 mutex_lock(&codec->spdif_mutex);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001716 val = codec->spdif_ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02001718 val |= AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 change = codec->spdif_ctls != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001720 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 codec->spdif_ctls = val;
Takashi Iwai2f728532008-09-25 16:32:41 +02001722 set_dig_out_convert(codec, nid, val & 0xff, -1);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001723 /* unmute amp switch (if any) */
1724 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
Takashi Iwai47fd8302007-08-10 17:11:07 +02001725 (val & AC_DIG1_ENABLE))
1726 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
1727 HDA_AMP_MUTE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 }
Ingo Molnar62932df2006-01-16 16:34:20 +01001729 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 return change;
1731}
1732
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001733static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 {
1735 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1736 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1737 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
1738 .info = snd_hda_spdif_mask_info,
1739 .get = snd_hda_spdif_cmask_get,
1740 },
1741 {
1742 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1743 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1744 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
1745 .info = snd_hda_spdif_mask_info,
1746 .get = snd_hda_spdif_pmask_get,
1747 },
1748 {
1749 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1750 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1751 .info = snd_hda_spdif_mask_info,
1752 .get = snd_hda_spdif_default_get,
1753 .put = snd_hda_spdif_default_put,
1754 },
1755 {
1756 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1757 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
1758 .info = snd_hda_spdif_out_switch_info,
1759 .get = snd_hda_spdif_out_switch_get,
1760 .put = snd_hda_spdif_out_switch_put,
1761 },
1762 { } /* end */
1763};
1764
Takashi Iwai09f99702008-02-04 12:31:13 +01001765#define SPDIF_MAX_IDX 4 /* 4 instances should be enough to probe */
1766
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767/**
1768 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
1769 * @codec: the HDA codec
1770 * @nid: audio out widget NID
1771 *
1772 * Creates controls related with the SPDIF output.
1773 * Called from each patch supporting the SPDIF out.
1774 *
1775 * Returns 0 if successful, or a negative error code.
1776 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02001777int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778{
1779 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001780 struct snd_kcontrol *kctl;
1781 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01001782 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783
Takashi Iwai09f99702008-02-04 12:31:13 +01001784 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
1785 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Playback Switch",
1786 idx))
1787 break;
1788 }
1789 if (idx >= SPDIF_MAX_IDX) {
1790 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
1791 return -EBUSY;
1792 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
1794 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01001795 if (!kctl)
1796 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01001797 kctl->id.index = idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 kctl->private_value = nid;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001799 err = snd_hda_ctl_add(codec, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001800 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 return err;
1802 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02001803 codec->spdif_ctls =
Andrew Paprocki3982d172007-12-19 12:13:44 +01001804 snd_hda_codec_read(codec, nid, 0,
1805 AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
1807 return 0;
1808}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001809EXPORT_SYMBOL_HDA(snd_hda_create_spdif_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
1811/*
Takashi Iwai9a081602008-02-12 18:37:26 +01001812 * SPDIF sharing with analog output
1813 */
1814static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
1815 struct snd_ctl_elem_value *ucontrol)
1816{
1817 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
1818 ucontrol->value.integer.value[0] = mout->share_spdif;
1819 return 0;
1820}
1821
1822static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
1823 struct snd_ctl_elem_value *ucontrol)
1824{
1825 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
1826 mout->share_spdif = !!ucontrol->value.integer.value[0];
1827 return 0;
1828}
1829
1830static struct snd_kcontrol_new spdif_share_sw = {
1831 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1832 .name = "IEC958 Default PCM Playback Switch",
1833 .info = snd_ctl_boolean_mono_info,
1834 .get = spdif_share_sw_get,
1835 .put = spdif_share_sw_put,
1836};
1837
1838int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
1839 struct hda_multi_out *mout)
1840{
1841 if (!mout->dig_out_nid)
1842 return 0;
1843 /* ATTENTION: here mout is passed as private_data, instead of codec */
Takashi Iwaid13bd412008-07-30 15:01:45 +02001844 return snd_hda_ctl_add(codec,
Takashi Iwai9a081602008-02-12 18:37:26 +01001845 snd_ctl_new1(&spdif_share_sw, mout));
1846}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001847EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01001848
1849/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 * SPDIF input
1851 */
1852
1853#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
1854
Takashi Iwai0ba21762007-04-16 11:29:14 +02001855static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
1856 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857{
1858 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1859
1860 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
1861 return 0;
1862}
1863
Takashi Iwai0ba21762007-04-16 11:29:14 +02001864static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
1865 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866{
1867 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1868 hda_nid_t nid = kcontrol->private_value;
1869 unsigned int val = !!ucontrol->value.integer.value[0];
1870 int change;
1871
Ingo Molnar62932df2006-01-16 16:34:20 +01001872 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001874 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001876 snd_hda_codec_write_cache(codec, nid, 0,
1877 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 }
Ingo Molnar62932df2006-01-16 16:34:20 +01001879 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 return change;
1881}
1882
Takashi Iwai0ba21762007-04-16 11:29:14 +02001883static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
1884 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885{
1886 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1887 hda_nid_t nid = kcontrol->private_value;
1888 unsigned short val;
1889 unsigned int sbits;
1890
Andrew Paprocki3982d172007-12-19 12:13:44 +01001891 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 sbits = convert_to_spdif_status(val);
1893 ucontrol->value.iec958.status[0] = sbits;
1894 ucontrol->value.iec958.status[1] = sbits >> 8;
1895 ucontrol->value.iec958.status[2] = sbits >> 16;
1896 ucontrol->value.iec958.status[3] = sbits >> 24;
1897 return 0;
1898}
1899
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001900static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 {
1902 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1903 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH),
1904 .info = snd_hda_spdif_in_switch_info,
1905 .get = snd_hda_spdif_in_switch_get,
1906 .put = snd_hda_spdif_in_switch_put,
1907 },
1908 {
1909 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1910 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1911 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT),
1912 .info = snd_hda_spdif_mask_info,
1913 .get = snd_hda_spdif_in_status_get,
1914 },
1915 { } /* end */
1916};
1917
1918/**
1919 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
1920 * @codec: the HDA codec
1921 * @nid: audio in widget NID
1922 *
1923 * Creates controls related with the SPDIF input.
1924 * Called from each patch supporting the SPDIF in.
1925 *
1926 * Returns 0 if successful, or a negative error code.
1927 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02001928int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929{
1930 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001931 struct snd_kcontrol *kctl;
1932 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01001933 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
Takashi Iwai09f99702008-02-04 12:31:13 +01001935 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
1936 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Capture Switch",
1937 idx))
1938 break;
1939 }
1940 if (idx >= SPDIF_MAX_IDX) {
1941 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
1942 return -EBUSY;
1943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
1945 kctl = snd_ctl_new1(dig_mix, codec);
1946 kctl->private_value = nid;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001947 err = snd_hda_ctl_add(codec, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001948 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 return err;
1950 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02001951 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01001952 snd_hda_codec_read(codec, nid, 0,
1953 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02001954 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 return 0;
1956}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001957EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
Takashi Iwaicb53c622007-08-10 17:21:45 +02001959#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001960/*
1961 * command cache
1962 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001964/* build a 32bit cache key with the widget id and the command parameter */
1965#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
1966#define get_cmd_cache_nid(key) ((key) & 0xff)
1967#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
1968
1969/**
1970 * snd_hda_codec_write_cache - send a single command with caching
1971 * @codec: the HDA codec
1972 * @nid: NID to send the command
1973 * @direct: direct flag
1974 * @verb: the verb to send
1975 * @parm: the parameter for the verb
1976 *
1977 * Send a single command without waiting for response.
1978 *
1979 * Returns 0 if successful, or a negative error code.
1980 */
1981int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
1982 int direct, unsigned int verb, unsigned int parm)
1983{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01001984 struct hda_bus *bus = codec->bus;
1985 unsigned int res;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001986 int err;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01001987
1988 res = make_codec_cmd(codec, nid, direct, verb, parm);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001989 snd_hda_power_up(codec);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01001990 mutex_lock(&bus->cmd_mutex);
1991 err = bus->ops.command(bus, res);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001992 if (!err) {
1993 struct hda_cache_head *c;
1994 u32 key = build_cmd_cache_key(nid, verb);
1995 c = get_alloc_hash(&codec->cmd_cache, key);
1996 if (c)
1997 c->val = parm;
1998 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01001999 mutex_unlock(&bus->cmd_mutex);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002000 snd_hda_power_down(codec);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002001 return err;
2002}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002003EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002004
2005/* resume the all commands from the cache */
2006void snd_hda_codec_resume_cache(struct hda_codec *codec)
2007{
Takashi Iwai603c4012008-07-30 15:01:44 +02002008 struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002009 int i;
2010
Takashi Iwai603c4012008-07-30 15:01:44 +02002011 for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002012 u32 key = buffer->key;
2013 if (!key)
2014 continue;
2015 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
2016 get_cmd_cache_cmd(key), buffer->val);
2017 }
2018}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002019EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002020
2021/**
2022 * snd_hda_sequence_write_cache - sequence writes with caching
2023 * @codec: the HDA codec
2024 * @seq: VERB array to send
2025 *
2026 * Send the commands sequentially from the given array.
2027 * Thte commands are recorded on cache for power-save and resume.
2028 * The array must be terminated with NID=0.
2029 */
2030void snd_hda_sequence_write_cache(struct hda_codec *codec,
2031 const struct hda_verb *seq)
2032{
2033 for (; seq->nid; seq++)
2034 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
2035 seq->param);
2036}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002037EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002038#endif /* SND_HDA_NEEDS_RESUME */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002039
Takashi Iwai54d17402005-11-21 16:33:22 +01002040/*
2041 * set power state of the codec
2042 */
2043static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2044 unsigned int power_state)
2045{
Takashi Iwaicb53c622007-08-10 17:21:45 +02002046 hda_nid_t nid;
2047 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01002048
2049 snd_hda_codec_write(codec, fg, 0, AC_VERB_SET_POWER_STATE,
2050 power_state);
Marc Boucherd2595d82008-01-22 15:23:30 +01002051 msleep(10); /* partial workaround for "azx_get_response timeout" */
Takashi Iwai54d17402005-11-21 16:33:22 +01002052
Takashi Iwaicb53c622007-08-10 17:21:45 +02002053 nid = codec->start_nid;
2054 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01002055 unsigned int wcaps = get_wcaps(codec, nid);
2056 if (wcaps & AC_WCAP_POWER) {
2057 unsigned int wid_type = (wcaps & AC_WCAP_TYPE) >>
2058 AC_WCAP_TYPE_SHIFT;
2059 if (wid_type == AC_WID_PIN) {
2060 unsigned int pincap;
2061 /*
2062 * don't power down the widget if it controls
2063 * eapd and EAPD_BTLENABLE is set.
2064 */
2065 pincap = snd_hda_param_read(codec, nid,
2066 AC_PAR_PIN_CAP);
2067 if (pincap & AC_PINCAP_EAPD) {
2068 int eapd = snd_hda_codec_read(codec,
2069 nid, 0,
2070 AC_VERB_GET_EAPD_BTLENABLE, 0);
2071 eapd &= 0x02;
2072 if (power_state == AC_PWRST_D3 && eapd)
2073 continue;
2074 }
Takashi Iwai1194b5b2007-10-10 10:04:26 +02002075 }
Takashi Iwai54d17402005-11-21 16:33:22 +01002076 snd_hda_codec_write(codec, nid, 0,
2077 AC_VERB_SET_POWER_STATE,
2078 power_state);
Takashi Iwai1194b5b2007-10-10 10:04:26 +02002079 }
Takashi Iwai54d17402005-11-21 16:33:22 +01002080 }
2081
Takashi Iwaicb53c622007-08-10 17:21:45 +02002082 if (power_state == AC_PWRST_D0) {
2083 unsigned long end_time;
2084 int state;
Takashi Iwai54d17402005-11-21 16:33:22 +01002085 msleep(10);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002086 /* wait until the codec reachs to D0 */
2087 end_time = jiffies + msecs_to_jiffies(500);
2088 do {
2089 state = snd_hda_codec_read(codec, fg, 0,
2090 AC_VERB_GET_POWER_STATE, 0);
2091 if (state == power_state)
2092 break;
2093 msleep(1);
2094 } while (time_after_eq(end_time, jiffies));
2095 }
Takashi Iwai54d17402005-11-21 16:33:22 +01002096}
2097
Takashi Iwai11aeff02008-07-30 15:01:46 +02002098#ifdef CONFIG_SND_HDA_HWDEP
2099/* execute additional init verbs */
2100static void hda_exec_init_verbs(struct hda_codec *codec)
2101{
2102 if (codec->init_verbs.list)
2103 snd_hda_sequence_write(codec, codec->init_verbs.list);
2104}
2105#else
2106static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
2107#endif
2108
Takashi Iwaicb53c622007-08-10 17:21:45 +02002109#ifdef SND_HDA_NEEDS_RESUME
2110/*
2111 * call suspend and power-down; used both from PM and power-save
2112 */
2113static void hda_call_codec_suspend(struct hda_codec *codec)
2114{
2115 if (codec->patch_ops.suspend)
2116 codec->patch_ops.suspend(codec, PMSG_SUSPEND);
2117 hda_set_power_state(codec,
2118 codec->afg ? codec->afg : codec->mfg,
2119 AC_PWRST_D3);
2120#ifdef CONFIG_SND_HDA_POWER_SAVE
2121 cancel_delayed_work(&codec->power_work);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02002122 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02002123 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002124#endif
2125}
2126
2127/*
2128 * kick up codec; used both from PM and power-save
2129 */
2130static void hda_call_codec_resume(struct hda_codec *codec)
2131{
2132 hda_set_power_state(codec,
2133 codec->afg ? codec->afg : codec->mfg,
2134 AC_PWRST_D0);
Takashi Iwai11aeff02008-07-30 15:01:46 +02002135 hda_exec_init_verbs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002136 if (codec->patch_ops.resume)
2137 codec->patch_ops.resume(codec);
2138 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02002139 if (codec->patch_ops.init)
2140 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002141 snd_hda_codec_resume_amp(codec);
2142 snd_hda_codec_resume_cache(codec);
2143 }
2144}
2145#endif /* SND_HDA_NEEDS_RESUME */
2146
Takashi Iwai54d17402005-11-21 16:33:22 +01002147
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148/**
2149 * snd_hda_build_controls - build mixer controls
2150 * @bus: the BUS
2151 *
2152 * Creates mixer controls for each codec included in the bus.
2153 *
2154 * Returns 0 if successful, otherwise a negative error code.
2155 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002156int /*__devinit*/ snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157{
Takashi Iwai0ba21762007-04-16 11:29:14 +02002158 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159
Takashi Iwai0ba21762007-04-16 11:29:14 +02002160 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002161 int err = snd_hda_codec_build_controls(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 if (err < 0)
2163 return err;
2164 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002165 return 0;
2166}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002167EXPORT_SYMBOL_HDA(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002169int snd_hda_codec_build_controls(struct hda_codec *codec)
2170{
2171 int err = 0;
2172 /* fake as if already powered-on */
2173 hda_keep_power_on(codec);
2174 /* then fire up */
2175 hda_set_power_state(codec,
2176 codec->afg ? codec->afg : codec->mfg,
2177 AC_PWRST_D0);
Takashi Iwai11aeff02008-07-30 15:01:46 +02002178 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002179 /* continue to initialize... */
2180 if (codec->patch_ops.init)
2181 err = codec->patch_ops.init(codec);
2182 if (!err && codec->patch_ops.build_controls)
2183 err = codec->patch_ops.build_controls(codec);
2184 snd_hda_power_down(codec);
2185 if (err < 0)
2186 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 return 0;
2188}
2189
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190/*
2191 * stream formats
2192 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02002193struct hda_rate_tbl {
2194 unsigned int hz;
2195 unsigned int alsa_bits;
2196 unsigned int hda_fmt;
2197};
2198
2199static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02002201
2202 /* autodetected value used in snd_hda_query_supported_pcm */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 { 8000, SNDRV_PCM_RATE_8000, 0x0500 }, /* 1/6 x 48 */
2204 { 11025, SNDRV_PCM_RATE_11025, 0x4300 }, /* 1/4 x 44 */
2205 { 16000, SNDRV_PCM_RATE_16000, 0x0200 }, /* 1/3 x 48 */
2206 { 22050, SNDRV_PCM_RATE_22050, 0x4100 }, /* 1/2 x 44 */
2207 { 32000, SNDRV_PCM_RATE_32000, 0x0a00 }, /* 2/3 x 48 */
2208 { 44100, SNDRV_PCM_RATE_44100, 0x4000 }, /* 44 */
2209 { 48000, SNDRV_PCM_RATE_48000, 0x0000 }, /* 48 */
2210 { 88200, SNDRV_PCM_RATE_88200, 0x4800 }, /* 2 x 44 */
2211 { 96000, SNDRV_PCM_RATE_96000, 0x0800 }, /* 2 x 48 */
2212 { 176400, SNDRV_PCM_RATE_176400, 0x5800 },/* 4 x 44 */
2213 { 192000, SNDRV_PCM_RATE_192000, 0x1800 }, /* 4 x 48 */
Takashi Iwaia961f9f2007-04-12 13:08:09 +02002214#define AC_PAR_PCM_RATE_BITS 11
2215 /* up to bits 10, 384kHZ isn't supported properly */
2216
2217 /* not autodetected value */
2218 { 9600, SNDRV_PCM_RATE_KNOT, 0x0400 }, /* 1/5 x 48 */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02002219
Takashi Iwaibefdf312005-08-22 13:57:55 +02002220 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221};
2222
2223/**
2224 * snd_hda_calc_stream_format - calculate format bitset
2225 * @rate: the sample rate
2226 * @channels: the number of channels
2227 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
2228 * @maxbps: the max. bps
2229 *
2230 * Calculate the format bitset from the given rate, channels and th PCM format.
2231 *
2232 * Return zero if invalid.
2233 */
2234unsigned int snd_hda_calc_stream_format(unsigned int rate,
2235 unsigned int channels,
2236 unsigned int format,
2237 unsigned int maxbps)
2238{
2239 int i;
2240 unsigned int val = 0;
2241
Takashi Iwaibefdf312005-08-22 13:57:55 +02002242 for (i = 0; rate_bits[i].hz; i++)
2243 if (rate_bits[i].hz == rate) {
2244 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 break;
2246 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002247 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 snd_printdd("invalid rate %d\n", rate);
2249 return 0;
2250 }
2251
2252 if (channels == 0 || channels > 8) {
2253 snd_printdd("invalid channels %d\n", channels);
2254 return 0;
2255 }
2256 val |= channels - 1;
2257
2258 switch (snd_pcm_format_width(format)) {
2259 case 8: val |= 0x00; break;
2260 case 16: val |= 0x10; break;
2261 case 20:
2262 case 24:
2263 case 32:
2264 if (maxbps >= 32)
2265 val |= 0x40;
2266 else if (maxbps >= 24)
2267 val |= 0x30;
2268 else
2269 val |= 0x20;
2270 break;
2271 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002272 snd_printdd("invalid format width %d\n",
2273 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 return 0;
2275 }
2276
2277 return val;
2278}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002279EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280
2281/**
2282 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
2283 * @codec: the HDA codec
2284 * @nid: NID to query
2285 * @ratesp: the pointer to store the detected rate bitflags
2286 * @formatsp: the pointer to store the detected formats
2287 * @bpsp: the pointer to store the detected format widths
2288 *
2289 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
2290 * or @bsps argument is ignored.
2291 *
2292 * Returns 0 if successful, otherwise a negative error code.
2293 */
Takashi Iwai986862bd2008-11-27 12:40:13 +01002294static int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
2296{
2297 int i;
2298 unsigned int val, streams;
2299
2300 val = 0;
2301 if (nid != codec->afg &&
Takashi Iwai54d17402005-11-21 16:33:22 +01002302 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
2304 if (val == -1)
2305 return -EIO;
2306 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002307 if (!val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
2309
2310 if (ratesp) {
2311 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02002312 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02002314 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 }
2316 *ratesp = rates;
2317 }
2318
2319 if (formatsp || bpsp) {
2320 u64 formats = 0;
2321 unsigned int bps;
2322 unsigned int wcaps;
2323
Takashi Iwai54d17402005-11-21 16:33:22 +01002324 wcaps = get_wcaps(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
2326 if (streams == -1)
2327 return -EIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002328 if (!streams) {
2329 streams = snd_hda_param_read(codec, codec->afg,
2330 AC_PAR_STREAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 if (streams == -1)
2332 return -EIO;
2333 }
2334
2335 bps = 0;
2336 if (streams & AC_SUPFMT_PCM) {
2337 if (val & AC_SUPPCM_BITS_8) {
2338 formats |= SNDRV_PCM_FMTBIT_U8;
2339 bps = 8;
2340 }
2341 if (val & AC_SUPPCM_BITS_16) {
2342 formats |= SNDRV_PCM_FMTBIT_S16_LE;
2343 bps = 16;
2344 }
2345 if (wcaps & AC_WCAP_DIGITAL) {
2346 if (val & AC_SUPPCM_BITS_32)
2347 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
2348 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
2349 formats |= SNDRV_PCM_FMTBIT_S32_LE;
2350 if (val & AC_SUPPCM_BITS_24)
2351 bps = 24;
2352 else if (val & AC_SUPPCM_BITS_20)
2353 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002354 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
2355 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 formats |= SNDRV_PCM_FMTBIT_S32_LE;
2357 if (val & AC_SUPPCM_BITS_32)
2358 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 else if (val & AC_SUPPCM_BITS_24)
2360 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02002361 else if (val & AC_SUPPCM_BITS_20)
2362 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 }
2364 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002365 else if (streams == AC_SUPFMT_FLOAT32) {
2366 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
2368 bps = 32;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002369 } else if (streams == AC_SUPFMT_AC3) {
2370 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 /* temporary hack: we have still no proper support
2372 * for the direct AC3 stream...
2373 */
2374 formats |= SNDRV_PCM_FMTBIT_U8;
2375 bps = 8;
2376 }
2377 if (formatsp)
2378 *formatsp = formats;
2379 if (bpsp)
2380 *bpsp = bps;
2381 }
2382
2383 return 0;
2384}
2385
2386/**
Takashi Iwai0ba21762007-04-16 11:29:14 +02002387 * snd_hda_is_supported_format - check whether the given node supports
2388 * the format val
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 *
2390 * Returns 1 if supported, 0 if not.
2391 */
2392int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
2393 unsigned int format)
2394{
2395 int i;
2396 unsigned int val = 0, rate, stream;
2397
2398 if (nid != codec->afg &&
Takashi Iwai54d17402005-11-21 16:33:22 +01002399 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
2401 if (val == -1)
2402 return 0;
2403 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002404 if (!val) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
2406 if (val == -1)
2407 return 0;
2408 }
2409
2410 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02002411 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02002412 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 if (val & (1 << i))
2414 break;
2415 return 0;
2416 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02002417 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 return 0;
2419
2420 stream = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
2421 if (stream == -1)
2422 return 0;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002423 if (!stream && nid != codec->afg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 stream = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002425 if (!stream || stream == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 return 0;
2427
2428 if (stream & AC_SUPFMT_PCM) {
2429 switch (format & 0xf0) {
2430 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002431 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 return 0;
2433 break;
2434 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002435 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 return 0;
2437 break;
2438 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002439 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 return 0;
2441 break;
2442 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002443 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 return 0;
2445 break;
2446 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002447 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 return 0;
2449 break;
2450 default:
2451 return 0;
2452 }
2453 } else {
2454 /* FIXME: check for float32 and AC3? */
2455 }
2456
2457 return 1;
2458}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002459EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460
2461/*
2462 * PCM stuff
2463 */
2464static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
2465 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002466 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467{
2468 return 0;
2469}
2470
2471static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
2472 struct hda_codec *codec,
2473 unsigned int stream_tag,
2474 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002475 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476{
2477 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
2478 return 0;
2479}
2480
2481static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
2482 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002483 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484{
Takashi Iwai888afa12008-03-18 09:57:50 +01002485 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 return 0;
2487}
2488
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002489static int set_pcm_default_values(struct hda_codec *codec,
2490 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491{
Takashi Iwai0ba21762007-04-16 11:29:14 +02002492 /* query support PCM information from the given NID */
2493 if (info->nid && (!info->rates || !info->formats)) {
2494 snd_hda_query_supported_pcm(codec, info->nid,
2495 info->rates ? NULL : &info->rates,
2496 info->formats ? NULL : &info->formats,
2497 info->maxbps ? NULL : &info->maxbps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 }
2499 if (info->ops.open == NULL)
2500 info->ops.open = hda_pcm_default_open_close;
2501 if (info->ops.close == NULL)
2502 info->ops.close = hda_pcm_default_open_close;
2503 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02002504 if (snd_BUG_ON(!info->nid))
2505 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 info->ops.prepare = hda_pcm_default_prepare;
2507 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02002509 if (snd_BUG_ON(!info->nid))
2510 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 info->ops.cleanup = hda_pcm_default_cleanup;
2512 }
2513 return 0;
2514}
2515
Takashi Iwai176d5332008-07-30 15:01:44 +02002516/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002517 * get the empty PCM device number to assign
2518 */
2519static int get_empty_pcm_device(struct hda_bus *bus, int type)
2520{
2521 static const char *dev_name[HDA_PCM_NTYPES] = {
2522 "Audio", "SPDIF", "HDMI", "Modem"
2523 };
2524 /* starting device index for each PCM type */
2525 static int dev_idx[HDA_PCM_NTYPES] = {
2526 [HDA_PCM_TYPE_AUDIO] = 0,
2527 [HDA_PCM_TYPE_SPDIF] = 1,
2528 [HDA_PCM_TYPE_HDMI] = 3,
2529 [HDA_PCM_TYPE_MODEM] = 6
2530 };
2531 /* normal audio device indices; not linear to keep compatibility */
2532 static int audio_idx[4] = { 0, 2, 4, 5 };
2533 int i, dev;
2534
2535 switch (type) {
2536 case HDA_PCM_TYPE_AUDIO:
2537 for (i = 0; i < ARRAY_SIZE(audio_idx); i++) {
2538 dev = audio_idx[i];
2539 if (!test_bit(dev, bus->pcm_dev_bits))
2540 break;
2541 }
2542 if (i >= ARRAY_SIZE(audio_idx)) {
2543 snd_printk(KERN_WARNING "Too many audio devices\n");
2544 return -EAGAIN;
2545 }
2546 break;
2547 case HDA_PCM_TYPE_SPDIF:
2548 case HDA_PCM_TYPE_HDMI:
2549 case HDA_PCM_TYPE_MODEM:
2550 dev = dev_idx[type];
2551 if (test_bit(dev, bus->pcm_dev_bits)) {
2552 snd_printk(KERN_WARNING "%s already defined\n",
2553 dev_name[type]);
2554 return -EAGAIN;
2555 }
2556 break;
2557 default:
2558 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
2559 return -EINVAL;
2560 }
2561 set_bit(dev, bus->pcm_dev_bits);
2562 return dev;
2563}
2564
2565/*
Takashi Iwai176d5332008-07-30 15:01:44 +02002566 * attach a new PCM stream
2567 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002568static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02002569{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01002570 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02002571 struct hda_pcm_stream *info;
2572 int stream, err;
2573
Takashi Iwaib91f0802008-11-04 08:43:08 +01002574 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02002575 return -EINVAL;
2576 for (stream = 0; stream < 2; stream++) {
2577 info = &pcm->stream[stream];
2578 if (info->substreams) {
2579 err = set_pcm_default_values(codec, info);
2580 if (err < 0)
2581 return err;
2582 }
2583 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01002584 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02002585}
2586
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002587/* assign all PCMs of the given codec */
2588int snd_hda_codec_build_pcms(struct hda_codec *codec)
2589{
2590 unsigned int pcm;
2591 int err;
2592
2593 if (!codec->num_pcms) {
2594 if (!codec->patch_ops.build_pcms)
2595 return 0;
2596 err = codec->patch_ops.build_pcms(codec);
2597 if (err < 0)
2598 return err;
2599 }
2600 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2601 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2602 int dev;
2603
2604 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
2605 return 0; /* no substreams assigned */
2606
2607 if (!cpcm->pcm) {
2608 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
2609 if (dev < 0)
2610 return 0;
2611 cpcm->device = dev;
2612 err = snd_hda_attach_pcm(codec, cpcm);
2613 if (err < 0)
2614 return err;
2615 }
2616 }
2617 return 0;
2618}
2619
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620/**
2621 * snd_hda_build_pcms - build PCM information
2622 * @bus: the BUS
2623 *
2624 * Create PCM information for each codec included in the bus.
2625 *
2626 * The build_pcms codec patch is requested to set up codec->num_pcms and
2627 * codec->pcm_info properly. The array is referred by the top-level driver
2628 * to create its PCM instances.
2629 * The allocated codec->pcm_info should be released in codec->patch_ops.free
2630 * callback.
2631 *
2632 * At least, substreams, channels_min and channels_max must be filled for
2633 * each stream. substreams = 0 indicates that the stream doesn't exist.
2634 * When rates and/or formats are zero, the supported values are queried
2635 * from the given nid. The nid is used also by the default ops.prepare
2636 * and ops.cleanup callbacks.
2637 *
2638 * The driver needs to call ops.open in its open callback. Similarly,
2639 * ops.close is supposed to be called in the close callback.
2640 * ops.prepare should be called in the prepare or hw_params callback
2641 * with the proper parameters for set up.
2642 * ops.cleanup should be called in hw_free for clean up of streams.
2643 *
2644 * This function returns 0 if successfull, or a negative error code.
2645 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002646int __devinit snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647{
Takashi Iwai0ba21762007-04-16 11:29:14 +02002648 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649
Takashi Iwai0ba21762007-04-16 11:29:14 +02002650 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002651 int err = snd_hda_codec_build_pcms(codec);
2652 if (err < 0)
2653 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 }
2655 return 0;
2656}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002657EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659/**
2660 * snd_hda_check_board_config - compare the current codec with the config table
2661 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002662 * @num_configs: number of config enums
2663 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 * @tbl: configuration table, terminated by null entries
2665 *
2666 * Compares the modelname or PCI subsystem id of the current codec with the
2667 * given configuration table. If a matching entry is found, returns its
2668 * config value (supposed to be 0 or positive).
2669 *
2670 * If no entries are matching, the function returns a negative value.
2671 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002672int snd_hda_check_board_config(struct hda_codec *codec,
2673 int num_configs, const char **models,
2674 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675{
Takashi Iwaif44ac832008-07-30 15:01:45 +02002676 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002677 int i;
2678 for (i = 0; i < num_configs; i++) {
2679 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02002680 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002681 snd_printd(KERN_INFO "hda_codec: model '%s' is "
2682 "selected\n", models[i]);
2683 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 }
2685 }
2686 }
2687
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002688 if (!codec->bus->pci || !tbl)
2689 return -1;
2690
2691 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
2692 if (!tbl)
2693 return -1;
2694 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02002695#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002696 char tmp[10];
2697 const char *model = NULL;
2698 if (models)
2699 model = models[tbl->value];
2700 if (!model) {
2701 sprintf(tmp, "#%d", tbl->value);
2702 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002704 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
2705 "for config %x:%x (%s)\n",
2706 model, tbl->subvendor, tbl->subdevice,
2707 (tbl->name ? tbl->name : "Unknown device"));
2708#endif
2709 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 }
2711 return -1;
2712}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002713EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714
2715/**
2716 * snd_hda_add_new_ctls - create controls from the array
2717 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002718 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 *
2720 * This helper function creates and add new controls in the given array.
2721 * The array must be terminated with an empty entry as terminator.
2722 *
2723 * Returns 0 if successful, or a negative error code.
2724 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002725int snd_hda_add_new_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726{
Takashi Iwaicb53c622007-08-10 17:21:45 +02002727 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728
2729 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01002730 struct snd_kcontrol *kctl;
2731 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002732 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01002733 return -ENOMEM;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002734 err = snd_hda_ctl_add(codec, kctl);
Takashi Iwai54d17402005-11-21 16:33:22 +01002735 if (err < 0) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002736 if (!codec->addr)
Takashi Iwai54d17402005-11-21 16:33:22 +01002737 return err;
2738 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002739 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01002740 return -ENOMEM;
2741 kctl->id.device = codec->addr;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002742 err = snd_hda_ctl_add(codec, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002743 if (err < 0)
Takashi Iwai54d17402005-11-21 16:33:22 +01002744 return err;
2745 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 }
2747 return 0;
2748}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002749EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750
Takashi Iwaicb53c622007-08-10 17:21:45 +02002751#ifdef CONFIG_SND_HDA_POWER_SAVE
2752static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2753 unsigned int power_state);
2754
2755static void hda_power_work(struct work_struct *work)
2756{
2757 struct hda_codec *codec =
2758 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01002759 struct hda_bus *bus = codec->bus;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002760
Maxim Levitsky2e492462007-09-03 15:26:57 +02002761 if (!codec->power_on || codec->power_count) {
2762 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002763 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02002764 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02002765
2766 hda_call_codec_suspend(codec);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01002767 if (bus->ops.pm_notify)
2768 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002769}
2770
2771static void hda_keep_power_on(struct hda_codec *codec)
2772{
2773 codec->power_count++;
2774 codec->power_on = 1;
2775}
2776
2777void snd_hda_power_up(struct hda_codec *codec)
2778{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01002779 struct hda_bus *bus = codec->bus;
2780
Takashi Iwaicb53c622007-08-10 17:21:45 +02002781 codec->power_count++;
Takashi Iwaia221e282007-08-16 16:35:33 +02002782 if (codec->power_on || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02002783 return;
2784
2785 codec->power_on = 1;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01002786 if (bus->ops.pm_notify)
2787 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002788 hda_call_codec_resume(codec);
2789 cancel_delayed_work(&codec->power_work);
Takashi Iwaia221e282007-08-16 16:35:33 +02002790 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002791}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002792EXPORT_SYMBOL_HDA(snd_hda_power_up);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002793
2794#define power_save(codec) \
2795 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02002796
Takashi Iwaifee2fba2008-11-27 12:43:28 +01002797#define power_save(codec) \
2798 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
2799
Takashi Iwaicb53c622007-08-10 17:21:45 +02002800void snd_hda_power_down(struct hda_codec *codec)
2801{
2802 --codec->power_count;
Takashi Iwaia221e282007-08-16 16:35:33 +02002803 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02002804 return;
Takashi Iwaifee2fba2008-11-27 12:43:28 +01002805 if (power_save(codec)) {
Takashi Iwaia221e282007-08-16 16:35:33 +02002806 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002807 schedule_delayed_work(&codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01002808 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02002809 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02002810}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002811EXPORT_SYMBOL_HDA(snd_hda_power_down);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002812
2813int snd_hda_check_amp_list_power(struct hda_codec *codec,
2814 struct hda_loopback_check *check,
2815 hda_nid_t nid)
2816{
2817 struct hda_amp_list *p;
2818 int ch, v;
2819
2820 if (!check->amplist)
2821 return 0;
2822 for (p = check->amplist; p->nid; p++) {
2823 if (p->nid == nid)
2824 break;
2825 }
2826 if (!p->nid)
2827 return 0; /* nothing changed */
2828
2829 for (p = check->amplist; p->nid; p++) {
2830 for (ch = 0; ch < 2; ch++) {
2831 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
2832 p->idx);
2833 if (!(v & HDA_AMP_MUTE) && v > 0) {
2834 if (!check->power_on) {
2835 check->power_on = 1;
2836 snd_hda_power_up(codec);
2837 }
2838 return 1;
2839 }
2840 }
2841 }
2842 if (check->power_on) {
2843 check->power_on = 0;
2844 snd_hda_power_down(codec);
2845 }
2846 return 0;
2847}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002848EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002849#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002851/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002852 * Channel mode helper
2853 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002854int snd_hda_ch_mode_info(struct hda_codec *codec,
2855 struct snd_ctl_elem_info *uinfo,
2856 const struct hda_channel_mode *chmode,
2857 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002858{
2859 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2860 uinfo->count = 1;
2861 uinfo->value.enumerated.items = num_chmodes;
2862 if (uinfo->value.enumerated.item >= num_chmodes)
2863 uinfo->value.enumerated.item = num_chmodes - 1;
2864 sprintf(uinfo->value.enumerated.name, "%dch",
2865 chmode[uinfo->value.enumerated.item].channels);
2866 return 0;
2867}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002868EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002869
Takashi Iwai0ba21762007-04-16 11:29:14 +02002870int snd_hda_ch_mode_get(struct hda_codec *codec,
2871 struct snd_ctl_elem_value *ucontrol,
2872 const struct hda_channel_mode *chmode,
2873 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002874 int max_channels)
2875{
2876 int i;
2877
2878 for (i = 0; i < num_chmodes; i++) {
2879 if (max_channels == chmode[i].channels) {
2880 ucontrol->value.enumerated.item[0] = i;
2881 break;
2882 }
2883 }
2884 return 0;
2885}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002886EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002887
Takashi Iwai0ba21762007-04-16 11:29:14 +02002888int snd_hda_ch_mode_put(struct hda_codec *codec,
2889 struct snd_ctl_elem_value *ucontrol,
2890 const struct hda_channel_mode *chmode,
2891 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002892 int *max_channelsp)
2893{
2894 unsigned int mode;
2895
2896 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01002897 if (mode >= num_chmodes)
2898 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002899 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002900 return 0;
2901 /* change the current channel setting */
2902 *max_channelsp = chmode[mode].channels;
2903 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002904 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002905 return 1;
2906}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002907EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002908
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909/*
2910 * input MUX helper
2911 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002912int snd_hda_input_mux_info(const struct hda_input_mux *imux,
2913 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914{
2915 unsigned int index;
2916
2917 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2918 uinfo->count = 1;
2919 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02002920 if (!imux->num_items)
2921 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 index = uinfo->value.enumerated.item;
2923 if (index >= imux->num_items)
2924 index = imux->num_items - 1;
2925 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
2926 return 0;
2927}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002928EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929
Takashi Iwai0ba21762007-04-16 11:29:14 +02002930int snd_hda_input_mux_put(struct hda_codec *codec,
2931 const struct hda_input_mux *imux,
2932 struct snd_ctl_elem_value *ucontrol,
2933 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 unsigned int *cur_val)
2935{
2936 unsigned int idx;
2937
Takashi Iwai5513b0c2007-10-09 11:58:41 +02002938 if (!imux->num_items)
2939 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 idx = ucontrol->value.enumerated.item[0];
2941 if (idx >= imux->num_items)
2942 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002943 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002945 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
2946 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 *cur_val = idx;
2948 return 1;
2949}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002950EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951
2952
2953/*
2954 * Multi-channel / digital-out PCM helper functions
2955 */
2956
Takashi Iwai6b97eb42007-04-05 14:51:48 +02002957/* setup SPDIF output stream */
2958static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
2959 unsigned int stream_tag, unsigned int format)
2960{
2961 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Takashi Iwai2f728532008-09-25 16:32:41 +02002962 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
2963 set_dig_out_convert(codec, nid,
2964 codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff,
2965 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02002966 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02002967 if (codec->slave_dig_outs) {
2968 hda_nid_t *d;
2969 for (d = codec->slave_dig_outs; *d; d++)
2970 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
2971 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04002972 }
Takashi Iwai2f728532008-09-25 16:32:41 +02002973 /* turn on again (if needed) */
2974 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
2975 set_dig_out_convert(codec, nid,
2976 codec->spdif_ctls & 0xff, -1);
2977}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04002978
Takashi Iwai2f728532008-09-25 16:32:41 +02002979static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
2980{
2981 snd_hda_codec_cleanup_stream(codec, nid);
2982 if (codec->slave_dig_outs) {
2983 hda_nid_t *d;
2984 for (d = codec->slave_dig_outs; *d; d++)
2985 snd_hda_codec_cleanup_stream(codec, *d);
2986 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02002987}
2988
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989/*
2990 * open the digital out in the exclusive mode
2991 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002992int snd_hda_multi_out_dig_open(struct hda_codec *codec,
2993 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994{
Ingo Molnar62932df2006-01-16 16:34:20 +01002995 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02002996 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
2997 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02002998 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01003000 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 return 0;
3002}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003003EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003005int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
3006 struct hda_multi_out *mout,
3007 unsigned int stream_tag,
3008 unsigned int format,
3009 struct snd_pcm_substream *substream)
3010{
3011 mutex_lock(&codec->spdif_mutex);
3012 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
3013 mutex_unlock(&codec->spdif_mutex);
3014 return 0;
3015}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003016EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003017
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018/*
3019 * release the digital out
3020 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003021int snd_hda_multi_out_dig_close(struct hda_codec *codec,
3022 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023{
Ingo Molnar62932df2006-01-16 16:34:20 +01003024 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01003026 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 return 0;
3028}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003029EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030
3031/*
3032 * set up more restrictions for analog out
3033 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003034int snd_hda_multi_out_analog_open(struct hda_codec *codec,
3035 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01003036 struct snd_pcm_substream *substream,
3037 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038{
Takashi Iwai9a081602008-02-12 18:37:26 +01003039 struct snd_pcm_runtime *runtime = substream->runtime;
3040 runtime->hw.channels_max = mout->max_channels;
3041 if (mout->dig_out_nid) {
3042 if (!mout->analog_rates) {
3043 mout->analog_rates = hinfo->rates;
3044 mout->analog_formats = hinfo->formats;
3045 mout->analog_maxbps = hinfo->maxbps;
3046 } else {
3047 runtime->hw.rates = mout->analog_rates;
3048 runtime->hw.formats = mout->analog_formats;
3049 hinfo->maxbps = mout->analog_maxbps;
3050 }
3051 if (!mout->spdif_rates) {
3052 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
3053 &mout->spdif_rates,
3054 &mout->spdif_formats,
3055 &mout->spdif_maxbps);
3056 }
3057 mutex_lock(&codec->spdif_mutex);
3058 if (mout->share_spdif) {
3059 runtime->hw.rates &= mout->spdif_rates;
3060 runtime->hw.formats &= mout->spdif_formats;
3061 if (mout->spdif_maxbps < hinfo->maxbps)
3062 hinfo->maxbps = mout->spdif_maxbps;
3063 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02003064 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01003065 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 return snd_pcm_hw_constraint_step(substream->runtime, 0,
3067 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
3068}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003069EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070
3071/*
3072 * set up the i/o for analog out
3073 * when the digital out is available, copy the front out to digital out, too.
3074 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003075int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
3076 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 unsigned int stream_tag,
3078 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003079 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080{
3081 hda_nid_t *nids = mout->dac_nids;
3082 int chs = substream->runtime->channels;
3083 int i;
3084
Ingo Molnar62932df2006-01-16 16:34:20 +01003085 mutex_lock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01003086 if (mout->dig_out_nid && mout->share_spdif &&
3087 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02003089 snd_hda_is_supported_format(codec, mout->dig_out_nid,
3090 format) &&
3091 !(codec->spdif_status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003093 setup_dig_out_stream(codec, mout->dig_out_nid,
3094 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 } else {
3096 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02003097 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 }
3099 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003100 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101
3102 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003103 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
3104 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02003105 if (!mout->no_share_stream &&
3106 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003108 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
3109 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01003110 /* extra outputs copied from front */
3111 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02003112 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01003113 snd_hda_codec_setup_stream(codec,
3114 mout->extra_out_nid[i],
3115 stream_tag, 0, format);
3116
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 /* surrounds */
3118 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02003119 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003120 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
3121 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02003122 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003123 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
3124 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 }
3126 return 0;
3127}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003128EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129
3130/*
3131 * clean up the setting for analog out
3132 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003133int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
3134 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135{
3136 hda_nid_t *nids = mout->dac_nids;
3137 int i;
3138
3139 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01003140 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01003142 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwai82bc9552006-03-21 11:24:42 +01003143 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
3144 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01003145 snd_hda_codec_cleanup_stream(codec,
3146 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01003147 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02003149 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 mout->dig_out_used = 0;
3151 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003152 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 return 0;
3154}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003155EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003157/*
Wu Fengguang6b345002008-10-07 14:21:41 +08003158 * Helper for automatic pin configuration
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003159 */
Kailang Yangdf694da2005-12-05 19:42:22 +01003160
Takashi Iwai12f288b2007-08-02 15:51:59 +02003161static int is_in_nid_list(hda_nid_t nid, hda_nid_t *list)
Kailang Yangdf694da2005-12-05 19:42:22 +01003162{
3163 for (; *list; list++)
3164 if (*list == nid)
3165 return 1;
3166 return 0;
3167}
3168
Steve Longerbeam81937d32007-05-08 15:33:03 +02003169
3170/*
3171 * Sort an associated group of pins according to their sequence numbers.
3172 */
3173static void sort_pins_by_sequence(hda_nid_t * pins, short * sequences,
3174 int num_pins)
3175{
3176 int i, j;
3177 short seq;
3178 hda_nid_t nid;
3179
3180 for (i = 0; i < num_pins; i++) {
3181 for (j = i + 1; j < num_pins; j++) {
3182 if (sequences[i] > sequences[j]) {
3183 seq = sequences[i];
3184 sequences[i] = sequences[j];
3185 sequences[j] = seq;
3186 nid = pins[i];
3187 pins[i] = pins[j];
3188 pins[j] = nid;
3189 }
3190 }
3191 }
3192}
3193
3194
Takashi Iwai82bc9552006-03-21 11:24:42 +01003195/*
3196 * Parse all pin widgets and store the useful pin nids to cfg
3197 *
3198 * The number of line-outs or any primary output is stored in line_outs,
3199 * and the corresponding output pins are assigned to line_out_pins[],
3200 * in the order of front, rear, CLFE, side, ...
3201 *
3202 * If more extra outputs (speaker and headphone) are found, the pins are
Takashi Iwaieb06ed82006-09-20 17:10:27 +02003203 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
Takashi Iwai82bc9552006-03-21 11:24:42 +01003204 * is detected, one of speaker of HP pins is assigned as the primary
3205 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
3206 * if any analog output exists.
3207 *
3208 * The analog input pins are assigned to input_pins array.
3209 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
3210 * respectively.
3211 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003212int snd_hda_parse_pin_def_config(struct hda_codec *codec,
3213 struct auto_pin_cfg *cfg,
3214 hda_nid_t *ignore_nids)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003215{
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01003216 hda_nid_t nid, end_nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02003217 short seq, assoc_line_out, assoc_speaker;
3218 short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
3219 short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
Takashi Iwaif889fa92007-10-31 15:49:32 +01003220 short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003221
3222 memset(cfg, 0, sizeof(*cfg));
3223
Steve Longerbeam81937d32007-05-08 15:33:03 +02003224 memset(sequences_line_out, 0, sizeof(sequences_line_out));
3225 memset(sequences_speaker, 0, sizeof(sequences_speaker));
Takashi Iwaif889fa92007-10-31 15:49:32 +01003226 memset(sequences_hp, 0, sizeof(sequences_hp));
Steve Longerbeam81937d32007-05-08 15:33:03 +02003227 assoc_line_out = assoc_speaker = 0;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003228
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01003229 end_nid = codec->start_nid + codec->num_nodes;
3230 for (nid = codec->start_nid; nid < end_nid; nid++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01003231 unsigned int wid_caps = get_wcaps(codec, nid);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003232 unsigned int wid_type =
3233 (wid_caps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003234 unsigned int def_conf;
3235 short assoc, loc;
3236
3237 /* read all default configuration for pin complex */
3238 if (wid_type != AC_WID_PIN)
3239 continue;
Kailang Yangdf694da2005-12-05 19:42:22 +01003240 /* ignore the given nids (e.g. pc-beep returns error) */
3241 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
3242 continue;
3243
Takashi Iwai0ba21762007-04-16 11:29:14 +02003244 def_conf = snd_hda_codec_read(codec, nid, 0,
3245 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003246 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
3247 continue;
3248 loc = get_defcfg_location(def_conf);
3249 switch (get_defcfg_device(def_conf)) {
3250 case AC_JACK_LINE_OUT:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003251 seq = get_defcfg_sequence(def_conf);
3252 assoc = get_defcfg_association(def_conf);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01003253
3254 if (!(wid_caps & AC_WCAP_STEREO))
3255 if (!cfg->mono_out_pin)
3256 cfg->mono_out_pin = nid;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003257 if (!assoc)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003258 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003259 if (!assoc_line_out)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003260 assoc_line_out = assoc;
3261 else if (assoc_line_out != assoc)
3262 continue;
3263 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
3264 continue;
3265 cfg->line_out_pins[cfg->line_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02003266 sequences_line_out[cfg->line_outs] = seq;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003267 cfg->line_outs++;
3268 break;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01003269 case AC_JACK_SPEAKER:
Steve Longerbeam81937d32007-05-08 15:33:03 +02003270 seq = get_defcfg_sequence(def_conf);
3271 assoc = get_defcfg_association(def_conf);
3272 if (! assoc)
3273 continue;
3274 if (! assoc_speaker)
3275 assoc_speaker = assoc;
3276 else if (assoc_speaker != assoc)
3277 continue;
Takashi Iwai82bc9552006-03-21 11:24:42 +01003278 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
3279 continue;
3280 cfg->speaker_pins[cfg->speaker_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02003281 sequences_speaker[cfg->speaker_outs] = seq;
Takashi Iwai82bc9552006-03-21 11:24:42 +01003282 cfg->speaker_outs++;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01003283 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003284 case AC_JACK_HP_OUT:
Takashi Iwaif889fa92007-10-31 15:49:32 +01003285 seq = get_defcfg_sequence(def_conf);
3286 assoc = get_defcfg_association(def_conf);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02003287 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
3288 continue;
3289 cfg->hp_pins[cfg->hp_outs] = nid;
Takashi Iwaif889fa92007-10-31 15:49:32 +01003290 sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
Takashi Iwaieb06ed82006-09-20 17:10:27 +02003291 cfg->hp_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003292 break;
Takashi Iwai314634b2006-09-21 11:56:18 +02003293 case AC_JACK_MIC_IN: {
3294 int preferred, alt;
3295 if (loc == AC_JACK_LOC_FRONT) {
3296 preferred = AUTO_PIN_FRONT_MIC;
3297 alt = AUTO_PIN_MIC;
3298 } else {
3299 preferred = AUTO_PIN_MIC;
3300 alt = AUTO_PIN_FRONT_MIC;
3301 }
3302 if (!cfg->input_pins[preferred])
3303 cfg->input_pins[preferred] = nid;
3304 else if (!cfg->input_pins[alt])
3305 cfg->input_pins[alt] = nid;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003306 break;
Takashi Iwai314634b2006-09-21 11:56:18 +02003307 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003308 case AC_JACK_LINE_IN:
3309 if (loc == AC_JACK_LOC_FRONT)
3310 cfg->input_pins[AUTO_PIN_FRONT_LINE] = nid;
3311 else
3312 cfg->input_pins[AUTO_PIN_LINE] = nid;
3313 break;
3314 case AC_JACK_CD:
3315 cfg->input_pins[AUTO_PIN_CD] = nid;
3316 break;
3317 case AC_JACK_AUX:
3318 cfg->input_pins[AUTO_PIN_AUX] = nid;
3319 break;
3320 case AC_JACK_SPDIF_OUT:
3321 cfg->dig_out_pin = nid;
3322 break;
3323 case AC_JACK_SPDIF_IN:
3324 cfg->dig_in_pin = nid;
3325 break;
3326 }
3327 }
3328
Takashi Iwai5832fcf2008-02-12 18:30:12 +01003329 /* FIX-UP:
3330 * If no line-out is defined but multiple HPs are found,
3331 * some of them might be the real line-outs.
3332 */
3333 if (!cfg->line_outs && cfg->hp_outs > 1) {
3334 int i = 0;
3335 while (i < cfg->hp_outs) {
3336 /* The real HPs should have the sequence 0x0f */
3337 if ((sequences_hp[i] & 0x0f) == 0x0f) {
3338 i++;
3339 continue;
3340 }
3341 /* Move it to the line-out table */
3342 cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
3343 sequences_line_out[cfg->line_outs] = sequences_hp[i];
3344 cfg->line_outs++;
3345 cfg->hp_outs--;
3346 memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
3347 sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
3348 memmove(sequences_hp + i - 1, sequences_hp + i,
3349 sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
3350 }
3351 }
3352
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003353 /* sort by sequence */
Steve Longerbeam81937d32007-05-08 15:33:03 +02003354 sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
3355 cfg->line_outs);
3356 sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
3357 cfg->speaker_outs);
Takashi Iwaif889fa92007-10-31 15:49:32 +01003358 sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
3359 cfg->hp_outs);
Steve Longerbeam81937d32007-05-08 15:33:03 +02003360
Takashi Iwaif889fa92007-10-31 15:49:32 +01003361 /* if we have only one mic, make it AUTO_PIN_MIC */
3362 if (!cfg->input_pins[AUTO_PIN_MIC] &&
3363 cfg->input_pins[AUTO_PIN_FRONT_MIC]) {
3364 cfg->input_pins[AUTO_PIN_MIC] =
3365 cfg->input_pins[AUTO_PIN_FRONT_MIC];
3366 cfg->input_pins[AUTO_PIN_FRONT_MIC] = 0;
3367 }
3368 /* ditto for line-in */
3369 if (!cfg->input_pins[AUTO_PIN_LINE] &&
3370 cfg->input_pins[AUTO_PIN_FRONT_LINE]) {
3371 cfg->input_pins[AUTO_PIN_LINE] =
3372 cfg->input_pins[AUTO_PIN_FRONT_LINE];
3373 cfg->input_pins[AUTO_PIN_FRONT_LINE] = 0;
3374 }
3375
Steve Longerbeam81937d32007-05-08 15:33:03 +02003376 /*
3377 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
3378 * as a primary output
3379 */
3380 if (!cfg->line_outs) {
3381 if (cfg->speaker_outs) {
3382 cfg->line_outs = cfg->speaker_outs;
3383 memcpy(cfg->line_out_pins, cfg->speaker_pins,
3384 sizeof(cfg->speaker_pins));
3385 cfg->speaker_outs = 0;
3386 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
3387 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
3388 } else if (cfg->hp_outs) {
3389 cfg->line_outs = cfg->hp_outs;
3390 memcpy(cfg->line_out_pins, cfg->hp_pins,
3391 sizeof(cfg->hp_pins));
3392 cfg->hp_outs = 0;
3393 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
3394 cfg->line_out_type = AUTO_PIN_HP_OUT;
3395 }
3396 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003397
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02003398 /* Reorder the surround channels
3399 * ALSA sequence is front/surr/clfe/side
3400 * HDA sequence is:
3401 * 4-ch: front/surr => OK as it is
3402 * 6-ch: front/clfe/surr
Takashi Iwai9422db42007-04-20 16:11:43 +02003403 * 8-ch: front/clfe/rear/side|fc
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02003404 */
3405 switch (cfg->line_outs) {
3406 case 3:
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02003407 case 4:
3408 nid = cfg->line_out_pins[1];
Takashi Iwai9422db42007-04-20 16:11:43 +02003409 cfg->line_out_pins[1] = cfg->line_out_pins[2];
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02003410 cfg->line_out_pins[2] = nid;
3411 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003412 }
3413
Takashi Iwai82bc9552006-03-21 11:24:42 +01003414 /*
3415 * debug prints of the parsed results
3416 */
3417 snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3418 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
3419 cfg->line_out_pins[2], cfg->line_out_pins[3],
3420 cfg->line_out_pins[4]);
3421 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3422 cfg->speaker_outs, cfg->speaker_pins[0],
3423 cfg->speaker_pins[1], cfg->speaker_pins[2],
3424 cfg->speaker_pins[3], cfg->speaker_pins[4]);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02003425 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3426 cfg->hp_outs, cfg->hp_pins[0],
3427 cfg->hp_pins[1], cfg->hp_pins[2],
3428 cfg->hp_pins[3], cfg->hp_pins[4]);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01003429 snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
Takashi Iwai82bc9552006-03-21 11:24:42 +01003430 snd_printd(" inputs: mic=0x%x, fmic=0x%x, line=0x%x, fline=0x%x,"
3431 " cd=0x%x, aux=0x%x\n",
3432 cfg->input_pins[AUTO_PIN_MIC],
3433 cfg->input_pins[AUTO_PIN_FRONT_MIC],
3434 cfg->input_pins[AUTO_PIN_LINE],
3435 cfg->input_pins[AUTO_PIN_FRONT_LINE],
3436 cfg->input_pins[AUTO_PIN_CD],
3437 cfg->input_pins[AUTO_PIN_AUX]);
3438
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003439 return 0;
3440}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003441EXPORT_SYMBOL_HDA(snd_hda_parse_pin_def_config);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003442
Takashi Iwai4a471b72005-12-07 13:56:29 +01003443/* labels for input pins */
3444const char *auto_pin_cfg_labels[AUTO_PIN_LAST] = {
3445 "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux"
3446};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003447EXPORT_SYMBOL_HDA(auto_pin_cfg_labels);
Takashi Iwai4a471b72005-12-07 13:56:29 +01003448
3449
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450#ifdef CONFIG_PM
3451/*
3452 * power management
3453 */
3454
3455/**
3456 * snd_hda_suspend - suspend the codecs
3457 * @bus: the HDA bus
3458 * @state: suspsend state
3459 *
3460 * Returns 0 if successful.
3461 */
3462int snd_hda_suspend(struct hda_bus *bus, pm_message_t state)
3463{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003464 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465
Takashi Iwai0ba21762007-04-16 11:29:14 +02003466 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai0b7a2e92007-08-14 15:18:26 +02003467#ifdef CONFIG_SND_HDA_POWER_SAVE
3468 if (!codec->power_on)
3469 continue;
3470#endif
Takashi Iwaicb53c622007-08-10 17:21:45 +02003471 hda_call_codec_suspend(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 }
3473 return 0;
3474}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003475EXPORT_SYMBOL_HDA(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476
3477/**
3478 * snd_hda_resume - resume the codecs
3479 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 *
3481 * Returns 0 if successful.
Takashi Iwaicb53c622007-08-10 17:21:45 +02003482 *
3483 * This fucntion is defined only when POWER_SAVE isn't set.
3484 * In the power-save mode, the codec is resumed dynamically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 */
3486int snd_hda_resume(struct hda_bus *bus)
3487{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003488 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489
Takashi Iwai0ba21762007-04-16 11:29:14 +02003490 list_for_each_entry(codec, &bus->codec_list, list) {
Maxim Levitskyd804ad92007-09-03 15:28:04 +02003491 if (snd_hda_codec_needs_resume(codec))
3492 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 return 0;
3495}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003496EXPORT_SYMBOL_HDA(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003497#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02003498
3499/*
3500 * generic arrays
3501 */
3502
3503/* get a new element from the given array
3504 * if it exceeds the pre-allocated array size, re-allocate the array
3505 */
3506void *snd_array_new(struct snd_array *array)
3507{
3508 if (array->used >= array->alloced) {
3509 int num = array->alloced + array->alloc_align;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01003510 void *nlist;
3511 if (snd_BUG_ON(num >= 4096))
3512 return NULL;
3513 nlist = kcalloc(num + 1, array->elem_size, GFP_KERNEL);
Takashi Iwaib2e18592008-07-30 15:01:44 +02003514 if (!nlist)
3515 return NULL;
3516 if (array->list) {
3517 memcpy(nlist, array->list,
3518 array->elem_size * array->alloced);
3519 kfree(array->list);
3520 }
3521 array->list = nlist;
3522 array->alloced = num;
3523 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01003524 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02003525}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003526EXPORT_SYMBOL_HDA(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02003527
3528/* free the given array elements */
3529void snd_array_free(struct snd_array *array)
3530{
3531 kfree(array->list);
3532 array->used = 0;
3533 array->alloced = 0;
3534 array->list = NULL;
3535}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003536EXPORT_SYMBOL_HDA(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01003537
3538/*
3539 * used by hda_proc.c and hda_eld.c
3540 */
3541void snd_print_pcm_rates(int pcm, char *buf, int buflen)
3542{
3543 static unsigned int rates[] = {
3544 8000, 11025, 16000, 22050, 32000, 44100, 48000, 88200,
3545 96000, 176400, 192000, 384000
3546 };
3547 int i, j;
3548
3549 for (i = 0, j = 0; i < ARRAY_SIZE(rates); i++)
3550 if (pcm & (1 << i))
3551 j += snprintf(buf + j, buflen - j, " %d", rates[i]);
3552
3553 buf[j] = '\0'; /* necessary when j == 0 */
3554}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003555EXPORT_SYMBOL_HDA(snd_print_pcm_rates);
Takashi Iwaib2022262008-11-21 21:24:03 +01003556
3557void snd_print_pcm_bits(int pcm, char *buf, int buflen)
3558{
3559 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
3560 int i, j;
3561
3562 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
3563 if (pcm & (AC_SUPPCM_BITS_8 << i))
3564 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
3565
3566 buf[j] = '\0'; /* necessary when j == 0 */
3567}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003568EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003569
3570MODULE_DESCRIPTION("HDA codec core");
3571MODULE_LICENSE("GPL");