blob: f6832e16068407ac855be219338592065b83524c [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" },
Matt2f2f4252005-04-13 14:45:30 +020061 { 0x8384, "SigmaTel" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 {} /* terminator */
63};
64
Takashi Iwai1289e9e2008-11-27 15:47:11 +010065static DEFINE_MUTEX(preset_mutex);
66static LIST_HEAD(hda_preset_tables);
67
68int snd_hda_add_codec_preset(struct hda_codec_preset_list *preset)
69{
70 mutex_lock(&preset_mutex);
71 list_add_tail(&preset->list, &hda_preset_tables);
72 mutex_unlock(&preset_mutex);
73 return 0;
74}
Takashi Iwaiff7a3262008-11-28 15:17:06 +010075EXPORT_SYMBOL_HDA(snd_hda_add_codec_preset);
Takashi Iwai1289e9e2008-11-27 15:47:11 +010076
77int snd_hda_delete_codec_preset(struct hda_codec_preset_list *preset)
78{
79 mutex_lock(&preset_mutex);
80 list_del(&preset->list);
81 mutex_unlock(&preset_mutex);
82 return 0;
83}
Takashi Iwaiff7a3262008-11-28 15:17:06 +010084EXPORT_SYMBOL_HDA(snd_hda_delete_codec_preset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Takashi Iwaicb53c622007-08-10 17:21:45 +020086#ifdef CONFIG_SND_HDA_POWER_SAVE
87static void hda_power_work(struct work_struct *work);
88static void hda_keep_power_on(struct hda_codec *codec);
89#else
90static inline void hda_keep_power_on(struct hda_codec *codec) {}
91#endif
92
Matthew Ranostay50a9f792008-10-25 01:05:45 -040093const char *snd_hda_get_jack_location(u32 cfg)
94{
95 static char *bases[7] = {
96 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
97 };
98 static unsigned char specials_idx[] = {
99 0x07, 0x08,
100 0x17, 0x18, 0x19,
101 0x37, 0x38
102 };
103 static char *specials[] = {
104 "Rear Panel", "Drive Bar",
105 "Riser", "HDMI", "ATAPI",
106 "Mobile-In", "Mobile-Out"
107 };
108 int i;
109 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
110 if ((cfg & 0x0f) < 7)
111 return bases[cfg & 0x0f];
112 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
113 if (cfg == specials_idx[i])
114 return specials[i];
115 }
116 return "UNKNOWN";
117}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100118EXPORT_SYMBOL_HDA(snd_hda_get_jack_location);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400119
120const char *snd_hda_get_jack_connectivity(u32 cfg)
121{
122 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
123
124 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
125}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100126EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400127
128const char *snd_hda_get_jack_type(u32 cfg)
129{
130 static char *jack_types[16] = {
131 "Line Out", "Speaker", "HP Out", "CD",
132 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
133 "Line In", "Aux", "Mic", "Telephony",
134 "SPDIF In", "Digitial In", "Reserved", "Other"
135 };
136
137 return jack_types[(cfg & AC_DEFCFG_DEVICE)
138 >> AC_DEFCFG_DEVICE_SHIFT];
139}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100140EXPORT_SYMBOL_HDA(snd_hda_get_jack_type);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400141
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100142/*
143 * Compose a 32bit command word to be sent to the HD-audio controller
144 */
145static inline unsigned int
146make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int direct,
147 unsigned int verb, unsigned int parm)
148{
149 u32 val;
150
151 val = (u32)(codec->addr & 0x0f) << 28;
152 val |= (u32)direct << 27;
153 val |= (u32)nid << 20;
154 val |= verb << 8;
155 val |= parm;
156 return val;
157}
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159/**
160 * snd_hda_codec_read - send a command and get the response
161 * @codec: the HDA codec
162 * @nid: NID to send the command
163 * @direct: direct flag
164 * @verb: the verb to send
165 * @parm: the parameter for the verb
166 *
167 * Send a single command and read the corresponding response.
168 *
169 * Returns the obtained response value, or -1 for an error.
170 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200171unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
172 int direct,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 unsigned int verb, unsigned int parm)
174{
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100175 struct hda_bus *bus = codec->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 unsigned int res;
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100177
178 res = make_codec_cmd(codec, nid, direct, verb, parm);
Takashi Iwaicb53c622007-08-10 17:21:45 +0200179 snd_hda_power_up(codec);
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100180 mutex_lock(&bus->cmd_mutex);
181 if (!bus->ops.command(bus, res))
182 res = bus->ops.get_response(bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 else
184 res = (unsigned int)-1;
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100185 mutex_unlock(&bus->cmd_mutex);
Takashi Iwaicb53c622007-08-10 17:21:45 +0200186 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 return res;
188}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100189EXPORT_SYMBOL_HDA(snd_hda_codec_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191/**
192 * snd_hda_codec_write - send a single command without waiting for response
193 * @codec: the HDA codec
194 * @nid: NID to send the command
195 * @direct: direct flag
196 * @verb: the verb to send
197 * @parm: the parameter for the verb
198 *
199 * Send a single command without waiting for response.
200 *
201 * Returns 0 if successful, or a negative error code.
202 */
203int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
204 unsigned int verb, unsigned int parm)
205{
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100206 struct hda_bus *bus = codec->bus;
207 unsigned int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 int err;
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100209
210 res = make_codec_cmd(codec, nid, direct, verb, parm);
Takashi Iwaicb53c622007-08-10 17:21:45 +0200211 snd_hda_power_up(codec);
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100212 mutex_lock(&bus->cmd_mutex);
213 err = bus->ops.command(bus, res);
214 mutex_unlock(&bus->cmd_mutex);
Takashi Iwaicb53c622007-08-10 17:21:45 +0200215 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 return err;
217}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100218EXPORT_SYMBOL_HDA(snd_hda_codec_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220/**
221 * snd_hda_sequence_write - sequence writes
222 * @codec: the HDA codec
223 * @seq: VERB array to send
224 *
225 * Send the commands sequentially from the given array.
226 * The array must be terminated with NID=0.
227 */
228void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
229{
230 for (; seq->nid; seq++)
231 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
232}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100233EXPORT_SYMBOL_HDA(snd_hda_sequence_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
235/**
236 * snd_hda_get_sub_nodes - get the range of sub nodes
237 * @codec: the HDA codec
238 * @nid: NID to parse
239 * @start_id: the pointer to store the start NID
240 *
241 * Parse the NID and store the start NID of its sub-nodes.
242 * Returns the number of sub-nodes.
243 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200244int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
245 hda_nid_t *start_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
247 unsigned int parm;
248
249 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
Danny Tholene8a7f132007-09-11 21:41:56 +0200250 if (parm == -1)
251 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 *start_id = (parm >> 16) & 0x7fff;
253 return (int)(parm & 0x7fff);
254}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100255EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257/**
258 * snd_hda_get_connections - get connection list
259 * @codec: the HDA codec
260 * @nid: NID to parse
261 * @conn_list: connection list array
262 * @max_conns: max. number of connections to store
263 *
264 * Parses the connection list of the given widget and stores the list
265 * of NIDs.
266 *
267 * Returns the number of connections, or a negative error code.
268 */
269int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
270 hda_nid_t *conn_list, int max_conns)
271{
272 unsigned int parm;
Takashi Iwai54d17402005-11-21 16:33:22 +0100273 int i, conn_len, conns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 unsigned int shift, num_elems, mask;
Takashi Iwai54d17402005-11-21 16:33:22 +0100275 hda_nid_t prev_nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Takashi Iwaida3cec32008-08-08 17:12:14 +0200277 if (snd_BUG_ON(!conn_list || max_conns <= 0))
278 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
281 if (parm & AC_CLIST_LONG) {
282 /* long form */
283 shift = 16;
284 num_elems = 2;
285 } else {
286 /* short form */
287 shift = 8;
288 num_elems = 4;
289 }
290 conn_len = parm & AC_CLIST_LENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 mask = (1 << (shift-1)) - 1;
292
Takashi Iwai0ba21762007-04-16 11:29:14 +0200293 if (!conn_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 return 0; /* no connection */
295
296 if (conn_len == 1) {
297 /* single connection */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200298 parm = snd_hda_codec_read(codec, nid, 0,
299 AC_VERB_GET_CONNECT_LIST, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 conn_list[0] = parm & mask;
301 return 1;
302 }
303
304 /* multi connection */
305 conns = 0;
Takashi Iwai54d17402005-11-21 16:33:22 +0100306 prev_nid = 0;
307 for (i = 0; i < conn_len; i++) {
308 int range_val;
309 hda_nid_t val, n;
310
311 if (i % num_elems == 0)
312 parm = snd_hda_codec_read(codec, nid, 0,
313 AC_VERB_GET_CONNECT_LIST, i);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200314 range_val = !!(parm & (1 << (shift-1))); /* ranges */
Takashi Iwai54d17402005-11-21 16:33:22 +0100315 val = parm & mask;
316 parm >>= shift;
317 if (range_val) {
318 /* ranges between the previous and this one */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200319 if (!prev_nid || prev_nid >= val) {
320 snd_printk(KERN_WARNING "hda_codec: "
321 "invalid dep_range_val %x:%x\n",
322 prev_nid, val);
Takashi Iwai54d17402005-11-21 16:33:22 +0100323 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100325 for (n = prev_nid + 1; n <= val; n++) {
326 if (conns >= max_conns) {
Takashi Iwai0ba21762007-04-16 11:29:14 +0200327 snd_printk(KERN_ERR
328 "Too many connections\n");
Takashi Iwai54d17402005-11-21 16:33:22 +0100329 return -EINVAL;
330 }
331 conn_list[conns++] = n;
332 }
333 } else {
334 if (conns >= max_conns) {
335 snd_printk(KERN_ERR "Too many connections\n");
336 return -EINVAL;
337 }
338 conn_list[conns++] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100340 prev_nid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 }
342 return conns;
343}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100344EXPORT_SYMBOL_HDA(snd_hda_get_connections);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346
347/**
348 * snd_hda_queue_unsol_event - add an unsolicited event to queue
349 * @bus: the BUS
350 * @res: unsolicited event (lower 32bit of RIRB entry)
351 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
352 *
353 * Adds the given event to the queue. The events are processed in
354 * the workqueue asynchronously. Call this function in the interrupt
355 * hanlder when RIRB receives an unsolicited event.
356 *
357 * Returns 0 if successful, or a negative error code.
358 */
359int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
360{
361 struct hda_bus_unsolicited *unsol;
362 unsigned int wp;
363
Takashi Iwai0ba21762007-04-16 11:29:14 +0200364 unsol = bus->unsol;
365 if (!unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 return 0;
367
368 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
369 unsol->wp = wp;
370
371 wp <<= 1;
372 unsol->queue[wp] = res;
373 unsol->queue[wp + 1] = res_ex;
374
Takashi Iwaie250af22006-12-19 17:08:52 +0100375 schedule_work(&unsol->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377 return 0;
378}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100379EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381/*
Wu Fengguang5c1d1a92008-10-07 14:17:53 +0800382 * process queued unsolicited events
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 */
David Howellsc4028952006-11-22 14:57:56 +0000384static void process_unsol_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
David Howellsc4028952006-11-22 14:57:56 +0000386 struct hda_bus_unsolicited *unsol =
387 container_of(work, struct hda_bus_unsolicited, work);
388 struct hda_bus *bus = unsol->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 struct hda_codec *codec;
390 unsigned int rp, caddr, res;
391
392 while (unsol->rp != unsol->wp) {
393 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
394 unsol->rp = rp;
395 rp <<= 1;
396 res = unsol->queue[rp];
397 caddr = unsol->queue[rp + 1];
Takashi Iwai0ba21762007-04-16 11:29:14 +0200398 if (!(caddr & (1 << 4))) /* no unsolicited event? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 continue;
400 codec = bus->caddr_tbl[caddr & 0x0f];
401 if (codec && codec->patch_ops.unsol_event)
402 codec->patch_ops.unsol_event(codec, res);
403 }
404}
405
406/*
407 * initialize unsolicited queue
408 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200409static int init_unsol_queue(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
411 struct hda_bus_unsolicited *unsol;
412
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100413 if (bus->unsol) /* already initialized */
414 return 0;
415
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200416 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200417 if (!unsol) {
418 snd_printk(KERN_ERR "hda_codec: "
419 "can't allocate unsolicited queue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 return -ENOMEM;
421 }
David Howellsc4028952006-11-22 14:57:56 +0000422 INIT_WORK(&unsol->work, process_unsol_events);
423 unsol->bus = bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 bus->unsol = unsol;
425 return 0;
426}
427
428/*
429 * destructor
430 */
431static void snd_hda_codec_free(struct hda_codec *codec);
432
433static int snd_hda_bus_free(struct hda_bus *bus)
434{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200435 struct hda_codec *codec, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Takashi Iwai0ba21762007-04-16 11:29:14 +0200437 if (!bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 return 0;
439 if (bus->unsol) {
Takashi Iwaie250af22006-12-19 17:08:52 +0100440 flush_scheduled_work();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 kfree(bus->unsol);
442 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200443 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 snd_hda_codec_free(codec);
445 }
446 if (bus->ops.private_free)
447 bus->ops.private_free(bus);
448 kfree(bus);
449 return 0;
450}
451
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100452static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
454 struct hda_bus *bus = device->device_data;
Takashi Iwaib94d35392008-11-21 09:08:06 +0100455 bus->shutdown = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 return snd_hda_bus_free(bus);
457}
458
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200459#ifdef CONFIG_SND_HDA_HWDEP
460static int snd_hda_bus_dev_register(struct snd_device *device)
461{
462 struct hda_bus *bus = device->device_data;
463 struct hda_codec *codec;
464 list_for_each_entry(codec, &bus->codec_list, list) {
465 snd_hda_hwdep_add_sysfs(codec);
466 }
467 return 0;
468}
469#else
470#define snd_hda_bus_dev_register NULL
471#endif
472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473/**
474 * snd_hda_bus_new - create a HDA bus
475 * @card: the card entry
476 * @temp: the template for hda_bus information
477 * @busp: the pointer to store the created bus instance
478 *
479 * Returns 0 if successful, or a negative error code.
480 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100481int /*__devinit*/ snd_hda_bus_new(struct snd_card *card,
Takashi Iwai756e2b02007-04-16 11:27:07 +0200482 const struct hda_bus_template *temp,
483 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
485 struct hda_bus *bus;
486 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100487 static struct snd_device_ops dev_ops = {
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200488 .dev_register = snd_hda_bus_dev_register,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 .dev_free = snd_hda_bus_dev_free,
490 };
491
Takashi Iwaida3cec32008-08-08 17:12:14 +0200492 if (snd_BUG_ON(!temp))
493 return -EINVAL;
494 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
495 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
497 if (busp)
498 *busp = NULL;
499
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200500 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 if (bus == NULL) {
502 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
503 return -ENOMEM;
504 }
505
506 bus->card = card;
507 bus->private_data = temp->private_data;
508 bus->pci = temp->pci;
509 bus->modelname = temp->modelname;
Takashi Iwaifee2fba2008-11-27 12:43:28 +0100510 bus->power_save = temp->power_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 bus->ops = temp->ops;
512
Ingo Molnar62932df2006-01-16 16:34:20 +0100513 mutex_init(&bus->cmd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 INIT_LIST_HEAD(&bus->codec_list);
515
Takashi Iwai0ba21762007-04-16 11:29:14 +0200516 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
517 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 snd_hda_bus_free(bus);
519 return err;
520 }
521 if (busp)
522 *busp = bus;
523 return 0;
524}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100525EXPORT_SYMBOL_HDA(snd_hda_bus_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Takashi Iwai82467612007-07-27 19:15:54 +0200527#ifdef CONFIG_SND_HDA_GENERIC
528#define is_generic_config(codec) \
Takashi Iwaif44ac832008-07-30 15:01:45 +0200529 (codec->modelname && !strcmp(codec->modelname, "generic"))
Takashi Iwai82467612007-07-27 19:15:54 +0200530#else
531#define is_generic_config(codec) 0
532#endif
533
Takashi Iwai645f10c2008-11-28 15:07:37 +0100534#ifdef MODULE
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100535#define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
536#else
Takashi Iwai645f10c2008-11-28 15:07:37 +0100537#define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100538#endif
539
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540/*
541 * find a matching codec preset
542 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200543static const struct hda_codec_preset *
Takashi Iwai756e2b02007-04-16 11:27:07 +0200544find_codec_preset(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100546 struct hda_codec_preset_list *tbl;
547 const struct hda_codec_preset *preset;
548 int mod_requested = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Takashi Iwai82467612007-07-27 19:15:54 +0200550 if (is_generic_config(codec))
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100551 return NULL; /* use the generic parser */
552
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100553 again:
554 mutex_lock(&preset_mutex);
555 list_for_each_entry(tbl, &hda_preset_tables, list) {
556 if (!try_module_get(tbl->owner)) {
557 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
558 continue;
559 }
560 for (preset = tbl->preset; preset->id; preset++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 u32 mask = preset->mask;
Marc Boucherca7cfae2008-01-22 15:32:25 +0100562 if (preset->afg && preset->afg != codec->afg)
563 continue;
564 if (preset->mfg && preset->mfg != codec->mfg)
565 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200566 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 mask = ~0;
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200568 if (preset->id == (codec->vendor_id & mask) &&
Takashi Iwai0ba21762007-04-16 11:29:14 +0200569 (!preset->rev ||
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100570 preset->rev == codec->revision_id)) {
571 mutex_unlock(&preset_mutex);
572 codec->owner = tbl->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 return preset;
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100574 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 }
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100576 module_put(tbl->owner);
577 }
578 mutex_unlock(&preset_mutex);
579
580 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
581 char name[32];
582 if (!mod_requested)
583 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
584 codec->vendor_id);
585 else
586 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
587 (codec->vendor_id >> 16) & 0xffff);
588 request_module(name);
589 mod_requested++;
590 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 }
592 return NULL;
593}
594
595/*
Takashi Iwaif44ac832008-07-30 15:01:45 +0200596 * get_codec_name - store the codec name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 */
Takashi Iwaif44ac832008-07-30 15:01:45 +0200598static int get_codec_name(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
600 const struct hda_vendor_id *c;
601 const char *vendor = NULL;
602 u16 vendor_id = codec->vendor_id >> 16;
Takashi Iwaif44ac832008-07-30 15:01:45 +0200603 char tmp[16], name[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605 for (c = hda_vendor_ids; c->id; c++) {
606 if (c->id == vendor_id) {
607 vendor = c->name;
608 break;
609 }
610 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200611 if (!vendor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 sprintf(tmp, "Generic %04x", vendor_id);
613 vendor = tmp;
614 }
615 if (codec->preset && codec->preset->name)
Takashi Iwaif44ac832008-07-30 15:01:45 +0200616 snprintf(name, sizeof(name), "%s %s", vendor,
617 codec->preset->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 else
Takashi Iwaif44ac832008-07-30 15:01:45 +0200619 snprintf(name, sizeof(name), "%s ID %x", vendor,
Takashi Iwai0ba21762007-04-16 11:29:14 +0200620 codec->vendor_id & 0xffff);
Takashi Iwaif44ac832008-07-30 15:01:45 +0200621 codec->name = kstrdup(name, GFP_KERNEL);
622 if (!codec->name)
623 return -ENOMEM;
624 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625}
626
627/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200628 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100630static void /*__devinit*/ setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
632 int i, total_nodes;
633 hda_nid_t nid;
634
635 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
636 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +0200637 unsigned int func;
638 func = snd_hda_param_read(codec, nid, AC_PAR_FUNCTION_TYPE);
639 switch (func & 0xff) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200640 case AC_GRP_AUDIO_FUNCTION:
641 codec->afg = nid;
642 break;
643 case AC_GRP_MODEM_FUNCTION:
644 codec->mfg = nid;
645 break;
646 default:
647 break;
648 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650}
651
652/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100653 * read widget caps for each widget and store in cache
654 */
655static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
656{
657 int i;
658 hda_nid_t nid;
659
660 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
661 &codec->start_nid);
662 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200663 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +0100664 return -ENOMEM;
665 nid = codec->start_nid;
666 for (i = 0; i < codec->num_nodes; i++, nid++)
667 codec->wcaps[i] = snd_hda_param_read(codec, nid,
668 AC_PAR_AUDIO_WIDGET_CAP);
669 return 0;
670}
671
672
Takashi Iwai01751f52007-08-10 16:59:39 +0200673static void init_hda_cache(struct hda_cache_rec *cache,
674 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +0200675static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +0200676
Takashi Iwai54d17402005-11-21 16:33:22 +0100677/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 * codec destructor
679 */
680static void snd_hda_codec_free(struct hda_codec *codec)
681{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200682 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 return;
Takashi Iwaicb53c622007-08-10 17:21:45 +0200684#ifdef CONFIG_SND_HDA_POWER_SAVE
685 cancel_delayed_work(&codec->power_work);
Takashi Iwai2525fdc2007-08-15 22:18:22 +0200686 flush_scheduled_work();
Takashi Iwaicb53c622007-08-10 17:21:45 +0200687#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +0200689 snd_array_free(&codec->mixers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 codec->bus->caddr_tbl[codec->addr] = NULL;
691 if (codec->patch_ops.free)
692 codec->patch_ops.free(codec);
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100693 module_put(codec->owner);
Takashi Iwai01751f52007-08-10 16:59:39 +0200694 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +0200695 free_hda_cache(&codec->cmd_cache);
Takashi Iwaif44ac832008-07-30 15:01:45 +0200696 kfree(codec->name);
697 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +0100698 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 kfree(codec);
700}
701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702/**
703 * snd_hda_codec_new - create a HDA codec
704 * @bus: the bus to assign
705 * @codec_addr: the codec address
706 * @codecp: the pointer to store the generated codec
707 *
708 * Returns 0 if successful, or a negative error code.
709 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100710int /*__devinit*/ snd_hda_codec_new(struct hda_bus *bus, unsigned int codec_addr,
Takashi Iwai756e2b02007-04-16 11:27:07 +0200711 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
713 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +0200714 char component[31];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 int err;
716
Takashi Iwaida3cec32008-08-08 17:12:14 +0200717 if (snd_BUG_ON(!bus))
718 return -EINVAL;
719 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
720 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
722 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +0200723 snd_printk(KERN_ERR "hda_codec: "
724 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 return -EBUSY;
726 }
727
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200728 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 if (codec == NULL) {
730 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
731 return -ENOMEM;
732 }
733
734 codec->bus = bus;
735 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +0100736 mutex_init(&codec->spdif_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +0200737 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +0200738 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwaid13bd412008-07-30 15:01:45 +0200739 snd_array_init(&codec->mixers, sizeof(struct snd_kcontrol *), 32);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200740 if (codec->bus->modelname) {
741 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
742 if (!codec->modelname) {
743 snd_hda_codec_free(codec);
744 return -ENODEV;
745 }
746 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Takashi Iwaicb53c622007-08-10 17:21:45 +0200748#ifdef CONFIG_SND_HDA_POWER_SAVE
749 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
750 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
751 * the caller has to power down appropriatley after initialization
752 * phase.
753 */
754 hda_keep_power_on(codec);
755#endif
756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 list_add_tail(&codec->list, &bus->codec_list);
758 bus->caddr_tbl[codec_addr] = codec;
759
Takashi Iwai0ba21762007-04-16 11:29:14 +0200760 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
761 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +0100762 if (codec->vendor_id == -1)
763 /* read again, hopefully the access method was corrected
764 * in the last read...
765 */
766 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
767 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200768 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
769 AC_PAR_SUBSYSTEM_ID);
770 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
771 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200773 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200774 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200775 snd_printdd("hda_codec: no AFG or MFG node found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 snd_hda_codec_free(codec);
777 return -ENODEV;
778 }
779
Takashi Iwai54d17402005-11-21 16:33:22 +0100780 if (read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg) < 0) {
781 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
782 snd_hda_codec_free(codec);
783 return -ENOMEM;
784 }
785
Takashi Iwai0ba21762007-04-16 11:29:14 +0200786 if (!codec->subsystem_id) {
Takashi Iwai86284e42005-10-11 15:05:54 +0200787 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200788 codec->subsystem_id =
789 snd_hda_codec_read(codec, nid, 0,
790 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +0200791 }
Takashi Iwaif44ac832008-07-30 15:01:45 +0200792 if (bus->modelname)
793 codec->modelname = kstrdup(bus->modelname, GFP_KERNEL);
Takashi Iwai86284e42005-10-11 15:05:54 +0200794
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200795 err = snd_hda_codec_configure(codec);
796 if (err < 0) {
797 snd_hda_codec_free(codec);
798 return err;
799 }
800 snd_hda_codec_proc_new(codec);
801
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200802 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200803
804 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
805 codec->subsystem_id, codec->revision_id);
806 snd_component_add(codec->bus->card, component);
807
808 if (codecp)
809 *codecp = codec;
810 return 0;
811}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100812EXPORT_SYMBOL_HDA(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200813
814int snd_hda_codec_configure(struct hda_codec *codec)
815{
816 int err;
817
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100818 codec->preset = find_codec_preset(codec);
Takashi Iwaif44ac832008-07-30 15:01:45 +0200819 if (!codec->name) {
820 err = get_codec_name(codec);
821 if (err < 0)
822 return err;
823 }
Takashi Iwai43ea1d42007-04-26 19:12:08 +0200824 /* audio codec should override the mixer name */
Takashi Iwaif44ac832008-07-30 15:01:45 +0200825 if (codec->afg || !*codec->bus->card->mixername)
826 strlcpy(codec->bus->card->mixername, codec->name,
827 sizeof(codec->bus->card->mixername));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Takashi Iwai82467612007-07-27 19:15:54 +0200829 if (is_generic_config(codec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai82467612007-07-27 19:15:54 +0200831 goto patched;
832 }
Takashi Iwai82467612007-07-27 19:15:54 +0200833 if (codec->preset && codec->preset->patch) {
834 err = codec->preset->patch(codec);
835 goto patched;
836 }
837
838 /* call the default parser */
Takashi Iwai82467612007-07-27 19:15:54 +0200839 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai35a1e0c2007-10-19 08:13:40 +0200840 if (err < 0)
841 printk(KERN_ERR "hda-codec: No codec parser is available\n");
Takashi Iwai82467612007-07-27 19:15:54 +0200842
843 patched:
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200844 if (!err && codec->patch_ops.unsol_event)
845 err = init_unsol_queue(codec->bus);
846 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847}
848
849/**
850 * snd_hda_codec_setup_stream - set up the codec for streaming
851 * @codec: the CODEC to set up
852 * @nid: the NID to set up
853 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
854 * @channel_id: channel id to pass, zero based.
855 * @format: stream format.
856 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200857void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
858 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 int channel_id, int format)
860{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200861 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +0200862 return;
863
Takashi Iwai0ba21762007-04-16 11:29:14 +0200864 snd_printdd("hda_codec_setup_stream: "
865 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 nid, stream_tag, channel_id, format);
867 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID,
868 (stream_tag << 4) | channel_id);
869 msleep(1);
870 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, format);
871}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100872EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
Takashi Iwai888afa12008-03-18 09:57:50 +0100874void snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid)
875{
876 if (!nid)
877 return;
878
879 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
880 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
881#if 0 /* keep the format */
882 msleep(1);
883 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0);
884#endif
885}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100886EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +0100887
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888/*
889 * amp access functions
890 */
891
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200892/* FIXME: more better hash key? */
893#define HDA_HASH_KEY(nid,dir,idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200895#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
897/* initialize the hash table */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100898static void /*__devinit*/ init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +0200899 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900{
Takashi Iwai01751f52007-08-10 16:59:39 +0200901 memset(cache, 0, sizeof(*cache));
902 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +0200903 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +0200904}
905
Takashi Iwai1fcaee62007-08-23 00:01:09 +0200906static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +0200907{
Takashi Iwai603c4012008-07-30 15:01:44 +0200908 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909}
910
911/* query the hash. allocate an entry if not found. */
Takashi Iwai01751f52007-08-10 16:59:39 +0200912static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
913 u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914{
Takashi Iwai01751f52007-08-10 16:59:39 +0200915 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
916 u16 cur = cache->hash[idx];
917 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
919 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +0100920 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 if (info->key == key)
922 return info;
923 cur = info->next;
924 }
925
926 /* add a new hash entry */
Takashi Iwai603c4012008-07-30 15:01:44 +0200927 info = snd_array_new(&cache->buf);
Takashi Iwaic2174292008-11-07 00:23:30 +0100928 if (!info)
929 return NULL;
Takashi Iwaif43aa022008-11-10 16:24:26 +0100930 cur = snd_array_index(&cache->buf, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 info->key = key;
Takashi Iwai01751f52007-08-10 16:59:39 +0200932 info->val = 0;
933 info->next = cache->hash[idx];
934 cache->hash[idx] = cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
936 return info;
937}
938
Takashi Iwai01751f52007-08-10 16:59:39 +0200939/* query and allocate an amp hash entry */
940static inline struct hda_amp_info *
941get_alloc_amp_hash(struct hda_codec *codec, u32 key)
942{
943 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
944}
945
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946/*
947 * query AMP capabilities for the given widget and direction
948 */
Matthew Ranostay09a99952008-01-24 11:49:21 +0100949u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200951 struct hda_amp_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
Takashi Iwai0ba21762007-04-16 11:29:14 +0200953 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
954 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 return 0;
Takashi Iwai01751f52007-08-10 16:59:39 +0200956 if (!(info->head.val & INFO_AMP_CAPS)) {
Takashi Iwai0ba21762007-04-16 11:29:14 +0200957 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 nid = codec->afg;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200959 info->amp_caps = snd_hda_param_read(codec, nid,
960 direction == HDA_OUTPUT ?
961 AC_PAR_AMP_OUT_CAP :
962 AC_PAR_AMP_IN_CAP);
Takashi Iwaib75e53f2007-05-10 16:56:09 +0200963 if (info->amp_caps)
Takashi Iwai01751f52007-08-10 16:59:39 +0200964 info->head.val |= INFO_AMP_CAPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 }
966 return info->amp_caps;
967}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100968EXPORT_SYMBOL_HDA(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
Takashi Iwai897cc182007-05-29 19:01:37 +0200970int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
971 unsigned int caps)
972{
973 struct hda_amp_info *info;
974
975 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
976 if (!info)
977 return -EINVAL;
978 info->amp_caps = caps;
Takashi Iwai01751f52007-08-10 16:59:39 +0200979 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai897cc182007-05-29 19:01:37 +0200980 return 0;
981}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100982EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +0200983
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984/*
985 * read the current volume to info
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200986 * if the cache exists, read the cache value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200988static unsigned int get_vol_mute(struct hda_codec *codec,
989 struct hda_amp_info *info, hda_nid_t nid,
990 int ch, int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991{
992 u32 val, parm;
993
Takashi Iwai01751f52007-08-10 16:59:39 +0200994 if (info->head.val & INFO_AMP_VOL(ch))
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200995 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
997 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
998 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
999 parm |= index;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001000 val = snd_hda_codec_read(codec, nid, 0,
1001 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 info->vol[ch] = val & 0xff;
Takashi Iwai01751f52007-08-10 16:59:39 +02001003 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001004 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005}
1006
1007/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001008 * write the current volume in info to the h/w and update the cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001010static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001011 hda_nid_t nid, int ch, int direction, int index,
1012 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013{
1014 u32 parm;
1015
1016 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1017 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1018 parm |= index << AC_AMP_SET_INDEX_SHIFT;
1019 parm |= val;
1020 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001021 info->vol[ch] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022}
1023
1024/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001025 * read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 */
Takashi Iwai834be882006-03-01 14:16:17 +01001027int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1028 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001030 struct hda_amp_info *info;
1031 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1032 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001034 return get_vol_mute(codec, info, nid, ch, direction, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001036EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001038/*
1039 * update the AMP value, mask = bit mask to set, val = the value
1040 */
Takashi Iwai834be882006-03-01 14:16:17 +01001041int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1042 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001044 struct hda_amp_info *info;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001045
Takashi Iwai0ba21762007-04-16 11:29:14 +02001046 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
1047 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001049 val &= mask;
1050 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001051 if (info->vol[ch] == val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001053 put_vol_mute(codec, info, nid, ch, direction, idx, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 return 1;
1055}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001056EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
Takashi Iwai47fd8302007-08-10 17:11:07 +02001058/*
1059 * update the AMP stereo with the same mask and value
1060 */
1061int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1062 int direction, int idx, int mask, int val)
1063{
1064 int ch, ret = 0;
1065 for (ch = 0; ch < 2; ch++)
1066 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1067 idx, mask, val);
1068 return ret;
1069}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001070EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001071
Takashi Iwaicb53c622007-08-10 17:21:45 +02001072#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001073/* resume the all amp commands from the cache */
1074void snd_hda_codec_resume_amp(struct hda_codec *codec)
1075{
Takashi Iwai603c4012008-07-30 15:01:44 +02001076 struct hda_amp_info *buffer = codec->amp_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001077 int i;
1078
Takashi Iwai603c4012008-07-30 15:01:44 +02001079 for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001080 u32 key = buffer->head.key;
1081 hda_nid_t nid;
1082 unsigned int idx, dir, ch;
1083 if (!key)
1084 continue;
1085 nid = key & 0xff;
1086 idx = (key >> 16) & 0xff;
1087 dir = (key >> 24) & 0xff;
1088 for (ch = 0; ch < 2; ch++) {
1089 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
1090 continue;
1091 put_vol_mute(codec, buffer, nid, ch, dir, idx,
1092 buffer->vol[ch]);
1093 }
1094 }
1095}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001096EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001097#endif /* SND_HDA_NEEDS_RESUME */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099/* volume */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001100int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1101 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102{
1103 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1104 u16 nid = get_amp_nid(kcontrol);
1105 u8 chs = get_amp_channels(kcontrol);
1106 int dir = get_amp_direction(kcontrol);
1107 u32 caps;
1108
1109 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001110 /* num steps */
1111 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1112 if (!caps) {
1113 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01001114 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
1115 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 return -EINVAL;
1117 }
1118 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1119 uinfo->count = chs == 3 ? 2 : 1;
1120 uinfo->value.integer.min = 0;
1121 uinfo->value.integer.max = caps;
1122 return 0;
1123}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001124EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
Takashi Iwai0ba21762007-04-16 11:29:14 +02001126int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1127 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128{
1129 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1130 hda_nid_t nid = get_amp_nid(kcontrol);
1131 int chs = get_amp_channels(kcontrol);
1132 int dir = get_amp_direction(kcontrol);
1133 int idx = get_amp_index(kcontrol);
1134 long *valp = ucontrol->value.integer.value;
1135
1136 if (chs & 1)
Takashi Iwai47fd8302007-08-10 17:11:07 +02001137 *valp++ = snd_hda_codec_amp_read(codec, nid, 0, dir, idx)
1138 & HDA_AMP_VOLMASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 if (chs & 2)
Takashi Iwai47fd8302007-08-10 17:11:07 +02001140 *valp = snd_hda_codec_amp_read(codec, nid, 1, dir, idx)
1141 & HDA_AMP_VOLMASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 return 0;
1143}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001144EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
Takashi Iwai0ba21762007-04-16 11:29:14 +02001146int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1147 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148{
1149 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1150 hda_nid_t nid = get_amp_nid(kcontrol);
1151 int chs = get_amp_channels(kcontrol);
1152 int dir = get_amp_direction(kcontrol);
1153 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 long *valp = ucontrol->value.integer.value;
1155 int change = 0;
1156
Takashi Iwaicb53c622007-08-10 17:21:45 +02001157 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001158 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001159 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
1160 0x7f, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001161 valp++;
1162 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001163 if (chs & 2)
1164 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001165 0x7f, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001166 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 return change;
1168}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001169EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001171int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1172 unsigned int size, unsigned int __user *_tlv)
1173{
1174 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1175 hda_nid_t nid = get_amp_nid(kcontrol);
1176 int dir = get_amp_direction(kcontrol);
1177 u32 caps, val1, val2;
1178
1179 if (size < 4 * sizeof(unsigned int))
1180 return -ENOMEM;
1181 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001182 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1183 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001184 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
1185 val1 = ((int)val1) * ((int)val2);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001186 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
1187 return -EFAULT;
1188 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
1189 return -EFAULT;
1190 if (put_user(val1, _tlv + 2))
1191 return -EFAULT;
1192 if (put_user(val2, _tlv + 3))
1193 return -EFAULT;
1194 return 0;
1195}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001196EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001197
Takashi Iwai2134ea42008-01-10 16:53:55 +01001198/*
1199 * set (static) TLV for virtual master volume; recalculated as max 0dB
1200 */
1201void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
1202 unsigned int *tlv)
1203{
1204 u32 caps;
1205 int nums, step;
1206
1207 caps = query_amp_caps(codec, nid, dir);
1208 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1209 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1210 step = (step + 1) * 25;
1211 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
1212 tlv[1] = 2 * sizeof(unsigned int);
1213 tlv[2] = -nums * step;
1214 tlv[3] = step;
1215}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001216EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001217
1218/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01001219static struct snd_kcontrol *
1220_snd_hda_find_mixer_ctl(struct hda_codec *codec,
1221 const char *name, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01001222{
1223 struct snd_ctl_elem_id id;
1224 memset(&id, 0, sizeof(id));
1225 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwai09f99702008-02-04 12:31:13 +01001226 id.index = idx;
Takashi Iwai2134ea42008-01-10 16:53:55 +01001227 strcpy(id.name, name);
1228 return snd_ctl_find_id(codec->bus->card, &id);
1229}
1230
Takashi Iwai09f99702008-02-04 12:31:13 +01001231struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
1232 const char *name)
1233{
1234 return _snd_hda_find_mixer_ctl(codec, name, 0);
1235}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001236EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01001237
Takashi Iwaid13bd412008-07-30 15:01:45 +02001238/* Add a control element and assign to the codec */
1239int snd_hda_ctl_add(struct hda_codec *codec, struct snd_kcontrol *kctl)
1240{
1241 int err;
1242 struct snd_kcontrol **knewp;
1243
1244 err = snd_ctl_add(codec->bus->card, kctl);
1245 if (err < 0)
1246 return err;
1247 knewp = snd_array_new(&codec->mixers);
1248 if (!knewp)
1249 return -ENOMEM;
1250 *knewp = kctl;
1251 return 0;
1252}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001253EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001254
Takashi Iwai529bd6c2008-11-27 14:17:01 +01001255#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwaid13bd412008-07-30 15:01:45 +02001256/* Clear all controls assigned to the given codec */
1257void snd_hda_ctls_clear(struct hda_codec *codec)
1258{
1259 int i;
1260 struct snd_kcontrol **kctls = codec->mixers.list;
1261 for (i = 0; i < codec->mixers.used; i++)
1262 snd_ctl_remove(codec->bus->card, kctls[i]);
1263 snd_array_free(&codec->mixers);
1264}
1265
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001266void snd_hda_codec_reset(struct hda_codec *codec)
1267{
1268 int i;
1269
1270#ifdef CONFIG_SND_HDA_POWER_SAVE
1271 cancel_delayed_work(&codec->power_work);
1272 flush_scheduled_work();
1273#endif
1274 snd_hda_ctls_clear(codec);
1275 /* relase PCMs */
1276 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01001277 if (codec->pcm_info[i].pcm) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001278 snd_device_free(codec->bus->card,
1279 codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01001280 clear_bit(codec->pcm_info[i].device,
1281 codec->bus->pcm_dev_bits);
1282 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001283 }
1284 if (codec->patch_ops.free)
1285 codec->patch_ops.free(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01001286 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001287 codec->spec = NULL;
1288 free_hda_cache(&codec->amp_cache);
1289 free_hda_cache(&codec->cmd_cache);
1290 codec->num_pcms = 0;
1291 codec->pcm_info = NULL;
1292 codec->preset = NULL;
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001293 module_put(codec->owner);
1294 codec->owner = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001295}
Takashi Iwai529bd6c2008-11-27 14:17:01 +01001296#endif /* CONFIG_SND_HDA_RECONFIG */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001297
Takashi Iwai2134ea42008-01-10 16:53:55 +01001298/* create a virtual master control and add slaves */
1299int snd_hda_add_vmaster(struct hda_codec *codec, char *name,
1300 unsigned int *tlv, const char **slaves)
1301{
1302 struct snd_kcontrol *kctl;
1303 const char **s;
1304 int err;
1305
Takashi Iwai2f085542008-02-22 18:43:50 +01001306 for (s = slaves; *s && !snd_hda_find_mixer_ctl(codec, *s); s++)
1307 ;
1308 if (!*s) {
1309 snd_printdd("No slave found for %s\n", name);
1310 return 0;
1311 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01001312 kctl = snd_ctl_make_virtual_master(name, tlv);
1313 if (!kctl)
1314 return -ENOMEM;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001315 err = snd_hda_ctl_add(codec, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001316 if (err < 0)
1317 return err;
1318
1319 for (s = slaves; *s; s++) {
1320 struct snd_kcontrol *sctl;
1321
1322 sctl = snd_hda_find_mixer_ctl(codec, *s);
1323 if (!sctl) {
1324 snd_printdd("Cannot find slave %s, skipped\n", *s);
1325 continue;
1326 }
1327 err = snd_ctl_add_slave(kctl, sctl);
1328 if (err < 0)
1329 return err;
1330 }
1331 return 0;
1332}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001333EXPORT_SYMBOL_HDA(snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001334
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335/* switch */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001336int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
1337 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338{
1339 int chs = get_amp_channels(kcontrol);
1340
1341 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1342 uinfo->count = chs == 3 ? 2 : 1;
1343 uinfo->value.integer.min = 0;
1344 uinfo->value.integer.max = 1;
1345 return 0;
1346}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001347EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
Takashi Iwai0ba21762007-04-16 11:29:14 +02001349int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
1350 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351{
1352 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1353 hda_nid_t nid = get_amp_nid(kcontrol);
1354 int chs = get_amp_channels(kcontrol);
1355 int dir = get_amp_direction(kcontrol);
1356 int idx = get_amp_index(kcontrol);
1357 long *valp = ucontrol->value.integer.value;
1358
1359 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02001360 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02001361 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02001363 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02001364 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 return 0;
1366}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001367EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
Takashi Iwai0ba21762007-04-16 11:29:14 +02001369int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
1370 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371{
1372 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1373 hda_nid_t nid = get_amp_nid(kcontrol);
1374 int chs = get_amp_channels(kcontrol);
1375 int dir = get_amp_direction(kcontrol);
1376 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 long *valp = ucontrol->value.integer.value;
1378 int change = 0;
1379
Takashi Iwaicb53c622007-08-10 17:21:45 +02001380 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001381 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001382 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02001383 HDA_AMP_MUTE,
1384 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001385 valp++;
1386 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001387 if (chs & 2)
1388 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02001389 HDA_AMP_MUTE,
1390 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001391#ifdef CONFIG_SND_HDA_POWER_SAVE
1392 if (codec->patch_ops.check_power_status)
1393 codec->patch_ops.check_power_status(codec, nid);
1394#endif
1395 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 return change;
1397}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001398EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
1400/*
Takashi Iwai985be542005-11-02 18:26:49 +01001401 * bound volume controls
1402 *
1403 * bind multiple volumes (# indices, from 0)
1404 */
1405
1406#define AMP_VAL_IDX_SHIFT 19
1407#define AMP_VAL_IDX_MASK (0x0f<<19)
1408
Takashi Iwai0ba21762007-04-16 11:29:14 +02001409int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
1410 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01001411{
1412 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1413 unsigned long pval;
1414 int err;
1415
Ingo Molnar62932df2006-01-16 16:34:20 +01001416 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Takashi Iwai985be542005-11-02 18:26:49 +01001417 pval = kcontrol->private_value;
1418 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
1419 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
1420 kcontrol->private_value = pval;
Ingo Molnar62932df2006-01-16 16:34:20 +01001421 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01001422 return err;
1423}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001424EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01001425
Takashi Iwai0ba21762007-04-16 11:29:14 +02001426int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
1427 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01001428{
1429 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1430 unsigned long pval;
1431 int i, indices, err = 0, change = 0;
1432
Ingo Molnar62932df2006-01-16 16:34:20 +01001433 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Takashi Iwai985be542005-11-02 18:26:49 +01001434 pval = kcontrol->private_value;
1435 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
1436 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001437 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
1438 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01001439 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1440 if (err < 0)
1441 break;
1442 change |= err;
1443 }
1444 kcontrol->private_value = pval;
Ingo Molnar62932df2006-01-16 16:34:20 +01001445 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01001446 return err < 0 ? err : change;
1447}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001448EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01001449
1450/*
Takashi Iwai532d5382007-07-27 19:02:40 +02001451 * generic bound volume/swtich controls
1452 */
1453int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
1454 struct snd_ctl_elem_info *uinfo)
1455{
1456 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1457 struct hda_bind_ctls *c;
1458 int err;
1459
Takashi Iwai532d5382007-07-27 19:02:40 +02001460 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01001461 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02001462 kcontrol->private_value = *c->values;
1463 err = c->ops->info(kcontrol, uinfo);
1464 kcontrol->private_value = (long)c;
1465 mutex_unlock(&codec->spdif_mutex);
1466 return err;
1467}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001468EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02001469
1470int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
1471 struct snd_ctl_elem_value *ucontrol)
1472{
1473 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1474 struct hda_bind_ctls *c;
1475 int err;
1476
Takashi Iwai532d5382007-07-27 19:02:40 +02001477 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01001478 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02001479 kcontrol->private_value = *c->values;
1480 err = c->ops->get(kcontrol, ucontrol);
1481 kcontrol->private_value = (long)c;
1482 mutex_unlock(&codec->spdif_mutex);
1483 return err;
1484}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001485EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02001486
1487int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
1488 struct snd_ctl_elem_value *ucontrol)
1489{
1490 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1491 struct hda_bind_ctls *c;
1492 unsigned long *vals;
1493 int err = 0, change = 0;
1494
Takashi Iwai532d5382007-07-27 19:02:40 +02001495 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01001496 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02001497 for (vals = c->values; *vals; vals++) {
1498 kcontrol->private_value = *vals;
1499 err = c->ops->put(kcontrol, ucontrol);
1500 if (err < 0)
1501 break;
1502 change |= err;
1503 }
1504 kcontrol->private_value = (long)c;
1505 mutex_unlock(&codec->spdif_mutex);
1506 return err < 0 ? err : change;
1507}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001508EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02001509
1510int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1511 unsigned int size, unsigned int __user *tlv)
1512{
1513 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1514 struct hda_bind_ctls *c;
1515 int err;
1516
Takashi Iwai532d5382007-07-27 19:02:40 +02001517 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01001518 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02001519 kcontrol->private_value = *c->values;
1520 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
1521 kcontrol->private_value = (long)c;
1522 mutex_unlock(&codec->spdif_mutex);
1523 return err;
1524}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001525EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02001526
1527struct hda_ctl_ops snd_hda_bind_vol = {
1528 .info = snd_hda_mixer_amp_volume_info,
1529 .get = snd_hda_mixer_amp_volume_get,
1530 .put = snd_hda_mixer_amp_volume_put,
1531 .tlv = snd_hda_mixer_amp_tlv
1532};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001533EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02001534
1535struct hda_ctl_ops snd_hda_bind_sw = {
1536 .info = snd_hda_mixer_amp_switch_info,
1537 .get = snd_hda_mixer_amp_switch_get,
1538 .put = snd_hda_mixer_amp_switch_put,
1539 .tlv = snd_hda_mixer_amp_tlv
1540};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001541EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02001542
1543/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 * SPDIF out controls
1545 */
1546
Takashi Iwai0ba21762007-04-16 11:29:14 +02001547static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
1548 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549{
1550 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1551 uinfo->count = 1;
1552 return 0;
1553}
1554
Takashi Iwai0ba21762007-04-16 11:29:14 +02001555static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
1556 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557{
1558 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
1559 IEC958_AES0_NONAUDIO |
1560 IEC958_AES0_CON_EMPHASIS_5015 |
1561 IEC958_AES0_CON_NOT_COPYRIGHT;
1562 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
1563 IEC958_AES1_CON_ORIGINAL;
1564 return 0;
1565}
1566
Takashi Iwai0ba21762007-04-16 11:29:14 +02001567static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
1568 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569{
1570 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
1571 IEC958_AES0_NONAUDIO |
1572 IEC958_AES0_PRO_EMPHASIS_5015;
1573 return 0;
1574}
1575
Takashi Iwai0ba21762007-04-16 11:29:14 +02001576static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
1577 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578{
1579 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1580
1581 ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
1582 ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
1583 ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
1584 ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
1585
1586 return 0;
1587}
1588
1589/* convert from SPDIF status bits to HDA SPDIF bits
1590 * bit 0 (DigEn) is always set zero (to be filled later)
1591 */
1592static unsigned short convert_from_spdif_status(unsigned int sbits)
1593{
1594 unsigned short val = 0;
1595
1596 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02001597 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02001599 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001601 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
1602 IEC958_AES0_PRO_EMPHASIS_5015)
1603 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001605 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
1606 IEC958_AES0_CON_EMPHASIS_5015)
1607 val |= AC_DIG1_EMPHASIS;
1608 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
1609 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02001611 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
1613 }
1614 return val;
1615}
1616
1617/* convert to SPDIF status bits from HDA SPDIF bits
1618 */
1619static unsigned int convert_to_spdif_status(unsigned short val)
1620{
1621 unsigned int sbits = 0;
1622
Takashi Iwai0ba21762007-04-16 11:29:14 +02001623 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001625 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 sbits |= IEC958_AES0_PROFESSIONAL;
1627 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001628 if (sbits & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
1630 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001631 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001633 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001635 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
1637 sbits |= val & (0x7f << 8);
1638 }
1639 return sbits;
1640}
1641
Takashi Iwai2f728532008-09-25 16:32:41 +02001642/* set digital convert verbs both for the given NID and its slaves */
1643static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
1644 int verb, int val)
1645{
1646 hda_nid_t *d;
1647
Takashi Iwai9e976972008-11-25 08:17:20 +01001648 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02001649 d = codec->slave_dig_outs;
1650 if (!d)
1651 return;
1652 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01001653 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02001654}
1655
1656static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
1657 int dig1, int dig2)
1658{
1659 if (dig1 != -1)
1660 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
1661 if (dig2 != -1)
1662 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
1663}
1664
Takashi Iwai0ba21762007-04-16 11:29:14 +02001665static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
1666 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667{
1668 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1669 hda_nid_t nid = kcontrol->private_value;
1670 unsigned short val;
1671 int change;
1672
Ingo Molnar62932df2006-01-16 16:34:20 +01001673 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 codec->spdif_status = ucontrol->value.iec958.status[0] |
1675 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
1676 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
1677 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
1678 val = convert_from_spdif_status(codec->spdif_status);
1679 val |= codec->spdif_ctls & 1;
1680 change = codec->spdif_ctls != val;
1681 codec->spdif_ctls = val;
1682
Takashi Iwai2f728532008-09-25 16:32:41 +02001683 if (change)
1684 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685
Ingo Molnar62932df2006-01-16 16:34:20 +01001686 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 return change;
1688}
1689
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001690#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
Takashi Iwai0ba21762007-04-16 11:29:14 +02001692static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
1693 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694{
1695 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1696
Takashi Iwai0ba21762007-04-16 11:29:14 +02001697 ucontrol->value.integer.value[0] = codec->spdif_ctls & AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 return 0;
1699}
1700
Takashi Iwai0ba21762007-04-16 11:29:14 +02001701static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
1702 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703{
1704 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1705 hda_nid_t nid = kcontrol->private_value;
1706 unsigned short val;
1707 int change;
1708
Ingo Molnar62932df2006-01-16 16:34:20 +01001709 mutex_lock(&codec->spdif_mutex);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001710 val = codec->spdif_ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02001712 val |= AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 change = codec->spdif_ctls != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001714 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 codec->spdif_ctls = val;
Takashi Iwai2f728532008-09-25 16:32:41 +02001716 set_dig_out_convert(codec, nid, val & 0xff, -1);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001717 /* unmute amp switch (if any) */
1718 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
Takashi Iwai47fd8302007-08-10 17:11:07 +02001719 (val & AC_DIG1_ENABLE))
1720 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
1721 HDA_AMP_MUTE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 }
Ingo Molnar62932df2006-01-16 16:34:20 +01001723 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 return change;
1725}
1726
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001727static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 {
1729 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1730 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1731 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
1732 .info = snd_hda_spdif_mask_info,
1733 .get = snd_hda_spdif_cmask_get,
1734 },
1735 {
1736 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1737 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1738 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
1739 .info = snd_hda_spdif_mask_info,
1740 .get = snd_hda_spdif_pmask_get,
1741 },
1742 {
1743 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1744 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1745 .info = snd_hda_spdif_mask_info,
1746 .get = snd_hda_spdif_default_get,
1747 .put = snd_hda_spdif_default_put,
1748 },
1749 {
1750 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1751 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
1752 .info = snd_hda_spdif_out_switch_info,
1753 .get = snd_hda_spdif_out_switch_get,
1754 .put = snd_hda_spdif_out_switch_put,
1755 },
1756 { } /* end */
1757};
1758
Takashi Iwai09f99702008-02-04 12:31:13 +01001759#define SPDIF_MAX_IDX 4 /* 4 instances should be enough to probe */
1760
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761/**
1762 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
1763 * @codec: the HDA codec
1764 * @nid: audio out widget NID
1765 *
1766 * Creates controls related with the SPDIF output.
1767 * Called from each patch supporting the SPDIF out.
1768 *
1769 * Returns 0 if successful, or a negative error code.
1770 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02001771int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772{
1773 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001774 struct snd_kcontrol *kctl;
1775 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01001776 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
Takashi Iwai09f99702008-02-04 12:31:13 +01001778 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
1779 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Playback Switch",
1780 idx))
1781 break;
1782 }
1783 if (idx >= SPDIF_MAX_IDX) {
1784 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
1785 return -EBUSY;
1786 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
1788 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01001789 if (!kctl)
1790 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01001791 kctl->id.index = idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 kctl->private_value = nid;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001793 err = snd_hda_ctl_add(codec, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001794 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 return err;
1796 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02001797 codec->spdif_ctls =
Andrew Paprocki3982d172007-12-19 12:13:44 +01001798 snd_hda_codec_read(codec, nid, 0,
1799 AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
1801 return 0;
1802}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001803EXPORT_SYMBOL_HDA(snd_hda_create_spdif_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
1805/*
Takashi Iwai9a081602008-02-12 18:37:26 +01001806 * SPDIF sharing with analog output
1807 */
1808static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
1809 struct snd_ctl_elem_value *ucontrol)
1810{
1811 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
1812 ucontrol->value.integer.value[0] = mout->share_spdif;
1813 return 0;
1814}
1815
1816static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
1817 struct snd_ctl_elem_value *ucontrol)
1818{
1819 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
1820 mout->share_spdif = !!ucontrol->value.integer.value[0];
1821 return 0;
1822}
1823
1824static struct snd_kcontrol_new spdif_share_sw = {
1825 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1826 .name = "IEC958 Default PCM Playback Switch",
1827 .info = snd_ctl_boolean_mono_info,
1828 .get = spdif_share_sw_get,
1829 .put = spdif_share_sw_put,
1830};
1831
1832int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
1833 struct hda_multi_out *mout)
1834{
1835 if (!mout->dig_out_nid)
1836 return 0;
1837 /* ATTENTION: here mout is passed as private_data, instead of codec */
Takashi Iwaid13bd412008-07-30 15:01:45 +02001838 return snd_hda_ctl_add(codec,
Takashi Iwai9a081602008-02-12 18:37:26 +01001839 snd_ctl_new1(&spdif_share_sw, mout));
1840}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001841EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01001842
1843/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 * SPDIF input
1845 */
1846
1847#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
1848
Takashi Iwai0ba21762007-04-16 11:29:14 +02001849static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
1850 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851{
1852 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1853
1854 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
1855 return 0;
1856}
1857
Takashi Iwai0ba21762007-04-16 11:29:14 +02001858static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
1859 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860{
1861 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1862 hda_nid_t nid = kcontrol->private_value;
1863 unsigned int val = !!ucontrol->value.integer.value[0];
1864 int change;
1865
Ingo Molnar62932df2006-01-16 16:34:20 +01001866 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001868 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001870 snd_hda_codec_write_cache(codec, nid, 0,
1871 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 }
Ingo Molnar62932df2006-01-16 16:34:20 +01001873 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 return change;
1875}
1876
Takashi Iwai0ba21762007-04-16 11:29:14 +02001877static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
1878 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879{
1880 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1881 hda_nid_t nid = kcontrol->private_value;
1882 unsigned short val;
1883 unsigned int sbits;
1884
Andrew Paprocki3982d172007-12-19 12:13:44 +01001885 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 sbits = convert_to_spdif_status(val);
1887 ucontrol->value.iec958.status[0] = sbits;
1888 ucontrol->value.iec958.status[1] = sbits >> 8;
1889 ucontrol->value.iec958.status[2] = sbits >> 16;
1890 ucontrol->value.iec958.status[3] = sbits >> 24;
1891 return 0;
1892}
1893
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001894static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 {
1896 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1897 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH),
1898 .info = snd_hda_spdif_in_switch_info,
1899 .get = snd_hda_spdif_in_switch_get,
1900 .put = snd_hda_spdif_in_switch_put,
1901 },
1902 {
1903 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1904 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1905 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT),
1906 .info = snd_hda_spdif_mask_info,
1907 .get = snd_hda_spdif_in_status_get,
1908 },
1909 { } /* end */
1910};
1911
1912/**
1913 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
1914 * @codec: the HDA codec
1915 * @nid: audio in widget NID
1916 *
1917 * Creates controls related with the SPDIF input.
1918 * Called from each patch supporting the SPDIF in.
1919 *
1920 * Returns 0 if successful, or a negative error code.
1921 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02001922int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923{
1924 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001925 struct snd_kcontrol *kctl;
1926 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01001927 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928
Takashi Iwai09f99702008-02-04 12:31:13 +01001929 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
1930 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Capture Switch",
1931 idx))
1932 break;
1933 }
1934 if (idx >= SPDIF_MAX_IDX) {
1935 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
1936 return -EBUSY;
1937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
1939 kctl = snd_ctl_new1(dig_mix, codec);
1940 kctl->private_value = nid;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001941 err = snd_hda_ctl_add(codec, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001942 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 return err;
1944 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02001945 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01001946 snd_hda_codec_read(codec, nid, 0,
1947 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02001948 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 return 0;
1950}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001951EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
Takashi Iwaicb53c622007-08-10 17:21:45 +02001953#ifdef SND_HDA_NEEDS_RESUME
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001954/*
1955 * command cache
1956 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001958/* build a 32bit cache key with the widget id and the command parameter */
1959#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
1960#define get_cmd_cache_nid(key) ((key) & 0xff)
1961#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
1962
1963/**
1964 * snd_hda_codec_write_cache - send a single command with caching
1965 * @codec: the HDA codec
1966 * @nid: NID to send the command
1967 * @direct: direct flag
1968 * @verb: the verb to send
1969 * @parm: the parameter for the verb
1970 *
1971 * Send a single command without waiting for response.
1972 *
1973 * Returns 0 if successful, or a negative error code.
1974 */
1975int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
1976 int direct, unsigned int verb, unsigned int parm)
1977{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01001978 struct hda_bus *bus = codec->bus;
1979 unsigned int res;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001980 int err;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01001981
1982 res = make_codec_cmd(codec, nid, direct, verb, parm);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001983 snd_hda_power_up(codec);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01001984 mutex_lock(&bus->cmd_mutex);
1985 err = bus->ops.command(bus, res);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001986 if (!err) {
1987 struct hda_cache_head *c;
1988 u32 key = build_cmd_cache_key(nid, verb);
1989 c = get_alloc_hash(&codec->cmd_cache, key);
1990 if (c)
1991 c->val = parm;
1992 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01001993 mutex_unlock(&bus->cmd_mutex);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001994 snd_hda_power_down(codec);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001995 return err;
1996}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001997EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001998
1999/* resume the all commands from the cache */
2000void snd_hda_codec_resume_cache(struct hda_codec *codec)
2001{
Takashi Iwai603c4012008-07-30 15:01:44 +02002002 struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002003 int i;
2004
Takashi Iwai603c4012008-07-30 15:01:44 +02002005 for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002006 u32 key = buffer->key;
2007 if (!key)
2008 continue;
2009 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
2010 get_cmd_cache_cmd(key), buffer->val);
2011 }
2012}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002013EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002014
2015/**
2016 * snd_hda_sequence_write_cache - sequence writes with caching
2017 * @codec: the HDA codec
2018 * @seq: VERB array to send
2019 *
2020 * Send the commands sequentially from the given array.
2021 * Thte commands are recorded on cache for power-save and resume.
2022 * The array must be terminated with NID=0.
2023 */
2024void snd_hda_sequence_write_cache(struct hda_codec *codec,
2025 const struct hda_verb *seq)
2026{
2027 for (; seq->nid; seq++)
2028 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
2029 seq->param);
2030}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002031EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002032#endif /* SND_HDA_NEEDS_RESUME */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002033
Takashi Iwai54d17402005-11-21 16:33:22 +01002034/*
2035 * set power state of the codec
2036 */
2037static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2038 unsigned int power_state)
2039{
Takashi Iwaicb53c622007-08-10 17:21:45 +02002040 hda_nid_t nid;
2041 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01002042
2043 snd_hda_codec_write(codec, fg, 0, AC_VERB_SET_POWER_STATE,
2044 power_state);
Marc Boucherd2595d82008-01-22 15:23:30 +01002045 msleep(10); /* partial workaround for "azx_get_response timeout" */
Takashi Iwai54d17402005-11-21 16:33:22 +01002046
Takashi Iwaicb53c622007-08-10 17:21:45 +02002047 nid = codec->start_nid;
2048 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01002049 unsigned int wcaps = get_wcaps(codec, nid);
2050 if (wcaps & AC_WCAP_POWER) {
2051 unsigned int wid_type = (wcaps & AC_WCAP_TYPE) >>
2052 AC_WCAP_TYPE_SHIFT;
2053 if (wid_type == AC_WID_PIN) {
2054 unsigned int pincap;
2055 /*
2056 * don't power down the widget if it controls
2057 * eapd and EAPD_BTLENABLE is set.
2058 */
2059 pincap = snd_hda_param_read(codec, nid,
2060 AC_PAR_PIN_CAP);
2061 if (pincap & AC_PINCAP_EAPD) {
2062 int eapd = snd_hda_codec_read(codec,
2063 nid, 0,
2064 AC_VERB_GET_EAPD_BTLENABLE, 0);
2065 eapd &= 0x02;
2066 if (power_state == AC_PWRST_D3 && eapd)
2067 continue;
2068 }
Takashi Iwai1194b5b2007-10-10 10:04:26 +02002069 }
Takashi Iwai54d17402005-11-21 16:33:22 +01002070 snd_hda_codec_write(codec, nid, 0,
2071 AC_VERB_SET_POWER_STATE,
2072 power_state);
Takashi Iwai1194b5b2007-10-10 10:04:26 +02002073 }
Takashi Iwai54d17402005-11-21 16:33:22 +01002074 }
2075
Takashi Iwaicb53c622007-08-10 17:21:45 +02002076 if (power_state == AC_PWRST_D0) {
2077 unsigned long end_time;
2078 int state;
Takashi Iwai54d17402005-11-21 16:33:22 +01002079 msleep(10);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002080 /* wait until the codec reachs to D0 */
2081 end_time = jiffies + msecs_to_jiffies(500);
2082 do {
2083 state = snd_hda_codec_read(codec, fg, 0,
2084 AC_VERB_GET_POWER_STATE, 0);
2085 if (state == power_state)
2086 break;
2087 msleep(1);
2088 } while (time_after_eq(end_time, jiffies));
2089 }
Takashi Iwai54d17402005-11-21 16:33:22 +01002090}
2091
Takashi Iwai11aeff02008-07-30 15:01:46 +02002092#ifdef CONFIG_SND_HDA_HWDEP
2093/* execute additional init verbs */
2094static void hda_exec_init_verbs(struct hda_codec *codec)
2095{
2096 if (codec->init_verbs.list)
2097 snd_hda_sequence_write(codec, codec->init_verbs.list);
2098}
2099#else
2100static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
2101#endif
2102
Takashi Iwaicb53c622007-08-10 17:21:45 +02002103#ifdef SND_HDA_NEEDS_RESUME
2104/*
2105 * call suspend and power-down; used both from PM and power-save
2106 */
2107static void hda_call_codec_suspend(struct hda_codec *codec)
2108{
2109 if (codec->patch_ops.suspend)
2110 codec->patch_ops.suspend(codec, PMSG_SUSPEND);
2111 hda_set_power_state(codec,
2112 codec->afg ? codec->afg : codec->mfg,
2113 AC_PWRST_D3);
2114#ifdef CONFIG_SND_HDA_POWER_SAVE
2115 cancel_delayed_work(&codec->power_work);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02002116 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02002117 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002118#endif
2119}
2120
2121/*
2122 * kick up codec; used both from PM and power-save
2123 */
2124static void hda_call_codec_resume(struct hda_codec *codec)
2125{
2126 hda_set_power_state(codec,
2127 codec->afg ? codec->afg : codec->mfg,
2128 AC_PWRST_D0);
Takashi Iwai11aeff02008-07-30 15:01:46 +02002129 hda_exec_init_verbs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002130 if (codec->patch_ops.resume)
2131 codec->patch_ops.resume(codec);
2132 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02002133 if (codec->patch_ops.init)
2134 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002135 snd_hda_codec_resume_amp(codec);
2136 snd_hda_codec_resume_cache(codec);
2137 }
2138}
2139#endif /* SND_HDA_NEEDS_RESUME */
2140
Takashi Iwai54d17402005-11-21 16:33:22 +01002141
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142/**
2143 * snd_hda_build_controls - build mixer controls
2144 * @bus: the BUS
2145 *
2146 * Creates mixer controls for each codec included in the bus.
2147 *
2148 * Returns 0 if successful, otherwise a negative error code.
2149 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002150int /*__devinit*/ snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151{
Takashi Iwai0ba21762007-04-16 11:29:14 +02002152 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153
Takashi Iwai0ba21762007-04-16 11:29:14 +02002154 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002155 int err = snd_hda_codec_build_controls(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 if (err < 0)
2157 return err;
2158 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002159 return 0;
2160}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002161EXPORT_SYMBOL_HDA(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002163int snd_hda_codec_build_controls(struct hda_codec *codec)
2164{
2165 int err = 0;
2166 /* fake as if already powered-on */
2167 hda_keep_power_on(codec);
2168 /* then fire up */
2169 hda_set_power_state(codec,
2170 codec->afg ? codec->afg : codec->mfg,
2171 AC_PWRST_D0);
Takashi Iwai11aeff02008-07-30 15:01:46 +02002172 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002173 /* continue to initialize... */
2174 if (codec->patch_ops.init)
2175 err = codec->patch_ops.init(codec);
2176 if (!err && codec->patch_ops.build_controls)
2177 err = codec->patch_ops.build_controls(codec);
2178 snd_hda_power_down(codec);
2179 if (err < 0)
2180 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 return 0;
2182}
2183
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184/*
2185 * stream formats
2186 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02002187struct hda_rate_tbl {
2188 unsigned int hz;
2189 unsigned int alsa_bits;
2190 unsigned int hda_fmt;
2191};
2192
2193static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02002195
2196 /* autodetected value used in snd_hda_query_supported_pcm */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 { 8000, SNDRV_PCM_RATE_8000, 0x0500 }, /* 1/6 x 48 */
2198 { 11025, SNDRV_PCM_RATE_11025, 0x4300 }, /* 1/4 x 44 */
2199 { 16000, SNDRV_PCM_RATE_16000, 0x0200 }, /* 1/3 x 48 */
2200 { 22050, SNDRV_PCM_RATE_22050, 0x4100 }, /* 1/2 x 44 */
2201 { 32000, SNDRV_PCM_RATE_32000, 0x0a00 }, /* 2/3 x 48 */
2202 { 44100, SNDRV_PCM_RATE_44100, 0x4000 }, /* 44 */
2203 { 48000, SNDRV_PCM_RATE_48000, 0x0000 }, /* 48 */
2204 { 88200, SNDRV_PCM_RATE_88200, 0x4800 }, /* 2 x 44 */
2205 { 96000, SNDRV_PCM_RATE_96000, 0x0800 }, /* 2 x 48 */
2206 { 176400, SNDRV_PCM_RATE_176400, 0x5800 },/* 4 x 44 */
2207 { 192000, SNDRV_PCM_RATE_192000, 0x1800 }, /* 4 x 48 */
Takashi Iwaia961f9f2007-04-12 13:08:09 +02002208#define AC_PAR_PCM_RATE_BITS 11
2209 /* up to bits 10, 384kHZ isn't supported properly */
2210
2211 /* not autodetected value */
2212 { 9600, SNDRV_PCM_RATE_KNOT, 0x0400 }, /* 1/5 x 48 */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02002213
Takashi Iwaibefdf312005-08-22 13:57:55 +02002214 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215};
2216
2217/**
2218 * snd_hda_calc_stream_format - calculate format bitset
2219 * @rate: the sample rate
2220 * @channels: the number of channels
2221 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
2222 * @maxbps: the max. bps
2223 *
2224 * Calculate the format bitset from the given rate, channels and th PCM format.
2225 *
2226 * Return zero if invalid.
2227 */
2228unsigned int snd_hda_calc_stream_format(unsigned int rate,
2229 unsigned int channels,
2230 unsigned int format,
2231 unsigned int maxbps)
2232{
2233 int i;
2234 unsigned int val = 0;
2235
Takashi Iwaibefdf312005-08-22 13:57:55 +02002236 for (i = 0; rate_bits[i].hz; i++)
2237 if (rate_bits[i].hz == rate) {
2238 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 break;
2240 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002241 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 snd_printdd("invalid rate %d\n", rate);
2243 return 0;
2244 }
2245
2246 if (channels == 0 || channels > 8) {
2247 snd_printdd("invalid channels %d\n", channels);
2248 return 0;
2249 }
2250 val |= channels - 1;
2251
2252 switch (snd_pcm_format_width(format)) {
2253 case 8: val |= 0x00; break;
2254 case 16: val |= 0x10; break;
2255 case 20:
2256 case 24:
2257 case 32:
2258 if (maxbps >= 32)
2259 val |= 0x40;
2260 else if (maxbps >= 24)
2261 val |= 0x30;
2262 else
2263 val |= 0x20;
2264 break;
2265 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002266 snd_printdd("invalid format width %d\n",
2267 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 return 0;
2269 }
2270
2271 return val;
2272}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002273EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274
2275/**
2276 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
2277 * @codec: the HDA codec
2278 * @nid: NID to query
2279 * @ratesp: the pointer to store the detected rate bitflags
2280 * @formatsp: the pointer to store the detected formats
2281 * @bpsp: the pointer to store the detected format widths
2282 *
2283 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
2284 * or @bsps argument is ignored.
2285 *
2286 * Returns 0 if successful, otherwise a negative error code.
2287 */
Takashi Iwai986862bd2008-11-27 12:40:13 +01002288static int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
2290{
2291 int i;
2292 unsigned int val, streams;
2293
2294 val = 0;
2295 if (nid != codec->afg &&
Takashi Iwai54d17402005-11-21 16:33:22 +01002296 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
2298 if (val == -1)
2299 return -EIO;
2300 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002301 if (!val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
2303
2304 if (ratesp) {
2305 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02002306 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02002308 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 }
2310 *ratesp = rates;
2311 }
2312
2313 if (formatsp || bpsp) {
2314 u64 formats = 0;
2315 unsigned int bps;
2316 unsigned int wcaps;
2317
Takashi Iwai54d17402005-11-21 16:33:22 +01002318 wcaps = get_wcaps(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
2320 if (streams == -1)
2321 return -EIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002322 if (!streams) {
2323 streams = snd_hda_param_read(codec, codec->afg,
2324 AC_PAR_STREAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 if (streams == -1)
2326 return -EIO;
2327 }
2328
2329 bps = 0;
2330 if (streams & AC_SUPFMT_PCM) {
2331 if (val & AC_SUPPCM_BITS_8) {
2332 formats |= SNDRV_PCM_FMTBIT_U8;
2333 bps = 8;
2334 }
2335 if (val & AC_SUPPCM_BITS_16) {
2336 formats |= SNDRV_PCM_FMTBIT_S16_LE;
2337 bps = 16;
2338 }
2339 if (wcaps & AC_WCAP_DIGITAL) {
2340 if (val & AC_SUPPCM_BITS_32)
2341 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
2342 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
2343 formats |= SNDRV_PCM_FMTBIT_S32_LE;
2344 if (val & AC_SUPPCM_BITS_24)
2345 bps = 24;
2346 else if (val & AC_SUPPCM_BITS_20)
2347 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002348 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
2349 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 formats |= SNDRV_PCM_FMTBIT_S32_LE;
2351 if (val & AC_SUPPCM_BITS_32)
2352 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 else if (val & AC_SUPPCM_BITS_24)
2354 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02002355 else if (val & AC_SUPPCM_BITS_20)
2356 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 }
2358 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002359 else if (streams == AC_SUPFMT_FLOAT32) {
2360 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
2362 bps = 32;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002363 } else if (streams == AC_SUPFMT_AC3) {
2364 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 /* temporary hack: we have still no proper support
2366 * for the direct AC3 stream...
2367 */
2368 formats |= SNDRV_PCM_FMTBIT_U8;
2369 bps = 8;
2370 }
2371 if (formatsp)
2372 *formatsp = formats;
2373 if (bpsp)
2374 *bpsp = bps;
2375 }
2376
2377 return 0;
2378}
2379
2380/**
Takashi Iwai0ba21762007-04-16 11:29:14 +02002381 * snd_hda_is_supported_format - check whether the given node supports
2382 * the format val
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 *
2384 * Returns 1 if supported, 0 if not.
2385 */
2386int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
2387 unsigned int format)
2388{
2389 int i;
2390 unsigned int val = 0, rate, stream;
2391
2392 if (nid != codec->afg &&
Takashi Iwai54d17402005-11-21 16:33:22 +01002393 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
2395 if (val == -1)
2396 return 0;
2397 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002398 if (!val) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
2400 if (val == -1)
2401 return 0;
2402 }
2403
2404 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02002405 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02002406 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 if (val & (1 << i))
2408 break;
2409 return 0;
2410 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02002411 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 return 0;
2413
2414 stream = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
2415 if (stream == -1)
2416 return 0;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002417 if (!stream && nid != codec->afg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 stream = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002419 if (!stream || stream == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 return 0;
2421
2422 if (stream & AC_SUPFMT_PCM) {
2423 switch (format & 0xf0) {
2424 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002425 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 return 0;
2427 break;
2428 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002429 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 return 0;
2431 break;
2432 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002433 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 return 0;
2435 break;
2436 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002437 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 return 0;
2439 break;
2440 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02002441 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 return 0;
2443 break;
2444 default:
2445 return 0;
2446 }
2447 } else {
2448 /* FIXME: check for float32 and AC3? */
2449 }
2450
2451 return 1;
2452}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002453EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454
2455/*
2456 * PCM stuff
2457 */
2458static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
2459 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002460 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461{
2462 return 0;
2463}
2464
2465static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
2466 struct hda_codec *codec,
2467 unsigned int stream_tag,
2468 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002469 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470{
2471 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
2472 return 0;
2473}
2474
2475static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
2476 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002477 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478{
Takashi Iwai888afa12008-03-18 09:57:50 +01002479 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 return 0;
2481}
2482
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002483static int set_pcm_default_values(struct hda_codec *codec,
2484 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485{
Takashi Iwai0ba21762007-04-16 11:29:14 +02002486 /* query support PCM information from the given NID */
2487 if (info->nid && (!info->rates || !info->formats)) {
2488 snd_hda_query_supported_pcm(codec, info->nid,
2489 info->rates ? NULL : &info->rates,
2490 info->formats ? NULL : &info->formats,
2491 info->maxbps ? NULL : &info->maxbps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 }
2493 if (info->ops.open == NULL)
2494 info->ops.open = hda_pcm_default_open_close;
2495 if (info->ops.close == NULL)
2496 info->ops.close = hda_pcm_default_open_close;
2497 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02002498 if (snd_BUG_ON(!info->nid))
2499 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 info->ops.prepare = hda_pcm_default_prepare;
2501 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02002503 if (snd_BUG_ON(!info->nid))
2504 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 info->ops.cleanup = hda_pcm_default_cleanup;
2506 }
2507 return 0;
2508}
2509
Takashi Iwai176d5332008-07-30 15:01:44 +02002510/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002511 * get the empty PCM device number to assign
2512 */
2513static int get_empty_pcm_device(struct hda_bus *bus, int type)
2514{
2515 static const char *dev_name[HDA_PCM_NTYPES] = {
2516 "Audio", "SPDIF", "HDMI", "Modem"
2517 };
2518 /* starting device index for each PCM type */
2519 static int dev_idx[HDA_PCM_NTYPES] = {
2520 [HDA_PCM_TYPE_AUDIO] = 0,
2521 [HDA_PCM_TYPE_SPDIF] = 1,
2522 [HDA_PCM_TYPE_HDMI] = 3,
2523 [HDA_PCM_TYPE_MODEM] = 6
2524 };
2525 /* normal audio device indices; not linear to keep compatibility */
2526 static int audio_idx[4] = { 0, 2, 4, 5 };
2527 int i, dev;
2528
2529 switch (type) {
2530 case HDA_PCM_TYPE_AUDIO:
2531 for (i = 0; i < ARRAY_SIZE(audio_idx); i++) {
2532 dev = audio_idx[i];
2533 if (!test_bit(dev, bus->pcm_dev_bits))
2534 break;
2535 }
2536 if (i >= ARRAY_SIZE(audio_idx)) {
2537 snd_printk(KERN_WARNING "Too many audio devices\n");
2538 return -EAGAIN;
2539 }
2540 break;
2541 case HDA_PCM_TYPE_SPDIF:
2542 case HDA_PCM_TYPE_HDMI:
2543 case HDA_PCM_TYPE_MODEM:
2544 dev = dev_idx[type];
2545 if (test_bit(dev, bus->pcm_dev_bits)) {
2546 snd_printk(KERN_WARNING "%s already defined\n",
2547 dev_name[type]);
2548 return -EAGAIN;
2549 }
2550 break;
2551 default:
2552 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
2553 return -EINVAL;
2554 }
2555 set_bit(dev, bus->pcm_dev_bits);
2556 return dev;
2557}
2558
2559/*
Takashi Iwai176d5332008-07-30 15:01:44 +02002560 * attach a new PCM stream
2561 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002562static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02002563{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01002564 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02002565 struct hda_pcm_stream *info;
2566 int stream, err;
2567
Takashi Iwaib91f0802008-11-04 08:43:08 +01002568 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02002569 return -EINVAL;
2570 for (stream = 0; stream < 2; stream++) {
2571 info = &pcm->stream[stream];
2572 if (info->substreams) {
2573 err = set_pcm_default_values(codec, info);
2574 if (err < 0)
2575 return err;
2576 }
2577 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01002578 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02002579}
2580
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002581/* assign all PCMs of the given codec */
2582int snd_hda_codec_build_pcms(struct hda_codec *codec)
2583{
2584 unsigned int pcm;
2585 int err;
2586
2587 if (!codec->num_pcms) {
2588 if (!codec->patch_ops.build_pcms)
2589 return 0;
2590 err = codec->patch_ops.build_pcms(codec);
2591 if (err < 0)
2592 return err;
2593 }
2594 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2595 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2596 int dev;
2597
2598 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
2599 return 0; /* no substreams assigned */
2600
2601 if (!cpcm->pcm) {
2602 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
2603 if (dev < 0)
2604 return 0;
2605 cpcm->device = dev;
2606 err = snd_hda_attach_pcm(codec, cpcm);
2607 if (err < 0)
2608 return err;
2609 }
2610 }
2611 return 0;
2612}
2613
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614/**
2615 * snd_hda_build_pcms - build PCM information
2616 * @bus: the BUS
2617 *
2618 * Create PCM information for each codec included in the bus.
2619 *
2620 * The build_pcms codec patch is requested to set up codec->num_pcms and
2621 * codec->pcm_info properly. The array is referred by the top-level driver
2622 * to create its PCM instances.
2623 * The allocated codec->pcm_info should be released in codec->patch_ops.free
2624 * callback.
2625 *
2626 * At least, substreams, channels_min and channels_max must be filled for
2627 * each stream. substreams = 0 indicates that the stream doesn't exist.
2628 * When rates and/or formats are zero, the supported values are queried
2629 * from the given nid. The nid is used also by the default ops.prepare
2630 * and ops.cleanup callbacks.
2631 *
2632 * The driver needs to call ops.open in its open callback. Similarly,
2633 * ops.close is supposed to be called in the close callback.
2634 * ops.prepare should be called in the prepare or hw_params callback
2635 * with the proper parameters for set up.
2636 * ops.cleanup should be called in hw_free for clean up of streams.
2637 *
2638 * This function returns 0 if successfull, or a negative error code.
2639 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002640int __devinit snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641{
Takashi Iwai0ba21762007-04-16 11:29:14 +02002642 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643
Takashi Iwai0ba21762007-04-16 11:29:14 +02002644 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002645 int err = snd_hda_codec_build_pcms(codec);
2646 if (err < 0)
2647 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 }
2649 return 0;
2650}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002651EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653/**
2654 * snd_hda_check_board_config - compare the current codec with the config table
2655 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002656 * @num_configs: number of config enums
2657 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 * @tbl: configuration table, terminated by null entries
2659 *
2660 * Compares the modelname or PCI subsystem id of the current codec with the
2661 * given configuration table. If a matching entry is found, returns its
2662 * config value (supposed to be 0 or positive).
2663 *
2664 * If no entries are matching, the function returns a negative value.
2665 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002666int snd_hda_check_board_config(struct hda_codec *codec,
2667 int num_configs, const char **models,
2668 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669{
Takashi Iwaif44ac832008-07-30 15:01:45 +02002670 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002671 int i;
2672 for (i = 0; i < num_configs; i++) {
2673 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02002674 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002675 snd_printd(KERN_INFO "hda_codec: model '%s' is "
2676 "selected\n", models[i]);
2677 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 }
2679 }
2680 }
2681
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002682 if (!codec->bus->pci || !tbl)
2683 return -1;
2684
2685 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
2686 if (!tbl)
2687 return -1;
2688 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02002689#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002690 char tmp[10];
2691 const char *model = NULL;
2692 if (models)
2693 model = models[tbl->value];
2694 if (!model) {
2695 sprintf(tmp, "#%d", tbl->value);
2696 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01002698 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
2699 "for config %x:%x (%s)\n",
2700 model, tbl->subvendor, tbl->subdevice,
2701 (tbl->name ? tbl->name : "Unknown device"));
2702#endif
2703 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 }
2705 return -1;
2706}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002707EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708
2709/**
2710 * snd_hda_add_new_ctls - create controls from the array
2711 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002712 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 *
2714 * This helper function creates and add new controls in the given array.
2715 * The array must be terminated with an empty entry as terminator.
2716 *
2717 * Returns 0 if successful, or a negative error code.
2718 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002719int snd_hda_add_new_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720{
Takashi Iwaicb53c622007-08-10 17:21:45 +02002721 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722
2723 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01002724 struct snd_kcontrol *kctl;
2725 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002726 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01002727 return -ENOMEM;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002728 err = snd_hda_ctl_add(codec, kctl);
Takashi Iwai54d17402005-11-21 16:33:22 +01002729 if (err < 0) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002730 if (!codec->addr)
Takashi Iwai54d17402005-11-21 16:33:22 +01002731 return err;
2732 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002733 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01002734 return -ENOMEM;
2735 kctl->id.device = codec->addr;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002736 err = snd_hda_ctl_add(codec, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002737 if (err < 0)
Takashi Iwai54d17402005-11-21 16:33:22 +01002738 return err;
2739 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 }
2741 return 0;
2742}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002743EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744
Takashi Iwaicb53c622007-08-10 17:21:45 +02002745#ifdef CONFIG_SND_HDA_POWER_SAVE
2746static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2747 unsigned int power_state);
2748
2749static void hda_power_work(struct work_struct *work)
2750{
2751 struct hda_codec *codec =
2752 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01002753 struct hda_bus *bus = codec->bus;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002754
Maxim Levitsky2e492462007-09-03 15:26:57 +02002755 if (!codec->power_on || codec->power_count) {
2756 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002757 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02002758 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02002759
2760 hda_call_codec_suspend(codec);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01002761 if (bus->ops.pm_notify)
2762 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002763}
2764
2765static void hda_keep_power_on(struct hda_codec *codec)
2766{
2767 codec->power_count++;
2768 codec->power_on = 1;
2769}
2770
2771void snd_hda_power_up(struct hda_codec *codec)
2772{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01002773 struct hda_bus *bus = codec->bus;
2774
Takashi Iwaicb53c622007-08-10 17:21:45 +02002775 codec->power_count++;
Takashi Iwaia221e282007-08-16 16:35:33 +02002776 if (codec->power_on || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02002777 return;
2778
2779 codec->power_on = 1;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01002780 if (bus->ops.pm_notify)
2781 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002782 hda_call_codec_resume(codec);
2783 cancel_delayed_work(&codec->power_work);
Takashi Iwaia221e282007-08-16 16:35:33 +02002784 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002785}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002786EXPORT_SYMBOL_HDA(snd_hda_power_up);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002787
2788#define power_save(codec) \
2789 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02002790
Takashi Iwaifee2fba2008-11-27 12:43:28 +01002791#define power_save(codec) \
2792 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
2793
Takashi Iwaicb53c622007-08-10 17:21:45 +02002794void snd_hda_power_down(struct hda_codec *codec)
2795{
2796 --codec->power_count;
Takashi Iwaia221e282007-08-16 16:35:33 +02002797 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02002798 return;
Takashi Iwaifee2fba2008-11-27 12:43:28 +01002799 if (power_save(codec)) {
Takashi Iwaia221e282007-08-16 16:35:33 +02002800 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002801 schedule_delayed_work(&codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01002802 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02002803 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02002804}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002805EXPORT_SYMBOL_HDA(snd_hda_power_down);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002806
2807int snd_hda_check_amp_list_power(struct hda_codec *codec,
2808 struct hda_loopback_check *check,
2809 hda_nid_t nid)
2810{
2811 struct hda_amp_list *p;
2812 int ch, v;
2813
2814 if (!check->amplist)
2815 return 0;
2816 for (p = check->amplist; p->nid; p++) {
2817 if (p->nid == nid)
2818 break;
2819 }
2820 if (!p->nid)
2821 return 0; /* nothing changed */
2822
2823 for (p = check->amplist; p->nid; p++) {
2824 for (ch = 0; ch < 2; ch++) {
2825 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
2826 p->idx);
2827 if (!(v & HDA_AMP_MUTE) && v > 0) {
2828 if (!check->power_on) {
2829 check->power_on = 1;
2830 snd_hda_power_up(codec);
2831 }
2832 return 1;
2833 }
2834 }
2835 }
2836 if (check->power_on) {
2837 check->power_on = 0;
2838 snd_hda_power_down(codec);
2839 }
2840 return 0;
2841}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002842EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002843#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002845/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002846 * Channel mode helper
2847 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002848int snd_hda_ch_mode_info(struct hda_codec *codec,
2849 struct snd_ctl_elem_info *uinfo,
2850 const struct hda_channel_mode *chmode,
2851 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002852{
2853 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2854 uinfo->count = 1;
2855 uinfo->value.enumerated.items = num_chmodes;
2856 if (uinfo->value.enumerated.item >= num_chmodes)
2857 uinfo->value.enumerated.item = num_chmodes - 1;
2858 sprintf(uinfo->value.enumerated.name, "%dch",
2859 chmode[uinfo->value.enumerated.item].channels);
2860 return 0;
2861}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002862EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002863
Takashi Iwai0ba21762007-04-16 11:29:14 +02002864int snd_hda_ch_mode_get(struct hda_codec *codec,
2865 struct snd_ctl_elem_value *ucontrol,
2866 const struct hda_channel_mode *chmode,
2867 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002868 int max_channels)
2869{
2870 int i;
2871
2872 for (i = 0; i < num_chmodes; i++) {
2873 if (max_channels == chmode[i].channels) {
2874 ucontrol->value.enumerated.item[0] = i;
2875 break;
2876 }
2877 }
2878 return 0;
2879}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002880EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002881
Takashi Iwai0ba21762007-04-16 11:29:14 +02002882int snd_hda_ch_mode_put(struct hda_codec *codec,
2883 struct snd_ctl_elem_value *ucontrol,
2884 const struct hda_channel_mode *chmode,
2885 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002886 int *max_channelsp)
2887{
2888 unsigned int mode;
2889
2890 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01002891 if (mode >= num_chmodes)
2892 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002893 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002894 return 0;
2895 /* change the current channel setting */
2896 *max_channelsp = chmode[mode].channels;
2897 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002898 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002899 return 1;
2900}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002901EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01002902
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903/*
2904 * input MUX helper
2905 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002906int snd_hda_input_mux_info(const struct hda_input_mux *imux,
2907 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908{
2909 unsigned int index;
2910
2911 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2912 uinfo->count = 1;
2913 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02002914 if (!imux->num_items)
2915 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 index = uinfo->value.enumerated.item;
2917 if (index >= imux->num_items)
2918 index = imux->num_items - 1;
2919 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
2920 return 0;
2921}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002922EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923
Takashi Iwai0ba21762007-04-16 11:29:14 +02002924int snd_hda_input_mux_put(struct hda_codec *codec,
2925 const struct hda_input_mux *imux,
2926 struct snd_ctl_elem_value *ucontrol,
2927 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 unsigned int *cur_val)
2929{
2930 unsigned int idx;
2931
Takashi Iwai5513b0c2007-10-09 11:58:41 +02002932 if (!imux->num_items)
2933 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 idx = ucontrol->value.enumerated.item[0];
2935 if (idx >= imux->num_items)
2936 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002937 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002939 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
2940 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 *cur_val = idx;
2942 return 1;
2943}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002944EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945
2946
2947/*
2948 * Multi-channel / digital-out PCM helper functions
2949 */
2950
Takashi Iwai6b97eb42007-04-05 14:51:48 +02002951/* setup SPDIF output stream */
2952static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
2953 unsigned int stream_tag, unsigned int format)
2954{
2955 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Takashi Iwai2f728532008-09-25 16:32:41 +02002956 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
2957 set_dig_out_convert(codec, nid,
2958 codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff,
2959 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02002960 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02002961 if (codec->slave_dig_outs) {
2962 hda_nid_t *d;
2963 for (d = codec->slave_dig_outs; *d; d++)
2964 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
2965 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04002966 }
Takashi Iwai2f728532008-09-25 16:32:41 +02002967 /* turn on again (if needed) */
2968 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
2969 set_dig_out_convert(codec, nid,
2970 codec->spdif_ctls & 0xff, -1);
2971}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04002972
Takashi Iwai2f728532008-09-25 16:32:41 +02002973static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
2974{
2975 snd_hda_codec_cleanup_stream(codec, nid);
2976 if (codec->slave_dig_outs) {
2977 hda_nid_t *d;
2978 for (d = codec->slave_dig_outs; *d; d++)
2979 snd_hda_codec_cleanup_stream(codec, *d);
2980 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02002981}
2982
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983/*
2984 * open the digital out in the exclusive mode
2985 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002986int snd_hda_multi_out_dig_open(struct hda_codec *codec,
2987 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988{
Ingo Molnar62932df2006-01-16 16:34:20 +01002989 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02002990 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
2991 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02002992 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01002994 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 return 0;
2996}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002997EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998
Takashi Iwai6b97eb42007-04-05 14:51:48 +02002999int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
3000 struct hda_multi_out *mout,
3001 unsigned int stream_tag,
3002 unsigned int format,
3003 struct snd_pcm_substream *substream)
3004{
3005 mutex_lock(&codec->spdif_mutex);
3006 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
3007 mutex_unlock(&codec->spdif_mutex);
3008 return 0;
3009}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003010EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003011
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012/*
3013 * release the digital out
3014 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003015int snd_hda_multi_out_dig_close(struct hda_codec *codec,
3016 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017{
Ingo Molnar62932df2006-01-16 16:34:20 +01003018 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01003020 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 return 0;
3022}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003023EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024
3025/*
3026 * set up more restrictions for analog out
3027 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003028int snd_hda_multi_out_analog_open(struct hda_codec *codec,
3029 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01003030 struct snd_pcm_substream *substream,
3031 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032{
Takashi Iwai9a081602008-02-12 18:37:26 +01003033 struct snd_pcm_runtime *runtime = substream->runtime;
3034 runtime->hw.channels_max = mout->max_channels;
3035 if (mout->dig_out_nid) {
3036 if (!mout->analog_rates) {
3037 mout->analog_rates = hinfo->rates;
3038 mout->analog_formats = hinfo->formats;
3039 mout->analog_maxbps = hinfo->maxbps;
3040 } else {
3041 runtime->hw.rates = mout->analog_rates;
3042 runtime->hw.formats = mout->analog_formats;
3043 hinfo->maxbps = mout->analog_maxbps;
3044 }
3045 if (!mout->spdif_rates) {
3046 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
3047 &mout->spdif_rates,
3048 &mout->spdif_formats,
3049 &mout->spdif_maxbps);
3050 }
3051 mutex_lock(&codec->spdif_mutex);
3052 if (mout->share_spdif) {
3053 runtime->hw.rates &= mout->spdif_rates;
3054 runtime->hw.formats &= mout->spdif_formats;
3055 if (mout->spdif_maxbps < hinfo->maxbps)
3056 hinfo->maxbps = mout->spdif_maxbps;
3057 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02003058 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01003059 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 return snd_pcm_hw_constraint_step(substream->runtime, 0,
3061 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
3062}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003063EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064
3065/*
3066 * set up the i/o for analog out
3067 * when the digital out is available, copy the front out to digital out, too.
3068 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003069int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
3070 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 unsigned int stream_tag,
3072 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003073 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074{
3075 hda_nid_t *nids = mout->dac_nids;
3076 int chs = substream->runtime->channels;
3077 int i;
3078
Ingo Molnar62932df2006-01-16 16:34:20 +01003079 mutex_lock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01003080 if (mout->dig_out_nid && mout->share_spdif &&
3081 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02003083 snd_hda_is_supported_format(codec, mout->dig_out_nid,
3084 format) &&
3085 !(codec->spdif_status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003087 setup_dig_out_stream(codec, mout->dig_out_nid,
3088 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 } else {
3090 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02003091 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 }
3093 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003094 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095
3096 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003097 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
3098 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02003099 if (!mout->no_share_stream &&
3100 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003102 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
3103 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01003104 /* extra outputs copied from front */
3105 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02003106 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01003107 snd_hda_codec_setup_stream(codec,
3108 mout->extra_out_nid[i],
3109 stream_tag, 0, format);
3110
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 /* surrounds */
3112 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02003113 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003114 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
3115 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02003116 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003117 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
3118 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 }
3120 return 0;
3121}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003122EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123
3124/*
3125 * clean up the setting for analog out
3126 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003127int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
3128 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129{
3130 hda_nid_t *nids = mout->dac_nids;
3131 int i;
3132
3133 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01003134 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01003136 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwai82bc9552006-03-21 11:24:42 +01003137 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
3138 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01003139 snd_hda_codec_cleanup_stream(codec,
3140 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01003141 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02003143 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 mout->dig_out_used = 0;
3145 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003146 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 return 0;
3148}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003149EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003151/*
Wu Fengguang6b345002008-10-07 14:21:41 +08003152 * Helper for automatic pin configuration
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003153 */
Kailang Yangdf694da2005-12-05 19:42:22 +01003154
Takashi Iwai12f288b2007-08-02 15:51:59 +02003155static int is_in_nid_list(hda_nid_t nid, hda_nid_t *list)
Kailang Yangdf694da2005-12-05 19:42:22 +01003156{
3157 for (; *list; list++)
3158 if (*list == nid)
3159 return 1;
3160 return 0;
3161}
3162
Steve Longerbeam81937d32007-05-08 15:33:03 +02003163
3164/*
3165 * Sort an associated group of pins according to their sequence numbers.
3166 */
3167static void sort_pins_by_sequence(hda_nid_t * pins, short * sequences,
3168 int num_pins)
3169{
3170 int i, j;
3171 short seq;
3172 hda_nid_t nid;
3173
3174 for (i = 0; i < num_pins; i++) {
3175 for (j = i + 1; j < num_pins; j++) {
3176 if (sequences[i] > sequences[j]) {
3177 seq = sequences[i];
3178 sequences[i] = sequences[j];
3179 sequences[j] = seq;
3180 nid = pins[i];
3181 pins[i] = pins[j];
3182 pins[j] = nid;
3183 }
3184 }
3185 }
3186}
3187
3188
Takashi Iwai82bc9552006-03-21 11:24:42 +01003189/*
3190 * Parse all pin widgets and store the useful pin nids to cfg
3191 *
3192 * The number of line-outs or any primary output is stored in line_outs,
3193 * and the corresponding output pins are assigned to line_out_pins[],
3194 * in the order of front, rear, CLFE, side, ...
3195 *
3196 * If more extra outputs (speaker and headphone) are found, the pins are
Takashi Iwaieb06ed82006-09-20 17:10:27 +02003197 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
Takashi Iwai82bc9552006-03-21 11:24:42 +01003198 * is detected, one of speaker of HP pins is assigned as the primary
3199 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
3200 * if any analog output exists.
3201 *
3202 * The analog input pins are assigned to input_pins array.
3203 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
3204 * respectively.
3205 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003206int snd_hda_parse_pin_def_config(struct hda_codec *codec,
3207 struct auto_pin_cfg *cfg,
3208 hda_nid_t *ignore_nids)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003209{
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01003210 hda_nid_t nid, end_nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02003211 short seq, assoc_line_out, assoc_speaker;
3212 short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
3213 short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
Takashi Iwaif889fa92007-10-31 15:49:32 +01003214 short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003215
3216 memset(cfg, 0, sizeof(*cfg));
3217
Steve Longerbeam81937d32007-05-08 15:33:03 +02003218 memset(sequences_line_out, 0, sizeof(sequences_line_out));
3219 memset(sequences_speaker, 0, sizeof(sequences_speaker));
Takashi Iwaif889fa92007-10-31 15:49:32 +01003220 memset(sequences_hp, 0, sizeof(sequences_hp));
Steve Longerbeam81937d32007-05-08 15:33:03 +02003221 assoc_line_out = assoc_speaker = 0;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003222
Takashi Iwai0ef6ce72008-01-22 15:35:37 +01003223 end_nid = codec->start_nid + codec->num_nodes;
3224 for (nid = codec->start_nid; nid < end_nid; nid++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01003225 unsigned int wid_caps = get_wcaps(codec, nid);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003226 unsigned int wid_type =
3227 (wid_caps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003228 unsigned int def_conf;
3229 short assoc, loc;
3230
3231 /* read all default configuration for pin complex */
3232 if (wid_type != AC_WID_PIN)
3233 continue;
Kailang Yangdf694da2005-12-05 19:42:22 +01003234 /* ignore the given nids (e.g. pc-beep returns error) */
3235 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
3236 continue;
3237
Takashi Iwai0ba21762007-04-16 11:29:14 +02003238 def_conf = snd_hda_codec_read(codec, nid, 0,
3239 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003240 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
3241 continue;
3242 loc = get_defcfg_location(def_conf);
3243 switch (get_defcfg_device(def_conf)) {
3244 case AC_JACK_LINE_OUT:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003245 seq = get_defcfg_sequence(def_conf);
3246 assoc = get_defcfg_association(def_conf);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01003247
3248 if (!(wid_caps & AC_WCAP_STEREO))
3249 if (!cfg->mono_out_pin)
3250 cfg->mono_out_pin = nid;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003251 if (!assoc)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003252 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003253 if (!assoc_line_out)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003254 assoc_line_out = assoc;
3255 else if (assoc_line_out != assoc)
3256 continue;
3257 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
3258 continue;
3259 cfg->line_out_pins[cfg->line_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02003260 sequences_line_out[cfg->line_outs] = seq;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003261 cfg->line_outs++;
3262 break;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01003263 case AC_JACK_SPEAKER:
Steve Longerbeam81937d32007-05-08 15:33:03 +02003264 seq = get_defcfg_sequence(def_conf);
3265 assoc = get_defcfg_association(def_conf);
3266 if (! assoc)
3267 continue;
3268 if (! assoc_speaker)
3269 assoc_speaker = assoc;
3270 else if (assoc_speaker != assoc)
3271 continue;
Takashi Iwai82bc9552006-03-21 11:24:42 +01003272 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
3273 continue;
3274 cfg->speaker_pins[cfg->speaker_outs] = nid;
Steve Longerbeam81937d32007-05-08 15:33:03 +02003275 sequences_speaker[cfg->speaker_outs] = seq;
Takashi Iwai82bc9552006-03-21 11:24:42 +01003276 cfg->speaker_outs++;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01003277 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003278 case AC_JACK_HP_OUT:
Takashi Iwaif889fa92007-10-31 15:49:32 +01003279 seq = get_defcfg_sequence(def_conf);
3280 assoc = get_defcfg_association(def_conf);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02003281 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
3282 continue;
3283 cfg->hp_pins[cfg->hp_outs] = nid;
Takashi Iwaif889fa92007-10-31 15:49:32 +01003284 sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
Takashi Iwaieb06ed82006-09-20 17:10:27 +02003285 cfg->hp_outs++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003286 break;
Takashi Iwai314634b2006-09-21 11:56:18 +02003287 case AC_JACK_MIC_IN: {
3288 int preferred, alt;
3289 if (loc == AC_JACK_LOC_FRONT) {
3290 preferred = AUTO_PIN_FRONT_MIC;
3291 alt = AUTO_PIN_MIC;
3292 } else {
3293 preferred = AUTO_PIN_MIC;
3294 alt = AUTO_PIN_FRONT_MIC;
3295 }
3296 if (!cfg->input_pins[preferred])
3297 cfg->input_pins[preferred] = nid;
3298 else if (!cfg->input_pins[alt])
3299 cfg->input_pins[alt] = nid;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003300 break;
Takashi Iwai314634b2006-09-21 11:56:18 +02003301 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003302 case AC_JACK_LINE_IN:
3303 if (loc == AC_JACK_LOC_FRONT)
3304 cfg->input_pins[AUTO_PIN_FRONT_LINE] = nid;
3305 else
3306 cfg->input_pins[AUTO_PIN_LINE] = nid;
3307 break;
3308 case AC_JACK_CD:
3309 cfg->input_pins[AUTO_PIN_CD] = nid;
3310 break;
3311 case AC_JACK_AUX:
3312 cfg->input_pins[AUTO_PIN_AUX] = nid;
3313 break;
3314 case AC_JACK_SPDIF_OUT:
3315 cfg->dig_out_pin = nid;
3316 break;
3317 case AC_JACK_SPDIF_IN:
3318 cfg->dig_in_pin = nid;
3319 break;
3320 }
3321 }
3322
Takashi Iwai5832fcf2008-02-12 18:30:12 +01003323 /* FIX-UP:
3324 * If no line-out is defined but multiple HPs are found,
3325 * some of them might be the real line-outs.
3326 */
3327 if (!cfg->line_outs && cfg->hp_outs > 1) {
3328 int i = 0;
3329 while (i < cfg->hp_outs) {
3330 /* The real HPs should have the sequence 0x0f */
3331 if ((sequences_hp[i] & 0x0f) == 0x0f) {
3332 i++;
3333 continue;
3334 }
3335 /* Move it to the line-out table */
3336 cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
3337 sequences_line_out[cfg->line_outs] = sequences_hp[i];
3338 cfg->line_outs++;
3339 cfg->hp_outs--;
3340 memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
3341 sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
3342 memmove(sequences_hp + i - 1, sequences_hp + i,
3343 sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
3344 }
3345 }
3346
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003347 /* sort by sequence */
Steve Longerbeam81937d32007-05-08 15:33:03 +02003348 sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
3349 cfg->line_outs);
3350 sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
3351 cfg->speaker_outs);
Takashi Iwaif889fa92007-10-31 15:49:32 +01003352 sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
3353 cfg->hp_outs);
Steve Longerbeam81937d32007-05-08 15:33:03 +02003354
Takashi Iwaif889fa92007-10-31 15:49:32 +01003355 /* if we have only one mic, make it AUTO_PIN_MIC */
3356 if (!cfg->input_pins[AUTO_PIN_MIC] &&
3357 cfg->input_pins[AUTO_PIN_FRONT_MIC]) {
3358 cfg->input_pins[AUTO_PIN_MIC] =
3359 cfg->input_pins[AUTO_PIN_FRONT_MIC];
3360 cfg->input_pins[AUTO_PIN_FRONT_MIC] = 0;
3361 }
3362 /* ditto for line-in */
3363 if (!cfg->input_pins[AUTO_PIN_LINE] &&
3364 cfg->input_pins[AUTO_PIN_FRONT_LINE]) {
3365 cfg->input_pins[AUTO_PIN_LINE] =
3366 cfg->input_pins[AUTO_PIN_FRONT_LINE];
3367 cfg->input_pins[AUTO_PIN_FRONT_LINE] = 0;
3368 }
3369
Steve Longerbeam81937d32007-05-08 15:33:03 +02003370 /*
3371 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
3372 * as a primary output
3373 */
3374 if (!cfg->line_outs) {
3375 if (cfg->speaker_outs) {
3376 cfg->line_outs = cfg->speaker_outs;
3377 memcpy(cfg->line_out_pins, cfg->speaker_pins,
3378 sizeof(cfg->speaker_pins));
3379 cfg->speaker_outs = 0;
3380 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
3381 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
3382 } else if (cfg->hp_outs) {
3383 cfg->line_outs = cfg->hp_outs;
3384 memcpy(cfg->line_out_pins, cfg->hp_pins,
3385 sizeof(cfg->hp_pins));
3386 cfg->hp_outs = 0;
3387 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
3388 cfg->line_out_type = AUTO_PIN_HP_OUT;
3389 }
3390 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003391
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02003392 /* Reorder the surround channels
3393 * ALSA sequence is front/surr/clfe/side
3394 * HDA sequence is:
3395 * 4-ch: front/surr => OK as it is
3396 * 6-ch: front/clfe/surr
Takashi Iwai9422db42007-04-20 16:11:43 +02003397 * 8-ch: front/clfe/rear/side|fc
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02003398 */
3399 switch (cfg->line_outs) {
3400 case 3:
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02003401 case 4:
3402 nid = cfg->line_out_pins[1];
Takashi Iwai9422db42007-04-20 16:11:43 +02003403 cfg->line_out_pins[1] = cfg->line_out_pins[2];
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02003404 cfg->line_out_pins[2] = nid;
3405 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003406 }
3407
Takashi Iwai82bc9552006-03-21 11:24:42 +01003408 /*
3409 * debug prints of the parsed results
3410 */
3411 snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3412 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
3413 cfg->line_out_pins[2], cfg->line_out_pins[3],
3414 cfg->line_out_pins[4]);
3415 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3416 cfg->speaker_outs, cfg->speaker_pins[0],
3417 cfg->speaker_pins[1], cfg->speaker_pins[2],
3418 cfg->speaker_pins[3], cfg->speaker_pins[4]);
Takashi Iwaieb06ed82006-09-20 17:10:27 +02003419 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3420 cfg->hp_outs, cfg->hp_pins[0],
3421 cfg->hp_pins[1], cfg->hp_pins[2],
3422 cfg->hp_pins[3], cfg->hp_pins[4]);
Matthew Ranostay90da78b2008-01-24 11:48:01 +01003423 snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
Takashi Iwai82bc9552006-03-21 11:24:42 +01003424 snd_printd(" inputs: mic=0x%x, fmic=0x%x, line=0x%x, fline=0x%x,"
3425 " cd=0x%x, aux=0x%x\n",
3426 cfg->input_pins[AUTO_PIN_MIC],
3427 cfg->input_pins[AUTO_PIN_FRONT_MIC],
3428 cfg->input_pins[AUTO_PIN_LINE],
3429 cfg->input_pins[AUTO_PIN_FRONT_LINE],
3430 cfg->input_pins[AUTO_PIN_CD],
3431 cfg->input_pins[AUTO_PIN_AUX]);
3432
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003433 return 0;
3434}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003435EXPORT_SYMBOL_HDA(snd_hda_parse_pin_def_config);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02003436
Takashi Iwai4a471b72005-12-07 13:56:29 +01003437/* labels for input pins */
3438const char *auto_pin_cfg_labels[AUTO_PIN_LAST] = {
3439 "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux"
3440};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003441EXPORT_SYMBOL_HDA(auto_pin_cfg_labels);
Takashi Iwai4a471b72005-12-07 13:56:29 +01003442
3443
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444#ifdef CONFIG_PM
3445/*
3446 * power management
3447 */
3448
3449/**
3450 * snd_hda_suspend - suspend the codecs
3451 * @bus: the HDA bus
3452 * @state: suspsend state
3453 *
3454 * Returns 0 if successful.
3455 */
3456int snd_hda_suspend(struct hda_bus *bus, pm_message_t state)
3457{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003458 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459
Takashi Iwai0ba21762007-04-16 11:29:14 +02003460 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai0b7a2e92007-08-14 15:18:26 +02003461#ifdef CONFIG_SND_HDA_POWER_SAVE
3462 if (!codec->power_on)
3463 continue;
3464#endif
Takashi Iwaicb53c622007-08-10 17:21:45 +02003465 hda_call_codec_suspend(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466 }
3467 return 0;
3468}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003469EXPORT_SYMBOL_HDA(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470
3471/**
3472 * snd_hda_resume - resume the codecs
3473 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474 *
3475 * Returns 0 if successful.
Takashi Iwaicb53c622007-08-10 17:21:45 +02003476 *
3477 * This fucntion is defined only when POWER_SAVE isn't set.
3478 * In the power-save mode, the codec is resumed dynamically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479 */
3480int snd_hda_resume(struct hda_bus *bus)
3481{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003482 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483
Takashi Iwai0ba21762007-04-16 11:29:14 +02003484 list_for_each_entry(codec, &bus->codec_list, list) {
Maxim Levitskyd804ad92007-09-03 15:28:04 +02003485 if (snd_hda_codec_needs_resume(codec))
3486 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488 return 0;
3489}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003490EXPORT_SYMBOL_HDA(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003491#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02003492
3493/*
3494 * generic arrays
3495 */
3496
3497/* get a new element from the given array
3498 * if it exceeds the pre-allocated array size, re-allocate the array
3499 */
3500void *snd_array_new(struct snd_array *array)
3501{
3502 if (array->used >= array->alloced) {
3503 int num = array->alloced + array->alloc_align;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01003504 void *nlist;
3505 if (snd_BUG_ON(num >= 4096))
3506 return NULL;
3507 nlist = kcalloc(num + 1, array->elem_size, GFP_KERNEL);
Takashi Iwaib2e18592008-07-30 15:01:44 +02003508 if (!nlist)
3509 return NULL;
3510 if (array->list) {
3511 memcpy(nlist, array->list,
3512 array->elem_size * array->alloced);
3513 kfree(array->list);
3514 }
3515 array->list = nlist;
3516 array->alloced = num;
3517 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01003518 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02003519}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003520EXPORT_SYMBOL_HDA(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02003521
3522/* free the given array elements */
3523void snd_array_free(struct snd_array *array)
3524{
3525 kfree(array->list);
3526 array->used = 0;
3527 array->alloced = 0;
3528 array->list = NULL;
3529}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003530EXPORT_SYMBOL_HDA(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01003531
3532/*
3533 * used by hda_proc.c and hda_eld.c
3534 */
3535void snd_print_pcm_rates(int pcm, char *buf, int buflen)
3536{
3537 static unsigned int rates[] = {
3538 8000, 11025, 16000, 22050, 32000, 44100, 48000, 88200,
3539 96000, 176400, 192000, 384000
3540 };
3541 int i, j;
3542
3543 for (i = 0, j = 0; i < ARRAY_SIZE(rates); i++)
3544 if (pcm & (1 << i))
3545 j += snprintf(buf + j, buflen - j, " %d", rates[i]);
3546
3547 buf[j] = '\0'; /* necessary when j == 0 */
3548}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003549EXPORT_SYMBOL_HDA(snd_print_pcm_rates);
Takashi Iwaib2022262008-11-21 21:24:03 +01003550
3551void snd_print_pcm_bits(int pcm, char *buf, int buflen)
3552{
3553 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
3554 int i, j;
3555
3556 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
3557 if (pcm & (AC_SUPPCM_BITS_8 << i))
3558 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
3559
3560 buf[j] = '\0'; /* necessary when j == 0 */
3561}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003562EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003563
3564MODULE_DESCRIPTION("HDA codec core");
3565MODULE_LICENSE("GPL");