blob: 0de1f76733d2a6c2fe40e5fa4cc55751c8493c8d [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
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001212static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec,
1213 hda_nid_t fg, unsigned int power_state);
1214
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001215static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1216 unsigned int power_state);
1217
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218/**
1219 * snd_hda_codec_new - create a HDA codec
1220 * @bus: the bus to assign
1221 * @codec_addr: the codec address
1222 * @codecp: the pointer to store the generated codec
1223 *
1224 * Returns 0 if successful, or a negative error code.
1225 */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001226int /*__devinit*/ snd_hda_codec_new(struct hda_bus *bus,
1227 unsigned int codec_addr,
1228 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229{
1230 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +02001231 char component[31];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 int err;
1233
Takashi Iwaida3cec32008-08-08 17:12:14 +02001234 if (snd_BUG_ON(!bus))
1235 return -EINVAL;
1236 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1237 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
1239 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001240 snd_printk(KERN_ERR "hda_codec: "
1241 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 return -EBUSY;
1243 }
1244
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001245 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 if (codec == NULL) {
1247 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1248 return -ENOMEM;
1249 }
1250
1251 codec->bus = bus;
1252 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001253 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001254 mutex_init(&codec->control_mutex);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001255 mutex_init(&codec->hash_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +02001256 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001257 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001258 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1259 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001260 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001261 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +02001262 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Takashi Iwaia12d3e12011-04-07 15:55:15 +02001263 snd_array_init(&codec->conn_lists, sizeof(hda_nid_t), 64);
Stephen Warren7c9359762011-06-01 11:14:17 -06001264 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
Takashi Iwaicb53c622007-08-10 17:21:45 +02001266#ifdef CONFIG_SND_HDA_POWER_SAVE
Takashi Iwai5536c6d2012-05-08 17:08:10 +02001267 spin_lock_init(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001268 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1269 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1270 * the caller has to power down appropriatley after initialization
1271 * phase.
1272 */
1273 hda_keep_power_on(codec);
1274#endif
1275
Takashi Iwaic382a9f2012-05-07 15:01:02 +02001276 if (codec->bus->modelname) {
1277 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1278 if (!codec->modelname) {
1279 snd_hda_codec_free(codec);
1280 return -ENODEV;
1281 }
1282 }
1283
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 list_add_tail(&codec->list, &bus->codec_list);
1285 bus->caddr_tbl[codec_addr] = codec;
1286
Takashi Iwai0ba21762007-04-16 11:29:14 +02001287 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1288 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001289 if (codec->vendor_id == -1)
1290 /* read again, hopefully the access method was corrected
1291 * in the last read...
1292 */
1293 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1294 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001295 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1296 AC_PAR_SUBSYSTEM_ID);
1297 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1298 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001300 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001301 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001302 snd_printdd("hda_codec: no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001303 err = -ENODEV;
1304 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 }
1306
Takashi Iwai3be14142009-02-20 14:11:16 +01001307 err = read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg);
1308 if (err < 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +01001309 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001310 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001311 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001312 err = read_pin_defaults(codec);
1313 if (err < 0)
1314 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001315
Takashi Iwai0ba21762007-04-16 11:29:14 +02001316 if (!codec->subsystem_id) {
Takashi Iwai86284e42005-10-11 15:05:54 +02001317 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001318 codec->subsystem_id =
1319 snd_hda_codec_read(codec, nid, 0,
1320 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001321 }
1322
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001323 codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec,
1324 codec->afg ? codec->afg : codec->mfg,
1325 AC_PWRST_CLKSTOP);
1326 if (!codec->d3_stop_clk)
1327 bus->power_keep_link_on = 1;
1328
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001329 /* power-up all before initialization */
1330 hda_set_power_state(codec,
1331 codec->afg ? codec->afg : codec->mfg,
1332 AC_PWRST_D0);
1333
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001334 snd_hda_codec_proc_new(codec);
1335
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001336 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001337
1338 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1339 codec->subsystem_id, codec->revision_id);
1340 snd_component_add(codec->bus->card, component);
1341
1342 if (codecp)
1343 *codecp = codec;
1344 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001345
1346 error:
1347 snd_hda_codec_free(codec);
1348 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001349}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001350EXPORT_SYMBOL_HDA(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001351
Takashi Iwaid5191e52009-11-16 14:58:17 +01001352/**
1353 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1354 * @codec: the HDA codec
1355 *
1356 * Start parsing of the given codec tree and (re-)initialize the whole
1357 * patch instance.
1358 *
1359 * Returns 0 if successful or a negative error code.
1360 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001361int snd_hda_codec_configure(struct hda_codec *codec)
1362{
1363 int err;
1364
Takashi Iwaid5ad6302007-03-07 15:55:59 +01001365 codec->preset = find_codec_preset(codec);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001366 if (!codec->vendor_name || !codec->chip_name) {
Takashi Iwaif44ac832008-07-30 15:01:45 +02001367 err = get_codec_name(codec);
1368 if (err < 0)
1369 return err;
1370 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
Takashi Iwai82467612007-07-27 19:15:54 +02001372 if (is_generic_config(codec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai82467612007-07-27 19:15:54 +02001374 goto patched;
1375 }
Takashi Iwai82467612007-07-27 19:15:54 +02001376 if (codec->preset && codec->preset->patch) {
1377 err = codec->preset->patch(codec);
1378 goto patched;
1379 }
1380
1381 /* call the default parser */
Takashi Iwai82467612007-07-27 19:15:54 +02001382 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai35a1e0c2007-10-19 08:13:40 +02001383 if (err < 0)
1384 printk(KERN_ERR "hda-codec: No codec parser is available\n");
Takashi Iwai82467612007-07-27 19:15:54 +02001385
1386 patched:
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001387 if (!err && codec->patch_ops.unsol_event)
1388 err = init_unsol_queue(codec->bus);
Takashi Iwaif62faed2009-12-23 09:27:51 +01001389 /* audio codec should override the mixer name */
1390 if (!err && (codec->afg || !*codec->bus->card->mixername))
1391 snprintf(codec->bus->card->mixername,
1392 sizeof(codec->bus->card->mixername),
1393 "%s %s", codec->vendor_name, codec->chip_name);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001394 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395}
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001396EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
Takashi Iwaied360812012-08-08 17:12:52 +02001398/* update the stream-id if changed */
1399static void update_pcm_stream_id(struct hda_codec *codec,
1400 struct hda_cvt_setup *p, hda_nid_t nid,
1401 u32 stream_tag, int channel_id)
1402{
1403 unsigned int oldval, newval;
1404
1405 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1406 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1407 newval = (stream_tag << 4) | channel_id;
1408 if (oldval != newval)
1409 snd_hda_codec_write(codec, nid, 0,
1410 AC_VERB_SET_CHANNEL_STREAMID,
1411 newval);
1412 p->stream_tag = stream_tag;
1413 p->channel_id = channel_id;
1414 }
1415}
1416
1417/* update the format-id if changed */
1418static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1419 hda_nid_t nid, int format)
1420{
1421 unsigned int oldval;
1422
1423 if (p->format_id != format) {
1424 oldval = snd_hda_codec_read(codec, nid, 0,
1425 AC_VERB_GET_STREAM_FORMAT, 0);
1426 if (oldval != format) {
1427 msleep(1);
1428 snd_hda_codec_write(codec, nid, 0,
1429 AC_VERB_SET_STREAM_FORMAT,
1430 format);
1431 }
1432 p->format_id = format;
1433 }
1434}
1435
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436/**
1437 * snd_hda_codec_setup_stream - set up the codec for streaming
1438 * @codec: the CODEC to set up
1439 * @nid: the NID to set up
1440 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1441 * @channel_id: channel id to pass, zero based.
1442 * @format: stream format.
1443 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001444void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1445 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 int channel_id, int format)
1447{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001448 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001449 struct hda_cvt_setup *p;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001450 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001451 int i;
1452
Takashi Iwai0ba21762007-04-16 11:29:14 +02001453 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001454 return;
1455
Takashi Iwai0ba21762007-04-16 11:29:14 +02001456 snd_printdd("hda_codec_setup_stream: "
1457 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001459 p = get_hda_cvt_setup(codec, nid);
1460 if (!p)
1461 return;
Takashi Iwaied360812012-08-08 17:12:52 +02001462
1463 if (codec->pcm_format_first)
1464 update_pcm_format(codec, p, nid, format);
1465 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1466 if (!codec->pcm_format_first)
1467 update_pcm_format(codec, p, nid, format);
1468
Takashi Iwaieb541332010-08-06 13:48:11 +02001469 p->active = 1;
1470 p->dirty = 0;
1471
1472 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001473 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001474 list_for_each_entry(c, &codec->bus->codec_list, list) {
1475 for (i = 0; i < c->cvt_setups.used; i++) {
1476 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001477 if (!p->active && p->stream_tag == stream_tag &&
David Henningsson54c2a892012-02-01 12:05:41 +01001478 get_wcaps_type(get_wcaps(c, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001479 p->dirty = 1;
1480 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001483EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
Takashi Iwaif0cea792010-08-13 11:56:53 +02001485static void really_cleanup_stream(struct hda_codec *codec,
1486 struct hda_cvt_setup *q);
1487
Takashi Iwaid5191e52009-11-16 14:58:17 +01001488/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001489 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001490 * @codec: the CODEC to clean up
1491 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001492 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001493 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001494void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1495 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001496{
Takashi Iwaieb541332010-08-06 13:48:11 +02001497 struct hda_cvt_setup *p;
1498
Takashi Iwai888afa12008-03-18 09:57:50 +01001499 if (!nid)
1500 return;
1501
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001502 if (codec->no_sticky_stream)
1503 do_now = 1;
1504
Takashi Iwai888afa12008-03-18 09:57:50 +01001505 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001506 p = get_hda_cvt_setup(codec, nid);
Takashi Iwaif0cea792010-08-13 11:56:53 +02001507 if (p) {
1508 /* here we just clear the active flag when do_now isn't set;
1509 * actual clean-ups will be done later in
1510 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1511 */
1512 if (do_now)
1513 really_cleanup_stream(codec, p);
1514 else
1515 p->active = 0;
1516 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001517}
Takashi Iwaif0cea792010-08-13 11:56:53 +02001518EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001519
Takashi Iwaieb541332010-08-06 13:48:11 +02001520static void really_cleanup_stream(struct hda_codec *codec,
1521 struct hda_cvt_setup *q)
1522{
1523 hda_nid_t nid = q->nid;
Takashi Iwai218264a2011-09-27 17:33:45 +02001524 if (q->stream_tag || q->channel_id)
1525 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1526 if (q->format_id)
1527 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1528);
Takashi Iwaieb541332010-08-06 13:48:11 +02001529 memset(q, 0, sizeof(*q));
1530 q->nid = nid;
1531}
1532
1533/* clean up the all conflicting obsolete streams */
1534static void purify_inactive_streams(struct hda_codec *codec)
1535{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001536 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001537 int i;
1538
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001539 list_for_each_entry(c, &codec->bus->codec_list, list) {
1540 for (i = 0; i < c->cvt_setups.used; i++) {
1541 struct hda_cvt_setup *p;
1542 p = snd_array_elem(&c->cvt_setups, i);
1543 if (p->dirty)
1544 really_cleanup_stream(c, p);
1545 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001546 }
1547}
1548
Takashi Iwai2a439522011-07-26 09:52:50 +02001549#ifdef CONFIG_PM
Takashi Iwaieb541332010-08-06 13:48:11 +02001550/* clean up all streams; called from suspend */
1551static void hda_cleanup_all_streams(struct hda_codec *codec)
1552{
1553 int i;
1554
1555 for (i = 0; i < codec->cvt_setups.used; i++) {
1556 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1557 if (p->stream_tag)
1558 really_cleanup_stream(codec, p);
1559 }
1560}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001561#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001562
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563/*
1564 * amp access functions
1565 */
1566
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001567/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001568#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001569#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001570#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1571#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001573#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
1575/* initialize the hash table */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001576static void /*__devinit*/ init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001577 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578{
Takashi Iwai01751f52007-08-10 16:59:39 +02001579 memset(cache, 0, sizeof(*cache));
1580 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001581 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001582}
1583
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001584static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001585{
Takashi Iwai603c4012008-07-30 15:01:44 +02001586 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587}
1588
1589/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001590static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591{
Takashi Iwai01751f52007-08-10 16:59:39 +02001592 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1593 u16 cur = cache->hash[idx];
1594 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
1596 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001597 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 if (info->key == key)
1599 return info;
1600 cur = info->next;
1601 }
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001602 return NULL;
1603}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001605/* query the hash. allocate an entry if not found. */
1606static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1607 u32 key)
1608{
1609 struct hda_cache_head *info = get_hash(cache, key);
1610 if (!info) {
1611 u16 idx, cur;
1612 /* add a new hash entry */
1613 info = snd_array_new(&cache->buf);
1614 if (!info)
1615 return NULL;
1616 cur = snd_array_index(&cache->buf, info);
1617 info->key = key;
1618 info->val = 0;
1619 idx = key % (u16)ARRAY_SIZE(cache->hash);
1620 info->next = cache->hash[idx];
1621 cache->hash[idx] = cur;
1622 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 return info;
1624}
1625
Takashi Iwai01751f52007-08-10 16:59:39 +02001626/* query and allocate an amp hash entry */
1627static inline struct hda_amp_info *
1628get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1629{
1630 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1631}
1632
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001633/* overwrite the value with the key in the caps hash */
1634static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val)
1635{
1636 struct hda_amp_info *info;
1637
1638 mutex_lock(&codec->hash_mutex);
1639 info = get_alloc_amp_hash(codec, key);
1640 if (!info) {
1641 mutex_unlock(&codec->hash_mutex);
1642 return -EINVAL;
1643 }
1644 info->amp_caps = val;
1645 info->head.val |= INFO_AMP_CAPS;
1646 mutex_unlock(&codec->hash_mutex);
1647 return 0;
1648}
1649
1650/* query the value from the caps hash; if not found, fetch the current
1651 * value from the given function and store in the hash
1652 */
1653static unsigned int
1654query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key,
1655 unsigned int (*func)(struct hda_codec *, hda_nid_t, int))
1656{
1657 struct hda_amp_info *info;
1658 unsigned int val;
1659
1660 mutex_lock(&codec->hash_mutex);
1661 info = get_alloc_amp_hash(codec, key);
1662 if (!info) {
1663 mutex_unlock(&codec->hash_mutex);
1664 return 0;
1665 }
1666 if (!(info->head.val & INFO_AMP_CAPS)) {
1667 mutex_unlock(&codec->hash_mutex); /* for reentrance */
1668 val = func(codec, nid, dir);
1669 write_caps_hash(codec, key, val);
1670 } else {
1671 val = info->amp_caps;
1672 mutex_unlock(&codec->hash_mutex);
1673 }
1674 return val;
1675}
1676
1677static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid,
1678 int direction)
1679{
1680 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1681 nid = codec->afg;
1682 return snd_hda_param_read(codec, nid,
1683 direction == HDA_OUTPUT ?
1684 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
1685}
1686
Takashi Iwaid5191e52009-11-16 14:58:17 +01001687/**
1688 * query_amp_caps - query AMP capabilities
1689 * @codec: the HD-auio codec
1690 * @nid: the NID to query
1691 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1692 *
1693 * Query AMP capabilities for the given widget and direction.
1694 * Returns the obtained capability bits.
1695 *
1696 * When cap bits have been already read, this doesn't read again but
1697 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001699u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001701 return query_caps_hash(codec, nid, direction,
1702 HDA_HASH_KEY(nid, direction, 0),
1703 read_amp_cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001705EXPORT_SYMBOL_HDA(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
Takashi Iwaid5191e52009-11-16 14:58:17 +01001707/**
1708 * snd_hda_override_amp_caps - Override the AMP capabilities
1709 * @codec: the CODEC to clean up
1710 * @nid: the NID to clean up
1711 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1712 * @caps: the capability bits to set
1713 *
1714 * Override the cached AMP caps bits value by the given one.
1715 * This function is useful if the driver needs to adjust the AMP ranges,
1716 * e.g. limit to 0dB, etc.
1717 *
1718 * Returns zero if successful or a negative error code.
1719 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001720int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1721 unsigned int caps)
1722{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001723 return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001724}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001725EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001726
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001727static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid,
1728 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001729{
1730 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1731}
1732
Takashi Iwaid5191e52009-11-16 14:58:17 +01001733/**
1734 * snd_hda_query_pin_caps - Query PIN capabilities
1735 * @codec: the HD-auio codec
1736 * @nid: the NID to query
1737 *
1738 * Query PIN capabilities for the given widget.
1739 * Returns the obtained capability bits.
1740 *
1741 * When cap bits have been already read, this doesn't read again but
1742 * returns the cached value.
1743 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001744u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1745{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001746 return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001747 read_pin_cap);
1748}
Takashi Iwai1327a322009-03-23 13:07:47 +01001749EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1750
Wu Fengguang864f92b2009-11-18 12:38:02 +08001751/**
Takashi Iwaif57c2562011-08-15 12:49:07 +02001752 * snd_hda_override_pin_caps - Override the pin capabilities
1753 * @codec: the CODEC
1754 * @nid: the NID to override
1755 * @caps: the capability bits to set
1756 *
1757 * Override the cached PIN capabilitiy bits value by the given one.
1758 *
1759 * Returns zero if successful or a negative error code.
1760 */
1761int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
1762 unsigned int caps)
1763{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001764 return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps);
Takashi Iwaif57c2562011-08-15 12:49:07 +02001765}
1766EXPORT_SYMBOL_HDA(snd_hda_override_pin_caps);
1767
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001768/* read or sync the hash value with the current value;
1769 * call within hash_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 */
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001771static struct hda_amp_info *
1772update_amp_hash(struct hda_codec *codec, hda_nid_t nid, int ch,
1773 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001775 struct hda_amp_info *info;
1776 unsigned int parm, val = 0;
1777 bool val_read = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001779 retry:
1780 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1781 if (!info)
1782 return NULL;
1783 if (!(info->head.val & INFO_AMP_VOL(ch))) {
1784 if (!val_read) {
1785 mutex_unlock(&codec->hash_mutex);
1786 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1787 parm |= direction == HDA_OUTPUT ?
1788 AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1789 parm |= index;
1790 val = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001791 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001792 val &= 0xff;
1793 val_read = true;
1794 mutex_lock(&codec->hash_mutex);
1795 goto retry;
1796 }
1797 info->vol[ch] = val;
1798 info->head.val |= INFO_AMP_VOL(ch);
1799 }
1800 return info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801}
1802
1803/*
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001804 * write the current volume in info to the h/w
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001806static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001807 hda_nid_t nid, int ch, int direction, int index,
1808 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809{
1810 u32 parm;
1811
1812 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1813 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1814 parm |= index << AC_AMP_SET_INDEX_SHIFT;
Takashi Iwai38681372012-02-27 15:00:58 +01001815 if ((val & HDA_AMP_MUTE) && !(info->amp_caps & AC_AMPCAP_MUTE) &&
1816 (info->amp_caps & AC_AMPCAP_MIN_MUTE))
1817 ; /* set the zero value as a fake mute */
1818 else
1819 parm |= val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
1821}
1822
Takashi Iwaid5191e52009-11-16 14:58:17 +01001823/**
1824 * snd_hda_codec_amp_read - Read AMP value
1825 * @codec: HD-audio codec
1826 * @nid: NID to read the AMP value
1827 * @ch: channel (left=0 or right=1)
1828 * @direction: #HDA_INPUT or #HDA_OUTPUT
1829 * @index: the index value (only for input direction)
1830 *
1831 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 */
Takashi Iwai834be882006-03-01 14:16:17 +01001833int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1834 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001836 struct hda_amp_info *info;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001837 unsigned int val = 0;
1838
1839 mutex_lock(&codec->hash_mutex);
1840 info = update_amp_hash(codec, nid, ch, direction, index);
1841 if (info)
1842 val = info->vol[ch];
1843 mutex_unlock(&codec->hash_mutex);
1844 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001846EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847
Takashi Iwaid5191e52009-11-16 14:58:17 +01001848/**
1849 * snd_hda_codec_amp_update - update the AMP value
1850 * @codec: HD-audio codec
1851 * @nid: NID to read the AMP value
1852 * @ch: channel (left=0 or right=1)
1853 * @direction: #HDA_INPUT or #HDA_OUTPUT
1854 * @idx: the index value (only for input direction)
1855 * @mask: bit mask to set
1856 * @val: the bits value to set
1857 *
1858 * Update the AMP value with a bit mask.
1859 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001860 */
Takashi Iwai834be882006-03-01 14:16:17 +01001861int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1862 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001864 struct hda_amp_info *info;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001865
Takashi Iwai467126462010-03-29 09:19:38 +02001866 if (snd_BUG_ON(mask & ~0xff))
1867 mask &= 0xff;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001868 val &= mask;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001869
1870 mutex_lock(&codec->hash_mutex);
1871 info = update_amp_hash(codec, nid, ch, direction, idx);
1872 if (!info) {
1873 mutex_unlock(&codec->hash_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 return 0;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001875 }
1876 val |= info->vol[ch] & ~mask;
1877 if (info->vol[ch] == val) {
1878 mutex_unlock(&codec->hash_mutex);
1879 return 0;
1880 }
1881 info->vol[ch] = val;
1882 mutex_unlock(&codec->hash_mutex);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001883 put_vol_mute(codec, info, nid, ch, direction, idx, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 return 1;
1885}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001886EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
Takashi Iwaid5191e52009-11-16 14:58:17 +01001888/**
1889 * snd_hda_codec_amp_stereo - update the AMP stereo values
1890 * @codec: HD-audio codec
1891 * @nid: NID to read the AMP value
1892 * @direction: #HDA_INPUT or #HDA_OUTPUT
1893 * @idx: the index value (only for input direction)
1894 * @mask: bit mask to set
1895 * @val: the bits value to set
1896 *
1897 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1898 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02001899 */
1900int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1901 int direction, int idx, int mask, int val)
1902{
1903 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001904
1905 if (snd_BUG_ON(mask & ~0xff))
1906 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02001907 for (ch = 0; ch < 2; ch++)
1908 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1909 idx, mask, val);
1910 return ret;
1911}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001912EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001913
Takashi Iwai2a439522011-07-26 09:52:50 +02001914#ifdef CONFIG_PM
Takashi Iwaid5191e52009-11-16 14:58:17 +01001915/**
1916 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
1917 * @codec: HD-audio codec
1918 *
1919 * Resume the all amp commands from the cache.
1920 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001921void snd_hda_codec_resume_amp(struct hda_codec *codec)
1922{
Takashi Iwai603c4012008-07-30 15:01:44 +02001923 struct hda_amp_info *buffer = codec->amp_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001924 int i;
1925
Takashi Iwai603c4012008-07-30 15:01:44 +02001926 for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001927 u32 key = buffer->head.key;
1928 hda_nid_t nid;
1929 unsigned int idx, dir, ch;
1930 if (!key)
1931 continue;
1932 nid = key & 0xff;
1933 idx = (key >> 16) & 0xff;
1934 dir = (key >> 24) & 0xff;
1935 for (ch = 0; ch < 2; ch++) {
1936 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
1937 continue;
1938 put_vol_mute(codec, buffer, nid, ch, dir, idx,
1939 buffer->vol[ch]);
1940 }
1941 }
1942}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001943EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
Takashi Iwai2a439522011-07-26 09:52:50 +02001944#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001946static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1947 unsigned int ofs)
1948{
1949 u32 caps = query_amp_caps(codec, nid, dir);
1950 /* get num steps */
1951 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1952 if (ofs < caps)
1953 caps -= ofs;
1954 return caps;
1955}
1956
Takashi Iwaid5191e52009-11-16 14:58:17 +01001957/**
1958 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
1959 *
1960 * The control element is supposed to have the private_value field
1961 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1962 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001963int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1964 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965{
1966 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1967 u16 nid = get_amp_nid(kcontrol);
1968 u8 chs = get_amp_channels(kcontrol);
1969 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001970 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001972 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1973 uinfo->count = chs == 3 ? 2 : 1;
1974 uinfo->value.integer.min = 0;
1975 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
1976 if (!uinfo->value.integer.max) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001977 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01001978 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
1979 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 return -EINVAL;
1981 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 return 0;
1983}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001984EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001986
1987static inline unsigned int
1988read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1989 int ch, int dir, int idx, unsigned int ofs)
1990{
1991 unsigned int val;
1992 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1993 val &= HDA_AMP_VOLMASK;
1994 if (val >= ofs)
1995 val -= ofs;
1996 else
1997 val = 0;
1998 return val;
1999}
2000
2001static inline int
2002update_amp_value(struct hda_codec *codec, hda_nid_t nid,
2003 int ch, int dir, int idx, unsigned int ofs,
2004 unsigned int val)
2005{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002006 unsigned int maxval;
2007
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002008 if (val > 0)
2009 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02002010 /* ofs = 0: raw max value */
2011 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002012 if (val > maxval)
2013 val = maxval;
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002014 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
2015 HDA_AMP_VOLMASK, val);
2016}
2017
Takashi Iwaid5191e52009-11-16 14:58:17 +01002018/**
2019 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
2020 *
2021 * The control element is supposed to have the private_value field
2022 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2023 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002024int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
2025 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026{
2027 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2028 hda_nid_t nid = get_amp_nid(kcontrol);
2029 int chs = get_amp_channels(kcontrol);
2030 int dir = get_amp_direction(kcontrol);
2031 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002032 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 long *valp = ucontrol->value.integer.value;
2034
2035 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002036 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002038 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 return 0;
2040}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002041EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042
Takashi Iwaid5191e52009-11-16 14:58:17 +01002043/**
2044 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
2045 *
2046 * The control element is supposed to have the private_value field
2047 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2048 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002049int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
2050 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051{
2052 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2053 hda_nid_t nid = get_amp_nid(kcontrol);
2054 int chs = get_amp_channels(kcontrol);
2055 int dir = get_amp_direction(kcontrol);
2056 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002057 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 long *valp = ucontrol->value.integer.value;
2059 int change = 0;
2060
Takashi Iwaicb53c622007-08-10 17:21:45 +02002061 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002062 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002063 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002064 valp++;
2065 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002066 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002067 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002068 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 return change;
2070}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002071EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
Takashi Iwaid5191e52009-11-16 14:58:17 +01002073/**
2074 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2075 *
2076 * The control element is supposed to have the private_value field
2077 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2078 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002079int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2080 unsigned int size, unsigned int __user *_tlv)
2081{
2082 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2083 hda_nid_t nid = get_amp_nid(kcontrol);
2084 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002085 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02002086 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002087 u32 caps, val1, val2;
2088
2089 if (size < 4 * sizeof(unsigned int))
2090 return -ENOMEM;
2091 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002092 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2093 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002094 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002095 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002096 val1 = ((int)val1) * ((int)val2);
Takashi Iwai38681372012-02-27 15:00:58 +01002097 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwaic08d9162010-10-17 10:40:53 +02002098 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002099 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2100 return -EFAULT;
2101 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2102 return -EFAULT;
2103 if (put_user(val1, _tlv + 2))
2104 return -EFAULT;
2105 if (put_user(val2, _tlv + 3))
2106 return -EFAULT;
2107 return 0;
2108}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002109EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002110
Takashi Iwaid5191e52009-11-16 14:58:17 +01002111/**
2112 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2113 * @codec: HD-audio codec
2114 * @nid: NID of a reference widget
2115 * @dir: #HDA_INPUT or #HDA_OUTPUT
2116 * @tlv: TLV data to be stored, at least 4 elements
2117 *
2118 * Set (static) TLV data for a virtual master volume using the AMP caps
2119 * obtained from the reference NID.
2120 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01002121 */
2122void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2123 unsigned int *tlv)
2124{
2125 u32 caps;
2126 int nums, step;
2127
2128 caps = query_amp_caps(codec, nid, dir);
2129 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2130 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2131 step = (step + 1) * 25;
2132 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2133 tlv[1] = 2 * sizeof(unsigned int);
2134 tlv[2] = -nums * step;
2135 tlv[3] = step;
2136}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002137EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002138
2139/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01002140static struct snd_kcontrol *
2141_snd_hda_find_mixer_ctl(struct hda_codec *codec,
2142 const char *name, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002143{
2144 struct snd_ctl_elem_id id;
2145 memset(&id, 0, sizeof(id));
2146 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwai09f99702008-02-04 12:31:13 +01002147 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02002148 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2149 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002150 strcpy(id.name, name);
2151 return snd_ctl_find_id(codec->bus->card, &id);
2152}
2153
Takashi Iwaid5191e52009-11-16 14:58:17 +01002154/**
2155 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2156 * @codec: HD-audio codec
2157 * @name: ctl id name string
2158 *
2159 * Get the control element with the given id string and IFACE_MIXER.
2160 */
Takashi Iwai09f99702008-02-04 12:31:13 +01002161struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2162 const char *name)
2163{
2164 return _snd_hda_find_mixer_ctl(codec, name, 0);
2165}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002166EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01002167
Takashi Iwai1afe2062010-12-23 10:17:52 +01002168static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name)
2169{
2170 int idx;
2171 for (idx = 0; idx < 16; idx++) { /* 16 ctlrs should be large enough */
2172 if (!_snd_hda_find_mixer_ctl(codec, name, idx))
2173 return idx;
2174 }
2175 return -EBUSY;
2176}
2177
Takashi Iwaid5191e52009-11-16 14:58:17 +01002178/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002179 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01002180 * @codec: HD-audio codec
2181 * @nid: corresponding NID (optional)
2182 * @kctl: the control element to assign
2183 *
2184 * Add the given control element to an array inside the codec instance.
2185 * All control elements belonging to a codec are supposed to be added
2186 * by this function so that a proper clean-up works at the free or
2187 * reconfiguration time.
2188 *
2189 * If non-zero @nid is passed, the NID is assigned to the control element.
2190 * The assignment is shown in the codec proc file.
2191 *
2192 * snd_hda_ctl_add() checks the control subdev id field whether
2193 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002194 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2195 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01002196 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002197int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2198 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002199{
2200 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002201 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002202 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002203
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002204 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002205 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002206 if (nid == 0)
2207 nid = get_amp_nid_(kctl->private_value);
2208 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002209 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2210 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002211 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01002212 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002213 err = snd_ctl_add(codec->bus->card, kctl);
2214 if (err < 0)
2215 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002216 item = snd_array_new(&codec->mixers);
2217 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002218 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002219 item->kctl = kctl;
2220 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002221 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002222 return 0;
2223}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002224EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002225
Takashi Iwaid5191e52009-11-16 14:58:17 +01002226/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002227 * snd_hda_add_nid - Assign a NID to a control element
2228 * @codec: HD-audio codec
2229 * @nid: corresponding NID (optional)
2230 * @kctl: the control element to assign
2231 * @index: index to kctl
2232 *
2233 * Add the given control element to an array inside the codec instance.
2234 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2235 * NID:KCTL mapping - for example "Capture Source" selector.
2236 */
2237int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2238 unsigned int index, hda_nid_t nid)
2239{
2240 struct hda_nid_item *item;
2241
2242 if (nid > 0) {
2243 item = snd_array_new(&codec->nids);
2244 if (!item)
2245 return -ENOMEM;
2246 item->kctl = kctl;
2247 item->index = index;
2248 item->nid = nid;
2249 return 0;
2250 }
Takashi Iwai28d1a852010-03-15 09:05:46 +01002251 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
2252 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002253 return -EINVAL;
2254}
2255EXPORT_SYMBOL_HDA(snd_hda_add_nid);
2256
2257/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002258 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2259 * @codec: HD-audio codec
2260 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002261void snd_hda_ctls_clear(struct hda_codec *codec)
2262{
2263 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002264 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002265 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002266 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002267 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002268 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002269}
2270
Takashi Iwaia65d6292009-02-23 16:57:04 +01002271/* pseudo device locking
2272 * toggle card->shutdown to allow/disallow the device access (as a hack)
2273 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002274int snd_hda_lock_devices(struct hda_bus *bus)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002275{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002276 struct snd_card *card = bus->card;
2277 struct hda_codec *codec;
2278
Takashi Iwaia65d6292009-02-23 16:57:04 +01002279 spin_lock(&card->files_lock);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002280 if (card->shutdown)
2281 goto err_unlock;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002282 card->shutdown = 1;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002283 if (!list_empty(&card->ctl_files))
2284 goto err_clear;
2285
2286 list_for_each_entry(codec, &bus->codec_list, list) {
2287 int pcm;
2288 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2289 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2290 if (!cpcm->pcm)
2291 continue;
2292 if (cpcm->pcm->streams[0].substream_opened ||
2293 cpcm->pcm->streams[1].substream_opened)
2294 goto err_clear;
2295 }
2296 }
Takashi Iwaia65d6292009-02-23 16:57:04 +01002297 spin_unlock(&card->files_lock);
2298 return 0;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002299
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002300 err_clear:
2301 card->shutdown = 0;
2302 err_unlock:
2303 spin_unlock(&card->files_lock);
2304 return -EINVAL;
2305}
2306EXPORT_SYMBOL_HDA(snd_hda_lock_devices);
2307
2308void snd_hda_unlock_devices(struct hda_bus *bus)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002309{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002310 struct snd_card *card = bus->card;
2311
2312 card = bus->card;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002313 spin_lock(&card->files_lock);
2314 card->shutdown = 0;
2315 spin_unlock(&card->files_lock);
2316}
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002317EXPORT_SYMBOL_HDA(snd_hda_unlock_devices);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002318
Takashi Iwaid5191e52009-11-16 14:58:17 +01002319/**
2320 * snd_hda_codec_reset - Clear all objects assigned to the codec
2321 * @codec: HD-audio codec
2322 *
2323 * This frees the all PCM and control elements assigned to the codec, and
2324 * clears the caches and restores the pin default configurations.
2325 *
2326 * When a device is being used, it returns -EBSY. If successfully freed,
2327 * returns zero.
2328 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002329int snd_hda_codec_reset(struct hda_codec *codec)
2330{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002331 struct hda_bus *bus = codec->bus;
2332 struct snd_card *card = bus->card;
2333 int i;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002334
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002335 if (snd_hda_lock_devices(bus) < 0)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002336 return -EBUSY;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002337
2338 /* OK, let it free */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002339
2340#ifdef CONFIG_SND_HDA_POWER_SAVE
Takashi Iwaia2d96e72012-05-09 12:36:22 +02002341 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwai339876d2012-05-08 16:57:12 +02002342 codec->power_on = 0;
2343 codec->power_transition = 0;
2344 codec->power_jiffies = jiffies;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002345 flush_workqueue(bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002346#endif
2347 snd_hda_ctls_clear(codec);
2348 /* relase PCMs */
2349 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002350 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01002351 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002352 clear_bit(codec->pcm_info[i].device,
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002353 bus->pcm_dev_bits);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002354 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002355 }
2356 if (codec->patch_ops.free)
2357 codec->patch_ops.free(codec);
Takashi Iwai1835a0f2011-10-27 22:12:46 +02002358 snd_hda_jack_tbl_clear(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01002359 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002360 codec->spec = NULL;
2361 free_hda_cache(&codec->amp_cache);
2362 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01002363 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2364 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01002365 /* free only driver_pins so that init_pins + user_pins are restored */
2366 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01002367 restore_pincfgs(codec);
Takashi Iwai09a60712012-06-26 15:01:33 +02002368 snd_array_free(&codec->cvt_setups);
2369 snd_array_free(&codec->spdif_out);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002370 codec->num_pcms = 0;
2371 codec->pcm_info = NULL;
2372 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01002373 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
2374 codec->slave_dig_outs = NULL;
2375 codec->spdif_status_reset = 0;
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002376 module_put(codec->owner);
2377 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002378
2379 /* allow device access again */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002380 snd_hda_unlock_devices(bus);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002381 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002382}
2383
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002384typedef int (*map_slave_func_t)(void *, struct snd_kcontrol *);
2385
2386/* apply the function to all matching slave ctls in the mixer list */
2387static int map_slaves(struct hda_codec *codec, const char * const *slaves,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002388 const char *suffix, map_slave_func_t func, void *data)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002389{
2390 struct hda_nid_item *items;
2391 const char * const *s;
2392 int i, err;
2393
2394 items = codec->mixers.list;
2395 for (i = 0; i < codec->mixers.used; i++) {
2396 struct snd_kcontrol *sctl = items[i].kctl;
2397 if (!sctl || !sctl->id.name ||
2398 sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
2399 continue;
2400 for (s = slaves; *s; s++) {
Takashi Iwai9322ca52012-02-03 14:28:01 +01002401 char tmpname[sizeof(sctl->id.name)];
2402 const char *name = *s;
2403 if (suffix) {
2404 snprintf(tmpname, sizeof(tmpname), "%s %s",
2405 name, suffix);
2406 name = tmpname;
2407 }
Takashi Iwai9322ca52012-02-03 14:28:01 +01002408 if (!strcmp(sctl->id.name, name)) {
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002409 err = func(data, sctl);
2410 if (err)
2411 return err;
2412 break;
2413 }
2414 }
2415 }
2416 return 0;
2417}
2418
2419static int check_slave_present(void *data, struct snd_kcontrol *sctl)
2420{
2421 return 1;
2422}
2423
Takashi Iwai18478e82012-03-09 17:51:10 +01002424/* guess the value corresponding to 0dB */
2425static int get_kctl_0dB_offset(struct snd_kcontrol *kctl)
2426{
2427 int _tlv[4];
2428 const int *tlv = NULL;
2429 int val = -1;
2430
2431 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2432 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2433 mm_segment_t fs = get_fs();
2434 set_fs(get_ds());
2435 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2436 tlv = _tlv;
2437 set_fs(fs);
2438 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2439 tlv = kctl->tlv.p;
2440 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE)
2441 val = -tlv[2] / tlv[3];
2442 return val;
2443}
2444
2445/* call kctl->put with the given value(s) */
2446static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2447{
2448 struct snd_ctl_elem_value *ucontrol;
2449 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2450 if (!ucontrol)
2451 return -ENOMEM;
2452 ucontrol->value.integer.value[0] = val;
2453 ucontrol->value.integer.value[1] = val;
2454 kctl->put(kctl, ucontrol);
2455 kfree(ucontrol);
2456 return 0;
2457}
2458
2459/* initialize the slave volume with 0dB */
2460static int init_slave_0dB(void *data, struct snd_kcontrol *slave)
2461{
2462 int offset = get_kctl_0dB_offset(slave);
2463 if (offset > 0)
2464 put_kctl_with_value(slave, offset);
2465 return 0;
2466}
2467
2468/* unmute the slave */
2469static int init_slave_unmute(void *data, struct snd_kcontrol *slave)
2470{
2471 return put_kctl_with_value(slave, 1);
2472}
2473
Takashi Iwaid5191e52009-11-16 14:58:17 +01002474/**
2475 * snd_hda_add_vmaster - create a virtual master control and add slaves
2476 * @codec: HD-audio codec
2477 * @name: vmaster control name
2478 * @tlv: TLV data (optional)
2479 * @slaves: slave control names (optional)
Takashi Iwai9322ca52012-02-03 14:28:01 +01002480 * @suffix: suffix string to each slave name (optional)
Takashi Iwai18478e82012-03-09 17:51:10 +01002481 * @init_slave_vol: initialize slaves to unmute/0dB
Takashi Iwai29e58532012-03-12 12:25:03 +01002482 * @ctl_ret: store the vmaster kcontrol in return
Takashi Iwaid5191e52009-11-16 14:58:17 +01002483 *
2484 * Create a virtual master control with the given name. The TLV data
2485 * must be either NULL or a valid data.
2486 *
2487 * @slaves is a NULL-terminated array of strings, each of which is a
2488 * slave control name. All controls with these names are assigned to
2489 * the new virtual master control.
2490 *
2491 * This function returns zero if successful or a negative error code.
2492 */
Takashi Iwai18478e82012-03-09 17:51:10 +01002493int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002494 unsigned int *tlv, const char * const *slaves,
Takashi Iwai29e58532012-03-12 12:25:03 +01002495 const char *suffix, bool init_slave_vol,
2496 struct snd_kcontrol **ctl_ret)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002497{
2498 struct snd_kcontrol *kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002499 int err;
2500
Takashi Iwai29e58532012-03-12 12:25:03 +01002501 if (ctl_ret)
2502 *ctl_ret = NULL;
2503
Takashi Iwai9322ca52012-02-03 14:28:01 +01002504 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002505 if (err != 1) {
Takashi Iwai2f085542008-02-22 18:43:50 +01002506 snd_printdd("No slave found for %s\n", name);
2507 return 0;
2508 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002509 kctl = snd_ctl_make_virtual_master(name, tlv);
2510 if (!kctl)
2511 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002512 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002513 if (err < 0)
2514 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002515
Takashi Iwai9322ca52012-02-03 14:28:01 +01002516 err = map_slaves(codec, slaves, suffix,
2517 (map_slave_func_t)snd_ctl_add_slave, kctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002518 if (err < 0)
2519 return err;
Takashi Iwai18478e82012-03-09 17:51:10 +01002520
2521 /* init with master mute & zero volume */
2522 put_kctl_with_value(kctl, 0);
2523 if (init_slave_vol)
2524 map_slaves(codec, slaves, suffix,
2525 tlv ? init_slave_0dB : init_slave_unmute, kctl);
2526
Takashi Iwai29e58532012-03-12 12:25:03 +01002527 if (ctl_ret)
2528 *ctl_ret = kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002529 return 0;
2530}
Takashi Iwai18478e82012-03-09 17:51:10 +01002531EXPORT_SYMBOL_HDA(__snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002532
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002533/*
2534 * mute-LED control using vmaster
2535 */
2536static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2537 struct snd_ctl_elem_info *uinfo)
2538{
2539 static const char * const texts[] = {
2540 "Off", "On", "Follow Master"
2541 };
2542 unsigned int index;
2543
2544 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2545 uinfo->count = 1;
2546 uinfo->value.enumerated.items = 3;
2547 index = uinfo->value.enumerated.item;
2548 if (index >= 3)
2549 index = 2;
2550 strcpy(uinfo->value.enumerated.name, texts[index]);
2551 return 0;
2552}
2553
2554static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2555 struct snd_ctl_elem_value *ucontrol)
2556{
2557 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2558 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2559 return 0;
2560}
2561
2562static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2563 struct snd_ctl_elem_value *ucontrol)
2564{
2565 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2566 unsigned int old_mode = hook->mute_mode;
2567
2568 hook->mute_mode = ucontrol->value.enumerated.item[0];
2569 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2570 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2571 if (old_mode == hook->mute_mode)
2572 return 0;
2573 snd_hda_sync_vmaster_hook(hook);
2574 return 1;
2575}
2576
2577static struct snd_kcontrol_new vmaster_mute_mode = {
2578 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2579 .name = "Mute-LED Mode",
2580 .info = vmaster_mute_mode_info,
2581 .get = vmaster_mute_mode_get,
2582 .put = vmaster_mute_mode_put,
2583};
2584
2585/*
2586 * Add a mute-LED hook with the given vmaster switch kctl
2587 * "Mute-LED Mode" control is automatically created and associated with
2588 * the given hook.
2589 */
2590int snd_hda_add_vmaster_hook(struct hda_codec *codec,
Takashi Iwaif29735c2012-03-13 07:55:10 +01002591 struct hda_vmaster_mute_hook *hook,
2592 bool expose_enum_ctl)
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002593{
2594 struct snd_kcontrol *kctl;
2595
2596 if (!hook->hook || !hook->sw_kctl)
2597 return 0;
2598 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
2599 hook->codec = codec;
2600 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
Takashi Iwaif29735c2012-03-13 07:55:10 +01002601 if (!expose_enum_ctl)
2602 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002603 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2604 if (!kctl)
2605 return -ENOMEM;
2606 return snd_hda_ctl_add(codec, 0, kctl);
2607}
2608EXPORT_SYMBOL_HDA(snd_hda_add_vmaster_hook);
2609
2610/*
2611 * Call the hook with the current value for synchronization
2612 * Should be called in init callback
2613 */
2614void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2615{
2616 if (!hook->hook || !hook->codec)
2617 return;
2618 switch (hook->mute_mode) {
2619 case HDA_VMUTE_FOLLOW_MASTER:
2620 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2621 break;
2622 default:
2623 hook->hook(hook->codec, hook->mute_mode);
2624 break;
2625 }
2626}
2627EXPORT_SYMBOL_HDA(snd_hda_sync_vmaster_hook);
2628
2629
Takashi Iwaid5191e52009-11-16 14:58:17 +01002630/**
2631 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2632 *
2633 * The control element is supposed to have the private_value field
2634 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2635 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002636int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2637 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638{
2639 int chs = get_amp_channels(kcontrol);
2640
2641 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2642 uinfo->count = chs == 3 ? 2 : 1;
2643 uinfo->value.integer.min = 0;
2644 uinfo->value.integer.max = 1;
2645 return 0;
2646}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002647EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648
Takashi Iwaid5191e52009-11-16 14:58:17 +01002649/**
2650 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2651 *
2652 * The control element is supposed to have the private_value field
2653 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2654 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002655int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2656 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657{
2658 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2659 hda_nid_t nid = get_amp_nid(kcontrol);
2660 int chs = get_amp_channels(kcontrol);
2661 int dir = get_amp_direction(kcontrol);
2662 int idx = get_amp_index(kcontrol);
2663 long *valp = ucontrol->value.integer.value;
2664
2665 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002666 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002667 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002669 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002670 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 return 0;
2672}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002673EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674
Takashi Iwaid5191e52009-11-16 14:58:17 +01002675/**
2676 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2677 *
2678 * The control element is supposed to have the private_value field
2679 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2680 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002681int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2682 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683{
2684 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2685 hda_nid_t nid = get_amp_nid(kcontrol);
2686 int chs = get_amp_channels(kcontrol);
2687 int dir = get_amp_direction(kcontrol);
2688 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 long *valp = ucontrol->value.integer.value;
2690 int change = 0;
2691
Takashi Iwaicb53c622007-08-10 17:21:45 +02002692 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002693 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002694 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002695 HDA_AMP_MUTE,
2696 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002697 valp++;
2698 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002699 if (chs & 2)
2700 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002701 HDA_AMP_MUTE,
2702 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002703 hda_call_check_power_status(codec, nid);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002704 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 return change;
2706}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002707EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708
2709/*
Takashi Iwai985be542005-11-02 18:26:49 +01002710 * bound volume controls
2711 *
2712 * bind multiple volumes (# indices, from 0)
2713 */
2714
2715#define AMP_VAL_IDX_SHIFT 19
2716#define AMP_VAL_IDX_MASK (0x0f<<19)
2717
Takashi Iwaid5191e52009-11-16 14:58:17 +01002718/**
2719 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2720 *
2721 * The control element is supposed to have the private_value field
2722 * set up via HDA_BIND_MUTE*() macros.
2723 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002724int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2725 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002726{
2727 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2728 unsigned long pval;
2729 int err;
2730
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002731 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002732 pval = kcontrol->private_value;
2733 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2734 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2735 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002736 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002737 return err;
2738}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002739EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002740
Takashi Iwaid5191e52009-11-16 14:58:17 +01002741/**
2742 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2743 *
2744 * The control element is supposed to have the private_value field
2745 * set up via HDA_BIND_MUTE*() macros.
2746 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002747int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2748 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002749{
2750 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2751 unsigned long pval;
2752 int i, indices, err = 0, change = 0;
2753
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002754 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002755 pval = kcontrol->private_value;
2756 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2757 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002758 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2759 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002760 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2761 if (err < 0)
2762 break;
2763 change |= err;
2764 }
2765 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002766 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002767 return err < 0 ? err : change;
2768}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002769EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002770
Takashi Iwaid5191e52009-11-16 14:58:17 +01002771/**
2772 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2773 *
2774 * The control element is supposed to have the private_value field
2775 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002776 */
2777int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2778 struct snd_ctl_elem_info *uinfo)
2779{
2780 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2781 struct hda_bind_ctls *c;
2782 int err;
2783
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002784 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002785 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002786 kcontrol->private_value = *c->values;
2787 err = c->ops->info(kcontrol, uinfo);
2788 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002789 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002790 return err;
2791}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002792EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002793
Takashi Iwaid5191e52009-11-16 14:58:17 +01002794/**
2795 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2796 *
2797 * The control element is supposed to have the private_value field
2798 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2799 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002800int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2801 struct snd_ctl_elem_value *ucontrol)
2802{
2803 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2804 struct hda_bind_ctls *c;
2805 int err;
2806
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002807 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002808 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002809 kcontrol->private_value = *c->values;
2810 err = c->ops->get(kcontrol, ucontrol);
2811 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002812 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002813 return err;
2814}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002815EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002816
Takashi Iwaid5191e52009-11-16 14:58:17 +01002817/**
2818 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2819 *
2820 * The control element is supposed to have the private_value field
2821 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2822 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002823int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2824 struct snd_ctl_elem_value *ucontrol)
2825{
2826 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2827 struct hda_bind_ctls *c;
2828 unsigned long *vals;
2829 int err = 0, change = 0;
2830
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002831 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002832 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002833 for (vals = c->values; *vals; vals++) {
2834 kcontrol->private_value = *vals;
2835 err = c->ops->put(kcontrol, ucontrol);
2836 if (err < 0)
2837 break;
2838 change |= err;
2839 }
2840 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002841 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002842 return err < 0 ? err : change;
2843}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002844EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02002845
Takashi Iwaid5191e52009-11-16 14:58:17 +01002846/**
2847 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2848 *
2849 * The control element is supposed to have the private_value field
2850 * set up via HDA_BIND_VOL() macro.
2851 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002852int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2853 unsigned int size, unsigned int __user *tlv)
2854{
2855 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2856 struct hda_bind_ctls *c;
2857 int err;
2858
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002859 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002860 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002861 kcontrol->private_value = *c->values;
2862 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2863 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002864 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002865 return err;
2866}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002867EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02002868
2869struct hda_ctl_ops snd_hda_bind_vol = {
2870 .info = snd_hda_mixer_amp_volume_info,
2871 .get = snd_hda_mixer_amp_volume_get,
2872 .put = snd_hda_mixer_amp_volume_put,
2873 .tlv = snd_hda_mixer_amp_tlv
2874};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002875EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02002876
2877struct hda_ctl_ops snd_hda_bind_sw = {
2878 .info = snd_hda_mixer_amp_switch_info,
2879 .get = snd_hda_mixer_amp_switch_get,
2880 .put = snd_hda_mixer_amp_switch_put,
2881 .tlv = snd_hda_mixer_amp_tlv
2882};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002883EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02002884
2885/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 * SPDIF out controls
2887 */
2888
Takashi Iwai0ba21762007-04-16 11:29:14 +02002889static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2890 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891{
2892 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2893 uinfo->count = 1;
2894 return 0;
2895}
2896
Takashi Iwai0ba21762007-04-16 11:29:14 +02002897static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2898 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899{
2900 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2901 IEC958_AES0_NONAUDIO |
2902 IEC958_AES0_CON_EMPHASIS_5015 |
2903 IEC958_AES0_CON_NOT_COPYRIGHT;
2904 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2905 IEC958_AES1_CON_ORIGINAL;
2906 return 0;
2907}
2908
Takashi Iwai0ba21762007-04-16 11:29:14 +02002909static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2910 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911{
2912 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2913 IEC958_AES0_NONAUDIO |
2914 IEC958_AES0_PRO_EMPHASIS_5015;
2915 return 0;
2916}
2917
Takashi Iwai0ba21762007-04-16 11:29:14 +02002918static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2919 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920{
2921 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002922 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002923 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002925 mutex_lock(&codec->spdif_mutex);
2926 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c9359762011-06-01 11:14:17 -06002927 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
2928 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
2929 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
2930 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002931 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932
2933 return 0;
2934}
2935
2936/* convert from SPDIF status bits to HDA SPDIF bits
2937 * bit 0 (DigEn) is always set zero (to be filled later)
2938 */
2939static unsigned short convert_from_spdif_status(unsigned int sbits)
2940{
2941 unsigned short val = 0;
2942
2943 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002944 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002946 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002948 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2949 IEC958_AES0_PRO_EMPHASIS_5015)
2950 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002952 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2953 IEC958_AES0_CON_EMPHASIS_5015)
2954 val |= AC_DIG1_EMPHASIS;
2955 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2956 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02002958 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2960 }
2961 return val;
2962}
2963
2964/* convert to SPDIF status bits from HDA SPDIF bits
2965 */
2966static unsigned int convert_to_spdif_status(unsigned short val)
2967{
2968 unsigned int sbits = 0;
2969
Takashi Iwai0ba21762007-04-16 11:29:14 +02002970 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002972 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 sbits |= IEC958_AES0_PROFESSIONAL;
2974 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002975 if (sbits & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2977 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002978 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002980 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002982 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2984 sbits |= val & (0x7f << 8);
2985 }
2986 return sbits;
2987}
2988
Takashi Iwai2f728532008-09-25 16:32:41 +02002989/* set digital convert verbs both for the given NID and its slaves */
2990static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
2991 int verb, int val)
2992{
Takashi Iwaidda14412011-05-02 11:29:30 +02002993 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02002994
Takashi Iwai9e976972008-11-25 08:17:20 +01002995 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002996 d = codec->slave_dig_outs;
2997 if (!d)
2998 return;
2999 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01003000 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003001}
3002
3003static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
3004 int dig1, int dig2)
3005{
3006 if (dig1 != -1)
3007 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
3008 if (dig2 != -1)
3009 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
3010}
3011
Takashi Iwai0ba21762007-04-16 11:29:14 +02003012static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
3013 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014{
3015 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06003016 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003017 struct hda_spdif_out *spdif;
3018 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 unsigned short val;
3020 int change;
3021
Ingo Molnar62932df2006-01-16 16:34:20 +01003022 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003023 spdif = snd_array_elem(&codec->spdif_out, idx);
3024 nid = spdif->nid;
Stephen Warren7c9359762011-06-01 11:14:17 -06003025 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
3027 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
3028 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c9359762011-06-01 11:14:17 -06003029 val = convert_from_spdif_status(spdif->status);
3030 val |= spdif->ctls & 1;
3031 change = spdif->ctls != val;
3032 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003033 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02003034 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01003035 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 return change;
3037}
3038
Takashi Iwaia5ce8892007-07-23 15:42:26 +02003039#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040
Takashi Iwai0ba21762007-04-16 11:29:14 +02003041static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
3042 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043{
3044 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06003045 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003046 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003048 mutex_lock(&codec->spdif_mutex);
3049 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c9359762011-06-01 11:14:17 -06003050 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003051 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 return 0;
3053}
3054
Stephen Warren74b654c2011-06-01 11:14:18 -06003055static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
3056 int dig1, int dig2)
3057{
3058 set_dig_out_convert(codec, nid, dig1, dig2);
3059 /* unmute amp switch (if any) */
3060 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
3061 (dig1 & AC_DIG1_ENABLE))
3062 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
3063 HDA_AMP_MUTE, 0);
3064}
3065
Takashi Iwai0ba21762007-04-16 11:29:14 +02003066static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
3067 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068{
3069 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06003070 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003071 struct hda_spdif_out *spdif;
3072 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 unsigned short val;
3074 int change;
3075
Ingo Molnar62932df2006-01-16 16:34:20 +01003076 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003077 spdif = snd_array_elem(&codec->spdif_out, idx);
3078 nid = spdif->nid;
Stephen Warren7c9359762011-06-01 11:14:17 -06003079 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02003081 val |= AC_DIG1_ENABLE;
Stephen Warren7c9359762011-06-01 11:14:17 -06003082 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003083 spdif->ctls = val;
3084 if (change && nid != (u16)-1)
3085 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01003086 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 return change;
3088}
3089
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003090static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 {
3092 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3093 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003094 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 .info = snd_hda_spdif_mask_info,
3096 .get = snd_hda_spdif_cmask_get,
3097 },
3098 {
3099 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3100 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003101 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 .info = snd_hda_spdif_mask_info,
3103 .get = snd_hda_spdif_pmask_get,
3104 },
3105 {
3106 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003107 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 .info = snd_hda_spdif_mask_info,
3109 .get = snd_hda_spdif_default_get,
3110 .put = snd_hda_spdif_default_put,
3111 },
3112 {
3113 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003114 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 .info = snd_hda_spdif_out_switch_info,
3116 .get = snd_hda_spdif_out_switch_get,
3117 .put = snd_hda_spdif_out_switch_put,
3118 },
3119 { } /* end */
3120};
3121
3122/**
3123 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
3124 * @codec: the HDA codec
3125 * @nid: audio out widget NID
3126 *
3127 * Creates controls related with the SPDIF output.
3128 * Called from each patch supporting the SPDIF out.
3129 *
3130 * Returns 0 if successful, or a negative error code.
3131 */
Stephen Warren74b654c2011-06-01 11:14:18 -06003132int snd_hda_create_spdif_out_ctls(struct hda_codec *codec,
3133 hda_nid_t associated_nid,
3134 hda_nid_t cvt_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135{
3136 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003137 struct snd_kcontrol *kctl;
3138 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003139 int idx;
Stephen Warren7c9359762011-06-01 11:14:17 -06003140 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141
Takashi Iwai1afe2062010-12-23 10:17:52 +01003142 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch");
3143 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003144 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
3145 return -EBUSY;
3146 }
Stephen Warren7c9359762011-06-01 11:14:17 -06003147 spdif = snd_array_new(&codec->spdif_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3149 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01003150 if (!kctl)
3151 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01003152 kctl->id.index = idx;
Stephen Warren7c9359762011-06-01 11:14:17 -06003153 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06003154 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003155 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 return err;
3157 }
Stephen Warren74b654c2011-06-01 11:14:18 -06003158 spdif->nid = cvt_nid;
3159 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
Stephen Warren7c9359762011-06-01 11:14:17 -06003160 AC_VERB_GET_DIGI_CONVERT_1, 0);
3161 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 return 0;
3163}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003164EXPORT_SYMBOL_HDA(snd_hda_create_spdif_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003166/* get the hda_spdif_out entry from the given NID
3167 * call within spdif_mutex lock
3168 */
Stephen Warren7c9359762011-06-01 11:14:17 -06003169struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3170 hda_nid_t nid)
3171{
3172 int i;
3173 for (i = 0; i < codec->spdif_out.used; i++) {
3174 struct hda_spdif_out *spdif =
3175 snd_array_elem(&codec->spdif_out, i);
3176 if (spdif->nid == nid)
3177 return spdif;
3178 }
3179 return NULL;
3180}
3181EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid);
3182
Stephen Warren74b654c2011-06-01 11:14:18 -06003183void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3184{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003185 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003186
3187 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003188 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003189 spdif->nid = (u16)-1;
3190 mutex_unlock(&codec->spdif_mutex);
3191}
3192EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign);
3193
3194void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3195{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003196 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003197 unsigned short val;
3198
3199 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003200 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003201 if (spdif->nid != nid) {
3202 spdif->nid = nid;
3203 val = spdif->ctls;
3204 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3205 }
3206 mutex_unlock(&codec->spdif_mutex);
3207}
3208EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign);
3209
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210/*
Takashi Iwai9a081602008-02-12 18:37:26 +01003211 * SPDIF sharing with analog output
3212 */
3213static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3214 struct snd_ctl_elem_value *ucontrol)
3215{
3216 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3217 ucontrol->value.integer.value[0] = mout->share_spdif;
3218 return 0;
3219}
3220
3221static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3222 struct snd_ctl_elem_value *ucontrol)
3223{
3224 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3225 mout->share_spdif = !!ucontrol->value.integer.value[0];
3226 return 0;
3227}
3228
3229static struct snd_kcontrol_new spdif_share_sw = {
3230 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3231 .name = "IEC958 Default PCM Playback Switch",
3232 .info = snd_ctl_boolean_mono_info,
3233 .get = spdif_share_sw_get,
3234 .put = spdif_share_sw_put,
3235};
3236
Takashi Iwaid5191e52009-11-16 14:58:17 +01003237/**
3238 * snd_hda_create_spdif_share_sw - create Default PCM switch
3239 * @codec: the HDA codec
3240 * @mout: multi-out instance
3241 */
Takashi Iwai9a081602008-02-12 18:37:26 +01003242int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3243 struct hda_multi_out *mout)
3244{
3245 if (!mout->dig_out_nid)
3246 return 0;
3247 /* ATTENTION: here mout is passed as private_data, instead of codec */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003248 return snd_hda_ctl_add(codec, mout->dig_out_nid,
3249 snd_ctl_new1(&spdif_share_sw, mout));
Takashi Iwai9a081602008-02-12 18:37:26 +01003250}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003251EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01003252
3253/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 * SPDIF input
3255 */
3256
3257#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3258
Takashi Iwai0ba21762007-04-16 11:29:14 +02003259static int snd_hda_spdif_in_switch_get(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
3264 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3265 return 0;
3266}
3267
Takashi Iwai0ba21762007-04-16 11:29:14 +02003268static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3269 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270{
3271 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3272 hda_nid_t nid = kcontrol->private_value;
3273 unsigned int val = !!ucontrol->value.integer.value[0];
3274 int change;
3275
Ingo Molnar62932df2006-01-16 16:34:20 +01003276 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003278 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003280 snd_hda_codec_write_cache(codec, nid, 0,
3281 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003283 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 return change;
3285}
3286
Takashi Iwai0ba21762007-04-16 11:29:14 +02003287static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3288 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289{
3290 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3291 hda_nid_t nid = kcontrol->private_value;
3292 unsigned short val;
3293 unsigned int sbits;
3294
Andrew Paprocki3982d172007-12-19 12:13:44 +01003295 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 sbits = convert_to_spdif_status(val);
3297 ucontrol->value.iec958.status[0] = sbits;
3298 ucontrol->value.iec958.status[1] = sbits >> 8;
3299 ucontrol->value.iec958.status[2] = sbits >> 16;
3300 ucontrol->value.iec958.status[3] = sbits >> 24;
3301 return 0;
3302}
3303
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003304static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 {
3306 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003307 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 .info = snd_hda_spdif_in_switch_info,
3309 .get = snd_hda_spdif_in_switch_get,
3310 .put = snd_hda_spdif_in_switch_put,
3311 },
3312 {
3313 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3314 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003315 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 .info = snd_hda_spdif_mask_info,
3317 .get = snd_hda_spdif_in_status_get,
3318 },
3319 { } /* end */
3320};
3321
3322/**
3323 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3324 * @codec: the HDA codec
3325 * @nid: audio in widget NID
3326 *
3327 * Creates controls related with the SPDIF input.
3328 * Called from each patch supporting the SPDIF in.
3329 *
3330 * Returns 0 if successful, or a negative error code.
3331 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003332int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333{
3334 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003335 struct snd_kcontrol *kctl;
3336 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003337 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338
Takashi Iwai1afe2062010-12-23 10:17:52 +01003339 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch");
3340 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003341 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
3342 return -EBUSY;
3343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3345 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01003346 if (!kctl)
3347 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003349 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003350 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 return err;
3352 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003353 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01003354 snd_hda_codec_read(codec, nid, 0,
3355 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02003356 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 return 0;
3358}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003359EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360
Takashi Iwai2a439522011-07-26 09:52:50 +02003361#ifdef CONFIG_PM
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003362/*
3363 * command cache
3364 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003366/* build a 32bit cache key with the widget id and the command parameter */
3367#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3368#define get_cmd_cache_nid(key) ((key) & 0xff)
3369#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3370
3371/**
3372 * snd_hda_codec_write_cache - send a single command with caching
3373 * @codec: the HDA codec
3374 * @nid: NID to send the command
3375 * @direct: direct flag
3376 * @verb: the verb to send
3377 * @parm: the parameter for the verb
3378 *
3379 * Send a single command without waiting for response.
3380 *
3381 * Returns 0 if successful, or a negative error code.
3382 */
3383int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
3384 int direct, unsigned int verb, unsigned int parm)
3385{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003386 int err = snd_hda_codec_write(codec, nid, direct, verb, parm);
3387 struct hda_cache_head *c;
3388 u32 key;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003389
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003390 if (err < 0)
3391 return err;
3392 /* parm may contain the verb stuff for get/set amp */
3393 verb = verb | (parm >> 8);
3394 parm &= 0xff;
3395 key = build_cmd_cache_key(nid, verb);
3396 mutex_lock(&codec->bus->cmd_mutex);
3397 c = get_alloc_hash(&codec->cmd_cache, key);
3398 if (c)
3399 c->val = parm;
3400 mutex_unlock(&codec->bus->cmd_mutex);
3401 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003402}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003403EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003404
Takashi Iwaid5191e52009-11-16 14:58:17 +01003405/**
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003406 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3407 * @codec: the HDA codec
3408 * @nid: NID to send the command
3409 * @direct: direct flag
3410 * @verb: the verb to send
3411 * @parm: the parameter for the verb
3412 *
3413 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3414 * command if the parameter is already identical with the cached value.
3415 * If not, it sends the command and refreshes the cache.
3416 *
3417 * Returns 0 if successful, or a negative error code.
3418 */
3419int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
3420 int direct, unsigned int verb, unsigned int parm)
3421{
3422 struct hda_cache_head *c;
3423 u32 key;
3424
3425 /* parm may contain the verb stuff for get/set amp */
3426 verb = verb | (parm >> 8);
3427 parm &= 0xff;
3428 key = build_cmd_cache_key(nid, verb);
3429 mutex_lock(&codec->bus->cmd_mutex);
3430 c = get_hash(&codec->cmd_cache, key);
3431 if (c && c->val == parm) {
3432 mutex_unlock(&codec->bus->cmd_mutex);
3433 return 0;
3434 }
3435 mutex_unlock(&codec->bus->cmd_mutex);
3436 return snd_hda_codec_write_cache(codec, nid, direct, verb, parm);
3437}
3438EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
3439
3440/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003441 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3442 * @codec: HD-audio codec
3443 *
3444 * Execute all verbs recorded in the command caches to resume.
3445 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003446void snd_hda_codec_resume_cache(struct hda_codec *codec)
3447{
Takashi Iwai603c4012008-07-30 15:01:44 +02003448 struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003449 int i;
3450
Takashi Iwai603c4012008-07-30 15:01:44 +02003451 for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003452 u32 key = buffer->key;
3453 if (!key)
3454 continue;
3455 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3456 get_cmd_cache_cmd(key), buffer->val);
3457 }
3458}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003459EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003460
3461/**
3462 * snd_hda_sequence_write_cache - sequence writes with caching
3463 * @codec: the HDA codec
3464 * @seq: VERB array to send
3465 *
3466 * Send the commands sequentially from the given array.
3467 * Thte commands are recorded on cache for power-save and resume.
3468 * The array must be terminated with NID=0.
3469 */
3470void snd_hda_sequence_write_cache(struct hda_codec *codec,
3471 const struct hda_verb *seq)
3472{
3473 for (; seq->nid; seq++)
3474 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3475 seq->param);
3476}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003477EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
Takashi Iwai2a439522011-07-26 09:52:50 +02003478#endif /* CONFIG_PM */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003479
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003480void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
3481 unsigned int power_state,
3482 bool eapd_workaround)
Takashi Iwai54d17402005-11-21 16:33:22 +01003483{
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003484 hda_nid_t nid = codec->start_nid;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003485 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01003486
Takashi Iwaicb53c622007-08-10 17:21:45 +02003487 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003488 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003489 if (!(wcaps & AC_WCAP_POWER))
3490 continue;
3491 /* don't power down the widget if it controls eapd and
3492 * EAPD_BTLENABLE is set.
3493 */
3494 if (eapd_workaround && power_state == AC_PWRST_D3 &&
3495 get_wcaps_type(wcaps) == AC_WID_PIN &&
3496 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3497 int eapd = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003498 AC_VERB_GET_EAPD_BTLENABLE, 0);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003499 if (eapd & 0x02)
3500 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003501 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003502 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
3503 power_state);
Takashi Iwai54d17402005-11-21 16:33:22 +01003504 }
3505
Takashi Iwaicb53c622007-08-10 17:21:45 +02003506 if (power_state == AC_PWRST_D0) {
3507 unsigned long end_time;
3508 int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003509 /* wait until the codec reachs to D0 */
3510 end_time = jiffies + msecs_to_jiffies(500);
3511 do {
3512 state = snd_hda_codec_read(codec, fg, 0,
3513 AC_VERB_GET_POWER_STATE, 0);
3514 if (state == power_state)
3515 break;
3516 msleep(1);
3517 } while (time_after_eq(end_time, jiffies));
3518 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003519}
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003520EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all);
3521
3522/*
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003523 * supported power states check
3524 */
3525static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg,
3526 unsigned int power_state)
3527{
3528 int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE);
3529
Mengdong Line037cb42012-08-10 14:11:58 +02003530 if (sup == -1)
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003531 return false;
3532 if (sup & power_state)
3533 return true;
3534 else
3535 return false;
3536}
3537
3538/*
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003539 * set power state of the codec
3540 */
3541static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
3542 unsigned int power_state)
3543{
Wang Xingchao09617ce2012-06-08 10:26:08 +08003544 int count;
3545 unsigned int state;
3546
Mengdong Linb8dfc4622012-08-23 17:32:30 +08003547 codec->d3_stop_clk_ok = 0;
3548
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003549 if (codec->patch_ops.set_power_state) {
3550 codec->patch_ops.set_power_state(codec, fg, power_state);
3551 return;
3552 }
3553
3554 /* this delay seems necessary to avoid click noise at power-down */
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003555 if (power_state == AC_PWRST_D3) {
3556 /* transition time less than 10ms for power down */
3557 bool epss = snd_hda_codec_get_supported_ps(codec, fg, AC_PWRST_EPSS);
3558 msleep(epss ? 10 : 100);
3559 }
Wang Xingchao09617ce2012-06-08 10:26:08 +08003560
3561 /* repeat power states setting at most 10 times*/
3562 for (count = 0; count < 10; count++) {
3563 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE,
3564 power_state);
3565 snd_hda_codec_set_power_to_all(codec, fg, power_state, true);
3566 state = snd_hda_codec_read(codec, fg, 0,
3567 AC_VERB_GET_POWER_STATE, 0);
3568 if (!(state & AC_PWRST_ERROR))
3569 break;
3570 }
Mengdong Linb8dfc4622012-08-23 17:32:30 +08003571
3572 if ((power_state == AC_PWRST_D3)
3573 && codec->d3_stop_clk && (state & AC_PWRST_CLK_STOP_OK))
3574 codec->d3_stop_clk_ok = 1;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003575}
Takashi Iwai54d17402005-11-21 16:33:22 +01003576
Takashi Iwai11aeff02008-07-30 15:01:46 +02003577#ifdef CONFIG_SND_HDA_HWDEP
3578/* execute additional init verbs */
3579static void hda_exec_init_verbs(struct hda_codec *codec)
3580{
3581 if (codec->init_verbs.list)
3582 snd_hda_sequence_write(codec, codec->init_verbs.list);
3583}
3584#else
3585static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3586#endif
3587
Takashi Iwai2a439522011-07-26 09:52:50 +02003588#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003589/*
3590 * call suspend and power-down; used both from PM and power-save
3591 */
3592static void hda_call_codec_suspend(struct hda_codec *codec)
3593{
3594 if (codec->patch_ops.suspend)
Takashi Iwai68cb2b52012-07-02 15:20:37 +02003595 codec->patch_ops.suspend(codec);
Takashi Iwaieb541332010-08-06 13:48:11 +02003596 hda_cleanup_all_streams(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003597 hda_set_power_state(codec,
3598 codec->afg ? codec->afg : codec->mfg,
3599 AC_PWRST_D3);
3600#ifdef CONFIG_SND_HDA_POWER_SAVE
3601 cancel_delayed_work(&codec->power_work);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02003602 spin_lock(&codec->power_lock);
3603 snd_hda_update_power_acct(codec);
3604 trace_hda_power_down(codec);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02003605 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02003606 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003607 codec->power_jiffies = jiffies;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02003608 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003609#endif
3610}
3611
3612/*
3613 * kick up codec; used both from PM and power-save
3614 */
3615static void hda_call_codec_resume(struct hda_codec *codec)
3616{
Takashi Iwai7f308302012-05-08 16:52:23 +02003617 /* set as if powered on for avoiding re-entering the resume
3618 * in the resume / power-save sequence
3619 */
3620 hda_keep_power_on(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003621 hda_set_power_state(codec,
3622 codec->afg ? codec->afg : codec->mfg,
3623 AC_PWRST_D0);
Takashi Iwai3be14142009-02-20 14:11:16 +01003624 restore_pincfgs(codec); /* restore all current pin configs */
Takashi Iwaiac0547d2010-07-05 16:50:13 +02003625 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02003626 hda_exec_init_verbs(codec);
Takashi Iwai1835a0f2011-10-27 22:12:46 +02003627 snd_hda_jack_set_dirty_all(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003628 if (codec->patch_ops.resume)
3629 codec->patch_ops.resume(codec);
3630 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02003631 if (codec->patch_ops.init)
3632 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003633 snd_hda_codec_resume_amp(codec);
3634 snd_hda_codec_resume_cache(codec);
3635 }
Takashi Iwai8a535412012-08-22 16:40:24 +02003636 snd_hda_jack_report_sync(codec);
Takashi Iwai7f308302012-05-08 16:52:23 +02003637 snd_hda_power_down(codec); /* flag down before returning */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003638}
Takashi Iwai2a439522011-07-26 09:52:50 +02003639#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003640
Takashi Iwai54d17402005-11-21 16:33:22 +01003641
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642/**
3643 * snd_hda_build_controls - build mixer controls
3644 * @bus: the BUS
3645 *
3646 * Creates mixer controls for each codec included in the bus.
3647 *
3648 * Returns 0 if successful, otherwise a negative error code.
3649 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003650int /*__devinit*/ snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003652 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653
Takashi Iwai0ba21762007-04-16 11:29:14 +02003654 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003655 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003656 if (err < 0) {
Takashi Iwai28d1a852010-03-15 09:05:46 +01003657 printk(KERN_ERR "hda_codec: cannot build controls "
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003658 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003659 err = snd_hda_codec_reset(codec);
3660 if (err < 0) {
3661 printk(KERN_ERR
3662 "hda_codec: cannot revert codec\n");
3663 return err;
3664 }
3665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003667 return 0;
3668}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003669EXPORT_SYMBOL_HDA(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003671int snd_hda_codec_build_controls(struct hda_codec *codec)
3672{
3673 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02003674 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003675 /* continue to initialize... */
3676 if (codec->patch_ops.init)
3677 err = codec->patch_ops.init(codec);
3678 if (!err && codec->patch_ops.build_controls)
3679 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003680 if (err < 0)
3681 return err;
Takashi Iwai8a535412012-08-22 16:40:24 +02003682 snd_hda_jack_report_sync(codec); /* call at the last init point */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683 return 0;
3684}
3685
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686/*
3687 * stream formats
3688 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02003689struct hda_rate_tbl {
3690 unsigned int hz;
3691 unsigned int alsa_bits;
3692 unsigned int hda_fmt;
3693};
3694
Takashi Iwai92f10b32010-08-03 14:21:00 +02003695/* rate = base * mult / div */
3696#define HDA_RATE(base, mult, div) \
3697 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
3698 (((div) - 1) << AC_FMT_DIV_SHIFT))
3699
Takashi Iwaibefdf312005-08-22 13:57:55 +02003700static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003702
3703 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003704 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
3705 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
3706 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
3707 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
3708 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
3709 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
3710 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
3711 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
3712 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
3713 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
3714 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003715#define AC_PAR_PCM_RATE_BITS 11
3716 /* up to bits 10, 384kHZ isn't supported properly */
3717
3718 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003719 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003720
Takashi Iwaibefdf312005-08-22 13:57:55 +02003721 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722};
3723
3724/**
3725 * snd_hda_calc_stream_format - calculate format bitset
3726 * @rate: the sample rate
3727 * @channels: the number of channels
3728 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3729 * @maxbps: the max. bps
3730 *
3731 * Calculate the format bitset from the given rate, channels and th PCM format.
3732 *
3733 * Return zero if invalid.
3734 */
3735unsigned int snd_hda_calc_stream_format(unsigned int rate,
3736 unsigned int channels,
3737 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03003738 unsigned int maxbps,
3739 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740{
3741 int i;
3742 unsigned int val = 0;
3743
Takashi Iwaibefdf312005-08-22 13:57:55 +02003744 for (i = 0; rate_bits[i].hz; i++)
3745 if (rate_bits[i].hz == rate) {
3746 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747 break;
3748 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003749 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750 snd_printdd("invalid rate %d\n", rate);
3751 return 0;
3752 }
3753
3754 if (channels == 0 || channels > 8) {
3755 snd_printdd("invalid channels %d\n", channels);
3756 return 0;
3757 }
3758 val |= channels - 1;
3759
3760 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003761 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003762 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003763 break;
3764 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003765 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003766 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767 case 20:
3768 case 24:
3769 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003770 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003771 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003773 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02003775 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776 break;
3777 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003778 snd_printdd("invalid format width %d\n",
3779 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780 return 0;
3781 }
3782
Anssi Hannula32c168c2010-08-03 13:28:57 +03003783 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003784 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03003785
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 return val;
3787}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003788EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003790static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid,
3791 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003792{
3793 unsigned int val = 0;
3794 if (nid != codec->afg &&
3795 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
3796 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
3797 if (!val || val == -1)
3798 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
3799 if (!val || val == -1)
3800 return 0;
3801 return val;
3802}
3803
3804static unsigned int query_pcm_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_PARPCM_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003807 get_pcm_param);
3808}
3809
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003810static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid,
3811 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003812{
3813 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
3814 if (!streams || streams == -1)
3815 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
3816 if (!streams || streams == -1)
3817 return 0;
3818 return streams;
3819}
3820
3821static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
3822{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003823 return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003824 get_stream_param);
3825}
3826
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827/**
3828 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
3829 * @codec: the HDA codec
3830 * @nid: NID to query
3831 * @ratesp: the pointer to store the detected rate bitflags
3832 * @formatsp: the pointer to store the detected formats
3833 * @bpsp: the pointer to store the detected format widths
3834 *
3835 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
3836 * or @bsps argument is ignored.
3837 *
3838 * Returns 0 if successful, otherwise a negative error code.
3839 */
Stephen Warren384a48d2011-06-01 11:14:21 -06003840int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
3842{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003843 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003845 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003846 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847
3848 if (ratesp) {
3849 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003850 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02003852 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003854 if (rates == 0) {
3855 snd_printk(KERN_ERR "hda_codec: rates == 0 "
3856 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
3857 nid, val,
3858 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
3859 return -EIO;
3860 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861 *ratesp = rates;
3862 }
3863
3864 if (formatsp || bpsp) {
3865 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003866 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003868 streams = query_stream_param(codec, nid);
3869 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871
3872 bps = 0;
3873 if (streams & AC_SUPFMT_PCM) {
3874 if (val & AC_SUPPCM_BITS_8) {
3875 formats |= SNDRV_PCM_FMTBIT_U8;
3876 bps = 8;
3877 }
3878 if (val & AC_SUPPCM_BITS_16) {
3879 formats |= SNDRV_PCM_FMTBIT_S16_LE;
3880 bps = 16;
3881 }
3882 if (wcaps & AC_WCAP_DIGITAL) {
3883 if (val & AC_SUPPCM_BITS_32)
3884 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
3885 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
3886 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3887 if (val & AC_SUPPCM_BITS_24)
3888 bps = 24;
3889 else if (val & AC_SUPPCM_BITS_20)
3890 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003891 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
3892 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3894 if (val & AC_SUPPCM_BITS_32)
3895 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896 else if (val & AC_SUPPCM_BITS_24)
3897 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02003898 else if (val & AC_SUPPCM_BITS_20)
3899 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900 }
3901 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02003902#if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
Takashi Iwaib5025c52009-07-01 18:05:27 +02003903 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003905 if (!bps)
3906 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02003907 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02003908#endif
Takashi Iwaib5025c52009-07-01 18:05:27 +02003909 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003910 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911 /* temporary hack: we have still no proper support
3912 * for the direct AC3 stream...
3913 */
3914 formats |= SNDRV_PCM_FMTBIT_U8;
3915 bps = 8;
3916 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003917 if (formats == 0) {
3918 snd_printk(KERN_ERR "hda_codec: formats == 0 "
3919 "(nid=0x%x, val=0x%x, ovrd=%i, "
3920 "streams=0x%x)\n",
3921 nid, val,
3922 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
3923 streams);
3924 return -EIO;
3925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926 if (formatsp)
3927 *formatsp = formats;
3928 if (bpsp)
3929 *bpsp = bps;
3930 }
3931
3932 return 0;
3933}
Stephen Warren384a48d2011-06-01 11:14:21 -06003934EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935
3936/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003937 * snd_hda_is_supported_format - Check the validity of the format
3938 * @codec: HD-audio codec
3939 * @nid: NID to check
3940 * @format: the HD-audio format value to check
3941 *
3942 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943 *
3944 * Returns 1 if supported, 0 if not.
3945 */
3946int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
3947 unsigned int format)
3948{
3949 int i;
3950 unsigned int val = 0, rate, stream;
3951
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003952 val = query_pcm_param(codec, nid);
3953 if (!val)
3954 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955
3956 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003957 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02003958 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 if (val & (1 << i))
3960 break;
3961 return 0;
3962 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003963 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964 return 0;
3965
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003966 stream = query_stream_param(codec, nid);
3967 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968 return 0;
3969
3970 if (stream & AC_SUPFMT_PCM) {
3971 switch (format & 0xf0) {
3972 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003973 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974 return 0;
3975 break;
3976 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003977 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978 return 0;
3979 break;
3980 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003981 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982 return 0;
3983 break;
3984 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003985 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986 return 0;
3987 break;
3988 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003989 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990 return 0;
3991 break;
3992 default:
3993 return 0;
3994 }
3995 } else {
3996 /* FIXME: check for float32 and AC3? */
3997 }
3998
3999 return 1;
4000}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004001EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002
4003/*
4004 * PCM stuff
4005 */
4006static int hda_pcm_default_open_close(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{
4010 return 0;
4011}
4012
4013static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
4014 struct hda_codec *codec,
4015 unsigned int stream_tag,
4016 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004017 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018{
4019 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4020 return 0;
4021}
4022
4023static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
4024 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004025 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026{
Takashi Iwai888afa12008-03-18 09:57:50 +01004027 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028 return 0;
4029}
4030
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004031static int set_pcm_default_values(struct hda_codec *codec,
4032 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004034 int err;
4035
Takashi Iwai0ba21762007-04-16 11:29:14 +02004036 /* query support PCM information from the given NID */
4037 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004038 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02004039 info->rates ? NULL : &info->rates,
4040 info->formats ? NULL : &info->formats,
4041 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004042 if (err < 0)
4043 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044 }
4045 if (info->ops.open == NULL)
4046 info->ops.open = hda_pcm_default_open_close;
4047 if (info->ops.close == NULL)
4048 info->ops.close = hda_pcm_default_open_close;
4049 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004050 if (snd_BUG_ON(!info->nid))
4051 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052 info->ops.prepare = hda_pcm_default_prepare;
4053 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004055 if (snd_BUG_ON(!info->nid))
4056 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057 info->ops.cleanup = hda_pcm_default_cleanup;
4058 }
4059 return 0;
4060}
4061
Takashi Iwaieb541332010-08-06 13:48:11 +02004062/*
4063 * codec prepare/cleanup entries
4064 */
4065int snd_hda_codec_prepare(struct hda_codec *codec,
4066 struct hda_pcm_stream *hinfo,
4067 unsigned int stream,
4068 unsigned int format,
4069 struct snd_pcm_substream *substream)
4070{
4071 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004072 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004073 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
4074 if (ret >= 0)
4075 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004076 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004077 return ret;
4078}
4079EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
4080
4081void snd_hda_codec_cleanup(struct hda_codec *codec,
4082 struct hda_pcm_stream *hinfo,
4083 struct snd_pcm_substream *substream)
4084{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004085 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004086 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004087 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004088}
4089EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
4090
Takashi Iwaid5191e52009-11-16 14:58:17 +01004091/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01004092const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
4093 "Audio", "SPDIF", "HDMI", "Modem"
4094};
4095
Takashi Iwai176d5332008-07-30 15:01:44 +02004096/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004097 * get the empty PCM device number to assign
Takashi Iwaic8936222010-01-28 17:08:53 +01004098 *
4099 * note the max device number is limited by HDA_MAX_PCMS, currently 10
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004100 */
4101static int get_empty_pcm_device(struct hda_bus *bus, int type)
4102{
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004103 /* audio device indices; not linear to keep compatibility */
4104 static int audio_idx[HDA_PCM_NTYPES][5] = {
4105 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
4106 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01004107 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004108 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004109 };
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004110 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004111
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004112 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004113 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
4114 return -EINVAL;
4115 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004116
4117 for (i = 0; audio_idx[type][i] >= 0 ; i++)
4118 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
4119 return audio_idx[type][i];
4120
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004121 /* non-fixed slots starting from 10 */
4122 for (i = 10; i < 32; i++) {
4123 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4124 return i;
4125 }
4126
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004127 snd_printk(KERN_WARNING "Too many %s devices\n",
4128 snd_hda_pcm_type_name[type]);
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004129 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004130}
4131
4132/*
Takashi Iwai176d5332008-07-30 15:01:44 +02004133 * attach a new PCM stream
4134 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004135static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02004136{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004137 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02004138 struct hda_pcm_stream *info;
4139 int stream, err;
4140
Takashi Iwaib91f0802008-11-04 08:43:08 +01004141 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02004142 return -EINVAL;
4143 for (stream = 0; stream < 2; stream++) {
4144 info = &pcm->stream[stream];
4145 if (info->substreams) {
4146 err = set_pcm_default_values(codec, info);
4147 if (err < 0)
4148 return err;
4149 }
4150 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004151 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02004152}
4153
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004154/* assign all PCMs of the given codec */
4155int snd_hda_codec_build_pcms(struct hda_codec *codec)
4156{
4157 unsigned int pcm;
4158 int err;
4159
4160 if (!codec->num_pcms) {
4161 if (!codec->patch_ops.build_pcms)
4162 return 0;
4163 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004164 if (err < 0) {
4165 printk(KERN_ERR "hda_codec: cannot build PCMs"
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004166 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004167 err = snd_hda_codec_reset(codec);
4168 if (err < 0) {
4169 printk(KERN_ERR
4170 "hda_codec: cannot revert codec\n");
4171 return err;
4172 }
4173 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004174 }
4175 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
4176 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
4177 int dev;
4178
4179 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01004180 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004181
4182 if (!cpcm->pcm) {
4183 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
4184 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004185 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004186 cpcm->device = dev;
4187 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004188 if (err < 0) {
4189 printk(KERN_ERR "hda_codec: cannot attach "
4190 "PCM stream %d for codec #%d\n",
4191 dev, codec->addr);
4192 continue; /* no fatal error */
4193 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004194 }
4195 }
4196 return 0;
4197}
4198
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199/**
4200 * snd_hda_build_pcms - build PCM information
4201 * @bus: the BUS
4202 *
4203 * Create PCM information for each codec included in the bus.
4204 *
4205 * The build_pcms codec patch is requested to set up codec->num_pcms and
4206 * codec->pcm_info properly. The array is referred by the top-level driver
4207 * to create its PCM instances.
4208 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4209 * callback.
4210 *
4211 * At least, substreams, channels_min and channels_max must be filled for
4212 * each stream. substreams = 0 indicates that the stream doesn't exist.
4213 * When rates and/or formats are zero, the supported values are queried
4214 * from the given nid. The nid is used also by the default ops.prepare
4215 * and ops.cleanup callbacks.
4216 *
4217 * The driver needs to call ops.open in its open callback. Similarly,
4218 * ops.close is supposed to be called in the close callback.
4219 * ops.prepare should be called in the prepare or hw_params callback
4220 * with the proper parameters for set up.
4221 * ops.cleanup should be called in hw_free for clean up of streams.
4222 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004223 * This function returns 0 if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224 */
Takashi Iwai5cb543d2012-08-09 13:49:23 +02004225int snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004227 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228
Takashi Iwai0ba21762007-04-16 11:29:14 +02004229 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004230 int err = snd_hda_codec_build_pcms(codec);
4231 if (err < 0)
4232 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233 }
4234 return 0;
4235}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004236EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238/**
4239 * snd_hda_check_board_config - compare the current codec with the config table
4240 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004241 * @num_configs: number of config enums
4242 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243 * @tbl: configuration table, terminated by null entries
4244 *
4245 * Compares the modelname or PCI subsystem id of the current codec with the
4246 * given configuration table. If a matching entry is found, returns its
4247 * config value (supposed to be 0 or positive).
4248 *
4249 * If no entries are matching, the function returns a negative value.
4250 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02004251int snd_hda_check_board_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004252 int num_configs, const char * const *models,
Takashi Iwai12f288b2007-08-02 15:51:59 +02004253 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254{
Takashi Iwaif44ac832008-07-30 15:01:45 +02004255 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004256 int i;
4257 for (i = 0; i < num_configs; i++) {
4258 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02004259 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004260 snd_printd(KERN_INFO "hda_codec: model '%s' is "
4261 "selected\n", models[i]);
4262 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263 }
4264 }
4265 }
4266
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004267 if (!codec->bus->pci || !tbl)
4268 return -1;
4269
4270 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
4271 if (!tbl)
4272 return -1;
4273 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02004274#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004275 char tmp[10];
4276 const char *model = NULL;
4277 if (models)
4278 model = models[tbl->value];
4279 if (!model) {
4280 sprintf(tmp, "#%d", tbl->value);
4281 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004283 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4284 "for config %x:%x (%s)\n",
4285 model, tbl->subvendor, tbl->subdevice,
4286 (tbl->name ? tbl->name : "Unknown device"));
4287#endif
4288 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289 }
4290 return -1;
4291}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004292EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293
4294/**
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004295 * snd_hda_check_board_codec_sid_config - compare the current codec
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004296 subsystem ID with the
4297 config table
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004298
4299 This is important for Gateway notebooks with SB450 HDA Audio
4300 where the vendor ID of the PCI device is:
4301 ATI Technologies Inc SB450 HDA Audio [1002:437b]
4302 and the vendor/subvendor are found only at the codec.
4303
4304 * @codec: the HDA codec
4305 * @num_configs: number of config enums
4306 * @models: array of model name strings
4307 * @tbl: configuration table, terminated by null entries
4308 *
4309 * Compares the modelname or PCI subsystem id of the current codec with the
4310 * given configuration table. If a matching entry is found, returns its
4311 * config value (supposed to be 0 or positive).
4312 *
4313 * If no entries are matching, the function returns a negative value.
4314 */
4315int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004316 int num_configs, const char * const *models,
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004317 const struct snd_pci_quirk *tbl)
4318{
4319 const struct snd_pci_quirk *q;
4320
4321 /* Search for codec ID */
4322 for (q = tbl; q->subvendor; q++) {
Takashi Iwaie2301a42011-11-22 19:58:56 +01004323 unsigned int mask = 0xffff0000 | q->subdevice_mask;
4324 unsigned int id = (q->subdevice | (q->subvendor << 16)) & mask;
4325 if ((codec->subsystem_id & mask) == id)
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004326 break;
4327 }
4328
4329 if (!q->subvendor)
4330 return -1;
4331
4332 tbl = q;
4333
4334 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwaid94ff6b2009-09-02 00:20:21 +02004335#ifdef CONFIG_SND_DEBUG_VERBOSE
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004336 char tmp[10];
4337 const char *model = NULL;
4338 if (models)
4339 model = models[tbl->value];
4340 if (!model) {
4341 sprintf(tmp, "#%d", tbl->value);
4342 model = tmp;
4343 }
4344 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4345 "for config %x:%x (%s)\n",
4346 model, tbl->subvendor, tbl->subdevice,
4347 (tbl->name ? tbl->name : "Unknown device"));
4348#endif
4349 return tbl->value;
4350 }
4351 return -1;
4352}
4353EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
4354
4355/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356 * snd_hda_add_new_ctls - create controls from the array
4357 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004358 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359 *
4360 * This helper function creates and add new controls in the given array.
4361 * The array must be terminated with an empty entry as terminator.
4362 *
4363 * Returns 0 if successful, or a negative error code.
4364 */
Takashi Iwai031024e2011-05-02 11:29:30 +02004365int snd_hda_add_new_ctls(struct hda_codec *codec,
4366 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01004368 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369
4370 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004371 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004372 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01004373 if (knew->iface == -1) /* skip this codec private value */
4374 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004375 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004376 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02004377 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01004378 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004379 if (addr > 0)
4380 kctl->id.device = addr;
4381 if (idx > 0)
4382 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01004383 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004384 if (!err)
4385 break;
4386 /* try first with another device index corresponding to
4387 * the codec addr; if it still fails (or it's the
4388 * primary codec), then try another control index
4389 */
4390 if (!addr && codec->addr)
4391 addr = codec->addr;
4392 else if (!idx && !knew->index) {
4393 idx = find_empty_mixer_ctl_idx(codec,
4394 knew->name);
4395 if (idx <= 0)
4396 return err;
4397 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01004398 return err;
4399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400 }
4401 return 0;
4402}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004403EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404
Takashi Iwaicb53c622007-08-10 17:21:45 +02004405#ifdef CONFIG_SND_HDA_POWER_SAVE
Takashi Iwaicb53c622007-08-10 17:21:45 +02004406static void hda_power_work(struct work_struct *work)
4407{
4408 struct hda_codec *codec =
4409 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004410 struct hda_bus *bus = codec->bus;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004411
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004412 spin_lock(&codec->power_lock);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004413 if (codec->power_transition > 0) { /* during power-up sequence? */
4414 spin_unlock(&codec->power_lock);
4415 return;
4416 }
Maxim Levitsky2e492462007-09-03 15:26:57 +02004417 if (!codec->power_on || codec->power_count) {
4418 codec->power_transition = 0;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004419 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004420 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02004421 }
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004422 spin_unlock(&codec->power_lock);
4423
Takashi Iwaicb53c622007-08-10 17:21:45 +02004424 hda_call_codec_suspend(codec);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004425 if (bus->ops.pm_notify)
Mengdong Linb8dfc4622012-08-23 17:32:30 +08004426 bus->ops.pm_notify(bus, codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004427}
4428
4429static void hda_keep_power_on(struct hda_codec *codec)
4430{
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004431 spin_lock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004432 codec->power_count++;
4433 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004434 codec->power_jiffies = jiffies;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004435 spin_unlock(&codec->power_lock);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004436}
4437
Takashi Iwaid5191e52009-11-16 14:58:17 +01004438/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01004439void snd_hda_update_power_acct(struct hda_codec *codec)
4440{
4441 unsigned long delta = jiffies - codec->power_jiffies;
4442 if (codec->power_on)
4443 codec->power_on_acct += delta;
4444 else
4445 codec->power_off_acct += delta;
4446 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004447}
4448
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004449/* Transition to powered up, if wait_power_down then wait for a pending
4450 * transition to D3 to complete. A pending D3 transition is indicated
4451 * with power_transition == -1. */
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004452/* call this with codec->power_lock held! */
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004453static void __snd_hda_power_up(struct hda_codec *codec, bool wait_power_down)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004454{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004455 struct hda_bus *bus = codec->bus;
4456
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004457 /* Return if power_on or transitioning to power_on, unless currently
4458 * powering down. */
4459 if ((codec->power_on || codec->power_transition > 0) &&
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004460 !(wait_power_down && codec->power_transition < 0))
Takashi Iwaicb53c622007-08-10 17:21:45 +02004461 return;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004462 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004463
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004464 cancel_delayed_work_sync(&codec->power_work);
4465
4466 spin_lock(&codec->power_lock);
Dylan Reidb43d2242012-06-21 21:51:22 -07004467 /* If the power down delayed work was cancelled above before starting,
4468 * then there is no need to go through power up here.
4469 */
4470 if (codec->power_on) {
Takashi Iwai535b6c52012-08-20 21:25:22 +02004471 if (codec->power_transition < 0)
4472 codec->power_transition = 0;
Dylan Reidb43d2242012-06-21 21:51:22 -07004473 return;
4474 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004475
Takashi Iwaid66fee52011-08-02 15:39:31 +02004476 trace_hda_power_up(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004477 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004478 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004479 codec->power_jiffies = jiffies;
Takashi Iwai7f308302012-05-08 16:52:23 +02004480 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004481 spin_unlock(&codec->power_lock);
4482
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004483 if (bus->ops.pm_notify)
Mengdong Linb8dfc4622012-08-23 17:32:30 +08004484 bus->ops.pm_notify(bus, codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004485 hda_call_codec_resume(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004486
4487 spin_lock(&codec->power_lock);
Takashi Iwaia221e282007-08-16 16:35:33 +02004488 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004489}
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004490
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004491#define power_save(codec) \
4492 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004493
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004494/* Transition to powered down */
4495static void __snd_hda_power_down(struct hda_codec *codec)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004496{
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004497 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004498 return;
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004499
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004500 if (power_save(codec)) {
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004501 codec->power_transition = -1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01004502 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004503 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02004504 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004505}
4506
4507/**
4508 * snd_hda_power_save - Power-up/down/sync the codec
4509 * @codec: HD-audio codec
4510 * @delta: the counter delta to change
4511 *
4512 * Change the power-up counter via @delta, and power up or down the hardware
4513 * appropriately. For the power-down, queue to the delayed action.
4514 * Passing zero to @delta means to synchronize the power state.
4515 */
4516void snd_hda_power_save(struct hda_codec *codec, int delta, bool d3wait)
4517{
4518 spin_lock(&codec->power_lock);
4519 codec->power_count += delta;
4520 trace_hda_power_count(codec);
4521 if (delta > 0)
4522 __snd_hda_power_up(codec, d3wait);
4523 else
4524 __snd_hda_power_down(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004525 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004526}
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004527EXPORT_SYMBOL_HDA(snd_hda_power_save);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004528
Takashi Iwaid5191e52009-11-16 14:58:17 +01004529/**
4530 * snd_hda_check_amp_list_power - Check the amp list and update the power
4531 * @codec: HD-audio codec
4532 * @check: the object containing an AMP list and the status
4533 * @nid: NID to check / update
4534 *
4535 * Check whether the given NID is in the amp list. If it's in the list,
4536 * check the current AMP status, and update the the power-status according
4537 * to the mute status.
4538 *
4539 * This function is supposed to be set or called from the check_power_status
4540 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004541 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004542int snd_hda_check_amp_list_power(struct hda_codec *codec,
4543 struct hda_loopback_check *check,
4544 hda_nid_t nid)
4545{
Takashi Iwai031024e2011-05-02 11:29:30 +02004546 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004547 int ch, v;
4548
4549 if (!check->amplist)
4550 return 0;
4551 for (p = check->amplist; p->nid; p++) {
4552 if (p->nid == nid)
4553 break;
4554 }
4555 if (!p->nid)
4556 return 0; /* nothing changed */
4557
4558 for (p = check->amplist; p->nid; p++) {
4559 for (ch = 0; ch < 2; ch++) {
4560 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4561 p->idx);
4562 if (!(v & HDA_AMP_MUTE) && v > 0) {
4563 if (!check->power_on) {
4564 check->power_on = 1;
4565 snd_hda_power_up(codec);
4566 }
4567 return 1;
4568 }
4569 }
4570 }
4571 if (check->power_on) {
4572 check->power_on = 0;
4573 snd_hda_power_down(codec);
4574 }
4575 return 0;
4576}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004577EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004578#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004580/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004581 * Channel mode helper
4582 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004583
4584/**
4585 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
4586 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004587int snd_hda_ch_mode_info(struct hda_codec *codec,
4588 struct snd_ctl_elem_info *uinfo,
4589 const struct hda_channel_mode *chmode,
4590 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004591{
4592 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4593 uinfo->count = 1;
4594 uinfo->value.enumerated.items = num_chmodes;
4595 if (uinfo->value.enumerated.item >= num_chmodes)
4596 uinfo->value.enumerated.item = num_chmodes - 1;
4597 sprintf(uinfo->value.enumerated.name, "%dch",
4598 chmode[uinfo->value.enumerated.item].channels);
4599 return 0;
4600}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004601EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004602
Takashi Iwaid5191e52009-11-16 14:58:17 +01004603/**
4604 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
4605 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004606int snd_hda_ch_mode_get(struct hda_codec *codec,
4607 struct snd_ctl_elem_value *ucontrol,
4608 const struct hda_channel_mode *chmode,
4609 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004610 int max_channels)
4611{
4612 int i;
4613
4614 for (i = 0; i < num_chmodes; i++) {
4615 if (max_channels == chmode[i].channels) {
4616 ucontrol->value.enumerated.item[0] = i;
4617 break;
4618 }
4619 }
4620 return 0;
4621}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004622EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004623
Takashi Iwaid5191e52009-11-16 14:58:17 +01004624/**
4625 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
4626 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004627int snd_hda_ch_mode_put(struct hda_codec *codec,
4628 struct snd_ctl_elem_value *ucontrol,
4629 const struct hda_channel_mode *chmode,
4630 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004631 int *max_channelsp)
4632{
4633 unsigned int mode;
4634
4635 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01004636 if (mode >= num_chmodes)
4637 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004638 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004639 return 0;
4640 /* change the current channel setting */
4641 *max_channelsp = chmode[mode].channels;
4642 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004643 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004644 return 1;
4645}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004646EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004647
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648/*
4649 * input MUX helper
4650 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004651
4652/**
4653 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
4654 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004655int snd_hda_input_mux_info(const struct hda_input_mux *imux,
4656 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657{
4658 unsigned int index;
4659
4660 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4661 uinfo->count = 1;
4662 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004663 if (!imux->num_items)
4664 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665 index = uinfo->value.enumerated.item;
4666 if (index >= imux->num_items)
4667 index = imux->num_items - 1;
4668 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
4669 return 0;
4670}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004671EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672
Takashi Iwaid5191e52009-11-16 14:58:17 +01004673/**
4674 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
4675 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004676int snd_hda_input_mux_put(struct hda_codec *codec,
4677 const struct hda_input_mux *imux,
4678 struct snd_ctl_elem_value *ucontrol,
4679 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680 unsigned int *cur_val)
4681{
4682 unsigned int idx;
4683
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004684 if (!imux->num_items)
4685 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686 idx = ucontrol->value.enumerated.item[0];
4687 if (idx >= imux->num_items)
4688 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004689 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004691 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
4692 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693 *cur_val = idx;
4694 return 1;
4695}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004696EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697
4698
4699/*
4700 * Multi-channel / digital-out PCM helper functions
4701 */
4702
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004703/* setup SPDIF output stream */
4704static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
4705 unsigned int stream_tag, unsigned int format)
4706{
Stephen Warren7c9359762011-06-01 11:14:17 -06004707 struct hda_spdif_out *spdif = snd_hda_spdif_out_of_nid(codec, nid);
4708
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004709 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Stephen Warren7c9359762011-06-01 11:14:17 -06004710 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004711 set_dig_out_convert(codec, nid,
Stephen Warren7c9359762011-06-01 11:14:17 -06004712 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02004713 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004714 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02004715 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004716 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004717 for (d = codec->slave_dig_outs; *d; d++)
4718 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
4719 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004720 }
Takashi Iwai2f728532008-09-25 16:32:41 +02004721 /* turn on again (if needed) */
Stephen Warren7c9359762011-06-01 11:14:17 -06004722 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai2f728532008-09-25 16:32:41 +02004723 set_dig_out_convert(codec, nid,
Stephen Warren7c9359762011-06-01 11:14:17 -06004724 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02004725}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004726
Takashi Iwai2f728532008-09-25 16:32:41 +02004727static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
4728{
4729 snd_hda_codec_cleanup_stream(codec, nid);
4730 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004731 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004732 for (d = codec->slave_dig_outs; *d; d++)
4733 snd_hda_codec_cleanup_stream(codec, *d);
4734 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004735}
4736
Takashi Iwaid5191e52009-11-16 14:58:17 +01004737/**
4738 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
4739 * @bus: HD-audio bus
4740 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004741void snd_hda_bus_reboot_notify(struct hda_bus *bus)
4742{
4743 struct hda_codec *codec;
4744
4745 if (!bus)
4746 return;
4747 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02004748 if (hda_codec_is_power_on(codec) &&
4749 codec->patch_ops.reboot_notify)
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004750 codec->patch_ops.reboot_notify(codec);
4751 }
4752}
Takashi Iwai8f217a22009-11-10 18:26:12 +01004753EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004754
Takashi Iwaid5191e52009-11-16 14:58:17 +01004755/**
4756 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004758int snd_hda_multi_out_dig_open(struct hda_codec *codec,
4759 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760{
Ingo Molnar62932df2006-01-16 16:34:20 +01004761 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02004762 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
4763 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02004764 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01004766 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767 return 0;
4768}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004769EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770
Takashi Iwaid5191e52009-11-16 14:58:17 +01004771/**
4772 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
4773 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004774int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
4775 struct hda_multi_out *mout,
4776 unsigned int stream_tag,
4777 unsigned int format,
4778 struct snd_pcm_substream *substream)
4779{
4780 mutex_lock(&codec->spdif_mutex);
4781 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
4782 mutex_unlock(&codec->spdif_mutex);
4783 return 0;
4784}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004785EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004786
Takashi Iwaid5191e52009-11-16 14:58:17 +01004787/**
4788 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
4789 */
Takashi Iwai9411e212009-02-13 11:32:28 +01004790int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
4791 struct hda_multi_out *mout)
4792{
4793 mutex_lock(&codec->spdif_mutex);
4794 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4795 mutex_unlock(&codec->spdif_mutex);
4796 return 0;
4797}
4798EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
4799
Takashi Iwaid5191e52009-11-16 14:58:17 +01004800/**
4801 * snd_hda_multi_out_dig_close - release the digital out stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004803int snd_hda_multi_out_dig_close(struct hda_codec *codec,
4804 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805{
Ingo Molnar62932df2006-01-16 16:34:20 +01004806 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01004808 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809 return 0;
4810}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004811EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812
Takashi Iwaid5191e52009-11-16 14:58:17 +01004813/**
4814 * snd_hda_multi_out_analog_open - open analog outputs
4815 *
4816 * Open analog outputs and set up the hw-constraints.
4817 * If the digital outputs can be opened as slave, open the digital
4818 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004820int snd_hda_multi_out_analog_open(struct hda_codec *codec,
4821 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01004822 struct snd_pcm_substream *substream,
4823 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824{
Takashi Iwai9a081602008-02-12 18:37:26 +01004825 struct snd_pcm_runtime *runtime = substream->runtime;
4826 runtime->hw.channels_max = mout->max_channels;
4827 if (mout->dig_out_nid) {
4828 if (!mout->analog_rates) {
4829 mout->analog_rates = hinfo->rates;
4830 mout->analog_formats = hinfo->formats;
4831 mout->analog_maxbps = hinfo->maxbps;
4832 } else {
4833 runtime->hw.rates = mout->analog_rates;
4834 runtime->hw.formats = mout->analog_formats;
4835 hinfo->maxbps = mout->analog_maxbps;
4836 }
4837 if (!mout->spdif_rates) {
4838 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
4839 &mout->spdif_rates,
4840 &mout->spdif_formats,
4841 &mout->spdif_maxbps);
4842 }
4843 mutex_lock(&codec->spdif_mutex);
4844 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02004845 if ((runtime->hw.rates & mout->spdif_rates) &&
4846 (runtime->hw.formats & mout->spdif_formats)) {
4847 runtime->hw.rates &= mout->spdif_rates;
4848 runtime->hw.formats &= mout->spdif_formats;
4849 if (mout->spdif_maxbps < hinfo->maxbps)
4850 hinfo->maxbps = mout->spdif_maxbps;
4851 } else {
4852 mout->share_spdif = 0;
4853 /* FIXME: need notify? */
4854 }
Takashi Iwai9a081602008-02-12 18:37:26 +01004855 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02004856 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004857 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858 return snd_pcm_hw_constraint_step(substream->runtime, 0,
4859 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
4860}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004861EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862
Takashi Iwaid5191e52009-11-16 14:58:17 +01004863/**
4864 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
4865 *
4866 * Set up the i/o for analog out.
4867 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004869int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
4870 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871 unsigned int stream_tag,
4872 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004873 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874{
Takashi Iwaidda14412011-05-02 11:29:30 +02004875 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876 int chs = substream->runtime->channels;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02004877 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878 int i;
4879
Ingo Molnar62932df2006-01-16 16:34:20 +01004880 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02004881 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Takashi Iwai9a081602008-02-12 18:37:26 +01004882 if (mout->dig_out_nid && mout->share_spdif &&
4883 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02004885 snd_hda_is_supported_format(codec, mout->dig_out_nid,
4886 format) &&
Stephen Warren7c9359762011-06-01 11:14:17 -06004887 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004889 setup_dig_out_stream(codec, mout->dig_out_nid,
4890 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891 } else {
4892 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02004893 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894 }
4895 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004896 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897
4898 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004899 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
4900 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004901 if (!mout->no_share_stream &&
4902 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004904 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
4905 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004906 /* extra outputs copied from front */
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02004907 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
4908 if (!mout->no_share_stream && mout->hp_out_nid[i])
4909 snd_hda_codec_setup_stream(codec,
4910 mout->hp_out_nid[i],
4911 stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004912 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02004913 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01004914 snd_hda_codec_setup_stream(codec,
4915 mout->extra_out_nid[i],
4916 stream_tag, 0, format);
4917
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918 /* surrounds */
4919 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02004920 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004921 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4922 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004923 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004924 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4925 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926 }
4927 return 0;
4928}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004929EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930
Takashi Iwaid5191e52009-11-16 14:58:17 +01004931/**
4932 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004934int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
4935 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936{
Takashi Iwaidda14412011-05-02 11:29:30 +02004937 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938 int i;
4939
4940 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01004941 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01004943 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02004944 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
4945 if (mout->hp_out_nid[i])
4946 snd_hda_codec_cleanup_stream(codec,
4947 mout->hp_out_nid[i]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004948 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
4949 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01004950 snd_hda_codec_cleanup_stream(codec,
4951 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01004952 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02004954 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955 mout->dig_out_used = 0;
4956 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004957 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958 return 0;
4959}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004960EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961
Takashi Iwai47408602012-04-20 13:06:53 +02004962/**
4963 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
4964 *
4965 * Guess the suitable VREF pin bits to be set as the pin-control value.
4966 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
4967 */
4968unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
4969{
4970 unsigned int pincap;
4971 unsigned int oldval;
4972 oldval = snd_hda_codec_read(codec, pin, 0,
4973 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
4974 pincap = snd_hda_query_pin_caps(codec, pin);
4975 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
4976 /* Exception: if the default pin setup is vref50, we give it priority */
4977 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
4978 return AC_PINCTL_VREF_80;
4979 else if (pincap & AC_PINCAP_VREF_50)
4980 return AC_PINCTL_VREF_50;
4981 else if (pincap & AC_PINCAP_VREF_100)
4982 return AC_PINCTL_VREF_100;
4983 else if (pincap & AC_PINCAP_VREF_GRD)
4984 return AC_PINCTL_VREF_GRD;
4985 return AC_PINCTL_VREF_HIZ;
4986}
4987EXPORT_SYMBOL_HDA(snd_hda_get_default_vref);
4988
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02004989int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
4990 unsigned int val, bool cached)
4991{
4992 if (val) {
4993 unsigned int cap = snd_hda_query_pin_caps(codec, pin);
Takashi Iwai6942c102012-04-20 13:08:40 +02004994 if (cap && (val & AC_PINCTL_OUT_EN)) {
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02004995 if (!(cap & AC_PINCAP_OUT))
4996 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
4997 else if ((val & AC_PINCTL_HP_EN) &&
4998 !(cap & AC_PINCAP_HP_DRV))
4999 val &= ~AC_PINCTL_HP_EN;
5000 }
Takashi Iwai6942c102012-04-20 13:08:40 +02005001 if (cap && (val & AC_PINCTL_IN_EN)) {
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005002 if (!(cap & AC_PINCAP_IN))
5003 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
5004 }
5005 }
5006 if (cached)
5007 return snd_hda_codec_update_cache(codec, pin, 0,
5008 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5009 else
5010 return snd_hda_codec_write(codec, pin, 0,
5011 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5012}
5013EXPORT_SYMBOL_HDA(_snd_hda_set_pin_ctl);
5014
Takashi Iwai990061c2010-09-09 22:08:44 +02005015/**
5016 * snd_hda_add_imux_item - Add an item to input_mux
5017 *
5018 * When the same label is used already in the existing items, the number
5019 * suffix is appended to the label. This label index number is stored
5020 * to type_idx when non-NULL pointer is given.
5021 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02005022int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
5023 int index, int *type_idx)
5024{
5025 int i, label_idx = 0;
5026 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
5027 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
5028 return -EINVAL;
5029 }
5030 for (i = 0; i < imux->num_items; i++) {
5031 if (!strncmp(label, imux->items[i].label, strlen(label)))
5032 label_idx++;
5033 }
5034 if (type_idx)
5035 *type_idx = label_idx;
5036 if (label_idx > 0)
5037 snprintf(imux->items[imux->num_items].label,
5038 sizeof(imux->items[imux->num_items].label),
5039 "%s %d", label, label_idx);
5040 else
5041 strlcpy(imux->items[imux->num_items].label, label,
5042 sizeof(imux->items[imux->num_items].label));
5043 imux->items[imux->num_items].index = index;
5044 imux->num_items++;
5045 return 0;
5046}
5047EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005048
Takashi Iwai4a471b72005-12-07 13:56:29 +01005049
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050#ifdef CONFIG_PM
5051/*
5052 * power management
5053 */
5054
5055/**
5056 * snd_hda_suspend - suspend the codecs
5057 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005058 *
5059 * Returns 0 if successful.
5060 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02005061int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005062{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005063 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064
Takashi Iwai0ba21762007-04-16 11:29:14 +02005065 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005066 if (hda_codec_is_power_on(codec))
5067 hda_call_codec_suspend(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068 }
5069 return 0;
5070}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005071EXPORT_SYMBOL_HDA(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072
5073/**
5074 * snd_hda_resume - resume the codecs
5075 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076 *
5077 * Returns 0 if successful.
Takashi Iwaicb53c622007-08-10 17:21:45 +02005078 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005079 * This function is defined only when POWER_SAVE isn't set.
Takashi Iwaicb53c622007-08-10 17:21:45 +02005080 * In the power-save mode, the codec is resumed dynamically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081 */
5082int snd_hda_resume(struct hda_bus *bus)
5083{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005084 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085
Takashi Iwai0ba21762007-04-16 11:29:14 +02005086 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai7f308302012-05-08 16:52:23 +02005087 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089 return 0;
5090}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005091EXPORT_SYMBOL_HDA(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005092#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005093
5094/*
5095 * generic arrays
5096 */
5097
Takashi Iwaid5191e52009-11-16 14:58:17 +01005098/**
5099 * snd_array_new - get a new element from the given array
5100 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005101 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01005102 * Get a new element from the given array. If it exceeds the
5103 * pre-allocated array size, re-allocate the array.
5104 *
5105 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02005106 */
5107void *snd_array_new(struct snd_array *array)
5108{
5109 if (array->used >= array->alloced) {
5110 int num = array->alloced + array->alloc_align;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005111 int size = (num + 1) * array->elem_size;
Takashi Iwai00ef9612011-07-14 15:57:27 +02005112 int oldsize = array->alloced * array->elem_size;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01005113 void *nlist;
5114 if (snd_BUG_ON(num >= 4096))
5115 return NULL;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005116 nlist = krealloc(array->list, size, GFP_KERNEL);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005117 if (!nlist)
5118 return NULL;
Takashi Iwai00ef9612011-07-14 15:57:27 +02005119 memset(nlist + oldsize, 0, size - oldsize);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005120 array->list = nlist;
5121 array->alloced = num;
5122 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01005123 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005124}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005125EXPORT_SYMBOL_HDA(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005126
Takashi Iwaid5191e52009-11-16 14:58:17 +01005127/**
5128 * snd_array_free - free the given array elements
5129 * @array: the array object
5130 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005131void snd_array_free(struct snd_array *array)
5132{
5133 kfree(array->list);
5134 array->used = 0;
5135 array->alloced = 0;
5136 array->list = NULL;
5137}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005138EXPORT_SYMBOL_HDA(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01005139
Takashi Iwaid5191e52009-11-16 14:58:17 +01005140/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01005141 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5142 * @pcm: PCM caps bits
5143 * @buf: the string buffer to write
5144 * @buflen: the max buffer length
5145 *
5146 * used by hda_proc.c and hda_eld.c
5147 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005148void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5149{
5150 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5151 int i, j;
5152
5153 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5154 if (pcm & (AC_SUPPCM_BITS_8 << i))
5155 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5156
5157 buf[j] = '\0'; /* necessary when j == 0 */
5158}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005159EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005160
5161MODULE_DESCRIPTION("HDA codec core");
5162MODULE_LICENSE("GPL");