blob: 5e7c4bf83abef038594eb5dbc2fce66c4ba41991 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337/**
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200338 * snd_hda_get_conn_list - get connection list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 * @codec: the HDA codec
340 * @nid: NID to parse
Takashi Iwaidce20792011-06-24 14:10:28 +0200341 * @listp: the pointer to store NID list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 *
343 * Parses the connection list of the given widget and stores the list
344 * of NIDs.
345 *
346 * Returns the number of connections, or a negative error code.
347 */
Takashi Iwaidce20792011-06-24 14:10:28 +0200348int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
349 const hda_nid_t **listp)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200350{
351 struct snd_array *array = &codec->conn_lists;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200352 int len, err;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200353 hda_nid_t list[HDA_MAX_CONNECTIONS];
Takashi Iwaidce20792011-06-24 14:10:28 +0200354 hda_nid_t *p;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200355 bool added = false;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200356
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200357 again:
358 /* if the connection-list is already cached, read it */
359 p = lookup_conn_list(array, nid);
360 if (p) {
361 if (listp)
362 *listp = p + 2;
363 return p[1];
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200364 }
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200365 if (snd_BUG_ON(added))
366 return -EINVAL;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200367
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200368 /* read the connection and add to the cache */
Takashi Iwai9e7717c2011-07-11 15:42:52 +0200369 len = snd_hda_get_raw_connections(codec, nid, list, HDA_MAX_CONNECTIONS);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200370 if (len < 0)
371 return len;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200372 err = snd_hda_override_conn_list(codec, nid, len, list);
373 if (err < 0)
374 return err;
375 added = true;
376 goto again;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200377}
Takashi Iwaidce20792011-06-24 14:10:28 +0200378EXPORT_SYMBOL_HDA(snd_hda_get_conn_list);
379
380/**
381 * snd_hda_get_connections - copy connection list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 * @codec: the HDA codec
383 * @nid: NID to parse
384 * @conn_list: connection list array
385 * @max_conns: max. number of connections to store
386 *
387 * Parses the connection list of the given widget and stores the list
388 * of NIDs.
389 *
390 * Returns the number of connections, or a negative error code.
391 */
392int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200393 hda_nid_t *conn_list, int max_conns)
394{
Takashi Iwaidce20792011-06-24 14:10:28 +0200395 const hda_nid_t *list;
396 int len = snd_hda_get_conn_list(codec, nid, &list);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200397
Takashi Iwaidce20792011-06-24 14:10:28 +0200398 if (len <= 0)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200399 return len;
Takashi Iwaidce20792011-06-24 14:10:28 +0200400 if (len > max_conns) {
401 snd_printk(KERN_ERR "hda_codec: "
402 "Too many connections %d for NID 0x%x\n",
403 len, nid);
404 return -EINVAL;
405 }
406 memcpy(conn_list, list, len * sizeof(hda_nid_t));
407 return len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200408}
409EXPORT_SYMBOL_HDA(snd_hda_get_connections);
410
Takashi Iwai9e7717c2011-07-11 15:42:52 +0200411/**
412 * snd_hda_get_raw_connections - copy connection list without cache
413 * @codec: the HDA codec
414 * @nid: NID to parse
415 * @conn_list: connection list array
416 * @max_conns: max. number of connections to store
417 *
418 * Like snd_hda_get_connections(), copy the connection list but without
419 * checking through the connection-list cache.
420 * Currently called only from hda_proc.c, so not exported.
421 */
422int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid,
423 hda_nid_t *conn_list, int max_conns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
425 unsigned int parm;
Takashi Iwai54d17402005-11-21 16:33:22 +0100426 int i, conn_len, conns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 unsigned int shift, num_elems, mask;
Takashi Iwai1ba7a7c2009-07-27 12:56:26 +0200428 unsigned int wcaps;
Takashi Iwai54d17402005-11-21 16:33:22 +0100429 hda_nid_t prev_nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Takashi Iwaida3cec32008-08-08 17:12:14 +0200431 if (snd_BUG_ON(!conn_list || max_conns <= 0))
432 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Takashi Iwai1ba7a7c2009-07-27 12:56:26 +0200434 wcaps = get_wcaps(codec, nid);
435 if (!(wcaps & AC_WCAP_CONN_LIST) &&
Takashi Iwai8d087c72011-06-28 12:45:47 +0200436 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
437 return 0;
Jaroslav Kysela16a433d2009-07-22 16:20:40 +0200438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
440 if (parm & AC_CLIST_LONG) {
441 /* long form */
442 shift = 16;
443 num_elems = 2;
444 } else {
445 /* short form */
446 shift = 8;
447 num_elems = 4;
448 }
449 conn_len = parm & AC_CLIST_LENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 mask = (1 << (shift-1)) - 1;
451
Takashi Iwai0ba21762007-04-16 11:29:14 +0200452 if (!conn_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 return 0; /* no connection */
454
455 if (conn_len == 1) {
456 /* single connection */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200457 parm = snd_hda_codec_read(codec, nid, 0,
458 AC_VERB_GET_CONNECT_LIST, 0);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200459 if (parm == -1 && codec->bus->rirb_error)
460 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 conn_list[0] = parm & mask;
462 return 1;
463 }
464
465 /* multi connection */
466 conns = 0;
Takashi Iwai54d17402005-11-21 16:33:22 +0100467 prev_nid = 0;
468 for (i = 0; i < conn_len; i++) {
469 int range_val;
470 hda_nid_t val, n;
471
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200472 if (i % num_elems == 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +0100473 parm = snd_hda_codec_read(codec, nid, 0,
474 AC_VERB_GET_CONNECT_LIST, i);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200475 if (parm == -1 && codec->bus->rirb_error)
476 return -EIO;
477 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200478 range_val = !!(parm & (1 << (shift-1))); /* ranges */
Takashi Iwai54d17402005-11-21 16:33:22 +0100479 val = parm & mask;
Jaroslav Kysela2e9bf242009-07-18 11:48:19 +0200480 if (val == 0) {
481 snd_printk(KERN_WARNING "hda_codec: "
482 "invalid CONNECT_LIST verb %x[%i]:%x\n",
483 nid, i, parm);
484 return 0;
485 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100486 parm >>= shift;
487 if (range_val) {
488 /* ranges between the previous and this one */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200489 if (!prev_nid || prev_nid >= val) {
490 snd_printk(KERN_WARNING "hda_codec: "
491 "invalid dep_range_val %x:%x\n",
492 prev_nid, val);
Takashi Iwai54d17402005-11-21 16:33:22 +0100493 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100495 for (n = prev_nid + 1; n <= val; n++) {
496 if (conns >= max_conns) {
Takashi Iwai5aacc212010-07-30 10:36:29 +0200497 snd_printk(KERN_ERR "hda_codec: "
498 "Too many connections %d for NID 0x%x\n",
499 conns, nid);
Takashi Iwai54d17402005-11-21 16:33:22 +0100500 return -EINVAL;
501 }
502 conn_list[conns++] = n;
503 }
504 } else {
505 if (conns >= max_conns) {
Takashi Iwai5aacc212010-07-30 10:36:29 +0200506 snd_printk(KERN_ERR "hda_codec: "
507 "Too many connections %d for NID 0x%x\n",
508 conns, nid);
Takashi Iwai54d17402005-11-21 16:33:22 +0100509 return -EINVAL;
510 }
511 conn_list[conns++] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100513 prev_nid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 }
515 return conns;
516}
517
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200518static bool add_conn_list(struct snd_array *array, hda_nid_t nid)
519{
520 hda_nid_t *p = snd_array_new(array);
521 if (!p)
522 return false;
523 *p = nid;
524 return true;
525}
526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527/**
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200528 * snd_hda_override_conn_list - add/modify the connection-list to cache
529 * @codec: the HDA codec
530 * @nid: NID to parse
531 * @len: number of connection list entries
532 * @list: the list of connection entries
533 *
534 * Add or modify the given connection-list to the cache. If the corresponding
535 * cache already exists, invalidate it and append a new one.
536 *
537 * Returns zero or a negative error code.
538 */
539int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
540 const hda_nid_t *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200542 struct snd_array *array = &codec->conn_lists;
543 hda_nid_t *p;
544 int i, old_used;
545
546 p = lookup_conn_list(array, nid);
547 if (p)
548 *p = -1; /* invalidate the old entry */
549
550 old_used = array->used;
551 if (!add_conn_list(array, nid) || !add_conn_list(array, len))
552 goto error_add;
553 for (i = 0; i < len; i++)
554 if (!add_conn_list(array, list[i]))
555 goto error_add;
556 return 0;
557
558 error_add:
559 array->used = old_used;
560 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561}
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200562EXPORT_SYMBOL_HDA(snd_hda_override_conn_list);
563
564/**
Takashi Iwai8d087c72011-06-28 12:45:47 +0200565 * snd_hda_get_conn_index - get the connection index of the given NID
566 * @codec: the HDA codec
567 * @mux: NID containing the list
568 * @nid: NID to select
569 * @recursive: 1 when searching NID recursively, otherwise 0
570 *
571 * Parses the connection list of the widget @mux and checks whether the
572 * widget @nid is present. If it is, return the connection index.
573 * Otherwise it returns -1.
574 */
575int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
576 hda_nid_t nid, int recursive)
577{
578 hda_nid_t conn[HDA_MAX_NUM_INPUTS];
579 int i, nums;
580
581 nums = snd_hda_get_connections(codec, mux, conn, ARRAY_SIZE(conn));
582 for (i = 0; i < nums; i++)
583 if (conn[i] == nid)
584 return i;
585 if (!recursive)
586 return -1;
587 if (recursive > 5) {
588 snd_printd("hda_codec: too deep connection for 0x%x\n", nid);
589 return -1;
590 }
591 recursive++;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200592 for (i = 0; i < nums; i++) {
593 unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
594 if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
595 continue;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200596 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
597 return i;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200598 }
Takashi Iwai8d087c72011-06-28 12:45:47 +0200599 return -1;
600}
601EXPORT_SYMBOL_HDA(snd_hda_get_conn_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603/**
604 * snd_hda_queue_unsol_event - add an unsolicited event to queue
605 * @bus: the BUS
606 * @res: unsolicited event (lower 32bit of RIRB entry)
607 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
608 *
609 * Adds the given event to the queue. The events are processed in
610 * the workqueue asynchronously. Call this function in the interrupt
611 * hanlder when RIRB receives an unsolicited event.
612 *
613 * Returns 0 if successful, or a negative error code.
614 */
615int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
616{
617 struct hda_bus_unsolicited *unsol;
618 unsigned int wp;
619
Takashi Iwaiecf726f2011-08-09 14:22:44 +0200620 trace_hda_unsol_event(bus, res, res_ex);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200621 unsol = bus->unsol;
622 if (!unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 return 0;
624
625 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
626 unsol->wp = wp;
627
628 wp <<= 1;
629 unsol->queue[wp] = res;
630 unsol->queue[wp + 1] = res_ex;
631
Takashi Iwai6acaed32009-01-12 10:09:24 +0100632 queue_work(bus->workq, &unsol->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634 return 0;
635}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100636EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
638/*
Wu Fengguang5c1d1a92008-10-07 14:17:53 +0800639 * process queued unsolicited events
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 */
David Howellsc4028952006-11-22 14:57:56 +0000641static void process_unsol_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
David Howellsc4028952006-11-22 14:57:56 +0000643 struct hda_bus_unsolicited *unsol =
644 container_of(work, struct hda_bus_unsolicited, work);
645 struct hda_bus *bus = unsol->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 struct hda_codec *codec;
647 unsigned int rp, caddr, res;
648
649 while (unsol->rp != unsol->wp) {
650 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
651 unsol->rp = rp;
652 rp <<= 1;
653 res = unsol->queue[rp];
654 caddr = unsol->queue[rp + 1];
Takashi Iwai0ba21762007-04-16 11:29:14 +0200655 if (!(caddr & (1 << 4))) /* no unsolicited event? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 continue;
657 codec = bus->caddr_tbl[caddr & 0x0f];
658 if (codec && codec->patch_ops.unsol_event)
659 codec->patch_ops.unsol_event(codec, res);
660 }
661}
662
663/*
664 * initialize unsolicited queue
665 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200666static int init_unsol_queue(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
668 struct hda_bus_unsolicited *unsol;
669
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100670 if (bus->unsol) /* already initialized */
671 return 0;
672
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200673 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200674 if (!unsol) {
675 snd_printk(KERN_ERR "hda_codec: "
676 "can't allocate unsolicited queue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 return -ENOMEM;
678 }
David Howellsc4028952006-11-22 14:57:56 +0000679 INIT_WORK(&unsol->work, process_unsol_events);
680 unsol->bus = bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 bus->unsol = unsol;
682 return 0;
683}
684
685/*
686 * destructor
687 */
688static void snd_hda_codec_free(struct hda_codec *codec);
689
690static int snd_hda_bus_free(struct hda_bus *bus)
691{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200692 struct hda_codec *codec, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Takashi Iwai0ba21762007-04-16 11:29:14 +0200694 if (!bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 return 0;
Takashi Iwai6acaed32009-01-12 10:09:24 +0100696 if (bus->workq)
697 flush_workqueue(bus->workq);
698 if (bus->unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 kfree(bus->unsol);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200700 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 snd_hda_codec_free(codec);
702 }
703 if (bus->ops.private_free)
704 bus->ops.private_free(bus);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100705 if (bus->workq)
706 destroy_workqueue(bus->workq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 kfree(bus);
708 return 0;
709}
710
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100711static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
713 struct hda_bus *bus = device->device_data;
Takashi Iwaib94d35392008-11-21 09:08:06 +0100714 bus->shutdown = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 return snd_hda_bus_free(bus);
716}
717
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200718#ifdef CONFIG_SND_HDA_HWDEP
719static int snd_hda_bus_dev_register(struct snd_device *device)
720{
721 struct hda_bus *bus = device->device_data;
722 struct hda_codec *codec;
723 list_for_each_entry(codec, &bus->codec_list, list) {
724 snd_hda_hwdep_add_sysfs(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +0100725 snd_hda_hwdep_add_power_sysfs(codec);
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200726 }
727 return 0;
728}
729#else
730#define snd_hda_bus_dev_register NULL
731#endif
732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733/**
734 * snd_hda_bus_new - create a HDA bus
735 * @card: the card entry
736 * @temp: the template for hda_bus information
737 * @busp: the pointer to store the created bus instance
738 *
739 * Returns 0 if successful, or a negative error code.
740 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100741int /*__devinit*/ snd_hda_bus_new(struct snd_card *card,
Takashi Iwai756e2b02007-04-16 11:27:07 +0200742 const struct hda_bus_template *temp,
743 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
745 struct hda_bus *bus;
746 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100747 static struct snd_device_ops dev_ops = {
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200748 .dev_register = snd_hda_bus_dev_register,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 .dev_free = snd_hda_bus_dev_free,
750 };
751
Takashi Iwaida3cec32008-08-08 17:12:14 +0200752 if (snd_BUG_ON(!temp))
753 return -EINVAL;
754 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
755 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
757 if (busp)
758 *busp = NULL;
759
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200760 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 if (bus == NULL) {
762 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
763 return -ENOMEM;
764 }
765
766 bus->card = card;
767 bus->private_data = temp->private_data;
768 bus->pci = temp->pci;
769 bus->modelname = temp->modelname;
Takashi Iwaifee2fba2008-11-27 12:43:28 +0100770 bus->power_save = temp->power_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 bus->ops = temp->ops;
772
Ingo Molnar62932df2006-01-16 16:34:20 +0100773 mutex_init(&bus->cmd_mutex);
Takashi Iwai3f50ac62010-08-20 09:44:36 +0200774 mutex_init(&bus->prepare_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 INIT_LIST_HEAD(&bus->codec_list);
776
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100777 snprintf(bus->workq_name, sizeof(bus->workq_name),
778 "hd-audio%d", card->number);
779 bus->workq = create_singlethread_workqueue(bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100780 if (!bus->workq) {
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100781 snd_printk(KERN_ERR "cannot create workqueue %s\n",
782 bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100783 kfree(bus);
784 return -ENOMEM;
785 }
786
Takashi Iwai0ba21762007-04-16 11:29:14 +0200787 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
788 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 snd_hda_bus_free(bus);
790 return err;
791 }
792 if (busp)
793 *busp = bus;
794 return 0;
795}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100796EXPORT_SYMBOL_HDA(snd_hda_bus_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Takashi Iwai82467612007-07-27 19:15:54 +0200798#ifdef CONFIG_SND_HDA_GENERIC
799#define is_generic_config(codec) \
Takashi Iwaif44ac832008-07-30 15:01:45 +0200800 (codec->modelname && !strcmp(codec->modelname, "generic"))
Takashi Iwai82467612007-07-27 19:15:54 +0200801#else
802#define is_generic_config(codec) 0
803#endif
804
Takashi Iwai645f10c2008-11-28 15:07:37 +0100805#ifdef MODULE
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100806#define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
807#else
Takashi Iwai645f10c2008-11-28 15:07:37 +0100808#define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100809#endif
810
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811/*
812 * find a matching codec preset
813 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200814static const struct hda_codec_preset *
Takashi Iwai756e2b02007-04-16 11:27:07 +0200815find_codec_preset(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816{
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100817 struct hda_codec_preset_list *tbl;
818 const struct hda_codec_preset *preset;
819 int mod_requested = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Takashi Iwai82467612007-07-27 19:15:54 +0200821 if (is_generic_config(codec))
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100822 return NULL; /* use the generic parser */
823
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100824 again:
825 mutex_lock(&preset_mutex);
826 list_for_each_entry(tbl, &hda_preset_tables, list) {
827 if (!try_module_get(tbl->owner)) {
828 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
829 continue;
830 }
831 for (preset = tbl->preset; preset->id; preset++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 u32 mask = preset->mask;
Marc Boucherca7cfae2008-01-22 15:32:25 +0100833 if (preset->afg && preset->afg != codec->afg)
834 continue;
835 if (preset->mfg && preset->mfg != codec->mfg)
836 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200837 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 mask = ~0;
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200839 if (preset->id == (codec->vendor_id & mask) &&
Takashi Iwai0ba21762007-04-16 11:29:14 +0200840 (!preset->rev ||
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100841 preset->rev == codec->revision_id)) {
842 mutex_unlock(&preset_mutex);
843 codec->owner = tbl->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 return preset;
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100845 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 }
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100847 module_put(tbl->owner);
848 }
849 mutex_unlock(&preset_mutex);
850
851 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
852 char name[32];
853 if (!mod_requested)
854 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
855 codec->vendor_id);
856 else
857 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
858 (codec->vendor_id >> 16) & 0xffff);
859 request_module(name);
860 mod_requested++;
861 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 }
863 return NULL;
864}
865
866/*
Takashi Iwaif44ac832008-07-30 15:01:45 +0200867 * get_codec_name - store the codec name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 */
Takashi Iwaif44ac832008-07-30 15:01:45 +0200869static int get_codec_name(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870{
871 const struct hda_vendor_id *c;
872 const char *vendor = NULL;
873 u16 vendor_id = codec->vendor_id >> 16;
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200874 char tmp[16];
875
876 if (codec->vendor_name)
877 goto get_chip_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
879 for (c = hda_vendor_ids; c->id; c++) {
880 if (c->id == vendor_id) {
881 vendor = c->name;
882 break;
883 }
884 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200885 if (!vendor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 sprintf(tmp, "Generic %04x", vendor_id);
887 vendor = tmp;
888 }
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200889 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
890 if (!codec->vendor_name)
891 return -ENOMEM;
892
893 get_chip_name:
894 if (codec->chip_name)
895 return 0;
896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 if (codec->preset && codec->preset->name)
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200898 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
899 else {
900 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
901 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
902 }
903 if (!codec->chip_name)
Takashi Iwaif44ac832008-07-30 15:01:45 +0200904 return -ENOMEM;
905 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906}
907
908/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200909 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100911static void /*__devinit*/ setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912{
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200913 int i, total_nodes, function_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 hda_nid_t nid;
915
916 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
917 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200918 function_id = snd_hda_param_read(codec, nid,
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200919 AC_PAR_FUNCTION_TYPE);
Jaroslav Kyselacd7643b2010-07-20 12:11:25 +0200920 switch (function_id & 0xff) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200921 case AC_GRP_AUDIO_FUNCTION:
922 codec->afg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200923 codec->afg_function_id = function_id & 0xff;
924 codec->afg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200925 break;
926 case AC_GRP_MODEM_FUNCTION:
927 codec->mfg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200928 codec->mfg_function_id = function_id & 0xff;
929 codec->mfg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200930 break;
931 default:
932 break;
933 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935}
936
937/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100938 * read widget caps for each widget and store in cache
939 */
940static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
941{
942 int i;
943 hda_nid_t nid;
944
945 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
946 &codec->start_nid);
947 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200948 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +0100949 return -ENOMEM;
950 nid = codec->start_nid;
951 for (i = 0; i < codec->num_nodes; i++, nid++)
952 codec->wcaps[i] = snd_hda_param_read(codec, nid,
953 AC_PAR_AUDIO_WIDGET_CAP);
954 return 0;
955}
956
Takashi Iwai3be14142009-02-20 14:11:16 +0100957/* read all pin default configurations and save codec->init_pins */
958static int read_pin_defaults(struct hda_codec *codec)
959{
960 int i;
961 hda_nid_t nid = codec->start_nid;
962
963 for (i = 0; i < codec->num_nodes; i++, nid++) {
964 struct hda_pincfg *pin;
965 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +0200966 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwai3be14142009-02-20 14:11:16 +0100967 if (wid_type != AC_WID_PIN)
968 continue;
969 pin = snd_array_new(&codec->init_pins);
970 if (!pin)
971 return -ENOMEM;
972 pin->nid = nid;
973 pin->cfg = snd_hda_codec_read(codec, nid, 0,
974 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200975 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
976 AC_VERB_GET_PIN_WIDGET_CONTROL,
977 0);
Takashi Iwai3be14142009-02-20 14:11:16 +0100978 }
979 return 0;
980}
981
982/* look up the given pin config list and return the item matching with NID */
983static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
984 struct snd_array *array,
985 hda_nid_t nid)
986{
987 int i;
988 for (i = 0; i < array->used; i++) {
989 struct hda_pincfg *pin = snd_array_elem(array, i);
990 if (pin->nid == nid)
991 return pin;
992 }
993 return NULL;
994}
995
996/* write a config value for the given NID */
997static void set_pincfg(struct hda_codec *codec, hda_nid_t nid,
998 unsigned int cfg)
999{
1000 int i;
1001 for (i = 0; i < 4; i++) {
1002 snd_hda_codec_write(codec, nid, 0,
1003 AC_VERB_SET_CONFIG_DEFAULT_BYTES_0 + i,
1004 cfg & 0xff);
1005 cfg >>= 8;
1006 }
1007}
1008
1009/* set the current pin config value for the given NID.
1010 * the value is cached, and read via snd_hda_codec_get_pincfg()
1011 */
1012int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
1013 hda_nid_t nid, unsigned int cfg)
1014{
1015 struct hda_pincfg *pin;
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001016 unsigned int oldcfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001017
Takashi Iwaib82855a2009-12-27 11:24:56 +01001018 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
1019 return -EINVAL;
1020
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001021 oldcfg = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +01001022 pin = look_up_pincfg(codec, list, nid);
1023 if (!pin) {
1024 pin = snd_array_new(list);
1025 if (!pin)
1026 return -ENOMEM;
1027 pin->nid = nid;
1028 }
1029 pin->cfg = cfg;
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001030
1031 /* change only when needed; e.g. if the pincfg is already present
1032 * in user_pins[], don't write it
1033 */
1034 cfg = snd_hda_codec_get_pincfg(codec, nid);
1035 if (oldcfg != cfg)
1036 set_pincfg(codec, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001037 return 0;
1038}
1039
Takashi Iwaid5191e52009-11-16 14:58:17 +01001040/**
1041 * snd_hda_codec_set_pincfg - Override a pin default configuration
1042 * @codec: the HDA codec
1043 * @nid: NID to set the pin config
1044 * @cfg: the pin default config value
1045 *
1046 * Override a pin default configuration value in the cache.
1047 * This value can be read by snd_hda_codec_get_pincfg() in a higher
1048 * priority than the real hardware value.
1049 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001050int snd_hda_codec_set_pincfg(struct hda_codec *codec,
1051 hda_nid_t nid, unsigned int cfg)
1052{
Takashi Iwai346ff702009-02-23 09:42:57 +01001053 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001054}
1055EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
1056
Takashi Iwaid5191e52009-11-16 14:58:17 +01001057/**
1058 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
1059 * @codec: the HDA codec
1060 * @nid: NID to get the pin config
1061 *
1062 * Get the current pin config value of the given pin NID.
1063 * If the pincfg value is cached or overridden via sysfs or driver,
1064 * returns the cached value.
1065 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001066unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
1067{
1068 struct hda_pincfg *pin;
1069
Takashi Iwai3be14142009-02-20 14:11:16 +01001070#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +01001071 pin = look_up_pincfg(codec, &codec->user_pins, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +01001072 if (pin)
1073 return pin->cfg;
1074#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001075 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
1076 if (pin)
1077 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001078 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1079 if (pin)
1080 return pin->cfg;
1081 return 0;
1082}
1083EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
1084
1085/* restore all current pin configs */
1086static void restore_pincfgs(struct hda_codec *codec)
1087{
1088 int i;
1089 for (i = 0; i < codec->init_pins.used; i++) {
1090 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1091 set_pincfg(codec, pin->nid,
1092 snd_hda_codec_get_pincfg(codec, pin->nid));
1093 }
1094}
Takashi Iwai54d17402005-11-21 16:33:22 +01001095
Takashi Iwai92ee6162009-12-27 11:18:59 +01001096/**
1097 * snd_hda_shutup_pins - Shut up all pins
1098 * @codec: the HDA codec
1099 *
1100 * Clear all pin controls to shup up before suspend for avoiding click noise.
1101 * The controls aren't cached so that they can be resumed properly.
1102 */
1103void snd_hda_shutup_pins(struct hda_codec *codec)
1104{
1105 int i;
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001106 /* don't shut up pins when unloading the driver; otherwise it breaks
1107 * the default pin setup at the next load of the driver
1108 */
1109 if (codec->bus->shutdown)
1110 return;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001111 for (i = 0; i < codec->init_pins.used; i++) {
1112 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1113 /* use read here for syncing after issuing each verb */
1114 snd_hda_codec_read(codec, pin->nid, 0,
1115 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
1116 }
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001117 codec->pins_shutup = 1;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001118}
1119EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);
1120
Takashi Iwai2a439522011-07-26 09:52:50 +02001121#ifdef CONFIG_PM
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001122/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1123static void restore_shutup_pins(struct hda_codec *codec)
1124{
1125 int i;
1126 if (!codec->pins_shutup)
1127 return;
1128 if (codec->bus->shutdown)
1129 return;
1130 for (i = 0; i < codec->init_pins.used; i++) {
1131 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1132 snd_hda_codec_write(codec, pin->nid, 0,
1133 AC_VERB_SET_PIN_WIDGET_CONTROL,
1134 pin->ctrl);
1135 }
1136 codec->pins_shutup = 0;
1137}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001138#endif
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001139
Takashi Iwai01751f52007-08-10 16:59:39 +02001140static void init_hda_cache(struct hda_cache_rec *cache,
1141 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001142static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +02001143
Takashi Iwai3be14142009-02-20 14:11:16 +01001144/* restore the initial pin cfgs and release all pincfg lists */
1145static void restore_init_pincfgs(struct hda_codec *codec)
1146{
Takashi Iwai346ff702009-02-23 09:42:57 +01001147 /* first free driver_pins and user_pins, then call restore_pincfg
Takashi Iwai3be14142009-02-20 14:11:16 +01001148 * so that only the values in init_pins are restored
1149 */
Takashi Iwai346ff702009-02-23 09:42:57 +01001150 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001151#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +01001152 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001153#endif
1154 restore_pincfgs(codec);
1155 snd_array_free(&codec->init_pins);
1156}
1157
Takashi Iwai54d17402005-11-21 16:33:22 +01001158/*
Takashi Iwaieb541332010-08-06 13:48:11 +02001159 * audio-converter setup caches
1160 */
1161struct hda_cvt_setup {
1162 hda_nid_t nid;
1163 u8 stream_tag;
1164 u8 channel_id;
1165 u16 format_id;
1166 unsigned char active; /* cvt is currently used */
1167 unsigned char dirty; /* setups should be cleared */
1168};
1169
1170/* get or create a cache entry for the given audio converter NID */
1171static struct hda_cvt_setup *
1172get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1173{
1174 struct hda_cvt_setup *p;
1175 int i;
1176
1177 for (i = 0; i < codec->cvt_setups.used; i++) {
1178 p = snd_array_elem(&codec->cvt_setups, i);
1179 if (p->nid == nid)
1180 return p;
1181 }
1182 p = snd_array_new(&codec->cvt_setups);
1183 if (p)
1184 p->nid = nid;
1185 return p;
1186}
1187
1188/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 * codec destructor
1190 */
1191static void snd_hda_codec_free(struct hda_codec *codec)
1192{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001193 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 return;
Takashi Iwai3be14142009-02-20 14:11:16 +01001195 restore_init_pincfgs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001196#ifdef CONFIG_SND_HDA_POWER_SAVE
1197 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01001198 flush_workqueue(codec->bus->workq);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001199#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001201 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001202 snd_array_free(&codec->nids);
Takashi Iwaia12d3e12011-04-07 15:55:15 +02001203 snd_array_free(&codec->conn_lists);
Stephen Warren7c9359762011-06-01 11:14:17 -06001204 snd_array_free(&codec->spdif_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 codec->bus->caddr_tbl[codec->addr] = NULL;
1206 if (codec->patch_ops.free)
1207 codec->patch_ops.free(codec);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001208 module_put(codec->owner);
Takashi Iwai01751f52007-08-10 16:59:39 +02001209 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001210 free_hda_cache(&codec->cmd_cache);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001211 kfree(codec->vendor_name);
1212 kfree(codec->chip_name);
Takashi Iwaif44ac832008-07-30 15:01:45 +02001213 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +01001214 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 kfree(codec);
1216}
1217
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001218static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1219 unsigned int power_state);
1220
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221/**
1222 * snd_hda_codec_new - create a HDA codec
1223 * @bus: the bus to assign
1224 * @codec_addr: the codec address
1225 * @codecp: the pointer to store the generated codec
1226 *
1227 * Returns 0 if successful, or a negative error code.
1228 */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001229int /*__devinit*/ snd_hda_codec_new(struct hda_bus *bus,
1230 unsigned int codec_addr,
1231 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232{
1233 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +02001234 char component[31];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 int err;
1236
Takashi Iwaida3cec32008-08-08 17:12:14 +02001237 if (snd_BUG_ON(!bus))
1238 return -EINVAL;
1239 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1240 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
1242 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001243 snd_printk(KERN_ERR "hda_codec: "
1244 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 return -EBUSY;
1246 }
1247
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001248 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 if (codec == NULL) {
1250 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1251 return -ENOMEM;
1252 }
1253
1254 codec->bus = bus;
1255 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001256 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001257 mutex_init(&codec->control_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +02001258 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001259 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001260 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1261 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001262 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001263 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +02001264 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Takashi Iwaia12d3e12011-04-07 15:55:15 +02001265 snd_array_init(&codec->conn_lists, sizeof(hda_nid_t), 64);
Stephen Warren7c9359762011-06-01 11:14:17 -06001266 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
Takashi Iwaicb53c622007-08-10 17:21:45 +02001268#ifdef CONFIG_SND_HDA_POWER_SAVE
Takashi Iwai5536c6d2012-05-08 17:08:10 +02001269 spin_lock_init(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001270 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1271 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1272 * the caller has to power down appropriatley after initialization
1273 * phase.
1274 */
1275 hda_keep_power_on(codec);
1276#endif
1277
Takashi Iwaic382a9f2012-05-07 15:01:02 +02001278 if (codec->bus->modelname) {
1279 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1280 if (!codec->modelname) {
1281 snd_hda_codec_free(codec);
1282 return -ENODEV;
1283 }
1284 }
1285
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 list_add_tail(&codec->list, &bus->codec_list);
1287 bus->caddr_tbl[codec_addr] = codec;
1288
Takashi Iwai0ba21762007-04-16 11:29:14 +02001289 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1290 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001291 if (codec->vendor_id == -1)
1292 /* read again, hopefully the access method was corrected
1293 * in the last read...
1294 */
1295 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1296 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001297 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1298 AC_PAR_SUBSYSTEM_ID);
1299 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1300 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001302 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001303 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001304 snd_printdd("hda_codec: no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001305 err = -ENODEV;
1306 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 }
1308
Takashi Iwai3be14142009-02-20 14:11:16 +01001309 err = read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg);
1310 if (err < 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +01001311 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001312 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001313 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001314 err = read_pin_defaults(codec);
1315 if (err < 0)
1316 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001317
Takashi Iwai0ba21762007-04-16 11:29:14 +02001318 if (!codec->subsystem_id) {
Takashi Iwai86284e42005-10-11 15:05:54 +02001319 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001320 codec->subsystem_id =
1321 snd_hda_codec_read(codec, nid, 0,
1322 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001323 }
1324
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001325 /* power-up all before initialization */
1326 hda_set_power_state(codec,
1327 codec->afg ? codec->afg : codec->mfg,
1328 AC_PWRST_D0);
1329
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001330 snd_hda_codec_proc_new(codec);
1331
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001332 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001333
1334 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1335 codec->subsystem_id, codec->revision_id);
1336 snd_component_add(codec->bus->card, component);
1337
1338 if (codecp)
1339 *codecp = codec;
1340 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001341
1342 error:
1343 snd_hda_codec_free(codec);
1344 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001345}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001346EXPORT_SYMBOL_HDA(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001347
Takashi Iwaid5191e52009-11-16 14:58:17 +01001348/**
1349 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1350 * @codec: the HDA codec
1351 *
1352 * Start parsing of the given codec tree and (re-)initialize the whole
1353 * patch instance.
1354 *
1355 * Returns 0 if successful or a negative error code.
1356 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001357int snd_hda_codec_configure(struct hda_codec *codec)
1358{
1359 int err;
1360
Takashi Iwaid5ad6302007-03-07 15:55:59 +01001361 codec->preset = find_codec_preset(codec);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001362 if (!codec->vendor_name || !codec->chip_name) {
Takashi Iwaif44ac832008-07-30 15:01:45 +02001363 err = get_codec_name(codec);
1364 if (err < 0)
1365 return err;
1366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
Takashi Iwai82467612007-07-27 19:15:54 +02001368 if (is_generic_config(codec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai82467612007-07-27 19:15:54 +02001370 goto patched;
1371 }
Takashi Iwai82467612007-07-27 19:15:54 +02001372 if (codec->preset && codec->preset->patch) {
1373 err = codec->preset->patch(codec);
1374 goto patched;
1375 }
1376
1377 /* call the default parser */
Takashi Iwai82467612007-07-27 19:15:54 +02001378 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai35a1e0c2007-10-19 08:13:40 +02001379 if (err < 0)
1380 printk(KERN_ERR "hda-codec: No codec parser is available\n");
Takashi Iwai82467612007-07-27 19:15:54 +02001381
1382 patched:
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001383 if (!err && codec->patch_ops.unsol_event)
1384 err = init_unsol_queue(codec->bus);
Takashi Iwaif62faed2009-12-23 09:27:51 +01001385 /* audio codec should override the mixer name */
1386 if (!err && (codec->afg || !*codec->bus->card->mixername))
1387 snprintf(codec->bus->card->mixername,
1388 sizeof(codec->bus->card->mixername),
1389 "%s %s", codec->vendor_name, codec->chip_name);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001390 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391}
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001392EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
1394/**
1395 * snd_hda_codec_setup_stream - set up the codec for streaming
1396 * @codec: the CODEC to set up
1397 * @nid: the NID to set up
1398 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1399 * @channel_id: channel id to pass, zero based.
1400 * @format: stream format.
1401 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001402void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1403 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 int channel_id, int format)
1405{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001406 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001407 struct hda_cvt_setup *p;
1408 unsigned int oldval, newval;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001409 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001410 int i;
1411
Takashi Iwai0ba21762007-04-16 11:29:14 +02001412 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001413 return;
1414
Takashi Iwai0ba21762007-04-16 11:29:14 +02001415 snd_printdd("hda_codec_setup_stream: "
1416 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001418 p = get_hda_cvt_setup(codec, nid);
1419 if (!p)
1420 return;
1421 /* update the stream-id if changed */
1422 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1423 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1424 newval = (stream_tag << 4) | channel_id;
1425 if (oldval != newval)
1426 snd_hda_codec_write(codec, nid, 0,
1427 AC_VERB_SET_CHANNEL_STREAMID,
1428 newval);
1429 p->stream_tag = stream_tag;
1430 p->channel_id = channel_id;
1431 }
1432 /* update the format-id if changed */
1433 if (p->format_id != format) {
1434 oldval = snd_hda_codec_read(codec, nid, 0,
1435 AC_VERB_GET_STREAM_FORMAT, 0);
1436 if (oldval != format) {
1437 msleep(1);
1438 snd_hda_codec_write(codec, nid, 0,
1439 AC_VERB_SET_STREAM_FORMAT,
1440 format);
1441 }
1442 p->format_id = format;
1443 }
1444 p->active = 1;
1445 p->dirty = 0;
1446
1447 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001448 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001449 list_for_each_entry(c, &codec->bus->codec_list, list) {
1450 for (i = 0; i < c->cvt_setups.used; i++) {
1451 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001452 if (!p->active && p->stream_tag == stream_tag &&
David Henningsson54c2a892012-02-01 12:05:41 +01001453 get_wcaps_type(get_wcaps(c, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001454 p->dirty = 1;
1455 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001458EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
Takashi Iwaif0cea792010-08-13 11:56:53 +02001460static void really_cleanup_stream(struct hda_codec *codec,
1461 struct hda_cvt_setup *q);
1462
Takashi Iwaid5191e52009-11-16 14:58:17 +01001463/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001464 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001465 * @codec: the CODEC to clean up
1466 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001467 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001468 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001469void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1470 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001471{
Takashi Iwaieb541332010-08-06 13:48:11 +02001472 struct hda_cvt_setup *p;
1473
Takashi Iwai888afa12008-03-18 09:57:50 +01001474 if (!nid)
1475 return;
1476
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001477 if (codec->no_sticky_stream)
1478 do_now = 1;
1479
Takashi Iwai888afa12008-03-18 09:57:50 +01001480 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001481 p = get_hda_cvt_setup(codec, nid);
Takashi Iwaif0cea792010-08-13 11:56:53 +02001482 if (p) {
1483 /* here we just clear the active flag when do_now isn't set;
1484 * actual clean-ups will be done later in
1485 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1486 */
1487 if (do_now)
1488 really_cleanup_stream(codec, p);
1489 else
1490 p->active = 0;
1491 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001492}
Takashi Iwaif0cea792010-08-13 11:56:53 +02001493EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001494
Takashi Iwaieb541332010-08-06 13:48:11 +02001495static void really_cleanup_stream(struct hda_codec *codec,
1496 struct hda_cvt_setup *q)
1497{
1498 hda_nid_t nid = q->nid;
Takashi Iwai218264a2011-09-27 17:33:45 +02001499 if (q->stream_tag || q->channel_id)
1500 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1501 if (q->format_id)
1502 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1503);
Takashi Iwaieb541332010-08-06 13:48:11 +02001504 memset(q, 0, sizeof(*q));
1505 q->nid = nid;
1506}
1507
1508/* clean up the all conflicting obsolete streams */
1509static void purify_inactive_streams(struct hda_codec *codec)
1510{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001511 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001512 int i;
1513
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001514 list_for_each_entry(c, &codec->bus->codec_list, list) {
1515 for (i = 0; i < c->cvt_setups.used; i++) {
1516 struct hda_cvt_setup *p;
1517 p = snd_array_elem(&c->cvt_setups, i);
1518 if (p->dirty)
1519 really_cleanup_stream(c, p);
1520 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001521 }
1522}
1523
Takashi Iwai2a439522011-07-26 09:52:50 +02001524#ifdef CONFIG_PM
Takashi Iwaieb541332010-08-06 13:48:11 +02001525/* clean up all streams; called from suspend */
1526static void hda_cleanup_all_streams(struct hda_codec *codec)
1527{
1528 int i;
1529
1530 for (i = 0; i < codec->cvt_setups.used; i++) {
1531 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1532 if (p->stream_tag)
1533 really_cleanup_stream(codec, p);
1534 }
1535}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001536#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001537
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538/*
1539 * amp access functions
1540 */
1541
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001542/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001543#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001544#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001545#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1546#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001548#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
1550/* initialize the hash table */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001551static void /*__devinit*/ init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001552 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553{
Takashi Iwai01751f52007-08-10 16:59:39 +02001554 memset(cache, 0, sizeof(*cache));
1555 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001556 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001557}
1558
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001559static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001560{
Takashi Iwai603c4012008-07-30 15:01:44 +02001561 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562}
1563
1564/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001565static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566{
Takashi Iwai01751f52007-08-10 16:59:39 +02001567 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1568 u16 cur = cache->hash[idx];
1569 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
1571 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001572 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 if (info->key == key)
1574 return info;
1575 cur = info->next;
1576 }
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001577 return NULL;
1578}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001580/* query the hash. allocate an entry if not found. */
1581static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1582 u32 key)
1583{
1584 struct hda_cache_head *info = get_hash(cache, key);
1585 if (!info) {
1586 u16 idx, cur;
1587 /* add a new hash entry */
1588 info = snd_array_new(&cache->buf);
1589 if (!info)
1590 return NULL;
1591 cur = snd_array_index(&cache->buf, info);
1592 info->key = key;
1593 info->val = 0;
1594 idx = key % (u16)ARRAY_SIZE(cache->hash);
1595 info->next = cache->hash[idx];
1596 cache->hash[idx] = cur;
1597 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 return info;
1599}
1600
Takashi Iwai01751f52007-08-10 16:59:39 +02001601/* query and allocate an amp hash entry */
1602static inline struct hda_amp_info *
1603get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1604{
1605 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1606}
1607
Takashi Iwaid5191e52009-11-16 14:58:17 +01001608/**
1609 * query_amp_caps - query AMP capabilities
1610 * @codec: the HD-auio codec
1611 * @nid: the NID to query
1612 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1613 *
1614 * Query AMP capabilities for the given widget and direction.
1615 * Returns the obtained capability bits.
1616 *
1617 * When cap bits have been already read, this doesn't read again but
1618 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001620u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001622 struct hda_amp_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
Takashi Iwai0ba21762007-04-16 11:29:14 +02001624 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
1625 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 return 0;
Takashi Iwai01751f52007-08-10 16:59:39 +02001627 if (!(info->head.val & INFO_AMP_CAPS)) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001628 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 nid = codec->afg;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001630 info->amp_caps = snd_hda_param_read(codec, nid,
1631 direction == HDA_OUTPUT ?
1632 AC_PAR_AMP_OUT_CAP :
1633 AC_PAR_AMP_IN_CAP);
Takashi Iwaib75e53f2007-05-10 16:56:09 +02001634 if (info->amp_caps)
Takashi Iwai01751f52007-08-10 16:59:39 +02001635 info->head.val |= INFO_AMP_CAPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 }
1637 return info->amp_caps;
1638}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001639EXPORT_SYMBOL_HDA(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
Takashi Iwaid5191e52009-11-16 14:58:17 +01001641/**
1642 * snd_hda_override_amp_caps - Override the AMP capabilities
1643 * @codec: the CODEC to clean up
1644 * @nid: the NID to clean up
1645 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1646 * @caps: the capability bits to set
1647 *
1648 * Override the cached AMP caps bits value by the given one.
1649 * This function is useful if the driver needs to adjust the AMP ranges,
1650 * e.g. limit to 0dB, etc.
1651 *
1652 * Returns zero if successful or a negative error code.
1653 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001654int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1655 unsigned int caps)
1656{
1657 struct hda_amp_info *info;
1658
1659 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
1660 if (!info)
1661 return -EINVAL;
1662 info->amp_caps = caps;
Takashi Iwai01751f52007-08-10 16:59:39 +02001663 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai897cc182007-05-29 19:01:37 +02001664 return 0;
1665}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001666EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001667
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001668static unsigned int
1669query_caps_hash(struct hda_codec *codec, hda_nid_t nid, u32 key,
1670 unsigned int (*func)(struct hda_codec *, hda_nid_t))
Takashi Iwai1327a322009-03-23 13:07:47 +01001671{
1672 struct hda_amp_info *info;
1673
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001674 info = get_alloc_amp_hash(codec, key);
Takashi Iwai1327a322009-03-23 13:07:47 +01001675 if (!info)
1676 return 0;
1677 if (!info->head.val) {
Takashi Iwai1327a322009-03-23 13:07:47 +01001678 info->head.val |= INFO_AMP_CAPS;
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001679 info->amp_caps = func(codec, nid);
Takashi Iwai1327a322009-03-23 13:07:47 +01001680 }
1681 return info->amp_caps;
1682}
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001683
1684static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid)
1685{
1686 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1687}
1688
Takashi Iwaid5191e52009-11-16 14:58:17 +01001689/**
1690 * snd_hda_query_pin_caps - Query PIN capabilities
1691 * @codec: the HD-auio codec
1692 * @nid: the NID to query
1693 *
1694 * Query PIN capabilities for the given widget.
1695 * Returns the obtained capability bits.
1696 *
1697 * When cap bits have been already read, this doesn't read again but
1698 * returns the cached value.
1699 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001700u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1701{
1702 return query_caps_hash(codec, nid, HDA_HASH_PINCAP_KEY(nid),
1703 read_pin_cap);
1704}
Takashi Iwai1327a322009-03-23 13:07:47 +01001705EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1706
Wu Fengguang864f92b2009-11-18 12:38:02 +08001707/**
Takashi Iwaif57c2562011-08-15 12:49:07 +02001708 * snd_hda_override_pin_caps - Override the pin capabilities
1709 * @codec: the CODEC
1710 * @nid: the NID to override
1711 * @caps: the capability bits to set
1712 *
1713 * Override the cached PIN capabilitiy bits value by the given one.
1714 *
1715 * Returns zero if successful or a negative error code.
1716 */
1717int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
1718 unsigned int caps)
1719{
1720 struct hda_amp_info *info;
1721 info = get_alloc_amp_hash(codec, HDA_HASH_PINCAP_KEY(nid));
1722 if (!info)
1723 return -ENOMEM;
1724 info->amp_caps = caps;
1725 info->head.val |= INFO_AMP_CAPS;
1726 return 0;
1727}
1728EXPORT_SYMBOL_HDA(snd_hda_override_pin_caps);
1729
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730/*
1731 * read the current volume to info
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001732 * if the cache exists, read the cache value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001734static unsigned int get_vol_mute(struct hda_codec *codec,
1735 struct hda_amp_info *info, hda_nid_t nid,
1736 int ch, int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737{
1738 u32 val, parm;
1739
Takashi Iwai01751f52007-08-10 16:59:39 +02001740 if (info->head.val & INFO_AMP_VOL(ch))
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001741 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
1743 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1744 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1745 parm |= index;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001746 val = snd_hda_codec_read(codec, nid, 0,
1747 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 info->vol[ch] = val & 0xff;
Takashi Iwai01751f52007-08-10 16:59:39 +02001749 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001750 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751}
1752
1753/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001754 * write the current volume in info to the h/w and update the cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001756static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001757 hda_nid_t nid, int ch, int direction, int index,
1758 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759{
1760 u32 parm;
1761
1762 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1763 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1764 parm |= index << AC_AMP_SET_INDEX_SHIFT;
Takashi Iwai38681372012-02-27 15:00:58 +01001765 if ((val & HDA_AMP_MUTE) && !(info->amp_caps & AC_AMPCAP_MUTE) &&
1766 (info->amp_caps & AC_AMPCAP_MIN_MUTE))
1767 ; /* set the zero value as a fake mute */
1768 else
1769 parm |= val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001771 info->vol[ch] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772}
1773
Takashi Iwaid5191e52009-11-16 14:58:17 +01001774/**
1775 * snd_hda_codec_amp_read - Read AMP value
1776 * @codec: HD-audio codec
1777 * @nid: NID to read the AMP value
1778 * @ch: channel (left=0 or right=1)
1779 * @direction: #HDA_INPUT or #HDA_OUTPUT
1780 * @index: the index value (only for input direction)
1781 *
1782 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 */
Takashi Iwai834be882006-03-01 14:16:17 +01001784int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1785 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001787 struct hda_amp_info *info;
1788 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1789 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001791 return get_vol_mute(codec, info, nid, ch, direction, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001793EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794
Takashi Iwaid5191e52009-11-16 14:58:17 +01001795/**
1796 * snd_hda_codec_amp_update - update the AMP value
1797 * @codec: HD-audio codec
1798 * @nid: NID to read the AMP value
1799 * @ch: channel (left=0 or right=1)
1800 * @direction: #HDA_INPUT or #HDA_OUTPUT
1801 * @idx: the index value (only for input direction)
1802 * @mask: bit mask to set
1803 * @val: the bits value to set
1804 *
1805 * Update the AMP value with a bit mask.
1806 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001807 */
Takashi Iwai834be882006-03-01 14:16:17 +01001808int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1809 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001811 struct hda_amp_info *info;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001812
Takashi Iwai0ba21762007-04-16 11:29:14 +02001813 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
1814 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 return 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001816 if (snd_BUG_ON(mask & ~0xff))
1817 mask &= 0xff;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001818 val &= mask;
1819 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02001820 if (info->vol[ch] == val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001822 put_vol_mute(codec, info, nid, ch, direction, idx, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 return 1;
1824}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001825EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
Takashi Iwaid5191e52009-11-16 14:58:17 +01001827/**
1828 * snd_hda_codec_amp_stereo - update the AMP stereo values
1829 * @codec: HD-audio codec
1830 * @nid: NID to read the AMP value
1831 * @direction: #HDA_INPUT or #HDA_OUTPUT
1832 * @idx: the index value (only for input direction)
1833 * @mask: bit mask to set
1834 * @val: the bits value to set
1835 *
1836 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1837 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02001838 */
1839int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1840 int direction, int idx, int mask, int val)
1841{
1842 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001843
1844 if (snd_BUG_ON(mask & ~0xff))
1845 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02001846 for (ch = 0; ch < 2; ch++)
1847 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1848 idx, mask, val);
1849 return ret;
1850}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001851EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001852
Takashi Iwai2a439522011-07-26 09:52:50 +02001853#ifdef CONFIG_PM
Takashi Iwaid5191e52009-11-16 14:58:17 +01001854/**
1855 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
1856 * @codec: HD-audio codec
1857 *
1858 * Resume the all amp commands from the cache.
1859 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001860void snd_hda_codec_resume_amp(struct hda_codec *codec)
1861{
Takashi Iwai603c4012008-07-30 15:01:44 +02001862 struct hda_amp_info *buffer = codec->amp_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001863 int i;
1864
Takashi Iwai603c4012008-07-30 15:01:44 +02001865 for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001866 u32 key = buffer->head.key;
1867 hda_nid_t nid;
1868 unsigned int idx, dir, ch;
1869 if (!key)
1870 continue;
1871 nid = key & 0xff;
1872 idx = (key >> 16) & 0xff;
1873 dir = (key >> 24) & 0xff;
1874 for (ch = 0; ch < 2; ch++) {
1875 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
1876 continue;
1877 put_vol_mute(codec, buffer, nid, ch, dir, idx,
1878 buffer->vol[ch]);
1879 }
1880 }
1881}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001882EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
Takashi Iwai2a439522011-07-26 09:52:50 +02001883#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001885static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1886 unsigned int ofs)
1887{
1888 u32 caps = query_amp_caps(codec, nid, dir);
1889 /* get num steps */
1890 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1891 if (ofs < caps)
1892 caps -= ofs;
1893 return caps;
1894}
1895
Takashi Iwaid5191e52009-11-16 14:58:17 +01001896/**
1897 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
1898 *
1899 * The control element is supposed to have the private_value field
1900 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1901 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001902int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1903 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904{
1905 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1906 u16 nid = get_amp_nid(kcontrol);
1907 u8 chs = get_amp_channels(kcontrol);
1908 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001909 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001911 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1912 uinfo->count = chs == 3 ? 2 : 1;
1913 uinfo->value.integer.min = 0;
1914 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
1915 if (!uinfo->value.integer.max) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001916 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01001917 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
1918 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 return -EINVAL;
1920 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 return 0;
1922}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001923EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001925
1926static inline unsigned int
1927read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1928 int ch, int dir, int idx, unsigned int ofs)
1929{
1930 unsigned int val;
1931 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1932 val &= HDA_AMP_VOLMASK;
1933 if (val >= ofs)
1934 val -= ofs;
1935 else
1936 val = 0;
1937 return val;
1938}
1939
1940static inline int
1941update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1942 int ch, int dir, int idx, unsigned int ofs,
1943 unsigned int val)
1944{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001945 unsigned int maxval;
1946
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001947 if (val > 0)
1948 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02001949 /* ofs = 0: raw max value */
1950 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001951 if (val > maxval)
1952 val = maxval;
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001953 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
1954 HDA_AMP_VOLMASK, val);
1955}
1956
Takashi Iwaid5191e52009-11-16 14:58:17 +01001957/**
1958 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
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_get(struct snd_kcontrol *kcontrol,
1964 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965{
1966 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1967 hda_nid_t nid = get_amp_nid(kcontrol);
1968 int chs = get_amp_channels(kcontrol);
1969 int dir = get_amp_direction(kcontrol);
1970 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001971 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 long *valp = ucontrol->value.integer.value;
1973
1974 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001975 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001977 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 return 0;
1979}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001980EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
Takashi Iwaid5191e52009-11-16 14:58:17 +01001982/**
1983 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
1984 *
1985 * The control element is supposed to have the private_value field
1986 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1987 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001988int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1989 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990{
1991 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1992 hda_nid_t nid = get_amp_nid(kcontrol);
1993 int chs = get_amp_channels(kcontrol);
1994 int dir = get_amp_direction(kcontrol);
1995 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001996 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 long *valp = ucontrol->value.integer.value;
1998 int change = 0;
1999
Takashi Iwaicb53c622007-08-10 17:21:45 +02002000 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002001 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002002 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002003 valp++;
2004 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002005 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002006 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002007 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 return change;
2009}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002010EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011
Takashi Iwaid5191e52009-11-16 14:58:17 +01002012/**
2013 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2014 *
2015 * The control element is supposed to have the private_value field
2016 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2017 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002018int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2019 unsigned int size, unsigned int __user *_tlv)
2020{
2021 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2022 hda_nid_t nid = get_amp_nid(kcontrol);
2023 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002024 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02002025 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002026 u32 caps, val1, val2;
2027
2028 if (size < 4 * sizeof(unsigned int))
2029 return -ENOMEM;
2030 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002031 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2032 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002033 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002034 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002035 val1 = ((int)val1) * ((int)val2);
Takashi Iwai38681372012-02-27 15:00:58 +01002036 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwaic08d9162010-10-17 10:40:53 +02002037 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002038 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2039 return -EFAULT;
2040 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2041 return -EFAULT;
2042 if (put_user(val1, _tlv + 2))
2043 return -EFAULT;
2044 if (put_user(val2, _tlv + 3))
2045 return -EFAULT;
2046 return 0;
2047}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002048EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002049
Takashi Iwaid5191e52009-11-16 14:58:17 +01002050/**
2051 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2052 * @codec: HD-audio codec
2053 * @nid: NID of a reference widget
2054 * @dir: #HDA_INPUT or #HDA_OUTPUT
2055 * @tlv: TLV data to be stored, at least 4 elements
2056 *
2057 * Set (static) TLV data for a virtual master volume using the AMP caps
2058 * obtained from the reference NID.
2059 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01002060 */
2061void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2062 unsigned int *tlv)
2063{
2064 u32 caps;
2065 int nums, step;
2066
2067 caps = query_amp_caps(codec, nid, dir);
2068 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2069 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2070 step = (step + 1) * 25;
2071 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2072 tlv[1] = 2 * sizeof(unsigned int);
2073 tlv[2] = -nums * step;
2074 tlv[3] = step;
2075}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002076EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002077
2078/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01002079static struct snd_kcontrol *
2080_snd_hda_find_mixer_ctl(struct hda_codec *codec,
2081 const char *name, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002082{
2083 struct snd_ctl_elem_id id;
2084 memset(&id, 0, sizeof(id));
2085 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwai09f99702008-02-04 12:31:13 +01002086 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02002087 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2088 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002089 strcpy(id.name, name);
2090 return snd_ctl_find_id(codec->bus->card, &id);
2091}
2092
Takashi Iwaid5191e52009-11-16 14:58:17 +01002093/**
2094 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2095 * @codec: HD-audio codec
2096 * @name: ctl id name string
2097 *
2098 * Get the control element with the given id string and IFACE_MIXER.
2099 */
Takashi Iwai09f99702008-02-04 12:31:13 +01002100struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2101 const char *name)
2102{
2103 return _snd_hda_find_mixer_ctl(codec, name, 0);
2104}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002105EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01002106
Takashi Iwai1afe2062010-12-23 10:17:52 +01002107static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name)
2108{
2109 int idx;
2110 for (idx = 0; idx < 16; idx++) { /* 16 ctlrs should be large enough */
2111 if (!_snd_hda_find_mixer_ctl(codec, name, idx))
2112 return idx;
2113 }
2114 return -EBUSY;
2115}
2116
Takashi Iwaid5191e52009-11-16 14:58:17 +01002117/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002118 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01002119 * @codec: HD-audio codec
2120 * @nid: corresponding NID (optional)
2121 * @kctl: the control element to assign
2122 *
2123 * Add the given control element to an array inside the codec instance.
2124 * All control elements belonging to a codec are supposed to be added
2125 * by this function so that a proper clean-up works at the free or
2126 * reconfiguration time.
2127 *
2128 * If non-zero @nid is passed, the NID is assigned to the control element.
2129 * The assignment is shown in the codec proc file.
2130 *
2131 * snd_hda_ctl_add() checks the control subdev id field whether
2132 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002133 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2134 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01002135 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002136int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2137 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002138{
2139 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002140 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002141 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002142
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002143 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002144 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002145 if (nid == 0)
2146 nid = get_amp_nid_(kctl->private_value);
2147 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002148 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2149 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002150 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01002151 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002152 err = snd_ctl_add(codec->bus->card, kctl);
2153 if (err < 0)
2154 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002155 item = snd_array_new(&codec->mixers);
2156 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002157 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002158 item->kctl = kctl;
2159 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002160 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002161 return 0;
2162}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002163EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002164
Takashi Iwaid5191e52009-11-16 14:58:17 +01002165/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002166 * snd_hda_add_nid - Assign a NID to a control element
2167 * @codec: HD-audio codec
2168 * @nid: corresponding NID (optional)
2169 * @kctl: the control element to assign
2170 * @index: index to kctl
2171 *
2172 * Add the given control element to an array inside the codec instance.
2173 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2174 * NID:KCTL mapping - for example "Capture Source" selector.
2175 */
2176int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2177 unsigned int index, hda_nid_t nid)
2178{
2179 struct hda_nid_item *item;
2180
2181 if (nid > 0) {
2182 item = snd_array_new(&codec->nids);
2183 if (!item)
2184 return -ENOMEM;
2185 item->kctl = kctl;
2186 item->index = index;
2187 item->nid = nid;
2188 return 0;
2189 }
Takashi Iwai28d1a852010-03-15 09:05:46 +01002190 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
2191 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002192 return -EINVAL;
2193}
2194EXPORT_SYMBOL_HDA(snd_hda_add_nid);
2195
2196/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002197 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2198 * @codec: HD-audio codec
2199 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002200void snd_hda_ctls_clear(struct hda_codec *codec)
2201{
2202 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002203 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002204 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002205 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002206 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002207 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002208}
2209
Takashi Iwaia65d6292009-02-23 16:57:04 +01002210/* pseudo device locking
2211 * toggle card->shutdown to allow/disallow the device access (as a hack)
2212 */
2213static int hda_lock_devices(struct snd_card *card)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002214{
Takashi Iwaia65d6292009-02-23 16:57:04 +01002215 spin_lock(&card->files_lock);
2216 if (card->shutdown) {
2217 spin_unlock(&card->files_lock);
2218 return -EINVAL;
2219 }
2220 card->shutdown = 1;
2221 spin_unlock(&card->files_lock);
2222 return 0;
2223}
2224
2225static void hda_unlock_devices(struct snd_card *card)
2226{
2227 spin_lock(&card->files_lock);
2228 card->shutdown = 0;
2229 spin_unlock(&card->files_lock);
2230}
2231
Takashi Iwaid5191e52009-11-16 14:58:17 +01002232/**
2233 * snd_hda_codec_reset - Clear all objects assigned to the codec
2234 * @codec: HD-audio codec
2235 *
2236 * This frees the all PCM and control elements assigned to the codec, and
2237 * clears the caches and restores the pin default configurations.
2238 *
2239 * When a device is being used, it returns -EBSY. If successfully freed,
2240 * returns zero.
2241 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002242int snd_hda_codec_reset(struct hda_codec *codec)
2243{
2244 struct snd_card *card = codec->bus->card;
2245 int i, pcm;
2246
2247 if (hda_lock_devices(card) < 0)
2248 return -EBUSY;
2249 /* check whether the codec isn't used by any mixer or PCM streams */
2250 if (!list_empty(&card->ctl_files)) {
2251 hda_unlock_devices(card);
2252 return -EBUSY;
2253 }
2254 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2255 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2256 if (!cpcm->pcm)
2257 continue;
2258 if (cpcm->pcm->streams[0].substream_opened ||
2259 cpcm->pcm->streams[1].substream_opened) {
2260 hda_unlock_devices(card);
2261 return -EBUSY;
2262 }
2263 }
2264
2265 /* OK, let it free */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002266
2267#ifdef CONFIG_SND_HDA_POWER_SAVE
Takashi Iwaia2d96e72012-05-09 12:36:22 +02002268 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwai339876d2012-05-08 16:57:12 +02002269 codec->power_on = 0;
2270 codec->power_transition = 0;
2271 codec->power_jiffies = jiffies;
Takashi Iwai6acaed32009-01-12 10:09:24 +01002272 flush_workqueue(codec->bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002273#endif
2274 snd_hda_ctls_clear(codec);
2275 /* relase PCMs */
2276 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002277 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01002278 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002279 clear_bit(codec->pcm_info[i].device,
2280 codec->bus->pcm_dev_bits);
2281 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002282 }
2283 if (codec->patch_ops.free)
2284 codec->patch_ops.free(codec);
Takashi Iwai1835a0f2011-10-27 22:12:46 +02002285 snd_hda_jack_tbl_clear(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01002286 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002287 codec->spec = NULL;
2288 free_hda_cache(&codec->amp_cache);
2289 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01002290 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2291 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01002292 /* free only driver_pins so that init_pins + user_pins are restored */
2293 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01002294 restore_pincfgs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002295 codec->num_pcms = 0;
2296 codec->pcm_info = NULL;
2297 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01002298 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
2299 codec->slave_dig_outs = NULL;
2300 codec->spdif_status_reset = 0;
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002301 module_put(codec->owner);
2302 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002303
2304 /* allow device access again */
2305 hda_unlock_devices(card);
2306 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002307}
2308
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002309typedef int (*map_slave_func_t)(void *, struct snd_kcontrol *);
2310
2311/* apply the function to all matching slave ctls in the mixer list */
2312static int map_slaves(struct hda_codec *codec, const char * const *slaves,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002313 const char *suffix, map_slave_func_t func, void *data)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002314{
2315 struct hda_nid_item *items;
2316 const char * const *s;
2317 int i, err;
2318
2319 items = codec->mixers.list;
2320 for (i = 0; i < codec->mixers.used; i++) {
2321 struct snd_kcontrol *sctl = items[i].kctl;
2322 if (!sctl || !sctl->id.name ||
2323 sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
2324 continue;
2325 for (s = slaves; *s; s++) {
Takashi Iwai9322ca52012-02-03 14:28:01 +01002326 char tmpname[sizeof(sctl->id.name)];
2327 const char *name = *s;
2328 if (suffix) {
2329 snprintf(tmpname, sizeof(tmpname), "%s %s",
2330 name, suffix);
2331 name = tmpname;
2332 }
Takashi Iwai9322ca52012-02-03 14:28:01 +01002333 if (!strcmp(sctl->id.name, name)) {
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002334 err = func(data, sctl);
2335 if (err)
2336 return err;
2337 break;
2338 }
2339 }
2340 }
2341 return 0;
2342}
2343
2344static int check_slave_present(void *data, struct snd_kcontrol *sctl)
2345{
2346 return 1;
2347}
2348
Takashi Iwai18478e82012-03-09 17:51:10 +01002349/* guess the value corresponding to 0dB */
2350static int get_kctl_0dB_offset(struct snd_kcontrol *kctl)
2351{
2352 int _tlv[4];
2353 const int *tlv = NULL;
2354 int val = -1;
2355
2356 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2357 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2358 mm_segment_t fs = get_fs();
2359 set_fs(get_ds());
2360 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2361 tlv = _tlv;
2362 set_fs(fs);
2363 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2364 tlv = kctl->tlv.p;
2365 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE)
2366 val = -tlv[2] / tlv[3];
2367 return val;
2368}
2369
2370/* call kctl->put with the given value(s) */
2371static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2372{
2373 struct snd_ctl_elem_value *ucontrol;
2374 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2375 if (!ucontrol)
2376 return -ENOMEM;
2377 ucontrol->value.integer.value[0] = val;
2378 ucontrol->value.integer.value[1] = val;
2379 kctl->put(kctl, ucontrol);
2380 kfree(ucontrol);
2381 return 0;
2382}
2383
2384/* initialize the slave volume with 0dB */
2385static int init_slave_0dB(void *data, struct snd_kcontrol *slave)
2386{
2387 int offset = get_kctl_0dB_offset(slave);
2388 if (offset > 0)
2389 put_kctl_with_value(slave, offset);
2390 return 0;
2391}
2392
2393/* unmute the slave */
2394static int init_slave_unmute(void *data, struct snd_kcontrol *slave)
2395{
2396 return put_kctl_with_value(slave, 1);
2397}
2398
Takashi Iwaid5191e52009-11-16 14:58:17 +01002399/**
2400 * snd_hda_add_vmaster - create a virtual master control and add slaves
2401 * @codec: HD-audio codec
2402 * @name: vmaster control name
2403 * @tlv: TLV data (optional)
2404 * @slaves: slave control names (optional)
Takashi Iwai9322ca52012-02-03 14:28:01 +01002405 * @suffix: suffix string to each slave name (optional)
Takashi Iwai18478e82012-03-09 17:51:10 +01002406 * @init_slave_vol: initialize slaves to unmute/0dB
Takashi Iwai29e58532012-03-12 12:25:03 +01002407 * @ctl_ret: store the vmaster kcontrol in return
Takashi Iwaid5191e52009-11-16 14:58:17 +01002408 *
2409 * Create a virtual master control with the given name. The TLV data
2410 * must be either NULL or a valid data.
2411 *
2412 * @slaves is a NULL-terminated array of strings, each of which is a
2413 * slave control name. All controls with these names are assigned to
2414 * the new virtual master control.
2415 *
2416 * This function returns zero if successful or a negative error code.
2417 */
Takashi Iwai18478e82012-03-09 17:51:10 +01002418int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002419 unsigned int *tlv, const char * const *slaves,
Takashi Iwai29e58532012-03-12 12:25:03 +01002420 const char *suffix, bool init_slave_vol,
2421 struct snd_kcontrol **ctl_ret)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002422{
2423 struct snd_kcontrol *kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002424 int err;
2425
Takashi Iwai29e58532012-03-12 12:25:03 +01002426 if (ctl_ret)
2427 *ctl_ret = NULL;
2428
Takashi Iwai9322ca52012-02-03 14:28:01 +01002429 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002430 if (err != 1) {
Takashi Iwai2f085542008-02-22 18:43:50 +01002431 snd_printdd("No slave found for %s\n", name);
2432 return 0;
2433 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002434 kctl = snd_ctl_make_virtual_master(name, tlv);
2435 if (!kctl)
2436 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002437 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002438 if (err < 0)
2439 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002440
Takashi Iwai9322ca52012-02-03 14:28:01 +01002441 err = map_slaves(codec, slaves, suffix,
2442 (map_slave_func_t)snd_ctl_add_slave, kctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002443 if (err < 0)
2444 return err;
Takashi Iwai18478e82012-03-09 17:51:10 +01002445
2446 /* init with master mute & zero volume */
2447 put_kctl_with_value(kctl, 0);
2448 if (init_slave_vol)
2449 map_slaves(codec, slaves, suffix,
2450 tlv ? init_slave_0dB : init_slave_unmute, kctl);
2451
Takashi Iwai29e58532012-03-12 12:25:03 +01002452 if (ctl_ret)
2453 *ctl_ret = kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002454 return 0;
2455}
Takashi Iwai18478e82012-03-09 17:51:10 +01002456EXPORT_SYMBOL_HDA(__snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002457
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002458/*
2459 * mute-LED control using vmaster
2460 */
2461static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2462 struct snd_ctl_elem_info *uinfo)
2463{
2464 static const char * const texts[] = {
2465 "Off", "On", "Follow Master"
2466 };
2467 unsigned int index;
2468
2469 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2470 uinfo->count = 1;
2471 uinfo->value.enumerated.items = 3;
2472 index = uinfo->value.enumerated.item;
2473 if (index >= 3)
2474 index = 2;
2475 strcpy(uinfo->value.enumerated.name, texts[index]);
2476 return 0;
2477}
2478
2479static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2480 struct snd_ctl_elem_value *ucontrol)
2481{
2482 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2483 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2484 return 0;
2485}
2486
2487static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2488 struct snd_ctl_elem_value *ucontrol)
2489{
2490 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2491 unsigned int old_mode = hook->mute_mode;
2492
2493 hook->mute_mode = ucontrol->value.enumerated.item[0];
2494 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2495 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2496 if (old_mode == hook->mute_mode)
2497 return 0;
2498 snd_hda_sync_vmaster_hook(hook);
2499 return 1;
2500}
2501
2502static struct snd_kcontrol_new vmaster_mute_mode = {
2503 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2504 .name = "Mute-LED Mode",
2505 .info = vmaster_mute_mode_info,
2506 .get = vmaster_mute_mode_get,
2507 .put = vmaster_mute_mode_put,
2508};
2509
2510/*
2511 * Add a mute-LED hook with the given vmaster switch kctl
2512 * "Mute-LED Mode" control is automatically created and associated with
2513 * the given hook.
2514 */
2515int snd_hda_add_vmaster_hook(struct hda_codec *codec,
Takashi Iwaif29735c2012-03-13 07:55:10 +01002516 struct hda_vmaster_mute_hook *hook,
2517 bool expose_enum_ctl)
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002518{
2519 struct snd_kcontrol *kctl;
2520
2521 if (!hook->hook || !hook->sw_kctl)
2522 return 0;
2523 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
2524 hook->codec = codec;
2525 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
Takashi Iwaif29735c2012-03-13 07:55:10 +01002526 if (!expose_enum_ctl)
2527 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002528 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2529 if (!kctl)
2530 return -ENOMEM;
2531 return snd_hda_ctl_add(codec, 0, kctl);
2532}
2533EXPORT_SYMBOL_HDA(snd_hda_add_vmaster_hook);
2534
2535/*
2536 * Call the hook with the current value for synchronization
2537 * Should be called in init callback
2538 */
2539void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2540{
2541 if (!hook->hook || !hook->codec)
2542 return;
2543 switch (hook->mute_mode) {
2544 case HDA_VMUTE_FOLLOW_MASTER:
2545 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2546 break;
2547 default:
2548 hook->hook(hook->codec, hook->mute_mode);
2549 break;
2550 }
2551}
2552EXPORT_SYMBOL_HDA(snd_hda_sync_vmaster_hook);
2553
2554
Takashi Iwaid5191e52009-11-16 14:58:17 +01002555/**
2556 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2557 *
2558 * The control element is supposed to have the private_value field
2559 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2560 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002561int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2562 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563{
2564 int chs = get_amp_channels(kcontrol);
2565
2566 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2567 uinfo->count = chs == 3 ? 2 : 1;
2568 uinfo->value.integer.min = 0;
2569 uinfo->value.integer.max = 1;
2570 return 0;
2571}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002572EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573
Takashi Iwaid5191e52009-11-16 14:58:17 +01002574/**
2575 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2576 *
2577 * The control element is supposed to have the private_value field
2578 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2579 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002580int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2581 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582{
2583 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2584 hda_nid_t nid = get_amp_nid(kcontrol);
2585 int chs = get_amp_channels(kcontrol);
2586 int dir = get_amp_direction(kcontrol);
2587 int idx = get_amp_index(kcontrol);
2588 long *valp = ucontrol->value.integer.value;
2589
2590 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002591 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002592 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002594 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002595 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 return 0;
2597}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002598EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599
Takashi Iwaid5191e52009-11-16 14:58:17 +01002600/**
2601 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2602 *
2603 * The control element is supposed to have the private_value field
2604 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2605 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002606int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2607 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608{
2609 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2610 hda_nid_t nid = get_amp_nid(kcontrol);
2611 int chs = get_amp_channels(kcontrol);
2612 int dir = get_amp_direction(kcontrol);
2613 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 long *valp = ucontrol->value.integer.value;
2615 int change = 0;
2616
Takashi Iwaicb53c622007-08-10 17:21:45 +02002617 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002618 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002619 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002620 HDA_AMP_MUTE,
2621 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002622 valp++;
2623 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002624 if (chs & 2)
2625 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002626 HDA_AMP_MUTE,
2627 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002628 hda_call_check_power_status(codec, nid);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002629 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 return change;
2631}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002632EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633
Takashi Iwai67d634c2009-11-16 15:35:59 +01002634#ifdef CONFIG_SND_HDA_INPUT_BEEP
Takashi Iwaid5191e52009-11-16 14:58:17 +01002635/**
2636 * snd_hda_mixer_amp_switch_put_beep - Put callback for a beep AMP switch
2637 *
2638 * This function calls snd_hda_enable_beep_device(), which behaves differently
2639 * depending on beep_mode option.
2640 */
Jaroslav Kysela123c07a2009-10-21 14:48:23 +02002641int snd_hda_mixer_amp_switch_put_beep(struct snd_kcontrol *kcontrol,
2642 struct snd_ctl_elem_value *ucontrol)
2643{
2644 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2645 long *valp = ucontrol->value.integer.value;
2646
2647 snd_hda_enable_beep_device(codec, *valp);
2648 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2649}
2650EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put_beep);
Takashi Iwai67d634c2009-11-16 15:35:59 +01002651#endif /* CONFIG_SND_HDA_INPUT_BEEP */
Jaroslav Kysela123c07a2009-10-21 14:48:23 +02002652
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653/*
Takashi Iwai985be542005-11-02 18:26:49 +01002654 * bound volume controls
2655 *
2656 * bind multiple volumes (# indices, from 0)
2657 */
2658
2659#define AMP_VAL_IDX_SHIFT 19
2660#define AMP_VAL_IDX_MASK (0x0f<<19)
2661
Takashi Iwaid5191e52009-11-16 14:58:17 +01002662/**
2663 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2664 *
2665 * The control element is supposed to have the private_value field
2666 * set up via HDA_BIND_MUTE*() macros.
2667 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002668int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2669 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002670{
2671 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2672 unsigned long pval;
2673 int err;
2674
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002675 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002676 pval = kcontrol->private_value;
2677 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2678 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2679 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002680 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002681 return err;
2682}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002683EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002684
Takashi Iwaid5191e52009-11-16 14:58:17 +01002685/**
2686 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2687 *
2688 * The control element is supposed to have the private_value field
2689 * set up via HDA_BIND_MUTE*() macros.
2690 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002691int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2692 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002693{
2694 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2695 unsigned long pval;
2696 int i, indices, err = 0, change = 0;
2697
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002698 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002699 pval = kcontrol->private_value;
2700 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2701 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002702 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2703 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002704 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2705 if (err < 0)
2706 break;
2707 change |= err;
2708 }
2709 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002710 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002711 return err < 0 ? err : change;
2712}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002713EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002714
Takashi Iwaid5191e52009-11-16 14:58:17 +01002715/**
2716 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2717 *
2718 * The control element is supposed to have the private_value field
2719 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002720 */
2721int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2722 struct snd_ctl_elem_info *uinfo)
2723{
2724 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2725 struct hda_bind_ctls *c;
2726 int err;
2727
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002728 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002729 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002730 kcontrol->private_value = *c->values;
2731 err = c->ops->info(kcontrol, uinfo);
2732 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002733 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002734 return err;
2735}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002736EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002737
Takashi Iwaid5191e52009-11-16 14:58:17 +01002738/**
2739 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2740 *
2741 * The control element is supposed to have the private_value field
2742 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2743 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002744int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2745 struct snd_ctl_elem_value *ucontrol)
2746{
2747 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2748 struct hda_bind_ctls *c;
2749 int err;
2750
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002751 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002752 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002753 kcontrol->private_value = *c->values;
2754 err = c->ops->get(kcontrol, ucontrol);
2755 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002756 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002757 return err;
2758}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002759EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002760
Takashi Iwaid5191e52009-11-16 14:58:17 +01002761/**
2762 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2763 *
2764 * The control element is supposed to have the private_value field
2765 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2766 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002767int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2768 struct snd_ctl_elem_value *ucontrol)
2769{
2770 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2771 struct hda_bind_ctls *c;
2772 unsigned long *vals;
2773 int err = 0, change = 0;
2774
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002775 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002776 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002777 for (vals = c->values; *vals; vals++) {
2778 kcontrol->private_value = *vals;
2779 err = c->ops->put(kcontrol, ucontrol);
2780 if (err < 0)
2781 break;
2782 change |= err;
2783 }
2784 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002785 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002786 return err < 0 ? err : change;
2787}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002788EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02002789
Takashi Iwaid5191e52009-11-16 14:58:17 +01002790/**
2791 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2792 *
2793 * The control element is supposed to have the private_value field
2794 * set up via HDA_BIND_VOL() macro.
2795 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002796int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2797 unsigned int size, unsigned int __user *tlv)
2798{
2799 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2800 struct hda_bind_ctls *c;
2801 int err;
2802
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002803 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002804 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002805 kcontrol->private_value = *c->values;
2806 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2807 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002808 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002809 return err;
2810}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002811EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02002812
2813struct hda_ctl_ops snd_hda_bind_vol = {
2814 .info = snd_hda_mixer_amp_volume_info,
2815 .get = snd_hda_mixer_amp_volume_get,
2816 .put = snd_hda_mixer_amp_volume_put,
2817 .tlv = snd_hda_mixer_amp_tlv
2818};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002819EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02002820
2821struct hda_ctl_ops snd_hda_bind_sw = {
2822 .info = snd_hda_mixer_amp_switch_info,
2823 .get = snd_hda_mixer_amp_switch_get,
2824 .put = snd_hda_mixer_amp_switch_put,
2825 .tlv = snd_hda_mixer_amp_tlv
2826};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002827EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02002828
2829/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 * SPDIF out controls
2831 */
2832
Takashi Iwai0ba21762007-04-16 11:29:14 +02002833static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2834 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835{
2836 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2837 uinfo->count = 1;
2838 return 0;
2839}
2840
Takashi Iwai0ba21762007-04-16 11:29:14 +02002841static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2842 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843{
2844 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2845 IEC958_AES0_NONAUDIO |
2846 IEC958_AES0_CON_EMPHASIS_5015 |
2847 IEC958_AES0_CON_NOT_COPYRIGHT;
2848 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2849 IEC958_AES1_CON_ORIGINAL;
2850 return 0;
2851}
2852
Takashi Iwai0ba21762007-04-16 11:29:14 +02002853static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2854 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855{
2856 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2857 IEC958_AES0_NONAUDIO |
2858 IEC958_AES0_PRO_EMPHASIS_5015;
2859 return 0;
2860}
2861
Takashi Iwai0ba21762007-04-16 11:29:14 +02002862static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2863 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864{
2865 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002866 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002867 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002869 mutex_lock(&codec->spdif_mutex);
2870 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c9359762011-06-01 11:14:17 -06002871 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
2872 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
2873 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
2874 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002875 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876
2877 return 0;
2878}
2879
2880/* convert from SPDIF status bits to HDA SPDIF bits
2881 * bit 0 (DigEn) is always set zero (to be filled later)
2882 */
2883static unsigned short convert_from_spdif_status(unsigned int sbits)
2884{
2885 unsigned short val = 0;
2886
2887 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002888 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002890 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002892 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2893 IEC958_AES0_PRO_EMPHASIS_5015)
2894 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002896 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2897 IEC958_AES0_CON_EMPHASIS_5015)
2898 val |= AC_DIG1_EMPHASIS;
2899 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2900 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02002902 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2904 }
2905 return val;
2906}
2907
2908/* convert to SPDIF status bits from HDA SPDIF bits
2909 */
2910static unsigned int convert_to_spdif_status(unsigned short val)
2911{
2912 unsigned int sbits = 0;
2913
Takashi Iwai0ba21762007-04-16 11:29:14 +02002914 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002916 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 sbits |= IEC958_AES0_PROFESSIONAL;
2918 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002919 if (sbits & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2921 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002922 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002924 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002926 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2928 sbits |= val & (0x7f << 8);
2929 }
2930 return sbits;
2931}
2932
Takashi Iwai2f728532008-09-25 16:32:41 +02002933/* set digital convert verbs both for the given NID and its slaves */
2934static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
2935 int verb, int val)
2936{
Takashi Iwaidda14412011-05-02 11:29:30 +02002937 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02002938
Takashi Iwai9e976972008-11-25 08:17:20 +01002939 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002940 d = codec->slave_dig_outs;
2941 if (!d)
2942 return;
2943 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01002944 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002945}
2946
2947static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
2948 int dig1, int dig2)
2949{
2950 if (dig1 != -1)
2951 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
2952 if (dig2 != -1)
2953 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
2954}
2955
Takashi Iwai0ba21762007-04-16 11:29:14 +02002956static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
2957 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958{
2959 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002960 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002961 struct hda_spdif_out *spdif;
2962 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 unsigned short val;
2964 int change;
2965
Ingo Molnar62932df2006-01-16 16:34:20 +01002966 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002967 spdif = snd_array_elem(&codec->spdif_out, idx);
2968 nid = spdif->nid;
Stephen Warren7c9359762011-06-01 11:14:17 -06002969 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
2971 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
2972 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c9359762011-06-01 11:14:17 -06002973 val = convert_from_spdif_status(spdif->status);
2974 val |= spdif->ctls & 1;
2975 change = spdif->ctls != val;
2976 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06002977 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02002978 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01002979 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 return change;
2981}
2982
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002983#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984
Takashi Iwai0ba21762007-04-16 11:29:14 +02002985static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
2986 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987{
2988 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002989 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002990 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002992 mutex_lock(&codec->spdif_mutex);
2993 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c9359762011-06-01 11:14:17 -06002994 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002995 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 return 0;
2997}
2998
Stephen Warren74b654c2011-06-01 11:14:18 -06002999static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
3000 int dig1, int dig2)
3001{
3002 set_dig_out_convert(codec, nid, dig1, dig2);
3003 /* unmute amp switch (if any) */
3004 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
3005 (dig1 & AC_DIG1_ENABLE))
3006 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
3007 HDA_AMP_MUTE, 0);
3008}
3009
Takashi Iwai0ba21762007-04-16 11:29:14 +02003010static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
3011 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012{
3013 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06003014 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003015 struct hda_spdif_out *spdif;
3016 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 unsigned short val;
3018 int change;
3019
Ingo Molnar62932df2006-01-16 16:34:20 +01003020 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003021 spdif = snd_array_elem(&codec->spdif_out, idx);
3022 nid = spdif->nid;
Stephen Warren7c9359762011-06-01 11:14:17 -06003023 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02003025 val |= AC_DIG1_ENABLE;
Stephen Warren7c9359762011-06-01 11:14:17 -06003026 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003027 spdif->ctls = val;
3028 if (change && nid != (u16)-1)
3029 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01003030 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 return change;
3032}
3033
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003034static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 {
3036 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3037 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003038 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 .info = snd_hda_spdif_mask_info,
3040 .get = snd_hda_spdif_cmask_get,
3041 },
3042 {
3043 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3044 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003045 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 .info = snd_hda_spdif_mask_info,
3047 .get = snd_hda_spdif_pmask_get,
3048 },
3049 {
3050 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003051 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 .info = snd_hda_spdif_mask_info,
3053 .get = snd_hda_spdif_default_get,
3054 .put = snd_hda_spdif_default_put,
3055 },
3056 {
3057 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003058 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 .info = snd_hda_spdif_out_switch_info,
3060 .get = snd_hda_spdif_out_switch_get,
3061 .put = snd_hda_spdif_out_switch_put,
3062 },
3063 { } /* end */
3064};
3065
3066/**
3067 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
3068 * @codec: the HDA codec
3069 * @nid: audio out widget NID
3070 *
3071 * Creates controls related with the SPDIF output.
3072 * Called from each patch supporting the SPDIF out.
3073 *
3074 * Returns 0 if successful, or a negative error code.
3075 */
Stephen Warren74b654c2011-06-01 11:14:18 -06003076int snd_hda_create_spdif_out_ctls(struct hda_codec *codec,
3077 hda_nid_t associated_nid,
3078 hda_nid_t cvt_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079{
3080 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003081 struct snd_kcontrol *kctl;
3082 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003083 int idx;
Stephen Warren7c9359762011-06-01 11:14:17 -06003084 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085
Takashi Iwai1afe2062010-12-23 10:17:52 +01003086 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch");
3087 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003088 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
3089 return -EBUSY;
3090 }
Stephen Warren7c9359762011-06-01 11:14:17 -06003091 spdif = snd_array_new(&codec->spdif_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3093 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01003094 if (!kctl)
3095 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01003096 kctl->id.index = idx;
Stephen Warren7c9359762011-06-01 11:14:17 -06003097 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06003098 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003099 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 return err;
3101 }
Stephen Warren74b654c2011-06-01 11:14:18 -06003102 spdif->nid = cvt_nid;
3103 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
Stephen Warren7c9359762011-06-01 11:14:17 -06003104 AC_VERB_GET_DIGI_CONVERT_1, 0);
3105 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 return 0;
3107}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003108EXPORT_SYMBOL_HDA(snd_hda_create_spdif_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003110/* get the hda_spdif_out entry from the given NID
3111 * call within spdif_mutex lock
3112 */
Stephen Warren7c9359762011-06-01 11:14:17 -06003113struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3114 hda_nid_t nid)
3115{
3116 int i;
3117 for (i = 0; i < codec->spdif_out.used; i++) {
3118 struct hda_spdif_out *spdif =
3119 snd_array_elem(&codec->spdif_out, i);
3120 if (spdif->nid == nid)
3121 return spdif;
3122 }
3123 return NULL;
3124}
3125EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid);
3126
Stephen Warren74b654c2011-06-01 11:14:18 -06003127void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3128{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003129 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003130
3131 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003132 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003133 spdif->nid = (u16)-1;
3134 mutex_unlock(&codec->spdif_mutex);
3135}
3136EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign);
3137
3138void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3139{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003140 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003141 unsigned short val;
3142
3143 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003144 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003145 if (spdif->nid != nid) {
3146 spdif->nid = nid;
3147 val = spdif->ctls;
3148 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3149 }
3150 mutex_unlock(&codec->spdif_mutex);
3151}
3152EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign);
3153
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154/*
Takashi Iwai9a081602008-02-12 18:37:26 +01003155 * SPDIF sharing with analog output
3156 */
3157static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3158 struct snd_ctl_elem_value *ucontrol)
3159{
3160 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3161 ucontrol->value.integer.value[0] = mout->share_spdif;
3162 return 0;
3163}
3164
3165static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3166 struct snd_ctl_elem_value *ucontrol)
3167{
3168 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3169 mout->share_spdif = !!ucontrol->value.integer.value[0];
3170 return 0;
3171}
3172
3173static struct snd_kcontrol_new spdif_share_sw = {
3174 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3175 .name = "IEC958 Default PCM Playback Switch",
3176 .info = snd_ctl_boolean_mono_info,
3177 .get = spdif_share_sw_get,
3178 .put = spdif_share_sw_put,
3179};
3180
Takashi Iwaid5191e52009-11-16 14:58:17 +01003181/**
3182 * snd_hda_create_spdif_share_sw - create Default PCM switch
3183 * @codec: the HDA codec
3184 * @mout: multi-out instance
3185 */
Takashi Iwai9a081602008-02-12 18:37:26 +01003186int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3187 struct hda_multi_out *mout)
3188{
3189 if (!mout->dig_out_nid)
3190 return 0;
3191 /* ATTENTION: here mout is passed as private_data, instead of codec */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003192 return snd_hda_ctl_add(codec, mout->dig_out_nid,
3193 snd_ctl_new1(&spdif_share_sw, mout));
Takashi Iwai9a081602008-02-12 18:37:26 +01003194}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003195EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01003196
3197/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 * SPDIF input
3199 */
3200
3201#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3202
Takashi Iwai0ba21762007-04-16 11:29:14 +02003203static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3204 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205{
3206 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3207
3208 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3209 return 0;
3210}
3211
Takashi Iwai0ba21762007-04-16 11:29:14 +02003212static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3213 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214{
3215 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3216 hda_nid_t nid = kcontrol->private_value;
3217 unsigned int val = !!ucontrol->value.integer.value[0];
3218 int change;
3219
Ingo Molnar62932df2006-01-16 16:34:20 +01003220 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003222 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003224 snd_hda_codec_write_cache(codec, nid, 0,
3225 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003227 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 return change;
3229}
3230
Takashi Iwai0ba21762007-04-16 11:29:14 +02003231static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3232 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233{
3234 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3235 hda_nid_t nid = kcontrol->private_value;
3236 unsigned short val;
3237 unsigned int sbits;
3238
Andrew Paprocki3982d172007-12-19 12:13:44 +01003239 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 sbits = convert_to_spdif_status(val);
3241 ucontrol->value.iec958.status[0] = sbits;
3242 ucontrol->value.iec958.status[1] = sbits >> 8;
3243 ucontrol->value.iec958.status[2] = sbits >> 16;
3244 ucontrol->value.iec958.status[3] = sbits >> 24;
3245 return 0;
3246}
3247
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003248static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 {
3250 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003251 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 .info = snd_hda_spdif_in_switch_info,
3253 .get = snd_hda_spdif_in_switch_get,
3254 .put = snd_hda_spdif_in_switch_put,
3255 },
3256 {
3257 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3258 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003259 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 .info = snd_hda_spdif_mask_info,
3261 .get = snd_hda_spdif_in_status_get,
3262 },
3263 { } /* end */
3264};
3265
3266/**
3267 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3268 * @codec: the HDA codec
3269 * @nid: audio in widget NID
3270 *
3271 * Creates controls related with the SPDIF input.
3272 * Called from each patch supporting the SPDIF in.
3273 *
3274 * Returns 0 if successful, or a negative error code.
3275 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003276int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277{
3278 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003279 struct snd_kcontrol *kctl;
3280 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003281 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282
Takashi Iwai1afe2062010-12-23 10:17:52 +01003283 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch");
3284 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003285 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
3286 return -EBUSY;
3287 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3289 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01003290 if (!kctl)
3291 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003293 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003294 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 return err;
3296 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003297 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01003298 snd_hda_codec_read(codec, nid, 0,
3299 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02003300 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 return 0;
3302}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003303EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304
Takashi Iwai2a439522011-07-26 09:52:50 +02003305#ifdef CONFIG_PM
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003306/*
3307 * command cache
3308 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003310/* build a 32bit cache key with the widget id and the command parameter */
3311#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3312#define get_cmd_cache_nid(key) ((key) & 0xff)
3313#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3314
3315/**
3316 * snd_hda_codec_write_cache - send a single command with caching
3317 * @codec: the HDA codec
3318 * @nid: NID to send the command
3319 * @direct: direct flag
3320 * @verb: the verb to send
3321 * @parm: the parameter for the verb
3322 *
3323 * Send a single command without waiting for response.
3324 *
3325 * Returns 0 if successful, or a negative error code.
3326 */
3327int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
3328 int direct, unsigned int verb, unsigned int parm)
3329{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003330 int err = snd_hda_codec_write(codec, nid, direct, verb, parm);
3331 struct hda_cache_head *c;
3332 u32 key;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003333
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003334 if (err < 0)
3335 return err;
3336 /* parm may contain the verb stuff for get/set amp */
3337 verb = verb | (parm >> 8);
3338 parm &= 0xff;
3339 key = build_cmd_cache_key(nid, verb);
3340 mutex_lock(&codec->bus->cmd_mutex);
3341 c = get_alloc_hash(&codec->cmd_cache, key);
3342 if (c)
3343 c->val = parm;
3344 mutex_unlock(&codec->bus->cmd_mutex);
3345 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003346}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003347EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003348
Takashi Iwaid5191e52009-11-16 14:58:17 +01003349/**
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003350 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3351 * @codec: the HDA codec
3352 * @nid: NID to send the command
3353 * @direct: direct flag
3354 * @verb: the verb to send
3355 * @parm: the parameter for the verb
3356 *
3357 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3358 * command if the parameter is already identical with the cached value.
3359 * If not, it sends the command and refreshes the cache.
3360 *
3361 * Returns 0 if successful, or a negative error code.
3362 */
3363int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
3364 int direct, unsigned int verb, unsigned int parm)
3365{
3366 struct hda_cache_head *c;
3367 u32 key;
3368
3369 /* parm may contain the verb stuff for get/set amp */
3370 verb = verb | (parm >> 8);
3371 parm &= 0xff;
3372 key = build_cmd_cache_key(nid, verb);
3373 mutex_lock(&codec->bus->cmd_mutex);
3374 c = get_hash(&codec->cmd_cache, key);
3375 if (c && c->val == parm) {
3376 mutex_unlock(&codec->bus->cmd_mutex);
3377 return 0;
3378 }
3379 mutex_unlock(&codec->bus->cmd_mutex);
3380 return snd_hda_codec_write_cache(codec, nid, direct, verb, parm);
3381}
3382EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
3383
3384/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003385 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3386 * @codec: HD-audio codec
3387 *
3388 * Execute all verbs recorded in the command caches to resume.
3389 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003390void snd_hda_codec_resume_cache(struct hda_codec *codec)
3391{
Takashi Iwai603c4012008-07-30 15:01:44 +02003392 struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003393 int i;
3394
Takashi Iwai603c4012008-07-30 15:01:44 +02003395 for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003396 u32 key = buffer->key;
3397 if (!key)
3398 continue;
3399 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3400 get_cmd_cache_cmd(key), buffer->val);
3401 }
3402}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003403EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003404
3405/**
3406 * snd_hda_sequence_write_cache - sequence writes with caching
3407 * @codec: the HDA codec
3408 * @seq: VERB array to send
3409 *
3410 * Send the commands sequentially from the given array.
3411 * Thte commands are recorded on cache for power-save and resume.
3412 * The array must be terminated with NID=0.
3413 */
3414void snd_hda_sequence_write_cache(struct hda_codec *codec,
3415 const struct hda_verb *seq)
3416{
3417 for (; seq->nid; seq++)
3418 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3419 seq->param);
3420}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003421EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
Takashi Iwai2a439522011-07-26 09:52:50 +02003422#endif /* CONFIG_PM */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003423
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003424void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
3425 unsigned int power_state,
3426 bool eapd_workaround)
Takashi Iwai54d17402005-11-21 16:33:22 +01003427{
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003428 hda_nid_t nid = codec->start_nid;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003429 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01003430
Takashi Iwaicb53c622007-08-10 17:21:45 +02003431 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003432 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003433 if (!(wcaps & AC_WCAP_POWER))
3434 continue;
3435 /* don't power down the widget if it controls eapd and
3436 * EAPD_BTLENABLE is set.
3437 */
3438 if (eapd_workaround && power_state == AC_PWRST_D3 &&
3439 get_wcaps_type(wcaps) == AC_WID_PIN &&
3440 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3441 int eapd = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003442 AC_VERB_GET_EAPD_BTLENABLE, 0);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003443 if (eapd & 0x02)
3444 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003445 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003446 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
3447 power_state);
Takashi Iwai54d17402005-11-21 16:33:22 +01003448 }
3449
Takashi Iwaicb53c622007-08-10 17:21:45 +02003450 if (power_state == AC_PWRST_D0) {
3451 unsigned long end_time;
3452 int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003453 /* wait until the codec reachs to D0 */
3454 end_time = jiffies + msecs_to_jiffies(500);
3455 do {
3456 state = snd_hda_codec_read(codec, fg, 0,
3457 AC_VERB_GET_POWER_STATE, 0);
3458 if (state == power_state)
3459 break;
3460 msleep(1);
3461 } while (time_after_eq(end_time, jiffies));
3462 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003463}
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003464EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all);
3465
3466/*
3467 * set power state of the codec
3468 */
3469static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
3470 unsigned int power_state)
3471{
3472 if (codec->patch_ops.set_power_state) {
3473 codec->patch_ops.set_power_state(codec, fg, power_state);
3474 return;
3475 }
3476
3477 /* this delay seems necessary to avoid click noise at power-down */
3478 if (power_state == AC_PWRST_D3)
3479 msleep(100);
3480 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE,
3481 power_state);
3482 snd_hda_codec_set_power_to_all(codec, fg, power_state, true);
3483}
Takashi Iwai54d17402005-11-21 16:33:22 +01003484
Takashi Iwai11aeff02008-07-30 15:01:46 +02003485#ifdef CONFIG_SND_HDA_HWDEP
3486/* execute additional init verbs */
3487static void hda_exec_init_verbs(struct hda_codec *codec)
3488{
3489 if (codec->init_verbs.list)
3490 snd_hda_sequence_write(codec, codec->init_verbs.list);
3491}
3492#else
3493static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3494#endif
3495
Takashi Iwai2a439522011-07-26 09:52:50 +02003496#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003497/*
3498 * call suspend and power-down; used both from PM and power-save
3499 */
3500static void hda_call_codec_suspend(struct hda_codec *codec)
3501{
3502 if (codec->patch_ops.suspend)
3503 codec->patch_ops.suspend(codec, PMSG_SUSPEND);
Takashi Iwaieb541332010-08-06 13:48:11 +02003504 hda_cleanup_all_streams(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003505 hda_set_power_state(codec,
3506 codec->afg ? codec->afg : codec->mfg,
3507 AC_PWRST_D3);
3508#ifdef CONFIG_SND_HDA_POWER_SAVE
3509 cancel_delayed_work(&codec->power_work);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02003510 spin_lock(&codec->power_lock);
3511 snd_hda_update_power_acct(codec);
3512 trace_hda_power_down(codec);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02003513 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02003514 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003515 codec->power_jiffies = jiffies;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02003516 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003517#endif
3518}
3519
3520/*
3521 * kick up codec; used both from PM and power-save
3522 */
3523static void hda_call_codec_resume(struct hda_codec *codec)
3524{
Takashi Iwai7f308302012-05-08 16:52:23 +02003525 /* set as if powered on for avoiding re-entering the resume
3526 * in the resume / power-save sequence
3527 */
3528 hda_keep_power_on(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003529 hda_set_power_state(codec,
3530 codec->afg ? codec->afg : codec->mfg,
3531 AC_PWRST_D0);
Takashi Iwai3be14142009-02-20 14:11:16 +01003532 restore_pincfgs(codec); /* restore all current pin configs */
Takashi Iwaiac0547d2010-07-05 16:50:13 +02003533 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02003534 hda_exec_init_verbs(codec);
Takashi Iwai1835a0f2011-10-27 22:12:46 +02003535 snd_hda_jack_set_dirty_all(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003536 if (codec->patch_ops.resume)
3537 codec->patch_ops.resume(codec);
3538 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02003539 if (codec->patch_ops.init)
3540 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003541 snd_hda_codec_resume_amp(codec);
3542 snd_hda_codec_resume_cache(codec);
3543 }
Takashi Iwai7f308302012-05-08 16:52:23 +02003544 snd_hda_power_down(codec); /* flag down before returning */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003545}
Takashi Iwai2a439522011-07-26 09:52:50 +02003546#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003547
Takashi Iwai54d17402005-11-21 16:33:22 +01003548
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549/**
3550 * snd_hda_build_controls - build mixer controls
3551 * @bus: the BUS
3552 *
3553 * Creates mixer controls for each codec included in the bus.
3554 *
3555 * Returns 0 if successful, otherwise a negative error code.
3556 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003557int /*__devinit*/ snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003559 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560
Takashi Iwai0ba21762007-04-16 11:29:14 +02003561 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003562 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003563 if (err < 0) {
Takashi Iwai28d1a852010-03-15 09:05:46 +01003564 printk(KERN_ERR "hda_codec: cannot build controls "
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003565 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003566 err = snd_hda_codec_reset(codec);
3567 if (err < 0) {
3568 printk(KERN_ERR
3569 "hda_codec: cannot revert codec\n");
3570 return err;
3571 }
3572 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003574 return 0;
3575}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003576EXPORT_SYMBOL_HDA(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003578int snd_hda_codec_build_controls(struct hda_codec *codec)
3579{
3580 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02003581 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003582 /* continue to initialize... */
3583 if (codec->patch_ops.init)
3584 err = codec->patch_ops.init(codec);
3585 if (!err && codec->patch_ops.build_controls)
3586 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003587 if (err < 0)
3588 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589 return 0;
3590}
3591
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592/*
3593 * stream formats
3594 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02003595struct hda_rate_tbl {
3596 unsigned int hz;
3597 unsigned int alsa_bits;
3598 unsigned int hda_fmt;
3599};
3600
Takashi Iwai92f10b32010-08-03 14:21:00 +02003601/* rate = base * mult / div */
3602#define HDA_RATE(base, mult, div) \
3603 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
3604 (((div) - 1) << AC_FMT_DIV_SHIFT))
3605
Takashi Iwaibefdf312005-08-22 13:57:55 +02003606static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003608
3609 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003610 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
3611 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
3612 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
3613 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
3614 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
3615 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
3616 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
3617 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
3618 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
3619 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
3620 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003621#define AC_PAR_PCM_RATE_BITS 11
3622 /* up to bits 10, 384kHZ isn't supported properly */
3623
3624 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003625 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003626
Takashi Iwaibefdf312005-08-22 13:57:55 +02003627 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628};
3629
3630/**
3631 * snd_hda_calc_stream_format - calculate format bitset
3632 * @rate: the sample rate
3633 * @channels: the number of channels
3634 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3635 * @maxbps: the max. bps
3636 *
3637 * Calculate the format bitset from the given rate, channels and th PCM format.
3638 *
3639 * Return zero if invalid.
3640 */
3641unsigned int snd_hda_calc_stream_format(unsigned int rate,
3642 unsigned int channels,
3643 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03003644 unsigned int maxbps,
3645 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646{
3647 int i;
3648 unsigned int val = 0;
3649
Takashi Iwaibefdf312005-08-22 13:57:55 +02003650 for (i = 0; rate_bits[i].hz; i++)
3651 if (rate_bits[i].hz == rate) {
3652 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653 break;
3654 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003655 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 snd_printdd("invalid rate %d\n", rate);
3657 return 0;
3658 }
3659
3660 if (channels == 0 || channels > 8) {
3661 snd_printdd("invalid channels %d\n", channels);
3662 return 0;
3663 }
3664 val |= channels - 1;
3665
3666 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003667 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003668 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003669 break;
3670 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003671 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003672 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 case 20:
3674 case 24:
3675 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003676 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003677 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003679 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02003681 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682 break;
3683 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003684 snd_printdd("invalid format width %d\n",
3685 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686 return 0;
3687 }
3688
Anssi Hannula32c168c2010-08-03 13:28:57 +03003689 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003690 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03003691
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692 return val;
3693}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003694EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003696static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3697{
3698 unsigned int val = 0;
3699 if (nid != codec->afg &&
3700 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
3701 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
3702 if (!val || val == -1)
3703 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
3704 if (!val || val == -1)
3705 return 0;
3706 return val;
3707}
3708
3709static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3710{
3711 return query_caps_hash(codec, nid, HDA_HASH_PARPCM_KEY(nid),
3712 get_pcm_param);
3713}
3714
3715static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid)
3716{
3717 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
3718 if (!streams || streams == -1)
3719 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
3720 if (!streams || streams == -1)
3721 return 0;
3722 return streams;
3723}
3724
3725static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
3726{
3727 return query_caps_hash(codec, nid, HDA_HASH_PARSTR_KEY(nid),
3728 get_stream_param);
3729}
3730
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731/**
3732 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
3733 * @codec: the HDA codec
3734 * @nid: NID to query
3735 * @ratesp: the pointer to store the detected rate bitflags
3736 * @formatsp: the pointer to store the detected formats
3737 * @bpsp: the pointer to store the detected format widths
3738 *
3739 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
3740 * or @bsps argument is ignored.
3741 *
3742 * Returns 0 if successful, otherwise a negative error code.
3743 */
Stephen Warren384a48d2011-06-01 11:14:21 -06003744int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
3746{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003747 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003749 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003750 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751
3752 if (ratesp) {
3753 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003754 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02003756 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003758 if (rates == 0) {
3759 snd_printk(KERN_ERR "hda_codec: rates == 0 "
3760 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
3761 nid, val,
3762 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
3763 return -EIO;
3764 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765 *ratesp = rates;
3766 }
3767
3768 if (formatsp || bpsp) {
3769 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003770 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003772 streams = query_stream_param(codec, nid);
3773 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775
3776 bps = 0;
3777 if (streams & AC_SUPFMT_PCM) {
3778 if (val & AC_SUPPCM_BITS_8) {
3779 formats |= SNDRV_PCM_FMTBIT_U8;
3780 bps = 8;
3781 }
3782 if (val & AC_SUPPCM_BITS_16) {
3783 formats |= SNDRV_PCM_FMTBIT_S16_LE;
3784 bps = 16;
3785 }
3786 if (wcaps & AC_WCAP_DIGITAL) {
3787 if (val & AC_SUPPCM_BITS_32)
3788 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
3789 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
3790 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3791 if (val & AC_SUPPCM_BITS_24)
3792 bps = 24;
3793 else if (val & AC_SUPPCM_BITS_20)
3794 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003795 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
3796 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3798 if (val & AC_SUPPCM_BITS_32)
3799 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800 else if (val & AC_SUPPCM_BITS_24)
3801 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02003802 else if (val & AC_SUPPCM_BITS_20)
3803 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804 }
3805 }
Takashi Iwaib5025c52009-07-01 18:05:27 +02003806 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003808 if (!bps)
3809 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02003810 }
3811 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003812 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813 /* temporary hack: we have still no proper support
3814 * for the direct AC3 stream...
3815 */
3816 formats |= SNDRV_PCM_FMTBIT_U8;
3817 bps = 8;
3818 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003819 if (formats == 0) {
3820 snd_printk(KERN_ERR "hda_codec: formats == 0 "
3821 "(nid=0x%x, val=0x%x, ovrd=%i, "
3822 "streams=0x%x)\n",
3823 nid, val,
3824 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
3825 streams);
3826 return -EIO;
3827 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828 if (formatsp)
3829 *formatsp = formats;
3830 if (bpsp)
3831 *bpsp = bps;
3832 }
3833
3834 return 0;
3835}
Stephen Warren384a48d2011-06-01 11:14:21 -06003836EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837
3838/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003839 * snd_hda_is_supported_format - Check the validity of the format
3840 * @codec: HD-audio codec
3841 * @nid: NID to check
3842 * @format: the HD-audio format value to check
3843 *
3844 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845 *
3846 * Returns 1 if supported, 0 if not.
3847 */
3848int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
3849 unsigned int format)
3850{
3851 int i;
3852 unsigned int val = 0, rate, stream;
3853
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003854 val = query_pcm_param(codec, nid);
3855 if (!val)
3856 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857
3858 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003859 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02003860 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861 if (val & (1 << i))
3862 break;
3863 return 0;
3864 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003865 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866 return 0;
3867
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003868 stream = query_stream_param(codec, nid);
3869 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870 return 0;
3871
3872 if (stream & AC_SUPFMT_PCM) {
3873 switch (format & 0xf0) {
3874 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003875 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876 return 0;
3877 break;
3878 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003879 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880 return 0;
3881 break;
3882 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003883 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884 return 0;
3885 break;
3886 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003887 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888 return 0;
3889 break;
3890 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003891 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892 return 0;
3893 break;
3894 default:
3895 return 0;
3896 }
3897 } else {
3898 /* FIXME: check for float32 and AC3? */
3899 }
3900
3901 return 1;
3902}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003903EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904
3905/*
3906 * PCM stuff
3907 */
3908static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
3909 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003910 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911{
3912 return 0;
3913}
3914
3915static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
3916 struct hda_codec *codec,
3917 unsigned int stream_tag,
3918 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003919 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920{
3921 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3922 return 0;
3923}
3924
3925static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
3926 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003927 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928{
Takashi Iwai888afa12008-03-18 09:57:50 +01003929 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930 return 0;
3931}
3932
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003933static int set_pcm_default_values(struct hda_codec *codec,
3934 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003936 int err;
3937
Takashi Iwai0ba21762007-04-16 11:29:14 +02003938 /* query support PCM information from the given NID */
3939 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003940 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02003941 info->rates ? NULL : &info->rates,
3942 info->formats ? NULL : &info->formats,
3943 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003944 if (err < 0)
3945 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946 }
3947 if (info->ops.open == NULL)
3948 info->ops.open = hda_pcm_default_open_close;
3949 if (info->ops.close == NULL)
3950 info->ops.close = hda_pcm_default_open_close;
3951 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003952 if (snd_BUG_ON(!info->nid))
3953 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954 info->ops.prepare = hda_pcm_default_prepare;
3955 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003957 if (snd_BUG_ON(!info->nid))
3958 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 info->ops.cleanup = hda_pcm_default_cleanup;
3960 }
3961 return 0;
3962}
3963
Takashi Iwaieb541332010-08-06 13:48:11 +02003964/*
3965 * codec prepare/cleanup entries
3966 */
3967int snd_hda_codec_prepare(struct hda_codec *codec,
3968 struct hda_pcm_stream *hinfo,
3969 unsigned int stream,
3970 unsigned int format,
3971 struct snd_pcm_substream *substream)
3972{
3973 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003974 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003975 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
3976 if (ret >= 0)
3977 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003978 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003979 return ret;
3980}
3981EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
3982
3983void snd_hda_codec_cleanup(struct hda_codec *codec,
3984 struct hda_pcm_stream *hinfo,
3985 struct snd_pcm_substream *substream)
3986{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003987 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003988 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003989 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003990}
3991EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
3992
Takashi Iwaid5191e52009-11-16 14:58:17 +01003993/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01003994const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
3995 "Audio", "SPDIF", "HDMI", "Modem"
3996};
3997
Takashi Iwai176d5332008-07-30 15:01:44 +02003998/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003999 * get the empty PCM device number to assign
Takashi Iwaic8936222010-01-28 17:08:53 +01004000 *
4001 * note the max device number is limited by HDA_MAX_PCMS, currently 10
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004002 */
4003static int get_empty_pcm_device(struct hda_bus *bus, int type)
4004{
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004005 /* audio device indices; not linear to keep compatibility */
4006 static int audio_idx[HDA_PCM_NTYPES][5] = {
4007 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
4008 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01004009 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004010 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004011 };
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004012 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004013
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004014 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004015 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
4016 return -EINVAL;
4017 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004018
4019 for (i = 0; audio_idx[type][i] >= 0 ; i++)
4020 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
4021 return audio_idx[type][i];
4022
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004023 /* non-fixed slots starting from 10 */
4024 for (i = 10; i < 32; i++) {
4025 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4026 return i;
4027 }
4028
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004029 snd_printk(KERN_WARNING "Too many %s devices\n",
4030 snd_hda_pcm_type_name[type]);
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004031 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004032}
4033
4034/*
Takashi Iwai176d5332008-07-30 15:01:44 +02004035 * attach a new PCM stream
4036 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004037static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02004038{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004039 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02004040 struct hda_pcm_stream *info;
4041 int stream, err;
4042
Takashi Iwaib91f0802008-11-04 08:43:08 +01004043 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02004044 return -EINVAL;
4045 for (stream = 0; stream < 2; stream++) {
4046 info = &pcm->stream[stream];
4047 if (info->substreams) {
4048 err = set_pcm_default_values(codec, info);
4049 if (err < 0)
4050 return err;
4051 }
4052 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004053 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02004054}
4055
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004056/* assign all PCMs of the given codec */
4057int snd_hda_codec_build_pcms(struct hda_codec *codec)
4058{
4059 unsigned int pcm;
4060 int err;
4061
4062 if (!codec->num_pcms) {
4063 if (!codec->patch_ops.build_pcms)
4064 return 0;
4065 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004066 if (err < 0) {
4067 printk(KERN_ERR "hda_codec: cannot build PCMs"
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004068 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004069 err = snd_hda_codec_reset(codec);
4070 if (err < 0) {
4071 printk(KERN_ERR
4072 "hda_codec: cannot revert codec\n");
4073 return err;
4074 }
4075 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004076 }
4077 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
4078 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
4079 int dev;
4080
4081 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01004082 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004083
4084 if (!cpcm->pcm) {
4085 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
4086 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004087 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004088 cpcm->device = dev;
4089 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004090 if (err < 0) {
4091 printk(KERN_ERR "hda_codec: cannot attach "
4092 "PCM stream %d for codec #%d\n",
4093 dev, codec->addr);
4094 continue; /* no fatal error */
4095 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004096 }
4097 }
4098 return 0;
4099}
4100
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101/**
4102 * snd_hda_build_pcms - build PCM information
4103 * @bus: the BUS
4104 *
4105 * Create PCM information for each codec included in the bus.
4106 *
4107 * The build_pcms codec patch is requested to set up codec->num_pcms and
4108 * codec->pcm_info properly. The array is referred by the top-level driver
4109 * to create its PCM instances.
4110 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4111 * callback.
4112 *
4113 * At least, substreams, channels_min and channels_max must be filled for
4114 * each stream. substreams = 0 indicates that the stream doesn't exist.
4115 * When rates and/or formats are zero, the supported values are queried
4116 * from the given nid. The nid is used also by the default ops.prepare
4117 * and ops.cleanup callbacks.
4118 *
4119 * The driver needs to call ops.open in its open callback. Similarly,
4120 * ops.close is supposed to be called in the close callback.
4121 * ops.prepare should be called in the prepare or hw_params callback
4122 * with the proper parameters for set up.
4123 * ops.cleanup should be called in hw_free for clean up of streams.
4124 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004125 * This function returns 0 if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004127int __devinit snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004129 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130
Takashi Iwai0ba21762007-04-16 11:29:14 +02004131 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004132 int err = snd_hda_codec_build_pcms(codec);
4133 if (err < 0)
4134 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135 }
4136 return 0;
4137}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004138EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140/**
4141 * snd_hda_check_board_config - compare the current codec with the config table
4142 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004143 * @num_configs: number of config enums
4144 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145 * @tbl: configuration table, terminated by null entries
4146 *
4147 * Compares the modelname or PCI subsystem id of the current codec with the
4148 * given configuration table. If a matching entry is found, returns its
4149 * config value (supposed to be 0 or positive).
4150 *
4151 * If no entries are matching, the function returns a negative value.
4152 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02004153int snd_hda_check_board_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004154 int num_configs, const char * const *models,
Takashi Iwai12f288b2007-08-02 15:51:59 +02004155 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156{
Takashi Iwaif44ac832008-07-30 15:01:45 +02004157 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004158 int i;
4159 for (i = 0; i < num_configs; i++) {
4160 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02004161 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004162 snd_printd(KERN_INFO "hda_codec: model '%s' is "
4163 "selected\n", models[i]);
4164 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165 }
4166 }
4167 }
4168
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004169 if (!codec->bus->pci || !tbl)
4170 return -1;
4171
4172 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
4173 if (!tbl)
4174 return -1;
4175 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02004176#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004177 char tmp[10];
4178 const char *model = NULL;
4179 if (models)
4180 model = models[tbl->value];
4181 if (!model) {
4182 sprintf(tmp, "#%d", tbl->value);
4183 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004185 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4186 "for config %x:%x (%s)\n",
4187 model, tbl->subvendor, tbl->subdevice,
4188 (tbl->name ? tbl->name : "Unknown device"));
4189#endif
4190 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191 }
4192 return -1;
4193}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004194EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195
4196/**
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004197 * snd_hda_check_board_codec_sid_config - compare the current codec
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004198 subsystem ID with the
4199 config table
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004200
4201 This is important for Gateway notebooks with SB450 HDA Audio
4202 where the vendor ID of the PCI device is:
4203 ATI Technologies Inc SB450 HDA Audio [1002:437b]
4204 and the vendor/subvendor are found only at the codec.
4205
4206 * @codec: the HDA codec
4207 * @num_configs: number of config enums
4208 * @models: array of model name strings
4209 * @tbl: configuration table, terminated by null entries
4210 *
4211 * Compares the modelname or PCI subsystem id of the current codec with the
4212 * given configuration table. If a matching entry is found, returns its
4213 * config value (supposed to be 0 or positive).
4214 *
4215 * If no entries are matching, the function returns a negative value.
4216 */
4217int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004218 int num_configs, const char * const *models,
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004219 const struct snd_pci_quirk *tbl)
4220{
4221 const struct snd_pci_quirk *q;
4222
4223 /* Search for codec ID */
4224 for (q = tbl; q->subvendor; q++) {
Takashi Iwaie2301a42011-11-22 19:58:56 +01004225 unsigned int mask = 0xffff0000 | q->subdevice_mask;
4226 unsigned int id = (q->subdevice | (q->subvendor << 16)) & mask;
4227 if ((codec->subsystem_id & mask) == id)
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004228 break;
4229 }
4230
4231 if (!q->subvendor)
4232 return -1;
4233
4234 tbl = q;
4235
4236 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwaid94ff6b2009-09-02 00:20:21 +02004237#ifdef CONFIG_SND_DEBUG_VERBOSE
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004238 char tmp[10];
4239 const char *model = NULL;
4240 if (models)
4241 model = models[tbl->value];
4242 if (!model) {
4243 sprintf(tmp, "#%d", tbl->value);
4244 model = tmp;
4245 }
4246 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4247 "for config %x:%x (%s)\n",
4248 model, tbl->subvendor, tbl->subdevice,
4249 (tbl->name ? tbl->name : "Unknown device"));
4250#endif
4251 return tbl->value;
4252 }
4253 return -1;
4254}
4255EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
4256
4257/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258 * snd_hda_add_new_ctls - create controls from the array
4259 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004260 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261 *
4262 * This helper function creates and add new controls in the given array.
4263 * The array must be terminated with an empty entry as terminator.
4264 *
4265 * Returns 0 if successful, or a negative error code.
4266 */
Takashi Iwai031024e2011-05-02 11:29:30 +02004267int snd_hda_add_new_ctls(struct hda_codec *codec,
4268 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01004270 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271
4272 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004273 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004274 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01004275 if (knew->iface == -1) /* skip this codec private value */
4276 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004277 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004278 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02004279 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01004280 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004281 if (addr > 0)
4282 kctl->id.device = addr;
4283 if (idx > 0)
4284 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01004285 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004286 if (!err)
4287 break;
4288 /* try first with another device index corresponding to
4289 * the codec addr; if it still fails (or it's the
4290 * primary codec), then try another control index
4291 */
4292 if (!addr && codec->addr)
4293 addr = codec->addr;
4294 else if (!idx && !knew->index) {
4295 idx = find_empty_mixer_ctl_idx(codec,
4296 knew->name);
4297 if (idx <= 0)
4298 return err;
4299 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01004300 return err;
4301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302 }
4303 return 0;
4304}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004305EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306
Takashi Iwaicb53c622007-08-10 17:21:45 +02004307#ifdef CONFIG_SND_HDA_POWER_SAVE
Takashi Iwaicb53c622007-08-10 17:21:45 +02004308static void hda_power_work(struct work_struct *work)
4309{
4310 struct hda_codec *codec =
4311 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004312 struct hda_bus *bus = codec->bus;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004313
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004314 spin_lock(&codec->power_lock);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004315 if (codec->power_transition > 0) { /* during power-up sequence? */
4316 spin_unlock(&codec->power_lock);
4317 return;
4318 }
Maxim Levitsky2e492462007-09-03 15:26:57 +02004319 if (!codec->power_on || codec->power_count) {
4320 codec->power_transition = 0;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004321 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004322 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02004323 }
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004324 spin_unlock(&codec->power_lock);
4325
Takashi Iwaicb53c622007-08-10 17:21:45 +02004326 hda_call_codec_suspend(codec);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004327 if (bus->ops.pm_notify)
4328 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004329}
4330
4331static void hda_keep_power_on(struct hda_codec *codec)
4332{
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004333 spin_lock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004334 codec->power_count++;
4335 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004336 codec->power_jiffies = jiffies;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004337 spin_unlock(&codec->power_lock);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004338}
4339
Takashi Iwaid5191e52009-11-16 14:58:17 +01004340/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01004341void snd_hda_update_power_acct(struct hda_codec *codec)
4342{
4343 unsigned long delta = jiffies - codec->power_jiffies;
4344 if (codec->power_on)
4345 codec->power_on_acct += delta;
4346 else
4347 codec->power_off_acct += delta;
4348 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004349}
4350
Takashi Iwaid5191e52009-11-16 14:58:17 +01004351/**
4352 * snd_hda_power_up - Power-up the codec
4353 * @codec: HD-audio codec
4354 *
4355 * Increment the power-up counter and power up the hardware really when
4356 * not turned on yet.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004357 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004358void snd_hda_power_up(struct hda_codec *codec)
4359{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004360 struct hda_bus *bus = codec->bus;
4361
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004362 spin_lock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004363 codec->power_count++;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004364 if (codec->power_on || codec->power_transition > 0) {
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004365 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004366 return;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004367 }
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004368 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004369
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004370 cancel_delayed_work_sync(&codec->power_work);
4371
4372 spin_lock(&codec->power_lock);
Takashi Iwaid66fee52011-08-02 15:39:31 +02004373 trace_hda_power_up(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004374 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004375 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004376 codec->power_jiffies = jiffies;
Takashi Iwai7f308302012-05-08 16:52:23 +02004377 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004378 spin_unlock(&codec->power_lock);
4379
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004380 if (bus->ops.pm_notify)
4381 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004382 hda_call_codec_resume(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004383
4384 spin_lock(&codec->power_lock);
Takashi Iwaia221e282007-08-16 16:35:33 +02004385 codec->power_transition = 0;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004386 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004387}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004388EXPORT_SYMBOL_HDA(snd_hda_power_up);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004389
4390#define power_save(codec) \
4391 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004392
Takashi Iwaid5191e52009-11-16 14:58:17 +01004393/**
4394 * snd_hda_power_down - Power-down the codec
4395 * @codec: HD-audio codec
4396 *
4397 * Decrement the power-up counter and schedules the power-off work if
4398 * the counter rearches to zero.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004399 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004400void snd_hda_power_down(struct hda_codec *codec)
4401{
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004402 spin_lock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004403 --codec->power_count;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004404 if (!codec->power_on || codec->power_count || codec->power_transition) {
4405 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004406 return;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004407 }
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004408 if (power_save(codec)) {
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004409 codec->power_transition = -1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01004410 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004411 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02004412 }
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004413 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004414}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004415EXPORT_SYMBOL_HDA(snd_hda_power_down);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004416
Takashi Iwaid5191e52009-11-16 14:58:17 +01004417/**
4418 * snd_hda_check_amp_list_power - Check the amp list and update the power
4419 * @codec: HD-audio codec
4420 * @check: the object containing an AMP list and the status
4421 * @nid: NID to check / update
4422 *
4423 * Check whether the given NID is in the amp list. If it's in the list,
4424 * check the current AMP status, and update the the power-status according
4425 * to the mute status.
4426 *
4427 * This function is supposed to be set or called from the check_power_status
4428 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004429 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004430int snd_hda_check_amp_list_power(struct hda_codec *codec,
4431 struct hda_loopback_check *check,
4432 hda_nid_t nid)
4433{
Takashi Iwai031024e2011-05-02 11:29:30 +02004434 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004435 int ch, v;
4436
4437 if (!check->amplist)
4438 return 0;
4439 for (p = check->amplist; p->nid; p++) {
4440 if (p->nid == nid)
4441 break;
4442 }
4443 if (!p->nid)
4444 return 0; /* nothing changed */
4445
4446 for (p = check->amplist; p->nid; p++) {
4447 for (ch = 0; ch < 2; ch++) {
4448 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4449 p->idx);
4450 if (!(v & HDA_AMP_MUTE) && v > 0) {
4451 if (!check->power_on) {
4452 check->power_on = 1;
4453 snd_hda_power_up(codec);
4454 }
4455 return 1;
4456 }
4457 }
4458 }
4459 if (check->power_on) {
4460 check->power_on = 0;
4461 snd_hda_power_down(codec);
4462 }
4463 return 0;
4464}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004465EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004466#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004468/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004469 * Channel mode helper
4470 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004471
4472/**
4473 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
4474 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004475int snd_hda_ch_mode_info(struct hda_codec *codec,
4476 struct snd_ctl_elem_info *uinfo,
4477 const struct hda_channel_mode *chmode,
4478 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004479{
4480 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4481 uinfo->count = 1;
4482 uinfo->value.enumerated.items = num_chmodes;
4483 if (uinfo->value.enumerated.item >= num_chmodes)
4484 uinfo->value.enumerated.item = num_chmodes - 1;
4485 sprintf(uinfo->value.enumerated.name, "%dch",
4486 chmode[uinfo->value.enumerated.item].channels);
4487 return 0;
4488}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004489EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004490
Takashi Iwaid5191e52009-11-16 14:58:17 +01004491/**
4492 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
4493 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004494int snd_hda_ch_mode_get(struct hda_codec *codec,
4495 struct snd_ctl_elem_value *ucontrol,
4496 const struct hda_channel_mode *chmode,
4497 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004498 int max_channels)
4499{
4500 int i;
4501
4502 for (i = 0; i < num_chmodes; i++) {
4503 if (max_channels == chmode[i].channels) {
4504 ucontrol->value.enumerated.item[0] = i;
4505 break;
4506 }
4507 }
4508 return 0;
4509}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004510EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004511
Takashi Iwaid5191e52009-11-16 14:58:17 +01004512/**
4513 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
4514 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004515int snd_hda_ch_mode_put(struct hda_codec *codec,
4516 struct snd_ctl_elem_value *ucontrol,
4517 const struct hda_channel_mode *chmode,
4518 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004519 int *max_channelsp)
4520{
4521 unsigned int mode;
4522
4523 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01004524 if (mode >= num_chmodes)
4525 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004526 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004527 return 0;
4528 /* change the current channel setting */
4529 *max_channelsp = chmode[mode].channels;
4530 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004531 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004532 return 1;
4533}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004534EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004535
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536/*
4537 * input MUX helper
4538 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004539
4540/**
4541 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
4542 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004543int snd_hda_input_mux_info(const struct hda_input_mux *imux,
4544 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545{
4546 unsigned int index;
4547
4548 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4549 uinfo->count = 1;
4550 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004551 if (!imux->num_items)
4552 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553 index = uinfo->value.enumerated.item;
4554 if (index >= imux->num_items)
4555 index = imux->num_items - 1;
4556 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
4557 return 0;
4558}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004559EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560
Takashi Iwaid5191e52009-11-16 14:58:17 +01004561/**
4562 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
4563 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004564int snd_hda_input_mux_put(struct hda_codec *codec,
4565 const struct hda_input_mux *imux,
4566 struct snd_ctl_elem_value *ucontrol,
4567 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568 unsigned int *cur_val)
4569{
4570 unsigned int idx;
4571
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004572 if (!imux->num_items)
4573 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574 idx = ucontrol->value.enumerated.item[0];
4575 if (idx >= imux->num_items)
4576 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004577 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004579 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
4580 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581 *cur_val = idx;
4582 return 1;
4583}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004584EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585
4586
4587/*
4588 * Multi-channel / digital-out PCM helper functions
4589 */
4590
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004591/* setup SPDIF output stream */
4592static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
4593 unsigned int stream_tag, unsigned int format)
4594{
Stephen Warren7c9359762011-06-01 11:14:17 -06004595 struct hda_spdif_out *spdif = snd_hda_spdif_out_of_nid(codec, nid);
4596
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004597 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Stephen Warren7c9359762011-06-01 11:14:17 -06004598 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004599 set_dig_out_convert(codec, nid,
Stephen Warren7c9359762011-06-01 11:14:17 -06004600 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02004601 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004602 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02004603 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004604 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004605 for (d = codec->slave_dig_outs; *d; d++)
4606 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
4607 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004608 }
Takashi Iwai2f728532008-09-25 16:32:41 +02004609 /* turn on again (if needed) */
Stephen Warren7c9359762011-06-01 11:14:17 -06004610 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai2f728532008-09-25 16:32:41 +02004611 set_dig_out_convert(codec, nid,
Stephen Warren7c9359762011-06-01 11:14:17 -06004612 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02004613}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004614
Takashi Iwai2f728532008-09-25 16:32:41 +02004615static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
4616{
4617 snd_hda_codec_cleanup_stream(codec, nid);
4618 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004619 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004620 for (d = codec->slave_dig_outs; *d; d++)
4621 snd_hda_codec_cleanup_stream(codec, *d);
4622 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004623}
4624
Takashi Iwaid5191e52009-11-16 14:58:17 +01004625/**
4626 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
4627 * @bus: HD-audio bus
4628 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004629void snd_hda_bus_reboot_notify(struct hda_bus *bus)
4630{
4631 struct hda_codec *codec;
4632
4633 if (!bus)
4634 return;
4635 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02004636 if (hda_codec_is_power_on(codec) &&
4637 codec->patch_ops.reboot_notify)
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004638 codec->patch_ops.reboot_notify(codec);
4639 }
4640}
Takashi Iwai8f217a22009-11-10 18:26:12 +01004641EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004642
Takashi Iwaid5191e52009-11-16 14:58:17 +01004643/**
4644 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004646int snd_hda_multi_out_dig_open(struct hda_codec *codec,
4647 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648{
Ingo Molnar62932df2006-01-16 16:34:20 +01004649 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02004650 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
4651 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02004652 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01004654 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655 return 0;
4656}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004657EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658
Takashi Iwaid5191e52009-11-16 14:58:17 +01004659/**
4660 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
4661 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004662int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
4663 struct hda_multi_out *mout,
4664 unsigned int stream_tag,
4665 unsigned int format,
4666 struct snd_pcm_substream *substream)
4667{
4668 mutex_lock(&codec->spdif_mutex);
4669 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
4670 mutex_unlock(&codec->spdif_mutex);
4671 return 0;
4672}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004673EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004674
Takashi Iwaid5191e52009-11-16 14:58:17 +01004675/**
4676 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
4677 */
Takashi Iwai9411e212009-02-13 11:32:28 +01004678int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
4679 struct hda_multi_out *mout)
4680{
4681 mutex_lock(&codec->spdif_mutex);
4682 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4683 mutex_unlock(&codec->spdif_mutex);
4684 return 0;
4685}
4686EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
4687
Takashi Iwaid5191e52009-11-16 14:58:17 +01004688/**
4689 * snd_hda_multi_out_dig_close - release the digital out stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004691int snd_hda_multi_out_dig_close(struct hda_codec *codec,
4692 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693{
Ingo Molnar62932df2006-01-16 16:34:20 +01004694 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01004696 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697 return 0;
4698}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004699EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700
Takashi Iwaid5191e52009-11-16 14:58:17 +01004701/**
4702 * snd_hda_multi_out_analog_open - open analog outputs
4703 *
4704 * Open analog outputs and set up the hw-constraints.
4705 * If the digital outputs can be opened as slave, open the digital
4706 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004708int snd_hda_multi_out_analog_open(struct hda_codec *codec,
4709 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01004710 struct snd_pcm_substream *substream,
4711 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712{
Takashi Iwai9a081602008-02-12 18:37:26 +01004713 struct snd_pcm_runtime *runtime = substream->runtime;
4714 runtime->hw.channels_max = mout->max_channels;
4715 if (mout->dig_out_nid) {
4716 if (!mout->analog_rates) {
4717 mout->analog_rates = hinfo->rates;
4718 mout->analog_formats = hinfo->formats;
4719 mout->analog_maxbps = hinfo->maxbps;
4720 } else {
4721 runtime->hw.rates = mout->analog_rates;
4722 runtime->hw.formats = mout->analog_formats;
4723 hinfo->maxbps = mout->analog_maxbps;
4724 }
4725 if (!mout->spdif_rates) {
4726 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
4727 &mout->spdif_rates,
4728 &mout->spdif_formats,
4729 &mout->spdif_maxbps);
4730 }
4731 mutex_lock(&codec->spdif_mutex);
4732 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02004733 if ((runtime->hw.rates & mout->spdif_rates) &&
4734 (runtime->hw.formats & mout->spdif_formats)) {
4735 runtime->hw.rates &= mout->spdif_rates;
4736 runtime->hw.formats &= mout->spdif_formats;
4737 if (mout->spdif_maxbps < hinfo->maxbps)
4738 hinfo->maxbps = mout->spdif_maxbps;
4739 } else {
4740 mout->share_spdif = 0;
4741 /* FIXME: need notify? */
4742 }
Takashi Iwai9a081602008-02-12 18:37:26 +01004743 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02004744 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004745 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746 return snd_pcm_hw_constraint_step(substream->runtime, 0,
4747 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
4748}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004749EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750
Takashi Iwaid5191e52009-11-16 14:58:17 +01004751/**
4752 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
4753 *
4754 * Set up the i/o for analog out.
4755 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004757int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
4758 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759 unsigned int stream_tag,
4760 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004761 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762{
Takashi Iwaidda14412011-05-02 11:29:30 +02004763 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764 int chs = substream->runtime->channels;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02004765 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766 int i;
4767
Ingo Molnar62932df2006-01-16 16:34:20 +01004768 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02004769 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Takashi Iwai9a081602008-02-12 18:37:26 +01004770 if (mout->dig_out_nid && mout->share_spdif &&
4771 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02004773 snd_hda_is_supported_format(codec, mout->dig_out_nid,
4774 format) &&
Stephen Warren7c9359762011-06-01 11:14:17 -06004775 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004777 setup_dig_out_stream(codec, mout->dig_out_nid,
4778 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779 } else {
4780 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02004781 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782 }
4783 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004784 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785
4786 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004787 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
4788 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004789 if (!mout->no_share_stream &&
4790 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004792 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
4793 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004794 /* extra outputs copied from front */
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02004795 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
4796 if (!mout->no_share_stream && mout->hp_out_nid[i])
4797 snd_hda_codec_setup_stream(codec,
4798 mout->hp_out_nid[i],
4799 stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004800 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02004801 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01004802 snd_hda_codec_setup_stream(codec,
4803 mout->extra_out_nid[i],
4804 stream_tag, 0, format);
4805
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806 /* surrounds */
4807 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02004808 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004809 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4810 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004811 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004812 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4813 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814 }
4815 return 0;
4816}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004817EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818
Takashi Iwaid5191e52009-11-16 14:58:17 +01004819/**
4820 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004822int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
4823 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824{
Takashi Iwaidda14412011-05-02 11:29:30 +02004825 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826 int i;
4827
4828 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01004829 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01004831 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02004832 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
4833 if (mout->hp_out_nid[i])
4834 snd_hda_codec_cleanup_stream(codec,
4835 mout->hp_out_nid[i]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004836 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
4837 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01004838 snd_hda_codec_cleanup_stream(codec,
4839 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01004840 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02004842 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843 mout->dig_out_used = 0;
4844 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004845 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846 return 0;
4847}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004848EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849
Takashi Iwai47408602012-04-20 13:06:53 +02004850/**
4851 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
4852 *
4853 * Guess the suitable VREF pin bits to be set as the pin-control value.
4854 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
4855 */
4856unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
4857{
4858 unsigned int pincap;
4859 unsigned int oldval;
4860 oldval = snd_hda_codec_read(codec, pin, 0,
4861 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
4862 pincap = snd_hda_query_pin_caps(codec, pin);
4863 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
4864 /* Exception: if the default pin setup is vref50, we give it priority */
4865 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
4866 return AC_PINCTL_VREF_80;
4867 else if (pincap & AC_PINCAP_VREF_50)
4868 return AC_PINCTL_VREF_50;
4869 else if (pincap & AC_PINCAP_VREF_100)
4870 return AC_PINCTL_VREF_100;
4871 else if (pincap & AC_PINCAP_VREF_GRD)
4872 return AC_PINCTL_VREF_GRD;
4873 return AC_PINCTL_VREF_HIZ;
4874}
4875EXPORT_SYMBOL_HDA(snd_hda_get_default_vref);
4876
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02004877int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
4878 unsigned int val, bool cached)
4879{
4880 if (val) {
4881 unsigned int cap = snd_hda_query_pin_caps(codec, pin);
Takashi Iwai6942c102012-04-20 13:08:40 +02004882 if (cap && (val & AC_PINCTL_OUT_EN)) {
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02004883 if (!(cap & AC_PINCAP_OUT))
4884 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
4885 else if ((val & AC_PINCTL_HP_EN) &&
4886 !(cap & AC_PINCAP_HP_DRV))
4887 val &= ~AC_PINCTL_HP_EN;
4888 }
Takashi Iwai6942c102012-04-20 13:08:40 +02004889 if (cap && (val & AC_PINCTL_IN_EN)) {
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02004890 if (!(cap & AC_PINCAP_IN))
4891 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
4892 }
4893 }
4894 if (cached)
4895 return snd_hda_codec_update_cache(codec, pin, 0,
4896 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
4897 else
4898 return snd_hda_codec_write(codec, pin, 0,
4899 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
4900}
4901EXPORT_SYMBOL_HDA(_snd_hda_set_pin_ctl);
4902
Takashi Iwai990061c2010-09-09 22:08:44 +02004903/**
4904 * snd_hda_add_imux_item - Add an item to input_mux
4905 *
4906 * When the same label is used already in the existing items, the number
4907 * suffix is appended to the label. This label index number is stored
4908 * to type_idx when non-NULL pointer is given.
4909 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02004910int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
4911 int index, int *type_idx)
4912{
4913 int i, label_idx = 0;
4914 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
4915 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
4916 return -EINVAL;
4917 }
4918 for (i = 0; i < imux->num_items; i++) {
4919 if (!strncmp(label, imux->items[i].label, strlen(label)))
4920 label_idx++;
4921 }
4922 if (type_idx)
4923 *type_idx = label_idx;
4924 if (label_idx > 0)
4925 snprintf(imux->items[imux->num_items].label,
4926 sizeof(imux->items[imux->num_items].label),
4927 "%s %d", label, label_idx);
4928 else
4929 strlcpy(imux->items[imux->num_items].label, label,
4930 sizeof(imux->items[imux->num_items].label));
4931 imux->items[imux->num_items].index = index;
4932 imux->num_items++;
4933 return 0;
4934}
4935EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02004936
Takashi Iwai4a471b72005-12-07 13:56:29 +01004937
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938#ifdef CONFIG_PM
4939/*
4940 * power management
4941 */
4942
4943/**
4944 * snd_hda_suspend - suspend the codecs
4945 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946 *
4947 * Returns 0 if successful.
4948 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02004949int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004951 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952
Takashi Iwai0ba21762007-04-16 11:29:14 +02004953 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02004954 if (hda_codec_is_power_on(codec))
4955 hda_call_codec_suspend(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956 }
4957 return 0;
4958}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004959EXPORT_SYMBOL_HDA(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960
4961/**
4962 * snd_hda_resume - resume the codecs
4963 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964 *
4965 * Returns 0 if successful.
Takashi Iwaicb53c622007-08-10 17:21:45 +02004966 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004967 * This function is defined only when POWER_SAVE isn't set.
Takashi Iwaicb53c622007-08-10 17:21:45 +02004968 * In the power-save mode, the codec is resumed dynamically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969 */
4970int snd_hda_resume(struct hda_bus *bus)
4971{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004972 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973
Takashi Iwai0ba21762007-04-16 11:29:14 +02004974 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai7f308302012-05-08 16:52:23 +02004975 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977 return 0;
4978}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004979EXPORT_SYMBOL_HDA(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004980#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02004981
4982/*
4983 * generic arrays
4984 */
4985
Takashi Iwaid5191e52009-11-16 14:58:17 +01004986/**
4987 * snd_array_new - get a new element from the given array
4988 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004989 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01004990 * Get a new element from the given array. If it exceeds the
4991 * pre-allocated array size, re-allocate the array.
4992 *
4993 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02004994 */
4995void *snd_array_new(struct snd_array *array)
4996{
4997 if (array->used >= array->alloced) {
4998 int num = array->alloced + array->alloc_align;
Takashi Iwai3101ba02011-07-12 08:05:16 +02004999 int size = (num + 1) * array->elem_size;
Takashi Iwai00ef9612011-07-14 15:57:27 +02005000 int oldsize = array->alloced * array->elem_size;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01005001 void *nlist;
5002 if (snd_BUG_ON(num >= 4096))
5003 return NULL;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005004 nlist = krealloc(array->list, size, GFP_KERNEL);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005005 if (!nlist)
5006 return NULL;
Takashi Iwai00ef9612011-07-14 15:57:27 +02005007 memset(nlist + oldsize, 0, size - oldsize);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005008 array->list = nlist;
5009 array->alloced = num;
5010 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01005011 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005012}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005013EXPORT_SYMBOL_HDA(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005014
Takashi Iwaid5191e52009-11-16 14:58:17 +01005015/**
5016 * snd_array_free - free the given array elements
5017 * @array: the array object
5018 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005019void snd_array_free(struct snd_array *array)
5020{
5021 kfree(array->list);
5022 array->used = 0;
5023 array->alloced = 0;
5024 array->list = NULL;
5025}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005026EXPORT_SYMBOL_HDA(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01005027
Takashi Iwaid5191e52009-11-16 14:58:17 +01005028/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01005029 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5030 * @pcm: PCM caps bits
5031 * @buf: the string buffer to write
5032 * @buflen: the max buffer length
5033 *
5034 * used by hda_proc.c and hda_eld.c
5035 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005036void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5037{
5038 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5039 int i, j;
5040
5041 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5042 if (pcm & (AC_SUPPCM_BITS_8 << i))
5043 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5044
5045 buf[j] = '\0'; /* necessary when j == 0 */
5046}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005047EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005048
5049MODULE_DESCRIPTION("HDA codec core");
5050MODULE_LICENSE("GPL");