blob: 2f890af820b2795dd190f1fa718c4f54d9c722aa [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 Warren7c935972011-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 Warren7c935972011-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 Iwaia68d5a542010-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 Iwaia68d5a542010-03-30 18:03:44 +02001596 return NULL;
1597}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
Takashi Iwaia68d5a542010-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 Iwaia68d5a542010-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,
1768 int direction, int index)
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);
1794 }
1795 return info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796}
1797
1798/*
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001799 * write the current volume in info to the h/w
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001801static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001802 hda_nid_t nid, int ch, int direction, int index,
1803 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804{
1805 u32 parm;
1806
1807 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1808 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1809 parm |= index << AC_AMP_SET_INDEX_SHIFT;
Takashi Iwai38681372012-02-27 15:00:58 +01001810 if ((val & HDA_AMP_MUTE) && !(info->amp_caps & AC_AMPCAP_MUTE) &&
1811 (info->amp_caps & AC_AMPCAP_MIN_MUTE))
1812 ; /* set the zero value as a fake mute */
1813 else
1814 parm |= val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
1816}
1817
Takashi Iwaid5191e52009-11-16 14:58:17 +01001818/**
1819 * snd_hda_codec_amp_read - Read AMP value
1820 * @codec: HD-audio codec
1821 * @nid: NID to read the AMP value
1822 * @ch: channel (left=0 or right=1)
1823 * @direction: #HDA_INPUT or #HDA_OUTPUT
1824 * @index: the index value (only for input direction)
1825 *
1826 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 */
Takashi Iwai834be882006-03-01 14:16:17 +01001828int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1829 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001831 struct hda_amp_info *info;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001832 unsigned int val = 0;
1833
1834 mutex_lock(&codec->hash_mutex);
1835 info = update_amp_hash(codec, nid, ch, direction, index);
1836 if (info)
1837 val = info->vol[ch];
1838 mutex_unlock(&codec->hash_mutex);
1839 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001841EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842
Takashi Iwaid5191e52009-11-16 14:58:17 +01001843/**
1844 * snd_hda_codec_amp_update - update the AMP value
1845 * @codec: HD-audio codec
1846 * @nid: NID to read the AMP value
1847 * @ch: channel (left=0 or right=1)
1848 * @direction: #HDA_INPUT or #HDA_OUTPUT
1849 * @idx: the index value (only for input direction)
1850 * @mask: bit mask to set
1851 * @val: the bits value to set
1852 *
1853 * Update the AMP value with a bit mask.
1854 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001855 */
Takashi Iwai834be882006-03-01 14:16:17 +01001856int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1857 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001859 struct hda_amp_info *info;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001860
Takashi Iwai467126462010-03-29 09:19:38 +02001861 if (snd_BUG_ON(mask & ~0xff))
1862 mask &= 0xff;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001863 val &= mask;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001864
1865 mutex_lock(&codec->hash_mutex);
1866 info = update_amp_hash(codec, nid, ch, direction, idx);
1867 if (!info) {
1868 mutex_unlock(&codec->hash_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 return 0;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001870 }
1871 val |= info->vol[ch] & ~mask;
1872 if (info->vol[ch] == val) {
1873 mutex_unlock(&codec->hash_mutex);
1874 return 0;
1875 }
1876 info->vol[ch] = val;
Takashi Iwaic370dd62012-12-13 18:30:04 +01001877 if (codec->cached_write)
1878 info->head.dirty = 1;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001879 mutex_unlock(&codec->hash_mutex);
Takashi Iwaic370dd62012-12-13 18:30:04 +01001880 if (!codec->cached_write)
1881 put_vol_mute(codec, info, nid, ch, direction, idx, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 return 1;
1883}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001884EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885
Takashi Iwaid5191e52009-11-16 14:58:17 +01001886/**
1887 * snd_hda_codec_amp_stereo - update the AMP stereo values
1888 * @codec: HD-audio codec
1889 * @nid: NID to read the AMP value
1890 * @direction: #HDA_INPUT or #HDA_OUTPUT
1891 * @idx: the index value (only for input direction)
1892 * @mask: bit mask to set
1893 * @val: the bits value to set
1894 *
1895 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1896 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02001897 */
1898int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1899 int direction, int idx, int mask, int val)
1900{
1901 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001902
1903 if (snd_BUG_ON(mask & ~0xff))
1904 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02001905 for (ch = 0; ch < 2; ch++)
1906 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1907 idx, mask, val);
1908 return ret;
1909}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001910EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001911
Takashi Iwaid5191e52009-11-16 14:58:17 +01001912/**
1913 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
1914 * @codec: HD-audio codec
1915 *
1916 * Resume the all amp commands from the cache.
1917 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001918void snd_hda_codec_resume_amp(struct hda_codec *codec)
1919{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001920 int i;
1921
Takashi Iwaic370dd62012-12-13 18:30:04 +01001922 mutex_lock(&codec->hash_mutex);
1923 for (i = 0; i < codec->amp_cache.buf.used; i++) {
1924 struct hda_amp_info *buffer;
1925 u32 key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001926 hda_nid_t nid;
1927 unsigned int idx, dir, ch;
Takashi Iwaic370dd62012-12-13 18:30:04 +01001928
1929 buffer = snd_array_elem(&codec->amp_cache.buf, i);
1930 key = buffer->head.key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001931 if (!key)
1932 continue;
1933 nid = key & 0xff;
1934 idx = (key >> 16) & 0xff;
1935 dir = (key >> 24) & 0xff;
1936 for (ch = 0; ch < 2; ch++) {
1937 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
1938 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01001939 if (!buffer->head.dirty)
1940 continue;
1941 buffer->head.dirty = 0;
1942 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001943 put_vol_mute(codec, buffer, nid, ch, dir, idx,
1944 buffer->vol[ch]);
Takashi Iwaic370dd62012-12-13 18:30:04 +01001945 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001946 }
1947 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01001948 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001949}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001950EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001952static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1953 unsigned int ofs)
1954{
1955 u32 caps = query_amp_caps(codec, nid, dir);
1956 /* get num steps */
1957 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1958 if (ofs < caps)
1959 caps -= ofs;
1960 return caps;
1961}
1962
Takashi Iwaid5191e52009-11-16 14:58:17 +01001963/**
1964 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
1965 *
1966 * The control element is supposed to have the private_value field
1967 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1968 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001969int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1970 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971{
1972 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1973 u16 nid = get_amp_nid(kcontrol);
1974 u8 chs = get_amp_channels(kcontrol);
1975 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001976 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001978 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1979 uinfo->count = chs == 3 ? 2 : 1;
1980 uinfo->value.integer.min = 0;
1981 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
1982 if (!uinfo->value.integer.max) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001983 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01001984 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
1985 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 return -EINVAL;
1987 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 return 0;
1989}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001990EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001992
1993static inline unsigned int
1994read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1995 int ch, int dir, int idx, unsigned int ofs)
1996{
1997 unsigned int val;
1998 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1999 val &= HDA_AMP_VOLMASK;
2000 if (val >= ofs)
2001 val -= ofs;
2002 else
2003 val = 0;
2004 return val;
2005}
2006
2007static inline int
2008update_amp_value(struct hda_codec *codec, hda_nid_t nid,
2009 int ch, int dir, int idx, unsigned int ofs,
2010 unsigned int val)
2011{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002012 unsigned int maxval;
2013
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002014 if (val > 0)
2015 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02002016 /* ofs = 0: raw max value */
2017 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002018 if (val > maxval)
2019 val = maxval;
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002020 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
2021 HDA_AMP_VOLMASK, val);
2022}
2023
Takashi Iwaid5191e52009-11-16 14:58:17 +01002024/**
2025 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
2026 *
2027 * The control element is supposed to have the private_value field
2028 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2029 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002030int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
2031 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032{
2033 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2034 hda_nid_t nid = get_amp_nid(kcontrol);
2035 int chs = get_amp_channels(kcontrol);
2036 int dir = get_amp_direction(kcontrol);
2037 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002038 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 long *valp = ucontrol->value.integer.value;
2040
2041 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002042 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002044 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 return 0;
2046}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002047EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048
Takashi Iwaid5191e52009-11-16 14:58:17 +01002049/**
2050 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
2051 *
2052 * The control element is supposed to have the private_value field
2053 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2054 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002055int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
2056 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057{
2058 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2059 hda_nid_t nid = get_amp_nid(kcontrol);
2060 int chs = get_amp_channels(kcontrol);
2061 int dir = get_amp_direction(kcontrol);
2062 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002063 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 long *valp = ucontrol->value.integer.value;
2065 int change = 0;
2066
Takashi Iwaicb53c622007-08-10 17:21:45 +02002067 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002068 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002069 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002070 valp++;
2071 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002072 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002073 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002074 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 return change;
2076}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002077EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078
Takashi Iwaid5191e52009-11-16 14:58:17 +01002079/**
2080 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2081 *
2082 * The control element is supposed to have the private_value field
2083 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2084 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002085int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2086 unsigned int size, unsigned int __user *_tlv)
2087{
2088 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2089 hda_nid_t nid = get_amp_nid(kcontrol);
2090 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002091 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02002092 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002093 u32 caps, val1, val2;
2094
2095 if (size < 4 * sizeof(unsigned int))
2096 return -ENOMEM;
2097 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002098 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2099 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002100 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002101 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002102 val1 = ((int)val1) * ((int)val2);
Takashi Iwai38681372012-02-27 15:00:58 +01002103 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwaic08d9162010-10-17 10:40:53 +02002104 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002105 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2106 return -EFAULT;
2107 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2108 return -EFAULT;
2109 if (put_user(val1, _tlv + 2))
2110 return -EFAULT;
2111 if (put_user(val2, _tlv + 3))
2112 return -EFAULT;
2113 return 0;
2114}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002115EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002116
Takashi Iwaid5191e52009-11-16 14:58:17 +01002117/**
2118 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2119 * @codec: HD-audio codec
2120 * @nid: NID of a reference widget
2121 * @dir: #HDA_INPUT or #HDA_OUTPUT
2122 * @tlv: TLV data to be stored, at least 4 elements
2123 *
2124 * Set (static) TLV data for a virtual master volume using the AMP caps
2125 * obtained from the reference NID.
2126 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01002127 */
2128void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2129 unsigned int *tlv)
2130{
2131 u32 caps;
2132 int nums, step;
2133
2134 caps = query_amp_caps(codec, nid, dir);
2135 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2136 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2137 step = (step + 1) * 25;
2138 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2139 tlv[1] = 2 * sizeof(unsigned int);
2140 tlv[2] = -nums * step;
2141 tlv[3] = step;
2142}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002143EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002144
2145/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01002146static struct snd_kcontrol *
Takashi Iwaidcda5802012-10-12 17:24:51 +02002147find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002148{
2149 struct snd_ctl_elem_id id;
2150 memset(&id, 0, sizeof(id));
2151 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwaidcda5802012-10-12 17:24:51 +02002152 id.device = dev;
Takashi Iwai09f99702008-02-04 12:31:13 +01002153 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02002154 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2155 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002156 strcpy(id.name, name);
2157 return snd_ctl_find_id(codec->bus->card, &id);
2158}
2159
Takashi Iwaid5191e52009-11-16 14:58:17 +01002160/**
2161 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2162 * @codec: HD-audio codec
2163 * @name: ctl id name string
2164 *
2165 * Get the control element with the given id string and IFACE_MIXER.
2166 */
Takashi Iwai09f99702008-02-04 12:31:13 +01002167struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2168 const char *name)
2169{
Takashi Iwaidcda5802012-10-12 17:24:51 +02002170 return find_mixer_ctl(codec, name, 0, 0);
Takashi Iwai09f99702008-02-04 12:31:13 +01002171}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002172EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01002173
Takashi Iwaidcda5802012-10-12 17:24:51 +02002174static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
2175 int dev)
Takashi Iwai1afe2062010-12-23 10:17:52 +01002176{
2177 int idx;
2178 for (idx = 0; idx < 16; idx++) { /* 16 ctlrs should be large enough */
Takashi Iwaidcda5802012-10-12 17:24:51 +02002179 if (!find_mixer_ctl(codec, name, dev, idx))
Takashi Iwai1afe2062010-12-23 10:17:52 +01002180 return idx;
2181 }
2182 return -EBUSY;
2183}
2184
Takashi Iwaid5191e52009-11-16 14:58:17 +01002185/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002186 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01002187 * @codec: HD-audio codec
2188 * @nid: corresponding NID (optional)
2189 * @kctl: the control element to assign
2190 *
2191 * Add the given control element to an array inside the codec instance.
2192 * All control elements belonging to a codec are supposed to be added
2193 * by this function so that a proper clean-up works at the free or
2194 * reconfiguration time.
2195 *
2196 * If non-zero @nid is passed, the NID is assigned to the control element.
2197 * The assignment is shown in the codec proc file.
2198 *
2199 * snd_hda_ctl_add() checks the control subdev id field whether
2200 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002201 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2202 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01002203 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002204int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2205 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002206{
2207 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002208 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002209 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002210
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002211 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002212 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002213 if (nid == 0)
2214 nid = get_amp_nid_(kctl->private_value);
2215 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002216 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2217 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002218 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01002219 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002220 err = snd_ctl_add(codec->bus->card, kctl);
2221 if (err < 0)
2222 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002223 item = snd_array_new(&codec->mixers);
2224 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002225 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002226 item->kctl = kctl;
2227 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002228 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002229 return 0;
2230}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002231EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002232
Takashi Iwaid5191e52009-11-16 14:58:17 +01002233/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002234 * snd_hda_add_nid - Assign a NID to a control element
2235 * @codec: HD-audio codec
2236 * @nid: corresponding NID (optional)
2237 * @kctl: the control element to assign
2238 * @index: index to kctl
2239 *
2240 * Add the given control element to an array inside the codec instance.
2241 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2242 * NID:KCTL mapping - for example "Capture Source" selector.
2243 */
2244int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2245 unsigned int index, hda_nid_t nid)
2246{
2247 struct hda_nid_item *item;
2248
2249 if (nid > 0) {
2250 item = snd_array_new(&codec->nids);
2251 if (!item)
2252 return -ENOMEM;
2253 item->kctl = kctl;
2254 item->index = index;
2255 item->nid = nid;
2256 return 0;
2257 }
Takashi Iwai28d1a852010-03-15 09:05:46 +01002258 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
2259 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002260 return -EINVAL;
2261}
2262EXPORT_SYMBOL_HDA(snd_hda_add_nid);
2263
2264/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002265 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2266 * @codec: HD-audio codec
2267 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002268void snd_hda_ctls_clear(struct hda_codec *codec)
2269{
2270 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002271 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002272 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002273 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002274 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002275 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002276}
2277
Takashi Iwaia65d6292009-02-23 16:57:04 +01002278/* pseudo device locking
2279 * toggle card->shutdown to allow/disallow the device access (as a hack)
2280 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002281int snd_hda_lock_devices(struct hda_bus *bus)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002282{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002283 struct snd_card *card = bus->card;
2284 struct hda_codec *codec;
2285
Takashi Iwaia65d6292009-02-23 16:57:04 +01002286 spin_lock(&card->files_lock);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002287 if (card->shutdown)
2288 goto err_unlock;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002289 card->shutdown = 1;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002290 if (!list_empty(&card->ctl_files))
2291 goto err_clear;
2292
2293 list_for_each_entry(codec, &bus->codec_list, list) {
2294 int pcm;
2295 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2296 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2297 if (!cpcm->pcm)
2298 continue;
2299 if (cpcm->pcm->streams[0].substream_opened ||
2300 cpcm->pcm->streams[1].substream_opened)
2301 goto err_clear;
2302 }
2303 }
Takashi Iwaia65d6292009-02-23 16:57:04 +01002304 spin_unlock(&card->files_lock);
2305 return 0;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002306
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002307 err_clear:
2308 card->shutdown = 0;
2309 err_unlock:
2310 spin_unlock(&card->files_lock);
2311 return -EINVAL;
2312}
2313EXPORT_SYMBOL_HDA(snd_hda_lock_devices);
2314
2315void snd_hda_unlock_devices(struct hda_bus *bus)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002316{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002317 struct snd_card *card = bus->card;
2318
2319 card = bus->card;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002320 spin_lock(&card->files_lock);
2321 card->shutdown = 0;
2322 spin_unlock(&card->files_lock);
2323}
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002324EXPORT_SYMBOL_HDA(snd_hda_unlock_devices);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002325
Takashi Iwaid5191e52009-11-16 14:58:17 +01002326/**
2327 * snd_hda_codec_reset - Clear all objects assigned to the codec
2328 * @codec: HD-audio codec
2329 *
2330 * This frees the all PCM and control elements assigned to the codec, and
2331 * clears the caches and restores the pin default configurations.
2332 *
2333 * When a device is being used, it returns -EBSY. If successfully freed,
2334 * returns zero.
2335 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002336int snd_hda_codec_reset(struct hda_codec *codec)
2337{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002338 struct hda_bus *bus = codec->bus;
2339 struct snd_card *card = bus->card;
2340 int i;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002341
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002342 if (snd_hda_lock_devices(bus) < 0)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002343 return -EBUSY;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002344
2345 /* OK, let it free */
David Henningsson26a6cb62012-10-09 15:04:21 +02002346 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai83012a72012-08-24 18:38:08 +02002347#ifdef CONFIG_PM
Takashi Iwaia2d96e72012-05-09 12:36:22 +02002348 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwai339876d2012-05-08 16:57:12 +02002349 codec->power_on = 0;
2350 codec->power_transition = 0;
2351 codec->power_jiffies = jiffies;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002352 flush_workqueue(bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002353#endif
2354 snd_hda_ctls_clear(codec);
2355 /* relase PCMs */
2356 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002357 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01002358 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002359 clear_bit(codec->pcm_info[i].device,
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002360 bus->pcm_dev_bits);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002361 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002362 }
2363 if (codec->patch_ops.free)
2364 codec->patch_ops.free(codec);
Takashi Iwai07dc59f2012-09-10 09:39:31 +02002365 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
Takashi Iwai1835a0f2011-10-27 22:12:46 +02002366 snd_hda_jack_tbl_clear(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01002367 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002368 codec->spec = NULL;
2369 free_hda_cache(&codec->amp_cache);
2370 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01002371 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2372 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01002373 /* free only driver_pins so that init_pins + user_pins are restored */
2374 snd_array_free(&codec->driver_pins);
Takashi Iwai09a60712012-06-26 15:01:33 +02002375 snd_array_free(&codec->cvt_setups);
2376 snd_array_free(&codec->spdif_out);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002377 codec->num_pcms = 0;
2378 codec->pcm_info = NULL;
2379 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01002380 codec->slave_dig_outs = NULL;
2381 codec->spdif_status_reset = 0;
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002382 module_put(codec->owner);
2383 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002384
2385 /* allow device access again */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002386 snd_hda_unlock_devices(bus);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002387 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002388}
2389
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002390typedef int (*map_slave_func_t)(void *, struct snd_kcontrol *);
2391
2392/* apply the function to all matching slave ctls in the mixer list */
2393static int map_slaves(struct hda_codec *codec, const char * const *slaves,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002394 const char *suffix, map_slave_func_t func, void *data)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002395{
2396 struct hda_nid_item *items;
2397 const char * const *s;
2398 int i, err;
2399
2400 items = codec->mixers.list;
2401 for (i = 0; i < codec->mixers.used; i++) {
2402 struct snd_kcontrol *sctl = items[i].kctl;
2403 if (!sctl || !sctl->id.name ||
2404 sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
2405 continue;
2406 for (s = slaves; *s; s++) {
Takashi Iwai9322ca52012-02-03 14:28:01 +01002407 char tmpname[sizeof(sctl->id.name)];
2408 const char *name = *s;
2409 if (suffix) {
2410 snprintf(tmpname, sizeof(tmpname), "%s %s",
2411 name, suffix);
2412 name = tmpname;
2413 }
Takashi Iwai9322ca52012-02-03 14:28:01 +01002414 if (!strcmp(sctl->id.name, name)) {
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002415 err = func(data, sctl);
2416 if (err)
2417 return err;
2418 break;
2419 }
2420 }
2421 }
2422 return 0;
2423}
2424
2425static int check_slave_present(void *data, struct snd_kcontrol *sctl)
2426{
2427 return 1;
2428}
2429
Takashi Iwai18478e82012-03-09 17:51:10 +01002430/* guess the value corresponding to 0dB */
2431static int get_kctl_0dB_offset(struct snd_kcontrol *kctl)
2432{
2433 int _tlv[4];
2434 const int *tlv = NULL;
2435 int val = -1;
2436
2437 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2438 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2439 mm_segment_t fs = get_fs();
2440 set_fs(get_ds());
2441 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2442 tlv = _tlv;
2443 set_fs(fs);
2444 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2445 tlv = kctl->tlv.p;
2446 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE)
2447 val = -tlv[2] / tlv[3];
2448 return val;
2449}
2450
2451/* call kctl->put with the given value(s) */
2452static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2453{
2454 struct snd_ctl_elem_value *ucontrol;
2455 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2456 if (!ucontrol)
2457 return -ENOMEM;
2458 ucontrol->value.integer.value[0] = val;
2459 ucontrol->value.integer.value[1] = val;
2460 kctl->put(kctl, ucontrol);
2461 kfree(ucontrol);
2462 return 0;
2463}
2464
2465/* initialize the slave volume with 0dB */
2466static int init_slave_0dB(void *data, struct snd_kcontrol *slave)
2467{
2468 int offset = get_kctl_0dB_offset(slave);
2469 if (offset > 0)
2470 put_kctl_with_value(slave, offset);
2471 return 0;
2472}
2473
2474/* unmute the slave */
2475static int init_slave_unmute(void *data, struct snd_kcontrol *slave)
2476{
2477 return put_kctl_with_value(slave, 1);
2478}
2479
Takashi Iwaid5191e52009-11-16 14:58:17 +01002480/**
2481 * snd_hda_add_vmaster - create a virtual master control and add slaves
2482 * @codec: HD-audio codec
2483 * @name: vmaster control name
2484 * @tlv: TLV data (optional)
2485 * @slaves: slave control names (optional)
Takashi Iwai9322ca52012-02-03 14:28:01 +01002486 * @suffix: suffix string to each slave name (optional)
Takashi Iwai18478e82012-03-09 17:51:10 +01002487 * @init_slave_vol: initialize slaves to unmute/0dB
Takashi Iwai29e58532012-03-12 12:25:03 +01002488 * @ctl_ret: store the vmaster kcontrol in return
Takashi Iwaid5191e52009-11-16 14:58:17 +01002489 *
2490 * Create a virtual master control with the given name. The TLV data
2491 * must be either NULL or a valid data.
2492 *
2493 * @slaves is a NULL-terminated array of strings, each of which is a
2494 * slave control name. All controls with these names are assigned to
2495 * the new virtual master control.
2496 *
2497 * This function returns zero if successful or a negative error code.
2498 */
Takashi Iwai18478e82012-03-09 17:51:10 +01002499int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002500 unsigned int *tlv, const char * const *slaves,
Takashi Iwai29e58532012-03-12 12:25:03 +01002501 const char *suffix, bool init_slave_vol,
2502 struct snd_kcontrol **ctl_ret)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002503{
2504 struct snd_kcontrol *kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002505 int err;
2506
Takashi Iwai29e58532012-03-12 12:25:03 +01002507 if (ctl_ret)
2508 *ctl_ret = NULL;
2509
Takashi Iwai9322ca52012-02-03 14:28:01 +01002510 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002511 if (err != 1) {
Takashi Iwai2f085542008-02-22 18:43:50 +01002512 snd_printdd("No slave found for %s\n", name);
2513 return 0;
2514 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002515 kctl = snd_ctl_make_virtual_master(name, tlv);
2516 if (!kctl)
2517 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002518 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002519 if (err < 0)
2520 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002521
Takashi Iwai9322ca52012-02-03 14:28:01 +01002522 err = map_slaves(codec, slaves, suffix,
2523 (map_slave_func_t)snd_ctl_add_slave, kctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002524 if (err < 0)
2525 return err;
Takashi Iwai18478e82012-03-09 17:51:10 +01002526
2527 /* init with master mute & zero volume */
2528 put_kctl_with_value(kctl, 0);
2529 if (init_slave_vol)
2530 map_slaves(codec, slaves, suffix,
2531 tlv ? init_slave_0dB : init_slave_unmute, kctl);
2532
Takashi Iwai29e58532012-03-12 12:25:03 +01002533 if (ctl_ret)
2534 *ctl_ret = kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002535 return 0;
2536}
Takashi Iwai18478e82012-03-09 17:51:10 +01002537EXPORT_SYMBOL_HDA(__snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002538
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002539/*
2540 * mute-LED control using vmaster
2541 */
2542static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2543 struct snd_ctl_elem_info *uinfo)
2544{
2545 static const char * const texts[] = {
David Henningssonc86c2d42013-01-03 14:12:29 +01002546 "On", "Off", "Follow Master"
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002547 };
2548 unsigned int index;
2549
2550 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2551 uinfo->count = 1;
2552 uinfo->value.enumerated.items = 3;
2553 index = uinfo->value.enumerated.item;
2554 if (index >= 3)
2555 index = 2;
2556 strcpy(uinfo->value.enumerated.name, texts[index]);
2557 return 0;
2558}
2559
2560static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2561 struct snd_ctl_elem_value *ucontrol)
2562{
2563 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2564 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2565 return 0;
2566}
2567
2568static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2569 struct snd_ctl_elem_value *ucontrol)
2570{
2571 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2572 unsigned int old_mode = hook->mute_mode;
2573
2574 hook->mute_mode = ucontrol->value.enumerated.item[0];
2575 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2576 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2577 if (old_mode == hook->mute_mode)
2578 return 0;
2579 snd_hda_sync_vmaster_hook(hook);
2580 return 1;
2581}
2582
2583static struct snd_kcontrol_new vmaster_mute_mode = {
2584 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2585 .name = "Mute-LED Mode",
2586 .info = vmaster_mute_mode_info,
2587 .get = vmaster_mute_mode_get,
2588 .put = vmaster_mute_mode_put,
2589};
2590
2591/*
2592 * Add a mute-LED hook with the given vmaster switch kctl
2593 * "Mute-LED Mode" control is automatically created and associated with
2594 * the given hook.
2595 */
2596int snd_hda_add_vmaster_hook(struct hda_codec *codec,
Takashi Iwaif29735cb2012-03-13 07:55:10 +01002597 struct hda_vmaster_mute_hook *hook,
2598 bool expose_enum_ctl)
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002599{
2600 struct snd_kcontrol *kctl;
2601
2602 if (!hook->hook || !hook->sw_kctl)
2603 return 0;
2604 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
2605 hook->codec = codec;
2606 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
Takashi Iwaif29735cb2012-03-13 07:55:10 +01002607 if (!expose_enum_ctl)
2608 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002609 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2610 if (!kctl)
2611 return -ENOMEM;
2612 return snd_hda_ctl_add(codec, 0, kctl);
2613}
2614EXPORT_SYMBOL_HDA(snd_hda_add_vmaster_hook);
2615
2616/*
2617 * Call the hook with the current value for synchronization
2618 * Should be called in init callback
2619 */
2620void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2621{
2622 if (!hook->hook || !hook->codec)
2623 return;
2624 switch (hook->mute_mode) {
2625 case HDA_VMUTE_FOLLOW_MASTER:
2626 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2627 break;
2628 default:
2629 hook->hook(hook->codec, hook->mute_mode);
2630 break;
2631 }
2632}
2633EXPORT_SYMBOL_HDA(snd_hda_sync_vmaster_hook);
2634
2635
Takashi Iwaid5191e52009-11-16 14:58:17 +01002636/**
2637 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2638 *
2639 * The control element is supposed to have the private_value field
2640 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2641 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002642int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2643 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644{
2645 int chs = get_amp_channels(kcontrol);
2646
2647 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2648 uinfo->count = chs == 3 ? 2 : 1;
2649 uinfo->value.integer.min = 0;
2650 uinfo->value.integer.max = 1;
2651 return 0;
2652}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002653EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654
Takashi Iwaid5191e52009-11-16 14:58:17 +01002655/**
2656 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2657 *
2658 * The control element is supposed to have the private_value field
2659 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2660 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002661int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2662 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663{
2664 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2665 hda_nid_t nid = get_amp_nid(kcontrol);
2666 int chs = get_amp_channels(kcontrol);
2667 int dir = get_amp_direction(kcontrol);
2668 int idx = get_amp_index(kcontrol);
2669 long *valp = ucontrol->value.integer.value;
2670
2671 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002672 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002673 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002675 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002676 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 return 0;
2678}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002679EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680
Takashi Iwaid5191e52009-11-16 14:58:17 +01002681/**
2682 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2683 *
2684 * The control element is supposed to have the private_value field
2685 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2686 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002687int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2688 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689{
2690 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2691 hda_nid_t nid = get_amp_nid(kcontrol);
2692 int chs = get_amp_channels(kcontrol);
2693 int dir = get_amp_direction(kcontrol);
2694 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 long *valp = ucontrol->value.integer.value;
2696 int change = 0;
2697
Takashi Iwaicb53c622007-08-10 17:21:45 +02002698 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002699 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002700 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002701 HDA_AMP_MUTE,
2702 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002703 valp++;
2704 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002705 if (chs & 2)
2706 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002707 HDA_AMP_MUTE,
2708 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002709 hda_call_check_power_status(codec, nid);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002710 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 return change;
2712}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002713EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714
2715/*
Takashi Iwai985be542005-11-02 18:26:49 +01002716 * bound volume controls
2717 *
2718 * bind multiple volumes (# indices, from 0)
2719 */
2720
2721#define AMP_VAL_IDX_SHIFT 19
2722#define AMP_VAL_IDX_MASK (0x0f<<19)
2723
Takashi Iwaid5191e52009-11-16 14:58:17 +01002724/**
2725 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2726 *
2727 * The control element is supposed to have the private_value field
2728 * set up via HDA_BIND_MUTE*() macros.
2729 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002730int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2731 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002732{
2733 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2734 unsigned long pval;
2735 int err;
2736
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002737 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002738 pval = kcontrol->private_value;
2739 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2740 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2741 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002742 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002743 return err;
2744}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002745EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002746
Takashi Iwaid5191e52009-11-16 14:58:17 +01002747/**
2748 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2749 *
2750 * The control element is supposed to have the private_value field
2751 * set up via HDA_BIND_MUTE*() macros.
2752 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002753int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2754 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002755{
2756 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2757 unsigned long pval;
2758 int i, indices, err = 0, change = 0;
2759
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002760 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002761 pval = kcontrol->private_value;
2762 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2763 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002764 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2765 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002766 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2767 if (err < 0)
2768 break;
2769 change |= err;
2770 }
2771 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002772 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002773 return err < 0 ? err : change;
2774}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002775EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002776
Takashi Iwaid5191e52009-11-16 14:58:17 +01002777/**
2778 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2779 *
2780 * The control element is supposed to have the private_value field
2781 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002782 */
2783int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2784 struct snd_ctl_elem_info *uinfo)
2785{
2786 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2787 struct hda_bind_ctls *c;
2788 int err;
2789
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002790 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002791 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002792 kcontrol->private_value = *c->values;
2793 err = c->ops->info(kcontrol, uinfo);
2794 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002795 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002796 return err;
2797}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002798EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002799
Takashi Iwaid5191e52009-11-16 14:58:17 +01002800/**
2801 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2802 *
2803 * The control element is supposed to have the private_value field
2804 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2805 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002806int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2807 struct snd_ctl_elem_value *ucontrol)
2808{
2809 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2810 struct hda_bind_ctls *c;
2811 int err;
2812
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002813 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002814 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002815 kcontrol->private_value = *c->values;
2816 err = c->ops->get(kcontrol, ucontrol);
2817 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002818 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002819 return err;
2820}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002821EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002822
Takashi Iwaid5191e52009-11-16 14:58:17 +01002823/**
2824 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2825 *
2826 * The control element is supposed to have the private_value field
2827 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2828 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002829int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2830 struct snd_ctl_elem_value *ucontrol)
2831{
2832 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2833 struct hda_bind_ctls *c;
2834 unsigned long *vals;
2835 int err = 0, change = 0;
2836
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002837 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002838 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002839 for (vals = c->values; *vals; vals++) {
2840 kcontrol->private_value = *vals;
2841 err = c->ops->put(kcontrol, ucontrol);
2842 if (err < 0)
2843 break;
2844 change |= err;
2845 }
2846 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002847 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002848 return err < 0 ? err : change;
2849}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002850EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02002851
Takashi Iwaid5191e52009-11-16 14:58:17 +01002852/**
2853 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2854 *
2855 * The control element is supposed to have the private_value field
2856 * set up via HDA_BIND_VOL() macro.
2857 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002858int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2859 unsigned int size, unsigned int __user *tlv)
2860{
2861 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2862 struct hda_bind_ctls *c;
2863 int err;
2864
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002865 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002866 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002867 kcontrol->private_value = *c->values;
2868 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2869 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002870 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002871 return err;
2872}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002873EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02002874
2875struct hda_ctl_ops snd_hda_bind_vol = {
2876 .info = snd_hda_mixer_amp_volume_info,
2877 .get = snd_hda_mixer_amp_volume_get,
2878 .put = snd_hda_mixer_amp_volume_put,
2879 .tlv = snd_hda_mixer_amp_tlv
2880};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002881EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02002882
2883struct hda_ctl_ops snd_hda_bind_sw = {
2884 .info = snd_hda_mixer_amp_switch_info,
2885 .get = snd_hda_mixer_amp_switch_get,
2886 .put = snd_hda_mixer_amp_switch_put,
2887 .tlv = snd_hda_mixer_amp_tlv
2888};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002889EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02002890
2891/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 * SPDIF out controls
2893 */
2894
Takashi Iwai0ba21762007-04-16 11:29:14 +02002895static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2896 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897{
2898 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2899 uinfo->count = 1;
2900 return 0;
2901}
2902
Takashi Iwai0ba21762007-04-16 11:29:14 +02002903static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2904 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905{
2906 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2907 IEC958_AES0_NONAUDIO |
2908 IEC958_AES0_CON_EMPHASIS_5015 |
2909 IEC958_AES0_CON_NOT_COPYRIGHT;
2910 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2911 IEC958_AES1_CON_ORIGINAL;
2912 return 0;
2913}
2914
Takashi Iwai0ba21762007-04-16 11:29:14 +02002915static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2916 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917{
2918 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2919 IEC958_AES0_NONAUDIO |
2920 IEC958_AES0_PRO_EMPHASIS_5015;
2921 return 0;
2922}
2923
Takashi Iwai0ba21762007-04-16 11:29:14 +02002924static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2925 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926{
2927 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06002928 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002929 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002931 mutex_lock(&codec->spdif_mutex);
2932 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06002933 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
2934 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
2935 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
2936 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002937 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938
2939 return 0;
2940}
2941
2942/* convert from SPDIF status bits to HDA SPDIF bits
2943 * bit 0 (DigEn) is always set zero (to be filled later)
2944 */
2945static unsigned short convert_from_spdif_status(unsigned int sbits)
2946{
2947 unsigned short val = 0;
2948
2949 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002950 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002952 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002954 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2955 IEC958_AES0_PRO_EMPHASIS_5015)
2956 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002958 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2959 IEC958_AES0_CON_EMPHASIS_5015)
2960 val |= AC_DIG1_EMPHASIS;
2961 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2962 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02002964 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2966 }
2967 return val;
2968}
2969
2970/* convert to SPDIF status bits from HDA SPDIF bits
2971 */
2972static unsigned int convert_to_spdif_status(unsigned short val)
2973{
2974 unsigned int sbits = 0;
2975
Takashi Iwai0ba21762007-04-16 11:29:14 +02002976 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002978 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 sbits |= IEC958_AES0_PROFESSIONAL;
2980 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002981 if (sbits & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2983 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002984 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002986 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002988 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2990 sbits |= val & (0x7f << 8);
2991 }
2992 return sbits;
2993}
2994
Takashi Iwai2f728532008-09-25 16:32:41 +02002995/* set digital convert verbs both for the given NID and its slaves */
2996static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
2997 int verb, int val)
2998{
Takashi Iwaidda14412011-05-02 11:29:30 +02002999 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02003000
Takashi Iwai9e976972008-11-25 08:17:20 +01003001 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003002 d = codec->slave_dig_outs;
3003 if (!d)
3004 return;
3005 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01003006 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003007}
3008
3009static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
3010 int dig1, int dig2)
3011{
3012 if (dig1 != -1)
3013 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
3014 if (dig2 != -1)
3015 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
3016}
3017
Takashi Iwai0ba21762007-04-16 11:29:14 +02003018static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
3019 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020{
3021 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003022 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003023 struct hda_spdif_out *spdif;
3024 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 unsigned short val;
3026 int change;
3027
Ingo Molnar62932df2006-01-16 16:34:20 +01003028 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003029 spdif = snd_array_elem(&codec->spdif_out, idx);
3030 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06003031 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
3033 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
3034 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c935972011-06-01 11:14:17 -06003035 val = convert_from_spdif_status(spdif->status);
3036 val |= spdif->ctls & 1;
3037 change = spdif->ctls != val;
3038 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003039 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02003040 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01003041 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 return change;
3043}
3044
Takashi Iwaia5ce8892007-07-23 15:42:26 +02003045#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046
Takashi Iwai0ba21762007-04-16 11:29:14 +02003047static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
3048 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049{
3050 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003051 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003052 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003054 mutex_lock(&codec->spdif_mutex);
3055 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06003056 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003057 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 return 0;
3059}
3060
Stephen Warren74b654c2011-06-01 11:14:18 -06003061static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
3062 int dig1, int dig2)
3063{
3064 set_dig_out_convert(codec, nid, dig1, dig2);
3065 /* unmute amp switch (if any) */
3066 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
3067 (dig1 & AC_DIG1_ENABLE))
3068 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
3069 HDA_AMP_MUTE, 0);
3070}
3071
Takashi Iwai0ba21762007-04-16 11:29:14 +02003072static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
3073 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074{
3075 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003076 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003077 struct hda_spdif_out *spdif;
3078 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 unsigned short val;
3080 int change;
3081
Ingo Molnar62932df2006-01-16 16:34:20 +01003082 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003083 spdif = snd_array_elem(&codec->spdif_out, idx);
3084 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06003085 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02003087 val |= AC_DIG1_ENABLE;
Stephen Warren7c935972011-06-01 11:14:17 -06003088 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003089 spdif->ctls = val;
3090 if (change && nid != (u16)-1)
3091 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01003092 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 return change;
3094}
3095
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003096static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 {
3098 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3099 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003100 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 .info = snd_hda_spdif_mask_info,
3102 .get = snd_hda_spdif_cmask_get,
3103 },
3104 {
3105 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3106 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003107 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 .info = snd_hda_spdif_mask_info,
3109 .get = snd_hda_spdif_pmask_get,
3110 },
3111 {
3112 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003113 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 .info = snd_hda_spdif_mask_info,
3115 .get = snd_hda_spdif_default_get,
3116 .put = snd_hda_spdif_default_put,
3117 },
3118 {
3119 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003120 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 .info = snd_hda_spdif_out_switch_info,
3122 .get = snd_hda_spdif_out_switch_get,
3123 .put = snd_hda_spdif_out_switch_put,
3124 },
3125 { } /* end */
3126};
3127
3128/**
Takashi Iwaidcda5802012-10-12 17:24:51 +02003129 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 * @codec: the HDA codec
Takashi Iwaidcda5802012-10-12 17:24:51 +02003131 * @associated_nid: NID that new ctls associated with
3132 * @cvt_nid: converter NID
3133 * @type: HDA_PCM_TYPE_*
3134 * Creates controls related with the digital output.
3135 * Called from each patch supporting the digital out.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 *
3137 * Returns 0 if successful, or a negative error code.
3138 */
Takashi Iwaidcda5802012-10-12 17:24:51 +02003139int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
3140 hda_nid_t associated_nid,
3141 hda_nid_t cvt_nid,
3142 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143{
3144 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003145 struct snd_kcontrol *kctl;
3146 struct snd_kcontrol_new *dig_mix;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003147 int idx, dev = 0;
3148 const int spdif_pcm_dev = 1;
Stephen Warren7c935972011-06-01 11:14:17 -06003149 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150
Takashi Iwaidcda5802012-10-12 17:24:51 +02003151 if (codec->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
3152 type == HDA_PCM_TYPE_SPDIF) {
3153 dev = spdif_pcm_dev;
3154 } else if (codec->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
3155 type == HDA_PCM_TYPE_HDMI) {
3156 for (idx = 0; idx < codec->spdif_out.used; idx++) {
3157 spdif = snd_array_elem(&codec->spdif_out, idx);
3158 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3159 kctl = find_mixer_ctl(codec, dig_mix->name, 0, idx);
3160 if (!kctl)
3161 break;
3162 kctl->id.device = spdif_pcm_dev;
3163 }
3164 }
3165 codec->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
3166 }
3167 if (!codec->primary_dig_out_type)
3168 codec->primary_dig_out_type = type;
3169
3170 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", dev);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003171 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003172 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
3173 return -EBUSY;
3174 }
Stephen Warren7c935972011-06-01 11:14:17 -06003175 spdif = snd_array_new(&codec->spdif_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3177 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01003178 if (!kctl)
3179 return -ENOMEM;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003180 kctl->id.device = dev;
Takashi Iwai09f99702008-02-04 12:31:13 +01003181 kctl->id.index = idx;
Stephen Warren7c935972011-06-01 11:14:17 -06003182 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06003183 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003184 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 return err;
3186 }
Stephen Warren74b654c2011-06-01 11:14:18 -06003187 spdif->nid = cvt_nid;
3188 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
Stephen Warren7c935972011-06-01 11:14:17 -06003189 AC_VERB_GET_DIGI_CONVERT_1, 0);
3190 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 return 0;
3192}
Takashi Iwaidcda5802012-10-12 17:24:51 +02003193EXPORT_SYMBOL_HDA(snd_hda_create_dig_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003195/* get the hda_spdif_out entry from the given NID
3196 * call within spdif_mutex lock
3197 */
Stephen Warren7c935972011-06-01 11:14:17 -06003198struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3199 hda_nid_t nid)
3200{
3201 int i;
3202 for (i = 0; i < codec->spdif_out.used; i++) {
3203 struct hda_spdif_out *spdif =
3204 snd_array_elem(&codec->spdif_out, i);
3205 if (spdif->nid == nid)
3206 return spdif;
3207 }
3208 return NULL;
3209}
3210EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid);
3211
Stephen Warren74b654c2011-06-01 11:14:18 -06003212void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3213{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003214 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003215
3216 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003217 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003218 spdif->nid = (u16)-1;
3219 mutex_unlock(&codec->spdif_mutex);
3220}
3221EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign);
3222
3223void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3224{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003225 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003226 unsigned short val;
3227
3228 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003229 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003230 if (spdif->nid != nid) {
3231 spdif->nid = nid;
3232 val = spdif->ctls;
3233 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3234 }
3235 mutex_unlock(&codec->spdif_mutex);
3236}
3237EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign);
3238
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239/*
Takashi Iwai9a081602008-02-12 18:37:26 +01003240 * SPDIF sharing with analog output
3241 */
3242static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3243 struct snd_ctl_elem_value *ucontrol)
3244{
3245 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3246 ucontrol->value.integer.value[0] = mout->share_spdif;
3247 return 0;
3248}
3249
3250static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3251 struct snd_ctl_elem_value *ucontrol)
3252{
3253 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3254 mout->share_spdif = !!ucontrol->value.integer.value[0];
3255 return 0;
3256}
3257
3258static struct snd_kcontrol_new spdif_share_sw = {
3259 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3260 .name = "IEC958 Default PCM Playback Switch",
3261 .info = snd_ctl_boolean_mono_info,
3262 .get = spdif_share_sw_get,
3263 .put = spdif_share_sw_put,
3264};
3265
Takashi Iwaid5191e52009-11-16 14:58:17 +01003266/**
3267 * snd_hda_create_spdif_share_sw - create Default PCM switch
3268 * @codec: the HDA codec
3269 * @mout: multi-out instance
3270 */
Takashi Iwai9a081602008-02-12 18:37:26 +01003271int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3272 struct hda_multi_out *mout)
3273{
3274 if (!mout->dig_out_nid)
3275 return 0;
3276 /* ATTENTION: here mout is passed as private_data, instead of codec */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003277 return snd_hda_ctl_add(codec, mout->dig_out_nid,
3278 snd_ctl_new1(&spdif_share_sw, mout));
Takashi Iwai9a081602008-02-12 18:37:26 +01003279}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003280EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01003281
3282/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 * SPDIF input
3284 */
3285
3286#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3287
Takashi Iwai0ba21762007-04-16 11:29:14 +02003288static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3289 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290{
3291 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3292
3293 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3294 return 0;
3295}
3296
Takashi Iwai0ba21762007-04-16 11:29:14 +02003297static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3298 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299{
3300 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3301 hda_nid_t nid = kcontrol->private_value;
3302 unsigned int val = !!ucontrol->value.integer.value[0];
3303 int change;
3304
Ingo Molnar62932df2006-01-16 16:34:20 +01003305 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003307 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003309 snd_hda_codec_write_cache(codec, nid, 0,
3310 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003312 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 return change;
3314}
3315
Takashi Iwai0ba21762007-04-16 11:29:14 +02003316static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3317 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318{
3319 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3320 hda_nid_t nid = kcontrol->private_value;
3321 unsigned short val;
3322 unsigned int sbits;
3323
Andrew Paprocki3982d172007-12-19 12:13:44 +01003324 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 sbits = convert_to_spdif_status(val);
3326 ucontrol->value.iec958.status[0] = sbits;
3327 ucontrol->value.iec958.status[1] = sbits >> 8;
3328 ucontrol->value.iec958.status[2] = sbits >> 16;
3329 ucontrol->value.iec958.status[3] = sbits >> 24;
3330 return 0;
3331}
3332
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003333static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 {
3335 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003336 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 .info = snd_hda_spdif_in_switch_info,
3338 .get = snd_hda_spdif_in_switch_get,
3339 .put = snd_hda_spdif_in_switch_put,
3340 },
3341 {
3342 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3343 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003344 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 .info = snd_hda_spdif_mask_info,
3346 .get = snd_hda_spdif_in_status_get,
3347 },
3348 { } /* end */
3349};
3350
3351/**
3352 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3353 * @codec: the HDA codec
3354 * @nid: audio in widget NID
3355 *
3356 * Creates controls related with the SPDIF input.
3357 * Called from each patch supporting the SPDIF in.
3358 *
3359 * Returns 0 if successful, or a negative error code.
3360 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003361int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362{
3363 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003364 struct snd_kcontrol *kctl;
3365 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003366 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367
Takashi Iwaidcda5802012-10-12 17:24:51 +02003368 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003369 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003370 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
3371 return -EBUSY;
3372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3374 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01003375 if (!kctl)
3376 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003378 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003379 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 return err;
3381 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003382 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01003383 snd_hda_codec_read(codec, nid, 0,
3384 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02003385 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 return 0;
3387}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003388EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003390/*
3391 * command cache
3392 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393
Takashi Iwaic370dd62012-12-13 18:30:04 +01003394/* build a 31bit cache key with the widget id and the command parameter */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003395#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3396#define get_cmd_cache_nid(key) ((key) & 0xff)
3397#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3398
3399/**
3400 * snd_hda_codec_write_cache - send a single command with caching
3401 * @codec: the HDA codec
3402 * @nid: NID to send the command
3403 * @direct: direct flag
3404 * @verb: the verb to send
3405 * @parm: the parameter for the verb
3406 *
3407 * Send a single command without waiting for response.
3408 *
3409 * Returns 0 if successful, or a negative error code.
3410 */
3411int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
3412 int direct, unsigned int verb, unsigned int parm)
3413{
Takashi Iwaic370dd62012-12-13 18:30:04 +01003414 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003415 struct hda_cache_head *c;
3416 u32 key;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003417
Takashi Iwaic370dd62012-12-13 18:30:04 +01003418 if (!codec->cached_write) {
3419 err = snd_hda_codec_write(codec, nid, direct, verb, parm);
3420 if (err < 0)
3421 return err;
3422 }
3423
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003424 /* parm may contain the verb stuff for get/set amp */
3425 verb = verb | (parm >> 8);
3426 parm &= 0xff;
3427 key = build_cmd_cache_key(nid, verb);
3428 mutex_lock(&codec->bus->cmd_mutex);
3429 c = get_alloc_hash(&codec->cmd_cache, key);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003430 if (c) {
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003431 c->val = parm;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003432 if (codec->cached_write)
3433 c->dirty = 1;
3434 }
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003435 mutex_unlock(&codec->bus->cmd_mutex);
3436 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003437}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003438EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003439
Takashi Iwaid5191e52009-11-16 14:58:17 +01003440/**
Takashi Iwaia68d5a542010-03-30 18:03:44 +02003441 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3442 * @codec: the HDA codec
3443 * @nid: NID to send the command
3444 * @direct: direct flag
3445 * @verb: the verb to send
3446 * @parm: the parameter for the verb
3447 *
3448 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3449 * command if the parameter is already identical with the cached value.
3450 * If not, it sends the command and refreshes the cache.
3451 *
3452 * Returns 0 if successful, or a negative error code.
3453 */
3454int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
3455 int direct, unsigned int verb, unsigned int parm)
3456{
3457 struct hda_cache_head *c;
3458 u32 key;
3459
3460 /* parm may contain the verb stuff for get/set amp */
3461 verb = verb | (parm >> 8);
3462 parm &= 0xff;
3463 key = build_cmd_cache_key(nid, verb);
3464 mutex_lock(&codec->bus->cmd_mutex);
3465 c = get_hash(&codec->cmd_cache, key);
3466 if (c && c->val == parm) {
3467 mutex_unlock(&codec->bus->cmd_mutex);
3468 return 0;
3469 }
3470 mutex_unlock(&codec->bus->cmd_mutex);
3471 return snd_hda_codec_write_cache(codec, nid, direct, verb, parm);
3472}
3473EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
3474
3475/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003476 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3477 * @codec: HD-audio codec
3478 *
3479 * Execute all verbs recorded in the command caches to resume.
3480 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003481void snd_hda_codec_resume_cache(struct hda_codec *codec)
3482{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003483 int i;
3484
Takashi Iwaic370dd62012-12-13 18:30:04 +01003485 mutex_lock(&codec->hash_mutex);
3486 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3487 struct hda_cache_head *buffer;
3488 u32 key;
3489
3490 buffer = snd_array_elem(&codec->cmd_cache.buf, i);
3491 key = buffer->key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003492 if (!key)
3493 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003494 if (!buffer->dirty)
3495 continue;
3496 buffer->dirty = 0;
3497 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003498 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3499 get_cmd_cache_cmd(key), buffer->val);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003500 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003501 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01003502 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003503}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003504EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003505
3506/**
3507 * snd_hda_sequence_write_cache - sequence writes with caching
3508 * @codec: the HDA codec
3509 * @seq: VERB array to send
3510 *
3511 * Send the commands sequentially from the given array.
3512 * Thte commands are recorded on cache for power-save and resume.
3513 * The array must be terminated with NID=0.
3514 */
3515void snd_hda_sequence_write_cache(struct hda_codec *codec,
3516 const struct hda_verb *seq)
3517{
3518 for (; seq->nid; seq++)
3519 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3520 seq->param);
3521}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003522EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003523
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003524void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
3525 unsigned int power_state,
3526 bool eapd_workaround)
Takashi Iwai54d17402005-11-21 16:33:22 +01003527{
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003528 hda_nid_t nid = codec->start_nid;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003529 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01003530
Takashi Iwaicb53c622007-08-10 17:21:45 +02003531 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003532 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003533 if (!(wcaps & AC_WCAP_POWER))
3534 continue;
3535 /* don't power down the widget if it controls eapd and
3536 * EAPD_BTLENABLE is set.
3537 */
3538 if (eapd_workaround && power_state == AC_PWRST_D3 &&
3539 get_wcaps_type(wcaps) == AC_WID_PIN &&
3540 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3541 int eapd = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003542 AC_VERB_GET_EAPD_BTLENABLE, 0);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003543 if (eapd & 0x02)
3544 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003545 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003546 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
3547 power_state);
Takashi Iwai54d17402005-11-21 16:33:22 +01003548 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003549}
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003550EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all);
3551
3552/*
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003553 * supported power states check
3554 */
3555static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg,
3556 unsigned int power_state)
3557{
3558 int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE);
3559
Mengdong Line037cb42012-08-10 14:11:58 +02003560 if (sup == -1)
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003561 return false;
3562 if (sup & power_state)
3563 return true;
3564 else
3565 return false;
3566}
3567
3568/*
Takashi Iwai432c6412012-08-28 09:59:20 -07003569 * wait until the state is reached, returns the current state
3570 */
3571static unsigned int hda_sync_power_state(struct hda_codec *codec,
3572 hda_nid_t fg,
3573 unsigned int power_state)
3574{
3575 unsigned long end_time = jiffies + msecs_to_jiffies(500);
3576 unsigned int state, actual_state;
3577
3578 for (;;) {
3579 state = snd_hda_codec_read(codec, fg, 0,
3580 AC_VERB_GET_POWER_STATE, 0);
3581 if (state & AC_PWRST_ERROR)
3582 break;
3583 actual_state = (state >> 4) & 0x0f;
3584 if (actual_state == power_state)
3585 break;
3586 if (time_after_eq(jiffies, end_time))
3587 break;
3588 /* wait until the codec reachs to the target state */
3589 msleep(1);
3590 }
3591 return state;
3592}
3593
3594/*
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003595 * set power state of the codec, and return the power state
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003596 */
Takashi Iwaid8193872012-08-31 07:54:38 -07003597static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003598 unsigned int power_state)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003599{
Takashi Iwaid8193872012-08-31 07:54:38 -07003600 hda_nid_t fg = codec->afg ? codec->afg : codec->mfg;
Wang Xingchao09617ce2012-06-08 10:26:08 +08003601 int count;
3602 unsigned int state;
3603
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003604 /* this delay seems necessary to avoid click noise at power-down */
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003605 if (power_state == AC_PWRST_D3) {
3606 /* transition time less than 10ms for power down */
Takashi Iwai983f6b92012-08-28 09:18:01 -07003607 msleep(codec->epss ? 10 : 100);
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003608 }
Wang Xingchao09617ce2012-06-08 10:26:08 +08003609
3610 /* repeat power states setting at most 10 times*/
3611 for (count = 0; count < 10; count++) {
Takashi Iwai432c6412012-08-28 09:59:20 -07003612 if (codec->patch_ops.set_power_state)
3613 codec->patch_ops.set_power_state(codec, fg,
3614 power_state);
3615 else {
3616 snd_hda_codec_read(codec, fg, 0,
3617 AC_VERB_SET_POWER_STATE,
3618 power_state);
3619 snd_hda_codec_set_power_to_all(codec, fg, power_state,
3620 true);
3621 }
3622 state = hda_sync_power_state(codec, fg, power_state);
Wang Xingchao09617ce2012-06-08 10:26:08 +08003623 if (!(state & AC_PWRST_ERROR))
3624 break;
3625 }
Mengdong Linb8dfc4622012-08-23 17:32:30 +08003626
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003627 return state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003628}
Takashi Iwai54d17402005-11-21 16:33:22 +01003629
Takashi Iwai11aeff02008-07-30 15:01:46 +02003630#ifdef CONFIG_SND_HDA_HWDEP
3631/* execute additional init verbs */
3632static void hda_exec_init_verbs(struct hda_codec *codec)
3633{
3634 if (codec->init_verbs.list)
3635 snd_hda_sequence_write(codec, codec->init_verbs.list);
3636}
3637#else
3638static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3639#endif
3640
Takashi Iwai2a439522011-07-26 09:52:50 +02003641#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003642/*
3643 * call suspend and power-down; used both from PM and power-save
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003644 * this function returns the power state in the end
Takashi Iwaicb53c622007-08-10 17:21:45 +02003645 */
Dylan Reidd17344b2012-09-28 15:57:01 -07003646static unsigned int hda_call_codec_suspend(struct hda_codec *codec, bool in_wq)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003647{
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003648 unsigned int state;
3649
Takashi Iwai989c3182012-11-19 14:14:58 +01003650 codec->in_pm = 1;
3651
Takashi Iwaicb53c622007-08-10 17:21:45 +02003652 if (codec->patch_ops.suspend)
Takashi Iwai68cb2b52012-07-02 15:20:37 +02003653 codec->patch_ops.suspend(codec);
Takashi Iwaieb541332010-08-06 13:48:11 +02003654 hda_cleanup_all_streams(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07003655 state = hda_set_power_state(codec, AC_PWRST_D3);
Dylan Reidd17344b2012-09-28 15:57:01 -07003656 /* Cancel delayed work if we aren't currently running from it. */
3657 if (!in_wq)
3658 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02003659 spin_lock(&codec->power_lock);
3660 snd_hda_update_power_acct(codec);
3661 trace_hda_power_down(codec);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02003662 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02003663 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003664 codec->power_jiffies = jiffies;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02003665 spin_unlock(&codec->power_lock);
Takashi Iwai989c3182012-11-19 14:14:58 +01003666 codec->in_pm = 0;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003667 return state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003668}
3669
Takashi Iwaic370dd62012-12-13 18:30:04 +01003670/* mark all entries of cmd and amp caches dirty */
3671static void hda_mark_cmd_cache_dirty(struct hda_codec *codec)
3672{
3673 int i;
3674 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3675 struct hda_cache_head *cmd;
3676 cmd = snd_array_elem(&codec->cmd_cache.buf, i);
3677 cmd->dirty = 1;
3678 }
3679 for (i = 0; i < codec->amp_cache.buf.used; i++) {
3680 struct hda_amp_info *amp;
3681 amp = snd_array_elem(&codec->cmd_cache.buf, i);
3682 amp->head.dirty = 1;
3683 }
3684}
3685
Takashi Iwaicb53c622007-08-10 17:21:45 +02003686/*
3687 * kick up codec; used both from PM and power-save
3688 */
3689static void hda_call_codec_resume(struct hda_codec *codec)
3690{
Takashi Iwai989c3182012-11-19 14:14:58 +01003691 codec->in_pm = 1;
3692
Takashi Iwaic370dd62012-12-13 18:30:04 +01003693 hda_mark_cmd_cache_dirty(codec);
3694
Takashi Iwai7f308302012-05-08 16:52:23 +02003695 /* set as if powered on for avoiding re-entering the resume
3696 * in the resume / power-save sequence
3697 */
3698 hda_keep_power_on(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07003699 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02003700 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02003701 hda_exec_init_verbs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003702 if (codec->patch_ops.resume)
3703 codec->patch_ops.resume(codec);
3704 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02003705 if (codec->patch_ops.init)
3706 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003707 snd_hda_codec_resume_amp(codec);
3708 snd_hda_codec_resume_cache(codec);
3709 }
David Henningsson26a6cb62012-10-09 15:04:21 +02003710
3711 if (codec->jackpoll_interval)
3712 hda_jackpoll_work(&codec->jackpoll_work.work);
3713 else {
3714 snd_hda_jack_set_dirty_all(codec);
3715 snd_hda_jack_report_sync(codec);
3716 }
Takashi Iwai989c3182012-11-19 14:14:58 +01003717
3718 codec->in_pm = 0;
Takashi Iwai7f308302012-05-08 16:52:23 +02003719 snd_hda_power_down(codec); /* flag down before returning */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003720}
Takashi Iwai2a439522011-07-26 09:52:50 +02003721#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003722
Takashi Iwai54d17402005-11-21 16:33:22 +01003723
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724/**
3725 * snd_hda_build_controls - build mixer controls
3726 * @bus: the BUS
3727 *
3728 * Creates mixer controls for each codec included in the bus.
3729 *
3730 * Returns 0 if successful, otherwise a negative error code.
3731 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01003732int snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003734 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735
Takashi Iwai0ba21762007-04-16 11:29:14 +02003736 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003737 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003738 if (err < 0) {
Takashi Iwai28d1a852010-03-15 09:05:46 +01003739 printk(KERN_ERR "hda_codec: cannot build controls "
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003740 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003741 err = snd_hda_codec_reset(codec);
3742 if (err < 0) {
3743 printk(KERN_ERR
3744 "hda_codec: cannot revert codec\n");
3745 return err;
3746 }
3747 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003749 return 0;
3750}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003751EXPORT_SYMBOL_HDA(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003753/*
3754 * add standard channel maps if not specified
3755 */
3756static int add_std_chmaps(struct hda_codec *codec)
3757{
3758 int i, str, err;
3759
3760 for (i = 0; i < codec->num_pcms; i++) {
3761 for (str = 0; str < 2; str++) {
3762 struct snd_pcm *pcm = codec->pcm_info[i].pcm;
3763 struct hda_pcm_stream *hinfo =
3764 &codec->pcm_info[i].stream[str];
3765 struct snd_pcm_chmap *chmap;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003766 const struct snd_pcm_chmap_elem *elem;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003767
3768 if (codec->pcm_info[i].own_chmap)
3769 continue;
3770 if (!pcm || !hinfo->substreams)
3771 continue;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003772 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
3773 err = snd_pcm_add_chmap_ctls(pcm, str, elem,
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003774 hinfo->channels_max,
3775 0, &chmap);
3776 if (err < 0)
3777 return err;
3778 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
3779 }
3780 }
3781 return 0;
3782}
3783
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003784/* default channel maps for 2.1 speakers;
3785 * since HD-audio supports only stereo, odd number channels are omitted
3786 */
3787const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
3788 { .channels = 2,
3789 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
3790 { .channels = 4,
3791 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
3792 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
3793 { }
3794};
3795EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
3796
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003797int snd_hda_codec_build_controls(struct hda_codec *codec)
3798{
3799 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02003800 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003801 /* continue to initialize... */
3802 if (codec->patch_ops.init)
3803 err = codec->patch_ops.init(codec);
3804 if (!err && codec->patch_ops.build_controls)
3805 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003806 if (err < 0)
3807 return err;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003808
3809 /* we create chmaps here instead of build_pcms */
3810 err = add_std_chmaps(codec);
3811 if (err < 0)
3812 return err;
3813
David Henningsson26a6cb62012-10-09 15:04:21 +02003814 if (codec->jackpoll_interval)
3815 hda_jackpoll_work(&codec->jackpoll_work.work);
3816 else
3817 snd_hda_jack_report_sync(codec); /* call at the last init point */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818 return 0;
3819}
3820
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821/*
3822 * stream formats
3823 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02003824struct hda_rate_tbl {
3825 unsigned int hz;
3826 unsigned int alsa_bits;
3827 unsigned int hda_fmt;
3828};
3829
Takashi Iwai92f10b32010-08-03 14:21:00 +02003830/* rate = base * mult / div */
3831#define HDA_RATE(base, mult, div) \
3832 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
3833 (((div) - 1) << AC_FMT_DIV_SHIFT))
3834
Takashi Iwaibefdf312005-08-22 13:57:55 +02003835static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003837
3838 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003839 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
3840 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
3841 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
3842 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
3843 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
3844 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
3845 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
3846 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
3847 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
3848 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
3849 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003850#define AC_PAR_PCM_RATE_BITS 11
3851 /* up to bits 10, 384kHZ isn't supported properly */
3852
3853 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003854 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003855
Takashi Iwaibefdf312005-08-22 13:57:55 +02003856 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857};
3858
3859/**
3860 * snd_hda_calc_stream_format - calculate format bitset
3861 * @rate: the sample rate
3862 * @channels: the number of channels
3863 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3864 * @maxbps: the max. bps
3865 *
3866 * Calculate the format bitset from the given rate, channels and th PCM format.
3867 *
3868 * Return zero if invalid.
3869 */
3870unsigned int snd_hda_calc_stream_format(unsigned int rate,
3871 unsigned int channels,
3872 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03003873 unsigned int maxbps,
3874 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875{
3876 int i;
3877 unsigned int val = 0;
3878
Takashi Iwaibefdf312005-08-22 13:57:55 +02003879 for (i = 0; rate_bits[i].hz; i++)
3880 if (rate_bits[i].hz == rate) {
3881 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882 break;
3883 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003884 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885 snd_printdd("invalid rate %d\n", rate);
3886 return 0;
3887 }
3888
3889 if (channels == 0 || channels > 8) {
3890 snd_printdd("invalid channels %d\n", channels);
3891 return 0;
3892 }
3893 val |= channels - 1;
3894
3895 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003896 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003897 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003898 break;
3899 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003900 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003901 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902 case 20:
3903 case 24:
3904 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003905 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003906 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003908 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02003910 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911 break;
3912 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003913 snd_printdd("invalid format width %d\n",
3914 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915 return 0;
3916 }
3917
Anssi Hannula32c168c2010-08-03 13:28:57 +03003918 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003919 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03003920
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921 return val;
3922}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003923EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003925static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid,
3926 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003927{
3928 unsigned int val = 0;
3929 if (nid != codec->afg &&
3930 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
3931 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
3932 if (!val || val == -1)
3933 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
3934 if (!val || val == -1)
3935 return 0;
3936 return val;
3937}
3938
3939static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3940{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003941 return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003942 get_pcm_param);
3943}
3944
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003945static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid,
3946 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003947{
3948 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
3949 if (!streams || streams == -1)
3950 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
3951 if (!streams || streams == -1)
3952 return 0;
3953 return streams;
3954}
3955
3956static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
3957{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003958 return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003959 get_stream_param);
3960}
3961
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962/**
3963 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
3964 * @codec: the HDA codec
3965 * @nid: NID to query
3966 * @ratesp: the pointer to store the detected rate bitflags
3967 * @formatsp: the pointer to store the detected formats
3968 * @bpsp: the pointer to store the detected format widths
3969 *
3970 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
3971 * or @bsps argument is ignored.
3972 *
3973 * Returns 0 if successful, otherwise a negative error code.
3974 */
Stephen Warren384a48d2011-06-01 11:14:21 -06003975int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
3977{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003978 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003980 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003981 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982
3983 if (ratesp) {
3984 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003985 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02003987 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003989 if (rates == 0) {
3990 snd_printk(KERN_ERR "hda_codec: rates == 0 "
3991 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
3992 nid, val,
3993 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
3994 return -EIO;
3995 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996 *ratesp = rates;
3997 }
3998
3999 if (formatsp || bpsp) {
4000 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004001 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004003 streams = query_stream_param(codec, nid);
4004 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006
4007 bps = 0;
4008 if (streams & AC_SUPFMT_PCM) {
4009 if (val & AC_SUPPCM_BITS_8) {
4010 formats |= SNDRV_PCM_FMTBIT_U8;
4011 bps = 8;
4012 }
4013 if (val & AC_SUPPCM_BITS_16) {
4014 formats |= SNDRV_PCM_FMTBIT_S16_LE;
4015 bps = 16;
4016 }
4017 if (wcaps & AC_WCAP_DIGITAL) {
4018 if (val & AC_SUPPCM_BITS_32)
4019 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
4020 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
4021 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4022 if (val & AC_SUPPCM_BITS_24)
4023 bps = 24;
4024 else if (val & AC_SUPPCM_BITS_20)
4025 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004026 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
4027 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4029 if (val & AC_SUPPCM_BITS_32)
4030 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 else if (val & AC_SUPPCM_BITS_24)
4032 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02004033 else if (val & AC_SUPPCM_BITS_20)
4034 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035 }
4036 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004037#if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
Takashi Iwaib5025c52009-07-01 18:05:27 +02004038 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004040 if (!bps)
4041 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02004042 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004043#endif
Takashi Iwaib5025c52009-07-01 18:05:27 +02004044 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02004045 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046 /* temporary hack: we have still no proper support
4047 * for the direct AC3 stream...
4048 */
4049 formats |= SNDRV_PCM_FMTBIT_U8;
4050 bps = 8;
4051 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004052 if (formats == 0) {
4053 snd_printk(KERN_ERR "hda_codec: formats == 0 "
4054 "(nid=0x%x, val=0x%x, ovrd=%i, "
4055 "streams=0x%x)\n",
4056 nid, val,
4057 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
4058 streams);
4059 return -EIO;
4060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061 if (formatsp)
4062 *formatsp = formats;
4063 if (bpsp)
4064 *bpsp = bps;
4065 }
4066
4067 return 0;
4068}
Stephen Warren384a48d2011-06-01 11:14:21 -06004069EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070
4071/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01004072 * snd_hda_is_supported_format - Check the validity of the format
4073 * @codec: HD-audio codec
4074 * @nid: NID to check
4075 * @format: the HD-audio format value to check
4076 *
4077 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078 *
4079 * Returns 1 if supported, 0 if not.
4080 */
4081int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
4082 unsigned int format)
4083{
4084 int i;
4085 unsigned int val = 0, rate, stream;
4086
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004087 val = query_pcm_param(codec, nid);
4088 if (!val)
4089 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090
4091 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004092 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02004093 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094 if (val & (1 << i))
4095 break;
4096 return 0;
4097 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004098 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099 return 0;
4100
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004101 stream = query_stream_param(codec, nid);
4102 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103 return 0;
4104
4105 if (stream & AC_SUPFMT_PCM) {
4106 switch (format & 0xf0) {
4107 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004108 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109 return 0;
4110 break;
4111 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004112 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113 return 0;
4114 break;
4115 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004116 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117 return 0;
4118 break;
4119 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004120 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121 return 0;
4122 break;
4123 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004124 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125 return 0;
4126 break;
4127 default:
4128 return 0;
4129 }
4130 } else {
4131 /* FIXME: check for float32 and AC3? */
4132 }
4133
4134 return 1;
4135}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004136EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137
4138/*
4139 * PCM stuff
4140 */
4141static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
4142 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004143 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144{
4145 return 0;
4146}
4147
4148static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
4149 struct hda_codec *codec,
4150 unsigned int stream_tag,
4151 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004152 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153{
4154 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4155 return 0;
4156}
4157
4158static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
4159 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004160 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161{
Takashi Iwai888afa12008-03-18 09:57:50 +01004162 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 return 0;
4164}
4165
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004166static int set_pcm_default_values(struct hda_codec *codec,
4167 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004169 int err;
4170
Takashi Iwai0ba21762007-04-16 11:29:14 +02004171 /* query support PCM information from the given NID */
4172 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004173 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02004174 info->rates ? NULL : &info->rates,
4175 info->formats ? NULL : &info->formats,
4176 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004177 if (err < 0)
4178 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179 }
4180 if (info->ops.open == NULL)
4181 info->ops.open = hda_pcm_default_open_close;
4182 if (info->ops.close == NULL)
4183 info->ops.close = hda_pcm_default_open_close;
4184 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004185 if (snd_BUG_ON(!info->nid))
4186 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187 info->ops.prepare = hda_pcm_default_prepare;
4188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004190 if (snd_BUG_ON(!info->nid))
4191 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192 info->ops.cleanup = hda_pcm_default_cleanup;
4193 }
4194 return 0;
4195}
4196
Takashi Iwaieb541332010-08-06 13:48:11 +02004197/*
4198 * codec prepare/cleanup entries
4199 */
4200int snd_hda_codec_prepare(struct hda_codec *codec,
4201 struct hda_pcm_stream *hinfo,
4202 unsigned int stream,
4203 unsigned int format,
4204 struct snd_pcm_substream *substream)
4205{
4206 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004207 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004208 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
4209 if (ret >= 0)
4210 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004211 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004212 return ret;
4213}
4214EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
4215
4216void snd_hda_codec_cleanup(struct hda_codec *codec,
4217 struct hda_pcm_stream *hinfo,
4218 struct snd_pcm_substream *substream)
4219{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004220 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004221 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004222 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004223}
4224EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
4225
Takashi Iwaid5191e52009-11-16 14:58:17 +01004226/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01004227const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
4228 "Audio", "SPDIF", "HDMI", "Modem"
4229};
4230
Takashi Iwai176d5332008-07-30 15:01:44 +02004231/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004232 * get the empty PCM device number to assign
Takashi Iwaic8936222010-01-28 17:08:53 +01004233 *
4234 * note the max device number is limited by HDA_MAX_PCMS, currently 10
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004235 */
4236static int get_empty_pcm_device(struct hda_bus *bus, int type)
4237{
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004238 /* audio device indices; not linear to keep compatibility */
4239 static int audio_idx[HDA_PCM_NTYPES][5] = {
4240 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
4241 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01004242 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004243 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004244 };
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004245 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004246
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004247 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004248 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
4249 return -EINVAL;
4250 }
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004251
4252 for (i = 0; audio_idx[type][i] >= 0 ; i++)
4253 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
4254 return audio_idx[type][i];
4255
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004256 /* non-fixed slots starting from 10 */
4257 for (i = 10; i < 32; i++) {
4258 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4259 return i;
4260 }
4261
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004262 snd_printk(KERN_WARNING "Too many %s devices\n",
4263 snd_hda_pcm_type_name[type]);
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004264 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004265}
4266
4267/*
Takashi Iwai176d5332008-07-30 15:01:44 +02004268 * attach a new PCM stream
4269 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004270static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02004271{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004272 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02004273 struct hda_pcm_stream *info;
4274 int stream, err;
4275
Takashi Iwaib91f0802008-11-04 08:43:08 +01004276 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02004277 return -EINVAL;
4278 for (stream = 0; stream < 2; stream++) {
4279 info = &pcm->stream[stream];
4280 if (info->substreams) {
4281 err = set_pcm_default_values(codec, info);
4282 if (err < 0)
4283 return err;
4284 }
4285 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004286 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02004287}
4288
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004289/* assign all PCMs of the given codec */
4290int snd_hda_codec_build_pcms(struct hda_codec *codec)
4291{
4292 unsigned int pcm;
4293 int err;
4294
4295 if (!codec->num_pcms) {
4296 if (!codec->patch_ops.build_pcms)
4297 return 0;
4298 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004299 if (err < 0) {
4300 printk(KERN_ERR "hda_codec: cannot build PCMs"
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004301 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004302 err = snd_hda_codec_reset(codec);
4303 if (err < 0) {
4304 printk(KERN_ERR
4305 "hda_codec: cannot revert codec\n");
4306 return err;
4307 }
4308 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004309 }
4310 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
4311 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
4312 int dev;
4313
4314 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01004315 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004316
4317 if (!cpcm->pcm) {
4318 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
4319 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004320 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004321 cpcm->device = dev;
4322 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004323 if (err < 0) {
4324 printk(KERN_ERR "hda_codec: cannot attach "
4325 "PCM stream %d for codec #%d\n",
4326 dev, codec->addr);
4327 continue; /* no fatal error */
4328 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004329 }
4330 }
4331 return 0;
4332}
4333
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334/**
4335 * snd_hda_build_pcms - build PCM information
4336 * @bus: the BUS
4337 *
4338 * Create PCM information for each codec included in the bus.
4339 *
4340 * The build_pcms codec patch is requested to set up codec->num_pcms and
4341 * codec->pcm_info properly. The array is referred by the top-level driver
4342 * to create its PCM instances.
4343 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4344 * callback.
4345 *
4346 * At least, substreams, channels_min and channels_max must be filled for
4347 * each stream. substreams = 0 indicates that the stream doesn't exist.
4348 * When rates and/or formats are zero, the supported values are queried
4349 * from the given nid. The nid is used also by the default ops.prepare
4350 * and ops.cleanup callbacks.
4351 *
4352 * The driver needs to call ops.open in its open callback. Similarly,
4353 * ops.close is supposed to be called in the close callback.
4354 * ops.prepare should be called in the prepare or hw_params callback
4355 * with the proper parameters for set up.
4356 * ops.cleanup should be called in hw_free for clean up of streams.
4357 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004358 * This function returns 0 if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359 */
Takashi Iwai5cb543d2012-08-09 13:49:23 +02004360int snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004362 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363
Takashi Iwai0ba21762007-04-16 11:29:14 +02004364 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004365 int err = snd_hda_codec_build_pcms(codec);
4366 if (err < 0)
4367 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368 }
4369 return 0;
4370}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004371EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373/**
4374 * snd_hda_check_board_config - compare the current codec with the config table
4375 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004376 * @num_configs: number of config enums
4377 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378 * @tbl: configuration table, terminated by null entries
4379 *
4380 * Compares the modelname or PCI subsystem id of the current codec with the
4381 * given configuration table. If a matching entry is found, returns its
4382 * config value (supposed to be 0 or positive).
4383 *
4384 * If no entries are matching, the function returns a negative value.
4385 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02004386int snd_hda_check_board_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004387 int num_configs, const char * const *models,
Takashi Iwai12f288b2007-08-02 15:51:59 +02004388 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389{
Takashi Iwaif44ac832008-07-30 15:01:45 +02004390 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004391 int i;
4392 for (i = 0; i < num_configs; i++) {
4393 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02004394 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004395 snd_printd(KERN_INFO "hda_codec: model '%s' is "
4396 "selected\n", models[i]);
4397 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398 }
4399 }
4400 }
4401
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004402 if (!codec->bus->pci || !tbl)
4403 return -1;
4404
4405 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
4406 if (!tbl)
4407 return -1;
4408 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02004409#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004410 char tmp[10];
4411 const char *model = NULL;
4412 if (models)
4413 model = models[tbl->value];
4414 if (!model) {
4415 sprintf(tmp, "#%d", tbl->value);
4416 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004418 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4419 "for config %x:%x (%s)\n",
4420 model, tbl->subvendor, tbl->subdevice,
4421 (tbl->name ? tbl->name : "Unknown device"));
4422#endif
4423 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424 }
4425 return -1;
4426}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004427EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428
4429/**
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004430 * snd_hda_check_board_codec_sid_config - compare the current codec
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004431 subsystem ID with the
4432 config table
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004433
4434 This is important for Gateway notebooks with SB450 HDA Audio
4435 where the vendor ID of the PCI device is:
4436 ATI Technologies Inc SB450 HDA Audio [1002:437b]
4437 and the vendor/subvendor are found only at the codec.
4438
4439 * @codec: the HDA codec
4440 * @num_configs: number of config enums
4441 * @models: array of model name strings
4442 * @tbl: configuration table, terminated by null entries
4443 *
4444 * Compares the modelname or PCI subsystem id of the current codec with the
4445 * given configuration table. If a matching entry is found, returns its
4446 * config value (supposed to be 0 or positive).
4447 *
4448 * If no entries are matching, the function returns a negative value.
4449 */
4450int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004451 int num_configs, const char * const *models,
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004452 const struct snd_pci_quirk *tbl)
4453{
4454 const struct snd_pci_quirk *q;
4455
4456 /* Search for codec ID */
4457 for (q = tbl; q->subvendor; q++) {
Takashi Iwaie2301a42011-11-22 19:58:56 +01004458 unsigned int mask = 0xffff0000 | q->subdevice_mask;
4459 unsigned int id = (q->subdevice | (q->subvendor << 16)) & mask;
4460 if ((codec->subsystem_id & mask) == id)
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004461 break;
4462 }
4463
4464 if (!q->subvendor)
4465 return -1;
4466
4467 tbl = q;
4468
4469 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwaid94ff6b2009-09-02 00:20:21 +02004470#ifdef CONFIG_SND_DEBUG_VERBOSE
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004471 char tmp[10];
4472 const char *model = NULL;
4473 if (models)
4474 model = models[tbl->value];
4475 if (!model) {
4476 sprintf(tmp, "#%d", tbl->value);
4477 model = tmp;
4478 }
4479 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4480 "for config %x:%x (%s)\n",
4481 model, tbl->subvendor, tbl->subdevice,
4482 (tbl->name ? tbl->name : "Unknown device"));
4483#endif
4484 return tbl->value;
4485 }
4486 return -1;
4487}
4488EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
4489
4490/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491 * snd_hda_add_new_ctls - create controls from the array
4492 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004493 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494 *
4495 * This helper function creates and add new controls in the given array.
4496 * The array must be terminated with an empty entry as terminator.
4497 *
4498 * Returns 0 if successful, or a negative error code.
4499 */
Takashi Iwai031024e2011-05-02 11:29:30 +02004500int snd_hda_add_new_ctls(struct hda_codec *codec,
4501 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01004503 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504
4505 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004506 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004507 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01004508 if (knew->iface == -1) /* skip this codec private value */
4509 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004510 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004511 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02004512 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01004513 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004514 if (addr > 0)
4515 kctl->id.device = addr;
4516 if (idx > 0)
4517 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01004518 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004519 if (!err)
4520 break;
4521 /* try first with another device index corresponding to
4522 * the codec addr; if it still fails (or it's the
4523 * primary codec), then try another control index
4524 */
4525 if (!addr && codec->addr)
4526 addr = codec->addr;
4527 else if (!idx && !knew->index) {
4528 idx = find_empty_mixer_ctl_idx(codec,
Takashi Iwaidcda5802012-10-12 17:24:51 +02004529 knew->name, 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004530 if (idx <= 0)
4531 return err;
4532 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01004533 return err;
4534 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535 }
4536 return 0;
4537}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004538EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539
Takashi Iwai83012a72012-08-24 18:38:08 +02004540#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02004541static void hda_power_work(struct work_struct *work)
4542{
4543 struct hda_codec *codec =
4544 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004545 struct hda_bus *bus = codec->bus;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004546 unsigned int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004547
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004548 spin_lock(&codec->power_lock);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004549 if (codec->power_transition > 0) { /* during power-up sequence? */
4550 spin_unlock(&codec->power_lock);
4551 return;
4552 }
Maxim Levitsky2e492462007-09-03 15:26:57 +02004553 if (!codec->power_on || codec->power_count) {
4554 codec->power_transition = 0;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004555 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004556 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02004557 }
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004558 spin_unlock(&codec->power_lock);
4559
Dylan Reidd17344b2012-09-28 15:57:01 -07004560 state = hda_call_codec_suspend(codec, true);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004561 codec->pm_down_notified = 0;
4562 if (!bus->power_keep_link_on && (state & AC_PWRST_CLK_STOP_OK)) {
4563 codec->pm_down_notified = 1;
Takashi Iwai68467f52012-08-28 09:14:29 -07004564 hda_call_pm_notify(bus, false);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004565 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02004566}
4567
4568static void hda_keep_power_on(struct hda_codec *codec)
4569{
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004570 spin_lock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004571 codec->power_count++;
4572 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004573 codec->power_jiffies = jiffies;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004574 spin_unlock(&codec->power_lock);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004575}
4576
Takashi Iwaid5191e52009-11-16 14:58:17 +01004577/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01004578void snd_hda_update_power_acct(struct hda_codec *codec)
4579{
4580 unsigned long delta = jiffies - codec->power_jiffies;
4581 if (codec->power_on)
4582 codec->power_on_acct += delta;
4583 else
4584 codec->power_off_acct += delta;
4585 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004586}
4587
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004588/* Transition to powered up, if wait_power_down then wait for a pending
4589 * transition to D3 to complete. A pending D3 transition is indicated
4590 * with power_transition == -1. */
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004591/* call this with codec->power_lock held! */
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004592static void __snd_hda_power_up(struct hda_codec *codec, bool wait_power_down)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004593{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004594 struct hda_bus *bus = codec->bus;
4595
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004596 /* Return if power_on or transitioning to power_on, unless currently
4597 * powering down. */
4598 if ((codec->power_on || codec->power_transition > 0) &&
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004599 !(wait_power_down && codec->power_transition < 0))
Takashi Iwaicb53c622007-08-10 17:21:45 +02004600 return;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004601 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004602
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004603 cancel_delayed_work_sync(&codec->power_work);
4604
4605 spin_lock(&codec->power_lock);
Dylan Reidb43d2242012-06-21 21:51:22 -07004606 /* If the power down delayed work was cancelled above before starting,
4607 * then there is no need to go through power up here.
4608 */
4609 if (codec->power_on) {
Takashi Iwai535b6c52012-08-20 21:25:22 +02004610 if (codec->power_transition < 0)
4611 codec->power_transition = 0;
Dylan Reidb43d2242012-06-21 21:51:22 -07004612 return;
4613 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004614
Takashi Iwaid66fee52011-08-02 15:39:31 +02004615 trace_hda_power_up(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004616 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004617 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004618 codec->power_jiffies = jiffies;
Takashi Iwai7f308302012-05-08 16:52:23 +02004619 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004620 spin_unlock(&codec->power_lock);
4621
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004622 if (codec->pm_down_notified) {
4623 codec->pm_down_notified = 0;
Takashi Iwai68467f52012-08-28 09:14:29 -07004624 hda_call_pm_notify(bus, true);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004625 }
4626
Takashi Iwaicb53c622007-08-10 17:21:45 +02004627 hda_call_codec_resume(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004628
4629 spin_lock(&codec->power_lock);
Takashi Iwaia221e282007-08-16 16:35:33 +02004630 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004631}
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004632
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004633#define power_save(codec) \
4634 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004635
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004636/* Transition to powered down */
4637static void __snd_hda_power_down(struct hda_codec *codec)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004638{
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004639 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004640 return;
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004641
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004642 if (power_save(codec)) {
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004643 codec->power_transition = -1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01004644 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004645 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02004646 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004647}
4648
4649/**
4650 * snd_hda_power_save - Power-up/down/sync the codec
4651 * @codec: HD-audio codec
4652 * @delta: the counter delta to change
4653 *
4654 * Change the power-up counter via @delta, and power up or down the hardware
4655 * appropriately. For the power-down, queue to the delayed action.
4656 * Passing zero to @delta means to synchronize the power state.
4657 */
4658void snd_hda_power_save(struct hda_codec *codec, int delta, bool d3wait)
4659{
4660 spin_lock(&codec->power_lock);
4661 codec->power_count += delta;
4662 trace_hda_power_count(codec);
4663 if (delta > 0)
4664 __snd_hda_power_up(codec, d3wait);
4665 else
4666 __snd_hda_power_down(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004667 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004668}
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004669EXPORT_SYMBOL_HDA(snd_hda_power_save);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004670
Takashi Iwaid5191e52009-11-16 14:58:17 +01004671/**
4672 * snd_hda_check_amp_list_power - Check the amp list and update the power
4673 * @codec: HD-audio codec
4674 * @check: the object containing an AMP list and the status
4675 * @nid: NID to check / update
4676 *
4677 * Check whether the given NID is in the amp list. If it's in the list,
4678 * check the current AMP status, and update the the power-status according
4679 * to the mute status.
4680 *
4681 * This function is supposed to be set or called from the check_power_status
4682 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004683 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004684int snd_hda_check_amp_list_power(struct hda_codec *codec,
4685 struct hda_loopback_check *check,
4686 hda_nid_t nid)
4687{
Takashi Iwai031024e2011-05-02 11:29:30 +02004688 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004689 int ch, v;
4690
4691 if (!check->amplist)
4692 return 0;
4693 for (p = check->amplist; p->nid; p++) {
4694 if (p->nid == nid)
4695 break;
4696 }
4697 if (!p->nid)
4698 return 0; /* nothing changed */
4699
4700 for (p = check->amplist; p->nid; p++) {
4701 for (ch = 0; ch < 2; ch++) {
4702 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4703 p->idx);
4704 if (!(v & HDA_AMP_MUTE) && v > 0) {
4705 if (!check->power_on) {
4706 check->power_on = 1;
4707 snd_hda_power_up(codec);
4708 }
4709 return 1;
4710 }
4711 }
4712 }
4713 if (check->power_on) {
4714 check->power_on = 0;
4715 snd_hda_power_down(codec);
4716 }
4717 return 0;
4718}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004719EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004720#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004722/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004723 * Channel mode helper
4724 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004725
4726/**
4727 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
4728 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004729int snd_hda_ch_mode_info(struct hda_codec *codec,
4730 struct snd_ctl_elem_info *uinfo,
4731 const struct hda_channel_mode *chmode,
4732 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004733{
4734 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4735 uinfo->count = 1;
4736 uinfo->value.enumerated.items = num_chmodes;
4737 if (uinfo->value.enumerated.item >= num_chmodes)
4738 uinfo->value.enumerated.item = num_chmodes - 1;
4739 sprintf(uinfo->value.enumerated.name, "%dch",
4740 chmode[uinfo->value.enumerated.item].channels);
4741 return 0;
4742}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004743EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004744
Takashi Iwaid5191e52009-11-16 14:58:17 +01004745/**
4746 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
4747 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004748int snd_hda_ch_mode_get(struct hda_codec *codec,
4749 struct snd_ctl_elem_value *ucontrol,
4750 const struct hda_channel_mode *chmode,
4751 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004752 int max_channels)
4753{
4754 int i;
4755
4756 for (i = 0; i < num_chmodes; i++) {
4757 if (max_channels == chmode[i].channels) {
4758 ucontrol->value.enumerated.item[0] = i;
4759 break;
4760 }
4761 }
4762 return 0;
4763}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004764EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004765
Takashi Iwaid5191e52009-11-16 14:58:17 +01004766/**
4767 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
4768 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004769int snd_hda_ch_mode_put(struct hda_codec *codec,
4770 struct snd_ctl_elem_value *ucontrol,
4771 const struct hda_channel_mode *chmode,
4772 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004773 int *max_channelsp)
4774{
4775 unsigned int mode;
4776
4777 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01004778 if (mode >= num_chmodes)
4779 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004780 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004781 return 0;
4782 /* change the current channel setting */
4783 *max_channelsp = chmode[mode].channels;
4784 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004785 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004786 return 1;
4787}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004788EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004789
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790/*
4791 * input MUX helper
4792 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004793
4794/**
4795 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
4796 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004797int snd_hda_input_mux_info(const struct hda_input_mux *imux,
4798 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799{
4800 unsigned int index;
4801
4802 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4803 uinfo->count = 1;
4804 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004805 if (!imux->num_items)
4806 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807 index = uinfo->value.enumerated.item;
4808 if (index >= imux->num_items)
4809 index = imux->num_items - 1;
4810 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
4811 return 0;
4812}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004813EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814
Takashi Iwaid5191e52009-11-16 14:58:17 +01004815/**
4816 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
4817 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004818int snd_hda_input_mux_put(struct hda_codec *codec,
4819 const struct hda_input_mux *imux,
4820 struct snd_ctl_elem_value *ucontrol,
4821 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822 unsigned int *cur_val)
4823{
4824 unsigned int idx;
4825
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004826 if (!imux->num_items)
4827 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828 idx = ucontrol->value.enumerated.item[0];
4829 if (idx >= imux->num_items)
4830 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004831 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004833 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
4834 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835 *cur_val = idx;
4836 return 1;
4837}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004838EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839
4840
4841/*
Takashi Iwaidda415d2012-11-30 18:34:38 +01004842 * process kcontrol info callback of a simple string enum array
4843 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
4844 */
4845int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
4846 struct snd_ctl_elem_info *uinfo,
4847 int num_items, const char * const *texts)
4848{
4849 static const char * const texts_default[] = {
4850 "Disabled", "Enabled"
4851 };
4852
4853 if (!texts || !num_items) {
4854 num_items = 2;
4855 texts = texts_default;
4856 }
4857
4858 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4859 uinfo->count = 1;
4860 uinfo->value.enumerated.items = num_items;
4861 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
4862 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
4863 strcpy(uinfo->value.enumerated.name,
4864 texts[uinfo->value.enumerated.item]);
4865 return 0;
4866}
4867EXPORT_SYMBOL_HDA(snd_hda_enum_helper_info);
4868
4869/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870 * Multi-channel / digital-out PCM helper functions
4871 */
4872
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004873/* setup SPDIF output stream */
4874static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
4875 unsigned int stream_tag, unsigned int format)
4876{
Laurence Darby3bef1c32012-11-03 17:00:06 +00004877 struct hda_spdif_out *spdif;
4878 unsigned int curr_fmt;
4879 bool reset;
Stephen Warren7c935972011-06-01 11:14:17 -06004880
Laurence Darby3bef1c32012-11-03 17:00:06 +00004881 spdif = snd_hda_spdif_out_of_nid(codec, nid);
4882 curr_fmt = snd_hda_codec_read(codec, nid, 0,
4883 AC_VERB_GET_STREAM_FORMAT, 0);
4884 reset = codec->spdif_status_reset &&
4885 (spdif->ctls & AC_DIG1_ENABLE) &&
4886 curr_fmt != format;
4887
4888 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
4889 updated */
4890 if (reset)
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004891 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06004892 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02004893 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004894 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02004895 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004896 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004897 for (d = codec->slave_dig_outs; *d; d++)
4898 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
4899 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004900 }
Takashi Iwai2f728532008-09-25 16:32:41 +02004901 /* turn on again (if needed) */
Laurence Darby3bef1c32012-11-03 17:00:06 +00004902 if (reset)
Takashi Iwai2f728532008-09-25 16:32:41 +02004903 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06004904 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02004905}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004906
Takashi Iwai2f728532008-09-25 16:32:41 +02004907static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
4908{
4909 snd_hda_codec_cleanup_stream(codec, nid);
4910 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004911 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004912 for (d = codec->slave_dig_outs; *d; d++)
4913 snd_hda_codec_cleanup_stream(codec, *d);
4914 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004915}
4916
Takashi Iwaid5191e52009-11-16 14:58:17 +01004917/**
4918 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
4919 * @bus: HD-audio bus
4920 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004921void snd_hda_bus_reboot_notify(struct hda_bus *bus)
4922{
4923 struct hda_codec *codec;
4924
4925 if (!bus)
4926 return;
4927 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02004928 if (hda_codec_is_power_on(codec) &&
4929 codec->patch_ops.reboot_notify)
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004930 codec->patch_ops.reboot_notify(codec);
4931 }
4932}
Takashi Iwai8f217a22009-11-10 18:26:12 +01004933EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004934
Takashi Iwaid5191e52009-11-16 14:58:17 +01004935/**
4936 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004938int snd_hda_multi_out_dig_open(struct hda_codec *codec,
4939 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940{
Ingo Molnar62932df2006-01-16 16:34:20 +01004941 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02004942 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
4943 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02004944 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01004946 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947 return 0;
4948}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004949EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950
Takashi Iwaid5191e52009-11-16 14:58:17 +01004951/**
4952 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
4953 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004954int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
4955 struct hda_multi_out *mout,
4956 unsigned int stream_tag,
4957 unsigned int format,
4958 struct snd_pcm_substream *substream)
4959{
4960 mutex_lock(&codec->spdif_mutex);
4961 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
4962 mutex_unlock(&codec->spdif_mutex);
4963 return 0;
4964}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004965EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004966
Takashi Iwaid5191e52009-11-16 14:58:17 +01004967/**
4968 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
4969 */
Takashi Iwai9411e212009-02-13 11:32:28 +01004970int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
4971 struct hda_multi_out *mout)
4972{
4973 mutex_lock(&codec->spdif_mutex);
4974 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4975 mutex_unlock(&codec->spdif_mutex);
4976 return 0;
4977}
4978EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
4979
Takashi Iwaid5191e52009-11-16 14:58:17 +01004980/**
4981 * snd_hda_multi_out_dig_close - release the digital out stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004983int snd_hda_multi_out_dig_close(struct hda_codec *codec,
4984 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985{
Ingo Molnar62932df2006-01-16 16:34:20 +01004986 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01004988 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989 return 0;
4990}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004991EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992
Takashi Iwaid5191e52009-11-16 14:58:17 +01004993/**
4994 * snd_hda_multi_out_analog_open - open analog outputs
4995 *
4996 * Open analog outputs and set up the hw-constraints.
4997 * If the digital outputs can be opened as slave, open the digital
4998 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005000int snd_hda_multi_out_analog_open(struct hda_codec *codec,
5001 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01005002 struct snd_pcm_substream *substream,
5003 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004{
Takashi Iwai9a081602008-02-12 18:37:26 +01005005 struct snd_pcm_runtime *runtime = substream->runtime;
5006 runtime->hw.channels_max = mout->max_channels;
5007 if (mout->dig_out_nid) {
5008 if (!mout->analog_rates) {
5009 mout->analog_rates = hinfo->rates;
5010 mout->analog_formats = hinfo->formats;
5011 mout->analog_maxbps = hinfo->maxbps;
5012 } else {
5013 runtime->hw.rates = mout->analog_rates;
5014 runtime->hw.formats = mout->analog_formats;
5015 hinfo->maxbps = mout->analog_maxbps;
5016 }
5017 if (!mout->spdif_rates) {
5018 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
5019 &mout->spdif_rates,
5020 &mout->spdif_formats,
5021 &mout->spdif_maxbps);
5022 }
5023 mutex_lock(&codec->spdif_mutex);
5024 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02005025 if ((runtime->hw.rates & mout->spdif_rates) &&
5026 (runtime->hw.formats & mout->spdif_formats)) {
5027 runtime->hw.rates &= mout->spdif_rates;
5028 runtime->hw.formats &= mout->spdif_formats;
5029 if (mout->spdif_maxbps < hinfo->maxbps)
5030 hinfo->maxbps = mout->spdif_maxbps;
5031 } else {
5032 mout->share_spdif = 0;
5033 /* FIXME: need notify? */
5034 }
Takashi Iwai9a081602008-02-12 18:37:26 +01005035 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02005036 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01005037 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038 return snd_pcm_hw_constraint_step(substream->runtime, 0,
5039 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
5040}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005041EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005042
Takashi Iwaid5191e52009-11-16 14:58:17 +01005043/**
5044 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
5045 *
5046 * Set up the i/o for analog out.
5047 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005049int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
5050 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005051 unsigned int stream_tag,
5052 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01005053 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054{
Takashi Iwaidda14412011-05-02 11:29:30 +02005055 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056 int chs = substream->runtime->channels;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005057 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005058 int i;
5059
Ingo Molnar62932df2006-01-16 16:34:20 +01005060 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005061 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Takashi Iwai9a081602008-02-12 18:37:26 +01005062 if (mout->dig_out_nid && mout->share_spdif &&
5063 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02005065 snd_hda_is_supported_format(codec, mout->dig_out_nid,
5066 format) &&
Stephen Warren7c935972011-06-01 11:14:17 -06005067 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005069 setup_dig_out_stream(codec, mout->dig_out_nid,
5070 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071 } else {
5072 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02005073 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074 }
5075 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005076 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077
5078 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005079 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
5080 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005081 if (!mout->no_share_stream &&
5082 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005084 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
5085 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005086 /* extra outputs copied from front */
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005087 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5088 if (!mout->no_share_stream && mout->hp_out_nid[i])
5089 snd_hda_codec_setup_stream(codec,
5090 mout->hp_out_nid[i],
5091 stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005092 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02005093 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01005094 snd_hda_codec_setup_stream(codec,
5095 mout->extra_out_nid[i],
5096 stream_tag, 0, format);
5097
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098 /* surrounds */
5099 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02005100 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005101 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5102 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005103 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005104 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5105 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106 }
5107 return 0;
5108}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005109EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005110
Takashi Iwaid5191e52009-11-16 14:58:17 +01005111/**
5112 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005114int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
5115 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116{
Takashi Iwaidda14412011-05-02 11:29:30 +02005117 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118 int i;
5119
5120 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01005121 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005122 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01005123 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005124 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5125 if (mout->hp_out_nid[i])
5126 snd_hda_codec_cleanup_stream(codec,
5127 mout->hp_out_nid[i]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005128 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
5129 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01005130 snd_hda_codec_cleanup_stream(codec,
5131 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01005132 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005133 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02005134 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135 mout->dig_out_used = 0;
5136 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005137 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005138 return 0;
5139}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005140EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141
Takashi Iwai47408602012-04-20 13:06:53 +02005142/**
5143 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
5144 *
5145 * Guess the suitable VREF pin bits to be set as the pin-control value.
5146 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
5147 */
5148unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
5149{
5150 unsigned int pincap;
5151 unsigned int oldval;
5152 oldval = snd_hda_codec_read(codec, pin, 0,
5153 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5154 pincap = snd_hda_query_pin_caps(codec, pin);
5155 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5156 /* Exception: if the default pin setup is vref50, we give it priority */
5157 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
5158 return AC_PINCTL_VREF_80;
5159 else if (pincap & AC_PINCAP_VREF_50)
5160 return AC_PINCTL_VREF_50;
5161 else if (pincap & AC_PINCAP_VREF_100)
5162 return AC_PINCTL_VREF_100;
5163 else if (pincap & AC_PINCAP_VREF_GRD)
5164 return AC_PINCTL_VREF_GRD;
5165 return AC_PINCTL_VREF_HIZ;
5166}
5167EXPORT_SYMBOL_HDA(snd_hda_get_default_vref);
5168
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005169int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
5170 unsigned int val, bool cached)
5171{
5172 if (val) {
5173 unsigned int cap = snd_hda_query_pin_caps(codec, pin);
Takashi Iwai6942c102012-04-20 13:08:40 +02005174 if (cap && (val & AC_PINCTL_OUT_EN)) {
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005175 if (!(cap & AC_PINCAP_OUT))
5176 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
5177 else if ((val & AC_PINCTL_HP_EN) &&
5178 !(cap & AC_PINCAP_HP_DRV))
5179 val &= ~AC_PINCTL_HP_EN;
5180 }
Takashi Iwai6942c102012-04-20 13:08:40 +02005181 if (cap && (val & AC_PINCTL_IN_EN)) {
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005182 if (!(cap & AC_PINCAP_IN))
5183 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
5184 }
5185 }
5186 if (cached)
5187 return snd_hda_codec_update_cache(codec, pin, 0,
5188 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5189 else
5190 return snd_hda_codec_write(codec, pin, 0,
5191 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5192}
5193EXPORT_SYMBOL_HDA(_snd_hda_set_pin_ctl);
5194
Takashi Iwai990061c2010-09-09 22:08:44 +02005195/**
5196 * snd_hda_add_imux_item - Add an item to input_mux
5197 *
5198 * When the same label is used already in the existing items, the number
5199 * suffix is appended to the label. This label index number is stored
5200 * to type_idx when non-NULL pointer is given.
5201 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02005202int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
5203 int index, int *type_idx)
5204{
5205 int i, label_idx = 0;
5206 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
5207 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
5208 return -EINVAL;
5209 }
5210 for (i = 0; i < imux->num_items; i++) {
5211 if (!strncmp(label, imux->items[i].label, strlen(label)))
5212 label_idx++;
5213 }
5214 if (type_idx)
5215 *type_idx = label_idx;
5216 if (label_idx > 0)
5217 snprintf(imux->items[imux->num_items].label,
5218 sizeof(imux->items[imux->num_items].label),
5219 "%s %d", label, label_idx);
5220 else
5221 strlcpy(imux->items[imux->num_items].label, label,
5222 sizeof(imux->items[imux->num_items].label));
5223 imux->items[imux->num_items].index = index;
5224 imux->num_items++;
5225 return 0;
5226}
5227EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005228
Takashi Iwai4a471b72005-12-07 13:56:29 +01005229
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230#ifdef CONFIG_PM
5231/*
5232 * power management
5233 */
5234
5235/**
5236 * snd_hda_suspend - suspend the codecs
5237 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005238 *
5239 * Returns 0 if successful.
5240 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02005241int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005243 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005244
Takashi Iwai0ba21762007-04-16 11:29:14 +02005245 list_for_each_entry(codec, &bus->codec_list, list) {
David Henningsson26a6cb62012-10-09 15:04:21 +02005246 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005247 if (hda_codec_is_power_on(codec))
Dylan Reidd17344b2012-09-28 15:57:01 -07005248 hda_call_codec_suspend(codec, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249 }
5250 return 0;
5251}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005252EXPORT_SYMBOL_HDA(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005253
5254/**
5255 * snd_hda_resume - resume the codecs
5256 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005257 *
5258 * Returns 0 if successful.
5259 */
5260int snd_hda_resume(struct hda_bus *bus)
5261{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005262 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263
Takashi Iwai0ba21762007-04-16 11:29:14 +02005264 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai7f308302012-05-08 16:52:23 +02005265 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267 return 0;
5268}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005269EXPORT_SYMBOL_HDA(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005270#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005271
5272/*
5273 * generic arrays
5274 */
5275
Takashi Iwaid5191e52009-11-16 14:58:17 +01005276/**
5277 * snd_array_new - get a new element from the given array
5278 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005279 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01005280 * Get a new element from the given array. If it exceeds the
5281 * pre-allocated array size, re-allocate the array.
5282 *
5283 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02005284 */
5285void *snd_array_new(struct snd_array *array)
5286{
Takashi Iwai12f17712012-10-10 08:59:14 +02005287 if (snd_BUG_ON(!array->elem_size))
5288 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005289 if (array->used >= array->alloced) {
5290 int num = array->alloced + array->alloc_align;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005291 int size = (num + 1) * array->elem_size;
Takashi Iwai00ef9612011-07-14 15:57:27 +02005292 int oldsize = array->alloced * array->elem_size;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01005293 void *nlist;
5294 if (snd_BUG_ON(num >= 4096))
5295 return NULL;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005296 nlist = krealloc(array->list, size, GFP_KERNEL);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005297 if (!nlist)
5298 return NULL;
Takashi Iwai00ef9612011-07-14 15:57:27 +02005299 memset(nlist + oldsize, 0, size - oldsize);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005300 array->list = nlist;
5301 array->alloced = num;
5302 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01005303 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005304}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005305EXPORT_SYMBOL_HDA(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005306
Takashi Iwaid5191e52009-11-16 14:58:17 +01005307/**
5308 * snd_array_free - free the given array elements
5309 * @array: the array object
5310 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005311void snd_array_free(struct snd_array *array)
5312{
5313 kfree(array->list);
5314 array->used = 0;
5315 array->alloced = 0;
5316 array->list = NULL;
5317}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005318EXPORT_SYMBOL_HDA(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01005319
Takashi Iwaid5191e52009-11-16 14:58:17 +01005320/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01005321 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5322 * @pcm: PCM caps bits
5323 * @buf: the string buffer to write
5324 * @buflen: the max buffer length
5325 *
5326 * used by hda_proc.c and hda_eld.c
5327 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005328void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5329{
5330 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5331 int i, j;
5332
5333 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5334 if (pcm & (AC_SUPPCM_BITS_8 << i))
5335 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5336
5337 buf[j] = '\0'; /* necessary when j == 0 */
5338}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005339EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005340
5341MODULE_DESCRIPTION("HDA codec core");
5342MODULE_LICENSE("GPL");