blob: 3207e5c571416b6ac3f7344677aff93d504ba74a [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;
Takashi Iwai5fdaecd2012-12-20 10:45:55 +0100427 int null_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Takashi Iwaida3cec32008-08-08 17:12:14 +0200429 if (snd_BUG_ON(!conn_list || max_conns <= 0))
430 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Takashi Iwai1ba7a7c2009-07-27 12:56:26 +0200432 wcaps = get_wcaps(codec, nid);
433 if (!(wcaps & AC_WCAP_CONN_LIST) &&
Takashi Iwai8d087c72011-06-28 12:45:47 +0200434 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
435 return 0;
Jaroslav Kysela16a433d2009-07-22 16:20:40 +0200436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
438 if (parm & AC_CLIST_LONG) {
439 /* long form */
440 shift = 16;
441 num_elems = 2;
442 } else {
443 /* short form */
444 shift = 8;
445 num_elems = 4;
446 }
447 conn_len = parm & AC_CLIST_LENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 mask = (1 << (shift-1)) - 1;
449
Takashi Iwai0ba21762007-04-16 11:29:14 +0200450 if (!conn_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 return 0; /* no connection */
452
453 if (conn_len == 1) {
454 /* single connection */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200455 parm = snd_hda_codec_read(codec, nid, 0,
456 AC_VERB_GET_CONNECT_LIST, 0);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200457 if (parm == -1 && codec->bus->rirb_error)
458 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 conn_list[0] = parm & mask;
460 return 1;
461 }
462
463 /* multi connection */
464 conns = 0;
Takashi Iwai54d17402005-11-21 16:33:22 +0100465 prev_nid = 0;
466 for (i = 0; i < conn_len; i++) {
467 int range_val;
468 hda_nid_t val, n;
469
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200470 if (i % num_elems == 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +0100471 parm = snd_hda_codec_read(codec, nid, 0,
472 AC_VERB_GET_CONNECT_LIST, i);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200473 if (parm == -1 && codec->bus->rirb_error)
474 return -EIO;
475 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200476 range_val = !!(parm & (1 << (shift-1))); /* ranges */
Takashi Iwai54d17402005-11-21 16:33:22 +0100477 val = parm & mask;
Takashi Iwai5fdaecd2012-12-20 10:45:55 +0100478 if (val == 0 && null_count++) { /* no second chance */
Jaroslav Kysela2e9bf242009-07-18 11:48:19 +0200479 snd_printk(KERN_WARNING "hda_codec: "
480 "invalid CONNECT_LIST verb %x[%i]:%x\n",
481 nid, i, parm);
482 return 0;
483 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100484 parm >>= shift;
485 if (range_val) {
486 /* ranges between the previous and this one */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200487 if (!prev_nid || prev_nid >= val) {
488 snd_printk(KERN_WARNING "hda_codec: "
489 "invalid dep_range_val %x:%x\n",
490 prev_nid, val);
Takashi Iwai54d17402005-11-21 16:33:22 +0100491 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100493 for (n = prev_nid + 1; n <= val; n++) {
494 if (conns >= max_conns) {
Takashi Iwai5aacc212010-07-30 10:36:29 +0200495 snd_printk(KERN_ERR "hda_codec: "
496 "Too many connections %d for NID 0x%x\n",
497 conns, nid);
Takashi Iwai54d17402005-11-21 16:33:22 +0100498 return -EINVAL;
499 }
500 conn_list[conns++] = n;
501 }
502 } else {
503 if (conns >= max_conns) {
Takashi Iwai5aacc212010-07-30 10:36:29 +0200504 snd_printk(KERN_ERR "hda_codec: "
505 "Too many connections %d for NID 0x%x\n",
506 conns, nid);
Takashi Iwai54d17402005-11-21 16:33:22 +0100507 return -EINVAL;
508 }
509 conn_list[conns++] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100511 prev_nid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 }
513 return conns;
514}
515
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200516static bool add_conn_list(struct snd_array *array, hda_nid_t nid)
517{
518 hda_nid_t *p = snd_array_new(array);
519 if (!p)
520 return false;
521 *p = nid;
522 return true;
523}
524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525/**
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200526 * snd_hda_override_conn_list - add/modify the connection-list to cache
527 * @codec: the HDA codec
528 * @nid: NID to parse
529 * @len: number of connection list entries
530 * @list: the list of connection entries
531 *
532 * Add or modify the given connection-list to the cache. If the corresponding
533 * cache already exists, invalidate it and append a new one.
534 *
535 * Returns zero or a negative error code.
536 */
537int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
538 const hda_nid_t *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539{
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200540 struct snd_array *array = &codec->conn_lists;
541 hda_nid_t *p;
542 int i, old_used;
543
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200544 mutex_lock(&codec->hash_mutex);
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200545 p = lookup_conn_list(array, nid);
546 if (p)
547 *p = -1; /* invalidate the old entry */
548
549 old_used = array->used;
550 if (!add_conn_list(array, nid) || !add_conn_list(array, len))
551 goto error_add;
552 for (i = 0; i < len; i++)
553 if (!add_conn_list(array, list[i]))
554 goto error_add;
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200555 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200556 return 0;
557
558 error_add:
559 array->used = old_used;
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200560 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200561 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562}
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200563EXPORT_SYMBOL_HDA(snd_hda_override_conn_list);
564
565/**
Takashi Iwai8d087c72011-06-28 12:45:47 +0200566 * snd_hda_get_conn_index - get the connection index of the given NID
567 * @codec: the HDA codec
568 * @mux: NID containing the list
569 * @nid: NID to select
570 * @recursive: 1 when searching NID recursively, otherwise 0
571 *
572 * Parses the connection list of the widget @mux and checks whether the
573 * widget @nid is present. If it is, return the connection index.
574 * Otherwise it returns -1.
575 */
576int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
577 hda_nid_t nid, int recursive)
578{
579 hda_nid_t conn[HDA_MAX_NUM_INPUTS];
580 int i, nums;
581
582 nums = snd_hda_get_connections(codec, mux, conn, ARRAY_SIZE(conn));
583 for (i = 0; i < nums; i++)
584 if (conn[i] == nid)
585 return i;
586 if (!recursive)
587 return -1;
588 if (recursive > 5) {
589 snd_printd("hda_codec: too deep connection for 0x%x\n", nid);
590 return -1;
591 }
592 recursive++;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200593 for (i = 0; i < nums; i++) {
594 unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
595 if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
596 continue;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200597 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
598 return i;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200599 }
Takashi Iwai8d087c72011-06-28 12:45:47 +0200600 return -1;
601}
602EXPORT_SYMBOL_HDA(snd_hda_get_conn_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
604/**
605 * snd_hda_queue_unsol_event - add an unsolicited event to queue
606 * @bus: the BUS
607 * @res: unsolicited event (lower 32bit of RIRB entry)
608 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
609 *
610 * Adds the given event to the queue. The events are processed in
611 * the workqueue asynchronously. Call this function in the interrupt
612 * hanlder when RIRB receives an unsolicited event.
613 *
614 * Returns 0 if successful, or a negative error code.
615 */
616int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
617{
618 struct hda_bus_unsolicited *unsol;
619 unsigned int wp;
620
Takashi Iwaiecf726f2011-08-09 14:22:44 +0200621 trace_hda_unsol_event(bus, res, res_ex);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200622 unsol = bus->unsol;
623 if (!unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 return 0;
625
626 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
627 unsol->wp = wp;
628
629 wp <<= 1;
630 unsol->queue[wp] = res;
631 unsol->queue[wp + 1] = res_ex;
632
Takashi Iwai6acaed32009-01-12 10:09:24 +0100633 queue_work(bus->workq, &unsol->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
635 return 0;
636}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100637EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
639/*
Wu Fengguang5c1d1a92008-10-07 14:17:53 +0800640 * process queued unsolicited events
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 */
David Howellsc4028952006-11-22 14:57:56 +0000642static void process_unsol_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643{
David Howellsc4028952006-11-22 14:57:56 +0000644 struct hda_bus_unsolicited *unsol =
645 container_of(work, struct hda_bus_unsolicited, work);
646 struct hda_bus *bus = unsol->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 struct hda_codec *codec;
648 unsigned int rp, caddr, res;
649
650 while (unsol->rp != unsol->wp) {
651 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
652 unsol->rp = rp;
653 rp <<= 1;
654 res = unsol->queue[rp];
655 caddr = unsol->queue[rp + 1];
Takashi Iwai0ba21762007-04-16 11:29:14 +0200656 if (!(caddr & (1 << 4))) /* no unsolicited event? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 continue;
658 codec = bus->caddr_tbl[caddr & 0x0f];
659 if (codec && codec->patch_ops.unsol_event)
660 codec->patch_ops.unsol_event(codec, res);
661 }
662}
663
664/*
665 * initialize unsolicited queue
666 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200667static int init_unsol_queue(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668{
669 struct hda_bus_unsolicited *unsol;
670
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100671 if (bus->unsol) /* already initialized */
672 return 0;
673
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200674 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200675 if (!unsol) {
676 snd_printk(KERN_ERR "hda_codec: "
677 "can't allocate unsolicited queue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 return -ENOMEM;
679 }
David Howellsc4028952006-11-22 14:57:56 +0000680 INIT_WORK(&unsol->work, process_unsol_events);
681 unsol->bus = bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 bus->unsol = unsol;
683 return 0;
684}
685
686/*
687 * destructor
688 */
689static void snd_hda_codec_free(struct hda_codec *codec);
690
691static int snd_hda_bus_free(struct hda_bus *bus)
692{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200693 struct hda_codec *codec, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Takashi Iwai0ba21762007-04-16 11:29:14 +0200695 if (!bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 return 0;
Takashi Iwai6acaed32009-01-12 10:09:24 +0100697 if (bus->workq)
698 flush_workqueue(bus->workq);
699 if (bus->unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 kfree(bus->unsol);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200701 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 snd_hda_codec_free(codec);
703 }
704 if (bus->ops.private_free)
705 bus->ops.private_free(bus);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100706 if (bus->workq)
707 destroy_workqueue(bus->workq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 kfree(bus);
709 return 0;
710}
711
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100712static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
714 struct hda_bus *bus = device->device_data;
Takashi Iwaib94d35392008-11-21 09:08:06 +0100715 bus->shutdown = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 return snd_hda_bus_free(bus);
717}
718
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200719#ifdef CONFIG_SND_HDA_HWDEP
720static int snd_hda_bus_dev_register(struct snd_device *device)
721{
722 struct hda_bus *bus = device->device_data;
723 struct hda_codec *codec;
724 list_for_each_entry(codec, &bus->codec_list, list) {
725 snd_hda_hwdep_add_sysfs(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +0100726 snd_hda_hwdep_add_power_sysfs(codec);
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200727 }
728 return 0;
729}
730#else
731#define snd_hda_bus_dev_register NULL
732#endif
733
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734/**
735 * snd_hda_bus_new - create a HDA bus
736 * @card: the card entry
737 * @temp: the template for hda_bus information
738 * @busp: the pointer to store the created bus instance
739 *
740 * Returns 0 if successful, or a negative error code.
741 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +0100742int snd_hda_bus_new(struct snd_card *card,
Takashi Iwai756e2b02007-04-16 11:27:07 +0200743 const struct hda_bus_template *temp,
744 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745{
746 struct hda_bus *bus;
747 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100748 static struct snd_device_ops dev_ops = {
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200749 .dev_register = snd_hda_bus_dev_register,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 .dev_free = snd_hda_bus_dev_free,
751 };
752
Takashi Iwaida3cec32008-08-08 17:12:14 +0200753 if (snd_BUG_ON(!temp))
754 return -EINVAL;
755 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
756 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
758 if (busp)
759 *busp = NULL;
760
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200761 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 if (bus == NULL) {
763 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
764 return -ENOMEM;
765 }
766
767 bus->card = card;
768 bus->private_data = temp->private_data;
769 bus->pci = temp->pci;
770 bus->modelname = temp->modelname;
Takashi Iwaifee2fba2008-11-27 12:43:28 +0100771 bus->power_save = temp->power_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 bus->ops = temp->ops;
773
Ingo Molnar62932df2006-01-16 16:34:20 +0100774 mutex_init(&bus->cmd_mutex);
Takashi Iwai3f50ac62010-08-20 09:44:36 +0200775 mutex_init(&bus->prepare_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 INIT_LIST_HEAD(&bus->codec_list);
777
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100778 snprintf(bus->workq_name, sizeof(bus->workq_name),
779 "hd-audio%d", card->number);
780 bus->workq = create_singlethread_workqueue(bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100781 if (!bus->workq) {
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100782 snd_printk(KERN_ERR "cannot create workqueue %s\n",
783 bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100784 kfree(bus);
785 return -ENOMEM;
786 }
787
Takashi Iwai0ba21762007-04-16 11:29:14 +0200788 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
789 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 snd_hda_bus_free(bus);
791 return err;
792 }
793 if (busp)
794 *busp = bus;
795 return 0;
796}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100797EXPORT_SYMBOL_HDA(snd_hda_bus_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Takashi Iwai82467612007-07-27 19:15:54 +0200799#ifdef CONFIG_SND_HDA_GENERIC
800#define is_generic_config(codec) \
Takashi Iwaif44ac832008-07-30 15:01:45 +0200801 (codec->modelname && !strcmp(codec->modelname, "generic"))
Takashi Iwai82467612007-07-27 19:15:54 +0200802#else
803#define is_generic_config(codec) 0
804#endif
805
Takashi Iwai645f10c2008-11-28 15:07:37 +0100806#ifdef MODULE
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100807#define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
808#else
Takashi Iwai645f10c2008-11-28 15:07:37 +0100809#define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100810#endif
811
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812/*
813 * find a matching codec preset
814 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200815static const struct hda_codec_preset *
Takashi Iwai756e2b02007-04-16 11:27:07 +0200816find_codec_preset(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817{
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100818 struct hda_codec_preset_list *tbl;
819 const struct hda_codec_preset *preset;
Takashi Iwai5d908ab2012-08-24 18:40:10 +0200820 unsigned int mod_requested = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Takashi Iwai82467612007-07-27 19:15:54 +0200822 if (is_generic_config(codec))
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100823 return NULL; /* use the generic parser */
824
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100825 again:
826 mutex_lock(&preset_mutex);
827 list_for_each_entry(tbl, &hda_preset_tables, list) {
828 if (!try_module_get(tbl->owner)) {
829 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
830 continue;
831 }
832 for (preset = tbl->preset; preset->id; preset++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 u32 mask = preset->mask;
Marc Boucherca7cfae2008-01-22 15:32:25 +0100834 if (preset->afg && preset->afg != codec->afg)
835 continue;
836 if (preset->mfg && preset->mfg != codec->mfg)
837 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200838 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 mask = ~0;
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200840 if (preset->id == (codec->vendor_id & mask) &&
Takashi Iwai0ba21762007-04-16 11:29:14 +0200841 (!preset->rev ||
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100842 preset->rev == codec->revision_id)) {
843 mutex_unlock(&preset_mutex);
844 codec->owner = tbl->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 return preset;
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 }
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100848 module_put(tbl->owner);
849 }
850 mutex_unlock(&preset_mutex);
851
852 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
853 char name[32];
854 if (!mod_requested)
855 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
856 codec->vendor_id);
857 else
858 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
859 (codec->vendor_id >> 16) & 0xffff);
860 request_module(name);
861 mod_requested++;
862 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 }
864 return NULL;
865}
866
867/*
Takashi Iwaif44ac832008-07-30 15:01:45 +0200868 * get_codec_name - store the codec name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 */
Takashi Iwaif44ac832008-07-30 15:01:45 +0200870static int get_codec_name(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
872 const struct hda_vendor_id *c;
873 const char *vendor = NULL;
874 u16 vendor_id = codec->vendor_id >> 16;
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200875 char tmp[16];
876
877 if (codec->vendor_name)
878 goto get_chip_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
880 for (c = hda_vendor_ids; c->id; c++) {
881 if (c->id == vendor_id) {
882 vendor = c->name;
883 break;
884 }
885 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200886 if (!vendor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 sprintf(tmp, "Generic %04x", vendor_id);
888 vendor = tmp;
889 }
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200890 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
891 if (!codec->vendor_name)
892 return -ENOMEM;
893
894 get_chip_name:
895 if (codec->chip_name)
896 return 0;
897
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 if (codec->preset && codec->preset->name)
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200899 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
900 else {
901 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
902 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
903 }
904 if (!codec->chip_name)
Takashi Iwaif44ac832008-07-30 15:01:45 +0200905 return -ENOMEM;
906 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907}
908
909/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200910 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +0100912static void setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913{
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200914 int i, total_nodes, function_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 hda_nid_t nid;
916
917 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
918 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200919 function_id = snd_hda_param_read(codec, nid,
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200920 AC_PAR_FUNCTION_TYPE);
Jaroslav Kyselacd7643b2010-07-20 12:11:25 +0200921 switch (function_id & 0xff) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200922 case AC_GRP_AUDIO_FUNCTION:
923 codec->afg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200924 codec->afg_function_id = function_id & 0xff;
925 codec->afg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200926 break;
927 case AC_GRP_MODEM_FUNCTION:
928 codec->mfg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200929 codec->mfg_function_id = function_id & 0xff;
930 codec->mfg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200931 break;
932 default:
933 break;
934 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936}
937
938/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100939 * read widget caps for each widget and store in cache
940 */
941static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
942{
943 int i;
944 hda_nid_t nid;
945
946 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
947 &codec->start_nid);
948 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200949 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +0100950 return -ENOMEM;
951 nid = codec->start_nid;
952 for (i = 0; i < codec->num_nodes; i++, nid++)
953 codec->wcaps[i] = snd_hda_param_read(codec, nid,
954 AC_PAR_AUDIO_WIDGET_CAP);
955 return 0;
956}
957
Takashi Iwai3be14142009-02-20 14:11:16 +0100958/* read all pin default configurations and save codec->init_pins */
959static int read_pin_defaults(struct hda_codec *codec)
960{
961 int i;
962 hda_nid_t nid = codec->start_nid;
963
964 for (i = 0; i < codec->num_nodes; i++, nid++) {
965 struct hda_pincfg *pin;
966 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +0200967 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwai3be14142009-02-20 14:11:16 +0100968 if (wid_type != AC_WID_PIN)
969 continue;
970 pin = snd_array_new(&codec->init_pins);
971 if (!pin)
972 return -ENOMEM;
973 pin->nid = nid;
974 pin->cfg = snd_hda_codec_read(codec, nid, 0,
975 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200976 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
977 AC_VERB_GET_PIN_WIDGET_CONTROL,
978 0);
Takashi Iwai3be14142009-02-20 14:11:16 +0100979 }
980 return 0;
981}
982
983/* look up the given pin config list and return the item matching with NID */
984static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
985 struct snd_array *array,
986 hda_nid_t nid)
987{
988 int i;
989 for (i = 0; i < array->used; i++) {
990 struct hda_pincfg *pin = snd_array_elem(array, i);
991 if (pin->nid == nid)
992 return pin;
993 }
994 return NULL;
995}
996
Takashi Iwai3be14142009-02-20 14:11:16 +0100997/* set the current pin config value for the given NID.
998 * the value is cached, and read via snd_hda_codec_get_pincfg()
999 */
1000int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
1001 hda_nid_t nid, unsigned int cfg)
1002{
1003 struct hda_pincfg *pin;
1004
Takashi Iwaib82855a2009-12-27 11:24:56 +01001005 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
1006 return -EINVAL;
1007
Takashi Iwai3be14142009-02-20 14:11:16 +01001008 pin = look_up_pincfg(codec, list, nid);
1009 if (!pin) {
1010 pin = snd_array_new(list);
1011 if (!pin)
1012 return -ENOMEM;
1013 pin->nid = nid;
1014 }
1015 pin->cfg = cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001016 return 0;
1017}
1018
Takashi Iwaid5191e52009-11-16 14:58:17 +01001019/**
1020 * snd_hda_codec_set_pincfg - Override a pin default configuration
1021 * @codec: the HDA codec
1022 * @nid: NID to set the pin config
1023 * @cfg: the pin default config value
1024 *
1025 * Override a pin default configuration value in the cache.
1026 * This value can be read by snd_hda_codec_get_pincfg() in a higher
1027 * priority than the real hardware value.
1028 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001029int snd_hda_codec_set_pincfg(struct hda_codec *codec,
1030 hda_nid_t nid, unsigned int cfg)
1031{
Takashi Iwai346ff702009-02-23 09:42:57 +01001032 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001033}
1034EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
1035
Takashi Iwaid5191e52009-11-16 14:58:17 +01001036/**
1037 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
1038 * @codec: the HDA codec
1039 * @nid: NID to get the pin config
1040 *
1041 * Get the current pin config value of the given pin NID.
1042 * If the pincfg value is cached or overridden via sysfs or driver,
1043 * returns the cached value.
1044 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001045unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
1046{
1047 struct hda_pincfg *pin;
1048
Takashi Iwai3be14142009-02-20 14:11:16 +01001049#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +01001050 pin = look_up_pincfg(codec, &codec->user_pins, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +01001051 if (pin)
1052 return pin->cfg;
1053#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001054 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
1055 if (pin)
1056 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001057 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1058 if (pin)
1059 return pin->cfg;
1060 return 0;
1061}
1062EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
1063
Takashi Iwai92ee6162009-12-27 11:18:59 +01001064/**
1065 * snd_hda_shutup_pins - Shut up all pins
1066 * @codec: the HDA codec
1067 *
1068 * Clear all pin controls to shup up before suspend for avoiding click noise.
1069 * The controls aren't cached so that they can be resumed properly.
1070 */
1071void snd_hda_shutup_pins(struct hda_codec *codec)
1072{
1073 int i;
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001074 /* don't shut up pins when unloading the driver; otherwise it breaks
1075 * the default pin setup at the next load of the driver
1076 */
1077 if (codec->bus->shutdown)
1078 return;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001079 for (i = 0; i < codec->init_pins.used; i++) {
1080 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1081 /* use read here for syncing after issuing each verb */
1082 snd_hda_codec_read(codec, pin->nid, 0,
1083 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
1084 }
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001085 codec->pins_shutup = 1;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001086}
1087EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);
1088
Takashi Iwai2a439522011-07-26 09:52:50 +02001089#ifdef CONFIG_PM
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001090/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1091static void restore_shutup_pins(struct hda_codec *codec)
1092{
1093 int i;
1094 if (!codec->pins_shutup)
1095 return;
1096 if (codec->bus->shutdown)
1097 return;
1098 for (i = 0; i < codec->init_pins.used; i++) {
1099 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1100 snd_hda_codec_write(codec, pin->nid, 0,
1101 AC_VERB_SET_PIN_WIDGET_CONTROL,
1102 pin->ctrl);
1103 }
1104 codec->pins_shutup = 0;
1105}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001106#endif
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001107
David Henningsson26a6cb62012-10-09 15:04:21 +02001108static void hda_jackpoll_work(struct work_struct *work)
1109{
1110 struct hda_codec *codec =
1111 container_of(work, struct hda_codec, jackpoll_work.work);
1112 if (!codec->jackpoll_interval)
1113 return;
1114
1115 snd_hda_jack_set_dirty_all(codec);
1116 snd_hda_jack_poll_all(codec);
1117 queue_delayed_work(codec->bus->workq, &codec->jackpoll_work,
1118 codec->jackpoll_interval);
1119}
1120
Takashi Iwai01751f52007-08-10 16:59:39 +02001121static void init_hda_cache(struct hda_cache_rec *cache,
1122 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001123static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +02001124
Takashi Iwai3fdf1462012-11-22 08:16:31 +01001125/* release all pincfg lists */
1126static void free_init_pincfgs(struct hda_codec *codec)
Takashi Iwai3be14142009-02-20 14:11:16 +01001127{
Takashi Iwai346ff702009-02-23 09:42:57 +01001128 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001129#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +01001130 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001131#endif
Takashi Iwai3be14142009-02-20 14:11:16 +01001132 snd_array_free(&codec->init_pins);
1133}
1134
Takashi Iwai54d17402005-11-21 16:33:22 +01001135/*
Takashi Iwaieb541332010-08-06 13:48:11 +02001136 * audio-converter setup caches
1137 */
1138struct hda_cvt_setup {
1139 hda_nid_t nid;
1140 u8 stream_tag;
1141 u8 channel_id;
1142 u16 format_id;
1143 unsigned char active; /* cvt is currently used */
1144 unsigned char dirty; /* setups should be cleared */
1145};
1146
1147/* get or create a cache entry for the given audio converter NID */
1148static struct hda_cvt_setup *
1149get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1150{
1151 struct hda_cvt_setup *p;
1152 int i;
1153
1154 for (i = 0; i < codec->cvt_setups.used; i++) {
1155 p = snd_array_elem(&codec->cvt_setups, i);
1156 if (p->nid == nid)
1157 return p;
1158 }
1159 p = snd_array_new(&codec->cvt_setups);
1160 if (p)
1161 p->nid = nid;
1162 return p;
1163}
1164
1165/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 * codec destructor
1167 */
1168static void snd_hda_codec_free(struct hda_codec *codec)
1169{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001170 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 return;
David Henningsson26a6cb62012-10-09 15:04:21 +02001172 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai59cad162012-06-26 15:00:20 +02001173 snd_hda_jack_tbl_clear(codec);
Takashi Iwai3fdf1462012-11-22 08:16:31 +01001174 free_init_pincfgs(codec);
Takashi Iwai83012a72012-08-24 18:38:08 +02001175#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02001176 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01001177 flush_workqueue(codec->bus->workq);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001178#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001180 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001181 snd_array_free(&codec->nids);
Takashi Iwai59cad162012-06-26 15:00:20 +02001182 snd_array_free(&codec->cvt_setups);
Takashi Iwaia12d3e12011-04-07 15:55:15 +02001183 snd_array_free(&codec->conn_lists);
Stephen Warren7c935972011-06-01 11:14:17 -06001184 snd_array_free(&codec->spdif_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 codec->bus->caddr_tbl[codec->addr] = NULL;
1186 if (codec->patch_ops.free)
1187 codec->patch_ops.free(codec);
Takashi Iwai83012a72012-08-24 18:38:08 +02001188#ifdef CONFIG_PM
Takashi Iwai08fa20a2012-08-31 07:46:56 -07001189 if (!codec->pm_down_notified) /* cancel leftover refcounts */
Takashi Iwai68467f52012-08-28 09:14:29 -07001190 hda_call_pm_notify(codec->bus, false);
1191#endif
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001192 module_put(codec->owner);
Takashi Iwai01751f52007-08-10 16:59:39 +02001193 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001194 free_hda_cache(&codec->cmd_cache);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001195 kfree(codec->vendor_name);
1196 kfree(codec->chip_name);
Takashi Iwaif44ac832008-07-30 15:01:45 +02001197 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +01001198 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 kfree(codec);
1200}
1201
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001202static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec,
1203 hda_nid_t fg, unsigned int power_state);
1204
Takashi Iwaid8193872012-08-31 07:54:38 -07001205static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001206 unsigned int power_state);
1207
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208/**
1209 * snd_hda_codec_new - create a HDA codec
1210 * @bus: the bus to assign
1211 * @codec_addr: the codec address
1212 * @codecp: the pointer to store the generated codec
1213 *
1214 * Returns 0 if successful, or a negative error code.
1215 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001216int snd_hda_codec_new(struct hda_bus *bus,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001217 unsigned int codec_addr,
1218 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219{
1220 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +02001221 char component[31];
Takashi Iwaid8193872012-08-31 07:54:38 -07001222 hda_nid_t fg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 int err;
1224
Takashi Iwaida3cec32008-08-08 17:12:14 +02001225 if (snd_BUG_ON(!bus))
1226 return -EINVAL;
1227 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1228 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
1230 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001231 snd_printk(KERN_ERR "hda_codec: "
1232 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 return -EBUSY;
1234 }
1235
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001236 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 if (codec == NULL) {
1238 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1239 return -ENOMEM;
1240 }
1241
1242 codec->bus = bus;
1243 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001244 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001245 mutex_init(&codec->control_mutex);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001246 mutex_init(&codec->hash_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +02001247 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001248 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001249 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1250 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001251 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001252 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +02001253 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Takashi Iwaia12d3e12011-04-07 15:55:15 +02001254 snd_array_init(&codec->conn_lists, sizeof(hda_nid_t), 64);
Stephen Warren7c935972011-06-01 11:14:17 -06001255 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
Takashi Iwai361dab32012-05-09 14:35:27 +02001256 snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01001257 snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8);
David Henningsson26a6cb62012-10-09 15:04:21 +02001258 INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
Takashi Iwai83012a72012-08-24 18:38:08 +02001260#ifdef CONFIG_PM
Takashi Iwai5536c6d2012-05-08 17:08:10 +02001261 spin_lock_init(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001262 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1263 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1264 * the caller has to power down appropriatley after initialization
1265 * phase.
1266 */
1267 hda_keep_power_on(codec);
Takashi Iwai68467f52012-08-28 09:14:29 -07001268 hda_call_pm_notify(bus, true);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001269#endif
1270
Takashi Iwaic382a9f2012-05-07 15:01:02 +02001271 if (codec->bus->modelname) {
1272 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1273 if (!codec->modelname) {
1274 snd_hda_codec_free(codec);
1275 return -ENODEV;
1276 }
1277 }
1278
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 list_add_tail(&codec->list, &bus->codec_list);
1280 bus->caddr_tbl[codec_addr] = codec;
1281
Takashi Iwai0ba21762007-04-16 11:29:14 +02001282 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1283 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001284 if (codec->vendor_id == -1)
1285 /* read again, hopefully the access method was corrected
1286 * in the last read...
1287 */
1288 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1289 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001290 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1291 AC_PAR_SUBSYSTEM_ID);
1292 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1293 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001295 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001296 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001297 snd_printdd("hda_codec: no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001298 err = -ENODEV;
1299 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 }
1301
Takashi Iwaid8193872012-08-31 07:54:38 -07001302 fg = codec->afg ? codec->afg : codec->mfg;
1303 err = read_widget_caps(codec, fg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001304 if (err < 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +01001305 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001306 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001307 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001308 err = read_pin_defaults(codec);
1309 if (err < 0)
1310 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001311
Takashi Iwai0ba21762007-04-16 11:29:14 +02001312 if (!codec->subsystem_id) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001313 codec->subsystem_id =
Takashi Iwaid8193872012-08-31 07:54:38 -07001314 snd_hda_codec_read(codec, fg, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001315 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001316 }
1317
Takashi Iwai83012a72012-08-24 18:38:08 +02001318#ifdef CONFIG_PM
Takashi Iwaid8193872012-08-31 07:54:38 -07001319 codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec, fg,
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001320 AC_PWRST_CLKSTOP);
1321 if (!codec->d3_stop_clk)
1322 bus->power_keep_link_on = 1;
Mengdong Lin5d6147f2012-08-24 12:06:30 +08001323#endif
Takashi Iwaid8193872012-08-31 07:54:38 -07001324 codec->epss = snd_hda_codec_get_supported_ps(codec, fg,
Takashi Iwai983f6b92012-08-28 09:18:01 -07001325 AC_PWRST_EPSS);
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001326
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001327 /* power-up all before initialization */
Takashi Iwaid8193872012-08-31 07:54:38 -07001328 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001329
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001330 snd_hda_codec_proc_new(codec);
1331
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001332 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001333
1334 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1335 codec->subsystem_id, codec->revision_id);
1336 snd_component_add(codec->bus->card, component);
1337
1338 if (codecp)
1339 *codecp = codec;
1340 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001341
1342 error:
1343 snd_hda_codec_free(codec);
1344 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001345}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001346EXPORT_SYMBOL_HDA(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001347
Takashi Iwaid5191e52009-11-16 14:58:17 +01001348/**
1349 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1350 * @codec: the HDA codec
1351 *
1352 * Start parsing of the given codec tree and (re-)initialize the whole
1353 * patch instance.
1354 *
1355 * Returns 0 if successful or a negative error code.
1356 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001357int snd_hda_codec_configure(struct hda_codec *codec)
1358{
1359 int err;
1360
Takashi Iwaid5ad6302007-03-07 15:55:59 +01001361 codec->preset = find_codec_preset(codec);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001362 if (!codec->vendor_name || !codec->chip_name) {
Takashi Iwaif44ac832008-07-30 15:01:45 +02001363 err = get_codec_name(codec);
1364 if (err < 0)
1365 return err;
1366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
Takashi Iwai82467612007-07-27 19:15:54 +02001368 if (is_generic_config(codec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai82467612007-07-27 19:15:54 +02001370 goto patched;
1371 }
Takashi Iwai82467612007-07-27 19:15:54 +02001372 if (codec->preset && codec->preset->patch) {
1373 err = codec->preset->patch(codec);
1374 goto patched;
1375 }
1376
1377 /* call the default parser */
Takashi Iwai82467612007-07-27 19:15:54 +02001378 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai35a1e0c2007-10-19 08:13:40 +02001379 if (err < 0)
1380 printk(KERN_ERR "hda-codec: No codec parser is available\n");
Takashi Iwai82467612007-07-27 19:15:54 +02001381
1382 patched:
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001383 if (!err && codec->patch_ops.unsol_event)
1384 err = init_unsol_queue(codec->bus);
Takashi Iwaif62faed2009-12-23 09:27:51 +01001385 /* audio codec should override the mixer name */
1386 if (!err && (codec->afg || !*codec->bus->card->mixername))
1387 snprintf(codec->bus->card->mixername,
1388 sizeof(codec->bus->card->mixername),
1389 "%s %s", codec->vendor_name, codec->chip_name);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001390 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391}
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001392EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
Takashi Iwaied360812012-08-08 17:12:52 +02001394/* update the stream-id if changed */
1395static void update_pcm_stream_id(struct hda_codec *codec,
1396 struct hda_cvt_setup *p, hda_nid_t nid,
1397 u32 stream_tag, int channel_id)
1398{
1399 unsigned int oldval, newval;
1400
1401 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1402 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1403 newval = (stream_tag << 4) | channel_id;
1404 if (oldval != newval)
1405 snd_hda_codec_write(codec, nid, 0,
1406 AC_VERB_SET_CHANNEL_STREAMID,
1407 newval);
1408 p->stream_tag = stream_tag;
1409 p->channel_id = channel_id;
1410 }
1411}
1412
1413/* update the format-id if changed */
1414static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1415 hda_nid_t nid, int format)
1416{
1417 unsigned int oldval;
1418
1419 if (p->format_id != format) {
1420 oldval = snd_hda_codec_read(codec, nid, 0,
1421 AC_VERB_GET_STREAM_FORMAT, 0);
1422 if (oldval != format) {
1423 msleep(1);
1424 snd_hda_codec_write(codec, nid, 0,
1425 AC_VERB_SET_STREAM_FORMAT,
1426 format);
1427 }
1428 p->format_id = format;
1429 }
1430}
1431
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432/**
1433 * snd_hda_codec_setup_stream - set up the codec for streaming
1434 * @codec: the CODEC to set up
1435 * @nid: the NID to set up
1436 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1437 * @channel_id: channel id to pass, zero based.
1438 * @format: stream format.
1439 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001440void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1441 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 int channel_id, int format)
1443{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001444 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001445 struct hda_cvt_setup *p;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001446 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001447 int i;
1448
Takashi Iwai0ba21762007-04-16 11:29:14 +02001449 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001450 return;
1451
Takashi Iwai0ba21762007-04-16 11:29:14 +02001452 snd_printdd("hda_codec_setup_stream: "
1453 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001455 p = get_hda_cvt_setup(codec, nid);
1456 if (!p)
1457 return;
Takashi Iwaied360812012-08-08 17:12:52 +02001458
1459 if (codec->pcm_format_first)
1460 update_pcm_format(codec, p, nid, format);
1461 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1462 if (!codec->pcm_format_first)
1463 update_pcm_format(codec, p, nid, format);
1464
Takashi Iwaieb541332010-08-06 13:48:11 +02001465 p->active = 1;
1466 p->dirty = 0;
1467
1468 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001469 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001470 list_for_each_entry(c, &codec->bus->codec_list, list) {
1471 for (i = 0; i < c->cvt_setups.used; i++) {
1472 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001473 if (!p->active && p->stream_tag == stream_tag &&
David Henningsson54c2a892012-02-01 12:05:41 +01001474 get_wcaps_type(get_wcaps(c, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001475 p->dirty = 1;
1476 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001479EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
Takashi Iwaif0cea792010-08-13 11:56:53 +02001481static void really_cleanup_stream(struct hda_codec *codec,
1482 struct hda_cvt_setup *q);
1483
Takashi Iwaid5191e52009-11-16 14:58:17 +01001484/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001485 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001486 * @codec: the CODEC to clean up
1487 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001488 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001489 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001490void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1491 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001492{
Takashi Iwaieb541332010-08-06 13:48:11 +02001493 struct hda_cvt_setup *p;
1494
Takashi Iwai888afa12008-03-18 09:57:50 +01001495 if (!nid)
1496 return;
1497
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001498 if (codec->no_sticky_stream)
1499 do_now = 1;
1500
Takashi Iwai888afa12008-03-18 09:57:50 +01001501 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001502 p = get_hda_cvt_setup(codec, nid);
Takashi Iwaif0cea792010-08-13 11:56:53 +02001503 if (p) {
1504 /* here we just clear the active flag when do_now isn't set;
1505 * actual clean-ups will be done later in
1506 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1507 */
1508 if (do_now)
1509 really_cleanup_stream(codec, p);
1510 else
1511 p->active = 0;
1512 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001513}
Takashi Iwaif0cea792010-08-13 11:56:53 +02001514EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001515
Takashi Iwaieb541332010-08-06 13:48:11 +02001516static void really_cleanup_stream(struct hda_codec *codec,
1517 struct hda_cvt_setup *q)
1518{
1519 hda_nid_t nid = q->nid;
Takashi Iwai218264a2011-09-27 17:33:45 +02001520 if (q->stream_tag || q->channel_id)
1521 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1522 if (q->format_id)
1523 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1524);
Takashi Iwaieb541332010-08-06 13:48:11 +02001525 memset(q, 0, sizeof(*q));
1526 q->nid = nid;
1527}
1528
1529/* clean up the all conflicting obsolete streams */
1530static void purify_inactive_streams(struct hda_codec *codec)
1531{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001532 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001533 int i;
1534
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001535 list_for_each_entry(c, &codec->bus->codec_list, list) {
1536 for (i = 0; i < c->cvt_setups.used; i++) {
1537 struct hda_cvt_setup *p;
1538 p = snd_array_elem(&c->cvt_setups, i);
1539 if (p->dirty)
1540 really_cleanup_stream(c, p);
1541 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001542 }
1543}
1544
Takashi Iwai2a439522011-07-26 09:52:50 +02001545#ifdef CONFIG_PM
Takashi Iwaieb541332010-08-06 13:48:11 +02001546/* clean up all streams; called from suspend */
1547static void hda_cleanup_all_streams(struct hda_codec *codec)
1548{
1549 int i;
1550
1551 for (i = 0; i < codec->cvt_setups.used; i++) {
1552 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1553 if (p->stream_tag)
1554 really_cleanup_stream(codec, p);
1555 }
1556}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001557#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001558
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559/*
1560 * amp access functions
1561 */
1562
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001563/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001564#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001565#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001566#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1567#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001569#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
1571/* initialize the hash table */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001572static void init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001573 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574{
Takashi Iwai01751f52007-08-10 16:59:39 +02001575 memset(cache, 0, sizeof(*cache));
1576 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001577 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001578}
1579
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001580static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001581{
Takashi Iwai603c4012008-07-30 15:01:44 +02001582 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583}
1584
1585/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001586static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587{
Takashi Iwai01751f52007-08-10 16:59:39 +02001588 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1589 u16 cur = cache->hash[idx];
1590 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
1592 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001593 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 if (info->key == key)
1595 return info;
1596 cur = info->next;
1597 }
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001598 return NULL;
1599}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001601/* query the hash. allocate an entry if not found. */
1602static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1603 u32 key)
1604{
1605 struct hda_cache_head *info = get_hash(cache, key);
1606 if (!info) {
1607 u16 idx, cur;
1608 /* add a new hash entry */
1609 info = snd_array_new(&cache->buf);
1610 if (!info)
1611 return NULL;
1612 cur = snd_array_index(&cache->buf, info);
1613 info->key = key;
1614 info->val = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01001615 info->dirty = 0;
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001616 idx = key % (u16)ARRAY_SIZE(cache->hash);
1617 info->next = cache->hash[idx];
1618 cache->hash[idx] = cur;
1619 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 return info;
1621}
1622
Takashi Iwai01751f52007-08-10 16:59:39 +02001623/* query and allocate an amp hash entry */
1624static inline struct hda_amp_info *
1625get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1626{
1627 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1628}
1629
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001630/* overwrite the value with the key in the caps hash */
1631static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val)
1632{
1633 struct hda_amp_info *info;
1634
1635 mutex_lock(&codec->hash_mutex);
1636 info = get_alloc_amp_hash(codec, key);
1637 if (!info) {
1638 mutex_unlock(&codec->hash_mutex);
1639 return -EINVAL;
1640 }
1641 info->amp_caps = val;
1642 info->head.val |= INFO_AMP_CAPS;
1643 mutex_unlock(&codec->hash_mutex);
1644 return 0;
1645}
1646
1647/* query the value from the caps hash; if not found, fetch the current
1648 * value from the given function and store in the hash
1649 */
1650static unsigned int
1651query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key,
1652 unsigned int (*func)(struct hda_codec *, hda_nid_t, int))
1653{
1654 struct hda_amp_info *info;
1655 unsigned int val;
1656
1657 mutex_lock(&codec->hash_mutex);
1658 info = get_alloc_amp_hash(codec, key);
1659 if (!info) {
1660 mutex_unlock(&codec->hash_mutex);
1661 return 0;
1662 }
1663 if (!(info->head.val & INFO_AMP_CAPS)) {
1664 mutex_unlock(&codec->hash_mutex); /* for reentrance */
1665 val = func(codec, nid, dir);
1666 write_caps_hash(codec, key, val);
1667 } else {
1668 val = info->amp_caps;
1669 mutex_unlock(&codec->hash_mutex);
1670 }
1671 return val;
1672}
1673
1674static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid,
1675 int direction)
1676{
1677 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1678 nid = codec->afg;
1679 return snd_hda_param_read(codec, nid,
1680 direction == HDA_OUTPUT ?
1681 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
1682}
1683
Takashi Iwaid5191e52009-11-16 14:58:17 +01001684/**
1685 * query_amp_caps - query AMP capabilities
1686 * @codec: the HD-auio codec
1687 * @nid: the NID to query
1688 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1689 *
1690 * Query AMP capabilities for the given widget and direction.
1691 * Returns the obtained capability bits.
1692 *
1693 * When cap bits have been already read, this doesn't read again but
1694 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001696u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001698 return query_caps_hash(codec, nid, direction,
1699 HDA_HASH_KEY(nid, direction, 0),
1700 read_amp_cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001702EXPORT_SYMBOL_HDA(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
Takashi Iwaid5191e52009-11-16 14:58:17 +01001704/**
1705 * snd_hda_override_amp_caps - Override the AMP capabilities
1706 * @codec: the CODEC to clean up
1707 * @nid: the NID to clean up
1708 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1709 * @caps: the capability bits to set
1710 *
1711 * Override the cached AMP caps bits value by the given one.
1712 * This function is useful if the driver needs to adjust the AMP ranges,
1713 * e.g. limit to 0dB, etc.
1714 *
1715 * Returns zero if successful or a negative error code.
1716 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001717int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1718 unsigned int caps)
1719{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001720 return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001721}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001722EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001723
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001724static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid,
1725 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001726{
1727 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1728}
1729
Takashi Iwaid5191e52009-11-16 14:58:17 +01001730/**
1731 * snd_hda_query_pin_caps - Query PIN capabilities
1732 * @codec: the HD-auio codec
1733 * @nid: the NID to query
1734 *
1735 * Query PIN capabilities for the given widget.
1736 * Returns the obtained capability bits.
1737 *
1738 * When cap bits have been already read, this doesn't read again but
1739 * returns the cached value.
1740 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001741u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1742{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001743 return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001744 read_pin_cap);
1745}
Takashi Iwai1327a322009-03-23 13:07:47 +01001746EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1747
Wu Fengguang864f92b2009-11-18 12:38:02 +08001748/**
Takashi Iwaif57c2562011-08-15 12:49:07 +02001749 * snd_hda_override_pin_caps - Override the pin capabilities
1750 * @codec: the CODEC
1751 * @nid: the NID to override
1752 * @caps: the capability bits to set
1753 *
1754 * Override the cached PIN capabilitiy bits value by the given one.
1755 *
1756 * Returns zero if successful or a negative error code.
1757 */
1758int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
1759 unsigned int caps)
1760{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001761 return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps);
Takashi Iwaif57c2562011-08-15 12:49:07 +02001762}
1763EXPORT_SYMBOL_HDA(snd_hda_override_pin_caps);
1764
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001765/* read or sync the hash value with the current value;
1766 * call within hash_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 */
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001768static struct hda_amp_info *
1769update_amp_hash(struct hda_codec *codec, hda_nid_t nid, int ch,
Takashi Iwai280e57d2012-12-14 10:32:21 +01001770 int direction, int index, bool init_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001772 struct hda_amp_info *info;
1773 unsigned int parm, val = 0;
1774 bool val_read = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001776 retry:
1777 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1778 if (!info)
1779 return NULL;
1780 if (!(info->head.val & INFO_AMP_VOL(ch))) {
1781 if (!val_read) {
1782 mutex_unlock(&codec->hash_mutex);
1783 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1784 parm |= direction == HDA_OUTPUT ?
1785 AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1786 parm |= index;
1787 val = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001788 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001789 val &= 0xff;
1790 val_read = true;
1791 mutex_lock(&codec->hash_mutex);
1792 goto retry;
1793 }
1794 info->vol[ch] = val;
1795 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001796 } else if (init_only)
1797 return NULL;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001798 return info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799}
1800
1801/*
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001802 * write the current volume in info to the h/w
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001804static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001805 hda_nid_t nid, int ch, int direction, int index,
1806 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807{
1808 u32 parm;
1809
1810 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1811 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1812 parm |= index << AC_AMP_SET_INDEX_SHIFT;
Takashi Iwai38681372012-02-27 15:00:58 +01001813 if ((val & HDA_AMP_MUTE) && !(info->amp_caps & AC_AMPCAP_MUTE) &&
1814 (info->amp_caps & AC_AMPCAP_MIN_MUTE))
1815 ; /* set the zero value as a fake mute */
1816 else
1817 parm |= val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
1819}
1820
Takashi Iwaid5191e52009-11-16 14:58:17 +01001821/**
1822 * snd_hda_codec_amp_read - Read AMP value
1823 * @codec: HD-audio codec
1824 * @nid: NID to read the AMP value
1825 * @ch: channel (left=0 or right=1)
1826 * @direction: #HDA_INPUT or #HDA_OUTPUT
1827 * @index: the index value (only for input direction)
1828 *
1829 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 */
Takashi Iwai834be882006-03-01 14:16:17 +01001831int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1832 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001834 struct hda_amp_info *info;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001835 unsigned int val = 0;
1836
1837 mutex_lock(&codec->hash_mutex);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001838 info = update_amp_hash(codec, nid, ch, direction, index, false);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001839 if (info)
1840 val = info->vol[ch];
1841 mutex_unlock(&codec->hash_mutex);
1842 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001844EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845
Takashi Iwai280e57d2012-12-14 10:32:21 +01001846static int codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1847 int direction, int idx, int mask, int val,
1848 bool init_only)
1849{
1850 struct hda_amp_info *info;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01001851 unsigned int cache_only;
Takashi Iwai280e57d2012-12-14 10:32:21 +01001852
1853 if (snd_BUG_ON(mask & ~0xff))
1854 mask &= 0xff;
1855 val &= mask;
1856
1857 mutex_lock(&codec->hash_mutex);
1858 info = update_amp_hash(codec, nid, ch, direction, idx, init_only);
1859 if (!info) {
1860 mutex_unlock(&codec->hash_mutex);
1861 return 0;
1862 }
1863 val |= info->vol[ch] & ~mask;
1864 if (info->vol[ch] == val) {
1865 mutex_unlock(&codec->hash_mutex);
1866 return 0;
1867 }
1868 info->vol[ch] = val;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01001869 cache_only = info->head.dirty = codec->cached_write;
Takashi Iwai280e57d2012-12-14 10:32:21 +01001870 mutex_unlock(&codec->hash_mutex);
Takashi Iwaide1e37b2012-12-20 11:00:21 +01001871 if (!cache_only)
Takashi Iwai280e57d2012-12-14 10:32:21 +01001872 put_vol_mute(codec, info, nid, ch, direction, idx, val);
1873 return 1;
1874}
1875
Takashi Iwaid5191e52009-11-16 14:58:17 +01001876/**
1877 * snd_hda_codec_amp_update - update the AMP value
1878 * @codec: HD-audio codec
1879 * @nid: NID to read the AMP value
1880 * @ch: channel (left=0 or right=1)
1881 * @direction: #HDA_INPUT or #HDA_OUTPUT
1882 * @idx: the index value (only for input direction)
1883 * @mask: bit mask to set
1884 * @val: the bits value to set
1885 *
1886 * Update the AMP value with a bit mask.
1887 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001888 */
Takashi Iwai834be882006-03-01 14:16:17 +01001889int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1890 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891{
Takashi Iwai280e57d2012-12-14 10:32:21 +01001892 return codec_amp_update(codec, nid, ch, direction, idx, mask, val, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001894EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895
Takashi Iwaid5191e52009-11-16 14:58:17 +01001896/**
1897 * snd_hda_codec_amp_stereo - update the AMP stereo values
1898 * @codec: HD-audio codec
1899 * @nid: NID to read the AMP value
1900 * @direction: #HDA_INPUT or #HDA_OUTPUT
1901 * @idx: the index value (only for input direction)
1902 * @mask: bit mask to set
1903 * @val: the bits value to set
1904 *
1905 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1906 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02001907 */
1908int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1909 int direction, int idx, int mask, int val)
1910{
1911 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001912
1913 if (snd_BUG_ON(mask & ~0xff))
1914 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02001915 for (ch = 0; ch < 2; ch++)
1916 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1917 idx, mask, val);
1918 return ret;
1919}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001920EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001921
Takashi Iwai280e57d2012-12-14 10:32:21 +01001922/* Works like snd_hda_codec_amp_update() but it writes the value only at
1923 * the first access. If the amp was already initialized / updated beforehand,
1924 * this does nothing.
1925 */
1926int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
1927 int dir, int idx, int mask, int val)
1928{
1929 return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true);
1930}
1931EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init);
1932
1933int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
1934 int dir, int idx, int mask, int val)
1935{
1936 int ch, ret = 0;
1937
1938 if (snd_BUG_ON(mask & ~0xff))
1939 mask &= 0xff;
1940 for (ch = 0; ch < 2; ch++)
1941 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
1942 idx, mask, val);
1943 return ret;
1944}
1945EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init_stereo);
1946
Takashi Iwaid5191e52009-11-16 14:58:17 +01001947/**
1948 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
1949 * @codec: HD-audio codec
1950 *
1951 * Resume the all amp commands from the cache.
1952 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001953void snd_hda_codec_resume_amp(struct hda_codec *codec)
1954{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001955 int i;
1956
Takashi Iwaic370dd62012-12-13 18:30:04 +01001957 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01001958 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01001959 for (i = 0; i < codec->amp_cache.buf.used; i++) {
1960 struct hda_amp_info *buffer;
1961 u32 key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001962 hda_nid_t nid;
1963 unsigned int idx, dir, ch;
Takashi Iwaic370dd62012-12-13 18:30:04 +01001964
1965 buffer = snd_array_elem(&codec->amp_cache.buf, i);
1966 key = buffer->head.key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001967 if (!key)
1968 continue;
1969 nid = key & 0xff;
1970 idx = (key >> 16) & 0xff;
1971 dir = (key >> 24) & 0xff;
1972 for (ch = 0; ch < 2; ch++) {
1973 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
1974 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01001975 if (!buffer->head.dirty)
1976 continue;
1977 buffer->head.dirty = 0;
1978 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001979 put_vol_mute(codec, buffer, nid, ch, dir, idx,
1980 buffer->vol[ch]);
Takashi Iwaic370dd62012-12-13 18:30:04 +01001981 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001982 }
1983 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01001984 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001985}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001986EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001988static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1989 unsigned int ofs)
1990{
1991 u32 caps = query_amp_caps(codec, nid, dir);
1992 /* get num steps */
1993 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1994 if (ofs < caps)
1995 caps -= ofs;
1996 return caps;
1997}
1998
Takashi Iwaid5191e52009-11-16 14:58:17 +01001999/**
2000 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
2001 *
2002 * The control element is supposed to have the private_value field
2003 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2004 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002005int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
2006 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007{
2008 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2009 u16 nid = get_amp_nid(kcontrol);
2010 u8 chs = get_amp_channels(kcontrol);
2011 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002012 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002014 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2015 uinfo->count = chs == 3 ? 2 : 1;
2016 uinfo->value.integer.min = 0;
2017 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
2018 if (!uinfo->value.integer.max) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002019 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01002020 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
2021 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 return -EINVAL;
2023 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 return 0;
2025}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002026EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002028
2029static inline unsigned int
2030read_amp_value(struct hda_codec *codec, hda_nid_t nid,
2031 int ch, int dir, int idx, unsigned int ofs)
2032{
2033 unsigned int val;
2034 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
2035 val &= HDA_AMP_VOLMASK;
2036 if (val >= ofs)
2037 val -= ofs;
2038 else
2039 val = 0;
2040 return val;
2041}
2042
2043static inline int
2044update_amp_value(struct hda_codec *codec, hda_nid_t nid,
2045 int ch, int dir, int idx, unsigned int ofs,
2046 unsigned int val)
2047{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002048 unsigned int maxval;
2049
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002050 if (val > 0)
2051 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02002052 /* ofs = 0: raw max value */
2053 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002054 if (val > maxval)
2055 val = maxval;
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002056 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
2057 HDA_AMP_VOLMASK, val);
2058}
2059
Takashi Iwaid5191e52009-11-16 14:58:17 +01002060/**
2061 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
2062 *
2063 * The control element is supposed to have the private_value field
2064 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2065 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002066int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
2067 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068{
2069 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2070 hda_nid_t nid = get_amp_nid(kcontrol);
2071 int chs = get_amp_channels(kcontrol);
2072 int dir = get_amp_direction(kcontrol);
2073 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002074 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 long *valp = ucontrol->value.integer.value;
2076
2077 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002078 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002080 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 return 0;
2082}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002083EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
Takashi Iwaid5191e52009-11-16 14:58:17 +01002085/**
2086 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
2087 *
2088 * The control element is supposed to have the private_value field
2089 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2090 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002091int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
2092 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093{
2094 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2095 hda_nid_t nid = get_amp_nid(kcontrol);
2096 int chs = get_amp_channels(kcontrol);
2097 int dir = get_amp_direction(kcontrol);
2098 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002099 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 long *valp = ucontrol->value.integer.value;
2101 int change = 0;
2102
Takashi Iwaicb53c622007-08-10 17:21:45 +02002103 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002104 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002105 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002106 valp++;
2107 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002108 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002109 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002110 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 return change;
2112}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002113EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114
Takashi Iwaid5191e52009-11-16 14:58:17 +01002115/**
2116 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2117 *
2118 * The control element is supposed to have the private_value field
2119 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2120 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002121int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2122 unsigned int size, unsigned int __user *_tlv)
2123{
2124 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2125 hda_nid_t nid = get_amp_nid(kcontrol);
2126 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002127 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02002128 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002129 u32 caps, val1, val2;
2130
2131 if (size < 4 * sizeof(unsigned int))
2132 return -ENOMEM;
2133 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002134 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2135 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002136 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002137 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002138 val1 = ((int)val1) * ((int)val2);
Takashi Iwai38681372012-02-27 15:00:58 +01002139 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwaic08d9162010-10-17 10:40:53 +02002140 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002141 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2142 return -EFAULT;
2143 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2144 return -EFAULT;
2145 if (put_user(val1, _tlv + 2))
2146 return -EFAULT;
2147 if (put_user(val2, _tlv + 3))
2148 return -EFAULT;
2149 return 0;
2150}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002151EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002152
Takashi Iwaid5191e52009-11-16 14:58:17 +01002153/**
2154 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2155 * @codec: HD-audio codec
2156 * @nid: NID of a reference widget
2157 * @dir: #HDA_INPUT or #HDA_OUTPUT
2158 * @tlv: TLV data to be stored, at least 4 elements
2159 *
2160 * Set (static) TLV data for a virtual master volume using the AMP caps
2161 * obtained from the reference NID.
2162 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01002163 */
2164void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2165 unsigned int *tlv)
2166{
2167 u32 caps;
2168 int nums, step;
2169
2170 caps = query_amp_caps(codec, nid, dir);
2171 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2172 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2173 step = (step + 1) * 25;
2174 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2175 tlv[1] = 2 * sizeof(unsigned int);
2176 tlv[2] = -nums * step;
2177 tlv[3] = step;
2178}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002179EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002180
2181/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01002182static struct snd_kcontrol *
Takashi Iwaidcda5802012-10-12 17:24:51 +02002183find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002184{
2185 struct snd_ctl_elem_id id;
2186 memset(&id, 0, sizeof(id));
2187 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwaidcda5802012-10-12 17:24:51 +02002188 id.device = dev;
Takashi Iwai09f99702008-02-04 12:31:13 +01002189 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02002190 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2191 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002192 strcpy(id.name, name);
2193 return snd_ctl_find_id(codec->bus->card, &id);
2194}
2195
Takashi Iwaid5191e52009-11-16 14:58:17 +01002196/**
2197 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2198 * @codec: HD-audio codec
2199 * @name: ctl id name string
2200 *
2201 * Get the control element with the given id string and IFACE_MIXER.
2202 */
Takashi Iwai09f99702008-02-04 12:31:13 +01002203struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2204 const char *name)
2205{
Takashi Iwaidcda5802012-10-12 17:24:51 +02002206 return find_mixer_ctl(codec, name, 0, 0);
Takashi Iwai09f99702008-02-04 12:31:13 +01002207}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002208EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01002209
Takashi Iwaidcda5802012-10-12 17:24:51 +02002210static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
2211 int dev)
Takashi Iwai1afe2062010-12-23 10:17:52 +01002212{
2213 int idx;
2214 for (idx = 0; idx < 16; idx++) { /* 16 ctlrs should be large enough */
Takashi Iwaidcda5802012-10-12 17:24:51 +02002215 if (!find_mixer_ctl(codec, name, dev, idx))
Takashi Iwai1afe2062010-12-23 10:17:52 +01002216 return idx;
2217 }
2218 return -EBUSY;
2219}
2220
Takashi Iwaid5191e52009-11-16 14:58:17 +01002221/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002222 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01002223 * @codec: HD-audio codec
2224 * @nid: corresponding NID (optional)
2225 * @kctl: the control element to assign
2226 *
2227 * Add the given control element to an array inside the codec instance.
2228 * All control elements belonging to a codec are supposed to be added
2229 * by this function so that a proper clean-up works at the free or
2230 * reconfiguration time.
2231 *
2232 * If non-zero @nid is passed, the NID is assigned to the control element.
2233 * The assignment is shown in the codec proc file.
2234 *
2235 * snd_hda_ctl_add() checks the control subdev id field whether
2236 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002237 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2238 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01002239 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002240int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2241 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002242{
2243 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002244 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002245 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002246
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002247 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002248 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002249 if (nid == 0)
2250 nid = get_amp_nid_(kctl->private_value);
2251 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002252 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2253 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002254 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01002255 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002256 err = snd_ctl_add(codec->bus->card, kctl);
2257 if (err < 0)
2258 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002259 item = snd_array_new(&codec->mixers);
2260 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002261 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002262 item->kctl = kctl;
2263 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002264 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002265 return 0;
2266}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002267EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002268
Takashi Iwaid5191e52009-11-16 14:58:17 +01002269/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002270 * snd_hda_add_nid - Assign a NID to a control element
2271 * @codec: HD-audio codec
2272 * @nid: corresponding NID (optional)
2273 * @kctl: the control element to assign
2274 * @index: index to kctl
2275 *
2276 * Add the given control element to an array inside the codec instance.
2277 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2278 * NID:KCTL mapping - for example "Capture Source" selector.
2279 */
2280int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2281 unsigned int index, hda_nid_t nid)
2282{
2283 struct hda_nid_item *item;
2284
2285 if (nid > 0) {
2286 item = snd_array_new(&codec->nids);
2287 if (!item)
2288 return -ENOMEM;
2289 item->kctl = kctl;
2290 item->index = index;
2291 item->nid = nid;
2292 return 0;
2293 }
Takashi Iwai28d1a852010-03-15 09:05:46 +01002294 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
2295 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002296 return -EINVAL;
2297}
2298EXPORT_SYMBOL_HDA(snd_hda_add_nid);
2299
2300/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002301 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2302 * @codec: HD-audio codec
2303 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002304void snd_hda_ctls_clear(struct hda_codec *codec)
2305{
2306 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002307 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002308 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002309 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002310 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002311 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002312}
2313
Takashi Iwaia65d6292009-02-23 16:57:04 +01002314/* pseudo device locking
2315 * toggle card->shutdown to allow/disallow the device access (as a hack)
2316 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002317int snd_hda_lock_devices(struct hda_bus *bus)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002318{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002319 struct snd_card *card = bus->card;
2320 struct hda_codec *codec;
2321
Takashi Iwaia65d6292009-02-23 16:57:04 +01002322 spin_lock(&card->files_lock);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002323 if (card->shutdown)
2324 goto err_unlock;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002325 card->shutdown = 1;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002326 if (!list_empty(&card->ctl_files))
2327 goto err_clear;
2328
2329 list_for_each_entry(codec, &bus->codec_list, list) {
2330 int pcm;
2331 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2332 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2333 if (!cpcm->pcm)
2334 continue;
2335 if (cpcm->pcm->streams[0].substream_opened ||
2336 cpcm->pcm->streams[1].substream_opened)
2337 goto err_clear;
2338 }
2339 }
Takashi Iwaia65d6292009-02-23 16:57:04 +01002340 spin_unlock(&card->files_lock);
2341 return 0;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002342
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002343 err_clear:
2344 card->shutdown = 0;
2345 err_unlock:
2346 spin_unlock(&card->files_lock);
2347 return -EINVAL;
2348}
2349EXPORT_SYMBOL_HDA(snd_hda_lock_devices);
2350
2351void snd_hda_unlock_devices(struct hda_bus *bus)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002352{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002353 struct snd_card *card = bus->card;
2354
2355 card = bus->card;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002356 spin_lock(&card->files_lock);
2357 card->shutdown = 0;
2358 spin_unlock(&card->files_lock);
2359}
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002360EXPORT_SYMBOL_HDA(snd_hda_unlock_devices);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002361
Takashi Iwaid5191e52009-11-16 14:58:17 +01002362/**
2363 * snd_hda_codec_reset - Clear all objects assigned to the codec
2364 * @codec: HD-audio codec
2365 *
2366 * This frees the all PCM and control elements assigned to the codec, and
2367 * clears the caches and restores the pin default configurations.
2368 *
2369 * When a device is being used, it returns -EBSY. If successfully freed,
2370 * returns zero.
2371 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002372int snd_hda_codec_reset(struct hda_codec *codec)
2373{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002374 struct hda_bus *bus = codec->bus;
2375 struct snd_card *card = bus->card;
2376 int i;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002377
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002378 if (snd_hda_lock_devices(bus) < 0)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002379 return -EBUSY;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002380
2381 /* OK, let it free */
David Henningsson26a6cb62012-10-09 15:04:21 +02002382 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai83012a72012-08-24 18:38:08 +02002383#ifdef CONFIG_PM
Takashi Iwaia2d96e72012-05-09 12:36:22 +02002384 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwai339876d2012-05-08 16:57:12 +02002385 codec->power_on = 0;
2386 codec->power_transition = 0;
2387 codec->power_jiffies = jiffies;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002388 flush_workqueue(bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002389#endif
2390 snd_hda_ctls_clear(codec);
2391 /* relase PCMs */
2392 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002393 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01002394 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002395 clear_bit(codec->pcm_info[i].device,
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002396 bus->pcm_dev_bits);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002397 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002398 }
2399 if (codec->patch_ops.free)
2400 codec->patch_ops.free(codec);
Takashi Iwai07dc59f2012-09-10 09:39:31 +02002401 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
Takashi Iwai1835a0f2011-10-27 22:12:46 +02002402 snd_hda_jack_tbl_clear(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01002403 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002404 codec->spec = NULL;
2405 free_hda_cache(&codec->amp_cache);
2406 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01002407 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2408 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01002409 /* free only driver_pins so that init_pins + user_pins are restored */
2410 snd_array_free(&codec->driver_pins);
Takashi Iwai09a60712012-06-26 15:01:33 +02002411 snd_array_free(&codec->cvt_setups);
2412 snd_array_free(&codec->spdif_out);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01002413 snd_array_free(&codec->verbs);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002414 codec->num_pcms = 0;
2415 codec->pcm_info = NULL;
2416 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01002417 codec->slave_dig_outs = NULL;
2418 codec->spdif_status_reset = 0;
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002419 module_put(codec->owner);
2420 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002421
2422 /* allow device access again */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002423 snd_hda_unlock_devices(bus);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002424 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002425}
2426
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002427typedef int (*map_slave_func_t)(void *, struct snd_kcontrol *);
2428
2429/* apply the function to all matching slave ctls in the mixer list */
2430static int map_slaves(struct hda_codec *codec, const char * const *slaves,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002431 const char *suffix, map_slave_func_t func, void *data)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002432{
2433 struct hda_nid_item *items;
2434 const char * const *s;
2435 int i, err;
2436
2437 items = codec->mixers.list;
2438 for (i = 0; i < codec->mixers.used; i++) {
2439 struct snd_kcontrol *sctl = items[i].kctl;
2440 if (!sctl || !sctl->id.name ||
2441 sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
2442 continue;
2443 for (s = slaves; *s; s++) {
Takashi Iwai9322ca52012-02-03 14:28:01 +01002444 char tmpname[sizeof(sctl->id.name)];
2445 const char *name = *s;
2446 if (suffix) {
2447 snprintf(tmpname, sizeof(tmpname), "%s %s",
2448 name, suffix);
2449 name = tmpname;
2450 }
Takashi Iwai9322ca52012-02-03 14:28:01 +01002451 if (!strcmp(sctl->id.name, name)) {
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002452 err = func(data, sctl);
2453 if (err)
2454 return err;
2455 break;
2456 }
2457 }
2458 }
2459 return 0;
2460}
2461
2462static int check_slave_present(void *data, struct snd_kcontrol *sctl)
2463{
2464 return 1;
2465}
2466
Takashi Iwai18478e82012-03-09 17:51:10 +01002467/* guess the value corresponding to 0dB */
2468static int get_kctl_0dB_offset(struct snd_kcontrol *kctl)
2469{
2470 int _tlv[4];
2471 const int *tlv = NULL;
2472 int val = -1;
2473
2474 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2475 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2476 mm_segment_t fs = get_fs();
2477 set_fs(get_ds());
2478 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2479 tlv = _tlv;
2480 set_fs(fs);
2481 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2482 tlv = kctl->tlv.p;
2483 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE)
2484 val = -tlv[2] / tlv[3];
2485 return val;
2486}
2487
2488/* call kctl->put with the given value(s) */
2489static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2490{
2491 struct snd_ctl_elem_value *ucontrol;
2492 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2493 if (!ucontrol)
2494 return -ENOMEM;
2495 ucontrol->value.integer.value[0] = val;
2496 ucontrol->value.integer.value[1] = val;
2497 kctl->put(kctl, ucontrol);
2498 kfree(ucontrol);
2499 return 0;
2500}
2501
2502/* initialize the slave volume with 0dB */
2503static int init_slave_0dB(void *data, struct snd_kcontrol *slave)
2504{
2505 int offset = get_kctl_0dB_offset(slave);
2506 if (offset > 0)
2507 put_kctl_with_value(slave, offset);
2508 return 0;
2509}
2510
2511/* unmute the slave */
2512static int init_slave_unmute(void *data, struct snd_kcontrol *slave)
2513{
2514 return put_kctl_with_value(slave, 1);
2515}
2516
Takashi Iwaid5191e52009-11-16 14:58:17 +01002517/**
2518 * snd_hda_add_vmaster - create a virtual master control and add slaves
2519 * @codec: HD-audio codec
2520 * @name: vmaster control name
2521 * @tlv: TLV data (optional)
2522 * @slaves: slave control names (optional)
Takashi Iwai9322ca52012-02-03 14:28:01 +01002523 * @suffix: suffix string to each slave name (optional)
Takashi Iwai18478e82012-03-09 17:51:10 +01002524 * @init_slave_vol: initialize slaves to unmute/0dB
Takashi Iwai29e58532012-03-12 12:25:03 +01002525 * @ctl_ret: store the vmaster kcontrol in return
Takashi Iwaid5191e52009-11-16 14:58:17 +01002526 *
2527 * Create a virtual master control with the given name. The TLV data
2528 * must be either NULL or a valid data.
2529 *
2530 * @slaves is a NULL-terminated array of strings, each of which is a
2531 * slave control name. All controls with these names are assigned to
2532 * the new virtual master control.
2533 *
2534 * This function returns zero if successful or a negative error code.
2535 */
Takashi Iwai18478e82012-03-09 17:51:10 +01002536int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002537 unsigned int *tlv, const char * const *slaves,
Takashi Iwai29e58532012-03-12 12:25:03 +01002538 const char *suffix, bool init_slave_vol,
2539 struct snd_kcontrol **ctl_ret)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002540{
2541 struct snd_kcontrol *kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002542 int err;
2543
Takashi Iwai29e58532012-03-12 12:25:03 +01002544 if (ctl_ret)
2545 *ctl_ret = NULL;
2546
Takashi Iwai9322ca52012-02-03 14:28:01 +01002547 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002548 if (err != 1) {
Takashi Iwai2f085542008-02-22 18:43:50 +01002549 snd_printdd("No slave found for %s\n", name);
2550 return 0;
2551 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002552 kctl = snd_ctl_make_virtual_master(name, tlv);
2553 if (!kctl)
2554 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002555 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002556 if (err < 0)
2557 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002558
Takashi Iwai9322ca52012-02-03 14:28:01 +01002559 err = map_slaves(codec, slaves, suffix,
2560 (map_slave_func_t)snd_ctl_add_slave, kctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002561 if (err < 0)
2562 return err;
Takashi Iwai18478e82012-03-09 17:51:10 +01002563
2564 /* init with master mute & zero volume */
2565 put_kctl_with_value(kctl, 0);
2566 if (init_slave_vol)
2567 map_slaves(codec, slaves, suffix,
2568 tlv ? init_slave_0dB : init_slave_unmute, kctl);
2569
Takashi Iwai29e58532012-03-12 12:25:03 +01002570 if (ctl_ret)
2571 *ctl_ret = kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002572 return 0;
2573}
Takashi Iwai18478e82012-03-09 17:51:10 +01002574EXPORT_SYMBOL_HDA(__snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002575
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002576/*
2577 * mute-LED control using vmaster
2578 */
2579static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2580 struct snd_ctl_elem_info *uinfo)
2581{
2582 static const char * const texts[] = {
David Henningssonc86c2d42013-01-03 14:12:29 +01002583 "On", "Off", "Follow Master"
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002584 };
2585 unsigned int index;
2586
2587 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2588 uinfo->count = 1;
2589 uinfo->value.enumerated.items = 3;
2590 index = uinfo->value.enumerated.item;
2591 if (index >= 3)
2592 index = 2;
2593 strcpy(uinfo->value.enumerated.name, texts[index]);
2594 return 0;
2595}
2596
2597static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2598 struct snd_ctl_elem_value *ucontrol)
2599{
2600 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2601 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2602 return 0;
2603}
2604
2605static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2606 struct snd_ctl_elem_value *ucontrol)
2607{
2608 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2609 unsigned int old_mode = hook->mute_mode;
2610
2611 hook->mute_mode = ucontrol->value.enumerated.item[0];
2612 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2613 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2614 if (old_mode == hook->mute_mode)
2615 return 0;
2616 snd_hda_sync_vmaster_hook(hook);
2617 return 1;
2618}
2619
2620static struct snd_kcontrol_new vmaster_mute_mode = {
2621 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2622 .name = "Mute-LED Mode",
2623 .info = vmaster_mute_mode_info,
2624 .get = vmaster_mute_mode_get,
2625 .put = vmaster_mute_mode_put,
2626};
2627
2628/*
2629 * Add a mute-LED hook with the given vmaster switch kctl
2630 * "Mute-LED Mode" control is automatically created and associated with
2631 * the given hook.
2632 */
2633int snd_hda_add_vmaster_hook(struct hda_codec *codec,
Takashi Iwaif29735c2012-03-13 07:55:10 +01002634 struct hda_vmaster_mute_hook *hook,
2635 bool expose_enum_ctl)
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002636{
2637 struct snd_kcontrol *kctl;
2638
2639 if (!hook->hook || !hook->sw_kctl)
2640 return 0;
2641 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
2642 hook->codec = codec;
2643 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
Takashi Iwaif29735c2012-03-13 07:55:10 +01002644 if (!expose_enum_ctl)
2645 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002646 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2647 if (!kctl)
2648 return -ENOMEM;
2649 return snd_hda_ctl_add(codec, 0, kctl);
2650}
2651EXPORT_SYMBOL_HDA(snd_hda_add_vmaster_hook);
2652
2653/*
2654 * Call the hook with the current value for synchronization
2655 * Should be called in init callback
2656 */
2657void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2658{
2659 if (!hook->hook || !hook->codec)
2660 return;
2661 switch (hook->mute_mode) {
2662 case HDA_VMUTE_FOLLOW_MASTER:
2663 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2664 break;
2665 default:
2666 hook->hook(hook->codec, hook->mute_mode);
2667 break;
2668 }
2669}
2670EXPORT_SYMBOL_HDA(snd_hda_sync_vmaster_hook);
2671
2672
Takashi Iwaid5191e52009-11-16 14:58:17 +01002673/**
2674 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2675 *
2676 * The control element is supposed to have the private_value field
2677 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2678 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002679int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2680 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681{
2682 int chs = get_amp_channels(kcontrol);
2683
2684 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2685 uinfo->count = chs == 3 ? 2 : 1;
2686 uinfo->value.integer.min = 0;
2687 uinfo->value.integer.max = 1;
2688 return 0;
2689}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002690EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691
Takashi Iwaid5191e52009-11-16 14:58:17 +01002692/**
2693 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2694 *
2695 * The control element is supposed to have the private_value field
2696 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2697 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002698int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2699 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700{
2701 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2702 hda_nid_t nid = get_amp_nid(kcontrol);
2703 int chs = get_amp_channels(kcontrol);
2704 int dir = get_amp_direction(kcontrol);
2705 int idx = get_amp_index(kcontrol);
2706 long *valp = ucontrol->value.integer.value;
2707
2708 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002709 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002710 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002712 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002713 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 return 0;
2715}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002716EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717
Takashi Iwaid5191e52009-11-16 14:58:17 +01002718/**
2719 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2720 *
2721 * The control element is supposed to have the private_value field
2722 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2723 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002724int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2725 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726{
2727 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2728 hda_nid_t nid = get_amp_nid(kcontrol);
2729 int chs = get_amp_channels(kcontrol);
2730 int dir = get_amp_direction(kcontrol);
2731 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 long *valp = ucontrol->value.integer.value;
2733 int change = 0;
2734
Takashi Iwaicb53c622007-08-10 17:21:45 +02002735 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002736 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002737 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002738 HDA_AMP_MUTE,
2739 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002740 valp++;
2741 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002742 if (chs & 2)
2743 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002744 HDA_AMP_MUTE,
2745 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002746 hda_call_check_power_status(codec, nid);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002747 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 return change;
2749}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002750EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751
2752/*
Takashi Iwai985be542005-11-02 18:26:49 +01002753 * bound volume controls
2754 *
2755 * bind multiple volumes (# indices, from 0)
2756 */
2757
2758#define AMP_VAL_IDX_SHIFT 19
2759#define AMP_VAL_IDX_MASK (0x0f<<19)
2760
Takashi Iwaid5191e52009-11-16 14:58:17 +01002761/**
2762 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2763 *
2764 * The control element is supposed to have the private_value field
2765 * set up via HDA_BIND_MUTE*() macros.
2766 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002767int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2768 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002769{
2770 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2771 unsigned long pval;
2772 int err;
2773
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002774 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002775 pval = kcontrol->private_value;
2776 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2777 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2778 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002779 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002780 return err;
2781}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002782EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002783
Takashi Iwaid5191e52009-11-16 14:58:17 +01002784/**
2785 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2786 *
2787 * The control element is supposed to have the private_value field
2788 * set up via HDA_BIND_MUTE*() macros.
2789 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002790int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2791 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002792{
2793 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2794 unsigned long pval;
2795 int i, indices, err = 0, change = 0;
2796
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002797 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002798 pval = kcontrol->private_value;
2799 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2800 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002801 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2802 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002803 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2804 if (err < 0)
2805 break;
2806 change |= err;
2807 }
2808 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002809 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002810 return err < 0 ? err : change;
2811}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002812EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002813
Takashi Iwaid5191e52009-11-16 14:58:17 +01002814/**
2815 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2816 *
2817 * The control element is supposed to have the private_value field
2818 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002819 */
2820int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2821 struct snd_ctl_elem_info *uinfo)
2822{
2823 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2824 struct hda_bind_ctls *c;
2825 int err;
2826
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002827 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002828 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002829 kcontrol->private_value = *c->values;
2830 err = c->ops->info(kcontrol, uinfo);
2831 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002832 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002833 return err;
2834}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002835EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002836
Takashi Iwaid5191e52009-11-16 14:58:17 +01002837/**
2838 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2839 *
2840 * The control element is supposed to have the private_value field
2841 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2842 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002843int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2844 struct snd_ctl_elem_value *ucontrol)
2845{
2846 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2847 struct hda_bind_ctls *c;
2848 int err;
2849
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002850 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002851 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002852 kcontrol->private_value = *c->values;
2853 err = c->ops->get(kcontrol, ucontrol);
2854 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002855 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002856 return err;
2857}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002858EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002859
Takashi Iwaid5191e52009-11-16 14:58:17 +01002860/**
2861 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2862 *
2863 * The control element is supposed to have the private_value field
2864 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2865 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002866int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2867 struct snd_ctl_elem_value *ucontrol)
2868{
2869 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2870 struct hda_bind_ctls *c;
2871 unsigned long *vals;
2872 int err = 0, change = 0;
2873
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002874 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002875 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002876 for (vals = c->values; *vals; vals++) {
2877 kcontrol->private_value = *vals;
2878 err = c->ops->put(kcontrol, ucontrol);
2879 if (err < 0)
2880 break;
2881 change |= err;
2882 }
2883 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002884 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002885 return err < 0 ? err : change;
2886}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002887EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02002888
Takashi Iwaid5191e52009-11-16 14:58:17 +01002889/**
2890 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2891 *
2892 * The control element is supposed to have the private_value field
2893 * set up via HDA_BIND_VOL() macro.
2894 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002895int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2896 unsigned int size, unsigned int __user *tlv)
2897{
2898 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2899 struct hda_bind_ctls *c;
2900 int err;
2901
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002902 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002903 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002904 kcontrol->private_value = *c->values;
2905 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2906 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002907 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002908 return err;
2909}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002910EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02002911
2912struct hda_ctl_ops snd_hda_bind_vol = {
2913 .info = snd_hda_mixer_amp_volume_info,
2914 .get = snd_hda_mixer_amp_volume_get,
2915 .put = snd_hda_mixer_amp_volume_put,
2916 .tlv = snd_hda_mixer_amp_tlv
2917};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002918EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02002919
2920struct hda_ctl_ops snd_hda_bind_sw = {
2921 .info = snd_hda_mixer_amp_switch_info,
2922 .get = snd_hda_mixer_amp_switch_get,
2923 .put = snd_hda_mixer_amp_switch_put,
2924 .tlv = snd_hda_mixer_amp_tlv
2925};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002926EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02002927
2928/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 * SPDIF out controls
2930 */
2931
Takashi Iwai0ba21762007-04-16 11:29:14 +02002932static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2933 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934{
2935 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2936 uinfo->count = 1;
2937 return 0;
2938}
2939
Takashi Iwai0ba21762007-04-16 11:29:14 +02002940static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2941 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942{
2943 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2944 IEC958_AES0_NONAUDIO |
2945 IEC958_AES0_CON_EMPHASIS_5015 |
2946 IEC958_AES0_CON_NOT_COPYRIGHT;
2947 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2948 IEC958_AES1_CON_ORIGINAL;
2949 return 0;
2950}
2951
Takashi Iwai0ba21762007-04-16 11:29:14 +02002952static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2953 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954{
2955 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2956 IEC958_AES0_NONAUDIO |
2957 IEC958_AES0_PRO_EMPHASIS_5015;
2958 return 0;
2959}
2960
Takashi Iwai0ba21762007-04-16 11:29:14 +02002961static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2962 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963{
2964 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06002965 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002966 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002968 mutex_lock(&codec->spdif_mutex);
2969 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06002970 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
2971 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
2972 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
2973 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002974 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975
2976 return 0;
2977}
2978
2979/* convert from SPDIF status bits to HDA SPDIF bits
2980 * bit 0 (DigEn) is always set zero (to be filled later)
2981 */
2982static unsigned short convert_from_spdif_status(unsigned int sbits)
2983{
2984 unsigned short val = 0;
2985
2986 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002987 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002989 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002991 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2992 IEC958_AES0_PRO_EMPHASIS_5015)
2993 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002995 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2996 IEC958_AES0_CON_EMPHASIS_5015)
2997 val |= AC_DIG1_EMPHASIS;
2998 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2999 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02003001 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
3003 }
3004 return val;
3005}
3006
3007/* convert to SPDIF status bits from HDA SPDIF bits
3008 */
3009static unsigned int convert_to_spdif_status(unsigned short val)
3010{
3011 unsigned int sbits = 0;
3012
Takashi Iwai0ba21762007-04-16 11:29:14 +02003013 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003015 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 sbits |= IEC958_AES0_PROFESSIONAL;
3017 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003018 if (sbits & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
3020 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003021 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003023 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003025 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
3027 sbits |= val & (0x7f << 8);
3028 }
3029 return sbits;
3030}
3031
Takashi Iwai2f728532008-09-25 16:32:41 +02003032/* set digital convert verbs both for the given NID and its slaves */
3033static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
3034 int verb, int val)
3035{
Takashi Iwaidda14412011-05-02 11:29:30 +02003036 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02003037
Takashi Iwai9e976972008-11-25 08:17:20 +01003038 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003039 d = codec->slave_dig_outs;
3040 if (!d)
3041 return;
3042 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01003043 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003044}
3045
3046static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
3047 int dig1, int dig2)
3048{
3049 if (dig1 != -1)
3050 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
3051 if (dig2 != -1)
3052 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
3053}
3054
Takashi Iwai0ba21762007-04-16 11:29:14 +02003055static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
3056 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057{
3058 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003059 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003060 struct hda_spdif_out *spdif;
3061 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 unsigned short val;
3063 int change;
3064
Ingo Molnar62932df2006-01-16 16:34:20 +01003065 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003066 spdif = snd_array_elem(&codec->spdif_out, idx);
3067 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06003068 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
3070 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
3071 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c935972011-06-01 11:14:17 -06003072 val = convert_from_spdif_status(spdif->status);
3073 val |= spdif->ctls & 1;
3074 change = spdif->ctls != val;
3075 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003076 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02003077 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01003078 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 return change;
3080}
3081
Takashi Iwaia5ce8892007-07-23 15:42:26 +02003082#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083
Takashi Iwai0ba21762007-04-16 11:29:14 +02003084static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
3085 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086{
3087 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003088 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003089 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003091 mutex_lock(&codec->spdif_mutex);
3092 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06003093 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003094 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 return 0;
3096}
3097
Stephen Warren74b654c2011-06-01 11:14:18 -06003098static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
3099 int dig1, int dig2)
3100{
3101 set_dig_out_convert(codec, nid, dig1, dig2);
3102 /* unmute amp switch (if any) */
3103 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
3104 (dig1 & AC_DIG1_ENABLE))
3105 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
3106 HDA_AMP_MUTE, 0);
3107}
3108
Takashi Iwai0ba21762007-04-16 11:29:14 +02003109static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
3110 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111{
3112 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003113 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003114 struct hda_spdif_out *spdif;
3115 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 unsigned short val;
3117 int change;
3118
Ingo Molnar62932df2006-01-16 16:34:20 +01003119 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003120 spdif = snd_array_elem(&codec->spdif_out, idx);
3121 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06003122 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02003124 val |= AC_DIG1_ENABLE;
Stephen Warren7c935972011-06-01 11:14:17 -06003125 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003126 spdif->ctls = val;
3127 if (change && nid != (u16)-1)
3128 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01003129 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 return change;
3131}
3132
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003133static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 {
3135 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3136 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003137 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 .info = snd_hda_spdif_mask_info,
3139 .get = snd_hda_spdif_cmask_get,
3140 },
3141 {
3142 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3143 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003144 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 .info = snd_hda_spdif_mask_info,
3146 .get = snd_hda_spdif_pmask_get,
3147 },
3148 {
3149 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003150 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 .info = snd_hda_spdif_mask_info,
3152 .get = snd_hda_spdif_default_get,
3153 .put = snd_hda_spdif_default_put,
3154 },
3155 {
3156 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003157 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 .info = snd_hda_spdif_out_switch_info,
3159 .get = snd_hda_spdif_out_switch_get,
3160 .put = snd_hda_spdif_out_switch_put,
3161 },
3162 { } /* end */
3163};
3164
3165/**
Takashi Iwaidcda5802012-10-12 17:24:51 +02003166 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 * @codec: the HDA codec
Takashi Iwaidcda5802012-10-12 17:24:51 +02003168 * @associated_nid: NID that new ctls associated with
3169 * @cvt_nid: converter NID
3170 * @type: HDA_PCM_TYPE_*
3171 * Creates controls related with the digital output.
3172 * Called from each patch supporting the digital out.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 *
3174 * Returns 0 if successful, or a negative error code.
3175 */
Takashi Iwaidcda5802012-10-12 17:24:51 +02003176int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
3177 hda_nid_t associated_nid,
3178 hda_nid_t cvt_nid,
3179 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180{
3181 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003182 struct snd_kcontrol *kctl;
3183 struct snd_kcontrol_new *dig_mix;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003184 int idx, dev = 0;
3185 const int spdif_pcm_dev = 1;
Stephen Warren7c935972011-06-01 11:14:17 -06003186 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187
Takashi Iwaidcda5802012-10-12 17:24:51 +02003188 if (codec->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
3189 type == HDA_PCM_TYPE_SPDIF) {
3190 dev = spdif_pcm_dev;
3191 } else if (codec->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
3192 type == HDA_PCM_TYPE_HDMI) {
3193 for (idx = 0; idx < codec->spdif_out.used; idx++) {
3194 spdif = snd_array_elem(&codec->spdif_out, idx);
3195 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3196 kctl = find_mixer_ctl(codec, dig_mix->name, 0, idx);
3197 if (!kctl)
3198 break;
3199 kctl->id.device = spdif_pcm_dev;
3200 }
3201 }
3202 codec->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
3203 }
3204 if (!codec->primary_dig_out_type)
3205 codec->primary_dig_out_type = type;
3206
3207 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", dev);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003208 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003209 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
3210 return -EBUSY;
3211 }
Stephen Warren7c935972011-06-01 11:14:17 -06003212 spdif = snd_array_new(&codec->spdif_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3214 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01003215 if (!kctl)
3216 return -ENOMEM;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003217 kctl->id.device = dev;
Takashi Iwai09f99702008-02-04 12:31:13 +01003218 kctl->id.index = idx;
Stephen Warren7c935972011-06-01 11:14:17 -06003219 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06003220 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003221 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 return err;
3223 }
Stephen Warren74b654c2011-06-01 11:14:18 -06003224 spdif->nid = cvt_nid;
3225 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
Stephen Warren7c935972011-06-01 11:14:17 -06003226 AC_VERB_GET_DIGI_CONVERT_1, 0);
3227 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 return 0;
3229}
Takashi Iwaidcda5802012-10-12 17:24:51 +02003230EXPORT_SYMBOL_HDA(snd_hda_create_dig_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003232/* get the hda_spdif_out entry from the given NID
3233 * call within spdif_mutex lock
3234 */
Stephen Warren7c935972011-06-01 11:14:17 -06003235struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3236 hda_nid_t nid)
3237{
3238 int i;
3239 for (i = 0; i < codec->spdif_out.used; i++) {
3240 struct hda_spdif_out *spdif =
3241 snd_array_elem(&codec->spdif_out, i);
3242 if (spdif->nid == nid)
3243 return spdif;
3244 }
3245 return NULL;
3246}
3247EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid);
3248
Stephen Warren74b654c2011-06-01 11:14:18 -06003249void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3250{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003251 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003252
3253 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003254 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003255 spdif->nid = (u16)-1;
3256 mutex_unlock(&codec->spdif_mutex);
3257}
3258EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign);
3259
3260void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3261{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003262 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003263 unsigned short val;
3264
3265 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003266 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003267 if (spdif->nid != nid) {
3268 spdif->nid = nid;
3269 val = spdif->ctls;
3270 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3271 }
3272 mutex_unlock(&codec->spdif_mutex);
3273}
3274EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign);
3275
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276/*
Takashi Iwai9a081602008-02-12 18:37:26 +01003277 * SPDIF sharing with analog output
3278 */
3279static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3280 struct snd_ctl_elem_value *ucontrol)
3281{
3282 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3283 ucontrol->value.integer.value[0] = mout->share_spdif;
3284 return 0;
3285}
3286
3287static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3288 struct snd_ctl_elem_value *ucontrol)
3289{
3290 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3291 mout->share_spdif = !!ucontrol->value.integer.value[0];
3292 return 0;
3293}
3294
3295static struct snd_kcontrol_new spdif_share_sw = {
3296 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3297 .name = "IEC958 Default PCM Playback Switch",
3298 .info = snd_ctl_boolean_mono_info,
3299 .get = spdif_share_sw_get,
3300 .put = spdif_share_sw_put,
3301};
3302
Takashi Iwaid5191e52009-11-16 14:58:17 +01003303/**
3304 * snd_hda_create_spdif_share_sw - create Default PCM switch
3305 * @codec: the HDA codec
3306 * @mout: multi-out instance
3307 */
Takashi Iwai9a081602008-02-12 18:37:26 +01003308int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3309 struct hda_multi_out *mout)
3310{
3311 if (!mout->dig_out_nid)
3312 return 0;
3313 /* ATTENTION: here mout is passed as private_data, instead of codec */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003314 return snd_hda_ctl_add(codec, mout->dig_out_nid,
3315 snd_ctl_new1(&spdif_share_sw, mout));
Takashi Iwai9a081602008-02-12 18:37:26 +01003316}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003317EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01003318
3319/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 * SPDIF input
3321 */
3322
3323#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3324
Takashi Iwai0ba21762007-04-16 11:29:14 +02003325static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3326 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327{
3328 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3329
3330 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3331 return 0;
3332}
3333
Takashi Iwai0ba21762007-04-16 11:29:14 +02003334static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3335 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336{
3337 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3338 hda_nid_t nid = kcontrol->private_value;
3339 unsigned int val = !!ucontrol->value.integer.value[0];
3340 int change;
3341
Ingo Molnar62932df2006-01-16 16:34:20 +01003342 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003344 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003346 snd_hda_codec_write_cache(codec, nid, 0,
3347 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003349 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 return change;
3351}
3352
Takashi Iwai0ba21762007-04-16 11:29:14 +02003353static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3354 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355{
3356 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3357 hda_nid_t nid = kcontrol->private_value;
3358 unsigned short val;
3359 unsigned int sbits;
3360
Andrew Paprocki3982d172007-12-19 12:13:44 +01003361 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362 sbits = convert_to_spdif_status(val);
3363 ucontrol->value.iec958.status[0] = sbits;
3364 ucontrol->value.iec958.status[1] = sbits >> 8;
3365 ucontrol->value.iec958.status[2] = sbits >> 16;
3366 ucontrol->value.iec958.status[3] = sbits >> 24;
3367 return 0;
3368}
3369
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003370static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 {
3372 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003373 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 .info = snd_hda_spdif_in_switch_info,
3375 .get = snd_hda_spdif_in_switch_get,
3376 .put = snd_hda_spdif_in_switch_put,
3377 },
3378 {
3379 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3380 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003381 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 .info = snd_hda_spdif_mask_info,
3383 .get = snd_hda_spdif_in_status_get,
3384 },
3385 { } /* end */
3386};
3387
3388/**
3389 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3390 * @codec: the HDA codec
3391 * @nid: audio in widget NID
3392 *
3393 * Creates controls related with the SPDIF input.
3394 * Called from each patch supporting the SPDIF in.
3395 *
3396 * Returns 0 if successful, or a negative error code.
3397 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003398int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399{
3400 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003401 struct snd_kcontrol *kctl;
3402 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003403 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404
Takashi Iwaidcda5802012-10-12 17:24:51 +02003405 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003406 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003407 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
3408 return -EBUSY;
3409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3411 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01003412 if (!kctl)
3413 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003415 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003416 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 return err;
3418 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003419 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01003420 snd_hda_codec_read(codec, nid, 0,
3421 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02003422 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 return 0;
3424}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003425EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003427/*
3428 * command cache
3429 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430
Takashi Iwaic370dd62012-12-13 18:30:04 +01003431/* build a 31bit cache key with the widget id and the command parameter */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003432#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3433#define get_cmd_cache_nid(key) ((key) & 0xff)
3434#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3435
3436/**
3437 * snd_hda_codec_write_cache - send a single command with caching
3438 * @codec: the HDA codec
3439 * @nid: NID to send the command
3440 * @direct: direct flag
3441 * @verb: the verb to send
3442 * @parm: the parameter for the verb
3443 *
3444 * Send a single command without waiting for response.
3445 *
3446 * Returns 0 if successful, or a negative error code.
3447 */
3448int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
3449 int direct, unsigned int verb, unsigned int parm)
3450{
Takashi Iwaic370dd62012-12-13 18:30:04 +01003451 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003452 struct hda_cache_head *c;
3453 u32 key;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003454 unsigned int cache_only;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003455
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003456 cache_only = codec->cached_write;
3457 if (!cache_only) {
Takashi Iwaic370dd62012-12-13 18:30:04 +01003458 err = snd_hda_codec_write(codec, nid, direct, verb, parm);
3459 if (err < 0)
3460 return err;
3461 }
3462
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003463 /* parm may contain the verb stuff for get/set amp */
3464 verb = verb | (parm >> 8);
3465 parm &= 0xff;
3466 key = build_cmd_cache_key(nid, verb);
3467 mutex_lock(&codec->bus->cmd_mutex);
3468 c = get_alloc_hash(&codec->cmd_cache, key);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003469 if (c) {
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003470 c->val = parm;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003471 c->dirty = cache_only;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003472 }
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003473 mutex_unlock(&codec->bus->cmd_mutex);
3474 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003475}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003476EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003477
Takashi Iwaid5191e52009-11-16 14:58:17 +01003478/**
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003479 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3480 * @codec: the HDA codec
3481 * @nid: NID to send the command
3482 * @direct: direct flag
3483 * @verb: the verb to send
3484 * @parm: the parameter for the verb
3485 *
3486 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3487 * command if the parameter is already identical with the cached value.
3488 * If not, it sends the command and refreshes the cache.
3489 *
3490 * Returns 0 if successful, or a negative error code.
3491 */
3492int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
3493 int direct, unsigned int verb, unsigned int parm)
3494{
3495 struct hda_cache_head *c;
3496 u32 key;
3497
3498 /* parm may contain the verb stuff for get/set amp */
3499 verb = verb | (parm >> 8);
3500 parm &= 0xff;
3501 key = build_cmd_cache_key(nid, verb);
3502 mutex_lock(&codec->bus->cmd_mutex);
3503 c = get_hash(&codec->cmd_cache, key);
3504 if (c && c->val == parm) {
3505 mutex_unlock(&codec->bus->cmd_mutex);
3506 return 0;
3507 }
3508 mutex_unlock(&codec->bus->cmd_mutex);
3509 return snd_hda_codec_write_cache(codec, nid, direct, verb, parm);
3510}
3511EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
3512
3513/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003514 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3515 * @codec: HD-audio codec
3516 *
3517 * Execute all verbs recorded in the command caches to resume.
3518 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003519void snd_hda_codec_resume_cache(struct hda_codec *codec)
3520{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003521 int i;
3522
Takashi Iwaic370dd62012-12-13 18:30:04 +01003523 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01003524 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003525 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3526 struct hda_cache_head *buffer;
3527 u32 key;
3528
3529 buffer = snd_array_elem(&codec->cmd_cache.buf, i);
3530 key = buffer->key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003531 if (!key)
3532 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003533 if (!buffer->dirty)
3534 continue;
3535 buffer->dirty = 0;
3536 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003537 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3538 get_cmd_cache_cmd(key), buffer->val);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003539 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003540 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01003541 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003542}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003543EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003544
3545/**
3546 * snd_hda_sequence_write_cache - sequence writes with caching
3547 * @codec: the HDA codec
3548 * @seq: VERB array to send
3549 *
3550 * Send the commands sequentially from the given array.
3551 * Thte commands are recorded on cache for power-save and resume.
3552 * The array must be terminated with NID=0.
3553 */
3554void snd_hda_sequence_write_cache(struct hda_codec *codec,
3555 const struct hda_verb *seq)
3556{
3557 for (; seq->nid; seq++)
3558 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3559 seq->param);
3560}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003561EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003562
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003563void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
3564 unsigned int power_state,
3565 bool eapd_workaround)
Takashi Iwai54d17402005-11-21 16:33:22 +01003566{
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003567 hda_nid_t nid = codec->start_nid;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003568 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01003569
Takashi Iwaicb53c622007-08-10 17:21:45 +02003570 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003571 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003572 if (!(wcaps & AC_WCAP_POWER))
3573 continue;
3574 /* don't power down the widget if it controls eapd and
3575 * EAPD_BTLENABLE is set.
3576 */
3577 if (eapd_workaround && power_state == AC_PWRST_D3 &&
3578 get_wcaps_type(wcaps) == AC_WID_PIN &&
3579 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3580 int eapd = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003581 AC_VERB_GET_EAPD_BTLENABLE, 0);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003582 if (eapd & 0x02)
3583 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003584 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003585 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
3586 power_state);
Takashi Iwai54d17402005-11-21 16:33:22 +01003587 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003588}
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003589EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all);
3590
3591/*
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003592 * supported power states check
3593 */
3594static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg,
3595 unsigned int power_state)
3596{
3597 int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE);
3598
Mengdong Line037cb42012-08-10 14:11:58 +02003599 if (sup == -1)
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003600 return false;
3601 if (sup & power_state)
3602 return true;
3603 else
3604 return false;
3605}
3606
3607/*
Takashi Iwai432c6412012-08-28 09:59:20 -07003608 * wait until the state is reached, returns the current state
3609 */
3610static unsigned int hda_sync_power_state(struct hda_codec *codec,
3611 hda_nid_t fg,
3612 unsigned int power_state)
3613{
3614 unsigned long end_time = jiffies + msecs_to_jiffies(500);
3615 unsigned int state, actual_state;
3616
3617 for (;;) {
3618 state = snd_hda_codec_read(codec, fg, 0,
3619 AC_VERB_GET_POWER_STATE, 0);
3620 if (state & AC_PWRST_ERROR)
3621 break;
3622 actual_state = (state >> 4) & 0x0f;
3623 if (actual_state == power_state)
3624 break;
3625 if (time_after_eq(jiffies, end_time))
3626 break;
3627 /* wait until the codec reachs to the target state */
3628 msleep(1);
3629 }
3630 return state;
3631}
3632
3633/*
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003634 * set power state of the codec, and return the power state
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003635 */
Takashi Iwaid8193872012-08-31 07:54:38 -07003636static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003637 unsigned int power_state)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003638{
Takashi Iwaid8193872012-08-31 07:54:38 -07003639 hda_nid_t fg = codec->afg ? codec->afg : codec->mfg;
Wang Xingchao09617ce2012-06-08 10:26:08 +08003640 int count;
3641 unsigned int state;
3642
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003643 /* this delay seems necessary to avoid click noise at power-down */
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003644 if (power_state == AC_PWRST_D3) {
3645 /* transition time less than 10ms for power down */
Takashi Iwai983f6b92012-08-28 09:18:01 -07003646 msleep(codec->epss ? 10 : 100);
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003647 }
Wang Xingchao09617ce2012-06-08 10:26:08 +08003648
3649 /* repeat power states setting at most 10 times*/
3650 for (count = 0; count < 10; count++) {
Takashi Iwai432c6412012-08-28 09:59:20 -07003651 if (codec->patch_ops.set_power_state)
3652 codec->patch_ops.set_power_state(codec, fg,
3653 power_state);
3654 else {
3655 snd_hda_codec_read(codec, fg, 0,
3656 AC_VERB_SET_POWER_STATE,
3657 power_state);
3658 snd_hda_codec_set_power_to_all(codec, fg, power_state,
3659 true);
3660 }
3661 state = hda_sync_power_state(codec, fg, power_state);
Wang Xingchao09617ce2012-06-08 10:26:08 +08003662 if (!(state & AC_PWRST_ERROR))
3663 break;
3664 }
Mengdong Linb8dfc4622012-08-23 17:32:30 +08003665
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003666 return state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003667}
Takashi Iwai54d17402005-11-21 16:33:22 +01003668
Takashi Iwai11aeff02008-07-30 15:01:46 +02003669#ifdef CONFIG_SND_HDA_HWDEP
3670/* execute additional init verbs */
3671static void hda_exec_init_verbs(struct hda_codec *codec)
3672{
3673 if (codec->init_verbs.list)
3674 snd_hda_sequence_write(codec, codec->init_verbs.list);
3675}
3676#else
3677static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3678#endif
3679
Takashi Iwai2a439522011-07-26 09:52:50 +02003680#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003681/*
3682 * call suspend and power-down; used both from PM and power-save
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003683 * this function returns the power state in the end
Takashi Iwaicb53c622007-08-10 17:21:45 +02003684 */
Dylan Reidd17344b2012-09-28 15:57:01 -07003685static unsigned int hda_call_codec_suspend(struct hda_codec *codec, bool in_wq)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003686{
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003687 unsigned int state;
3688
Takashi Iwai989c3182012-11-19 14:14:58 +01003689 codec->in_pm = 1;
3690
Takashi Iwaicb53c622007-08-10 17:21:45 +02003691 if (codec->patch_ops.suspend)
Takashi Iwai68cb2b52012-07-02 15:20:37 +02003692 codec->patch_ops.suspend(codec);
Takashi Iwaieb541332010-08-06 13:48:11 +02003693 hda_cleanup_all_streams(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07003694 state = hda_set_power_state(codec, AC_PWRST_D3);
Dylan Reidd17344b2012-09-28 15:57:01 -07003695 /* Cancel delayed work if we aren't currently running from it. */
3696 if (!in_wq)
3697 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02003698 spin_lock(&codec->power_lock);
3699 snd_hda_update_power_acct(codec);
3700 trace_hda_power_down(codec);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02003701 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02003702 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003703 codec->power_jiffies = jiffies;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02003704 spin_unlock(&codec->power_lock);
Takashi Iwai989c3182012-11-19 14:14:58 +01003705 codec->in_pm = 0;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003706 return state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003707}
3708
Takashi Iwaic370dd62012-12-13 18:30:04 +01003709/* mark all entries of cmd and amp caches dirty */
3710static void hda_mark_cmd_cache_dirty(struct hda_codec *codec)
3711{
3712 int i;
3713 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3714 struct hda_cache_head *cmd;
3715 cmd = snd_array_elem(&codec->cmd_cache.buf, i);
3716 cmd->dirty = 1;
3717 }
3718 for (i = 0; i < codec->amp_cache.buf.used; i++) {
3719 struct hda_amp_info *amp;
3720 amp = snd_array_elem(&codec->cmd_cache.buf, i);
3721 amp->head.dirty = 1;
3722 }
3723}
3724
Takashi Iwaicb53c622007-08-10 17:21:45 +02003725/*
3726 * kick up codec; used both from PM and power-save
3727 */
3728static void hda_call_codec_resume(struct hda_codec *codec)
3729{
Takashi Iwai989c3182012-11-19 14:14:58 +01003730 codec->in_pm = 1;
3731
Takashi Iwaic370dd62012-12-13 18:30:04 +01003732 hda_mark_cmd_cache_dirty(codec);
3733
Takashi Iwai7f308302012-05-08 16:52:23 +02003734 /* set as if powered on for avoiding re-entering the resume
3735 * in the resume / power-save sequence
3736 */
3737 hda_keep_power_on(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07003738 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02003739 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02003740 hda_exec_init_verbs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003741 if (codec->patch_ops.resume)
3742 codec->patch_ops.resume(codec);
3743 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02003744 if (codec->patch_ops.init)
3745 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003746 snd_hda_codec_resume_amp(codec);
3747 snd_hda_codec_resume_cache(codec);
3748 }
David Henningsson26a6cb62012-10-09 15:04:21 +02003749
3750 if (codec->jackpoll_interval)
3751 hda_jackpoll_work(&codec->jackpoll_work.work);
3752 else {
3753 snd_hda_jack_set_dirty_all(codec);
3754 snd_hda_jack_report_sync(codec);
3755 }
Takashi Iwai989c3182012-11-19 14:14:58 +01003756
3757 codec->in_pm = 0;
Takashi Iwai7f308302012-05-08 16:52:23 +02003758 snd_hda_power_down(codec); /* flag down before returning */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003759}
Takashi Iwai2a439522011-07-26 09:52:50 +02003760#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003761
Takashi Iwai54d17402005-11-21 16:33:22 +01003762
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763/**
3764 * snd_hda_build_controls - build mixer controls
3765 * @bus: the BUS
3766 *
3767 * Creates mixer controls for each codec included in the bus.
3768 *
3769 * Returns 0 if successful, otherwise a negative error code.
3770 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01003771int snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003773 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774
Takashi Iwai0ba21762007-04-16 11:29:14 +02003775 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003776 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003777 if (err < 0) {
Takashi Iwai28d1a852010-03-15 09:05:46 +01003778 printk(KERN_ERR "hda_codec: cannot build controls "
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003779 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003780 err = snd_hda_codec_reset(codec);
3781 if (err < 0) {
3782 printk(KERN_ERR
3783 "hda_codec: cannot revert codec\n");
3784 return err;
3785 }
3786 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003788 return 0;
3789}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003790EXPORT_SYMBOL_HDA(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003792/*
3793 * add standard channel maps if not specified
3794 */
3795static int add_std_chmaps(struct hda_codec *codec)
3796{
3797 int i, str, err;
3798
3799 for (i = 0; i < codec->num_pcms; i++) {
3800 for (str = 0; str < 2; str++) {
3801 struct snd_pcm *pcm = codec->pcm_info[i].pcm;
3802 struct hda_pcm_stream *hinfo =
3803 &codec->pcm_info[i].stream[str];
3804 struct snd_pcm_chmap *chmap;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003805 const struct snd_pcm_chmap_elem *elem;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003806
3807 if (codec->pcm_info[i].own_chmap)
3808 continue;
3809 if (!pcm || !hinfo->substreams)
3810 continue;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003811 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
3812 err = snd_pcm_add_chmap_ctls(pcm, str, elem,
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003813 hinfo->channels_max,
3814 0, &chmap);
3815 if (err < 0)
3816 return err;
3817 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
3818 }
3819 }
3820 return 0;
3821}
3822
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003823/* default channel maps for 2.1 speakers;
3824 * since HD-audio supports only stereo, odd number channels are omitted
3825 */
3826const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
3827 { .channels = 2,
3828 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
3829 { .channels = 4,
3830 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
3831 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
3832 { }
3833};
3834EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
3835
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003836int snd_hda_codec_build_controls(struct hda_codec *codec)
3837{
3838 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02003839 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003840 /* continue to initialize... */
3841 if (codec->patch_ops.init)
3842 err = codec->patch_ops.init(codec);
3843 if (!err && codec->patch_ops.build_controls)
3844 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003845 if (err < 0)
3846 return err;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003847
3848 /* we create chmaps here instead of build_pcms */
3849 err = add_std_chmaps(codec);
3850 if (err < 0)
3851 return err;
3852
David Henningsson26a6cb62012-10-09 15:04:21 +02003853 if (codec->jackpoll_interval)
3854 hda_jackpoll_work(&codec->jackpoll_work.work);
3855 else
3856 snd_hda_jack_report_sync(codec); /* call at the last init point */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857 return 0;
3858}
3859
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860/*
3861 * stream formats
3862 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02003863struct hda_rate_tbl {
3864 unsigned int hz;
3865 unsigned int alsa_bits;
3866 unsigned int hda_fmt;
3867};
3868
Takashi Iwai92f10b32010-08-03 14:21:00 +02003869/* rate = base * mult / div */
3870#define HDA_RATE(base, mult, div) \
3871 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
3872 (((div) - 1) << AC_FMT_DIV_SHIFT))
3873
Takashi Iwaibefdf312005-08-22 13:57:55 +02003874static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003876
3877 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003878 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
3879 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
3880 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
3881 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
3882 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
3883 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
3884 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
3885 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
3886 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
3887 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
3888 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003889#define AC_PAR_PCM_RATE_BITS 11
3890 /* up to bits 10, 384kHZ isn't supported properly */
3891
3892 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003893 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003894
Takashi Iwaibefdf312005-08-22 13:57:55 +02003895 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896};
3897
3898/**
3899 * snd_hda_calc_stream_format - calculate format bitset
3900 * @rate: the sample rate
3901 * @channels: the number of channels
3902 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3903 * @maxbps: the max. bps
3904 *
3905 * Calculate the format bitset from the given rate, channels and th PCM format.
3906 *
3907 * Return zero if invalid.
3908 */
3909unsigned int snd_hda_calc_stream_format(unsigned int rate,
3910 unsigned int channels,
3911 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03003912 unsigned int maxbps,
3913 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914{
3915 int i;
3916 unsigned int val = 0;
3917
Takashi Iwaibefdf312005-08-22 13:57:55 +02003918 for (i = 0; rate_bits[i].hz; i++)
3919 if (rate_bits[i].hz == rate) {
3920 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921 break;
3922 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003923 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924 snd_printdd("invalid rate %d\n", rate);
3925 return 0;
3926 }
3927
3928 if (channels == 0 || channels > 8) {
3929 snd_printdd("invalid channels %d\n", channels);
3930 return 0;
3931 }
3932 val |= channels - 1;
3933
3934 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003935 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003936 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003937 break;
3938 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003939 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003940 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941 case 20:
3942 case 24:
3943 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003944 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003945 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003947 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02003949 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950 break;
3951 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003952 snd_printdd("invalid format width %d\n",
3953 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954 return 0;
3955 }
3956
Anssi Hannula32c168c2010-08-03 13:28:57 +03003957 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003958 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03003959
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960 return val;
3961}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003962EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003964static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid,
3965 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003966{
3967 unsigned int val = 0;
3968 if (nid != codec->afg &&
3969 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
3970 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
3971 if (!val || val == -1)
3972 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
3973 if (!val || val == -1)
3974 return 0;
3975 return val;
3976}
3977
3978static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3979{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003980 return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003981 get_pcm_param);
3982}
3983
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003984static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid,
3985 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003986{
3987 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
3988 if (!streams || streams == -1)
3989 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
3990 if (!streams || streams == -1)
3991 return 0;
3992 return streams;
3993}
3994
3995static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
3996{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003997 return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003998 get_stream_param);
3999}
4000
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001/**
4002 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
4003 * @codec: the HDA codec
4004 * @nid: NID to query
4005 * @ratesp: the pointer to store the detected rate bitflags
4006 * @formatsp: the pointer to store the detected formats
4007 * @bpsp: the pointer to store the detected format widths
4008 *
4009 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
4010 * or @bsps argument is ignored.
4011 *
4012 * Returns 0 if successful, otherwise a negative error code.
4013 */
Stephen Warren384a48d2011-06-01 11:14:21 -06004014int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
4016{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004017 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004019 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004020 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021
4022 if (ratesp) {
4023 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004024 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02004026 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004028 if (rates == 0) {
4029 snd_printk(KERN_ERR "hda_codec: rates == 0 "
4030 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
4031 nid, val,
4032 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
4033 return -EIO;
4034 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035 *ratesp = rates;
4036 }
4037
4038 if (formatsp || bpsp) {
4039 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004040 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004042 streams = query_stream_param(codec, nid);
4043 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045
4046 bps = 0;
4047 if (streams & AC_SUPFMT_PCM) {
4048 if (val & AC_SUPPCM_BITS_8) {
4049 formats |= SNDRV_PCM_FMTBIT_U8;
4050 bps = 8;
4051 }
4052 if (val & AC_SUPPCM_BITS_16) {
4053 formats |= SNDRV_PCM_FMTBIT_S16_LE;
4054 bps = 16;
4055 }
4056 if (wcaps & AC_WCAP_DIGITAL) {
4057 if (val & AC_SUPPCM_BITS_32)
4058 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
4059 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
4060 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4061 if (val & AC_SUPPCM_BITS_24)
4062 bps = 24;
4063 else if (val & AC_SUPPCM_BITS_20)
4064 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004065 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
4066 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4068 if (val & AC_SUPPCM_BITS_32)
4069 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070 else if (val & AC_SUPPCM_BITS_24)
4071 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02004072 else if (val & AC_SUPPCM_BITS_20)
4073 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074 }
4075 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004076#if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
Takashi Iwaib5025c52009-07-01 18:05:27 +02004077 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004079 if (!bps)
4080 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02004081 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004082#endif
Takashi Iwaib5025c52009-07-01 18:05:27 +02004083 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02004084 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085 /* temporary hack: we have still no proper support
4086 * for the direct AC3 stream...
4087 */
4088 formats |= SNDRV_PCM_FMTBIT_U8;
4089 bps = 8;
4090 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004091 if (formats == 0) {
4092 snd_printk(KERN_ERR "hda_codec: formats == 0 "
4093 "(nid=0x%x, val=0x%x, ovrd=%i, "
4094 "streams=0x%x)\n",
4095 nid, val,
4096 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
4097 streams);
4098 return -EIO;
4099 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100 if (formatsp)
4101 *formatsp = formats;
4102 if (bpsp)
4103 *bpsp = bps;
4104 }
4105
4106 return 0;
4107}
Stephen Warren384a48d2011-06-01 11:14:21 -06004108EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109
4110/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01004111 * snd_hda_is_supported_format - Check the validity of the format
4112 * @codec: HD-audio codec
4113 * @nid: NID to check
4114 * @format: the HD-audio format value to check
4115 *
4116 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117 *
4118 * Returns 1 if supported, 0 if not.
4119 */
4120int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
4121 unsigned int format)
4122{
4123 int i;
4124 unsigned int val = 0, rate, stream;
4125
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004126 val = query_pcm_param(codec, nid);
4127 if (!val)
4128 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129
4130 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004131 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02004132 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133 if (val & (1 << i))
4134 break;
4135 return 0;
4136 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004137 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138 return 0;
4139
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004140 stream = query_stream_param(codec, nid);
4141 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142 return 0;
4143
4144 if (stream & AC_SUPFMT_PCM) {
4145 switch (format & 0xf0) {
4146 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004147 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148 return 0;
4149 break;
4150 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004151 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152 return 0;
4153 break;
4154 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004155 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156 return 0;
4157 break;
4158 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004159 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 return 0;
4161 break;
4162 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004163 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164 return 0;
4165 break;
4166 default:
4167 return 0;
4168 }
4169 } else {
4170 /* FIXME: check for float32 and AC3? */
4171 }
4172
4173 return 1;
4174}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004175EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176
4177/*
4178 * PCM stuff
4179 */
4180static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
4181 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004182 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183{
4184 return 0;
4185}
4186
4187static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
4188 struct hda_codec *codec,
4189 unsigned int stream_tag,
4190 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004191 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192{
4193 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4194 return 0;
4195}
4196
4197static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
4198 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004199 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200{
Takashi Iwai888afa12008-03-18 09:57:50 +01004201 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202 return 0;
4203}
4204
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004205static int set_pcm_default_values(struct hda_codec *codec,
4206 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004208 int err;
4209
Takashi Iwai0ba21762007-04-16 11:29:14 +02004210 /* query support PCM information from the given NID */
4211 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004212 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02004213 info->rates ? NULL : &info->rates,
4214 info->formats ? NULL : &info->formats,
4215 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004216 if (err < 0)
4217 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218 }
4219 if (info->ops.open == NULL)
4220 info->ops.open = hda_pcm_default_open_close;
4221 if (info->ops.close == NULL)
4222 info->ops.close = hda_pcm_default_open_close;
4223 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004224 if (snd_BUG_ON(!info->nid))
4225 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226 info->ops.prepare = hda_pcm_default_prepare;
4227 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004229 if (snd_BUG_ON(!info->nid))
4230 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231 info->ops.cleanup = hda_pcm_default_cleanup;
4232 }
4233 return 0;
4234}
4235
Takashi Iwaieb541332010-08-06 13:48:11 +02004236/*
4237 * codec prepare/cleanup entries
4238 */
4239int snd_hda_codec_prepare(struct hda_codec *codec,
4240 struct hda_pcm_stream *hinfo,
4241 unsigned int stream,
4242 unsigned int format,
4243 struct snd_pcm_substream *substream)
4244{
4245 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004246 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004247 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
4248 if (ret >= 0)
4249 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004250 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004251 return ret;
4252}
4253EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
4254
4255void snd_hda_codec_cleanup(struct hda_codec *codec,
4256 struct hda_pcm_stream *hinfo,
4257 struct snd_pcm_substream *substream)
4258{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004259 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004260 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004261 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004262}
4263EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
4264
Takashi Iwaid5191e52009-11-16 14:58:17 +01004265/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01004266const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
4267 "Audio", "SPDIF", "HDMI", "Modem"
4268};
4269
Takashi Iwai176d5332008-07-30 15:01:44 +02004270/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004271 * get the empty PCM device number to assign
Takashi Iwaic8936222010-01-28 17:08:53 +01004272 *
4273 * note the max device number is limited by HDA_MAX_PCMS, currently 10
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004274 */
4275static int get_empty_pcm_device(struct hda_bus *bus, int type)
4276{
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004277 /* audio device indices; not linear to keep compatibility */
4278 static int audio_idx[HDA_PCM_NTYPES][5] = {
4279 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
4280 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01004281 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004282 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004283 };
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004284 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004285
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004286 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004287 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
4288 return -EINVAL;
4289 }
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004290
4291 for (i = 0; audio_idx[type][i] >= 0 ; i++)
4292 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
4293 return audio_idx[type][i];
4294
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004295 /* non-fixed slots starting from 10 */
4296 for (i = 10; i < 32; i++) {
4297 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4298 return i;
4299 }
4300
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004301 snd_printk(KERN_WARNING "Too many %s devices\n",
4302 snd_hda_pcm_type_name[type]);
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004303 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004304}
4305
4306/*
Takashi Iwai176d5332008-07-30 15:01:44 +02004307 * attach a new PCM stream
4308 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004309static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02004310{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004311 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02004312 struct hda_pcm_stream *info;
4313 int stream, err;
4314
Takashi Iwaib91f0802008-11-04 08:43:08 +01004315 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02004316 return -EINVAL;
4317 for (stream = 0; stream < 2; stream++) {
4318 info = &pcm->stream[stream];
4319 if (info->substreams) {
4320 err = set_pcm_default_values(codec, info);
4321 if (err < 0)
4322 return err;
4323 }
4324 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004325 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02004326}
4327
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004328/* assign all PCMs of the given codec */
4329int snd_hda_codec_build_pcms(struct hda_codec *codec)
4330{
4331 unsigned int pcm;
4332 int err;
4333
4334 if (!codec->num_pcms) {
4335 if (!codec->patch_ops.build_pcms)
4336 return 0;
4337 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004338 if (err < 0) {
4339 printk(KERN_ERR "hda_codec: cannot build PCMs"
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004340 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004341 err = snd_hda_codec_reset(codec);
4342 if (err < 0) {
4343 printk(KERN_ERR
4344 "hda_codec: cannot revert codec\n");
4345 return err;
4346 }
4347 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004348 }
4349 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
4350 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
4351 int dev;
4352
4353 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01004354 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004355
4356 if (!cpcm->pcm) {
4357 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
4358 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004359 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004360 cpcm->device = dev;
4361 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004362 if (err < 0) {
4363 printk(KERN_ERR "hda_codec: cannot attach "
4364 "PCM stream %d for codec #%d\n",
4365 dev, codec->addr);
4366 continue; /* no fatal error */
4367 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004368 }
4369 }
4370 return 0;
4371}
4372
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373/**
4374 * snd_hda_build_pcms - build PCM information
4375 * @bus: the BUS
4376 *
4377 * Create PCM information for each codec included in the bus.
4378 *
4379 * The build_pcms codec patch is requested to set up codec->num_pcms and
4380 * codec->pcm_info properly. The array is referred by the top-level driver
4381 * to create its PCM instances.
4382 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4383 * callback.
4384 *
4385 * At least, substreams, channels_min and channels_max must be filled for
4386 * each stream. substreams = 0 indicates that the stream doesn't exist.
4387 * When rates and/or formats are zero, the supported values are queried
4388 * from the given nid. The nid is used also by the default ops.prepare
4389 * and ops.cleanup callbacks.
4390 *
4391 * The driver needs to call ops.open in its open callback. Similarly,
4392 * ops.close is supposed to be called in the close callback.
4393 * ops.prepare should be called in the prepare or hw_params callback
4394 * with the proper parameters for set up.
4395 * ops.cleanup should be called in hw_free for clean up of streams.
4396 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004397 * This function returns 0 if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398 */
Takashi Iwai5cb543d2012-08-09 13:49:23 +02004399int snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004401 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402
Takashi Iwai0ba21762007-04-16 11:29:14 +02004403 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004404 int err = snd_hda_codec_build_pcms(codec);
4405 if (err < 0)
4406 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407 }
4408 return 0;
4409}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004410EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412/**
4413 * snd_hda_check_board_config - compare the current codec with the config table
4414 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004415 * @num_configs: number of config enums
4416 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417 * @tbl: configuration table, terminated by null entries
4418 *
4419 * Compares the modelname or PCI subsystem id of the current codec with the
4420 * given configuration table. If a matching entry is found, returns its
4421 * config value (supposed to be 0 or positive).
4422 *
4423 * If no entries are matching, the function returns a negative value.
4424 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02004425int snd_hda_check_board_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004426 int num_configs, const char * const *models,
Takashi Iwai12f288b2007-08-02 15:51:59 +02004427 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428{
Takashi Iwaif44ac832008-07-30 15:01:45 +02004429 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004430 int i;
4431 for (i = 0; i < num_configs; i++) {
4432 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02004433 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004434 snd_printd(KERN_INFO "hda_codec: model '%s' is "
4435 "selected\n", models[i]);
4436 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437 }
4438 }
4439 }
4440
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004441 if (!codec->bus->pci || !tbl)
4442 return -1;
4443
4444 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
4445 if (!tbl)
4446 return -1;
4447 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02004448#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004449 char tmp[10];
4450 const char *model = NULL;
4451 if (models)
4452 model = models[tbl->value];
4453 if (!model) {
4454 sprintf(tmp, "#%d", tbl->value);
4455 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004457 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4458 "for config %x:%x (%s)\n",
4459 model, tbl->subvendor, tbl->subdevice,
4460 (tbl->name ? tbl->name : "Unknown device"));
4461#endif
4462 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 }
4464 return -1;
4465}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004466EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467
4468/**
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004469 * snd_hda_check_board_codec_sid_config - compare the current codec
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004470 subsystem ID with the
4471 config table
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004472
4473 This is important for Gateway notebooks with SB450 HDA Audio
4474 where the vendor ID of the PCI device is:
4475 ATI Technologies Inc SB450 HDA Audio [1002:437b]
4476 and the vendor/subvendor are found only at the codec.
4477
4478 * @codec: the HDA codec
4479 * @num_configs: number of config enums
4480 * @models: array of model name strings
4481 * @tbl: configuration table, terminated by null entries
4482 *
4483 * Compares the modelname or PCI subsystem id of the current codec with the
4484 * given configuration table. If a matching entry is found, returns its
4485 * config value (supposed to be 0 or positive).
4486 *
4487 * If no entries are matching, the function returns a negative value.
4488 */
4489int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004490 int num_configs, const char * const *models,
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004491 const struct snd_pci_quirk *tbl)
4492{
4493 const struct snd_pci_quirk *q;
4494
4495 /* Search for codec ID */
4496 for (q = tbl; q->subvendor; q++) {
Takashi Iwaie2301a42011-11-22 19:58:56 +01004497 unsigned int mask = 0xffff0000 | q->subdevice_mask;
4498 unsigned int id = (q->subdevice | (q->subvendor << 16)) & mask;
4499 if ((codec->subsystem_id & mask) == id)
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004500 break;
4501 }
4502
4503 if (!q->subvendor)
4504 return -1;
4505
4506 tbl = q;
4507
4508 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwaid94ff6b2009-09-02 00:20:21 +02004509#ifdef CONFIG_SND_DEBUG_VERBOSE
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004510 char tmp[10];
4511 const char *model = NULL;
4512 if (models)
4513 model = models[tbl->value];
4514 if (!model) {
4515 sprintf(tmp, "#%d", tbl->value);
4516 model = tmp;
4517 }
4518 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4519 "for config %x:%x (%s)\n",
4520 model, tbl->subvendor, tbl->subdevice,
4521 (tbl->name ? tbl->name : "Unknown device"));
4522#endif
4523 return tbl->value;
4524 }
4525 return -1;
4526}
4527EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
4528
4529/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530 * snd_hda_add_new_ctls - create controls from the array
4531 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004532 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533 *
4534 * This helper function creates and add new controls in the given array.
4535 * The array must be terminated with an empty entry as terminator.
4536 *
4537 * Returns 0 if successful, or a negative error code.
4538 */
Takashi Iwai031024e2011-05-02 11:29:30 +02004539int snd_hda_add_new_ctls(struct hda_codec *codec,
4540 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01004542 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543
4544 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004545 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004546 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01004547 if (knew->iface == -1) /* skip this codec private value */
4548 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004549 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004550 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02004551 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01004552 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004553 if (addr > 0)
4554 kctl->id.device = addr;
4555 if (idx > 0)
4556 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01004557 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004558 if (!err)
4559 break;
4560 /* try first with another device index corresponding to
4561 * the codec addr; if it still fails (or it's the
4562 * primary codec), then try another control index
4563 */
4564 if (!addr && codec->addr)
4565 addr = codec->addr;
4566 else if (!idx && !knew->index) {
4567 idx = find_empty_mixer_ctl_idx(codec,
Takashi Iwaidcda5802012-10-12 17:24:51 +02004568 knew->name, 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004569 if (idx <= 0)
4570 return err;
4571 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01004572 return err;
4573 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574 }
4575 return 0;
4576}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004577EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578
Takashi Iwai83012a72012-08-24 18:38:08 +02004579#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02004580static void hda_power_work(struct work_struct *work)
4581{
4582 struct hda_codec *codec =
4583 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004584 struct hda_bus *bus = codec->bus;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004585 unsigned int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004586
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004587 spin_lock(&codec->power_lock);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004588 if (codec->power_transition > 0) { /* during power-up sequence? */
4589 spin_unlock(&codec->power_lock);
4590 return;
4591 }
Maxim Levitsky2e492462007-09-03 15:26:57 +02004592 if (!codec->power_on || codec->power_count) {
4593 codec->power_transition = 0;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004594 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004595 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02004596 }
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004597 spin_unlock(&codec->power_lock);
4598
Dylan Reidd17344b2012-09-28 15:57:01 -07004599 state = hda_call_codec_suspend(codec, true);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004600 codec->pm_down_notified = 0;
4601 if (!bus->power_keep_link_on && (state & AC_PWRST_CLK_STOP_OK)) {
4602 codec->pm_down_notified = 1;
Takashi Iwai68467f52012-08-28 09:14:29 -07004603 hda_call_pm_notify(bus, false);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004604 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02004605}
4606
4607static void hda_keep_power_on(struct hda_codec *codec)
4608{
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004609 spin_lock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004610 codec->power_count++;
4611 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004612 codec->power_jiffies = jiffies;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004613 spin_unlock(&codec->power_lock);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004614}
4615
Takashi Iwaid5191e52009-11-16 14:58:17 +01004616/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01004617void snd_hda_update_power_acct(struct hda_codec *codec)
4618{
4619 unsigned long delta = jiffies - codec->power_jiffies;
4620 if (codec->power_on)
4621 codec->power_on_acct += delta;
4622 else
4623 codec->power_off_acct += delta;
4624 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004625}
4626
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004627/* Transition to powered up, if wait_power_down then wait for a pending
4628 * transition to D3 to complete. A pending D3 transition is indicated
4629 * with power_transition == -1. */
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004630/* call this with codec->power_lock held! */
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004631static void __snd_hda_power_up(struct hda_codec *codec, bool wait_power_down)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004632{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004633 struct hda_bus *bus = codec->bus;
4634
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004635 /* Return if power_on or transitioning to power_on, unless currently
4636 * powering down. */
4637 if ((codec->power_on || codec->power_transition > 0) &&
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004638 !(wait_power_down && codec->power_transition < 0))
Takashi Iwaicb53c622007-08-10 17:21:45 +02004639 return;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004640 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004641
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004642 cancel_delayed_work_sync(&codec->power_work);
4643
4644 spin_lock(&codec->power_lock);
Dylan Reidb43d2242012-06-21 21:51:22 -07004645 /* If the power down delayed work was cancelled above before starting,
4646 * then there is no need to go through power up here.
4647 */
4648 if (codec->power_on) {
Takashi Iwai535b6c52012-08-20 21:25:22 +02004649 if (codec->power_transition < 0)
4650 codec->power_transition = 0;
Dylan Reidb43d2242012-06-21 21:51:22 -07004651 return;
4652 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004653
Takashi Iwaid66fee52011-08-02 15:39:31 +02004654 trace_hda_power_up(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004655 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004656 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004657 codec->power_jiffies = jiffies;
Takashi Iwai7f308302012-05-08 16:52:23 +02004658 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004659 spin_unlock(&codec->power_lock);
4660
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004661 if (codec->pm_down_notified) {
4662 codec->pm_down_notified = 0;
Takashi Iwai68467f52012-08-28 09:14:29 -07004663 hda_call_pm_notify(bus, true);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004664 }
4665
Takashi Iwaicb53c622007-08-10 17:21:45 +02004666 hda_call_codec_resume(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004667
4668 spin_lock(&codec->power_lock);
Takashi Iwaia221e282007-08-16 16:35:33 +02004669 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004670}
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004671
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004672#define power_save(codec) \
4673 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004674
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004675/* Transition to powered down */
4676static void __snd_hda_power_down(struct hda_codec *codec)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004677{
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004678 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004679 return;
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004680
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004681 if (power_save(codec)) {
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004682 codec->power_transition = -1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01004683 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004684 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02004685 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004686}
4687
4688/**
4689 * snd_hda_power_save - Power-up/down/sync the codec
4690 * @codec: HD-audio codec
4691 * @delta: the counter delta to change
4692 *
4693 * Change the power-up counter via @delta, and power up or down the hardware
4694 * appropriately. For the power-down, queue to the delayed action.
4695 * Passing zero to @delta means to synchronize the power state.
4696 */
4697void snd_hda_power_save(struct hda_codec *codec, int delta, bool d3wait)
4698{
4699 spin_lock(&codec->power_lock);
4700 codec->power_count += delta;
4701 trace_hda_power_count(codec);
4702 if (delta > 0)
4703 __snd_hda_power_up(codec, d3wait);
4704 else
4705 __snd_hda_power_down(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004706 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004707}
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004708EXPORT_SYMBOL_HDA(snd_hda_power_save);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004709
Takashi Iwaid5191e52009-11-16 14:58:17 +01004710/**
4711 * snd_hda_check_amp_list_power - Check the amp list and update the power
4712 * @codec: HD-audio codec
4713 * @check: the object containing an AMP list and the status
4714 * @nid: NID to check / update
4715 *
4716 * Check whether the given NID is in the amp list. If it's in the list,
4717 * check the current AMP status, and update the the power-status according
4718 * to the mute status.
4719 *
4720 * This function is supposed to be set or called from the check_power_status
4721 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004722 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004723int snd_hda_check_amp_list_power(struct hda_codec *codec,
4724 struct hda_loopback_check *check,
4725 hda_nid_t nid)
4726{
Takashi Iwai031024e2011-05-02 11:29:30 +02004727 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004728 int ch, v;
4729
4730 if (!check->amplist)
4731 return 0;
4732 for (p = check->amplist; p->nid; p++) {
4733 if (p->nid == nid)
4734 break;
4735 }
4736 if (!p->nid)
4737 return 0; /* nothing changed */
4738
4739 for (p = check->amplist; p->nid; p++) {
4740 for (ch = 0; ch < 2; ch++) {
4741 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4742 p->idx);
4743 if (!(v & HDA_AMP_MUTE) && v > 0) {
4744 if (!check->power_on) {
4745 check->power_on = 1;
4746 snd_hda_power_up(codec);
4747 }
4748 return 1;
4749 }
4750 }
4751 }
4752 if (check->power_on) {
4753 check->power_on = 0;
4754 snd_hda_power_down(codec);
4755 }
4756 return 0;
4757}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004758EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004759#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004761/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004762 * Channel mode helper
4763 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004764
4765/**
4766 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
4767 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004768int snd_hda_ch_mode_info(struct hda_codec *codec,
4769 struct snd_ctl_elem_info *uinfo,
4770 const struct hda_channel_mode *chmode,
4771 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004772{
4773 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4774 uinfo->count = 1;
4775 uinfo->value.enumerated.items = num_chmodes;
4776 if (uinfo->value.enumerated.item >= num_chmodes)
4777 uinfo->value.enumerated.item = num_chmodes - 1;
4778 sprintf(uinfo->value.enumerated.name, "%dch",
4779 chmode[uinfo->value.enumerated.item].channels);
4780 return 0;
4781}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004782EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004783
Takashi Iwaid5191e52009-11-16 14:58:17 +01004784/**
4785 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
4786 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004787int snd_hda_ch_mode_get(struct hda_codec *codec,
4788 struct snd_ctl_elem_value *ucontrol,
4789 const struct hda_channel_mode *chmode,
4790 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004791 int max_channels)
4792{
4793 int i;
4794
4795 for (i = 0; i < num_chmodes; i++) {
4796 if (max_channels == chmode[i].channels) {
4797 ucontrol->value.enumerated.item[0] = i;
4798 break;
4799 }
4800 }
4801 return 0;
4802}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004803EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004804
Takashi Iwaid5191e52009-11-16 14:58:17 +01004805/**
4806 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
4807 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004808int snd_hda_ch_mode_put(struct hda_codec *codec,
4809 struct snd_ctl_elem_value *ucontrol,
4810 const struct hda_channel_mode *chmode,
4811 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004812 int *max_channelsp)
4813{
4814 unsigned int mode;
4815
4816 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01004817 if (mode >= num_chmodes)
4818 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004819 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004820 return 0;
4821 /* change the current channel setting */
4822 *max_channelsp = chmode[mode].channels;
4823 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004824 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004825 return 1;
4826}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004827EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004828
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829/*
4830 * input MUX helper
4831 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004832
4833/**
4834 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
4835 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004836int snd_hda_input_mux_info(const struct hda_input_mux *imux,
4837 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838{
4839 unsigned int index;
4840
4841 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4842 uinfo->count = 1;
4843 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004844 if (!imux->num_items)
4845 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846 index = uinfo->value.enumerated.item;
4847 if (index >= imux->num_items)
4848 index = imux->num_items - 1;
4849 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
4850 return 0;
4851}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004852EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853
Takashi Iwaid5191e52009-11-16 14:58:17 +01004854/**
4855 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
4856 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004857int snd_hda_input_mux_put(struct hda_codec *codec,
4858 const struct hda_input_mux *imux,
4859 struct snd_ctl_elem_value *ucontrol,
4860 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861 unsigned int *cur_val)
4862{
4863 unsigned int idx;
4864
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004865 if (!imux->num_items)
4866 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867 idx = ucontrol->value.enumerated.item[0];
4868 if (idx >= imux->num_items)
4869 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004870 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004872 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
4873 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874 *cur_val = idx;
4875 return 1;
4876}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004877EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878
4879
4880/*
Takashi Iwaidda415d2012-11-30 18:34:38 +01004881 * process kcontrol info callback of a simple string enum array
4882 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
4883 */
4884int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
4885 struct snd_ctl_elem_info *uinfo,
4886 int num_items, const char * const *texts)
4887{
4888 static const char * const texts_default[] = {
4889 "Disabled", "Enabled"
4890 };
4891
4892 if (!texts || !num_items) {
4893 num_items = 2;
4894 texts = texts_default;
4895 }
4896
4897 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4898 uinfo->count = 1;
4899 uinfo->value.enumerated.items = num_items;
4900 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
4901 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
4902 strcpy(uinfo->value.enumerated.name,
4903 texts[uinfo->value.enumerated.item]);
4904 return 0;
4905}
4906EXPORT_SYMBOL_HDA(snd_hda_enum_helper_info);
4907
4908/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909 * Multi-channel / digital-out PCM helper functions
4910 */
4911
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004912/* setup SPDIF output stream */
4913static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
4914 unsigned int stream_tag, unsigned int format)
4915{
Laurence Darby3bef1c32012-11-03 17:00:06 +00004916 struct hda_spdif_out *spdif;
4917 unsigned int curr_fmt;
4918 bool reset;
Stephen Warren7c935972011-06-01 11:14:17 -06004919
Laurence Darby3bef1c32012-11-03 17:00:06 +00004920 spdif = snd_hda_spdif_out_of_nid(codec, nid);
4921 curr_fmt = snd_hda_codec_read(codec, nid, 0,
4922 AC_VERB_GET_STREAM_FORMAT, 0);
4923 reset = codec->spdif_status_reset &&
4924 (spdif->ctls & AC_DIG1_ENABLE) &&
4925 curr_fmt != format;
4926
4927 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
4928 updated */
4929 if (reset)
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004930 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06004931 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02004932 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004933 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02004934 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004935 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004936 for (d = codec->slave_dig_outs; *d; d++)
4937 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
4938 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004939 }
Takashi Iwai2f728532008-09-25 16:32:41 +02004940 /* turn on again (if needed) */
Laurence Darby3bef1c32012-11-03 17:00:06 +00004941 if (reset)
Takashi Iwai2f728532008-09-25 16:32:41 +02004942 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06004943 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02004944}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004945
Takashi Iwai2f728532008-09-25 16:32:41 +02004946static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
4947{
4948 snd_hda_codec_cleanup_stream(codec, nid);
4949 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004950 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004951 for (d = codec->slave_dig_outs; *d; d++)
4952 snd_hda_codec_cleanup_stream(codec, *d);
4953 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004954}
4955
Takashi Iwaid5191e52009-11-16 14:58:17 +01004956/**
4957 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
4958 * @bus: HD-audio bus
4959 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004960void snd_hda_bus_reboot_notify(struct hda_bus *bus)
4961{
4962 struct hda_codec *codec;
4963
4964 if (!bus)
4965 return;
4966 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02004967 if (hda_codec_is_power_on(codec) &&
4968 codec->patch_ops.reboot_notify)
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004969 codec->patch_ops.reboot_notify(codec);
4970 }
4971}
Takashi Iwai8f217a22009-11-10 18:26:12 +01004972EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004973
Takashi Iwaid5191e52009-11-16 14:58:17 +01004974/**
4975 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004977int snd_hda_multi_out_dig_open(struct hda_codec *codec,
4978 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979{
Ingo Molnar62932df2006-01-16 16:34:20 +01004980 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02004981 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
4982 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02004983 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004984 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01004985 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986 return 0;
4987}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004988EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989
Takashi Iwaid5191e52009-11-16 14:58:17 +01004990/**
4991 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
4992 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004993int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
4994 struct hda_multi_out *mout,
4995 unsigned int stream_tag,
4996 unsigned int format,
4997 struct snd_pcm_substream *substream)
4998{
4999 mutex_lock(&codec->spdif_mutex);
5000 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
5001 mutex_unlock(&codec->spdif_mutex);
5002 return 0;
5003}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005004EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005005
Takashi Iwaid5191e52009-11-16 14:58:17 +01005006/**
5007 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
5008 */
Takashi Iwai9411e212009-02-13 11:32:28 +01005009int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
5010 struct hda_multi_out *mout)
5011{
5012 mutex_lock(&codec->spdif_mutex);
5013 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5014 mutex_unlock(&codec->spdif_mutex);
5015 return 0;
5016}
5017EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
5018
Takashi Iwaid5191e52009-11-16 14:58:17 +01005019/**
5020 * snd_hda_multi_out_dig_close - release the digital out stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005022int snd_hda_multi_out_dig_close(struct hda_codec *codec,
5023 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005024{
Ingo Molnar62932df2006-01-16 16:34:20 +01005025 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01005027 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028 return 0;
5029}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005030EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031
Takashi Iwaid5191e52009-11-16 14:58:17 +01005032/**
5033 * snd_hda_multi_out_analog_open - open analog outputs
5034 *
5035 * Open analog outputs and set up the hw-constraints.
5036 * If the digital outputs can be opened as slave, open the digital
5037 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005039int snd_hda_multi_out_analog_open(struct hda_codec *codec,
5040 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01005041 struct snd_pcm_substream *substream,
5042 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043{
Takashi Iwai9a081602008-02-12 18:37:26 +01005044 struct snd_pcm_runtime *runtime = substream->runtime;
5045 runtime->hw.channels_max = mout->max_channels;
5046 if (mout->dig_out_nid) {
5047 if (!mout->analog_rates) {
5048 mout->analog_rates = hinfo->rates;
5049 mout->analog_formats = hinfo->formats;
5050 mout->analog_maxbps = hinfo->maxbps;
5051 } else {
5052 runtime->hw.rates = mout->analog_rates;
5053 runtime->hw.formats = mout->analog_formats;
5054 hinfo->maxbps = mout->analog_maxbps;
5055 }
5056 if (!mout->spdif_rates) {
5057 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
5058 &mout->spdif_rates,
5059 &mout->spdif_formats,
5060 &mout->spdif_maxbps);
5061 }
5062 mutex_lock(&codec->spdif_mutex);
5063 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02005064 if ((runtime->hw.rates & mout->spdif_rates) &&
5065 (runtime->hw.formats & mout->spdif_formats)) {
5066 runtime->hw.rates &= mout->spdif_rates;
5067 runtime->hw.formats &= mout->spdif_formats;
5068 if (mout->spdif_maxbps < hinfo->maxbps)
5069 hinfo->maxbps = mout->spdif_maxbps;
5070 } else {
5071 mout->share_spdif = 0;
5072 /* FIXME: need notify? */
5073 }
Takashi Iwai9a081602008-02-12 18:37:26 +01005074 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02005075 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01005076 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077 return snd_pcm_hw_constraint_step(substream->runtime, 0,
5078 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
5079}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005080EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081
Takashi Iwaid5191e52009-11-16 14:58:17 +01005082/**
5083 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
5084 *
5085 * Set up the i/o for analog out.
5086 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005088int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
5089 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090 unsigned int stream_tag,
5091 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01005092 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093{
Takashi Iwaidda14412011-05-02 11:29:30 +02005094 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095 int chs = substream->runtime->channels;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005096 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097 int i;
5098
Ingo Molnar62932df2006-01-16 16:34:20 +01005099 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005100 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Takashi Iwai9a081602008-02-12 18:37:26 +01005101 if (mout->dig_out_nid && mout->share_spdif &&
5102 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02005104 snd_hda_is_supported_format(codec, mout->dig_out_nid,
5105 format) &&
Stephen Warren7c935972011-06-01 11:14:17 -06005106 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005108 setup_dig_out_stream(codec, mout->dig_out_nid,
5109 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005110 } else {
5111 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02005112 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113 }
5114 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005115 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116
5117 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005118 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
5119 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005120 if (!mout->no_share_stream &&
5121 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005122 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005123 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
5124 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005125 /* extra outputs copied from front */
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005126 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5127 if (!mout->no_share_stream && mout->hp_out_nid[i])
5128 snd_hda_codec_setup_stream(codec,
5129 mout->hp_out_nid[i],
5130 stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005131 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02005132 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01005133 snd_hda_codec_setup_stream(codec,
5134 mout->extra_out_nid[i],
5135 stream_tag, 0, format);
5136
Linus Torvalds1da177e2005-04-16 15:20:36 -07005137 /* surrounds */
5138 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02005139 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005140 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5141 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005142 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005143 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5144 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005145 }
5146 return 0;
5147}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005148EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149
Takashi Iwaid5191e52009-11-16 14:58:17 +01005150/**
5151 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Linus Torvalds1da177e2005-04-16 15:20:36 -07005152 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005153int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
5154 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155{
Takashi Iwaidda14412011-05-02 11:29:30 +02005156 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005157 int i;
5158
5159 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01005160 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01005162 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005163 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5164 if (mout->hp_out_nid[i])
5165 snd_hda_codec_cleanup_stream(codec,
5166 mout->hp_out_nid[i]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005167 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
5168 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01005169 snd_hda_codec_cleanup_stream(codec,
5170 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01005171 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02005173 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174 mout->dig_out_used = 0;
5175 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005176 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177 return 0;
5178}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005179EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180
Takashi Iwai47408602012-04-20 13:06:53 +02005181/**
5182 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
5183 *
5184 * Guess the suitable VREF pin bits to be set as the pin-control value.
5185 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
5186 */
5187unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
5188{
5189 unsigned int pincap;
5190 unsigned int oldval;
5191 oldval = snd_hda_codec_read(codec, pin, 0,
5192 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5193 pincap = snd_hda_query_pin_caps(codec, pin);
5194 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5195 /* Exception: if the default pin setup is vref50, we give it priority */
5196 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
5197 return AC_PINCTL_VREF_80;
5198 else if (pincap & AC_PINCAP_VREF_50)
5199 return AC_PINCTL_VREF_50;
5200 else if (pincap & AC_PINCAP_VREF_100)
5201 return AC_PINCTL_VREF_100;
5202 else if (pincap & AC_PINCAP_VREF_GRD)
5203 return AC_PINCTL_VREF_GRD;
5204 return AC_PINCTL_VREF_HIZ;
5205}
5206EXPORT_SYMBOL_HDA(snd_hda_get_default_vref);
5207
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005208int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
5209 unsigned int val, bool cached)
5210{
5211 if (val) {
5212 unsigned int cap = snd_hda_query_pin_caps(codec, pin);
Takashi Iwai6942c102012-04-20 13:08:40 +02005213 if (cap && (val & AC_PINCTL_OUT_EN)) {
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005214 if (!(cap & AC_PINCAP_OUT))
5215 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
5216 else if ((val & AC_PINCTL_HP_EN) &&
5217 !(cap & AC_PINCAP_HP_DRV))
5218 val &= ~AC_PINCTL_HP_EN;
5219 }
Takashi Iwai6942c102012-04-20 13:08:40 +02005220 if (cap && (val & AC_PINCTL_IN_EN)) {
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005221 if (!(cap & AC_PINCAP_IN))
5222 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
5223 }
5224 }
5225 if (cached)
5226 return snd_hda_codec_update_cache(codec, pin, 0,
5227 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5228 else
5229 return snd_hda_codec_write(codec, pin, 0,
5230 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5231}
5232EXPORT_SYMBOL_HDA(_snd_hda_set_pin_ctl);
5233
Takashi Iwai990061c2010-09-09 22:08:44 +02005234/**
5235 * snd_hda_add_imux_item - Add an item to input_mux
5236 *
5237 * When the same label is used already in the existing items, the number
5238 * suffix is appended to the label. This label index number is stored
5239 * to type_idx when non-NULL pointer is given.
5240 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02005241int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
5242 int index, int *type_idx)
5243{
5244 int i, label_idx = 0;
5245 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
5246 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
5247 return -EINVAL;
5248 }
5249 for (i = 0; i < imux->num_items; i++) {
5250 if (!strncmp(label, imux->items[i].label, strlen(label)))
5251 label_idx++;
5252 }
5253 if (type_idx)
5254 *type_idx = label_idx;
5255 if (label_idx > 0)
5256 snprintf(imux->items[imux->num_items].label,
5257 sizeof(imux->items[imux->num_items].label),
5258 "%s %d", label, label_idx);
5259 else
5260 strlcpy(imux->items[imux->num_items].label, label,
5261 sizeof(imux->items[imux->num_items].label));
5262 imux->items[imux->num_items].index = index;
5263 imux->num_items++;
5264 return 0;
5265}
5266EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005267
Takashi Iwai4a471b72005-12-07 13:56:29 +01005268
Linus Torvalds1da177e2005-04-16 15:20:36 -07005269#ifdef CONFIG_PM
5270/*
5271 * power management
5272 */
5273
5274/**
5275 * snd_hda_suspend - suspend the codecs
5276 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005277 *
5278 * Returns 0 if successful.
5279 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02005280int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005281{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005282 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005283
Takashi Iwai0ba21762007-04-16 11:29:14 +02005284 list_for_each_entry(codec, &bus->codec_list, list) {
David Henningsson26a6cb62012-10-09 15:04:21 +02005285 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005286 if (hda_codec_is_power_on(codec))
Dylan Reidd17344b2012-09-28 15:57:01 -07005287 hda_call_codec_suspend(codec, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005288 }
5289 return 0;
5290}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005291EXPORT_SYMBOL_HDA(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292
5293/**
5294 * snd_hda_resume - resume the codecs
5295 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005296 *
5297 * Returns 0 if successful.
5298 */
5299int snd_hda_resume(struct hda_bus *bus)
5300{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005301 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005302
Takashi Iwai0ba21762007-04-16 11:29:14 +02005303 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai7f308302012-05-08 16:52:23 +02005304 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005306 return 0;
5307}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005308EXPORT_SYMBOL_HDA(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005309#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005310
5311/*
5312 * generic arrays
5313 */
5314
Takashi Iwaid5191e52009-11-16 14:58:17 +01005315/**
5316 * snd_array_new - get a new element from the given array
5317 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005318 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01005319 * Get a new element from the given array. If it exceeds the
5320 * pre-allocated array size, re-allocate the array.
5321 *
5322 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02005323 */
5324void *snd_array_new(struct snd_array *array)
5325{
Takashi Iwai12f17712012-10-10 08:59:14 +02005326 if (snd_BUG_ON(!array->elem_size))
5327 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005328 if (array->used >= array->alloced) {
5329 int num = array->alloced + array->alloc_align;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005330 int size = (num + 1) * array->elem_size;
Takashi Iwai00ef9612011-07-14 15:57:27 +02005331 int oldsize = array->alloced * array->elem_size;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01005332 void *nlist;
5333 if (snd_BUG_ON(num >= 4096))
5334 return NULL;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005335 nlist = krealloc(array->list, size, GFP_KERNEL);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005336 if (!nlist)
5337 return NULL;
Takashi Iwai00ef9612011-07-14 15:57:27 +02005338 memset(nlist + oldsize, 0, size - oldsize);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005339 array->list = nlist;
5340 array->alloced = num;
5341 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01005342 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005343}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005344EXPORT_SYMBOL_HDA(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005345
Takashi Iwaid5191e52009-11-16 14:58:17 +01005346/**
5347 * snd_array_free - free the given array elements
5348 * @array: the array object
5349 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005350void snd_array_free(struct snd_array *array)
5351{
5352 kfree(array->list);
5353 array->used = 0;
5354 array->alloced = 0;
5355 array->list = NULL;
5356}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005357EXPORT_SYMBOL_HDA(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01005358
Takashi Iwaid5191e52009-11-16 14:58:17 +01005359/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01005360 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5361 * @pcm: PCM caps bits
5362 * @buf: the string buffer to write
5363 * @buflen: the max buffer length
5364 *
5365 * used by hda_proc.c and hda_eld.c
5366 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005367void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5368{
5369 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5370 int i, j;
5371
5372 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5373 if (pcm & (AC_SUPPCM_BITS_8 << i))
5374 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5375
5376 buf[j] = '\0'; /* necessary when j == 0 */
5377}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005378EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005379
5380MODULE_DESCRIPTION("HDA codec core");
5381MODULE_LICENSE("GPL");