blob: 2fb935d9dccc0cb48adfa0d4d61a96b789b12c1f [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 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002250int snd_hda_lock_devices(struct hda_bus *bus)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002251{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002252 struct snd_card *card = bus->card;
2253 struct hda_codec *codec;
2254
Takashi Iwaia65d6292009-02-23 16:57:04 +01002255 spin_lock(&card->files_lock);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002256 if (card->shutdown)
2257 goto err_unlock;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002258 card->shutdown = 1;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002259 if (!list_empty(&card->ctl_files))
2260 goto err_clear;
2261
2262 list_for_each_entry(codec, &bus->codec_list, list) {
2263 int pcm;
2264 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2265 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2266 if (!cpcm->pcm)
2267 continue;
2268 if (cpcm->pcm->streams[0].substream_opened ||
2269 cpcm->pcm->streams[1].substream_opened)
2270 goto err_clear;
2271 }
2272 }
Takashi Iwaia65d6292009-02-23 16:57:04 +01002273 spin_unlock(&card->files_lock);
2274 return 0;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002275
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002276 err_clear:
2277 card->shutdown = 0;
2278 err_unlock:
2279 spin_unlock(&card->files_lock);
2280 return -EINVAL;
2281}
2282EXPORT_SYMBOL_HDA(snd_hda_lock_devices);
2283
2284void snd_hda_unlock_devices(struct hda_bus *bus)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002285{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002286 struct snd_card *card = bus->card;
2287
2288 card = bus->card;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002289 spin_lock(&card->files_lock);
2290 card->shutdown = 0;
2291 spin_unlock(&card->files_lock);
2292}
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002293EXPORT_SYMBOL_HDA(snd_hda_unlock_devices);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002294
Takashi Iwaid5191e52009-11-16 14:58:17 +01002295/**
2296 * snd_hda_codec_reset - Clear all objects assigned to the codec
2297 * @codec: HD-audio codec
2298 *
2299 * This frees the all PCM and control elements assigned to the codec, and
2300 * clears the caches and restores the pin default configurations.
2301 *
2302 * When a device is being used, it returns -EBSY. If successfully freed,
2303 * returns zero.
2304 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002305int snd_hda_codec_reset(struct hda_codec *codec)
2306{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002307 struct hda_bus *bus = codec->bus;
2308 struct snd_card *card = bus->card;
2309 int i;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002310
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002311 if (snd_hda_lock_devices(bus) < 0)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002312 return -EBUSY;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002313
2314 /* OK, let it free */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002315
2316#ifdef CONFIG_SND_HDA_POWER_SAVE
Takashi Iwaia2d96e72012-05-09 12:36:22 +02002317 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwai339876d2012-05-08 16:57:12 +02002318 codec->power_on = 0;
2319 codec->power_transition = 0;
2320 codec->power_jiffies = jiffies;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002321 flush_workqueue(bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002322#endif
2323 snd_hda_ctls_clear(codec);
2324 /* relase PCMs */
2325 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002326 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01002327 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002328 clear_bit(codec->pcm_info[i].device,
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002329 bus->pcm_dev_bits);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002330 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002331 }
2332 if (codec->patch_ops.free)
2333 codec->patch_ops.free(codec);
Takashi Iwai1835a0f2011-10-27 22:12:46 +02002334 snd_hda_jack_tbl_clear(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01002335 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002336 codec->spec = NULL;
2337 free_hda_cache(&codec->amp_cache);
2338 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01002339 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2340 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01002341 /* free only driver_pins so that init_pins + user_pins are restored */
2342 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01002343 restore_pincfgs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002344 codec->num_pcms = 0;
2345 codec->pcm_info = NULL;
2346 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01002347 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
2348 codec->slave_dig_outs = NULL;
2349 codec->spdif_status_reset = 0;
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002350 module_put(codec->owner);
2351 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002352
2353 /* allow device access again */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002354 snd_hda_unlock_devices(bus);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002355 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002356}
2357
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002358typedef int (*map_slave_func_t)(void *, struct snd_kcontrol *);
2359
2360/* apply the function to all matching slave ctls in the mixer list */
2361static int map_slaves(struct hda_codec *codec, const char * const *slaves,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002362 const char *suffix, map_slave_func_t func, void *data)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002363{
2364 struct hda_nid_item *items;
2365 const char * const *s;
2366 int i, err;
2367
2368 items = codec->mixers.list;
2369 for (i = 0; i < codec->mixers.used; i++) {
2370 struct snd_kcontrol *sctl = items[i].kctl;
2371 if (!sctl || !sctl->id.name ||
2372 sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
2373 continue;
2374 for (s = slaves; *s; s++) {
Takashi Iwai9322ca52012-02-03 14:28:01 +01002375 char tmpname[sizeof(sctl->id.name)];
2376 const char *name = *s;
2377 if (suffix) {
2378 snprintf(tmpname, sizeof(tmpname), "%s %s",
2379 name, suffix);
2380 name = tmpname;
2381 }
Takashi Iwai9322ca52012-02-03 14:28:01 +01002382 if (!strcmp(sctl->id.name, name)) {
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002383 err = func(data, sctl);
2384 if (err)
2385 return err;
2386 break;
2387 }
2388 }
2389 }
2390 return 0;
2391}
2392
2393static int check_slave_present(void *data, struct snd_kcontrol *sctl)
2394{
2395 return 1;
2396}
2397
Takashi Iwai18478e82012-03-09 17:51:10 +01002398/* guess the value corresponding to 0dB */
2399static int get_kctl_0dB_offset(struct snd_kcontrol *kctl)
2400{
2401 int _tlv[4];
2402 const int *tlv = NULL;
2403 int val = -1;
2404
2405 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2406 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2407 mm_segment_t fs = get_fs();
2408 set_fs(get_ds());
2409 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2410 tlv = _tlv;
2411 set_fs(fs);
2412 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2413 tlv = kctl->tlv.p;
2414 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE)
2415 val = -tlv[2] / tlv[3];
2416 return val;
2417}
2418
2419/* call kctl->put with the given value(s) */
2420static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2421{
2422 struct snd_ctl_elem_value *ucontrol;
2423 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2424 if (!ucontrol)
2425 return -ENOMEM;
2426 ucontrol->value.integer.value[0] = val;
2427 ucontrol->value.integer.value[1] = val;
2428 kctl->put(kctl, ucontrol);
2429 kfree(ucontrol);
2430 return 0;
2431}
2432
2433/* initialize the slave volume with 0dB */
2434static int init_slave_0dB(void *data, struct snd_kcontrol *slave)
2435{
2436 int offset = get_kctl_0dB_offset(slave);
2437 if (offset > 0)
2438 put_kctl_with_value(slave, offset);
2439 return 0;
2440}
2441
2442/* unmute the slave */
2443static int init_slave_unmute(void *data, struct snd_kcontrol *slave)
2444{
2445 return put_kctl_with_value(slave, 1);
2446}
2447
Takashi Iwaid5191e52009-11-16 14:58:17 +01002448/**
2449 * snd_hda_add_vmaster - create a virtual master control and add slaves
2450 * @codec: HD-audio codec
2451 * @name: vmaster control name
2452 * @tlv: TLV data (optional)
2453 * @slaves: slave control names (optional)
Takashi Iwai9322ca52012-02-03 14:28:01 +01002454 * @suffix: suffix string to each slave name (optional)
Takashi Iwai18478e82012-03-09 17:51:10 +01002455 * @init_slave_vol: initialize slaves to unmute/0dB
Takashi Iwai29e58532012-03-12 12:25:03 +01002456 * @ctl_ret: store the vmaster kcontrol in return
Takashi Iwaid5191e52009-11-16 14:58:17 +01002457 *
2458 * Create a virtual master control with the given name. The TLV data
2459 * must be either NULL or a valid data.
2460 *
2461 * @slaves is a NULL-terminated array of strings, each of which is a
2462 * slave control name. All controls with these names are assigned to
2463 * the new virtual master control.
2464 *
2465 * This function returns zero if successful or a negative error code.
2466 */
Takashi Iwai18478e82012-03-09 17:51:10 +01002467int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002468 unsigned int *tlv, const char * const *slaves,
Takashi Iwai29e58532012-03-12 12:25:03 +01002469 const char *suffix, bool init_slave_vol,
2470 struct snd_kcontrol **ctl_ret)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002471{
2472 struct snd_kcontrol *kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002473 int err;
2474
Takashi Iwai29e58532012-03-12 12:25:03 +01002475 if (ctl_ret)
2476 *ctl_ret = NULL;
2477
Takashi Iwai9322ca52012-02-03 14:28:01 +01002478 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002479 if (err != 1) {
Takashi Iwai2f085542008-02-22 18:43:50 +01002480 snd_printdd("No slave found for %s\n", name);
2481 return 0;
2482 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002483 kctl = snd_ctl_make_virtual_master(name, tlv);
2484 if (!kctl)
2485 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002486 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002487 if (err < 0)
2488 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002489
Takashi Iwai9322ca52012-02-03 14:28:01 +01002490 err = map_slaves(codec, slaves, suffix,
2491 (map_slave_func_t)snd_ctl_add_slave, kctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002492 if (err < 0)
2493 return err;
Takashi Iwai18478e82012-03-09 17:51:10 +01002494
2495 /* init with master mute & zero volume */
2496 put_kctl_with_value(kctl, 0);
2497 if (init_slave_vol)
2498 map_slaves(codec, slaves, suffix,
2499 tlv ? init_slave_0dB : init_slave_unmute, kctl);
2500
Takashi Iwai29e58532012-03-12 12:25:03 +01002501 if (ctl_ret)
2502 *ctl_ret = kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002503 return 0;
2504}
Takashi Iwai18478e82012-03-09 17:51:10 +01002505EXPORT_SYMBOL_HDA(__snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002506
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002507/*
2508 * mute-LED control using vmaster
2509 */
2510static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2511 struct snd_ctl_elem_info *uinfo)
2512{
2513 static const char * const texts[] = {
2514 "Off", "On", "Follow Master"
2515 };
2516 unsigned int index;
2517
2518 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2519 uinfo->count = 1;
2520 uinfo->value.enumerated.items = 3;
2521 index = uinfo->value.enumerated.item;
2522 if (index >= 3)
2523 index = 2;
2524 strcpy(uinfo->value.enumerated.name, texts[index]);
2525 return 0;
2526}
2527
2528static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2529 struct snd_ctl_elem_value *ucontrol)
2530{
2531 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2532 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2533 return 0;
2534}
2535
2536static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2537 struct snd_ctl_elem_value *ucontrol)
2538{
2539 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2540 unsigned int old_mode = hook->mute_mode;
2541
2542 hook->mute_mode = ucontrol->value.enumerated.item[0];
2543 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2544 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2545 if (old_mode == hook->mute_mode)
2546 return 0;
2547 snd_hda_sync_vmaster_hook(hook);
2548 return 1;
2549}
2550
2551static struct snd_kcontrol_new vmaster_mute_mode = {
2552 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2553 .name = "Mute-LED Mode",
2554 .info = vmaster_mute_mode_info,
2555 .get = vmaster_mute_mode_get,
2556 .put = vmaster_mute_mode_put,
2557};
2558
2559/*
2560 * Add a mute-LED hook with the given vmaster switch kctl
2561 * "Mute-LED Mode" control is automatically created and associated with
2562 * the given hook.
2563 */
2564int snd_hda_add_vmaster_hook(struct hda_codec *codec,
Takashi Iwaif29735c2012-03-13 07:55:10 +01002565 struct hda_vmaster_mute_hook *hook,
2566 bool expose_enum_ctl)
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002567{
2568 struct snd_kcontrol *kctl;
2569
2570 if (!hook->hook || !hook->sw_kctl)
2571 return 0;
2572 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
2573 hook->codec = codec;
2574 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
Takashi Iwaif29735c2012-03-13 07:55:10 +01002575 if (!expose_enum_ctl)
2576 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002577 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2578 if (!kctl)
2579 return -ENOMEM;
2580 return snd_hda_ctl_add(codec, 0, kctl);
2581}
2582EXPORT_SYMBOL_HDA(snd_hda_add_vmaster_hook);
2583
2584/*
2585 * Call the hook with the current value for synchronization
2586 * Should be called in init callback
2587 */
2588void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2589{
2590 if (!hook->hook || !hook->codec)
2591 return;
2592 switch (hook->mute_mode) {
2593 case HDA_VMUTE_FOLLOW_MASTER:
2594 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2595 break;
2596 default:
2597 hook->hook(hook->codec, hook->mute_mode);
2598 break;
2599 }
2600}
2601EXPORT_SYMBOL_HDA(snd_hda_sync_vmaster_hook);
2602
2603
Takashi Iwaid5191e52009-11-16 14:58:17 +01002604/**
2605 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2606 *
2607 * The control element is supposed to have the private_value field
2608 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2609 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002610int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2611 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612{
2613 int chs = get_amp_channels(kcontrol);
2614
2615 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2616 uinfo->count = chs == 3 ? 2 : 1;
2617 uinfo->value.integer.min = 0;
2618 uinfo->value.integer.max = 1;
2619 return 0;
2620}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002621EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622
Takashi Iwaid5191e52009-11-16 14:58:17 +01002623/**
2624 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2625 *
2626 * The control element is supposed to have the private_value field
2627 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2628 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002629int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2630 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631{
2632 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2633 hda_nid_t nid = get_amp_nid(kcontrol);
2634 int chs = get_amp_channels(kcontrol);
2635 int dir = get_amp_direction(kcontrol);
2636 int idx = get_amp_index(kcontrol);
2637 long *valp = ucontrol->value.integer.value;
2638
2639 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002640 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002641 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002643 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002644 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 return 0;
2646}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002647EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648
Takashi Iwaid5191e52009-11-16 14:58:17 +01002649/**
2650 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2651 *
2652 * The control element is supposed to have the private_value field
2653 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2654 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002655int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2656 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657{
2658 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2659 hda_nid_t nid = get_amp_nid(kcontrol);
2660 int chs = get_amp_channels(kcontrol);
2661 int dir = get_amp_direction(kcontrol);
2662 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 long *valp = ucontrol->value.integer.value;
2664 int change = 0;
2665
Takashi Iwaicb53c622007-08-10 17:21:45 +02002666 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002667 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002668 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002669 HDA_AMP_MUTE,
2670 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002671 valp++;
2672 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002673 if (chs & 2)
2674 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002675 HDA_AMP_MUTE,
2676 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002677 hda_call_check_power_status(codec, nid);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002678 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 return change;
2680}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002681EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682
Takashi Iwai67d634c2009-11-16 15:35:59 +01002683#ifdef CONFIG_SND_HDA_INPUT_BEEP
Takashi Iwaid5191e52009-11-16 14:58:17 +01002684/**
2685 * snd_hda_mixer_amp_switch_put_beep - Put callback for a beep AMP switch
2686 *
2687 * This function calls snd_hda_enable_beep_device(), which behaves differently
2688 * depending on beep_mode option.
2689 */
Jaroslav Kysela123c07a2009-10-21 14:48:23 +02002690int snd_hda_mixer_amp_switch_put_beep(struct snd_kcontrol *kcontrol,
2691 struct snd_ctl_elem_value *ucontrol)
2692{
2693 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2694 long *valp = ucontrol->value.integer.value;
2695
2696 snd_hda_enable_beep_device(codec, *valp);
2697 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2698}
2699EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put_beep);
Takashi Iwai67d634c2009-11-16 15:35:59 +01002700#endif /* CONFIG_SND_HDA_INPUT_BEEP */
Jaroslav Kysela123c07a2009-10-21 14:48:23 +02002701
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702/*
Takashi Iwai985be542005-11-02 18:26:49 +01002703 * bound volume controls
2704 *
2705 * bind multiple volumes (# indices, from 0)
2706 */
2707
2708#define AMP_VAL_IDX_SHIFT 19
2709#define AMP_VAL_IDX_MASK (0x0f<<19)
2710
Takashi Iwaid5191e52009-11-16 14:58:17 +01002711/**
2712 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2713 *
2714 * The control element is supposed to have the private_value field
2715 * set up via HDA_BIND_MUTE*() macros.
2716 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002717int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2718 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002719{
2720 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2721 unsigned long pval;
2722 int err;
2723
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002724 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002725 pval = kcontrol->private_value;
2726 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2727 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2728 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002729 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002730 return err;
2731}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002732EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002733
Takashi Iwaid5191e52009-11-16 14:58:17 +01002734/**
2735 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2736 *
2737 * The control element is supposed to have the private_value field
2738 * set up via HDA_BIND_MUTE*() macros.
2739 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002740int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2741 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002742{
2743 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2744 unsigned long pval;
2745 int i, indices, err = 0, change = 0;
2746
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002747 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002748 pval = kcontrol->private_value;
2749 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2750 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002751 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2752 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002753 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2754 if (err < 0)
2755 break;
2756 change |= err;
2757 }
2758 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002759 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002760 return err < 0 ? err : change;
2761}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002762EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002763
Takashi Iwaid5191e52009-11-16 14:58:17 +01002764/**
2765 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2766 *
2767 * The control element is supposed to have the private_value field
2768 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002769 */
2770int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2771 struct snd_ctl_elem_info *uinfo)
2772{
2773 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2774 struct hda_bind_ctls *c;
2775 int err;
2776
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002777 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002778 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002779 kcontrol->private_value = *c->values;
2780 err = c->ops->info(kcontrol, uinfo);
2781 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002782 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002783 return err;
2784}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002785EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002786
Takashi Iwaid5191e52009-11-16 14:58:17 +01002787/**
2788 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2789 *
2790 * The control element is supposed to have the private_value field
2791 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2792 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002793int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2794 struct snd_ctl_elem_value *ucontrol)
2795{
2796 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2797 struct hda_bind_ctls *c;
2798 int err;
2799
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002800 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002801 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002802 kcontrol->private_value = *c->values;
2803 err = c->ops->get(kcontrol, ucontrol);
2804 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002805 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002806 return err;
2807}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002808EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002809
Takashi Iwaid5191e52009-11-16 14:58:17 +01002810/**
2811 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2812 *
2813 * The control element is supposed to have the private_value field
2814 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2815 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002816int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2817 struct snd_ctl_elem_value *ucontrol)
2818{
2819 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2820 struct hda_bind_ctls *c;
2821 unsigned long *vals;
2822 int err = 0, change = 0;
2823
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002824 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002825 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002826 for (vals = c->values; *vals; vals++) {
2827 kcontrol->private_value = *vals;
2828 err = c->ops->put(kcontrol, ucontrol);
2829 if (err < 0)
2830 break;
2831 change |= err;
2832 }
2833 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002834 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002835 return err < 0 ? err : change;
2836}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002837EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02002838
Takashi Iwaid5191e52009-11-16 14:58:17 +01002839/**
2840 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2841 *
2842 * The control element is supposed to have the private_value field
2843 * set up via HDA_BIND_VOL() macro.
2844 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002845int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2846 unsigned int size, unsigned int __user *tlv)
2847{
2848 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2849 struct hda_bind_ctls *c;
2850 int err;
2851
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002852 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002853 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002854 kcontrol->private_value = *c->values;
2855 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2856 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002857 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002858 return err;
2859}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002860EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02002861
2862struct hda_ctl_ops snd_hda_bind_vol = {
2863 .info = snd_hda_mixer_amp_volume_info,
2864 .get = snd_hda_mixer_amp_volume_get,
2865 .put = snd_hda_mixer_amp_volume_put,
2866 .tlv = snd_hda_mixer_amp_tlv
2867};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002868EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02002869
2870struct hda_ctl_ops snd_hda_bind_sw = {
2871 .info = snd_hda_mixer_amp_switch_info,
2872 .get = snd_hda_mixer_amp_switch_get,
2873 .put = snd_hda_mixer_amp_switch_put,
2874 .tlv = snd_hda_mixer_amp_tlv
2875};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002876EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02002877
2878/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 * SPDIF out controls
2880 */
2881
Takashi Iwai0ba21762007-04-16 11:29:14 +02002882static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2883 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884{
2885 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2886 uinfo->count = 1;
2887 return 0;
2888}
2889
Takashi Iwai0ba21762007-04-16 11:29:14 +02002890static int snd_hda_spdif_cmask_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_CON_EMPHASIS_5015 |
2896 IEC958_AES0_CON_NOT_COPYRIGHT;
2897 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2898 IEC958_AES1_CON_ORIGINAL;
2899 return 0;
2900}
2901
Takashi Iwai0ba21762007-04-16 11:29:14 +02002902static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2903 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904{
2905 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2906 IEC958_AES0_NONAUDIO |
2907 IEC958_AES0_PRO_EMPHASIS_5015;
2908 return 0;
2909}
2910
Takashi Iwai0ba21762007-04-16 11:29:14 +02002911static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2912 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913{
2914 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002915 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002916 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002918 mutex_lock(&codec->spdif_mutex);
2919 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c9359762011-06-01 11:14:17 -06002920 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
2921 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
2922 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
2923 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002924 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925
2926 return 0;
2927}
2928
2929/* convert from SPDIF status bits to HDA SPDIF bits
2930 * bit 0 (DigEn) is always set zero (to be filled later)
2931 */
2932static unsigned short convert_from_spdif_status(unsigned int sbits)
2933{
2934 unsigned short val = 0;
2935
2936 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002937 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002939 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002941 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2942 IEC958_AES0_PRO_EMPHASIS_5015)
2943 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002945 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2946 IEC958_AES0_CON_EMPHASIS_5015)
2947 val |= AC_DIG1_EMPHASIS;
2948 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2949 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02002951 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2953 }
2954 return val;
2955}
2956
2957/* convert to SPDIF status bits from HDA SPDIF bits
2958 */
2959static unsigned int convert_to_spdif_status(unsigned short val)
2960{
2961 unsigned int sbits = 0;
2962
Takashi Iwai0ba21762007-04-16 11:29:14 +02002963 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002965 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 sbits |= IEC958_AES0_PROFESSIONAL;
2967 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002968 if (sbits & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2970 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002971 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002973 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002975 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2977 sbits |= val & (0x7f << 8);
2978 }
2979 return sbits;
2980}
2981
Takashi Iwai2f728532008-09-25 16:32:41 +02002982/* set digital convert verbs both for the given NID and its slaves */
2983static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
2984 int verb, int val)
2985{
Takashi Iwaidda14412011-05-02 11:29:30 +02002986 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02002987
Takashi Iwai9e976972008-11-25 08:17:20 +01002988 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002989 d = codec->slave_dig_outs;
2990 if (!d)
2991 return;
2992 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01002993 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002994}
2995
2996static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
2997 int dig1, int dig2)
2998{
2999 if (dig1 != -1)
3000 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
3001 if (dig2 != -1)
3002 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
3003}
3004
Takashi Iwai0ba21762007-04-16 11:29:14 +02003005static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
3006 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007{
3008 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06003009 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003010 struct hda_spdif_out *spdif;
3011 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 unsigned short val;
3013 int change;
3014
Ingo Molnar62932df2006-01-16 16:34:20 +01003015 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003016 spdif = snd_array_elem(&codec->spdif_out, idx);
3017 nid = spdif->nid;
Stephen Warren7c9359762011-06-01 11:14:17 -06003018 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
3020 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
3021 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c9359762011-06-01 11:14:17 -06003022 val = convert_from_spdif_status(spdif->status);
3023 val |= spdif->ctls & 1;
3024 change = spdif->ctls != val;
3025 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003026 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02003027 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01003028 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 return change;
3030}
3031
Takashi Iwaia5ce8892007-07-23 15:42:26 +02003032#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033
Takashi Iwai0ba21762007-04-16 11:29:14 +02003034static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
3035 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036{
3037 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06003038 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003039 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003041 mutex_lock(&codec->spdif_mutex);
3042 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c9359762011-06-01 11:14:17 -06003043 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003044 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 return 0;
3046}
3047
Stephen Warren74b654c2011-06-01 11:14:18 -06003048static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
3049 int dig1, int dig2)
3050{
3051 set_dig_out_convert(codec, nid, dig1, dig2);
3052 /* unmute amp switch (if any) */
3053 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
3054 (dig1 & AC_DIG1_ENABLE))
3055 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
3056 HDA_AMP_MUTE, 0);
3057}
3058
Takashi Iwai0ba21762007-04-16 11:29:14 +02003059static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
3060 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061{
3062 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06003063 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003064 struct hda_spdif_out *spdif;
3065 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 unsigned short val;
3067 int change;
3068
Ingo Molnar62932df2006-01-16 16:34:20 +01003069 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003070 spdif = snd_array_elem(&codec->spdif_out, idx);
3071 nid = spdif->nid;
Stephen Warren7c9359762011-06-01 11:14:17 -06003072 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02003074 val |= AC_DIG1_ENABLE;
Stephen Warren7c9359762011-06-01 11:14:17 -06003075 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003076 spdif->ctls = val;
3077 if (change && nid != (u16)-1)
3078 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01003079 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 return change;
3081}
3082
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003083static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 {
3085 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3086 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003087 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 .info = snd_hda_spdif_mask_info,
3089 .get = snd_hda_spdif_cmask_get,
3090 },
3091 {
3092 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3093 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003094 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 .info = snd_hda_spdif_mask_info,
3096 .get = snd_hda_spdif_pmask_get,
3097 },
3098 {
3099 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003100 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 .info = snd_hda_spdif_mask_info,
3102 .get = snd_hda_spdif_default_get,
3103 .put = snd_hda_spdif_default_put,
3104 },
3105 {
3106 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003107 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 .info = snd_hda_spdif_out_switch_info,
3109 .get = snd_hda_spdif_out_switch_get,
3110 .put = snd_hda_spdif_out_switch_put,
3111 },
3112 { } /* end */
3113};
3114
3115/**
3116 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
3117 * @codec: the HDA codec
3118 * @nid: audio out widget NID
3119 *
3120 * Creates controls related with the SPDIF output.
3121 * Called from each patch supporting the SPDIF out.
3122 *
3123 * Returns 0 if successful, or a negative error code.
3124 */
Stephen Warren74b654c2011-06-01 11:14:18 -06003125int snd_hda_create_spdif_out_ctls(struct hda_codec *codec,
3126 hda_nid_t associated_nid,
3127 hda_nid_t cvt_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128{
3129 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003130 struct snd_kcontrol *kctl;
3131 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003132 int idx;
Stephen Warren7c9359762011-06-01 11:14:17 -06003133 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134
Takashi Iwai1afe2062010-12-23 10:17:52 +01003135 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch");
3136 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003137 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
3138 return -EBUSY;
3139 }
Stephen Warren7c9359762011-06-01 11:14:17 -06003140 spdif = snd_array_new(&codec->spdif_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3142 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01003143 if (!kctl)
3144 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01003145 kctl->id.index = idx;
Stephen Warren7c9359762011-06-01 11:14:17 -06003146 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06003147 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003148 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 return err;
3150 }
Stephen Warren74b654c2011-06-01 11:14:18 -06003151 spdif->nid = cvt_nid;
3152 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
Stephen Warren7c9359762011-06-01 11:14:17 -06003153 AC_VERB_GET_DIGI_CONVERT_1, 0);
3154 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 return 0;
3156}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003157EXPORT_SYMBOL_HDA(snd_hda_create_spdif_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003159/* get the hda_spdif_out entry from the given NID
3160 * call within spdif_mutex lock
3161 */
Stephen Warren7c9359762011-06-01 11:14:17 -06003162struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3163 hda_nid_t nid)
3164{
3165 int i;
3166 for (i = 0; i < codec->spdif_out.used; i++) {
3167 struct hda_spdif_out *spdif =
3168 snd_array_elem(&codec->spdif_out, i);
3169 if (spdif->nid == nid)
3170 return spdif;
3171 }
3172 return NULL;
3173}
3174EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid);
3175
Stephen Warren74b654c2011-06-01 11:14:18 -06003176void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3177{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003178 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003179
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 spdif->nid = (u16)-1;
3183 mutex_unlock(&codec->spdif_mutex);
3184}
3185EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign);
3186
3187void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3188{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003189 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003190 unsigned short val;
3191
3192 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003193 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003194 if (spdif->nid != nid) {
3195 spdif->nid = nid;
3196 val = spdif->ctls;
3197 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3198 }
3199 mutex_unlock(&codec->spdif_mutex);
3200}
3201EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign);
3202
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203/*
Takashi Iwai9a081602008-02-12 18:37:26 +01003204 * SPDIF sharing with analog output
3205 */
3206static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3207 struct snd_ctl_elem_value *ucontrol)
3208{
3209 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3210 ucontrol->value.integer.value[0] = mout->share_spdif;
3211 return 0;
3212}
3213
3214static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3215 struct snd_ctl_elem_value *ucontrol)
3216{
3217 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3218 mout->share_spdif = !!ucontrol->value.integer.value[0];
3219 return 0;
3220}
3221
3222static struct snd_kcontrol_new spdif_share_sw = {
3223 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3224 .name = "IEC958 Default PCM Playback Switch",
3225 .info = snd_ctl_boolean_mono_info,
3226 .get = spdif_share_sw_get,
3227 .put = spdif_share_sw_put,
3228};
3229
Takashi Iwaid5191e52009-11-16 14:58:17 +01003230/**
3231 * snd_hda_create_spdif_share_sw - create Default PCM switch
3232 * @codec: the HDA codec
3233 * @mout: multi-out instance
3234 */
Takashi Iwai9a081602008-02-12 18:37:26 +01003235int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3236 struct hda_multi_out *mout)
3237{
3238 if (!mout->dig_out_nid)
3239 return 0;
3240 /* ATTENTION: here mout is passed as private_data, instead of codec */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003241 return snd_hda_ctl_add(codec, mout->dig_out_nid,
3242 snd_ctl_new1(&spdif_share_sw, mout));
Takashi Iwai9a081602008-02-12 18:37:26 +01003243}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003244EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01003245
3246/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 * SPDIF input
3248 */
3249
3250#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3251
Takashi Iwai0ba21762007-04-16 11:29:14 +02003252static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3253 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254{
3255 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3256
3257 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3258 return 0;
3259}
3260
Takashi Iwai0ba21762007-04-16 11:29:14 +02003261static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3262 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263{
3264 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3265 hda_nid_t nid = kcontrol->private_value;
3266 unsigned int val = !!ucontrol->value.integer.value[0];
3267 int change;
3268
Ingo Molnar62932df2006-01-16 16:34:20 +01003269 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003271 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003273 snd_hda_codec_write_cache(codec, nid, 0,
3274 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003276 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 return change;
3278}
3279
Takashi Iwai0ba21762007-04-16 11:29:14 +02003280static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3281 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282{
3283 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3284 hda_nid_t nid = kcontrol->private_value;
3285 unsigned short val;
3286 unsigned int sbits;
3287
Andrew Paprocki3982d172007-12-19 12:13:44 +01003288 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 sbits = convert_to_spdif_status(val);
3290 ucontrol->value.iec958.status[0] = sbits;
3291 ucontrol->value.iec958.status[1] = sbits >> 8;
3292 ucontrol->value.iec958.status[2] = sbits >> 16;
3293 ucontrol->value.iec958.status[3] = sbits >> 24;
3294 return 0;
3295}
3296
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003297static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 {
3299 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003300 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 .info = snd_hda_spdif_in_switch_info,
3302 .get = snd_hda_spdif_in_switch_get,
3303 .put = snd_hda_spdif_in_switch_put,
3304 },
3305 {
3306 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3307 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003308 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 .info = snd_hda_spdif_mask_info,
3310 .get = snd_hda_spdif_in_status_get,
3311 },
3312 { } /* end */
3313};
3314
3315/**
3316 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3317 * @codec: the HDA codec
3318 * @nid: audio in widget NID
3319 *
3320 * Creates controls related with the SPDIF input.
3321 * Called from each patch supporting the SPDIF in.
3322 *
3323 * Returns 0 if successful, or a negative error code.
3324 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003325int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326{
3327 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003328 struct snd_kcontrol *kctl;
3329 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003330 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331
Takashi Iwai1afe2062010-12-23 10:17:52 +01003332 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch");
3333 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003334 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
3335 return -EBUSY;
3336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3338 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01003339 if (!kctl)
3340 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003342 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003343 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 return err;
3345 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003346 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01003347 snd_hda_codec_read(codec, nid, 0,
3348 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02003349 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 return 0;
3351}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003352EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353
Takashi Iwai2a439522011-07-26 09:52:50 +02003354#ifdef CONFIG_PM
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003355/*
3356 * command cache
3357 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003359/* build a 32bit cache key with the widget id and the command parameter */
3360#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3361#define get_cmd_cache_nid(key) ((key) & 0xff)
3362#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3363
3364/**
3365 * snd_hda_codec_write_cache - send a single command with caching
3366 * @codec: the HDA codec
3367 * @nid: NID to send the command
3368 * @direct: direct flag
3369 * @verb: the verb to send
3370 * @parm: the parameter for the verb
3371 *
3372 * Send a single command without waiting for response.
3373 *
3374 * Returns 0 if successful, or a negative error code.
3375 */
3376int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
3377 int direct, unsigned int verb, unsigned int parm)
3378{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003379 int err = snd_hda_codec_write(codec, nid, direct, verb, parm);
3380 struct hda_cache_head *c;
3381 u32 key;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003382
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003383 if (err < 0)
3384 return err;
3385 /* parm may contain the verb stuff for get/set amp */
3386 verb = verb | (parm >> 8);
3387 parm &= 0xff;
3388 key = build_cmd_cache_key(nid, verb);
3389 mutex_lock(&codec->bus->cmd_mutex);
3390 c = get_alloc_hash(&codec->cmd_cache, key);
3391 if (c)
3392 c->val = parm;
3393 mutex_unlock(&codec->bus->cmd_mutex);
3394 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003395}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003396EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003397
Takashi Iwaid5191e52009-11-16 14:58:17 +01003398/**
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003399 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3400 * @codec: the HDA codec
3401 * @nid: NID to send the command
3402 * @direct: direct flag
3403 * @verb: the verb to send
3404 * @parm: the parameter for the verb
3405 *
3406 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3407 * command if the parameter is already identical with the cached value.
3408 * If not, it sends the command and refreshes the cache.
3409 *
3410 * Returns 0 if successful, or a negative error code.
3411 */
3412int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
3413 int direct, unsigned int verb, unsigned int parm)
3414{
3415 struct hda_cache_head *c;
3416 u32 key;
3417
3418 /* parm may contain the verb stuff for get/set amp */
3419 verb = verb | (parm >> 8);
3420 parm &= 0xff;
3421 key = build_cmd_cache_key(nid, verb);
3422 mutex_lock(&codec->bus->cmd_mutex);
3423 c = get_hash(&codec->cmd_cache, key);
3424 if (c && c->val == parm) {
3425 mutex_unlock(&codec->bus->cmd_mutex);
3426 return 0;
3427 }
3428 mutex_unlock(&codec->bus->cmd_mutex);
3429 return snd_hda_codec_write_cache(codec, nid, direct, verb, parm);
3430}
3431EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
3432
3433/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003434 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3435 * @codec: HD-audio codec
3436 *
3437 * Execute all verbs recorded in the command caches to resume.
3438 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003439void snd_hda_codec_resume_cache(struct hda_codec *codec)
3440{
Takashi Iwai603c4012008-07-30 15:01:44 +02003441 struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003442 int i;
3443
Takashi Iwai603c4012008-07-30 15:01:44 +02003444 for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003445 u32 key = buffer->key;
3446 if (!key)
3447 continue;
3448 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3449 get_cmd_cache_cmd(key), buffer->val);
3450 }
3451}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003452EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003453
3454/**
3455 * snd_hda_sequence_write_cache - sequence writes with caching
3456 * @codec: the HDA codec
3457 * @seq: VERB array to send
3458 *
3459 * Send the commands sequentially from the given array.
3460 * Thte commands are recorded on cache for power-save and resume.
3461 * The array must be terminated with NID=0.
3462 */
3463void snd_hda_sequence_write_cache(struct hda_codec *codec,
3464 const struct hda_verb *seq)
3465{
3466 for (; seq->nid; seq++)
3467 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3468 seq->param);
3469}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003470EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
Takashi Iwai2a439522011-07-26 09:52:50 +02003471#endif /* CONFIG_PM */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003472
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003473void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
3474 unsigned int power_state,
3475 bool eapd_workaround)
Takashi Iwai54d17402005-11-21 16:33:22 +01003476{
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003477 hda_nid_t nid = codec->start_nid;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003478 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01003479
Takashi Iwaicb53c622007-08-10 17:21:45 +02003480 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003481 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003482 if (!(wcaps & AC_WCAP_POWER))
3483 continue;
3484 /* don't power down the widget if it controls eapd and
3485 * EAPD_BTLENABLE is set.
3486 */
3487 if (eapd_workaround && power_state == AC_PWRST_D3 &&
3488 get_wcaps_type(wcaps) == AC_WID_PIN &&
3489 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3490 int eapd = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003491 AC_VERB_GET_EAPD_BTLENABLE, 0);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003492 if (eapd & 0x02)
3493 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003494 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003495 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
3496 power_state);
Takashi Iwai54d17402005-11-21 16:33:22 +01003497 }
3498
Takashi Iwaicb53c622007-08-10 17:21:45 +02003499 if (power_state == AC_PWRST_D0) {
3500 unsigned long end_time;
3501 int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003502 /* wait until the codec reachs to D0 */
3503 end_time = jiffies + msecs_to_jiffies(500);
3504 do {
3505 state = snd_hda_codec_read(codec, fg, 0,
3506 AC_VERB_GET_POWER_STATE, 0);
3507 if (state == power_state)
3508 break;
3509 msleep(1);
3510 } while (time_after_eq(end_time, jiffies));
3511 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003512}
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003513EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all);
3514
3515/*
3516 * set power state of the codec
3517 */
3518static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
3519 unsigned int power_state)
3520{
3521 if (codec->patch_ops.set_power_state) {
3522 codec->patch_ops.set_power_state(codec, fg, power_state);
3523 return;
3524 }
3525
3526 /* this delay seems necessary to avoid click noise at power-down */
3527 if (power_state == AC_PWRST_D3)
3528 msleep(100);
3529 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE,
3530 power_state);
3531 snd_hda_codec_set_power_to_all(codec, fg, power_state, true);
3532}
Takashi Iwai54d17402005-11-21 16:33:22 +01003533
Takashi Iwai11aeff02008-07-30 15:01:46 +02003534#ifdef CONFIG_SND_HDA_HWDEP
3535/* execute additional init verbs */
3536static void hda_exec_init_verbs(struct hda_codec *codec)
3537{
3538 if (codec->init_verbs.list)
3539 snd_hda_sequence_write(codec, codec->init_verbs.list);
3540}
3541#else
3542static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3543#endif
3544
Takashi Iwai2a439522011-07-26 09:52:50 +02003545#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003546/*
3547 * call suspend and power-down; used both from PM and power-save
3548 */
3549static void hda_call_codec_suspend(struct hda_codec *codec)
3550{
3551 if (codec->patch_ops.suspend)
3552 codec->patch_ops.suspend(codec, PMSG_SUSPEND);
Takashi Iwaieb541332010-08-06 13:48:11 +02003553 hda_cleanup_all_streams(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003554 hda_set_power_state(codec,
3555 codec->afg ? codec->afg : codec->mfg,
3556 AC_PWRST_D3);
3557#ifdef CONFIG_SND_HDA_POWER_SAVE
3558 cancel_delayed_work(&codec->power_work);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02003559 spin_lock(&codec->power_lock);
3560 snd_hda_update_power_acct(codec);
3561 trace_hda_power_down(codec);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02003562 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02003563 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003564 codec->power_jiffies = jiffies;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02003565 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003566#endif
3567}
3568
3569/*
3570 * kick up codec; used both from PM and power-save
3571 */
3572static void hda_call_codec_resume(struct hda_codec *codec)
3573{
Takashi Iwai7f308302012-05-08 16:52:23 +02003574 /* set as if powered on for avoiding re-entering the resume
3575 * in the resume / power-save sequence
3576 */
3577 hda_keep_power_on(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003578 hda_set_power_state(codec,
3579 codec->afg ? codec->afg : codec->mfg,
3580 AC_PWRST_D0);
Takashi Iwai3be14142009-02-20 14:11:16 +01003581 restore_pincfgs(codec); /* restore all current pin configs */
Takashi Iwaiac0547d2010-07-05 16:50:13 +02003582 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02003583 hda_exec_init_verbs(codec);
Takashi Iwai1835a0f2011-10-27 22:12:46 +02003584 snd_hda_jack_set_dirty_all(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003585 if (codec->patch_ops.resume)
3586 codec->patch_ops.resume(codec);
3587 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02003588 if (codec->patch_ops.init)
3589 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003590 snd_hda_codec_resume_amp(codec);
3591 snd_hda_codec_resume_cache(codec);
3592 }
Takashi Iwai7f308302012-05-08 16:52:23 +02003593 snd_hda_power_down(codec); /* flag down before returning */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003594}
Takashi Iwai2a439522011-07-26 09:52:50 +02003595#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003596
Takashi Iwai54d17402005-11-21 16:33:22 +01003597
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598/**
3599 * snd_hda_build_controls - build mixer controls
3600 * @bus: the BUS
3601 *
3602 * Creates mixer controls for each codec included in the bus.
3603 *
3604 * Returns 0 if successful, otherwise a negative error code.
3605 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003606int /*__devinit*/ snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003608 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609
Takashi Iwai0ba21762007-04-16 11:29:14 +02003610 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003611 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003612 if (err < 0) {
Takashi Iwai28d1a852010-03-15 09:05:46 +01003613 printk(KERN_ERR "hda_codec: cannot build controls "
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003614 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003615 err = snd_hda_codec_reset(codec);
3616 if (err < 0) {
3617 printk(KERN_ERR
3618 "hda_codec: cannot revert codec\n");
3619 return err;
3620 }
3621 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003623 return 0;
3624}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003625EXPORT_SYMBOL_HDA(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003627int snd_hda_codec_build_controls(struct hda_codec *codec)
3628{
3629 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02003630 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003631 /* continue to initialize... */
3632 if (codec->patch_ops.init)
3633 err = codec->patch_ops.init(codec);
3634 if (!err && codec->patch_ops.build_controls)
3635 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003636 if (err < 0)
3637 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638 return 0;
3639}
3640
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641/*
3642 * stream formats
3643 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02003644struct hda_rate_tbl {
3645 unsigned int hz;
3646 unsigned int alsa_bits;
3647 unsigned int hda_fmt;
3648};
3649
Takashi Iwai92f10b32010-08-03 14:21:00 +02003650/* rate = base * mult / div */
3651#define HDA_RATE(base, mult, div) \
3652 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
3653 (((div) - 1) << AC_FMT_DIV_SHIFT))
3654
Takashi Iwaibefdf312005-08-22 13:57:55 +02003655static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003657
3658 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003659 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
3660 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
3661 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
3662 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
3663 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
3664 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
3665 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
3666 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
3667 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
3668 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
3669 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003670#define AC_PAR_PCM_RATE_BITS 11
3671 /* up to bits 10, 384kHZ isn't supported properly */
3672
3673 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003674 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003675
Takashi Iwaibefdf312005-08-22 13:57:55 +02003676 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677};
3678
3679/**
3680 * snd_hda_calc_stream_format - calculate format bitset
3681 * @rate: the sample rate
3682 * @channels: the number of channels
3683 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3684 * @maxbps: the max. bps
3685 *
3686 * Calculate the format bitset from the given rate, channels and th PCM format.
3687 *
3688 * Return zero if invalid.
3689 */
3690unsigned int snd_hda_calc_stream_format(unsigned int rate,
3691 unsigned int channels,
3692 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03003693 unsigned int maxbps,
3694 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695{
3696 int i;
3697 unsigned int val = 0;
3698
Takashi Iwaibefdf312005-08-22 13:57:55 +02003699 for (i = 0; rate_bits[i].hz; i++)
3700 if (rate_bits[i].hz == rate) {
3701 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702 break;
3703 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003704 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705 snd_printdd("invalid rate %d\n", rate);
3706 return 0;
3707 }
3708
3709 if (channels == 0 || channels > 8) {
3710 snd_printdd("invalid channels %d\n", channels);
3711 return 0;
3712 }
3713 val |= channels - 1;
3714
3715 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003716 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003717 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003718 break;
3719 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003720 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003721 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722 case 20:
3723 case 24:
3724 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003725 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003726 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003728 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02003730 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731 break;
3732 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003733 snd_printdd("invalid format width %d\n",
3734 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735 return 0;
3736 }
3737
Anssi Hannula32c168c2010-08-03 13:28:57 +03003738 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003739 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03003740
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741 return val;
3742}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003743EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003745static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid,
3746 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003747{
3748 unsigned int val = 0;
3749 if (nid != codec->afg &&
3750 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
3751 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
3752 if (!val || val == -1)
3753 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
3754 if (!val || val == -1)
3755 return 0;
3756 return val;
3757}
3758
3759static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3760{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003761 return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003762 get_pcm_param);
3763}
3764
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003765static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid,
3766 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003767{
3768 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
3769 if (!streams || streams == -1)
3770 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
3771 if (!streams || streams == -1)
3772 return 0;
3773 return streams;
3774}
3775
3776static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
3777{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003778 return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003779 get_stream_param);
3780}
3781
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782/**
3783 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
3784 * @codec: the HDA codec
3785 * @nid: NID to query
3786 * @ratesp: the pointer to store the detected rate bitflags
3787 * @formatsp: the pointer to store the detected formats
3788 * @bpsp: the pointer to store the detected format widths
3789 *
3790 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
3791 * or @bsps argument is ignored.
3792 *
3793 * Returns 0 if successful, otherwise a negative error code.
3794 */
Stephen Warren384a48d2011-06-01 11:14:21 -06003795int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
3797{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003798 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003800 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003801 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802
3803 if (ratesp) {
3804 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003805 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02003807 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003809 if (rates == 0) {
3810 snd_printk(KERN_ERR "hda_codec: rates == 0 "
3811 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
3812 nid, val,
3813 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
3814 return -EIO;
3815 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816 *ratesp = rates;
3817 }
3818
3819 if (formatsp || bpsp) {
3820 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003821 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003823 streams = query_stream_param(codec, nid);
3824 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826
3827 bps = 0;
3828 if (streams & AC_SUPFMT_PCM) {
3829 if (val & AC_SUPPCM_BITS_8) {
3830 formats |= SNDRV_PCM_FMTBIT_U8;
3831 bps = 8;
3832 }
3833 if (val & AC_SUPPCM_BITS_16) {
3834 formats |= SNDRV_PCM_FMTBIT_S16_LE;
3835 bps = 16;
3836 }
3837 if (wcaps & AC_WCAP_DIGITAL) {
3838 if (val & AC_SUPPCM_BITS_32)
3839 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
3840 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
3841 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3842 if (val & AC_SUPPCM_BITS_24)
3843 bps = 24;
3844 else if (val & AC_SUPPCM_BITS_20)
3845 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003846 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
3847 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3849 if (val & AC_SUPPCM_BITS_32)
3850 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851 else if (val & AC_SUPPCM_BITS_24)
3852 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02003853 else if (val & AC_SUPPCM_BITS_20)
3854 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855 }
3856 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02003857#if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
Takashi Iwaib5025c52009-07-01 18:05:27 +02003858 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003860 if (!bps)
3861 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02003862 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02003863#endif
Takashi Iwaib5025c52009-07-01 18:05:27 +02003864 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003865 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866 /* temporary hack: we have still no proper support
3867 * for the direct AC3 stream...
3868 */
3869 formats |= SNDRV_PCM_FMTBIT_U8;
3870 bps = 8;
3871 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003872 if (formats == 0) {
3873 snd_printk(KERN_ERR "hda_codec: formats == 0 "
3874 "(nid=0x%x, val=0x%x, ovrd=%i, "
3875 "streams=0x%x)\n",
3876 nid, val,
3877 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
3878 streams);
3879 return -EIO;
3880 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881 if (formatsp)
3882 *formatsp = formats;
3883 if (bpsp)
3884 *bpsp = bps;
3885 }
3886
3887 return 0;
3888}
Stephen Warren384a48d2011-06-01 11:14:21 -06003889EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890
3891/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003892 * snd_hda_is_supported_format - Check the validity of the format
3893 * @codec: HD-audio codec
3894 * @nid: NID to check
3895 * @format: the HD-audio format value to check
3896 *
3897 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898 *
3899 * Returns 1 if supported, 0 if not.
3900 */
3901int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
3902 unsigned int format)
3903{
3904 int i;
3905 unsigned int val = 0, rate, stream;
3906
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003907 val = query_pcm_param(codec, nid);
3908 if (!val)
3909 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910
3911 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003912 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02003913 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914 if (val & (1 << i))
3915 break;
3916 return 0;
3917 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003918 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 return 0;
3920
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003921 stream = query_stream_param(codec, nid);
3922 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 return 0;
3924
3925 if (stream & AC_SUPFMT_PCM) {
3926 switch (format & 0xf0) {
3927 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003928 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929 return 0;
3930 break;
3931 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003932 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933 return 0;
3934 break;
3935 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003936 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937 return 0;
3938 break;
3939 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003940 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941 return 0;
3942 break;
3943 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003944 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945 return 0;
3946 break;
3947 default:
3948 return 0;
3949 }
3950 } else {
3951 /* FIXME: check for float32 and AC3? */
3952 }
3953
3954 return 1;
3955}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003956EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957
3958/*
3959 * PCM stuff
3960 */
3961static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
3962 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003963 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964{
3965 return 0;
3966}
3967
3968static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
3969 struct hda_codec *codec,
3970 unsigned int stream_tag,
3971 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003972 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973{
3974 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3975 return 0;
3976}
3977
3978static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
3979 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003980 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981{
Takashi Iwai888afa12008-03-18 09:57:50 +01003982 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983 return 0;
3984}
3985
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003986static int set_pcm_default_values(struct hda_codec *codec,
3987 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003989 int err;
3990
Takashi Iwai0ba21762007-04-16 11:29:14 +02003991 /* query support PCM information from the given NID */
3992 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003993 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02003994 info->rates ? NULL : &info->rates,
3995 info->formats ? NULL : &info->formats,
3996 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003997 if (err < 0)
3998 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999 }
4000 if (info->ops.open == NULL)
4001 info->ops.open = hda_pcm_default_open_close;
4002 if (info->ops.close == NULL)
4003 info->ops.close = hda_pcm_default_open_close;
4004 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004005 if (snd_BUG_ON(!info->nid))
4006 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007 info->ops.prepare = hda_pcm_default_prepare;
4008 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004010 if (snd_BUG_ON(!info->nid))
4011 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012 info->ops.cleanup = hda_pcm_default_cleanup;
4013 }
4014 return 0;
4015}
4016
Takashi Iwaieb541332010-08-06 13:48:11 +02004017/*
4018 * codec prepare/cleanup entries
4019 */
4020int snd_hda_codec_prepare(struct hda_codec *codec,
4021 struct hda_pcm_stream *hinfo,
4022 unsigned int stream,
4023 unsigned int format,
4024 struct snd_pcm_substream *substream)
4025{
4026 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004027 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004028 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
4029 if (ret >= 0)
4030 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004031 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004032 return ret;
4033}
4034EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
4035
4036void snd_hda_codec_cleanup(struct hda_codec *codec,
4037 struct hda_pcm_stream *hinfo,
4038 struct snd_pcm_substream *substream)
4039{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004040 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004041 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004042 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004043}
4044EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
4045
Takashi Iwaid5191e52009-11-16 14:58:17 +01004046/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01004047const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
4048 "Audio", "SPDIF", "HDMI", "Modem"
4049};
4050
Takashi Iwai176d5332008-07-30 15:01:44 +02004051/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004052 * get the empty PCM device number to assign
Takashi Iwaic8936222010-01-28 17:08:53 +01004053 *
4054 * note the max device number is limited by HDA_MAX_PCMS, currently 10
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004055 */
4056static int get_empty_pcm_device(struct hda_bus *bus, int type)
4057{
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004058 /* audio device indices; not linear to keep compatibility */
4059 static int audio_idx[HDA_PCM_NTYPES][5] = {
4060 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
4061 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01004062 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004063 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004064 };
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004065 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004066
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004067 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004068 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
4069 return -EINVAL;
4070 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004071
4072 for (i = 0; audio_idx[type][i] >= 0 ; i++)
4073 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
4074 return audio_idx[type][i];
4075
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004076 /* non-fixed slots starting from 10 */
4077 for (i = 10; i < 32; i++) {
4078 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4079 return i;
4080 }
4081
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004082 snd_printk(KERN_WARNING "Too many %s devices\n",
4083 snd_hda_pcm_type_name[type]);
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004084 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004085}
4086
4087/*
Takashi Iwai176d5332008-07-30 15:01:44 +02004088 * attach a new PCM stream
4089 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004090static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02004091{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004092 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02004093 struct hda_pcm_stream *info;
4094 int stream, err;
4095
Takashi Iwaib91f0802008-11-04 08:43:08 +01004096 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02004097 return -EINVAL;
4098 for (stream = 0; stream < 2; stream++) {
4099 info = &pcm->stream[stream];
4100 if (info->substreams) {
4101 err = set_pcm_default_values(codec, info);
4102 if (err < 0)
4103 return err;
4104 }
4105 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004106 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02004107}
4108
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004109/* assign all PCMs of the given codec */
4110int snd_hda_codec_build_pcms(struct hda_codec *codec)
4111{
4112 unsigned int pcm;
4113 int err;
4114
4115 if (!codec->num_pcms) {
4116 if (!codec->patch_ops.build_pcms)
4117 return 0;
4118 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004119 if (err < 0) {
4120 printk(KERN_ERR "hda_codec: cannot build PCMs"
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004121 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004122 err = snd_hda_codec_reset(codec);
4123 if (err < 0) {
4124 printk(KERN_ERR
4125 "hda_codec: cannot revert codec\n");
4126 return err;
4127 }
4128 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004129 }
4130 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
4131 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
4132 int dev;
4133
4134 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01004135 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004136
4137 if (!cpcm->pcm) {
4138 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
4139 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004140 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004141 cpcm->device = dev;
4142 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004143 if (err < 0) {
4144 printk(KERN_ERR "hda_codec: cannot attach "
4145 "PCM stream %d for codec #%d\n",
4146 dev, codec->addr);
4147 continue; /* no fatal error */
4148 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004149 }
4150 }
4151 return 0;
4152}
4153
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154/**
4155 * snd_hda_build_pcms - build PCM information
4156 * @bus: the BUS
4157 *
4158 * Create PCM information for each codec included in the bus.
4159 *
4160 * The build_pcms codec patch is requested to set up codec->num_pcms and
4161 * codec->pcm_info properly. The array is referred by the top-level driver
4162 * to create its PCM instances.
4163 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4164 * callback.
4165 *
4166 * At least, substreams, channels_min and channels_max must be filled for
4167 * each stream. substreams = 0 indicates that the stream doesn't exist.
4168 * When rates and/or formats are zero, the supported values are queried
4169 * from the given nid. The nid is used also by the default ops.prepare
4170 * and ops.cleanup callbacks.
4171 *
4172 * The driver needs to call ops.open in its open callback. Similarly,
4173 * ops.close is supposed to be called in the close callback.
4174 * ops.prepare should be called in the prepare or hw_params callback
4175 * with the proper parameters for set up.
4176 * ops.cleanup should be called in hw_free for clean up of streams.
4177 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004178 * This function returns 0 if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004180int __devinit snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004182 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183
Takashi Iwai0ba21762007-04-16 11:29:14 +02004184 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004185 int err = snd_hda_codec_build_pcms(codec);
4186 if (err < 0)
4187 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 }
4189 return 0;
4190}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004191EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193/**
4194 * snd_hda_check_board_config - compare the current codec with the config table
4195 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004196 * @num_configs: number of config enums
4197 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198 * @tbl: configuration table, terminated by null entries
4199 *
4200 * Compares the modelname or PCI subsystem id of the current codec with the
4201 * given configuration table. If a matching entry is found, returns its
4202 * config value (supposed to be 0 or positive).
4203 *
4204 * If no entries are matching, the function returns a negative value.
4205 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02004206int snd_hda_check_board_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004207 int num_configs, const char * const *models,
Takashi Iwai12f288b2007-08-02 15:51:59 +02004208 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209{
Takashi Iwaif44ac832008-07-30 15:01:45 +02004210 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004211 int i;
4212 for (i = 0; i < num_configs; i++) {
4213 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02004214 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004215 snd_printd(KERN_INFO "hda_codec: model '%s' is "
4216 "selected\n", models[i]);
4217 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218 }
4219 }
4220 }
4221
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004222 if (!codec->bus->pci || !tbl)
4223 return -1;
4224
4225 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
4226 if (!tbl)
4227 return -1;
4228 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02004229#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004230 char tmp[10];
4231 const char *model = NULL;
4232 if (models)
4233 model = models[tbl->value];
4234 if (!model) {
4235 sprintf(tmp, "#%d", tbl->value);
4236 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004238 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4239 "for config %x:%x (%s)\n",
4240 model, tbl->subvendor, tbl->subdevice,
4241 (tbl->name ? tbl->name : "Unknown device"));
4242#endif
4243 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244 }
4245 return -1;
4246}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004247EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248
4249/**
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004250 * snd_hda_check_board_codec_sid_config - compare the current codec
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004251 subsystem ID with the
4252 config table
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004253
4254 This is important for Gateway notebooks with SB450 HDA Audio
4255 where the vendor ID of the PCI device is:
4256 ATI Technologies Inc SB450 HDA Audio [1002:437b]
4257 and the vendor/subvendor are found only at the codec.
4258
4259 * @codec: the HDA codec
4260 * @num_configs: number of config enums
4261 * @models: array of model name strings
4262 * @tbl: configuration table, terminated by null entries
4263 *
4264 * Compares the modelname or PCI subsystem id of the current codec with the
4265 * given configuration table. If a matching entry is found, returns its
4266 * config value (supposed to be 0 or positive).
4267 *
4268 * If no entries are matching, the function returns a negative value.
4269 */
4270int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004271 int num_configs, const char * const *models,
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004272 const struct snd_pci_quirk *tbl)
4273{
4274 const struct snd_pci_quirk *q;
4275
4276 /* Search for codec ID */
4277 for (q = tbl; q->subvendor; q++) {
Takashi Iwaie2301a42011-11-22 19:58:56 +01004278 unsigned int mask = 0xffff0000 | q->subdevice_mask;
4279 unsigned int id = (q->subdevice | (q->subvendor << 16)) & mask;
4280 if ((codec->subsystem_id & mask) == id)
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004281 break;
4282 }
4283
4284 if (!q->subvendor)
4285 return -1;
4286
4287 tbl = q;
4288
4289 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwaid94ff6b2009-09-02 00:20:21 +02004290#ifdef CONFIG_SND_DEBUG_VERBOSE
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004291 char tmp[10];
4292 const char *model = NULL;
4293 if (models)
4294 model = models[tbl->value];
4295 if (!model) {
4296 sprintf(tmp, "#%d", tbl->value);
4297 model = tmp;
4298 }
4299 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4300 "for config %x:%x (%s)\n",
4301 model, tbl->subvendor, tbl->subdevice,
4302 (tbl->name ? tbl->name : "Unknown device"));
4303#endif
4304 return tbl->value;
4305 }
4306 return -1;
4307}
4308EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
4309
4310/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 * snd_hda_add_new_ctls - create controls from the array
4312 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004313 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 *
4315 * This helper function creates and add new controls in the given array.
4316 * The array must be terminated with an empty entry as terminator.
4317 *
4318 * Returns 0 if successful, or a negative error code.
4319 */
Takashi Iwai031024e2011-05-02 11:29:30 +02004320int snd_hda_add_new_ctls(struct hda_codec *codec,
4321 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01004323 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324
4325 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004326 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004327 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01004328 if (knew->iface == -1) /* skip this codec private value */
4329 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004330 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004331 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02004332 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01004333 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004334 if (addr > 0)
4335 kctl->id.device = addr;
4336 if (idx > 0)
4337 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01004338 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004339 if (!err)
4340 break;
4341 /* try first with another device index corresponding to
4342 * the codec addr; if it still fails (or it's the
4343 * primary codec), then try another control index
4344 */
4345 if (!addr && codec->addr)
4346 addr = codec->addr;
4347 else if (!idx && !knew->index) {
4348 idx = find_empty_mixer_ctl_idx(codec,
4349 knew->name);
4350 if (idx <= 0)
4351 return err;
4352 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01004353 return err;
4354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355 }
4356 return 0;
4357}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004358EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359
Takashi Iwaicb53c622007-08-10 17:21:45 +02004360#ifdef CONFIG_SND_HDA_POWER_SAVE
Takashi Iwaicb53c622007-08-10 17:21:45 +02004361static void hda_power_work(struct work_struct *work)
4362{
4363 struct hda_codec *codec =
4364 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004365 struct hda_bus *bus = codec->bus;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004366
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004367 spin_lock(&codec->power_lock);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004368 if (codec->power_transition > 0) { /* during power-up sequence? */
4369 spin_unlock(&codec->power_lock);
4370 return;
4371 }
Maxim Levitsky2e492462007-09-03 15:26:57 +02004372 if (!codec->power_on || codec->power_count) {
4373 codec->power_transition = 0;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004374 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004375 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02004376 }
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004377 spin_unlock(&codec->power_lock);
4378
Takashi Iwaicb53c622007-08-10 17:21:45 +02004379 hda_call_codec_suspend(codec);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004380 if (bus->ops.pm_notify)
4381 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004382}
4383
4384static void hda_keep_power_on(struct hda_codec *codec)
4385{
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004386 spin_lock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004387 codec->power_count++;
4388 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004389 codec->power_jiffies = jiffies;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004390 spin_unlock(&codec->power_lock);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004391}
4392
Takashi Iwaid5191e52009-11-16 14:58:17 +01004393/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01004394void snd_hda_update_power_acct(struct hda_codec *codec)
4395{
4396 unsigned long delta = jiffies - codec->power_jiffies;
4397 if (codec->power_on)
4398 codec->power_on_acct += delta;
4399 else
4400 codec->power_off_acct += delta;
4401 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004402}
4403
Takashi Iwaid5191e52009-11-16 14:58:17 +01004404/**
4405 * snd_hda_power_up - Power-up the codec
4406 * @codec: HD-audio codec
4407 *
4408 * Increment the power-up counter and power up the hardware really when
4409 * not turned on yet.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004410 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004411void snd_hda_power_up(struct hda_codec *codec)
4412{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004413 struct hda_bus *bus = codec->bus;
4414
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004415 spin_lock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004416 codec->power_count++;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004417 if (codec->power_on || codec->power_transition > 0) {
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004418 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004419 return;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004420 }
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004421 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004422
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004423 cancel_delayed_work_sync(&codec->power_work);
4424
4425 spin_lock(&codec->power_lock);
Takashi Iwaid66fee52011-08-02 15:39:31 +02004426 trace_hda_power_up(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004427 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004428 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004429 codec->power_jiffies = jiffies;
Takashi Iwai7f308302012-05-08 16:52:23 +02004430 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004431 spin_unlock(&codec->power_lock);
4432
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004433 if (bus->ops.pm_notify)
4434 bus->ops.pm_notify(bus);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004435 hda_call_codec_resume(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004436
4437 spin_lock(&codec->power_lock);
Takashi Iwaia221e282007-08-16 16:35:33 +02004438 codec->power_transition = 0;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004439 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004440}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004441EXPORT_SYMBOL_HDA(snd_hda_power_up);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004442
4443#define power_save(codec) \
4444 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004445
Takashi Iwaid5191e52009-11-16 14:58:17 +01004446/**
4447 * snd_hda_power_down - Power-down the codec
4448 * @codec: HD-audio codec
4449 *
4450 * Decrement the power-up counter and schedules the power-off work if
4451 * the counter rearches to zero.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004452 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004453void snd_hda_power_down(struct hda_codec *codec)
4454{
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004455 spin_lock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004456 --codec->power_count;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004457 if (!codec->power_on || codec->power_count || codec->power_transition) {
4458 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004459 return;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004460 }
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004461 if (power_save(codec)) {
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004462 codec->power_transition = -1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01004463 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004464 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02004465 }
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004466 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004467}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004468EXPORT_SYMBOL_HDA(snd_hda_power_down);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004469
Takashi Iwaid5191e52009-11-16 14:58:17 +01004470/**
4471 * snd_hda_check_amp_list_power - Check the amp list and update the power
4472 * @codec: HD-audio codec
4473 * @check: the object containing an AMP list and the status
4474 * @nid: NID to check / update
4475 *
4476 * Check whether the given NID is in the amp list. If it's in the list,
4477 * check the current AMP status, and update the the power-status according
4478 * to the mute status.
4479 *
4480 * This function is supposed to be set or called from the check_power_status
4481 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004482 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004483int snd_hda_check_amp_list_power(struct hda_codec *codec,
4484 struct hda_loopback_check *check,
4485 hda_nid_t nid)
4486{
Takashi Iwai031024e2011-05-02 11:29:30 +02004487 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004488 int ch, v;
4489
4490 if (!check->amplist)
4491 return 0;
4492 for (p = check->amplist; p->nid; p++) {
4493 if (p->nid == nid)
4494 break;
4495 }
4496 if (!p->nid)
4497 return 0; /* nothing changed */
4498
4499 for (p = check->amplist; p->nid; p++) {
4500 for (ch = 0; ch < 2; ch++) {
4501 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4502 p->idx);
4503 if (!(v & HDA_AMP_MUTE) && v > 0) {
4504 if (!check->power_on) {
4505 check->power_on = 1;
4506 snd_hda_power_up(codec);
4507 }
4508 return 1;
4509 }
4510 }
4511 }
4512 if (check->power_on) {
4513 check->power_on = 0;
4514 snd_hda_power_down(codec);
4515 }
4516 return 0;
4517}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004518EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004519#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004521/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004522 * Channel mode helper
4523 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004524
4525/**
4526 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
4527 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004528int snd_hda_ch_mode_info(struct hda_codec *codec,
4529 struct snd_ctl_elem_info *uinfo,
4530 const struct hda_channel_mode *chmode,
4531 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004532{
4533 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4534 uinfo->count = 1;
4535 uinfo->value.enumerated.items = num_chmodes;
4536 if (uinfo->value.enumerated.item >= num_chmodes)
4537 uinfo->value.enumerated.item = num_chmodes - 1;
4538 sprintf(uinfo->value.enumerated.name, "%dch",
4539 chmode[uinfo->value.enumerated.item].channels);
4540 return 0;
4541}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004542EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004543
Takashi Iwaid5191e52009-11-16 14:58:17 +01004544/**
4545 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
4546 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004547int snd_hda_ch_mode_get(struct hda_codec *codec,
4548 struct snd_ctl_elem_value *ucontrol,
4549 const struct hda_channel_mode *chmode,
4550 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004551 int max_channels)
4552{
4553 int i;
4554
4555 for (i = 0; i < num_chmodes; i++) {
4556 if (max_channels == chmode[i].channels) {
4557 ucontrol->value.enumerated.item[0] = i;
4558 break;
4559 }
4560 }
4561 return 0;
4562}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004563EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004564
Takashi Iwaid5191e52009-11-16 14:58:17 +01004565/**
4566 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
4567 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004568int snd_hda_ch_mode_put(struct hda_codec *codec,
4569 struct snd_ctl_elem_value *ucontrol,
4570 const struct hda_channel_mode *chmode,
4571 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004572 int *max_channelsp)
4573{
4574 unsigned int mode;
4575
4576 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01004577 if (mode >= num_chmodes)
4578 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004579 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004580 return 0;
4581 /* change the current channel setting */
4582 *max_channelsp = chmode[mode].channels;
4583 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004584 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004585 return 1;
4586}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004587EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004588
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589/*
4590 * input MUX helper
4591 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004592
4593/**
4594 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
4595 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004596int snd_hda_input_mux_info(const struct hda_input_mux *imux,
4597 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598{
4599 unsigned int index;
4600
4601 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4602 uinfo->count = 1;
4603 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004604 if (!imux->num_items)
4605 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606 index = uinfo->value.enumerated.item;
4607 if (index >= imux->num_items)
4608 index = imux->num_items - 1;
4609 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
4610 return 0;
4611}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004612EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613
Takashi Iwaid5191e52009-11-16 14:58:17 +01004614/**
4615 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
4616 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004617int snd_hda_input_mux_put(struct hda_codec *codec,
4618 const struct hda_input_mux *imux,
4619 struct snd_ctl_elem_value *ucontrol,
4620 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621 unsigned int *cur_val)
4622{
4623 unsigned int idx;
4624
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004625 if (!imux->num_items)
4626 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627 idx = ucontrol->value.enumerated.item[0];
4628 if (idx >= imux->num_items)
4629 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004630 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004632 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
4633 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634 *cur_val = idx;
4635 return 1;
4636}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004637EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638
4639
4640/*
4641 * Multi-channel / digital-out PCM helper functions
4642 */
4643
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004644/* setup SPDIF output stream */
4645static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
4646 unsigned int stream_tag, unsigned int format)
4647{
Stephen Warren7c9359762011-06-01 11:14:17 -06004648 struct hda_spdif_out *spdif = snd_hda_spdif_out_of_nid(codec, nid);
4649
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004650 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Stephen Warren7c9359762011-06-01 11:14:17 -06004651 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004652 set_dig_out_convert(codec, nid,
Stephen Warren7c9359762011-06-01 11:14:17 -06004653 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02004654 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004655 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02004656 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004657 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004658 for (d = codec->slave_dig_outs; *d; d++)
4659 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
4660 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004661 }
Takashi Iwai2f728532008-09-25 16:32:41 +02004662 /* turn on again (if needed) */
Stephen Warren7c9359762011-06-01 11:14:17 -06004663 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai2f728532008-09-25 16:32:41 +02004664 set_dig_out_convert(codec, nid,
Stephen Warren7c9359762011-06-01 11:14:17 -06004665 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02004666}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004667
Takashi Iwai2f728532008-09-25 16:32:41 +02004668static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
4669{
4670 snd_hda_codec_cleanup_stream(codec, nid);
4671 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004672 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004673 for (d = codec->slave_dig_outs; *d; d++)
4674 snd_hda_codec_cleanup_stream(codec, *d);
4675 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004676}
4677
Takashi Iwaid5191e52009-11-16 14:58:17 +01004678/**
4679 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
4680 * @bus: HD-audio bus
4681 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004682void snd_hda_bus_reboot_notify(struct hda_bus *bus)
4683{
4684 struct hda_codec *codec;
4685
4686 if (!bus)
4687 return;
4688 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02004689 if (hda_codec_is_power_on(codec) &&
4690 codec->patch_ops.reboot_notify)
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004691 codec->patch_ops.reboot_notify(codec);
4692 }
4693}
Takashi Iwai8f217a22009-11-10 18:26:12 +01004694EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004695
Takashi Iwaid5191e52009-11-16 14:58:17 +01004696/**
4697 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004699int snd_hda_multi_out_dig_open(struct hda_codec *codec,
4700 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701{
Ingo Molnar62932df2006-01-16 16:34:20 +01004702 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02004703 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
4704 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02004705 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01004707 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708 return 0;
4709}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004710EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711
Takashi Iwaid5191e52009-11-16 14:58:17 +01004712/**
4713 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
4714 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004715int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
4716 struct hda_multi_out *mout,
4717 unsigned int stream_tag,
4718 unsigned int format,
4719 struct snd_pcm_substream *substream)
4720{
4721 mutex_lock(&codec->spdif_mutex);
4722 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
4723 mutex_unlock(&codec->spdif_mutex);
4724 return 0;
4725}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004726EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004727
Takashi Iwaid5191e52009-11-16 14:58:17 +01004728/**
4729 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
4730 */
Takashi Iwai9411e212009-02-13 11:32:28 +01004731int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
4732 struct hda_multi_out *mout)
4733{
4734 mutex_lock(&codec->spdif_mutex);
4735 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4736 mutex_unlock(&codec->spdif_mutex);
4737 return 0;
4738}
4739EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
4740
Takashi Iwaid5191e52009-11-16 14:58:17 +01004741/**
4742 * snd_hda_multi_out_dig_close - release the digital out stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004744int snd_hda_multi_out_dig_close(struct hda_codec *codec,
4745 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746{
Ingo Molnar62932df2006-01-16 16:34:20 +01004747 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01004749 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750 return 0;
4751}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004752EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753
Takashi Iwaid5191e52009-11-16 14:58:17 +01004754/**
4755 * snd_hda_multi_out_analog_open - open analog outputs
4756 *
4757 * Open analog outputs and set up the hw-constraints.
4758 * If the digital outputs can be opened as slave, open the digital
4759 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004761int snd_hda_multi_out_analog_open(struct hda_codec *codec,
4762 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01004763 struct snd_pcm_substream *substream,
4764 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765{
Takashi Iwai9a081602008-02-12 18:37:26 +01004766 struct snd_pcm_runtime *runtime = substream->runtime;
4767 runtime->hw.channels_max = mout->max_channels;
4768 if (mout->dig_out_nid) {
4769 if (!mout->analog_rates) {
4770 mout->analog_rates = hinfo->rates;
4771 mout->analog_formats = hinfo->formats;
4772 mout->analog_maxbps = hinfo->maxbps;
4773 } else {
4774 runtime->hw.rates = mout->analog_rates;
4775 runtime->hw.formats = mout->analog_formats;
4776 hinfo->maxbps = mout->analog_maxbps;
4777 }
4778 if (!mout->spdif_rates) {
4779 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
4780 &mout->spdif_rates,
4781 &mout->spdif_formats,
4782 &mout->spdif_maxbps);
4783 }
4784 mutex_lock(&codec->spdif_mutex);
4785 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02004786 if ((runtime->hw.rates & mout->spdif_rates) &&
4787 (runtime->hw.formats & mout->spdif_formats)) {
4788 runtime->hw.rates &= mout->spdif_rates;
4789 runtime->hw.formats &= mout->spdif_formats;
4790 if (mout->spdif_maxbps < hinfo->maxbps)
4791 hinfo->maxbps = mout->spdif_maxbps;
4792 } else {
4793 mout->share_spdif = 0;
4794 /* FIXME: need notify? */
4795 }
Takashi Iwai9a081602008-02-12 18:37:26 +01004796 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02004797 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004798 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799 return snd_pcm_hw_constraint_step(substream->runtime, 0,
4800 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
4801}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004802EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803
Takashi Iwaid5191e52009-11-16 14:58:17 +01004804/**
4805 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
4806 *
4807 * Set up the i/o for analog out.
4808 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004810int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
4811 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812 unsigned int stream_tag,
4813 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004814 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815{
Takashi Iwaidda14412011-05-02 11:29:30 +02004816 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817 int chs = substream->runtime->channels;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02004818 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819 int i;
4820
Ingo Molnar62932df2006-01-16 16:34:20 +01004821 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02004822 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Takashi Iwai9a081602008-02-12 18:37:26 +01004823 if (mout->dig_out_nid && mout->share_spdif &&
4824 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02004826 snd_hda_is_supported_format(codec, mout->dig_out_nid,
4827 format) &&
Stephen Warren7c9359762011-06-01 11:14:17 -06004828 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004830 setup_dig_out_stream(codec, mout->dig_out_nid,
4831 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832 } else {
4833 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02004834 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835 }
4836 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004837 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838
4839 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004840 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
4841 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004842 if (!mout->no_share_stream &&
4843 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004845 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
4846 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004847 /* extra outputs copied from front */
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02004848 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
4849 if (!mout->no_share_stream && mout->hp_out_nid[i])
4850 snd_hda_codec_setup_stream(codec,
4851 mout->hp_out_nid[i],
4852 stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004853 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02004854 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01004855 snd_hda_codec_setup_stream(codec,
4856 mout->extra_out_nid[i],
4857 stream_tag, 0, format);
4858
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859 /* surrounds */
4860 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02004861 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004862 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4863 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02004864 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004865 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4866 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867 }
4868 return 0;
4869}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004870EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871
Takashi Iwaid5191e52009-11-16 14:58:17 +01004872/**
4873 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004875int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
4876 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877{
Takashi Iwaidda14412011-05-02 11:29:30 +02004878 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879 int i;
4880
4881 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01004882 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01004884 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02004885 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
4886 if (mout->hp_out_nid[i])
4887 snd_hda_codec_cleanup_stream(codec,
4888 mout->hp_out_nid[i]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01004889 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
4890 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01004891 snd_hda_codec_cleanup_stream(codec,
4892 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01004893 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02004895 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896 mout->dig_out_used = 0;
4897 }
Ingo Molnar62932df2006-01-16 16:34:20 +01004898 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899 return 0;
4900}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004901EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902
Takashi Iwai47408602012-04-20 13:06:53 +02004903/**
4904 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
4905 *
4906 * Guess the suitable VREF pin bits to be set as the pin-control value.
4907 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
4908 */
4909unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
4910{
4911 unsigned int pincap;
4912 unsigned int oldval;
4913 oldval = snd_hda_codec_read(codec, pin, 0,
4914 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
4915 pincap = snd_hda_query_pin_caps(codec, pin);
4916 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
4917 /* Exception: if the default pin setup is vref50, we give it priority */
4918 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
4919 return AC_PINCTL_VREF_80;
4920 else if (pincap & AC_PINCAP_VREF_50)
4921 return AC_PINCTL_VREF_50;
4922 else if (pincap & AC_PINCAP_VREF_100)
4923 return AC_PINCTL_VREF_100;
4924 else if (pincap & AC_PINCAP_VREF_GRD)
4925 return AC_PINCTL_VREF_GRD;
4926 return AC_PINCTL_VREF_HIZ;
4927}
4928EXPORT_SYMBOL_HDA(snd_hda_get_default_vref);
4929
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02004930int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
4931 unsigned int val, bool cached)
4932{
4933 if (val) {
4934 unsigned int cap = snd_hda_query_pin_caps(codec, pin);
Takashi Iwai6942c102012-04-20 13:08:40 +02004935 if (cap && (val & AC_PINCTL_OUT_EN)) {
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02004936 if (!(cap & AC_PINCAP_OUT))
4937 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
4938 else if ((val & AC_PINCTL_HP_EN) &&
4939 !(cap & AC_PINCAP_HP_DRV))
4940 val &= ~AC_PINCTL_HP_EN;
4941 }
Takashi Iwai6942c102012-04-20 13:08:40 +02004942 if (cap && (val & AC_PINCTL_IN_EN)) {
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02004943 if (!(cap & AC_PINCAP_IN))
4944 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
4945 }
4946 }
4947 if (cached)
4948 return snd_hda_codec_update_cache(codec, pin, 0,
4949 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
4950 else
4951 return snd_hda_codec_write(codec, pin, 0,
4952 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
4953}
4954EXPORT_SYMBOL_HDA(_snd_hda_set_pin_ctl);
4955
Takashi Iwai990061c2010-09-09 22:08:44 +02004956/**
4957 * snd_hda_add_imux_item - Add an item to input_mux
4958 *
4959 * When the same label is used already in the existing items, the number
4960 * suffix is appended to the label. This label index number is stored
4961 * to type_idx when non-NULL pointer is given.
4962 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02004963int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
4964 int index, int *type_idx)
4965{
4966 int i, label_idx = 0;
4967 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
4968 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
4969 return -EINVAL;
4970 }
4971 for (i = 0; i < imux->num_items; i++) {
4972 if (!strncmp(label, imux->items[i].label, strlen(label)))
4973 label_idx++;
4974 }
4975 if (type_idx)
4976 *type_idx = label_idx;
4977 if (label_idx > 0)
4978 snprintf(imux->items[imux->num_items].label,
4979 sizeof(imux->items[imux->num_items].label),
4980 "%s %d", label, label_idx);
4981 else
4982 strlcpy(imux->items[imux->num_items].label, label,
4983 sizeof(imux->items[imux->num_items].label));
4984 imux->items[imux->num_items].index = index;
4985 imux->num_items++;
4986 return 0;
4987}
4988EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02004989
Takashi Iwai4a471b72005-12-07 13:56:29 +01004990
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991#ifdef CONFIG_PM
4992/*
4993 * power management
4994 */
4995
4996/**
4997 * snd_hda_suspend - suspend the codecs
4998 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999 *
5000 * Returns 0 if successful.
5001 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02005002int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005003{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005004 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005
Takashi Iwai0ba21762007-04-16 11:29:14 +02005006 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005007 if (hda_codec_is_power_on(codec))
5008 hda_call_codec_suspend(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009 }
5010 return 0;
5011}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005012EXPORT_SYMBOL_HDA(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013
5014/**
5015 * snd_hda_resume - resume the codecs
5016 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017 *
5018 * Returns 0 if successful.
Takashi Iwaicb53c622007-08-10 17:21:45 +02005019 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005020 * This function is defined only when POWER_SAVE isn't set.
Takashi Iwaicb53c622007-08-10 17:21:45 +02005021 * In the power-save mode, the codec is resumed dynamically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022 */
5023int snd_hda_resume(struct hda_bus *bus)
5024{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005025 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026
Takashi Iwai0ba21762007-04-16 11:29:14 +02005027 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai7f308302012-05-08 16:52:23 +02005028 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030 return 0;
5031}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005032EXPORT_SYMBOL_HDA(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005033#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005034
5035/*
5036 * generic arrays
5037 */
5038
Takashi Iwaid5191e52009-11-16 14:58:17 +01005039/**
5040 * snd_array_new - get a new element from the given array
5041 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005042 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01005043 * Get a new element from the given array. If it exceeds the
5044 * pre-allocated array size, re-allocate the array.
5045 *
5046 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02005047 */
5048void *snd_array_new(struct snd_array *array)
5049{
5050 if (array->used >= array->alloced) {
5051 int num = array->alloced + array->alloc_align;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005052 int size = (num + 1) * array->elem_size;
Takashi Iwai00ef9612011-07-14 15:57:27 +02005053 int oldsize = array->alloced * array->elem_size;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01005054 void *nlist;
5055 if (snd_BUG_ON(num >= 4096))
5056 return NULL;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005057 nlist = krealloc(array->list, size, GFP_KERNEL);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005058 if (!nlist)
5059 return NULL;
Takashi Iwai00ef9612011-07-14 15:57:27 +02005060 memset(nlist + oldsize, 0, size - oldsize);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005061 array->list = nlist;
5062 array->alloced = num;
5063 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01005064 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005065}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005066EXPORT_SYMBOL_HDA(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005067
Takashi Iwaid5191e52009-11-16 14:58:17 +01005068/**
5069 * snd_array_free - free the given array elements
5070 * @array: the array object
5071 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005072void snd_array_free(struct snd_array *array)
5073{
5074 kfree(array->list);
5075 array->used = 0;
5076 array->alloced = 0;
5077 array->list = NULL;
5078}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005079EXPORT_SYMBOL_HDA(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01005080
Takashi Iwaid5191e52009-11-16 14:58:17 +01005081/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01005082 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5083 * @pcm: PCM caps bits
5084 * @buf: the string buffer to write
5085 * @buflen: the max buffer length
5086 *
5087 * used by hda_proc.c and hda_eld.c
5088 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005089void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5090{
5091 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5092 int i, j;
5093
5094 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5095 if (pcm & (AC_SUPPCM_BITS_8 << i))
5096 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5097
5098 buf[j] = '\0'; /* necessary when j == 0 */
5099}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005100EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005101
5102MODULE_DESCRIPTION("HDA codec core");
5103MODULE_LICENSE("GPL");