blob: e7749dee7c407d67dba0a5fa2bcef6bc41f5233a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Universal Interface for Intel High Definition Audio Codec
3 *
4 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
5 *
6 *
7 * This driver is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This driver is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
Takashi Iwai18478e82012-03-09 17:51:10 +010022#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/init.h>
24#include <linux/delay.h>
25#include <linux/slab.h>
26#include <linux/pci.h>
Ingo Molnar62932df2006-01-16 16:34:20 +010027#include <linux/mutex.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040028#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <sound/core.h>
30#include "hda_codec.h"
31#include <sound/asoundef.h>
Jaroslav Kysela302e9c52006-07-05 17:39:49 +020032#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <sound/initval.h>
Takashi Iwaicd372fb2011-03-03 14:40:14 +010034#include <sound/jack.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "hda_local.h"
Jaroslav Kysela123c07a2009-10-21 14:48:23 +020036#include "hda_beep.h"
Takashi Iwai1835a0f2011-10-27 22:12:46 +020037#include "hda_jack.h"
Takashi Iwai28073142007-07-27 18:58:06 +020038#include <sound/hda_hwdep.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Takashi Iwaid66fee52011-08-02 15:39:31 +020040#define CREATE_TRACE_POINTS
41#include "hda_trace.h"
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043/*
44 * vendor / preset table
45 */
46
47struct hda_vendor_id {
48 unsigned int id;
49 const char *name;
50};
51
52/* codec vendor labels */
53static struct hda_vendor_id hda_vendor_ids[] = {
Takashi Iwaic8cd1282008-02-13 16:59:29 +010054 { 0x1002, "ATI" },
Takashi Iwaie5f14242009-07-01 18:11:44 +020055 { 0x1013, "Cirrus Logic" },
Takashi Iwaia9226252006-09-17 22:05:54 +020056 { 0x1057, "Motorola" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010057 { 0x1095, "Silicon Image" },
Takashi Iwai31117b72008-12-16 14:43:21 +010058 { 0x10de, "Nvidia" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010059 { 0x10ec, "Realtek" },
Takashi Iwai4e01f542009-04-16 08:53:34 +020060 { 0x1102, "Creative" },
Joseph Chanc577b8a2006-11-29 15:29:40 +010061 { 0x1106, "VIA" },
Matthew Ranostay7f168592007-10-18 17:38:17 +020062 { 0x111d, "IDT" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010063 { 0x11c1, "LSI" },
Takashi Iwai54b903e2005-05-15 14:30:10 +020064 { 0x11d4, "Analog Devices" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 { 0x13f6, "C-Media" },
Takashi Iwaia9226252006-09-17 22:05:54 +020066 { 0x14f1, "Conexant" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010067 { 0x17e8, "Chrontel" },
68 { 0x1854, "LG" },
Mark Brown8199de32008-10-28 14:50:13 +000069 { 0x1aec, "Wolfson Microelectronics" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 { 0x434d, "C-Media" },
Takashi Iwai74c61132008-12-18 09:11:33 +010071 { 0x8086, "Intel" },
Matt2f2f4252005-04-13 14:45:30 +020072 { 0x8384, "SigmaTel" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 {} /* terminator */
74};
75
Takashi Iwai1289e9e2008-11-27 15:47:11 +010076static DEFINE_MUTEX(preset_mutex);
77static LIST_HEAD(hda_preset_tables);
78
79int snd_hda_add_codec_preset(struct hda_codec_preset_list *preset)
80{
81 mutex_lock(&preset_mutex);
82 list_add_tail(&preset->list, &hda_preset_tables);
83 mutex_unlock(&preset_mutex);
84 return 0;
85}
Takashi Iwaiff7a3262008-11-28 15:17:06 +010086EXPORT_SYMBOL_HDA(snd_hda_add_codec_preset);
Takashi Iwai1289e9e2008-11-27 15:47:11 +010087
88int snd_hda_delete_codec_preset(struct hda_codec_preset_list *preset)
89{
90 mutex_lock(&preset_mutex);
91 list_del(&preset->list);
92 mutex_unlock(&preset_mutex);
93 return 0;
94}
Takashi Iwaiff7a3262008-11-28 15:17:06 +010095EXPORT_SYMBOL_HDA(snd_hda_delete_codec_preset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Takashi Iwai83012a72012-08-24 18:38:08 +020097#ifdef CONFIG_PM
Takashi Iwaid846b172012-11-24 11:58:24 +010098#define codec_in_pm(codec) ((codec)->in_pm)
Takashi Iwaicb53c622007-08-10 17:21:45 +020099static void hda_power_work(struct work_struct *work);
100static void hda_keep_power_on(struct hda_codec *codec);
Takashi Iwaie581f3d2011-07-26 10:19:20 +0200101#define hda_codec_is_power_on(codec) ((codec)->power_on)
Takashi Iwai68467f52012-08-28 09:14:29 -0700102static inline void hda_call_pm_notify(struct hda_bus *bus, bool power_up)
103{
104 if (bus->ops.pm_notify)
105 bus->ops.pm_notify(bus, power_up);
106}
Takashi Iwaicb53c622007-08-10 17:21:45 +0200107#else
Takashi Iwaid846b172012-11-24 11:58:24 +0100108#define codec_in_pm(codec) 0
Takashi Iwaicb53c622007-08-10 17:21:45 +0200109static inline void hda_keep_power_on(struct hda_codec *codec) {}
Takashi Iwaie581f3d2011-07-26 10:19:20 +0200110#define hda_codec_is_power_on(codec) 1
Takashi Iwai68467f52012-08-28 09:14:29 -0700111#define hda_call_pm_notify(bus, state) {}
Takashi Iwaicb53c622007-08-10 17:21:45 +0200112#endif
113
Takashi Iwaid5191e52009-11-16 14:58:17 +0100114/**
115 * snd_hda_get_jack_location - Give a location string of the jack
116 * @cfg: pin default config value
117 *
118 * Parse the pin default config value and returns the string of the
119 * jack location, e.g. "Rear", "Front", etc.
120 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400121const char *snd_hda_get_jack_location(u32 cfg)
122{
123 static char *bases[7] = {
124 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
125 };
126 static unsigned char specials_idx[] = {
127 0x07, 0x08,
128 0x17, 0x18, 0x19,
129 0x37, 0x38
130 };
131 static char *specials[] = {
132 "Rear Panel", "Drive Bar",
133 "Riser", "HDMI", "ATAPI",
134 "Mobile-In", "Mobile-Out"
135 };
136 int i;
137 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
138 if ((cfg & 0x0f) < 7)
139 return bases[cfg & 0x0f];
140 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
141 if (cfg == specials_idx[i])
142 return specials[i];
143 }
144 return "UNKNOWN";
145}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100146EXPORT_SYMBOL_HDA(snd_hda_get_jack_location);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400147
Takashi Iwaid5191e52009-11-16 14:58:17 +0100148/**
149 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
150 * @cfg: pin default config value
151 *
152 * Parse the pin default config value and returns the string of the
153 * jack connectivity, i.e. external or internal connection.
154 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400155const char *snd_hda_get_jack_connectivity(u32 cfg)
156{
157 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
158
159 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
160}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100161EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400162
Takashi Iwaid5191e52009-11-16 14:58:17 +0100163/**
164 * snd_hda_get_jack_type - Give a type string of the jack
165 * @cfg: pin default config value
166 *
167 * Parse the pin default config value and returns the string of the
168 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
169 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400170const char *snd_hda_get_jack_type(u32 cfg)
171{
172 static char *jack_types[16] = {
173 "Line Out", "Speaker", "HP Out", "CD",
174 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
175 "Line In", "Aux", "Mic", "Telephony",
176 "SPDIF In", "Digitial In", "Reserved", "Other"
177 };
178
179 return jack_types[(cfg & AC_DEFCFG_DEVICE)
180 >> AC_DEFCFG_DEVICE_SHIFT];
181}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100182EXPORT_SYMBOL_HDA(snd_hda_get_jack_type);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400183
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100184/*
185 * Compose a 32bit command word to be sent to the HD-audio controller
186 */
187static inline unsigned int
188make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int direct,
189 unsigned int verb, unsigned int parm)
190{
191 u32 val;
192
Takashi Iwai82e1b802009-07-17 12:47:34 +0200193 if ((codec->addr & ~0xf) || (direct & ~1) || (nid & ~0x7f) ||
194 (verb & ~0xfff) || (parm & ~0xffff)) {
Wu Fengguang6430aee2009-07-17 16:49:19 +0800195 printk(KERN_ERR "hda-codec: out of range cmd %x:%x:%x:%x:%x\n",
196 codec->addr, direct, nid, verb, parm);
197 return ~0;
198 }
199
200 val = (u32)codec->addr << 28;
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100201 val |= (u32)direct << 27;
202 val |= (u32)nid << 20;
203 val |= verb << 8;
204 val |= parm;
205 return val;
206}
207
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200208/*
209 * Send and receive a verb
210 */
211static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
212 unsigned int *res)
213{
214 struct hda_bus *bus = codec->bus;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200215 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200216
Wu Fengguang6430aee2009-07-17 16:49:19 +0800217 if (cmd == ~0)
218 return -1;
219
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200220 if (res)
221 *res = -1;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200222 again:
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200223 snd_hda_power_up(codec);
224 mutex_lock(&bus->cmd_mutex);
Takashi Iwaid66fee52011-08-02 15:39:31 +0200225 trace_hda_send_cmd(codec, cmd);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200226 err = bus->ops.command(bus, cmd);
Takashi Iwaid66fee52011-08-02 15:39:31 +0200227 if (!err && res) {
Wu Fengguangdeadff12009-08-01 18:45:16 +0800228 *res = bus->ops.get_response(bus, codec->addr);
Takashi Iwaid66fee52011-08-02 15:39:31 +0200229 trace_hda_get_response(codec, *res);
230 }
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200231 mutex_unlock(&bus->cmd_mutex);
232 snd_hda_power_down(codec);
Takashi Iwaid846b172012-11-24 11:58:24 +0100233 if (!codec_in_pm(codec) && res && *res == -1 && bus->rirb_error) {
Takashi Iwai8dd78332009-06-02 01:16:07 +0200234 if (bus->response_reset) {
235 snd_printd("hda_codec: resetting BUS due to "
236 "fatal communication error\n");
Takashi Iwaid66fee52011-08-02 15:39:31 +0200237 trace_hda_bus_reset(bus);
Takashi Iwai8dd78332009-06-02 01:16:07 +0200238 bus->ops.bus_reset(bus);
239 }
240 goto again;
241 }
242 /* clear reset-flag when the communication gets recovered */
Takashi Iwaid846b172012-11-24 11:58:24 +0100243 if (!err || codec_in_pm(codec))
Takashi Iwai8dd78332009-06-02 01:16:07 +0200244 bus->response_reset = 0;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200245 return err;
246}
247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248/**
249 * snd_hda_codec_read - send a command and get the response
250 * @codec: the HDA codec
251 * @nid: NID to send the command
252 * @direct: direct flag
253 * @verb: the verb to send
254 * @parm: the parameter for the verb
255 *
256 * Send a single command and read the corresponding response.
257 *
258 * Returns the obtained response value, or -1 for an error.
259 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200260unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
261 int direct,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 unsigned int verb, unsigned int parm)
263{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200264 unsigned cmd = make_codec_cmd(codec, nid, direct, verb, parm);
265 unsigned int res;
Greg Thelen9857edf2011-06-13 07:45:45 -0700266 if (codec_exec_verb(codec, cmd, &res))
267 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 return res;
269}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100270EXPORT_SYMBOL_HDA(snd_hda_codec_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272/**
273 * snd_hda_codec_write - send a single command without waiting for response
274 * @codec: the HDA codec
275 * @nid: NID to send the command
276 * @direct: direct flag
277 * @verb: the verb to send
278 * @parm: the parameter for the verb
279 *
280 * Send a single command without waiting for response.
281 *
282 * Returns 0 if successful, or a negative error code.
283 */
284int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
285 unsigned int verb, unsigned int parm)
286{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200287 unsigned int cmd = make_codec_cmd(codec, nid, direct, verb, parm);
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100288 unsigned int res;
Takashi Iwaib20f3b82009-06-02 01:20:22 +0200289 return codec_exec_verb(codec, cmd,
290 codec->bus->sync_write ? &res : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100292EXPORT_SYMBOL_HDA(snd_hda_codec_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
294/**
295 * snd_hda_sequence_write - sequence writes
296 * @codec: the HDA codec
297 * @seq: VERB array to send
298 *
299 * Send the commands sequentially from the given array.
300 * The array must be terminated with NID=0.
301 */
302void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
303{
304 for (; seq->nid; seq++)
305 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
306}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100307EXPORT_SYMBOL_HDA(snd_hda_sequence_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309/**
310 * snd_hda_get_sub_nodes - get the range of sub nodes
311 * @codec: the HDA codec
312 * @nid: NID to parse
313 * @start_id: the pointer to store the start NID
314 *
315 * Parse the NID and store the start NID of its sub-nodes.
316 * Returns the number of sub-nodes.
317 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200318int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
319 hda_nid_t *start_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
321 unsigned int parm;
322
323 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
Danny Tholene8a7f132007-09-11 21:41:56 +0200324 if (parm == -1)
325 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 *start_id = (parm >> 16) & 0x7fff;
327 return (int)(parm & 0x7fff);
328}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100329EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200331/* look up the cached results */
332static hda_nid_t *lookup_conn_list(struct snd_array *array, hda_nid_t nid)
333{
334 int i, len;
335 for (i = 0; i < array->used; ) {
336 hda_nid_t *p = snd_array_elem(array, i);
337 if (nid == *p)
338 return p;
339 len = p[1];
340 i += len + 2;
341 }
342 return NULL;
343}
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200344
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200345/* read the connection and add to the cache */
346static int read_and_add_raw_conns(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200347{
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200348 hda_nid_t list[HDA_MAX_CONNECTIONS];
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200349 int len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200350
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200351 len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list));
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200352 if (len < 0)
353 return len;
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200354 return snd_hda_override_conn_list(codec, nid, len, list);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200355}
Takashi Iwaidce20792011-06-24 14:10:28 +0200356
357/**
358 * snd_hda_get_connections - copy connection list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 * @codec: the HDA codec
360 * @nid: NID to parse
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200361 * @conn_list: connection list array; when NULL, checks only the size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 * @max_conns: max. number of connections to store
363 *
364 * Parses the connection list of the given widget and stores the list
365 * of NIDs.
366 *
367 * Returns the number of connections, or a negative error code.
368 */
369int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200370 hda_nid_t *conn_list, int max_conns)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200371{
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200372 struct snd_array *array = &codec->conn_lists;
373 int len;
374 hda_nid_t *p;
375 bool added = false;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200376
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200377 again:
378 mutex_lock(&codec->hash_mutex);
379 len = -1;
380 /* if the connection-list is already cached, read it */
381 p = lookup_conn_list(array, nid);
382 if (p) {
383 len = p[1];
384 if (conn_list && len > max_conns) {
385 snd_printk(KERN_ERR "hda_codec: "
386 "Too many connections %d for NID 0x%x\n",
387 len, nid);
388 mutex_unlock(&codec->hash_mutex);
389 return -EINVAL;
390 }
391 if (conn_list && len)
392 memcpy(conn_list, p + 2, len * sizeof(hda_nid_t));
Takashi Iwaidce20792011-06-24 14:10:28 +0200393 }
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200394 mutex_unlock(&codec->hash_mutex);
395 if (len >= 0)
396 return len;
397 if (snd_BUG_ON(added))
398 return -EINVAL;
399
400 len = read_and_add_raw_conns(codec, nid);
401 if (len < 0)
402 return len;
403 added = true;
404 goto again;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200405}
406EXPORT_SYMBOL_HDA(snd_hda_get_connections);
407
Takashi Iwai9e7717c2011-07-11 15:42:52 +0200408/**
409 * snd_hda_get_raw_connections - copy connection list without cache
410 * @codec: the HDA codec
411 * @nid: NID to parse
412 * @conn_list: connection list array
413 * @max_conns: max. number of connections to store
414 *
415 * Like snd_hda_get_connections(), copy the connection list but without
416 * checking through the connection-list cache.
417 * Currently called only from hda_proc.c, so not exported.
418 */
419int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid,
420 hda_nid_t *conn_list, int max_conns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
422 unsigned int parm;
Takashi Iwai54d17402005-11-21 16:33:22 +0100423 int i, conn_len, conns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 unsigned int shift, num_elems, mask;
Takashi Iwai1ba7a7c2009-07-27 12:56:26 +0200425 unsigned int wcaps;
Takashi Iwai54d17402005-11-21 16:33:22 +0100426 hda_nid_t prev_nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Takashi Iwaida3cec32008-08-08 17:12:14 +0200428 if (snd_BUG_ON(!conn_list || max_conns <= 0))
429 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Takashi Iwai1ba7a7c2009-07-27 12:56:26 +0200431 wcaps = get_wcaps(codec, nid);
432 if (!(wcaps & AC_WCAP_CONN_LIST) &&
Takashi Iwai8d087c72011-06-28 12:45:47 +0200433 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
434 return 0;
Jaroslav Kysela16a433d2009-07-22 16:20:40 +0200435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
437 if (parm & AC_CLIST_LONG) {
438 /* long form */
439 shift = 16;
440 num_elems = 2;
441 } else {
442 /* short form */
443 shift = 8;
444 num_elems = 4;
445 }
446 conn_len = parm & AC_CLIST_LENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 mask = (1 << (shift-1)) - 1;
448
Takashi Iwai0ba21762007-04-16 11:29:14 +0200449 if (!conn_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 return 0; /* no connection */
451
452 if (conn_len == 1) {
453 /* single connection */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200454 parm = snd_hda_codec_read(codec, nid, 0,
455 AC_VERB_GET_CONNECT_LIST, 0);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200456 if (parm == -1 && codec->bus->rirb_error)
457 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 conn_list[0] = parm & mask;
459 return 1;
460 }
461
462 /* multi connection */
463 conns = 0;
Takashi Iwai54d17402005-11-21 16:33:22 +0100464 prev_nid = 0;
465 for (i = 0; i < conn_len; i++) {
466 int range_val;
467 hda_nid_t val, n;
468
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200469 if (i % num_elems == 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +0100470 parm = snd_hda_codec_read(codec, nid, 0,
471 AC_VERB_GET_CONNECT_LIST, i);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200472 if (parm == -1 && codec->bus->rirb_error)
473 return -EIO;
474 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200475 range_val = !!(parm & (1 << (shift-1))); /* ranges */
Takashi Iwai54d17402005-11-21 16:33:22 +0100476 val = parm & mask;
Jaroslav Kysela2e9bf242009-07-18 11:48:19 +0200477 if (val == 0) {
478 snd_printk(KERN_WARNING "hda_codec: "
479 "invalid CONNECT_LIST verb %x[%i]:%x\n",
480 nid, i, parm);
481 return 0;
482 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100483 parm >>= shift;
484 if (range_val) {
485 /* ranges between the previous and this one */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200486 if (!prev_nid || prev_nid >= val) {
487 snd_printk(KERN_WARNING "hda_codec: "
488 "invalid dep_range_val %x:%x\n",
489 prev_nid, val);
Takashi Iwai54d17402005-11-21 16:33:22 +0100490 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100492 for (n = prev_nid + 1; n <= val; n++) {
493 if (conns >= max_conns) {
Takashi Iwai5aacc212010-07-30 10:36:29 +0200494 snd_printk(KERN_ERR "hda_codec: "
495 "Too many connections %d for NID 0x%x\n",
496 conns, nid);
Takashi Iwai54d17402005-11-21 16:33:22 +0100497 return -EINVAL;
498 }
499 conn_list[conns++] = n;
500 }
501 } else {
502 if (conns >= max_conns) {
Takashi Iwai5aacc212010-07-30 10:36:29 +0200503 snd_printk(KERN_ERR "hda_codec: "
504 "Too many connections %d for NID 0x%x\n",
505 conns, nid);
Takashi Iwai54d17402005-11-21 16:33:22 +0100506 return -EINVAL;
507 }
508 conn_list[conns++] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100510 prev_nid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 }
512 return conns;
513}
514
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200515static bool add_conn_list(struct snd_array *array, hda_nid_t nid)
516{
517 hda_nid_t *p = snd_array_new(array);
518 if (!p)
519 return false;
520 *p = nid;
521 return true;
522}
523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524/**
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200525 * snd_hda_override_conn_list - add/modify the connection-list to cache
526 * @codec: the HDA codec
527 * @nid: NID to parse
528 * @len: number of connection list entries
529 * @list: the list of connection entries
530 *
531 * Add or modify the given connection-list to the cache. If the corresponding
532 * cache already exists, invalidate it and append a new one.
533 *
534 * Returns zero or a negative error code.
535 */
536int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
537 const hda_nid_t *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200539 struct snd_array *array = &codec->conn_lists;
540 hda_nid_t *p;
541 int i, old_used;
542
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200543 mutex_lock(&codec->hash_mutex);
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200544 p = lookup_conn_list(array, nid);
545 if (p)
546 *p = -1; /* invalidate the old entry */
547
548 old_used = array->used;
549 if (!add_conn_list(array, nid) || !add_conn_list(array, len))
550 goto error_add;
551 for (i = 0; i < len; i++)
552 if (!add_conn_list(array, list[i]))
553 goto error_add;
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200554 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200555 return 0;
556
557 error_add:
558 array->used = old_used;
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200559 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200560 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561}
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200562EXPORT_SYMBOL_HDA(snd_hda_override_conn_list);
563
564/**
Takashi Iwai8d087c72011-06-28 12:45:47 +0200565 * snd_hda_get_conn_index - get the connection index of the given NID
566 * @codec: the HDA codec
567 * @mux: NID containing the list
568 * @nid: NID to select
569 * @recursive: 1 when searching NID recursively, otherwise 0
570 *
571 * Parses the connection list of the widget @mux and checks whether the
572 * widget @nid is present. If it is, return the connection index.
573 * Otherwise it returns -1.
574 */
575int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
576 hda_nid_t nid, int recursive)
577{
578 hda_nid_t conn[HDA_MAX_NUM_INPUTS];
579 int i, nums;
580
581 nums = snd_hda_get_connections(codec, mux, conn, ARRAY_SIZE(conn));
582 for (i = 0; i < nums; i++)
583 if (conn[i] == nid)
584 return i;
585 if (!recursive)
586 return -1;
587 if (recursive > 5) {
588 snd_printd("hda_codec: too deep connection for 0x%x\n", nid);
589 return -1;
590 }
591 recursive++;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200592 for (i = 0; i < nums; i++) {
593 unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
594 if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
595 continue;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200596 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
597 return i;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200598 }
Takashi Iwai8d087c72011-06-28 12:45:47 +0200599 return -1;
600}
601EXPORT_SYMBOL_HDA(snd_hda_get_conn_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603/**
604 * snd_hda_queue_unsol_event - add an unsolicited event to queue
605 * @bus: the BUS
606 * @res: unsolicited event (lower 32bit of RIRB entry)
607 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
608 *
609 * Adds the given event to the queue. The events are processed in
610 * the workqueue asynchronously. Call this function in the interrupt
611 * hanlder when RIRB receives an unsolicited event.
612 *
613 * Returns 0 if successful, or a negative error code.
614 */
615int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
616{
617 struct hda_bus_unsolicited *unsol;
618 unsigned int wp;
619
Takashi Iwaiecf726f2011-08-09 14:22:44 +0200620 trace_hda_unsol_event(bus, res, res_ex);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200621 unsol = bus->unsol;
622 if (!unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 return 0;
624
625 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
626 unsol->wp = wp;
627
628 wp <<= 1;
629 unsol->queue[wp] = res;
630 unsol->queue[wp + 1] = res_ex;
631
Takashi Iwai6acaed32009-01-12 10:09:24 +0100632 queue_work(bus->workq, &unsol->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634 return 0;
635}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100636EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
638/*
Wu Fengguang5c1d1a92008-10-07 14:17:53 +0800639 * process queued unsolicited events
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 */
David Howellsc4028952006-11-22 14:57:56 +0000641static void process_unsol_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
David Howellsc4028952006-11-22 14:57:56 +0000643 struct hda_bus_unsolicited *unsol =
644 container_of(work, struct hda_bus_unsolicited, work);
645 struct hda_bus *bus = unsol->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 struct hda_codec *codec;
647 unsigned int rp, caddr, res;
648
649 while (unsol->rp != unsol->wp) {
650 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
651 unsol->rp = rp;
652 rp <<= 1;
653 res = unsol->queue[rp];
654 caddr = unsol->queue[rp + 1];
Takashi Iwai0ba21762007-04-16 11:29:14 +0200655 if (!(caddr & (1 << 4))) /* no unsolicited event? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 continue;
657 codec = bus->caddr_tbl[caddr & 0x0f];
658 if (codec && codec->patch_ops.unsol_event)
659 codec->patch_ops.unsol_event(codec, res);
660 }
661}
662
663/*
664 * initialize unsolicited queue
665 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200666static int init_unsol_queue(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
668 struct hda_bus_unsolicited *unsol;
669
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100670 if (bus->unsol) /* already initialized */
671 return 0;
672
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200673 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200674 if (!unsol) {
675 snd_printk(KERN_ERR "hda_codec: "
676 "can't allocate unsolicited queue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 return -ENOMEM;
678 }
David Howellsc4028952006-11-22 14:57:56 +0000679 INIT_WORK(&unsol->work, process_unsol_events);
680 unsol->bus = bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 bus->unsol = unsol;
682 return 0;
683}
684
685/*
686 * destructor
687 */
688static void snd_hda_codec_free(struct hda_codec *codec);
689
690static int snd_hda_bus_free(struct hda_bus *bus)
691{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200692 struct hda_codec *codec, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Takashi Iwai0ba21762007-04-16 11:29:14 +0200694 if (!bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 return 0;
Takashi Iwai6acaed32009-01-12 10:09:24 +0100696 if (bus->workq)
697 flush_workqueue(bus->workq);
698 if (bus->unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 kfree(bus->unsol);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200700 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 snd_hda_codec_free(codec);
702 }
703 if (bus->ops.private_free)
704 bus->ops.private_free(bus);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100705 if (bus->workq)
706 destroy_workqueue(bus->workq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 kfree(bus);
708 return 0;
709}
710
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100711static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
713 struct hda_bus *bus = device->device_data;
Takashi Iwaib94d35392008-11-21 09:08:06 +0100714 bus->shutdown = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 return snd_hda_bus_free(bus);
716}
717
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200718#ifdef CONFIG_SND_HDA_HWDEP
719static int snd_hda_bus_dev_register(struct snd_device *device)
720{
721 struct hda_bus *bus = device->device_data;
722 struct hda_codec *codec;
723 list_for_each_entry(codec, &bus->codec_list, list) {
724 snd_hda_hwdep_add_sysfs(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +0100725 snd_hda_hwdep_add_power_sysfs(codec);
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200726 }
727 return 0;
728}
729#else
730#define snd_hda_bus_dev_register NULL
731#endif
732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733/**
734 * snd_hda_bus_new - create a HDA bus
735 * @card: the card entry
736 * @temp: the template for hda_bus information
737 * @busp: the pointer to store the created bus instance
738 *
739 * Returns 0 if successful, or a negative error code.
740 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +0100741int snd_hda_bus_new(struct snd_card *card,
Takashi Iwai756e2b02007-04-16 11:27:07 +0200742 const struct hda_bus_template *temp,
743 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
745 struct hda_bus *bus;
746 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100747 static struct snd_device_ops dev_ops = {
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200748 .dev_register = snd_hda_bus_dev_register,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 .dev_free = snd_hda_bus_dev_free,
750 };
751
Takashi Iwaida3cec32008-08-08 17:12:14 +0200752 if (snd_BUG_ON(!temp))
753 return -EINVAL;
754 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
755 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
757 if (busp)
758 *busp = NULL;
759
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200760 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 if (bus == NULL) {
762 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
763 return -ENOMEM;
764 }
765
766 bus->card = card;
767 bus->private_data = temp->private_data;
768 bus->pci = temp->pci;
769 bus->modelname = temp->modelname;
Takashi Iwaifee2fba2008-11-27 12:43:28 +0100770 bus->power_save = temp->power_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 bus->ops = temp->ops;
772
Ingo Molnar62932df2006-01-16 16:34:20 +0100773 mutex_init(&bus->cmd_mutex);
Takashi Iwai3f50ac62010-08-20 09:44:36 +0200774 mutex_init(&bus->prepare_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 INIT_LIST_HEAD(&bus->codec_list);
776
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100777 snprintf(bus->workq_name, sizeof(bus->workq_name),
778 "hd-audio%d", card->number);
779 bus->workq = create_singlethread_workqueue(bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100780 if (!bus->workq) {
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100781 snd_printk(KERN_ERR "cannot create workqueue %s\n",
782 bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100783 kfree(bus);
784 return -ENOMEM;
785 }
786
Takashi Iwai0ba21762007-04-16 11:29:14 +0200787 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
788 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 snd_hda_bus_free(bus);
790 return err;
791 }
792 if (busp)
793 *busp = bus;
794 return 0;
795}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100796EXPORT_SYMBOL_HDA(snd_hda_bus_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Takashi Iwai82467612007-07-27 19:15:54 +0200798#ifdef CONFIG_SND_HDA_GENERIC
799#define is_generic_config(codec) \
Takashi Iwaif44ac832008-07-30 15:01:45 +0200800 (codec->modelname && !strcmp(codec->modelname, "generic"))
Takashi Iwai82467612007-07-27 19:15:54 +0200801#else
802#define is_generic_config(codec) 0
803#endif
804
Takashi Iwai645f10c2008-11-28 15:07:37 +0100805#ifdef MODULE
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100806#define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
807#else
Takashi Iwai645f10c2008-11-28 15:07:37 +0100808#define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100809#endif
810
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811/*
812 * find a matching codec preset
813 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200814static const struct hda_codec_preset *
Takashi Iwai756e2b02007-04-16 11:27:07 +0200815find_codec_preset(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816{
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100817 struct hda_codec_preset_list *tbl;
818 const struct hda_codec_preset *preset;
Takashi Iwai5d908ab2012-08-24 18:40:10 +0200819 unsigned int mod_requested = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Takashi Iwai82467612007-07-27 19:15:54 +0200821 if (is_generic_config(codec))
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100822 return NULL; /* use the generic parser */
823
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100824 again:
825 mutex_lock(&preset_mutex);
826 list_for_each_entry(tbl, &hda_preset_tables, list) {
827 if (!try_module_get(tbl->owner)) {
828 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
829 continue;
830 }
831 for (preset = tbl->preset; preset->id; preset++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 u32 mask = preset->mask;
Marc Boucherca7cfae2008-01-22 15:32:25 +0100833 if (preset->afg && preset->afg != codec->afg)
834 continue;
835 if (preset->mfg && preset->mfg != codec->mfg)
836 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200837 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 mask = ~0;
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200839 if (preset->id == (codec->vendor_id & mask) &&
Takashi Iwai0ba21762007-04-16 11:29:14 +0200840 (!preset->rev ||
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100841 preset->rev == codec->revision_id)) {
842 mutex_unlock(&preset_mutex);
843 codec->owner = tbl->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 return preset;
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100845 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 }
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100847 module_put(tbl->owner);
848 }
849 mutex_unlock(&preset_mutex);
850
851 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
852 char name[32];
853 if (!mod_requested)
854 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
855 codec->vendor_id);
856 else
857 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
858 (codec->vendor_id >> 16) & 0xffff);
859 request_module(name);
860 mod_requested++;
861 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 }
863 return NULL;
864}
865
866/*
Takashi Iwaif44ac832008-07-30 15:01:45 +0200867 * get_codec_name - store the codec name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 */
Takashi Iwaif44ac832008-07-30 15:01:45 +0200869static int get_codec_name(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870{
871 const struct hda_vendor_id *c;
872 const char *vendor = NULL;
873 u16 vendor_id = codec->vendor_id >> 16;
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200874 char tmp[16];
875
876 if (codec->vendor_name)
877 goto get_chip_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
879 for (c = hda_vendor_ids; c->id; c++) {
880 if (c->id == vendor_id) {
881 vendor = c->name;
882 break;
883 }
884 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200885 if (!vendor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 sprintf(tmp, "Generic %04x", vendor_id);
887 vendor = tmp;
888 }
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200889 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
890 if (!codec->vendor_name)
891 return -ENOMEM;
892
893 get_chip_name:
894 if (codec->chip_name)
895 return 0;
896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 if (codec->preset && codec->preset->name)
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200898 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
899 else {
900 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
901 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
902 }
903 if (!codec->chip_name)
Takashi Iwaif44ac832008-07-30 15:01:45 +0200904 return -ENOMEM;
905 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906}
907
908/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200909 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +0100911static void setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912{
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200913 int i, total_nodes, function_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 hda_nid_t nid;
915
916 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
917 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200918 function_id = snd_hda_param_read(codec, nid,
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200919 AC_PAR_FUNCTION_TYPE);
Jaroslav Kyselacd7643b2010-07-20 12:11:25 +0200920 switch (function_id & 0xff) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200921 case AC_GRP_AUDIO_FUNCTION:
922 codec->afg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200923 codec->afg_function_id = function_id & 0xff;
924 codec->afg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200925 break;
926 case AC_GRP_MODEM_FUNCTION:
927 codec->mfg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200928 codec->mfg_function_id = function_id & 0xff;
929 codec->mfg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200930 break;
931 default:
932 break;
933 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935}
936
937/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100938 * read widget caps for each widget and store in cache
939 */
940static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
941{
942 int i;
943 hda_nid_t nid;
944
945 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
946 &codec->start_nid);
947 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200948 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +0100949 return -ENOMEM;
950 nid = codec->start_nid;
951 for (i = 0; i < codec->num_nodes; i++, nid++)
952 codec->wcaps[i] = snd_hda_param_read(codec, nid,
953 AC_PAR_AUDIO_WIDGET_CAP);
954 return 0;
955}
956
Takashi Iwai3be14142009-02-20 14:11:16 +0100957/* read all pin default configurations and save codec->init_pins */
958static int read_pin_defaults(struct hda_codec *codec)
959{
960 int i;
961 hda_nid_t nid = codec->start_nid;
962
963 for (i = 0; i < codec->num_nodes; i++, nid++) {
964 struct hda_pincfg *pin;
965 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +0200966 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwai3be14142009-02-20 14:11:16 +0100967 if (wid_type != AC_WID_PIN)
968 continue;
969 pin = snd_array_new(&codec->init_pins);
970 if (!pin)
971 return -ENOMEM;
972 pin->nid = nid;
973 pin->cfg = snd_hda_codec_read(codec, nid, 0,
974 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200975 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
976 AC_VERB_GET_PIN_WIDGET_CONTROL,
977 0);
Takashi Iwai3be14142009-02-20 14:11:16 +0100978 }
979 return 0;
980}
981
982/* look up the given pin config list and return the item matching with NID */
983static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
984 struct snd_array *array,
985 hda_nid_t nid)
986{
987 int i;
988 for (i = 0; i < array->used; i++) {
989 struct hda_pincfg *pin = snd_array_elem(array, i);
990 if (pin->nid == nid)
991 return pin;
992 }
993 return NULL;
994}
995
Takashi Iwai3be14142009-02-20 14:11:16 +0100996/* set the current pin config value for the given NID.
997 * the value is cached, and read via snd_hda_codec_get_pincfg()
998 */
999int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
1000 hda_nid_t nid, unsigned int cfg)
1001{
1002 struct hda_pincfg *pin;
1003
Takashi Iwaib82855a2009-12-27 11:24:56 +01001004 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
1005 return -EINVAL;
1006
Takashi Iwai3be14142009-02-20 14:11:16 +01001007 pin = look_up_pincfg(codec, list, nid);
1008 if (!pin) {
1009 pin = snd_array_new(list);
1010 if (!pin)
1011 return -ENOMEM;
1012 pin->nid = nid;
1013 }
1014 pin->cfg = cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001015 return 0;
1016}
1017
Takashi Iwaid5191e52009-11-16 14:58:17 +01001018/**
1019 * snd_hda_codec_set_pincfg - Override a pin default configuration
1020 * @codec: the HDA codec
1021 * @nid: NID to set the pin config
1022 * @cfg: the pin default config value
1023 *
1024 * Override a pin default configuration value in the cache.
1025 * This value can be read by snd_hda_codec_get_pincfg() in a higher
1026 * priority than the real hardware value.
1027 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001028int snd_hda_codec_set_pincfg(struct hda_codec *codec,
1029 hda_nid_t nid, unsigned int cfg)
1030{
Takashi Iwai346ff702009-02-23 09:42:57 +01001031 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001032}
1033EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
1034
Takashi Iwaid5191e52009-11-16 14:58:17 +01001035/**
1036 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
1037 * @codec: the HDA codec
1038 * @nid: NID to get the pin config
1039 *
1040 * Get the current pin config value of the given pin NID.
1041 * If the pincfg value is cached or overridden via sysfs or driver,
1042 * returns the cached value.
1043 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001044unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
1045{
1046 struct hda_pincfg *pin;
1047
Takashi Iwai3be14142009-02-20 14:11:16 +01001048#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +01001049 pin = look_up_pincfg(codec, &codec->user_pins, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +01001050 if (pin)
1051 return pin->cfg;
1052#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001053 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
1054 if (pin)
1055 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001056 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1057 if (pin)
1058 return pin->cfg;
1059 return 0;
1060}
1061EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
1062
Takashi Iwai92ee6162009-12-27 11:18:59 +01001063/**
1064 * snd_hda_shutup_pins - Shut up all pins
1065 * @codec: the HDA codec
1066 *
1067 * Clear all pin controls to shup up before suspend for avoiding click noise.
1068 * The controls aren't cached so that they can be resumed properly.
1069 */
1070void snd_hda_shutup_pins(struct hda_codec *codec)
1071{
1072 int i;
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001073 /* don't shut up pins when unloading the driver; otherwise it breaks
1074 * the default pin setup at the next load of the driver
1075 */
1076 if (codec->bus->shutdown)
1077 return;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001078 for (i = 0; i < codec->init_pins.used; i++) {
1079 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1080 /* use read here for syncing after issuing each verb */
1081 snd_hda_codec_read(codec, pin->nid, 0,
1082 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
1083 }
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001084 codec->pins_shutup = 1;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001085}
1086EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);
1087
Takashi Iwai2a439522011-07-26 09:52:50 +02001088#ifdef CONFIG_PM
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001089/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1090static void restore_shutup_pins(struct hda_codec *codec)
1091{
1092 int i;
1093 if (!codec->pins_shutup)
1094 return;
1095 if (codec->bus->shutdown)
1096 return;
1097 for (i = 0; i < codec->init_pins.used; i++) {
1098 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1099 snd_hda_codec_write(codec, pin->nid, 0,
1100 AC_VERB_SET_PIN_WIDGET_CONTROL,
1101 pin->ctrl);
1102 }
1103 codec->pins_shutup = 0;
1104}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001105#endif
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001106
David Henningsson26a6cb62012-10-09 15:04:21 +02001107static void hda_jackpoll_work(struct work_struct *work)
1108{
1109 struct hda_codec *codec =
1110 container_of(work, struct hda_codec, jackpoll_work.work);
1111 if (!codec->jackpoll_interval)
1112 return;
1113
1114 snd_hda_jack_set_dirty_all(codec);
1115 snd_hda_jack_poll_all(codec);
1116 queue_delayed_work(codec->bus->workq, &codec->jackpoll_work,
1117 codec->jackpoll_interval);
1118}
1119
Takashi Iwai01751f52007-08-10 16:59:39 +02001120static void init_hda_cache(struct hda_cache_rec *cache,
1121 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001122static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +02001123
Takashi Iwai3fdf1462012-11-22 08:16:31 +01001124/* release all pincfg lists */
1125static void free_init_pincfgs(struct hda_codec *codec)
Takashi Iwai3be14142009-02-20 14:11:16 +01001126{
Takashi Iwai346ff702009-02-23 09:42:57 +01001127 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001128#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +01001129 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001130#endif
Takashi Iwai3be14142009-02-20 14:11:16 +01001131 snd_array_free(&codec->init_pins);
1132}
1133
Takashi Iwai54d17402005-11-21 16:33:22 +01001134/*
Takashi Iwaieb541332010-08-06 13:48:11 +02001135 * audio-converter setup caches
1136 */
1137struct hda_cvt_setup {
1138 hda_nid_t nid;
1139 u8 stream_tag;
1140 u8 channel_id;
1141 u16 format_id;
1142 unsigned char active; /* cvt is currently used */
1143 unsigned char dirty; /* setups should be cleared */
1144};
1145
1146/* get or create a cache entry for the given audio converter NID */
1147static struct hda_cvt_setup *
1148get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1149{
1150 struct hda_cvt_setup *p;
1151 int i;
1152
1153 for (i = 0; i < codec->cvt_setups.used; i++) {
1154 p = snd_array_elem(&codec->cvt_setups, i);
1155 if (p->nid == nid)
1156 return p;
1157 }
1158 p = snd_array_new(&codec->cvt_setups);
1159 if (p)
1160 p->nid = nid;
1161 return p;
1162}
1163
1164/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 * codec destructor
1166 */
1167static void snd_hda_codec_free(struct hda_codec *codec)
1168{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001169 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 return;
David Henningsson26a6cb62012-10-09 15:04:21 +02001171 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai59cad162012-06-26 15:00:20 +02001172 snd_hda_jack_tbl_clear(codec);
Takashi Iwai3fdf1462012-11-22 08:16:31 +01001173 free_init_pincfgs(codec);
Takashi Iwai83012a72012-08-24 18:38:08 +02001174#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02001175 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01001176 flush_workqueue(codec->bus->workq);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001177#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001179 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001180 snd_array_free(&codec->nids);
Takashi Iwai59cad162012-06-26 15:00:20 +02001181 snd_array_free(&codec->cvt_setups);
Takashi Iwaia12d3e12011-04-07 15:55:15 +02001182 snd_array_free(&codec->conn_lists);
Stephen Warren7c9359762011-06-01 11:14:17 -06001183 snd_array_free(&codec->spdif_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 codec->bus->caddr_tbl[codec->addr] = NULL;
1185 if (codec->patch_ops.free)
1186 codec->patch_ops.free(codec);
Takashi Iwai83012a72012-08-24 18:38:08 +02001187#ifdef CONFIG_PM
Takashi Iwai08fa20a2012-08-31 07:46:56 -07001188 if (!codec->pm_down_notified) /* cancel leftover refcounts */
Takashi Iwai68467f52012-08-28 09:14:29 -07001189 hda_call_pm_notify(codec->bus, false);
1190#endif
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001191 module_put(codec->owner);
Takashi Iwai01751f52007-08-10 16:59:39 +02001192 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001193 free_hda_cache(&codec->cmd_cache);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001194 kfree(codec->vendor_name);
1195 kfree(codec->chip_name);
Takashi Iwaif44ac832008-07-30 15:01:45 +02001196 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +01001197 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 kfree(codec);
1199}
1200
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001201static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec,
1202 hda_nid_t fg, unsigned int power_state);
1203
Takashi Iwaid8193872012-08-31 07:54:38 -07001204static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001205 unsigned int power_state);
1206
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207/**
1208 * snd_hda_codec_new - create a HDA codec
1209 * @bus: the bus to assign
1210 * @codec_addr: the codec address
1211 * @codecp: the pointer to store the generated codec
1212 *
1213 * Returns 0 if successful, or a negative error code.
1214 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001215int snd_hda_codec_new(struct hda_bus *bus,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001216 unsigned int codec_addr,
1217 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218{
1219 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +02001220 char component[31];
Takashi Iwaid8193872012-08-31 07:54:38 -07001221 hda_nid_t fg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 int err;
1223
Takashi Iwaida3cec32008-08-08 17:12:14 +02001224 if (snd_BUG_ON(!bus))
1225 return -EINVAL;
1226 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1227 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
1229 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001230 snd_printk(KERN_ERR "hda_codec: "
1231 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 return -EBUSY;
1233 }
1234
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001235 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 if (codec == NULL) {
1237 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1238 return -ENOMEM;
1239 }
1240
1241 codec->bus = bus;
1242 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001243 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001244 mutex_init(&codec->control_mutex);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001245 mutex_init(&codec->hash_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +02001246 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001247 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001248 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1249 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001250 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001251 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +02001252 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Takashi Iwaia12d3e12011-04-07 15:55:15 +02001253 snd_array_init(&codec->conn_lists, sizeof(hda_nid_t), 64);
Stephen Warren7c9359762011-06-01 11:14:17 -06001254 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
Takashi Iwai361dab32012-05-09 14:35:27 +02001255 snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01001256 snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8);
David Henningsson26a6cb62012-10-09 15:04:21 +02001257 INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Takashi Iwai83012a72012-08-24 18:38:08 +02001259#ifdef CONFIG_PM
Takashi Iwai5536c6d2012-05-08 17:08:10 +02001260 spin_lock_init(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001261 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1262 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1263 * the caller has to power down appropriatley after initialization
1264 * phase.
1265 */
1266 hda_keep_power_on(codec);
Takashi Iwai68467f52012-08-28 09:14:29 -07001267 hda_call_pm_notify(bus, true);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001268#endif
1269
Takashi Iwaic382a9f2012-05-07 15:01:02 +02001270 if (codec->bus->modelname) {
1271 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1272 if (!codec->modelname) {
1273 snd_hda_codec_free(codec);
1274 return -ENODEV;
1275 }
1276 }
1277
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 list_add_tail(&codec->list, &bus->codec_list);
1279 bus->caddr_tbl[codec_addr] = codec;
1280
Takashi Iwai0ba21762007-04-16 11:29:14 +02001281 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1282 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001283 if (codec->vendor_id == -1)
1284 /* read again, hopefully the access method was corrected
1285 * in the last read...
1286 */
1287 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1288 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001289 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1290 AC_PAR_SUBSYSTEM_ID);
1291 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1292 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001294 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001295 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001296 snd_printdd("hda_codec: no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001297 err = -ENODEV;
1298 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 }
1300
Takashi Iwaid8193872012-08-31 07:54:38 -07001301 fg = codec->afg ? codec->afg : codec->mfg;
1302 err = read_widget_caps(codec, fg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001303 if (err < 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +01001304 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001305 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001306 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001307 err = read_pin_defaults(codec);
1308 if (err < 0)
1309 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001310
Takashi Iwai0ba21762007-04-16 11:29:14 +02001311 if (!codec->subsystem_id) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001312 codec->subsystem_id =
Takashi Iwaid8193872012-08-31 07:54:38 -07001313 snd_hda_codec_read(codec, fg, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001314 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001315 }
1316
Takashi Iwai83012a72012-08-24 18:38:08 +02001317#ifdef CONFIG_PM
Takashi Iwaid8193872012-08-31 07:54:38 -07001318 codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec, fg,
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001319 AC_PWRST_CLKSTOP);
1320 if (!codec->d3_stop_clk)
1321 bus->power_keep_link_on = 1;
Mengdong Lin5d6147f2012-08-24 12:06:30 +08001322#endif
Takashi Iwaid8193872012-08-31 07:54:38 -07001323 codec->epss = snd_hda_codec_get_supported_ps(codec, fg,
Takashi Iwai983f6b92012-08-28 09:18:01 -07001324 AC_PWRST_EPSS);
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001325
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001326 /* power-up all before initialization */
Takashi Iwaid8193872012-08-31 07:54:38 -07001327 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001328
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001329 snd_hda_codec_proc_new(codec);
1330
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001331 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001332
1333 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1334 codec->subsystem_id, codec->revision_id);
1335 snd_component_add(codec->bus->card, component);
1336
1337 if (codecp)
1338 *codecp = codec;
1339 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001340
1341 error:
1342 snd_hda_codec_free(codec);
1343 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001344}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001345EXPORT_SYMBOL_HDA(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001346
Takashi Iwaid5191e52009-11-16 14:58:17 +01001347/**
1348 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1349 * @codec: the HDA codec
1350 *
1351 * Start parsing of the given codec tree and (re-)initialize the whole
1352 * patch instance.
1353 *
1354 * Returns 0 if successful or a negative error code.
1355 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001356int snd_hda_codec_configure(struct hda_codec *codec)
1357{
1358 int err;
1359
Takashi Iwaid5ad6302007-03-07 15:55:59 +01001360 codec->preset = find_codec_preset(codec);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001361 if (!codec->vendor_name || !codec->chip_name) {
Takashi Iwaif44ac832008-07-30 15:01:45 +02001362 err = get_codec_name(codec);
1363 if (err < 0)
1364 return err;
1365 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
Takashi Iwai82467612007-07-27 19:15:54 +02001367 if (is_generic_config(codec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai82467612007-07-27 19:15:54 +02001369 goto patched;
1370 }
Takashi Iwai82467612007-07-27 19:15:54 +02001371 if (codec->preset && codec->preset->patch) {
1372 err = codec->preset->patch(codec);
1373 goto patched;
1374 }
1375
1376 /* call the default parser */
Takashi Iwai82467612007-07-27 19:15:54 +02001377 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai35a1e0c2007-10-19 08:13:40 +02001378 if (err < 0)
1379 printk(KERN_ERR "hda-codec: No codec parser is available\n");
Takashi Iwai82467612007-07-27 19:15:54 +02001380
1381 patched:
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001382 if (!err && codec->patch_ops.unsol_event)
1383 err = init_unsol_queue(codec->bus);
Takashi Iwaif62faed2009-12-23 09:27:51 +01001384 /* audio codec should override the mixer name */
1385 if (!err && (codec->afg || !*codec->bus->card->mixername))
1386 snprintf(codec->bus->card->mixername,
1387 sizeof(codec->bus->card->mixername),
1388 "%s %s", codec->vendor_name, codec->chip_name);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001389 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390}
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001391EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
Takashi Iwaied360812012-08-08 17:12:52 +02001393/* update the stream-id if changed */
1394static void update_pcm_stream_id(struct hda_codec *codec,
1395 struct hda_cvt_setup *p, hda_nid_t nid,
1396 u32 stream_tag, int channel_id)
1397{
1398 unsigned int oldval, newval;
1399
1400 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1401 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1402 newval = (stream_tag << 4) | channel_id;
1403 if (oldval != newval)
1404 snd_hda_codec_write(codec, nid, 0,
1405 AC_VERB_SET_CHANNEL_STREAMID,
1406 newval);
1407 p->stream_tag = stream_tag;
1408 p->channel_id = channel_id;
1409 }
1410}
1411
1412/* update the format-id if changed */
1413static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1414 hda_nid_t nid, int format)
1415{
1416 unsigned int oldval;
1417
1418 if (p->format_id != format) {
1419 oldval = snd_hda_codec_read(codec, nid, 0,
1420 AC_VERB_GET_STREAM_FORMAT, 0);
1421 if (oldval != format) {
1422 msleep(1);
1423 snd_hda_codec_write(codec, nid, 0,
1424 AC_VERB_SET_STREAM_FORMAT,
1425 format);
1426 }
1427 p->format_id = format;
1428 }
1429}
1430
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431/**
1432 * snd_hda_codec_setup_stream - set up the codec for streaming
1433 * @codec: the CODEC to set up
1434 * @nid: the NID to set up
1435 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1436 * @channel_id: channel id to pass, zero based.
1437 * @format: stream format.
1438 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001439void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1440 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 int channel_id, int format)
1442{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001443 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001444 struct hda_cvt_setup *p;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001445 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001446 int i;
1447
Takashi Iwai0ba21762007-04-16 11:29:14 +02001448 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001449 return;
1450
Takashi Iwai0ba21762007-04-16 11:29:14 +02001451 snd_printdd("hda_codec_setup_stream: "
1452 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001454 p = get_hda_cvt_setup(codec, nid);
1455 if (!p)
1456 return;
Takashi Iwaied360812012-08-08 17:12:52 +02001457
1458 if (codec->pcm_format_first)
1459 update_pcm_format(codec, p, nid, format);
1460 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1461 if (!codec->pcm_format_first)
1462 update_pcm_format(codec, p, nid, format);
1463
Takashi Iwaieb541332010-08-06 13:48:11 +02001464 p->active = 1;
1465 p->dirty = 0;
1466
1467 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001468 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001469 list_for_each_entry(c, &codec->bus->codec_list, list) {
1470 for (i = 0; i < c->cvt_setups.used; i++) {
1471 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001472 if (!p->active && p->stream_tag == stream_tag &&
David Henningsson54c2a892012-02-01 12:05:41 +01001473 get_wcaps_type(get_wcaps(c, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001474 p->dirty = 1;
1475 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001478EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
Takashi Iwaif0cea792010-08-13 11:56:53 +02001480static void really_cleanup_stream(struct hda_codec *codec,
1481 struct hda_cvt_setup *q);
1482
Takashi Iwaid5191e52009-11-16 14:58:17 +01001483/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001484 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001485 * @codec: the CODEC to clean up
1486 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001487 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001488 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001489void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1490 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001491{
Takashi Iwaieb541332010-08-06 13:48:11 +02001492 struct hda_cvt_setup *p;
1493
Takashi Iwai888afa12008-03-18 09:57:50 +01001494 if (!nid)
1495 return;
1496
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001497 if (codec->no_sticky_stream)
1498 do_now = 1;
1499
Takashi Iwai888afa12008-03-18 09:57:50 +01001500 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001501 p = get_hda_cvt_setup(codec, nid);
Takashi Iwaif0cea792010-08-13 11:56:53 +02001502 if (p) {
1503 /* here we just clear the active flag when do_now isn't set;
1504 * actual clean-ups will be done later in
1505 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1506 */
1507 if (do_now)
1508 really_cleanup_stream(codec, p);
1509 else
1510 p->active = 0;
1511 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001512}
Takashi Iwaif0cea792010-08-13 11:56:53 +02001513EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001514
Takashi Iwaieb541332010-08-06 13:48:11 +02001515static void really_cleanup_stream(struct hda_codec *codec,
1516 struct hda_cvt_setup *q)
1517{
1518 hda_nid_t nid = q->nid;
Takashi Iwai218264a2011-09-27 17:33:45 +02001519 if (q->stream_tag || q->channel_id)
1520 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1521 if (q->format_id)
1522 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1523);
Takashi Iwaieb541332010-08-06 13:48:11 +02001524 memset(q, 0, sizeof(*q));
1525 q->nid = nid;
1526}
1527
1528/* clean up the all conflicting obsolete streams */
1529static void purify_inactive_streams(struct hda_codec *codec)
1530{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001531 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001532 int i;
1533
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001534 list_for_each_entry(c, &codec->bus->codec_list, list) {
1535 for (i = 0; i < c->cvt_setups.used; i++) {
1536 struct hda_cvt_setup *p;
1537 p = snd_array_elem(&c->cvt_setups, i);
1538 if (p->dirty)
1539 really_cleanup_stream(c, p);
1540 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001541 }
1542}
1543
Takashi Iwai2a439522011-07-26 09:52:50 +02001544#ifdef CONFIG_PM
Takashi Iwaieb541332010-08-06 13:48:11 +02001545/* clean up all streams; called from suspend */
1546static void hda_cleanup_all_streams(struct hda_codec *codec)
1547{
1548 int i;
1549
1550 for (i = 0; i < codec->cvt_setups.used; i++) {
1551 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1552 if (p->stream_tag)
1553 really_cleanup_stream(codec, p);
1554 }
1555}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001556#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001557
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558/*
1559 * amp access functions
1560 */
1561
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001562/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001563#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001564#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001565#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1566#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001568#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
1570/* initialize the hash table */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001571static void init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001572 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573{
Takashi Iwai01751f52007-08-10 16:59:39 +02001574 memset(cache, 0, sizeof(*cache));
1575 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001576 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001577}
1578
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001579static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001580{
Takashi Iwai603c4012008-07-30 15:01:44 +02001581 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582}
1583
1584/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001585static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586{
Takashi Iwai01751f52007-08-10 16:59:39 +02001587 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1588 u16 cur = cache->hash[idx];
1589 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
1591 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001592 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 if (info->key == key)
1594 return info;
1595 cur = info->next;
1596 }
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001597 return NULL;
1598}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001600/* query the hash. allocate an entry if not found. */
1601static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1602 u32 key)
1603{
1604 struct hda_cache_head *info = get_hash(cache, key);
1605 if (!info) {
1606 u16 idx, cur;
1607 /* add a new hash entry */
1608 info = snd_array_new(&cache->buf);
1609 if (!info)
1610 return NULL;
1611 cur = snd_array_index(&cache->buf, info);
1612 info->key = key;
1613 info->val = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01001614 info->dirty = 0;
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001615 idx = key % (u16)ARRAY_SIZE(cache->hash);
1616 info->next = cache->hash[idx];
1617 cache->hash[idx] = cur;
1618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 return info;
1620}
1621
Takashi Iwai01751f52007-08-10 16:59:39 +02001622/* query and allocate an amp hash entry */
1623static inline struct hda_amp_info *
1624get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1625{
1626 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1627}
1628
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001629/* overwrite the value with the key in the caps hash */
1630static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val)
1631{
1632 struct hda_amp_info *info;
1633
1634 mutex_lock(&codec->hash_mutex);
1635 info = get_alloc_amp_hash(codec, key);
1636 if (!info) {
1637 mutex_unlock(&codec->hash_mutex);
1638 return -EINVAL;
1639 }
1640 info->amp_caps = val;
1641 info->head.val |= INFO_AMP_CAPS;
1642 mutex_unlock(&codec->hash_mutex);
1643 return 0;
1644}
1645
1646/* query the value from the caps hash; if not found, fetch the current
1647 * value from the given function and store in the hash
1648 */
1649static unsigned int
1650query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key,
1651 unsigned int (*func)(struct hda_codec *, hda_nid_t, int))
1652{
1653 struct hda_amp_info *info;
1654 unsigned int val;
1655
1656 mutex_lock(&codec->hash_mutex);
1657 info = get_alloc_amp_hash(codec, key);
1658 if (!info) {
1659 mutex_unlock(&codec->hash_mutex);
1660 return 0;
1661 }
1662 if (!(info->head.val & INFO_AMP_CAPS)) {
1663 mutex_unlock(&codec->hash_mutex); /* for reentrance */
1664 val = func(codec, nid, dir);
1665 write_caps_hash(codec, key, val);
1666 } else {
1667 val = info->amp_caps;
1668 mutex_unlock(&codec->hash_mutex);
1669 }
1670 return val;
1671}
1672
1673static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid,
1674 int direction)
1675{
1676 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1677 nid = codec->afg;
1678 return snd_hda_param_read(codec, nid,
1679 direction == HDA_OUTPUT ?
1680 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
1681}
1682
Takashi Iwaid5191e52009-11-16 14:58:17 +01001683/**
1684 * query_amp_caps - query AMP capabilities
1685 * @codec: the HD-auio codec
1686 * @nid: the NID to query
1687 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1688 *
1689 * Query AMP capabilities for the given widget and direction.
1690 * Returns the obtained capability bits.
1691 *
1692 * When cap bits have been already read, this doesn't read again but
1693 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001695u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001697 return query_caps_hash(codec, nid, direction,
1698 HDA_HASH_KEY(nid, direction, 0),
1699 read_amp_cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001701EXPORT_SYMBOL_HDA(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
Takashi Iwaid5191e52009-11-16 14:58:17 +01001703/**
1704 * snd_hda_override_amp_caps - Override the AMP capabilities
1705 * @codec: the CODEC to clean up
1706 * @nid: the NID to clean up
1707 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1708 * @caps: the capability bits to set
1709 *
1710 * Override the cached AMP caps bits value by the given one.
1711 * This function is useful if the driver needs to adjust the AMP ranges,
1712 * e.g. limit to 0dB, etc.
1713 *
1714 * Returns zero if successful or a negative error code.
1715 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001716int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1717 unsigned int caps)
1718{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001719 return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001720}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001721EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001722
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001723static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid,
1724 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001725{
1726 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1727}
1728
Takashi Iwaid5191e52009-11-16 14:58:17 +01001729/**
1730 * snd_hda_query_pin_caps - Query PIN capabilities
1731 * @codec: the HD-auio codec
1732 * @nid: the NID to query
1733 *
1734 * Query PIN capabilities for the given widget.
1735 * Returns the obtained capability bits.
1736 *
1737 * When cap bits have been already read, this doesn't read again but
1738 * returns the cached value.
1739 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001740u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1741{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001742 return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001743 read_pin_cap);
1744}
Takashi Iwai1327a322009-03-23 13:07:47 +01001745EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1746
Wu Fengguang864f92b2009-11-18 12:38:02 +08001747/**
Takashi Iwaif57c2562011-08-15 12:49:07 +02001748 * snd_hda_override_pin_caps - Override the pin capabilities
1749 * @codec: the CODEC
1750 * @nid: the NID to override
1751 * @caps: the capability bits to set
1752 *
1753 * Override the cached PIN capabilitiy bits value by the given one.
1754 *
1755 * Returns zero if successful or a negative error code.
1756 */
1757int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
1758 unsigned int caps)
1759{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001760 return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps);
Takashi Iwaif57c2562011-08-15 12:49:07 +02001761}
1762EXPORT_SYMBOL_HDA(snd_hda_override_pin_caps);
1763
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001764/* read or sync the hash value with the current value;
1765 * call within hash_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 */
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001767static struct hda_amp_info *
1768update_amp_hash(struct hda_codec *codec, hda_nid_t nid, int ch,
Takashi Iwai280e57d2012-12-14 10:32:21 +01001769 int direction, int index, bool init_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001771 struct hda_amp_info *info;
1772 unsigned int parm, val = 0;
1773 bool val_read = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001775 retry:
1776 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1777 if (!info)
1778 return NULL;
1779 if (!(info->head.val & INFO_AMP_VOL(ch))) {
1780 if (!val_read) {
1781 mutex_unlock(&codec->hash_mutex);
1782 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1783 parm |= direction == HDA_OUTPUT ?
1784 AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1785 parm |= index;
1786 val = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001787 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001788 val &= 0xff;
1789 val_read = true;
1790 mutex_lock(&codec->hash_mutex);
1791 goto retry;
1792 }
1793 info->vol[ch] = val;
1794 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001795 } else if (init_only)
1796 return NULL;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001797 return info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798}
1799
1800/*
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001801 * write the current volume in info to the h/w
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001803static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001804 hda_nid_t nid, int ch, int direction, int index,
1805 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806{
1807 u32 parm;
1808
1809 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1810 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1811 parm |= index << AC_AMP_SET_INDEX_SHIFT;
Takashi Iwai38681372012-02-27 15:00:58 +01001812 if ((val & HDA_AMP_MUTE) && !(info->amp_caps & AC_AMPCAP_MUTE) &&
1813 (info->amp_caps & AC_AMPCAP_MIN_MUTE))
1814 ; /* set the zero value as a fake mute */
1815 else
1816 parm |= val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
1818}
1819
Takashi Iwaid5191e52009-11-16 14:58:17 +01001820/**
1821 * snd_hda_codec_amp_read - Read AMP value
1822 * @codec: HD-audio codec
1823 * @nid: NID to read the AMP value
1824 * @ch: channel (left=0 or right=1)
1825 * @direction: #HDA_INPUT or #HDA_OUTPUT
1826 * @index: the index value (only for input direction)
1827 *
1828 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 */
Takashi Iwai834be882006-03-01 14:16:17 +01001830int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1831 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001833 struct hda_amp_info *info;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001834 unsigned int val = 0;
1835
1836 mutex_lock(&codec->hash_mutex);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001837 info = update_amp_hash(codec, nid, ch, direction, index, false);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001838 if (info)
1839 val = info->vol[ch];
1840 mutex_unlock(&codec->hash_mutex);
1841 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001843EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
Takashi Iwai280e57d2012-12-14 10:32:21 +01001845static int codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1846 int direction, int idx, int mask, int val,
1847 bool init_only)
1848{
1849 struct hda_amp_info *info;
1850
1851 if (snd_BUG_ON(mask & ~0xff))
1852 mask &= 0xff;
1853 val &= mask;
1854
1855 mutex_lock(&codec->hash_mutex);
1856 info = update_amp_hash(codec, nid, ch, direction, idx, init_only);
1857 if (!info) {
1858 mutex_unlock(&codec->hash_mutex);
1859 return 0;
1860 }
1861 val |= info->vol[ch] & ~mask;
1862 if (info->vol[ch] == val) {
1863 mutex_unlock(&codec->hash_mutex);
1864 return 0;
1865 }
1866 info->vol[ch] = val;
1867 if (codec->cached_write)
1868 info->head.dirty = 1;
1869 mutex_unlock(&codec->hash_mutex);
1870 if (!codec->cached_write)
1871 put_vol_mute(codec, info, nid, ch, direction, idx, val);
1872 return 1;
1873}
1874
Takashi Iwaid5191e52009-11-16 14:58:17 +01001875/**
1876 * snd_hda_codec_amp_update - update the AMP value
1877 * @codec: HD-audio codec
1878 * @nid: NID to read the AMP value
1879 * @ch: channel (left=0 or right=1)
1880 * @direction: #HDA_INPUT or #HDA_OUTPUT
1881 * @idx: the index value (only for input direction)
1882 * @mask: bit mask to set
1883 * @val: the bits value to set
1884 *
1885 * Update the AMP value with a bit mask.
1886 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001887 */
Takashi Iwai834be882006-03-01 14:16:17 +01001888int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1889 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890{
Takashi Iwai280e57d2012-12-14 10:32:21 +01001891 return codec_amp_update(codec, nid, ch, direction, idx, mask, val, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001893EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
Takashi Iwaid5191e52009-11-16 14:58:17 +01001895/**
1896 * snd_hda_codec_amp_stereo - update the AMP stereo values
1897 * @codec: HD-audio codec
1898 * @nid: NID to read the AMP value
1899 * @direction: #HDA_INPUT or #HDA_OUTPUT
1900 * @idx: the index value (only for input direction)
1901 * @mask: bit mask to set
1902 * @val: the bits value to set
1903 *
1904 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1905 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02001906 */
1907int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1908 int direction, int idx, int mask, int val)
1909{
1910 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001911
1912 if (snd_BUG_ON(mask & ~0xff))
1913 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02001914 for (ch = 0; ch < 2; ch++)
1915 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1916 idx, mask, val);
1917 return ret;
1918}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001919EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001920
Takashi Iwai280e57d2012-12-14 10:32:21 +01001921/* Works like snd_hda_codec_amp_update() but it writes the value only at
1922 * the first access. If the amp was already initialized / updated beforehand,
1923 * this does nothing.
1924 */
1925int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
1926 int dir, int idx, int mask, int val)
1927{
1928 return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true);
1929}
1930EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init);
1931
1932int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
1933 int dir, int idx, int mask, int val)
1934{
1935 int ch, ret = 0;
1936
1937 if (snd_BUG_ON(mask & ~0xff))
1938 mask &= 0xff;
1939 for (ch = 0; ch < 2; ch++)
1940 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
1941 idx, mask, val);
1942 return ret;
1943}
1944EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init_stereo);
1945
Takashi Iwaid5191e52009-11-16 14:58:17 +01001946/**
1947 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
1948 * @codec: HD-audio codec
1949 *
1950 * Resume the all amp commands from the cache.
1951 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001952void snd_hda_codec_resume_amp(struct hda_codec *codec)
1953{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001954 int i;
1955
Takashi Iwaic370dd62012-12-13 18:30:04 +01001956 mutex_lock(&codec->hash_mutex);
1957 for (i = 0; i < codec->amp_cache.buf.used; i++) {
1958 struct hda_amp_info *buffer;
1959 u32 key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001960 hda_nid_t nid;
1961 unsigned int idx, dir, ch;
Takashi Iwaic370dd62012-12-13 18:30:04 +01001962
1963 buffer = snd_array_elem(&codec->amp_cache.buf, i);
1964 key = buffer->head.key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001965 if (!key)
1966 continue;
1967 nid = key & 0xff;
1968 idx = (key >> 16) & 0xff;
1969 dir = (key >> 24) & 0xff;
1970 for (ch = 0; ch < 2; ch++) {
1971 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
1972 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01001973 if (!buffer->head.dirty)
1974 continue;
1975 buffer->head.dirty = 0;
1976 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001977 put_vol_mute(codec, buffer, nid, ch, dir, idx,
1978 buffer->vol[ch]);
Takashi Iwaic370dd62012-12-13 18:30:04 +01001979 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001980 }
1981 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01001982 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001983}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001984EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001986static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1987 unsigned int ofs)
1988{
1989 u32 caps = query_amp_caps(codec, nid, dir);
1990 /* get num steps */
1991 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1992 if (ofs < caps)
1993 caps -= ofs;
1994 return caps;
1995}
1996
Takashi Iwaid5191e52009-11-16 14:58:17 +01001997/**
1998 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
1999 *
2000 * The control element is supposed to have the private_value field
2001 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2002 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002003int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
2004 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005{
2006 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2007 u16 nid = get_amp_nid(kcontrol);
2008 u8 chs = get_amp_channels(kcontrol);
2009 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002010 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002012 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2013 uinfo->count = chs == 3 ? 2 : 1;
2014 uinfo->value.integer.min = 0;
2015 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
2016 if (!uinfo->value.integer.max) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002017 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01002018 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
2019 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 return -EINVAL;
2021 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 return 0;
2023}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002024EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002026
2027static inline unsigned int
2028read_amp_value(struct hda_codec *codec, hda_nid_t nid,
2029 int ch, int dir, int idx, unsigned int ofs)
2030{
2031 unsigned int val;
2032 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
2033 val &= HDA_AMP_VOLMASK;
2034 if (val >= ofs)
2035 val -= ofs;
2036 else
2037 val = 0;
2038 return val;
2039}
2040
2041static inline int
2042update_amp_value(struct hda_codec *codec, hda_nid_t nid,
2043 int ch, int dir, int idx, unsigned int ofs,
2044 unsigned int val)
2045{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002046 unsigned int maxval;
2047
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002048 if (val > 0)
2049 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02002050 /* ofs = 0: raw max value */
2051 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002052 if (val > maxval)
2053 val = maxval;
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002054 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
2055 HDA_AMP_VOLMASK, val);
2056}
2057
Takashi Iwaid5191e52009-11-16 14:58:17 +01002058/**
2059 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
2060 *
2061 * The control element is supposed to have the private_value field
2062 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2063 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002064int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
2065 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066{
2067 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2068 hda_nid_t nid = get_amp_nid(kcontrol);
2069 int chs = get_amp_channels(kcontrol);
2070 int dir = get_amp_direction(kcontrol);
2071 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002072 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 long *valp = ucontrol->value.integer.value;
2074
2075 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002076 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002078 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 return 0;
2080}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002081EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082
Takashi Iwaid5191e52009-11-16 14:58:17 +01002083/**
2084 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
2085 *
2086 * The control element is supposed to have the private_value field
2087 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2088 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002089int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
2090 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091{
2092 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2093 hda_nid_t nid = get_amp_nid(kcontrol);
2094 int chs = get_amp_channels(kcontrol);
2095 int dir = get_amp_direction(kcontrol);
2096 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002097 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 long *valp = ucontrol->value.integer.value;
2099 int change = 0;
2100
Takashi Iwaicb53c622007-08-10 17:21:45 +02002101 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002102 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002103 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002104 valp++;
2105 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002106 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002107 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002108 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 return change;
2110}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002111EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
Takashi Iwaid5191e52009-11-16 14:58:17 +01002113/**
2114 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2115 *
2116 * The control element is supposed to have the private_value field
2117 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2118 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002119int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2120 unsigned int size, unsigned int __user *_tlv)
2121{
2122 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2123 hda_nid_t nid = get_amp_nid(kcontrol);
2124 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002125 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02002126 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002127 u32 caps, val1, val2;
2128
2129 if (size < 4 * sizeof(unsigned int))
2130 return -ENOMEM;
2131 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002132 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2133 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002134 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002135 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002136 val1 = ((int)val1) * ((int)val2);
Takashi Iwai38681372012-02-27 15:00:58 +01002137 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwaic08d9162010-10-17 10:40:53 +02002138 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002139 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2140 return -EFAULT;
2141 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2142 return -EFAULT;
2143 if (put_user(val1, _tlv + 2))
2144 return -EFAULT;
2145 if (put_user(val2, _tlv + 3))
2146 return -EFAULT;
2147 return 0;
2148}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002149EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002150
Takashi Iwaid5191e52009-11-16 14:58:17 +01002151/**
2152 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2153 * @codec: HD-audio codec
2154 * @nid: NID of a reference widget
2155 * @dir: #HDA_INPUT or #HDA_OUTPUT
2156 * @tlv: TLV data to be stored, at least 4 elements
2157 *
2158 * Set (static) TLV data for a virtual master volume using the AMP caps
2159 * obtained from the reference NID.
2160 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01002161 */
2162void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2163 unsigned int *tlv)
2164{
2165 u32 caps;
2166 int nums, step;
2167
2168 caps = query_amp_caps(codec, nid, dir);
2169 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2170 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2171 step = (step + 1) * 25;
2172 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2173 tlv[1] = 2 * sizeof(unsigned int);
2174 tlv[2] = -nums * step;
2175 tlv[3] = step;
2176}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002177EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002178
2179/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01002180static struct snd_kcontrol *
Takashi Iwaidcda5802012-10-12 17:24:51 +02002181find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002182{
2183 struct snd_ctl_elem_id id;
2184 memset(&id, 0, sizeof(id));
2185 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwaidcda5802012-10-12 17:24:51 +02002186 id.device = dev;
Takashi Iwai09f99702008-02-04 12:31:13 +01002187 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02002188 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2189 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002190 strcpy(id.name, name);
2191 return snd_ctl_find_id(codec->bus->card, &id);
2192}
2193
Takashi Iwaid5191e52009-11-16 14:58:17 +01002194/**
2195 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2196 * @codec: HD-audio codec
2197 * @name: ctl id name string
2198 *
2199 * Get the control element with the given id string and IFACE_MIXER.
2200 */
Takashi Iwai09f99702008-02-04 12:31:13 +01002201struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2202 const char *name)
2203{
Takashi Iwaidcda5802012-10-12 17:24:51 +02002204 return find_mixer_ctl(codec, name, 0, 0);
Takashi Iwai09f99702008-02-04 12:31:13 +01002205}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002206EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01002207
Takashi Iwaidcda5802012-10-12 17:24:51 +02002208static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
2209 int dev)
Takashi Iwai1afe2062010-12-23 10:17:52 +01002210{
2211 int idx;
2212 for (idx = 0; idx < 16; idx++) { /* 16 ctlrs should be large enough */
Takashi Iwaidcda5802012-10-12 17:24:51 +02002213 if (!find_mixer_ctl(codec, name, dev, idx))
Takashi Iwai1afe2062010-12-23 10:17:52 +01002214 return idx;
2215 }
2216 return -EBUSY;
2217}
2218
Takashi Iwaid5191e52009-11-16 14:58:17 +01002219/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002220 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01002221 * @codec: HD-audio codec
2222 * @nid: corresponding NID (optional)
2223 * @kctl: the control element to assign
2224 *
2225 * Add the given control element to an array inside the codec instance.
2226 * All control elements belonging to a codec are supposed to be added
2227 * by this function so that a proper clean-up works at the free or
2228 * reconfiguration time.
2229 *
2230 * If non-zero @nid is passed, the NID is assigned to the control element.
2231 * The assignment is shown in the codec proc file.
2232 *
2233 * snd_hda_ctl_add() checks the control subdev id field whether
2234 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002235 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2236 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01002237 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002238int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2239 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002240{
2241 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002242 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002243 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002244
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002245 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002246 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002247 if (nid == 0)
2248 nid = get_amp_nid_(kctl->private_value);
2249 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002250 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2251 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002252 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01002253 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002254 err = snd_ctl_add(codec->bus->card, kctl);
2255 if (err < 0)
2256 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002257 item = snd_array_new(&codec->mixers);
2258 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002259 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002260 item->kctl = kctl;
2261 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002262 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002263 return 0;
2264}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002265EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002266
Takashi Iwaid5191e52009-11-16 14:58:17 +01002267/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002268 * snd_hda_add_nid - Assign a NID to a control element
2269 * @codec: HD-audio codec
2270 * @nid: corresponding NID (optional)
2271 * @kctl: the control element to assign
2272 * @index: index to kctl
2273 *
2274 * Add the given control element to an array inside the codec instance.
2275 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2276 * NID:KCTL mapping - for example "Capture Source" selector.
2277 */
2278int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2279 unsigned int index, hda_nid_t nid)
2280{
2281 struct hda_nid_item *item;
2282
2283 if (nid > 0) {
2284 item = snd_array_new(&codec->nids);
2285 if (!item)
2286 return -ENOMEM;
2287 item->kctl = kctl;
2288 item->index = index;
2289 item->nid = nid;
2290 return 0;
2291 }
Takashi Iwai28d1a852010-03-15 09:05:46 +01002292 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
2293 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002294 return -EINVAL;
2295}
2296EXPORT_SYMBOL_HDA(snd_hda_add_nid);
2297
2298/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002299 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2300 * @codec: HD-audio codec
2301 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002302void snd_hda_ctls_clear(struct hda_codec *codec)
2303{
2304 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002305 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002306 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002307 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002308 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002309 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002310}
2311
Takashi Iwaia65d6292009-02-23 16:57:04 +01002312/* pseudo device locking
2313 * toggle card->shutdown to allow/disallow the device access (as a hack)
2314 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002315int snd_hda_lock_devices(struct hda_bus *bus)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002316{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002317 struct snd_card *card = bus->card;
2318 struct hda_codec *codec;
2319
Takashi Iwaia65d6292009-02-23 16:57:04 +01002320 spin_lock(&card->files_lock);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002321 if (card->shutdown)
2322 goto err_unlock;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002323 card->shutdown = 1;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002324 if (!list_empty(&card->ctl_files))
2325 goto err_clear;
2326
2327 list_for_each_entry(codec, &bus->codec_list, list) {
2328 int pcm;
2329 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2330 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2331 if (!cpcm->pcm)
2332 continue;
2333 if (cpcm->pcm->streams[0].substream_opened ||
2334 cpcm->pcm->streams[1].substream_opened)
2335 goto err_clear;
2336 }
2337 }
Takashi Iwaia65d6292009-02-23 16:57:04 +01002338 spin_unlock(&card->files_lock);
2339 return 0;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002340
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002341 err_clear:
2342 card->shutdown = 0;
2343 err_unlock:
2344 spin_unlock(&card->files_lock);
2345 return -EINVAL;
2346}
2347EXPORT_SYMBOL_HDA(snd_hda_lock_devices);
2348
2349void snd_hda_unlock_devices(struct hda_bus *bus)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002350{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002351 struct snd_card *card = bus->card;
2352
2353 card = bus->card;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002354 spin_lock(&card->files_lock);
2355 card->shutdown = 0;
2356 spin_unlock(&card->files_lock);
2357}
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002358EXPORT_SYMBOL_HDA(snd_hda_unlock_devices);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002359
Takashi Iwaid5191e52009-11-16 14:58:17 +01002360/**
2361 * snd_hda_codec_reset - Clear all objects assigned to the codec
2362 * @codec: HD-audio codec
2363 *
2364 * This frees the all PCM and control elements assigned to the codec, and
2365 * clears the caches and restores the pin default configurations.
2366 *
2367 * When a device is being used, it returns -EBSY. If successfully freed,
2368 * returns zero.
2369 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002370int snd_hda_codec_reset(struct hda_codec *codec)
2371{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002372 struct hda_bus *bus = codec->bus;
2373 struct snd_card *card = bus->card;
2374 int i;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002375
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002376 if (snd_hda_lock_devices(bus) < 0)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002377 return -EBUSY;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002378
2379 /* OK, let it free */
David Henningsson26a6cb62012-10-09 15:04:21 +02002380 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai83012a72012-08-24 18:38:08 +02002381#ifdef CONFIG_PM
Takashi Iwaia2d96e72012-05-09 12:36:22 +02002382 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwai339876d2012-05-08 16:57:12 +02002383 codec->power_on = 0;
2384 codec->power_transition = 0;
2385 codec->power_jiffies = jiffies;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002386 flush_workqueue(bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002387#endif
2388 snd_hda_ctls_clear(codec);
2389 /* relase PCMs */
2390 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002391 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01002392 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002393 clear_bit(codec->pcm_info[i].device,
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002394 bus->pcm_dev_bits);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002395 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002396 }
2397 if (codec->patch_ops.free)
2398 codec->patch_ops.free(codec);
Takashi Iwai07dc59f2012-09-10 09:39:31 +02002399 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
Takashi Iwai1835a0f2011-10-27 22:12:46 +02002400 snd_hda_jack_tbl_clear(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01002401 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002402 codec->spec = NULL;
2403 free_hda_cache(&codec->amp_cache);
2404 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01002405 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2406 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01002407 /* free only driver_pins so that init_pins + user_pins are restored */
2408 snd_array_free(&codec->driver_pins);
Takashi Iwai09a60712012-06-26 15:01:33 +02002409 snd_array_free(&codec->cvt_setups);
2410 snd_array_free(&codec->spdif_out);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01002411 snd_array_free(&codec->verbs);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002412 codec->num_pcms = 0;
2413 codec->pcm_info = NULL;
2414 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01002415 codec->slave_dig_outs = NULL;
2416 codec->spdif_status_reset = 0;
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002417 module_put(codec->owner);
2418 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002419
2420 /* allow device access again */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002421 snd_hda_unlock_devices(bus);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002422 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002423}
2424
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002425typedef int (*map_slave_func_t)(void *, struct snd_kcontrol *);
2426
2427/* apply the function to all matching slave ctls in the mixer list */
2428static int map_slaves(struct hda_codec *codec, const char * const *slaves,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002429 const char *suffix, map_slave_func_t func, void *data)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002430{
2431 struct hda_nid_item *items;
2432 const char * const *s;
2433 int i, err;
2434
2435 items = codec->mixers.list;
2436 for (i = 0; i < codec->mixers.used; i++) {
2437 struct snd_kcontrol *sctl = items[i].kctl;
2438 if (!sctl || !sctl->id.name ||
2439 sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
2440 continue;
2441 for (s = slaves; *s; s++) {
Takashi Iwai9322ca52012-02-03 14:28:01 +01002442 char tmpname[sizeof(sctl->id.name)];
2443 const char *name = *s;
2444 if (suffix) {
2445 snprintf(tmpname, sizeof(tmpname), "%s %s",
2446 name, suffix);
2447 name = tmpname;
2448 }
Takashi Iwai9322ca52012-02-03 14:28:01 +01002449 if (!strcmp(sctl->id.name, name)) {
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002450 err = func(data, sctl);
2451 if (err)
2452 return err;
2453 break;
2454 }
2455 }
2456 }
2457 return 0;
2458}
2459
2460static int check_slave_present(void *data, struct snd_kcontrol *sctl)
2461{
2462 return 1;
2463}
2464
Takashi Iwai18478e82012-03-09 17:51:10 +01002465/* guess the value corresponding to 0dB */
2466static int get_kctl_0dB_offset(struct snd_kcontrol *kctl)
2467{
2468 int _tlv[4];
2469 const int *tlv = NULL;
2470 int val = -1;
2471
2472 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2473 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2474 mm_segment_t fs = get_fs();
2475 set_fs(get_ds());
2476 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2477 tlv = _tlv;
2478 set_fs(fs);
2479 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2480 tlv = kctl->tlv.p;
2481 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE)
2482 val = -tlv[2] / tlv[3];
2483 return val;
2484}
2485
2486/* call kctl->put with the given value(s) */
2487static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2488{
2489 struct snd_ctl_elem_value *ucontrol;
2490 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2491 if (!ucontrol)
2492 return -ENOMEM;
2493 ucontrol->value.integer.value[0] = val;
2494 ucontrol->value.integer.value[1] = val;
2495 kctl->put(kctl, ucontrol);
2496 kfree(ucontrol);
2497 return 0;
2498}
2499
2500/* initialize the slave volume with 0dB */
2501static int init_slave_0dB(void *data, struct snd_kcontrol *slave)
2502{
2503 int offset = get_kctl_0dB_offset(slave);
2504 if (offset > 0)
2505 put_kctl_with_value(slave, offset);
2506 return 0;
2507}
2508
2509/* unmute the slave */
2510static int init_slave_unmute(void *data, struct snd_kcontrol *slave)
2511{
2512 return put_kctl_with_value(slave, 1);
2513}
2514
Takashi Iwaid5191e52009-11-16 14:58:17 +01002515/**
2516 * snd_hda_add_vmaster - create a virtual master control and add slaves
2517 * @codec: HD-audio codec
2518 * @name: vmaster control name
2519 * @tlv: TLV data (optional)
2520 * @slaves: slave control names (optional)
Takashi Iwai9322ca52012-02-03 14:28:01 +01002521 * @suffix: suffix string to each slave name (optional)
Takashi Iwai18478e82012-03-09 17:51:10 +01002522 * @init_slave_vol: initialize slaves to unmute/0dB
Takashi Iwai29e58532012-03-12 12:25:03 +01002523 * @ctl_ret: store the vmaster kcontrol in return
Takashi Iwaid5191e52009-11-16 14:58:17 +01002524 *
2525 * Create a virtual master control with the given name. The TLV data
2526 * must be either NULL or a valid data.
2527 *
2528 * @slaves is a NULL-terminated array of strings, each of which is a
2529 * slave control name. All controls with these names are assigned to
2530 * the new virtual master control.
2531 *
2532 * This function returns zero if successful or a negative error code.
2533 */
Takashi Iwai18478e82012-03-09 17:51:10 +01002534int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002535 unsigned int *tlv, const char * const *slaves,
Takashi Iwai29e58532012-03-12 12:25:03 +01002536 const char *suffix, bool init_slave_vol,
2537 struct snd_kcontrol **ctl_ret)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002538{
2539 struct snd_kcontrol *kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002540 int err;
2541
Takashi Iwai29e58532012-03-12 12:25:03 +01002542 if (ctl_ret)
2543 *ctl_ret = NULL;
2544
Takashi Iwai9322ca52012-02-03 14:28:01 +01002545 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002546 if (err != 1) {
Takashi Iwai2f085542008-02-22 18:43:50 +01002547 snd_printdd("No slave found for %s\n", name);
2548 return 0;
2549 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002550 kctl = snd_ctl_make_virtual_master(name, tlv);
2551 if (!kctl)
2552 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002553 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002554 if (err < 0)
2555 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002556
Takashi Iwai9322ca52012-02-03 14:28:01 +01002557 err = map_slaves(codec, slaves, suffix,
2558 (map_slave_func_t)snd_ctl_add_slave, kctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002559 if (err < 0)
2560 return err;
Takashi Iwai18478e82012-03-09 17:51:10 +01002561
2562 /* init with master mute & zero volume */
2563 put_kctl_with_value(kctl, 0);
2564 if (init_slave_vol)
2565 map_slaves(codec, slaves, suffix,
2566 tlv ? init_slave_0dB : init_slave_unmute, kctl);
2567
Takashi Iwai29e58532012-03-12 12:25:03 +01002568 if (ctl_ret)
2569 *ctl_ret = kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002570 return 0;
2571}
Takashi Iwai18478e82012-03-09 17:51:10 +01002572EXPORT_SYMBOL_HDA(__snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002573
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002574/*
2575 * mute-LED control using vmaster
2576 */
2577static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2578 struct snd_ctl_elem_info *uinfo)
2579{
2580 static const char * const texts[] = {
David Henningssonc86c2d42013-01-03 14:12:29 +01002581 "On", "Off", "Follow Master"
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002582 };
2583 unsigned int index;
2584
2585 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2586 uinfo->count = 1;
2587 uinfo->value.enumerated.items = 3;
2588 index = uinfo->value.enumerated.item;
2589 if (index >= 3)
2590 index = 2;
2591 strcpy(uinfo->value.enumerated.name, texts[index]);
2592 return 0;
2593}
2594
2595static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2596 struct snd_ctl_elem_value *ucontrol)
2597{
2598 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2599 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2600 return 0;
2601}
2602
2603static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2604 struct snd_ctl_elem_value *ucontrol)
2605{
2606 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2607 unsigned int old_mode = hook->mute_mode;
2608
2609 hook->mute_mode = ucontrol->value.enumerated.item[0];
2610 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2611 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2612 if (old_mode == hook->mute_mode)
2613 return 0;
2614 snd_hda_sync_vmaster_hook(hook);
2615 return 1;
2616}
2617
2618static struct snd_kcontrol_new vmaster_mute_mode = {
2619 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2620 .name = "Mute-LED Mode",
2621 .info = vmaster_mute_mode_info,
2622 .get = vmaster_mute_mode_get,
2623 .put = vmaster_mute_mode_put,
2624};
2625
2626/*
2627 * Add a mute-LED hook with the given vmaster switch kctl
2628 * "Mute-LED Mode" control is automatically created and associated with
2629 * the given hook.
2630 */
2631int snd_hda_add_vmaster_hook(struct hda_codec *codec,
Takashi Iwaif29735c2012-03-13 07:55:10 +01002632 struct hda_vmaster_mute_hook *hook,
2633 bool expose_enum_ctl)
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002634{
2635 struct snd_kcontrol *kctl;
2636
2637 if (!hook->hook || !hook->sw_kctl)
2638 return 0;
2639 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
2640 hook->codec = codec;
2641 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
Takashi Iwaif29735c2012-03-13 07:55:10 +01002642 if (!expose_enum_ctl)
2643 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002644 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2645 if (!kctl)
2646 return -ENOMEM;
2647 return snd_hda_ctl_add(codec, 0, kctl);
2648}
2649EXPORT_SYMBOL_HDA(snd_hda_add_vmaster_hook);
2650
2651/*
2652 * Call the hook with the current value for synchronization
2653 * Should be called in init callback
2654 */
2655void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2656{
2657 if (!hook->hook || !hook->codec)
2658 return;
2659 switch (hook->mute_mode) {
2660 case HDA_VMUTE_FOLLOW_MASTER:
2661 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2662 break;
2663 default:
2664 hook->hook(hook->codec, hook->mute_mode);
2665 break;
2666 }
2667}
2668EXPORT_SYMBOL_HDA(snd_hda_sync_vmaster_hook);
2669
2670
Takashi Iwaid5191e52009-11-16 14:58:17 +01002671/**
2672 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2673 *
2674 * The control element is supposed to have the private_value field
2675 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2676 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002677int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2678 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679{
2680 int chs = get_amp_channels(kcontrol);
2681
2682 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2683 uinfo->count = chs == 3 ? 2 : 1;
2684 uinfo->value.integer.min = 0;
2685 uinfo->value.integer.max = 1;
2686 return 0;
2687}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002688EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689
Takashi Iwaid5191e52009-11-16 14:58:17 +01002690/**
2691 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2692 *
2693 * The control element is supposed to have the private_value field
2694 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2695 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002696int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2697 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698{
2699 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2700 hda_nid_t nid = get_amp_nid(kcontrol);
2701 int chs = get_amp_channels(kcontrol);
2702 int dir = get_amp_direction(kcontrol);
2703 int idx = get_amp_index(kcontrol);
2704 long *valp = ucontrol->value.integer.value;
2705
2706 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002707 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002708 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002710 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002711 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 return 0;
2713}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002714EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715
Takashi Iwaid5191e52009-11-16 14:58:17 +01002716/**
2717 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2718 *
2719 * The control element is supposed to have the private_value field
2720 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2721 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002722int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2723 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724{
2725 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2726 hda_nid_t nid = get_amp_nid(kcontrol);
2727 int chs = get_amp_channels(kcontrol);
2728 int dir = get_amp_direction(kcontrol);
2729 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 long *valp = ucontrol->value.integer.value;
2731 int change = 0;
2732
Takashi Iwaicb53c622007-08-10 17:21:45 +02002733 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002734 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002735 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002736 HDA_AMP_MUTE,
2737 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002738 valp++;
2739 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002740 if (chs & 2)
2741 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002742 HDA_AMP_MUTE,
2743 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002744 hda_call_check_power_status(codec, nid);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002745 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 return change;
2747}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002748EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749
2750/*
Takashi Iwai985be542005-11-02 18:26:49 +01002751 * bound volume controls
2752 *
2753 * bind multiple volumes (# indices, from 0)
2754 */
2755
2756#define AMP_VAL_IDX_SHIFT 19
2757#define AMP_VAL_IDX_MASK (0x0f<<19)
2758
Takashi Iwaid5191e52009-11-16 14:58:17 +01002759/**
2760 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2761 *
2762 * The control element is supposed to have the private_value field
2763 * set up via HDA_BIND_MUTE*() macros.
2764 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002765int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2766 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002767{
2768 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2769 unsigned long pval;
2770 int err;
2771
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002772 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002773 pval = kcontrol->private_value;
2774 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2775 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2776 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002777 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002778 return err;
2779}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002780EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002781
Takashi Iwaid5191e52009-11-16 14:58:17 +01002782/**
2783 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2784 *
2785 * The control element is supposed to have the private_value field
2786 * set up via HDA_BIND_MUTE*() macros.
2787 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002788int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2789 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002790{
2791 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2792 unsigned long pval;
2793 int i, indices, err = 0, change = 0;
2794
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002795 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002796 pval = kcontrol->private_value;
2797 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2798 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002799 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2800 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002801 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2802 if (err < 0)
2803 break;
2804 change |= err;
2805 }
2806 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002807 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002808 return err < 0 ? err : change;
2809}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002810EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002811
Takashi Iwaid5191e52009-11-16 14:58:17 +01002812/**
2813 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2814 *
2815 * The control element is supposed to have the private_value field
2816 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002817 */
2818int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2819 struct snd_ctl_elem_info *uinfo)
2820{
2821 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2822 struct hda_bind_ctls *c;
2823 int err;
2824
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002825 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002826 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002827 kcontrol->private_value = *c->values;
2828 err = c->ops->info(kcontrol, uinfo);
2829 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002830 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002831 return err;
2832}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002833EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002834
Takashi Iwaid5191e52009-11-16 14:58:17 +01002835/**
2836 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2837 *
2838 * The control element is supposed to have the private_value field
2839 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2840 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002841int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2842 struct snd_ctl_elem_value *ucontrol)
2843{
2844 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2845 struct hda_bind_ctls *c;
2846 int err;
2847
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002848 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002849 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002850 kcontrol->private_value = *c->values;
2851 err = c->ops->get(kcontrol, ucontrol);
2852 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002853 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002854 return err;
2855}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002856EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002857
Takashi Iwaid5191e52009-11-16 14:58:17 +01002858/**
2859 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2860 *
2861 * The control element is supposed to have the private_value field
2862 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2863 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002864int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2865 struct snd_ctl_elem_value *ucontrol)
2866{
2867 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2868 struct hda_bind_ctls *c;
2869 unsigned long *vals;
2870 int err = 0, change = 0;
2871
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002872 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002873 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002874 for (vals = c->values; *vals; vals++) {
2875 kcontrol->private_value = *vals;
2876 err = c->ops->put(kcontrol, ucontrol);
2877 if (err < 0)
2878 break;
2879 change |= err;
2880 }
2881 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002882 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002883 return err < 0 ? err : change;
2884}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002885EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02002886
Takashi Iwaid5191e52009-11-16 14:58:17 +01002887/**
2888 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2889 *
2890 * The control element is supposed to have the private_value field
2891 * set up via HDA_BIND_VOL() macro.
2892 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002893int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2894 unsigned int size, unsigned int __user *tlv)
2895{
2896 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2897 struct hda_bind_ctls *c;
2898 int err;
2899
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002900 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002901 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002902 kcontrol->private_value = *c->values;
2903 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2904 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002905 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002906 return err;
2907}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002908EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02002909
2910struct hda_ctl_ops snd_hda_bind_vol = {
2911 .info = snd_hda_mixer_amp_volume_info,
2912 .get = snd_hda_mixer_amp_volume_get,
2913 .put = snd_hda_mixer_amp_volume_put,
2914 .tlv = snd_hda_mixer_amp_tlv
2915};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002916EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02002917
2918struct hda_ctl_ops snd_hda_bind_sw = {
2919 .info = snd_hda_mixer_amp_switch_info,
2920 .get = snd_hda_mixer_amp_switch_get,
2921 .put = snd_hda_mixer_amp_switch_put,
2922 .tlv = snd_hda_mixer_amp_tlv
2923};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002924EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02002925
2926/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 * SPDIF out controls
2928 */
2929
Takashi Iwai0ba21762007-04-16 11:29:14 +02002930static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2931 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932{
2933 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2934 uinfo->count = 1;
2935 return 0;
2936}
2937
Takashi Iwai0ba21762007-04-16 11:29:14 +02002938static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2939 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940{
2941 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2942 IEC958_AES0_NONAUDIO |
2943 IEC958_AES0_CON_EMPHASIS_5015 |
2944 IEC958_AES0_CON_NOT_COPYRIGHT;
2945 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2946 IEC958_AES1_CON_ORIGINAL;
2947 return 0;
2948}
2949
Takashi Iwai0ba21762007-04-16 11:29:14 +02002950static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2951 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952{
2953 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2954 IEC958_AES0_NONAUDIO |
2955 IEC958_AES0_PRO_EMPHASIS_5015;
2956 return 0;
2957}
2958
Takashi Iwai0ba21762007-04-16 11:29:14 +02002959static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2960 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961{
2962 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002963 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002964 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002966 mutex_lock(&codec->spdif_mutex);
2967 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c9359762011-06-01 11:14:17 -06002968 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
2969 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
2970 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
2971 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002972 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973
2974 return 0;
2975}
2976
2977/* convert from SPDIF status bits to HDA SPDIF bits
2978 * bit 0 (DigEn) is always set zero (to be filled later)
2979 */
2980static unsigned short convert_from_spdif_status(unsigned int sbits)
2981{
2982 unsigned short val = 0;
2983
2984 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002985 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002987 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002989 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2990 IEC958_AES0_PRO_EMPHASIS_5015)
2991 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002993 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2994 IEC958_AES0_CON_EMPHASIS_5015)
2995 val |= AC_DIG1_EMPHASIS;
2996 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2997 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02002999 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
3001 }
3002 return val;
3003}
3004
3005/* convert to SPDIF status bits from HDA SPDIF bits
3006 */
3007static unsigned int convert_to_spdif_status(unsigned short val)
3008{
3009 unsigned int sbits = 0;
3010
Takashi Iwai0ba21762007-04-16 11:29:14 +02003011 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003013 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 sbits |= IEC958_AES0_PROFESSIONAL;
3015 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003016 if (sbits & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
3018 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003019 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003021 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003023 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
3025 sbits |= val & (0x7f << 8);
3026 }
3027 return sbits;
3028}
3029
Takashi Iwai2f728532008-09-25 16:32:41 +02003030/* set digital convert verbs both for the given NID and its slaves */
3031static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
3032 int verb, int val)
3033{
Takashi Iwaidda14412011-05-02 11:29:30 +02003034 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02003035
Takashi Iwai9e976972008-11-25 08:17:20 +01003036 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003037 d = codec->slave_dig_outs;
3038 if (!d)
3039 return;
3040 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01003041 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003042}
3043
3044static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
3045 int dig1, int dig2)
3046{
3047 if (dig1 != -1)
3048 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
3049 if (dig2 != -1)
3050 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
3051}
3052
Takashi Iwai0ba21762007-04-16 11:29:14 +02003053static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
3054 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055{
3056 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06003057 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003058 struct hda_spdif_out *spdif;
3059 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 unsigned short val;
3061 int change;
3062
Ingo Molnar62932df2006-01-16 16:34:20 +01003063 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003064 spdif = snd_array_elem(&codec->spdif_out, idx);
3065 nid = spdif->nid;
Stephen Warren7c9359762011-06-01 11:14:17 -06003066 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
3068 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
3069 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c9359762011-06-01 11:14:17 -06003070 val = convert_from_spdif_status(spdif->status);
3071 val |= spdif->ctls & 1;
3072 change = spdif->ctls != val;
3073 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003074 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02003075 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01003076 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 return change;
3078}
3079
Takashi Iwaia5ce8892007-07-23 15:42:26 +02003080#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081
Takashi Iwai0ba21762007-04-16 11:29:14 +02003082static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
3083 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084{
3085 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06003086 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003087 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003089 mutex_lock(&codec->spdif_mutex);
3090 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c9359762011-06-01 11:14:17 -06003091 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003092 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 return 0;
3094}
3095
Stephen Warren74b654c2011-06-01 11:14:18 -06003096static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
3097 int dig1, int dig2)
3098{
3099 set_dig_out_convert(codec, nid, dig1, dig2);
3100 /* unmute amp switch (if any) */
3101 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
3102 (dig1 & AC_DIG1_ENABLE))
3103 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
3104 HDA_AMP_MUTE, 0);
3105}
3106
Takashi Iwai0ba21762007-04-16 11:29:14 +02003107static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
3108 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109{
3110 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06003111 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003112 struct hda_spdif_out *spdif;
3113 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 unsigned short val;
3115 int change;
3116
Ingo Molnar62932df2006-01-16 16:34:20 +01003117 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003118 spdif = snd_array_elem(&codec->spdif_out, idx);
3119 nid = spdif->nid;
Stephen Warren7c9359762011-06-01 11:14:17 -06003120 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02003122 val |= AC_DIG1_ENABLE;
Stephen Warren7c9359762011-06-01 11:14:17 -06003123 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003124 spdif->ctls = val;
3125 if (change && nid != (u16)-1)
3126 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01003127 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 return change;
3129}
3130
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003131static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 {
3133 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3134 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003135 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 .info = snd_hda_spdif_mask_info,
3137 .get = snd_hda_spdif_cmask_get,
3138 },
3139 {
3140 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3141 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003142 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 .info = snd_hda_spdif_mask_info,
3144 .get = snd_hda_spdif_pmask_get,
3145 },
3146 {
3147 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003148 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 .info = snd_hda_spdif_mask_info,
3150 .get = snd_hda_spdif_default_get,
3151 .put = snd_hda_spdif_default_put,
3152 },
3153 {
3154 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003155 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 .info = snd_hda_spdif_out_switch_info,
3157 .get = snd_hda_spdif_out_switch_get,
3158 .put = snd_hda_spdif_out_switch_put,
3159 },
3160 { } /* end */
3161};
3162
3163/**
Takashi Iwaidcda5802012-10-12 17:24:51 +02003164 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 * @codec: the HDA codec
Takashi Iwaidcda5802012-10-12 17:24:51 +02003166 * @associated_nid: NID that new ctls associated with
3167 * @cvt_nid: converter NID
3168 * @type: HDA_PCM_TYPE_*
3169 * Creates controls related with the digital output.
3170 * Called from each patch supporting the digital out.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 *
3172 * Returns 0 if successful, or a negative error code.
3173 */
Takashi Iwaidcda5802012-10-12 17:24:51 +02003174int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
3175 hda_nid_t associated_nid,
3176 hda_nid_t cvt_nid,
3177 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178{
3179 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003180 struct snd_kcontrol *kctl;
3181 struct snd_kcontrol_new *dig_mix;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003182 int idx, dev = 0;
3183 const int spdif_pcm_dev = 1;
Stephen Warren7c9359762011-06-01 11:14:17 -06003184 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185
Takashi Iwaidcda5802012-10-12 17:24:51 +02003186 if (codec->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
3187 type == HDA_PCM_TYPE_SPDIF) {
3188 dev = spdif_pcm_dev;
3189 } else if (codec->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
3190 type == HDA_PCM_TYPE_HDMI) {
3191 for (idx = 0; idx < codec->spdif_out.used; idx++) {
3192 spdif = snd_array_elem(&codec->spdif_out, idx);
3193 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3194 kctl = find_mixer_ctl(codec, dig_mix->name, 0, idx);
3195 if (!kctl)
3196 break;
3197 kctl->id.device = spdif_pcm_dev;
3198 }
3199 }
3200 codec->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
3201 }
3202 if (!codec->primary_dig_out_type)
3203 codec->primary_dig_out_type = type;
3204
3205 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", dev);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003206 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003207 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
3208 return -EBUSY;
3209 }
Stephen Warren7c9359762011-06-01 11:14:17 -06003210 spdif = snd_array_new(&codec->spdif_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3212 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01003213 if (!kctl)
3214 return -ENOMEM;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003215 kctl->id.device = dev;
Takashi Iwai09f99702008-02-04 12:31:13 +01003216 kctl->id.index = idx;
Stephen Warren7c9359762011-06-01 11:14:17 -06003217 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06003218 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003219 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 return err;
3221 }
Stephen Warren74b654c2011-06-01 11:14:18 -06003222 spdif->nid = cvt_nid;
3223 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
Stephen Warren7c9359762011-06-01 11:14:17 -06003224 AC_VERB_GET_DIGI_CONVERT_1, 0);
3225 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 return 0;
3227}
Takashi Iwaidcda5802012-10-12 17:24:51 +02003228EXPORT_SYMBOL_HDA(snd_hda_create_dig_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003230/* get the hda_spdif_out entry from the given NID
3231 * call within spdif_mutex lock
3232 */
Stephen Warren7c9359762011-06-01 11:14:17 -06003233struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3234 hda_nid_t nid)
3235{
3236 int i;
3237 for (i = 0; i < codec->spdif_out.used; i++) {
3238 struct hda_spdif_out *spdif =
3239 snd_array_elem(&codec->spdif_out, i);
3240 if (spdif->nid == nid)
3241 return spdif;
3242 }
3243 return NULL;
3244}
3245EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid);
3246
Stephen Warren74b654c2011-06-01 11:14:18 -06003247void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3248{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003249 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003250
3251 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003252 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003253 spdif->nid = (u16)-1;
3254 mutex_unlock(&codec->spdif_mutex);
3255}
3256EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign);
3257
3258void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3259{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003260 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003261 unsigned short val;
3262
3263 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003264 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003265 if (spdif->nid != nid) {
3266 spdif->nid = nid;
3267 val = spdif->ctls;
3268 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3269 }
3270 mutex_unlock(&codec->spdif_mutex);
3271}
3272EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign);
3273
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274/*
Takashi Iwai9a081602008-02-12 18:37:26 +01003275 * SPDIF sharing with analog output
3276 */
3277static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3278 struct snd_ctl_elem_value *ucontrol)
3279{
3280 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3281 ucontrol->value.integer.value[0] = mout->share_spdif;
3282 return 0;
3283}
3284
3285static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3286 struct snd_ctl_elem_value *ucontrol)
3287{
3288 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3289 mout->share_spdif = !!ucontrol->value.integer.value[0];
3290 return 0;
3291}
3292
3293static struct snd_kcontrol_new spdif_share_sw = {
3294 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3295 .name = "IEC958 Default PCM Playback Switch",
3296 .info = snd_ctl_boolean_mono_info,
3297 .get = spdif_share_sw_get,
3298 .put = spdif_share_sw_put,
3299};
3300
Takashi Iwaid5191e52009-11-16 14:58:17 +01003301/**
3302 * snd_hda_create_spdif_share_sw - create Default PCM switch
3303 * @codec: the HDA codec
3304 * @mout: multi-out instance
3305 */
Takashi Iwai9a081602008-02-12 18:37:26 +01003306int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3307 struct hda_multi_out *mout)
3308{
3309 if (!mout->dig_out_nid)
3310 return 0;
3311 /* ATTENTION: here mout is passed as private_data, instead of codec */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003312 return snd_hda_ctl_add(codec, mout->dig_out_nid,
3313 snd_ctl_new1(&spdif_share_sw, mout));
Takashi Iwai9a081602008-02-12 18:37:26 +01003314}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003315EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01003316
3317/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 * SPDIF input
3319 */
3320
3321#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3322
Takashi Iwai0ba21762007-04-16 11:29:14 +02003323static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3324 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325{
3326 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3327
3328 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3329 return 0;
3330}
3331
Takashi Iwai0ba21762007-04-16 11:29:14 +02003332static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3333 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334{
3335 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3336 hda_nid_t nid = kcontrol->private_value;
3337 unsigned int val = !!ucontrol->value.integer.value[0];
3338 int change;
3339
Ingo Molnar62932df2006-01-16 16:34:20 +01003340 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003342 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003344 snd_hda_codec_write_cache(codec, nid, 0,
3345 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003347 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 return change;
3349}
3350
Takashi Iwai0ba21762007-04-16 11:29:14 +02003351static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3352 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353{
3354 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3355 hda_nid_t nid = kcontrol->private_value;
3356 unsigned short val;
3357 unsigned int sbits;
3358
Andrew Paprocki3982d172007-12-19 12:13:44 +01003359 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 sbits = convert_to_spdif_status(val);
3361 ucontrol->value.iec958.status[0] = sbits;
3362 ucontrol->value.iec958.status[1] = sbits >> 8;
3363 ucontrol->value.iec958.status[2] = sbits >> 16;
3364 ucontrol->value.iec958.status[3] = sbits >> 24;
3365 return 0;
3366}
3367
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003368static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 {
3370 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003371 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 .info = snd_hda_spdif_in_switch_info,
3373 .get = snd_hda_spdif_in_switch_get,
3374 .put = snd_hda_spdif_in_switch_put,
3375 },
3376 {
3377 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3378 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003379 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 .info = snd_hda_spdif_mask_info,
3381 .get = snd_hda_spdif_in_status_get,
3382 },
3383 { } /* end */
3384};
3385
3386/**
3387 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3388 * @codec: the HDA codec
3389 * @nid: audio in widget NID
3390 *
3391 * Creates controls related with the SPDIF input.
3392 * Called from each patch supporting the SPDIF in.
3393 *
3394 * Returns 0 if successful, or a negative error code.
3395 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003396int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397{
3398 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003399 struct snd_kcontrol *kctl;
3400 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003401 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402
Takashi Iwaidcda5802012-10-12 17:24:51 +02003403 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003404 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003405 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
3406 return -EBUSY;
3407 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3409 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01003410 if (!kctl)
3411 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003413 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003414 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 return err;
3416 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003417 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01003418 snd_hda_codec_read(codec, nid, 0,
3419 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02003420 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 return 0;
3422}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003423EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003425/*
3426 * command cache
3427 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428
Takashi Iwaic370dd62012-12-13 18:30:04 +01003429/* build a 31bit cache key with the widget id and the command parameter */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003430#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3431#define get_cmd_cache_nid(key) ((key) & 0xff)
3432#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3433
3434/**
3435 * snd_hda_codec_write_cache - send a single command with caching
3436 * @codec: the HDA codec
3437 * @nid: NID to send the command
3438 * @direct: direct flag
3439 * @verb: the verb to send
3440 * @parm: the parameter for the verb
3441 *
3442 * Send a single command without waiting for response.
3443 *
3444 * Returns 0 if successful, or a negative error code.
3445 */
3446int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
3447 int direct, unsigned int verb, unsigned int parm)
3448{
Takashi Iwaic370dd62012-12-13 18:30:04 +01003449 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003450 struct hda_cache_head *c;
3451 u32 key;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003452
Takashi Iwaic370dd62012-12-13 18:30:04 +01003453 if (!codec->cached_write) {
3454 err = snd_hda_codec_write(codec, nid, direct, verb, parm);
3455 if (err < 0)
3456 return err;
3457 }
3458
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003459 /* parm may contain the verb stuff for get/set amp */
3460 verb = verb | (parm >> 8);
3461 parm &= 0xff;
3462 key = build_cmd_cache_key(nid, verb);
3463 mutex_lock(&codec->bus->cmd_mutex);
3464 c = get_alloc_hash(&codec->cmd_cache, key);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003465 if (c) {
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003466 c->val = parm;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003467 if (codec->cached_write)
3468 c->dirty = 1;
3469 }
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003470 mutex_unlock(&codec->bus->cmd_mutex);
3471 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003472}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003473EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003474
Takashi Iwaid5191e52009-11-16 14:58:17 +01003475/**
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003476 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3477 * @codec: the HDA codec
3478 * @nid: NID to send the command
3479 * @direct: direct flag
3480 * @verb: the verb to send
3481 * @parm: the parameter for the verb
3482 *
3483 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3484 * command if the parameter is already identical with the cached value.
3485 * If not, it sends the command and refreshes the cache.
3486 *
3487 * Returns 0 if successful, or a negative error code.
3488 */
3489int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
3490 int direct, unsigned int verb, unsigned int parm)
3491{
3492 struct hda_cache_head *c;
3493 u32 key;
3494
3495 /* parm may contain the verb stuff for get/set amp */
3496 verb = verb | (parm >> 8);
3497 parm &= 0xff;
3498 key = build_cmd_cache_key(nid, verb);
3499 mutex_lock(&codec->bus->cmd_mutex);
3500 c = get_hash(&codec->cmd_cache, key);
3501 if (c && c->val == parm) {
3502 mutex_unlock(&codec->bus->cmd_mutex);
3503 return 0;
3504 }
3505 mutex_unlock(&codec->bus->cmd_mutex);
3506 return snd_hda_codec_write_cache(codec, nid, direct, verb, parm);
3507}
3508EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
3509
3510/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003511 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3512 * @codec: HD-audio codec
3513 *
3514 * Execute all verbs recorded in the command caches to resume.
3515 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003516void snd_hda_codec_resume_cache(struct hda_codec *codec)
3517{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003518 int i;
3519
Takashi Iwaic370dd62012-12-13 18:30:04 +01003520 mutex_lock(&codec->hash_mutex);
3521 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3522 struct hda_cache_head *buffer;
3523 u32 key;
3524
3525 buffer = snd_array_elem(&codec->cmd_cache.buf, i);
3526 key = buffer->key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003527 if (!key)
3528 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003529 if (!buffer->dirty)
3530 continue;
3531 buffer->dirty = 0;
3532 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003533 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3534 get_cmd_cache_cmd(key), buffer->val);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003535 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003536 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01003537 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003538}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003539EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003540
3541/**
3542 * snd_hda_sequence_write_cache - sequence writes with caching
3543 * @codec: the HDA codec
3544 * @seq: VERB array to send
3545 *
3546 * Send the commands sequentially from the given array.
3547 * Thte commands are recorded on cache for power-save and resume.
3548 * The array must be terminated with NID=0.
3549 */
3550void snd_hda_sequence_write_cache(struct hda_codec *codec,
3551 const struct hda_verb *seq)
3552{
3553 for (; seq->nid; seq++)
3554 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3555 seq->param);
3556}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003557EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003558
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003559void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
3560 unsigned int power_state,
3561 bool eapd_workaround)
Takashi Iwai54d17402005-11-21 16:33:22 +01003562{
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003563 hda_nid_t nid = codec->start_nid;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003564 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01003565
Takashi Iwaicb53c622007-08-10 17:21:45 +02003566 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003567 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003568 if (!(wcaps & AC_WCAP_POWER))
3569 continue;
3570 /* don't power down the widget if it controls eapd and
3571 * EAPD_BTLENABLE is set.
3572 */
3573 if (eapd_workaround && power_state == AC_PWRST_D3 &&
3574 get_wcaps_type(wcaps) == AC_WID_PIN &&
3575 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3576 int eapd = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003577 AC_VERB_GET_EAPD_BTLENABLE, 0);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003578 if (eapd & 0x02)
3579 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003580 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003581 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
3582 power_state);
Takashi Iwai54d17402005-11-21 16:33:22 +01003583 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003584}
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003585EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all);
3586
3587/*
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003588 * supported power states check
3589 */
3590static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg,
3591 unsigned int power_state)
3592{
3593 int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE);
3594
Mengdong Line037cb42012-08-10 14:11:58 +02003595 if (sup == -1)
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003596 return false;
3597 if (sup & power_state)
3598 return true;
3599 else
3600 return false;
3601}
3602
3603/*
Takashi Iwai432c6412012-08-28 09:59:20 -07003604 * wait until the state is reached, returns the current state
3605 */
3606static unsigned int hda_sync_power_state(struct hda_codec *codec,
3607 hda_nid_t fg,
3608 unsigned int power_state)
3609{
3610 unsigned long end_time = jiffies + msecs_to_jiffies(500);
3611 unsigned int state, actual_state;
3612
3613 for (;;) {
3614 state = snd_hda_codec_read(codec, fg, 0,
3615 AC_VERB_GET_POWER_STATE, 0);
3616 if (state & AC_PWRST_ERROR)
3617 break;
3618 actual_state = (state >> 4) & 0x0f;
3619 if (actual_state == power_state)
3620 break;
3621 if (time_after_eq(jiffies, end_time))
3622 break;
3623 /* wait until the codec reachs to the target state */
3624 msleep(1);
3625 }
3626 return state;
3627}
3628
3629/*
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003630 * set power state of the codec, and return the power state
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003631 */
Takashi Iwaid8193872012-08-31 07:54:38 -07003632static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003633 unsigned int power_state)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003634{
Takashi Iwaid8193872012-08-31 07:54:38 -07003635 hda_nid_t fg = codec->afg ? codec->afg : codec->mfg;
Wang Xingchao09617ce2012-06-08 10:26:08 +08003636 int count;
3637 unsigned int state;
3638
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003639 /* this delay seems necessary to avoid click noise at power-down */
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003640 if (power_state == AC_PWRST_D3) {
3641 /* transition time less than 10ms for power down */
Takashi Iwai983f6b92012-08-28 09:18:01 -07003642 msleep(codec->epss ? 10 : 100);
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003643 }
Wang Xingchao09617ce2012-06-08 10:26:08 +08003644
3645 /* repeat power states setting at most 10 times*/
3646 for (count = 0; count < 10; count++) {
Takashi Iwai432c6412012-08-28 09:59:20 -07003647 if (codec->patch_ops.set_power_state)
3648 codec->patch_ops.set_power_state(codec, fg,
3649 power_state);
3650 else {
3651 snd_hda_codec_read(codec, fg, 0,
3652 AC_VERB_SET_POWER_STATE,
3653 power_state);
3654 snd_hda_codec_set_power_to_all(codec, fg, power_state,
3655 true);
3656 }
3657 state = hda_sync_power_state(codec, fg, power_state);
Wang Xingchao09617ce2012-06-08 10:26:08 +08003658 if (!(state & AC_PWRST_ERROR))
3659 break;
3660 }
Mengdong Linb8dfc4622012-08-23 17:32:30 +08003661
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003662 return state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003663}
Takashi Iwai54d17402005-11-21 16:33:22 +01003664
Takashi Iwai11aeff02008-07-30 15:01:46 +02003665#ifdef CONFIG_SND_HDA_HWDEP
3666/* execute additional init verbs */
3667static void hda_exec_init_verbs(struct hda_codec *codec)
3668{
3669 if (codec->init_verbs.list)
3670 snd_hda_sequence_write(codec, codec->init_verbs.list);
3671}
3672#else
3673static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3674#endif
3675
Takashi Iwai2a439522011-07-26 09:52:50 +02003676#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003677/*
3678 * call suspend and power-down; used both from PM and power-save
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003679 * this function returns the power state in the end
Takashi Iwaicb53c622007-08-10 17:21:45 +02003680 */
Dylan Reidd17344b2012-09-28 15:57:01 -07003681static unsigned int hda_call_codec_suspend(struct hda_codec *codec, bool in_wq)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003682{
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003683 unsigned int state;
3684
Takashi Iwai989c3182012-11-19 14:14:58 +01003685 codec->in_pm = 1;
3686
Takashi Iwaicb53c622007-08-10 17:21:45 +02003687 if (codec->patch_ops.suspend)
Takashi Iwai68cb2b52012-07-02 15:20:37 +02003688 codec->patch_ops.suspend(codec);
Takashi Iwaieb541332010-08-06 13:48:11 +02003689 hda_cleanup_all_streams(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07003690 state = hda_set_power_state(codec, AC_PWRST_D3);
Dylan Reidd17344b2012-09-28 15:57:01 -07003691 /* Cancel delayed work if we aren't currently running from it. */
3692 if (!in_wq)
3693 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02003694 spin_lock(&codec->power_lock);
3695 snd_hda_update_power_acct(codec);
3696 trace_hda_power_down(codec);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02003697 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02003698 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003699 codec->power_jiffies = jiffies;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02003700 spin_unlock(&codec->power_lock);
Takashi Iwai989c3182012-11-19 14:14:58 +01003701 codec->in_pm = 0;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003702 return state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003703}
3704
Takashi Iwaic370dd62012-12-13 18:30:04 +01003705/* mark all entries of cmd and amp caches dirty */
3706static void hda_mark_cmd_cache_dirty(struct hda_codec *codec)
3707{
3708 int i;
3709 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3710 struct hda_cache_head *cmd;
3711 cmd = snd_array_elem(&codec->cmd_cache.buf, i);
3712 cmd->dirty = 1;
3713 }
3714 for (i = 0; i < codec->amp_cache.buf.used; i++) {
3715 struct hda_amp_info *amp;
3716 amp = snd_array_elem(&codec->cmd_cache.buf, i);
3717 amp->head.dirty = 1;
3718 }
3719}
3720
Takashi Iwaicb53c622007-08-10 17:21:45 +02003721/*
3722 * kick up codec; used both from PM and power-save
3723 */
3724static void hda_call_codec_resume(struct hda_codec *codec)
3725{
Takashi Iwai989c3182012-11-19 14:14:58 +01003726 codec->in_pm = 1;
3727
Takashi Iwaic370dd62012-12-13 18:30:04 +01003728 hda_mark_cmd_cache_dirty(codec);
3729
Takashi Iwai7f308302012-05-08 16:52:23 +02003730 /* set as if powered on for avoiding re-entering the resume
3731 * in the resume / power-save sequence
3732 */
3733 hda_keep_power_on(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07003734 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02003735 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02003736 hda_exec_init_verbs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003737 if (codec->patch_ops.resume)
3738 codec->patch_ops.resume(codec);
3739 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02003740 if (codec->patch_ops.init)
3741 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003742 snd_hda_codec_resume_amp(codec);
3743 snd_hda_codec_resume_cache(codec);
3744 }
David Henningsson26a6cb62012-10-09 15:04:21 +02003745
3746 if (codec->jackpoll_interval)
3747 hda_jackpoll_work(&codec->jackpoll_work.work);
3748 else {
3749 snd_hda_jack_set_dirty_all(codec);
3750 snd_hda_jack_report_sync(codec);
3751 }
Takashi Iwai989c3182012-11-19 14:14:58 +01003752
3753 codec->in_pm = 0;
Takashi Iwai7f308302012-05-08 16:52:23 +02003754 snd_hda_power_down(codec); /* flag down before returning */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003755}
Takashi Iwai2a439522011-07-26 09:52:50 +02003756#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003757
Takashi Iwai54d17402005-11-21 16:33:22 +01003758
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759/**
3760 * snd_hda_build_controls - build mixer controls
3761 * @bus: the BUS
3762 *
3763 * Creates mixer controls for each codec included in the bus.
3764 *
3765 * Returns 0 if successful, otherwise a negative error code.
3766 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01003767int snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003769 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770
Takashi Iwai0ba21762007-04-16 11:29:14 +02003771 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003772 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003773 if (err < 0) {
Takashi Iwai28d1a852010-03-15 09:05:46 +01003774 printk(KERN_ERR "hda_codec: cannot build controls "
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003775 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003776 err = snd_hda_codec_reset(codec);
3777 if (err < 0) {
3778 printk(KERN_ERR
3779 "hda_codec: cannot revert codec\n");
3780 return err;
3781 }
3782 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003784 return 0;
3785}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003786EXPORT_SYMBOL_HDA(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003788/*
3789 * add standard channel maps if not specified
3790 */
3791static int add_std_chmaps(struct hda_codec *codec)
3792{
3793 int i, str, err;
3794
3795 for (i = 0; i < codec->num_pcms; i++) {
3796 for (str = 0; str < 2; str++) {
3797 struct snd_pcm *pcm = codec->pcm_info[i].pcm;
3798 struct hda_pcm_stream *hinfo =
3799 &codec->pcm_info[i].stream[str];
3800 struct snd_pcm_chmap *chmap;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003801 const struct snd_pcm_chmap_elem *elem;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003802
3803 if (codec->pcm_info[i].own_chmap)
3804 continue;
3805 if (!pcm || !hinfo->substreams)
3806 continue;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003807 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
3808 err = snd_pcm_add_chmap_ctls(pcm, str, elem,
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003809 hinfo->channels_max,
3810 0, &chmap);
3811 if (err < 0)
3812 return err;
3813 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
3814 }
3815 }
3816 return 0;
3817}
3818
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003819/* default channel maps for 2.1 speakers;
3820 * since HD-audio supports only stereo, odd number channels are omitted
3821 */
3822const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
3823 { .channels = 2,
3824 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
3825 { .channels = 4,
3826 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
3827 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
3828 { }
3829};
3830EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
3831
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003832int snd_hda_codec_build_controls(struct hda_codec *codec)
3833{
3834 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02003835 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003836 /* continue to initialize... */
3837 if (codec->patch_ops.init)
3838 err = codec->patch_ops.init(codec);
3839 if (!err && codec->patch_ops.build_controls)
3840 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003841 if (err < 0)
3842 return err;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003843
3844 /* we create chmaps here instead of build_pcms */
3845 err = add_std_chmaps(codec);
3846 if (err < 0)
3847 return err;
3848
David Henningsson26a6cb62012-10-09 15:04:21 +02003849 if (codec->jackpoll_interval)
3850 hda_jackpoll_work(&codec->jackpoll_work.work);
3851 else
3852 snd_hda_jack_report_sync(codec); /* call at the last init point */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853 return 0;
3854}
3855
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856/*
3857 * stream formats
3858 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02003859struct hda_rate_tbl {
3860 unsigned int hz;
3861 unsigned int alsa_bits;
3862 unsigned int hda_fmt;
3863};
3864
Takashi Iwai92f10b32010-08-03 14:21:00 +02003865/* rate = base * mult / div */
3866#define HDA_RATE(base, mult, div) \
3867 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
3868 (((div) - 1) << AC_FMT_DIV_SHIFT))
3869
Takashi Iwaibefdf312005-08-22 13:57:55 +02003870static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003872
3873 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003874 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
3875 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
3876 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
3877 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
3878 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
3879 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
3880 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
3881 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
3882 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
3883 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
3884 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003885#define AC_PAR_PCM_RATE_BITS 11
3886 /* up to bits 10, 384kHZ isn't supported properly */
3887
3888 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003889 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003890
Takashi Iwaibefdf312005-08-22 13:57:55 +02003891 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892};
3893
3894/**
3895 * snd_hda_calc_stream_format - calculate format bitset
3896 * @rate: the sample rate
3897 * @channels: the number of channels
3898 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3899 * @maxbps: the max. bps
3900 *
3901 * Calculate the format bitset from the given rate, channels and th PCM format.
3902 *
3903 * Return zero if invalid.
3904 */
3905unsigned int snd_hda_calc_stream_format(unsigned int rate,
3906 unsigned int channels,
3907 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03003908 unsigned int maxbps,
3909 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910{
3911 int i;
3912 unsigned int val = 0;
3913
Takashi Iwaibefdf312005-08-22 13:57:55 +02003914 for (i = 0; rate_bits[i].hz; i++)
3915 if (rate_bits[i].hz == rate) {
3916 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917 break;
3918 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003919 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920 snd_printdd("invalid rate %d\n", rate);
3921 return 0;
3922 }
3923
3924 if (channels == 0 || channels > 8) {
3925 snd_printdd("invalid channels %d\n", channels);
3926 return 0;
3927 }
3928 val |= channels - 1;
3929
3930 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003931 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003932 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003933 break;
3934 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003935 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003936 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937 case 20:
3938 case 24:
3939 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003940 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003941 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003943 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02003945 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946 break;
3947 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003948 snd_printdd("invalid format width %d\n",
3949 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950 return 0;
3951 }
3952
Anssi Hannula32c168c2010-08-03 13:28:57 +03003953 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003954 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03003955
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956 return val;
3957}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003958EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003960static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid,
3961 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003962{
3963 unsigned int val = 0;
3964 if (nid != codec->afg &&
3965 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
3966 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
3967 if (!val || val == -1)
3968 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
3969 if (!val || val == -1)
3970 return 0;
3971 return val;
3972}
3973
3974static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3975{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003976 return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003977 get_pcm_param);
3978}
3979
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003980static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid,
3981 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003982{
3983 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
3984 if (!streams || streams == -1)
3985 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
3986 if (!streams || streams == -1)
3987 return 0;
3988 return streams;
3989}
3990
3991static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
3992{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003993 return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003994 get_stream_param);
3995}
3996
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997/**
3998 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
3999 * @codec: the HDA codec
4000 * @nid: NID to query
4001 * @ratesp: the pointer to store the detected rate bitflags
4002 * @formatsp: the pointer to store the detected formats
4003 * @bpsp: the pointer to store the detected format widths
4004 *
4005 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
4006 * or @bsps argument is ignored.
4007 *
4008 * Returns 0 if successful, otherwise a negative error code.
4009 */
Stephen Warren384a48d2011-06-01 11:14:21 -06004010int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
4012{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004013 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004015 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004016 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017
4018 if (ratesp) {
4019 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004020 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02004022 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004024 if (rates == 0) {
4025 snd_printk(KERN_ERR "hda_codec: rates == 0 "
4026 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
4027 nid, val,
4028 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
4029 return -EIO;
4030 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 *ratesp = rates;
4032 }
4033
4034 if (formatsp || bpsp) {
4035 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004036 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004038 streams = query_stream_param(codec, nid);
4039 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041
4042 bps = 0;
4043 if (streams & AC_SUPFMT_PCM) {
4044 if (val & AC_SUPPCM_BITS_8) {
4045 formats |= SNDRV_PCM_FMTBIT_U8;
4046 bps = 8;
4047 }
4048 if (val & AC_SUPPCM_BITS_16) {
4049 formats |= SNDRV_PCM_FMTBIT_S16_LE;
4050 bps = 16;
4051 }
4052 if (wcaps & AC_WCAP_DIGITAL) {
4053 if (val & AC_SUPPCM_BITS_32)
4054 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
4055 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
4056 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4057 if (val & AC_SUPPCM_BITS_24)
4058 bps = 24;
4059 else if (val & AC_SUPPCM_BITS_20)
4060 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004061 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
4062 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4064 if (val & AC_SUPPCM_BITS_32)
4065 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066 else if (val & AC_SUPPCM_BITS_24)
4067 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02004068 else if (val & AC_SUPPCM_BITS_20)
4069 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070 }
4071 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004072#if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
Takashi Iwaib5025c52009-07-01 18:05:27 +02004073 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004075 if (!bps)
4076 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02004077 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004078#endif
Takashi Iwaib5025c52009-07-01 18:05:27 +02004079 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02004080 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081 /* temporary hack: we have still no proper support
4082 * for the direct AC3 stream...
4083 */
4084 formats |= SNDRV_PCM_FMTBIT_U8;
4085 bps = 8;
4086 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004087 if (formats == 0) {
4088 snd_printk(KERN_ERR "hda_codec: formats == 0 "
4089 "(nid=0x%x, val=0x%x, ovrd=%i, "
4090 "streams=0x%x)\n",
4091 nid, val,
4092 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
4093 streams);
4094 return -EIO;
4095 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096 if (formatsp)
4097 *formatsp = formats;
4098 if (bpsp)
4099 *bpsp = bps;
4100 }
4101
4102 return 0;
4103}
Stephen Warren384a48d2011-06-01 11:14:21 -06004104EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105
4106/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01004107 * snd_hda_is_supported_format - Check the validity of the format
4108 * @codec: HD-audio codec
4109 * @nid: NID to check
4110 * @format: the HD-audio format value to check
4111 *
4112 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113 *
4114 * Returns 1 if supported, 0 if not.
4115 */
4116int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
4117 unsigned int format)
4118{
4119 int i;
4120 unsigned int val = 0, rate, stream;
4121
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004122 val = query_pcm_param(codec, nid);
4123 if (!val)
4124 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125
4126 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004127 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02004128 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129 if (val & (1 << i))
4130 break;
4131 return 0;
4132 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004133 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134 return 0;
4135
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004136 stream = query_stream_param(codec, nid);
4137 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138 return 0;
4139
4140 if (stream & AC_SUPFMT_PCM) {
4141 switch (format & 0xf0) {
4142 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004143 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144 return 0;
4145 break;
4146 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004147 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148 return 0;
4149 break;
4150 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004151 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152 return 0;
4153 break;
4154 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004155 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156 return 0;
4157 break;
4158 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004159 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 return 0;
4161 break;
4162 default:
4163 return 0;
4164 }
4165 } else {
4166 /* FIXME: check for float32 and AC3? */
4167 }
4168
4169 return 1;
4170}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004171EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172
4173/*
4174 * PCM stuff
4175 */
4176static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
4177 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004178 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179{
4180 return 0;
4181}
4182
4183static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
4184 struct hda_codec *codec,
4185 unsigned int stream_tag,
4186 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004187 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188{
4189 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4190 return 0;
4191}
4192
4193static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
4194 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004195 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196{
Takashi Iwai888afa12008-03-18 09:57:50 +01004197 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198 return 0;
4199}
4200
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004201static int set_pcm_default_values(struct hda_codec *codec,
4202 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004204 int err;
4205
Takashi Iwai0ba21762007-04-16 11:29:14 +02004206 /* query support PCM information from the given NID */
4207 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004208 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02004209 info->rates ? NULL : &info->rates,
4210 info->formats ? NULL : &info->formats,
4211 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004212 if (err < 0)
4213 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214 }
4215 if (info->ops.open == NULL)
4216 info->ops.open = hda_pcm_default_open_close;
4217 if (info->ops.close == NULL)
4218 info->ops.close = hda_pcm_default_open_close;
4219 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004220 if (snd_BUG_ON(!info->nid))
4221 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222 info->ops.prepare = hda_pcm_default_prepare;
4223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004225 if (snd_BUG_ON(!info->nid))
4226 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 info->ops.cleanup = hda_pcm_default_cleanup;
4228 }
4229 return 0;
4230}
4231
Takashi Iwaieb541332010-08-06 13:48:11 +02004232/*
4233 * codec prepare/cleanup entries
4234 */
4235int snd_hda_codec_prepare(struct hda_codec *codec,
4236 struct hda_pcm_stream *hinfo,
4237 unsigned int stream,
4238 unsigned int format,
4239 struct snd_pcm_substream *substream)
4240{
4241 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004242 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004243 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
4244 if (ret >= 0)
4245 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004246 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004247 return ret;
4248}
4249EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
4250
4251void snd_hda_codec_cleanup(struct hda_codec *codec,
4252 struct hda_pcm_stream *hinfo,
4253 struct snd_pcm_substream *substream)
4254{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004255 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004256 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004257 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004258}
4259EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
4260
Takashi Iwaid5191e52009-11-16 14:58:17 +01004261/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01004262const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
4263 "Audio", "SPDIF", "HDMI", "Modem"
4264};
4265
Takashi Iwai176d5332008-07-30 15:01:44 +02004266/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004267 * get the empty PCM device number to assign
Takashi Iwaic8936222010-01-28 17:08:53 +01004268 *
4269 * note the max device number is limited by HDA_MAX_PCMS, currently 10
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004270 */
4271static int get_empty_pcm_device(struct hda_bus *bus, int type)
4272{
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004273 /* audio device indices; not linear to keep compatibility */
4274 static int audio_idx[HDA_PCM_NTYPES][5] = {
4275 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
4276 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01004277 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004278 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004279 };
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004280 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004281
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004282 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004283 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
4284 return -EINVAL;
4285 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004286
4287 for (i = 0; audio_idx[type][i] >= 0 ; i++)
4288 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
4289 return audio_idx[type][i];
4290
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004291 /* non-fixed slots starting from 10 */
4292 for (i = 10; i < 32; i++) {
4293 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4294 return i;
4295 }
4296
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004297 snd_printk(KERN_WARNING "Too many %s devices\n",
4298 snd_hda_pcm_type_name[type]);
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004299 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004300}
4301
4302/*
Takashi Iwai176d5332008-07-30 15:01:44 +02004303 * attach a new PCM stream
4304 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004305static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02004306{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004307 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02004308 struct hda_pcm_stream *info;
4309 int stream, err;
4310
Takashi Iwaib91f0802008-11-04 08:43:08 +01004311 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02004312 return -EINVAL;
4313 for (stream = 0; stream < 2; stream++) {
4314 info = &pcm->stream[stream];
4315 if (info->substreams) {
4316 err = set_pcm_default_values(codec, info);
4317 if (err < 0)
4318 return err;
4319 }
4320 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004321 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02004322}
4323
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004324/* assign all PCMs of the given codec */
4325int snd_hda_codec_build_pcms(struct hda_codec *codec)
4326{
4327 unsigned int pcm;
4328 int err;
4329
4330 if (!codec->num_pcms) {
4331 if (!codec->patch_ops.build_pcms)
4332 return 0;
4333 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004334 if (err < 0) {
4335 printk(KERN_ERR "hda_codec: cannot build PCMs"
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004336 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004337 err = snd_hda_codec_reset(codec);
4338 if (err < 0) {
4339 printk(KERN_ERR
4340 "hda_codec: cannot revert codec\n");
4341 return err;
4342 }
4343 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004344 }
4345 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
4346 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
4347 int dev;
4348
4349 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01004350 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004351
4352 if (!cpcm->pcm) {
4353 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
4354 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004355 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004356 cpcm->device = dev;
4357 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004358 if (err < 0) {
4359 printk(KERN_ERR "hda_codec: cannot attach "
4360 "PCM stream %d for codec #%d\n",
4361 dev, codec->addr);
4362 continue; /* no fatal error */
4363 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004364 }
4365 }
4366 return 0;
4367}
4368
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369/**
4370 * snd_hda_build_pcms - build PCM information
4371 * @bus: the BUS
4372 *
4373 * Create PCM information for each codec included in the bus.
4374 *
4375 * The build_pcms codec patch is requested to set up codec->num_pcms and
4376 * codec->pcm_info properly. The array is referred by the top-level driver
4377 * to create its PCM instances.
4378 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4379 * callback.
4380 *
4381 * At least, substreams, channels_min and channels_max must be filled for
4382 * each stream. substreams = 0 indicates that the stream doesn't exist.
4383 * When rates and/or formats are zero, the supported values are queried
4384 * from the given nid. The nid is used also by the default ops.prepare
4385 * and ops.cleanup callbacks.
4386 *
4387 * The driver needs to call ops.open in its open callback. Similarly,
4388 * ops.close is supposed to be called in the close callback.
4389 * ops.prepare should be called in the prepare or hw_params callback
4390 * with the proper parameters for set up.
4391 * ops.cleanup should be called in hw_free for clean up of streams.
4392 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004393 * This function returns 0 if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394 */
Takashi Iwai5cb543d2012-08-09 13:49:23 +02004395int snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004397 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398
Takashi Iwai0ba21762007-04-16 11:29:14 +02004399 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004400 int err = snd_hda_codec_build_pcms(codec);
4401 if (err < 0)
4402 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403 }
4404 return 0;
4405}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004406EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408/**
4409 * snd_hda_check_board_config - compare the current codec with the config table
4410 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004411 * @num_configs: number of config enums
4412 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413 * @tbl: configuration table, terminated by null entries
4414 *
4415 * Compares the modelname or PCI subsystem id of the current codec with the
4416 * given configuration table. If a matching entry is found, returns its
4417 * config value (supposed to be 0 or positive).
4418 *
4419 * If no entries are matching, the function returns a negative value.
4420 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02004421int snd_hda_check_board_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004422 int num_configs, const char * const *models,
Takashi Iwai12f288b2007-08-02 15:51:59 +02004423 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424{
Takashi Iwaif44ac832008-07-30 15:01:45 +02004425 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004426 int i;
4427 for (i = 0; i < num_configs; i++) {
4428 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02004429 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004430 snd_printd(KERN_INFO "hda_codec: model '%s' is "
4431 "selected\n", models[i]);
4432 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433 }
4434 }
4435 }
4436
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004437 if (!codec->bus->pci || !tbl)
4438 return -1;
4439
4440 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
4441 if (!tbl)
4442 return -1;
4443 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02004444#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004445 char tmp[10];
4446 const char *model = NULL;
4447 if (models)
4448 model = models[tbl->value];
4449 if (!model) {
4450 sprintf(tmp, "#%d", tbl->value);
4451 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004453 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4454 "for config %x:%x (%s)\n",
4455 model, tbl->subvendor, tbl->subdevice,
4456 (tbl->name ? tbl->name : "Unknown device"));
4457#endif
4458 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459 }
4460 return -1;
4461}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004462EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463
4464/**
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004465 * snd_hda_check_board_codec_sid_config - compare the current codec
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004466 subsystem ID with the
4467 config table
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004468
4469 This is important for Gateway notebooks with SB450 HDA Audio
4470 where the vendor ID of the PCI device is:
4471 ATI Technologies Inc SB450 HDA Audio [1002:437b]
4472 and the vendor/subvendor are found only at the codec.
4473
4474 * @codec: the HDA codec
4475 * @num_configs: number of config enums
4476 * @models: array of model name strings
4477 * @tbl: configuration table, terminated by null entries
4478 *
4479 * Compares the modelname or PCI subsystem id of the current codec with the
4480 * given configuration table. If a matching entry is found, returns its
4481 * config value (supposed to be 0 or positive).
4482 *
4483 * If no entries are matching, the function returns a negative value.
4484 */
4485int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004486 int num_configs, const char * const *models,
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004487 const struct snd_pci_quirk *tbl)
4488{
4489 const struct snd_pci_quirk *q;
4490
4491 /* Search for codec ID */
4492 for (q = tbl; q->subvendor; q++) {
Takashi Iwaie2301a42011-11-22 19:58:56 +01004493 unsigned int mask = 0xffff0000 | q->subdevice_mask;
4494 unsigned int id = (q->subdevice | (q->subvendor << 16)) & mask;
4495 if ((codec->subsystem_id & mask) == id)
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004496 break;
4497 }
4498
4499 if (!q->subvendor)
4500 return -1;
4501
4502 tbl = q;
4503
4504 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwaid94ff6b2009-09-02 00:20:21 +02004505#ifdef CONFIG_SND_DEBUG_VERBOSE
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004506 char tmp[10];
4507 const char *model = NULL;
4508 if (models)
4509 model = models[tbl->value];
4510 if (!model) {
4511 sprintf(tmp, "#%d", tbl->value);
4512 model = tmp;
4513 }
4514 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4515 "for config %x:%x (%s)\n",
4516 model, tbl->subvendor, tbl->subdevice,
4517 (tbl->name ? tbl->name : "Unknown device"));
4518#endif
4519 return tbl->value;
4520 }
4521 return -1;
4522}
4523EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
4524
4525/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526 * snd_hda_add_new_ctls - create controls from the array
4527 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004528 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529 *
4530 * This helper function creates and add new controls in the given array.
4531 * The array must be terminated with an empty entry as terminator.
4532 *
4533 * Returns 0 if successful, or a negative error code.
4534 */
Takashi Iwai031024e2011-05-02 11:29:30 +02004535int snd_hda_add_new_ctls(struct hda_codec *codec,
4536 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01004538 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539
4540 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004541 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004542 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01004543 if (knew->iface == -1) /* skip this codec private value */
4544 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004545 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004546 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02004547 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01004548 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004549 if (addr > 0)
4550 kctl->id.device = addr;
4551 if (idx > 0)
4552 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01004553 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004554 if (!err)
4555 break;
4556 /* try first with another device index corresponding to
4557 * the codec addr; if it still fails (or it's the
4558 * primary codec), then try another control index
4559 */
4560 if (!addr && codec->addr)
4561 addr = codec->addr;
4562 else if (!idx && !knew->index) {
4563 idx = find_empty_mixer_ctl_idx(codec,
Takashi Iwaidcda5802012-10-12 17:24:51 +02004564 knew->name, 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004565 if (idx <= 0)
4566 return err;
4567 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01004568 return err;
4569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570 }
4571 return 0;
4572}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004573EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574
Takashi Iwai83012a72012-08-24 18:38:08 +02004575#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02004576static void hda_power_work(struct work_struct *work)
4577{
4578 struct hda_codec *codec =
4579 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004580 struct hda_bus *bus = codec->bus;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004581 unsigned int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004582
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004583 spin_lock(&codec->power_lock);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004584 if (codec->power_transition > 0) { /* during power-up sequence? */
4585 spin_unlock(&codec->power_lock);
4586 return;
4587 }
Maxim Levitsky2e492462007-09-03 15:26:57 +02004588 if (!codec->power_on || codec->power_count) {
4589 codec->power_transition = 0;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004590 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004591 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02004592 }
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004593 spin_unlock(&codec->power_lock);
4594
Dylan Reidd17344b2012-09-28 15:57:01 -07004595 state = hda_call_codec_suspend(codec, true);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004596 codec->pm_down_notified = 0;
4597 if (!bus->power_keep_link_on && (state & AC_PWRST_CLK_STOP_OK)) {
4598 codec->pm_down_notified = 1;
Takashi Iwai68467f52012-08-28 09:14:29 -07004599 hda_call_pm_notify(bus, false);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004600 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02004601}
4602
4603static void hda_keep_power_on(struct hda_codec *codec)
4604{
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004605 spin_lock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004606 codec->power_count++;
4607 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004608 codec->power_jiffies = jiffies;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004609 spin_unlock(&codec->power_lock);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004610}
4611
Takashi Iwaid5191e52009-11-16 14:58:17 +01004612/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01004613void snd_hda_update_power_acct(struct hda_codec *codec)
4614{
4615 unsigned long delta = jiffies - codec->power_jiffies;
4616 if (codec->power_on)
4617 codec->power_on_acct += delta;
4618 else
4619 codec->power_off_acct += delta;
4620 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004621}
4622
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004623/* Transition to powered up, if wait_power_down then wait for a pending
4624 * transition to D3 to complete. A pending D3 transition is indicated
4625 * with power_transition == -1. */
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004626/* call this with codec->power_lock held! */
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004627static void __snd_hda_power_up(struct hda_codec *codec, bool wait_power_down)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004628{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004629 struct hda_bus *bus = codec->bus;
4630
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004631 /* Return if power_on or transitioning to power_on, unless currently
4632 * powering down. */
4633 if ((codec->power_on || codec->power_transition > 0) &&
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004634 !(wait_power_down && codec->power_transition < 0))
Takashi Iwaicb53c622007-08-10 17:21:45 +02004635 return;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004636 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004637
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004638 cancel_delayed_work_sync(&codec->power_work);
4639
4640 spin_lock(&codec->power_lock);
Dylan Reidb43d2242012-06-21 21:51:22 -07004641 /* If the power down delayed work was cancelled above before starting,
4642 * then there is no need to go through power up here.
4643 */
4644 if (codec->power_on) {
Takashi Iwai535b6c52012-08-20 21:25:22 +02004645 if (codec->power_transition < 0)
4646 codec->power_transition = 0;
Dylan Reidb43d2242012-06-21 21:51:22 -07004647 return;
4648 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004649
Takashi Iwaid66fee52011-08-02 15:39:31 +02004650 trace_hda_power_up(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004651 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004652 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004653 codec->power_jiffies = jiffies;
Takashi Iwai7f308302012-05-08 16:52:23 +02004654 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004655 spin_unlock(&codec->power_lock);
4656
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004657 if (codec->pm_down_notified) {
4658 codec->pm_down_notified = 0;
Takashi Iwai68467f52012-08-28 09:14:29 -07004659 hda_call_pm_notify(bus, true);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004660 }
4661
Takashi Iwaicb53c622007-08-10 17:21:45 +02004662 hda_call_codec_resume(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004663
4664 spin_lock(&codec->power_lock);
Takashi Iwaia221e282007-08-16 16:35:33 +02004665 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004666}
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004667
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004668#define power_save(codec) \
4669 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004670
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004671/* Transition to powered down */
4672static void __snd_hda_power_down(struct hda_codec *codec)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004673{
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004674 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004675 return;
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004676
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004677 if (power_save(codec)) {
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004678 codec->power_transition = -1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01004679 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004680 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02004681 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004682}
4683
4684/**
4685 * snd_hda_power_save - Power-up/down/sync the codec
4686 * @codec: HD-audio codec
4687 * @delta: the counter delta to change
4688 *
4689 * Change the power-up counter via @delta, and power up or down the hardware
4690 * appropriately. For the power-down, queue to the delayed action.
4691 * Passing zero to @delta means to synchronize the power state.
4692 */
4693void snd_hda_power_save(struct hda_codec *codec, int delta, bool d3wait)
4694{
4695 spin_lock(&codec->power_lock);
4696 codec->power_count += delta;
4697 trace_hda_power_count(codec);
4698 if (delta > 0)
4699 __snd_hda_power_up(codec, d3wait);
4700 else
4701 __snd_hda_power_down(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004702 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004703}
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004704EXPORT_SYMBOL_HDA(snd_hda_power_save);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004705
Takashi Iwaid5191e52009-11-16 14:58:17 +01004706/**
4707 * snd_hda_check_amp_list_power - Check the amp list and update the power
4708 * @codec: HD-audio codec
4709 * @check: the object containing an AMP list and the status
4710 * @nid: NID to check / update
4711 *
4712 * Check whether the given NID is in the amp list. If it's in the list,
4713 * check the current AMP status, and update the the power-status according
4714 * to the mute status.
4715 *
4716 * This function is supposed to be set or called from the check_power_status
4717 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004718 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004719int snd_hda_check_amp_list_power(struct hda_codec *codec,
4720 struct hda_loopback_check *check,
4721 hda_nid_t nid)
4722{
Takashi Iwai031024e2011-05-02 11:29:30 +02004723 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004724 int ch, v;
4725
4726 if (!check->amplist)
4727 return 0;
4728 for (p = check->amplist; p->nid; p++) {
4729 if (p->nid == nid)
4730 break;
4731 }
4732 if (!p->nid)
4733 return 0; /* nothing changed */
4734
4735 for (p = check->amplist; p->nid; p++) {
4736 for (ch = 0; ch < 2; ch++) {
4737 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4738 p->idx);
4739 if (!(v & HDA_AMP_MUTE) && v > 0) {
4740 if (!check->power_on) {
4741 check->power_on = 1;
4742 snd_hda_power_up(codec);
4743 }
4744 return 1;
4745 }
4746 }
4747 }
4748 if (check->power_on) {
4749 check->power_on = 0;
4750 snd_hda_power_down(codec);
4751 }
4752 return 0;
4753}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004754EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004755#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004757/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004758 * Channel mode helper
4759 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004760
4761/**
4762 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
4763 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004764int snd_hda_ch_mode_info(struct hda_codec *codec,
4765 struct snd_ctl_elem_info *uinfo,
4766 const struct hda_channel_mode *chmode,
4767 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004768{
4769 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4770 uinfo->count = 1;
4771 uinfo->value.enumerated.items = num_chmodes;
4772 if (uinfo->value.enumerated.item >= num_chmodes)
4773 uinfo->value.enumerated.item = num_chmodes - 1;
4774 sprintf(uinfo->value.enumerated.name, "%dch",
4775 chmode[uinfo->value.enumerated.item].channels);
4776 return 0;
4777}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004778EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004779
Takashi Iwaid5191e52009-11-16 14:58:17 +01004780/**
4781 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
4782 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004783int snd_hda_ch_mode_get(struct hda_codec *codec,
4784 struct snd_ctl_elem_value *ucontrol,
4785 const struct hda_channel_mode *chmode,
4786 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004787 int max_channels)
4788{
4789 int i;
4790
4791 for (i = 0; i < num_chmodes; i++) {
4792 if (max_channels == chmode[i].channels) {
4793 ucontrol->value.enumerated.item[0] = i;
4794 break;
4795 }
4796 }
4797 return 0;
4798}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004799EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004800
Takashi Iwaid5191e52009-11-16 14:58:17 +01004801/**
4802 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
4803 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004804int snd_hda_ch_mode_put(struct hda_codec *codec,
4805 struct snd_ctl_elem_value *ucontrol,
4806 const struct hda_channel_mode *chmode,
4807 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004808 int *max_channelsp)
4809{
4810 unsigned int mode;
4811
4812 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01004813 if (mode >= num_chmodes)
4814 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004815 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004816 return 0;
4817 /* change the current channel setting */
4818 *max_channelsp = chmode[mode].channels;
4819 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004820 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004821 return 1;
4822}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004823EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004824
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825/*
4826 * input MUX helper
4827 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004828
4829/**
4830 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
4831 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004832int snd_hda_input_mux_info(const struct hda_input_mux *imux,
4833 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834{
4835 unsigned int index;
4836
4837 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4838 uinfo->count = 1;
4839 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004840 if (!imux->num_items)
4841 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842 index = uinfo->value.enumerated.item;
4843 if (index >= imux->num_items)
4844 index = imux->num_items - 1;
4845 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
4846 return 0;
4847}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004848EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849
Takashi Iwaid5191e52009-11-16 14:58:17 +01004850/**
4851 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
4852 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004853int snd_hda_input_mux_put(struct hda_codec *codec,
4854 const struct hda_input_mux *imux,
4855 struct snd_ctl_elem_value *ucontrol,
4856 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857 unsigned int *cur_val)
4858{
4859 unsigned int idx;
4860
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004861 if (!imux->num_items)
4862 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863 idx = ucontrol->value.enumerated.item[0];
4864 if (idx >= imux->num_items)
4865 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004866 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004868 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
4869 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870 *cur_val = idx;
4871 return 1;
4872}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004873EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874
4875
4876/*
Takashi Iwaidda415d2012-11-30 18:34:38 +01004877 * process kcontrol info callback of a simple string enum array
4878 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
4879 */
4880int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
4881 struct snd_ctl_elem_info *uinfo,
4882 int num_items, const char * const *texts)
4883{
4884 static const char * const texts_default[] = {
4885 "Disabled", "Enabled"
4886 };
4887
4888 if (!texts || !num_items) {
4889 num_items = 2;
4890 texts = texts_default;
4891 }
4892
4893 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4894 uinfo->count = 1;
4895 uinfo->value.enumerated.items = num_items;
4896 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
4897 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
4898 strcpy(uinfo->value.enumerated.name,
4899 texts[uinfo->value.enumerated.item]);
4900 return 0;
4901}
4902EXPORT_SYMBOL_HDA(snd_hda_enum_helper_info);
4903
4904/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905 * Multi-channel / digital-out PCM helper functions
4906 */
4907
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004908/* setup SPDIF output stream */
4909static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
4910 unsigned int stream_tag, unsigned int format)
4911{
Laurence Darby3bef1c32012-11-03 17:00:06 +00004912 struct hda_spdif_out *spdif;
4913 unsigned int curr_fmt;
4914 bool reset;
Stephen Warren7c9359762011-06-01 11:14:17 -06004915
Laurence Darby3bef1c32012-11-03 17:00:06 +00004916 spdif = snd_hda_spdif_out_of_nid(codec, nid);
4917 curr_fmt = snd_hda_codec_read(codec, nid, 0,
4918 AC_VERB_GET_STREAM_FORMAT, 0);
4919 reset = codec->spdif_status_reset &&
4920 (spdif->ctls & AC_DIG1_ENABLE) &&
4921 curr_fmt != format;
4922
4923 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
4924 updated */
4925 if (reset)
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004926 set_dig_out_convert(codec, nid,
Stephen Warren7c9359762011-06-01 11:14:17 -06004927 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02004928 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004929 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02004930 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004931 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004932 for (d = codec->slave_dig_outs; *d; d++)
4933 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
4934 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004935 }
Takashi Iwai2f728532008-09-25 16:32:41 +02004936 /* turn on again (if needed) */
Laurence Darby3bef1c32012-11-03 17:00:06 +00004937 if (reset)
Takashi Iwai2f728532008-09-25 16:32:41 +02004938 set_dig_out_convert(codec, nid,
Stephen Warren7c9359762011-06-01 11:14:17 -06004939 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02004940}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004941
Takashi Iwai2f728532008-09-25 16:32:41 +02004942static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
4943{
4944 snd_hda_codec_cleanup_stream(codec, nid);
4945 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004946 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004947 for (d = codec->slave_dig_outs; *d; d++)
4948 snd_hda_codec_cleanup_stream(codec, *d);
4949 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004950}
4951
Takashi Iwaid5191e52009-11-16 14:58:17 +01004952/**
4953 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
4954 * @bus: HD-audio bus
4955 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004956void snd_hda_bus_reboot_notify(struct hda_bus *bus)
4957{
4958 struct hda_codec *codec;
4959
4960 if (!bus)
4961 return;
4962 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02004963 if (hda_codec_is_power_on(codec) &&
4964 codec->patch_ops.reboot_notify)
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004965 codec->patch_ops.reboot_notify(codec);
4966 }
4967}
Takashi Iwai8f217a22009-11-10 18:26:12 +01004968EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004969
Takashi Iwaid5191e52009-11-16 14:58:17 +01004970/**
4971 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004973int snd_hda_multi_out_dig_open(struct hda_codec *codec,
4974 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975{
Ingo Molnar62932df2006-01-16 16:34:20 +01004976 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02004977 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
4978 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02004979 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01004981 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982 return 0;
4983}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004984EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985
Takashi Iwaid5191e52009-11-16 14:58:17 +01004986/**
4987 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
4988 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004989int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
4990 struct hda_multi_out *mout,
4991 unsigned int stream_tag,
4992 unsigned int format,
4993 struct snd_pcm_substream *substream)
4994{
4995 mutex_lock(&codec->spdif_mutex);
4996 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
4997 mutex_unlock(&codec->spdif_mutex);
4998 return 0;
4999}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005000EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005001
Takashi Iwaid5191e52009-11-16 14:58:17 +01005002/**
5003 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
5004 */
Takashi Iwai9411e212009-02-13 11:32:28 +01005005int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
5006 struct hda_multi_out *mout)
5007{
5008 mutex_lock(&codec->spdif_mutex);
5009 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5010 mutex_unlock(&codec->spdif_mutex);
5011 return 0;
5012}
5013EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
5014
Takashi Iwaid5191e52009-11-16 14:58:17 +01005015/**
5016 * snd_hda_multi_out_dig_close - release the digital out stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005018int snd_hda_multi_out_dig_close(struct hda_codec *codec,
5019 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005020{
Ingo Molnar62932df2006-01-16 16:34:20 +01005021 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01005023 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005024 return 0;
5025}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005026EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027
Takashi Iwaid5191e52009-11-16 14:58:17 +01005028/**
5029 * snd_hda_multi_out_analog_open - open analog outputs
5030 *
5031 * Open analog outputs and set up the hw-constraints.
5032 * If the digital outputs can be opened as slave, open the digital
5033 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005035int snd_hda_multi_out_analog_open(struct hda_codec *codec,
5036 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01005037 struct snd_pcm_substream *substream,
5038 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005039{
Takashi Iwai9a081602008-02-12 18:37:26 +01005040 struct snd_pcm_runtime *runtime = substream->runtime;
5041 runtime->hw.channels_max = mout->max_channels;
5042 if (mout->dig_out_nid) {
5043 if (!mout->analog_rates) {
5044 mout->analog_rates = hinfo->rates;
5045 mout->analog_formats = hinfo->formats;
5046 mout->analog_maxbps = hinfo->maxbps;
5047 } else {
5048 runtime->hw.rates = mout->analog_rates;
5049 runtime->hw.formats = mout->analog_formats;
5050 hinfo->maxbps = mout->analog_maxbps;
5051 }
5052 if (!mout->spdif_rates) {
5053 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
5054 &mout->spdif_rates,
5055 &mout->spdif_formats,
5056 &mout->spdif_maxbps);
5057 }
5058 mutex_lock(&codec->spdif_mutex);
5059 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02005060 if ((runtime->hw.rates & mout->spdif_rates) &&
5061 (runtime->hw.formats & mout->spdif_formats)) {
5062 runtime->hw.rates &= mout->spdif_rates;
5063 runtime->hw.formats &= mout->spdif_formats;
5064 if (mout->spdif_maxbps < hinfo->maxbps)
5065 hinfo->maxbps = mout->spdif_maxbps;
5066 } else {
5067 mout->share_spdif = 0;
5068 /* FIXME: need notify? */
5069 }
Takashi Iwai9a081602008-02-12 18:37:26 +01005070 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02005071 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01005072 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073 return snd_pcm_hw_constraint_step(substream->runtime, 0,
5074 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
5075}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005076EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077
Takashi Iwaid5191e52009-11-16 14:58:17 +01005078/**
5079 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
5080 *
5081 * Set up the i/o for analog out.
5082 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005084int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
5085 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086 unsigned int stream_tag,
5087 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01005088 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089{
Takashi Iwaidda14412011-05-02 11:29:30 +02005090 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005091 int chs = substream->runtime->channels;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005092 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093 int i;
5094
Ingo Molnar62932df2006-01-16 16:34:20 +01005095 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005096 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Takashi Iwai9a081602008-02-12 18:37:26 +01005097 if (mout->dig_out_nid && mout->share_spdif &&
5098 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005099 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02005100 snd_hda_is_supported_format(codec, mout->dig_out_nid,
5101 format) &&
Stephen Warren7c9359762011-06-01 11:14:17 -06005102 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005104 setup_dig_out_stream(codec, mout->dig_out_nid,
5105 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106 } else {
5107 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02005108 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109 }
5110 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005111 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112
5113 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005114 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
5115 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005116 if (!mout->no_share_stream &&
5117 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005119 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
5120 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005121 /* extra outputs copied from front */
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005122 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5123 if (!mout->no_share_stream && mout->hp_out_nid[i])
5124 snd_hda_codec_setup_stream(codec,
5125 mout->hp_out_nid[i],
5126 stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005127 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02005128 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01005129 snd_hda_codec_setup_stream(codec,
5130 mout->extra_out_nid[i],
5131 stream_tag, 0, format);
5132
Linus Torvalds1da177e2005-04-16 15:20:36 -07005133 /* surrounds */
5134 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02005135 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005136 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5137 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005138 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005139 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5140 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141 }
5142 return 0;
5143}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005144EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005145
Takashi Iwaid5191e52009-11-16 14:58:17 +01005146/**
5147 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Linus Torvalds1da177e2005-04-16 15:20:36 -07005148 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005149int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
5150 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005151{
Takashi Iwaidda14412011-05-02 11:29:30 +02005152 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153 int i;
5154
5155 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01005156 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005157 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01005158 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005159 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5160 if (mout->hp_out_nid[i])
5161 snd_hda_codec_cleanup_stream(codec,
5162 mout->hp_out_nid[i]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005163 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
5164 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01005165 snd_hda_codec_cleanup_stream(codec,
5166 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01005167 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005168 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02005169 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170 mout->dig_out_used = 0;
5171 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005172 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005173 return 0;
5174}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005175EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176
Takashi Iwai47408602012-04-20 13:06:53 +02005177/**
5178 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
5179 *
5180 * Guess the suitable VREF pin bits to be set as the pin-control value.
5181 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
5182 */
5183unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
5184{
5185 unsigned int pincap;
5186 unsigned int oldval;
5187 oldval = snd_hda_codec_read(codec, pin, 0,
5188 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5189 pincap = snd_hda_query_pin_caps(codec, pin);
5190 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5191 /* Exception: if the default pin setup is vref50, we give it priority */
5192 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
5193 return AC_PINCTL_VREF_80;
5194 else if (pincap & AC_PINCAP_VREF_50)
5195 return AC_PINCTL_VREF_50;
5196 else if (pincap & AC_PINCAP_VREF_100)
5197 return AC_PINCTL_VREF_100;
5198 else if (pincap & AC_PINCAP_VREF_GRD)
5199 return AC_PINCTL_VREF_GRD;
5200 return AC_PINCTL_VREF_HIZ;
5201}
5202EXPORT_SYMBOL_HDA(snd_hda_get_default_vref);
5203
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005204int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
5205 unsigned int val, bool cached)
5206{
5207 if (val) {
5208 unsigned int cap = snd_hda_query_pin_caps(codec, pin);
Takashi Iwai6942c102012-04-20 13:08:40 +02005209 if (cap && (val & AC_PINCTL_OUT_EN)) {
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005210 if (!(cap & AC_PINCAP_OUT))
5211 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
5212 else if ((val & AC_PINCTL_HP_EN) &&
5213 !(cap & AC_PINCAP_HP_DRV))
5214 val &= ~AC_PINCTL_HP_EN;
5215 }
Takashi Iwai6942c102012-04-20 13:08:40 +02005216 if (cap && (val & AC_PINCTL_IN_EN)) {
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005217 if (!(cap & AC_PINCAP_IN))
5218 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
5219 }
5220 }
5221 if (cached)
5222 return snd_hda_codec_update_cache(codec, pin, 0,
5223 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5224 else
5225 return snd_hda_codec_write(codec, pin, 0,
5226 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5227}
5228EXPORT_SYMBOL_HDA(_snd_hda_set_pin_ctl);
5229
Takashi Iwai990061c2010-09-09 22:08:44 +02005230/**
5231 * snd_hda_add_imux_item - Add an item to input_mux
5232 *
5233 * When the same label is used already in the existing items, the number
5234 * suffix is appended to the label. This label index number is stored
5235 * to type_idx when non-NULL pointer is given.
5236 */
Takashi Iwai10a20af72010-09-09 16:28:02 +02005237int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
5238 int index, int *type_idx)
5239{
5240 int i, label_idx = 0;
5241 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
5242 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
5243 return -EINVAL;
5244 }
5245 for (i = 0; i < imux->num_items; i++) {
5246 if (!strncmp(label, imux->items[i].label, strlen(label)))
5247 label_idx++;
5248 }
5249 if (type_idx)
5250 *type_idx = label_idx;
5251 if (label_idx > 0)
5252 snprintf(imux->items[imux->num_items].label,
5253 sizeof(imux->items[imux->num_items].label),
5254 "%s %d", label, label_idx);
5255 else
5256 strlcpy(imux->items[imux->num_items].label, label,
5257 sizeof(imux->items[imux->num_items].label));
5258 imux->items[imux->num_items].index = index;
5259 imux->num_items++;
5260 return 0;
5261}
5262EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005263
Takashi Iwai4a471b72005-12-07 13:56:29 +01005264
Linus Torvalds1da177e2005-04-16 15:20:36 -07005265#ifdef CONFIG_PM
5266/*
5267 * power management
5268 */
5269
5270/**
5271 * snd_hda_suspend - suspend the codecs
5272 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005273 *
5274 * Returns 0 if successful.
5275 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02005276int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005277{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005278 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005279
Takashi Iwai0ba21762007-04-16 11:29:14 +02005280 list_for_each_entry(codec, &bus->codec_list, list) {
David Henningsson26a6cb62012-10-09 15:04:21 +02005281 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005282 if (hda_codec_is_power_on(codec))
Dylan Reidd17344b2012-09-28 15:57:01 -07005283 hda_call_codec_suspend(codec, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005284 }
5285 return 0;
5286}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005287EXPORT_SYMBOL_HDA(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005288
5289/**
5290 * snd_hda_resume - resume the codecs
5291 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292 *
5293 * Returns 0 if successful.
5294 */
5295int snd_hda_resume(struct hda_bus *bus)
5296{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005297 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298
Takashi Iwai0ba21762007-04-16 11:29:14 +02005299 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai7f308302012-05-08 16:52:23 +02005300 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005302 return 0;
5303}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005304EXPORT_SYMBOL_HDA(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005305#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005306
5307/*
5308 * generic arrays
5309 */
5310
Takashi Iwaid5191e52009-11-16 14:58:17 +01005311/**
5312 * snd_array_new - get a new element from the given array
5313 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005314 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01005315 * Get a new element from the given array. If it exceeds the
5316 * pre-allocated array size, re-allocate the array.
5317 *
5318 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02005319 */
5320void *snd_array_new(struct snd_array *array)
5321{
Takashi Iwai12f17712012-10-10 08:59:14 +02005322 if (snd_BUG_ON(!array->elem_size))
5323 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005324 if (array->used >= array->alloced) {
5325 int num = array->alloced + array->alloc_align;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005326 int size = (num + 1) * array->elem_size;
Takashi Iwai00ef9612011-07-14 15:57:27 +02005327 int oldsize = array->alloced * array->elem_size;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01005328 void *nlist;
5329 if (snd_BUG_ON(num >= 4096))
5330 return NULL;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005331 nlist = krealloc(array->list, size, GFP_KERNEL);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005332 if (!nlist)
5333 return NULL;
Takashi Iwai00ef9612011-07-14 15:57:27 +02005334 memset(nlist + oldsize, 0, size - oldsize);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005335 array->list = nlist;
5336 array->alloced = num;
5337 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01005338 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005339}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005340EXPORT_SYMBOL_HDA(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005341
Takashi Iwaid5191e52009-11-16 14:58:17 +01005342/**
5343 * snd_array_free - free the given array elements
5344 * @array: the array object
5345 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005346void snd_array_free(struct snd_array *array)
5347{
5348 kfree(array->list);
5349 array->used = 0;
5350 array->alloced = 0;
5351 array->list = NULL;
5352}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005353EXPORT_SYMBOL_HDA(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01005354
Takashi Iwaid5191e52009-11-16 14:58:17 +01005355/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01005356 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5357 * @pcm: PCM caps bits
5358 * @buf: the string buffer to write
5359 * @buflen: the max buffer length
5360 *
5361 * used by hda_proc.c and hda_eld.c
5362 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005363void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5364{
5365 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5366 int i, j;
5367
5368 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5369 if (pcm & (AC_SUPPCM_BITS_8 << i))
5370 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5371
5372 buf[j] = '\0'; /* necessary when j == 0 */
5373}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005374EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005375
5376MODULE_DESCRIPTION("HDA codec core");
5377MODULE_LICENSE("GPL");