blob: f4a0f9d3fd84aa0b1b5e2139abd007c6937330ee [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 Iwai3bcce5c2012-12-20 11:17:17 +0100225 for (;;) {
226 trace_hda_send_cmd(codec, cmd);
227 err = bus->ops.command(bus, cmd);
228 if (err != -EAGAIN)
229 break;
230 /* process pending verbs */
231 bus->ops.get_response(bus, codec->addr);
232 }
Takashi Iwaid66fee52011-08-02 15:39:31 +0200233 if (!err && res) {
Wu Fengguangdeadff12009-08-01 18:45:16 +0800234 *res = bus->ops.get_response(bus, codec->addr);
Takashi Iwaid66fee52011-08-02 15:39:31 +0200235 trace_hda_get_response(codec, *res);
236 }
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200237 mutex_unlock(&bus->cmd_mutex);
238 snd_hda_power_down(codec);
Takashi Iwaid846b172012-11-24 11:58:24 +0100239 if (!codec_in_pm(codec) && res && *res == -1 && bus->rirb_error) {
Takashi Iwai8dd78332009-06-02 01:16:07 +0200240 if (bus->response_reset) {
241 snd_printd("hda_codec: resetting BUS due to "
242 "fatal communication error\n");
Takashi Iwaid66fee52011-08-02 15:39:31 +0200243 trace_hda_bus_reset(bus);
Takashi Iwai8dd78332009-06-02 01:16:07 +0200244 bus->ops.bus_reset(bus);
245 }
246 goto again;
247 }
248 /* clear reset-flag when the communication gets recovered */
Takashi Iwaid846b172012-11-24 11:58:24 +0100249 if (!err || codec_in_pm(codec))
Takashi Iwai8dd78332009-06-02 01:16:07 +0200250 bus->response_reset = 0;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200251 return err;
252}
253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254/**
255 * snd_hda_codec_read - send a command and get the response
256 * @codec: the HDA codec
257 * @nid: NID to send the command
258 * @direct: direct flag
259 * @verb: the verb to send
260 * @parm: the parameter for the verb
261 *
262 * Send a single command and read the corresponding response.
263 *
264 * Returns the obtained response value, or -1 for an error.
265 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200266unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
267 int direct,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 unsigned int verb, unsigned int parm)
269{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200270 unsigned cmd = make_codec_cmd(codec, nid, direct, verb, parm);
271 unsigned int res;
Greg Thelen9857edf2011-06-13 07:45:45 -0700272 if (codec_exec_verb(codec, cmd, &res))
273 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 return res;
275}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100276EXPORT_SYMBOL_HDA(snd_hda_codec_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278/**
279 * snd_hda_codec_write - send a single command without waiting for response
280 * @codec: the HDA codec
281 * @nid: NID to send the command
282 * @direct: direct flag
283 * @verb: the verb to send
284 * @parm: the parameter for the verb
285 *
286 * Send a single command without waiting for response.
287 *
288 * Returns 0 if successful, or a negative error code.
289 */
290int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
291 unsigned int verb, unsigned int parm)
292{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200293 unsigned int cmd = make_codec_cmd(codec, nid, direct, verb, parm);
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100294 unsigned int res;
Takashi Iwaib20f3b82009-06-02 01:20:22 +0200295 return codec_exec_verb(codec, cmd,
296 codec->bus->sync_write ? &res : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100298EXPORT_SYMBOL_HDA(snd_hda_codec_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300/**
301 * snd_hda_sequence_write - sequence writes
302 * @codec: the HDA codec
303 * @seq: VERB array to send
304 *
305 * Send the commands sequentially from the given array.
306 * The array must be terminated with NID=0.
307 */
308void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
309{
310 for (; seq->nid; seq++)
311 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
312}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100313EXPORT_SYMBOL_HDA(snd_hda_sequence_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315/**
316 * snd_hda_get_sub_nodes - get the range of sub nodes
317 * @codec: the HDA codec
318 * @nid: NID to parse
319 * @start_id: the pointer to store the start NID
320 *
321 * Parse the NID and store the start NID of its sub-nodes.
322 * Returns the number of sub-nodes.
323 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200324int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
325 hda_nid_t *start_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
327 unsigned int parm;
328
329 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
Danny Tholene8a7f132007-09-11 21:41:56 +0200330 if (parm == -1)
331 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 *start_id = (parm >> 16) & 0x7fff;
333 return (int)(parm & 0x7fff);
334}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100335EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200337/* look up the cached results */
338static hda_nid_t *lookup_conn_list(struct snd_array *array, hda_nid_t nid)
339{
340 int i, len;
341 for (i = 0; i < array->used; ) {
342 hda_nid_t *p = snd_array_elem(array, i);
343 if (nid == *p)
344 return p;
345 len = p[1];
346 i += len + 2;
347 }
348 return NULL;
349}
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200350
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200351/* read the connection and add to the cache */
352static int read_and_add_raw_conns(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200353{
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200354 hda_nid_t list[HDA_MAX_CONNECTIONS];
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200355 int len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200356
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200357 len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list));
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200358 if (len < 0)
359 return len;
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200360 return snd_hda_override_conn_list(codec, nid, len, list);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200361}
Takashi Iwaidce20792011-06-24 14:10:28 +0200362
363/**
364 * snd_hda_get_connections - copy connection list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 * @codec: the HDA codec
366 * @nid: NID to parse
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200367 * @conn_list: connection list array; when NULL, checks only the size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 * @max_conns: max. number of connections to store
369 *
370 * Parses the connection list of the given widget and stores the list
371 * of NIDs.
372 *
373 * Returns the number of connections, or a negative error code.
374 */
375int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200376 hda_nid_t *conn_list, int max_conns)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200377{
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200378 struct snd_array *array = &codec->conn_lists;
379 int len;
380 hda_nid_t *p;
381 bool added = false;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200382
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200383 again:
384 mutex_lock(&codec->hash_mutex);
385 len = -1;
386 /* if the connection-list is already cached, read it */
387 p = lookup_conn_list(array, nid);
388 if (p) {
389 len = p[1];
390 if (conn_list && len > max_conns) {
391 snd_printk(KERN_ERR "hda_codec: "
392 "Too many connections %d for NID 0x%x\n",
393 len, nid);
394 mutex_unlock(&codec->hash_mutex);
395 return -EINVAL;
396 }
397 if (conn_list && len)
398 memcpy(conn_list, p + 2, len * sizeof(hda_nid_t));
Takashi Iwaidce20792011-06-24 14:10:28 +0200399 }
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200400 mutex_unlock(&codec->hash_mutex);
401 if (len >= 0)
402 return len;
403 if (snd_BUG_ON(added))
404 return -EINVAL;
405
406 len = read_and_add_raw_conns(codec, nid);
407 if (len < 0)
408 return len;
409 added = true;
410 goto again;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200411}
412EXPORT_SYMBOL_HDA(snd_hda_get_connections);
413
Takashi Iwai9e7717c2011-07-11 15:42:52 +0200414/**
415 * snd_hda_get_raw_connections - copy connection list without cache
416 * @codec: the HDA codec
417 * @nid: NID to parse
418 * @conn_list: connection list array
419 * @max_conns: max. number of connections to store
420 *
421 * Like snd_hda_get_connections(), copy the connection list but without
422 * checking through the connection-list cache.
423 * Currently called only from hda_proc.c, so not exported.
424 */
425int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid,
426 hda_nid_t *conn_list, int max_conns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
428 unsigned int parm;
Takashi Iwai54d17402005-11-21 16:33:22 +0100429 int i, conn_len, conns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 unsigned int shift, num_elems, mask;
Takashi Iwai1ba7a7c2009-07-27 12:56:26 +0200431 unsigned int wcaps;
Takashi Iwai54d17402005-11-21 16:33:22 +0100432 hda_nid_t prev_nid;
Takashi Iwai5fdaecd2012-12-20 10:45:55 +0100433 int null_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Takashi Iwaida3cec32008-08-08 17:12:14 +0200435 if (snd_BUG_ON(!conn_list || max_conns <= 0))
436 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Takashi Iwai1ba7a7c2009-07-27 12:56:26 +0200438 wcaps = get_wcaps(codec, nid);
439 if (!(wcaps & AC_WCAP_CONN_LIST) &&
Takashi Iwai8d087c72011-06-28 12:45:47 +0200440 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
441 return 0;
Jaroslav Kysela16a433d2009-07-22 16:20:40 +0200442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
444 if (parm & AC_CLIST_LONG) {
445 /* long form */
446 shift = 16;
447 num_elems = 2;
448 } else {
449 /* short form */
450 shift = 8;
451 num_elems = 4;
452 }
453 conn_len = parm & AC_CLIST_LENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 mask = (1 << (shift-1)) - 1;
455
Takashi Iwai0ba21762007-04-16 11:29:14 +0200456 if (!conn_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 return 0; /* no connection */
458
459 if (conn_len == 1) {
460 /* single connection */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200461 parm = snd_hda_codec_read(codec, nid, 0,
462 AC_VERB_GET_CONNECT_LIST, 0);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200463 if (parm == -1 && codec->bus->rirb_error)
464 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 conn_list[0] = parm & mask;
466 return 1;
467 }
468
469 /* multi connection */
470 conns = 0;
Takashi Iwai54d17402005-11-21 16:33:22 +0100471 prev_nid = 0;
472 for (i = 0; i < conn_len; i++) {
473 int range_val;
474 hda_nid_t val, n;
475
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200476 if (i % num_elems == 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +0100477 parm = snd_hda_codec_read(codec, nid, 0,
478 AC_VERB_GET_CONNECT_LIST, i);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200479 if (parm == -1 && codec->bus->rirb_error)
480 return -EIO;
481 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200482 range_val = !!(parm & (1 << (shift-1))); /* ranges */
Takashi Iwai54d17402005-11-21 16:33:22 +0100483 val = parm & mask;
Takashi Iwai5fdaecd2012-12-20 10:45:55 +0100484 if (val == 0 && null_count++) { /* no second chance */
Jaroslav Kysela2e9bf242009-07-18 11:48:19 +0200485 snd_printk(KERN_WARNING "hda_codec: "
486 "invalid CONNECT_LIST verb %x[%i]:%x\n",
487 nid, i, parm);
488 return 0;
489 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100490 parm >>= shift;
491 if (range_val) {
492 /* ranges between the previous and this one */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200493 if (!prev_nid || prev_nid >= val) {
494 snd_printk(KERN_WARNING "hda_codec: "
495 "invalid dep_range_val %x:%x\n",
496 prev_nid, val);
Takashi Iwai54d17402005-11-21 16:33:22 +0100497 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100499 for (n = prev_nid + 1; n <= val; n++) {
500 if (conns >= max_conns) {
Takashi Iwai5aacc212010-07-30 10:36:29 +0200501 snd_printk(KERN_ERR "hda_codec: "
502 "Too many connections %d for NID 0x%x\n",
503 conns, nid);
Takashi Iwai54d17402005-11-21 16:33:22 +0100504 return -EINVAL;
505 }
506 conn_list[conns++] = n;
507 }
508 } else {
509 if (conns >= max_conns) {
Takashi Iwai5aacc212010-07-30 10:36:29 +0200510 snd_printk(KERN_ERR "hda_codec: "
511 "Too many connections %d for NID 0x%x\n",
512 conns, nid);
Takashi Iwai54d17402005-11-21 16:33:22 +0100513 return -EINVAL;
514 }
515 conn_list[conns++] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100517 prev_nid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 }
519 return conns;
520}
521
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200522static bool add_conn_list(struct snd_array *array, hda_nid_t nid)
523{
524 hda_nid_t *p = snd_array_new(array);
525 if (!p)
526 return false;
527 *p = nid;
528 return true;
529}
530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531/**
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200532 * snd_hda_override_conn_list - add/modify the connection-list to cache
533 * @codec: the HDA codec
534 * @nid: NID to parse
535 * @len: number of connection list entries
536 * @list: the list of connection entries
537 *
538 * Add or modify the given connection-list to the cache. If the corresponding
539 * cache already exists, invalidate it and append a new one.
540 *
541 * Returns zero or a negative error code.
542 */
543int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
544 const hda_nid_t *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200546 struct snd_array *array = &codec->conn_lists;
547 hda_nid_t *p;
548 int i, old_used;
549
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200550 mutex_lock(&codec->hash_mutex);
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200551 p = lookup_conn_list(array, nid);
552 if (p)
553 *p = -1; /* invalidate the old entry */
554
555 old_used = array->used;
556 if (!add_conn_list(array, nid) || !add_conn_list(array, len))
557 goto error_add;
558 for (i = 0; i < len; i++)
559 if (!add_conn_list(array, list[i]))
560 goto error_add;
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200561 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200562 return 0;
563
564 error_add:
565 array->used = old_used;
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200566 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200567 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568}
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200569EXPORT_SYMBOL_HDA(snd_hda_override_conn_list);
570
571/**
Takashi Iwai8d087c72011-06-28 12:45:47 +0200572 * snd_hda_get_conn_index - get the connection index of the given NID
573 * @codec: the HDA codec
574 * @mux: NID containing the list
575 * @nid: NID to select
576 * @recursive: 1 when searching NID recursively, otherwise 0
577 *
578 * Parses the connection list of the widget @mux and checks whether the
579 * widget @nid is present. If it is, return the connection index.
580 * Otherwise it returns -1.
581 */
582int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
583 hda_nid_t nid, int recursive)
584{
585 hda_nid_t conn[HDA_MAX_NUM_INPUTS];
586 int i, nums;
587
588 nums = snd_hda_get_connections(codec, mux, conn, ARRAY_SIZE(conn));
589 for (i = 0; i < nums; i++)
590 if (conn[i] == nid)
591 return i;
592 if (!recursive)
593 return -1;
Takashi Iwaid94ddd82012-12-20 14:42:42 +0100594 if (recursive > 10) {
Takashi Iwai8d087c72011-06-28 12:45:47 +0200595 snd_printd("hda_codec: too deep connection for 0x%x\n", nid);
596 return -1;
597 }
598 recursive++;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200599 for (i = 0; i < nums; i++) {
600 unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
601 if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
602 continue;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200603 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
604 return i;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200605 }
Takashi Iwai8d087c72011-06-28 12:45:47 +0200606 return -1;
607}
608EXPORT_SYMBOL_HDA(snd_hda_get_conn_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610/**
611 * snd_hda_queue_unsol_event - add an unsolicited event to queue
612 * @bus: the BUS
613 * @res: unsolicited event (lower 32bit of RIRB entry)
614 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
615 *
616 * Adds the given event to the queue. The events are processed in
617 * the workqueue asynchronously. Call this function in the interrupt
618 * hanlder when RIRB receives an unsolicited event.
619 *
620 * Returns 0 if successful, or a negative error code.
621 */
622int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
623{
624 struct hda_bus_unsolicited *unsol;
625 unsigned int wp;
626
Takashi Iwaiecf726f2011-08-09 14:22:44 +0200627 trace_hda_unsol_event(bus, res, res_ex);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200628 unsol = bus->unsol;
629 if (!unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 return 0;
631
632 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
633 unsol->wp = wp;
634
635 wp <<= 1;
636 unsol->queue[wp] = res;
637 unsol->queue[wp + 1] = res_ex;
638
Takashi Iwai6acaed32009-01-12 10:09:24 +0100639 queue_work(bus->workq, &unsol->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
641 return 0;
642}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100643EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
645/*
Wu Fengguang5c1d1a92008-10-07 14:17:53 +0800646 * process queued unsolicited events
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 */
David Howellsc4028952006-11-22 14:57:56 +0000648static void process_unsol_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649{
David Howellsc4028952006-11-22 14:57:56 +0000650 struct hda_bus_unsolicited *unsol =
651 container_of(work, struct hda_bus_unsolicited, work);
652 struct hda_bus *bus = unsol->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 struct hda_codec *codec;
654 unsigned int rp, caddr, res;
655
656 while (unsol->rp != unsol->wp) {
657 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
658 unsol->rp = rp;
659 rp <<= 1;
660 res = unsol->queue[rp];
661 caddr = unsol->queue[rp + 1];
Takashi Iwai0ba21762007-04-16 11:29:14 +0200662 if (!(caddr & (1 << 4))) /* no unsolicited event? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 continue;
664 codec = bus->caddr_tbl[caddr & 0x0f];
665 if (codec && codec->patch_ops.unsol_event)
666 codec->patch_ops.unsol_event(codec, res);
667 }
668}
669
670/*
671 * initialize unsolicited queue
672 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200673static int init_unsol_queue(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
675 struct hda_bus_unsolicited *unsol;
676
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100677 if (bus->unsol) /* already initialized */
678 return 0;
679
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200680 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200681 if (!unsol) {
682 snd_printk(KERN_ERR "hda_codec: "
683 "can't allocate unsolicited queue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 return -ENOMEM;
685 }
David Howellsc4028952006-11-22 14:57:56 +0000686 INIT_WORK(&unsol->work, process_unsol_events);
687 unsol->bus = bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 bus->unsol = unsol;
689 return 0;
690}
691
692/*
693 * destructor
694 */
695static void snd_hda_codec_free(struct hda_codec *codec);
696
697static int snd_hda_bus_free(struct hda_bus *bus)
698{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200699 struct hda_codec *codec, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Takashi Iwai0ba21762007-04-16 11:29:14 +0200701 if (!bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 return 0;
Takashi Iwai6acaed32009-01-12 10:09:24 +0100703 if (bus->workq)
704 flush_workqueue(bus->workq);
705 if (bus->unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 kfree(bus->unsol);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200707 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 snd_hda_codec_free(codec);
709 }
710 if (bus->ops.private_free)
711 bus->ops.private_free(bus);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100712 if (bus->workq)
713 destroy_workqueue(bus->workq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 kfree(bus);
715 return 0;
716}
717
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100718static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
720 struct hda_bus *bus = device->device_data;
Takashi Iwaib94d35392008-11-21 09:08:06 +0100721 bus->shutdown = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 return snd_hda_bus_free(bus);
723}
724
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200725#ifdef CONFIG_SND_HDA_HWDEP
726static int snd_hda_bus_dev_register(struct snd_device *device)
727{
728 struct hda_bus *bus = device->device_data;
729 struct hda_codec *codec;
730 list_for_each_entry(codec, &bus->codec_list, list) {
731 snd_hda_hwdep_add_sysfs(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +0100732 snd_hda_hwdep_add_power_sysfs(codec);
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200733 }
734 return 0;
735}
736#else
737#define snd_hda_bus_dev_register NULL
738#endif
739
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740/**
741 * snd_hda_bus_new - create a HDA bus
742 * @card: the card entry
743 * @temp: the template for hda_bus information
744 * @busp: the pointer to store the created bus instance
745 *
746 * Returns 0 if successful, or a negative error code.
747 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +0100748int snd_hda_bus_new(struct snd_card *card,
Takashi Iwai756e2b02007-04-16 11:27:07 +0200749 const struct hda_bus_template *temp,
750 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751{
752 struct hda_bus *bus;
753 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100754 static struct snd_device_ops dev_ops = {
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200755 .dev_register = snd_hda_bus_dev_register,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 .dev_free = snd_hda_bus_dev_free,
757 };
758
Takashi Iwaida3cec32008-08-08 17:12:14 +0200759 if (snd_BUG_ON(!temp))
760 return -EINVAL;
761 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
762 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
764 if (busp)
765 *busp = NULL;
766
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200767 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 if (bus == NULL) {
769 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
770 return -ENOMEM;
771 }
772
773 bus->card = card;
774 bus->private_data = temp->private_data;
775 bus->pci = temp->pci;
776 bus->modelname = temp->modelname;
Takashi Iwaifee2fba2008-11-27 12:43:28 +0100777 bus->power_save = temp->power_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 bus->ops = temp->ops;
779
Ingo Molnar62932df2006-01-16 16:34:20 +0100780 mutex_init(&bus->cmd_mutex);
Takashi Iwai3f50ac62010-08-20 09:44:36 +0200781 mutex_init(&bus->prepare_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 INIT_LIST_HEAD(&bus->codec_list);
783
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100784 snprintf(bus->workq_name, sizeof(bus->workq_name),
785 "hd-audio%d", card->number);
786 bus->workq = create_singlethread_workqueue(bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100787 if (!bus->workq) {
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100788 snd_printk(KERN_ERR "cannot create workqueue %s\n",
789 bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100790 kfree(bus);
791 return -ENOMEM;
792 }
793
Takashi Iwai0ba21762007-04-16 11:29:14 +0200794 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
795 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 snd_hda_bus_free(bus);
797 return err;
798 }
799 if (busp)
800 *busp = bus;
801 return 0;
802}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100803EXPORT_SYMBOL_HDA(snd_hda_bus_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Takashi Iwai82467612007-07-27 19:15:54 +0200805#ifdef CONFIG_SND_HDA_GENERIC
806#define is_generic_config(codec) \
Takashi Iwaif44ac832008-07-30 15:01:45 +0200807 (codec->modelname && !strcmp(codec->modelname, "generic"))
Takashi Iwai82467612007-07-27 19:15:54 +0200808#else
809#define is_generic_config(codec) 0
810#endif
811
Takashi Iwai645f10c2008-11-28 15:07:37 +0100812#ifdef MODULE
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100813#define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
814#else
Takashi Iwai645f10c2008-11-28 15:07:37 +0100815#define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100816#endif
817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818/*
819 * find a matching codec preset
820 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200821static const struct hda_codec_preset *
Takashi Iwai756e2b02007-04-16 11:27:07 +0200822find_codec_preset(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823{
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100824 struct hda_codec_preset_list *tbl;
825 const struct hda_codec_preset *preset;
Takashi Iwai5d908ab2012-08-24 18:40:10 +0200826 unsigned int mod_requested = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
Takashi Iwai82467612007-07-27 19:15:54 +0200828 if (is_generic_config(codec))
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100829 return NULL; /* use the generic parser */
830
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100831 again:
832 mutex_lock(&preset_mutex);
833 list_for_each_entry(tbl, &hda_preset_tables, list) {
834 if (!try_module_get(tbl->owner)) {
835 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
836 continue;
837 }
838 for (preset = tbl->preset; preset->id; preset++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 u32 mask = preset->mask;
Marc Boucherca7cfae2008-01-22 15:32:25 +0100840 if (preset->afg && preset->afg != codec->afg)
841 continue;
842 if (preset->mfg && preset->mfg != codec->mfg)
843 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200844 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 mask = ~0;
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200846 if (preset->id == (codec->vendor_id & mask) &&
Takashi Iwai0ba21762007-04-16 11:29:14 +0200847 (!preset->rev ||
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100848 preset->rev == codec->revision_id)) {
849 mutex_unlock(&preset_mutex);
850 codec->owner = tbl->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 return preset;
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100852 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 }
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100854 module_put(tbl->owner);
855 }
856 mutex_unlock(&preset_mutex);
857
858 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
859 char name[32];
860 if (!mod_requested)
861 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
862 codec->vendor_id);
863 else
864 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
865 (codec->vendor_id >> 16) & 0xffff);
866 request_module(name);
867 mod_requested++;
868 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 }
870 return NULL;
871}
872
873/*
Takashi Iwaif44ac832008-07-30 15:01:45 +0200874 * get_codec_name - store the codec name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 */
Takashi Iwaif44ac832008-07-30 15:01:45 +0200876static int get_codec_name(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
878 const struct hda_vendor_id *c;
879 const char *vendor = NULL;
880 u16 vendor_id = codec->vendor_id >> 16;
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200881 char tmp[16];
882
883 if (codec->vendor_name)
884 goto get_chip_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
886 for (c = hda_vendor_ids; c->id; c++) {
887 if (c->id == vendor_id) {
888 vendor = c->name;
889 break;
890 }
891 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200892 if (!vendor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 sprintf(tmp, "Generic %04x", vendor_id);
894 vendor = tmp;
895 }
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200896 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
897 if (!codec->vendor_name)
898 return -ENOMEM;
899
900 get_chip_name:
901 if (codec->chip_name)
902 return 0;
903
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 if (codec->preset && codec->preset->name)
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200905 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
906 else {
907 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
908 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
909 }
910 if (!codec->chip_name)
Takashi Iwaif44ac832008-07-30 15:01:45 +0200911 return -ENOMEM;
912 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913}
914
915/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200916 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +0100918static void setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919{
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200920 int i, total_nodes, function_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 hda_nid_t nid;
922
923 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
924 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200925 function_id = snd_hda_param_read(codec, nid,
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200926 AC_PAR_FUNCTION_TYPE);
Jaroslav Kyselacd7643b2010-07-20 12:11:25 +0200927 switch (function_id & 0xff) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200928 case AC_GRP_AUDIO_FUNCTION:
929 codec->afg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200930 codec->afg_function_id = function_id & 0xff;
931 codec->afg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200932 break;
933 case AC_GRP_MODEM_FUNCTION:
934 codec->mfg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200935 codec->mfg_function_id = function_id & 0xff;
936 codec->mfg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200937 break;
938 default:
939 break;
940 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942}
943
944/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100945 * read widget caps for each widget and store in cache
946 */
947static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
948{
949 int i;
950 hda_nid_t nid;
951
952 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
953 &codec->start_nid);
954 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200955 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +0100956 return -ENOMEM;
957 nid = codec->start_nid;
958 for (i = 0; i < codec->num_nodes; i++, nid++)
959 codec->wcaps[i] = snd_hda_param_read(codec, nid,
960 AC_PAR_AUDIO_WIDGET_CAP);
961 return 0;
962}
963
Takashi Iwai3be14142009-02-20 14:11:16 +0100964/* read all pin default configurations and save codec->init_pins */
965static int read_pin_defaults(struct hda_codec *codec)
966{
967 int i;
968 hda_nid_t nid = codec->start_nid;
969
970 for (i = 0; i < codec->num_nodes; i++, nid++) {
971 struct hda_pincfg *pin;
972 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +0200973 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwai3be14142009-02-20 14:11:16 +0100974 if (wid_type != AC_WID_PIN)
975 continue;
976 pin = snd_array_new(&codec->init_pins);
977 if (!pin)
978 return -ENOMEM;
979 pin->nid = nid;
980 pin->cfg = snd_hda_codec_read(codec, nid, 0,
981 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200982 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
983 AC_VERB_GET_PIN_WIDGET_CONTROL,
984 0);
Takashi Iwai3be14142009-02-20 14:11:16 +0100985 }
986 return 0;
987}
988
989/* look up the given pin config list and return the item matching with NID */
990static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
991 struct snd_array *array,
992 hda_nid_t nid)
993{
994 int i;
995 for (i = 0; i < array->used; i++) {
996 struct hda_pincfg *pin = snd_array_elem(array, i);
997 if (pin->nid == nid)
998 return pin;
999 }
1000 return NULL;
1001}
1002
Takashi Iwai3be14142009-02-20 14:11:16 +01001003/* set the current pin config value for the given NID.
1004 * the value is cached, and read via snd_hda_codec_get_pincfg()
1005 */
1006int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
1007 hda_nid_t nid, unsigned int cfg)
1008{
1009 struct hda_pincfg *pin;
1010
Takashi Iwaib82855a2009-12-27 11:24:56 +01001011 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
1012 return -EINVAL;
1013
Takashi Iwai3be14142009-02-20 14:11:16 +01001014 pin = look_up_pincfg(codec, list, nid);
1015 if (!pin) {
1016 pin = snd_array_new(list);
1017 if (!pin)
1018 return -ENOMEM;
1019 pin->nid = nid;
1020 }
1021 pin->cfg = cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001022 return 0;
1023}
1024
Takashi Iwaid5191e52009-11-16 14:58:17 +01001025/**
1026 * snd_hda_codec_set_pincfg - Override a pin default configuration
1027 * @codec: the HDA codec
1028 * @nid: NID to set the pin config
1029 * @cfg: the pin default config value
1030 *
1031 * Override a pin default configuration value in the cache.
1032 * This value can be read by snd_hda_codec_get_pincfg() in a higher
1033 * priority than the real hardware value.
1034 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001035int snd_hda_codec_set_pincfg(struct hda_codec *codec,
1036 hda_nid_t nid, unsigned int cfg)
1037{
Takashi Iwai346ff702009-02-23 09:42:57 +01001038 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001039}
1040EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
1041
Takashi Iwaid5191e52009-11-16 14:58:17 +01001042/**
1043 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
1044 * @codec: the HDA codec
1045 * @nid: NID to get the pin config
1046 *
1047 * Get the current pin config value of the given pin NID.
1048 * If the pincfg value is cached or overridden via sysfs or driver,
1049 * returns the cached value.
1050 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001051unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
1052{
1053 struct hda_pincfg *pin;
1054
Takashi Iwai3be14142009-02-20 14:11:16 +01001055#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +01001056 pin = look_up_pincfg(codec, &codec->user_pins, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +01001057 if (pin)
1058 return pin->cfg;
1059#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001060 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
1061 if (pin)
1062 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001063 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1064 if (pin)
1065 return pin->cfg;
1066 return 0;
1067}
1068EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
1069
Takashi Iwai92ee6162009-12-27 11:18:59 +01001070/**
1071 * snd_hda_shutup_pins - Shut up all pins
1072 * @codec: the HDA codec
1073 *
1074 * Clear all pin controls to shup up before suspend for avoiding click noise.
1075 * The controls aren't cached so that they can be resumed properly.
1076 */
1077void snd_hda_shutup_pins(struct hda_codec *codec)
1078{
1079 int i;
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001080 /* don't shut up pins when unloading the driver; otherwise it breaks
1081 * the default pin setup at the next load of the driver
1082 */
1083 if (codec->bus->shutdown)
1084 return;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001085 for (i = 0; i < codec->init_pins.used; i++) {
1086 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1087 /* use read here for syncing after issuing each verb */
1088 snd_hda_codec_read(codec, pin->nid, 0,
1089 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
1090 }
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001091 codec->pins_shutup = 1;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001092}
1093EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);
1094
Takashi Iwai2a439522011-07-26 09:52:50 +02001095#ifdef CONFIG_PM
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001096/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1097static void restore_shutup_pins(struct hda_codec *codec)
1098{
1099 int i;
1100 if (!codec->pins_shutup)
1101 return;
1102 if (codec->bus->shutdown)
1103 return;
1104 for (i = 0; i < codec->init_pins.used; i++) {
1105 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1106 snd_hda_codec_write(codec, pin->nid, 0,
1107 AC_VERB_SET_PIN_WIDGET_CONTROL,
1108 pin->ctrl);
1109 }
1110 codec->pins_shutup = 0;
1111}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001112#endif
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001113
David Henningsson26a6cb62012-10-09 15:04:21 +02001114static void hda_jackpoll_work(struct work_struct *work)
1115{
1116 struct hda_codec *codec =
1117 container_of(work, struct hda_codec, jackpoll_work.work);
1118 if (!codec->jackpoll_interval)
1119 return;
1120
1121 snd_hda_jack_set_dirty_all(codec);
1122 snd_hda_jack_poll_all(codec);
1123 queue_delayed_work(codec->bus->workq, &codec->jackpoll_work,
1124 codec->jackpoll_interval);
1125}
1126
Takashi Iwai01751f52007-08-10 16:59:39 +02001127static void init_hda_cache(struct hda_cache_rec *cache,
1128 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001129static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +02001130
Takashi Iwai3fdf1462012-11-22 08:16:31 +01001131/* release all pincfg lists */
1132static void free_init_pincfgs(struct hda_codec *codec)
Takashi Iwai3be14142009-02-20 14:11:16 +01001133{
Takashi Iwai346ff702009-02-23 09:42:57 +01001134 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001135#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +01001136 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001137#endif
Takashi Iwai3be14142009-02-20 14:11:16 +01001138 snd_array_free(&codec->init_pins);
1139}
1140
Takashi Iwai54d17402005-11-21 16:33:22 +01001141/*
Takashi Iwaieb541332010-08-06 13:48:11 +02001142 * audio-converter setup caches
1143 */
1144struct hda_cvt_setup {
1145 hda_nid_t nid;
1146 u8 stream_tag;
1147 u8 channel_id;
1148 u16 format_id;
1149 unsigned char active; /* cvt is currently used */
1150 unsigned char dirty; /* setups should be cleared */
1151};
1152
1153/* get or create a cache entry for the given audio converter NID */
1154static struct hda_cvt_setup *
1155get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1156{
1157 struct hda_cvt_setup *p;
1158 int i;
1159
1160 for (i = 0; i < codec->cvt_setups.used; i++) {
1161 p = snd_array_elem(&codec->cvt_setups, i);
1162 if (p->nid == nid)
1163 return p;
1164 }
1165 p = snd_array_new(&codec->cvt_setups);
1166 if (p)
1167 p->nid = nid;
1168 return p;
1169}
1170
1171/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 * codec destructor
1173 */
1174static void snd_hda_codec_free(struct hda_codec *codec)
1175{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001176 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 return;
David Henningsson26a6cb62012-10-09 15:04:21 +02001178 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai59cad162012-06-26 15:00:20 +02001179 snd_hda_jack_tbl_clear(codec);
Takashi Iwai3fdf1462012-11-22 08:16:31 +01001180 free_init_pincfgs(codec);
Takashi Iwai83012a72012-08-24 18:38:08 +02001181#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02001182 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01001183 flush_workqueue(codec->bus->workq);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001184#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001186 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001187 snd_array_free(&codec->nids);
Takashi Iwai59cad162012-06-26 15:00:20 +02001188 snd_array_free(&codec->cvt_setups);
Takashi Iwaia12d3e12011-04-07 15:55:15 +02001189 snd_array_free(&codec->conn_lists);
Stephen Warren7c935972011-06-01 11:14:17 -06001190 snd_array_free(&codec->spdif_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 codec->bus->caddr_tbl[codec->addr] = NULL;
1192 if (codec->patch_ops.free)
1193 codec->patch_ops.free(codec);
Takashi Iwai83012a72012-08-24 18:38:08 +02001194#ifdef CONFIG_PM
Takashi Iwai08fa20a2012-08-31 07:46:56 -07001195 if (!codec->pm_down_notified) /* cancel leftover refcounts */
Takashi Iwai68467f52012-08-28 09:14:29 -07001196 hda_call_pm_notify(codec->bus, false);
1197#endif
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001198 module_put(codec->owner);
Takashi Iwai01751f52007-08-10 16:59:39 +02001199 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001200 free_hda_cache(&codec->cmd_cache);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001201 kfree(codec->vendor_name);
1202 kfree(codec->chip_name);
Takashi Iwaif44ac832008-07-30 15:01:45 +02001203 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +01001204 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 kfree(codec);
1206}
1207
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001208static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec,
1209 hda_nid_t fg, unsigned int power_state);
1210
Takashi Iwaid8193872012-08-31 07:54:38 -07001211static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001212 unsigned int power_state);
1213
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214/**
1215 * snd_hda_codec_new - create a HDA codec
1216 * @bus: the bus to assign
1217 * @codec_addr: the codec address
1218 * @codecp: the pointer to store the generated codec
1219 *
1220 * Returns 0 if successful, or a negative error code.
1221 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001222int snd_hda_codec_new(struct hda_bus *bus,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001223 unsigned int codec_addr,
1224 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225{
1226 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +02001227 char component[31];
Takashi Iwaid8193872012-08-31 07:54:38 -07001228 hda_nid_t fg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 int err;
1230
Takashi Iwaida3cec32008-08-08 17:12:14 +02001231 if (snd_BUG_ON(!bus))
1232 return -EINVAL;
1233 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1234 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
1236 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001237 snd_printk(KERN_ERR "hda_codec: "
1238 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 return -EBUSY;
1240 }
1241
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001242 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 if (codec == NULL) {
1244 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1245 return -ENOMEM;
1246 }
1247
1248 codec->bus = bus;
1249 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001250 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001251 mutex_init(&codec->control_mutex);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001252 mutex_init(&codec->hash_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +02001253 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001254 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001255 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1256 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001257 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001258 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +02001259 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Takashi Iwaia12d3e12011-04-07 15:55:15 +02001260 snd_array_init(&codec->conn_lists, sizeof(hda_nid_t), 64);
Stephen Warren7c935972011-06-01 11:14:17 -06001261 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
Takashi Iwai361dab32012-05-09 14:35:27 +02001262 snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01001263 snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8);
David Henningsson26a6cb62012-10-09 15:04:21 +02001264 INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
Takashi Iwai83012a72012-08-24 18:38:08 +02001266#ifdef CONFIG_PM
Takashi Iwai5536c6d2012-05-08 17:08:10 +02001267 spin_lock_init(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001268 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1269 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1270 * the caller has to power down appropriatley after initialization
1271 * phase.
1272 */
1273 hda_keep_power_on(codec);
Takashi Iwai68467f52012-08-28 09:14:29 -07001274 hda_call_pm_notify(bus, true);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001275#endif
1276
Takashi Iwaic382a9f2012-05-07 15:01:02 +02001277 if (codec->bus->modelname) {
1278 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1279 if (!codec->modelname) {
1280 snd_hda_codec_free(codec);
1281 return -ENODEV;
1282 }
1283 }
1284
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 list_add_tail(&codec->list, &bus->codec_list);
1286 bus->caddr_tbl[codec_addr] = codec;
1287
Takashi Iwai0ba21762007-04-16 11:29:14 +02001288 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1289 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001290 if (codec->vendor_id == -1)
1291 /* read again, hopefully the access method was corrected
1292 * in the last read...
1293 */
1294 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1295 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001296 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1297 AC_PAR_SUBSYSTEM_ID);
1298 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1299 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001301 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001302 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001303 snd_printdd("hda_codec: no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001304 err = -ENODEV;
1305 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 }
1307
Takashi Iwaid8193872012-08-31 07:54:38 -07001308 fg = codec->afg ? codec->afg : codec->mfg;
1309 err = read_widget_caps(codec, fg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001310 if (err < 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +01001311 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001312 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001313 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001314 err = read_pin_defaults(codec);
1315 if (err < 0)
1316 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001317
Takashi Iwai0ba21762007-04-16 11:29:14 +02001318 if (!codec->subsystem_id) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001319 codec->subsystem_id =
Takashi Iwaid8193872012-08-31 07:54:38 -07001320 snd_hda_codec_read(codec, fg, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001321 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001322 }
1323
Takashi Iwai83012a72012-08-24 18:38:08 +02001324#ifdef CONFIG_PM
Takashi Iwaid8193872012-08-31 07:54:38 -07001325 codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec, fg,
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001326 AC_PWRST_CLKSTOP);
1327 if (!codec->d3_stop_clk)
1328 bus->power_keep_link_on = 1;
Mengdong Lin5d6147f2012-08-24 12:06:30 +08001329#endif
Takashi Iwaid8193872012-08-31 07:54:38 -07001330 codec->epss = snd_hda_codec_get_supported_ps(codec, fg,
Takashi Iwai983f6b92012-08-28 09:18:01 -07001331 AC_PWRST_EPSS);
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001332
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001333 /* power-up all before initialization */
Takashi Iwaid8193872012-08-31 07:54:38 -07001334 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001335
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001336 snd_hda_codec_proc_new(codec);
1337
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001338 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001339
1340 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1341 codec->subsystem_id, codec->revision_id);
1342 snd_component_add(codec->bus->card, component);
1343
1344 if (codecp)
1345 *codecp = codec;
1346 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001347
1348 error:
1349 snd_hda_codec_free(codec);
1350 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001351}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001352EXPORT_SYMBOL_HDA(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001353
Takashi Iwaid5191e52009-11-16 14:58:17 +01001354/**
1355 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1356 * @codec: the HDA codec
1357 *
1358 * Start parsing of the given codec tree and (re-)initialize the whole
1359 * patch instance.
1360 *
1361 * Returns 0 if successful or a negative error code.
1362 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001363int snd_hda_codec_configure(struct hda_codec *codec)
1364{
1365 int err;
1366
Takashi Iwaid5ad6302007-03-07 15:55:59 +01001367 codec->preset = find_codec_preset(codec);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001368 if (!codec->vendor_name || !codec->chip_name) {
Takashi Iwaif44ac832008-07-30 15:01:45 +02001369 err = get_codec_name(codec);
1370 if (err < 0)
1371 return err;
1372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
Takashi Iwai82467612007-07-27 19:15:54 +02001374 if (is_generic_config(codec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai82467612007-07-27 19:15:54 +02001376 goto patched;
1377 }
Takashi Iwai82467612007-07-27 19:15:54 +02001378 if (codec->preset && codec->preset->patch) {
1379 err = codec->preset->patch(codec);
1380 goto patched;
1381 }
1382
1383 /* call the default parser */
Takashi Iwai82467612007-07-27 19:15:54 +02001384 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai35a1e0c2007-10-19 08:13:40 +02001385 if (err < 0)
1386 printk(KERN_ERR "hda-codec: No codec parser is available\n");
Takashi Iwai82467612007-07-27 19:15:54 +02001387
1388 patched:
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001389 if (!err && codec->patch_ops.unsol_event)
1390 err = init_unsol_queue(codec->bus);
Takashi Iwaif62faed2009-12-23 09:27:51 +01001391 /* audio codec should override the mixer name */
1392 if (!err && (codec->afg || !*codec->bus->card->mixername))
1393 snprintf(codec->bus->card->mixername,
1394 sizeof(codec->bus->card->mixername),
1395 "%s %s", codec->vendor_name, codec->chip_name);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001396 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397}
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001398EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
Takashi Iwaied360812012-08-08 17:12:52 +02001400/* update the stream-id if changed */
1401static void update_pcm_stream_id(struct hda_codec *codec,
1402 struct hda_cvt_setup *p, hda_nid_t nid,
1403 u32 stream_tag, int channel_id)
1404{
1405 unsigned int oldval, newval;
1406
1407 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1408 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1409 newval = (stream_tag << 4) | channel_id;
1410 if (oldval != newval)
1411 snd_hda_codec_write(codec, nid, 0,
1412 AC_VERB_SET_CHANNEL_STREAMID,
1413 newval);
1414 p->stream_tag = stream_tag;
1415 p->channel_id = channel_id;
1416 }
1417}
1418
1419/* update the format-id if changed */
1420static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1421 hda_nid_t nid, int format)
1422{
1423 unsigned int oldval;
1424
1425 if (p->format_id != format) {
1426 oldval = snd_hda_codec_read(codec, nid, 0,
1427 AC_VERB_GET_STREAM_FORMAT, 0);
1428 if (oldval != format) {
1429 msleep(1);
1430 snd_hda_codec_write(codec, nid, 0,
1431 AC_VERB_SET_STREAM_FORMAT,
1432 format);
1433 }
1434 p->format_id = format;
1435 }
1436}
1437
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438/**
1439 * snd_hda_codec_setup_stream - set up the codec for streaming
1440 * @codec: the CODEC to set up
1441 * @nid: the NID to set up
1442 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1443 * @channel_id: channel id to pass, zero based.
1444 * @format: stream format.
1445 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001446void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1447 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 int channel_id, int format)
1449{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001450 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001451 struct hda_cvt_setup *p;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001452 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001453 int i;
1454
Takashi Iwai0ba21762007-04-16 11:29:14 +02001455 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001456 return;
1457
Takashi Iwai0ba21762007-04-16 11:29:14 +02001458 snd_printdd("hda_codec_setup_stream: "
1459 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001461 p = get_hda_cvt_setup(codec, nid);
1462 if (!p)
1463 return;
Takashi Iwaied360812012-08-08 17:12:52 +02001464
1465 if (codec->pcm_format_first)
1466 update_pcm_format(codec, p, nid, format);
1467 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1468 if (!codec->pcm_format_first)
1469 update_pcm_format(codec, p, nid, format);
1470
Takashi Iwaieb541332010-08-06 13:48:11 +02001471 p->active = 1;
1472 p->dirty = 0;
1473
1474 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001475 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001476 list_for_each_entry(c, &codec->bus->codec_list, list) {
1477 for (i = 0; i < c->cvt_setups.used; i++) {
1478 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001479 if (!p->active && p->stream_tag == stream_tag &&
David Henningsson54c2a892012-02-01 12:05:41 +01001480 get_wcaps_type(get_wcaps(c, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001481 p->dirty = 1;
1482 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001483 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001485EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
Takashi Iwaif0cea792010-08-13 11:56:53 +02001487static void really_cleanup_stream(struct hda_codec *codec,
1488 struct hda_cvt_setup *q);
1489
Takashi Iwaid5191e52009-11-16 14:58:17 +01001490/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001491 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001492 * @codec: the CODEC to clean up
1493 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001494 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001495 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001496void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1497 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001498{
Takashi Iwaieb541332010-08-06 13:48:11 +02001499 struct hda_cvt_setup *p;
1500
Takashi Iwai888afa12008-03-18 09:57:50 +01001501 if (!nid)
1502 return;
1503
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001504 if (codec->no_sticky_stream)
1505 do_now = 1;
1506
Takashi Iwai888afa12008-03-18 09:57:50 +01001507 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001508 p = get_hda_cvt_setup(codec, nid);
Takashi Iwaif0cea792010-08-13 11:56:53 +02001509 if (p) {
1510 /* here we just clear the active flag when do_now isn't set;
1511 * actual clean-ups will be done later in
1512 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1513 */
1514 if (do_now)
1515 really_cleanup_stream(codec, p);
1516 else
1517 p->active = 0;
1518 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001519}
Takashi Iwaif0cea792010-08-13 11:56:53 +02001520EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001521
Takashi Iwaieb541332010-08-06 13:48:11 +02001522static void really_cleanup_stream(struct hda_codec *codec,
1523 struct hda_cvt_setup *q)
1524{
1525 hda_nid_t nid = q->nid;
Takashi Iwai218264a2011-09-27 17:33:45 +02001526 if (q->stream_tag || q->channel_id)
1527 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1528 if (q->format_id)
1529 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1530);
Takashi Iwaieb541332010-08-06 13:48:11 +02001531 memset(q, 0, sizeof(*q));
1532 q->nid = nid;
1533}
1534
1535/* clean up the all conflicting obsolete streams */
1536static void purify_inactive_streams(struct hda_codec *codec)
1537{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001538 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001539 int i;
1540
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001541 list_for_each_entry(c, &codec->bus->codec_list, list) {
1542 for (i = 0; i < c->cvt_setups.used; i++) {
1543 struct hda_cvt_setup *p;
1544 p = snd_array_elem(&c->cvt_setups, i);
1545 if (p->dirty)
1546 really_cleanup_stream(c, p);
1547 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001548 }
1549}
1550
Takashi Iwai2a439522011-07-26 09:52:50 +02001551#ifdef CONFIG_PM
Takashi Iwaieb541332010-08-06 13:48:11 +02001552/* clean up all streams; called from suspend */
1553static void hda_cleanup_all_streams(struct hda_codec *codec)
1554{
1555 int i;
1556
1557 for (i = 0; i < codec->cvt_setups.used; i++) {
1558 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1559 if (p->stream_tag)
1560 really_cleanup_stream(codec, p);
1561 }
1562}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001563#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001564
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565/*
1566 * amp access functions
1567 */
1568
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001569/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001570#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001571#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001572#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1573#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001575#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
1577/* initialize the hash table */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001578static void init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001579 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580{
Takashi Iwai01751f52007-08-10 16:59:39 +02001581 memset(cache, 0, sizeof(*cache));
1582 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001583 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001584}
1585
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001586static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001587{
Takashi Iwai603c4012008-07-30 15:01:44 +02001588 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589}
1590
1591/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001592static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593{
Takashi Iwai01751f52007-08-10 16:59:39 +02001594 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1595 u16 cur = cache->hash[idx];
1596 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
1598 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001599 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 if (info->key == key)
1601 return info;
1602 cur = info->next;
1603 }
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001604 return NULL;
1605}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001607/* query the hash. allocate an entry if not found. */
1608static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1609 u32 key)
1610{
1611 struct hda_cache_head *info = get_hash(cache, key);
1612 if (!info) {
1613 u16 idx, cur;
1614 /* add a new hash entry */
1615 info = snd_array_new(&cache->buf);
1616 if (!info)
1617 return NULL;
1618 cur = snd_array_index(&cache->buf, info);
1619 info->key = key;
1620 info->val = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01001621 info->dirty = 0;
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001622 idx = key % (u16)ARRAY_SIZE(cache->hash);
1623 info->next = cache->hash[idx];
1624 cache->hash[idx] = cur;
1625 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 return info;
1627}
1628
Takashi Iwai01751f52007-08-10 16:59:39 +02001629/* query and allocate an amp hash entry */
1630static inline struct hda_amp_info *
1631get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1632{
1633 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1634}
1635
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001636/* overwrite the value with the key in the caps hash */
1637static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val)
1638{
1639 struct hda_amp_info *info;
1640
1641 mutex_lock(&codec->hash_mutex);
1642 info = get_alloc_amp_hash(codec, key);
1643 if (!info) {
1644 mutex_unlock(&codec->hash_mutex);
1645 return -EINVAL;
1646 }
1647 info->amp_caps = val;
1648 info->head.val |= INFO_AMP_CAPS;
1649 mutex_unlock(&codec->hash_mutex);
1650 return 0;
1651}
1652
1653/* query the value from the caps hash; if not found, fetch the current
1654 * value from the given function and store in the hash
1655 */
1656static unsigned int
1657query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key,
1658 unsigned int (*func)(struct hda_codec *, hda_nid_t, int))
1659{
1660 struct hda_amp_info *info;
1661 unsigned int val;
1662
1663 mutex_lock(&codec->hash_mutex);
1664 info = get_alloc_amp_hash(codec, key);
1665 if (!info) {
1666 mutex_unlock(&codec->hash_mutex);
1667 return 0;
1668 }
1669 if (!(info->head.val & INFO_AMP_CAPS)) {
1670 mutex_unlock(&codec->hash_mutex); /* for reentrance */
1671 val = func(codec, nid, dir);
1672 write_caps_hash(codec, key, val);
1673 } else {
1674 val = info->amp_caps;
1675 mutex_unlock(&codec->hash_mutex);
1676 }
1677 return val;
1678}
1679
1680static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid,
1681 int direction)
1682{
1683 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1684 nid = codec->afg;
1685 return snd_hda_param_read(codec, nid,
1686 direction == HDA_OUTPUT ?
1687 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
1688}
1689
Takashi Iwaid5191e52009-11-16 14:58:17 +01001690/**
1691 * query_amp_caps - query AMP capabilities
1692 * @codec: the HD-auio codec
1693 * @nid: the NID to query
1694 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1695 *
1696 * Query AMP capabilities for the given widget and direction.
1697 * Returns the obtained capability bits.
1698 *
1699 * When cap bits have been already read, this doesn't read again but
1700 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001702u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001704 return query_caps_hash(codec, nid, direction,
1705 HDA_HASH_KEY(nid, direction, 0),
1706 read_amp_cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001708EXPORT_SYMBOL_HDA(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709
Takashi Iwaid5191e52009-11-16 14:58:17 +01001710/**
1711 * snd_hda_override_amp_caps - Override the AMP capabilities
1712 * @codec: the CODEC to clean up
1713 * @nid: the NID to clean up
1714 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1715 * @caps: the capability bits to set
1716 *
1717 * Override the cached AMP caps bits value by the given one.
1718 * This function is useful if the driver needs to adjust the AMP ranges,
1719 * e.g. limit to 0dB, etc.
1720 *
1721 * Returns zero if successful or a negative error code.
1722 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001723int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1724 unsigned int caps)
1725{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001726 return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001727}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001728EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001729
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001730static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid,
1731 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001732{
1733 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1734}
1735
Takashi Iwaid5191e52009-11-16 14:58:17 +01001736/**
1737 * snd_hda_query_pin_caps - Query PIN capabilities
1738 * @codec: the HD-auio codec
1739 * @nid: the NID to query
1740 *
1741 * Query PIN capabilities for the given widget.
1742 * Returns the obtained capability bits.
1743 *
1744 * When cap bits have been already read, this doesn't read again but
1745 * returns the cached value.
1746 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001747u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1748{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001749 return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001750 read_pin_cap);
1751}
Takashi Iwai1327a322009-03-23 13:07:47 +01001752EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1753
Wu Fengguang864f92b2009-11-18 12:38:02 +08001754/**
Takashi Iwaif57c2562011-08-15 12:49:07 +02001755 * snd_hda_override_pin_caps - Override the pin capabilities
1756 * @codec: the CODEC
1757 * @nid: the NID to override
1758 * @caps: the capability bits to set
1759 *
1760 * Override the cached PIN capabilitiy bits value by the given one.
1761 *
1762 * Returns zero if successful or a negative error code.
1763 */
1764int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
1765 unsigned int caps)
1766{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001767 return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps);
Takashi Iwaif57c2562011-08-15 12:49:07 +02001768}
1769EXPORT_SYMBOL_HDA(snd_hda_override_pin_caps);
1770
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001771/* read or sync the hash value with the current value;
1772 * call within hash_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 */
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001774static struct hda_amp_info *
1775update_amp_hash(struct hda_codec *codec, hda_nid_t nid, int ch,
Takashi Iwai280e57d2012-12-14 10:32:21 +01001776 int direction, int index, bool init_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001778 struct hda_amp_info *info;
1779 unsigned int parm, val = 0;
1780 bool val_read = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001782 retry:
1783 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1784 if (!info)
1785 return NULL;
1786 if (!(info->head.val & INFO_AMP_VOL(ch))) {
1787 if (!val_read) {
1788 mutex_unlock(&codec->hash_mutex);
1789 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1790 parm |= direction == HDA_OUTPUT ?
1791 AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1792 parm |= index;
1793 val = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001794 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001795 val &= 0xff;
1796 val_read = true;
1797 mutex_lock(&codec->hash_mutex);
1798 goto retry;
1799 }
1800 info->vol[ch] = val;
1801 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001802 } else if (init_only)
1803 return NULL;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001804 return info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805}
1806
1807/*
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001808 * write the current volume in info to the h/w
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 */
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001810static void put_vol_mute(struct hda_codec *codec, unsigned int amp_caps,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001811 hda_nid_t nid, int ch, int direction, int index,
1812 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813{
1814 u32 parm;
1815
1816 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1817 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1818 parm |= index << AC_AMP_SET_INDEX_SHIFT;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001819 if ((val & HDA_AMP_MUTE) && !(amp_caps & AC_AMPCAP_MUTE) &&
1820 (amp_caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwai38681372012-02-27 15:00:58 +01001821 ; /* set the zero value as a fake mute */
1822 else
1823 parm |= val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
1825}
1826
Takashi Iwaid5191e52009-11-16 14:58:17 +01001827/**
1828 * snd_hda_codec_amp_read - Read AMP value
1829 * @codec: HD-audio codec
1830 * @nid: NID to read the AMP value
1831 * @ch: channel (left=0 or right=1)
1832 * @direction: #HDA_INPUT or #HDA_OUTPUT
1833 * @index: the index value (only for input direction)
1834 *
1835 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 */
Takashi Iwai834be882006-03-01 14:16:17 +01001837int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1838 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001840 struct hda_amp_info *info;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001841 unsigned int val = 0;
1842
1843 mutex_lock(&codec->hash_mutex);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001844 info = update_amp_hash(codec, nid, ch, direction, index, false);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001845 if (info)
1846 val = info->vol[ch];
1847 mutex_unlock(&codec->hash_mutex);
1848 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001850EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
Takashi Iwai280e57d2012-12-14 10:32:21 +01001852static int codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1853 int direction, int idx, int mask, int val,
1854 bool init_only)
1855{
1856 struct hda_amp_info *info;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001857 unsigned int caps;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01001858 unsigned int cache_only;
Takashi Iwai280e57d2012-12-14 10:32:21 +01001859
1860 if (snd_BUG_ON(mask & ~0xff))
1861 mask &= 0xff;
1862 val &= mask;
1863
1864 mutex_lock(&codec->hash_mutex);
1865 info = update_amp_hash(codec, nid, ch, direction, idx, init_only);
1866 if (!info) {
1867 mutex_unlock(&codec->hash_mutex);
1868 return 0;
1869 }
1870 val |= info->vol[ch] & ~mask;
1871 if (info->vol[ch] == val) {
1872 mutex_unlock(&codec->hash_mutex);
1873 return 0;
1874 }
1875 info->vol[ch] = val;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01001876 cache_only = info->head.dirty = codec->cached_write;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001877 caps = info->amp_caps;
Takashi Iwai280e57d2012-12-14 10:32:21 +01001878 mutex_unlock(&codec->hash_mutex);
Takashi Iwaide1e37b2012-12-20 11:00:21 +01001879 if (!cache_only)
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001880 put_vol_mute(codec, caps, nid, ch, direction, idx, val);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001881 return 1;
1882}
1883
Takashi Iwaid5191e52009-11-16 14:58:17 +01001884/**
1885 * snd_hda_codec_amp_update - update the AMP value
1886 * @codec: HD-audio codec
1887 * @nid: NID to read the AMP value
1888 * @ch: channel (left=0 or right=1)
1889 * @direction: #HDA_INPUT or #HDA_OUTPUT
1890 * @idx: the index value (only for input direction)
1891 * @mask: bit mask to set
1892 * @val: the bits value to set
1893 *
1894 * Update the AMP value with a bit mask.
1895 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001896 */
Takashi Iwai834be882006-03-01 14:16:17 +01001897int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1898 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899{
Takashi Iwai280e57d2012-12-14 10:32:21 +01001900 return codec_amp_update(codec, nid, ch, direction, idx, mask, val, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001902EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
Takashi Iwaid5191e52009-11-16 14:58:17 +01001904/**
1905 * snd_hda_codec_amp_stereo - update the AMP stereo values
1906 * @codec: HD-audio codec
1907 * @nid: NID to read the AMP value
1908 * @direction: #HDA_INPUT or #HDA_OUTPUT
1909 * @idx: the index value (only for input direction)
1910 * @mask: bit mask to set
1911 * @val: the bits value to set
1912 *
1913 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1914 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02001915 */
1916int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1917 int direction, int idx, int mask, int val)
1918{
1919 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001920
1921 if (snd_BUG_ON(mask & ~0xff))
1922 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02001923 for (ch = 0; ch < 2; ch++)
1924 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1925 idx, mask, val);
1926 return ret;
1927}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001928EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001929
Takashi Iwai280e57d2012-12-14 10:32:21 +01001930/* Works like snd_hda_codec_amp_update() but it writes the value only at
1931 * the first access. If the amp was already initialized / updated beforehand,
1932 * this does nothing.
1933 */
1934int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
1935 int dir, int idx, int mask, int val)
1936{
1937 return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true);
1938}
1939EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init);
1940
1941int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
1942 int dir, int idx, int mask, int val)
1943{
1944 int ch, ret = 0;
1945
1946 if (snd_BUG_ON(mask & ~0xff))
1947 mask &= 0xff;
1948 for (ch = 0; ch < 2; ch++)
1949 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
1950 idx, mask, val);
1951 return ret;
1952}
1953EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init_stereo);
1954
Takashi Iwaid5191e52009-11-16 14:58:17 +01001955/**
1956 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
1957 * @codec: HD-audio codec
1958 *
1959 * Resume the all amp commands from the cache.
1960 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001961void snd_hda_codec_resume_amp(struct hda_codec *codec)
1962{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001963 int i;
1964
Takashi Iwaic370dd62012-12-13 18:30:04 +01001965 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01001966 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01001967 for (i = 0; i < codec->amp_cache.buf.used; i++) {
1968 struct hda_amp_info *buffer;
1969 u32 key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001970 hda_nid_t nid;
1971 unsigned int idx, dir, ch;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001972 struct hda_amp_info info;
Takashi Iwaic370dd62012-12-13 18:30:04 +01001973
1974 buffer = snd_array_elem(&codec->amp_cache.buf, i);
Takashi Iwai8565f052012-12-20 12:54:18 +01001975 if (!buffer->head.dirty)
1976 continue;
1977 buffer->head.dirty = 0;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001978 info = *buffer;
1979 key = info.head.key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001980 if (!key)
1981 continue;
1982 nid = key & 0xff;
1983 idx = (key >> 16) & 0xff;
1984 dir = (key >> 24) & 0xff;
1985 for (ch = 0; ch < 2; ch++) {
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001986 if (!(info.head.val & INFO_AMP_VOL(ch)))
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001987 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01001988 mutex_unlock(&codec->hash_mutex);
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001989 put_vol_mute(codec, info.amp_caps, nid, ch, dir, idx,
1990 info.vol[ch]);
Takashi Iwaic370dd62012-12-13 18:30:04 +01001991 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001992 }
1993 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01001994 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001995}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001996EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001998static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1999 unsigned int ofs)
2000{
2001 u32 caps = query_amp_caps(codec, nid, dir);
2002 /* get num steps */
2003 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2004 if (ofs < caps)
2005 caps -= ofs;
2006 return caps;
2007}
2008
Takashi Iwaid5191e52009-11-16 14:58:17 +01002009/**
2010 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
2011 *
2012 * The control element is supposed to have the private_value field
2013 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2014 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002015int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
2016 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017{
2018 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2019 u16 nid = get_amp_nid(kcontrol);
2020 u8 chs = get_amp_channels(kcontrol);
2021 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002022 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002024 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2025 uinfo->count = chs == 3 ? 2 : 1;
2026 uinfo->value.integer.min = 0;
2027 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
2028 if (!uinfo->value.integer.max) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002029 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01002030 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
2031 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 return -EINVAL;
2033 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 return 0;
2035}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002036EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002038
2039static inline unsigned int
2040read_amp_value(struct hda_codec *codec, hda_nid_t nid,
2041 int ch, int dir, int idx, unsigned int ofs)
2042{
2043 unsigned int val;
2044 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
2045 val &= HDA_AMP_VOLMASK;
2046 if (val >= ofs)
2047 val -= ofs;
2048 else
2049 val = 0;
2050 return val;
2051}
2052
2053static inline int
2054update_amp_value(struct hda_codec *codec, hda_nid_t nid,
2055 int ch, int dir, int idx, unsigned int ofs,
2056 unsigned int val)
2057{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002058 unsigned int maxval;
2059
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002060 if (val > 0)
2061 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02002062 /* ofs = 0: raw max value */
2063 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002064 if (val > maxval)
2065 val = maxval;
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002066 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
2067 HDA_AMP_VOLMASK, val);
2068}
2069
Takashi Iwaid5191e52009-11-16 14:58:17 +01002070/**
2071 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
2072 *
2073 * The control element is supposed to have the private_value field
2074 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2075 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002076int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
2077 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078{
2079 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2080 hda_nid_t nid = get_amp_nid(kcontrol);
2081 int chs = get_amp_channels(kcontrol);
2082 int dir = get_amp_direction(kcontrol);
2083 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002084 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 long *valp = ucontrol->value.integer.value;
2086
2087 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002088 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002090 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 return 0;
2092}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002093EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094
Takashi Iwaid5191e52009-11-16 14:58:17 +01002095/**
2096 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
2097 *
2098 * The control element is supposed to have the private_value field
2099 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2100 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002101int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
2102 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103{
2104 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2105 hda_nid_t nid = get_amp_nid(kcontrol);
2106 int chs = get_amp_channels(kcontrol);
2107 int dir = get_amp_direction(kcontrol);
2108 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002109 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 long *valp = ucontrol->value.integer.value;
2111 int change = 0;
2112
Takashi Iwaicb53c622007-08-10 17:21:45 +02002113 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002114 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002115 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002116 valp++;
2117 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002118 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002119 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002120 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 return change;
2122}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002123EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124
Takashi Iwaid5191e52009-11-16 14:58:17 +01002125/**
2126 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2127 *
2128 * The control element is supposed to have the private_value field
2129 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2130 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002131int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2132 unsigned int size, unsigned int __user *_tlv)
2133{
2134 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2135 hda_nid_t nid = get_amp_nid(kcontrol);
2136 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002137 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02002138 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002139 u32 caps, val1, val2;
2140
2141 if (size < 4 * sizeof(unsigned int))
2142 return -ENOMEM;
2143 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002144 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2145 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002146 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002147 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002148 val1 = ((int)val1) * ((int)val2);
Takashi Iwai38681372012-02-27 15:00:58 +01002149 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwaic08d9162010-10-17 10:40:53 +02002150 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002151 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2152 return -EFAULT;
2153 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2154 return -EFAULT;
2155 if (put_user(val1, _tlv + 2))
2156 return -EFAULT;
2157 if (put_user(val2, _tlv + 3))
2158 return -EFAULT;
2159 return 0;
2160}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002161EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002162
Takashi Iwaid5191e52009-11-16 14:58:17 +01002163/**
2164 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2165 * @codec: HD-audio codec
2166 * @nid: NID of a reference widget
2167 * @dir: #HDA_INPUT or #HDA_OUTPUT
2168 * @tlv: TLV data to be stored, at least 4 elements
2169 *
2170 * Set (static) TLV data for a virtual master volume using the AMP caps
2171 * obtained from the reference NID.
2172 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01002173 */
2174void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2175 unsigned int *tlv)
2176{
2177 u32 caps;
2178 int nums, step;
2179
2180 caps = query_amp_caps(codec, nid, dir);
2181 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2182 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2183 step = (step + 1) * 25;
2184 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2185 tlv[1] = 2 * sizeof(unsigned int);
2186 tlv[2] = -nums * step;
2187 tlv[3] = step;
2188}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002189EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002190
2191/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01002192static struct snd_kcontrol *
Takashi Iwaidcda5802012-10-12 17:24:51 +02002193find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002194{
2195 struct snd_ctl_elem_id id;
2196 memset(&id, 0, sizeof(id));
2197 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwaidcda5802012-10-12 17:24:51 +02002198 id.device = dev;
Takashi Iwai09f99702008-02-04 12:31:13 +01002199 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02002200 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2201 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002202 strcpy(id.name, name);
2203 return snd_ctl_find_id(codec->bus->card, &id);
2204}
2205
Takashi Iwaid5191e52009-11-16 14:58:17 +01002206/**
2207 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2208 * @codec: HD-audio codec
2209 * @name: ctl id name string
2210 *
2211 * Get the control element with the given id string and IFACE_MIXER.
2212 */
Takashi Iwai09f99702008-02-04 12:31:13 +01002213struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2214 const char *name)
2215{
Takashi Iwaidcda5802012-10-12 17:24:51 +02002216 return find_mixer_ctl(codec, name, 0, 0);
Takashi Iwai09f99702008-02-04 12:31:13 +01002217}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002218EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01002219
Takashi Iwaidcda5802012-10-12 17:24:51 +02002220static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
2221 int dev)
Takashi Iwai1afe2062010-12-23 10:17:52 +01002222{
2223 int idx;
2224 for (idx = 0; idx < 16; idx++) { /* 16 ctlrs should be large enough */
Takashi Iwaidcda5802012-10-12 17:24:51 +02002225 if (!find_mixer_ctl(codec, name, dev, idx))
Takashi Iwai1afe2062010-12-23 10:17:52 +01002226 return idx;
2227 }
2228 return -EBUSY;
2229}
2230
Takashi Iwaid5191e52009-11-16 14:58:17 +01002231/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002232 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01002233 * @codec: HD-audio codec
2234 * @nid: corresponding NID (optional)
2235 * @kctl: the control element to assign
2236 *
2237 * Add the given control element to an array inside the codec instance.
2238 * All control elements belonging to a codec are supposed to be added
2239 * by this function so that a proper clean-up works at the free or
2240 * reconfiguration time.
2241 *
2242 * If non-zero @nid is passed, the NID is assigned to the control element.
2243 * The assignment is shown in the codec proc file.
2244 *
2245 * snd_hda_ctl_add() checks the control subdev id field whether
2246 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002247 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2248 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01002249 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002250int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2251 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002252{
2253 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002254 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002255 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002256
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002257 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002258 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002259 if (nid == 0)
2260 nid = get_amp_nid_(kctl->private_value);
2261 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002262 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2263 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002264 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01002265 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002266 err = snd_ctl_add(codec->bus->card, kctl);
2267 if (err < 0)
2268 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002269 item = snd_array_new(&codec->mixers);
2270 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002271 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002272 item->kctl = kctl;
2273 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002274 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002275 return 0;
2276}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002277EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002278
Takashi Iwaid5191e52009-11-16 14:58:17 +01002279/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002280 * snd_hda_add_nid - Assign a NID to a control element
2281 * @codec: HD-audio codec
2282 * @nid: corresponding NID (optional)
2283 * @kctl: the control element to assign
2284 * @index: index to kctl
2285 *
2286 * Add the given control element to an array inside the codec instance.
2287 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2288 * NID:KCTL mapping - for example "Capture Source" selector.
2289 */
2290int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2291 unsigned int index, hda_nid_t nid)
2292{
2293 struct hda_nid_item *item;
2294
2295 if (nid > 0) {
2296 item = snd_array_new(&codec->nids);
2297 if (!item)
2298 return -ENOMEM;
2299 item->kctl = kctl;
2300 item->index = index;
2301 item->nid = nid;
2302 return 0;
2303 }
Takashi Iwai28d1a852010-03-15 09:05:46 +01002304 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
2305 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002306 return -EINVAL;
2307}
2308EXPORT_SYMBOL_HDA(snd_hda_add_nid);
2309
2310/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002311 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2312 * @codec: HD-audio codec
2313 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002314void snd_hda_ctls_clear(struct hda_codec *codec)
2315{
2316 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002317 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002318 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002319 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002320 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002321 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002322}
2323
Takashi Iwaia65d6292009-02-23 16:57:04 +01002324/* pseudo device locking
2325 * toggle card->shutdown to allow/disallow the device access (as a hack)
2326 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002327int snd_hda_lock_devices(struct hda_bus *bus)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002328{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002329 struct snd_card *card = bus->card;
2330 struct hda_codec *codec;
2331
Takashi Iwaia65d6292009-02-23 16:57:04 +01002332 spin_lock(&card->files_lock);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002333 if (card->shutdown)
2334 goto err_unlock;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002335 card->shutdown = 1;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002336 if (!list_empty(&card->ctl_files))
2337 goto err_clear;
2338
2339 list_for_each_entry(codec, &bus->codec_list, list) {
2340 int pcm;
2341 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2342 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2343 if (!cpcm->pcm)
2344 continue;
2345 if (cpcm->pcm->streams[0].substream_opened ||
2346 cpcm->pcm->streams[1].substream_opened)
2347 goto err_clear;
2348 }
2349 }
Takashi Iwaia65d6292009-02-23 16:57:04 +01002350 spin_unlock(&card->files_lock);
2351 return 0;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002352
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002353 err_clear:
2354 card->shutdown = 0;
2355 err_unlock:
2356 spin_unlock(&card->files_lock);
2357 return -EINVAL;
2358}
2359EXPORT_SYMBOL_HDA(snd_hda_lock_devices);
2360
2361void snd_hda_unlock_devices(struct hda_bus *bus)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002362{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002363 struct snd_card *card = bus->card;
2364
2365 card = bus->card;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002366 spin_lock(&card->files_lock);
2367 card->shutdown = 0;
2368 spin_unlock(&card->files_lock);
2369}
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002370EXPORT_SYMBOL_HDA(snd_hda_unlock_devices);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002371
Takashi Iwaid5191e52009-11-16 14:58:17 +01002372/**
2373 * snd_hda_codec_reset - Clear all objects assigned to the codec
2374 * @codec: HD-audio codec
2375 *
2376 * This frees the all PCM and control elements assigned to the codec, and
2377 * clears the caches and restores the pin default configurations.
2378 *
2379 * When a device is being used, it returns -EBSY. If successfully freed,
2380 * returns zero.
2381 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002382int snd_hda_codec_reset(struct hda_codec *codec)
2383{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002384 struct hda_bus *bus = codec->bus;
2385 struct snd_card *card = bus->card;
2386 int i;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002387
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002388 if (snd_hda_lock_devices(bus) < 0)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002389 return -EBUSY;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002390
2391 /* OK, let it free */
David Henningsson26a6cb62012-10-09 15:04:21 +02002392 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai83012a72012-08-24 18:38:08 +02002393#ifdef CONFIG_PM
Takashi Iwaia2d96e72012-05-09 12:36:22 +02002394 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwai339876d2012-05-08 16:57:12 +02002395 codec->power_on = 0;
2396 codec->power_transition = 0;
2397 codec->power_jiffies = jiffies;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002398 flush_workqueue(bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002399#endif
2400 snd_hda_ctls_clear(codec);
2401 /* relase PCMs */
2402 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002403 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01002404 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002405 clear_bit(codec->pcm_info[i].device,
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002406 bus->pcm_dev_bits);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002407 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002408 }
2409 if (codec->patch_ops.free)
2410 codec->patch_ops.free(codec);
Takashi Iwai07dc59f2012-09-10 09:39:31 +02002411 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
Takashi Iwai1835a0f2011-10-27 22:12:46 +02002412 snd_hda_jack_tbl_clear(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01002413 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002414 codec->spec = NULL;
2415 free_hda_cache(&codec->amp_cache);
2416 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01002417 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2418 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01002419 /* free only driver_pins so that init_pins + user_pins are restored */
2420 snd_array_free(&codec->driver_pins);
Takashi Iwai09a60712012-06-26 15:01:33 +02002421 snd_array_free(&codec->cvt_setups);
2422 snd_array_free(&codec->spdif_out);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01002423 snd_array_free(&codec->verbs);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002424 codec->num_pcms = 0;
2425 codec->pcm_info = NULL;
2426 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01002427 codec->slave_dig_outs = NULL;
2428 codec->spdif_status_reset = 0;
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002429 module_put(codec->owner);
2430 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002431
2432 /* allow device access again */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002433 snd_hda_unlock_devices(bus);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002434 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002435}
2436
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002437typedef int (*map_slave_func_t)(void *, struct snd_kcontrol *);
2438
2439/* apply the function to all matching slave ctls in the mixer list */
2440static int map_slaves(struct hda_codec *codec, const char * const *slaves,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002441 const char *suffix, map_slave_func_t func, void *data)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002442{
2443 struct hda_nid_item *items;
2444 const char * const *s;
2445 int i, err;
2446
2447 items = codec->mixers.list;
2448 for (i = 0; i < codec->mixers.used; i++) {
2449 struct snd_kcontrol *sctl = items[i].kctl;
2450 if (!sctl || !sctl->id.name ||
2451 sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
2452 continue;
2453 for (s = slaves; *s; s++) {
Takashi Iwai9322ca52012-02-03 14:28:01 +01002454 char tmpname[sizeof(sctl->id.name)];
2455 const char *name = *s;
2456 if (suffix) {
2457 snprintf(tmpname, sizeof(tmpname), "%s %s",
2458 name, suffix);
2459 name = tmpname;
2460 }
Takashi Iwai9322ca52012-02-03 14:28:01 +01002461 if (!strcmp(sctl->id.name, name)) {
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002462 err = func(data, sctl);
2463 if (err)
2464 return err;
2465 break;
2466 }
2467 }
2468 }
2469 return 0;
2470}
2471
2472static int check_slave_present(void *data, struct snd_kcontrol *sctl)
2473{
2474 return 1;
2475}
2476
Takashi Iwai18478e82012-03-09 17:51:10 +01002477/* guess the value corresponding to 0dB */
2478static int get_kctl_0dB_offset(struct snd_kcontrol *kctl)
2479{
2480 int _tlv[4];
2481 const int *tlv = NULL;
2482 int val = -1;
2483
2484 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2485 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2486 mm_segment_t fs = get_fs();
2487 set_fs(get_ds());
2488 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2489 tlv = _tlv;
2490 set_fs(fs);
2491 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2492 tlv = kctl->tlv.p;
2493 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE)
2494 val = -tlv[2] / tlv[3];
2495 return val;
2496}
2497
2498/* call kctl->put with the given value(s) */
2499static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2500{
2501 struct snd_ctl_elem_value *ucontrol;
2502 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2503 if (!ucontrol)
2504 return -ENOMEM;
2505 ucontrol->value.integer.value[0] = val;
2506 ucontrol->value.integer.value[1] = val;
2507 kctl->put(kctl, ucontrol);
2508 kfree(ucontrol);
2509 return 0;
2510}
2511
2512/* initialize the slave volume with 0dB */
2513static int init_slave_0dB(void *data, struct snd_kcontrol *slave)
2514{
2515 int offset = get_kctl_0dB_offset(slave);
2516 if (offset > 0)
2517 put_kctl_with_value(slave, offset);
2518 return 0;
2519}
2520
2521/* unmute the slave */
2522static int init_slave_unmute(void *data, struct snd_kcontrol *slave)
2523{
2524 return put_kctl_with_value(slave, 1);
2525}
2526
Takashi Iwaid5191e52009-11-16 14:58:17 +01002527/**
2528 * snd_hda_add_vmaster - create a virtual master control and add slaves
2529 * @codec: HD-audio codec
2530 * @name: vmaster control name
2531 * @tlv: TLV data (optional)
2532 * @slaves: slave control names (optional)
Takashi Iwai9322ca52012-02-03 14:28:01 +01002533 * @suffix: suffix string to each slave name (optional)
Takashi Iwai18478e82012-03-09 17:51:10 +01002534 * @init_slave_vol: initialize slaves to unmute/0dB
Takashi Iwai29e58532012-03-12 12:25:03 +01002535 * @ctl_ret: store the vmaster kcontrol in return
Takashi Iwaid5191e52009-11-16 14:58:17 +01002536 *
2537 * Create a virtual master control with the given name. The TLV data
2538 * must be either NULL or a valid data.
2539 *
2540 * @slaves is a NULL-terminated array of strings, each of which is a
2541 * slave control name. All controls with these names are assigned to
2542 * the new virtual master control.
2543 *
2544 * This function returns zero if successful or a negative error code.
2545 */
Takashi Iwai18478e82012-03-09 17:51:10 +01002546int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002547 unsigned int *tlv, const char * const *slaves,
Takashi Iwai29e58532012-03-12 12:25:03 +01002548 const char *suffix, bool init_slave_vol,
2549 struct snd_kcontrol **ctl_ret)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002550{
2551 struct snd_kcontrol *kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002552 int err;
2553
Takashi Iwai29e58532012-03-12 12:25:03 +01002554 if (ctl_ret)
2555 *ctl_ret = NULL;
2556
Takashi Iwai9322ca52012-02-03 14:28:01 +01002557 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002558 if (err != 1) {
Takashi Iwai2f085542008-02-22 18:43:50 +01002559 snd_printdd("No slave found for %s\n", name);
2560 return 0;
2561 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002562 kctl = snd_ctl_make_virtual_master(name, tlv);
2563 if (!kctl)
2564 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002565 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002566 if (err < 0)
2567 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002568
Takashi Iwai9322ca52012-02-03 14:28:01 +01002569 err = map_slaves(codec, slaves, suffix,
2570 (map_slave_func_t)snd_ctl_add_slave, kctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002571 if (err < 0)
2572 return err;
Takashi Iwai18478e82012-03-09 17:51:10 +01002573
2574 /* init with master mute & zero volume */
2575 put_kctl_with_value(kctl, 0);
2576 if (init_slave_vol)
2577 map_slaves(codec, slaves, suffix,
2578 tlv ? init_slave_0dB : init_slave_unmute, kctl);
2579
Takashi Iwai29e58532012-03-12 12:25:03 +01002580 if (ctl_ret)
2581 *ctl_ret = kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002582 return 0;
2583}
Takashi Iwai18478e82012-03-09 17:51:10 +01002584EXPORT_SYMBOL_HDA(__snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002585
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002586/*
2587 * mute-LED control using vmaster
2588 */
2589static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2590 struct snd_ctl_elem_info *uinfo)
2591{
2592 static const char * const texts[] = {
David Henningssonc86c2d42013-01-03 14:12:29 +01002593 "On", "Off", "Follow Master"
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002594 };
2595 unsigned int index;
2596
2597 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2598 uinfo->count = 1;
2599 uinfo->value.enumerated.items = 3;
2600 index = uinfo->value.enumerated.item;
2601 if (index >= 3)
2602 index = 2;
2603 strcpy(uinfo->value.enumerated.name, texts[index]);
2604 return 0;
2605}
2606
2607static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2608 struct snd_ctl_elem_value *ucontrol)
2609{
2610 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2611 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2612 return 0;
2613}
2614
2615static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2616 struct snd_ctl_elem_value *ucontrol)
2617{
2618 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2619 unsigned int old_mode = hook->mute_mode;
2620
2621 hook->mute_mode = ucontrol->value.enumerated.item[0];
2622 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2623 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2624 if (old_mode == hook->mute_mode)
2625 return 0;
2626 snd_hda_sync_vmaster_hook(hook);
2627 return 1;
2628}
2629
2630static struct snd_kcontrol_new vmaster_mute_mode = {
2631 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2632 .name = "Mute-LED Mode",
2633 .info = vmaster_mute_mode_info,
2634 .get = vmaster_mute_mode_get,
2635 .put = vmaster_mute_mode_put,
2636};
2637
2638/*
2639 * Add a mute-LED hook with the given vmaster switch kctl
2640 * "Mute-LED Mode" control is automatically created and associated with
2641 * the given hook.
2642 */
2643int snd_hda_add_vmaster_hook(struct hda_codec *codec,
Takashi Iwaif29735c2012-03-13 07:55:10 +01002644 struct hda_vmaster_mute_hook *hook,
2645 bool expose_enum_ctl)
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002646{
2647 struct snd_kcontrol *kctl;
2648
2649 if (!hook->hook || !hook->sw_kctl)
2650 return 0;
2651 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
2652 hook->codec = codec;
2653 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
Takashi Iwaif29735c2012-03-13 07:55:10 +01002654 if (!expose_enum_ctl)
2655 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002656 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2657 if (!kctl)
2658 return -ENOMEM;
2659 return snd_hda_ctl_add(codec, 0, kctl);
2660}
2661EXPORT_SYMBOL_HDA(snd_hda_add_vmaster_hook);
2662
2663/*
2664 * Call the hook with the current value for synchronization
2665 * Should be called in init callback
2666 */
2667void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2668{
2669 if (!hook->hook || !hook->codec)
2670 return;
2671 switch (hook->mute_mode) {
2672 case HDA_VMUTE_FOLLOW_MASTER:
2673 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2674 break;
2675 default:
2676 hook->hook(hook->codec, hook->mute_mode);
2677 break;
2678 }
2679}
2680EXPORT_SYMBOL_HDA(snd_hda_sync_vmaster_hook);
2681
2682
Takashi Iwaid5191e52009-11-16 14:58:17 +01002683/**
2684 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2685 *
2686 * The control element is supposed to have the private_value field
2687 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2688 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002689int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2690 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691{
2692 int chs = get_amp_channels(kcontrol);
2693
2694 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2695 uinfo->count = chs == 3 ? 2 : 1;
2696 uinfo->value.integer.min = 0;
2697 uinfo->value.integer.max = 1;
2698 return 0;
2699}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002700EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701
Takashi Iwaid5191e52009-11-16 14:58:17 +01002702/**
2703 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2704 *
2705 * The control element is supposed to have the private_value field
2706 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2707 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002708int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2709 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710{
2711 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2712 hda_nid_t nid = get_amp_nid(kcontrol);
2713 int chs = get_amp_channels(kcontrol);
2714 int dir = get_amp_direction(kcontrol);
2715 int idx = get_amp_index(kcontrol);
2716 long *valp = ucontrol->value.integer.value;
2717
2718 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002719 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002720 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002722 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002723 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 return 0;
2725}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002726EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727
Takashi Iwaid5191e52009-11-16 14:58:17 +01002728/**
2729 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2730 *
2731 * The control element is supposed to have the private_value field
2732 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2733 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002734int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2735 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736{
2737 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2738 hda_nid_t nid = get_amp_nid(kcontrol);
2739 int chs = get_amp_channels(kcontrol);
2740 int dir = get_amp_direction(kcontrol);
2741 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 long *valp = ucontrol->value.integer.value;
2743 int change = 0;
2744
Takashi Iwaicb53c622007-08-10 17:21:45 +02002745 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002746 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002747 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002748 HDA_AMP_MUTE,
2749 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002750 valp++;
2751 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002752 if (chs & 2)
2753 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002754 HDA_AMP_MUTE,
2755 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002756 hda_call_check_power_status(codec, nid);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002757 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 return change;
2759}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002760EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761
2762/*
Takashi Iwai985be542005-11-02 18:26:49 +01002763 * bound volume controls
2764 *
2765 * bind multiple volumes (# indices, from 0)
2766 */
2767
2768#define AMP_VAL_IDX_SHIFT 19
2769#define AMP_VAL_IDX_MASK (0x0f<<19)
2770
Takashi Iwaid5191e52009-11-16 14:58:17 +01002771/**
2772 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2773 *
2774 * The control element is supposed to have the private_value field
2775 * set up via HDA_BIND_MUTE*() macros.
2776 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002777int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2778 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002779{
2780 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2781 unsigned long pval;
2782 int err;
2783
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002784 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002785 pval = kcontrol->private_value;
2786 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2787 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2788 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002789 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002790 return err;
2791}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002792EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002793
Takashi Iwaid5191e52009-11-16 14:58:17 +01002794/**
2795 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2796 *
2797 * The control element is supposed to have the private_value field
2798 * set up via HDA_BIND_MUTE*() macros.
2799 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002800int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2801 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002802{
2803 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2804 unsigned long pval;
2805 int i, indices, err = 0, change = 0;
2806
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002807 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002808 pval = kcontrol->private_value;
2809 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2810 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002811 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2812 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002813 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2814 if (err < 0)
2815 break;
2816 change |= err;
2817 }
2818 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002819 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002820 return err < 0 ? err : change;
2821}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002822EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002823
Takashi Iwaid5191e52009-11-16 14:58:17 +01002824/**
2825 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2826 *
2827 * The control element is supposed to have the private_value field
2828 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002829 */
2830int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2831 struct snd_ctl_elem_info *uinfo)
2832{
2833 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2834 struct hda_bind_ctls *c;
2835 int err;
2836
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002837 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002838 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002839 kcontrol->private_value = *c->values;
2840 err = c->ops->info(kcontrol, uinfo);
2841 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002842 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002843 return err;
2844}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002845EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002846
Takashi Iwaid5191e52009-11-16 14:58:17 +01002847/**
2848 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2849 *
2850 * The control element is supposed to have the private_value field
2851 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2852 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002853int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2854 struct snd_ctl_elem_value *ucontrol)
2855{
2856 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2857 struct hda_bind_ctls *c;
2858 int err;
2859
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002860 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002861 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002862 kcontrol->private_value = *c->values;
2863 err = c->ops->get(kcontrol, ucontrol);
2864 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002865 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002866 return err;
2867}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002868EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002869
Takashi Iwaid5191e52009-11-16 14:58:17 +01002870/**
2871 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2872 *
2873 * The control element is supposed to have the private_value field
2874 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2875 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002876int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2877 struct snd_ctl_elem_value *ucontrol)
2878{
2879 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2880 struct hda_bind_ctls *c;
2881 unsigned long *vals;
2882 int err = 0, change = 0;
2883
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002884 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002885 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002886 for (vals = c->values; *vals; vals++) {
2887 kcontrol->private_value = *vals;
2888 err = c->ops->put(kcontrol, ucontrol);
2889 if (err < 0)
2890 break;
2891 change |= err;
2892 }
2893 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002894 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002895 return err < 0 ? err : change;
2896}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002897EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02002898
Takashi Iwaid5191e52009-11-16 14:58:17 +01002899/**
2900 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2901 *
2902 * The control element is supposed to have the private_value field
2903 * set up via HDA_BIND_VOL() macro.
2904 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002905int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2906 unsigned int size, unsigned int __user *tlv)
2907{
2908 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2909 struct hda_bind_ctls *c;
2910 int err;
2911
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002912 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002913 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002914 kcontrol->private_value = *c->values;
2915 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2916 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002917 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002918 return err;
2919}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002920EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02002921
2922struct hda_ctl_ops snd_hda_bind_vol = {
2923 .info = snd_hda_mixer_amp_volume_info,
2924 .get = snd_hda_mixer_amp_volume_get,
2925 .put = snd_hda_mixer_amp_volume_put,
2926 .tlv = snd_hda_mixer_amp_tlv
2927};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002928EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02002929
2930struct hda_ctl_ops snd_hda_bind_sw = {
2931 .info = snd_hda_mixer_amp_switch_info,
2932 .get = snd_hda_mixer_amp_switch_get,
2933 .put = snd_hda_mixer_amp_switch_put,
2934 .tlv = snd_hda_mixer_amp_tlv
2935};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002936EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02002937
2938/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 * SPDIF out controls
2940 */
2941
Takashi Iwai0ba21762007-04-16 11:29:14 +02002942static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2943 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944{
2945 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2946 uinfo->count = 1;
2947 return 0;
2948}
2949
Takashi Iwai0ba21762007-04-16 11:29:14 +02002950static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2951 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952{
2953 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2954 IEC958_AES0_NONAUDIO |
2955 IEC958_AES0_CON_EMPHASIS_5015 |
2956 IEC958_AES0_CON_NOT_COPYRIGHT;
2957 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2958 IEC958_AES1_CON_ORIGINAL;
2959 return 0;
2960}
2961
Takashi Iwai0ba21762007-04-16 11:29:14 +02002962static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2963 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964{
2965 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2966 IEC958_AES0_NONAUDIO |
2967 IEC958_AES0_PRO_EMPHASIS_5015;
2968 return 0;
2969}
2970
Takashi Iwai0ba21762007-04-16 11:29:14 +02002971static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2972 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973{
2974 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06002975 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002976 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002978 mutex_lock(&codec->spdif_mutex);
2979 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06002980 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
2981 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
2982 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
2983 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002984 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985
2986 return 0;
2987}
2988
2989/* convert from SPDIF status bits to HDA SPDIF bits
2990 * bit 0 (DigEn) is always set zero (to be filled later)
2991 */
2992static unsigned short convert_from_spdif_status(unsigned int sbits)
2993{
2994 unsigned short val = 0;
2995
2996 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002997 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002999 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003001 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
3002 IEC958_AES0_PRO_EMPHASIS_5015)
3003 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003005 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
3006 IEC958_AES0_CON_EMPHASIS_5015)
3007 val |= AC_DIG1_EMPHASIS;
3008 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
3009 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02003011 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
3013 }
3014 return val;
3015}
3016
3017/* convert to SPDIF status bits from HDA SPDIF bits
3018 */
3019static unsigned int convert_to_spdif_status(unsigned short val)
3020{
3021 unsigned int sbits = 0;
3022
Takashi Iwai0ba21762007-04-16 11:29:14 +02003023 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003025 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 sbits |= IEC958_AES0_PROFESSIONAL;
3027 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003028 if (sbits & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
3030 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003031 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003033 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003035 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
3037 sbits |= val & (0x7f << 8);
3038 }
3039 return sbits;
3040}
3041
Takashi Iwai2f728532008-09-25 16:32:41 +02003042/* set digital convert verbs both for the given NID and its slaves */
3043static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
3044 int verb, int val)
3045{
Takashi Iwaidda14412011-05-02 11:29:30 +02003046 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02003047
Takashi Iwai9e976972008-11-25 08:17:20 +01003048 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003049 d = codec->slave_dig_outs;
3050 if (!d)
3051 return;
3052 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01003053 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003054}
3055
3056static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
3057 int dig1, int dig2)
3058{
3059 if (dig1 != -1)
3060 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
3061 if (dig2 != -1)
3062 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
3063}
3064
Takashi Iwai0ba21762007-04-16 11:29:14 +02003065static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
3066 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067{
3068 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003069 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003070 struct hda_spdif_out *spdif;
3071 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 unsigned short val;
3073 int change;
3074
Ingo Molnar62932df2006-01-16 16:34:20 +01003075 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003076 spdif = snd_array_elem(&codec->spdif_out, idx);
3077 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06003078 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
3080 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
3081 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c935972011-06-01 11:14:17 -06003082 val = convert_from_spdif_status(spdif->status);
3083 val |= spdif->ctls & 1;
3084 change = spdif->ctls != val;
3085 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003086 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02003087 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01003088 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 return change;
3090}
3091
Takashi Iwaia5ce8892007-07-23 15:42:26 +02003092#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093
Takashi Iwai0ba21762007-04-16 11:29:14 +02003094static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
3095 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096{
3097 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003098 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003099 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003101 mutex_lock(&codec->spdif_mutex);
3102 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06003103 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003104 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 return 0;
3106}
3107
Stephen Warren74b654c2011-06-01 11:14:18 -06003108static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
3109 int dig1, int dig2)
3110{
3111 set_dig_out_convert(codec, nid, dig1, dig2);
3112 /* unmute amp switch (if any) */
3113 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
3114 (dig1 & AC_DIG1_ENABLE))
3115 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
3116 HDA_AMP_MUTE, 0);
3117}
3118
Takashi Iwai0ba21762007-04-16 11:29:14 +02003119static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
3120 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121{
3122 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003123 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003124 struct hda_spdif_out *spdif;
3125 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 unsigned short val;
3127 int change;
3128
Ingo Molnar62932df2006-01-16 16:34:20 +01003129 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003130 spdif = snd_array_elem(&codec->spdif_out, idx);
3131 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06003132 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02003134 val |= AC_DIG1_ENABLE;
Stephen Warren7c935972011-06-01 11:14:17 -06003135 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003136 spdif->ctls = val;
3137 if (change && nid != (u16)-1)
3138 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01003139 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 return change;
3141}
3142
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003143static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 {
3145 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3146 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003147 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 .info = snd_hda_spdif_mask_info,
3149 .get = snd_hda_spdif_cmask_get,
3150 },
3151 {
3152 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3153 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003154 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 .info = snd_hda_spdif_mask_info,
3156 .get = snd_hda_spdif_pmask_get,
3157 },
3158 {
3159 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003160 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 .info = snd_hda_spdif_mask_info,
3162 .get = snd_hda_spdif_default_get,
3163 .put = snd_hda_spdif_default_put,
3164 },
3165 {
3166 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003167 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 .info = snd_hda_spdif_out_switch_info,
3169 .get = snd_hda_spdif_out_switch_get,
3170 .put = snd_hda_spdif_out_switch_put,
3171 },
3172 { } /* end */
3173};
3174
3175/**
Takashi Iwaidcda5802012-10-12 17:24:51 +02003176 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 * @codec: the HDA codec
Takashi Iwaidcda5802012-10-12 17:24:51 +02003178 * @associated_nid: NID that new ctls associated with
3179 * @cvt_nid: converter NID
3180 * @type: HDA_PCM_TYPE_*
3181 * Creates controls related with the digital output.
3182 * Called from each patch supporting the digital out.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 *
3184 * Returns 0 if successful, or a negative error code.
3185 */
Takashi Iwaidcda5802012-10-12 17:24:51 +02003186int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
3187 hda_nid_t associated_nid,
3188 hda_nid_t cvt_nid,
3189 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190{
3191 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003192 struct snd_kcontrol *kctl;
3193 struct snd_kcontrol_new *dig_mix;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003194 int idx, dev = 0;
3195 const int spdif_pcm_dev = 1;
Stephen Warren7c935972011-06-01 11:14:17 -06003196 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197
Takashi Iwaidcda5802012-10-12 17:24:51 +02003198 if (codec->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
3199 type == HDA_PCM_TYPE_SPDIF) {
3200 dev = spdif_pcm_dev;
3201 } else if (codec->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
3202 type == HDA_PCM_TYPE_HDMI) {
3203 for (idx = 0; idx < codec->spdif_out.used; idx++) {
3204 spdif = snd_array_elem(&codec->spdif_out, idx);
3205 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3206 kctl = find_mixer_ctl(codec, dig_mix->name, 0, idx);
3207 if (!kctl)
3208 break;
3209 kctl->id.device = spdif_pcm_dev;
3210 }
3211 }
3212 codec->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
3213 }
3214 if (!codec->primary_dig_out_type)
3215 codec->primary_dig_out_type = type;
3216
3217 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", dev);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003218 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003219 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
3220 return -EBUSY;
3221 }
Stephen Warren7c935972011-06-01 11:14:17 -06003222 spdif = snd_array_new(&codec->spdif_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3224 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01003225 if (!kctl)
3226 return -ENOMEM;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003227 kctl->id.device = dev;
Takashi Iwai09f99702008-02-04 12:31:13 +01003228 kctl->id.index = idx;
Stephen Warren7c935972011-06-01 11:14:17 -06003229 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06003230 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003231 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 return err;
3233 }
Stephen Warren74b654c2011-06-01 11:14:18 -06003234 spdif->nid = cvt_nid;
3235 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
Stephen Warren7c935972011-06-01 11:14:17 -06003236 AC_VERB_GET_DIGI_CONVERT_1, 0);
3237 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 return 0;
3239}
Takashi Iwaidcda5802012-10-12 17:24:51 +02003240EXPORT_SYMBOL_HDA(snd_hda_create_dig_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003242/* get the hda_spdif_out entry from the given NID
3243 * call within spdif_mutex lock
3244 */
Stephen Warren7c935972011-06-01 11:14:17 -06003245struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3246 hda_nid_t nid)
3247{
3248 int i;
3249 for (i = 0; i < codec->spdif_out.used; i++) {
3250 struct hda_spdif_out *spdif =
3251 snd_array_elem(&codec->spdif_out, i);
3252 if (spdif->nid == nid)
3253 return spdif;
3254 }
3255 return NULL;
3256}
3257EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid);
3258
Stephen Warren74b654c2011-06-01 11:14:18 -06003259void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3260{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003261 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003262
3263 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003264 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003265 spdif->nid = (u16)-1;
3266 mutex_unlock(&codec->spdif_mutex);
3267}
3268EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign);
3269
3270void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3271{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003272 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003273 unsigned short val;
3274
3275 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003276 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003277 if (spdif->nid != nid) {
3278 spdif->nid = nid;
3279 val = spdif->ctls;
3280 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3281 }
3282 mutex_unlock(&codec->spdif_mutex);
3283}
3284EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign);
3285
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286/*
Takashi Iwai9a081602008-02-12 18:37:26 +01003287 * SPDIF sharing with analog output
3288 */
3289static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3290 struct snd_ctl_elem_value *ucontrol)
3291{
3292 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3293 ucontrol->value.integer.value[0] = mout->share_spdif;
3294 return 0;
3295}
3296
3297static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3298 struct snd_ctl_elem_value *ucontrol)
3299{
3300 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3301 mout->share_spdif = !!ucontrol->value.integer.value[0];
3302 return 0;
3303}
3304
3305static struct snd_kcontrol_new spdif_share_sw = {
3306 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3307 .name = "IEC958 Default PCM Playback Switch",
3308 .info = snd_ctl_boolean_mono_info,
3309 .get = spdif_share_sw_get,
3310 .put = spdif_share_sw_put,
3311};
3312
Takashi Iwaid5191e52009-11-16 14:58:17 +01003313/**
3314 * snd_hda_create_spdif_share_sw - create Default PCM switch
3315 * @codec: the HDA codec
3316 * @mout: multi-out instance
3317 */
Takashi Iwai9a081602008-02-12 18:37:26 +01003318int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3319 struct hda_multi_out *mout)
3320{
3321 if (!mout->dig_out_nid)
3322 return 0;
3323 /* ATTENTION: here mout is passed as private_data, instead of codec */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003324 return snd_hda_ctl_add(codec, mout->dig_out_nid,
3325 snd_ctl_new1(&spdif_share_sw, mout));
Takashi Iwai9a081602008-02-12 18:37:26 +01003326}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003327EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01003328
3329/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 * SPDIF input
3331 */
3332
3333#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3334
Takashi Iwai0ba21762007-04-16 11:29:14 +02003335static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3336 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337{
3338 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3339
3340 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3341 return 0;
3342}
3343
Takashi Iwai0ba21762007-04-16 11:29:14 +02003344static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3345 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346{
3347 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3348 hda_nid_t nid = kcontrol->private_value;
3349 unsigned int val = !!ucontrol->value.integer.value[0];
3350 int change;
3351
Ingo Molnar62932df2006-01-16 16:34:20 +01003352 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003354 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003356 snd_hda_codec_write_cache(codec, nid, 0,
3357 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003359 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 return change;
3361}
3362
Takashi Iwai0ba21762007-04-16 11:29:14 +02003363static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3364 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365{
3366 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3367 hda_nid_t nid = kcontrol->private_value;
3368 unsigned short val;
3369 unsigned int sbits;
3370
Andrew Paprocki3982d172007-12-19 12:13:44 +01003371 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 sbits = convert_to_spdif_status(val);
3373 ucontrol->value.iec958.status[0] = sbits;
3374 ucontrol->value.iec958.status[1] = sbits >> 8;
3375 ucontrol->value.iec958.status[2] = sbits >> 16;
3376 ucontrol->value.iec958.status[3] = sbits >> 24;
3377 return 0;
3378}
3379
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003380static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 {
3382 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003383 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 .info = snd_hda_spdif_in_switch_info,
3385 .get = snd_hda_spdif_in_switch_get,
3386 .put = snd_hda_spdif_in_switch_put,
3387 },
3388 {
3389 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3390 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003391 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 .info = snd_hda_spdif_mask_info,
3393 .get = snd_hda_spdif_in_status_get,
3394 },
3395 { } /* end */
3396};
3397
3398/**
3399 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3400 * @codec: the HDA codec
3401 * @nid: audio in widget NID
3402 *
3403 * Creates controls related with the SPDIF input.
3404 * Called from each patch supporting the SPDIF in.
3405 *
3406 * Returns 0 if successful, or a negative error code.
3407 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003408int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409{
3410 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003411 struct snd_kcontrol *kctl;
3412 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003413 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414
Takashi Iwaidcda5802012-10-12 17:24:51 +02003415 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003416 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003417 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
3418 return -EBUSY;
3419 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3421 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01003422 if (!kctl)
3423 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003425 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003426 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 return err;
3428 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003429 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01003430 snd_hda_codec_read(codec, nid, 0,
3431 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02003432 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 return 0;
3434}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003435EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003437/*
3438 * command cache
3439 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440
Takashi Iwaic370dd62012-12-13 18:30:04 +01003441/* build a 31bit cache key with the widget id and the command parameter */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003442#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3443#define get_cmd_cache_nid(key) ((key) & 0xff)
3444#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3445
3446/**
3447 * snd_hda_codec_write_cache - send a single command with caching
3448 * @codec: the HDA codec
3449 * @nid: NID to send the command
3450 * @direct: direct flag
3451 * @verb: the verb to send
3452 * @parm: the parameter for the verb
3453 *
3454 * Send a single command without waiting for response.
3455 *
3456 * Returns 0 if successful, or a negative error code.
3457 */
3458int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
3459 int direct, unsigned int verb, unsigned int parm)
3460{
Takashi Iwaic370dd62012-12-13 18:30:04 +01003461 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003462 struct hda_cache_head *c;
3463 u32 key;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003464 unsigned int cache_only;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003465
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003466 cache_only = codec->cached_write;
3467 if (!cache_only) {
Takashi Iwaic370dd62012-12-13 18:30:04 +01003468 err = snd_hda_codec_write(codec, nid, direct, verb, parm);
3469 if (err < 0)
3470 return err;
3471 }
3472
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003473 /* parm may contain the verb stuff for get/set amp */
3474 verb = verb | (parm >> 8);
3475 parm &= 0xff;
3476 key = build_cmd_cache_key(nid, verb);
3477 mutex_lock(&codec->bus->cmd_mutex);
3478 c = get_alloc_hash(&codec->cmd_cache, key);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003479 if (c) {
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003480 c->val = parm;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003481 c->dirty = cache_only;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003482 }
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003483 mutex_unlock(&codec->bus->cmd_mutex);
3484 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003485}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003486EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003487
Takashi Iwaid5191e52009-11-16 14:58:17 +01003488/**
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003489 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3490 * @codec: the HDA codec
3491 * @nid: NID to send the command
3492 * @direct: direct flag
3493 * @verb: the verb to send
3494 * @parm: the parameter for the verb
3495 *
3496 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3497 * command if the parameter is already identical with the cached value.
3498 * If not, it sends the command and refreshes the cache.
3499 *
3500 * Returns 0 if successful, or a negative error code.
3501 */
3502int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
3503 int direct, unsigned int verb, unsigned int parm)
3504{
3505 struct hda_cache_head *c;
3506 u32 key;
3507
3508 /* parm may contain the verb stuff for get/set amp */
3509 verb = verb | (parm >> 8);
3510 parm &= 0xff;
3511 key = build_cmd_cache_key(nid, verb);
3512 mutex_lock(&codec->bus->cmd_mutex);
3513 c = get_hash(&codec->cmd_cache, key);
3514 if (c && c->val == parm) {
3515 mutex_unlock(&codec->bus->cmd_mutex);
3516 return 0;
3517 }
3518 mutex_unlock(&codec->bus->cmd_mutex);
3519 return snd_hda_codec_write_cache(codec, nid, direct, verb, parm);
3520}
3521EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
3522
3523/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003524 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3525 * @codec: HD-audio codec
3526 *
3527 * Execute all verbs recorded in the command caches to resume.
3528 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003529void snd_hda_codec_resume_cache(struct hda_codec *codec)
3530{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003531 int i;
3532
Takashi Iwaic370dd62012-12-13 18:30:04 +01003533 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01003534 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003535 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3536 struct hda_cache_head *buffer;
3537 u32 key;
3538
3539 buffer = snd_array_elem(&codec->cmd_cache.buf, i);
3540 key = buffer->key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003541 if (!key)
3542 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003543 if (!buffer->dirty)
3544 continue;
3545 buffer->dirty = 0;
3546 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003547 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3548 get_cmd_cache_cmd(key), buffer->val);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003549 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003550 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01003551 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003552}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003553EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003554
3555/**
3556 * snd_hda_sequence_write_cache - sequence writes with caching
3557 * @codec: the HDA codec
3558 * @seq: VERB array to send
3559 *
3560 * Send the commands sequentially from the given array.
3561 * Thte commands are recorded on cache for power-save and resume.
3562 * The array must be terminated with NID=0.
3563 */
3564void snd_hda_sequence_write_cache(struct hda_codec *codec,
3565 const struct hda_verb *seq)
3566{
3567 for (; seq->nid; seq++)
3568 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3569 seq->param);
3570}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003571EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003572
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003573void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
3574 unsigned int power_state,
3575 bool eapd_workaround)
Takashi Iwai54d17402005-11-21 16:33:22 +01003576{
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003577 hda_nid_t nid = codec->start_nid;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003578 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01003579
Takashi Iwaicb53c622007-08-10 17:21:45 +02003580 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003581 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003582 if (!(wcaps & AC_WCAP_POWER))
3583 continue;
3584 /* don't power down the widget if it controls eapd and
3585 * EAPD_BTLENABLE is set.
3586 */
3587 if (eapd_workaround && power_state == AC_PWRST_D3 &&
3588 get_wcaps_type(wcaps) == AC_WID_PIN &&
3589 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3590 int eapd = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003591 AC_VERB_GET_EAPD_BTLENABLE, 0);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003592 if (eapd & 0x02)
3593 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003594 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003595 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
3596 power_state);
Takashi Iwai54d17402005-11-21 16:33:22 +01003597 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003598}
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003599EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all);
3600
3601/*
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003602 * supported power states check
3603 */
3604static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg,
3605 unsigned int power_state)
3606{
3607 int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE);
3608
Mengdong Line037cb42012-08-10 14:11:58 +02003609 if (sup == -1)
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003610 return false;
3611 if (sup & power_state)
3612 return true;
3613 else
3614 return false;
3615}
3616
3617/*
Takashi Iwai432c6412012-08-28 09:59:20 -07003618 * wait until the state is reached, returns the current state
3619 */
3620static unsigned int hda_sync_power_state(struct hda_codec *codec,
3621 hda_nid_t fg,
3622 unsigned int power_state)
3623{
3624 unsigned long end_time = jiffies + msecs_to_jiffies(500);
3625 unsigned int state, actual_state;
3626
3627 for (;;) {
3628 state = snd_hda_codec_read(codec, fg, 0,
3629 AC_VERB_GET_POWER_STATE, 0);
3630 if (state & AC_PWRST_ERROR)
3631 break;
3632 actual_state = (state >> 4) & 0x0f;
3633 if (actual_state == power_state)
3634 break;
3635 if (time_after_eq(jiffies, end_time))
3636 break;
3637 /* wait until the codec reachs to the target state */
3638 msleep(1);
3639 }
3640 return state;
3641}
3642
3643/*
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003644 * set power state of the codec, and return the power state
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003645 */
Takashi Iwaid8193872012-08-31 07:54:38 -07003646static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003647 unsigned int power_state)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003648{
Takashi Iwaid8193872012-08-31 07:54:38 -07003649 hda_nid_t fg = codec->afg ? codec->afg : codec->mfg;
Wang Xingchao09617ce2012-06-08 10:26:08 +08003650 int count;
3651 unsigned int state;
3652
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003653 /* this delay seems necessary to avoid click noise at power-down */
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003654 if (power_state == AC_PWRST_D3) {
3655 /* transition time less than 10ms for power down */
Takashi Iwai983f6b92012-08-28 09:18:01 -07003656 msleep(codec->epss ? 10 : 100);
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003657 }
Wang Xingchao09617ce2012-06-08 10:26:08 +08003658
3659 /* repeat power states setting at most 10 times*/
3660 for (count = 0; count < 10; count++) {
Takashi Iwai432c6412012-08-28 09:59:20 -07003661 if (codec->patch_ops.set_power_state)
3662 codec->patch_ops.set_power_state(codec, fg,
3663 power_state);
3664 else {
3665 snd_hda_codec_read(codec, fg, 0,
3666 AC_VERB_SET_POWER_STATE,
3667 power_state);
3668 snd_hda_codec_set_power_to_all(codec, fg, power_state,
3669 true);
3670 }
3671 state = hda_sync_power_state(codec, fg, power_state);
Wang Xingchao09617ce2012-06-08 10:26:08 +08003672 if (!(state & AC_PWRST_ERROR))
3673 break;
3674 }
Mengdong Linb8dfc4622012-08-23 17:32:30 +08003675
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003676 return state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003677}
Takashi Iwai54d17402005-11-21 16:33:22 +01003678
Takashi Iwai11aeff02008-07-30 15:01:46 +02003679#ifdef CONFIG_SND_HDA_HWDEP
3680/* execute additional init verbs */
3681static void hda_exec_init_verbs(struct hda_codec *codec)
3682{
3683 if (codec->init_verbs.list)
3684 snd_hda_sequence_write(codec, codec->init_verbs.list);
3685}
3686#else
3687static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3688#endif
3689
Takashi Iwai2a439522011-07-26 09:52:50 +02003690#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003691/*
3692 * call suspend and power-down; used both from PM and power-save
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003693 * this function returns the power state in the end
Takashi Iwaicb53c622007-08-10 17:21:45 +02003694 */
Dylan Reidd17344b2012-09-28 15:57:01 -07003695static unsigned int hda_call_codec_suspend(struct hda_codec *codec, bool in_wq)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003696{
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003697 unsigned int state;
3698
Takashi Iwai989c3182012-11-19 14:14:58 +01003699 codec->in_pm = 1;
3700
Takashi Iwaicb53c622007-08-10 17:21:45 +02003701 if (codec->patch_ops.suspend)
Takashi Iwai68cb2b52012-07-02 15:20:37 +02003702 codec->patch_ops.suspend(codec);
Takashi Iwaieb541332010-08-06 13:48:11 +02003703 hda_cleanup_all_streams(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07003704 state = hda_set_power_state(codec, AC_PWRST_D3);
Dylan Reidd17344b2012-09-28 15:57:01 -07003705 /* Cancel delayed work if we aren't currently running from it. */
3706 if (!in_wq)
3707 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02003708 spin_lock(&codec->power_lock);
3709 snd_hda_update_power_acct(codec);
3710 trace_hda_power_down(codec);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02003711 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02003712 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003713 codec->power_jiffies = jiffies;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02003714 spin_unlock(&codec->power_lock);
Takashi Iwai989c3182012-11-19 14:14:58 +01003715 codec->in_pm = 0;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003716 return state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003717}
3718
Takashi Iwaic370dd62012-12-13 18:30:04 +01003719/* mark all entries of cmd and amp caches dirty */
3720static void hda_mark_cmd_cache_dirty(struct hda_codec *codec)
3721{
3722 int i;
3723 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3724 struct hda_cache_head *cmd;
3725 cmd = snd_array_elem(&codec->cmd_cache.buf, i);
3726 cmd->dirty = 1;
3727 }
3728 for (i = 0; i < codec->amp_cache.buf.used; i++) {
3729 struct hda_amp_info *amp;
3730 amp = snd_array_elem(&codec->cmd_cache.buf, i);
3731 amp->head.dirty = 1;
3732 }
3733}
3734
Takashi Iwaicb53c622007-08-10 17:21:45 +02003735/*
3736 * kick up codec; used both from PM and power-save
3737 */
3738static void hda_call_codec_resume(struct hda_codec *codec)
3739{
Takashi Iwai989c3182012-11-19 14:14:58 +01003740 codec->in_pm = 1;
3741
Takashi Iwaic370dd62012-12-13 18:30:04 +01003742 hda_mark_cmd_cache_dirty(codec);
3743
Takashi Iwai7f308302012-05-08 16:52:23 +02003744 /* set as if powered on for avoiding re-entering the resume
3745 * in the resume / power-save sequence
3746 */
3747 hda_keep_power_on(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07003748 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02003749 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02003750 hda_exec_init_verbs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003751 if (codec->patch_ops.resume)
3752 codec->patch_ops.resume(codec);
3753 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02003754 if (codec->patch_ops.init)
3755 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003756 snd_hda_codec_resume_amp(codec);
3757 snd_hda_codec_resume_cache(codec);
3758 }
David Henningsson26a6cb62012-10-09 15:04:21 +02003759
3760 if (codec->jackpoll_interval)
3761 hda_jackpoll_work(&codec->jackpoll_work.work);
3762 else {
3763 snd_hda_jack_set_dirty_all(codec);
3764 snd_hda_jack_report_sync(codec);
3765 }
Takashi Iwai989c3182012-11-19 14:14:58 +01003766
3767 codec->in_pm = 0;
Takashi Iwai7f308302012-05-08 16:52:23 +02003768 snd_hda_power_down(codec); /* flag down before returning */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003769}
Takashi Iwai2a439522011-07-26 09:52:50 +02003770#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003771
Takashi Iwai54d17402005-11-21 16:33:22 +01003772
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773/**
3774 * snd_hda_build_controls - build mixer controls
3775 * @bus: the BUS
3776 *
3777 * Creates mixer controls for each codec included in the bus.
3778 *
3779 * Returns 0 if successful, otherwise a negative error code.
3780 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01003781int snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003783 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784
Takashi Iwai0ba21762007-04-16 11:29:14 +02003785 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003786 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003787 if (err < 0) {
Takashi Iwai28d1a852010-03-15 09:05:46 +01003788 printk(KERN_ERR "hda_codec: cannot build controls "
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003789 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003790 err = snd_hda_codec_reset(codec);
3791 if (err < 0) {
3792 printk(KERN_ERR
3793 "hda_codec: cannot revert codec\n");
3794 return err;
3795 }
3796 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003798 return 0;
3799}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003800EXPORT_SYMBOL_HDA(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003802/*
3803 * add standard channel maps if not specified
3804 */
3805static int add_std_chmaps(struct hda_codec *codec)
3806{
3807 int i, str, err;
3808
3809 for (i = 0; i < codec->num_pcms; i++) {
3810 for (str = 0; str < 2; str++) {
3811 struct snd_pcm *pcm = codec->pcm_info[i].pcm;
3812 struct hda_pcm_stream *hinfo =
3813 &codec->pcm_info[i].stream[str];
3814 struct snd_pcm_chmap *chmap;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003815 const struct snd_pcm_chmap_elem *elem;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003816
3817 if (codec->pcm_info[i].own_chmap)
3818 continue;
3819 if (!pcm || !hinfo->substreams)
3820 continue;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003821 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
3822 err = snd_pcm_add_chmap_ctls(pcm, str, elem,
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003823 hinfo->channels_max,
3824 0, &chmap);
3825 if (err < 0)
3826 return err;
3827 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
3828 }
3829 }
3830 return 0;
3831}
3832
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003833/* default channel maps for 2.1 speakers;
3834 * since HD-audio supports only stereo, odd number channels are omitted
3835 */
3836const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
3837 { .channels = 2,
3838 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
3839 { .channels = 4,
3840 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
3841 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
3842 { }
3843};
3844EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
3845
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003846int snd_hda_codec_build_controls(struct hda_codec *codec)
3847{
3848 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02003849 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003850 /* continue to initialize... */
3851 if (codec->patch_ops.init)
3852 err = codec->patch_ops.init(codec);
3853 if (!err && codec->patch_ops.build_controls)
3854 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003855 if (err < 0)
3856 return err;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003857
3858 /* we create chmaps here instead of build_pcms */
3859 err = add_std_chmaps(codec);
3860 if (err < 0)
3861 return err;
3862
David Henningsson26a6cb62012-10-09 15:04:21 +02003863 if (codec->jackpoll_interval)
3864 hda_jackpoll_work(&codec->jackpoll_work.work);
3865 else
3866 snd_hda_jack_report_sync(codec); /* call at the last init point */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867 return 0;
3868}
3869
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870/*
3871 * stream formats
3872 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02003873struct hda_rate_tbl {
3874 unsigned int hz;
3875 unsigned int alsa_bits;
3876 unsigned int hda_fmt;
3877};
3878
Takashi Iwai92f10b32010-08-03 14:21:00 +02003879/* rate = base * mult / div */
3880#define HDA_RATE(base, mult, div) \
3881 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
3882 (((div) - 1) << AC_FMT_DIV_SHIFT))
3883
Takashi Iwaibefdf312005-08-22 13:57:55 +02003884static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003886
3887 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003888 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
3889 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
3890 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
3891 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
3892 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
3893 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
3894 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
3895 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
3896 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
3897 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
3898 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003899#define AC_PAR_PCM_RATE_BITS 11
3900 /* up to bits 10, 384kHZ isn't supported properly */
3901
3902 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003903 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003904
Takashi Iwaibefdf312005-08-22 13:57:55 +02003905 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906};
3907
3908/**
3909 * snd_hda_calc_stream_format - calculate format bitset
3910 * @rate: the sample rate
3911 * @channels: the number of channels
3912 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3913 * @maxbps: the max. bps
3914 *
3915 * Calculate the format bitset from the given rate, channels and th PCM format.
3916 *
3917 * Return zero if invalid.
3918 */
3919unsigned int snd_hda_calc_stream_format(unsigned int rate,
3920 unsigned int channels,
3921 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03003922 unsigned int maxbps,
3923 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924{
3925 int i;
3926 unsigned int val = 0;
3927
Takashi Iwaibefdf312005-08-22 13:57:55 +02003928 for (i = 0; rate_bits[i].hz; i++)
3929 if (rate_bits[i].hz == rate) {
3930 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931 break;
3932 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003933 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934 snd_printdd("invalid rate %d\n", rate);
3935 return 0;
3936 }
3937
3938 if (channels == 0 || channels > 8) {
3939 snd_printdd("invalid channels %d\n", channels);
3940 return 0;
3941 }
3942 val |= channels - 1;
3943
3944 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003945 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003946 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003947 break;
3948 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003949 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003950 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 case 20:
3952 case 24:
3953 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003954 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003955 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003957 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02003959 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960 break;
3961 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003962 snd_printdd("invalid format width %d\n",
3963 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964 return 0;
3965 }
3966
Anssi Hannula32c168c2010-08-03 13:28:57 +03003967 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003968 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03003969
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970 return val;
3971}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003972EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003974static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid,
3975 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003976{
3977 unsigned int val = 0;
3978 if (nid != codec->afg &&
3979 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
3980 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
3981 if (!val || val == -1)
3982 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
3983 if (!val || val == -1)
3984 return 0;
3985 return val;
3986}
3987
3988static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3989{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003990 return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003991 get_pcm_param);
3992}
3993
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003994static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid,
3995 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003996{
3997 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
3998 if (!streams || streams == -1)
3999 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
4000 if (!streams || streams == -1)
4001 return 0;
4002 return streams;
4003}
4004
4005static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
4006{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004007 return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004008 get_stream_param);
4009}
4010
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011/**
4012 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
4013 * @codec: the HDA codec
4014 * @nid: NID to query
4015 * @ratesp: the pointer to store the detected rate bitflags
4016 * @formatsp: the pointer to store the detected formats
4017 * @bpsp: the pointer to store the detected format widths
4018 *
4019 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
4020 * or @bsps argument is ignored.
4021 *
4022 * Returns 0 if successful, otherwise a negative error code.
4023 */
Stephen Warren384a48d2011-06-01 11:14:21 -06004024int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
4026{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004027 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004029 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004030 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031
4032 if (ratesp) {
4033 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004034 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02004036 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004038 if (rates == 0) {
4039 snd_printk(KERN_ERR "hda_codec: rates == 0 "
4040 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
4041 nid, val,
4042 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
4043 return -EIO;
4044 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045 *ratesp = rates;
4046 }
4047
4048 if (formatsp || bpsp) {
4049 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004050 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004052 streams = query_stream_param(codec, nid);
4053 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055
4056 bps = 0;
4057 if (streams & AC_SUPFMT_PCM) {
4058 if (val & AC_SUPPCM_BITS_8) {
4059 formats |= SNDRV_PCM_FMTBIT_U8;
4060 bps = 8;
4061 }
4062 if (val & AC_SUPPCM_BITS_16) {
4063 formats |= SNDRV_PCM_FMTBIT_S16_LE;
4064 bps = 16;
4065 }
4066 if (wcaps & AC_WCAP_DIGITAL) {
4067 if (val & AC_SUPPCM_BITS_32)
4068 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
4069 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
4070 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4071 if (val & AC_SUPPCM_BITS_24)
4072 bps = 24;
4073 else if (val & AC_SUPPCM_BITS_20)
4074 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004075 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
4076 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4078 if (val & AC_SUPPCM_BITS_32)
4079 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080 else if (val & AC_SUPPCM_BITS_24)
4081 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02004082 else if (val & AC_SUPPCM_BITS_20)
4083 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084 }
4085 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004086#if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
Takashi Iwaib5025c52009-07-01 18:05:27 +02004087 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004089 if (!bps)
4090 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02004091 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004092#endif
Takashi Iwaib5025c52009-07-01 18:05:27 +02004093 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02004094 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 /* temporary hack: we have still no proper support
4096 * for the direct AC3 stream...
4097 */
4098 formats |= SNDRV_PCM_FMTBIT_U8;
4099 bps = 8;
4100 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004101 if (formats == 0) {
4102 snd_printk(KERN_ERR "hda_codec: formats == 0 "
4103 "(nid=0x%x, val=0x%x, ovrd=%i, "
4104 "streams=0x%x)\n",
4105 nid, val,
4106 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
4107 streams);
4108 return -EIO;
4109 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110 if (formatsp)
4111 *formatsp = formats;
4112 if (bpsp)
4113 *bpsp = bps;
4114 }
4115
4116 return 0;
4117}
Stephen Warren384a48d2011-06-01 11:14:21 -06004118EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119
4120/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01004121 * snd_hda_is_supported_format - Check the validity of the format
4122 * @codec: HD-audio codec
4123 * @nid: NID to check
4124 * @format: the HD-audio format value to check
4125 *
4126 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127 *
4128 * Returns 1 if supported, 0 if not.
4129 */
4130int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
4131 unsigned int format)
4132{
4133 int i;
4134 unsigned int val = 0, rate, stream;
4135
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004136 val = query_pcm_param(codec, nid);
4137 if (!val)
4138 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139
4140 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004141 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02004142 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 if (val & (1 << i))
4144 break;
4145 return 0;
4146 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004147 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148 return 0;
4149
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004150 stream = query_stream_param(codec, nid);
4151 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152 return 0;
4153
4154 if (stream & AC_SUPFMT_PCM) {
4155 switch (format & 0xf0) {
4156 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004157 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 return 0;
4159 break;
4160 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004161 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162 return 0;
4163 break;
4164 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004165 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166 return 0;
4167 break;
4168 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004169 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 return 0;
4171 break;
4172 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004173 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174 return 0;
4175 break;
4176 default:
4177 return 0;
4178 }
4179 } else {
4180 /* FIXME: check for float32 and AC3? */
4181 }
4182
4183 return 1;
4184}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004185EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186
4187/*
4188 * PCM stuff
4189 */
4190static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
4191 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004192 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193{
4194 return 0;
4195}
4196
4197static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
4198 struct hda_codec *codec,
4199 unsigned int stream_tag,
4200 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004201 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202{
4203 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4204 return 0;
4205}
4206
4207static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
4208 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004209 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210{
Takashi Iwai888afa12008-03-18 09:57:50 +01004211 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212 return 0;
4213}
4214
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004215static int set_pcm_default_values(struct hda_codec *codec,
4216 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004218 int err;
4219
Takashi Iwai0ba21762007-04-16 11:29:14 +02004220 /* query support PCM information from the given NID */
4221 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004222 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02004223 info->rates ? NULL : &info->rates,
4224 info->formats ? NULL : &info->formats,
4225 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004226 if (err < 0)
4227 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228 }
4229 if (info->ops.open == NULL)
4230 info->ops.open = hda_pcm_default_open_close;
4231 if (info->ops.close == NULL)
4232 info->ops.close = hda_pcm_default_open_close;
4233 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004234 if (snd_BUG_ON(!info->nid))
4235 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236 info->ops.prepare = hda_pcm_default_prepare;
4237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004239 if (snd_BUG_ON(!info->nid))
4240 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241 info->ops.cleanup = hda_pcm_default_cleanup;
4242 }
4243 return 0;
4244}
4245
Takashi Iwaieb541332010-08-06 13:48:11 +02004246/*
4247 * codec prepare/cleanup entries
4248 */
4249int snd_hda_codec_prepare(struct hda_codec *codec,
4250 struct hda_pcm_stream *hinfo,
4251 unsigned int stream,
4252 unsigned int format,
4253 struct snd_pcm_substream *substream)
4254{
4255 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004256 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004257 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
4258 if (ret >= 0)
4259 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004260 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004261 return ret;
4262}
4263EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
4264
4265void snd_hda_codec_cleanup(struct hda_codec *codec,
4266 struct hda_pcm_stream *hinfo,
4267 struct snd_pcm_substream *substream)
4268{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004269 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004270 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004271 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004272}
4273EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
4274
Takashi Iwaid5191e52009-11-16 14:58:17 +01004275/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01004276const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
4277 "Audio", "SPDIF", "HDMI", "Modem"
4278};
4279
Takashi Iwai176d5332008-07-30 15:01:44 +02004280/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004281 * get the empty PCM device number to assign
Takashi Iwaic8936222010-01-28 17:08:53 +01004282 *
4283 * note the max device number is limited by HDA_MAX_PCMS, currently 10
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004284 */
4285static int get_empty_pcm_device(struct hda_bus *bus, int type)
4286{
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004287 /* audio device indices; not linear to keep compatibility */
4288 static int audio_idx[HDA_PCM_NTYPES][5] = {
4289 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
4290 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01004291 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004292 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004293 };
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004294 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004295
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004296 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004297 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
4298 return -EINVAL;
4299 }
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004300
4301 for (i = 0; audio_idx[type][i] >= 0 ; i++)
4302 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
4303 return audio_idx[type][i];
4304
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004305 /* non-fixed slots starting from 10 */
4306 for (i = 10; i < 32; i++) {
4307 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4308 return i;
4309 }
4310
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004311 snd_printk(KERN_WARNING "Too many %s devices\n",
4312 snd_hda_pcm_type_name[type]);
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004313 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004314}
4315
4316/*
Takashi Iwai176d5332008-07-30 15:01:44 +02004317 * attach a new PCM stream
4318 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004319static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02004320{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004321 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02004322 struct hda_pcm_stream *info;
4323 int stream, err;
4324
Takashi Iwaib91f0802008-11-04 08:43:08 +01004325 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02004326 return -EINVAL;
4327 for (stream = 0; stream < 2; stream++) {
4328 info = &pcm->stream[stream];
4329 if (info->substreams) {
4330 err = set_pcm_default_values(codec, info);
4331 if (err < 0)
4332 return err;
4333 }
4334 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004335 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02004336}
4337
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004338/* assign all PCMs of the given codec */
4339int snd_hda_codec_build_pcms(struct hda_codec *codec)
4340{
4341 unsigned int pcm;
4342 int err;
4343
4344 if (!codec->num_pcms) {
4345 if (!codec->patch_ops.build_pcms)
4346 return 0;
4347 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004348 if (err < 0) {
4349 printk(KERN_ERR "hda_codec: cannot build PCMs"
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004350 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004351 err = snd_hda_codec_reset(codec);
4352 if (err < 0) {
4353 printk(KERN_ERR
4354 "hda_codec: cannot revert codec\n");
4355 return err;
4356 }
4357 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004358 }
4359 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
4360 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
4361 int dev;
4362
4363 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01004364 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004365
4366 if (!cpcm->pcm) {
4367 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
4368 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004369 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004370 cpcm->device = dev;
4371 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004372 if (err < 0) {
4373 printk(KERN_ERR "hda_codec: cannot attach "
4374 "PCM stream %d for codec #%d\n",
4375 dev, codec->addr);
4376 continue; /* no fatal error */
4377 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004378 }
4379 }
4380 return 0;
4381}
4382
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383/**
4384 * snd_hda_build_pcms - build PCM information
4385 * @bus: the BUS
4386 *
4387 * Create PCM information for each codec included in the bus.
4388 *
4389 * The build_pcms codec patch is requested to set up codec->num_pcms and
4390 * codec->pcm_info properly. The array is referred by the top-level driver
4391 * to create its PCM instances.
4392 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4393 * callback.
4394 *
4395 * At least, substreams, channels_min and channels_max must be filled for
4396 * each stream. substreams = 0 indicates that the stream doesn't exist.
4397 * When rates and/or formats are zero, the supported values are queried
4398 * from the given nid. The nid is used also by the default ops.prepare
4399 * and ops.cleanup callbacks.
4400 *
4401 * The driver needs to call ops.open in its open callback. Similarly,
4402 * ops.close is supposed to be called in the close callback.
4403 * ops.prepare should be called in the prepare or hw_params callback
4404 * with the proper parameters for set up.
4405 * ops.cleanup should be called in hw_free for clean up of streams.
4406 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004407 * This function returns 0 if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408 */
Takashi Iwai5cb543d2012-08-09 13:49:23 +02004409int snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004411 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412
Takashi Iwai0ba21762007-04-16 11:29:14 +02004413 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004414 int err = snd_hda_codec_build_pcms(codec);
4415 if (err < 0)
4416 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417 }
4418 return 0;
4419}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004420EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422/**
4423 * snd_hda_check_board_config - compare the current codec with the config table
4424 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004425 * @num_configs: number of config enums
4426 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427 * @tbl: configuration table, terminated by null entries
4428 *
4429 * Compares the modelname or PCI subsystem id of the current codec with the
4430 * given configuration table. If a matching entry is found, returns its
4431 * config value (supposed to be 0 or positive).
4432 *
4433 * If no entries are matching, the function returns a negative value.
4434 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02004435int snd_hda_check_board_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004436 int num_configs, const char * const *models,
Takashi Iwai12f288b2007-08-02 15:51:59 +02004437 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438{
Takashi Iwaif44ac832008-07-30 15:01:45 +02004439 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004440 int i;
4441 for (i = 0; i < num_configs; i++) {
4442 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02004443 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004444 snd_printd(KERN_INFO "hda_codec: model '%s' is "
4445 "selected\n", models[i]);
4446 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447 }
4448 }
4449 }
4450
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004451 if (!codec->bus->pci || !tbl)
4452 return -1;
4453
4454 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
4455 if (!tbl)
4456 return -1;
4457 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02004458#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004459 char tmp[10];
4460 const char *model = NULL;
4461 if (models)
4462 model = models[tbl->value];
4463 if (!model) {
4464 sprintf(tmp, "#%d", tbl->value);
4465 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004467 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4468 "for config %x:%x (%s)\n",
4469 model, tbl->subvendor, tbl->subdevice,
4470 (tbl->name ? tbl->name : "Unknown device"));
4471#endif
4472 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473 }
4474 return -1;
4475}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004476EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477
4478/**
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004479 * snd_hda_check_board_codec_sid_config - compare the current codec
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004480 subsystem ID with the
4481 config table
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004482
4483 This is important for Gateway notebooks with SB450 HDA Audio
4484 where the vendor ID of the PCI device is:
4485 ATI Technologies Inc SB450 HDA Audio [1002:437b]
4486 and the vendor/subvendor are found only at the codec.
4487
4488 * @codec: the HDA codec
4489 * @num_configs: number of config enums
4490 * @models: array of model name strings
4491 * @tbl: configuration table, terminated by null entries
4492 *
4493 * Compares the modelname or PCI subsystem id of the current codec with the
4494 * given configuration table. If a matching entry is found, returns its
4495 * config value (supposed to be 0 or positive).
4496 *
4497 * If no entries are matching, the function returns a negative value.
4498 */
4499int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004500 int num_configs, const char * const *models,
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004501 const struct snd_pci_quirk *tbl)
4502{
4503 const struct snd_pci_quirk *q;
4504
4505 /* Search for codec ID */
4506 for (q = tbl; q->subvendor; q++) {
Takashi Iwaie2301a42011-11-22 19:58:56 +01004507 unsigned int mask = 0xffff0000 | q->subdevice_mask;
4508 unsigned int id = (q->subdevice | (q->subvendor << 16)) & mask;
4509 if ((codec->subsystem_id & mask) == id)
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004510 break;
4511 }
4512
4513 if (!q->subvendor)
4514 return -1;
4515
4516 tbl = q;
4517
4518 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwaid94ff6b2009-09-02 00:20:21 +02004519#ifdef CONFIG_SND_DEBUG_VERBOSE
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004520 char tmp[10];
4521 const char *model = NULL;
4522 if (models)
4523 model = models[tbl->value];
4524 if (!model) {
4525 sprintf(tmp, "#%d", tbl->value);
4526 model = tmp;
4527 }
4528 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4529 "for config %x:%x (%s)\n",
4530 model, tbl->subvendor, tbl->subdevice,
4531 (tbl->name ? tbl->name : "Unknown device"));
4532#endif
4533 return tbl->value;
4534 }
4535 return -1;
4536}
4537EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
4538
4539/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540 * snd_hda_add_new_ctls - create controls from the array
4541 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004542 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543 *
4544 * This helper function creates and add new controls in the given array.
4545 * The array must be terminated with an empty entry as terminator.
4546 *
4547 * Returns 0 if successful, or a negative error code.
4548 */
Takashi Iwai031024e2011-05-02 11:29:30 +02004549int snd_hda_add_new_ctls(struct hda_codec *codec,
4550 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01004552 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553
4554 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004555 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004556 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01004557 if (knew->iface == -1) /* skip this codec private value */
4558 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004559 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004560 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02004561 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01004562 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004563 if (addr > 0)
4564 kctl->id.device = addr;
4565 if (idx > 0)
4566 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01004567 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004568 if (!err)
4569 break;
4570 /* try first with another device index corresponding to
4571 * the codec addr; if it still fails (or it's the
4572 * primary codec), then try another control index
4573 */
4574 if (!addr && codec->addr)
4575 addr = codec->addr;
4576 else if (!idx && !knew->index) {
4577 idx = find_empty_mixer_ctl_idx(codec,
Takashi Iwaidcda5802012-10-12 17:24:51 +02004578 knew->name, 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004579 if (idx <= 0)
4580 return err;
4581 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01004582 return err;
4583 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584 }
4585 return 0;
4586}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004587EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588
Takashi Iwai83012a72012-08-24 18:38:08 +02004589#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02004590static void hda_power_work(struct work_struct *work)
4591{
4592 struct hda_codec *codec =
4593 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004594 struct hda_bus *bus = codec->bus;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004595 unsigned int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004596
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004597 spin_lock(&codec->power_lock);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004598 if (codec->power_transition > 0) { /* during power-up sequence? */
4599 spin_unlock(&codec->power_lock);
4600 return;
4601 }
Maxim Levitsky2e492462007-09-03 15:26:57 +02004602 if (!codec->power_on || codec->power_count) {
4603 codec->power_transition = 0;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004604 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004605 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02004606 }
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004607 spin_unlock(&codec->power_lock);
4608
Dylan Reidd17344b2012-09-28 15:57:01 -07004609 state = hda_call_codec_suspend(codec, true);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004610 codec->pm_down_notified = 0;
4611 if (!bus->power_keep_link_on && (state & AC_PWRST_CLK_STOP_OK)) {
4612 codec->pm_down_notified = 1;
Takashi Iwai68467f52012-08-28 09:14:29 -07004613 hda_call_pm_notify(bus, false);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004614 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02004615}
4616
4617static void hda_keep_power_on(struct hda_codec *codec)
4618{
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004619 spin_lock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004620 codec->power_count++;
4621 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004622 codec->power_jiffies = jiffies;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004623 spin_unlock(&codec->power_lock);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004624}
4625
Takashi Iwaid5191e52009-11-16 14:58:17 +01004626/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01004627void snd_hda_update_power_acct(struct hda_codec *codec)
4628{
4629 unsigned long delta = jiffies - codec->power_jiffies;
4630 if (codec->power_on)
4631 codec->power_on_acct += delta;
4632 else
4633 codec->power_off_acct += delta;
4634 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004635}
4636
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004637/* Transition to powered up, if wait_power_down then wait for a pending
4638 * transition to D3 to complete. A pending D3 transition is indicated
4639 * with power_transition == -1. */
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004640/* call this with codec->power_lock held! */
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004641static void __snd_hda_power_up(struct hda_codec *codec, bool wait_power_down)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004642{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004643 struct hda_bus *bus = codec->bus;
4644
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004645 /* Return if power_on or transitioning to power_on, unless currently
4646 * powering down. */
4647 if ((codec->power_on || codec->power_transition > 0) &&
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004648 !(wait_power_down && codec->power_transition < 0))
Takashi Iwaicb53c622007-08-10 17:21:45 +02004649 return;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004650 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004651
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004652 cancel_delayed_work_sync(&codec->power_work);
4653
4654 spin_lock(&codec->power_lock);
Dylan Reidb43d2242012-06-21 21:51:22 -07004655 /* If the power down delayed work was cancelled above before starting,
4656 * then there is no need to go through power up here.
4657 */
4658 if (codec->power_on) {
Takashi Iwai535b6c52012-08-20 21:25:22 +02004659 if (codec->power_transition < 0)
4660 codec->power_transition = 0;
Dylan Reidb43d2242012-06-21 21:51:22 -07004661 return;
4662 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004663
Takashi Iwaid66fee52011-08-02 15:39:31 +02004664 trace_hda_power_up(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004665 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004666 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004667 codec->power_jiffies = jiffies;
Takashi Iwai7f308302012-05-08 16:52:23 +02004668 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004669 spin_unlock(&codec->power_lock);
4670
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004671 if (codec->pm_down_notified) {
4672 codec->pm_down_notified = 0;
Takashi Iwai68467f52012-08-28 09:14:29 -07004673 hda_call_pm_notify(bus, true);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004674 }
4675
Takashi Iwaicb53c622007-08-10 17:21:45 +02004676 hda_call_codec_resume(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004677
4678 spin_lock(&codec->power_lock);
Takashi Iwaia221e282007-08-16 16:35:33 +02004679 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004680}
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004681
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004682#define power_save(codec) \
4683 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004684
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004685/* Transition to powered down */
4686static void __snd_hda_power_down(struct hda_codec *codec)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004687{
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004688 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004689 return;
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004690
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004691 if (power_save(codec)) {
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004692 codec->power_transition = -1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01004693 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004694 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02004695 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004696}
4697
4698/**
4699 * snd_hda_power_save - Power-up/down/sync the codec
4700 * @codec: HD-audio codec
4701 * @delta: the counter delta to change
4702 *
4703 * Change the power-up counter via @delta, and power up or down the hardware
4704 * appropriately. For the power-down, queue to the delayed action.
4705 * Passing zero to @delta means to synchronize the power state.
4706 */
4707void snd_hda_power_save(struct hda_codec *codec, int delta, bool d3wait)
4708{
4709 spin_lock(&codec->power_lock);
4710 codec->power_count += delta;
4711 trace_hda_power_count(codec);
4712 if (delta > 0)
4713 __snd_hda_power_up(codec, d3wait);
4714 else
4715 __snd_hda_power_down(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004716 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004717}
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004718EXPORT_SYMBOL_HDA(snd_hda_power_save);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004719
Takashi Iwaid5191e52009-11-16 14:58:17 +01004720/**
4721 * snd_hda_check_amp_list_power - Check the amp list and update the power
4722 * @codec: HD-audio codec
4723 * @check: the object containing an AMP list and the status
4724 * @nid: NID to check / update
4725 *
4726 * Check whether the given NID is in the amp list. If it's in the list,
4727 * check the current AMP status, and update the the power-status according
4728 * to the mute status.
4729 *
4730 * This function is supposed to be set or called from the check_power_status
4731 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004732 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004733int snd_hda_check_amp_list_power(struct hda_codec *codec,
4734 struct hda_loopback_check *check,
4735 hda_nid_t nid)
4736{
Takashi Iwai031024e2011-05-02 11:29:30 +02004737 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004738 int ch, v;
4739
4740 if (!check->amplist)
4741 return 0;
4742 for (p = check->amplist; p->nid; p++) {
4743 if (p->nid == nid)
4744 break;
4745 }
4746 if (!p->nid)
4747 return 0; /* nothing changed */
4748
4749 for (p = check->amplist; p->nid; p++) {
4750 for (ch = 0; ch < 2; ch++) {
4751 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4752 p->idx);
4753 if (!(v & HDA_AMP_MUTE) && v > 0) {
4754 if (!check->power_on) {
4755 check->power_on = 1;
4756 snd_hda_power_up(codec);
4757 }
4758 return 1;
4759 }
4760 }
4761 }
4762 if (check->power_on) {
4763 check->power_on = 0;
4764 snd_hda_power_down(codec);
4765 }
4766 return 0;
4767}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004768EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004769#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004771/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004772 * Channel mode helper
4773 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004774
4775/**
4776 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
4777 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004778int snd_hda_ch_mode_info(struct hda_codec *codec,
4779 struct snd_ctl_elem_info *uinfo,
4780 const struct hda_channel_mode *chmode,
4781 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004782{
4783 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4784 uinfo->count = 1;
4785 uinfo->value.enumerated.items = num_chmodes;
4786 if (uinfo->value.enumerated.item >= num_chmodes)
4787 uinfo->value.enumerated.item = num_chmodes - 1;
4788 sprintf(uinfo->value.enumerated.name, "%dch",
4789 chmode[uinfo->value.enumerated.item].channels);
4790 return 0;
4791}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004792EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004793
Takashi Iwaid5191e52009-11-16 14:58:17 +01004794/**
4795 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
4796 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004797int snd_hda_ch_mode_get(struct hda_codec *codec,
4798 struct snd_ctl_elem_value *ucontrol,
4799 const struct hda_channel_mode *chmode,
4800 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004801 int max_channels)
4802{
4803 int i;
4804
4805 for (i = 0; i < num_chmodes; i++) {
4806 if (max_channels == chmode[i].channels) {
4807 ucontrol->value.enumerated.item[0] = i;
4808 break;
4809 }
4810 }
4811 return 0;
4812}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004813EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004814
Takashi Iwaid5191e52009-11-16 14:58:17 +01004815/**
4816 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
4817 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004818int snd_hda_ch_mode_put(struct hda_codec *codec,
4819 struct snd_ctl_elem_value *ucontrol,
4820 const struct hda_channel_mode *chmode,
4821 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004822 int *max_channelsp)
4823{
4824 unsigned int mode;
4825
4826 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01004827 if (mode >= num_chmodes)
4828 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004829 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004830 return 0;
4831 /* change the current channel setting */
4832 *max_channelsp = chmode[mode].channels;
4833 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004834 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004835 return 1;
4836}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004837EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004838
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839/*
4840 * input MUX helper
4841 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004842
4843/**
4844 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
4845 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004846int snd_hda_input_mux_info(const struct hda_input_mux *imux,
4847 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848{
4849 unsigned int index;
4850
4851 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4852 uinfo->count = 1;
4853 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004854 if (!imux->num_items)
4855 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856 index = uinfo->value.enumerated.item;
4857 if (index >= imux->num_items)
4858 index = imux->num_items - 1;
4859 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
4860 return 0;
4861}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004862EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863
Takashi Iwaid5191e52009-11-16 14:58:17 +01004864/**
4865 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
4866 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004867int snd_hda_input_mux_put(struct hda_codec *codec,
4868 const struct hda_input_mux *imux,
4869 struct snd_ctl_elem_value *ucontrol,
4870 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871 unsigned int *cur_val)
4872{
4873 unsigned int idx;
4874
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004875 if (!imux->num_items)
4876 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877 idx = ucontrol->value.enumerated.item[0];
4878 if (idx >= imux->num_items)
4879 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004880 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004882 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
4883 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884 *cur_val = idx;
4885 return 1;
4886}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004887EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888
4889
4890/*
Takashi Iwaidda415d2012-11-30 18:34:38 +01004891 * process kcontrol info callback of a simple string enum array
4892 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
4893 */
4894int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
4895 struct snd_ctl_elem_info *uinfo,
4896 int num_items, const char * const *texts)
4897{
4898 static const char * const texts_default[] = {
4899 "Disabled", "Enabled"
4900 };
4901
4902 if (!texts || !num_items) {
4903 num_items = 2;
4904 texts = texts_default;
4905 }
4906
4907 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4908 uinfo->count = 1;
4909 uinfo->value.enumerated.items = num_items;
4910 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
4911 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
4912 strcpy(uinfo->value.enumerated.name,
4913 texts[uinfo->value.enumerated.item]);
4914 return 0;
4915}
4916EXPORT_SYMBOL_HDA(snd_hda_enum_helper_info);
4917
4918/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919 * Multi-channel / digital-out PCM helper functions
4920 */
4921
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004922/* setup SPDIF output stream */
4923static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
4924 unsigned int stream_tag, unsigned int format)
4925{
Laurence Darby3bef1c32012-11-03 17:00:06 +00004926 struct hda_spdif_out *spdif;
4927 unsigned int curr_fmt;
4928 bool reset;
Stephen Warren7c935972011-06-01 11:14:17 -06004929
Laurence Darby3bef1c32012-11-03 17:00:06 +00004930 spdif = snd_hda_spdif_out_of_nid(codec, nid);
4931 curr_fmt = snd_hda_codec_read(codec, nid, 0,
4932 AC_VERB_GET_STREAM_FORMAT, 0);
4933 reset = codec->spdif_status_reset &&
4934 (spdif->ctls & AC_DIG1_ENABLE) &&
4935 curr_fmt != format;
4936
4937 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
4938 updated */
4939 if (reset)
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004940 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06004941 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02004942 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004943 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02004944 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004945 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004946 for (d = codec->slave_dig_outs; *d; d++)
4947 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
4948 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004949 }
Takashi Iwai2f728532008-09-25 16:32:41 +02004950 /* turn on again (if needed) */
Laurence Darby3bef1c32012-11-03 17:00:06 +00004951 if (reset)
Takashi Iwai2f728532008-09-25 16:32:41 +02004952 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06004953 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02004954}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004955
Takashi Iwai2f728532008-09-25 16:32:41 +02004956static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
4957{
4958 snd_hda_codec_cleanup_stream(codec, nid);
4959 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004960 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004961 for (d = codec->slave_dig_outs; *d; d++)
4962 snd_hda_codec_cleanup_stream(codec, *d);
4963 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004964}
4965
Takashi Iwaid5191e52009-11-16 14:58:17 +01004966/**
4967 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
4968 * @bus: HD-audio bus
4969 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004970void snd_hda_bus_reboot_notify(struct hda_bus *bus)
4971{
4972 struct hda_codec *codec;
4973
4974 if (!bus)
4975 return;
4976 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02004977 if (hda_codec_is_power_on(codec) &&
4978 codec->patch_ops.reboot_notify)
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004979 codec->patch_ops.reboot_notify(codec);
4980 }
4981}
Takashi Iwai8f217a22009-11-10 18:26:12 +01004982EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004983
Takashi Iwaid5191e52009-11-16 14:58:17 +01004984/**
4985 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004987int snd_hda_multi_out_dig_open(struct hda_codec *codec,
4988 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989{
Ingo Molnar62932df2006-01-16 16:34:20 +01004990 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02004991 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
4992 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02004993 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01004995 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996 return 0;
4997}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004998EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999
Takashi Iwaid5191e52009-11-16 14:58:17 +01005000/**
5001 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
5002 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005003int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
5004 struct hda_multi_out *mout,
5005 unsigned int stream_tag,
5006 unsigned int format,
5007 struct snd_pcm_substream *substream)
5008{
5009 mutex_lock(&codec->spdif_mutex);
5010 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
5011 mutex_unlock(&codec->spdif_mutex);
5012 return 0;
5013}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005014EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005015
Takashi Iwaid5191e52009-11-16 14:58:17 +01005016/**
5017 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
5018 */
Takashi Iwai9411e212009-02-13 11:32:28 +01005019int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
5020 struct hda_multi_out *mout)
5021{
5022 mutex_lock(&codec->spdif_mutex);
5023 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5024 mutex_unlock(&codec->spdif_mutex);
5025 return 0;
5026}
5027EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
5028
Takashi Iwaid5191e52009-11-16 14:58:17 +01005029/**
5030 * snd_hda_multi_out_dig_close - release the digital out stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005032int snd_hda_multi_out_dig_close(struct hda_codec *codec,
5033 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034{
Ingo Molnar62932df2006-01-16 16:34:20 +01005035 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01005037 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038 return 0;
5039}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005040EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041
Takashi Iwaid5191e52009-11-16 14:58:17 +01005042/**
5043 * snd_hda_multi_out_analog_open - open analog outputs
5044 *
5045 * Open analog outputs and set up the hw-constraints.
5046 * If the digital outputs can be opened as slave, open the digital
5047 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005049int snd_hda_multi_out_analog_open(struct hda_codec *codec,
5050 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01005051 struct snd_pcm_substream *substream,
5052 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053{
Takashi Iwai9a081602008-02-12 18:37:26 +01005054 struct snd_pcm_runtime *runtime = substream->runtime;
5055 runtime->hw.channels_max = mout->max_channels;
5056 if (mout->dig_out_nid) {
5057 if (!mout->analog_rates) {
5058 mout->analog_rates = hinfo->rates;
5059 mout->analog_formats = hinfo->formats;
5060 mout->analog_maxbps = hinfo->maxbps;
5061 } else {
5062 runtime->hw.rates = mout->analog_rates;
5063 runtime->hw.formats = mout->analog_formats;
5064 hinfo->maxbps = mout->analog_maxbps;
5065 }
5066 if (!mout->spdif_rates) {
5067 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
5068 &mout->spdif_rates,
5069 &mout->spdif_formats,
5070 &mout->spdif_maxbps);
5071 }
5072 mutex_lock(&codec->spdif_mutex);
5073 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02005074 if ((runtime->hw.rates & mout->spdif_rates) &&
5075 (runtime->hw.formats & mout->spdif_formats)) {
5076 runtime->hw.rates &= mout->spdif_rates;
5077 runtime->hw.formats &= mout->spdif_formats;
5078 if (mout->spdif_maxbps < hinfo->maxbps)
5079 hinfo->maxbps = mout->spdif_maxbps;
5080 } else {
5081 mout->share_spdif = 0;
5082 /* FIXME: need notify? */
5083 }
Takashi Iwai9a081602008-02-12 18:37:26 +01005084 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02005085 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01005086 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087 return snd_pcm_hw_constraint_step(substream->runtime, 0,
5088 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
5089}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005090EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005091
Takashi Iwaid5191e52009-11-16 14:58:17 +01005092/**
5093 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
5094 *
5095 * Set up the i/o for analog out.
5096 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005098int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
5099 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100 unsigned int stream_tag,
5101 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01005102 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103{
Takashi Iwaidda14412011-05-02 11:29:30 +02005104 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105 int chs = substream->runtime->channels;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005106 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107 int i;
5108
Ingo Molnar62932df2006-01-16 16:34:20 +01005109 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005110 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Takashi Iwai9a081602008-02-12 18:37:26 +01005111 if (mout->dig_out_nid && mout->share_spdif &&
5112 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02005114 snd_hda_is_supported_format(codec, mout->dig_out_nid,
5115 format) &&
Stephen Warren7c935972011-06-01 11:14:17 -06005116 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005117 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005118 setup_dig_out_stream(codec, mout->dig_out_nid,
5119 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005120 } else {
5121 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02005122 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123 }
5124 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005125 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005126
5127 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005128 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
5129 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005130 if (!mout->no_share_stream &&
5131 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005132 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005133 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
5134 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005135 /* extra outputs copied from front */
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005136 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5137 if (!mout->no_share_stream && mout->hp_out_nid[i])
5138 snd_hda_codec_setup_stream(codec,
5139 mout->hp_out_nid[i],
5140 stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005141 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02005142 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01005143 snd_hda_codec_setup_stream(codec,
5144 mout->extra_out_nid[i],
5145 stream_tag, 0, format);
5146
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147 /* surrounds */
5148 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02005149 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005150 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5151 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005152 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005153 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5154 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155 }
5156 return 0;
5157}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005158EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005159
Takashi Iwaid5191e52009-11-16 14:58:17 +01005160/**
5161 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005163int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
5164 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165{
Takashi Iwaidda14412011-05-02 11:29:30 +02005166 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005167 int i;
5168
5169 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01005170 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005171 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01005172 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005173 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5174 if (mout->hp_out_nid[i])
5175 snd_hda_codec_cleanup_stream(codec,
5176 mout->hp_out_nid[i]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005177 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
5178 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01005179 snd_hda_codec_cleanup_stream(codec,
5180 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01005181 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005182 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02005183 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184 mout->dig_out_used = 0;
5185 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005186 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005187 return 0;
5188}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005189EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005190
Takashi Iwai47408602012-04-20 13:06:53 +02005191/**
5192 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
5193 *
5194 * Guess the suitable VREF pin bits to be set as the pin-control value.
5195 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
5196 */
5197unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
5198{
5199 unsigned int pincap;
5200 unsigned int oldval;
5201 oldval = snd_hda_codec_read(codec, pin, 0,
5202 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5203 pincap = snd_hda_query_pin_caps(codec, pin);
5204 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5205 /* Exception: if the default pin setup is vref50, we give it priority */
5206 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
5207 return AC_PINCTL_VREF_80;
5208 else if (pincap & AC_PINCAP_VREF_50)
5209 return AC_PINCTL_VREF_50;
5210 else if (pincap & AC_PINCAP_VREF_100)
5211 return AC_PINCTL_VREF_100;
5212 else if (pincap & AC_PINCAP_VREF_GRD)
5213 return AC_PINCTL_VREF_GRD;
5214 return AC_PINCTL_VREF_HIZ;
5215}
5216EXPORT_SYMBOL_HDA(snd_hda_get_default_vref);
5217
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005218int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
5219 unsigned int val, bool cached)
5220{
5221 if (val) {
5222 unsigned int cap = snd_hda_query_pin_caps(codec, pin);
Takashi Iwai6942c102012-04-20 13:08:40 +02005223 if (cap && (val & AC_PINCTL_OUT_EN)) {
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005224 if (!(cap & AC_PINCAP_OUT))
5225 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
5226 else if ((val & AC_PINCTL_HP_EN) &&
5227 !(cap & AC_PINCAP_HP_DRV))
5228 val &= ~AC_PINCTL_HP_EN;
5229 }
Takashi Iwai6942c102012-04-20 13:08:40 +02005230 if (cap && (val & AC_PINCTL_IN_EN)) {
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005231 if (!(cap & AC_PINCAP_IN))
5232 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
5233 }
5234 }
5235 if (cached)
5236 return snd_hda_codec_update_cache(codec, pin, 0,
5237 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5238 else
5239 return snd_hda_codec_write(codec, pin, 0,
5240 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5241}
5242EXPORT_SYMBOL_HDA(_snd_hda_set_pin_ctl);
5243
Takashi Iwai990061c2010-09-09 22:08:44 +02005244/**
5245 * snd_hda_add_imux_item - Add an item to input_mux
5246 *
5247 * When the same label is used already in the existing items, the number
5248 * suffix is appended to the label. This label index number is stored
5249 * to type_idx when non-NULL pointer is given.
5250 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02005251int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
5252 int index, int *type_idx)
5253{
5254 int i, label_idx = 0;
5255 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
5256 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
5257 return -EINVAL;
5258 }
5259 for (i = 0; i < imux->num_items; i++) {
5260 if (!strncmp(label, imux->items[i].label, strlen(label)))
5261 label_idx++;
5262 }
5263 if (type_idx)
5264 *type_idx = label_idx;
5265 if (label_idx > 0)
5266 snprintf(imux->items[imux->num_items].label,
5267 sizeof(imux->items[imux->num_items].label),
5268 "%s %d", label, label_idx);
5269 else
5270 strlcpy(imux->items[imux->num_items].label, label,
5271 sizeof(imux->items[imux->num_items].label));
5272 imux->items[imux->num_items].index = index;
5273 imux->num_items++;
5274 return 0;
5275}
5276EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005277
Takashi Iwai4a471b72005-12-07 13:56:29 +01005278
Linus Torvalds1da177e2005-04-16 15:20:36 -07005279#ifdef CONFIG_PM
5280/*
5281 * power management
5282 */
5283
5284/**
5285 * snd_hda_suspend - suspend the codecs
5286 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287 *
5288 * Returns 0 if successful.
5289 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02005290int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005291{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005292 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005293
Takashi Iwai0ba21762007-04-16 11:29:14 +02005294 list_for_each_entry(codec, &bus->codec_list, list) {
David Henningsson26a6cb62012-10-09 15:04:21 +02005295 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005296 if (hda_codec_is_power_on(codec))
Dylan Reidd17344b2012-09-28 15:57:01 -07005297 hda_call_codec_suspend(codec, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298 }
5299 return 0;
5300}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005301EXPORT_SYMBOL_HDA(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005302
5303/**
5304 * snd_hda_resume - resume the codecs
5305 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005306 *
5307 * Returns 0 if successful.
5308 */
5309int snd_hda_resume(struct hda_bus *bus)
5310{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005311 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005312
Takashi Iwai0ba21762007-04-16 11:29:14 +02005313 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai7f308302012-05-08 16:52:23 +02005314 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316 return 0;
5317}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005318EXPORT_SYMBOL_HDA(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005319#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005320
5321/*
5322 * generic arrays
5323 */
5324
Takashi Iwaid5191e52009-11-16 14:58:17 +01005325/**
5326 * snd_array_new - get a new element from the given array
5327 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005328 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01005329 * Get a new element from the given array. If it exceeds the
5330 * pre-allocated array size, re-allocate the array.
5331 *
5332 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02005333 */
5334void *snd_array_new(struct snd_array *array)
5335{
Takashi Iwai12f17712012-10-10 08:59:14 +02005336 if (snd_BUG_ON(!array->elem_size))
5337 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005338 if (array->used >= array->alloced) {
5339 int num = array->alloced + array->alloc_align;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005340 int size = (num + 1) * array->elem_size;
Takashi Iwai00ef9612011-07-14 15:57:27 +02005341 int oldsize = array->alloced * array->elem_size;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01005342 void *nlist;
5343 if (snd_BUG_ON(num >= 4096))
5344 return NULL;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005345 nlist = krealloc(array->list, size, GFP_KERNEL);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005346 if (!nlist)
5347 return NULL;
Takashi Iwai00ef9612011-07-14 15:57:27 +02005348 memset(nlist + oldsize, 0, size - oldsize);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005349 array->list = nlist;
5350 array->alloced = num;
5351 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01005352 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005353}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005354EXPORT_SYMBOL_HDA(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005355
Takashi Iwaid5191e52009-11-16 14:58:17 +01005356/**
5357 * snd_array_free - free the given array elements
5358 * @array: the array object
5359 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005360void snd_array_free(struct snd_array *array)
5361{
5362 kfree(array->list);
5363 array->used = 0;
5364 array->alloced = 0;
5365 array->list = NULL;
5366}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005367EXPORT_SYMBOL_HDA(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01005368
Takashi Iwaid5191e52009-11-16 14:58:17 +01005369/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01005370 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5371 * @pcm: PCM caps bits
5372 * @buf: the string buffer to write
5373 * @buflen: the max buffer length
5374 *
5375 * used by hda_proc.c and hda_eld.c
5376 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005377void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5378{
5379 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5380 int i, j;
5381
5382 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5383 if (pcm & (AC_SUPPCM_BITS_8 << i))
5384 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5385
5386 buf[j] = '\0'; /* necessary when j == 0 */
5387}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005388EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005389
5390MODULE_DESCRIPTION("HDA codec core");
5391MODULE_LICENSE("GPL");