blob: 0037147dcd544035993bc5d6829c002435c36fab [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 Iwai83012a72012-08-24 18:38:08 +020097#ifdef CONFIG_PM
Takashi Iwaid846b172012-11-24 11:58:24 +010098#define codec_in_pm(codec) ((codec)->in_pm)
Takashi Iwaicb53c622007-08-10 17:21:45 +020099static void hda_power_work(struct work_struct *work);
100static void hda_keep_power_on(struct hda_codec *codec);
Takashi Iwaie581f3d2011-07-26 10:19:20 +0200101#define hda_codec_is_power_on(codec) ((codec)->power_on)
Takashi Iwai68467f52012-08-28 09:14:29 -0700102static inline void hda_call_pm_notify(struct hda_bus *bus, bool power_up)
103{
104 if (bus->ops.pm_notify)
105 bus->ops.pm_notify(bus, power_up);
106}
Takashi Iwaicb53c622007-08-10 17:21:45 +0200107#else
Takashi Iwaid846b172012-11-24 11:58:24 +0100108#define codec_in_pm(codec) 0
Takashi Iwaicb53c622007-08-10 17:21:45 +0200109static inline void hda_keep_power_on(struct hda_codec *codec) {}
Takashi Iwaie581f3d2011-07-26 10:19:20 +0200110#define hda_codec_is_power_on(codec) 1
Takashi Iwai68467f52012-08-28 09:14:29 -0700111#define hda_call_pm_notify(bus, state) {}
Takashi Iwaicb53c622007-08-10 17:21:45 +0200112#endif
113
Takashi Iwaid5191e52009-11-16 14:58:17 +0100114/**
115 * snd_hda_get_jack_location - Give a location string of the jack
116 * @cfg: pin default config value
117 *
118 * Parse the pin default config value and returns the string of the
119 * jack location, e.g. "Rear", "Front", etc.
120 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400121const char *snd_hda_get_jack_location(u32 cfg)
122{
123 static char *bases[7] = {
124 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
125 };
126 static unsigned char specials_idx[] = {
127 0x07, 0x08,
128 0x17, 0x18, 0x19,
129 0x37, 0x38
130 };
131 static char *specials[] = {
132 "Rear Panel", "Drive Bar",
133 "Riser", "HDMI", "ATAPI",
134 "Mobile-In", "Mobile-Out"
135 };
136 int i;
137 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
138 if ((cfg & 0x0f) < 7)
139 return bases[cfg & 0x0f];
140 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
141 if (cfg == specials_idx[i])
142 return specials[i];
143 }
144 return "UNKNOWN";
145}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100146EXPORT_SYMBOL_HDA(snd_hda_get_jack_location);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400147
Takashi Iwaid5191e52009-11-16 14:58:17 +0100148/**
149 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
150 * @cfg: pin default config value
151 *
152 * Parse the pin default config value and returns the string of the
153 * jack connectivity, i.e. external or internal connection.
154 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400155const char *snd_hda_get_jack_connectivity(u32 cfg)
156{
157 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
158
159 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
160}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100161EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400162
Takashi Iwaid5191e52009-11-16 14:58:17 +0100163/**
164 * snd_hda_get_jack_type - Give a type string of the jack
165 * @cfg: pin default config value
166 *
167 * Parse the pin default config value and returns the string of the
168 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
169 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400170const char *snd_hda_get_jack_type(u32 cfg)
171{
172 static char *jack_types[16] = {
173 "Line Out", "Speaker", "HP Out", "CD",
174 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
175 "Line In", "Aux", "Mic", "Telephony",
176 "SPDIF In", "Digitial In", "Reserved", "Other"
177 };
178
179 return jack_types[(cfg & AC_DEFCFG_DEVICE)
180 >> AC_DEFCFG_DEVICE_SHIFT];
181}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100182EXPORT_SYMBOL_HDA(snd_hda_get_jack_type);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400183
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100184/*
185 * Compose a 32bit command word to be sent to the HD-audio controller
186 */
187static inline unsigned int
188make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int direct,
189 unsigned int verb, unsigned int parm)
190{
191 u32 val;
192
Takashi Iwai82e1b802009-07-17 12:47:34 +0200193 if ((codec->addr & ~0xf) || (direct & ~1) || (nid & ~0x7f) ||
194 (verb & ~0xfff) || (parm & ~0xffff)) {
Wu Fengguang6430aee2009-07-17 16:49:19 +0800195 printk(KERN_ERR "hda-codec: out of range cmd %x:%x:%x:%x:%x\n",
196 codec->addr, direct, nid, verb, parm);
197 return ~0;
198 }
199
200 val = (u32)codec->addr << 28;
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100201 val |= (u32)direct << 27;
202 val |= (u32)nid << 20;
203 val |= verb << 8;
204 val |= parm;
205 return val;
206}
207
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200208/*
209 * Send and receive a verb
210 */
211static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
212 unsigned int *res)
213{
214 struct hda_bus *bus = codec->bus;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200215 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200216
Wu Fengguang6430aee2009-07-17 16:49:19 +0800217 if (cmd == ~0)
218 return -1;
219
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200220 if (res)
221 *res = -1;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200222 again:
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200223 snd_hda_power_up(codec);
224 mutex_lock(&bus->cmd_mutex);
Takashi Iwaid66fee52011-08-02 15:39:31 +0200225 trace_hda_send_cmd(codec, cmd);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200226 err = bus->ops.command(bus, cmd);
Takashi Iwaid66fee52011-08-02 15:39:31 +0200227 if (!err && res) {
Wu Fengguangdeadff12009-08-01 18:45:16 +0800228 *res = bus->ops.get_response(bus, codec->addr);
Takashi Iwaid66fee52011-08-02 15:39:31 +0200229 trace_hda_get_response(codec, *res);
230 }
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200231 mutex_unlock(&bus->cmd_mutex);
232 snd_hda_power_down(codec);
Takashi Iwaid846b172012-11-24 11:58:24 +0100233 if (!codec_in_pm(codec) && res && *res == -1 && bus->rirb_error) {
Takashi Iwai8dd78332009-06-02 01:16:07 +0200234 if (bus->response_reset) {
235 snd_printd("hda_codec: resetting BUS due to "
236 "fatal communication error\n");
Takashi Iwaid66fee52011-08-02 15:39:31 +0200237 trace_hda_bus_reset(bus);
Takashi Iwai8dd78332009-06-02 01:16:07 +0200238 bus->ops.bus_reset(bus);
239 }
240 goto again;
241 }
242 /* clear reset-flag when the communication gets recovered */
Takashi Iwaid846b172012-11-24 11:58:24 +0100243 if (!err || codec_in_pm(codec))
Takashi Iwai8dd78332009-06-02 01:16:07 +0200244 bus->response_reset = 0;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200245 return err;
246}
247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248/**
249 * snd_hda_codec_read - send a command and get the response
250 * @codec: the HDA codec
251 * @nid: NID to send the command
252 * @direct: direct flag
253 * @verb: the verb to send
254 * @parm: the parameter for the verb
255 *
256 * Send a single command and read the corresponding response.
257 *
258 * Returns the obtained response value, or -1 for an error.
259 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200260unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
261 int direct,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 unsigned int verb, unsigned int parm)
263{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200264 unsigned cmd = make_codec_cmd(codec, nid, direct, verb, parm);
265 unsigned int res;
Greg Thelen9857edf2011-06-13 07:45:45 -0700266 if (codec_exec_verb(codec, cmd, &res))
267 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 return res;
269}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100270EXPORT_SYMBOL_HDA(snd_hda_codec_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272/**
273 * snd_hda_codec_write - send a single command without waiting for response
274 * @codec: the HDA codec
275 * @nid: NID to send the command
276 * @direct: direct flag
277 * @verb: the verb to send
278 * @parm: the parameter for the verb
279 *
280 * Send a single command without waiting for response.
281 *
282 * Returns 0 if successful, or a negative error code.
283 */
284int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
285 unsigned int verb, unsigned int parm)
286{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200287 unsigned int cmd = make_codec_cmd(codec, nid, direct, verb, parm);
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100288 unsigned int res;
Takashi Iwaib20f3b82009-06-02 01:20:22 +0200289 return codec_exec_verb(codec, cmd,
290 codec->bus->sync_write ? &res : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100292EXPORT_SYMBOL_HDA(snd_hda_codec_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
294/**
295 * snd_hda_sequence_write - sequence writes
296 * @codec: the HDA codec
297 * @seq: VERB array to send
298 *
299 * Send the commands sequentially from the given array.
300 * The array must be terminated with NID=0.
301 */
302void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
303{
304 for (; seq->nid; seq++)
305 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
306}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100307EXPORT_SYMBOL_HDA(snd_hda_sequence_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309/**
310 * snd_hda_get_sub_nodes - get the range of sub nodes
311 * @codec: the HDA codec
312 * @nid: NID to parse
313 * @start_id: the pointer to store the start NID
314 *
315 * Parse the NID and store the start NID of its sub-nodes.
316 * Returns the number of sub-nodes.
317 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200318int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
319 hda_nid_t *start_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
321 unsigned int parm;
322
323 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
Danny Tholene8a7f132007-09-11 21:41:56 +0200324 if (parm == -1)
325 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 *start_id = (parm >> 16) & 0x7fff;
327 return (int)(parm & 0x7fff);
328}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100329EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200331/* look up the cached results */
332static hda_nid_t *lookup_conn_list(struct snd_array *array, hda_nid_t nid)
333{
334 int i, len;
335 for (i = 0; i < array->used; ) {
336 hda_nid_t *p = snd_array_elem(array, i);
337 if (nid == *p)
338 return p;
339 len = p[1];
340 i += len + 2;
341 }
342 return NULL;
343}
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200344
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200345/* read the connection and add to the cache */
346static int read_and_add_raw_conns(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200347{
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200348 hda_nid_t list[HDA_MAX_CONNECTIONS];
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200349 int len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200350
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200351 len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list));
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200352 if (len < 0)
353 return len;
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200354 return snd_hda_override_conn_list(codec, nid, len, list);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200355}
Takashi Iwaidce20792011-06-24 14:10:28 +0200356
357/**
358 * snd_hda_get_connections - copy connection list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 * @codec: the HDA codec
360 * @nid: NID to parse
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200361 * @conn_list: connection list array; when NULL, checks only the size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 * @max_conns: max. number of connections to store
363 *
364 * Parses the connection list of the given widget and stores the list
365 * of NIDs.
366 *
367 * Returns the number of connections, or a negative error code.
368 */
369int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200370 hda_nid_t *conn_list, int max_conns)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200371{
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200372 struct snd_array *array = &codec->conn_lists;
373 int len;
374 hda_nid_t *p;
375 bool added = false;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200376
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200377 again:
378 mutex_lock(&codec->hash_mutex);
379 len = -1;
380 /* if the connection-list is already cached, read it */
381 p = lookup_conn_list(array, nid);
382 if (p) {
383 len = p[1];
384 if (conn_list && len > max_conns) {
385 snd_printk(KERN_ERR "hda_codec: "
386 "Too many connections %d for NID 0x%x\n",
387 len, nid);
388 mutex_unlock(&codec->hash_mutex);
389 return -EINVAL;
390 }
391 if (conn_list && len)
392 memcpy(conn_list, p + 2, len * sizeof(hda_nid_t));
Takashi Iwaidce20792011-06-24 14:10:28 +0200393 }
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200394 mutex_unlock(&codec->hash_mutex);
395 if (len >= 0)
396 return len;
397 if (snd_BUG_ON(added))
398 return -EINVAL;
399
400 len = read_and_add_raw_conns(codec, nid);
401 if (len < 0)
402 return len;
403 added = true;
404 goto again;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200405}
406EXPORT_SYMBOL_HDA(snd_hda_get_connections);
407
Takashi Iwai9e7717c2011-07-11 15:42:52 +0200408/**
409 * snd_hda_get_raw_connections - copy connection list without cache
410 * @codec: the HDA codec
411 * @nid: NID to parse
412 * @conn_list: connection list array
413 * @max_conns: max. number of connections to store
414 *
415 * Like snd_hda_get_connections(), copy the connection list but without
416 * checking through the connection-list cache.
417 * Currently called only from hda_proc.c, so not exported.
418 */
419int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid,
420 hda_nid_t *conn_list, int max_conns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
422 unsigned int parm;
Takashi Iwai54d17402005-11-21 16:33:22 +0100423 int i, conn_len, conns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 unsigned int shift, num_elems, mask;
Takashi Iwai1ba7a7c2009-07-27 12:56:26 +0200425 unsigned int wcaps;
Takashi Iwai54d17402005-11-21 16:33:22 +0100426 hda_nid_t prev_nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Takashi Iwaida3cec32008-08-08 17:12:14 +0200428 if (snd_BUG_ON(!conn_list || max_conns <= 0))
429 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Takashi Iwai1ba7a7c2009-07-27 12:56:26 +0200431 wcaps = get_wcaps(codec, nid);
432 if (!(wcaps & AC_WCAP_CONN_LIST) &&
Takashi Iwai8d087c72011-06-28 12:45:47 +0200433 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
434 return 0;
Jaroslav Kysela16a433d2009-07-22 16:20:40 +0200435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
437 if (parm & AC_CLIST_LONG) {
438 /* long form */
439 shift = 16;
440 num_elems = 2;
441 } else {
442 /* short form */
443 shift = 8;
444 num_elems = 4;
445 }
446 conn_len = parm & AC_CLIST_LENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 mask = (1 << (shift-1)) - 1;
448
Takashi Iwai0ba21762007-04-16 11:29:14 +0200449 if (!conn_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 return 0; /* no connection */
451
452 if (conn_len == 1) {
453 /* single connection */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200454 parm = snd_hda_codec_read(codec, nid, 0,
455 AC_VERB_GET_CONNECT_LIST, 0);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200456 if (parm == -1 && codec->bus->rirb_error)
457 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 conn_list[0] = parm & mask;
459 return 1;
460 }
461
462 /* multi connection */
463 conns = 0;
Takashi Iwai54d17402005-11-21 16:33:22 +0100464 prev_nid = 0;
465 for (i = 0; i < conn_len; i++) {
466 int range_val;
467 hda_nid_t val, n;
468
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200469 if (i % num_elems == 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +0100470 parm = snd_hda_codec_read(codec, nid, 0,
471 AC_VERB_GET_CONNECT_LIST, i);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200472 if (parm == -1 && codec->bus->rirb_error)
473 return -EIO;
474 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200475 range_val = !!(parm & (1 << (shift-1))); /* ranges */
Takashi Iwai54d17402005-11-21 16:33:22 +0100476 val = parm & mask;
Jaroslav Kysela2e9bf242009-07-18 11:48:19 +0200477 if (val == 0) {
478 snd_printk(KERN_WARNING "hda_codec: "
479 "invalid CONNECT_LIST verb %x[%i]:%x\n",
480 nid, i, parm);
481 return 0;
482 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100483 parm >>= shift;
484 if (range_val) {
485 /* ranges between the previous and this one */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200486 if (!prev_nid || prev_nid >= val) {
487 snd_printk(KERN_WARNING "hda_codec: "
488 "invalid dep_range_val %x:%x\n",
489 prev_nid, val);
Takashi Iwai54d17402005-11-21 16:33:22 +0100490 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100492 for (n = prev_nid + 1; n <= val; n++) {
493 if (conns >= max_conns) {
Takashi Iwai5aacc212010-07-30 10:36:29 +0200494 snd_printk(KERN_ERR "hda_codec: "
495 "Too many connections %d for NID 0x%x\n",
496 conns, nid);
Takashi Iwai54d17402005-11-21 16:33:22 +0100497 return -EINVAL;
498 }
499 conn_list[conns++] = n;
500 }
501 } else {
502 if (conns >= max_conns) {
Takashi Iwai5aacc212010-07-30 10:36:29 +0200503 snd_printk(KERN_ERR "hda_codec: "
504 "Too many connections %d for NID 0x%x\n",
505 conns, nid);
Takashi Iwai54d17402005-11-21 16:33:22 +0100506 return -EINVAL;
507 }
508 conn_list[conns++] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100510 prev_nid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 }
512 return conns;
513}
514
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200515static bool add_conn_list(struct snd_array *array, hda_nid_t nid)
516{
517 hda_nid_t *p = snd_array_new(array);
518 if (!p)
519 return false;
520 *p = nid;
521 return true;
522}
523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524/**
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200525 * snd_hda_override_conn_list - add/modify the connection-list to cache
526 * @codec: the HDA codec
527 * @nid: NID to parse
528 * @len: number of connection list entries
529 * @list: the list of connection entries
530 *
531 * Add or modify the given connection-list to the cache. If the corresponding
532 * cache already exists, invalidate it and append a new one.
533 *
534 * Returns zero or a negative error code.
535 */
536int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
537 const hda_nid_t *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200539 struct snd_array *array = &codec->conn_lists;
540 hda_nid_t *p;
541 int i, old_used;
542
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200543 mutex_lock(&codec->hash_mutex);
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200544 p = lookup_conn_list(array, nid);
545 if (p)
546 *p = -1; /* invalidate the old entry */
547
548 old_used = array->used;
549 if (!add_conn_list(array, nid) || !add_conn_list(array, len))
550 goto error_add;
551 for (i = 0; i < len; i++)
552 if (!add_conn_list(array, list[i]))
553 goto error_add;
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200554 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200555 return 0;
556
557 error_add:
558 array->used = old_used;
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200559 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200560 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 Iwai6a0f56a2012-12-07 07:41:56 +0100741int 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;
Takashi Iwai5d908ab2012-08-24 18:40:10 +0200819 unsigned 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 Iwai6a0f56a2012-12-07 07:41:56 +0100911static void 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
Takashi Iwai3be14142009-02-20 14:11:16 +0100996/* set the current pin config value for the given NID.
997 * the value is cached, and read via snd_hda_codec_get_pincfg()
998 */
999int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
1000 hda_nid_t nid, unsigned int cfg)
1001{
1002 struct hda_pincfg *pin;
1003
Takashi Iwaib82855a2009-12-27 11:24:56 +01001004 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
1005 return -EINVAL;
1006
Takashi Iwai3be14142009-02-20 14:11:16 +01001007 pin = look_up_pincfg(codec, list, nid);
1008 if (!pin) {
1009 pin = snd_array_new(list);
1010 if (!pin)
1011 return -ENOMEM;
1012 pin->nid = nid;
1013 }
1014 pin->cfg = cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001015 return 0;
1016}
1017
Takashi Iwaid5191e52009-11-16 14:58:17 +01001018/**
1019 * snd_hda_codec_set_pincfg - Override a pin default configuration
1020 * @codec: the HDA codec
1021 * @nid: NID to set the pin config
1022 * @cfg: the pin default config value
1023 *
1024 * Override a pin default configuration value in the cache.
1025 * This value can be read by snd_hda_codec_get_pincfg() in a higher
1026 * priority than the real hardware value.
1027 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001028int snd_hda_codec_set_pincfg(struct hda_codec *codec,
1029 hda_nid_t nid, unsigned int cfg)
1030{
Takashi Iwai346ff702009-02-23 09:42:57 +01001031 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001032}
1033EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
1034
Takashi Iwaid5191e52009-11-16 14:58:17 +01001035/**
1036 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
1037 * @codec: the HDA codec
1038 * @nid: NID to get the pin config
1039 *
1040 * Get the current pin config value of the given pin NID.
1041 * If the pincfg value is cached or overridden via sysfs or driver,
1042 * returns the cached value.
1043 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001044unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
1045{
1046 struct hda_pincfg *pin;
1047
Takashi Iwai3be14142009-02-20 14:11:16 +01001048#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +01001049 pin = look_up_pincfg(codec, &codec->user_pins, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +01001050 if (pin)
1051 return pin->cfg;
1052#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001053 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
1054 if (pin)
1055 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001056 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1057 if (pin)
1058 return pin->cfg;
1059 return 0;
1060}
1061EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
1062
Takashi Iwai92ee6162009-12-27 11:18:59 +01001063/**
1064 * snd_hda_shutup_pins - Shut up all pins
1065 * @codec: the HDA codec
1066 *
1067 * Clear all pin controls to shup up before suspend for avoiding click noise.
1068 * The controls aren't cached so that they can be resumed properly.
1069 */
1070void snd_hda_shutup_pins(struct hda_codec *codec)
1071{
1072 int i;
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001073 /* don't shut up pins when unloading the driver; otherwise it breaks
1074 * the default pin setup at the next load of the driver
1075 */
1076 if (codec->bus->shutdown)
1077 return;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001078 for (i = 0; i < codec->init_pins.used; i++) {
1079 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1080 /* use read here for syncing after issuing each verb */
1081 snd_hda_codec_read(codec, pin->nid, 0,
1082 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
1083 }
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001084 codec->pins_shutup = 1;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001085}
1086EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);
1087
Takashi Iwai2a439522011-07-26 09:52:50 +02001088#ifdef CONFIG_PM
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001089/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1090static void restore_shutup_pins(struct hda_codec *codec)
1091{
1092 int i;
1093 if (!codec->pins_shutup)
1094 return;
1095 if (codec->bus->shutdown)
1096 return;
1097 for (i = 0; i < codec->init_pins.used; i++) {
1098 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1099 snd_hda_codec_write(codec, pin->nid, 0,
1100 AC_VERB_SET_PIN_WIDGET_CONTROL,
1101 pin->ctrl);
1102 }
1103 codec->pins_shutup = 0;
1104}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001105#endif
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001106
David Henningsson26a6cb62012-10-09 15:04:21 +02001107static void hda_jackpoll_work(struct work_struct *work)
1108{
1109 struct hda_codec *codec =
1110 container_of(work, struct hda_codec, jackpoll_work.work);
1111 if (!codec->jackpoll_interval)
1112 return;
1113
1114 snd_hda_jack_set_dirty_all(codec);
1115 snd_hda_jack_poll_all(codec);
1116 queue_delayed_work(codec->bus->workq, &codec->jackpoll_work,
1117 codec->jackpoll_interval);
1118}
1119
Takashi Iwai01751f52007-08-10 16:59:39 +02001120static void init_hda_cache(struct hda_cache_rec *cache,
1121 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001122static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +02001123
Takashi Iwai3fdf1462012-11-22 08:16:31 +01001124/* release all pincfg lists */
1125static void free_init_pincfgs(struct hda_codec *codec)
Takashi Iwai3be14142009-02-20 14:11:16 +01001126{
Takashi Iwai346ff702009-02-23 09:42:57 +01001127 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001128#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +01001129 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001130#endif
Takashi Iwai3be14142009-02-20 14:11:16 +01001131 snd_array_free(&codec->init_pins);
1132}
1133
Takashi Iwai54d17402005-11-21 16:33:22 +01001134/*
Takashi Iwaieb541332010-08-06 13:48:11 +02001135 * audio-converter setup caches
1136 */
1137struct hda_cvt_setup {
1138 hda_nid_t nid;
1139 u8 stream_tag;
1140 u8 channel_id;
1141 u16 format_id;
1142 unsigned char active; /* cvt is currently used */
1143 unsigned char dirty; /* setups should be cleared */
1144};
1145
1146/* get or create a cache entry for the given audio converter NID */
1147static struct hda_cvt_setup *
1148get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1149{
1150 struct hda_cvt_setup *p;
1151 int i;
1152
1153 for (i = 0; i < codec->cvt_setups.used; i++) {
1154 p = snd_array_elem(&codec->cvt_setups, i);
1155 if (p->nid == nid)
1156 return p;
1157 }
1158 p = snd_array_new(&codec->cvt_setups);
1159 if (p)
1160 p->nid = nid;
1161 return p;
1162}
1163
1164/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 * codec destructor
1166 */
1167static void snd_hda_codec_free(struct hda_codec *codec)
1168{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001169 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 return;
David Henningsson26a6cb62012-10-09 15:04:21 +02001171 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai59cad162012-06-26 15:00:20 +02001172 snd_hda_jack_tbl_clear(codec);
Takashi Iwai3fdf1462012-11-22 08:16:31 +01001173 free_init_pincfgs(codec);
Takashi Iwai83012a72012-08-24 18:38:08 +02001174#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02001175 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01001176 flush_workqueue(codec->bus->workq);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001177#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001179 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001180 snd_array_free(&codec->nids);
Takashi Iwai59cad162012-06-26 15:00:20 +02001181 snd_array_free(&codec->cvt_setups);
Takashi Iwaia12d3e12011-04-07 15:55:15 +02001182 snd_array_free(&codec->conn_lists);
Stephen Warren7c9359762011-06-01 11:14:17 -06001183 snd_array_free(&codec->spdif_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 codec->bus->caddr_tbl[codec->addr] = NULL;
1185 if (codec->patch_ops.free)
1186 codec->patch_ops.free(codec);
Takashi Iwai83012a72012-08-24 18:38:08 +02001187#ifdef CONFIG_PM
Takashi Iwai08fa20a2012-08-31 07:46:56 -07001188 if (!codec->pm_down_notified) /* cancel leftover refcounts */
Takashi Iwai68467f52012-08-28 09:14:29 -07001189 hda_call_pm_notify(codec->bus, false);
1190#endif
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001191 module_put(codec->owner);
Takashi Iwai01751f52007-08-10 16:59:39 +02001192 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001193 free_hda_cache(&codec->cmd_cache);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001194 kfree(codec->vendor_name);
1195 kfree(codec->chip_name);
Takashi Iwaif44ac832008-07-30 15:01:45 +02001196 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +01001197 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 kfree(codec);
1199}
1200
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001201static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec,
1202 hda_nid_t fg, unsigned int power_state);
1203
Takashi Iwaid8193872012-08-31 07:54:38 -07001204static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001205 unsigned int power_state);
1206
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207/**
1208 * snd_hda_codec_new - create a HDA codec
1209 * @bus: the bus to assign
1210 * @codec_addr: the codec address
1211 * @codecp: the pointer to store the generated codec
1212 *
1213 * Returns 0 if successful, or a negative error code.
1214 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001215int snd_hda_codec_new(struct hda_bus *bus,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001216 unsigned int codec_addr,
1217 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218{
1219 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +02001220 char component[31];
Takashi Iwaid8193872012-08-31 07:54:38 -07001221 hda_nid_t fg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 int err;
1223
Takashi Iwaida3cec32008-08-08 17:12:14 +02001224 if (snd_BUG_ON(!bus))
1225 return -EINVAL;
1226 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1227 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
1229 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001230 snd_printk(KERN_ERR "hda_codec: "
1231 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 return -EBUSY;
1233 }
1234
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001235 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 if (codec == NULL) {
1237 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1238 return -ENOMEM;
1239 }
1240
1241 codec->bus = bus;
1242 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001243 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001244 mutex_init(&codec->control_mutex);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001245 mutex_init(&codec->hash_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +02001246 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001247 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001248 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1249 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001250 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001251 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +02001252 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Takashi Iwaia12d3e12011-04-07 15:55:15 +02001253 snd_array_init(&codec->conn_lists, sizeof(hda_nid_t), 64);
Stephen Warren7c9359762011-06-01 11:14:17 -06001254 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
Takashi Iwai361dab32012-05-09 14:35:27 +02001255 snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16);
David Henningsson26a6cb62012-10-09 15:04:21 +02001256 INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Takashi Iwai83012a72012-08-24 18:38:08 +02001258#ifdef CONFIG_PM
Takashi Iwai5536c6d2012-05-08 17:08:10 +02001259 spin_lock_init(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001260 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1261 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1262 * the caller has to power down appropriatley after initialization
1263 * phase.
1264 */
1265 hda_keep_power_on(codec);
Takashi Iwai68467f52012-08-28 09:14:29 -07001266 hda_call_pm_notify(bus, true);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001267#endif
1268
Takashi Iwaic382a9f2012-05-07 15:01:02 +02001269 if (codec->bus->modelname) {
1270 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1271 if (!codec->modelname) {
1272 snd_hda_codec_free(codec);
1273 return -ENODEV;
1274 }
1275 }
1276
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 list_add_tail(&codec->list, &bus->codec_list);
1278 bus->caddr_tbl[codec_addr] = codec;
1279
Takashi Iwai0ba21762007-04-16 11:29:14 +02001280 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1281 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001282 if (codec->vendor_id == -1)
1283 /* read again, hopefully the access method was corrected
1284 * in the last read...
1285 */
1286 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1287 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001288 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1289 AC_PAR_SUBSYSTEM_ID);
1290 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1291 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001293 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001294 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001295 snd_printdd("hda_codec: no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001296 err = -ENODEV;
1297 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 }
1299
Takashi Iwaid8193872012-08-31 07:54:38 -07001300 fg = codec->afg ? codec->afg : codec->mfg;
1301 err = read_widget_caps(codec, fg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001302 if (err < 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +01001303 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001304 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001305 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001306 err = read_pin_defaults(codec);
1307 if (err < 0)
1308 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001309
Takashi Iwai0ba21762007-04-16 11:29:14 +02001310 if (!codec->subsystem_id) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001311 codec->subsystem_id =
Takashi Iwaid8193872012-08-31 07:54:38 -07001312 snd_hda_codec_read(codec, fg, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001313 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001314 }
1315
Takashi Iwai83012a72012-08-24 18:38:08 +02001316#ifdef CONFIG_PM
Takashi Iwaid8193872012-08-31 07:54:38 -07001317 codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec, fg,
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001318 AC_PWRST_CLKSTOP);
1319 if (!codec->d3_stop_clk)
1320 bus->power_keep_link_on = 1;
Mengdong Lin5d6147f2012-08-24 12:06:30 +08001321#endif
Takashi Iwaid8193872012-08-31 07:54:38 -07001322 codec->epss = snd_hda_codec_get_supported_ps(codec, fg,
Takashi Iwai983f6b92012-08-28 09:18:01 -07001323 AC_PWRST_EPSS);
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001324
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001325 /* power-up all before initialization */
Takashi Iwaid8193872012-08-31 07:54:38 -07001326 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001327
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001328 snd_hda_codec_proc_new(codec);
1329
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001330 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001331
1332 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1333 codec->subsystem_id, codec->revision_id);
1334 snd_component_add(codec->bus->card, component);
1335
1336 if (codecp)
1337 *codecp = codec;
1338 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001339
1340 error:
1341 snd_hda_codec_free(codec);
1342 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001343}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001344EXPORT_SYMBOL_HDA(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001345
Takashi Iwaid5191e52009-11-16 14:58:17 +01001346/**
1347 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1348 * @codec: the HDA codec
1349 *
1350 * Start parsing of the given codec tree and (re-)initialize the whole
1351 * patch instance.
1352 *
1353 * Returns 0 if successful or a negative error code.
1354 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001355int snd_hda_codec_configure(struct hda_codec *codec)
1356{
1357 int err;
1358
Takashi Iwaid5ad6302007-03-07 15:55:59 +01001359 codec->preset = find_codec_preset(codec);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001360 if (!codec->vendor_name || !codec->chip_name) {
Takashi Iwaif44ac832008-07-30 15:01:45 +02001361 err = get_codec_name(codec);
1362 if (err < 0)
1363 return err;
1364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
Takashi Iwai82467612007-07-27 19:15:54 +02001366 if (is_generic_config(codec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai82467612007-07-27 19:15:54 +02001368 goto patched;
1369 }
Takashi Iwai82467612007-07-27 19:15:54 +02001370 if (codec->preset && codec->preset->patch) {
1371 err = codec->preset->patch(codec);
1372 goto patched;
1373 }
1374
1375 /* call the default parser */
Takashi Iwai82467612007-07-27 19:15:54 +02001376 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai35a1e0c2007-10-19 08:13:40 +02001377 if (err < 0)
1378 printk(KERN_ERR "hda-codec: No codec parser is available\n");
Takashi Iwai82467612007-07-27 19:15:54 +02001379
1380 patched:
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001381 if (!err && codec->patch_ops.unsol_event)
1382 err = init_unsol_queue(codec->bus);
Takashi Iwaif62faed2009-12-23 09:27:51 +01001383 /* audio codec should override the mixer name */
1384 if (!err && (codec->afg || !*codec->bus->card->mixername))
1385 snprintf(codec->bus->card->mixername,
1386 sizeof(codec->bus->card->mixername),
1387 "%s %s", codec->vendor_name, codec->chip_name);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001388 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389}
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001390EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
Takashi Iwaied360812012-08-08 17:12:52 +02001392/* update the stream-id if changed */
1393static void update_pcm_stream_id(struct hda_codec *codec,
1394 struct hda_cvt_setup *p, hda_nid_t nid,
1395 u32 stream_tag, int channel_id)
1396{
1397 unsigned int oldval, newval;
1398
1399 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1400 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1401 newval = (stream_tag << 4) | channel_id;
1402 if (oldval != newval)
1403 snd_hda_codec_write(codec, nid, 0,
1404 AC_VERB_SET_CHANNEL_STREAMID,
1405 newval);
1406 p->stream_tag = stream_tag;
1407 p->channel_id = channel_id;
1408 }
1409}
1410
1411/* update the format-id if changed */
1412static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1413 hda_nid_t nid, int format)
1414{
1415 unsigned int oldval;
1416
1417 if (p->format_id != format) {
1418 oldval = snd_hda_codec_read(codec, nid, 0,
1419 AC_VERB_GET_STREAM_FORMAT, 0);
1420 if (oldval != format) {
1421 msleep(1);
1422 snd_hda_codec_write(codec, nid, 0,
1423 AC_VERB_SET_STREAM_FORMAT,
1424 format);
1425 }
1426 p->format_id = format;
1427 }
1428}
1429
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430/**
1431 * snd_hda_codec_setup_stream - set up the codec for streaming
1432 * @codec: the CODEC to set up
1433 * @nid: the NID to set up
1434 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1435 * @channel_id: channel id to pass, zero based.
1436 * @format: stream format.
1437 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001438void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1439 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 int channel_id, int format)
1441{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001442 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001443 struct hda_cvt_setup *p;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001444 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001445 int i;
1446
Takashi Iwai0ba21762007-04-16 11:29:14 +02001447 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001448 return;
1449
Takashi Iwai0ba21762007-04-16 11:29:14 +02001450 snd_printdd("hda_codec_setup_stream: "
1451 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001453 p = get_hda_cvt_setup(codec, nid);
1454 if (!p)
1455 return;
Takashi Iwaied360812012-08-08 17:12:52 +02001456
1457 if (codec->pcm_format_first)
1458 update_pcm_format(codec, p, nid, format);
1459 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1460 if (!codec->pcm_format_first)
1461 update_pcm_format(codec, p, nid, format);
1462
Takashi Iwaieb541332010-08-06 13:48:11 +02001463 p->active = 1;
1464 p->dirty = 0;
1465
1466 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001467 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001468 list_for_each_entry(c, &codec->bus->codec_list, list) {
1469 for (i = 0; i < c->cvt_setups.used; i++) {
1470 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001471 if (!p->active && p->stream_tag == stream_tag &&
David Henningsson54c2a892012-02-01 12:05:41 +01001472 get_wcaps_type(get_wcaps(c, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001473 p->dirty = 1;
1474 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001477EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
Takashi Iwaif0cea792010-08-13 11:56:53 +02001479static void really_cleanup_stream(struct hda_codec *codec,
1480 struct hda_cvt_setup *q);
1481
Takashi Iwaid5191e52009-11-16 14:58:17 +01001482/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001483 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001484 * @codec: the CODEC to clean up
1485 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001486 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001487 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001488void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1489 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001490{
Takashi Iwaieb541332010-08-06 13:48:11 +02001491 struct hda_cvt_setup *p;
1492
Takashi Iwai888afa12008-03-18 09:57:50 +01001493 if (!nid)
1494 return;
1495
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001496 if (codec->no_sticky_stream)
1497 do_now = 1;
1498
Takashi Iwai888afa12008-03-18 09:57:50 +01001499 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001500 p = get_hda_cvt_setup(codec, nid);
Takashi Iwaif0cea792010-08-13 11:56:53 +02001501 if (p) {
1502 /* here we just clear the active flag when do_now isn't set;
1503 * actual clean-ups will be done later in
1504 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1505 */
1506 if (do_now)
1507 really_cleanup_stream(codec, p);
1508 else
1509 p->active = 0;
1510 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001511}
Takashi Iwaif0cea792010-08-13 11:56:53 +02001512EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001513
Takashi Iwaieb541332010-08-06 13:48:11 +02001514static void really_cleanup_stream(struct hda_codec *codec,
1515 struct hda_cvt_setup *q)
1516{
1517 hda_nid_t nid = q->nid;
Takashi Iwai218264a2011-09-27 17:33:45 +02001518 if (q->stream_tag || q->channel_id)
1519 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1520 if (q->format_id)
1521 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1522);
Takashi Iwaieb541332010-08-06 13:48:11 +02001523 memset(q, 0, sizeof(*q));
1524 q->nid = nid;
1525}
1526
1527/* clean up the all conflicting obsolete streams */
1528static void purify_inactive_streams(struct hda_codec *codec)
1529{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001530 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001531 int i;
1532
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001533 list_for_each_entry(c, &codec->bus->codec_list, list) {
1534 for (i = 0; i < c->cvt_setups.used; i++) {
1535 struct hda_cvt_setup *p;
1536 p = snd_array_elem(&c->cvt_setups, i);
1537 if (p->dirty)
1538 really_cleanup_stream(c, p);
1539 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001540 }
1541}
1542
Takashi Iwai2a439522011-07-26 09:52:50 +02001543#ifdef CONFIG_PM
Takashi Iwaieb541332010-08-06 13:48:11 +02001544/* clean up all streams; called from suspend */
1545static void hda_cleanup_all_streams(struct hda_codec *codec)
1546{
1547 int i;
1548
1549 for (i = 0; i < codec->cvt_setups.used; i++) {
1550 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1551 if (p->stream_tag)
1552 really_cleanup_stream(codec, p);
1553 }
1554}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001555#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001556
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557/*
1558 * amp access functions
1559 */
1560
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001561/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001562#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001563#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001564#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1565#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001567#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568
1569/* initialize the hash table */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001570static void init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001571 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572{
Takashi Iwai01751f52007-08-10 16:59:39 +02001573 memset(cache, 0, sizeof(*cache));
1574 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001575 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001576}
1577
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001578static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001579{
Takashi Iwai603c4012008-07-30 15:01:44 +02001580 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581}
1582
1583/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001584static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585{
Takashi Iwai01751f52007-08-10 16:59:39 +02001586 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1587 u16 cur = cache->hash[idx];
1588 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
1590 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001591 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 if (info->key == key)
1593 return info;
1594 cur = info->next;
1595 }
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001596 return NULL;
1597}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001599/* query the hash. allocate an entry if not found. */
1600static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1601 u32 key)
1602{
1603 struct hda_cache_head *info = get_hash(cache, key);
1604 if (!info) {
1605 u16 idx, cur;
1606 /* add a new hash entry */
1607 info = snd_array_new(&cache->buf);
1608 if (!info)
1609 return NULL;
1610 cur = snd_array_index(&cache->buf, info);
1611 info->key = key;
1612 info->val = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01001613 info->dirty = 0;
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001614 idx = key % (u16)ARRAY_SIZE(cache->hash);
1615 info->next = cache->hash[idx];
1616 cache->hash[idx] = cur;
1617 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 return info;
1619}
1620
Takashi Iwai01751f52007-08-10 16:59:39 +02001621/* query and allocate an amp hash entry */
1622static inline struct hda_amp_info *
1623get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1624{
1625 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1626}
1627
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001628/* overwrite the value with the key in the caps hash */
1629static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val)
1630{
1631 struct hda_amp_info *info;
1632
1633 mutex_lock(&codec->hash_mutex);
1634 info = get_alloc_amp_hash(codec, key);
1635 if (!info) {
1636 mutex_unlock(&codec->hash_mutex);
1637 return -EINVAL;
1638 }
1639 info->amp_caps = val;
1640 info->head.val |= INFO_AMP_CAPS;
1641 mutex_unlock(&codec->hash_mutex);
1642 return 0;
1643}
1644
1645/* query the value from the caps hash; if not found, fetch the current
1646 * value from the given function and store in the hash
1647 */
1648static unsigned int
1649query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key,
1650 unsigned int (*func)(struct hda_codec *, hda_nid_t, int))
1651{
1652 struct hda_amp_info *info;
1653 unsigned int val;
1654
1655 mutex_lock(&codec->hash_mutex);
1656 info = get_alloc_amp_hash(codec, key);
1657 if (!info) {
1658 mutex_unlock(&codec->hash_mutex);
1659 return 0;
1660 }
1661 if (!(info->head.val & INFO_AMP_CAPS)) {
1662 mutex_unlock(&codec->hash_mutex); /* for reentrance */
1663 val = func(codec, nid, dir);
1664 write_caps_hash(codec, key, val);
1665 } else {
1666 val = info->amp_caps;
1667 mutex_unlock(&codec->hash_mutex);
1668 }
1669 return val;
1670}
1671
1672static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid,
1673 int direction)
1674{
1675 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1676 nid = codec->afg;
1677 return snd_hda_param_read(codec, nid,
1678 direction == HDA_OUTPUT ?
1679 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
1680}
1681
Takashi Iwaid5191e52009-11-16 14:58:17 +01001682/**
1683 * query_amp_caps - query AMP capabilities
1684 * @codec: the HD-auio codec
1685 * @nid: the NID to query
1686 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1687 *
1688 * Query AMP capabilities for the given widget and direction.
1689 * Returns the obtained capability bits.
1690 *
1691 * When cap bits have been already read, this doesn't read again but
1692 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001694u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001696 return query_caps_hash(codec, nid, direction,
1697 HDA_HASH_KEY(nid, direction, 0),
1698 read_amp_cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001700EXPORT_SYMBOL_HDA(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
Takashi Iwaid5191e52009-11-16 14:58:17 +01001702/**
1703 * snd_hda_override_amp_caps - Override the AMP capabilities
1704 * @codec: the CODEC to clean up
1705 * @nid: the NID to clean up
1706 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1707 * @caps: the capability bits to set
1708 *
1709 * Override the cached AMP caps bits value by the given one.
1710 * This function is useful if the driver needs to adjust the AMP ranges,
1711 * e.g. limit to 0dB, etc.
1712 *
1713 * Returns zero if successful or a negative error code.
1714 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001715int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1716 unsigned int caps)
1717{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001718 return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001719}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001720EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001721
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001722static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid,
1723 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001724{
1725 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1726}
1727
Takashi Iwaid5191e52009-11-16 14:58:17 +01001728/**
1729 * snd_hda_query_pin_caps - Query PIN capabilities
1730 * @codec: the HD-auio codec
1731 * @nid: the NID to query
1732 *
1733 * Query PIN capabilities for the given widget.
1734 * Returns the obtained capability bits.
1735 *
1736 * When cap bits have been already read, this doesn't read again but
1737 * returns the cached value.
1738 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001739u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1740{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001741 return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001742 read_pin_cap);
1743}
Takashi Iwai1327a322009-03-23 13:07:47 +01001744EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1745
Wu Fengguang864f92b2009-11-18 12:38:02 +08001746/**
Takashi Iwaif57c2562011-08-15 12:49:07 +02001747 * snd_hda_override_pin_caps - Override the pin capabilities
1748 * @codec: the CODEC
1749 * @nid: the NID to override
1750 * @caps: the capability bits to set
1751 *
1752 * Override the cached PIN capabilitiy bits value by the given one.
1753 *
1754 * Returns zero if successful or a negative error code.
1755 */
1756int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
1757 unsigned int caps)
1758{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001759 return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps);
Takashi Iwaif57c2562011-08-15 12:49:07 +02001760}
1761EXPORT_SYMBOL_HDA(snd_hda_override_pin_caps);
1762
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001763/* read or sync the hash value with the current value;
1764 * call within hash_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 */
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001766static struct hda_amp_info *
1767update_amp_hash(struct hda_codec *codec, hda_nid_t nid, int ch,
Takashi Iwai280e57d2012-12-14 10:32:21 +01001768 int direction, int index, bool init_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001770 struct hda_amp_info *info;
1771 unsigned int parm, val = 0;
1772 bool val_read = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001774 retry:
1775 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1776 if (!info)
1777 return NULL;
1778 if (!(info->head.val & INFO_AMP_VOL(ch))) {
1779 if (!val_read) {
1780 mutex_unlock(&codec->hash_mutex);
1781 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1782 parm |= direction == HDA_OUTPUT ?
1783 AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1784 parm |= index;
1785 val = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001786 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001787 val &= 0xff;
1788 val_read = true;
1789 mutex_lock(&codec->hash_mutex);
1790 goto retry;
1791 }
1792 info->vol[ch] = val;
1793 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001794 } else if (init_only)
1795 return NULL;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001796 return info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797}
1798
1799/*
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001800 * write the current volume in info to the h/w
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001802static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001803 hda_nid_t nid, int ch, int direction, int index,
1804 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805{
1806 u32 parm;
1807
1808 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1809 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1810 parm |= index << AC_AMP_SET_INDEX_SHIFT;
Takashi Iwai38681372012-02-27 15:00:58 +01001811 if ((val & HDA_AMP_MUTE) && !(info->amp_caps & AC_AMPCAP_MUTE) &&
1812 (info->amp_caps & AC_AMPCAP_MIN_MUTE))
1813 ; /* set the zero value as a fake mute */
1814 else
1815 parm |= val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
1817}
1818
Takashi Iwaid5191e52009-11-16 14:58:17 +01001819/**
1820 * snd_hda_codec_amp_read - Read AMP value
1821 * @codec: HD-audio codec
1822 * @nid: NID to read the AMP value
1823 * @ch: channel (left=0 or right=1)
1824 * @direction: #HDA_INPUT or #HDA_OUTPUT
1825 * @index: the index value (only for input direction)
1826 *
1827 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 */
Takashi Iwai834be882006-03-01 14:16:17 +01001829int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1830 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001832 struct hda_amp_info *info;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001833 unsigned int val = 0;
1834
1835 mutex_lock(&codec->hash_mutex);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001836 info = update_amp_hash(codec, nid, ch, direction, index, false);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001837 if (info)
1838 val = info->vol[ch];
1839 mutex_unlock(&codec->hash_mutex);
1840 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001842EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843
Takashi Iwai280e57d2012-12-14 10:32:21 +01001844static int codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1845 int direction, int idx, int mask, int val,
1846 bool init_only)
1847{
1848 struct hda_amp_info *info;
1849
1850 if (snd_BUG_ON(mask & ~0xff))
1851 mask &= 0xff;
1852 val &= mask;
1853
1854 mutex_lock(&codec->hash_mutex);
1855 info = update_amp_hash(codec, nid, ch, direction, idx, init_only);
1856 if (!info) {
1857 mutex_unlock(&codec->hash_mutex);
1858 return 0;
1859 }
1860 val |= info->vol[ch] & ~mask;
1861 if (info->vol[ch] == val) {
1862 mutex_unlock(&codec->hash_mutex);
1863 return 0;
1864 }
1865 info->vol[ch] = val;
1866 if (codec->cached_write)
1867 info->head.dirty = 1;
1868 mutex_unlock(&codec->hash_mutex);
1869 if (!codec->cached_write)
1870 put_vol_mute(codec, info, nid, ch, direction, idx, val);
1871 return 1;
1872}
1873
Takashi Iwaid5191e52009-11-16 14:58:17 +01001874/**
1875 * snd_hda_codec_amp_update - update the AMP value
1876 * @codec: HD-audio codec
1877 * @nid: NID to read the AMP value
1878 * @ch: channel (left=0 or right=1)
1879 * @direction: #HDA_INPUT or #HDA_OUTPUT
1880 * @idx: the index value (only for input direction)
1881 * @mask: bit mask to set
1882 * @val: the bits value to set
1883 *
1884 * Update the AMP value with a bit mask.
1885 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001886 */
Takashi Iwai834be882006-03-01 14:16:17 +01001887int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1888 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889{
Takashi Iwai280e57d2012-12-14 10:32:21 +01001890 return codec_amp_update(codec, nid, ch, direction, idx, mask, val, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001892EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893
Takashi Iwaid5191e52009-11-16 14:58:17 +01001894/**
1895 * snd_hda_codec_amp_stereo - update the AMP stereo values
1896 * @codec: HD-audio codec
1897 * @nid: NID to read the AMP value
1898 * @direction: #HDA_INPUT or #HDA_OUTPUT
1899 * @idx: the index value (only for input direction)
1900 * @mask: bit mask to set
1901 * @val: the bits value to set
1902 *
1903 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1904 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02001905 */
1906int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1907 int direction, int idx, int mask, int val)
1908{
1909 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001910
1911 if (snd_BUG_ON(mask & ~0xff))
1912 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02001913 for (ch = 0; ch < 2; ch++)
1914 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1915 idx, mask, val);
1916 return ret;
1917}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001918EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001919
Takashi Iwai280e57d2012-12-14 10:32:21 +01001920/* Works like snd_hda_codec_amp_update() but it writes the value only at
1921 * the first access. If the amp was already initialized / updated beforehand,
1922 * this does nothing.
1923 */
1924int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
1925 int dir, int idx, int mask, int val)
1926{
1927 return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true);
1928}
1929EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init);
1930
1931int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
1932 int dir, int idx, int mask, int val)
1933{
1934 int ch, ret = 0;
1935
1936 if (snd_BUG_ON(mask & ~0xff))
1937 mask &= 0xff;
1938 for (ch = 0; ch < 2; ch++)
1939 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
1940 idx, mask, val);
1941 return ret;
1942}
1943EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init_stereo);
1944
Takashi Iwaid5191e52009-11-16 14:58:17 +01001945/**
1946 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
1947 * @codec: HD-audio codec
1948 *
1949 * Resume the all amp commands from the cache.
1950 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001951void snd_hda_codec_resume_amp(struct hda_codec *codec)
1952{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001953 int i;
1954
Takashi Iwaic370dd62012-12-13 18:30:04 +01001955 mutex_lock(&codec->hash_mutex);
1956 for (i = 0; i < codec->amp_cache.buf.used; i++) {
1957 struct hda_amp_info *buffer;
1958 u32 key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001959 hda_nid_t nid;
1960 unsigned int idx, dir, ch;
Takashi Iwaic370dd62012-12-13 18:30:04 +01001961
1962 buffer = snd_array_elem(&codec->amp_cache.buf, i);
1963 key = buffer->head.key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001964 if (!key)
1965 continue;
1966 nid = key & 0xff;
1967 idx = (key >> 16) & 0xff;
1968 dir = (key >> 24) & 0xff;
1969 for (ch = 0; ch < 2; ch++) {
1970 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
1971 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01001972 if (!buffer->head.dirty)
1973 continue;
1974 buffer->head.dirty = 0;
1975 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001976 put_vol_mute(codec, buffer, nid, ch, dir, idx,
1977 buffer->vol[ch]);
Takashi Iwaic370dd62012-12-13 18:30:04 +01001978 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001979 }
1980 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01001981 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001982}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001983EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001985static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1986 unsigned int ofs)
1987{
1988 u32 caps = query_amp_caps(codec, nid, dir);
1989 /* get num steps */
1990 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1991 if (ofs < caps)
1992 caps -= ofs;
1993 return caps;
1994}
1995
Takashi Iwaid5191e52009-11-16 14:58:17 +01001996/**
1997 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
1998 *
1999 * The control element is supposed to have the private_value field
2000 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2001 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002002int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
2003 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004{
2005 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2006 u16 nid = get_amp_nid(kcontrol);
2007 u8 chs = get_amp_channels(kcontrol);
2008 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002009 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002011 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2012 uinfo->count = chs == 3 ? 2 : 1;
2013 uinfo->value.integer.min = 0;
2014 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
2015 if (!uinfo->value.integer.max) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002016 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01002017 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
2018 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 return -EINVAL;
2020 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 return 0;
2022}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002023EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002025
2026static inline unsigned int
2027read_amp_value(struct hda_codec *codec, hda_nid_t nid,
2028 int ch, int dir, int idx, unsigned int ofs)
2029{
2030 unsigned int val;
2031 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
2032 val &= HDA_AMP_VOLMASK;
2033 if (val >= ofs)
2034 val -= ofs;
2035 else
2036 val = 0;
2037 return val;
2038}
2039
2040static inline int
2041update_amp_value(struct hda_codec *codec, hda_nid_t nid,
2042 int ch, int dir, int idx, unsigned int ofs,
2043 unsigned int val)
2044{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002045 unsigned int maxval;
2046
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002047 if (val > 0)
2048 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02002049 /* ofs = 0: raw max value */
2050 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002051 if (val > maxval)
2052 val = maxval;
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002053 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
2054 HDA_AMP_VOLMASK, val);
2055}
2056
Takashi Iwaid5191e52009-11-16 14:58:17 +01002057/**
2058 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
2059 *
2060 * The control element is supposed to have the private_value field
2061 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2062 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002063int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
2064 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065{
2066 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2067 hda_nid_t nid = get_amp_nid(kcontrol);
2068 int chs = get_amp_channels(kcontrol);
2069 int dir = get_amp_direction(kcontrol);
2070 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002071 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 long *valp = ucontrol->value.integer.value;
2073
2074 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002075 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002077 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 return 0;
2079}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002080EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
Takashi Iwaid5191e52009-11-16 14:58:17 +01002082/**
2083 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
2084 *
2085 * The control element is supposed to have the private_value field
2086 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2087 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002088int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
2089 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090{
2091 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2092 hda_nid_t nid = get_amp_nid(kcontrol);
2093 int chs = get_amp_channels(kcontrol);
2094 int dir = get_amp_direction(kcontrol);
2095 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002096 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 long *valp = ucontrol->value.integer.value;
2098 int change = 0;
2099
Takashi Iwaicb53c622007-08-10 17:21:45 +02002100 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002101 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002102 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002103 valp++;
2104 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002105 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002106 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002107 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 return change;
2109}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002110EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111
Takashi Iwaid5191e52009-11-16 14:58:17 +01002112/**
2113 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2114 *
2115 * The control element is supposed to have the private_value field
2116 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2117 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002118int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2119 unsigned int size, unsigned int __user *_tlv)
2120{
2121 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2122 hda_nid_t nid = get_amp_nid(kcontrol);
2123 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002124 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02002125 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002126 u32 caps, val1, val2;
2127
2128 if (size < 4 * sizeof(unsigned int))
2129 return -ENOMEM;
2130 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002131 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2132 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002133 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002134 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002135 val1 = ((int)val1) * ((int)val2);
Takashi Iwai38681372012-02-27 15:00:58 +01002136 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwaic08d9162010-10-17 10:40:53 +02002137 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002138 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2139 return -EFAULT;
2140 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2141 return -EFAULT;
2142 if (put_user(val1, _tlv + 2))
2143 return -EFAULT;
2144 if (put_user(val2, _tlv + 3))
2145 return -EFAULT;
2146 return 0;
2147}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002148EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002149
Takashi Iwaid5191e52009-11-16 14:58:17 +01002150/**
2151 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2152 * @codec: HD-audio codec
2153 * @nid: NID of a reference widget
2154 * @dir: #HDA_INPUT or #HDA_OUTPUT
2155 * @tlv: TLV data to be stored, at least 4 elements
2156 *
2157 * Set (static) TLV data for a virtual master volume using the AMP caps
2158 * obtained from the reference NID.
2159 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01002160 */
2161void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2162 unsigned int *tlv)
2163{
2164 u32 caps;
2165 int nums, step;
2166
2167 caps = query_amp_caps(codec, nid, dir);
2168 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2169 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2170 step = (step + 1) * 25;
2171 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2172 tlv[1] = 2 * sizeof(unsigned int);
2173 tlv[2] = -nums * step;
2174 tlv[3] = step;
2175}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002176EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002177
2178/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01002179static struct snd_kcontrol *
Takashi Iwaidcda5802012-10-12 17:24:51 +02002180find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002181{
2182 struct snd_ctl_elem_id id;
2183 memset(&id, 0, sizeof(id));
2184 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwaidcda5802012-10-12 17:24:51 +02002185 id.device = dev;
Takashi Iwai09f99702008-02-04 12:31:13 +01002186 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02002187 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2188 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002189 strcpy(id.name, name);
2190 return snd_ctl_find_id(codec->bus->card, &id);
2191}
2192
Takashi Iwaid5191e52009-11-16 14:58:17 +01002193/**
2194 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2195 * @codec: HD-audio codec
2196 * @name: ctl id name string
2197 *
2198 * Get the control element with the given id string and IFACE_MIXER.
2199 */
Takashi Iwai09f99702008-02-04 12:31:13 +01002200struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2201 const char *name)
2202{
Takashi Iwaidcda5802012-10-12 17:24:51 +02002203 return find_mixer_ctl(codec, name, 0, 0);
Takashi Iwai09f99702008-02-04 12:31:13 +01002204}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002205EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01002206
Takashi Iwaidcda5802012-10-12 17:24:51 +02002207static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
2208 int dev)
Takashi Iwai1afe2062010-12-23 10:17:52 +01002209{
2210 int idx;
2211 for (idx = 0; idx < 16; idx++) { /* 16 ctlrs should be large enough */
Takashi Iwaidcda5802012-10-12 17:24:51 +02002212 if (!find_mixer_ctl(codec, name, dev, idx))
Takashi Iwai1afe2062010-12-23 10:17:52 +01002213 return idx;
2214 }
2215 return -EBUSY;
2216}
2217
Takashi Iwaid5191e52009-11-16 14:58:17 +01002218/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002219 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01002220 * @codec: HD-audio codec
2221 * @nid: corresponding NID (optional)
2222 * @kctl: the control element to assign
2223 *
2224 * Add the given control element to an array inside the codec instance.
2225 * All control elements belonging to a codec are supposed to be added
2226 * by this function so that a proper clean-up works at the free or
2227 * reconfiguration time.
2228 *
2229 * If non-zero @nid is passed, the NID is assigned to the control element.
2230 * The assignment is shown in the codec proc file.
2231 *
2232 * snd_hda_ctl_add() checks the control subdev id field whether
2233 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002234 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2235 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01002236 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002237int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2238 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002239{
2240 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002241 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002242 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002243
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002244 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002245 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002246 if (nid == 0)
2247 nid = get_amp_nid_(kctl->private_value);
2248 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002249 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2250 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002251 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01002252 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002253 err = snd_ctl_add(codec->bus->card, kctl);
2254 if (err < 0)
2255 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002256 item = snd_array_new(&codec->mixers);
2257 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002258 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002259 item->kctl = kctl;
2260 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002261 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002262 return 0;
2263}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002264EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002265
Takashi Iwaid5191e52009-11-16 14:58:17 +01002266/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002267 * snd_hda_add_nid - Assign a NID to a control element
2268 * @codec: HD-audio codec
2269 * @nid: corresponding NID (optional)
2270 * @kctl: the control element to assign
2271 * @index: index to kctl
2272 *
2273 * Add the given control element to an array inside the codec instance.
2274 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2275 * NID:KCTL mapping - for example "Capture Source" selector.
2276 */
2277int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2278 unsigned int index, hda_nid_t nid)
2279{
2280 struct hda_nid_item *item;
2281
2282 if (nid > 0) {
2283 item = snd_array_new(&codec->nids);
2284 if (!item)
2285 return -ENOMEM;
2286 item->kctl = kctl;
2287 item->index = index;
2288 item->nid = nid;
2289 return 0;
2290 }
Takashi Iwai28d1a852010-03-15 09:05:46 +01002291 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
2292 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002293 return -EINVAL;
2294}
2295EXPORT_SYMBOL_HDA(snd_hda_add_nid);
2296
2297/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002298 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2299 * @codec: HD-audio codec
2300 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002301void snd_hda_ctls_clear(struct hda_codec *codec)
2302{
2303 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002304 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002305 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002306 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002307 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002308 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002309}
2310
Takashi Iwaia65d6292009-02-23 16:57:04 +01002311/* pseudo device locking
2312 * toggle card->shutdown to allow/disallow the device access (as a hack)
2313 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002314int snd_hda_lock_devices(struct hda_bus *bus)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002315{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002316 struct snd_card *card = bus->card;
2317 struct hda_codec *codec;
2318
Takashi Iwaia65d6292009-02-23 16:57:04 +01002319 spin_lock(&card->files_lock);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002320 if (card->shutdown)
2321 goto err_unlock;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002322 card->shutdown = 1;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002323 if (!list_empty(&card->ctl_files))
2324 goto err_clear;
2325
2326 list_for_each_entry(codec, &bus->codec_list, list) {
2327 int pcm;
2328 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2329 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2330 if (!cpcm->pcm)
2331 continue;
2332 if (cpcm->pcm->streams[0].substream_opened ||
2333 cpcm->pcm->streams[1].substream_opened)
2334 goto err_clear;
2335 }
2336 }
Takashi Iwaia65d6292009-02-23 16:57:04 +01002337 spin_unlock(&card->files_lock);
2338 return 0;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002339
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002340 err_clear:
2341 card->shutdown = 0;
2342 err_unlock:
2343 spin_unlock(&card->files_lock);
2344 return -EINVAL;
2345}
2346EXPORT_SYMBOL_HDA(snd_hda_lock_devices);
2347
2348void snd_hda_unlock_devices(struct hda_bus *bus)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002349{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002350 struct snd_card *card = bus->card;
2351
2352 card = bus->card;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002353 spin_lock(&card->files_lock);
2354 card->shutdown = 0;
2355 spin_unlock(&card->files_lock);
2356}
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002357EXPORT_SYMBOL_HDA(snd_hda_unlock_devices);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002358
Takashi Iwaid5191e52009-11-16 14:58:17 +01002359/**
2360 * snd_hda_codec_reset - Clear all objects assigned to the codec
2361 * @codec: HD-audio codec
2362 *
2363 * This frees the all PCM and control elements assigned to the codec, and
2364 * clears the caches and restores the pin default configurations.
2365 *
2366 * When a device is being used, it returns -EBSY. If successfully freed,
2367 * returns zero.
2368 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002369int snd_hda_codec_reset(struct hda_codec *codec)
2370{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002371 struct hda_bus *bus = codec->bus;
2372 struct snd_card *card = bus->card;
2373 int i;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002374
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002375 if (snd_hda_lock_devices(bus) < 0)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002376 return -EBUSY;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002377
2378 /* OK, let it free */
David Henningsson26a6cb62012-10-09 15:04:21 +02002379 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai83012a72012-08-24 18:38:08 +02002380#ifdef CONFIG_PM
Takashi Iwaia2d96e72012-05-09 12:36:22 +02002381 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwai339876d2012-05-08 16:57:12 +02002382 codec->power_on = 0;
2383 codec->power_transition = 0;
2384 codec->power_jiffies = jiffies;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002385 flush_workqueue(bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002386#endif
2387 snd_hda_ctls_clear(codec);
2388 /* relase PCMs */
2389 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002390 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01002391 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002392 clear_bit(codec->pcm_info[i].device,
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002393 bus->pcm_dev_bits);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002394 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002395 }
2396 if (codec->patch_ops.free)
2397 codec->patch_ops.free(codec);
Takashi Iwai07dc59f2012-09-10 09:39:31 +02002398 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
Takashi Iwai1835a0f2011-10-27 22:12:46 +02002399 snd_hda_jack_tbl_clear(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01002400 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002401 codec->spec = NULL;
2402 free_hda_cache(&codec->amp_cache);
2403 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01002404 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2405 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01002406 /* free only driver_pins so that init_pins + user_pins are restored */
2407 snd_array_free(&codec->driver_pins);
Takashi Iwai09a60712012-06-26 15:01:33 +02002408 snd_array_free(&codec->cvt_setups);
2409 snd_array_free(&codec->spdif_out);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002410 codec->num_pcms = 0;
2411 codec->pcm_info = NULL;
2412 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01002413 codec->slave_dig_outs = NULL;
2414 codec->spdif_status_reset = 0;
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002415 module_put(codec->owner);
2416 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002417
2418 /* allow device access again */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002419 snd_hda_unlock_devices(bus);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002420 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002421}
2422
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002423typedef int (*map_slave_func_t)(void *, struct snd_kcontrol *);
2424
2425/* apply the function to all matching slave ctls in the mixer list */
2426static int map_slaves(struct hda_codec *codec, const char * const *slaves,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002427 const char *suffix, map_slave_func_t func, void *data)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002428{
2429 struct hda_nid_item *items;
2430 const char * const *s;
2431 int i, err;
2432
2433 items = codec->mixers.list;
2434 for (i = 0; i < codec->mixers.used; i++) {
2435 struct snd_kcontrol *sctl = items[i].kctl;
2436 if (!sctl || !sctl->id.name ||
2437 sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
2438 continue;
2439 for (s = slaves; *s; s++) {
Takashi Iwai9322ca52012-02-03 14:28:01 +01002440 char tmpname[sizeof(sctl->id.name)];
2441 const char *name = *s;
2442 if (suffix) {
2443 snprintf(tmpname, sizeof(tmpname), "%s %s",
2444 name, suffix);
2445 name = tmpname;
2446 }
Takashi Iwai9322ca52012-02-03 14:28:01 +01002447 if (!strcmp(sctl->id.name, name)) {
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002448 err = func(data, sctl);
2449 if (err)
2450 return err;
2451 break;
2452 }
2453 }
2454 }
2455 return 0;
2456}
2457
2458static int check_slave_present(void *data, struct snd_kcontrol *sctl)
2459{
2460 return 1;
2461}
2462
Takashi Iwai18478e82012-03-09 17:51:10 +01002463/* guess the value corresponding to 0dB */
2464static int get_kctl_0dB_offset(struct snd_kcontrol *kctl)
2465{
2466 int _tlv[4];
2467 const int *tlv = NULL;
2468 int val = -1;
2469
2470 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2471 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2472 mm_segment_t fs = get_fs();
2473 set_fs(get_ds());
2474 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2475 tlv = _tlv;
2476 set_fs(fs);
2477 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2478 tlv = kctl->tlv.p;
2479 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE)
2480 val = -tlv[2] / tlv[3];
2481 return val;
2482}
2483
2484/* call kctl->put with the given value(s) */
2485static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2486{
2487 struct snd_ctl_elem_value *ucontrol;
2488 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2489 if (!ucontrol)
2490 return -ENOMEM;
2491 ucontrol->value.integer.value[0] = val;
2492 ucontrol->value.integer.value[1] = val;
2493 kctl->put(kctl, ucontrol);
2494 kfree(ucontrol);
2495 return 0;
2496}
2497
2498/* initialize the slave volume with 0dB */
2499static int init_slave_0dB(void *data, struct snd_kcontrol *slave)
2500{
2501 int offset = get_kctl_0dB_offset(slave);
2502 if (offset > 0)
2503 put_kctl_with_value(slave, offset);
2504 return 0;
2505}
2506
2507/* unmute the slave */
2508static int init_slave_unmute(void *data, struct snd_kcontrol *slave)
2509{
2510 return put_kctl_with_value(slave, 1);
2511}
2512
Takashi Iwaid5191e52009-11-16 14:58:17 +01002513/**
2514 * snd_hda_add_vmaster - create a virtual master control and add slaves
2515 * @codec: HD-audio codec
2516 * @name: vmaster control name
2517 * @tlv: TLV data (optional)
2518 * @slaves: slave control names (optional)
Takashi Iwai9322ca52012-02-03 14:28:01 +01002519 * @suffix: suffix string to each slave name (optional)
Takashi Iwai18478e82012-03-09 17:51:10 +01002520 * @init_slave_vol: initialize slaves to unmute/0dB
Takashi Iwai29e58532012-03-12 12:25:03 +01002521 * @ctl_ret: store the vmaster kcontrol in return
Takashi Iwaid5191e52009-11-16 14:58:17 +01002522 *
2523 * Create a virtual master control with the given name. The TLV data
2524 * must be either NULL or a valid data.
2525 *
2526 * @slaves is a NULL-terminated array of strings, each of which is a
2527 * slave control name. All controls with these names are assigned to
2528 * the new virtual master control.
2529 *
2530 * This function returns zero if successful or a negative error code.
2531 */
Takashi Iwai18478e82012-03-09 17:51:10 +01002532int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002533 unsigned int *tlv, const char * const *slaves,
Takashi Iwai29e58532012-03-12 12:25:03 +01002534 const char *suffix, bool init_slave_vol,
2535 struct snd_kcontrol **ctl_ret)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002536{
2537 struct snd_kcontrol *kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002538 int err;
2539
Takashi Iwai29e58532012-03-12 12:25:03 +01002540 if (ctl_ret)
2541 *ctl_ret = NULL;
2542
Takashi Iwai9322ca52012-02-03 14:28:01 +01002543 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002544 if (err != 1) {
Takashi Iwai2f085542008-02-22 18:43:50 +01002545 snd_printdd("No slave found for %s\n", name);
2546 return 0;
2547 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002548 kctl = snd_ctl_make_virtual_master(name, tlv);
2549 if (!kctl)
2550 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002551 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002552 if (err < 0)
2553 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002554
Takashi Iwai9322ca52012-02-03 14:28:01 +01002555 err = map_slaves(codec, slaves, suffix,
2556 (map_slave_func_t)snd_ctl_add_slave, kctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002557 if (err < 0)
2558 return err;
Takashi Iwai18478e82012-03-09 17:51:10 +01002559
2560 /* init with master mute & zero volume */
2561 put_kctl_with_value(kctl, 0);
2562 if (init_slave_vol)
2563 map_slaves(codec, slaves, suffix,
2564 tlv ? init_slave_0dB : init_slave_unmute, kctl);
2565
Takashi Iwai29e58532012-03-12 12:25:03 +01002566 if (ctl_ret)
2567 *ctl_ret = kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002568 return 0;
2569}
Takashi Iwai18478e82012-03-09 17:51:10 +01002570EXPORT_SYMBOL_HDA(__snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002571
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002572/*
2573 * mute-LED control using vmaster
2574 */
2575static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2576 struct snd_ctl_elem_info *uinfo)
2577{
2578 static const char * const texts[] = {
David Henningssonc86c2d42013-01-03 14:12:29 +01002579 "On", "Off", "Follow Master"
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002580 };
2581 unsigned int index;
2582
2583 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2584 uinfo->count = 1;
2585 uinfo->value.enumerated.items = 3;
2586 index = uinfo->value.enumerated.item;
2587 if (index >= 3)
2588 index = 2;
2589 strcpy(uinfo->value.enumerated.name, texts[index]);
2590 return 0;
2591}
2592
2593static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2594 struct snd_ctl_elem_value *ucontrol)
2595{
2596 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2597 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2598 return 0;
2599}
2600
2601static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2602 struct snd_ctl_elem_value *ucontrol)
2603{
2604 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2605 unsigned int old_mode = hook->mute_mode;
2606
2607 hook->mute_mode = ucontrol->value.enumerated.item[0];
2608 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2609 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2610 if (old_mode == hook->mute_mode)
2611 return 0;
2612 snd_hda_sync_vmaster_hook(hook);
2613 return 1;
2614}
2615
2616static struct snd_kcontrol_new vmaster_mute_mode = {
2617 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2618 .name = "Mute-LED Mode",
2619 .info = vmaster_mute_mode_info,
2620 .get = vmaster_mute_mode_get,
2621 .put = vmaster_mute_mode_put,
2622};
2623
2624/*
2625 * Add a mute-LED hook with the given vmaster switch kctl
2626 * "Mute-LED Mode" control is automatically created and associated with
2627 * the given hook.
2628 */
2629int snd_hda_add_vmaster_hook(struct hda_codec *codec,
Takashi Iwaif29735c2012-03-13 07:55:10 +01002630 struct hda_vmaster_mute_hook *hook,
2631 bool expose_enum_ctl)
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002632{
2633 struct snd_kcontrol *kctl;
2634
2635 if (!hook->hook || !hook->sw_kctl)
2636 return 0;
2637 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
2638 hook->codec = codec;
2639 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
Takashi Iwaif29735c2012-03-13 07:55:10 +01002640 if (!expose_enum_ctl)
2641 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002642 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2643 if (!kctl)
2644 return -ENOMEM;
2645 return snd_hda_ctl_add(codec, 0, kctl);
2646}
2647EXPORT_SYMBOL_HDA(snd_hda_add_vmaster_hook);
2648
2649/*
2650 * Call the hook with the current value for synchronization
2651 * Should be called in init callback
2652 */
2653void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2654{
2655 if (!hook->hook || !hook->codec)
2656 return;
2657 switch (hook->mute_mode) {
2658 case HDA_VMUTE_FOLLOW_MASTER:
2659 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2660 break;
2661 default:
2662 hook->hook(hook->codec, hook->mute_mode);
2663 break;
2664 }
2665}
2666EXPORT_SYMBOL_HDA(snd_hda_sync_vmaster_hook);
2667
2668
Takashi Iwaid5191e52009-11-16 14:58:17 +01002669/**
2670 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2671 *
2672 * The control element is supposed to have the private_value field
2673 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2674 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002675int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2676 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677{
2678 int chs = get_amp_channels(kcontrol);
2679
2680 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2681 uinfo->count = chs == 3 ? 2 : 1;
2682 uinfo->value.integer.min = 0;
2683 uinfo->value.integer.max = 1;
2684 return 0;
2685}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002686EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687
Takashi Iwaid5191e52009-11-16 14:58:17 +01002688/**
2689 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2690 *
2691 * The control element is supposed to have the private_value field
2692 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2693 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002694int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2695 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696{
2697 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2698 hda_nid_t nid = get_amp_nid(kcontrol);
2699 int chs = get_amp_channels(kcontrol);
2700 int dir = get_amp_direction(kcontrol);
2701 int idx = get_amp_index(kcontrol);
2702 long *valp = ucontrol->value.integer.value;
2703
2704 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002705 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002706 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002708 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002709 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 return 0;
2711}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002712EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713
Takashi Iwaid5191e52009-11-16 14:58:17 +01002714/**
2715 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2716 *
2717 * The control element is supposed to have the private_value field
2718 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2719 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002720int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2721 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722{
2723 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2724 hda_nid_t nid = get_amp_nid(kcontrol);
2725 int chs = get_amp_channels(kcontrol);
2726 int dir = get_amp_direction(kcontrol);
2727 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 long *valp = ucontrol->value.integer.value;
2729 int change = 0;
2730
Takashi Iwaicb53c622007-08-10 17:21:45 +02002731 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002732 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002733 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002734 HDA_AMP_MUTE,
2735 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002736 valp++;
2737 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002738 if (chs & 2)
2739 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002740 HDA_AMP_MUTE,
2741 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002742 hda_call_check_power_status(codec, nid);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002743 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 return change;
2745}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002746EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747
2748/*
Takashi Iwai985be542005-11-02 18:26:49 +01002749 * bound volume controls
2750 *
2751 * bind multiple volumes (# indices, from 0)
2752 */
2753
2754#define AMP_VAL_IDX_SHIFT 19
2755#define AMP_VAL_IDX_MASK (0x0f<<19)
2756
Takashi Iwaid5191e52009-11-16 14:58:17 +01002757/**
2758 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2759 *
2760 * The control element is supposed to have the private_value field
2761 * set up via HDA_BIND_MUTE*() macros.
2762 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002763int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2764 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002765{
2766 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2767 unsigned long pval;
2768 int err;
2769
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002770 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002771 pval = kcontrol->private_value;
2772 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2773 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2774 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002775 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002776 return err;
2777}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002778EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002779
Takashi Iwaid5191e52009-11-16 14:58:17 +01002780/**
2781 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2782 *
2783 * The control element is supposed to have the private_value field
2784 * set up via HDA_BIND_MUTE*() macros.
2785 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002786int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2787 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002788{
2789 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2790 unsigned long pval;
2791 int i, indices, err = 0, change = 0;
2792
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002793 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002794 pval = kcontrol->private_value;
2795 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2796 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002797 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2798 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002799 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2800 if (err < 0)
2801 break;
2802 change |= err;
2803 }
2804 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002805 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002806 return err < 0 ? err : change;
2807}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002808EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002809
Takashi Iwaid5191e52009-11-16 14:58:17 +01002810/**
2811 * snd_hda_mixer_bind_ctls_info - Info 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.
Takashi Iwai532d5382007-07-27 19:02:40 +02002815 */
2816int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2817 struct snd_ctl_elem_info *uinfo)
2818{
2819 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2820 struct hda_bind_ctls *c;
2821 int err;
2822
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002823 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002824 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002825 kcontrol->private_value = *c->values;
2826 err = c->ops->info(kcontrol, uinfo);
2827 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002828 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002829 return err;
2830}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002831EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002832
Takashi Iwaid5191e52009-11-16 14:58:17 +01002833/**
2834 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2835 *
2836 * The control element is supposed to have the private_value field
2837 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2838 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002839int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2840 struct snd_ctl_elem_value *ucontrol)
2841{
2842 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2843 struct hda_bind_ctls *c;
2844 int err;
2845
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002846 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002847 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002848 kcontrol->private_value = *c->values;
2849 err = c->ops->get(kcontrol, ucontrol);
2850 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002851 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002852 return err;
2853}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002854EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002855
Takashi Iwaid5191e52009-11-16 14:58:17 +01002856/**
2857 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2858 *
2859 * The control element is supposed to have the private_value field
2860 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2861 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002862int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2863 struct snd_ctl_elem_value *ucontrol)
2864{
2865 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2866 struct hda_bind_ctls *c;
2867 unsigned long *vals;
2868 int err = 0, change = 0;
2869
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002870 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002871 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002872 for (vals = c->values; *vals; vals++) {
2873 kcontrol->private_value = *vals;
2874 err = c->ops->put(kcontrol, ucontrol);
2875 if (err < 0)
2876 break;
2877 change |= err;
2878 }
2879 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002880 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002881 return err < 0 ? err : change;
2882}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002883EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02002884
Takashi Iwaid5191e52009-11-16 14:58:17 +01002885/**
2886 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2887 *
2888 * The control element is supposed to have the private_value field
2889 * set up via HDA_BIND_VOL() macro.
2890 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002891int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2892 unsigned int size, unsigned int __user *tlv)
2893{
2894 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2895 struct hda_bind_ctls *c;
2896 int err;
2897
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002898 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002899 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002900 kcontrol->private_value = *c->values;
2901 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2902 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002903 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002904 return err;
2905}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002906EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02002907
2908struct hda_ctl_ops snd_hda_bind_vol = {
2909 .info = snd_hda_mixer_amp_volume_info,
2910 .get = snd_hda_mixer_amp_volume_get,
2911 .put = snd_hda_mixer_amp_volume_put,
2912 .tlv = snd_hda_mixer_amp_tlv
2913};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002914EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02002915
2916struct hda_ctl_ops snd_hda_bind_sw = {
2917 .info = snd_hda_mixer_amp_switch_info,
2918 .get = snd_hda_mixer_amp_switch_get,
2919 .put = snd_hda_mixer_amp_switch_put,
2920 .tlv = snd_hda_mixer_amp_tlv
2921};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002922EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02002923
2924/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 * SPDIF out controls
2926 */
2927
Takashi Iwai0ba21762007-04-16 11:29:14 +02002928static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2929 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930{
2931 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2932 uinfo->count = 1;
2933 return 0;
2934}
2935
Takashi Iwai0ba21762007-04-16 11:29:14 +02002936static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2937 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938{
2939 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2940 IEC958_AES0_NONAUDIO |
2941 IEC958_AES0_CON_EMPHASIS_5015 |
2942 IEC958_AES0_CON_NOT_COPYRIGHT;
2943 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2944 IEC958_AES1_CON_ORIGINAL;
2945 return 0;
2946}
2947
Takashi Iwai0ba21762007-04-16 11:29:14 +02002948static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2949 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950{
2951 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2952 IEC958_AES0_NONAUDIO |
2953 IEC958_AES0_PRO_EMPHASIS_5015;
2954 return 0;
2955}
2956
Takashi Iwai0ba21762007-04-16 11:29:14 +02002957static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2958 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959{
2960 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002961 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002962 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002964 mutex_lock(&codec->spdif_mutex);
2965 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c9359762011-06-01 11:14:17 -06002966 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
2967 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
2968 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
2969 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002970 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971
2972 return 0;
2973}
2974
2975/* convert from SPDIF status bits to HDA SPDIF bits
2976 * bit 0 (DigEn) is always set zero (to be filled later)
2977 */
2978static unsigned short convert_from_spdif_status(unsigned int sbits)
2979{
2980 unsigned short val = 0;
2981
2982 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002983 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002985 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002987 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2988 IEC958_AES0_PRO_EMPHASIS_5015)
2989 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002991 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2992 IEC958_AES0_CON_EMPHASIS_5015)
2993 val |= AC_DIG1_EMPHASIS;
2994 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2995 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02002997 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2999 }
3000 return val;
3001}
3002
3003/* convert to SPDIF status bits from HDA SPDIF bits
3004 */
3005static unsigned int convert_to_spdif_status(unsigned short val)
3006{
3007 unsigned int sbits = 0;
3008
Takashi Iwai0ba21762007-04-16 11:29:14 +02003009 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003011 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 sbits |= IEC958_AES0_PROFESSIONAL;
3013 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003014 if (sbits & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
3016 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003017 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003019 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003021 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
3023 sbits |= val & (0x7f << 8);
3024 }
3025 return sbits;
3026}
3027
Takashi Iwai2f728532008-09-25 16:32:41 +02003028/* set digital convert verbs both for the given NID and its slaves */
3029static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
3030 int verb, int val)
3031{
Takashi Iwaidda14412011-05-02 11:29:30 +02003032 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02003033
Takashi Iwai9e976972008-11-25 08:17:20 +01003034 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003035 d = codec->slave_dig_outs;
3036 if (!d)
3037 return;
3038 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01003039 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003040}
3041
3042static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
3043 int dig1, int dig2)
3044{
3045 if (dig1 != -1)
3046 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
3047 if (dig2 != -1)
3048 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
3049}
3050
Takashi Iwai0ba21762007-04-16 11:29:14 +02003051static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
3052 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053{
3054 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06003055 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003056 struct hda_spdif_out *spdif;
3057 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 unsigned short val;
3059 int change;
3060
Ingo Molnar62932df2006-01-16 16:34:20 +01003061 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003062 spdif = snd_array_elem(&codec->spdif_out, idx);
3063 nid = spdif->nid;
Stephen Warren7c9359762011-06-01 11:14:17 -06003064 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
3066 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
3067 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c9359762011-06-01 11:14:17 -06003068 val = convert_from_spdif_status(spdif->status);
3069 val |= spdif->ctls & 1;
3070 change = spdif->ctls != val;
3071 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003072 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02003073 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01003074 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 return change;
3076}
3077
Takashi Iwaia5ce8892007-07-23 15:42:26 +02003078#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079
Takashi Iwai0ba21762007-04-16 11:29:14 +02003080static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
3081 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082{
3083 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06003084 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003085 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003087 mutex_lock(&codec->spdif_mutex);
3088 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c9359762011-06-01 11:14:17 -06003089 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003090 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 return 0;
3092}
3093
Stephen Warren74b654c2011-06-01 11:14:18 -06003094static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
3095 int dig1, int dig2)
3096{
3097 set_dig_out_convert(codec, nid, dig1, dig2);
3098 /* unmute amp switch (if any) */
3099 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
3100 (dig1 & AC_DIG1_ENABLE))
3101 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
3102 HDA_AMP_MUTE, 0);
3103}
3104
Takashi Iwai0ba21762007-04-16 11:29:14 +02003105static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
3106 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107{
3108 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06003109 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003110 struct hda_spdif_out *spdif;
3111 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 unsigned short val;
3113 int change;
3114
Ingo Molnar62932df2006-01-16 16:34:20 +01003115 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003116 spdif = snd_array_elem(&codec->spdif_out, idx);
3117 nid = spdif->nid;
Stephen Warren7c9359762011-06-01 11:14:17 -06003118 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02003120 val |= AC_DIG1_ENABLE;
Stephen Warren7c9359762011-06-01 11:14:17 -06003121 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003122 spdif->ctls = val;
3123 if (change && nid != (u16)-1)
3124 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01003125 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 return change;
3127}
3128
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003129static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 {
3131 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3132 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003133 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 .info = snd_hda_spdif_mask_info,
3135 .get = snd_hda_spdif_cmask_get,
3136 },
3137 {
3138 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3139 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003140 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 .info = snd_hda_spdif_mask_info,
3142 .get = snd_hda_spdif_pmask_get,
3143 },
3144 {
3145 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003146 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 .info = snd_hda_spdif_mask_info,
3148 .get = snd_hda_spdif_default_get,
3149 .put = snd_hda_spdif_default_put,
3150 },
3151 {
3152 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003153 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 .info = snd_hda_spdif_out_switch_info,
3155 .get = snd_hda_spdif_out_switch_get,
3156 .put = snd_hda_spdif_out_switch_put,
3157 },
3158 { } /* end */
3159};
3160
3161/**
Takashi Iwaidcda5802012-10-12 17:24:51 +02003162 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 * @codec: the HDA codec
Takashi Iwaidcda5802012-10-12 17:24:51 +02003164 * @associated_nid: NID that new ctls associated with
3165 * @cvt_nid: converter NID
3166 * @type: HDA_PCM_TYPE_*
3167 * Creates controls related with the digital output.
3168 * Called from each patch supporting the digital out.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 *
3170 * Returns 0 if successful, or a negative error code.
3171 */
Takashi Iwaidcda5802012-10-12 17:24:51 +02003172int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
3173 hda_nid_t associated_nid,
3174 hda_nid_t cvt_nid,
3175 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176{
3177 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003178 struct snd_kcontrol *kctl;
3179 struct snd_kcontrol_new *dig_mix;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003180 int idx, dev = 0;
3181 const int spdif_pcm_dev = 1;
Stephen Warren7c9359762011-06-01 11:14:17 -06003182 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183
Takashi Iwaidcda5802012-10-12 17:24:51 +02003184 if (codec->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
3185 type == HDA_PCM_TYPE_SPDIF) {
3186 dev = spdif_pcm_dev;
3187 } else if (codec->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
3188 type == HDA_PCM_TYPE_HDMI) {
3189 for (idx = 0; idx < codec->spdif_out.used; idx++) {
3190 spdif = snd_array_elem(&codec->spdif_out, idx);
3191 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3192 kctl = find_mixer_ctl(codec, dig_mix->name, 0, idx);
3193 if (!kctl)
3194 break;
3195 kctl->id.device = spdif_pcm_dev;
3196 }
3197 }
3198 codec->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
3199 }
3200 if (!codec->primary_dig_out_type)
3201 codec->primary_dig_out_type = type;
3202
3203 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", dev);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003204 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003205 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
3206 return -EBUSY;
3207 }
Stephen Warren7c9359762011-06-01 11:14:17 -06003208 spdif = snd_array_new(&codec->spdif_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3210 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01003211 if (!kctl)
3212 return -ENOMEM;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003213 kctl->id.device = dev;
Takashi Iwai09f99702008-02-04 12:31:13 +01003214 kctl->id.index = idx;
Stephen Warren7c9359762011-06-01 11:14:17 -06003215 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06003216 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003217 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 return err;
3219 }
Stephen Warren74b654c2011-06-01 11:14:18 -06003220 spdif->nid = cvt_nid;
3221 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
Stephen Warren7c9359762011-06-01 11:14:17 -06003222 AC_VERB_GET_DIGI_CONVERT_1, 0);
3223 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 return 0;
3225}
Takashi Iwaidcda5802012-10-12 17:24:51 +02003226EXPORT_SYMBOL_HDA(snd_hda_create_dig_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003228/* get the hda_spdif_out entry from the given NID
3229 * call within spdif_mutex lock
3230 */
Stephen Warren7c9359762011-06-01 11:14:17 -06003231struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3232 hda_nid_t nid)
3233{
3234 int i;
3235 for (i = 0; i < codec->spdif_out.used; i++) {
3236 struct hda_spdif_out *spdif =
3237 snd_array_elem(&codec->spdif_out, i);
3238 if (spdif->nid == nid)
3239 return spdif;
3240 }
3241 return NULL;
3242}
3243EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid);
3244
Stephen Warren74b654c2011-06-01 11:14:18 -06003245void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3246{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003247 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003248
3249 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003250 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003251 spdif->nid = (u16)-1;
3252 mutex_unlock(&codec->spdif_mutex);
3253}
3254EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign);
3255
3256void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3257{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003258 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003259 unsigned short val;
3260
3261 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003262 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003263 if (spdif->nid != nid) {
3264 spdif->nid = nid;
3265 val = spdif->ctls;
3266 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3267 }
3268 mutex_unlock(&codec->spdif_mutex);
3269}
3270EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign);
3271
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272/*
Takashi Iwai9a081602008-02-12 18:37:26 +01003273 * SPDIF sharing with analog output
3274 */
3275static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3276 struct snd_ctl_elem_value *ucontrol)
3277{
3278 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3279 ucontrol->value.integer.value[0] = mout->share_spdif;
3280 return 0;
3281}
3282
3283static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3284 struct snd_ctl_elem_value *ucontrol)
3285{
3286 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3287 mout->share_spdif = !!ucontrol->value.integer.value[0];
3288 return 0;
3289}
3290
3291static struct snd_kcontrol_new spdif_share_sw = {
3292 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3293 .name = "IEC958 Default PCM Playback Switch",
3294 .info = snd_ctl_boolean_mono_info,
3295 .get = spdif_share_sw_get,
3296 .put = spdif_share_sw_put,
3297};
3298
Takashi Iwaid5191e52009-11-16 14:58:17 +01003299/**
3300 * snd_hda_create_spdif_share_sw - create Default PCM switch
3301 * @codec: the HDA codec
3302 * @mout: multi-out instance
3303 */
Takashi Iwai9a081602008-02-12 18:37:26 +01003304int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3305 struct hda_multi_out *mout)
3306{
3307 if (!mout->dig_out_nid)
3308 return 0;
3309 /* ATTENTION: here mout is passed as private_data, instead of codec */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003310 return snd_hda_ctl_add(codec, mout->dig_out_nid,
3311 snd_ctl_new1(&spdif_share_sw, mout));
Takashi Iwai9a081602008-02-12 18:37:26 +01003312}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003313EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01003314
3315/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 * SPDIF input
3317 */
3318
3319#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3320
Takashi Iwai0ba21762007-04-16 11:29:14 +02003321static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3322 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323{
3324 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3325
3326 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3327 return 0;
3328}
3329
Takashi Iwai0ba21762007-04-16 11:29:14 +02003330static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3331 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332{
3333 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3334 hda_nid_t nid = kcontrol->private_value;
3335 unsigned int val = !!ucontrol->value.integer.value[0];
3336 int change;
3337
Ingo Molnar62932df2006-01-16 16:34:20 +01003338 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003340 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003342 snd_hda_codec_write_cache(codec, nid, 0,
3343 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003345 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 return change;
3347}
3348
Takashi Iwai0ba21762007-04-16 11:29:14 +02003349static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3350 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351{
3352 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3353 hda_nid_t nid = kcontrol->private_value;
3354 unsigned short val;
3355 unsigned int sbits;
3356
Andrew Paprocki3982d172007-12-19 12:13:44 +01003357 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 sbits = convert_to_spdif_status(val);
3359 ucontrol->value.iec958.status[0] = sbits;
3360 ucontrol->value.iec958.status[1] = sbits >> 8;
3361 ucontrol->value.iec958.status[2] = sbits >> 16;
3362 ucontrol->value.iec958.status[3] = sbits >> 24;
3363 return 0;
3364}
3365
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003366static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 {
3368 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003369 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 .info = snd_hda_spdif_in_switch_info,
3371 .get = snd_hda_spdif_in_switch_get,
3372 .put = snd_hda_spdif_in_switch_put,
3373 },
3374 {
3375 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3376 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003377 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 .info = snd_hda_spdif_mask_info,
3379 .get = snd_hda_spdif_in_status_get,
3380 },
3381 { } /* end */
3382};
3383
3384/**
3385 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3386 * @codec: the HDA codec
3387 * @nid: audio in widget NID
3388 *
3389 * Creates controls related with the SPDIF input.
3390 * Called from each patch supporting the SPDIF in.
3391 *
3392 * Returns 0 if successful, or a negative error code.
3393 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003394int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395{
3396 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003397 struct snd_kcontrol *kctl;
3398 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003399 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400
Takashi Iwaidcda5802012-10-12 17:24:51 +02003401 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003402 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003403 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
3404 return -EBUSY;
3405 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3407 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01003408 if (!kctl)
3409 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003411 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003412 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 return err;
3414 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003415 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01003416 snd_hda_codec_read(codec, nid, 0,
3417 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02003418 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 return 0;
3420}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003421EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003423/*
3424 * command cache
3425 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426
Takashi Iwaic370dd62012-12-13 18:30:04 +01003427/* build a 31bit cache key with the widget id and the command parameter */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003428#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3429#define get_cmd_cache_nid(key) ((key) & 0xff)
3430#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3431
3432/**
3433 * snd_hda_codec_write_cache - send a single command with caching
3434 * @codec: the HDA codec
3435 * @nid: NID to send the command
3436 * @direct: direct flag
3437 * @verb: the verb to send
3438 * @parm: the parameter for the verb
3439 *
3440 * Send a single command without waiting for response.
3441 *
3442 * Returns 0 if successful, or a negative error code.
3443 */
3444int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
3445 int direct, unsigned int verb, unsigned int parm)
3446{
Takashi Iwaic370dd62012-12-13 18:30:04 +01003447 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003448 struct hda_cache_head *c;
3449 u32 key;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003450
Takashi Iwaic370dd62012-12-13 18:30:04 +01003451 if (!codec->cached_write) {
3452 err = snd_hda_codec_write(codec, nid, direct, verb, parm);
3453 if (err < 0)
3454 return err;
3455 }
3456
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003457 /* parm may contain the verb stuff for get/set amp */
3458 verb = verb | (parm >> 8);
3459 parm &= 0xff;
3460 key = build_cmd_cache_key(nid, verb);
3461 mutex_lock(&codec->bus->cmd_mutex);
3462 c = get_alloc_hash(&codec->cmd_cache, key);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003463 if (c) {
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003464 c->val = parm;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003465 if (codec->cached_write)
3466 c->dirty = 1;
3467 }
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003468 mutex_unlock(&codec->bus->cmd_mutex);
3469 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003470}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003471EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003472
Takashi Iwaid5191e52009-11-16 14:58:17 +01003473/**
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003474 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3475 * @codec: the HDA codec
3476 * @nid: NID to send the command
3477 * @direct: direct flag
3478 * @verb: the verb to send
3479 * @parm: the parameter for the verb
3480 *
3481 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3482 * command if the parameter is already identical with the cached value.
3483 * If not, it sends the command and refreshes the cache.
3484 *
3485 * Returns 0 if successful, or a negative error code.
3486 */
3487int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
3488 int direct, unsigned int verb, unsigned int parm)
3489{
3490 struct hda_cache_head *c;
3491 u32 key;
3492
3493 /* parm may contain the verb stuff for get/set amp */
3494 verb = verb | (parm >> 8);
3495 parm &= 0xff;
3496 key = build_cmd_cache_key(nid, verb);
3497 mutex_lock(&codec->bus->cmd_mutex);
3498 c = get_hash(&codec->cmd_cache, key);
3499 if (c && c->val == parm) {
3500 mutex_unlock(&codec->bus->cmd_mutex);
3501 return 0;
3502 }
3503 mutex_unlock(&codec->bus->cmd_mutex);
3504 return snd_hda_codec_write_cache(codec, nid, direct, verb, parm);
3505}
3506EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
3507
3508/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003509 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3510 * @codec: HD-audio codec
3511 *
3512 * Execute all verbs recorded in the command caches to resume.
3513 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003514void snd_hda_codec_resume_cache(struct hda_codec *codec)
3515{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003516 int i;
3517
Takashi Iwaic370dd62012-12-13 18:30:04 +01003518 mutex_lock(&codec->hash_mutex);
3519 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3520 struct hda_cache_head *buffer;
3521 u32 key;
3522
3523 buffer = snd_array_elem(&codec->cmd_cache.buf, i);
3524 key = buffer->key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003525 if (!key)
3526 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003527 if (!buffer->dirty)
3528 continue;
3529 buffer->dirty = 0;
3530 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003531 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3532 get_cmd_cache_cmd(key), buffer->val);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003533 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003534 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01003535 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003536}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003537EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003538
3539/**
3540 * snd_hda_sequence_write_cache - sequence writes with caching
3541 * @codec: the HDA codec
3542 * @seq: VERB array to send
3543 *
3544 * Send the commands sequentially from the given array.
3545 * Thte commands are recorded on cache for power-save and resume.
3546 * The array must be terminated with NID=0.
3547 */
3548void snd_hda_sequence_write_cache(struct hda_codec *codec,
3549 const struct hda_verb *seq)
3550{
3551 for (; seq->nid; seq++)
3552 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3553 seq->param);
3554}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003555EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003556
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003557void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
3558 unsigned int power_state,
3559 bool eapd_workaround)
Takashi Iwai54d17402005-11-21 16:33:22 +01003560{
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003561 hda_nid_t nid = codec->start_nid;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003562 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01003563
Takashi Iwaicb53c622007-08-10 17:21:45 +02003564 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003565 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003566 if (!(wcaps & AC_WCAP_POWER))
3567 continue;
3568 /* don't power down the widget if it controls eapd and
3569 * EAPD_BTLENABLE is set.
3570 */
3571 if (eapd_workaround && power_state == AC_PWRST_D3 &&
3572 get_wcaps_type(wcaps) == AC_WID_PIN &&
3573 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3574 int eapd = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003575 AC_VERB_GET_EAPD_BTLENABLE, 0);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003576 if (eapd & 0x02)
3577 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003578 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003579 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
3580 power_state);
Takashi Iwai54d17402005-11-21 16:33:22 +01003581 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003582}
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003583EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all);
3584
3585/*
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003586 * supported power states check
3587 */
3588static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg,
3589 unsigned int power_state)
3590{
3591 int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE);
3592
Mengdong Line037cb42012-08-10 14:11:58 +02003593 if (sup == -1)
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003594 return false;
3595 if (sup & power_state)
3596 return true;
3597 else
3598 return false;
3599}
3600
3601/*
Takashi Iwai432c6412012-08-28 09:59:20 -07003602 * wait until the state is reached, returns the current state
3603 */
3604static unsigned int hda_sync_power_state(struct hda_codec *codec,
3605 hda_nid_t fg,
3606 unsigned int power_state)
3607{
3608 unsigned long end_time = jiffies + msecs_to_jiffies(500);
3609 unsigned int state, actual_state;
3610
3611 for (;;) {
3612 state = snd_hda_codec_read(codec, fg, 0,
3613 AC_VERB_GET_POWER_STATE, 0);
3614 if (state & AC_PWRST_ERROR)
3615 break;
3616 actual_state = (state >> 4) & 0x0f;
3617 if (actual_state == power_state)
3618 break;
3619 if (time_after_eq(jiffies, end_time))
3620 break;
3621 /* wait until the codec reachs to the target state */
3622 msleep(1);
3623 }
3624 return state;
3625}
3626
3627/*
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003628 * set power state of the codec, and return the power state
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003629 */
Takashi Iwaid8193872012-08-31 07:54:38 -07003630static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003631 unsigned int power_state)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003632{
Takashi Iwaid8193872012-08-31 07:54:38 -07003633 hda_nid_t fg = codec->afg ? codec->afg : codec->mfg;
Wang Xingchao09617ce2012-06-08 10:26:08 +08003634 int count;
3635 unsigned int state;
3636
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003637 /* this delay seems necessary to avoid click noise at power-down */
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003638 if (power_state == AC_PWRST_D3) {
3639 /* transition time less than 10ms for power down */
Takashi Iwai983f6b92012-08-28 09:18:01 -07003640 msleep(codec->epss ? 10 : 100);
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003641 }
Wang Xingchao09617ce2012-06-08 10:26:08 +08003642
3643 /* repeat power states setting at most 10 times*/
3644 for (count = 0; count < 10; count++) {
Takashi Iwai432c6412012-08-28 09:59:20 -07003645 if (codec->patch_ops.set_power_state)
3646 codec->patch_ops.set_power_state(codec, fg,
3647 power_state);
3648 else {
3649 snd_hda_codec_read(codec, fg, 0,
3650 AC_VERB_SET_POWER_STATE,
3651 power_state);
3652 snd_hda_codec_set_power_to_all(codec, fg, power_state,
3653 true);
3654 }
3655 state = hda_sync_power_state(codec, fg, power_state);
Wang Xingchao09617ce2012-06-08 10:26:08 +08003656 if (!(state & AC_PWRST_ERROR))
3657 break;
3658 }
Mengdong Linb8dfc4622012-08-23 17:32:30 +08003659
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003660 return state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003661}
Takashi Iwai54d17402005-11-21 16:33:22 +01003662
Takashi Iwai11aeff02008-07-30 15:01:46 +02003663#ifdef CONFIG_SND_HDA_HWDEP
3664/* execute additional init verbs */
3665static void hda_exec_init_verbs(struct hda_codec *codec)
3666{
3667 if (codec->init_verbs.list)
3668 snd_hda_sequence_write(codec, codec->init_verbs.list);
3669}
3670#else
3671static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3672#endif
3673
Takashi Iwai2a439522011-07-26 09:52:50 +02003674#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003675/*
3676 * call suspend and power-down; used both from PM and power-save
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003677 * this function returns the power state in the end
Takashi Iwaicb53c622007-08-10 17:21:45 +02003678 */
Dylan Reidd17344b2012-09-28 15:57:01 -07003679static unsigned int hda_call_codec_suspend(struct hda_codec *codec, bool in_wq)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003680{
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003681 unsigned int state;
3682
Takashi Iwai989c3182012-11-19 14:14:58 +01003683 codec->in_pm = 1;
3684
Takashi Iwaicb53c622007-08-10 17:21:45 +02003685 if (codec->patch_ops.suspend)
Takashi Iwai68cb2b52012-07-02 15:20:37 +02003686 codec->patch_ops.suspend(codec);
Takashi Iwaieb541332010-08-06 13:48:11 +02003687 hda_cleanup_all_streams(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07003688 state = hda_set_power_state(codec, AC_PWRST_D3);
Dylan Reidd17344b2012-09-28 15:57:01 -07003689 /* Cancel delayed work if we aren't currently running from it. */
3690 if (!in_wq)
3691 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02003692 spin_lock(&codec->power_lock);
3693 snd_hda_update_power_acct(codec);
3694 trace_hda_power_down(codec);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02003695 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02003696 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003697 codec->power_jiffies = jiffies;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02003698 spin_unlock(&codec->power_lock);
Takashi Iwai989c3182012-11-19 14:14:58 +01003699 codec->in_pm = 0;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003700 return state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003701}
3702
Takashi Iwaic370dd62012-12-13 18:30:04 +01003703/* mark all entries of cmd and amp caches dirty */
3704static void hda_mark_cmd_cache_dirty(struct hda_codec *codec)
3705{
3706 int i;
3707 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3708 struct hda_cache_head *cmd;
3709 cmd = snd_array_elem(&codec->cmd_cache.buf, i);
3710 cmd->dirty = 1;
3711 }
3712 for (i = 0; i < codec->amp_cache.buf.used; i++) {
3713 struct hda_amp_info *amp;
3714 amp = snd_array_elem(&codec->cmd_cache.buf, i);
3715 amp->head.dirty = 1;
3716 }
3717}
3718
Takashi Iwaicb53c622007-08-10 17:21:45 +02003719/*
3720 * kick up codec; used both from PM and power-save
3721 */
3722static void hda_call_codec_resume(struct hda_codec *codec)
3723{
Takashi Iwai989c3182012-11-19 14:14:58 +01003724 codec->in_pm = 1;
3725
Takashi Iwaic370dd62012-12-13 18:30:04 +01003726 hda_mark_cmd_cache_dirty(codec);
3727
Takashi Iwai7f308302012-05-08 16:52:23 +02003728 /* set as if powered on for avoiding re-entering the resume
3729 * in the resume / power-save sequence
3730 */
3731 hda_keep_power_on(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07003732 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02003733 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02003734 hda_exec_init_verbs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003735 if (codec->patch_ops.resume)
3736 codec->patch_ops.resume(codec);
3737 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02003738 if (codec->patch_ops.init)
3739 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003740 snd_hda_codec_resume_amp(codec);
3741 snd_hda_codec_resume_cache(codec);
3742 }
David Henningsson26a6cb62012-10-09 15:04:21 +02003743
3744 if (codec->jackpoll_interval)
3745 hda_jackpoll_work(&codec->jackpoll_work.work);
3746 else {
3747 snd_hda_jack_set_dirty_all(codec);
3748 snd_hda_jack_report_sync(codec);
3749 }
Takashi Iwai989c3182012-11-19 14:14:58 +01003750
3751 codec->in_pm = 0;
Takashi Iwai7f308302012-05-08 16:52:23 +02003752 snd_hda_power_down(codec); /* flag down before returning */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003753}
Takashi Iwai2a439522011-07-26 09:52:50 +02003754#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003755
Takashi Iwai54d17402005-11-21 16:33:22 +01003756
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757/**
3758 * snd_hda_build_controls - build mixer controls
3759 * @bus: the BUS
3760 *
3761 * Creates mixer controls for each codec included in the bus.
3762 *
3763 * Returns 0 if successful, otherwise a negative error code.
3764 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01003765int snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003767 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768
Takashi Iwai0ba21762007-04-16 11:29:14 +02003769 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003770 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003771 if (err < 0) {
Takashi Iwai28d1a852010-03-15 09:05:46 +01003772 printk(KERN_ERR "hda_codec: cannot build controls "
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003773 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003774 err = snd_hda_codec_reset(codec);
3775 if (err < 0) {
3776 printk(KERN_ERR
3777 "hda_codec: cannot revert codec\n");
3778 return err;
3779 }
3780 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003782 return 0;
3783}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003784EXPORT_SYMBOL_HDA(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003786/*
3787 * add standard channel maps if not specified
3788 */
3789static int add_std_chmaps(struct hda_codec *codec)
3790{
3791 int i, str, err;
3792
3793 for (i = 0; i < codec->num_pcms; i++) {
3794 for (str = 0; str < 2; str++) {
3795 struct snd_pcm *pcm = codec->pcm_info[i].pcm;
3796 struct hda_pcm_stream *hinfo =
3797 &codec->pcm_info[i].stream[str];
3798 struct snd_pcm_chmap *chmap;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003799 const struct snd_pcm_chmap_elem *elem;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003800
3801 if (codec->pcm_info[i].own_chmap)
3802 continue;
3803 if (!pcm || !hinfo->substreams)
3804 continue;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003805 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
3806 err = snd_pcm_add_chmap_ctls(pcm, str, elem,
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003807 hinfo->channels_max,
3808 0, &chmap);
3809 if (err < 0)
3810 return err;
3811 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
3812 }
3813 }
3814 return 0;
3815}
3816
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003817/* default channel maps for 2.1 speakers;
3818 * since HD-audio supports only stereo, odd number channels are omitted
3819 */
3820const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
3821 { .channels = 2,
3822 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
3823 { .channels = 4,
3824 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
3825 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
3826 { }
3827};
3828EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
3829
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003830int snd_hda_codec_build_controls(struct hda_codec *codec)
3831{
3832 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02003833 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003834 /* continue to initialize... */
3835 if (codec->patch_ops.init)
3836 err = codec->patch_ops.init(codec);
3837 if (!err && codec->patch_ops.build_controls)
3838 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003839 if (err < 0)
3840 return err;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003841
3842 /* we create chmaps here instead of build_pcms */
3843 err = add_std_chmaps(codec);
3844 if (err < 0)
3845 return err;
3846
David Henningsson26a6cb62012-10-09 15:04:21 +02003847 if (codec->jackpoll_interval)
3848 hda_jackpoll_work(&codec->jackpoll_work.work);
3849 else
3850 snd_hda_jack_report_sync(codec); /* call at the last init point */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851 return 0;
3852}
3853
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854/*
3855 * stream formats
3856 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02003857struct hda_rate_tbl {
3858 unsigned int hz;
3859 unsigned int alsa_bits;
3860 unsigned int hda_fmt;
3861};
3862
Takashi Iwai92f10b32010-08-03 14:21:00 +02003863/* rate = base * mult / div */
3864#define HDA_RATE(base, mult, div) \
3865 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
3866 (((div) - 1) << AC_FMT_DIV_SHIFT))
3867
Takashi Iwaibefdf312005-08-22 13:57:55 +02003868static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003870
3871 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003872 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
3873 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
3874 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
3875 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
3876 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
3877 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
3878 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
3879 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
3880 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
3881 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
3882 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003883#define AC_PAR_PCM_RATE_BITS 11
3884 /* up to bits 10, 384kHZ isn't supported properly */
3885
3886 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003887 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003888
Takashi Iwaibefdf312005-08-22 13:57:55 +02003889 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890};
3891
3892/**
3893 * snd_hda_calc_stream_format - calculate format bitset
3894 * @rate: the sample rate
3895 * @channels: the number of channels
3896 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3897 * @maxbps: the max. bps
3898 *
3899 * Calculate the format bitset from the given rate, channels and th PCM format.
3900 *
3901 * Return zero if invalid.
3902 */
3903unsigned int snd_hda_calc_stream_format(unsigned int rate,
3904 unsigned int channels,
3905 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03003906 unsigned int maxbps,
3907 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908{
3909 int i;
3910 unsigned int val = 0;
3911
Takashi Iwaibefdf312005-08-22 13:57:55 +02003912 for (i = 0; rate_bits[i].hz; i++)
3913 if (rate_bits[i].hz == rate) {
3914 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915 break;
3916 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003917 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918 snd_printdd("invalid rate %d\n", rate);
3919 return 0;
3920 }
3921
3922 if (channels == 0 || channels > 8) {
3923 snd_printdd("invalid channels %d\n", channels);
3924 return 0;
3925 }
3926 val |= channels - 1;
3927
3928 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003929 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003930 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003931 break;
3932 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003933 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003934 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935 case 20:
3936 case 24:
3937 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003938 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003939 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003941 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02003943 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944 break;
3945 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003946 snd_printdd("invalid format width %d\n",
3947 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948 return 0;
3949 }
3950
Anssi Hannula32c168c2010-08-03 13:28:57 +03003951 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003952 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03003953
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954 return val;
3955}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003956EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003958static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid,
3959 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003960{
3961 unsigned int val = 0;
3962 if (nid != codec->afg &&
3963 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
3964 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
3965 if (!val || val == -1)
3966 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
3967 if (!val || val == -1)
3968 return 0;
3969 return val;
3970}
3971
3972static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3973{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003974 return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003975 get_pcm_param);
3976}
3977
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003978static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid,
3979 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003980{
3981 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
3982 if (!streams || streams == -1)
3983 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
3984 if (!streams || streams == -1)
3985 return 0;
3986 return streams;
3987}
3988
3989static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
3990{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003991 return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003992 get_stream_param);
3993}
3994
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995/**
3996 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
3997 * @codec: the HDA codec
3998 * @nid: NID to query
3999 * @ratesp: the pointer to store the detected rate bitflags
4000 * @formatsp: the pointer to store the detected formats
4001 * @bpsp: the pointer to store the detected format widths
4002 *
4003 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
4004 * or @bsps argument is ignored.
4005 *
4006 * Returns 0 if successful, otherwise a negative error code.
4007 */
Stephen Warren384a48d2011-06-01 11:14:21 -06004008int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
4010{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004011 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004013 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004014 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015
4016 if (ratesp) {
4017 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004018 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02004020 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004022 if (rates == 0) {
4023 snd_printk(KERN_ERR "hda_codec: rates == 0 "
4024 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
4025 nid, val,
4026 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
4027 return -EIO;
4028 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029 *ratesp = rates;
4030 }
4031
4032 if (formatsp || bpsp) {
4033 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004034 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004036 streams = query_stream_param(codec, nid);
4037 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039
4040 bps = 0;
4041 if (streams & AC_SUPFMT_PCM) {
4042 if (val & AC_SUPPCM_BITS_8) {
4043 formats |= SNDRV_PCM_FMTBIT_U8;
4044 bps = 8;
4045 }
4046 if (val & AC_SUPPCM_BITS_16) {
4047 formats |= SNDRV_PCM_FMTBIT_S16_LE;
4048 bps = 16;
4049 }
4050 if (wcaps & AC_WCAP_DIGITAL) {
4051 if (val & AC_SUPPCM_BITS_32)
4052 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
4053 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
4054 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4055 if (val & AC_SUPPCM_BITS_24)
4056 bps = 24;
4057 else if (val & AC_SUPPCM_BITS_20)
4058 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004059 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
4060 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4062 if (val & AC_SUPPCM_BITS_32)
4063 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064 else if (val & AC_SUPPCM_BITS_24)
4065 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02004066 else if (val & AC_SUPPCM_BITS_20)
4067 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068 }
4069 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004070#if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
Takashi Iwaib5025c52009-07-01 18:05:27 +02004071 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004073 if (!bps)
4074 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02004075 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004076#endif
Takashi Iwaib5025c52009-07-01 18:05:27 +02004077 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02004078 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079 /* temporary hack: we have still no proper support
4080 * for the direct AC3 stream...
4081 */
4082 formats |= SNDRV_PCM_FMTBIT_U8;
4083 bps = 8;
4084 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004085 if (formats == 0) {
4086 snd_printk(KERN_ERR "hda_codec: formats == 0 "
4087 "(nid=0x%x, val=0x%x, ovrd=%i, "
4088 "streams=0x%x)\n",
4089 nid, val,
4090 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
4091 streams);
4092 return -EIO;
4093 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094 if (formatsp)
4095 *formatsp = formats;
4096 if (bpsp)
4097 *bpsp = bps;
4098 }
4099
4100 return 0;
4101}
Stephen Warren384a48d2011-06-01 11:14:21 -06004102EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103
4104/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01004105 * snd_hda_is_supported_format - Check the validity of the format
4106 * @codec: HD-audio codec
4107 * @nid: NID to check
4108 * @format: the HD-audio format value to check
4109 *
4110 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111 *
4112 * Returns 1 if supported, 0 if not.
4113 */
4114int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
4115 unsigned int format)
4116{
4117 int i;
4118 unsigned int val = 0, rate, stream;
4119
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004120 val = query_pcm_param(codec, nid);
4121 if (!val)
4122 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123
4124 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004125 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02004126 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127 if (val & (1 << i))
4128 break;
4129 return 0;
4130 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004131 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132 return 0;
4133
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004134 stream = query_stream_param(codec, nid);
4135 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136 return 0;
4137
4138 if (stream & AC_SUPFMT_PCM) {
4139 switch (format & 0xf0) {
4140 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004141 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142 return 0;
4143 break;
4144 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004145 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146 return 0;
4147 break;
4148 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004149 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150 return 0;
4151 break;
4152 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004153 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154 return 0;
4155 break;
4156 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004157 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 return 0;
4159 break;
4160 default:
4161 return 0;
4162 }
4163 } else {
4164 /* FIXME: check for float32 and AC3? */
4165 }
4166
4167 return 1;
4168}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004169EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170
4171/*
4172 * PCM stuff
4173 */
4174static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
4175 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004176 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177{
4178 return 0;
4179}
4180
4181static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
4182 struct hda_codec *codec,
4183 unsigned int stream_tag,
4184 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004185 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186{
4187 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4188 return 0;
4189}
4190
4191static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
4192 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004193 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194{
Takashi Iwai888afa12008-03-18 09:57:50 +01004195 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196 return 0;
4197}
4198
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004199static int set_pcm_default_values(struct hda_codec *codec,
4200 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004202 int err;
4203
Takashi Iwai0ba21762007-04-16 11:29:14 +02004204 /* query support PCM information from the given NID */
4205 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004206 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02004207 info->rates ? NULL : &info->rates,
4208 info->formats ? NULL : &info->formats,
4209 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004210 if (err < 0)
4211 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212 }
4213 if (info->ops.open == NULL)
4214 info->ops.open = hda_pcm_default_open_close;
4215 if (info->ops.close == NULL)
4216 info->ops.close = hda_pcm_default_open_close;
4217 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004218 if (snd_BUG_ON(!info->nid))
4219 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220 info->ops.prepare = hda_pcm_default_prepare;
4221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004223 if (snd_BUG_ON(!info->nid))
4224 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225 info->ops.cleanup = hda_pcm_default_cleanup;
4226 }
4227 return 0;
4228}
4229
Takashi Iwaieb541332010-08-06 13:48:11 +02004230/*
4231 * codec prepare/cleanup entries
4232 */
4233int snd_hda_codec_prepare(struct hda_codec *codec,
4234 struct hda_pcm_stream *hinfo,
4235 unsigned int stream,
4236 unsigned int format,
4237 struct snd_pcm_substream *substream)
4238{
4239 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004240 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004241 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
4242 if (ret >= 0)
4243 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004244 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004245 return ret;
4246}
4247EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
4248
4249void snd_hda_codec_cleanup(struct hda_codec *codec,
4250 struct hda_pcm_stream *hinfo,
4251 struct snd_pcm_substream *substream)
4252{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004253 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004254 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004255 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004256}
4257EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
4258
Takashi Iwaid5191e52009-11-16 14:58:17 +01004259/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01004260const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
4261 "Audio", "SPDIF", "HDMI", "Modem"
4262};
4263
Takashi Iwai176d5332008-07-30 15:01:44 +02004264/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004265 * get the empty PCM device number to assign
Takashi Iwaic8936222010-01-28 17:08:53 +01004266 *
4267 * note the max device number is limited by HDA_MAX_PCMS, currently 10
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004268 */
4269static int get_empty_pcm_device(struct hda_bus *bus, int type)
4270{
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004271 /* audio device indices; not linear to keep compatibility */
4272 static int audio_idx[HDA_PCM_NTYPES][5] = {
4273 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
4274 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01004275 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004276 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004277 };
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004278 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004279
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004280 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004281 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
4282 return -EINVAL;
4283 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004284
4285 for (i = 0; audio_idx[type][i] >= 0 ; i++)
4286 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
4287 return audio_idx[type][i];
4288
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004289 /* non-fixed slots starting from 10 */
4290 for (i = 10; i < 32; i++) {
4291 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4292 return i;
4293 }
4294
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004295 snd_printk(KERN_WARNING "Too many %s devices\n",
4296 snd_hda_pcm_type_name[type]);
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004297 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004298}
4299
4300/*
Takashi Iwai176d5332008-07-30 15:01:44 +02004301 * attach a new PCM stream
4302 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004303static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02004304{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004305 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02004306 struct hda_pcm_stream *info;
4307 int stream, err;
4308
Takashi Iwaib91f0802008-11-04 08:43:08 +01004309 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02004310 return -EINVAL;
4311 for (stream = 0; stream < 2; stream++) {
4312 info = &pcm->stream[stream];
4313 if (info->substreams) {
4314 err = set_pcm_default_values(codec, info);
4315 if (err < 0)
4316 return err;
4317 }
4318 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004319 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02004320}
4321
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004322/* assign all PCMs of the given codec */
4323int snd_hda_codec_build_pcms(struct hda_codec *codec)
4324{
4325 unsigned int pcm;
4326 int err;
4327
4328 if (!codec->num_pcms) {
4329 if (!codec->patch_ops.build_pcms)
4330 return 0;
4331 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004332 if (err < 0) {
4333 printk(KERN_ERR "hda_codec: cannot build PCMs"
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004334 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004335 err = snd_hda_codec_reset(codec);
4336 if (err < 0) {
4337 printk(KERN_ERR
4338 "hda_codec: cannot revert codec\n");
4339 return err;
4340 }
4341 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004342 }
4343 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
4344 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
4345 int dev;
4346
4347 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01004348 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004349
4350 if (!cpcm->pcm) {
4351 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
4352 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004353 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004354 cpcm->device = dev;
4355 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004356 if (err < 0) {
4357 printk(KERN_ERR "hda_codec: cannot attach "
4358 "PCM stream %d for codec #%d\n",
4359 dev, codec->addr);
4360 continue; /* no fatal error */
4361 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004362 }
4363 }
4364 return 0;
4365}
4366
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367/**
4368 * snd_hda_build_pcms - build PCM information
4369 * @bus: the BUS
4370 *
4371 * Create PCM information for each codec included in the bus.
4372 *
4373 * The build_pcms codec patch is requested to set up codec->num_pcms and
4374 * codec->pcm_info properly. The array is referred by the top-level driver
4375 * to create its PCM instances.
4376 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4377 * callback.
4378 *
4379 * At least, substreams, channels_min and channels_max must be filled for
4380 * each stream. substreams = 0 indicates that the stream doesn't exist.
4381 * When rates and/or formats are zero, the supported values are queried
4382 * from the given nid. The nid is used also by the default ops.prepare
4383 * and ops.cleanup callbacks.
4384 *
4385 * The driver needs to call ops.open in its open callback. Similarly,
4386 * ops.close is supposed to be called in the close callback.
4387 * ops.prepare should be called in the prepare or hw_params callback
4388 * with the proper parameters for set up.
4389 * ops.cleanup should be called in hw_free for clean up of streams.
4390 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004391 * This function returns 0 if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392 */
Takashi Iwai5cb543d2012-08-09 13:49:23 +02004393int snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004395 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396
Takashi Iwai0ba21762007-04-16 11:29:14 +02004397 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004398 int err = snd_hda_codec_build_pcms(codec);
4399 if (err < 0)
4400 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401 }
4402 return 0;
4403}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004404EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406/**
4407 * snd_hda_check_board_config - compare the current codec with the config table
4408 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004409 * @num_configs: number of config enums
4410 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411 * @tbl: configuration table, terminated by null entries
4412 *
4413 * Compares the modelname or PCI subsystem id of the current codec with the
4414 * given configuration table. If a matching entry is found, returns its
4415 * config value (supposed to be 0 or positive).
4416 *
4417 * If no entries are matching, the function returns a negative value.
4418 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02004419int snd_hda_check_board_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004420 int num_configs, const char * const *models,
Takashi Iwai12f288b2007-08-02 15:51:59 +02004421 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422{
Takashi Iwaif44ac832008-07-30 15:01:45 +02004423 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004424 int i;
4425 for (i = 0; i < num_configs; i++) {
4426 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02004427 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004428 snd_printd(KERN_INFO "hda_codec: model '%s' is "
4429 "selected\n", models[i]);
4430 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431 }
4432 }
4433 }
4434
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004435 if (!codec->bus->pci || !tbl)
4436 return -1;
4437
4438 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
4439 if (!tbl)
4440 return -1;
4441 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02004442#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004443 char tmp[10];
4444 const char *model = NULL;
4445 if (models)
4446 model = models[tbl->value];
4447 if (!model) {
4448 sprintf(tmp, "#%d", tbl->value);
4449 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004451 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4452 "for config %x:%x (%s)\n",
4453 model, tbl->subvendor, tbl->subdevice,
4454 (tbl->name ? tbl->name : "Unknown device"));
4455#endif
4456 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457 }
4458 return -1;
4459}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004460EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461
4462/**
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004463 * snd_hda_check_board_codec_sid_config - compare the current codec
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004464 subsystem ID with the
4465 config table
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004466
4467 This is important for Gateway notebooks with SB450 HDA Audio
4468 where the vendor ID of the PCI device is:
4469 ATI Technologies Inc SB450 HDA Audio [1002:437b]
4470 and the vendor/subvendor are found only at the codec.
4471
4472 * @codec: the HDA codec
4473 * @num_configs: number of config enums
4474 * @models: array of model name strings
4475 * @tbl: configuration table, terminated by null entries
4476 *
4477 * Compares the modelname or PCI subsystem id of the current codec with the
4478 * given configuration table. If a matching entry is found, returns its
4479 * config value (supposed to be 0 or positive).
4480 *
4481 * If no entries are matching, the function returns a negative value.
4482 */
4483int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004484 int num_configs, const char * const *models,
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004485 const struct snd_pci_quirk *tbl)
4486{
4487 const struct snd_pci_quirk *q;
4488
4489 /* Search for codec ID */
4490 for (q = tbl; q->subvendor; q++) {
Takashi Iwaie2301a42011-11-22 19:58:56 +01004491 unsigned int mask = 0xffff0000 | q->subdevice_mask;
4492 unsigned int id = (q->subdevice | (q->subvendor << 16)) & mask;
4493 if ((codec->subsystem_id & mask) == id)
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004494 break;
4495 }
4496
4497 if (!q->subvendor)
4498 return -1;
4499
4500 tbl = q;
4501
4502 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwaid94ff6b2009-09-02 00:20:21 +02004503#ifdef CONFIG_SND_DEBUG_VERBOSE
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004504 char tmp[10];
4505 const char *model = NULL;
4506 if (models)
4507 model = models[tbl->value];
4508 if (!model) {
4509 sprintf(tmp, "#%d", tbl->value);
4510 model = tmp;
4511 }
4512 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4513 "for config %x:%x (%s)\n",
4514 model, tbl->subvendor, tbl->subdevice,
4515 (tbl->name ? tbl->name : "Unknown device"));
4516#endif
4517 return tbl->value;
4518 }
4519 return -1;
4520}
4521EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
4522
4523/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524 * snd_hda_add_new_ctls - create controls from the array
4525 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004526 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527 *
4528 * This helper function creates and add new controls in the given array.
4529 * The array must be terminated with an empty entry as terminator.
4530 *
4531 * Returns 0 if successful, or a negative error code.
4532 */
Takashi Iwai031024e2011-05-02 11:29:30 +02004533int snd_hda_add_new_ctls(struct hda_codec *codec,
4534 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01004536 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537
4538 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004539 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004540 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01004541 if (knew->iface == -1) /* skip this codec private value */
4542 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004543 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004544 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02004545 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01004546 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004547 if (addr > 0)
4548 kctl->id.device = addr;
4549 if (idx > 0)
4550 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01004551 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004552 if (!err)
4553 break;
4554 /* try first with another device index corresponding to
4555 * the codec addr; if it still fails (or it's the
4556 * primary codec), then try another control index
4557 */
4558 if (!addr && codec->addr)
4559 addr = codec->addr;
4560 else if (!idx && !knew->index) {
4561 idx = find_empty_mixer_ctl_idx(codec,
Takashi Iwaidcda5802012-10-12 17:24:51 +02004562 knew->name, 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004563 if (idx <= 0)
4564 return err;
4565 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01004566 return err;
4567 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568 }
4569 return 0;
4570}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004571EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572
Takashi Iwai83012a72012-08-24 18:38:08 +02004573#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02004574static void hda_power_work(struct work_struct *work)
4575{
4576 struct hda_codec *codec =
4577 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004578 struct hda_bus *bus = codec->bus;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004579 unsigned int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004580
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004581 spin_lock(&codec->power_lock);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004582 if (codec->power_transition > 0) { /* during power-up sequence? */
4583 spin_unlock(&codec->power_lock);
4584 return;
4585 }
Maxim Levitsky2e492462007-09-03 15:26:57 +02004586 if (!codec->power_on || codec->power_count) {
4587 codec->power_transition = 0;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004588 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004589 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02004590 }
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004591 spin_unlock(&codec->power_lock);
4592
Dylan Reidd17344b2012-09-28 15:57:01 -07004593 state = hda_call_codec_suspend(codec, true);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004594 codec->pm_down_notified = 0;
4595 if (!bus->power_keep_link_on && (state & AC_PWRST_CLK_STOP_OK)) {
4596 codec->pm_down_notified = 1;
Takashi Iwai68467f52012-08-28 09:14:29 -07004597 hda_call_pm_notify(bus, false);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004598 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02004599}
4600
4601static void hda_keep_power_on(struct hda_codec *codec)
4602{
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004603 spin_lock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004604 codec->power_count++;
4605 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004606 codec->power_jiffies = jiffies;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004607 spin_unlock(&codec->power_lock);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004608}
4609
Takashi Iwaid5191e52009-11-16 14:58:17 +01004610/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01004611void snd_hda_update_power_acct(struct hda_codec *codec)
4612{
4613 unsigned long delta = jiffies - codec->power_jiffies;
4614 if (codec->power_on)
4615 codec->power_on_acct += delta;
4616 else
4617 codec->power_off_acct += delta;
4618 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004619}
4620
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004621/* Transition to powered up, if wait_power_down then wait for a pending
4622 * transition to D3 to complete. A pending D3 transition is indicated
4623 * with power_transition == -1. */
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004624/* call this with codec->power_lock held! */
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004625static void __snd_hda_power_up(struct hda_codec *codec, bool wait_power_down)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004626{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004627 struct hda_bus *bus = codec->bus;
4628
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004629 /* Return if power_on or transitioning to power_on, unless currently
4630 * powering down. */
4631 if ((codec->power_on || codec->power_transition > 0) &&
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004632 !(wait_power_down && codec->power_transition < 0))
Takashi Iwaicb53c622007-08-10 17:21:45 +02004633 return;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004634 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004635
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004636 cancel_delayed_work_sync(&codec->power_work);
4637
4638 spin_lock(&codec->power_lock);
Dylan Reidb43d2242012-06-21 21:51:22 -07004639 /* If the power down delayed work was cancelled above before starting,
4640 * then there is no need to go through power up here.
4641 */
4642 if (codec->power_on) {
Takashi Iwai535b6c52012-08-20 21:25:22 +02004643 if (codec->power_transition < 0)
4644 codec->power_transition = 0;
Dylan Reidb43d2242012-06-21 21:51:22 -07004645 return;
4646 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004647
Takashi Iwaid66fee52011-08-02 15:39:31 +02004648 trace_hda_power_up(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004649 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004650 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004651 codec->power_jiffies = jiffies;
Takashi Iwai7f308302012-05-08 16:52:23 +02004652 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004653 spin_unlock(&codec->power_lock);
4654
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004655 if (codec->pm_down_notified) {
4656 codec->pm_down_notified = 0;
Takashi Iwai68467f52012-08-28 09:14:29 -07004657 hda_call_pm_notify(bus, true);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004658 }
4659
Takashi Iwaicb53c622007-08-10 17:21:45 +02004660 hda_call_codec_resume(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004661
4662 spin_lock(&codec->power_lock);
Takashi Iwaia221e282007-08-16 16:35:33 +02004663 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004664}
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004665
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004666#define power_save(codec) \
4667 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004668
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004669/* Transition to powered down */
4670static void __snd_hda_power_down(struct hda_codec *codec)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004671{
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004672 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004673 return;
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004674
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004675 if (power_save(codec)) {
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004676 codec->power_transition = -1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01004677 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004678 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02004679 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004680}
4681
4682/**
4683 * snd_hda_power_save - Power-up/down/sync the codec
4684 * @codec: HD-audio codec
4685 * @delta: the counter delta to change
4686 *
4687 * Change the power-up counter via @delta, and power up or down the hardware
4688 * appropriately. For the power-down, queue to the delayed action.
4689 * Passing zero to @delta means to synchronize the power state.
4690 */
4691void snd_hda_power_save(struct hda_codec *codec, int delta, bool d3wait)
4692{
4693 spin_lock(&codec->power_lock);
4694 codec->power_count += delta;
4695 trace_hda_power_count(codec);
4696 if (delta > 0)
4697 __snd_hda_power_up(codec, d3wait);
4698 else
4699 __snd_hda_power_down(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004700 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004701}
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004702EXPORT_SYMBOL_HDA(snd_hda_power_save);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004703
Takashi Iwaid5191e52009-11-16 14:58:17 +01004704/**
4705 * snd_hda_check_amp_list_power - Check the amp list and update the power
4706 * @codec: HD-audio codec
4707 * @check: the object containing an AMP list and the status
4708 * @nid: NID to check / update
4709 *
4710 * Check whether the given NID is in the amp list. If it's in the list,
4711 * check the current AMP status, and update the the power-status according
4712 * to the mute status.
4713 *
4714 * This function is supposed to be set or called from the check_power_status
4715 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004716 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004717int snd_hda_check_amp_list_power(struct hda_codec *codec,
4718 struct hda_loopback_check *check,
4719 hda_nid_t nid)
4720{
Takashi Iwai031024e2011-05-02 11:29:30 +02004721 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004722 int ch, v;
4723
4724 if (!check->amplist)
4725 return 0;
4726 for (p = check->amplist; p->nid; p++) {
4727 if (p->nid == nid)
4728 break;
4729 }
4730 if (!p->nid)
4731 return 0; /* nothing changed */
4732
4733 for (p = check->amplist; p->nid; p++) {
4734 for (ch = 0; ch < 2; ch++) {
4735 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4736 p->idx);
4737 if (!(v & HDA_AMP_MUTE) && v > 0) {
4738 if (!check->power_on) {
4739 check->power_on = 1;
4740 snd_hda_power_up(codec);
4741 }
4742 return 1;
4743 }
4744 }
4745 }
4746 if (check->power_on) {
4747 check->power_on = 0;
4748 snd_hda_power_down(codec);
4749 }
4750 return 0;
4751}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004752EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004753#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004755/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004756 * Channel mode helper
4757 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004758
4759/**
4760 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
4761 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004762int snd_hda_ch_mode_info(struct hda_codec *codec,
4763 struct snd_ctl_elem_info *uinfo,
4764 const struct hda_channel_mode *chmode,
4765 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004766{
4767 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4768 uinfo->count = 1;
4769 uinfo->value.enumerated.items = num_chmodes;
4770 if (uinfo->value.enumerated.item >= num_chmodes)
4771 uinfo->value.enumerated.item = num_chmodes - 1;
4772 sprintf(uinfo->value.enumerated.name, "%dch",
4773 chmode[uinfo->value.enumerated.item].channels);
4774 return 0;
4775}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004776EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004777
Takashi Iwaid5191e52009-11-16 14:58:17 +01004778/**
4779 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
4780 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004781int snd_hda_ch_mode_get(struct hda_codec *codec,
4782 struct snd_ctl_elem_value *ucontrol,
4783 const struct hda_channel_mode *chmode,
4784 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004785 int max_channels)
4786{
4787 int i;
4788
4789 for (i = 0; i < num_chmodes; i++) {
4790 if (max_channels == chmode[i].channels) {
4791 ucontrol->value.enumerated.item[0] = i;
4792 break;
4793 }
4794 }
4795 return 0;
4796}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004797EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004798
Takashi Iwaid5191e52009-11-16 14:58:17 +01004799/**
4800 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
4801 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004802int snd_hda_ch_mode_put(struct hda_codec *codec,
4803 struct snd_ctl_elem_value *ucontrol,
4804 const struct hda_channel_mode *chmode,
4805 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004806 int *max_channelsp)
4807{
4808 unsigned int mode;
4809
4810 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01004811 if (mode >= num_chmodes)
4812 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004813 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004814 return 0;
4815 /* change the current channel setting */
4816 *max_channelsp = chmode[mode].channels;
4817 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004818 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004819 return 1;
4820}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004821EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004822
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823/*
4824 * input MUX helper
4825 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004826
4827/**
4828 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
4829 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004830int snd_hda_input_mux_info(const struct hda_input_mux *imux,
4831 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832{
4833 unsigned int index;
4834
4835 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4836 uinfo->count = 1;
4837 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004838 if (!imux->num_items)
4839 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840 index = uinfo->value.enumerated.item;
4841 if (index >= imux->num_items)
4842 index = imux->num_items - 1;
4843 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
4844 return 0;
4845}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004846EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847
Takashi Iwaid5191e52009-11-16 14:58:17 +01004848/**
4849 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
4850 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004851int snd_hda_input_mux_put(struct hda_codec *codec,
4852 const struct hda_input_mux *imux,
4853 struct snd_ctl_elem_value *ucontrol,
4854 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855 unsigned int *cur_val)
4856{
4857 unsigned int idx;
4858
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004859 if (!imux->num_items)
4860 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861 idx = ucontrol->value.enumerated.item[0];
4862 if (idx >= imux->num_items)
4863 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004864 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004866 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
4867 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868 *cur_val = idx;
4869 return 1;
4870}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004871EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872
4873
4874/*
Takashi Iwaidda415d2012-11-30 18:34:38 +01004875 * process kcontrol info callback of a simple string enum array
4876 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
4877 */
4878int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
4879 struct snd_ctl_elem_info *uinfo,
4880 int num_items, const char * const *texts)
4881{
4882 static const char * const texts_default[] = {
4883 "Disabled", "Enabled"
4884 };
4885
4886 if (!texts || !num_items) {
4887 num_items = 2;
4888 texts = texts_default;
4889 }
4890
4891 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4892 uinfo->count = 1;
4893 uinfo->value.enumerated.items = num_items;
4894 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
4895 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
4896 strcpy(uinfo->value.enumerated.name,
4897 texts[uinfo->value.enumerated.item]);
4898 return 0;
4899}
4900EXPORT_SYMBOL_HDA(snd_hda_enum_helper_info);
4901
4902/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903 * Multi-channel / digital-out PCM helper functions
4904 */
4905
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004906/* setup SPDIF output stream */
4907static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
4908 unsigned int stream_tag, unsigned int format)
4909{
Laurence Darby3bef1c32012-11-03 17:00:06 +00004910 struct hda_spdif_out *spdif;
4911 unsigned int curr_fmt;
4912 bool reset;
Stephen Warren7c9359762011-06-01 11:14:17 -06004913
Laurence Darby3bef1c32012-11-03 17:00:06 +00004914 spdif = snd_hda_spdif_out_of_nid(codec, nid);
4915 curr_fmt = snd_hda_codec_read(codec, nid, 0,
4916 AC_VERB_GET_STREAM_FORMAT, 0);
4917 reset = codec->spdif_status_reset &&
4918 (spdif->ctls & AC_DIG1_ENABLE) &&
4919 curr_fmt != format;
4920
4921 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
4922 updated */
4923 if (reset)
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004924 set_dig_out_convert(codec, nid,
Stephen Warren7c9359762011-06-01 11:14:17 -06004925 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02004926 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004927 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02004928 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004929 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004930 for (d = codec->slave_dig_outs; *d; d++)
4931 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
4932 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004933 }
Takashi Iwai2f728532008-09-25 16:32:41 +02004934 /* turn on again (if needed) */
Laurence Darby3bef1c32012-11-03 17:00:06 +00004935 if (reset)
Takashi Iwai2f728532008-09-25 16:32:41 +02004936 set_dig_out_convert(codec, nid,
Stephen Warren7c9359762011-06-01 11:14:17 -06004937 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02004938}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004939
Takashi Iwai2f728532008-09-25 16:32:41 +02004940static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
4941{
4942 snd_hda_codec_cleanup_stream(codec, nid);
4943 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004944 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004945 for (d = codec->slave_dig_outs; *d; d++)
4946 snd_hda_codec_cleanup_stream(codec, *d);
4947 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004948}
4949
Takashi Iwaid5191e52009-11-16 14:58:17 +01004950/**
4951 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
4952 * @bus: HD-audio bus
4953 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004954void snd_hda_bus_reboot_notify(struct hda_bus *bus)
4955{
4956 struct hda_codec *codec;
4957
4958 if (!bus)
4959 return;
4960 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02004961 if (hda_codec_is_power_on(codec) &&
4962 codec->patch_ops.reboot_notify)
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004963 codec->patch_ops.reboot_notify(codec);
4964 }
4965}
Takashi Iwai8f217a22009-11-10 18:26:12 +01004966EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004967
Takashi Iwaid5191e52009-11-16 14:58:17 +01004968/**
4969 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004971int snd_hda_multi_out_dig_open(struct hda_codec *codec,
4972 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973{
Ingo Molnar62932df2006-01-16 16:34:20 +01004974 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02004975 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
4976 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02004977 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01004979 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980 return 0;
4981}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004982EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983
Takashi Iwaid5191e52009-11-16 14:58:17 +01004984/**
4985 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
4986 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004987int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
4988 struct hda_multi_out *mout,
4989 unsigned int stream_tag,
4990 unsigned int format,
4991 struct snd_pcm_substream *substream)
4992{
4993 mutex_lock(&codec->spdif_mutex);
4994 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
4995 mutex_unlock(&codec->spdif_mutex);
4996 return 0;
4997}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004998EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004999
Takashi Iwaid5191e52009-11-16 14:58:17 +01005000/**
5001 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
5002 */
Takashi Iwai9411e212009-02-13 11:32:28 +01005003int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
5004 struct hda_multi_out *mout)
5005{
5006 mutex_lock(&codec->spdif_mutex);
5007 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5008 mutex_unlock(&codec->spdif_mutex);
5009 return 0;
5010}
5011EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
5012
Takashi Iwaid5191e52009-11-16 14:58:17 +01005013/**
5014 * snd_hda_multi_out_dig_close - release the digital out stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005016int snd_hda_multi_out_dig_close(struct hda_codec *codec,
5017 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005018{
Ingo Molnar62932df2006-01-16 16:34:20 +01005019 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005020 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01005021 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022 return 0;
5023}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005024EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025
Takashi Iwaid5191e52009-11-16 14:58:17 +01005026/**
5027 * snd_hda_multi_out_analog_open - open analog outputs
5028 *
5029 * Open analog outputs and set up the hw-constraints.
5030 * If the digital outputs can be opened as slave, open the digital
5031 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005033int snd_hda_multi_out_analog_open(struct hda_codec *codec,
5034 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01005035 struct snd_pcm_substream *substream,
5036 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037{
Takashi Iwai9a081602008-02-12 18:37:26 +01005038 struct snd_pcm_runtime *runtime = substream->runtime;
5039 runtime->hw.channels_max = mout->max_channels;
5040 if (mout->dig_out_nid) {
5041 if (!mout->analog_rates) {
5042 mout->analog_rates = hinfo->rates;
5043 mout->analog_formats = hinfo->formats;
5044 mout->analog_maxbps = hinfo->maxbps;
5045 } else {
5046 runtime->hw.rates = mout->analog_rates;
5047 runtime->hw.formats = mout->analog_formats;
5048 hinfo->maxbps = mout->analog_maxbps;
5049 }
5050 if (!mout->spdif_rates) {
5051 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
5052 &mout->spdif_rates,
5053 &mout->spdif_formats,
5054 &mout->spdif_maxbps);
5055 }
5056 mutex_lock(&codec->spdif_mutex);
5057 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02005058 if ((runtime->hw.rates & mout->spdif_rates) &&
5059 (runtime->hw.formats & mout->spdif_formats)) {
5060 runtime->hw.rates &= mout->spdif_rates;
5061 runtime->hw.formats &= mout->spdif_formats;
5062 if (mout->spdif_maxbps < hinfo->maxbps)
5063 hinfo->maxbps = mout->spdif_maxbps;
5064 } else {
5065 mout->share_spdif = 0;
5066 /* FIXME: need notify? */
5067 }
Takashi Iwai9a081602008-02-12 18:37:26 +01005068 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02005069 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01005070 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071 return snd_pcm_hw_constraint_step(substream->runtime, 0,
5072 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
5073}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005074EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005075
Takashi Iwaid5191e52009-11-16 14:58:17 +01005076/**
5077 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
5078 *
5079 * Set up the i/o for analog out.
5080 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005082int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
5083 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084 unsigned int stream_tag,
5085 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01005086 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087{
Takashi Iwaidda14412011-05-02 11:29:30 +02005088 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089 int chs = substream->runtime->channels;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005090 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005091 int i;
5092
Ingo Molnar62932df2006-01-16 16:34:20 +01005093 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005094 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Takashi Iwai9a081602008-02-12 18:37:26 +01005095 if (mout->dig_out_nid && mout->share_spdif &&
5096 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02005098 snd_hda_is_supported_format(codec, mout->dig_out_nid,
5099 format) &&
Stephen Warren7c9359762011-06-01 11:14:17 -06005100 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005102 setup_dig_out_stream(codec, mout->dig_out_nid,
5103 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104 } else {
5105 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02005106 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107 }
5108 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005109 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005110
5111 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005112 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
5113 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005114 if (!mout->no_share_stream &&
5115 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005117 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
5118 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005119 /* extra outputs copied from front */
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005120 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5121 if (!mout->no_share_stream && mout->hp_out_nid[i])
5122 snd_hda_codec_setup_stream(codec,
5123 mout->hp_out_nid[i],
5124 stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005125 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02005126 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01005127 snd_hda_codec_setup_stream(codec,
5128 mout->extra_out_nid[i],
5129 stream_tag, 0, format);
5130
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131 /* surrounds */
5132 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02005133 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005134 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5135 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005136 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005137 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5138 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005139 }
5140 return 0;
5141}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005142EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005143
Takashi Iwaid5191e52009-11-16 14:58:17 +01005144/**
5145 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005147int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
5148 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149{
Takashi Iwaidda14412011-05-02 11:29:30 +02005150 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005151 int i;
5152
5153 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01005154 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01005156 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005157 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5158 if (mout->hp_out_nid[i])
5159 snd_hda_codec_cleanup_stream(codec,
5160 mout->hp_out_nid[i]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005161 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
5162 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01005163 snd_hda_codec_cleanup_stream(codec,
5164 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01005165 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02005167 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005168 mout->dig_out_used = 0;
5169 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005170 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005171 return 0;
5172}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005173EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174
Takashi Iwai47408602012-04-20 13:06:53 +02005175/**
5176 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
5177 *
5178 * Guess the suitable VREF pin bits to be set as the pin-control value.
5179 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
5180 */
5181unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
5182{
5183 unsigned int pincap;
5184 unsigned int oldval;
5185 oldval = snd_hda_codec_read(codec, pin, 0,
5186 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5187 pincap = snd_hda_query_pin_caps(codec, pin);
5188 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5189 /* Exception: if the default pin setup is vref50, we give it priority */
5190 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
5191 return AC_PINCTL_VREF_80;
5192 else if (pincap & AC_PINCAP_VREF_50)
5193 return AC_PINCTL_VREF_50;
5194 else if (pincap & AC_PINCAP_VREF_100)
5195 return AC_PINCTL_VREF_100;
5196 else if (pincap & AC_PINCAP_VREF_GRD)
5197 return AC_PINCTL_VREF_GRD;
5198 return AC_PINCTL_VREF_HIZ;
5199}
5200EXPORT_SYMBOL_HDA(snd_hda_get_default_vref);
5201
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005202int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
5203 unsigned int val, bool cached)
5204{
5205 if (val) {
5206 unsigned int cap = snd_hda_query_pin_caps(codec, pin);
Takashi Iwai6942c102012-04-20 13:08:40 +02005207 if (cap && (val & AC_PINCTL_OUT_EN)) {
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005208 if (!(cap & AC_PINCAP_OUT))
5209 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
5210 else if ((val & AC_PINCTL_HP_EN) &&
5211 !(cap & AC_PINCAP_HP_DRV))
5212 val &= ~AC_PINCTL_HP_EN;
5213 }
Takashi Iwai6942c102012-04-20 13:08:40 +02005214 if (cap && (val & AC_PINCTL_IN_EN)) {
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005215 if (!(cap & AC_PINCAP_IN))
5216 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
5217 }
5218 }
5219 if (cached)
5220 return snd_hda_codec_update_cache(codec, pin, 0,
5221 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5222 else
5223 return snd_hda_codec_write(codec, pin, 0,
5224 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5225}
5226EXPORT_SYMBOL_HDA(_snd_hda_set_pin_ctl);
5227
Takashi Iwai990061c2010-09-09 22:08:44 +02005228/**
5229 * snd_hda_add_imux_item - Add an item to input_mux
5230 *
5231 * When the same label is used already in the existing items, the number
5232 * suffix is appended to the label. This label index number is stored
5233 * to type_idx when non-NULL pointer is given.
5234 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02005235int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
5236 int index, int *type_idx)
5237{
5238 int i, label_idx = 0;
5239 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
5240 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
5241 return -EINVAL;
5242 }
5243 for (i = 0; i < imux->num_items; i++) {
5244 if (!strncmp(label, imux->items[i].label, strlen(label)))
5245 label_idx++;
5246 }
5247 if (type_idx)
5248 *type_idx = label_idx;
5249 if (label_idx > 0)
5250 snprintf(imux->items[imux->num_items].label,
5251 sizeof(imux->items[imux->num_items].label),
5252 "%s %d", label, label_idx);
5253 else
5254 strlcpy(imux->items[imux->num_items].label, label,
5255 sizeof(imux->items[imux->num_items].label));
5256 imux->items[imux->num_items].index = index;
5257 imux->num_items++;
5258 return 0;
5259}
5260EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005261
Takashi Iwai4a471b72005-12-07 13:56:29 +01005262
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263#ifdef CONFIG_PM
5264/*
5265 * power management
5266 */
5267
5268/**
5269 * snd_hda_suspend - suspend the codecs
5270 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005271 *
5272 * Returns 0 if successful.
5273 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02005274int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005275{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005276 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005277
Takashi Iwai0ba21762007-04-16 11:29:14 +02005278 list_for_each_entry(codec, &bus->codec_list, list) {
David Henningsson26a6cb62012-10-09 15:04:21 +02005279 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005280 if (hda_codec_is_power_on(codec))
Dylan Reidd17344b2012-09-28 15:57:01 -07005281 hda_call_codec_suspend(codec, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005282 }
5283 return 0;
5284}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005285EXPORT_SYMBOL_HDA(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005286
5287/**
5288 * snd_hda_resume - resume the codecs
5289 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005290 *
5291 * Returns 0 if successful.
5292 */
5293int snd_hda_resume(struct hda_bus *bus)
5294{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005295 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005296
Takashi Iwai0ba21762007-04-16 11:29:14 +02005297 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai7f308302012-05-08 16:52:23 +02005298 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005300 return 0;
5301}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005302EXPORT_SYMBOL_HDA(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005303#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005304
5305/*
5306 * generic arrays
5307 */
5308
Takashi Iwaid5191e52009-11-16 14:58:17 +01005309/**
5310 * snd_array_new - get a new element from the given array
5311 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005312 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01005313 * Get a new element from the given array. If it exceeds the
5314 * pre-allocated array size, re-allocate the array.
5315 *
5316 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02005317 */
5318void *snd_array_new(struct snd_array *array)
5319{
Takashi Iwai12f17712012-10-10 08:59:14 +02005320 if (snd_BUG_ON(!array->elem_size))
5321 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005322 if (array->used >= array->alloced) {
5323 int num = array->alloced + array->alloc_align;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005324 int size = (num + 1) * array->elem_size;
Takashi Iwai00ef9612011-07-14 15:57:27 +02005325 int oldsize = array->alloced * array->elem_size;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01005326 void *nlist;
5327 if (snd_BUG_ON(num >= 4096))
5328 return NULL;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005329 nlist = krealloc(array->list, size, GFP_KERNEL);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005330 if (!nlist)
5331 return NULL;
Takashi Iwai00ef9612011-07-14 15:57:27 +02005332 memset(nlist + oldsize, 0, size - oldsize);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005333 array->list = nlist;
5334 array->alloced = num;
5335 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01005336 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005337}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005338EXPORT_SYMBOL_HDA(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005339
Takashi Iwaid5191e52009-11-16 14:58:17 +01005340/**
5341 * snd_array_free - free the given array elements
5342 * @array: the array object
5343 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005344void snd_array_free(struct snd_array *array)
5345{
5346 kfree(array->list);
5347 array->used = 0;
5348 array->alloced = 0;
5349 array->list = NULL;
5350}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005351EXPORT_SYMBOL_HDA(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01005352
Takashi Iwaid5191e52009-11-16 14:58:17 +01005353/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01005354 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5355 * @pcm: PCM caps bits
5356 * @buf: the string buffer to write
5357 * @buflen: the max buffer length
5358 *
5359 * used by hda_proc.c and hda_eld.c
5360 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005361void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5362{
5363 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5364 int i, j;
5365
5366 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5367 if (pcm & (AC_SUPPCM_BITS_8 << i))
5368 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5369
5370 buf[j] = '\0'; /* necessary when j == 0 */
5371}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005372EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005373
5374MODULE_DESCRIPTION("HDA codec core");
5375MODULE_LICENSE("GPL");