blob: c556fe1c25eb8676f4dc14e841cbca8111766e09 [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 Iwaic3b6bcc2012-05-10 16:11:15 +02001258 mutex_init(&codec->hash_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +02001259 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001260 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001261 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1262 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001263 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001264 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +02001265 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Takashi Iwaia12d3e12011-04-07 15:55:15 +02001266 snd_array_init(&codec->conn_lists, sizeof(hda_nid_t), 64);
Stephen Warren7c9359762011-06-01 11:14:17 -06001267 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
Takashi Iwaicb53c622007-08-10 17:21:45 +02001269#ifdef CONFIG_SND_HDA_POWER_SAVE
Takashi Iwai5536c6d2012-05-08 17:08:10 +02001270 spin_lock_init(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001271 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1272 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1273 * the caller has to power down appropriatley after initialization
1274 * phase.
1275 */
1276 hda_keep_power_on(codec);
1277#endif
1278
Takashi Iwaic382a9f2012-05-07 15:01:02 +02001279 if (codec->bus->modelname) {
1280 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1281 if (!codec->modelname) {
1282 snd_hda_codec_free(codec);
1283 return -ENODEV;
1284 }
1285 }
1286
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 list_add_tail(&codec->list, &bus->codec_list);
1288 bus->caddr_tbl[codec_addr] = codec;
1289
Takashi Iwai0ba21762007-04-16 11:29:14 +02001290 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1291 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001292 if (codec->vendor_id == -1)
1293 /* read again, hopefully the access method was corrected
1294 * in the last read...
1295 */
1296 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1297 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001298 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1299 AC_PAR_SUBSYSTEM_ID);
1300 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1301 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001303 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001304 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001305 snd_printdd("hda_codec: no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001306 err = -ENODEV;
1307 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 }
1309
Takashi Iwai3be14142009-02-20 14:11:16 +01001310 err = read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg);
1311 if (err < 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +01001312 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001313 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001314 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001315 err = read_pin_defaults(codec);
1316 if (err < 0)
1317 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001318
Takashi Iwai0ba21762007-04-16 11:29:14 +02001319 if (!codec->subsystem_id) {
Takashi Iwai86284e42005-10-11 15:05:54 +02001320 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
Takashi Iwai0ba21762007-04-16 11:29:14 +02001321 codec->subsystem_id =
1322 snd_hda_codec_read(codec, nid, 0,
1323 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001324 }
1325
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001326 /* power-up all before initialization */
1327 hda_set_power_state(codec,
1328 codec->afg ? codec->afg : codec->mfg,
1329 AC_PWRST_D0);
1330
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001331 snd_hda_codec_proc_new(codec);
1332
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001333 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001334
1335 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1336 codec->subsystem_id, codec->revision_id);
1337 snd_component_add(codec->bus->card, component);
1338
1339 if (codecp)
1340 *codecp = codec;
1341 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001342
1343 error:
1344 snd_hda_codec_free(codec);
1345 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001346}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001347EXPORT_SYMBOL_HDA(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001348
Takashi Iwaid5191e52009-11-16 14:58:17 +01001349/**
1350 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1351 * @codec: the HDA codec
1352 *
1353 * Start parsing of the given codec tree and (re-)initialize the whole
1354 * patch instance.
1355 *
1356 * Returns 0 if successful or a negative error code.
1357 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001358int snd_hda_codec_configure(struct hda_codec *codec)
1359{
1360 int err;
1361
Takashi Iwaid5ad6302007-03-07 15:55:59 +01001362 codec->preset = find_codec_preset(codec);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001363 if (!codec->vendor_name || !codec->chip_name) {
Takashi Iwaif44ac832008-07-30 15:01:45 +02001364 err = get_codec_name(codec);
1365 if (err < 0)
1366 return err;
1367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
Takashi Iwai82467612007-07-27 19:15:54 +02001369 if (is_generic_config(codec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai82467612007-07-27 19:15:54 +02001371 goto patched;
1372 }
Takashi Iwai82467612007-07-27 19:15:54 +02001373 if (codec->preset && codec->preset->patch) {
1374 err = codec->preset->patch(codec);
1375 goto patched;
1376 }
1377
1378 /* call the default parser */
Takashi Iwai82467612007-07-27 19:15:54 +02001379 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai35a1e0c2007-10-19 08:13:40 +02001380 if (err < 0)
1381 printk(KERN_ERR "hda-codec: No codec parser is available\n");
Takashi Iwai82467612007-07-27 19:15:54 +02001382
1383 patched:
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001384 if (!err && codec->patch_ops.unsol_event)
1385 err = init_unsol_queue(codec->bus);
Takashi Iwaif62faed2009-12-23 09:27:51 +01001386 /* audio codec should override the mixer name */
1387 if (!err && (codec->afg || !*codec->bus->card->mixername))
1388 snprintf(codec->bus->card->mixername,
1389 sizeof(codec->bus->card->mixername),
1390 "%s %s", codec->vendor_name, codec->chip_name);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001391 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392}
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001393EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
1395/**
1396 * snd_hda_codec_setup_stream - set up the codec for streaming
1397 * @codec: the CODEC to set up
1398 * @nid: the NID to set up
1399 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1400 * @channel_id: channel id to pass, zero based.
1401 * @format: stream format.
1402 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001403void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1404 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 int channel_id, int format)
1406{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001407 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001408 struct hda_cvt_setup *p;
1409 unsigned int oldval, newval;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001410 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001411 int i;
1412
Takashi Iwai0ba21762007-04-16 11:29:14 +02001413 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001414 return;
1415
Takashi Iwai0ba21762007-04-16 11:29:14 +02001416 snd_printdd("hda_codec_setup_stream: "
1417 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001419 p = get_hda_cvt_setup(codec, nid);
1420 if (!p)
1421 return;
1422 /* update the stream-id if changed */
1423 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1424 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1425 newval = (stream_tag << 4) | channel_id;
1426 if (oldval != newval)
1427 snd_hda_codec_write(codec, nid, 0,
1428 AC_VERB_SET_CHANNEL_STREAMID,
1429 newval);
1430 p->stream_tag = stream_tag;
1431 p->channel_id = channel_id;
1432 }
1433 /* update the format-id if changed */
1434 if (p->format_id != format) {
1435 oldval = snd_hda_codec_read(codec, nid, 0,
1436 AC_VERB_GET_STREAM_FORMAT, 0);
1437 if (oldval != format) {
1438 msleep(1);
1439 snd_hda_codec_write(codec, nid, 0,
1440 AC_VERB_SET_STREAM_FORMAT,
1441 format);
1442 }
1443 p->format_id = format;
1444 }
1445 p->active = 1;
1446 p->dirty = 0;
1447
1448 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001449 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001450 list_for_each_entry(c, &codec->bus->codec_list, list) {
1451 for (i = 0; i < c->cvt_setups.used; i++) {
1452 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001453 if (!p->active && p->stream_tag == stream_tag &&
David Henningsson54c2a892012-02-01 12:05:41 +01001454 get_wcaps_type(get_wcaps(c, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001455 p->dirty = 1;
1456 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001457 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001459EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
Takashi Iwaif0cea792010-08-13 11:56:53 +02001461static void really_cleanup_stream(struct hda_codec *codec,
1462 struct hda_cvt_setup *q);
1463
Takashi Iwaid5191e52009-11-16 14:58:17 +01001464/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001465 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001466 * @codec: the CODEC to clean up
1467 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001468 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001469 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001470void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1471 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001472{
Takashi Iwaieb541332010-08-06 13:48:11 +02001473 struct hda_cvt_setup *p;
1474
Takashi Iwai888afa12008-03-18 09:57:50 +01001475 if (!nid)
1476 return;
1477
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001478 if (codec->no_sticky_stream)
1479 do_now = 1;
1480
Takashi Iwai888afa12008-03-18 09:57:50 +01001481 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001482 p = get_hda_cvt_setup(codec, nid);
Takashi Iwaif0cea792010-08-13 11:56:53 +02001483 if (p) {
1484 /* here we just clear the active flag when do_now isn't set;
1485 * actual clean-ups will be done later in
1486 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1487 */
1488 if (do_now)
1489 really_cleanup_stream(codec, p);
1490 else
1491 p->active = 0;
1492 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001493}
Takashi Iwaif0cea792010-08-13 11:56:53 +02001494EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001495
Takashi Iwaieb541332010-08-06 13:48:11 +02001496static void really_cleanup_stream(struct hda_codec *codec,
1497 struct hda_cvt_setup *q)
1498{
1499 hda_nid_t nid = q->nid;
Takashi Iwai218264a2011-09-27 17:33:45 +02001500 if (q->stream_tag || q->channel_id)
1501 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1502 if (q->format_id)
1503 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1504);
Takashi Iwaieb541332010-08-06 13:48:11 +02001505 memset(q, 0, sizeof(*q));
1506 q->nid = nid;
1507}
1508
1509/* clean up the all conflicting obsolete streams */
1510static void purify_inactive_streams(struct hda_codec *codec)
1511{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001512 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001513 int i;
1514
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001515 list_for_each_entry(c, &codec->bus->codec_list, list) {
1516 for (i = 0; i < c->cvt_setups.used; i++) {
1517 struct hda_cvt_setup *p;
1518 p = snd_array_elem(&c->cvt_setups, i);
1519 if (p->dirty)
1520 really_cleanup_stream(c, p);
1521 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001522 }
1523}
1524
Takashi Iwai2a439522011-07-26 09:52:50 +02001525#ifdef CONFIG_PM
Takashi Iwaieb541332010-08-06 13:48:11 +02001526/* clean up all streams; called from suspend */
1527static void hda_cleanup_all_streams(struct hda_codec *codec)
1528{
1529 int i;
1530
1531 for (i = 0; i < codec->cvt_setups.used; i++) {
1532 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1533 if (p->stream_tag)
1534 really_cleanup_stream(codec, p);
1535 }
1536}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001537#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001538
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539/*
1540 * amp access functions
1541 */
1542
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001543/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001544#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001545#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001546#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1547#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001549#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
1551/* initialize the hash table */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001552static void /*__devinit*/ init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001553 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554{
Takashi Iwai01751f52007-08-10 16:59:39 +02001555 memset(cache, 0, sizeof(*cache));
1556 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001557 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001558}
1559
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001560static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001561{
Takashi Iwai603c4012008-07-30 15:01:44 +02001562 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563}
1564
1565/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001566static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567{
Takashi Iwai01751f52007-08-10 16:59:39 +02001568 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1569 u16 cur = cache->hash[idx];
1570 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
1572 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001573 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 if (info->key == key)
1575 return info;
1576 cur = info->next;
1577 }
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001578 return NULL;
1579}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001581/* query the hash. allocate an entry if not found. */
1582static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1583 u32 key)
1584{
1585 struct hda_cache_head *info = get_hash(cache, key);
1586 if (!info) {
1587 u16 idx, cur;
1588 /* add a new hash entry */
1589 info = snd_array_new(&cache->buf);
1590 if (!info)
1591 return NULL;
1592 cur = snd_array_index(&cache->buf, info);
1593 info->key = key;
1594 info->val = 0;
1595 idx = key % (u16)ARRAY_SIZE(cache->hash);
1596 info->next = cache->hash[idx];
1597 cache->hash[idx] = cur;
1598 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 return info;
1600}
1601
Takashi Iwai01751f52007-08-10 16:59:39 +02001602/* query and allocate an amp hash entry */
1603static inline struct hda_amp_info *
1604get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1605{
1606 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1607}
1608
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001609/* overwrite the value with the key in the caps hash */
1610static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val)
1611{
1612 struct hda_amp_info *info;
1613
1614 mutex_lock(&codec->hash_mutex);
1615 info = get_alloc_amp_hash(codec, key);
1616 if (!info) {
1617 mutex_unlock(&codec->hash_mutex);
1618 return -EINVAL;
1619 }
1620 info->amp_caps = val;
1621 info->head.val |= INFO_AMP_CAPS;
1622 mutex_unlock(&codec->hash_mutex);
1623 return 0;
1624}
1625
1626/* query the value from the caps hash; if not found, fetch the current
1627 * value from the given function and store in the hash
1628 */
1629static unsigned int
1630query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key,
1631 unsigned int (*func)(struct hda_codec *, hda_nid_t, int))
1632{
1633 struct hda_amp_info *info;
1634 unsigned int val;
1635
1636 mutex_lock(&codec->hash_mutex);
1637 info = get_alloc_amp_hash(codec, key);
1638 if (!info) {
1639 mutex_unlock(&codec->hash_mutex);
1640 return 0;
1641 }
1642 if (!(info->head.val & INFO_AMP_CAPS)) {
1643 mutex_unlock(&codec->hash_mutex); /* for reentrance */
1644 val = func(codec, nid, dir);
1645 write_caps_hash(codec, key, val);
1646 } else {
1647 val = info->amp_caps;
1648 mutex_unlock(&codec->hash_mutex);
1649 }
1650 return val;
1651}
1652
1653static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid,
1654 int direction)
1655{
1656 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1657 nid = codec->afg;
1658 return snd_hda_param_read(codec, nid,
1659 direction == HDA_OUTPUT ?
1660 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
1661}
1662
Takashi Iwaid5191e52009-11-16 14:58:17 +01001663/**
1664 * query_amp_caps - query AMP capabilities
1665 * @codec: the HD-auio codec
1666 * @nid: the NID to query
1667 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1668 *
1669 * Query AMP capabilities for the given widget and direction.
1670 * Returns the obtained capability bits.
1671 *
1672 * When cap bits have been already read, this doesn't read again but
1673 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001675u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001677 return query_caps_hash(codec, nid, direction,
1678 HDA_HASH_KEY(nid, direction, 0),
1679 read_amp_cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001681EXPORT_SYMBOL_HDA(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682
Takashi Iwaid5191e52009-11-16 14:58:17 +01001683/**
1684 * snd_hda_override_amp_caps - Override the AMP capabilities
1685 * @codec: the CODEC to clean up
1686 * @nid: the NID to clean up
1687 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1688 * @caps: the capability bits to set
1689 *
1690 * Override the cached AMP caps bits value by the given one.
1691 * This function is useful if the driver needs to adjust the AMP ranges,
1692 * e.g. limit to 0dB, etc.
1693 *
1694 * Returns zero if successful or a negative error code.
1695 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001696int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1697 unsigned int caps)
1698{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001699 return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001700}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001701EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001702
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001703static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid,
1704 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001705{
1706 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1707}
1708
Takashi Iwaid5191e52009-11-16 14:58:17 +01001709/**
1710 * snd_hda_query_pin_caps - Query PIN capabilities
1711 * @codec: the HD-auio codec
1712 * @nid: the NID to query
1713 *
1714 * Query PIN capabilities for the given widget.
1715 * Returns the obtained capability bits.
1716 *
1717 * When cap bits have been already read, this doesn't read again but
1718 * returns the cached value.
1719 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001720u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1721{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001722 return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001723 read_pin_cap);
1724}
Takashi Iwai1327a322009-03-23 13:07:47 +01001725EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1726
Wu Fengguang864f92b2009-11-18 12:38:02 +08001727/**
Takashi Iwaif57c2562011-08-15 12:49:07 +02001728 * snd_hda_override_pin_caps - Override the pin capabilities
1729 * @codec: the CODEC
1730 * @nid: the NID to override
1731 * @caps: the capability bits to set
1732 *
1733 * Override the cached PIN capabilitiy bits value by the given one.
1734 *
1735 * Returns zero if successful or a negative error code.
1736 */
1737int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
1738 unsigned int caps)
1739{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001740 return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps);
Takashi Iwaif57c2562011-08-15 12:49:07 +02001741}
1742EXPORT_SYMBOL_HDA(snd_hda_override_pin_caps);
1743
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001744/* read or sync the hash value with the current value;
1745 * call within hash_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 */
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001747static struct hda_amp_info *
1748update_amp_hash(struct hda_codec *codec, hda_nid_t nid, int ch,
1749 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001751 struct hda_amp_info *info;
1752 unsigned int parm, val = 0;
1753 bool val_read = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001755 retry:
1756 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1757 if (!info)
1758 return NULL;
1759 if (!(info->head.val & INFO_AMP_VOL(ch))) {
1760 if (!val_read) {
1761 mutex_unlock(&codec->hash_mutex);
1762 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1763 parm |= direction == HDA_OUTPUT ?
1764 AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1765 parm |= index;
1766 val = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001767 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001768 val &= 0xff;
1769 val_read = true;
1770 mutex_lock(&codec->hash_mutex);
1771 goto retry;
1772 }
1773 info->vol[ch] = val;
1774 info->head.val |= INFO_AMP_VOL(ch);
1775 }
1776 return info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777}
1778
1779/*
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001780 * write the current volume in info to the h/w
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001782static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001783 hda_nid_t nid, int ch, int direction, int index,
1784 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785{
1786 u32 parm;
1787
1788 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1789 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1790 parm |= index << AC_AMP_SET_INDEX_SHIFT;
Takashi Iwai38681372012-02-27 15:00:58 +01001791 if ((val & HDA_AMP_MUTE) && !(info->amp_caps & AC_AMPCAP_MUTE) &&
1792 (info->amp_caps & AC_AMPCAP_MIN_MUTE))
1793 ; /* set the zero value as a fake mute */
1794 else
1795 parm |= val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
1797}
1798
Takashi Iwaid5191e52009-11-16 14:58:17 +01001799/**
1800 * snd_hda_codec_amp_read - Read AMP value
1801 * @codec: HD-audio codec
1802 * @nid: NID to read the AMP value
1803 * @ch: channel (left=0 or right=1)
1804 * @direction: #HDA_INPUT or #HDA_OUTPUT
1805 * @index: the index value (only for input direction)
1806 *
1807 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 */
Takashi Iwai834be882006-03-01 14:16:17 +01001809int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1810 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001812 struct hda_amp_info *info;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001813 unsigned int val = 0;
1814
1815 mutex_lock(&codec->hash_mutex);
1816 info = update_amp_hash(codec, nid, ch, direction, index);
1817 if (info)
1818 val = info->vol[ch];
1819 mutex_unlock(&codec->hash_mutex);
1820 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001822EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
Takashi Iwaid5191e52009-11-16 14:58:17 +01001824/**
1825 * snd_hda_codec_amp_update - update the AMP value
1826 * @codec: HD-audio codec
1827 * @nid: NID to read the AMP value
1828 * @ch: channel (left=0 or right=1)
1829 * @direction: #HDA_INPUT or #HDA_OUTPUT
1830 * @idx: the index value (only for input direction)
1831 * @mask: bit mask to set
1832 * @val: the bits value to set
1833 *
1834 * Update the AMP value with a bit mask.
1835 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001836 */
Takashi Iwai834be882006-03-01 14:16:17 +01001837int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1838 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001840 struct hda_amp_info *info;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001841
Takashi Iwai467126462010-03-29 09:19:38 +02001842 if (snd_BUG_ON(mask & ~0xff))
1843 mask &= 0xff;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001844 val &= mask;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001845
1846 mutex_lock(&codec->hash_mutex);
1847 info = update_amp_hash(codec, nid, ch, direction, idx);
1848 if (!info) {
1849 mutex_unlock(&codec->hash_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 return 0;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001851 }
1852 val |= info->vol[ch] & ~mask;
1853 if (info->vol[ch] == val) {
1854 mutex_unlock(&codec->hash_mutex);
1855 return 0;
1856 }
1857 info->vol[ch] = val;
1858 mutex_unlock(&codec->hash_mutex);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001859 put_vol_mute(codec, info, nid, ch, direction, idx, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 return 1;
1861}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001862EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863
Takashi Iwaid5191e52009-11-16 14:58:17 +01001864/**
1865 * snd_hda_codec_amp_stereo - update the AMP stereo values
1866 * @codec: HD-audio codec
1867 * @nid: NID to read the AMP value
1868 * @direction: #HDA_INPUT or #HDA_OUTPUT
1869 * @idx: the index value (only for input direction)
1870 * @mask: bit mask to set
1871 * @val: the bits value to set
1872 *
1873 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1874 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02001875 */
1876int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1877 int direction, int idx, int mask, int val)
1878{
1879 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001880
1881 if (snd_BUG_ON(mask & ~0xff))
1882 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02001883 for (ch = 0; ch < 2; ch++)
1884 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1885 idx, mask, val);
1886 return ret;
1887}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001888EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001889
Takashi Iwai2a439522011-07-26 09:52:50 +02001890#ifdef CONFIG_PM
Takashi Iwaid5191e52009-11-16 14:58:17 +01001891/**
1892 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
1893 * @codec: HD-audio codec
1894 *
1895 * Resume the all amp commands from the cache.
1896 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001897void snd_hda_codec_resume_amp(struct hda_codec *codec)
1898{
Takashi Iwai603c4012008-07-30 15:01:44 +02001899 struct hda_amp_info *buffer = codec->amp_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001900 int i;
1901
Takashi Iwai603c4012008-07-30 15:01:44 +02001902 for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001903 u32 key = buffer->head.key;
1904 hda_nid_t nid;
1905 unsigned int idx, dir, ch;
1906 if (!key)
1907 continue;
1908 nid = key & 0xff;
1909 idx = (key >> 16) & 0xff;
1910 dir = (key >> 24) & 0xff;
1911 for (ch = 0; ch < 2; ch++) {
1912 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
1913 continue;
1914 put_vol_mute(codec, buffer, nid, ch, dir, idx,
1915 buffer->vol[ch]);
1916 }
1917 }
1918}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001919EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
Takashi Iwai2a439522011-07-26 09:52:50 +02001920#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001922static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1923 unsigned int ofs)
1924{
1925 u32 caps = query_amp_caps(codec, nid, dir);
1926 /* get num steps */
1927 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1928 if (ofs < caps)
1929 caps -= ofs;
1930 return caps;
1931}
1932
Takashi Iwaid5191e52009-11-16 14:58:17 +01001933/**
1934 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
1935 *
1936 * The control element is supposed to have the private_value field
1937 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1938 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001939int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1940 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941{
1942 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1943 u16 nid = get_amp_nid(kcontrol);
1944 u8 chs = get_amp_channels(kcontrol);
1945 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001946 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001948 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1949 uinfo->count = chs == 3 ? 2 : 1;
1950 uinfo->value.integer.min = 0;
1951 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
1952 if (!uinfo->value.integer.max) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001953 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01001954 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
1955 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 return -EINVAL;
1957 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 return 0;
1959}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001960EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001962
1963static inline unsigned int
1964read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1965 int ch, int dir, int idx, unsigned int ofs)
1966{
1967 unsigned int val;
1968 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1969 val &= HDA_AMP_VOLMASK;
1970 if (val >= ofs)
1971 val -= ofs;
1972 else
1973 val = 0;
1974 return val;
1975}
1976
1977static inline int
1978update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1979 int ch, int dir, int idx, unsigned int ofs,
1980 unsigned int val)
1981{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001982 unsigned int maxval;
1983
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001984 if (val > 0)
1985 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02001986 /* ofs = 0: raw max value */
1987 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001988 if (val > maxval)
1989 val = maxval;
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001990 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
1991 HDA_AMP_VOLMASK, val);
1992}
1993
Takashi Iwaid5191e52009-11-16 14:58:17 +01001994/**
1995 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
1996 *
1997 * The control element is supposed to have the private_value field
1998 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1999 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002000int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
2001 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002{
2003 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2004 hda_nid_t nid = get_amp_nid(kcontrol);
2005 int chs = get_amp_channels(kcontrol);
2006 int dir = get_amp_direction(kcontrol);
2007 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002008 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 long *valp = ucontrol->value.integer.value;
2010
2011 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002012 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002014 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 return 0;
2016}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002017EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018
Takashi Iwaid5191e52009-11-16 14:58:17 +01002019/**
2020 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
2021 *
2022 * The control element is supposed to have the private_value field
2023 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2024 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002025int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
2026 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027{
2028 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2029 hda_nid_t nid = get_amp_nid(kcontrol);
2030 int chs = get_amp_channels(kcontrol);
2031 int dir = get_amp_direction(kcontrol);
2032 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002033 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 long *valp = ucontrol->value.integer.value;
2035 int change = 0;
2036
Takashi Iwaicb53c622007-08-10 17:21:45 +02002037 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002038 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002039 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002040 valp++;
2041 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002042 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002043 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002044 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 return change;
2046}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002047EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048
Takashi Iwaid5191e52009-11-16 14:58:17 +01002049/**
2050 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2051 *
2052 * The control element is supposed to have the private_value field
2053 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2054 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002055int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2056 unsigned int size, unsigned int __user *_tlv)
2057{
2058 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2059 hda_nid_t nid = get_amp_nid(kcontrol);
2060 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002061 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02002062 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002063 u32 caps, val1, val2;
2064
2065 if (size < 4 * sizeof(unsigned int))
2066 return -ENOMEM;
2067 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002068 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2069 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002070 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002071 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002072 val1 = ((int)val1) * ((int)val2);
Takashi Iwai38681372012-02-27 15:00:58 +01002073 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwaic08d9162010-10-17 10:40:53 +02002074 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002075 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2076 return -EFAULT;
2077 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2078 return -EFAULT;
2079 if (put_user(val1, _tlv + 2))
2080 return -EFAULT;
2081 if (put_user(val2, _tlv + 3))
2082 return -EFAULT;
2083 return 0;
2084}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002085EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002086
Takashi Iwaid5191e52009-11-16 14:58:17 +01002087/**
2088 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2089 * @codec: HD-audio codec
2090 * @nid: NID of a reference widget
2091 * @dir: #HDA_INPUT or #HDA_OUTPUT
2092 * @tlv: TLV data to be stored, at least 4 elements
2093 *
2094 * Set (static) TLV data for a virtual master volume using the AMP caps
2095 * obtained from the reference NID.
2096 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01002097 */
2098void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2099 unsigned int *tlv)
2100{
2101 u32 caps;
2102 int nums, step;
2103
2104 caps = query_amp_caps(codec, nid, dir);
2105 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2106 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2107 step = (step + 1) * 25;
2108 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2109 tlv[1] = 2 * sizeof(unsigned int);
2110 tlv[2] = -nums * step;
2111 tlv[3] = step;
2112}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002113EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002114
2115/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01002116static struct snd_kcontrol *
2117_snd_hda_find_mixer_ctl(struct hda_codec *codec,
2118 const char *name, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002119{
2120 struct snd_ctl_elem_id id;
2121 memset(&id, 0, sizeof(id));
2122 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwai09f99702008-02-04 12:31:13 +01002123 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02002124 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2125 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002126 strcpy(id.name, name);
2127 return snd_ctl_find_id(codec->bus->card, &id);
2128}
2129
Takashi Iwaid5191e52009-11-16 14:58:17 +01002130/**
2131 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2132 * @codec: HD-audio codec
2133 * @name: ctl id name string
2134 *
2135 * Get the control element with the given id string and IFACE_MIXER.
2136 */
Takashi Iwai09f99702008-02-04 12:31:13 +01002137struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2138 const char *name)
2139{
2140 return _snd_hda_find_mixer_ctl(codec, name, 0);
2141}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002142EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01002143
Takashi Iwai1afe2062010-12-23 10:17:52 +01002144static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name)
2145{
2146 int idx;
2147 for (idx = 0; idx < 16; idx++) { /* 16 ctlrs should be large enough */
2148 if (!_snd_hda_find_mixer_ctl(codec, name, idx))
2149 return idx;
2150 }
2151 return -EBUSY;
2152}
2153
Takashi Iwaid5191e52009-11-16 14:58:17 +01002154/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002155 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01002156 * @codec: HD-audio codec
2157 * @nid: corresponding NID (optional)
2158 * @kctl: the control element to assign
2159 *
2160 * Add the given control element to an array inside the codec instance.
2161 * All control elements belonging to a codec are supposed to be added
2162 * by this function so that a proper clean-up works at the free or
2163 * reconfiguration time.
2164 *
2165 * If non-zero @nid is passed, the NID is assigned to the control element.
2166 * The assignment is shown in the codec proc file.
2167 *
2168 * snd_hda_ctl_add() checks the control subdev id field whether
2169 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002170 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2171 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01002172 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002173int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2174 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002175{
2176 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002177 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002178 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002179
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002180 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002181 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002182 if (nid == 0)
2183 nid = get_amp_nid_(kctl->private_value);
2184 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002185 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2186 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002187 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01002188 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002189 err = snd_ctl_add(codec->bus->card, kctl);
2190 if (err < 0)
2191 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002192 item = snd_array_new(&codec->mixers);
2193 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002194 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002195 item->kctl = kctl;
2196 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002197 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002198 return 0;
2199}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002200EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002201
Takashi Iwaid5191e52009-11-16 14:58:17 +01002202/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002203 * snd_hda_add_nid - Assign a NID to a control element
2204 * @codec: HD-audio codec
2205 * @nid: corresponding NID (optional)
2206 * @kctl: the control element to assign
2207 * @index: index to kctl
2208 *
2209 * Add the given control element to an array inside the codec instance.
2210 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2211 * NID:KCTL mapping - for example "Capture Source" selector.
2212 */
2213int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2214 unsigned int index, hda_nid_t nid)
2215{
2216 struct hda_nid_item *item;
2217
2218 if (nid > 0) {
2219 item = snd_array_new(&codec->nids);
2220 if (!item)
2221 return -ENOMEM;
2222 item->kctl = kctl;
2223 item->index = index;
2224 item->nid = nid;
2225 return 0;
2226 }
Takashi Iwai28d1a852010-03-15 09:05:46 +01002227 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
2228 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002229 return -EINVAL;
2230}
2231EXPORT_SYMBOL_HDA(snd_hda_add_nid);
2232
2233/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002234 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2235 * @codec: HD-audio codec
2236 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002237void snd_hda_ctls_clear(struct hda_codec *codec)
2238{
2239 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002240 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002241 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002242 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002243 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002244 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002245}
2246
Takashi Iwaia65d6292009-02-23 16:57:04 +01002247/* pseudo device locking
2248 * toggle card->shutdown to allow/disallow the device access (as a hack)
2249 */
2250static int hda_lock_devices(struct snd_card *card)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002251{
Takashi Iwaia65d6292009-02-23 16:57:04 +01002252 spin_lock(&card->files_lock);
2253 if (card->shutdown) {
2254 spin_unlock(&card->files_lock);
2255 return -EINVAL;
2256 }
2257 card->shutdown = 1;
2258 spin_unlock(&card->files_lock);
2259 return 0;
2260}
2261
2262static void hda_unlock_devices(struct snd_card *card)
2263{
2264 spin_lock(&card->files_lock);
2265 card->shutdown = 0;
2266 spin_unlock(&card->files_lock);
2267}
2268
Takashi Iwaid5191e52009-11-16 14:58:17 +01002269/**
2270 * snd_hda_codec_reset - Clear all objects assigned to the codec
2271 * @codec: HD-audio codec
2272 *
2273 * This frees the all PCM and control elements assigned to the codec, and
2274 * clears the caches and restores the pin default configurations.
2275 *
2276 * When a device is being used, it returns -EBSY. If successfully freed,
2277 * returns zero.
2278 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002279int snd_hda_codec_reset(struct hda_codec *codec)
2280{
2281 struct snd_card *card = codec->bus->card;
2282 int i, pcm;
2283
2284 if (hda_lock_devices(card) < 0)
2285 return -EBUSY;
2286 /* check whether the codec isn't used by any mixer or PCM streams */
2287 if (!list_empty(&card->ctl_files)) {
2288 hda_unlock_devices(card);
2289 return -EBUSY;
2290 }
2291 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2292 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2293 if (!cpcm->pcm)
2294 continue;
2295 if (cpcm->pcm->streams[0].substream_opened ||
2296 cpcm->pcm->streams[1].substream_opened) {
2297 hda_unlock_devices(card);
2298 return -EBUSY;
2299 }
2300 }
2301
2302 /* OK, let it free */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002303
2304#ifdef CONFIG_SND_HDA_POWER_SAVE
Takashi Iwaia2d96e72012-05-09 12:36:22 +02002305 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwai339876d2012-05-08 16:57:12 +02002306 codec->power_on = 0;
2307 codec->power_transition = 0;
2308 codec->power_jiffies = jiffies;
Takashi Iwai6acaed32009-01-12 10:09:24 +01002309 flush_workqueue(codec->bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002310#endif
2311 snd_hda_ctls_clear(codec);
2312 /* relase PCMs */
2313 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002314 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01002315 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002316 clear_bit(codec->pcm_info[i].device,
2317 codec->bus->pcm_dev_bits);
2318 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002319 }
2320 if (codec->patch_ops.free)
2321 codec->patch_ops.free(codec);
Takashi Iwai1835a0f2011-10-27 22:12:46 +02002322 snd_hda_jack_tbl_clear(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01002323 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002324 codec->spec = NULL;
2325 free_hda_cache(&codec->amp_cache);
2326 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01002327 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2328 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01002329 /* free only driver_pins so that init_pins + user_pins are restored */
2330 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01002331 restore_pincfgs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002332 codec->num_pcms = 0;
2333 codec->pcm_info = NULL;
2334 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01002335 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
2336 codec->slave_dig_outs = NULL;
2337 codec->spdif_status_reset = 0;
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002338 module_put(codec->owner);
2339 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002340
2341 /* allow device access again */
2342 hda_unlock_devices(card);
2343 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002344}
2345
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002346typedef int (*map_slave_func_t)(void *, struct snd_kcontrol *);
2347
2348/* apply the function to all matching slave ctls in the mixer list */
2349static int map_slaves(struct hda_codec *codec, const char * const *slaves,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002350 const char *suffix, map_slave_func_t func, void *data)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002351{
2352 struct hda_nid_item *items;
2353 const char * const *s;
2354 int i, err;
2355
2356 items = codec->mixers.list;
2357 for (i = 0; i < codec->mixers.used; i++) {
2358 struct snd_kcontrol *sctl = items[i].kctl;
2359 if (!sctl || !sctl->id.name ||
2360 sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
2361 continue;
2362 for (s = slaves; *s; s++) {
Takashi Iwai9322ca52012-02-03 14:28:01 +01002363 char tmpname[sizeof(sctl->id.name)];
2364 const char *name = *s;
2365 if (suffix) {
2366 snprintf(tmpname, sizeof(tmpname), "%s %s",
2367 name, suffix);
2368 name = tmpname;
2369 }
Takashi Iwai9322ca52012-02-03 14:28:01 +01002370 if (!strcmp(sctl->id.name, name)) {
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002371 err = func(data, sctl);
2372 if (err)
2373 return err;
2374 break;
2375 }
2376 }
2377 }
2378 return 0;
2379}
2380
2381static int check_slave_present(void *data, struct snd_kcontrol *sctl)
2382{
2383 return 1;
2384}
2385
Takashi Iwai18478e82012-03-09 17:51:10 +01002386/* guess the value corresponding to 0dB */
2387static int get_kctl_0dB_offset(struct snd_kcontrol *kctl)
2388{
2389 int _tlv[4];
2390 const int *tlv = NULL;
2391 int val = -1;
2392
2393 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2394 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2395 mm_segment_t fs = get_fs();
2396 set_fs(get_ds());
2397 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2398 tlv = _tlv;
2399 set_fs(fs);
2400 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2401 tlv = kctl->tlv.p;
2402 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE)
2403 val = -tlv[2] / tlv[3];
2404 return val;
2405}
2406
2407/* call kctl->put with the given value(s) */
2408static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2409{
2410 struct snd_ctl_elem_value *ucontrol;
2411 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2412 if (!ucontrol)
2413 return -ENOMEM;
2414 ucontrol->value.integer.value[0] = val;
2415 ucontrol->value.integer.value[1] = val;
2416 kctl->put(kctl, ucontrol);
2417 kfree(ucontrol);
2418 return 0;
2419}
2420
2421/* initialize the slave volume with 0dB */
2422static int init_slave_0dB(void *data, struct snd_kcontrol *slave)
2423{
2424 int offset = get_kctl_0dB_offset(slave);
2425 if (offset > 0)
2426 put_kctl_with_value(slave, offset);
2427 return 0;
2428}
2429
2430/* unmute the slave */
2431static int init_slave_unmute(void *data, struct snd_kcontrol *slave)
2432{
2433 return put_kctl_with_value(slave, 1);
2434}
2435
Takashi Iwaid5191e52009-11-16 14:58:17 +01002436/**
2437 * snd_hda_add_vmaster - create a virtual master control and add slaves
2438 * @codec: HD-audio codec
2439 * @name: vmaster control name
2440 * @tlv: TLV data (optional)
2441 * @slaves: slave control names (optional)
Takashi Iwai9322ca52012-02-03 14:28:01 +01002442 * @suffix: suffix string to each slave name (optional)
Takashi Iwai18478e82012-03-09 17:51:10 +01002443 * @init_slave_vol: initialize slaves to unmute/0dB
Takashi Iwai29e58532012-03-12 12:25:03 +01002444 * @ctl_ret: store the vmaster kcontrol in return
Takashi Iwaid5191e52009-11-16 14:58:17 +01002445 *
2446 * Create a virtual master control with the given name. The TLV data
2447 * must be either NULL or a valid data.
2448 *
2449 * @slaves is a NULL-terminated array of strings, each of which is a
2450 * slave control name. All controls with these names are assigned to
2451 * the new virtual master control.
2452 *
2453 * This function returns zero if successful or a negative error code.
2454 */
Takashi Iwai18478e82012-03-09 17:51:10 +01002455int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002456 unsigned int *tlv, const char * const *slaves,
Takashi Iwai29e58532012-03-12 12:25:03 +01002457 const char *suffix, bool init_slave_vol,
2458 struct snd_kcontrol **ctl_ret)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002459{
2460 struct snd_kcontrol *kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002461 int err;
2462
Takashi Iwai29e58532012-03-12 12:25:03 +01002463 if (ctl_ret)
2464 *ctl_ret = NULL;
2465
Takashi Iwai9322ca52012-02-03 14:28:01 +01002466 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002467 if (err != 1) {
Takashi Iwai2f085542008-02-22 18:43:50 +01002468 snd_printdd("No slave found for %s\n", name);
2469 return 0;
2470 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002471 kctl = snd_ctl_make_virtual_master(name, tlv);
2472 if (!kctl)
2473 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002474 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002475 if (err < 0)
2476 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002477
Takashi Iwai9322ca52012-02-03 14:28:01 +01002478 err = map_slaves(codec, slaves, suffix,
2479 (map_slave_func_t)snd_ctl_add_slave, kctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002480 if (err < 0)
2481 return err;
Takashi Iwai18478e82012-03-09 17:51:10 +01002482
2483 /* init with master mute & zero volume */
2484 put_kctl_with_value(kctl, 0);
2485 if (init_slave_vol)
2486 map_slaves(codec, slaves, suffix,
2487 tlv ? init_slave_0dB : init_slave_unmute, kctl);
2488
Takashi Iwai29e58532012-03-12 12:25:03 +01002489 if (ctl_ret)
2490 *ctl_ret = kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002491 return 0;
2492}
Takashi Iwai18478e82012-03-09 17:51:10 +01002493EXPORT_SYMBOL_HDA(__snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002494
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002495/*
2496 * mute-LED control using vmaster
2497 */
2498static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2499 struct snd_ctl_elem_info *uinfo)
2500{
2501 static const char * const texts[] = {
2502 "Off", "On", "Follow Master"
2503 };
2504 unsigned int index;
2505
2506 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2507 uinfo->count = 1;
2508 uinfo->value.enumerated.items = 3;
2509 index = uinfo->value.enumerated.item;
2510 if (index >= 3)
2511 index = 2;
2512 strcpy(uinfo->value.enumerated.name, texts[index]);
2513 return 0;
2514}
2515
2516static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2517 struct snd_ctl_elem_value *ucontrol)
2518{
2519 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2520 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2521 return 0;
2522}
2523
2524static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2525 struct snd_ctl_elem_value *ucontrol)
2526{
2527 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2528 unsigned int old_mode = hook->mute_mode;
2529
2530 hook->mute_mode = ucontrol->value.enumerated.item[0];
2531 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2532 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2533 if (old_mode == hook->mute_mode)
2534 return 0;
2535 snd_hda_sync_vmaster_hook(hook);
2536 return 1;
2537}
2538
2539static struct snd_kcontrol_new vmaster_mute_mode = {
2540 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2541 .name = "Mute-LED Mode",
2542 .info = vmaster_mute_mode_info,
2543 .get = vmaster_mute_mode_get,
2544 .put = vmaster_mute_mode_put,
2545};
2546
2547/*
2548 * Add a mute-LED hook with the given vmaster switch kctl
2549 * "Mute-LED Mode" control is automatically created and associated with
2550 * the given hook.
2551 */
2552int snd_hda_add_vmaster_hook(struct hda_codec *codec,
Takashi Iwaif29735c2012-03-13 07:55:10 +01002553 struct hda_vmaster_mute_hook *hook,
2554 bool expose_enum_ctl)
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002555{
2556 struct snd_kcontrol *kctl;
2557
2558 if (!hook->hook || !hook->sw_kctl)
2559 return 0;
2560 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
2561 hook->codec = codec;
2562 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
Takashi Iwaif29735c2012-03-13 07:55:10 +01002563 if (!expose_enum_ctl)
2564 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002565 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2566 if (!kctl)
2567 return -ENOMEM;
2568 return snd_hda_ctl_add(codec, 0, kctl);
2569}
2570EXPORT_SYMBOL_HDA(snd_hda_add_vmaster_hook);
2571
2572/*
2573 * Call the hook with the current value for synchronization
2574 * Should be called in init callback
2575 */
2576void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2577{
2578 if (!hook->hook || !hook->codec)
2579 return;
2580 switch (hook->mute_mode) {
2581 case HDA_VMUTE_FOLLOW_MASTER:
2582 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2583 break;
2584 default:
2585 hook->hook(hook->codec, hook->mute_mode);
2586 break;
2587 }
2588}
2589EXPORT_SYMBOL_HDA(snd_hda_sync_vmaster_hook);
2590
2591
Takashi Iwaid5191e52009-11-16 14:58:17 +01002592/**
2593 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2594 *
2595 * The control element is supposed to have the private_value field
2596 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2597 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002598int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2599 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600{
2601 int chs = get_amp_channels(kcontrol);
2602
2603 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2604 uinfo->count = chs == 3 ? 2 : 1;
2605 uinfo->value.integer.min = 0;
2606 uinfo->value.integer.max = 1;
2607 return 0;
2608}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002609EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610
Takashi Iwaid5191e52009-11-16 14:58:17 +01002611/**
2612 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2613 *
2614 * The control element is supposed to have the private_value field
2615 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2616 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002617int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2618 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619{
2620 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2621 hda_nid_t nid = get_amp_nid(kcontrol);
2622 int chs = get_amp_channels(kcontrol);
2623 int dir = get_amp_direction(kcontrol);
2624 int idx = get_amp_index(kcontrol);
2625 long *valp = ucontrol->value.integer.value;
2626
2627 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002628 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002629 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002631 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002632 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 return 0;
2634}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002635EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636
Takashi Iwaid5191e52009-11-16 14:58:17 +01002637/**
2638 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2639 *
2640 * The control element is supposed to have the private_value field
2641 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2642 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002643int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2644 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645{
2646 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2647 hda_nid_t nid = get_amp_nid(kcontrol);
2648 int chs = get_amp_channels(kcontrol);
2649 int dir = get_amp_direction(kcontrol);
2650 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 long *valp = ucontrol->value.integer.value;
2652 int change = 0;
2653
Takashi Iwaicb53c622007-08-10 17:21:45 +02002654 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002655 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002656 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002657 HDA_AMP_MUTE,
2658 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002659 valp++;
2660 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002661 if (chs & 2)
2662 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002663 HDA_AMP_MUTE,
2664 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002665 hda_call_check_power_status(codec, nid);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002666 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 return change;
2668}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002669EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670
Takashi Iwai67d634c2009-11-16 15:35:59 +01002671#ifdef CONFIG_SND_HDA_INPUT_BEEP
Takashi Iwaid5191e52009-11-16 14:58:17 +01002672/**
2673 * snd_hda_mixer_amp_switch_put_beep - Put callback for a beep AMP switch
2674 *
2675 * This function calls snd_hda_enable_beep_device(), which behaves differently
2676 * depending on beep_mode option.
2677 */
Jaroslav Kysela123c07a2009-10-21 14:48:23 +02002678int snd_hda_mixer_amp_switch_put_beep(struct snd_kcontrol *kcontrol,
2679 struct snd_ctl_elem_value *ucontrol)
2680{
2681 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2682 long *valp = ucontrol->value.integer.value;
2683
2684 snd_hda_enable_beep_device(codec, *valp);
2685 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2686}
2687EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put_beep);
Takashi Iwai67d634c2009-11-16 15:35:59 +01002688#endif /* CONFIG_SND_HDA_INPUT_BEEP */
Jaroslav Kysela123c07a2009-10-21 14:48:23 +02002689
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690/*
Takashi Iwai985be542005-11-02 18:26:49 +01002691 * bound volume controls
2692 *
2693 * bind multiple volumes (# indices, from 0)
2694 */
2695
2696#define AMP_VAL_IDX_SHIFT 19
2697#define AMP_VAL_IDX_MASK (0x0f<<19)
2698
Takashi Iwaid5191e52009-11-16 14:58:17 +01002699/**
2700 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2701 *
2702 * The control element is supposed to have the private_value field
2703 * set up via HDA_BIND_MUTE*() macros.
2704 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002705int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2706 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002707{
2708 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2709 unsigned long pval;
2710 int err;
2711
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002712 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002713 pval = kcontrol->private_value;
2714 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2715 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2716 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002717 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002718 return err;
2719}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002720EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002721
Takashi Iwaid5191e52009-11-16 14:58:17 +01002722/**
2723 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2724 *
2725 * The control element is supposed to have the private_value field
2726 * set up via HDA_BIND_MUTE*() macros.
2727 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002728int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2729 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002730{
2731 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2732 unsigned long pval;
2733 int i, indices, err = 0, change = 0;
2734
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002735 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002736 pval = kcontrol->private_value;
2737 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2738 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002739 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2740 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002741 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2742 if (err < 0)
2743 break;
2744 change |= err;
2745 }
2746 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002747 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002748 return err < 0 ? err : change;
2749}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002750EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002751
Takashi Iwaid5191e52009-11-16 14:58:17 +01002752/**
2753 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2754 *
2755 * The control element is supposed to have the private_value field
2756 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002757 */
2758int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2759 struct snd_ctl_elem_info *uinfo)
2760{
2761 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2762 struct hda_bind_ctls *c;
2763 int err;
2764
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002765 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002766 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002767 kcontrol->private_value = *c->values;
2768 err = c->ops->info(kcontrol, uinfo);
2769 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002770 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002771 return err;
2772}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002773EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002774
Takashi Iwaid5191e52009-11-16 14:58:17 +01002775/**
2776 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2777 *
2778 * The control element is supposed to have the private_value field
2779 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2780 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002781int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2782 struct snd_ctl_elem_value *ucontrol)
2783{
2784 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2785 struct hda_bind_ctls *c;
2786 int err;
2787
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002788 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002789 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002790 kcontrol->private_value = *c->values;
2791 err = c->ops->get(kcontrol, ucontrol);
2792 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002793 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002794 return err;
2795}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002796EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002797
Takashi Iwaid5191e52009-11-16 14:58:17 +01002798/**
2799 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2800 *
2801 * The control element is supposed to have the private_value field
2802 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2803 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002804int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2805 struct snd_ctl_elem_value *ucontrol)
2806{
2807 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2808 struct hda_bind_ctls *c;
2809 unsigned long *vals;
2810 int err = 0, change = 0;
2811
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002812 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002813 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002814 for (vals = c->values; *vals; vals++) {
2815 kcontrol->private_value = *vals;
2816 err = c->ops->put(kcontrol, ucontrol);
2817 if (err < 0)
2818 break;
2819 change |= err;
2820 }
2821 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002822 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002823 return err < 0 ? err : change;
2824}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002825EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02002826
Takashi Iwaid5191e52009-11-16 14:58:17 +01002827/**
2828 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2829 *
2830 * The control element is supposed to have the private_value field
2831 * set up via HDA_BIND_VOL() macro.
2832 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002833int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2834 unsigned int size, unsigned int __user *tlv)
2835{
2836 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2837 struct hda_bind_ctls *c;
2838 int err;
2839
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002840 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002841 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002842 kcontrol->private_value = *c->values;
2843 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2844 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002845 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002846 return err;
2847}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002848EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02002849
2850struct hda_ctl_ops snd_hda_bind_vol = {
2851 .info = snd_hda_mixer_amp_volume_info,
2852 .get = snd_hda_mixer_amp_volume_get,
2853 .put = snd_hda_mixer_amp_volume_put,
2854 .tlv = snd_hda_mixer_amp_tlv
2855};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002856EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02002857
2858struct hda_ctl_ops snd_hda_bind_sw = {
2859 .info = snd_hda_mixer_amp_switch_info,
2860 .get = snd_hda_mixer_amp_switch_get,
2861 .put = snd_hda_mixer_amp_switch_put,
2862 .tlv = snd_hda_mixer_amp_tlv
2863};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002864EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02002865
2866/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 * SPDIF out controls
2868 */
2869
Takashi Iwai0ba21762007-04-16 11:29:14 +02002870static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2871 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872{
2873 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2874 uinfo->count = 1;
2875 return 0;
2876}
2877
Takashi Iwai0ba21762007-04-16 11:29:14 +02002878static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2879 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880{
2881 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2882 IEC958_AES0_NONAUDIO |
2883 IEC958_AES0_CON_EMPHASIS_5015 |
2884 IEC958_AES0_CON_NOT_COPYRIGHT;
2885 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2886 IEC958_AES1_CON_ORIGINAL;
2887 return 0;
2888}
2889
Takashi Iwai0ba21762007-04-16 11:29:14 +02002890static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2891 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892{
2893 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2894 IEC958_AES0_NONAUDIO |
2895 IEC958_AES0_PRO_EMPHASIS_5015;
2896 return 0;
2897}
2898
Takashi Iwai0ba21762007-04-16 11:29:14 +02002899static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2900 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901{
2902 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002903 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002904 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002906 mutex_lock(&codec->spdif_mutex);
2907 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c9359762011-06-01 11:14:17 -06002908 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
2909 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
2910 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
2911 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002912 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913
2914 return 0;
2915}
2916
2917/* convert from SPDIF status bits to HDA SPDIF bits
2918 * bit 0 (DigEn) is always set zero (to be filled later)
2919 */
2920static unsigned short convert_from_spdif_status(unsigned int sbits)
2921{
2922 unsigned short val = 0;
2923
2924 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002925 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002927 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002929 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2930 IEC958_AES0_PRO_EMPHASIS_5015)
2931 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002933 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2934 IEC958_AES0_CON_EMPHASIS_5015)
2935 val |= AC_DIG1_EMPHASIS;
2936 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2937 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02002939 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2941 }
2942 return val;
2943}
2944
2945/* convert to SPDIF status bits from HDA SPDIF bits
2946 */
2947static unsigned int convert_to_spdif_status(unsigned short val)
2948{
2949 unsigned int sbits = 0;
2950
Takashi Iwai0ba21762007-04-16 11:29:14 +02002951 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002953 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 sbits |= IEC958_AES0_PROFESSIONAL;
2955 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002956 if (sbits & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2958 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002959 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002961 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002963 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2965 sbits |= val & (0x7f << 8);
2966 }
2967 return sbits;
2968}
2969
Takashi Iwai2f728532008-09-25 16:32:41 +02002970/* set digital convert verbs both for the given NID and its slaves */
2971static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
2972 int verb, int val)
2973{
Takashi Iwaidda14412011-05-02 11:29:30 +02002974 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02002975
Takashi Iwai9e976972008-11-25 08:17:20 +01002976 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002977 d = codec->slave_dig_outs;
2978 if (!d)
2979 return;
2980 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01002981 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002982}
2983
2984static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
2985 int dig1, int dig2)
2986{
2987 if (dig1 != -1)
2988 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
2989 if (dig2 != -1)
2990 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
2991}
2992
Takashi Iwai0ba21762007-04-16 11:29:14 +02002993static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
2994 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995{
2996 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002997 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002998 struct hda_spdif_out *spdif;
2999 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 unsigned short val;
3001 int change;
3002
Ingo Molnar62932df2006-01-16 16:34:20 +01003003 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003004 spdif = snd_array_elem(&codec->spdif_out, idx);
3005 nid = spdif->nid;
Stephen Warren7c9359762011-06-01 11:14:17 -06003006 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
3008 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
3009 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c9359762011-06-01 11:14:17 -06003010 val = convert_from_spdif_status(spdif->status);
3011 val |= spdif->ctls & 1;
3012 change = spdif->ctls != val;
3013 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003014 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02003015 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01003016 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 return change;
3018}
3019
Takashi Iwaia5ce8892007-07-23 15:42:26 +02003020#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021
Takashi Iwai0ba21762007-04-16 11:29:14 +02003022static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
3023 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024{
3025 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06003026 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003027 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003029 mutex_lock(&codec->spdif_mutex);
3030 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c9359762011-06-01 11:14:17 -06003031 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003032 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 return 0;
3034}
3035
Stephen Warren74b654c2011-06-01 11:14:18 -06003036static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
3037 int dig1, int dig2)
3038{
3039 set_dig_out_convert(codec, nid, dig1, dig2);
3040 /* unmute amp switch (if any) */
3041 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
3042 (dig1 & AC_DIG1_ENABLE))
3043 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
3044 HDA_AMP_MUTE, 0);
3045}
3046
Takashi Iwai0ba21762007-04-16 11:29:14 +02003047static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
3048 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049{
3050 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06003051 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003052 struct hda_spdif_out *spdif;
3053 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 unsigned short val;
3055 int change;
3056
Ingo Molnar62932df2006-01-16 16:34:20 +01003057 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003058 spdif = snd_array_elem(&codec->spdif_out, idx);
3059 nid = spdif->nid;
Stephen Warren7c9359762011-06-01 11:14:17 -06003060 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02003062 val |= AC_DIG1_ENABLE;
Stephen Warren7c9359762011-06-01 11:14:17 -06003063 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003064 spdif->ctls = val;
3065 if (change && nid != (u16)-1)
3066 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01003067 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 return change;
3069}
3070
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003071static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 {
3073 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3074 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003075 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 .info = snd_hda_spdif_mask_info,
3077 .get = snd_hda_spdif_cmask_get,
3078 },
3079 {
3080 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3081 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003082 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 .info = snd_hda_spdif_mask_info,
3084 .get = snd_hda_spdif_pmask_get,
3085 },
3086 {
3087 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003088 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 .info = snd_hda_spdif_mask_info,
3090 .get = snd_hda_spdif_default_get,
3091 .put = snd_hda_spdif_default_put,
3092 },
3093 {
3094 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003095 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 .info = snd_hda_spdif_out_switch_info,
3097 .get = snd_hda_spdif_out_switch_get,
3098 .put = snd_hda_spdif_out_switch_put,
3099 },
3100 { } /* end */
3101};
3102
3103/**
3104 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
3105 * @codec: the HDA codec
3106 * @nid: audio out widget NID
3107 *
3108 * Creates controls related with the SPDIF output.
3109 * Called from each patch supporting the SPDIF out.
3110 *
3111 * Returns 0 if successful, or a negative error code.
3112 */
Stephen Warren74b654c2011-06-01 11:14:18 -06003113int snd_hda_create_spdif_out_ctls(struct hda_codec *codec,
3114 hda_nid_t associated_nid,
3115 hda_nid_t cvt_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116{
3117 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003118 struct snd_kcontrol *kctl;
3119 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003120 int idx;
Stephen Warren7c9359762011-06-01 11:14:17 -06003121 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122
Takashi Iwai1afe2062010-12-23 10:17:52 +01003123 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch");
3124 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003125 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
3126 return -EBUSY;
3127 }
Stephen Warren7c9359762011-06-01 11:14:17 -06003128 spdif = snd_array_new(&codec->spdif_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3130 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01003131 if (!kctl)
3132 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01003133 kctl->id.index = idx;
Stephen Warren7c9359762011-06-01 11:14:17 -06003134 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06003135 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003136 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 return err;
3138 }
Stephen Warren74b654c2011-06-01 11:14:18 -06003139 spdif->nid = cvt_nid;
3140 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
Stephen Warren7c9359762011-06-01 11:14:17 -06003141 AC_VERB_GET_DIGI_CONVERT_1, 0);
3142 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 return 0;
3144}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003145EXPORT_SYMBOL_HDA(snd_hda_create_spdif_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003147/* get the hda_spdif_out entry from the given NID
3148 * call within spdif_mutex lock
3149 */
Stephen Warren7c9359762011-06-01 11:14:17 -06003150struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3151 hda_nid_t nid)
3152{
3153 int i;
3154 for (i = 0; i < codec->spdif_out.used; i++) {
3155 struct hda_spdif_out *spdif =
3156 snd_array_elem(&codec->spdif_out, i);
3157 if (spdif->nid == nid)
3158 return spdif;
3159 }
3160 return NULL;
3161}
3162EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid);
3163
Stephen Warren74b654c2011-06-01 11:14:18 -06003164void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3165{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003166 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003167
3168 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003169 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003170 spdif->nid = (u16)-1;
3171 mutex_unlock(&codec->spdif_mutex);
3172}
3173EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign);
3174
3175void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3176{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003177 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003178 unsigned short val;
3179
3180 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003181 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003182 if (spdif->nid != nid) {
3183 spdif->nid = nid;
3184 val = spdif->ctls;
3185 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3186 }
3187 mutex_unlock(&codec->spdif_mutex);
3188}
3189EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign);
3190
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191/*
Takashi Iwai9a081602008-02-12 18:37:26 +01003192 * SPDIF sharing with analog output
3193 */
3194static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3195 struct snd_ctl_elem_value *ucontrol)
3196{
3197 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3198 ucontrol->value.integer.value[0] = mout->share_spdif;
3199 return 0;
3200}
3201
3202static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3203 struct snd_ctl_elem_value *ucontrol)
3204{
3205 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3206 mout->share_spdif = !!ucontrol->value.integer.value[0];
3207 return 0;
3208}
3209
3210static struct snd_kcontrol_new spdif_share_sw = {
3211 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3212 .name = "IEC958 Default PCM Playback Switch",
3213 .info = snd_ctl_boolean_mono_info,
3214 .get = spdif_share_sw_get,
3215 .put = spdif_share_sw_put,
3216};
3217
Takashi Iwaid5191e52009-11-16 14:58:17 +01003218/**
3219 * snd_hda_create_spdif_share_sw - create Default PCM switch
3220 * @codec: the HDA codec
3221 * @mout: multi-out instance
3222 */
Takashi Iwai9a081602008-02-12 18:37:26 +01003223int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3224 struct hda_multi_out *mout)
3225{
3226 if (!mout->dig_out_nid)
3227 return 0;
3228 /* ATTENTION: here mout is passed as private_data, instead of codec */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003229 return snd_hda_ctl_add(codec, mout->dig_out_nid,
3230 snd_ctl_new1(&spdif_share_sw, mout));
Takashi Iwai9a081602008-02-12 18:37:26 +01003231}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003232EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01003233
3234/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 * SPDIF input
3236 */
3237
3238#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3239
Takashi Iwai0ba21762007-04-16 11:29:14 +02003240static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3241 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242{
3243 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3244
3245 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3246 return 0;
3247}
3248
Takashi Iwai0ba21762007-04-16 11:29:14 +02003249static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3250 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251{
3252 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3253 hda_nid_t nid = kcontrol->private_value;
3254 unsigned int val = !!ucontrol->value.integer.value[0];
3255 int change;
3256
Ingo Molnar62932df2006-01-16 16:34:20 +01003257 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003259 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003261 snd_hda_codec_write_cache(codec, nid, 0,
3262 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003264 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 return change;
3266}
3267
Takashi Iwai0ba21762007-04-16 11:29:14 +02003268static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3269 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270{
3271 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3272 hda_nid_t nid = kcontrol->private_value;
3273 unsigned short val;
3274 unsigned int sbits;
3275
Andrew Paprocki3982d172007-12-19 12:13:44 +01003276 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 sbits = convert_to_spdif_status(val);
3278 ucontrol->value.iec958.status[0] = sbits;
3279 ucontrol->value.iec958.status[1] = sbits >> 8;
3280 ucontrol->value.iec958.status[2] = sbits >> 16;
3281 ucontrol->value.iec958.status[3] = sbits >> 24;
3282 return 0;
3283}
3284
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003285static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 {
3287 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003288 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 .info = snd_hda_spdif_in_switch_info,
3290 .get = snd_hda_spdif_in_switch_get,
3291 .put = snd_hda_spdif_in_switch_put,
3292 },
3293 {
3294 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3295 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003296 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 .info = snd_hda_spdif_mask_info,
3298 .get = snd_hda_spdif_in_status_get,
3299 },
3300 { } /* end */
3301};
3302
3303/**
3304 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3305 * @codec: the HDA codec
3306 * @nid: audio in widget NID
3307 *
3308 * Creates controls related with the SPDIF input.
3309 * Called from each patch supporting the SPDIF in.
3310 *
3311 * Returns 0 if successful, or a negative error code.
3312 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003313int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314{
3315 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003316 struct snd_kcontrol *kctl;
3317 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003318 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319
Takashi Iwai1afe2062010-12-23 10:17:52 +01003320 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch");
3321 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003322 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
3323 return -EBUSY;
3324 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3326 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01003327 if (!kctl)
3328 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003330 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003331 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 return err;
3333 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003334 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01003335 snd_hda_codec_read(codec, nid, 0,
3336 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02003337 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 return 0;
3339}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003340EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341
Takashi Iwai2a439522011-07-26 09:52:50 +02003342#ifdef CONFIG_PM
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003343/*
3344 * command cache
3345 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003347/* build a 32bit cache key with the widget id and the command parameter */
3348#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3349#define get_cmd_cache_nid(key) ((key) & 0xff)
3350#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3351
3352/**
3353 * snd_hda_codec_write_cache - send a single command with caching
3354 * @codec: the HDA codec
3355 * @nid: NID to send the command
3356 * @direct: direct flag
3357 * @verb: the verb to send
3358 * @parm: the parameter for the verb
3359 *
3360 * Send a single command without waiting for response.
3361 *
3362 * Returns 0 if successful, or a negative error code.
3363 */
3364int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
3365 int direct, unsigned int verb, unsigned int parm)
3366{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003367 int err = snd_hda_codec_write(codec, nid, direct, verb, parm);
3368 struct hda_cache_head *c;
3369 u32 key;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003370
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003371 if (err < 0)
3372 return err;
3373 /* parm may contain the verb stuff for get/set amp */
3374 verb = verb | (parm >> 8);
3375 parm &= 0xff;
3376 key = build_cmd_cache_key(nid, verb);
3377 mutex_lock(&codec->bus->cmd_mutex);
3378 c = get_alloc_hash(&codec->cmd_cache, key);
3379 if (c)
3380 c->val = parm;
3381 mutex_unlock(&codec->bus->cmd_mutex);
3382 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003383}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003384EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003385
Takashi Iwaid5191e52009-11-16 14:58:17 +01003386/**
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003387 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3388 * @codec: the HDA codec
3389 * @nid: NID to send the command
3390 * @direct: direct flag
3391 * @verb: the verb to send
3392 * @parm: the parameter for the verb
3393 *
3394 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3395 * command if the parameter is already identical with the cached value.
3396 * If not, it sends the command and refreshes the cache.
3397 *
3398 * Returns 0 if successful, or a negative error code.
3399 */
3400int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
3401 int direct, unsigned int verb, unsigned int parm)
3402{
3403 struct hda_cache_head *c;
3404 u32 key;
3405
3406 /* parm may contain the verb stuff for get/set amp */
3407 verb = verb | (parm >> 8);
3408 parm &= 0xff;
3409 key = build_cmd_cache_key(nid, verb);
3410 mutex_lock(&codec->bus->cmd_mutex);
3411 c = get_hash(&codec->cmd_cache, key);
3412 if (c && c->val == parm) {
3413 mutex_unlock(&codec->bus->cmd_mutex);
3414 return 0;
3415 }
3416 mutex_unlock(&codec->bus->cmd_mutex);
3417 return snd_hda_codec_write_cache(codec, nid, direct, verb, parm);
3418}
3419EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
3420
3421/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003422 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3423 * @codec: HD-audio codec
3424 *
3425 * Execute all verbs recorded in the command caches to resume.
3426 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003427void snd_hda_codec_resume_cache(struct hda_codec *codec)
3428{
Takashi Iwai603c4012008-07-30 15:01:44 +02003429 struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003430 int i;
3431
Takashi Iwai603c4012008-07-30 15:01:44 +02003432 for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003433 u32 key = buffer->key;
3434 if (!key)
3435 continue;
3436 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3437 get_cmd_cache_cmd(key), buffer->val);
3438 }
3439}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003440EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003441
3442/**
3443 * snd_hda_sequence_write_cache - sequence writes with caching
3444 * @codec: the HDA codec
3445 * @seq: VERB array to send
3446 *
3447 * Send the commands sequentially from the given array.
3448 * Thte commands are recorded on cache for power-save and resume.
3449 * The array must be terminated with NID=0.
3450 */
3451void snd_hda_sequence_write_cache(struct hda_codec *codec,
3452 const struct hda_verb *seq)
3453{
3454 for (; seq->nid; seq++)
3455 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3456 seq->param);
3457}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003458EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
Takashi Iwai2a439522011-07-26 09:52:50 +02003459#endif /* CONFIG_PM */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003460
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003461void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
3462 unsigned int power_state,
3463 bool eapd_workaround)
Takashi Iwai54d17402005-11-21 16:33:22 +01003464{
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003465 hda_nid_t nid = codec->start_nid;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003466 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01003467
Takashi Iwaicb53c622007-08-10 17:21:45 +02003468 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003469 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003470 if (!(wcaps & AC_WCAP_POWER))
3471 continue;
3472 /* don't power down the widget if it controls eapd and
3473 * EAPD_BTLENABLE is set.
3474 */
3475 if (eapd_workaround && power_state == AC_PWRST_D3 &&
3476 get_wcaps_type(wcaps) == AC_WID_PIN &&
3477 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3478 int eapd = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003479 AC_VERB_GET_EAPD_BTLENABLE, 0);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003480 if (eapd & 0x02)
3481 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003482 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003483 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
3484 power_state);
Takashi Iwai54d17402005-11-21 16:33:22 +01003485 }
3486
Takashi Iwaicb53c622007-08-10 17:21:45 +02003487 if (power_state == AC_PWRST_D0) {
3488 unsigned long end_time;
3489 int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003490 /* wait until the codec reachs to D0 */
3491 end_time = jiffies + msecs_to_jiffies(500);
3492 do {
3493 state = snd_hda_codec_read(codec, fg, 0,
3494 AC_VERB_GET_POWER_STATE, 0);
3495 if (state == power_state)
3496 break;
3497 msleep(1);
3498 } while (time_after_eq(end_time, jiffies));
3499 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003500}
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003501EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all);
3502
3503/*
3504 * set power state of the codec
3505 */
3506static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
3507 unsigned int power_state)
3508{
3509 if (codec->patch_ops.set_power_state) {
3510 codec->patch_ops.set_power_state(codec, fg, power_state);
3511 return;
3512 }
3513
3514 /* this delay seems necessary to avoid click noise at power-down */
3515 if (power_state == AC_PWRST_D3)
3516 msleep(100);
3517 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE,
3518 power_state);
3519 snd_hda_codec_set_power_to_all(codec, fg, power_state, true);
3520}
Takashi Iwai54d17402005-11-21 16:33:22 +01003521
Takashi Iwai11aeff02008-07-30 15:01:46 +02003522#ifdef CONFIG_SND_HDA_HWDEP
3523/* execute additional init verbs */
3524static void hda_exec_init_verbs(struct hda_codec *codec)
3525{
3526 if (codec->init_verbs.list)
3527 snd_hda_sequence_write(codec, codec->init_verbs.list);
3528}
3529#else
3530static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3531#endif
3532
Takashi Iwai2a439522011-07-26 09:52:50 +02003533#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003534/*
3535 * call suspend and power-down; used both from PM and power-save
3536 */
3537static void hda_call_codec_suspend(struct hda_codec *codec)
3538{
3539 if (codec->patch_ops.suspend)
3540 codec->patch_ops.suspend(codec, PMSG_SUSPEND);
Takashi Iwaieb541332010-08-06 13:48:11 +02003541 hda_cleanup_all_streams(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003542 hda_set_power_state(codec,
3543 codec->afg ? codec->afg : codec->mfg,
3544 AC_PWRST_D3);
3545#ifdef CONFIG_SND_HDA_POWER_SAVE
3546 cancel_delayed_work(&codec->power_work);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02003547 spin_lock(&codec->power_lock);
3548 snd_hda_update_power_acct(codec);
3549 trace_hda_power_down(codec);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02003550 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02003551 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003552 codec->power_jiffies = jiffies;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02003553 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003554#endif
3555}
3556
3557/*
3558 * kick up codec; used both from PM and power-save
3559 */
3560static void hda_call_codec_resume(struct hda_codec *codec)
3561{
Takashi Iwai7f308302012-05-08 16:52:23 +02003562 /* set as if powered on for avoiding re-entering the resume
3563 * in the resume / power-save sequence
3564 */
3565 hda_keep_power_on(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003566 hda_set_power_state(codec,
3567 codec->afg ? codec->afg : codec->mfg,
3568 AC_PWRST_D0);
Takashi Iwai3be14142009-02-20 14:11:16 +01003569 restore_pincfgs(codec); /* restore all current pin configs */
Takashi Iwaiac0547d2010-07-05 16:50:13 +02003570 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02003571 hda_exec_init_verbs(codec);
Takashi Iwai1835a0f2011-10-27 22:12:46 +02003572 snd_hda_jack_set_dirty_all(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003573 if (codec->patch_ops.resume)
3574 codec->patch_ops.resume(codec);
3575 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02003576 if (codec->patch_ops.init)
3577 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003578 snd_hda_codec_resume_amp(codec);
3579 snd_hda_codec_resume_cache(codec);
3580 }
Takashi Iwai7f308302012-05-08 16:52:23 +02003581 snd_hda_power_down(codec); /* flag down before returning */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003582}
Takashi Iwai2a439522011-07-26 09:52:50 +02003583#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003584
Takashi Iwai54d17402005-11-21 16:33:22 +01003585
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586/**
3587 * snd_hda_build_controls - build mixer controls
3588 * @bus: the BUS
3589 *
3590 * Creates mixer controls for each codec included in the bus.
3591 *
3592 * Returns 0 if successful, otherwise a negative error code.
3593 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003594int /*__devinit*/ snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003596 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597
Takashi Iwai0ba21762007-04-16 11:29:14 +02003598 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003599 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003600 if (err < 0) {
Takashi Iwai28d1a852010-03-15 09:05:46 +01003601 printk(KERN_ERR "hda_codec: cannot build controls "
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003602 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003603 err = snd_hda_codec_reset(codec);
3604 if (err < 0) {
3605 printk(KERN_ERR
3606 "hda_codec: cannot revert codec\n");
3607 return err;
3608 }
3609 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003611 return 0;
3612}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003613EXPORT_SYMBOL_HDA(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003615int snd_hda_codec_build_controls(struct hda_codec *codec)
3616{
3617 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02003618 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003619 /* continue to initialize... */
3620 if (codec->patch_ops.init)
3621 err = codec->patch_ops.init(codec);
3622 if (!err && codec->patch_ops.build_controls)
3623 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003624 if (err < 0)
3625 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626 return 0;
3627}
3628
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629/*
3630 * stream formats
3631 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02003632struct hda_rate_tbl {
3633 unsigned int hz;
3634 unsigned int alsa_bits;
3635 unsigned int hda_fmt;
3636};
3637
Takashi Iwai92f10b32010-08-03 14:21:00 +02003638/* rate = base * mult / div */
3639#define HDA_RATE(base, mult, div) \
3640 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
3641 (((div) - 1) << AC_FMT_DIV_SHIFT))
3642
Takashi Iwaibefdf312005-08-22 13:57:55 +02003643static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003645
3646 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003647 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
3648 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
3649 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
3650 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
3651 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
3652 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
3653 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
3654 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
3655 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
3656 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
3657 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003658#define AC_PAR_PCM_RATE_BITS 11
3659 /* up to bits 10, 384kHZ isn't supported properly */
3660
3661 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003662 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003663
Takashi Iwaibefdf312005-08-22 13:57:55 +02003664 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665};
3666
3667/**
3668 * snd_hda_calc_stream_format - calculate format bitset
3669 * @rate: the sample rate
3670 * @channels: the number of channels
3671 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3672 * @maxbps: the max. bps
3673 *
3674 * Calculate the format bitset from the given rate, channels and th PCM format.
3675 *
3676 * Return zero if invalid.
3677 */
3678unsigned int snd_hda_calc_stream_format(unsigned int rate,
3679 unsigned int channels,
3680 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03003681 unsigned int maxbps,
3682 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683{
3684 int i;
3685 unsigned int val = 0;
3686
Takashi Iwaibefdf312005-08-22 13:57:55 +02003687 for (i = 0; rate_bits[i].hz; i++)
3688 if (rate_bits[i].hz == rate) {
3689 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690 break;
3691 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003692 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693 snd_printdd("invalid rate %d\n", rate);
3694 return 0;
3695 }
3696
3697 if (channels == 0 || channels > 8) {
3698 snd_printdd("invalid channels %d\n", channels);
3699 return 0;
3700 }
3701 val |= channels - 1;
3702
3703 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003704 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003705 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003706 break;
3707 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003708 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003709 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710 case 20:
3711 case 24:
3712 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003713 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003714 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003716 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02003718 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719 break;
3720 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003721 snd_printdd("invalid format width %d\n",
3722 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723 return 0;
3724 }
3725
Anssi Hannula32c168c2010-08-03 13:28:57 +03003726 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003727 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03003728
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729 return val;
3730}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003731EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003733static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid,
3734 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003735{
3736 unsigned int val = 0;
3737 if (nid != codec->afg &&
3738 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
3739 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
3740 if (!val || val == -1)
3741 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
3742 if (!val || val == -1)
3743 return 0;
3744 return val;
3745}
3746
3747static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3748{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003749 return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003750 get_pcm_param);
3751}
3752
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003753static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid,
3754 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003755{
3756 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
3757 if (!streams || streams == -1)
3758 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
3759 if (!streams || streams == -1)
3760 return 0;
3761 return streams;
3762}
3763
3764static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
3765{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003766 return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003767 get_stream_param);
3768}
3769
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770/**
3771 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
3772 * @codec: the HDA codec
3773 * @nid: NID to query
3774 * @ratesp: the pointer to store the detected rate bitflags
3775 * @formatsp: the pointer to store the detected formats
3776 * @bpsp: the pointer to store the detected format widths
3777 *
3778 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
3779 * or @bsps argument is ignored.
3780 *
3781 * Returns 0 if successful, otherwise a negative error code.
3782 */
Stephen Warren384a48d2011-06-01 11:14:21 -06003783int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
3785{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003786 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003788 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003789 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790
3791 if (ratesp) {
3792 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003793 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02003795 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003797 if (rates == 0) {
3798 snd_printk(KERN_ERR "hda_codec: rates == 0 "
3799 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
3800 nid, val,
3801 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
3802 return -EIO;
3803 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804 *ratesp = rates;
3805 }
3806
3807 if (formatsp || bpsp) {
3808 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003809 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003811 streams = query_stream_param(codec, nid);
3812 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814
3815 bps = 0;
3816 if (streams & AC_SUPFMT_PCM) {
3817 if (val & AC_SUPPCM_BITS_8) {
3818 formats |= SNDRV_PCM_FMTBIT_U8;
3819 bps = 8;
3820 }
3821 if (val & AC_SUPPCM_BITS_16) {
3822 formats |= SNDRV_PCM_FMTBIT_S16_LE;
3823 bps = 16;
3824 }
3825 if (wcaps & AC_WCAP_DIGITAL) {
3826 if (val & AC_SUPPCM_BITS_32)
3827 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
3828 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
3829 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3830 if (val & AC_SUPPCM_BITS_24)
3831 bps = 24;
3832 else if (val & AC_SUPPCM_BITS_20)
3833 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003834 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
3835 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3837 if (val & AC_SUPPCM_BITS_32)
3838 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839 else if (val & AC_SUPPCM_BITS_24)
3840 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02003841 else if (val & AC_SUPPCM_BITS_20)
3842 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843 }
3844 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02003845#if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
Takashi Iwaib5025c52009-07-01 18:05:27 +02003846 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003848 if (!bps)
3849 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02003850 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02003851#endif
Takashi Iwaib5025c52009-07-01 18:05:27 +02003852 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003853 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854 /* temporary hack: we have still no proper support
3855 * for the direct AC3 stream...
3856 */
3857 formats |= SNDRV_PCM_FMTBIT_U8;
3858 bps = 8;
3859 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003860 if (formats == 0) {
3861 snd_printk(KERN_ERR "hda_codec: formats == 0 "
3862 "(nid=0x%x, val=0x%x, ovrd=%i, "
3863 "streams=0x%x)\n",
3864 nid, val,
3865 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
3866 streams);
3867 return -EIO;
3868 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869 if (formatsp)
3870 *formatsp = formats;
3871 if (bpsp)
3872 *bpsp = bps;
3873 }
3874
3875 return 0;
3876}
Stephen Warren384a48d2011-06-01 11:14:21 -06003877EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878
3879/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003880 * snd_hda_is_supported_format - Check the validity of the format
3881 * @codec: HD-audio codec
3882 * @nid: NID to check
3883 * @format: the HD-audio format value to check
3884 *
3885 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886 *
3887 * Returns 1 if supported, 0 if not.
3888 */
3889int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
3890 unsigned int format)
3891{
3892 int i;
3893 unsigned int val = 0, rate, stream;
3894
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003895 val = query_pcm_param(codec, nid);
3896 if (!val)
3897 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898
3899 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003900 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02003901 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902 if (val & (1 << i))
3903 break;
3904 return 0;
3905 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003906 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907 return 0;
3908
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003909 stream = query_stream_param(codec, nid);
3910 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911 return 0;
3912
3913 if (stream & AC_SUPFMT_PCM) {
3914 switch (format & 0xf0) {
3915 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003916 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917 return 0;
3918 break;
3919 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003920 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921 return 0;
3922 break;
3923 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003924 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925 return 0;
3926 break;
3927 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003928 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929 return 0;
3930 break;
3931 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003932 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933 return 0;
3934 break;
3935 default:
3936 return 0;
3937 }
3938 } else {
3939 /* FIXME: check for float32 and AC3? */
3940 }
3941
3942 return 1;
3943}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003944EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945
3946/*
3947 * PCM stuff
3948 */
3949static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
3950 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003951 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952{
3953 return 0;
3954}
3955
3956static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
3957 struct hda_codec *codec,
3958 unsigned int stream_tag,
3959 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003960 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961{
3962 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3963 return 0;
3964}
3965
3966static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
3967 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003968 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969{
Takashi Iwai888afa12008-03-18 09:57:50 +01003970 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971 return 0;
3972}
3973
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003974static int set_pcm_default_values(struct hda_codec *codec,
3975 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003977 int err;
3978
Takashi Iwai0ba21762007-04-16 11:29:14 +02003979 /* query support PCM information from the given NID */
3980 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003981 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02003982 info->rates ? NULL : &info->rates,
3983 info->formats ? NULL : &info->formats,
3984 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003985 if (err < 0)
3986 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987 }
3988 if (info->ops.open == NULL)
3989 info->ops.open = hda_pcm_default_open_close;
3990 if (info->ops.close == NULL)
3991 info->ops.close = hda_pcm_default_open_close;
3992 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003993 if (snd_BUG_ON(!info->nid))
3994 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995 info->ops.prepare = hda_pcm_default_prepare;
3996 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003998 if (snd_BUG_ON(!info->nid))
3999 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000 info->ops.cleanup = hda_pcm_default_cleanup;
4001 }
4002 return 0;
4003}
4004
Takashi Iwaieb541332010-08-06 13:48:11 +02004005/*
4006 * codec prepare/cleanup entries
4007 */
4008int snd_hda_codec_prepare(struct hda_codec *codec,
4009 struct hda_pcm_stream *hinfo,
4010 unsigned int stream,
4011 unsigned int format,
4012 struct snd_pcm_substream *substream)
4013{
4014 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004015 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004016 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
4017 if (ret >= 0)
4018 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004019 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004020 return ret;
4021}
4022EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
4023
4024void snd_hda_codec_cleanup(struct hda_codec *codec,
4025 struct hda_pcm_stream *hinfo,
4026 struct snd_pcm_substream *substream)
4027{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004028 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004029 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004030 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004031}
4032EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
4033
Takashi Iwaid5191e52009-11-16 14:58:17 +01004034/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01004035const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
4036 "Audio", "SPDIF", "HDMI", "Modem"
4037};
4038
Takashi Iwai176d5332008-07-30 15:01:44 +02004039/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004040 * get the empty PCM device number to assign
Takashi Iwaic8936222010-01-28 17:08:53 +01004041 *
4042 * note the max device number is limited by HDA_MAX_PCMS, currently 10
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004043 */
4044static int get_empty_pcm_device(struct hda_bus *bus, int type)
4045{
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004046 /* audio device indices; not linear to keep compatibility */
4047 static int audio_idx[HDA_PCM_NTYPES][5] = {
4048 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
4049 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01004050 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004051 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004052 };
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004053 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004054
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004055 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004056 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
4057 return -EINVAL;
4058 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004059
4060 for (i = 0; audio_idx[type][i] >= 0 ; i++)
4061 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
4062 return audio_idx[type][i];
4063
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004064 /* non-fixed slots starting from 10 */
4065 for (i = 10; i < 32; i++) {
4066 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4067 return i;
4068 }
4069
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004070 snd_printk(KERN_WARNING "Too many %s devices\n",
4071 snd_hda_pcm_type_name[type]);
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004072 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004073}
4074
4075/*
Takashi Iwai176d5332008-07-30 15:01:44 +02004076 * attach a new PCM stream
4077 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004078static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02004079{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004080 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02004081 struct hda_pcm_stream *info;
4082 int stream, err;
4083
Takashi Iwaib91f0802008-11-04 08:43:08 +01004084 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02004085 return -EINVAL;
4086 for (stream = 0; stream < 2; stream++) {
4087 info = &pcm->stream[stream];
4088 if (info->substreams) {
4089 err = set_pcm_default_values(codec, info);
4090 if (err < 0)
4091 return err;
4092 }
4093 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004094 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02004095}
4096
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004097/* assign all PCMs of the given codec */
4098int snd_hda_codec_build_pcms(struct hda_codec *codec)
4099{
4100 unsigned int pcm;
4101 int err;
4102
4103 if (!codec->num_pcms) {
4104 if (!codec->patch_ops.build_pcms)
4105 return 0;
4106 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004107 if (err < 0) {
4108 printk(KERN_ERR "hda_codec: cannot build PCMs"
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004109 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004110 err = snd_hda_codec_reset(codec);
4111 if (err < 0) {
4112 printk(KERN_ERR
4113 "hda_codec: cannot revert codec\n");
4114 return err;
4115 }
4116 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004117 }
4118 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
4119 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
4120 int dev;
4121
4122 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01004123 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004124
4125 if (!cpcm->pcm) {
4126 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
4127 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004128 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004129 cpcm->device = dev;
4130 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004131 if (err < 0) {
4132 printk(KERN_ERR "hda_codec: cannot attach "
4133 "PCM stream %d for codec #%d\n",
4134 dev, codec->addr);
4135 continue; /* no fatal error */
4136 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004137 }
4138 }
4139 return 0;
4140}
4141
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142/**
4143 * snd_hda_build_pcms - build PCM information
4144 * @bus: the BUS
4145 *
4146 * Create PCM information for each codec included in the bus.
4147 *
4148 * The build_pcms codec patch is requested to set up codec->num_pcms and
4149 * codec->pcm_info properly. The array is referred by the top-level driver
4150 * to create its PCM instances.
4151 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4152 * callback.
4153 *
4154 * At least, substreams, channels_min and channels_max must be filled for
4155 * each stream. substreams = 0 indicates that the stream doesn't exist.
4156 * When rates and/or formats are zero, the supported values are queried
4157 * from the given nid. The nid is used also by the default ops.prepare
4158 * and ops.cleanup callbacks.
4159 *
4160 * The driver needs to call ops.open in its open callback. Similarly,
4161 * ops.close is supposed to be called in the close callback.
4162 * ops.prepare should be called in the prepare or hw_params callback
4163 * with the proper parameters for set up.
4164 * ops.cleanup should be called in hw_free for clean up of streams.
4165 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004166 * This function returns 0 if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004168int __devinit snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004170 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171
Takashi Iwai0ba21762007-04-16 11:29:14 +02004172 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004173 int err = snd_hda_codec_build_pcms(codec);
4174 if (err < 0)
4175 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176 }
4177 return 0;
4178}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004179EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181/**
4182 * snd_hda_check_board_config - compare the current codec with the config table
4183 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004184 * @num_configs: number of config enums
4185 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186 * @tbl: configuration table, terminated by null entries
4187 *
4188 * Compares the modelname or PCI subsystem id of the current codec with the
4189 * given configuration table. If a matching entry is found, returns its
4190 * config value (supposed to be 0 or positive).
4191 *
4192 * If no entries are matching, the function returns a negative value.
4193 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02004194int snd_hda_check_board_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004195 int num_configs, const char * const *models,
Takashi Iwai12f288b2007-08-02 15:51:59 +02004196 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197{
Takashi Iwaif44ac832008-07-30 15:01:45 +02004198 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004199 int i;
4200 for (i = 0; i < num_configs; i++) {
4201 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02004202 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004203 snd_printd(KERN_INFO "hda_codec: model '%s' is "
4204 "selected\n", models[i]);
4205 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206 }
4207 }
4208 }
4209
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004210 if (!codec->bus->pci || !tbl)
4211 return -1;
4212
4213 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
4214 if (!tbl)
4215 return -1;
4216 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02004217#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004218 char tmp[10];
4219 const char *model = NULL;
4220 if (models)
4221 model = models[tbl->value];
4222 if (!model) {
4223 sprintf(tmp, "#%d", tbl->value);
4224 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004226 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4227 "for config %x:%x (%s)\n",
4228 model, tbl->subvendor, tbl->subdevice,
4229 (tbl->name ? tbl->name : "Unknown device"));
4230#endif
4231 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232 }
4233 return -1;
4234}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004235EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236
4237/**
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004238 * snd_hda_check_board_codec_sid_config - compare the current codec
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004239 subsystem ID with the
4240 config table
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004241
4242 This is important for Gateway notebooks with SB450 HDA Audio
4243 where the vendor ID of the PCI device is:
4244 ATI Technologies Inc SB450 HDA Audio [1002:437b]
4245 and the vendor/subvendor are found only at the codec.
4246
4247 * @codec: the HDA codec
4248 * @num_configs: number of config enums
4249 * @models: array of model name strings
4250 * @tbl: configuration table, terminated by null entries
4251 *
4252 * Compares the modelname or PCI subsystem id of the current codec with the
4253 * given configuration table. If a matching entry is found, returns its
4254 * config value (supposed to be 0 or positive).
4255 *
4256 * If no entries are matching, the function returns a negative value.
4257 */
4258int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004259 int num_configs, const char * const *models,
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004260 const struct snd_pci_quirk *tbl)
4261{
4262 const struct snd_pci_quirk *q;
4263
4264 /* Search for codec ID */
4265 for (q = tbl; q->subvendor; q++) {
Takashi Iwaie2301a42011-11-22 19:58:56 +01004266 unsigned int mask = 0xffff0000 | q->subdevice_mask;
4267 unsigned int id = (q->subdevice | (q->subvendor << 16)) & mask;
4268 if ((codec->subsystem_id & mask) == id)
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004269 break;
4270 }
4271
4272 if (!q->subvendor)
4273 return -1;
4274
4275 tbl = q;
4276
4277 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwaid94ff6b2009-09-02 00:20:21 +02004278#ifdef CONFIG_SND_DEBUG_VERBOSE
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004279 char tmp[10];
4280 const char *model = NULL;
4281 if (models)
4282 model = models[tbl->value];
4283 if (!model) {
4284 sprintf(tmp, "#%d", tbl->value);
4285 model = tmp;
4286 }
4287 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4288 "for config %x:%x (%s)\n",
4289 model, tbl->subvendor, tbl->subdevice,
4290 (tbl->name ? tbl->name : "Unknown device"));
4291#endif
4292 return tbl->value;
4293 }
4294 return -1;
4295}
4296EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
4297
4298/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299 * snd_hda_add_new_ctls - create controls from the array
4300 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004301 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302 *
4303 * This helper function creates and add new controls in the given array.
4304 * The array must be terminated with an empty entry as terminator.
4305 *
4306 * Returns 0 if successful, or a negative error code.
4307 */
Takashi Iwai031024e2011-05-02 11:29:30 +02004308int snd_hda_add_new_ctls(struct hda_codec *codec,
4309 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01004311 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312
4313 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004314 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004315 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01004316 if (knew->iface == -1) /* skip this codec private value */
4317 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004318 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004319 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02004320 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01004321 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004322 if (addr > 0)
4323 kctl->id.device = addr;
4324 if (idx > 0)
4325 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01004326 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004327 if (!err)
4328 break;
4329 /* try first with another device index corresponding to
4330 * the codec addr; if it still fails (or it's the
4331 * primary codec), then try another control index
4332 */
4333 if (!addr && codec->addr)
4334 addr = codec->addr;
4335 else if (!idx && !knew->index) {
4336 idx = find_empty_mixer_ctl_idx(codec,
4337 knew->name);
4338 if (idx <= 0)
4339 return err;
4340 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01004341 return err;
4342 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343 }
4344 return 0;
4345}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004346EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347
Takashi Iwaicb53c622007-08-10 17:21:45 +02004348#ifdef CONFIG_SND_HDA_POWER_SAVE
Takashi Iwaicb53c622007-08-10 17:21:45 +02004349static void hda_power_work(struct work_struct *work)
4350{
4351 struct hda_codec *codec =
4352 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004353 struct hda_bus *bus = codec->bus;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004354
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004355 spin_lock(&codec->power_lock);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004356 if (codec->power_transition > 0) { /* during power-up sequence? */
4357 spin_unlock(&codec->power_lock);
4358 return;
4359 }
Maxim Levitsky2e492462007-09-03 15:26:57 +02004360 if (!codec->power_on || codec->power_count) {
4361 codec->power_transition = 0;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004362 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004363 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02004364 }
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004365 spin_unlock(&codec->power_lock);
4366
Takashi Iwaicb53c622007-08-10 17:21:45 +02004367 hda_call_codec_suspend(codec);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004368 if (bus->ops.pm_notify)
4369 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004370}
4371
4372static void hda_keep_power_on(struct hda_codec *codec)
4373{
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004374 spin_lock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004375 codec->power_count++;
4376 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004377 codec->power_jiffies = jiffies;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004378 spin_unlock(&codec->power_lock);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004379}
4380
Takashi Iwaid5191e52009-11-16 14:58:17 +01004381/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01004382void snd_hda_update_power_acct(struct hda_codec *codec)
4383{
4384 unsigned long delta = jiffies - codec->power_jiffies;
4385 if (codec->power_on)
4386 codec->power_on_acct += delta;
4387 else
4388 codec->power_off_acct += delta;
4389 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004390}
4391
Takashi Iwaid5191e52009-11-16 14:58:17 +01004392/**
4393 * snd_hda_power_up - Power-up the codec
4394 * @codec: HD-audio codec
4395 *
4396 * Increment the power-up counter and power up the hardware really when
4397 * not turned on yet.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004398 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004399void snd_hda_power_up(struct hda_codec *codec)
4400{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004401 struct hda_bus *bus = codec->bus;
4402
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004403 spin_lock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004404 codec->power_count++;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004405 if (codec->power_on || codec->power_transition > 0) {
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004406 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004407 return;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004408 }
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004409 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004410
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004411 cancel_delayed_work_sync(&codec->power_work);
4412
4413 spin_lock(&codec->power_lock);
Takashi Iwaid66fee52011-08-02 15:39:31 +02004414 trace_hda_power_up(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004415 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004416 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004417 codec->power_jiffies = jiffies;
Takashi Iwai7f308302012-05-08 16:52:23 +02004418 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004419 spin_unlock(&codec->power_lock);
4420
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004421 if (bus->ops.pm_notify)
4422 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004423 hda_call_codec_resume(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004424
4425 spin_lock(&codec->power_lock);
Takashi Iwaia221e282007-08-16 16:35:33 +02004426 codec->power_transition = 0;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004427 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004428}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004429EXPORT_SYMBOL_HDA(snd_hda_power_up);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004430
4431#define power_save(codec) \
4432 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004433
Takashi Iwaid5191e52009-11-16 14:58:17 +01004434/**
4435 * snd_hda_power_down - Power-down the codec
4436 * @codec: HD-audio codec
4437 *
4438 * Decrement the power-up counter and schedules the power-off work if
4439 * the counter rearches to zero.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004440 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004441void snd_hda_power_down(struct hda_codec *codec)
4442{
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004443 spin_lock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004444 --codec->power_count;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004445 if (!codec->power_on || codec->power_count || codec->power_transition) {
4446 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004447 return;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004448 }
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004449 if (power_save(codec)) {
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004450 codec->power_transition = -1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01004451 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004452 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02004453 }
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004454 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004455}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004456EXPORT_SYMBOL_HDA(snd_hda_power_down);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004457
Takashi Iwaid5191e52009-11-16 14:58:17 +01004458/**
4459 * snd_hda_check_amp_list_power - Check the amp list and update the power
4460 * @codec: HD-audio codec
4461 * @check: the object containing an AMP list and the status
4462 * @nid: NID to check / update
4463 *
4464 * Check whether the given NID is in the amp list. If it's in the list,
4465 * check the current AMP status, and update the the power-status according
4466 * to the mute status.
4467 *
4468 * This function is supposed to be set or called from the check_power_status
4469 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004470 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004471int snd_hda_check_amp_list_power(struct hda_codec *codec,
4472 struct hda_loopback_check *check,
4473 hda_nid_t nid)
4474{
Takashi Iwai031024e2011-05-02 11:29:30 +02004475 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004476 int ch, v;
4477
4478 if (!check->amplist)
4479 return 0;
4480 for (p = check->amplist; p->nid; p++) {
4481 if (p->nid == nid)
4482 break;
4483 }
4484 if (!p->nid)
4485 return 0; /* nothing changed */
4486
4487 for (p = check->amplist; p->nid; p++) {
4488 for (ch = 0; ch < 2; ch++) {
4489 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4490 p->idx);
4491 if (!(v & HDA_AMP_MUTE) && v > 0) {
4492 if (!check->power_on) {
4493 check->power_on = 1;
4494 snd_hda_power_up(codec);
4495 }
4496 return 1;
4497 }
4498 }
4499 }
4500 if (check->power_on) {
4501 check->power_on = 0;
4502 snd_hda_power_down(codec);
4503 }
4504 return 0;
4505}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004506EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004507#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004509/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004510 * Channel mode helper
4511 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004512
4513/**
4514 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
4515 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004516int snd_hda_ch_mode_info(struct hda_codec *codec,
4517 struct snd_ctl_elem_info *uinfo,
4518 const struct hda_channel_mode *chmode,
4519 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004520{
4521 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4522 uinfo->count = 1;
4523 uinfo->value.enumerated.items = num_chmodes;
4524 if (uinfo->value.enumerated.item >= num_chmodes)
4525 uinfo->value.enumerated.item = num_chmodes - 1;
4526 sprintf(uinfo->value.enumerated.name, "%dch",
4527 chmode[uinfo->value.enumerated.item].channels);
4528 return 0;
4529}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004530EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004531
Takashi Iwaid5191e52009-11-16 14:58:17 +01004532/**
4533 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
4534 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004535int snd_hda_ch_mode_get(struct hda_codec *codec,
4536 struct snd_ctl_elem_value *ucontrol,
4537 const struct hda_channel_mode *chmode,
4538 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004539 int max_channels)
4540{
4541 int i;
4542
4543 for (i = 0; i < num_chmodes; i++) {
4544 if (max_channels == chmode[i].channels) {
4545 ucontrol->value.enumerated.item[0] = i;
4546 break;
4547 }
4548 }
4549 return 0;
4550}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004551EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004552
Takashi Iwaid5191e52009-11-16 14:58:17 +01004553/**
4554 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
4555 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004556int snd_hda_ch_mode_put(struct hda_codec *codec,
4557 struct snd_ctl_elem_value *ucontrol,
4558 const struct hda_channel_mode *chmode,
4559 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004560 int *max_channelsp)
4561{
4562 unsigned int mode;
4563
4564 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01004565 if (mode >= num_chmodes)
4566 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004567 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004568 return 0;
4569 /* change the current channel setting */
4570 *max_channelsp = chmode[mode].channels;
4571 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004572 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004573 return 1;
4574}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004575EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004576
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577/*
4578 * input MUX helper
4579 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004580
4581/**
4582 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
4583 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004584int snd_hda_input_mux_info(const struct hda_input_mux *imux,
4585 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586{
4587 unsigned int index;
4588
4589 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4590 uinfo->count = 1;
4591 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004592 if (!imux->num_items)
4593 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594 index = uinfo->value.enumerated.item;
4595 if (index >= imux->num_items)
4596 index = imux->num_items - 1;
4597 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
4598 return 0;
4599}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004600EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601
Takashi Iwaid5191e52009-11-16 14:58:17 +01004602/**
4603 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
4604 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004605int snd_hda_input_mux_put(struct hda_codec *codec,
4606 const struct hda_input_mux *imux,
4607 struct snd_ctl_elem_value *ucontrol,
4608 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609 unsigned int *cur_val)
4610{
4611 unsigned int idx;
4612
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004613 if (!imux->num_items)
4614 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615 idx = ucontrol->value.enumerated.item[0];
4616 if (idx >= imux->num_items)
4617 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004618 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004620 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
4621 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622 *cur_val = idx;
4623 return 1;
4624}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004625EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626
4627
4628/*
4629 * Multi-channel / digital-out PCM helper functions
4630 */
4631
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004632/* setup SPDIF output stream */
4633static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
4634 unsigned int stream_tag, unsigned int format)
4635{
Stephen Warren7c9359762011-06-01 11:14:17 -06004636 struct hda_spdif_out *spdif = snd_hda_spdif_out_of_nid(codec, nid);
4637
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004638 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Stephen Warren7c9359762011-06-01 11:14:17 -06004639 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004640 set_dig_out_convert(codec, nid,
Stephen Warren7c9359762011-06-01 11:14:17 -06004641 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02004642 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004643 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02004644 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004645 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004646 for (d = codec->slave_dig_outs; *d; d++)
4647 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
4648 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004649 }
Takashi Iwai2f728532008-09-25 16:32:41 +02004650 /* turn on again (if needed) */
Stephen Warren7c9359762011-06-01 11:14:17 -06004651 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai2f728532008-09-25 16:32:41 +02004652 set_dig_out_convert(codec, nid,
Stephen Warren7c9359762011-06-01 11:14:17 -06004653 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02004654}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004655
Takashi Iwai2f728532008-09-25 16:32:41 +02004656static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
4657{
4658 snd_hda_codec_cleanup_stream(codec, nid);
4659 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004660 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004661 for (d = codec->slave_dig_outs; *d; d++)
4662 snd_hda_codec_cleanup_stream(codec, *d);
4663 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004664}
4665
Takashi Iwaid5191e52009-11-16 14:58:17 +01004666/**
4667 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
4668 * @bus: HD-audio bus
4669 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004670void snd_hda_bus_reboot_notify(struct hda_bus *bus)
4671{
4672 struct hda_codec *codec;
4673
4674 if (!bus)
4675 return;
4676 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02004677 if (hda_codec_is_power_on(codec) &&
4678 codec->patch_ops.reboot_notify)
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004679 codec->patch_ops.reboot_notify(codec);
4680 }
4681}
Takashi Iwai8f217a22009-11-10 18:26:12 +01004682EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004683
Takashi Iwaid5191e52009-11-16 14:58:17 +01004684/**
4685 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004687int snd_hda_multi_out_dig_open(struct hda_codec *codec,
4688 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689{
Ingo Molnar62932df2006-01-16 16:34:20 +01004690 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02004691 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
4692 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02004693 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01004695 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696 return 0;
4697}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004698EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699
Takashi Iwaid5191e52009-11-16 14:58:17 +01004700/**
4701 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
4702 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004703int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
4704 struct hda_multi_out *mout,
4705 unsigned int stream_tag,
4706 unsigned int format,
4707 struct snd_pcm_substream *substream)
4708{
4709 mutex_lock(&codec->spdif_mutex);
4710 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
4711 mutex_unlock(&codec->spdif_mutex);
4712 return 0;
4713}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004714EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004715
Takashi Iwaid5191e52009-11-16 14:58:17 +01004716/**
4717 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
4718 */
Takashi Iwai9411e212009-02-13 11:32:28 +01004719int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
4720 struct hda_multi_out *mout)
4721{
4722 mutex_lock(&codec->spdif_mutex);
4723 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4724 mutex_unlock(&codec->spdif_mutex);
4725 return 0;
4726}
4727EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
4728
Takashi Iwaid5191e52009-11-16 14:58:17 +01004729/**
4730 * snd_hda_multi_out_dig_close - release the digital out stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004732int snd_hda_multi_out_dig_close(struct hda_codec *codec,
4733 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734{
Ingo Molnar62932df2006-01-16 16:34:20 +01004735 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01004737 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738 return 0;
4739}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004740EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741
Takashi Iwaid5191e52009-11-16 14:58:17 +01004742/**
4743 * snd_hda_multi_out_analog_open - open analog outputs
4744 *
4745 * Open analog outputs and set up the hw-constraints.
4746 * If the digital outputs can be opened as slave, open the digital
4747 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004749int snd_hda_multi_out_analog_open(struct hda_codec *codec,
4750 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01004751 struct snd_pcm_substream *substream,
4752 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753{
Takashi Iwai9a081602008-02-12 18:37:26 +01004754 struct snd_pcm_runtime *runtime = substream->runtime;
4755 runtime->hw.channels_max = mout->max_channels;
4756 if (mout->dig_out_nid) {
4757 if (!mout->analog_rates) {
4758 mout->analog_rates = hinfo->rates;
4759 mout->analog_formats = hinfo->formats;
4760 mout->analog_maxbps = hinfo->maxbps;
4761 } else {
4762 runtime->hw.rates = mout->analog_rates;
4763 runtime->hw.formats = mout->analog_formats;
4764 hinfo->maxbps = mout->analog_maxbps;
4765 }
4766 if (!mout->spdif_rates) {
4767 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
4768 &mout->spdif_rates,
4769 &mout->spdif_formats,
4770 &mout->spdif_maxbps);
4771 }
4772 mutex_lock(&codec->spdif_mutex);
4773 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02004774 if ((runtime->hw.rates & mout->spdif_rates) &&
4775 (runtime->hw.formats & mout->spdif_formats)) {
4776 runtime->hw.rates &= mout->spdif_rates;
4777 runtime->hw.formats &= mout->spdif_formats;
4778 if (mout->spdif_maxbps < hinfo->maxbps)
4779 hinfo->maxbps = mout->spdif_maxbps;
4780 } else {
4781 mout->share_spdif = 0;
4782 /* FIXME: need notify? */
4783 }
Takashi Iwai9a081602008-02-12 18:37:26 +01004784 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02004785 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004786 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787 return snd_pcm_hw_constraint_step(substream->runtime, 0,
4788 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
4789}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004790EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791
Takashi Iwaid5191e52009-11-16 14:58:17 +01004792/**
4793 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
4794 *
4795 * Set up the i/o for analog out.
4796 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004798int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
4799 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800 unsigned int stream_tag,
4801 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004802 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803{
Takashi Iwaidda14412011-05-02 11:29:30 +02004804 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805 int chs = substream->runtime->channels;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02004806 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807 int i;
4808
Ingo Molnar62932df2006-01-16 16:34:20 +01004809 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02004810 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Takashi Iwai9a081602008-02-12 18:37:26 +01004811 if (mout->dig_out_nid && mout->share_spdif &&
4812 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02004814 snd_hda_is_supported_format(codec, mout->dig_out_nid,
4815 format) &&
Stephen Warren7c9359762011-06-01 11:14:17 -06004816 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004818 setup_dig_out_stream(codec, mout->dig_out_nid,
4819 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820 } else {
4821 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02004822 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823 }
4824 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004825 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826
4827 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004828 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
4829 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004830 if (!mout->no_share_stream &&
4831 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004833 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
4834 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004835 /* extra outputs copied from front */
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02004836 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
4837 if (!mout->no_share_stream && mout->hp_out_nid[i])
4838 snd_hda_codec_setup_stream(codec,
4839 mout->hp_out_nid[i],
4840 stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004841 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02004842 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01004843 snd_hda_codec_setup_stream(codec,
4844 mout->extra_out_nid[i],
4845 stream_tag, 0, format);
4846
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847 /* surrounds */
4848 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02004849 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004850 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4851 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004852 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004853 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4854 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855 }
4856 return 0;
4857}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004858EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859
Takashi Iwaid5191e52009-11-16 14:58:17 +01004860/**
4861 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004863int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
4864 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865{
Takashi Iwaidda14412011-05-02 11:29:30 +02004866 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867 int i;
4868
4869 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01004870 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01004872 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02004873 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
4874 if (mout->hp_out_nid[i])
4875 snd_hda_codec_cleanup_stream(codec,
4876 mout->hp_out_nid[i]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004877 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
4878 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01004879 snd_hda_codec_cleanup_stream(codec,
4880 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01004881 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02004883 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884 mout->dig_out_used = 0;
4885 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004886 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887 return 0;
4888}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004889EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890
Takashi Iwai47408602012-04-20 13:06:53 +02004891/**
4892 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
4893 *
4894 * Guess the suitable VREF pin bits to be set as the pin-control value.
4895 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
4896 */
4897unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
4898{
4899 unsigned int pincap;
4900 unsigned int oldval;
4901 oldval = snd_hda_codec_read(codec, pin, 0,
4902 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
4903 pincap = snd_hda_query_pin_caps(codec, pin);
4904 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
4905 /* Exception: if the default pin setup is vref50, we give it priority */
4906 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
4907 return AC_PINCTL_VREF_80;
4908 else if (pincap & AC_PINCAP_VREF_50)
4909 return AC_PINCTL_VREF_50;
4910 else if (pincap & AC_PINCAP_VREF_100)
4911 return AC_PINCTL_VREF_100;
4912 else if (pincap & AC_PINCAP_VREF_GRD)
4913 return AC_PINCTL_VREF_GRD;
4914 return AC_PINCTL_VREF_HIZ;
4915}
4916EXPORT_SYMBOL_HDA(snd_hda_get_default_vref);
4917
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02004918int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
4919 unsigned int val, bool cached)
4920{
4921 if (val) {
4922 unsigned int cap = snd_hda_query_pin_caps(codec, pin);
Takashi Iwai6942c102012-04-20 13:08:40 +02004923 if (cap && (val & AC_PINCTL_OUT_EN)) {
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02004924 if (!(cap & AC_PINCAP_OUT))
4925 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
4926 else if ((val & AC_PINCTL_HP_EN) &&
4927 !(cap & AC_PINCAP_HP_DRV))
4928 val &= ~AC_PINCTL_HP_EN;
4929 }
Takashi Iwai6942c102012-04-20 13:08:40 +02004930 if (cap && (val & AC_PINCTL_IN_EN)) {
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02004931 if (!(cap & AC_PINCAP_IN))
4932 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
4933 }
4934 }
4935 if (cached)
4936 return snd_hda_codec_update_cache(codec, pin, 0,
4937 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
4938 else
4939 return snd_hda_codec_write(codec, pin, 0,
4940 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
4941}
4942EXPORT_SYMBOL_HDA(_snd_hda_set_pin_ctl);
4943
Takashi Iwai990061c2010-09-09 22:08:44 +02004944/**
4945 * snd_hda_add_imux_item - Add an item to input_mux
4946 *
4947 * When the same label is used already in the existing items, the number
4948 * suffix is appended to the label. This label index number is stored
4949 * to type_idx when non-NULL pointer is given.
4950 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02004951int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
4952 int index, int *type_idx)
4953{
4954 int i, label_idx = 0;
4955 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
4956 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
4957 return -EINVAL;
4958 }
4959 for (i = 0; i < imux->num_items; i++) {
4960 if (!strncmp(label, imux->items[i].label, strlen(label)))
4961 label_idx++;
4962 }
4963 if (type_idx)
4964 *type_idx = label_idx;
4965 if (label_idx > 0)
4966 snprintf(imux->items[imux->num_items].label,
4967 sizeof(imux->items[imux->num_items].label),
4968 "%s %d", label, label_idx);
4969 else
4970 strlcpy(imux->items[imux->num_items].label, label,
4971 sizeof(imux->items[imux->num_items].label));
4972 imux->items[imux->num_items].index = index;
4973 imux->num_items++;
4974 return 0;
4975}
4976EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02004977
Takashi Iwai4a471b72005-12-07 13:56:29 +01004978
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979#ifdef CONFIG_PM
4980/*
4981 * power management
4982 */
4983
4984/**
4985 * snd_hda_suspend - suspend the codecs
4986 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987 *
4988 * Returns 0 if successful.
4989 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02004990int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004992 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993
Takashi Iwai0ba21762007-04-16 11:29:14 +02004994 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02004995 if (hda_codec_is_power_on(codec))
4996 hda_call_codec_suspend(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997 }
4998 return 0;
4999}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005000EXPORT_SYMBOL_HDA(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001
5002/**
5003 * snd_hda_resume - resume the codecs
5004 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005 *
5006 * Returns 0 if successful.
Takashi Iwaicb53c622007-08-10 17:21:45 +02005007 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005008 * This function is defined only when POWER_SAVE isn't set.
Takashi Iwaicb53c622007-08-10 17:21:45 +02005009 * In the power-save mode, the codec is resumed dynamically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010 */
5011int snd_hda_resume(struct hda_bus *bus)
5012{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005013 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014
Takashi Iwai0ba21762007-04-16 11:29:14 +02005015 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai7f308302012-05-08 16:52:23 +02005016 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005018 return 0;
5019}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005020EXPORT_SYMBOL_HDA(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005021#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005022
5023/*
5024 * generic arrays
5025 */
5026
Takashi Iwaid5191e52009-11-16 14:58:17 +01005027/**
5028 * snd_array_new - get a new element from the given array
5029 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005030 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01005031 * Get a new element from the given array. If it exceeds the
5032 * pre-allocated array size, re-allocate the array.
5033 *
5034 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02005035 */
5036void *snd_array_new(struct snd_array *array)
5037{
5038 if (array->used >= array->alloced) {
5039 int num = array->alloced + array->alloc_align;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005040 int size = (num + 1) * array->elem_size;
Takashi Iwai00ef9612011-07-14 15:57:27 +02005041 int oldsize = array->alloced * array->elem_size;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01005042 void *nlist;
5043 if (snd_BUG_ON(num >= 4096))
5044 return NULL;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005045 nlist = krealloc(array->list, size, GFP_KERNEL);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005046 if (!nlist)
5047 return NULL;
Takashi Iwai00ef9612011-07-14 15:57:27 +02005048 memset(nlist + oldsize, 0, size - oldsize);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005049 array->list = nlist;
5050 array->alloced = num;
5051 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01005052 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005053}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005054EXPORT_SYMBOL_HDA(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005055
Takashi Iwaid5191e52009-11-16 14:58:17 +01005056/**
5057 * snd_array_free - free the given array elements
5058 * @array: the array object
5059 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005060void snd_array_free(struct snd_array *array)
5061{
5062 kfree(array->list);
5063 array->used = 0;
5064 array->alloced = 0;
5065 array->list = NULL;
5066}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005067EXPORT_SYMBOL_HDA(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01005068
Takashi Iwaid5191e52009-11-16 14:58:17 +01005069/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01005070 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5071 * @pcm: PCM caps bits
5072 * @buf: the string buffer to write
5073 * @buflen: the max buffer length
5074 *
5075 * used by hda_proc.c and hda_eld.c
5076 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005077void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5078{
5079 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5080 int i, j;
5081
5082 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5083 if (pcm & (AC_SUPPCM_BITS_8 << i))
5084 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5085
5086 buf[j] = '\0'; /* necessary when j == 0 */
5087}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005088EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005089
5090MODULE_DESCRIPTION("HDA codec core");
5091MODULE_LICENSE("GPL");