blob: 598b9e2d85e646f50b95f6320a2cc1c3b3112c5e [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
Takashi Iwai18478e82012-03-09 17:51:10 +010022#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/init.h>
24#include <linux/delay.h>
25#include <linux/slab.h>
26#include <linux/pci.h>
Ingo Molnar62932df2006-01-16 16:34:20 +010027#include <linux/mutex.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040028#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <sound/core.h>
30#include "hda_codec.h"
31#include <sound/asoundef.h>
Jaroslav Kysela302e9c52006-07-05 17:39:49 +020032#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <sound/initval.h>
Takashi Iwaicd372fb2011-03-03 14:40:14 +010034#include <sound/jack.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "hda_local.h"
Jaroslav Kysela123c07a2009-10-21 14:48:23 +020036#include "hda_beep.h"
Takashi Iwai1835a0f2011-10-27 22:12:46 +020037#include "hda_jack.h"
Takashi Iwai28073142007-07-27 18:58:06 +020038#include <sound/hda_hwdep.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Takashi Iwaid66fee52011-08-02 15:39:31 +020040#define CREATE_TRACE_POINTS
41#include "hda_trace.h"
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043/*
44 * vendor / preset table
45 */
46
47struct hda_vendor_id {
48 unsigned int id;
49 const char *name;
50};
51
52/* codec vendor labels */
53static struct hda_vendor_id hda_vendor_ids[] = {
Takashi Iwaic8cd1282008-02-13 16:59:29 +010054 { 0x1002, "ATI" },
Takashi Iwaie5f14242009-07-01 18:11:44 +020055 { 0x1013, "Cirrus Logic" },
Takashi Iwaia9226252006-09-17 22:05:54 +020056 { 0x1057, "Motorola" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010057 { 0x1095, "Silicon Image" },
Takashi Iwai31117b72008-12-16 14:43:21 +010058 { 0x10de, "Nvidia" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010059 { 0x10ec, "Realtek" },
Takashi Iwai4e01f542009-04-16 08:53:34 +020060 { 0x1102, "Creative" },
Joseph Chanc577b8a2006-11-29 15:29:40 +010061 { 0x1106, "VIA" },
Matthew Ranostay7f168592007-10-18 17:38:17 +020062 { 0x111d, "IDT" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010063 { 0x11c1, "LSI" },
Takashi Iwai54b903e2005-05-15 14:30:10 +020064 { 0x11d4, "Analog Devices" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 { 0x13f6, "C-Media" },
Takashi Iwaia9226252006-09-17 22:05:54 +020066 { 0x14f1, "Conexant" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010067 { 0x17e8, "Chrontel" },
68 { 0x1854, "LG" },
Mark Brown8199de32008-10-28 14:50:13 +000069 { 0x1aec, "Wolfson Microelectronics" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 { 0x434d, "C-Media" },
Takashi Iwai74c61132008-12-18 09:11:33 +010071 { 0x8086, "Intel" },
Matt2f2f4252005-04-13 14:45:30 +020072 { 0x8384, "SigmaTel" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 {} /* terminator */
74};
75
Takashi Iwai1289e9e2008-11-27 15:47:11 +010076static DEFINE_MUTEX(preset_mutex);
77static LIST_HEAD(hda_preset_tables);
78
79int snd_hda_add_codec_preset(struct hda_codec_preset_list *preset)
80{
81 mutex_lock(&preset_mutex);
82 list_add_tail(&preset->list, &hda_preset_tables);
83 mutex_unlock(&preset_mutex);
84 return 0;
85}
Takashi Iwaiff7a3262008-11-28 15:17:06 +010086EXPORT_SYMBOL_HDA(snd_hda_add_codec_preset);
Takashi Iwai1289e9e2008-11-27 15:47:11 +010087
88int snd_hda_delete_codec_preset(struct hda_codec_preset_list *preset)
89{
90 mutex_lock(&preset_mutex);
91 list_del(&preset->list);
92 mutex_unlock(&preset_mutex);
93 return 0;
94}
Takashi Iwaiff7a3262008-11-28 15:17:06 +010095EXPORT_SYMBOL_HDA(snd_hda_delete_codec_preset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Takashi Iwaicb53c622007-08-10 17:21:45 +020097#ifdef CONFIG_SND_HDA_POWER_SAVE
98static void hda_power_work(struct work_struct *work);
99static void hda_keep_power_on(struct hda_codec *codec);
Takashi Iwaie581f3d2011-07-26 10:19:20 +0200100#define hda_codec_is_power_on(codec) ((codec)->power_on)
Takashi Iwaicb53c622007-08-10 17:21:45 +0200101#else
102static inline void hda_keep_power_on(struct hda_codec *codec) {}
Takashi Iwaie581f3d2011-07-26 10:19:20 +0200103#define hda_codec_is_power_on(codec) 1
Takashi Iwaicb53c622007-08-10 17:21:45 +0200104#endif
105
Takashi Iwaid5191e52009-11-16 14:58:17 +0100106/**
107 * snd_hda_get_jack_location - Give a location string of the jack
108 * @cfg: pin default config value
109 *
110 * Parse the pin default config value and returns the string of the
111 * jack location, e.g. "Rear", "Front", etc.
112 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400113const char *snd_hda_get_jack_location(u32 cfg)
114{
115 static char *bases[7] = {
116 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
117 };
118 static unsigned char specials_idx[] = {
119 0x07, 0x08,
120 0x17, 0x18, 0x19,
121 0x37, 0x38
122 };
123 static char *specials[] = {
124 "Rear Panel", "Drive Bar",
125 "Riser", "HDMI", "ATAPI",
126 "Mobile-In", "Mobile-Out"
127 };
128 int i;
129 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
130 if ((cfg & 0x0f) < 7)
131 return bases[cfg & 0x0f];
132 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
133 if (cfg == specials_idx[i])
134 return specials[i];
135 }
136 return "UNKNOWN";
137}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100138EXPORT_SYMBOL_HDA(snd_hda_get_jack_location);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400139
Takashi Iwaid5191e52009-11-16 14:58:17 +0100140/**
141 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
142 * @cfg: pin default config value
143 *
144 * Parse the pin default config value and returns the string of the
145 * jack connectivity, i.e. external or internal connection.
146 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400147const char *snd_hda_get_jack_connectivity(u32 cfg)
148{
149 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
150
151 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
152}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100153EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400154
Takashi Iwaid5191e52009-11-16 14:58:17 +0100155/**
156 * snd_hda_get_jack_type - Give a type string of the jack
157 * @cfg: pin default config value
158 *
159 * Parse the pin default config value and returns the string of the
160 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
161 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400162const char *snd_hda_get_jack_type(u32 cfg)
163{
164 static char *jack_types[16] = {
165 "Line Out", "Speaker", "HP Out", "CD",
166 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
167 "Line In", "Aux", "Mic", "Telephony",
168 "SPDIF In", "Digitial In", "Reserved", "Other"
169 };
170
171 return jack_types[(cfg & AC_DEFCFG_DEVICE)
172 >> AC_DEFCFG_DEVICE_SHIFT];
173}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100174EXPORT_SYMBOL_HDA(snd_hda_get_jack_type);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400175
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100176/*
177 * Compose a 32bit command word to be sent to the HD-audio controller
178 */
179static inline unsigned int
180make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int direct,
181 unsigned int verb, unsigned int parm)
182{
183 u32 val;
184
Takashi Iwai82e1b802009-07-17 12:47:34 +0200185 if ((codec->addr & ~0xf) || (direct & ~1) || (nid & ~0x7f) ||
186 (verb & ~0xfff) || (parm & ~0xffff)) {
Wu Fengguang6430aee2009-07-17 16:49:19 +0800187 printk(KERN_ERR "hda-codec: out of range cmd %x:%x:%x:%x:%x\n",
188 codec->addr, direct, nid, verb, parm);
189 return ~0;
190 }
191
192 val = (u32)codec->addr << 28;
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100193 val |= (u32)direct << 27;
194 val |= (u32)nid << 20;
195 val |= verb << 8;
196 val |= parm;
197 return val;
198}
199
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200200/*
201 * Send and receive a verb
202 */
203static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
204 unsigned int *res)
205{
206 struct hda_bus *bus = codec->bus;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200207 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200208
Wu Fengguang6430aee2009-07-17 16:49:19 +0800209 if (cmd == ~0)
210 return -1;
211
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200212 if (res)
213 *res = -1;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200214 again:
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200215 snd_hda_power_up(codec);
216 mutex_lock(&bus->cmd_mutex);
Takashi Iwaid66fee52011-08-02 15:39:31 +0200217 trace_hda_send_cmd(codec, cmd);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200218 err = bus->ops.command(bus, cmd);
Takashi Iwaid66fee52011-08-02 15:39:31 +0200219 if (!err && res) {
Wu Fengguangdeadff12009-08-01 18:45:16 +0800220 *res = bus->ops.get_response(bus, codec->addr);
Takashi Iwaid66fee52011-08-02 15:39:31 +0200221 trace_hda_get_response(codec, *res);
222 }
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200223 mutex_unlock(&bus->cmd_mutex);
224 snd_hda_power_down(codec);
Takashi Iwai8dd78332009-06-02 01:16:07 +0200225 if (res && *res == -1 && bus->rirb_error) {
226 if (bus->response_reset) {
227 snd_printd("hda_codec: resetting BUS due to "
228 "fatal communication error\n");
Takashi Iwaid66fee52011-08-02 15:39:31 +0200229 trace_hda_bus_reset(bus);
Takashi Iwai8dd78332009-06-02 01:16:07 +0200230 bus->ops.bus_reset(bus);
231 }
232 goto again;
233 }
234 /* clear reset-flag when the communication gets recovered */
235 if (!err)
236 bus->response_reset = 0;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200237 return err;
238}
239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240/**
241 * snd_hda_codec_read - send a command and get the response
242 * @codec: the HDA codec
243 * @nid: NID to send the command
244 * @direct: direct flag
245 * @verb: the verb to send
246 * @parm: the parameter for the verb
247 *
248 * Send a single command and read the corresponding response.
249 *
250 * Returns the obtained response value, or -1 for an error.
251 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200252unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
253 int direct,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 unsigned int verb, unsigned int parm)
255{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200256 unsigned cmd = make_codec_cmd(codec, nid, direct, verb, parm);
257 unsigned int res;
Greg Thelen9857edf2011-06-13 07:45:45 -0700258 if (codec_exec_verb(codec, cmd, &res))
259 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 return res;
261}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100262EXPORT_SYMBOL_HDA(snd_hda_codec_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264/**
265 * snd_hda_codec_write - send a single command without waiting for response
266 * @codec: the HDA codec
267 * @nid: NID to send the command
268 * @direct: direct flag
269 * @verb: the verb to send
270 * @parm: the parameter for the verb
271 *
272 * Send a single command without waiting for response.
273 *
274 * Returns 0 if successful, or a negative error code.
275 */
276int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
277 unsigned int verb, unsigned int parm)
278{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200279 unsigned int cmd = make_codec_cmd(codec, nid, direct, verb, parm);
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100280 unsigned int res;
Takashi Iwaib20f3b82009-06-02 01:20:22 +0200281 return codec_exec_verb(codec, cmd,
282 codec->bus->sync_write ? &res : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100284EXPORT_SYMBOL_HDA(snd_hda_codec_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286/**
287 * snd_hda_sequence_write - sequence writes
288 * @codec: the HDA codec
289 * @seq: VERB array to send
290 *
291 * Send the commands sequentially from the given array.
292 * The array must be terminated with NID=0.
293 */
294void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
295{
296 for (; seq->nid; seq++)
297 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
298}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100299EXPORT_SYMBOL_HDA(snd_hda_sequence_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301/**
302 * snd_hda_get_sub_nodes - get the range of sub nodes
303 * @codec: the HDA codec
304 * @nid: NID to parse
305 * @start_id: the pointer to store the start NID
306 *
307 * Parse the NID and store the start NID of its sub-nodes.
308 * Returns the number of sub-nodes.
309 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200310int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
311 hda_nid_t *start_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{
313 unsigned int parm;
314
315 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
Danny Tholene8a7f132007-09-11 21:41:56 +0200316 if (parm == -1)
317 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 *start_id = (parm >> 16) & 0x7fff;
319 return (int)(parm & 0x7fff);
320}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100321EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200323/* look up the cached results */
324static hda_nid_t *lookup_conn_list(struct snd_array *array, hda_nid_t nid)
325{
326 int i, len;
327 for (i = 0; i < array->used; ) {
328 hda_nid_t *p = snd_array_elem(array, i);
329 if (nid == *p)
330 return p;
331 len = p[1];
332 i += len + 2;
333 }
334 return NULL;
335}
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200336
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200337/* read the connection and add to the cache */
338static int read_and_add_raw_conns(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200339{
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200340 hda_nid_t list[HDA_MAX_CONNECTIONS];
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200341 int len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200342
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200343 len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list));
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200344 if (len < 0)
345 return len;
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200346 return snd_hda_override_conn_list(codec, nid, len, list);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200347}
Takashi Iwaidce20792011-06-24 14:10:28 +0200348
349/**
350 * snd_hda_get_connections - copy connection list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 * @codec: the HDA codec
352 * @nid: NID to parse
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200353 * @conn_list: connection list array; when NULL, checks only the size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 * @max_conns: max. number of connections to store
355 *
356 * Parses the connection list of the given widget and stores the list
357 * of NIDs.
358 *
359 * Returns the number of connections, or a negative error code.
360 */
361int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200362 hda_nid_t *conn_list, int max_conns)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200363{
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200364 struct snd_array *array = &codec->conn_lists;
365 int len;
366 hda_nid_t *p;
367 bool added = false;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200368
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200369 again:
370 mutex_lock(&codec->hash_mutex);
371 len = -1;
372 /* if the connection-list is already cached, read it */
373 p = lookup_conn_list(array, nid);
374 if (p) {
375 len = p[1];
376 if (conn_list && len > max_conns) {
377 snd_printk(KERN_ERR "hda_codec: "
378 "Too many connections %d for NID 0x%x\n",
379 len, nid);
380 mutex_unlock(&codec->hash_mutex);
381 return -EINVAL;
382 }
383 if (conn_list && len)
384 memcpy(conn_list, p + 2, len * sizeof(hda_nid_t));
Takashi Iwaidce20792011-06-24 14:10:28 +0200385 }
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200386 mutex_unlock(&codec->hash_mutex);
387 if (len >= 0)
388 return len;
389 if (snd_BUG_ON(added))
390 return -EINVAL;
391
392 len = read_and_add_raw_conns(codec, nid);
393 if (len < 0)
394 return len;
395 added = true;
396 goto again;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200397}
398EXPORT_SYMBOL_HDA(snd_hda_get_connections);
399
Takashi Iwai9e7717c2011-07-11 15:42:52 +0200400/**
401 * snd_hda_get_raw_connections - copy connection list without cache
402 * @codec: the HDA codec
403 * @nid: NID to parse
404 * @conn_list: connection list array
405 * @max_conns: max. number of connections to store
406 *
407 * Like snd_hda_get_connections(), copy the connection list but without
408 * checking through the connection-list cache.
409 * Currently called only from hda_proc.c, so not exported.
410 */
411int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid,
412 hda_nid_t *conn_list, int max_conns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
414 unsigned int parm;
Takashi Iwai54d17402005-11-21 16:33:22 +0100415 int i, conn_len, conns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 unsigned int shift, num_elems, mask;
Takashi Iwai1ba7a7c2009-07-27 12:56:26 +0200417 unsigned int wcaps;
Takashi Iwai54d17402005-11-21 16:33:22 +0100418 hda_nid_t prev_nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Takashi Iwaida3cec32008-08-08 17:12:14 +0200420 if (snd_BUG_ON(!conn_list || max_conns <= 0))
421 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Takashi Iwai1ba7a7c2009-07-27 12:56:26 +0200423 wcaps = get_wcaps(codec, nid);
424 if (!(wcaps & AC_WCAP_CONN_LIST) &&
Takashi Iwai8d087c72011-06-28 12:45:47 +0200425 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
426 return 0;
Jaroslav Kysela16a433d2009-07-22 16:20:40 +0200427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
429 if (parm & AC_CLIST_LONG) {
430 /* long form */
431 shift = 16;
432 num_elems = 2;
433 } else {
434 /* short form */
435 shift = 8;
436 num_elems = 4;
437 }
438 conn_len = parm & AC_CLIST_LENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 mask = (1 << (shift-1)) - 1;
440
Takashi Iwai0ba21762007-04-16 11:29:14 +0200441 if (!conn_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 return 0; /* no connection */
443
444 if (conn_len == 1) {
445 /* single connection */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200446 parm = snd_hda_codec_read(codec, nid, 0,
447 AC_VERB_GET_CONNECT_LIST, 0);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200448 if (parm == -1 && codec->bus->rirb_error)
449 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 conn_list[0] = parm & mask;
451 return 1;
452 }
453
454 /* multi connection */
455 conns = 0;
Takashi Iwai54d17402005-11-21 16:33:22 +0100456 prev_nid = 0;
457 for (i = 0; i < conn_len; i++) {
458 int range_val;
459 hda_nid_t val, n;
460
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200461 if (i % num_elems == 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +0100462 parm = snd_hda_codec_read(codec, nid, 0,
463 AC_VERB_GET_CONNECT_LIST, i);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200464 if (parm == -1 && codec->bus->rirb_error)
465 return -EIO;
466 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200467 range_val = !!(parm & (1 << (shift-1))); /* ranges */
Takashi Iwai54d17402005-11-21 16:33:22 +0100468 val = parm & mask;
Jaroslav Kysela2e9bf242009-07-18 11:48:19 +0200469 if (val == 0) {
470 snd_printk(KERN_WARNING "hda_codec: "
471 "invalid CONNECT_LIST verb %x[%i]:%x\n",
472 nid, i, parm);
473 return 0;
474 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100475 parm >>= shift;
476 if (range_val) {
477 /* ranges between the previous and this one */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200478 if (!prev_nid || prev_nid >= val) {
479 snd_printk(KERN_WARNING "hda_codec: "
480 "invalid dep_range_val %x:%x\n",
481 prev_nid, val);
Takashi Iwai54d17402005-11-21 16:33:22 +0100482 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100484 for (n = prev_nid + 1; n <= val; n++) {
485 if (conns >= max_conns) {
Takashi Iwai5aacc212010-07-30 10:36:29 +0200486 snd_printk(KERN_ERR "hda_codec: "
487 "Too many connections %d for NID 0x%x\n",
488 conns, nid);
Takashi Iwai54d17402005-11-21 16:33:22 +0100489 return -EINVAL;
490 }
491 conn_list[conns++] = n;
492 }
493 } else {
494 if (conns >= max_conns) {
Takashi Iwai5aacc212010-07-30 10:36:29 +0200495 snd_printk(KERN_ERR "hda_codec: "
496 "Too many connections %d for NID 0x%x\n",
497 conns, nid);
Takashi Iwai54d17402005-11-21 16:33:22 +0100498 return -EINVAL;
499 }
500 conn_list[conns++] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100502 prev_nid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 }
504 return conns;
505}
506
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200507static bool add_conn_list(struct snd_array *array, hda_nid_t nid)
508{
509 hda_nid_t *p = snd_array_new(array);
510 if (!p)
511 return false;
512 *p = nid;
513 return true;
514}
515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516/**
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200517 * snd_hda_override_conn_list - add/modify the connection-list to cache
518 * @codec: the HDA codec
519 * @nid: NID to parse
520 * @len: number of connection list entries
521 * @list: the list of connection entries
522 *
523 * Add or modify the given connection-list to the cache. If the corresponding
524 * cache already exists, invalidate it and append a new one.
525 *
526 * Returns zero or a negative error code.
527 */
528int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
529 const hda_nid_t *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530{
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200531 struct snd_array *array = &codec->conn_lists;
532 hda_nid_t *p;
533 int i, old_used;
534
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200535 mutex_lock(&codec->hash_mutex);
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200536 p = lookup_conn_list(array, nid);
537 if (p)
538 *p = -1; /* invalidate the old entry */
539
540 old_used = array->used;
541 if (!add_conn_list(array, nid) || !add_conn_list(array, len))
542 goto error_add;
543 for (i = 0; i < len; i++)
544 if (!add_conn_list(array, list[i]))
545 goto error_add;
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200546 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200547 return 0;
548
549 error_add:
550 array->used = old_used;
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200551 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200552 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553}
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200554EXPORT_SYMBOL_HDA(snd_hda_override_conn_list);
555
556/**
Takashi Iwai8d087c72011-06-28 12:45:47 +0200557 * snd_hda_get_conn_index - get the connection index of the given NID
558 * @codec: the HDA codec
559 * @mux: NID containing the list
560 * @nid: NID to select
561 * @recursive: 1 when searching NID recursively, otherwise 0
562 *
563 * Parses the connection list of the widget @mux and checks whether the
564 * widget @nid is present. If it is, return the connection index.
565 * Otherwise it returns -1.
566 */
567int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
568 hda_nid_t nid, int recursive)
569{
570 hda_nid_t conn[HDA_MAX_NUM_INPUTS];
571 int i, nums;
572
573 nums = snd_hda_get_connections(codec, mux, conn, ARRAY_SIZE(conn));
574 for (i = 0; i < nums; i++)
575 if (conn[i] == nid)
576 return i;
577 if (!recursive)
578 return -1;
579 if (recursive > 5) {
580 snd_printd("hda_codec: too deep connection for 0x%x\n", nid);
581 return -1;
582 }
583 recursive++;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200584 for (i = 0; i < nums; i++) {
585 unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
586 if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
587 continue;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200588 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
589 return i;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200590 }
Takashi Iwai8d087c72011-06-28 12:45:47 +0200591 return -1;
592}
593EXPORT_SYMBOL_HDA(snd_hda_get_conn_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
595/**
596 * snd_hda_queue_unsol_event - add an unsolicited event to queue
597 * @bus: the BUS
598 * @res: unsolicited event (lower 32bit of RIRB entry)
599 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
600 *
601 * Adds the given event to the queue. The events are processed in
602 * the workqueue asynchronously. Call this function in the interrupt
603 * hanlder when RIRB receives an unsolicited event.
604 *
605 * Returns 0 if successful, or a negative error code.
606 */
607int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
608{
609 struct hda_bus_unsolicited *unsol;
610 unsigned int wp;
611
Takashi Iwaiecf726f2011-08-09 14:22:44 +0200612 trace_hda_unsol_event(bus, res, res_ex);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200613 unsol = bus->unsol;
614 if (!unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 return 0;
616
617 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
618 unsol->wp = wp;
619
620 wp <<= 1;
621 unsol->queue[wp] = res;
622 unsol->queue[wp + 1] = res_ex;
623
Takashi Iwai6acaed32009-01-12 10:09:24 +0100624 queue_work(bus->workq, &unsol->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
626 return 0;
627}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100628EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
630/*
Wu Fengguang5c1d1a92008-10-07 14:17:53 +0800631 * process queued unsolicited events
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 */
David Howellsc4028952006-11-22 14:57:56 +0000633static void process_unsol_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
David Howellsc4028952006-11-22 14:57:56 +0000635 struct hda_bus_unsolicited *unsol =
636 container_of(work, struct hda_bus_unsolicited, work);
637 struct hda_bus *bus = unsol->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 struct hda_codec *codec;
639 unsigned int rp, caddr, res;
640
641 while (unsol->rp != unsol->wp) {
642 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
643 unsol->rp = rp;
644 rp <<= 1;
645 res = unsol->queue[rp];
646 caddr = unsol->queue[rp + 1];
Takashi Iwai0ba21762007-04-16 11:29:14 +0200647 if (!(caddr & (1 << 4))) /* no unsolicited event? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 continue;
649 codec = bus->caddr_tbl[caddr & 0x0f];
650 if (codec && codec->patch_ops.unsol_event)
651 codec->patch_ops.unsol_event(codec, res);
652 }
653}
654
655/*
656 * initialize unsolicited queue
657 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200658static int init_unsol_queue(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
660 struct hda_bus_unsolicited *unsol;
661
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100662 if (bus->unsol) /* already initialized */
663 return 0;
664
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200665 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200666 if (!unsol) {
667 snd_printk(KERN_ERR "hda_codec: "
668 "can't allocate unsolicited queue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 return -ENOMEM;
670 }
David Howellsc4028952006-11-22 14:57:56 +0000671 INIT_WORK(&unsol->work, process_unsol_events);
672 unsol->bus = bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 bus->unsol = unsol;
674 return 0;
675}
676
677/*
678 * destructor
679 */
680static void snd_hda_codec_free(struct hda_codec *codec);
681
682static int snd_hda_bus_free(struct hda_bus *bus)
683{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200684 struct hda_codec *codec, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Takashi Iwai0ba21762007-04-16 11:29:14 +0200686 if (!bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 return 0;
Takashi Iwai6acaed32009-01-12 10:09:24 +0100688 if (bus->workq)
689 flush_workqueue(bus->workq);
690 if (bus->unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 kfree(bus->unsol);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200692 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 snd_hda_codec_free(codec);
694 }
695 if (bus->ops.private_free)
696 bus->ops.private_free(bus);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100697 if (bus->workq)
698 destroy_workqueue(bus->workq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 kfree(bus);
700 return 0;
701}
702
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100703static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
705 struct hda_bus *bus = device->device_data;
Takashi Iwaib94d35392008-11-21 09:08:06 +0100706 bus->shutdown = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 return snd_hda_bus_free(bus);
708}
709
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200710#ifdef CONFIG_SND_HDA_HWDEP
711static int snd_hda_bus_dev_register(struct snd_device *device)
712{
713 struct hda_bus *bus = device->device_data;
714 struct hda_codec *codec;
715 list_for_each_entry(codec, &bus->codec_list, list) {
716 snd_hda_hwdep_add_sysfs(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +0100717 snd_hda_hwdep_add_power_sysfs(codec);
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200718 }
719 return 0;
720}
721#else
722#define snd_hda_bus_dev_register NULL
723#endif
724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725/**
726 * snd_hda_bus_new - create a HDA bus
727 * @card: the card entry
728 * @temp: the template for hda_bus information
729 * @busp: the pointer to store the created bus instance
730 *
731 * Returns 0 if successful, or a negative error code.
732 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100733int /*__devinit*/ snd_hda_bus_new(struct snd_card *card,
Takashi Iwai756e2b02007-04-16 11:27:07 +0200734 const struct hda_bus_template *temp,
735 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736{
737 struct hda_bus *bus;
738 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100739 static struct snd_device_ops dev_ops = {
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200740 .dev_register = snd_hda_bus_dev_register,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 .dev_free = snd_hda_bus_dev_free,
742 };
743
Takashi Iwaida3cec32008-08-08 17:12:14 +0200744 if (snd_BUG_ON(!temp))
745 return -EINVAL;
746 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
747 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749 if (busp)
750 *busp = NULL;
751
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200752 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 if (bus == NULL) {
754 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
755 return -ENOMEM;
756 }
757
758 bus->card = card;
759 bus->private_data = temp->private_data;
760 bus->pci = temp->pci;
761 bus->modelname = temp->modelname;
Takashi Iwaifee2fba2008-11-27 12:43:28 +0100762 bus->power_save = temp->power_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 bus->ops = temp->ops;
764
Ingo Molnar62932df2006-01-16 16:34:20 +0100765 mutex_init(&bus->cmd_mutex);
Takashi Iwai3f50ac62010-08-20 09:44:36 +0200766 mutex_init(&bus->prepare_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 INIT_LIST_HEAD(&bus->codec_list);
768
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100769 snprintf(bus->workq_name, sizeof(bus->workq_name),
770 "hd-audio%d", card->number);
771 bus->workq = create_singlethread_workqueue(bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100772 if (!bus->workq) {
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100773 snd_printk(KERN_ERR "cannot create workqueue %s\n",
774 bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100775 kfree(bus);
776 return -ENOMEM;
777 }
778
Takashi Iwai0ba21762007-04-16 11:29:14 +0200779 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
780 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 snd_hda_bus_free(bus);
782 return err;
783 }
784 if (busp)
785 *busp = bus;
786 return 0;
787}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100788EXPORT_SYMBOL_HDA(snd_hda_bus_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Takashi Iwai82467612007-07-27 19:15:54 +0200790#ifdef CONFIG_SND_HDA_GENERIC
791#define is_generic_config(codec) \
Takashi Iwaif44ac832008-07-30 15:01:45 +0200792 (codec->modelname && !strcmp(codec->modelname, "generic"))
Takashi Iwai82467612007-07-27 19:15:54 +0200793#else
794#define is_generic_config(codec) 0
795#endif
796
Takashi Iwai645f10c2008-11-28 15:07:37 +0100797#ifdef MODULE
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100798#define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
799#else
Takashi Iwai645f10c2008-11-28 15:07:37 +0100800#define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100801#endif
802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803/*
804 * find a matching codec preset
805 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200806static const struct hda_codec_preset *
Takashi Iwai756e2b02007-04-16 11:27:07 +0200807find_codec_preset(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808{
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100809 struct hda_codec_preset_list *tbl;
810 const struct hda_codec_preset *preset;
811 int mod_requested = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Takashi Iwai82467612007-07-27 19:15:54 +0200813 if (is_generic_config(codec))
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100814 return NULL; /* use the generic parser */
815
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100816 again:
817 mutex_lock(&preset_mutex);
818 list_for_each_entry(tbl, &hda_preset_tables, list) {
819 if (!try_module_get(tbl->owner)) {
820 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
821 continue;
822 }
823 for (preset = tbl->preset; preset->id; preset++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 u32 mask = preset->mask;
Marc Boucherca7cfae2008-01-22 15:32:25 +0100825 if (preset->afg && preset->afg != codec->afg)
826 continue;
827 if (preset->mfg && preset->mfg != codec->mfg)
828 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200829 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 mask = ~0;
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200831 if (preset->id == (codec->vendor_id & mask) &&
Takashi Iwai0ba21762007-04-16 11:29:14 +0200832 (!preset->rev ||
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100833 preset->rev == codec->revision_id)) {
834 mutex_unlock(&preset_mutex);
835 codec->owner = tbl->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 return preset;
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100837 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 }
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100839 module_put(tbl->owner);
840 }
841 mutex_unlock(&preset_mutex);
842
843 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
844 char name[32];
845 if (!mod_requested)
846 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
847 codec->vendor_id);
848 else
849 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
850 (codec->vendor_id >> 16) & 0xffff);
851 request_module(name);
852 mod_requested++;
853 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 }
855 return NULL;
856}
857
858/*
Takashi Iwaif44ac832008-07-30 15:01:45 +0200859 * get_codec_name - store the codec name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 */
Takashi Iwaif44ac832008-07-30 15:01:45 +0200861static int get_codec_name(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862{
863 const struct hda_vendor_id *c;
864 const char *vendor = NULL;
865 u16 vendor_id = codec->vendor_id >> 16;
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200866 char tmp[16];
867
868 if (codec->vendor_name)
869 goto get_chip_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
871 for (c = hda_vendor_ids; c->id; c++) {
872 if (c->id == vendor_id) {
873 vendor = c->name;
874 break;
875 }
876 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200877 if (!vendor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 sprintf(tmp, "Generic %04x", vendor_id);
879 vendor = tmp;
880 }
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200881 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
882 if (!codec->vendor_name)
883 return -ENOMEM;
884
885 get_chip_name:
886 if (codec->chip_name)
887 return 0;
888
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 if (codec->preset && codec->preset->name)
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200890 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
891 else {
892 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
893 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
894 }
895 if (!codec->chip_name)
Takashi Iwaif44ac832008-07-30 15:01:45 +0200896 return -ENOMEM;
897 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898}
899
900/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200901 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100903static void /*__devinit*/ setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904{
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200905 int i, total_nodes, function_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 hda_nid_t nid;
907
908 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
909 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200910 function_id = snd_hda_param_read(codec, nid,
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200911 AC_PAR_FUNCTION_TYPE);
Jaroslav Kyselacd7643b2010-07-20 12:11:25 +0200912 switch (function_id & 0xff) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200913 case AC_GRP_AUDIO_FUNCTION:
914 codec->afg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200915 codec->afg_function_id = function_id & 0xff;
916 codec->afg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200917 break;
918 case AC_GRP_MODEM_FUNCTION:
919 codec->mfg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200920 codec->mfg_function_id = function_id & 0xff;
921 codec->mfg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200922 break;
923 default:
924 break;
925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927}
928
929/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100930 * read widget caps for each widget and store in cache
931 */
932static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
933{
934 int i;
935 hda_nid_t nid;
936
937 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
938 &codec->start_nid);
939 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200940 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +0100941 return -ENOMEM;
942 nid = codec->start_nid;
943 for (i = 0; i < codec->num_nodes; i++, nid++)
944 codec->wcaps[i] = snd_hda_param_read(codec, nid,
945 AC_PAR_AUDIO_WIDGET_CAP);
946 return 0;
947}
948
Takashi Iwai3be14142009-02-20 14:11:16 +0100949/* read all pin default configurations and save codec->init_pins */
950static int read_pin_defaults(struct hda_codec *codec)
951{
952 int i;
953 hda_nid_t nid = codec->start_nid;
954
955 for (i = 0; i < codec->num_nodes; i++, nid++) {
956 struct hda_pincfg *pin;
957 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +0200958 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwai3be14142009-02-20 14:11:16 +0100959 if (wid_type != AC_WID_PIN)
960 continue;
961 pin = snd_array_new(&codec->init_pins);
962 if (!pin)
963 return -ENOMEM;
964 pin->nid = nid;
965 pin->cfg = snd_hda_codec_read(codec, nid, 0,
966 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200967 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
968 AC_VERB_GET_PIN_WIDGET_CONTROL,
969 0);
Takashi Iwai3be14142009-02-20 14:11:16 +0100970 }
971 return 0;
972}
973
974/* look up the given pin config list and return the item matching with NID */
975static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
976 struct snd_array *array,
977 hda_nid_t nid)
978{
979 int i;
980 for (i = 0; i < array->used; i++) {
981 struct hda_pincfg *pin = snd_array_elem(array, i);
982 if (pin->nid == nid)
983 return pin;
984 }
985 return NULL;
986}
987
988/* write a config value for the given NID */
989static void set_pincfg(struct hda_codec *codec, hda_nid_t nid,
990 unsigned int cfg)
991{
992 int i;
993 for (i = 0; i < 4; i++) {
994 snd_hda_codec_write(codec, nid, 0,
995 AC_VERB_SET_CONFIG_DEFAULT_BYTES_0 + i,
996 cfg & 0xff);
997 cfg >>= 8;
998 }
999}
1000
1001/* set the current pin config value for the given NID.
1002 * the value is cached, and read via snd_hda_codec_get_pincfg()
1003 */
1004int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
1005 hda_nid_t nid, unsigned int cfg)
1006{
1007 struct hda_pincfg *pin;
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001008 unsigned int oldcfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001009
Takashi Iwaib82855a2009-12-27 11:24:56 +01001010 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
1011 return -EINVAL;
1012
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001013 oldcfg = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +01001014 pin = look_up_pincfg(codec, list, nid);
1015 if (!pin) {
1016 pin = snd_array_new(list);
1017 if (!pin)
1018 return -ENOMEM;
1019 pin->nid = nid;
1020 }
1021 pin->cfg = cfg;
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001022
1023 /* change only when needed; e.g. if the pincfg is already present
1024 * in user_pins[], don't write it
1025 */
1026 cfg = snd_hda_codec_get_pincfg(codec, nid);
1027 if (oldcfg != cfg)
1028 set_pincfg(codec, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001029 return 0;
1030}
1031
Takashi Iwaid5191e52009-11-16 14:58:17 +01001032/**
1033 * snd_hda_codec_set_pincfg - Override a pin default configuration
1034 * @codec: the HDA codec
1035 * @nid: NID to set the pin config
1036 * @cfg: the pin default config value
1037 *
1038 * Override a pin default configuration value in the cache.
1039 * This value can be read by snd_hda_codec_get_pincfg() in a higher
1040 * priority than the real hardware value.
1041 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001042int snd_hda_codec_set_pincfg(struct hda_codec *codec,
1043 hda_nid_t nid, unsigned int cfg)
1044{
Takashi Iwai346ff702009-02-23 09:42:57 +01001045 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001046}
1047EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
1048
Takashi Iwaid5191e52009-11-16 14:58:17 +01001049/**
1050 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
1051 * @codec: the HDA codec
1052 * @nid: NID to get the pin config
1053 *
1054 * Get the current pin config value of the given pin NID.
1055 * If the pincfg value is cached or overridden via sysfs or driver,
1056 * returns the cached value.
1057 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001058unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
1059{
1060 struct hda_pincfg *pin;
1061
Takashi Iwai3be14142009-02-20 14:11:16 +01001062#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +01001063 pin = look_up_pincfg(codec, &codec->user_pins, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +01001064 if (pin)
1065 return pin->cfg;
1066#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001067 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
1068 if (pin)
1069 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001070 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1071 if (pin)
1072 return pin->cfg;
1073 return 0;
1074}
1075EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
1076
1077/* restore all current pin configs */
1078static void restore_pincfgs(struct hda_codec *codec)
1079{
1080 int i;
1081 for (i = 0; i < codec->init_pins.used; i++) {
1082 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1083 set_pincfg(codec, pin->nid,
1084 snd_hda_codec_get_pincfg(codec, pin->nid));
1085 }
1086}
Takashi Iwai54d17402005-11-21 16:33:22 +01001087
Takashi Iwai92ee6162009-12-27 11:18:59 +01001088/**
1089 * snd_hda_shutup_pins - Shut up all pins
1090 * @codec: the HDA codec
1091 *
1092 * Clear all pin controls to shup up before suspend for avoiding click noise.
1093 * The controls aren't cached so that they can be resumed properly.
1094 */
1095void snd_hda_shutup_pins(struct hda_codec *codec)
1096{
1097 int i;
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001098 /* don't shut up pins when unloading the driver; otherwise it breaks
1099 * the default pin setup at the next load of the driver
1100 */
1101 if (codec->bus->shutdown)
1102 return;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001103 for (i = 0; i < codec->init_pins.used; i++) {
1104 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1105 /* use read here for syncing after issuing each verb */
1106 snd_hda_codec_read(codec, pin->nid, 0,
1107 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
1108 }
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001109 codec->pins_shutup = 1;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001110}
1111EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);
1112
Takashi Iwai2a439522011-07-26 09:52:50 +02001113#ifdef CONFIG_PM
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001114/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1115static void restore_shutup_pins(struct hda_codec *codec)
1116{
1117 int i;
1118 if (!codec->pins_shutup)
1119 return;
1120 if (codec->bus->shutdown)
1121 return;
1122 for (i = 0; i < codec->init_pins.used; i++) {
1123 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1124 snd_hda_codec_write(codec, pin->nid, 0,
1125 AC_VERB_SET_PIN_WIDGET_CONTROL,
1126 pin->ctrl);
1127 }
1128 codec->pins_shutup = 0;
1129}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001130#endif
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001131
Takashi Iwai01751f52007-08-10 16:59:39 +02001132static void init_hda_cache(struct hda_cache_rec *cache,
1133 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001134static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +02001135
Takashi Iwai3be14142009-02-20 14:11:16 +01001136/* restore the initial pin cfgs and release all pincfg lists */
1137static void restore_init_pincfgs(struct hda_codec *codec)
1138{
Takashi Iwai346ff702009-02-23 09:42:57 +01001139 /* first free driver_pins and user_pins, then call restore_pincfg
Takashi Iwai3be14142009-02-20 14:11:16 +01001140 * so that only the values in init_pins are restored
1141 */
Takashi Iwai346ff702009-02-23 09:42:57 +01001142 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001143#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +01001144 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001145#endif
1146 restore_pincfgs(codec);
1147 snd_array_free(&codec->init_pins);
1148}
1149
Takashi Iwai54d17402005-11-21 16:33:22 +01001150/*
Takashi Iwaieb541332010-08-06 13:48:11 +02001151 * audio-converter setup caches
1152 */
1153struct hda_cvt_setup {
1154 hda_nid_t nid;
1155 u8 stream_tag;
1156 u8 channel_id;
1157 u16 format_id;
1158 unsigned char active; /* cvt is currently used */
1159 unsigned char dirty; /* setups should be cleared */
1160};
1161
1162/* get or create a cache entry for the given audio converter NID */
1163static struct hda_cvt_setup *
1164get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1165{
1166 struct hda_cvt_setup *p;
1167 int i;
1168
1169 for (i = 0; i < codec->cvt_setups.used; i++) {
1170 p = snd_array_elem(&codec->cvt_setups, i);
1171 if (p->nid == nid)
1172 return p;
1173 }
1174 p = snd_array_new(&codec->cvt_setups);
1175 if (p)
1176 p->nid = nid;
1177 return p;
1178}
1179
1180/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 * codec destructor
1182 */
1183static void snd_hda_codec_free(struct hda_codec *codec)
1184{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001185 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 return;
Takashi Iwai59cad162012-06-26 15:00:20 +02001187 snd_hda_jack_tbl_clear(codec);
Takashi Iwai3be14142009-02-20 14:11:16 +01001188 restore_init_pincfgs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001189#ifdef CONFIG_SND_HDA_POWER_SAVE
1190 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01001191 flush_workqueue(codec->bus->workq);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001192#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001194 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001195 snd_array_free(&codec->nids);
Takashi Iwai59cad162012-06-26 15:00:20 +02001196 snd_array_free(&codec->cvt_setups);
Takashi Iwaia12d3e12011-04-07 15:55:15 +02001197 snd_array_free(&codec->conn_lists);
Stephen Warren7c9359762011-06-01 11:14:17 -06001198 snd_array_free(&codec->spdif_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 codec->bus->caddr_tbl[codec->addr] = NULL;
1200 if (codec->patch_ops.free)
1201 codec->patch_ops.free(codec);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001202 module_put(codec->owner);
Takashi Iwai01751f52007-08-10 16:59:39 +02001203 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001204 free_hda_cache(&codec->cmd_cache);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001205 kfree(codec->vendor_name);
1206 kfree(codec->chip_name);
Takashi Iwaif44ac832008-07-30 15:01:45 +02001207 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +01001208 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 kfree(codec);
1210}
1211
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001212static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1213 unsigned int power_state);
1214
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215/**
1216 * snd_hda_codec_new - create a HDA codec
1217 * @bus: the bus to assign
1218 * @codec_addr: the codec address
1219 * @codecp: the pointer to store the generated codec
1220 *
1221 * Returns 0 if successful, or a negative error code.
1222 */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001223int /*__devinit*/ snd_hda_codec_new(struct hda_bus *bus,
1224 unsigned int codec_addr,
1225 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226{
1227 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +02001228 char component[31];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 int err;
1230
Takashi Iwaida3cec32008-08-08 17:12:14 +02001231 if (snd_BUG_ON(!bus))
1232 return -EINVAL;
1233 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1234 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
1236 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001237 snd_printk(KERN_ERR "hda_codec: "
1238 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 return -EBUSY;
1240 }
1241
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001242 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 if (codec == NULL) {
1244 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1245 return -ENOMEM;
1246 }
1247
1248 codec->bus = bus;
1249 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001250 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001251 mutex_init(&codec->control_mutex);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001252 mutex_init(&codec->hash_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +02001253 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001254 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001255 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1256 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001257 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001258 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +02001259 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Takashi Iwaia12d3e12011-04-07 15:55:15 +02001260 snd_array_init(&codec->conn_lists, sizeof(hda_nid_t), 64);
Stephen Warren7c9359762011-06-01 11:14:17 -06001261 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Takashi Iwaicb53c622007-08-10 17:21:45 +02001263#ifdef CONFIG_SND_HDA_POWER_SAVE
Takashi Iwai5536c6d2012-05-08 17:08:10 +02001264 spin_lock_init(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001265 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1266 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1267 * the caller has to power down appropriatley after initialization
1268 * phase.
1269 */
1270 hda_keep_power_on(codec);
1271#endif
1272
Takashi Iwaic382a9f2012-05-07 15:01:02 +02001273 if (codec->bus->modelname) {
1274 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1275 if (!codec->modelname) {
1276 snd_hda_codec_free(codec);
1277 return -ENODEV;
1278 }
1279 }
1280
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 list_add_tail(&codec->list, &bus->codec_list);
1282 bus->caddr_tbl[codec_addr] = codec;
1283
Takashi Iwai0ba21762007-04-16 11:29:14 +02001284 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1285 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001286 if (codec->vendor_id == -1)
1287 /* read again, hopefully the access method was corrected
1288 * in the last read...
1289 */
1290 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1291 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001292 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1293 AC_PAR_SUBSYSTEM_ID);
1294 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1295 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001297 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001298 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001299 snd_printdd("hda_codec: no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001300 err = -ENODEV;
1301 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 }
1303
Takashi Iwai3be14142009-02-20 14:11:16 +01001304 err = read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg);
1305 if (err < 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +01001306 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001307 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001308 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001309 err = read_pin_defaults(codec);
1310 if (err < 0)
1311 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001312
Takashi Iwai0ba21762007-04-16 11:29:14 +02001313 if (!codec->subsystem_id) {
Takashi Iwai86284e42005-10-11 15:05:54 +02001314 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001315 codec->subsystem_id =
1316 snd_hda_codec_read(codec, nid, 0,
1317 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001318 }
1319
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001320 /* power-up all before initialization */
1321 hda_set_power_state(codec,
1322 codec->afg ? codec->afg : codec->mfg,
1323 AC_PWRST_D0);
1324
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001325 snd_hda_codec_proc_new(codec);
1326
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001327 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001328
1329 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1330 codec->subsystem_id, codec->revision_id);
1331 snd_component_add(codec->bus->card, component);
1332
1333 if (codecp)
1334 *codecp = codec;
1335 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001336
1337 error:
1338 snd_hda_codec_free(codec);
1339 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001340}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001341EXPORT_SYMBOL_HDA(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001342
Takashi Iwaid5191e52009-11-16 14:58:17 +01001343/**
1344 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1345 * @codec: the HDA codec
1346 *
1347 * Start parsing of the given codec tree and (re-)initialize the whole
1348 * patch instance.
1349 *
1350 * Returns 0 if successful or a negative error code.
1351 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001352int snd_hda_codec_configure(struct hda_codec *codec)
1353{
1354 int err;
1355
Takashi Iwaid5ad6302007-03-07 15:55:59 +01001356 codec->preset = find_codec_preset(codec);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001357 if (!codec->vendor_name || !codec->chip_name) {
Takashi Iwaif44ac832008-07-30 15:01:45 +02001358 err = get_codec_name(codec);
1359 if (err < 0)
1360 return err;
1361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
Takashi Iwai82467612007-07-27 19:15:54 +02001363 if (is_generic_config(codec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai82467612007-07-27 19:15:54 +02001365 goto patched;
1366 }
Takashi Iwai82467612007-07-27 19:15:54 +02001367 if (codec->preset && codec->preset->patch) {
1368 err = codec->preset->patch(codec);
1369 goto patched;
1370 }
1371
1372 /* call the default parser */
Takashi Iwai82467612007-07-27 19:15:54 +02001373 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai35a1e0c2007-10-19 08:13:40 +02001374 if (err < 0)
1375 printk(KERN_ERR "hda-codec: No codec parser is available\n");
Takashi Iwai82467612007-07-27 19:15:54 +02001376
1377 patched:
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001378 if (!err && codec->patch_ops.unsol_event)
1379 err = init_unsol_queue(codec->bus);
Takashi Iwaif62faed2009-12-23 09:27:51 +01001380 /* audio codec should override the mixer name */
1381 if (!err && (codec->afg || !*codec->bus->card->mixername))
1382 snprintf(codec->bus->card->mixername,
1383 sizeof(codec->bus->card->mixername),
1384 "%s %s", codec->vendor_name, codec->chip_name);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001385 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386}
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001387EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
Takashi Iwaied360812012-08-08 17:12:52 +02001389/* update the stream-id if changed */
1390static void update_pcm_stream_id(struct hda_codec *codec,
1391 struct hda_cvt_setup *p, hda_nid_t nid,
1392 u32 stream_tag, int channel_id)
1393{
1394 unsigned int oldval, newval;
1395
1396 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1397 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1398 newval = (stream_tag << 4) | channel_id;
1399 if (oldval != newval)
1400 snd_hda_codec_write(codec, nid, 0,
1401 AC_VERB_SET_CHANNEL_STREAMID,
1402 newval);
1403 p->stream_tag = stream_tag;
1404 p->channel_id = channel_id;
1405 }
1406}
1407
1408/* update the format-id if changed */
1409static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1410 hda_nid_t nid, int format)
1411{
1412 unsigned int oldval;
1413
1414 if (p->format_id != format) {
1415 oldval = snd_hda_codec_read(codec, nid, 0,
1416 AC_VERB_GET_STREAM_FORMAT, 0);
1417 if (oldval != format) {
1418 msleep(1);
1419 snd_hda_codec_write(codec, nid, 0,
1420 AC_VERB_SET_STREAM_FORMAT,
1421 format);
1422 }
1423 p->format_id = format;
1424 }
1425}
1426
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427/**
1428 * snd_hda_codec_setup_stream - set up the codec for streaming
1429 * @codec: the CODEC to set up
1430 * @nid: the NID to set up
1431 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1432 * @channel_id: channel id to pass, zero based.
1433 * @format: stream format.
1434 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001435void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1436 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 int channel_id, int format)
1438{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001439 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001440 struct hda_cvt_setup *p;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001441 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001442 int i;
1443
Takashi Iwai0ba21762007-04-16 11:29:14 +02001444 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001445 return;
1446
Takashi Iwai0ba21762007-04-16 11:29:14 +02001447 snd_printdd("hda_codec_setup_stream: "
1448 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001450 p = get_hda_cvt_setup(codec, nid);
1451 if (!p)
1452 return;
Takashi Iwaied360812012-08-08 17:12:52 +02001453
1454 if (codec->pcm_format_first)
1455 update_pcm_format(codec, p, nid, format);
1456 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1457 if (!codec->pcm_format_first)
1458 update_pcm_format(codec, p, nid, format);
1459
Takashi Iwaieb541332010-08-06 13:48:11 +02001460 p->active = 1;
1461 p->dirty = 0;
1462
1463 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001464 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001465 list_for_each_entry(c, &codec->bus->codec_list, list) {
1466 for (i = 0; i < c->cvt_setups.used; i++) {
1467 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001468 if (!p->active && p->stream_tag == stream_tag &&
David Henningsson54c2a892012-02-01 12:05:41 +01001469 get_wcaps_type(get_wcaps(c, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001470 p->dirty = 1;
1471 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001472 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001474EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
Takashi Iwaif0cea792010-08-13 11:56:53 +02001476static void really_cleanup_stream(struct hda_codec *codec,
1477 struct hda_cvt_setup *q);
1478
Takashi Iwaid5191e52009-11-16 14:58:17 +01001479/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001480 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001481 * @codec: the CODEC to clean up
1482 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001483 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001484 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001485void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1486 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001487{
Takashi Iwaieb541332010-08-06 13:48:11 +02001488 struct hda_cvt_setup *p;
1489
Takashi Iwai888afa12008-03-18 09:57:50 +01001490 if (!nid)
1491 return;
1492
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001493 if (codec->no_sticky_stream)
1494 do_now = 1;
1495
Takashi Iwai888afa12008-03-18 09:57:50 +01001496 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001497 p = get_hda_cvt_setup(codec, nid);
Takashi Iwaif0cea792010-08-13 11:56:53 +02001498 if (p) {
1499 /* here we just clear the active flag when do_now isn't set;
1500 * actual clean-ups will be done later in
1501 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1502 */
1503 if (do_now)
1504 really_cleanup_stream(codec, p);
1505 else
1506 p->active = 0;
1507 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001508}
Takashi Iwaif0cea792010-08-13 11:56:53 +02001509EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001510
Takashi Iwaieb541332010-08-06 13:48:11 +02001511static void really_cleanup_stream(struct hda_codec *codec,
1512 struct hda_cvt_setup *q)
1513{
1514 hda_nid_t nid = q->nid;
Takashi Iwai218264a2011-09-27 17:33:45 +02001515 if (q->stream_tag || q->channel_id)
1516 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1517 if (q->format_id)
1518 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1519);
Takashi Iwaieb541332010-08-06 13:48:11 +02001520 memset(q, 0, sizeof(*q));
1521 q->nid = nid;
1522}
1523
1524/* clean up the all conflicting obsolete streams */
1525static void purify_inactive_streams(struct hda_codec *codec)
1526{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001527 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001528 int i;
1529
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001530 list_for_each_entry(c, &codec->bus->codec_list, list) {
1531 for (i = 0; i < c->cvt_setups.used; i++) {
1532 struct hda_cvt_setup *p;
1533 p = snd_array_elem(&c->cvt_setups, i);
1534 if (p->dirty)
1535 really_cleanup_stream(c, p);
1536 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001537 }
1538}
1539
Takashi Iwai2a439522011-07-26 09:52:50 +02001540#ifdef CONFIG_PM
Takashi Iwaieb541332010-08-06 13:48:11 +02001541/* clean up all streams; called from suspend */
1542static void hda_cleanup_all_streams(struct hda_codec *codec)
1543{
1544 int i;
1545
1546 for (i = 0; i < codec->cvt_setups.used; i++) {
1547 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1548 if (p->stream_tag)
1549 really_cleanup_stream(codec, p);
1550 }
1551}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001552#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001553
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554/*
1555 * amp access functions
1556 */
1557
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001558/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001559#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001560#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001561#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1562#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001564#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
1566/* initialize the hash table */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001567static void /*__devinit*/ init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001568 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569{
Takashi Iwai01751f52007-08-10 16:59:39 +02001570 memset(cache, 0, sizeof(*cache));
1571 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001572 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001573}
1574
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001575static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001576{
Takashi Iwai603c4012008-07-30 15:01:44 +02001577 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578}
1579
1580/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001581static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582{
Takashi Iwai01751f52007-08-10 16:59:39 +02001583 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1584 u16 cur = cache->hash[idx];
1585 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
1587 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001588 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 if (info->key == key)
1590 return info;
1591 cur = info->next;
1592 }
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001593 return NULL;
1594}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001596/* query the hash. allocate an entry if not found. */
1597static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1598 u32 key)
1599{
1600 struct hda_cache_head *info = get_hash(cache, key);
1601 if (!info) {
1602 u16 idx, cur;
1603 /* add a new hash entry */
1604 info = snd_array_new(&cache->buf);
1605 if (!info)
1606 return NULL;
1607 cur = snd_array_index(&cache->buf, info);
1608 info->key = key;
1609 info->val = 0;
1610 idx = key % (u16)ARRAY_SIZE(cache->hash);
1611 info->next = cache->hash[idx];
1612 cache->hash[idx] = cur;
1613 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 return info;
1615}
1616
Takashi Iwai01751f52007-08-10 16:59:39 +02001617/* query and allocate an amp hash entry */
1618static inline struct hda_amp_info *
1619get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1620{
1621 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1622}
1623
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001624/* overwrite the value with the key in the caps hash */
1625static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val)
1626{
1627 struct hda_amp_info *info;
1628
1629 mutex_lock(&codec->hash_mutex);
1630 info = get_alloc_amp_hash(codec, key);
1631 if (!info) {
1632 mutex_unlock(&codec->hash_mutex);
1633 return -EINVAL;
1634 }
1635 info->amp_caps = val;
1636 info->head.val |= INFO_AMP_CAPS;
1637 mutex_unlock(&codec->hash_mutex);
1638 return 0;
1639}
1640
1641/* query the value from the caps hash; if not found, fetch the current
1642 * value from the given function and store in the hash
1643 */
1644static unsigned int
1645query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key,
1646 unsigned int (*func)(struct hda_codec *, hda_nid_t, int))
1647{
1648 struct hda_amp_info *info;
1649 unsigned int val;
1650
1651 mutex_lock(&codec->hash_mutex);
1652 info = get_alloc_amp_hash(codec, key);
1653 if (!info) {
1654 mutex_unlock(&codec->hash_mutex);
1655 return 0;
1656 }
1657 if (!(info->head.val & INFO_AMP_CAPS)) {
1658 mutex_unlock(&codec->hash_mutex); /* for reentrance */
1659 val = func(codec, nid, dir);
1660 write_caps_hash(codec, key, val);
1661 } else {
1662 val = info->amp_caps;
1663 mutex_unlock(&codec->hash_mutex);
1664 }
1665 return val;
1666}
1667
1668static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid,
1669 int direction)
1670{
1671 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1672 nid = codec->afg;
1673 return snd_hda_param_read(codec, nid,
1674 direction == HDA_OUTPUT ?
1675 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
1676}
1677
Takashi Iwaid5191e52009-11-16 14:58:17 +01001678/**
1679 * query_amp_caps - query AMP capabilities
1680 * @codec: the HD-auio codec
1681 * @nid: the NID to query
1682 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1683 *
1684 * Query AMP capabilities for the given widget and direction.
1685 * Returns the obtained capability bits.
1686 *
1687 * When cap bits have been already read, this doesn't read again but
1688 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001690u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001692 return query_caps_hash(codec, nid, direction,
1693 HDA_HASH_KEY(nid, direction, 0),
1694 read_amp_cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001696EXPORT_SYMBOL_HDA(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
Takashi Iwaid5191e52009-11-16 14:58:17 +01001698/**
1699 * snd_hda_override_amp_caps - Override the AMP capabilities
1700 * @codec: the CODEC to clean up
1701 * @nid: the NID to clean up
1702 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1703 * @caps: the capability bits to set
1704 *
1705 * Override the cached AMP caps bits value by the given one.
1706 * This function is useful if the driver needs to adjust the AMP ranges,
1707 * e.g. limit to 0dB, etc.
1708 *
1709 * Returns zero if successful or a negative error code.
1710 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001711int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1712 unsigned int caps)
1713{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001714 return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001715}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001716EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001717
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001718static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid,
1719 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001720{
1721 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1722}
1723
Takashi Iwaid5191e52009-11-16 14:58:17 +01001724/**
1725 * snd_hda_query_pin_caps - Query PIN capabilities
1726 * @codec: the HD-auio codec
1727 * @nid: the NID to query
1728 *
1729 * Query PIN capabilities for the given widget.
1730 * Returns the obtained capability bits.
1731 *
1732 * When cap bits have been already read, this doesn't read again but
1733 * returns the cached value.
1734 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001735u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1736{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001737 return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001738 read_pin_cap);
1739}
Takashi Iwai1327a322009-03-23 13:07:47 +01001740EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1741
Wu Fengguang864f92b2009-11-18 12:38:02 +08001742/**
Takashi Iwaif57c2562011-08-15 12:49:07 +02001743 * snd_hda_override_pin_caps - Override the pin capabilities
1744 * @codec: the CODEC
1745 * @nid: the NID to override
1746 * @caps: the capability bits to set
1747 *
1748 * Override the cached PIN capabilitiy bits value by the given one.
1749 *
1750 * Returns zero if successful or a negative error code.
1751 */
1752int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
1753 unsigned int caps)
1754{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001755 return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps);
Takashi Iwaif57c2562011-08-15 12:49:07 +02001756}
1757EXPORT_SYMBOL_HDA(snd_hda_override_pin_caps);
1758
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001759/* read or sync the hash value with the current value;
1760 * call within hash_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 */
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001762static struct hda_amp_info *
1763update_amp_hash(struct hda_codec *codec, hda_nid_t nid, int ch,
1764 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001766 struct hda_amp_info *info;
1767 unsigned int parm, val = 0;
1768 bool val_read = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001770 retry:
1771 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1772 if (!info)
1773 return NULL;
1774 if (!(info->head.val & INFO_AMP_VOL(ch))) {
1775 if (!val_read) {
1776 mutex_unlock(&codec->hash_mutex);
1777 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1778 parm |= direction == HDA_OUTPUT ?
1779 AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1780 parm |= index;
1781 val = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001782 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001783 val &= 0xff;
1784 val_read = true;
1785 mutex_lock(&codec->hash_mutex);
1786 goto retry;
1787 }
1788 info->vol[ch] = val;
1789 info->head.val |= INFO_AMP_VOL(ch);
1790 }
1791 return info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792}
1793
1794/*
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001795 * write the current volume in info to the h/w
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001797static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001798 hda_nid_t nid, int ch, int direction, int index,
1799 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800{
1801 u32 parm;
1802
1803 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1804 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1805 parm |= index << AC_AMP_SET_INDEX_SHIFT;
Takashi Iwai38681372012-02-27 15:00:58 +01001806 if ((val & HDA_AMP_MUTE) && !(info->amp_caps & AC_AMPCAP_MUTE) &&
1807 (info->amp_caps & AC_AMPCAP_MIN_MUTE))
1808 ; /* set the zero value as a fake mute */
1809 else
1810 parm |= val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
1812}
1813
Takashi Iwaid5191e52009-11-16 14:58:17 +01001814/**
1815 * snd_hda_codec_amp_read - Read AMP value
1816 * @codec: HD-audio codec
1817 * @nid: NID to read the AMP value
1818 * @ch: channel (left=0 or right=1)
1819 * @direction: #HDA_INPUT or #HDA_OUTPUT
1820 * @index: the index value (only for input direction)
1821 *
1822 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 */
Takashi Iwai834be882006-03-01 14:16:17 +01001824int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1825 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001827 struct hda_amp_info *info;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001828 unsigned int val = 0;
1829
1830 mutex_lock(&codec->hash_mutex);
1831 info = update_amp_hash(codec, nid, ch, direction, index);
1832 if (info)
1833 val = info->vol[ch];
1834 mutex_unlock(&codec->hash_mutex);
1835 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001837EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
Takashi Iwaid5191e52009-11-16 14:58:17 +01001839/**
1840 * snd_hda_codec_amp_update - update the AMP value
1841 * @codec: HD-audio codec
1842 * @nid: NID to read the AMP value
1843 * @ch: channel (left=0 or right=1)
1844 * @direction: #HDA_INPUT or #HDA_OUTPUT
1845 * @idx: the index value (only for input direction)
1846 * @mask: bit mask to set
1847 * @val: the bits value to set
1848 *
1849 * Update the AMP value with a bit mask.
1850 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001851 */
Takashi Iwai834be882006-03-01 14:16:17 +01001852int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1853 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001855 struct hda_amp_info *info;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001856
Takashi Iwai467126462010-03-29 09:19:38 +02001857 if (snd_BUG_ON(mask & ~0xff))
1858 mask &= 0xff;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001859 val &= mask;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001860
1861 mutex_lock(&codec->hash_mutex);
1862 info = update_amp_hash(codec, nid, ch, direction, idx);
1863 if (!info) {
1864 mutex_unlock(&codec->hash_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 return 0;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001866 }
1867 val |= info->vol[ch] & ~mask;
1868 if (info->vol[ch] == val) {
1869 mutex_unlock(&codec->hash_mutex);
1870 return 0;
1871 }
1872 info->vol[ch] = val;
1873 mutex_unlock(&codec->hash_mutex);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001874 put_vol_mute(codec, info, nid, ch, direction, idx, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 return 1;
1876}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001877EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
Takashi Iwaid5191e52009-11-16 14:58:17 +01001879/**
1880 * snd_hda_codec_amp_stereo - update the AMP stereo values
1881 * @codec: HD-audio codec
1882 * @nid: NID to read the AMP value
1883 * @direction: #HDA_INPUT or #HDA_OUTPUT
1884 * @idx: the index value (only for input direction)
1885 * @mask: bit mask to set
1886 * @val: the bits value to set
1887 *
1888 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1889 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02001890 */
1891int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1892 int direction, int idx, int mask, int val)
1893{
1894 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001895
1896 if (snd_BUG_ON(mask & ~0xff))
1897 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02001898 for (ch = 0; ch < 2; ch++)
1899 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1900 idx, mask, val);
1901 return ret;
1902}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001903EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001904
Takashi Iwai2a439522011-07-26 09:52:50 +02001905#ifdef CONFIG_PM
Takashi Iwaid5191e52009-11-16 14:58:17 +01001906/**
1907 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
1908 * @codec: HD-audio codec
1909 *
1910 * Resume the all amp commands from the cache.
1911 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001912void snd_hda_codec_resume_amp(struct hda_codec *codec)
1913{
Takashi Iwai603c4012008-07-30 15:01:44 +02001914 struct hda_amp_info *buffer = codec->amp_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001915 int i;
1916
Takashi Iwai603c4012008-07-30 15:01:44 +02001917 for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001918 u32 key = buffer->head.key;
1919 hda_nid_t nid;
1920 unsigned int idx, dir, ch;
1921 if (!key)
1922 continue;
1923 nid = key & 0xff;
1924 idx = (key >> 16) & 0xff;
1925 dir = (key >> 24) & 0xff;
1926 for (ch = 0; ch < 2; ch++) {
1927 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
1928 continue;
1929 put_vol_mute(codec, buffer, nid, ch, dir, idx,
1930 buffer->vol[ch]);
1931 }
1932 }
1933}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001934EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
Takashi Iwai2a439522011-07-26 09:52:50 +02001935#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001937static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1938 unsigned int ofs)
1939{
1940 u32 caps = query_amp_caps(codec, nid, dir);
1941 /* get num steps */
1942 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1943 if (ofs < caps)
1944 caps -= ofs;
1945 return caps;
1946}
1947
Takashi Iwaid5191e52009-11-16 14:58:17 +01001948/**
1949 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
1950 *
1951 * The control element is supposed to have the private_value field
1952 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1953 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001954int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1955 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956{
1957 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1958 u16 nid = get_amp_nid(kcontrol);
1959 u8 chs = get_amp_channels(kcontrol);
1960 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001961 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001963 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1964 uinfo->count = chs == 3 ? 2 : 1;
1965 uinfo->value.integer.min = 0;
1966 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
1967 if (!uinfo->value.integer.max) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001968 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01001969 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
1970 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 return -EINVAL;
1972 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 return 0;
1974}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001975EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001977
1978static inline unsigned int
1979read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1980 int ch, int dir, int idx, unsigned int ofs)
1981{
1982 unsigned int val;
1983 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1984 val &= HDA_AMP_VOLMASK;
1985 if (val >= ofs)
1986 val -= ofs;
1987 else
1988 val = 0;
1989 return val;
1990}
1991
1992static inline int
1993update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1994 int ch, int dir, int idx, unsigned int ofs,
1995 unsigned int val)
1996{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001997 unsigned int maxval;
1998
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001999 if (val > 0)
2000 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02002001 /* ofs = 0: raw max value */
2002 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002003 if (val > maxval)
2004 val = maxval;
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002005 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
2006 HDA_AMP_VOLMASK, val);
2007}
2008
Takashi Iwaid5191e52009-11-16 14:58:17 +01002009/**
2010 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
2011 *
2012 * The control element is supposed to have the private_value field
2013 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2014 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002015int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
2016 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017{
2018 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2019 hda_nid_t nid = get_amp_nid(kcontrol);
2020 int chs = get_amp_channels(kcontrol);
2021 int dir = get_amp_direction(kcontrol);
2022 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002023 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 long *valp = ucontrol->value.integer.value;
2025
2026 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002027 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002029 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 return 0;
2031}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002032EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
Takashi Iwaid5191e52009-11-16 14:58:17 +01002034/**
2035 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
2036 *
2037 * The control element is supposed to have the private_value field
2038 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2039 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002040int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
2041 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042{
2043 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2044 hda_nid_t nid = get_amp_nid(kcontrol);
2045 int chs = get_amp_channels(kcontrol);
2046 int dir = get_amp_direction(kcontrol);
2047 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002048 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 long *valp = ucontrol->value.integer.value;
2050 int change = 0;
2051
Takashi Iwaicb53c622007-08-10 17:21:45 +02002052 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002053 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002054 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002055 valp++;
2056 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002057 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002058 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002059 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 return change;
2061}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002062EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063
Takashi Iwaid5191e52009-11-16 14:58:17 +01002064/**
2065 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2066 *
2067 * The control element is supposed to have the private_value field
2068 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2069 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002070int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2071 unsigned int size, unsigned int __user *_tlv)
2072{
2073 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2074 hda_nid_t nid = get_amp_nid(kcontrol);
2075 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002076 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02002077 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002078 u32 caps, val1, val2;
2079
2080 if (size < 4 * sizeof(unsigned int))
2081 return -ENOMEM;
2082 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002083 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2084 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002085 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002086 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002087 val1 = ((int)val1) * ((int)val2);
Takashi Iwai38681372012-02-27 15:00:58 +01002088 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwaic08d9162010-10-17 10:40:53 +02002089 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002090 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2091 return -EFAULT;
2092 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2093 return -EFAULT;
2094 if (put_user(val1, _tlv + 2))
2095 return -EFAULT;
2096 if (put_user(val2, _tlv + 3))
2097 return -EFAULT;
2098 return 0;
2099}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002100EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002101
Takashi Iwaid5191e52009-11-16 14:58:17 +01002102/**
2103 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2104 * @codec: HD-audio codec
2105 * @nid: NID of a reference widget
2106 * @dir: #HDA_INPUT or #HDA_OUTPUT
2107 * @tlv: TLV data to be stored, at least 4 elements
2108 *
2109 * Set (static) TLV data for a virtual master volume using the AMP caps
2110 * obtained from the reference NID.
2111 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01002112 */
2113void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2114 unsigned int *tlv)
2115{
2116 u32 caps;
2117 int nums, step;
2118
2119 caps = query_amp_caps(codec, nid, dir);
2120 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2121 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2122 step = (step + 1) * 25;
2123 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2124 tlv[1] = 2 * sizeof(unsigned int);
2125 tlv[2] = -nums * step;
2126 tlv[3] = step;
2127}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002128EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002129
2130/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01002131static struct snd_kcontrol *
2132_snd_hda_find_mixer_ctl(struct hda_codec *codec,
2133 const char *name, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002134{
2135 struct snd_ctl_elem_id id;
2136 memset(&id, 0, sizeof(id));
2137 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwai09f99702008-02-04 12:31:13 +01002138 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02002139 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2140 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002141 strcpy(id.name, name);
2142 return snd_ctl_find_id(codec->bus->card, &id);
2143}
2144
Takashi Iwaid5191e52009-11-16 14:58:17 +01002145/**
2146 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2147 * @codec: HD-audio codec
2148 * @name: ctl id name string
2149 *
2150 * Get the control element with the given id string and IFACE_MIXER.
2151 */
Takashi Iwai09f99702008-02-04 12:31:13 +01002152struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2153 const char *name)
2154{
2155 return _snd_hda_find_mixer_ctl(codec, name, 0);
2156}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002157EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01002158
Takashi Iwai1afe2062010-12-23 10:17:52 +01002159static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name)
2160{
2161 int idx;
2162 for (idx = 0; idx < 16; idx++) { /* 16 ctlrs should be large enough */
2163 if (!_snd_hda_find_mixer_ctl(codec, name, idx))
2164 return idx;
2165 }
2166 return -EBUSY;
2167}
2168
Takashi Iwaid5191e52009-11-16 14:58:17 +01002169/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002170 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01002171 * @codec: HD-audio codec
2172 * @nid: corresponding NID (optional)
2173 * @kctl: the control element to assign
2174 *
2175 * Add the given control element to an array inside the codec instance.
2176 * All control elements belonging to a codec are supposed to be added
2177 * by this function so that a proper clean-up works at the free or
2178 * reconfiguration time.
2179 *
2180 * If non-zero @nid is passed, the NID is assigned to the control element.
2181 * The assignment is shown in the codec proc file.
2182 *
2183 * snd_hda_ctl_add() checks the control subdev id field whether
2184 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002185 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2186 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01002187 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002188int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2189 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002190{
2191 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002192 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002193 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002194
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002195 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002196 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002197 if (nid == 0)
2198 nid = get_amp_nid_(kctl->private_value);
2199 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002200 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2201 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002202 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01002203 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002204 err = snd_ctl_add(codec->bus->card, kctl);
2205 if (err < 0)
2206 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002207 item = snd_array_new(&codec->mixers);
2208 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002209 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002210 item->kctl = kctl;
2211 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002212 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002213 return 0;
2214}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002215EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002216
Takashi Iwaid5191e52009-11-16 14:58:17 +01002217/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002218 * snd_hda_add_nid - Assign a NID to a control element
2219 * @codec: HD-audio codec
2220 * @nid: corresponding NID (optional)
2221 * @kctl: the control element to assign
2222 * @index: index to kctl
2223 *
2224 * Add the given control element to an array inside the codec instance.
2225 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2226 * NID:KCTL mapping - for example "Capture Source" selector.
2227 */
2228int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2229 unsigned int index, hda_nid_t nid)
2230{
2231 struct hda_nid_item *item;
2232
2233 if (nid > 0) {
2234 item = snd_array_new(&codec->nids);
2235 if (!item)
2236 return -ENOMEM;
2237 item->kctl = kctl;
2238 item->index = index;
2239 item->nid = nid;
2240 return 0;
2241 }
Takashi Iwai28d1a852010-03-15 09:05:46 +01002242 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
2243 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002244 return -EINVAL;
2245}
2246EXPORT_SYMBOL_HDA(snd_hda_add_nid);
2247
2248/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002249 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2250 * @codec: HD-audio codec
2251 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002252void snd_hda_ctls_clear(struct hda_codec *codec)
2253{
2254 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002255 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002256 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002257 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002258 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002259 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002260}
2261
Takashi Iwaia65d6292009-02-23 16:57:04 +01002262/* pseudo device locking
2263 * toggle card->shutdown to allow/disallow the device access (as a hack)
2264 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002265int snd_hda_lock_devices(struct hda_bus *bus)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002266{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002267 struct snd_card *card = bus->card;
2268 struct hda_codec *codec;
2269
Takashi Iwaia65d6292009-02-23 16:57:04 +01002270 spin_lock(&card->files_lock);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002271 if (card->shutdown)
2272 goto err_unlock;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002273 card->shutdown = 1;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002274 if (!list_empty(&card->ctl_files))
2275 goto err_clear;
2276
2277 list_for_each_entry(codec, &bus->codec_list, list) {
2278 int pcm;
2279 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2280 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2281 if (!cpcm->pcm)
2282 continue;
2283 if (cpcm->pcm->streams[0].substream_opened ||
2284 cpcm->pcm->streams[1].substream_opened)
2285 goto err_clear;
2286 }
2287 }
Takashi Iwaia65d6292009-02-23 16:57:04 +01002288 spin_unlock(&card->files_lock);
2289 return 0;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002290
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002291 err_clear:
2292 card->shutdown = 0;
2293 err_unlock:
2294 spin_unlock(&card->files_lock);
2295 return -EINVAL;
2296}
2297EXPORT_SYMBOL_HDA(snd_hda_lock_devices);
2298
2299void snd_hda_unlock_devices(struct hda_bus *bus)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002300{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002301 struct snd_card *card = bus->card;
2302
2303 card = bus->card;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002304 spin_lock(&card->files_lock);
2305 card->shutdown = 0;
2306 spin_unlock(&card->files_lock);
2307}
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002308EXPORT_SYMBOL_HDA(snd_hda_unlock_devices);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002309
Takashi Iwaid5191e52009-11-16 14:58:17 +01002310/**
2311 * snd_hda_codec_reset - Clear all objects assigned to the codec
2312 * @codec: HD-audio codec
2313 *
2314 * This frees the all PCM and control elements assigned to the codec, and
2315 * clears the caches and restores the pin default configurations.
2316 *
2317 * When a device is being used, it returns -EBSY. If successfully freed,
2318 * returns zero.
2319 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002320int snd_hda_codec_reset(struct hda_codec *codec)
2321{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002322 struct hda_bus *bus = codec->bus;
2323 struct snd_card *card = bus->card;
2324 int i;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002325
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002326 if (snd_hda_lock_devices(bus) < 0)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002327 return -EBUSY;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002328
2329 /* OK, let it free */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002330
2331#ifdef CONFIG_SND_HDA_POWER_SAVE
Takashi Iwaia2d96e72012-05-09 12:36:22 +02002332 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwai339876d2012-05-08 16:57:12 +02002333 codec->power_on = 0;
2334 codec->power_transition = 0;
2335 codec->power_jiffies = jiffies;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002336 flush_workqueue(bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002337#endif
2338 snd_hda_ctls_clear(codec);
2339 /* relase PCMs */
2340 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002341 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01002342 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002343 clear_bit(codec->pcm_info[i].device,
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002344 bus->pcm_dev_bits);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002345 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002346 }
2347 if (codec->patch_ops.free)
2348 codec->patch_ops.free(codec);
Takashi Iwai1835a0f2011-10-27 22:12:46 +02002349 snd_hda_jack_tbl_clear(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01002350 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002351 codec->spec = NULL;
2352 free_hda_cache(&codec->amp_cache);
2353 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01002354 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2355 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01002356 /* free only driver_pins so that init_pins + user_pins are restored */
2357 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01002358 restore_pincfgs(codec);
Takashi Iwai09a60712012-06-26 15:01:33 +02002359 snd_array_free(&codec->cvt_setups);
2360 snd_array_free(&codec->spdif_out);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002361 codec->num_pcms = 0;
2362 codec->pcm_info = NULL;
2363 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01002364 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
2365 codec->slave_dig_outs = NULL;
2366 codec->spdif_status_reset = 0;
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002367 module_put(codec->owner);
2368 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002369
2370 /* allow device access again */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002371 snd_hda_unlock_devices(bus);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002372 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002373}
2374
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002375typedef int (*map_slave_func_t)(void *, struct snd_kcontrol *);
2376
2377/* apply the function to all matching slave ctls in the mixer list */
2378static int map_slaves(struct hda_codec *codec, const char * const *slaves,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002379 const char *suffix, map_slave_func_t func, void *data)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002380{
2381 struct hda_nid_item *items;
2382 const char * const *s;
2383 int i, err;
2384
2385 items = codec->mixers.list;
2386 for (i = 0; i < codec->mixers.used; i++) {
2387 struct snd_kcontrol *sctl = items[i].kctl;
2388 if (!sctl || !sctl->id.name ||
2389 sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
2390 continue;
2391 for (s = slaves; *s; s++) {
Takashi Iwai9322ca52012-02-03 14:28:01 +01002392 char tmpname[sizeof(sctl->id.name)];
2393 const char *name = *s;
2394 if (suffix) {
2395 snprintf(tmpname, sizeof(tmpname), "%s %s",
2396 name, suffix);
2397 name = tmpname;
2398 }
Takashi Iwai9322ca52012-02-03 14:28:01 +01002399 if (!strcmp(sctl->id.name, name)) {
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002400 err = func(data, sctl);
2401 if (err)
2402 return err;
2403 break;
2404 }
2405 }
2406 }
2407 return 0;
2408}
2409
2410static int check_slave_present(void *data, struct snd_kcontrol *sctl)
2411{
2412 return 1;
2413}
2414
Takashi Iwai18478e82012-03-09 17:51:10 +01002415/* guess the value corresponding to 0dB */
2416static int get_kctl_0dB_offset(struct snd_kcontrol *kctl)
2417{
2418 int _tlv[4];
2419 const int *tlv = NULL;
2420 int val = -1;
2421
2422 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2423 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2424 mm_segment_t fs = get_fs();
2425 set_fs(get_ds());
2426 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2427 tlv = _tlv;
2428 set_fs(fs);
2429 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2430 tlv = kctl->tlv.p;
2431 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE)
2432 val = -tlv[2] / tlv[3];
2433 return val;
2434}
2435
2436/* call kctl->put with the given value(s) */
2437static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2438{
2439 struct snd_ctl_elem_value *ucontrol;
2440 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2441 if (!ucontrol)
2442 return -ENOMEM;
2443 ucontrol->value.integer.value[0] = val;
2444 ucontrol->value.integer.value[1] = val;
2445 kctl->put(kctl, ucontrol);
2446 kfree(ucontrol);
2447 return 0;
2448}
2449
2450/* initialize the slave volume with 0dB */
2451static int init_slave_0dB(void *data, struct snd_kcontrol *slave)
2452{
2453 int offset = get_kctl_0dB_offset(slave);
2454 if (offset > 0)
2455 put_kctl_with_value(slave, offset);
2456 return 0;
2457}
2458
2459/* unmute the slave */
2460static int init_slave_unmute(void *data, struct snd_kcontrol *slave)
2461{
2462 return put_kctl_with_value(slave, 1);
2463}
2464
Takashi Iwaid5191e52009-11-16 14:58:17 +01002465/**
2466 * snd_hda_add_vmaster - create a virtual master control and add slaves
2467 * @codec: HD-audio codec
2468 * @name: vmaster control name
2469 * @tlv: TLV data (optional)
2470 * @slaves: slave control names (optional)
Takashi Iwai9322ca52012-02-03 14:28:01 +01002471 * @suffix: suffix string to each slave name (optional)
Takashi Iwai18478e82012-03-09 17:51:10 +01002472 * @init_slave_vol: initialize slaves to unmute/0dB
Takashi Iwai29e58532012-03-12 12:25:03 +01002473 * @ctl_ret: store the vmaster kcontrol in return
Takashi Iwaid5191e52009-11-16 14:58:17 +01002474 *
2475 * Create a virtual master control with the given name. The TLV data
2476 * must be either NULL or a valid data.
2477 *
2478 * @slaves is a NULL-terminated array of strings, each of which is a
2479 * slave control name. All controls with these names are assigned to
2480 * the new virtual master control.
2481 *
2482 * This function returns zero if successful or a negative error code.
2483 */
Takashi Iwai18478e82012-03-09 17:51:10 +01002484int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002485 unsigned int *tlv, const char * const *slaves,
Takashi Iwai29e58532012-03-12 12:25:03 +01002486 const char *suffix, bool init_slave_vol,
2487 struct snd_kcontrol **ctl_ret)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002488{
2489 struct snd_kcontrol *kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002490 int err;
2491
Takashi Iwai29e58532012-03-12 12:25:03 +01002492 if (ctl_ret)
2493 *ctl_ret = NULL;
2494
Takashi Iwai9322ca52012-02-03 14:28:01 +01002495 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002496 if (err != 1) {
Takashi Iwai2f085542008-02-22 18:43:50 +01002497 snd_printdd("No slave found for %s\n", name);
2498 return 0;
2499 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002500 kctl = snd_ctl_make_virtual_master(name, tlv);
2501 if (!kctl)
2502 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002503 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002504 if (err < 0)
2505 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002506
Takashi Iwai9322ca52012-02-03 14:28:01 +01002507 err = map_slaves(codec, slaves, suffix,
2508 (map_slave_func_t)snd_ctl_add_slave, kctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002509 if (err < 0)
2510 return err;
Takashi Iwai18478e82012-03-09 17:51:10 +01002511
2512 /* init with master mute & zero volume */
2513 put_kctl_with_value(kctl, 0);
2514 if (init_slave_vol)
2515 map_slaves(codec, slaves, suffix,
2516 tlv ? init_slave_0dB : init_slave_unmute, kctl);
2517
Takashi Iwai29e58532012-03-12 12:25:03 +01002518 if (ctl_ret)
2519 *ctl_ret = kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002520 return 0;
2521}
Takashi Iwai18478e82012-03-09 17:51:10 +01002522EXPORT_SYMBOL_HDA(__snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002523
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002524/*
2525 * mute-LED control using vmaster
2526 */
2527static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2528 struct snd_ctl_elem_info *uinfo)
2529{
2530 static const char * const texts[] = {
2531 "Off", "On", "Follow Master"
2532 };
2533 unsigned int index;
2534
2535 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2536 uinfo->count = 1;
2537 uinfo->value.enumerated.items = 3;
2538 index = uinfo->value.enumerated.item;
2539 if (index >= 3)
2540 index = 2;
2541 strcpy(uinfo->value.enumerated.name, texts[index]);
2542 return 0;
2543}
2544
2545static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2546 struct snd_ctl_elem_value *ucontrol)
2547{
2548 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2549 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2550 return 0;
2551}
2552
2553static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2554 struct snd_ctl_elem_value *ucontrol)
2555{
2556 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2557 unsigned int old_mode = hook->mute_mode;
2558
2559 hook->mute_mode = ucontrol->value.enumerated.item[0];
2560 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2561 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2562 if (old_mode == hook->mute_mode)
2563 return 0;
2564 snd_hda_sync_vmaster_hook(hook);
2565 return 1;
2566}
2567
2568static struct snd_kcontrol_new vmaster_mute_mode = {
2569 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2570 .name = "Mute-LED Mode",
2571 .info = vmaster_mute_mode_info,
2572 .get = vmaster_mute_mode_get,
2573 .put = vmaster_mute_mode_put,
2574};
2575
2576/*
2577 * Add a mute-LED hook with the given vmaster switch kctl
2578 * "Mute-LED Mode" control is automatically created and associated with
2579 * the given hook.
2580 */
2581int snd_hda_add_vmaster_hook(struct hda_codec *codec,
Takashi Iwaif29735c2012-03-13 07:55:10 +01002582 struct hda_vmaster_mute_hook *hook,
2583 bool expose_enum_ctl)
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002584{
2585 struct snd_kcontrol *kctl;
2586
2587 if (!hook->hook || !hook->sw_kctl)
2588 return 0;
2589 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
2590 hook->codec = codec;
2591 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
Takashi Iwaif29735c2012-03-13 07:55:10 +01002592 if (!expose_enum_ctl)
2593 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002594 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2595 if (!kctl)
2596 return -ENOMEM;
2597 return snd_hda_ctl_add(codec, 0, kctl);
2598}
2599EXPORT_SYMBOL_HDA(snd_hda_add_vmaster_hook);
2600
2601/*
2602 * Call the hook with the current value for synchronization
2603 * Should be called in init callback
2604 */
2605void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2606{
2607 if (!hook->hook || !hook->codec)
2608 return;
2609 switch (hook->mute_mode) {
2610 case HDA_VMUTE_FOLLOW_MASTER:
2611 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2612 break;
2613 default:
2614 hook->hook(hook->codec, hook->mute_mode);
2615 break;
2616 }
2617}
2618EXPORT_SYMBOL_HDA(snd_hda_sync_vmaster_hook);
2619
2620
Takashi Iwaid5191e52009-11-16 14:58:17 +01002621/**
2622 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2623 *
2624 * The control element is supposed to have the private_value field
2625 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2626 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002627int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2628 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629{
2630 int chs = get_amp_channels(kcontrol);
2631
2632 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2633 uinfo->count = chs == 3 ? 2 : 1;
2634 uinfo->value.integer.min = 0;
2635 uinfo->value.integer.max = 1;
2636 return 0;
2637}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002638EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639
Takashi Iwaid5191e52009-11-16 14:58:17 +01002640/**
2641 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2642 *
2643 * The control element is supposed to have the private_value field
2644 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2645 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002646int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2647 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648{
2649 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2650 hda_nid_t nid = get_amp_nid(kcontrol);
2651 int chs = get_amp_channels(kcontrol);
2652 int dir = get_amp_direction(kcontrol);
2653 int idx = get_amp_index(kcontrol);
2654 long *valp = ucontrol->value.integer.value;
2655
2656 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002657 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002658 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002660 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002661 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 return 0;
2663}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002664EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665
Takashi Iwaid5191e52009-11-16 14:58:17 +01002666/**
2667 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2668 *
2669 * The control element is supposed to have the private_value field
2670 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2671 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002672int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2673 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674{
2675 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2676 hda_nid_t nid = get_amp_nid(kcontrol);
2677 int chs = get_amp_channels(kcontrol);
2678 int dir = get_amp_direction(kcontrol);
2679 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 long *valp = ucontrol->value.integer.value;
2681 int change = 0;
2682
Takashi Iwaicb53c622007-08-10 17:21:45 +02002683 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002684 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002685 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002686 HDA_AMP_MUTE,
2687 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002688 valp++;
2689 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002690 if (chs & 2)
2691 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002692 HDA_AMP_MUTE,
2693 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002694 hda_call_check_power_status(codec, nid);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002695 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 return change;
2697}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002698EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699
2700/*
Takashi Iwai985be542005-11-02 18:26:49 +01002701 * bound volume controls
2702 *
2703 * bind multiple volumes (# indices, from 0)
2704 */
2705
2706#define AMP_VAL_IDX_SHIFT 19
2707#define AMP_VAL_IDX_MASK (0x0f<<19)
2708
Takashi Iwaid5191e52009-11-16 14:58:17 +01002709/**
2710 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2711 *
2712 * The control element is supposed to have the private_value field
2713 * set up via HDA_BIND_MUTE*() macros.
2714 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002715int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2716 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002717{
2718 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2719 unsigned long pval;
2720 int err;
2721
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002722 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002723 pval = kcontrol->private_value;
2724 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2725 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2726 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002727 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002728 return err;
2729}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002730EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002731
Takashi Iwaid5191e52009-11-16 14:58:17 +01002732/**
2733 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2734 *
2735 * The control element is supposed to have the private_value field
2736 * set up via HDA_BIND_MUTE*() macros.
2737 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002738int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2739 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002740{
2741 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2742 unsigned long pval;
2743 int i, indices, err = 0, change = 0;
2744
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002745 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002746 pval = kcontrol->private_value;
2747 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2748 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002749 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2750 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002751 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2752 if (err < 0)
2753 break;
2754 change |= err;
2755 }
2756 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002757 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002758 return err < 0 ? err : change;
2759}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002760EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002761
Takashi Iwaid5191e52009-11-16 14:58:17 +01002762/**
2763 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2764 *
2765 * The control element is supposed to have the private_value field
2766 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002767 */
2768int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2769 struct snd_ctl_elem_info *uinfo)
2770{
2771 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2772 struct hda_bind_ctls *c;
2773 int err;
2774
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002775 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002776 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002777 kcontrol->private_value = *c->values;
2778 err = c->ops->info(kcontrol, uinfo);
2779 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002780 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002781 return err;
2782}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002783EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002784
Takashi Iwaid5191e52009-11-16 14:58:17 +01002785/**
2786 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2787 *
2788 * The control element is supposed to have the private_value field
2789 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2790 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002791int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2792 struct snd_ctl_elem_value *ucontrol)
2793{
2794 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2795 struct hda_bind_ctls *c;
2796 int err;
2797
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002798 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002799 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002800 kcontrol->private_value = *c->values;
2801 err = c->ops->get(kcontrol, ucontrol);
2802 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002803 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002804 return err;
2805}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002806EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002807
Takashi Iwaid5191e52009-11-16 14:58:17 +01002808/**
2809 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2810 *
2811 * The control element is supposed to have the private_value field
2812 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2813 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002814int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2815 struct snd_ctl_elem_value *ucontrol)
2816{
2817 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2818 struct hda_bind_ctls *c;
2819 unsigned long *vals;
2820 int err = 0, change = 0;
2821
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002822 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002823 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002824 for (vals = c->values; *vals; vals++) {
2825 kcontrol->private_value = *vals;
2826 err = c->ops->put(kcontrol, ucontrol);
2827 if (err < 0)
2828 break;
2829 change |= err;
2830 }
2831 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002832 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002833 return err < 0 ? err : change;
2834}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002835EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02002836
Takashi Iwaid5191e52009-11-16 14:58:17 +01002837/**
2838 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2839 *
2840 * The control element is supposed to have the private_value field
2841 * set up via HDA_BIND_VOL() macro.
2842 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002843int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2844 unsigned int size, unsigned int __user *tlv)
2845{
2846 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2847 struct hda_bind_ctls *c;
2848 int err;
2849
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002850 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002851 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002852 kcontrol->private_value = *c->values;
2853 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2854 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002855 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002856 return err;
2857}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002858EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02002859
2860struct hda_ctl_ops snd_hda_bind_vol = {
2861 .info = snd_hda_mixer_amp_volume_info,
2862 .get = snd_hda_mixer_amp_volume_get,
2863 .put = snd_hda_mixer_amp_volume_put,
2864 .tlv = snd_hda_mixer_amp_tlv
2865};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002866EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02002867
2868struct hda_ctl_ops snd_hda_bind_sw = {
2869 .info = snd_hda_mixer_amp_switch_info,
2870 .get = snd_hda_mixer_amp_switch_get,
2871 .put = snd_hda_mixer_amp_switch_put,
2872 .tlv = snd_hda_mixer_amp_tlv
2873};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002874EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02002875
2876/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 * SPDIF out controls
2878 */
2879
Takashi Iwai0ba21762007-04-16 11:29:14 +02002880static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2881 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882{
2883 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2884 uinfo->count = 1;
2885 return 0;
2886}
2887
Takashi Iwai0ba21762007-04-16 11:29:14 +02002888static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2889 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890{
2891 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2892 IEC958_AES0_NONAUDIO |
2893 IEC958_AES0_CON_EMPHASIS_5015 |
2894 IEC958_AES0_CON_NOT_COPYRIGHT;
2895 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2896 IEC958_AES1_CON_ORIGINAL;
2897 return 0;
2898}
2899
Takashi Iwai0ba21762007-04-16 11:29:14 +02002900static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2901 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902{
2903 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2904 IEC958_AES0_NONAUDIO |
2905 IEC958_AES0_PRO_EMPHASIS_5015;
2906 return 0;
2907}
2908
Takashi Iwai0ba21762007-04-16 11:29:14 +02002909static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2910 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911{
2912 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002913 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002914 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002916 mutex_lock(&codec->spdif_mutex);
2917 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c9359762011-06-01 11:14:17 -06002918 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
2919 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
2920 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
2921 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002922 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923
2924 return 0;
2925}
2926
2927/* convert from SPDIF status bits to HDA SPDIF bits
2928 * bit 0 (DigEn) is always set zero (to be filled later)
2929 */
2930static unsigned short convert_from_spdif_status(unsigned int sbits)
2931{
2932 unsigned short val = 0;
2933
2934 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002935 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002937 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002939 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2940 IEC958_AES0_PRO_EMPHASIS_5015)
2941 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002943 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2944 IEC958_AES0_CON_EMPHASIS_5015)
2945 val |= AC_DIG1_EMPHASIS;
2946 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2947 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02002949 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2951 }
2952 return val;
2953}
2954
2955/* convert to SPDIF status bits from HDA SPDIF bits
2956 */
2957static unsigned int convert_to_spdif_status(unsigned short val)
2958{
2959 unsigned int sbits = 0;
2960
Takashi Iwai0ba21762007-04-16 11:29:14 +02002961 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002963 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 sbits |= IEC958_AES0_PROFESSIONAL;
2965 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002966 if (sbits & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2968 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002969 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002971 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002973 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2975 sbits |= val & (0x7f << 8);
2976 }
2977 return sbits;
2978}
2979
Takashi Iwai2f728532008-09-25 16:32:41 +02002980/* set digital convert verbs both for the given NID and its slaves */
2981static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
2982 int verb, int val)
2983{
Takashi Iwaidda14412011-05-02 11:29:30 +02002984 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02002985
Takashi Iwai9e976972008-11-25 08:17:20 +01002986 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002987 d = codec->slave_dig_outs;
2988 if (!d)
2989 return;
2990 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01002991 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002992}
2993
2994static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
2995 int dig1, int dig2)
2996{
2997 if (dig1 != -1)
2998 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
2999 if (dig2 != -1)
3000 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
3001}
3002
Takashi Iwai0ba21762007-04-16 11:29:14 +02003003static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
3004 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005{
3006 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06003007 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003008 struct hda_spdif_out *spdif;
3009 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 unsigned short val;
3011 int change;
3012
Ingo Molnar62932df2006-01-16 16:34:20 +01003013 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003014 spdif = snd_array_elem(&codec->spdif_out, idx);
3015 nid = spdif->nid;
Stephen Warren7c9359762011-06-01 11:14:17 -06003016 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
3018 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
3019 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c9359762011-06-01 11:14:17 -06003020 val = convert_from_spdif_status(spdif->status);
3021 val |= spdif->ctls & 1;
3022 change = spdif->ctls != val;
3023 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003024 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02003025 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01003026 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 return change;
3028}
3029
Takashi Iwaia5ce8892007-07-23 15:42:26 +02003030#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031
Takashi Iwai0ba21762007-04-16 11:29:14 +02003032static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
3033 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034{
3035 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06003036 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003037 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003039 mutex_lock(&codec->spdif_mutex);
3040 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c9359762011-06-01 11:14:17 -06003041 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003042 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 return 0;
3044}
3045
Stephen Warren74b654c2011-06-01 11:14:18 -06003046static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
3047 int dig1, int dig2)
3048{
3049 set_dig_out_convert(codec, nid, dig1, dig2);
3050 /* unmute amp switch (if any) */
3051 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
3052 (dig1 & AC_DIG1_ENABLE))
3053 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
3054 HDA_AMP_MUTE, 0);
3055}
3056
Takashi Iwai0ba21762007-04-16 11:29:14 +02003057static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
3058 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059{
3060 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06003061 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003062 struct hda_spdif_out *spdif;
3063 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 unsigned short val;
3065 int change;
3066
Ingo Molnar62932df2006-01-16 16:34:20 +01003067 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003068 spdif = snd_array_elem(&codec->spdif_out, idx);
3069 nid = spdif->nid;
Stephen Warren7c9359762011-06-01 11:14:17 -06003070 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02003072 val |= AC_DIG1_ENABLE;
Stephen Warren7c9359762011-06-01 11:14:17 -06003073 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003074 spdif->ctls = val;
3075 if (change && nid != (u16)-1)
3076 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01003077 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 return change;
3079}
3080
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003081static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 {
3083 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3084 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003085 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 .info = snd_hda_spdif_mask_info,
3087 .get = snd_hda_spdif_cmask_get,
3088 },
3089 {
3090 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3091 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003092 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 .info = snd_hda_spdif_mask_info,
3094 .get = snd_hda_spdif_pmask_get,
3095 },
3096 {
3097 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003098 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 .info = snd_hda_spdif_mask_info,
3100 .get = snd_hda_spdif_default_get,
3101 .put = snd_hda_spdif_default_put,
3102 },
3103 {
3104 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003105 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 .info = snd_hda_spdif_out_switch_info,
3107 .get = snd_hda_spdif_out_switch_get,
3108 .put = snd_hda_spdif_out_switch_put,
3109 },
3110 { } /* end */
3111};
3112
3113/**
3114 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
3115 * @codec: the HDA codec
3116 * @nid: audio out widget NID
3117 *
3118 * Creates controls related with the SPDIF output.
3119 * Called from each patch supporting the SPDIF out.
3120 *
3121 * Returns 0 if successful, or a negative error code.
3122 */
Stephen Warren74b654c2011-06-01 11:14:18 -06003123int snd_hda_create_spdif_out_ctls(struct hda_codec *codec,
3124 hda_nid_t associated_nid,
3125 hda_nid_t cvt_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126{
3127 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003128 struct snd_kcontrol *kctl;
3129 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003130 int idx;
Stephen Warren7c9359762011-06-01 11:14:17 -06003131 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132
Takashi Iwai1afe2062010-12-23 10:17:52 +01003133 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch");
3134 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003135 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
3136 return -EBUSY;
3137 }
Stephen Warren7c9359762011-06-01 11:14:17 -06003138 spdif = snd_array_new(&codec->spdif_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3140 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01003141 if (!kctl)
3142 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01003143 kctl->id.index = idx;
Stephen Warren7c9359762011-06-01 11:14:17 -06003144 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06003145 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003146 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 return err;
3148 }
Stephen Warren74b654c2011-06-01 11:14:18 -06003149 spdif->nid = cvt_nid;
3150 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
Stephen Warren7c9359762011-06-01 11:14:17 -06003151 AC_VERB_GET_DIGI_CONVERT_1, 0);
3152 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 return 0;
3154}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003155EXPORT_SYMBOL_HDA(snd_hda_create_spdif_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003157/* get the hda_spdif_out entry from the given NID
3158 * call within spdif_mutex lock
3159 */
Stephen Warren7c9359762011-06-01 11:14:17 -06003160struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3161 hda_nid_t nid)
3162{
3163 int i;
3164 for (i = 0; i < codec->spdif_out.used; i++) {
3165 struct hda_spdif_out *spdif =
3166 snd_array_elem(&codec->spdif_out, i);
3167 if (spdif->nid == nid)
3168 return spdif;
3169 }
3170 return NULL;
3171}
3172EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid);
3173
Stephen Warren74b654c2011-06-01 11:14:18 -06003174void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3175{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003176 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003177
3178 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003179 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003180 spdif->nid = (u16)-1;
3181 mutex_unlock(&codec->spdif_mutex);
3182}
3183EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign);
3184
3185void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3186{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003187 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003188 unsigned short val;
3189
3190 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003191 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003192 if (spdif->nid != nid) {
3193 spdif->nid = nid;
3194 val = spdif->ctls;
3195 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3196 }
3197 mutex_unlock(&codec->spdif_mutex);
3198}
3199EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign);
3200
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201/*
Takashi Iwai9a081602008-02-12 18:37:26 +01003202 * SPDIF sharing with analog output
3203 */
3204static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3205 struct snd_ctl_elem_value *ucontrol)
3206{
3207 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3208 ucontrol->value.integer.value[0] = mout->share_spdif;
3209 return 0;
3210}
3211
3212static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3213 struct snd_ctl_elem_value *ucontrol)
3214{
3215 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3216 mout->share_spdif = !!ucontrol->value.integer.value[0];
3217 return 0;
3218}
3219
3220static struct snd_kcontrol_new spdif_share_sw = {
3221 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3222 .name = "IEC958 Default PCM Playback Switch",
3223 .info = snd_ctl_boolean_mono_info,
3224 .get = spdif_share_sw_get,
3225 .put = spdif_share_sw_put,
3226};
3227
Takashi Iwaid5191e52009-11-16 14:58:17 +01003228/**
3229 * snd_hda_create_spdif_share_sw - create Default PCM switch
3230 * @codec: the HDA codec
3231 * @mout: multi-out instance
3232 */
Takashi Iwai9a081602008-02-12 18:37:26 +01003233int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3234 struct hda_multi_out *mout)
3235{
3236 if (!mout->dig_out_nid)
3237 return 0;
3238 /* ATTENTION: here mout is passed as private_data, instead of codec */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003239 return snd_hda_ctl_add(codec, mout->dig_out_nid,
3240 snd_ctl_new1(&spdif_share_sw, mout));
Takashi Iwai9a081602008-02-12 18:37:26 +01003241}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003242EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01003243
3244/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 * SPDIF input
3246 */
3247
3248#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3249
Takashi Iwai0ba21762007-04-16 11:29:14 +02003250static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3251 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252{
3253 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3254
3255 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3256 return 0;
3257}
3258
Takashi Iwai0ba21762007-04-16 11:29:14 +02003259static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3260 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261{
3262 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3263 hda_nid_t nid = kcontrol->private_value;
3264 unsigned int val = !!ucontrol->value.integer.value[0];
3265 int change;
3266
Ingo Molnar62932df2006-01-16 16:34:20 +01003267 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003269 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003271 snd_hda_codec_write_cache(codec, nid, 0,
3272 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003274 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 return change;
3276}
3277
Takashi Iwai0ba21762007-04-16 11:29:14 +02003278static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3279 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280{
3281 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3282 hda_nid_t nid = kcontrol->private_value;
3283 unsigned short val;
3284 unsigned int sbits;
3285
Andrew Paprocki3982d172007-12-19 12:13:44 +01003286 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 sbits = convert_to_spdif_status(val);
3288 ucontrol->value.iec958.status[0] = sbits;
3289 ucontrol->value.iec958.status[1] = sbits >> 8;
3290 ucontrol->value.iec958.status[2] = sbits >> 16;
3291 ucontrol->value.iec958.status[3] = sbits >> 24;
3292 return 0;
3293}
3294
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003295static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 {
3297 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003298 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 .info = snd_hda_spdif_in_switch_info,
3300 .get = snd_hda_spdif_in_switch_get,
3301 .put = snd_hda_spdif_in_switch_put,
3302 },
3303 {
3304 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3305 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003306 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 .info = snd_hda_spdif_mask_info,
3308 .get = snd_hda_spdif_in_status_get,
3309 },
3310 { } /* end */
3311};
3312
3313/**
3314 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3315 * @codec: the HDA codec
3316 * @nid: audio in widget NID
3317 *
3318 * Creates controls related with the SPDIF input.
3319 * Called from each patch supporting the SPDIF in.
3320 *
3321 * Returns 0 if successful, or a negative error code.
3322 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003323int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324{
3325 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003326 struct snd_kcontrol *kctl;
3327 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003328 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329
Takashi Iwai1afe2062010-12-23 10:17:52 +01003330 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch");
3331 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003332 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
3333 return -EBUSY;
3334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3336 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01003337 if (!kctl)
3338 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003340 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003341 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 return err;
3343 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003344 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01003345 snd_hda_codec_read(codec, nid, 0,
3346 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02003347 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 return 0;
3349}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003350EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351
Takashi Iwai2a439522011-07-26 09:52:50 +02003352#ifdef CONFIG_PM
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003353/*
3354 * command cache
3355 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003357/* build a 32bit cache key with the widget id and the command parameter */
3358#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3359#define get_cmd_cache_nid(key) ((key) & 0xff)
3360#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3361
3362/**
3363 * snd_hda_codec_write_cache - send a single command with caching
3364 * @codec: the HDA codec
3365 * @nid: NID to send the command
3366 * @direct: direct flag
3367 * @verb: the verb to send
3368 * @parm: the parameter for the verb
3369 *
3370 * Send a single command without waiting for response.
3371 *
3372 * Returns 0 if successful, or a negative error code.
3373 */
3374int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
3375 int direct, unsigned int verb, unsigned int parm)
3376{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003377 int err = snd_hda_codec_write(codec, nid, direct, verb, parm);
3378 struct hda_cache_head *c;
3379 u32 key;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003380
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003381 if (err < 0)
3382 return err;
3383 /* parm may contain the verb stuff for get/set amp */
3384 verb = verb | (parm >> 8);
3385 parm &= 0xff;
3386 key = build_cmd_cache_key(nid, verb);
3387 mutex_lock(&codec->bus->cmd_mutex);
3388 c = get_alloc_hash(&codec->cmd_cache, key);
3389 if (c)
3390 c->val = parm;
3391 mutex_unlock(&codec->bus->cmd_mutex);
3392 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003393}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003394EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003395
Takashi Iwaid5191e52009-11-16 14:58:17 +01003396/**
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003397 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3398 * @codec: the HDA codec
3399 * @nid: NID to send the command
3400 * @direct: direct flag
3401 * @verb: the verb to send
3402 * @parm: the parameter for the verb
3403 *
3404 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3405 * command if the parameter is already identical with the cached value.
3406 * If not, it sends the command and refreshes the cache.
3407 *
3408 * Returns 0 if successful, or a negative error code.
3409 */
3410int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
3411 int direct, unsigned int verb, unsigned int parm)
3412{
3413 struct hda_cache_head *c;
3414 u32 key;
3415
3416 /* parm may contain the verb stuff for get/set amp */
3417 verb = verb | (parm >> 8);
3418 parm &= 0xff;
3419 key = build_cmd_cache_key(nid, verb);
3420 mutex_lock(&codec->bus->cmd_mutex);
3421 c = get_hash(&codec->cmd_cache, key);
3422 if (c && c->val == parm) {
3423 mutex_unlock(&codec->bus->cmd_mutex);
3424 return 0;
3425 }
3426 mutex_unlock(&codec->bus->cmd_mutex);
3427 return snd_hda_codec_write_cache(codec, nid, direct, verb, parm);
3428}
3429EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
3430
3431/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003432 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3433 * @codec: HD-audio codec
3434 *
3435 * Execute all verbs recorded in the command caches to resume.
3436 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003437void snd_hda_codec_resume_cache(struct hda_codec *codec)
3438{
Takashi Iwai603c4012008-07-30 15:01:44 +02003439 struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003440 int i;
3441
Takashi Iwai603c4012008-07-30 15:01:44 +02003442 for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003443 u32 key = buffer->key;
3444 if (!key)
3445 continue;
3446 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3447 get_cmd_cache_cmd(key), buffer->val);
3448 }
3449}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003450EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003451
3452/**
3453 * snd_hda_sequence_write_cache - sequence writes with caching
3454 * @codec: the HDA codec
3455 * @seq: VERB array to send
3456 *
3457 * Send the commands sequentially from the given array.
3458 * Thte commands are recorded on cache for power-save and resume.
3459 * The array must be terminated with NID=0.
3460 */
3461void snd_hda_sequence_write_cache(struct hda_codec *codec,
3462 const struct hda_verb *seq)
3463{
3464 for (; seq->nid; seq++)
3465 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3466 seq->param);
3467}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003468EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
Takashi Iwai2a439522011-07-26 09:52:50 +02003469#endif /* CONFIG_PM */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003470
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003471void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
3472 unsigned int power_state,
3473 bool eapd_workaround)
Takashi Iwai54d17402005-11-21 16:33:22 +01003474{
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003475 hda_nid_t nid = codec->start_nid;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003476 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01003477
Takashi Iwaicb53c622007-08-10 17:21:45 +02003478 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003479 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003480 if (!(wcaps & AC_WCAP_POWER))
3481 continue;
3482 /* don't power down the widget if it controls eapd and
3483 * EAPD_BTLENABLE is set.
3484 */
3485 if (eapd_workaround && power_state == AC_PWRST_D3 &&
3486 get_wcaps_type(wcaps) == AC_WID_PIN &&
3487 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3488 int eapd = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003489 AC_VERB_GET_EAPD_BTLENABLE, 0);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003490 if (eapd & 0x02)
3491 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003492 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003493 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
3494 power_state);
Takashi Iwai54d17402005-11-21 16:33:22 +01003495 }
3496
Takashi Iwaicb53c622007-08-10 17:21:45 +02003497 if (power_state == AC_PWRST_D0) {
3498 unsigned long end_time;
3499 int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003500 /* wait until the codec reachs to D0 */
3501 end_time = jiffies + msecs_to_jiffies(500);
3502 do {
3503 state = snd_hda_codec_read(codec, fg, 0,
3504 AC_VERB_GET_POWER_STATE, 0);
3505 if (state == power_state)
3506 break;
3507 msleep(1);
3508 } while (time_after_eq(end_time, jiffies));
3509 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003510}
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003511EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all);
3512
3513/*
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003514 * supported power states check
3515 */
3516static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg,
3517 unsigned int power_state)
3518{
3519 int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE);
3520
3521 if (sup < 0)
3522 return false;
3523 if (sup & power_state)
3524 return true;
3525 else
3526 return false;
3527}
3528
3529/*
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003530 * set power state of the codec
3531 */
3532static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
3533 unsigned int power_state)
3534{
Wang Xingchao09617ce2012-06-08 10:26:08 +08003535 int count;
3536 unsigned int state;
3537
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003538 if (codec->patch_ops.set_power_state) {
3539 codec->patch_ops.set_power_state(codec, fg, power_state);
3540 return;
3541 }
3542
3543 /* this delay seems necessary to avoid click noise at power-down */
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003544 if (power_state == AC_PWRST_D3) {
3545 /* transition time less than 10ms for power down */
3546 bool epss = snd_hda_codec_get_supported_ps(codec, fg, AC_PWRST_EPSS);
3547 msleep(epss ? 10 : 100);
3548 }
Wang Xingchao09617ce2012-06-08 10:26:08 +08003549
3550 /* repeat power states setting at most 10 times*/
3551 for (count = 0; count < 10; count++) {
3552 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE,
3553 power_state);
3554 snd_hda_codec_set_power_to_all(codec, fg, power_state, true);
3555 state = snd_hda_codec_read(codec, fg, 0,
3556 AC_VERB_GET_POWER_STATE, 0);
3557 if (!(state & AC_PWRST_ERROR))
3558 break;
3559 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003560}
Takashi Iwai54d17402005-11-21 16:33:22 +01003561
Takashi Iwai11aeff02008-07-30 15:01:46 +02003562#ifdef CONFIG_SND_HDA_HWDEP
3563/* execute additional init verbs */
3564static void hda_exec_init_verbs(struct hda_codec *codec)
3565{
3566 if (codec->init_verbs.list)
3567 snd_hda_sequence_write(codec, codec->init_verbs.list);
3568}
3569#else
3570static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3571#endif
3572
Takashi Iwai2a439522011-07-26 09:52:50 +02003573#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003574/*
3575 * call suspend and power-down; used both from PM and power-save
3576 */
3577static void hda_call_codec_suspend(struct hda_codec *codec)
3578{
3579 if (codec->patch_ops.suspend)
Takashi Iwai68cb2b52012-07-02 15:20:37 +02003580 codec->patch_ops.suspend(codec);
Takashi Iwaieb541332010-08-06 13:48:11 +02003581 hda_cleanup_all_streams(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003582 hda_set_power_state(codec,
3583 codec->afg ? codec->afg : codec->mfg,
3584 AC_PWRST_D3);
3585#ifdef CONFIG_SND_HDA_POWER_SAVE
3586 cancel_delayed_work(&codec->power_work);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02003587 spin_lock(&codec->power_lock);
3588 snd_hda_update_power_acct(codec);
3589 trace_hda_power_down(codec);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02003590 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02003591 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003592 codec->power_jiffies = jiffies;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02003593 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003594#endif
3595}
3596
3597/*
3598 * kick up codec; used both from PM and power-save
3599 */
3600static void hda_call_codec_resume(struct hda_codec *codec)
3601{
Takashi Iwai7f308302012-05-08 16:52:23 +02003602 /* set as if powered on for avoiding re-entering the resume
3603 * in the resume / power-save sequence
3604 */
3605 hda_keep_power_on(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003606 hda_set_power_state(codec,
3607 codec->afg ? codec->afg : codec->mfg,
3608 AC_PWRST_D0);
Takashi Iwai3be14142009-02-20 14:11:16 +01003609 restore_pincfgs(codec); /* restore all current pin configs */
Takashi Iwaiac0547d2010-07-05 16:50:13 +02003610 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02003611 hda_exec_init_verbs(codec);
Takashi Iwai1835a0f2011-10-27 22:12:46 +02003612 snd_hda_jack_set_dirty_all(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003613 if (codec->patch_ops.resume)
3614 codec->patch_ops.resume(codec);
3615 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02003616 if (codec->patch_ops.init)
3617 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003618 snd_hda_codec_resume_amp(codec);
3619 snd_hda_codec_resume_cache(codec);
3620 }
Takashi Iwai7f308302012-05-08 16:52:23 +02003621 snd_hda_power_down(codec); /* flag down before returning */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003622}
Takashi Iwai2a439522011-07-26 09:52:50 +02003623#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003624
Takashi Iwai54d17402005-11-21 16:33:22 +01003625
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626/**
3627 * snd_hda_build_controls - build mixer controls
3628 * @bus: the BUS
3629 *
3630 * Creates mixer controls for each codec included in the bus.
3631 *
3632 * Returns 0 if successful, otherwise a negative error code.
3633 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003634int /*__devinit*/ snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003636 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637
Takashi Iwai0ba21762007-04-16 11:29:14 +02003638 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003639 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003640 if (err < 0) {
Takashi Iwai28d1a852010-03-15 09:05:46 +01003641 printk(KERN_ERR "hda_codec: cannot build controls "
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003642 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003643 err = snd_hda_codec_reset(codec);
3644 if (err < 0) {
3645 printk(KERN_ERR
3646 "hda_codec: cannot revert codec\n");
3647 return err;
3648 }
3649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003651 return 0;
3652}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003653EXPORT_SYMBOL_HDA(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003655int snd_hda_codec_build_controls(struct hda_codec *codec)
3656{
3657 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02003658 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003659 /* continue to initialize... */
3660 if (codec->patch_ops.init)
3661 err = codec->patch_ops.init(codec);
3662 if (!err && codec->patch_ops.build_controls)
3663 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003664 if (err < 0)
3665 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 return 0;
3667}
3668
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669/*
3670 * stream formats
3671 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02003672struct hda_rate_tbl {
3673 unsigned int hz;
3674 unsigned int alsa_bits;
3675 unsigned int hda_fmt;
3676};
3677
Takashi Iwai92f10b32010-08-03 14:21:00 +02003678/* rate = base * mult / div */
3679#define HDA_RATE(base, mult, div) \
3680 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
3681 (((div) - 1) << AC_FMT_DIV_SHIFT))
3682
Takashi Iwaibefdf312005-08-22 13:57:55 +02003683static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003685
3686 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003687 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
3688 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
3689 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
3690 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
3691 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
3692 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
3693 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
3694 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
3695 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
3696 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
3697 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003698#define AC_PAR_PCM_RATE_BITS 11
3699 /* up to bits 10, 384kHZ isn't supported properly */
3700
3701 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003702 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003703
Takashi Iwaibefdf312005-08-22 13:57:55 +02003704 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705};
3706
3707/**
3708 * snd_hda_calc_stream_format - calculate format bitset
3709 * @rate: the sample rate
3710 * @channels: the number of channels
3711 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3712 * @maxbps: the max. bps
3713 *
3714 * Calculate the format bitset from the given rate, channels and th PCM format.
3715 *
3716 * Return zero if invalid.
3717 */
3718unsigned int snd_hda_calc_stream_format(unsigned int rate,
3719 unsigned int channels,
3720 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03003721 unsigned int maxbps,
3722 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723{
3724 int i;
3725 unsigned int val = 0;
3726
Takashi Iwaibefdf312005-08-22 13:57:55 +02003727 for (i = 0; rate_bits[i].hz; i++)
3728 if (rate_bits[i].hz == rate) {
3729 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730 break;
3731 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003732 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733 snd_printdd("invalid rate %d\n", rate);
3734 return 0;
3735 }
3736
3737 if (channels == 0 || channels > 8) {
3738 snd_printdd("invalid channels %d\n", channels);
3739 return 0;
3740 }
3741 val |= channels - 1;
3742
3743 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003744 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003745 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003746 break;
3747 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003748 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003749 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750 case 20:
3751 case 24:
3752 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003753 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003754 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003756 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02003758 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759 break;
3760 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003761 snd_printdd("invalid format width %d\n",
3762 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763 return 0;
3764 }
3765
Anssi Hannula32c168c2010-08-03 13:28:57 +03003766 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003767 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03003768
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769 return val;
3770}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003771EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003773static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid,
3774 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003775{
3776 unsigned int val = 0;
3777 if (nid != codec->afg &&
3778 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
3779 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
3780 if (!val || val == -1)
3781 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
3782 if (!val || val == -1)
3783 return 0;
3784 return val;
3785}
3786
3787static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3788{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003789 return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003790 get_pcm_param);
3791}
3792
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003793static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid,
3794 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003795{
3796 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
3797 if (!streams || streams == -1)
3798 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
3799 if (!streams || streams == -1)
3800 return 0;
3801 return streams;
3802}
3803
3804static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
3805{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003806 return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003807 get_stream_param);
3808}
3809
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810/**
3811 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
3812 * @codec: the HDA codec
3813 * @nid: NID to query
3814 * @ratesp: the pointer to store the detected rate bitflags
3815 * @formatsp: the pointer to store the detected formats
3816 * @bpsp: the pointer to store the detected format widths
3817 *
3818 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
3819 * or @bsps argument is ignored.
3820 *
3821 * Returns 0 if successful, otherwise a negative error code.
3822 */
Stephen Warren384a48d2011-06-01 11:14:21 -06003823int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
3825{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003826 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003828 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003829 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830
3831 if (ratesp) {
3832 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003833 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02003835 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003837 if (rates == 0) {
3838 snd_printk(KERN_ERR "hda_codec: rates == 0 "
3839 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
3840 nid, val,
3841 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
3842 return -EIO;
3843 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844 *ratesp = rates;
3845 }
3846
3847 if (formatsp || bpsp) {
3848 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003849 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003851 streams = query_stream_param(codec, nid);
3852 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854
3855 bps = 0;
3856 if (streams & AC_SUPFMT_PCM) {
3857 if (val & AC_SUPPCM_BITS_8) {
3858 formats |= SNDRV_PCM_FMTBIT_U8;
3859 bps = 8;
3860 }
3861 if (val & AC_SUPPCM_BITS_16) {
3862 formats |= SNDRV_PCM_FMTBIT_S16_LE;
3863 bps = 16;
3864 }
3865 if (wcaps & AC_WCAP_DIGITAL) {
3866 if (val & AC_SUPPCM_BITS_32)
3867 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
3868 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
3869 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3870 if (val & AC_SUPPCM_BITS_24)
3871 bps = 24;
3872 else if (val & AC_SUPPCM_BITS_20)
3873 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003874 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
3875 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3877 if (val & AC_SUPPCM_BITS_32)
3878 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879 else if (val & AC_SUPPCM_BITS_24)
3880 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02003881 else if (val & AC_SUPPCM_BITS_20)
3882 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883 }
3884 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02003885#if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
Takashi Iwaib5025c52009-07-01 18:05:27 +02003886 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003888 if (!bps)
3889 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02003890 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02003891#endif
Takashi Iwaib5025c52009-07-01 18:05:27 +02003892 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003893 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894 /* temporary hack: we have still no proper support
3895 * for the direct AC3 stream...
3896 */
3897 formats |= SNDRV_PCM_FMTBIT_U8;
3898 bps = 8;
3899 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003900 if (formats == 0) {
3901 snd_printk(KERN_ERR "hda_codec: formats == 0 "
3902 "(nid=0x%x, val=0x%x, ovrd=%i, "
3903 "streams=0x%x)\n",
3904 nid, val,
3905 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
3906 streams);
3907 return -EIO;
3908 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909 if (formatsp)
3910 *formatsp = formats;
3911 if (bpsp)
3912 *bpsp = bps;
3913 }
3914
3915 return 0;
3916}
Stephen Warren384a48d2011-06-01 11:14:21 -06003917EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918
3919/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003920 * snd_hda_is_supported_format - Check the validity of the format
3921 * @codec: HD-audio codec
3922 * @nid: NID to check
3923 * @format: the HD-audio format value to check
3924 *
3925 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926 *
3927 * Returns 1 if supported, 0 if not.
3928 */
3929int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
3930 unsigned int format)
3931{
3932 int i;
3933 unsigned int val = 0, rate, stream;
3934
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003935 val = query_pcm_param(codec, nid);
3936 if (!val)
3937 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938
3939 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003940 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02003941 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 if (val & (1 << i))
3943 break;
3944 return 0;
3945 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003946 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947 return 0;
3948
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003949 stream = query_stream_param(codec, nid);
3950 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 return 0;
3952
3953 if (stream & AC_SUPFMT_PCM) {
3954 switch (format & 0xf0) {
3955 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003956 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957 return 0;
3958 break;
3959 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003960 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961 return 0;
3962 break;
3963 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003964 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965 return 0;
3966 break;
3967 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003968 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 return 0;
3970 break;
3971 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003972 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973 return 0;
3974 break;
3975 default:
3976 return 0;
3977 }
3978 } else {
3979 /* FIXME: check for float32 and AC3? */
3980 }
3981
3982 return 1;
3983}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003984EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985
3986/*
3987 * PCM stuff
3988 */
3989static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
3990 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003991 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992{
3993 return 0;
3994}
3995
3996static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
3997 struct hda_codec *codec,
3998 unsigned int stream_tag,
3999 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004000 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001{
4002 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4003 return 0;
4004}
4005
4006static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
4007 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004008 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009{
Takashi Iwai888afa12008-03-18 09:57:50 +01004010 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011 return 0;
4012}
4013
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004014static int set_pcm_default_values(struct hda_codec *codec,
4015 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004017 int err;
4018
Takashi Iwai0ba21762007-04-16 11:29:14 +02004019 /* query support PCM information from the given NID */
4020 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004021 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02004022 info->rates ? NULL : &info->rates,
4023 info->formats ? NULL : &info->formats,
4024 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004025 if (err < 0)
4026 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027 }
4028 if (info->ops.open == NULL)
4029 info->ops.open = hda_pcm_default_open_close;
4030 if (info->ops.close == NULL)
4031 info->ops.close = hda_pcm_default_open_close;
4032 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004033 if (snd_BUG_ON(!info->nid))
4034 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035 info->ops.prepare = hda_pcm_default_prepare;
4036 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004038 if (snd_BUG_ON(!info->nid))
4039 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040 info->ops.cleanup = hda_pcm_default_cleanup;
4041 }
4042 return 0;
4043}
4044
Takashi Iwaieb541332010-08-06 13:48:11 +02004045/*
4046 * codec prepare/cleanup entries
4047 */
4048int snd_hda_codec_prepare(struct hda_codec *codec,
4049 struct hda_pcm_stream *hinfo,
4050 unsigned int stream,
4051 unsigned int format,
4052 struct snd_pcm_substream *substream)
4053{
4054 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004055 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004056 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
4057 if (ret >= 0)
4058 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004059 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004060 return ret;
4061}
4062EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
4063
4064void snd_hda_codec_cleanup(struct hda_codec *codec,
4065 struct hda_pcm_stream *hinfo,
4066 struct snd_pcm_substream *substream)
4067{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004068 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004069 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004070 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004071}
4072EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
4073
Takashi Iwaid5191e52009-11-16 14:58:17 +01004074/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01004075const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
4076 "Audio", "SPDIF", "HDMI", "Modem"
4077};
4078
Takashi Iwai176d5332008-07-30 15:01:44 +02004079/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004080 * get the empty PCM device number to assign
Takashi Iwaic8936222010-01-28 17:08:53 +01004081 *
4082 * note the max device number is limited by HDA_MAX_PCMS, currently 10
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004083 */
4084static int get_empty_pcm_device(struct hda_bus *bus, int type)
4085{
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004086 /* audio device indices; not linear to keep compatibility */
4087 static int audio_idx[HDA_PCM_NTYPES][5] = {
4088 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
4089 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01004090 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004091 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004092 };
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004093 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004094
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004095 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004096 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
4097 return -EINVAL;
4098 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004099
4100 for (i = 0; audio_idx[type][i] >= 0 ; i++)
4101 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
4102 return audio_idx[type][i];
4103
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004104 /* non-fixed slots starting from 10 */
4105 for (i = 10; i < 32; i++) {
4106 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4107 return i;
4108 }
4109
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004110 snd_printk(KERN_WARNING "Too many %s devices\n",
4111 snd_hda_pcm_type_name[type]);
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004112 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004113}
4114
4115/*
Takashi Iwai176d5332008-07-30 15:01:44 +02004116 * attach a new PCM stream
4117 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004118static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02004119{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004120 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02004121 struct hda_pcm_stream *info;
4122 int stream, err;
4123
Takashi Iwaib91f0802008-11-04 08:43:08 +01004124 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02004125 return -EINVAL;
4126 for (stream = 0; stream < 2; stream++) {
4127 info = &pcm->stream[stream];
4128 if (info->substreams) {
4129 err = set_pcm_default_values(codec, info);
4130 if (err < 0)
4131 return err;
4132 }
4133 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004134 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02004135}
4136
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004137/* assign all PCMs of the given codec */
4138int snd_hda_codec_build_pcms(struct hda_codec *codec)
4139{
4140 unsigned int pcm;
4141 int err;
4142
4143 if (!codec->num_pcms) {
4144 if (!codec->patch_ops.build_pcms)
4145 return 0;
4146 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004147 if (err < 0) {
4148 printk(KERN_ERR "hda_codec: cannot build PCMs"
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004149 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004150 err = snd_hda_codec_reset(codec);
4151 if (err < 0) {
4152 printk(KERN_ERR
4153 "hda_codec: cannot revert codec\n");
4154 return err;
4155 }
4156 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004157 }
4158 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
4159 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
4160 int dev;
4161
4162 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01004163 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004164
4165 if (!cpcm->pcm) {
4166 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
4167 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004168 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004169 cpcm->device = dev;
4170 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004171 if (err < 0) {
4172 printk(KERN_ERR "hda_codec: cannot attach "
4173 "PCM stream %d for codec #%d\n",
4174 dev, codec->addr);
4175 continue; /* no fatal error */
4176 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004177 }
4178 }
4179 return 0;
4180}
4181
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182/**
4183 * snd_hda_build_pcms - build PCM information
4184 * @bus: the BUS
4185 *
4186 * Create PCM information for each codec included in the bus.
4187 *
4188 * The build_pcms codec patch is requested to set up codec->num_pcms and
4189 * codec->pcm_info properly. The array is referred by the top-level driver
4190 * to create its PCM instances.
4191 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4192 * callback.
4193 *
4194 * At least, substreams, channels_min and channels_max must be filled for
4195 * each stream. substreams = 0 indicates that the stream doesn't exist.
4196 * When rates and/or formats are zero, the supported values are queried
4197 * from the given nid. The nid is used also by the default ops.prepare
4198 * and ops.cleanup callbacks.
4199 *
4200 * The driver needs to call ops.open in its open callback. Similarly,
4201 * ops.close is supposed to be called in the close callback.
4202 * ops.prepare should be called in the prepare or hw_params callback
4203 * with the proper parameters for set up.
4204 * ops.cleanup should be called in hw_free for clean up of streams.
4205 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004206 * This function returns 0 if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004208int __devinit snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004210 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211
Takashi Iwai0ba21762007-04-16 11:29:14 +02004212 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004213 int err = snd_hda_codec_build_pcms(codec);
4214 if (err < 0)
4215 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216 }
4217 return 0;
4218}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004219EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221/**
4222 * snd_hda_check_board_config - compare the current codec with the config table
4223 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004224 * @num_configs: number of config enums
4225 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226 * @tbl: configuration table, terminated by null entries
4227 *
4228 * Compares the modelname or PCI subsystem id of the current codec with the
4229 * given configuration table. If a matching entry is found, returns its
4230 * config value (supposed to be 0 or positive).
4231 *
4232 * If no entries are matching, the function returns a negative value.
4233 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02004234int snd_hda_check_board_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004235 int num_configs, const char * const *models,
Takashi Iwai12f288b2007-08-02 15:51:59 +02004236 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237{
Takashi Iwaif44ac832008-07-30 15:01:45 +02004238 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004239 int i;
4240 for (i = 0; i < num_configs; i++) {
4241 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02004242 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004243 snd_printd(KERN_INFO "hda_codec: model '%s' is "
4244 "selected\n", models[i]);
4245 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246 }
4247 }
4248 }
4249
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004250 if (!codec->bus->pci || !tbl)
4251 return -1;
4252
4253 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
4254 if (!tbl)
4255 return -1;
4256 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02004257#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004258 char tmp[10];
4259 const char *model = NULL;
4260 if (models)
4261 model = models[tbl->value];
4262 if (!model) {
4263 sprintf(tmp, "#%d", tbl->value);
4264 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004266 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4267 "for config %x:%x (%s)\n",
4268 model, tbl->subvendor, tbl->subdevice,
4269 (tbl->name ? tbl->name : "Unknown device"));
4270#endif
4271 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272 }
4273 return -1;
4274}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004275EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276
4277/**
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004278 * snd_hda_check_board_codec_sid_config - compare the current codec
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004279 subsystem ID with the
4280 config table
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004281
4282 This is important for Gateway notebooks with SB450 HDA Audio
4283 where the vendor ID of the PCI device is:
4284 ATI Technologies Inc SB450 HDA Audio [1002:437b]
4285 and the vendor/subvendor are found only at the codec.
4286
4287 * @codec: the HDA codec
4288 * @num_configs: number of config enums
4289 * @models: array of model name strings
4290 * @tbl: configuration table, terminated by null entries
4291 *
4292 * Compares the modelname or PCI subsystem id of the current codec with the
4293 * given configuration table. If a matching entry is found, returns its
4294 * config value (supposed to be 0 or positive).
4295 *
4296 * If no entries are matching, the function returns a negative value.
4297 */
4298int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004299 int num_configs, const char * const *models,
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004300 const struct snd_pci_quirk *tbl)
4301{
4302 const struct snd_pci_quirk *q;
4303
4304 /* Search for codec ID */
4305 for (q = tbl; q->subvendor; q++) {
Takashi Iwaie2301a42011-11-22 19:58:56 +01004306 unsigned int mask = 0xffff0000 | q->subdevice_mask;
4307 unsigned int id = (q->subdevice | (q->subvendor << 16)) & mask;
4308 if ((codec->subsystem_id & mask) == id)
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004309 break;
4310 }
4311
4312 if (!q->subvendor)
4313 return -1;
4314
4315 tbl = q;
4316
4317 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwaid94ff6b2009-09-02 00:20:21 +02004318#ifdef CONFIG_SND_DEBUG_VERBOSE
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004319 char tmp[10];
4320 const char *model = NULL;
4321 if (models)
4322 model = models[tbl->value];
4323 if (!model) {
4324 sprintf(tmp, "#%d", tbl->value);
4325 model = tmp;
4326 }
4327 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4328 "for config %x:%x (%s)\n",
4329 model, tbl->subvendor, tbl->subdevice,
4330 (tbl->name ? tbl->name : "Unknown device"));
4331#endif
4332 return tbl->value;
4333 }
4334 return -1;
4335}
4336EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
4337
4338/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339 * snd_hda_add_new_ctls - create controls from the array
4340 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004341 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342 *
4343 * This helper function creates and add new controls in the given array.
4344 * The array must be terminated with an empty entry as terminator.
4345 *
4346 * Returns 0 if successful, or a negative error code.
4347 */
Takashi Iwai031024e2011-05-02 11:29:30 +02004348int snd_hda_add_new_ctls(struct hda_codec *codec,
4349 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01004351 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352
4353 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004354 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004355 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01004356 if (knew->iface == -1) /* skip this codec private value */
4357 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004358 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004359 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02004360 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01004361 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004362 if (addr > 0)
4363 kctl->id.device = addr;
4364 if (idx > 0)
4365 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01004366 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004367 if (!err)
4368 break;
4369 /* try first with another device index corresponding to
4370 * the codec addr; if it still fails (or it's the
4371 * primary codec), then try another control index
4372 */
4373 if (!addr && codec->addr)
4374 addr = codec->addr;
4375 else if (!idx && !knew->index) {
4376 idx = find_empty_mixer_ctl_idx(codec,
4377 knew->name);
4378 if (idx <= 0)
4379 return err;
4380 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01004381 return err;
4382 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383 }
4384 return 0;
4385}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004386EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387
Takashi Iwaicb53c622007-08-10 17:21:45 +02004388#ifdef CONFIG_SND_HDA_POWER_SAVE
Takashi Iwaicb53c622007-08-10 17:21:45 +02004389static void hda_power_work(struct work_struct *work)
4390{
4391 struct hda_codec *codec =
4392 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004393 struct hda_bus *bus = codec->bus;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004394
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004395 spin_lock(&codec->power_lock);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004396 if (codec->power_transition > 0) { /* during power-up sequence? */
4397 spin_unlock(&codec->power_lock);
4398 return;
4399 }
Maxim Levitsky2e492462007-09-03 15:26:57 +02004400 if (!codec->power_on || codec->power_count) {
4401 codec->power_transition = 0;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004402 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004403 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02004404 }
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004405 spin_unlock(&codec->power_lock);
4406
Takashi Iwaicb53c622007-08-10 17:21:45 +02004407 hda_call_codec_suspend(codec);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004408 if (bus->ops.pm_notify)
4409 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004410}
4411
4412static void hda_keep_power_on(struct hda_codec *codec)
4413{
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004414 spin_lock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004415 codec->power_count++;
4416 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004417 codec->power_jiffies = jiffies;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004418 spin_unlock(&codec->power_lock);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004419}
4420
Takashi Iwaid5191e52009-11-16 14:58:17 +01004421/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01004422void snd_hda_update_power_acct(struct hda_codec *codec)
4423{
4424 unsigned long delta = jiffies - codec->power_jiffies;
4425 if (codec->power_on)
4426 codec->power_on_acct += delta;
4427 else
4428 codec->power_off_acct += delta;
4429 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004430}
4431
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004432/* Transition to powered up, if wait_power_down then wait for a pending
4433 * transition to D3 to complete. A pending D3 transition is indicated
4434 * with power_transition == -1. */
4435static void __snd_hda_power_up(struct hda_codec *codec, bool wait_power_down)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004436{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004437 struct hda_bus *bus = codec->bus;
4438
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004439 spin_lock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004440 codec->power_count++;
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004441 /* Return if power_on or transitioning to power_on, unless currently
4442 * powering down. */
4443 if ((codec->power_on || codec->power_transition > 0) &&
4444 !(wait_power_down && codec->power_transition < 0)) {
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004445 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004446 return;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004447 }
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004448 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004449
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004450 cancel_delayed_work_sync(&codec->power_work);
4451
4452 spin_lock(&codec->power_lock);
Dylan Reidb43d2242012-06-21 21:51:22 -07004453 /* If the power down delayed work was cancelled above before starting,
4454 * then there is no need to go through power up here.
4455 */
4456 if (codec->power_on) {
4457 spin_unlock(&codec->power_lock);
4458 return;
4459 }
Takashi Iwaid66fee52011-08-02 15:39:31 +02004460 trace_hda_power_up(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004461 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004462 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004463 codec->power_jiffies = jiffies;
Takashi Iwai7f308302012-05-08 16:52:23 +02004464 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004465 spin_unlock(&codec->power_lock);
4466
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004467 if (bus->ops.pm_notify)
4468 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004469 hda_call_codec_resume(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004470
4471 spin_lock(&codec->power_lock);
Takashi Iwaia221e282007-08-16 16:35:33 +02004472 codec->power_transition = 0;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004473 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004474}
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004475
4476/**
4477 * snd_hda_power_up - Power-up the codec
4478 * @codec: HD-audio codec
4479 *
4480 * Increment the power-up counter and power up the hardware really when
4481 * not turned on yet.
4482 */
4483void snd_hda_power_up(struct hda_codec *codec)
4484{
4485 __snd_hda_power_up(codec, false);
4486}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004487EXPORT_SYMBOL_HDA(snd_hda_power_up);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004488
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004489/**
4490 * snd_hda_power_up_d3wait - Power-up the codec after waiting for any pending
4491 * D3 transition to complete. This differs from snd_hda_power_up() when
4492 * power_transition == -1. snd_hda_power_up sees this case as a nop,
4493 * snd_hda_power_up_d3wait waits for the D3 transition to complete then powers
4494 * back up.
4495 * @codec: HD-audio codec
4496 *
4497 * Cancel any power down operation hapenning on the work queue, then power up.
4498 */
4499void snd_hda_power_up_d3wait(struct hda_codec *codec)
4500{
4501 /* This will cancel and wait for pending power_work to complete. */
4502 __snd_hda_power_up(codec, true);
4503}
4504EXPORT_SYMBOL_HDA(snd_hda_power_up_d3wait);
4505
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004506#define power_save(codec) \
4507 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004508
Takashi Iwaid5191e52009-11-16 14:58:17 +01004509/**
4510 * snd_hda_power_down - Power-down the codec
4511 * @codec: HD-audio codec
4512 *
4513 * Decrement the power-up counter and schedules the power-off work if
4514 * the counter rearches to zero.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004515 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004516void snd_hda_power_down(struct hda_codec *codec)
4517{
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004518 spin_lock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004519 --codec->power_count;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004520 if (!codec->power_on || codec->power_count || codec->power_transition) {
4521 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004522 return;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004523 }
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004524 if (power_save(codec)) {
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004525 codec->power_transition = -1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01004526 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004527 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02004528 }
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004529 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004530}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004531EXPORT_SYMBOL_HDA(snd_hda_power_down);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004532
Takashi Iwaid5191e52009-11-16 14:58:17 +01004533/**
4534 * snd_hda_check_amp_list_power - Check the amp list and update the power
4535 * @codec: HD-audio codec
4536 * @check: the object containing an AMP list and the status
4537 * @nid: NID to check / update
4538 *
4539 * Check whether the given NID is in the amp list. If it's in the list,
4540 * check the current AMP status, and update the the power-status according
4541 * to the mute status.
4542 *
4543 * This function is supposed to be set or called from the check_power_status
4544 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004545 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004546int snd_hda_check_amp_list_power(struct hda_codec *codec,
4547 struct hda_loopback_check *check,
4548 hda_nid_t nid)
4549{
Takashi Iwai031024e2011-05-02 11:29:30 +02004550 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004551 int ch, v;
4552
4553 if (!check->amplist)
4554 return 0;
4555 for (p = check->amplist; p->nid; p++) {
4556 if (p->nid == nid)
4557 break;
4558 }
4559 if (!p->nid)
4560 return 0; /* nothing changed */
4561
4562 for (p = check->amplist; p->nid; p++) {
4563 for (ch = 0; ch < 2; ch++) {
4564 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4565 p->idx);
4566 if (!(v & HDA_AMP_MUTE) && v > 0) {
4567 if (!check->power_on) {
4568 check->power_on = 1;
4569 snd_hda_power_up(codec);
4570 }
4571 return 1;
4572 }
4573 }
4574 }
4575 if (check->power_on) {
4576 check->power_on = 0;
4577 snd_hda_power_down(codec);
4578 }
4579 return 0;
4580}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004581EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004582#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004584/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004585 * Channel mode helper
4586 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004587
4588/**
4589 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
4590 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004591int snd_hda_ch_mode_info(struct hda_codec *codec,
4592 struct snd_ctl_elem_info *uinfo,
4593 const struct hda_channel_mode *chmode,
4594 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004595{
4596 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4597 uinfo->count = 1;
4598 uinfo->value.enumerated.items = num_chmodes;
4599 if (uinfo->value.enumerated.item >= num_chmodes)
4600 uinfo->value.enumerated.item = num_chmodes - 1;
4601 sprintf(uinfo->value.enumerated.name, "%dch",
4602 chmode[uinfo->value.enumerated.item].channels);
4603 return 0;
4604}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004605EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004606
Takashi Iwaid5191e52009-11-16 14:58:17 +01004607/**
4608 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
4609 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004610int snd_hda_ch_mode_get(struct hda_codec *codec,
4611 struct snd_ctl_elem_value *ucontrol,
4612 const struct hda_channel_mode *chmode,
4613 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004614 int max_channels)
4615{
4616 int i;
4617
4618 for (i = 0; i < num_chmodes; i++) {
4619 if (max_channels == chmode[i].channels) {
4620 ucontrol->value.enumerated.item[0] = i;
4621 break;
4622 }
4623 }
4624 return 0;
4625}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004626EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004627
Takashi Iwaid5191e52009-11-16 14:58:17 +01004628/**
4629 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
4630 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004631int snd_hda_ch_mode_put(struct hda_codec *codec,
4632 struct snd_ctl_elem_value *ucontrol,
4633 const struct hda_channel_mode *chmode,
4634 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004635 int *max_channelsp)
4636{
4637 unsigned int mode;
4638
4639 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01004640 if (mode >= num_chmodes)
4641 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004642 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004643 return 0;
4644 /* change the current channel setting */
4645 *max_channelsp = chmode[mode].channels;
4646 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004647 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004648 return 1;
4649}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004650EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004651
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652/*
4653 * input MUX helper
4654 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004655
4656/**
4657 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
4658 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004659int snd_hda_input_mux_info(const struct hda_input_mux *imux,
4660 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661{
4662 unsigned int index;
4663
4664 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4665 uinfo->count = 1;
4666 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004667 if (!imux->num_items)
4668 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669 index = uinfo->value.enumerated.item;
4670 if (index >= imux->num_items)
4671 index = imux->num_items - 1;
4672 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
4673 return 0;
4674}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004675EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676
Takashi Iwaid5191e52009-11-16 14:58:17 +01004677/**
4678 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
4679 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004680int snd_hda_input_mux_put(struct hda_codec *codec,
4681 const struct hda_input_mux *imux,
4682 struct snd_ctl_elem_value *ucontrol,
4683 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 unsigned int *cur_val)
4685{
4686 unsigned int idx;
4687
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004688 if (!imux->num_items)
4689 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690 idx = ucontrol->value.enumerated.item[0];
4691 if (idx >= imux->num_items)
4692 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004693 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004695 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
4696 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697 *cur_val = idx;
4698 return 1;
4699}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004700EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701
4702
4703/*
4704 * Multi-channel / digital-out PCM helper functions
4705 */
4706
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004707/* setup SPDIF output stream */
4708static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
4709 unsigned int stream_tag, unsigned int format)
4710{
Stephen Warren7c9359762011-06-01 11:14:17 -06004711 struct hda_spdif_out *spdif = snd_hda_spdif_out_of_nid(codec, nid);
4712
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004713 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Stephen Warren7c9359762011-06-01 11:14:17 -06004714 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004715 set_dig_out_convert(codec, nid,
Stephen Warren7c9359762011-06-01 11:14:17 -06004716 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02004717 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004718 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02004719 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004720 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004721 for (d = codec->slave_dig_outs; *d; d++)
4722 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
4723 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004724 }
Takashi Iwai2f728532008-09-25 16:32:41 +02004725 /* turn on again (if needed) */
Stephen Warren7c9359762011-06-01 11:14:17 -06004726 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai2f728532008-09-25 16:32:41 +02004727 set_dig_out_convert(codec, nid,
Stephen Warren7c9359762011-06-01 11:14:17 -06004728 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02004729}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004730
Takashi Iwai2f728532008-09-25 16:32:41 +02004731static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
4732{
4733 snd_hda_codec_cleanup_stream(codec, nid);
4734 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004735 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004736 for (d = codec->slave_dig_outs; *d; d++)
4737 snd_hda_codec_cleanup_stream(codec, *d);
4738 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004739}
4740
Takashi Iwaid5191e52009-11-16 14:58:17 +01004741/**
4742 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
4743 * @bus: HD-audio bus
4744 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004745void snd_hda_bus_reboot_notify(struct hda_bus *bus)
4746{
4747 struct hda_codec *codec;
4748
4749 if (!bus)
4750 return;
4751 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02004752 if (hda_codec_is_power_on(codec) &&
4753 codec->patch_ops.reboot_notify)
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004754 codec->patch_ops.reboot_notify(codec);
4755 }
4756}
Takashi Iwai8f217a22009-11-10 18:26:12 +01004757EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004758
Takashi Iwaid5191e52009-11-16 14:58:17 +01004759/**
4760 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004762int snd_hda_multi_out_dig_open(struct hda_codec *codec,
4763 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764{
Ingo Molnar62932df2006-01-16 16:34:20 +01004765 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02004766 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
4767 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02004768 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01004770 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771 return 0;
4772}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004773EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774
Takashi Iwaid5191e52009-11-16 14:58:17 +01004775/**
4776 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
4777 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004778int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
4779 struct hda_multi_out *mout,
4780 unsigned int stream_tag,
4781 unsigned int format,
4782 struct snd_pcm_substream *substream)
4783{
4784 mutex_lock(&codec->spdif_mutex);
4785 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
4786 mutex_unlock(&codec->spdif_mutex);
4787 return 0;
4788}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004789EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004790
Takashi Iwaid5191e52009-11-16 14:58:17 +01004791/**
4792 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
4793 */
Takashi Iwai9411e212009-02-13 11:32:28 +01004794int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
4795 struct hda_multi_out *mout)
4796{
4797 mutex_lock(&codec->spdif_mutex);
4798 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4799 mutex_unlock(&codec->spdif_mutex);
4800 return 0;
4801}
4802EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
4803
Takashi Iwaid5191e52009-11-16 14:58:17 +01004804/**
4805 * snd_hda_multi_out_dig_close - release the digital out stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004807int snd_hda_multi_out_dig_close(struct hda_codec *codec,
4808 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809{
Ingo Molnar62932df2006-01-16 16:34:20 +01004810 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01004812 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813 return 0;
4814}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004815EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816
Takashi Iwaid5191e52009-11-16 14:58:17 +01004817/**
4818 * snd_hda_multi_out_analog_open - open analog outputs
4819 *
4820 * Open analog outputs and set up the hw-constraints.
4821 * If the digital outputs can be opened as slave, open the digital
4822 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004824int snd_hda_multi_out_analog_open(struct hda_codec *codec,
4825 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01004826 struct snd_pcm_substream *substream,
4827 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828{
Takashi Iwai9a081602008-02-12 18:37:26 +01004829 struct snd_pcm_runtime *runtime = substream->runtime;
4830 runtime->hw.channels_max = mout->max_channels;
4831 if (mout->dig_out_nid) {
4832 if (!mout->analog_rates) {
4833 mout->analog_rates = hinfo->rates;
4834 mout->analog_formats = hinfo->formats;
4835 mout->analog_maxbps = hinfo->maxbps;
4836 } else {
4837 runtime->hw.rates = mout->analog_rates;
4838 runtime->hw.formats = mout->analog_formats;
4839 hinfo->maxbps = mout->analog_maxbps;
4840 }
4841 if (!mout->spdif_rates) {
4842 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
4843 &mout->spdif_rates,
4844 &mout->spdif_formats,
4845 &mout->spdif_maxbps);
4846 }
4847 mutex_lock(&codec->spdif_mutex);
4848 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02004849 if ((runtime->hw.rates & mout->spdif_rates) &&
4850 (runtime->hw.formats & mout->spdif_formats)) {
4851 runtime->hw.rates &= mout->spdif_rates;
4852 runtime->hw.formats &= mout->spdif_formats;
4853 if (mout->spdif_maxbps < hinfo->maxbps)
4854 hinfo->maxbps = mout->spdif_maxbps;
4855 } else {
4856 mout->share_spdif = 0;
4857 /* FIXME: need notify? */
4858 }
Takashi Iwai9a081602008-02-12 18:37:26 +01004859 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02004860 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004861 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862 return snd_pcm_hw_constraint_step(substream->runtime, 0,
4863 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
4864}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004865EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866
Takashi Iwaid5191e52009-11-16 14:58:17 +01004867/**
4868 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
4869 *
4870 * Set up the i/o for analog out.
4871 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004873int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
4874 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875 unsigned int stream_tag,
4876 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004877 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878{
Takashi Iwaidda14412011-05-02 11:29:30 +02004879 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880 int chs = substream->runtime->channels;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02004881 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882 int i;
4883
Ingo Molnar62932df2006-01-16 16:34:20 +01004884 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02004885 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Takashi Iwai9a081602008-02-12 18:37:26 +01004886 if (mout->dig_out_nid && mout->share_spdif &&
4887 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02004889 snd_hda_is_supported_format(codec, mout->dig_out_nid,
4890 format) &&
Stephen Warren7c9359762011-06-01 11:14:17 -06004891 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004892 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004893 setup_dig_out_stream(codec, mout->dig_out_nid,
4894 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895 } else {
4896 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02004897 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898 }
4899 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004900 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901
4902 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004903 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
4904 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004905 if (!mout->no_share_stream &&
4906 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004908 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
4909 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004910 /* extra outputs copied from front */
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02004911 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
4912 if (!mout->no_share_stream && mout->hp_out_nid[i])
4913 snd_hda_codec_setup_stream(codec,
4914 mout->hp_out_nid[i],
4915 stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004916 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02004917 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01004918 snd_hda_codec_setup_stream(codec,
4919 mout->extra_out_nid[i],
4920 stream_tag, 0, format);
4921
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922 /* surrounds */
4923 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02004924 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004925 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4926 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004927 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004928 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4929 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930 }
4931 return 0;
4932}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004933EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934
Takashi Iwaid5191e52009-11-16 14:58:17 +01004935/**
4936 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004938int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
4939 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940{
Takashi Iwaidda14412011-05-02 11:29:30 +02004941 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942 int i;
4943
4944 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01004945 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01004947 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02004948 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
4949 if (mout->hp_out_nid[i])
4950 snd_hda_codec_cleanup_stream(codec,
4951 mout->hp_out_nid[i]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004952 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
4953 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01004954 snd_hda_codec_cleanup_stream(codec,
4955 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01004956 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02004958 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959 mout->dig_out_used = 0;
4960 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004961 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962 return 0;
4963}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004964EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965
Takashi Iwai47408602012-04-20 13:06:53 +02004966/**
4967 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
4968 *
4969 * Guess the suitable VREF pin bits to be set as the pin-control value.
4970 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
4971 */
4972unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
4973{
4974 unsigned int pincap;
4975 unsigned int oldval;
4976 oldval = snd_hda_codec_read(codec, pin, 0,
4977 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
4978 pincap = snd_hda_query_pin_caps(codec, pin);
4979 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
4980 /* Exception: if the default pin setup is vref50, we give it priority */
4981 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
4982 return AC_PINCTL_VREF_80;
4983 else if (pincap & AC_PINCAP_VREF_50)
4984 return AC_PINCTL_VREF_50;
4985 else if (pincap & AC_PINCAP_VREF_100)
4986 return AC_PINCTL_VREF_100;
4987 else if (pincap & AC_PINCAP_VREF_GRD)
4988 return AC_PINCTL_VREF_GRD;
4989 return AC_PINCTL_VREF_HIZ;
4990}
4991EXPORT_SYMBOL_HDA(snd_hda_get_default_vref);
4992
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02004993int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
4994 unsigned int val, bool cached)
4995{
4996 if (val) {
4997 unsigned int cap = snd_hda_query_pin_caps(codec, pin);
Takashi Iwai6942c102012-04-20 13:08:40 +02004998 if (cap && (val & AC_PINCTL_OUT_EN)) {
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02004999 if (!(cap & AC_PINCAP_OUT))
5000 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
5001 else if ((val & AC_PINCTL_HP_EN) &&
5002 !(cap & AC_PINCAP_HP_DRV))
5003 val &= ~AC_PINCTL_HP_EN;
5004 }
Takashi Iwai6942c102012-04-20 13:08:40 +02005005 if (cap && (val & AC_PINCTL_IN_EN)) {
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005006 if (!(cap & AC_PINCAP_IN))
5007 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
5008 }
5009 }
5010 if (cached)
5011 return snd_hda_codec_update_cache(codec, pin, 0,
5012 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5013 else
5014 return snd_hda_codec_write(codec, pin, 0,
5015 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5016}
5017EXPORT_SYMBOL_HDA(_snd_hda_set_pin_ctl);
5018
Takashi Iwai990061c2010-09-09 22:08:44 +02005019/**
5020 * snd_hda_add_imux_item - Add an item to input_mux
5021 *
5022 * When the same label is used already in the existing items, the number
5023 * suffix is appended to the label. This label index number is stored
5024 * to type_idx when non-NULL pointer is given.
5025 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02005026int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
5027 int index, int *type_idx)
5028{
5029 int i, label_idx = 0;
5030 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
5031 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
5032 return -EINVAL;
5033 }
5034 for (i = 0; i < imux->num_items; i++) {
5035 if (!strncmp(label, imux->items[i].label, strlen(label)))
5036 label_idx++;
5037 }
5038 if (type_idx)
5039 *type_idx = label_idx;
5040 if (label_idx > 0)
5041 snprintf(imux->items[imux->num_items].label,
5042 sizeof(imux->items[imux->num_items].label),
5043 "%s %d", label, label_idx);
5044 else
5045 strlcpy(imux->items[imux->num_items].label, label,
5046 sizeof(imux->items[imux->num_items].label));
5047 imux->items[imux->num_items].index = index;
5048 imux->num_items++;
5049 return 0;
5050}
5051EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005052
Takashi Iwai4a471b72005-12-07 13:56:29 +01005053
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054#ifdef CONFIG_PM
5055/*
5056 * power management
5057 */
5058
5059/**
5060 * snd_hda_suspend - suspend the codecs
5061 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005062 *
5063 * Returns 0 if successful.
5064 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02005065int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005067 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068
Takashi Iwai0ba21762007-04-16 11:29:14 +02005069 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005070 if (hda_codec_is_power_on(codec))
5071 hda_call_codec_suspend(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072 }
5073 return 0;
5074}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005075EXPORT_SYMBOL_HDA(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076
5077/**
5078 * snd_hda_resume - resume the codecs
5079 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080 *
5081 * Returns 0 if successful.
Takashi Iwaicb53c622007-08-10 17:21:45 +02005082 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005083 * This function is defined only when POWER_SAVE isn't set.
Takashi Iwaicb53c622007-08-10 17:21:45 +02005084 * In the power-save mode, the codec is resumed dynamically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085 */
5086int snd_hda_resume(struct hda_bus *bus)
5087{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005088 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089
Takashi Iwai0ba21762007-04-16 11:29:14 +02005090 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai7f308302012-05-08 16:52:23 +02005091 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093 return 0;
5094}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005095EXPORT_SYMBOL_HDA(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005096#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005097
5098/*
5099 * generic arrays
5100 */
5101
Takashi Iwaid5191e52009-11-16 14:58:17 +01005102/**
5103 * snd_array_new - get a new element from the given array
5104 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005105 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01005106 * Get a new element from the given array. If it exceeds the
5107 * pre-allocated array size, re-allocate the array.
5108 *
5109 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02005110 */
5111void *snd_array_new(struct snd_array *array)
5112{
5113 if (array->used >= array->alloced) {
5114 int num = array->alloced + array->alloc_align;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005115 int size = (num + 1) * array->elem_size;
Takashi Iwai00ef9612011-07-14 15:57:27 +02005116 int oldsize = array->alloced * array->elem_size;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01005117 void *nlist;
5118 if (snd_BUG_ON(num >= 4096))
5119 return NULL;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005120 nlist = krealloc(array->list, size, GFP_KERNEL);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005121 if (!nlist)
5122 return NULL;
Takashi Iwai00ef9612011-07-14 15:57:27 +02005123 memset(nlist + oldsize, 0, size - oldsize);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005124 array->list = nlist;
5125 array->alloced = num;
5126 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01005127 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005128}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005129EXPORT_SYMBOL_HDA(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005130
Takashi Iwaid5191e52009-11-16 14:58:17 +01005131/**
5132 * snd_array_free - free the given array elements
5133 * @array: the array object
5134 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005135void snd_array_free(struct snd_array *array)
5136{
5137 kfree(array->list);
5138 array->used = 0;
5139 array->alloced = 0;
5140 array->list = NULL;
5141}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005142EXPORT_SYMBOL_HDA(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01005143
Takashi Iwaid5191e52009-11-16 14:58:17 +01005144/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01005145 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5146 * @pcm: PCM caps bits
5147 * @buf: the string buffer to write
5148 * @buflen: the max buffer length
5149 *
5150 * used by hda_proc.c and hda_eld.c
5151 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005152void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5153{
5154 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5155 int i, j;
5156
5157 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5158 if (pcm & (AC_SUPPCM_BITS_8 << i))
5159 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5160
5161 buf[j] = '\0'; /* necessary when j == 0 */
5162}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005163EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005164
5165MODULE_DESCRIPTION("HDA codec core");
5166MODULE_LICENSE("GPL");