blob: febadc9ed593a600e648a880f04b61000ad35fa6 [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;
594 if (recursive > 5) {
595 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 Iwaia68d5a542010-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 Iwaia68d5a542010-03-30 18:03:44 +02001604 return NULL;
1605}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
Takashi Iwaia68d5a542010-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 Iwaia68d5a542010-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 Iwai4a19fae2005-06-08 14:43:58 +02001810static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
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 Iwai38681372012-02-27 15:00:58 +01001819 if ((val & HDA_AMP_MUTE) && !(info->amp_caps & AC_AMPCAP_MUTE) &&
1820 (info->amp_caps & AC_AMPCAP_MIN_MUTE))
1821 ; /* 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 Iwaide1e37b2012-12-20 11:00:21 +01001857 unsigned int cache_only;
Takashi Iwai280e57d2012-12-14 10:32:21 +01001858
1859 if (snd_BUG_ON(mask & ~0xff))
1860 mask &= 0xff;
1861 val &= mask;
1862
1863 mutex_lock(&codec->hash_mutex);
1864 info = update_amp_hash(codec, nid, ch, direction, idx, init_only);
1865 if (!info) {
1866 mutex_unlock(&codec->hash_mutex);
1867 return 0;
1868 }
1869 val |= info->vol[ch] & ~mask;
1870 if (info->vol[ch] == val) {
1871 mutex_unlock(&codec->hash_mutex);
1872 return 0;
1873 }
1874 info->vol[ch] = val;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01001875 cache_only = info->head.dirty = codec->cached_write;
Takashi Iwai280e57d2012-12-14 10:32:21 +01001876 mutex_unlock(&codec->hash_mutex);
Takashi Iwaide1e37b2012-12-20 11:00:21 +01001877 if (!cache_only)
Takashi Iwai280e57d2012-12-14 10:32:21 +01001878 put_vol_mute(codec, info, nid, ch, direction, idx, val);
1879 return 1;
1880}
1881
Takashi Iwaid5191e52009-11-16 14:58:17 +01001882/**
1883 * snd_hda_codec_amp_update - update the AMP value
1884 * @codec: HD-audio codec
1885 * @nid: NID to read the AMP value
1886 * @ch: channel (left=0 or right=1)
1887 * @direction: #HDA_INPUT or #HDA_OUTPUT
1888 * @idx: the index value (only for input direction)
1889 * @mask: bit mask to set
1890 * @val: the bits value to set
1891 *
1892 * Update the AMP value with a bit mask.
1893 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001894 */
Takashi Iwai834be882006-03-01 14:16:17 +01001895int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1896 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897{
Takashi Iwai280e57d2012-12-14 10:32:21 +01001898 return codec_amp_update(codec, nid, ch, direction, idx, mask, val, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001900EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
Takashi Iwaid5191e52009-11-16 14:58:17 +01001902/**
1903 * snd_hda_codec_amp_stereo - update the AMP stereo values
1904 * @codec: HD-audio codec
1905 * @nid: NID to read the AMP value
1906 * @direction: #HDA_INPUT or #HDA_OUTPUT
1907 * @idx: the index value (only for input direction)
1908 * @mask: bit mask to set
1909 * @val: the bits value to set
1910 *
1911 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1912 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02001913 */
1914int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1915 int direction, int idx, int mask, int val)
1916{
1917 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001918
1919 if (snd_BUG_ON(mask & ~0xff))
1920 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02001921 for (ch = 0; ch < 2; ch++)
1922 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1923 idx, mask, val);
1924 return ret;
1925}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001926EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001927
Takashi Iwai280e57d2012-12-14 10:32:21 +01001928/* Works like snd_hda_codec_amp_update() but it writes the value only at
1929 * the first access. If the amp was already initialized / updated beforehand,
1930 * this does nothing.
1931 */
1932int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
1933 int dir, int idx, int mask, int val)
1934{
1935 return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true);
1936}
1937EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init);
1938
1939int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
1940 int dir, int idx, int mask, int val)
1941{
1942 int ch, ret = 0;
1943
1944 if (snd_BUG_ON(mask & ~0xff))
1945 mask &= 0xff;
1946 for (ch = 0; ch < 2; ch++)
1947 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
1948 idx, mask, val);
1949 return ret;
1950}
1951EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init_stereo);
1952
Takashi Iwaid5191e52009-11-16 14:58:17 +01001953/**
1954 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
1955 * @codec: HD-audio codec
1956 *
1957 * Resume the all amp commands from the cache.
1958 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001959void snd_hda_codec_resume_amp(struct hda_codec *codec)
1960{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001961 int i;
1962
Takashi Iwaic370dd62012-12-13 18:30:04 +01001963 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01001964 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01001965 for (i = 0; i < codec->amp_cache.buf.used; i++) {
1966 struct hda_amp_info *buffer;
1967 u32 key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001968 hda_nid_t nid;
1969 unsigned int idx, dir, ch;
Takashi Iwaic370dd62012-12-13 18:30:04 +01001970
1971 buffer = snd_array_elem(&codec->amp_cache.buf, i);
Takashi Iwai8565f052012-12-20 12:54:18 +01001972 if (!buffer->head.dirty)
1973 continue;
1974 buffer->head.dirty = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01001975 key = buffer->head.key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001976 if (!key)
1977 continue;
1978 nid = key & 0xff;
1979 idx = (key >> 16) & 0xff;
1980 dir = (key >> 24) & 0xff;
1981 for (ch = 0; ch < 2; ch++) {
1982 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
1983 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01001984 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001985 put_vol_mute(codec, buffer, nid, ch, dir, idx,
1986 buffer->vol[ch]);
Takashi Iwaic370dd62012-12-13 18:30:04 +01001987 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001988 }
1989 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01001990 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001991}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001992EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001994static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1995 unsigned int ofs)
1996{
1997 u32 caps = query_amp_caps(codec, nid, dir);
1998 /* get num steps */
1999 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2000 if (ofs < caps)
2001 caps -= ofs;
2002 return caps;
2003}
2004
Takashi Iwaid5191e52009-11-16 14:58:17 +01002005/**
2006 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
2007 *
2008 * The control element is supposed to have the private_value field
2009 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2010 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002011int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
2012 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013{
2014 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2015 u16 nid = get_amp_nid(kcontrol);
2016 u8 chs = get_amp_channels(kcontrol);
2017 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002018 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002020 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2021 uinfo->count = chs == 3 ? 2 : 1;
2022 uinfo->value.integer.min = 0;
2023 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
2024 if (!uinfo->value.integer.max) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002025 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01002026 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
2027 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 return -EINVAL;
2029 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 return 0;
2031}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002032EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002034
2035static inline unsigned int
2036read_amp_value(struct hda_codec *codec, hda_nid_t nid,
2037 int ch, int dir, int idx, unsigned int ofs)
2038{
2039 unsigned int val;
2040 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
2041 val &= HDA_AMP_VOLMASK;
2042 if (val >= ofs)
2043 val -= ofs;
2044 else
2045 val = 0;
2046 return val;
2047}
2048
2049static inline int
2050update_amp_value(struct hda_codec *codec, hda_nid_t nid,
2051 int ch, int dir, int idx, unsigned int ofs,
2052 unsigned int val)
2053{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002054 unsigned int maxval;
2055
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002056 if (val > 0)
2057 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02002058 /* ofs = 0: raw max value */
2059 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002060 if (val > maxval)
2061 val = maxval;
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002062 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
2063 HDA_AMP_VOLMASK, val);
2064}
2065
Takashi Iwaid5191e52009-11-16 14:58:17 +01002066/**
2067 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
2068 *
2069 * The control element is supposed to have the private_value field
2070 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2071 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002072int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
2073 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074{
2075 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2076 hda_nid_t nid = get_amp_nid(kcontrol);
2077 int chs = get_amp_channels(kcontrol);
2078 int dir = get_amp_direction(kcontrol);
2079 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002080 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 long *valp = ucontrol->value.integer.value;
2082
2083 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002084 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002086 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 return 0;
2088}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002089EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
Takashi Iwaid5191e52009-11-16 14:58:17 +01002091/**
2092 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
2093 *
2094 * The control element is supposed to have the private_value field
2095 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2096 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002097int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
2098 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099{
2100 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2101 hda_nid_t nid = get_amp_nid(kcontrol);
2102 int chs = get_amp_channels(kcontrol);
2103 int dir = get_amp_direction(kcontrol);
2104 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002105 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 long *valp = ucontrol->value.integer.value;
2107 int change = 0;
2108
Takashi Iwaicb53c622007-08-10 17:21:45 +02002109 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002110 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002111 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002112 valp++;
2113 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002114 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002115 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002116 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 return change;
2118}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002119EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120
Takashi Iwaid5191e52009-11-16 14:58:17 +01002121/**
2122 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2123 *
2124 * The control element is supposed to have the private_value field
2125 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2126 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002127int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2128 unsigned int size, unsigned int __user *_tlv)
2129{
2130 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2131 hda_nid_t nid = get_amp_nid(kcontrol);
2132 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002133 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02002134 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002135 u32 caps, val1, val2;
2136
2137 if (size < 4 * sizeof(unsigned int))
2138 return -ENOMEM;
2139 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002140 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2141 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002142 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002143 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002144 val1 = ((int)val1) * ((int)val2);
Takashi Iwai38681372012-02-27 15:00:58 +01002145 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwaic08d9162010-10-17 10:40:53 +02002146 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002147 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2148 return -EFAULT;
2149 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2150 return -EFAULT;
2151 if (put_user(val1, _tlv + 2))
2152 return -EFAULT;
2153 if (put_user(val2, _tlv + 3))
2154 return -EFAULT;
2155 return 0;
2156}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002157EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002158
Takashi Iwaid5191e52009-11-16 14:58:17 +01002159/**
2160 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2161 * @codec: HD-audio codec
2162 * @nid: NID of a reference widget
2163 * @dir: #HDA_INPUT or #HDA_OUTPUT
2164 * @tlv: TLV data to be stored, at least 4 elements
2165 *
2166 * Set (static) TLV data for a virtual master volume using the AMP caps
2167 * obtained from the reference NID.
2168 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01002169 */
2170void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2171 unsigned int *tlv)
2172{
2173 u32 caps;
2174 int nums, step;
2175
2176 caps = query_amp_caps(codec, nid, dir);
2177 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2178 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2179 step = (step + 1) * 25;
2180 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2181 tlv[1] = 2 * sizeof(unsigned int);
2182 tlv[2] = -nums * step;
2183 tlv[3] = step;
2184}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002185EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002186
2187/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01002188static struct snd_kcontrol *
Takashi Iwaidcda5802012-10-12 17:24:51 +02002189find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002190{
2191 struct snd_ctl_elem_id id;
2192 memset(&id, 0, sizeof(id));
2193 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwaidcda5802012-10-12 17:24:51 +02002194 id.device = dev;
Takashi Iwai09f99702008-02-04 12:31:13 +01002195 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02002196 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2197 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002198 strcpy(id.name, name);
2199 return snd_ctl_find_id(codec->bus->card, &id);
2200}
2201
Takashi Iwaid5191e52009-11-16 14:58:17 +01002202/**
2203 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2204 * @codec: HD-audio codec
2205 * @name: ctl id name string
2206 *
2207 * Get the control element with the given id string and IFACE_MIXER.
2208 */
Takashi Iwai09f99702008-02-04 12:31:13 +01002209struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2210 const char *name)
2211{
Takashi Iwaidcda5802012-10-12 17:24:51 +02002212 return find_mixer_ctl(codec, name, 0, 0);
Takashi Iwai09f99702008-02-04 12:31:13 +01002213}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002214EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01002215
Takashi Iwaidcda5802012-10-12 17:24:51 +02002216static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
2217 int dev)
Takashi Iwai1afe2062010-12-23 10:17:52 +01002218{
2219 int idx;
2220 for (idx = 0; idx < 16; idx++) { /* 16 ctlrs should be large enough */
Takashi Iwaidcda5802012-10-12 17:24:51 +02002221 if (!find_mixer_ctl(codec, name, dev, idx))
Takashi Iwai1afe2062010-12-23 10:17:52 +01002222 return idx;
2223 }
2224 return -EBUSY;
2225}
2226
Takashi Iwaid5191e52009-11-16 14:58:17 +01002227/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002228 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01002229 * @codec: HD-audio codec
2230 * @nid: corresponding NID (optional)
2231 * @kctl: the control element to assign
2232 *
2233 * Add the given control element to an array inside the codec instance.
2234 * All control elements belonging to a codec are supposed to be added
2235 * by this function so that a proper clean-up works at the free or
2236 * reconfiguration time.
2237 *
2238 * If non-zero @nid is passed, the NID is assigned to the control element.
2239 * The assignment is shown in the codec proc file.
2240 *
2241 * snd_hda_ctl_add() checks the control subdev id field whether
2242 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002243 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2244 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01002245 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002246int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2247 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002248{
2249 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002250 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002251 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002252
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002253 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002254 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002255 if (nid == 0)
2256 nid = get_amp_nid_(kctl->private_value);
2257 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002258 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2259 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002260 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01002261 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002262 err = snd_ctl_add(codec->bus->card, kctl);
2263 if (err < 0)
2264 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002265 item = snd_array_new(&codec->mixers);
2266 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002267 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002268 item->kctl = kctl;
2269 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002270 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002271 return 0;
2272}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002273EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002274
Takashi Iwaid5191e52009-11-16 14:58:17 +01002275/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002276 * snd_hda_add_nid - Assign a NID to a control element
2277 * @codec: HD-audio codec
2278 * @nid: corresponding NID (optional)
2279 * @kctl: the control element to assign
2280 * @index: index to kctl
2281 *
2282 * Add the given control element to an array inside the codec instance.
2283 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2284 * NID:KCTL mapping - for example "Capture Source" selector.
2285 */
2286int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2287 unsigned int index, hda_nid_t nid)
2288{
2289 struct hda_nid_item *item;
2290
2291 if (nid > 0) {
2292 item = snd_array_new(&codec->nids);
2293 if (!item)
2294 return -ENOMEM;
2295 item->kctl = kctl;
2296 item->index = index;
2297 item->nid = nid;
2298 return 0;
2299 }
Takashi Iwai28d1a852010-03-15 09:05:46 +01002300 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
2301 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002302 return -EINVAL;
2303}
2304EXPORT_SYMBOL_HDA(snd_hda_add_nid);
2305
2306/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002307 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2308 * @codec: HD-audio codec
2309 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002310void snd_hda_ctls_clear(struct hda_codec *codec)
2311{
2312 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002313 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002314 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002315 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002316 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002317 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002318}
2319
Takashi Iwaia65d6292009-02-23 16:57:04 +01002320/* pseudo device locking
2321 * toggle card->shutdown to allow/disallow the device access (as a hack)
2322 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002323int snd_hda_lock_devices(struct hda_bus *bus)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002324{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002325 struct snd_card *card = bus->card;
2326 struct hda_codec *codec;
2327
Takashi Iwaia65d6292009-02-23 16:57:04 +01002328 spin_lock(&card->files_lock);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002329 if (card->shutdown)
2330 goto err_unlock;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002331 card->shutdown = 1;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002332 if (!list_empty(&card->ctl_files))
2333 goto err_clear;
2334
2335 list_for_each_entry(codec, &bus->codec_list, list) {
2336 int pcm;
2337 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2338 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2339 if (!cpcm->pcm)
2340 continue;
2341 if (cpcm->pcm->streams[0].substream_opened ||
2342 cpcm->pcm->streams[1].substream_opened)
2343 goto err_clear;
2344 }
2345 }
Takashi Iwaia65d6292009-02-23 16:57:04 +01002346 spin_unlock(&card->files_lock);
2347 return 0;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002348
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002349 err_clear:
2350 card->shutdown = 0;
2351 err_unlock:
2352 spin_unlock(&card->files_lock);
2353 return -EINVAL;
2354}
2355EXPORT_SYMBOL_HDA(snd_hda_lock_devices);
2356
2357void snd_hda_unlock_devices(struct hda_bus *bus)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002358{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002359 struct snd_card *card = bus->card;
2360
2361 card = bus->card;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002362 spin_lock(&card->files_lock);
2363 card->shutdown = 0;
2364 spin_unlock(&card->files_lock);
2365}
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002366EXPORT_SYMBOL_HDA(snd_hda_unlock_devices);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002367
Takashi Iwaid5191e52009-11-16 14:58:17 +01002368/**
2369 * snd_hda_codec_reset - Clear all objects assigned to the codec
2370 * @codec: HD-audio codec
2371 *
2372 * This frees the all PCM and control elements assigned to the codec, and
2373 * clears the caches and restores the pin default configurations.
2374 *
2375 * When a device is being used, it returns -EBSY. If successfully freed,
2376 * returns zero.
2377 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002378int snd_hda_codec_reset(struct hda_codec *codec)
2379{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002380 struct hda_bus *bus = codec->bus;
2381 struct snd_card *card = bus->card;
2382 int i;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002383
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002384 if (snd_hda_lock_devices(bus) < 0)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002385 return -EBUSY;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002386
2387 /* OK, let it free */
David Henningsson26a6cb62012-10-09 15:04:21 +02002388 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai83012a72012-08-24 18:38:08 +02002389#ifdef CONFIG_PM
Takashi Iwaia2d96e72012-05-09 12:36:22 +02002390 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwai339876d2012-05-08 16:57:12 +02002391 codec->power_on = 0;
2392 codec->power_transition = 0;
2393 codec->power_jiffies = jiffies;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002394 flush_workqueue(bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002395#endif
2396 snd_hda_ctls_clear(codec);
2397 /* relase PCMs */
2398 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002399 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01002400 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002401 clear_bit(codec->pcm_info[i].device,
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002402 bus->pcm_dev_bits);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002403 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002404 }
2405 if (codec->patch_ops.free)
2406 codec->patch_ops.free(codec);
Takashi Iwai07dc59f2012-09-10 09:39:31 +02002407 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
Takashi Iwai1835a0f2011-10-27 22:12:46 +02002408 snd_hda_jack_tbl_clear(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01002409 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002410 codec->spec = NULL;
2411 free_hda_cache(&codec->amp_cache);
2412 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01002413 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2414 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01002415 /* free only driver_pins so that init_pins + user_pins are restored */
2416 snd_array_free(&codec->driver_pins);
Takashi Iwai09a60712012-06-26 15:01:33 +02002417 snd_array_free(&codec->cvt_setups);
2418 snd_array_free(&codec->spdif_out);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01002419 snd_array_free(&codec->verbs);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002420 codec->num_pcms = 0;
2421 codec->pcm_info = NULL;
2422 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01002423 codec->slave_dig_outs = NULL;
2424 codec->spdif_status_reset = 0;
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002425 module_put(codec->owner);
2426 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002427
2428 /* allow device access again */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002429 snd_hda_unlock_devices(bus);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002430 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002431}
2432
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002433typedef int (*map_slave_func_t)(void *, struct snd_kcontrol *);
2434
2435/* apply the function to all matching slave ctls in the mixer list */
2436static int map_slaves(struct hda_codec *codec, const char * const *slaves,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002437 const char *suffix, map_slave_func_t func, void *data)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002438{
2439 struct hda_nid_item *items;
2440 const char * const *s;
2441 int i, err;
2442
2443 items = codec->mixers.list;
2444 for (i = 0; i < codec->mixers.used; i++) {
2445 struct snd_kcontrol *sctl = items[i].kctl;
2446 if (!sctl || !sctl->id.name ||
2447 sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
2448 continue;
2449 for (s = slaves; *s; s++) {
Takashi Iwai9322ca52012-02-03 14:28:01 +01002450 char tmpname[sizeof(sctl->id.name)];
2451 const char *name = *s;
2452 if (suffix) {
2453 snprintf(tmpname, sizeof(tmpname), "%s %s",
2454 name, suffix);
2455 name = tmpname;
2456 }
Takashi Iwai9322ca52012-02-03 14:28:01 +01002457 if (!strcmp(sctl->id.name, name)) {
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002458 err = func(data, sctl);
2459 if (err)
2460 return err;
2461 break;
2462 }
2463 }
2464 }
2465 return 0;
2466}
2467
2468static int check_slave_present(void *data, struct snd_kcontrol *sctl)
2469{
2470 return 1;
2471}
2472
Takashi Iwai18478e82012-03-09 17:51:10 +01002473/* guess the value corresponding to 0dB */
2474static int get_kctl_0dB_offset(struct snd_kcontrol *kctl)
2475{
2476 int _tlv[4];
2477 const int *tlv = NULL;
2478 int val = -1;
2479
2480 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2481 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2482 mm_segment_t fs = get_fs();
2483 set_fs(get_ds());
2484 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2485 tlv = _tlv;
2486 set_fs(fs);
2487 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2488 tlv = kctl->tlv.p;
2489 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE)
2490 val = -tlv[2] / tlv[3];
2491 return val;
2492}
2493
2494/* call kctl->put with the given value(s) */
2495static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2496{
2497 struct snd_ctl_elem_value *ucontrol;
2498 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2499 if (!ucontrol)
2500 return -ENOMEM;
2501 ucontrol->value.integer.value[0] = val;
2502 ucontrol->value.integer.value[1] = val;
2503 kctl->put(kctl, ucontrol);
2504 kfree(ucontrol);
2505 return 0;
2506}
2507
2508/* initialize the slave volume with 0dB */
2509static int init_slave_0dB(void *data, struct snd_kcontrol *slave)
2510{
2511 int offset = get_kctl_0dB_offset(slave);
2512 if (offset > 0)
2513 put_kctl_with_value(slave, offset);
2514 return 0;
2515}
2516
2517/* unmute the slave */
2518static int init_slave_unmute(void *data, struct snd_kcontrol *slave)
2519{
2520 return put_kctl_with_value(slave, 1);
2521}
2522
Takashi Iwaid5191e52009-11-16 14:58:17 +01002523/**
2524 * snd_hda_add_vmaster - create a virtual master control and add slaves
2525 * @codec: HD-audio codec
2526 * @name: vmaster control name
2527 * @tlv: TLV data (optional)
2528 * @slaves: slave control names (optional)
Takashi Iwai9322ca52012-02-03 14:28:01 +01002529 * @suffix: suffix string to each slave name (optional)
Takashi Iwai18478e82012-03-09 17:51:10 +01002530 * @init_slave_vol: initialize slaves to unmute/0dB
Takashi Iwai29e58532012-03-12 12:25:03 +01002531 * @ctl_ret: store the vmaster kcontrol in return
Takashi Iwaid5191e52009-11-16 14:58:17 +01002532 *
2533 * Create a virtual master control with the given name. The TLV data
2534 * must be either NULL or a valid data.
2535 *
2536 * @slaves is a NULL-terminated array of strings, each of which is a
2537 * slave control name. All controls with these names are assigned to
2538 * the new virtual master control.
2539 *
2540 * This function returns zero if successful or a negative error code.
2541 */
Takashi Iwai18478e82012-03-09 17:51:10 +01002542int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002543 unsigned int *tlv, const char * const *slaves,
Takashi Iwai29e58532012-03-12 12:25:03 +01002544 const char *suffix, bool init_slave_vol,
2545 struct snd_kcontrol **ctl_ret)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002546{
2547 struct snd_kcontrol *kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002548 int err;
2549
Takashi Iwai29e58532012-03-12 12:25:03 +01002550 if (ctl_ret)
2551 *ctl_ret = NULL;
2552
Takashi Iwai9322ca52012-02-03 14:28:01 +01002553 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002554 if (err != 1) {
Takashi Iwai2f085542008-02-22 18:43:50 +01002555 snd_printdd("No slave found for %s\n", name);
2556 return 0;
2557 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002558 kctl = snd_ctl_make_virtual_master(name, tlv);
2559 if (!kctl)
2560 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002561 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002562 if (err < 0)
2563 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002564
Takashi Iwai9322ca52012-02-03 14:28:01 +01002565 err = map_slaves(codec, slaves, suffix,
2566 (map_slave_func_t)snd_ctl_add_slave, kctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002567 if (err < 0)
2568 return err;
Takashi Iwai18478e82012-03-09 17:51:10 +01002569
2570 /* init with master mute & zero volume */
2571 put_kctl_with_value(kctl, 0);
2572 if (init_slave_vol)
2573 map_slaves(codec, slaves, suffix,
2574 tlv ? init_slave_0dB : init_slave_unmute, kctl);
2575
Takashi Iwai29e58532012-03-12 12:25:03 +01002576 if (ctl_ret)
2577 *ctl_ret = kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002578 return 0;
2579}
Takashi Iwai18478e82012-03-09 17:51:10 +01002580EXPORT_SYMBOL_HDA(__snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002581
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002582/*
2583 * mute-LED control using vmaster
2584 */
2585static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2586 struct snd_ctl_elem_info *uinfo)
2587{
2588 static const char * const texts[] = {
David Henningssonc86c2d42013-01-03 14:12:29 +01002589 "On", "Off", "Follow Master"
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002590 };
2591 unsigned int index;
2592
2593 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2594 uinfo->count = 1;
2595 uinfo->value.enumerated.items = 3;
2596 index = uinfo->value.enumerated.item;
2597 if (index >= 3)
2598 index = 2;
2599 strcpy(uinfo->value.enumerated.name, texts[index]);
2600 return 0;
2601}
2602
2603static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2604 struct snd_ctl_elem_value *ucontrol)
2605{
2606 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2607 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2608 return 0;
2609}
2610
2611static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2612 struct snd_ctl_elem_value *ucontrol)
2613{
2614 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2615 unsigned int old_mode = hook->mute_mode;
2616
2617 hook->mute_mode = ucontrol->value.enumerated.item[0];
2618 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2619 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2620 if (old_mode == hook->mute_mode)
2621 return 0;
2622 snd_hda_sync_vmaster_hook(hook);
2623 return 1;
2624}
2625
2626static struct snd_kcontrol_new vmaster_mute_mode = {
2627 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2628 .name = "Mute-LED Mode",
2629 .info = vmaster_mute_mode_info,
2630 .get = vmaster_mute_mode_get,
2631 .put = vmaster_mute_mode_put,
2632};
2633
2634/*
2635 * Add a mute-LED hook with the given vmaster switch kctl
2636 * "Mute-LED Mode" control is automatically created and associated with
2637 * the given hook.
2638 */
2639int snd_hda_add_vmaster_hook(struct hda_codec *codec,
Takashi Iwaif29735cb2012-03-13 07:55:10 +01002640 struct hda_vmaster_mute_hook *hook,
2641 bool expose_enum_ctl)
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002642{
2643 struct snd_kcontrol *kctl;
2644
2645 if (!hook->hook || !hook->sw_kctl)
2646 return 0;
2647 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
2648 hook->codec = codec;
2649 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
Takashi Iwaif29735cb2012-03-13 07:55:10 +01002650 if (!expose_enum_ctl)
2651 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002652 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2653 if (!kctl)
2654 return -ENOMEM;
2655 return snd_hda_ctl_add(codec, 0, kctl);
2656}
2657EXPORT_SYMBOL_HDA(snd_hda_add_vmaster_hook);
2658
2659/*
2660 * Call the hook with the current value for synchronization
2661 * Should be called in init callback
2662 */
2663void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2664{
2665 if (!hook->hook || !hook->codec)
2666 return;
2667 switch (hook->mute_mode) {
2668 case HDA_VMUTE_FOLLOW_MASTER:
2669 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2670 break;
2671 default:
2672 hook->hook(hook->codec, hook->mute_mode);
2673 break;
2674 }
2675}
2676EXPORT_SYMBOL_HDA(snd_hda_sync_vmaster_hook);
2677
2678
Takashi Iwaid5191e52009-11-16 14:58:17 +01002679/**
2680 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2681 *
2682 * The control element is supposed to have the private_value field
2683 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2684 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002685int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2686 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687{
2688 int chs = get_amp_channels(kcontrol);
2689
2690 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2691 uinfo->count = chs == 3 ? 2 : 1;
2692 uinfo->value.integer.min = 0;
2693 uinfo->value.integer.max = 1;
2694 return 0;
2695}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002696EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697
Takashi Iwaid5191e52009-11-16 14:58:17 +01002698/**
2699 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2700 *
2701 * The control element is supposed to have the private_value field
2702 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2703 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002704int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2705 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706{
2707 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2708 hda_nid_t nid = get_amp_nid(kcontrol);
2709 int chs = get_amp_channels(kcontrol);
2710 int dir = get_amp_direction(kcontrol);
2711 int idx = get_amp_index(kcontrol);
2712 long *valp = ucontrol->value.integer.value;
2713
2714 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002715 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002716 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002718 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002719 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 return 0;
2721}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002722EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723
Takashi Iwaid5191e52009-11-16 14:58:17 +01002724/**
2725 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2726 *
2727 * The control element is supposed to have the private_value field
2728 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2729 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002730int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2731 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732{
2733 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2734 hda_nid_t nid = get_amp_nid(kcontrol);
2735 int chs = get_amp_channels(kcontrol);
2736 int dir = get_amp_direction(kcontrol);
2737 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 long *valp = ucontrol->value.integer.value;
2739 int change = 0;
2740
Takashi Iwaicb53c622007-08-10 17:21:45 +02002741 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002742 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002743 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002744 HDA_AMP_MUTE,
2745 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002746 valp++;
2747 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002748 if (chs & 2)
2749 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002750 HDA_AMP_MUTE,
2751 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002752 hda_call_check_power_status(codec, nid);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002753 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 return change;
2755}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002756EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757
2758/*
Takashi Iwai985be542005-11-02 18:26:49 +01002759 * bound volume controls
2760 *
2761 * bind multiple volumes (# indices, from 0)
2762 */
2763
2764#define AMP_VAL_IDX_SHIFT 19
2765#define AMP_VAL_IDX_MASK (0x0f<<19)
2766
Takashi Iwaid5191e52009-11-16 14:58:17 +01002767/**
2768 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2769 *
2770 * The control element is supposed to have the private_value field
2771 * set up via HDA_BIND_MUTE*() macros.
2772 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002773int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2774 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002775{
2776 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2777 unsigned long pval;
2778 int err;
2779
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002780 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002781 pval = kcontrol->private_value;
2782 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2783 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2784 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002785 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002786 return err;
2787}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002788EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002789
Takashi Iwaid5191e52009-11-16 14:58:17 +01002790/**
2791 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2792 *
2793 * The control element is supposed to have the private_value field
2794 * set up via HDA_BIND_MUTE*() macros.
2795 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002796int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2797 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002798{
2799 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2800 unsigned long pval;
2801 int i, indices, err = 0, change = 0;
2802
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002803 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002804 pval = kcontrol->private_value;
2805 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2806 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002807 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2808 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002809 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2810 if (err < 0)
2811 break;
2812 change |= err;
2813 }
2814 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002815 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002816 return err < 0 ? err : change;
2817}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002818EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002819
Takashi Iwaid5191e52009-11-16 14:58:17 +01002820/**
2821 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2822 *
2823 * The control element is supposed to have the private_value field
2824 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002825 */
2826int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2827 struct snd_ctl_elem_info *uinfo)
2828{
2829 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2830 struct hda_bind_ctls *c;
2831 int err;
2832
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002833 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002834 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002835 kcontrol->private_value = *c->values;
2836 err = c->ops->info(kcontrol, uinfo);
2837 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002838 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002839 return err;
2840}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002841EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002842
Takashi Iwaid5191e52009-11-16 14:58:17 +01002843/**
2844 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2845 *
2846 * The control element is supposed to have the private_value field
2847 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2848 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002849int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2850 struct snd_ctl_elem_value *ucontrol)
2851{
2852 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2853 struct hda_bind_ctls *c;
2854 int err;
2855
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002856 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002857 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002858 kcontrol->private_value = *c->values;
2859 err = c->ops->get(kcontrol, ucontrol);
2860 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002861 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002862 return err;
2863}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002864EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002865
Takashi Iwaid5191e52009-11-16 14:58:17 +01002866/**
2867 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2868 *
2869 * The control element is supposed to have the private_value field
2870 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2871 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002872int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2873 struct snd_ctl_elem_value *ucontrol)
2874{
2875 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2876 struct hda_bind_ctls *c;
2877 unsigned long *vals;
2878 int err = 0, change = 0;
2879
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002880 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002881 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002882 for (vals = c->values; *vals; vals++) {
2883 kcontrol->private_value = *vals;
2884 err = c->ops->put(kcontrol, ucontrol);
2885 if (err < 0)
2886 break;
2887 change |= err;
2888 }
2889 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002890 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002891 return err < 0 ? err : change;
2892}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002893EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02002894
Takashi Iwaid5191e52009-11-16 14:58:17 +01002895/**
2896 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2897 *
2898 * The control element is supposed to have the private_value field
2899 * set up via HDA_BIND_VOL() macro.
2900 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002901int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2902 unsigned int size, unsigned int __user *tlv)
2903{
2904 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2905 struct hda_bind_ctls *c;
2906 int err;
2907
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002908 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002909 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002910 kcontrol->private_value = *c->values;
2911 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2912 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002913 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002914 return err;
2915}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002916EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02002917
2918struct hda_ctl_ops snd_hda_bind_vol = {
2919 .info = snd_hda_mixer_amp_volume_info,
2920 .get = snd_hda_mixer_amp_volume_get,
2921 .put = snd_hda_mixer_amp_volume_put,
2922 .tlv = snd_hda_mixer_amp_tlv
2923};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002924EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02002925
2926struct hda_ctl_ops snd_hda_bind_sw = {
2927 .info = snd_hda_mixer_amp_switch_info,
2928 .get = snd_hda_mixer_amp_switch_get,
2929 .put = snd_hda_mixer_amp_switch_put,
2930 .tlv = snd_hda_mixer_amp_tlv
2931};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002932EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02002933
2934/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 * SPDIF out controls
2936 */
2937
Takashi Iwai0ba21762007-04-16 11:29:14 +02002938static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2939 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940{
2941 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2942 uinfo->count = 1;
2943 return 0;
2944}
2945
Takashi Iwai0ba21762007-04-16 11:29:14 +02002946static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2947 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948{
2949 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2950 IEC958_AES0_NONAUDIO |
2951 IEC958_AES0_CON_EMPHASIS_5015 |
2952 IEC958_AES0_CON_NOT_COPYRIGHT;
2953 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2954 IEC958_AES1_CON_ORIGINAL;
2955 return 0;
2956}
2957
Takashi Iwai0ba21762007-04-16 11:29:14 +02002958static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2959 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960{
2961 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2962 IEC958_AES0_NONAUDIO |
2963 IEC958_AES0_PRO_EMPHASIS_5015;
2964 return 0;
2965}
2966
Takashi Iwai0ba21762007-04-16 11:29:14 +02002967static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2968 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969{
2970 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06002971 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002972 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002974 mutex_lock(&codec->spdif_mutex);
2975 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06002976 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
2977 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
2978 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
2979 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002980 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981
2982 return 0;
2983}
2984
2985/* convert from SPDIF status bits to HDA SPDIF bits
2986 * bit 0 (DigEn) is always set zero (to be filled later)
2987 */
2988static unsigned short convert_from_spdif_status(unsigned int sbits)
2989{
2990 unsigned short val = 0;
2991
2992 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002993 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002995 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002997 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2998 IEC958_AES0_PRO_EMPHASIS_5015)
2999 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003001 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
3002 IEC958_AES0_CON_EMPHASIS_5015)
3003 val |= AC_DIG1_EMPHASIS;
3004 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
3005 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02003007 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
3009 }
3010 return val;
3011}
3012
3013/* convert to SPDIF status bits from HDA SPDIF bits
3014 */
3015static unsigned int convert_to_spdif_status(unsigned short val)
3016{
3017 unsigned int sbits = 0;
3018
Takashi Iwai0ba21762007-04-16 11:29:14 +02003019 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003021 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 sbits |= IEC958_AES0_PROFESSIONAL;
3023 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003024 if (sbits & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
3026 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003027 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003029 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003031 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
3033 sbits |= val & (0x7f << 8);
3034 }
3035 return sbits;
3036}
3037
Takashi Iwai2f728532008-09-25 16:32:41 +02003038/* set digital convert verbs both for the given NID and its slaves */
3039static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
3040 int verb, int val)
3041{
Takashi Iwaidda14412011-05-02 11:29:30 +02003042 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02003043
Takashi Iwai9e976972008-11-25 08:17:20 +01003044 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003045 d = codec->slave_dig_outs;
3046 if (!d)
3047 return;
3048 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01003049 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003050}
3051
3052static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
3053 int dig1, int dig2)
3054{
3055 if (dig1 != -1)
3056 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
3057 if (dig2 != -1)
3058 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
3059}
3060
Takashi Iwai0ba21762007-04-16 11:29:14 +02003061static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
3062 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063{
3064 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003065 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003066 struct hda_spdif_out *spdif;
3067 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 unsigned short val;
3069 int change;
3070
Ingo Molnar62932df2006-01-16 16:34:20 +01003071 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003072 spdif = snd_array_elem(&codec->spdif_out, idx);
3073 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06003074 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
3076 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
3077 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c935972011-06-01 11:14:17 -06003078 val = convert_from_spdif_status(spdif->status);
3079 val |= spdif->ctls & 1;
3080 change = spdif->ctls != val;
3081 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003082 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02003083 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01003084 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 return change;
3086}
3087
Takashi Iwaia5ce8892007-07-23 15:42:26 +02003088#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089
Takashi Iwai0ba21762007-04-16 11:29:14 +02003090static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
3091 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092{
3093 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003094 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003095 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003097 mutex_lock(&codec->spdif_mutex);
3098 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06003099 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003100 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 return 0;
3102}
3103
Stephen Warren74b654c2011-06-01 11:14:18 -06003104static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
3105 int dig1, int dig2)
3106{
3107 set_dig_out_convert(codec, nid, dig1, dig2);
3108 /* unmute amp switch (if any) */
3109 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
3110 (dig1 & AC_DIG1_ENABLE))
3111 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
3112 HDA_AMP_MUTE, 0);
3113}
3114
Takashi Iwai0ba21762007-04-16 11:29:14 +02003115static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
3116 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117{
3118 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003119 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003120 struct hda_spdif_out *spdif;
3121 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 unsigned short val;
3123 int change;
3124
Ingo Molnar62932df2006-01-16 16:34:20 +01003125 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003126 spdif = snd_array_elem(&codec->spdif_out, idx);
3127 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06003128 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02003130 val |= AC_DIG1_ENABLE;
Stephen Warren7c935972011-06-01 11:14:17 -06003131 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003132 spdif->ctls = val;
3133 if (change && nid != (u16)-1)
3134 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01003135 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 return change;
3137}
3138
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003139static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 {
3141 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3142 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003143 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 .info = snd_hda_spdif_mask_info,
3145 .get = snd_hda_spdif_cmask_get,
3146 },
3147 {
3148 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3149 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003150 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 .info = snd_hda_spdif_mask_info,
3152 .get = snd_hda_spdif_pmask_get,
3153 },
3154 {
3155 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003156 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 .info = snd_hda_spdif_mask_info,
3158 .get = snd_hda_spdif_default_get,
3159 .put = snd_hda_spdif_default_put,
3160 },
3161 {
3162 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003163 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 .info = snd_hda_spdif_out_switch_info,
3165 .get = snd_hda_spdif_out_switch_get,
3166 .put = snd_hda_spdif_out_switch_put,
3167 },
3168 { } /* end */
3169};
3170
3171/**
Takashi Iwaidcda5802012-10-12 17:24:51 +02003172 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 * @codec: the HDA codec
Takashi Iwaidcda5802012-10-12 17:24:51 +02003174 * @associated_nid: NID that new ctls associated with
3175 * @cvt_nid: converter NID
3176 * @type: HDA_PCM_TYPE_*
3177 * Creates controls related with the digital output.
3178 * Called from each patch supporting the digital out.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 *
3180 * Returns 0 if successful, or a negative error code.
3181 */
Takashi Iwaidcda5802012-10-12 17:24:51 +02003182int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
3183 hda_nid_t associated_nid,
3184 hda_nid_t cvt_nid,
3185 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186{
3187 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003188 struct snd_kcontrol *kctl;
3189 struct snd_kcontrol_new *dig_mix;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003190 int idx, dev = 0;
3191 const int spdif_pcm_dev = 1;
Stephen Warren7c935972011-06-01 11:14:17 -06003192 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193
Takashi Iwaidcda5802012-10-12 17:24:51 +02003194 if (codec->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
3195 type == HDA_PCM_TYPE_SPDIF) {
3196 dev = spdif_pcm_dev;
3197 } else if (codec->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
3198 type == HDA_PCM_TYPE_HDMI) {
3199 for (idx = 0; idx < codec->spdif_out.used; idx++) {
3200 spdif = snd_array_elem(&codec->spdif_out, idx);
3201 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3202 kctl = find_mixer_ctl(codec, dig_mix->name, 0, idx);
3203 if (!kctl)
3204 break;
3205 kctl->id.device = spdif_pcm_dev;
3206 }
3207 }
3208 codec->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
3209 }
3210 if (!codec->primary_dig_out_type)
3211 codec->primary_dig_out_type = type;
3212
3213 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", dev);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003214 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003215 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
3216 return -EBUSY;
3217 }
Stephen Warren7c935972011-06-01 11:14:17 -06003218 spdif = snd_array_new(&codec->spdif_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3220 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01003221 if (!kctl)
3222 return -ENOMEM;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003223 kctl->id.device = dev;
Takashi Iwai09f99702008-02-04 12:31:13 +01003224 kctl->id.index = idx;
Stephen Warren7c935972011-06-01 11:14:17 -06003225 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06003226 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003227 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 return err;
3229 }
Stephen Warren74b654c2011-06-01 11:14:18 -06003230 spdif->nid = cvt_nid;
3231 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
Stephen Warren7c935972011-06-01 11:14:17 -06003232 AC_VERB_GET_DIGI_CONVERT_1, 0);
3233 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 return 0;
3235}
Takashi Iwaidcda5802012-10-12 17:24:51 +02003236EXPORT_SYMBOL_HDA(snd_hda_create_dig_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003238/* get the hda_spdif_out entry from the given NID
3239 * call within spdif_mutex lock
3240 */
Stephen Warren7c935972011-06-01 11:14:17 -06003241struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3242 hda_nid_t nid)
3243{
3244 int i;
3245 for (i = 0; i < codec->spdif_out.used; i++) {
3246 struct hda_spdif_out *spdif =
3247 snd_array_elem(&codec->spdif_out, i);
3248 if (spdif->nid == nid)
3249 return spdif;
3250 }
3251 return NULL;
3252}
3253EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid);
3254
Stephen Warren74b654c2011-06-01 11:14:18 -06003255void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3256{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003257 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003258
3259 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003260 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003261 spdif->nid = (u16)-1;
3262 mutex_unlock(&codec->spdif_mutex);
3263}
3264EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign);
3265
3266void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3267{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003268 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003269 unsigned short val;
3270
3271 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003272 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003273 if (spdif->nid != nid) {
3274 spdif->nid = nid;
3275 val = spdif->ctls;
3276 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3277 }
3278 mutex_unlock(&codec->spdif_mutex);
3279}
3280EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign);
3281
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282/*
Takashi Iwai9a081602008-02-12 18:37:26 +01003283 * SPDIF sharing with analog output
3284 */
3285static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3286 struct snd_ctl_elem_value *ucontrol)
3287{
3288 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3289 ucontrol->value.integer.value[0] = mout->share_spdif;
3290 return 0;
3291}
3292
3293static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3294 struct snd_ctl_elem_value *ucontrol)
3295{
3296 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3297 mout->share_spdif = !!ucontrol->value.integer.value[0];
3298 return 0;
3299}
3300
3301static struct snd_kcontrol_new spdif_share_sw = {
3302 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3303 .name = "IEC958 Default PCM Playback Switch",
3304 .info = snd_ctl_boolean_mono_info,
3305 .get = spdif_share_sw_get,
3306 .put = spdif_share_sw_put,
3307};
3308
Takashi Iwaid5191e52009-11-16 14:58:17 +01003309/**
3310 * snd_hda_create_spdif_share_sw - create Default PCM switch
3311 * @codec: the HDA codec
3312 * @mout: multi-out instance
3313 */
Takashi Iwai9a081602008-02-12 18:37:26 +01003314int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3315 struct hda_multi_out *mout)
3316{
3317 if (!mout->dig_out_nid)
3318 return 0;
3319 /* ATTENTION: here mout is passed as private_data, instead of codec */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003320 return snd_hda_ctl_add(codec, mout->dig_out_nid,
3321 snd_ctl_new1(&spdif_share_sw, mout));
Takashi Iwai9a081602008-02-12 18:37:26 +01003322}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003323EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01003324
3325/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 * SPDIF input
3327 */
3328
3329#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3330
Takashi Iwai0ba21762007-04-16 11:29:14 +02003331static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3332 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333{
3334 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3335
3336 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3337 return 0;
3338}
3339
Takashi Iwai0ba21762007-04-16 11:29:14 +02003340static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3341 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342{
3343 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3344 hda_nid_t nid = kcontrol->private_value;
3345 unsigned int val = !!ucontrol->value.integer.value[0];
3346 int change;
3347
Ingo Molnar62932df2006-01-16 16:34:20 +01003348 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003350 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003352 snd_hda_codec_write_cache(codec, nid, 0,
3353 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003355 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 return change;
3357}
3358
Takashi Iwai0ba21762007-04-16 11:29:14 +02003359static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3360 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361{
3362 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3363 hda_nid_t nid = kcontrol->private_value;
3364 unsigned short val;
3365 unsigned int sbits;
3366
Andrew Paprocki3982d172007-12-19 12:13:44 +01003367 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 sbits = convert_to_spdif_status(val);
3369 ucontrol->value.iec958.status[0] = sbits;
3370 ucontrol->value.iec958.status[1] = sbits >> 8;
3371 ucontrol->value.iec958.status[2] = sbits >> 16;
3372 ucontrol->value.iec958.status[3] = sbits >> 24;
3373 return 0;
3374}
3375
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003376static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 {
3378 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003379 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 .info = snd_hda_spdif_in_switch_info,
3381 .get = snd_hda_spdif_in_switch_get,
3382 .put = snd_hda_spdif_in_switch_put,
3383 },
3384 {
3385 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3386 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003387 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388 .info = snd_hda_spdif_mask_info,
3389 .get = snd_hda_spdif_in_status_get,
3390 },
3391 { } /* end */
3392};
3393
3394/**
3395 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3396 * @codec: the HDA codec
3397 * @nid: audio in widget NID
3398 *
3399 * Creates controls related with the SPDIF input.
3400 * Called from each patch supporting the SPDIF in.
3401 *
3402 * Returns 0 if successful, or a negative error code.
3403 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003404int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405{
3406 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003407 struct snd_kcontrol *kctl;
3408 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003409 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410
Takashi Iwaidcda5802012-10-12 17:24:51 +02003411 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003412 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003413 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
3414 return -EBUSY;
3415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3417 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01003418 if (!kctl)
3419 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003421 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003422 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 return err;
3424 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003425 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01003426 snd_hda_codec_read(codec, nid, 0,
3427 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02003428 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 return 0;
3430}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003431EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003433/*
3434 * command cache
3435 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436
Takashi Iwaic370dd62012-12-13 18:30:04 +01003437/* build a 31bit cache key with the widget id and the command parameter */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003438#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3439#define get_cmd_cache_nid(key) ((key) & 0xff)
3440#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3441
3442/**
3443 * snd_hda_codec_write_cache - send a single command with caching
3444 * @codec: the HDA codec
3445 * @nid: NID to send the command
3446 * @direct: direct flag
3447 * @verb: the verb to send
3448 * @parm: the parameter for the verb
3449 *
3450 * Send a single command without waiting for response.
3451 *
3452 * Returns 0 if successful, or a negative error code.
3453 */
3454int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
3455 int direct, unsigned int verb, unsigned int parm)
3456{
Takashi Iwaic370dd62012-12-13 18:30:04 +01003457 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003458 struct hda_cache_head *c;
3459 u32 key;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003460 unsigned int cache_only;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003461
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003462 cache_only = codec->cached_write;
3463 if (!cache_only) {
Takashi Iwaic370dd62012-12-13 18:30:04 +01003464 err = snd_hda_codec_write(codec, nid, direct, verb, parm);
3465 if (err < 0)
3466 return err;
3467 }
3468
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003469 /* parm may contain the verb stuff for get/set amp */
3470 verb = verb | (parm >> 8);
3471 parm &= 0xff;
3472 key = build_cmd_cache_key(nid, verb);
3473 mutex_lock(&codec->bus->cmd_mutex);
3474 c = get_alloc_hash(&codec->cmd_cache, key);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003475 if (c) {
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003476 c->val = parm;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003477 c->dirty = cache_only;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003478 }
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003479 mutex_unlock(&codec->bus->cmd_mutex);
3480 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003481}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003482EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003483
Takashi Iwaid5191e52009-11-16 14:58:17 +01003484/**
Takashi Iwaia68d5a542010-03-30 18:03:44 +02003485 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3486 * @codec: the HDA codec
3487 * @nid: NID to send the command
3488 * @direct: direct flag
3489 * @verb: the verb to send
3490 * @parm: the parameter for the verb
3491 *
3492 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3493 * command if the parameter is already identical with the cached value.
3494 * If not, it sends the command and refreshes the cache.
3495 *
3496 * Returns 0 if successful, or a negative error code.
3497 */
3498int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
3499 int direct, unsigned int verb, unsigned int parm)
3500{
3501 struct hda_cache_head *c;
3502 u32 key;
3503
3504 /* parm may contain the verb stuff for get/set amp */
3505 verb = verb | (parm >> 8);
3506 parm &= 0xff;
3507 key = build_cmd_cache_key(nid, verb);
3508 mutex_lock(&codec->bus->cmd_mutex);
3509 c = get_hash(&codec->cmd_cache, key);
3510 if (c && c->val == parm) {
3511 mutex_unlock(&codec->bus->cmd_mutex);
3512 return 0;
3513 }
3514 mutex_unlock(&codec->bus->cmd_mutex);
3515 return snd_hda_codec_write_cache(codec, nid, direct, verb, parm);
3516}
3517EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
3518
3519/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003520 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3521 * @codec: HD-audio codec
3522 *
3523 * Execute all verbs recorded in the command caches to resume.
3524 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003525void snd_hda_codec_resume_cache(struct hda_codec *codec)
3526{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003527 int i;
3528
Takashi Iwaic370dd62012-12-13 18:30:04 +01003529 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01003530 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003531 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3532 struct hda_cache_head *buffer;
3533 u32 key;
3534
3535 buffer = snd_array_elem(&codec->cmd_cache.buf, i);
3536 key = buffer->key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003537 if (!key)
3538 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003539 if (!buffer->dirty)
3540 continue;
3541 buffer->dirty = 0;
3542 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003543 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3544 get_cmd_cache_cmd(key), buffer->val);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003545 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003546 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01003547 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003548}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003549EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003550
3551/**
3552 * snd_hda_sequence_write_cache - sequence writes with caching
3553 * @codec: the HDA codec
3554 * @seq: VERB array to send
3555 *
3556 * Send the commands sequentially from the given array.
3557 * Thte commands are recorded on cache for power-save and resume.
3558 * The array must be terminated with NID=0.
3559 */
3560void snd_hda_sequence_write_cache(struct hda_codec *codec,
3561 const struct hda_verb *seq)
3562{
3563 for (; seq->nid; seq++)
3564 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3565 seq->param);
3566}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003567EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003568
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003569void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
3570 unsigned int power_state,
3571 bool eapd_workaround)
Takashi Iwai54d17402005-11-21 16:33:22 +01003572{
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003573 hda_nid_t nid = codec->start_nid;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003574 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01003575
Takashi Iwaicb53c622007-08-10 17:21:45 +02003576 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003577 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003578 if (!(wcaps & AC_WCAP_POWER))
3579 continue;
3580 /* don't power down the widget if it controls eapd and
3581 * EAPD_BTLENABLE is set.
3582 */
3583 if (eapd_workaround && power_state == AC_PWRST_D3 &&
3584 get_wcaps_type(wcaps) == AC_WID_PIN &&
3585 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3586 int eapd = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003587 AC_VERB_GET_EAPD_BTLENABLE, 0);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003588 if (eapd & 0x02)
3589 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003590 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003591 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
3592 power_state);
Takashi Iwai54d17402005-11-21 16:33:22 +01003593 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003594}
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003595EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all);
3596
3597/*
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003598 * supported power states check
3599 */
3600static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg,
3601 unsigned int power_state)
3602{
3603 int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE);
3604
Mengdong Line037cb42012-08-10 14:11:58 +02003605 if (sup == -1)
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003606 return false;
3607 if (sup & power_state)
3608 return true;
3609 else
3610 return false;
3611}
3612
3613/*
Takashi Iwai432c6412012-08-28 09:59:20 -07003614 * wait until the state is reached, returns the current state
3615 */
3616static unsigned int hda_sync_power_state(struct hda_codec *codec,
3617 hda_nid_t fg,
3618 unsigned int power_state)
3619{
3620 unsigned long end_time = jiffies + msecs_to_jiffies(500);
3621 unsigned int state, actual_state;
3622
3623 for (;;) {
3624 state = snd_hda_codec_read(codec, fg, 0,
3625 AC_VERB_GET_POWER_STATE, 0);
3626 if (state & AC_PWRST_ERROR)
3627 break;
3628 actual_state = (state >> 4) & 0x0f;
3629 if (actual_state == power_state)
3630 break;
3631 if (time_after_eq(jiffies, end_time))
3632 break;
3633 /* wait until the codec reachs to the target state */
3634 msleep(1);
3635 }
3636 return state;
3637}
3638
3639/*
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003640 * set power state of the codec, and return the power state
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003641 */
Takashi Iwaid8193872012-08-31 07:54:38 -07003642static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003643 unsigned int power_state)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003644{
Takashi Iwaid8193872012-08-31 07:54:38 -07003645 hda_nid_t fg = codec->afg ? codec->afg : codec->mfg;
Wang Xingchao09617ce2012-06-08 10:26:08 +08003646 int count;
3647 unsigned int state;
3648
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003649 /* this delay seems necessary to avoid click noise at power-down */
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003650 if (power_state == AC_PWRST_D3) {
3651 /* transition time less than 10ms for power down */
Takashi Iwai983f6b92012-08-28 09:18:01 -07003652 msleep(codec->epss ? 10 : 100);
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003653 }
Wang Xingchao09617ce2012-06-08 10:26:08 +08003654
3655 /* repeat power states setting at most 10 times*/
3656 for (count = 0; count < 10; count++) {
Takashi Iwai432c6412012-08-28 09:59:20 -07003657 if (codec->patch_ops.set_power_state)
3658 codec->patch_ops.set_power_state(codec, fg,
3659 power_state);
3660 else {
3661 snd_hda_codec_read(codec, fg, 0,
3662 AC_VERB_SET_POWER_STATE,
3663 power_state);
3664 snd_hda_codec_set_power_to_all(codec, fg, power_state,
3665 true);
3666 }
3667 state = hda_sync_power_state(codec, fg, power_state);
Wang Xingchao09617ce2012-06-08 10:26:08 +08003668 if (!(state & AC_PWRST_ERROR))
3669 break;
3670 }
Mengdong Linb8dfc4622012-08-23 17:32:30 +08003671
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003672 return state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003673}
Takashi Iwai54d17402005-11-21 16:33:22 +01003674
Takashi Iwai11aeff02008-07-30 15:01:46 +02003675#ifdef CONFIG_SND_HDA_HWDEP
3676/* execute additional init verbs */
3677static void hda_exec_init_verbs(struct hda_codec *codec)
3678{
3679 if (codec->init_verbs.list)
3680 snd_hda_sequence_write(codec, codec->init_verbs.list);
3681}
3682#else
3683static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3684#endif
3685
Takashi Iwai2a439522011-07-26 09:52:50 +02003686#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003687/*
3688 * call suspend and power-down; used both from PM and power-save
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003689 * this function returns the power state in the end
Takashi Iwaicb53c622007-08-10 17:21:45 +02003690 */
Dylan Reidd17344b2012-09-28 15:57:01 -07003691static unsigned int hda_call_codec_suspend(struct hda_codec *codec, bool in_wq)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003692{
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003693 unsigned int state;
3694
Takashi Iwai989c3182012-11-19 14:14:58 +01003695 codec->in_pm = 1;
3696
Takashi Iwaicb53c622007-08-10 17:21:45 +02003697 if (codec->patch_ops.suspend)
Takashi Iwai68cb2b52012-07-02 15:20:37 +02003698 codec->patch_ops.suspend(codec);
Takashi Iwaieb541332010-08-06 13:48:11 +02003699 hda_cleanup_all_streams(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07003700 state = hda_set_power_state(codec, AC_PWRST_D3);
Dylan Reidd17344b2012-09-28 15:57:01 -07003701 /* Cancel delayed work if we aren't currently running from it. */
3702 if (!in_wq)
3703 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02003704 spin_lock(&codec->power_lock);
3705 snd_hda_update_power_acct(codec);
3706 trace_hda_power_down(codec);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02003707 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02003708 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003709 codec->power_jiffies = jiffies;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02003710 spin_unlock(&codec->power_lock);
Takashi Iwai989c3182012-11-19 14:14:58 +01003711 codec->in_pm = 0;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003712 return state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003713}
3714
Takashi Iwaic370dd62012-12-13 18:30:04 +01003715/* mark all entries of cmd and amp caches dirty */
3716static void hda_mark_cmd_cache_dirty(struct hda_codec *codec)
3717{
3718 int i;
3719 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3720 struct hda_cache_head *cmd;
3721 cmd = snd_array_elem(&codec->cmd_cache.buf, i);
3722 cmd->dirty = 1;
3723 }
3724 for (i = 0; i < codec->amp_cache.buf.used; i++) {
3725 struct hda_amp_info *amp;
3726 amp = snd_array_elem(&codec->cmd_cache.buf, i);
3727 amp->head.dirty = 1;
3728 }
3729}
3730
Takashi Iwaicb53c622007-08-10 17:21:45 +02003731/*
3732 * kick up codec; used both from PM and power-save
3733 */
3734static void hda_call_codec_resume(struct hda_codec *codec)
3735{
Takashi Iwai989c3182012-11-19 14:14:58 +01003736 codec->in_pm = 1;
3737
Takashi Iwaic370dd62012-12-13 18:30:04 +01003738 hda_mark_cmd_cache_dirty(codec);
3739
Takashi Iwai7f308302012-05-08 16:52:23 +02003740 /* set as if powered on for avoiding re-entering the resume
3741 * in the resume / power-save sequence
3742 */
3743 hda_keep_power_on(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07003744 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02003745 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02003746 hda_exec_init_verbs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003747 if (codec->patch_ops.resume)
3748 codec->patch_ops.resume(codec);
3749 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02003750 if (codec->patch_ops.init)
3751 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003752 snd_hda_codec_resume_amp(codec);
3753 snd_hda_codec_resume_cache(codec);
3754 }
David Henningsson26a6cb62012-10-09 15:04:21 +02003755
3756 if (codec->jackpoll_interval)
3757 hda_jackpoll_work(&codec->jackpoll_work.work);
3758 else {
3759 snd_hda_jack_set_dirty_all(codec);
3760 snd_hda_jack_report_sync(codec);
3761 }
Takashi Iwai989c3182012-11-19 14:14:58 +01003762
3763 codec->in_pm = 0;
Takashi Iwai7f308302012-05-08 16:52:23 +02003764 snd_hda_power_down(codec); /* flag down before returning */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003765}
Takashi Iwai2a439522011-07-26 09:52:50 +02003766#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003767
Takashi Iwai54d17402005-11-21 16:33:22 +01003768
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769/**
3770 * snd_hda_build_controls - build mixer controls
3771 * @bus: the BUS
3772 *
3773 * Creates mixer controls for each codec included in the bus.
3774 *
3775 * Returns 0 if successful, otherwise a negative error code.
3776 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01003777int snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003779 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780
Takashi Iwai0ba21762007-04-16 11:29:14 +02003781 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003782 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003783 if (err < 0) {
Takashi Iwai28d1a852010-03-15 09:05:46 +01003784 printk(KERN_ERR "hda_codec: cannot build controls "
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003785 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003786 err = snd_hda_codec_reset(codec);
3787 if (err < 0) {
3788 printk(KERN_ERR
3789 "hda_codec: cannot revert codec\n");
3790 return err;
3791 }
3792 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003794 return 0;
3795}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003796EXPORT_SYMBOL_HDA(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003798/*
3799 * add standard channel maps if not specified
3800 */
3801static int add_std_chmaps(struct hda_codec *codec)
3802{
3803 int i, str, err;
3804
3805 for (i = 0; i < codec->num_pcms; i++) {
3806 for (str = 0; str < 2; str++) {
3807 struct snd_pcm *pcm = codec->pcm_info[i].pcm;
3808 struct hda_pcm_stream *hinfo =
3809 &codec->pcm_info[i].stream[str];
3810 struct snd_pcm_chmap *chmap;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003811 const struct snd_pcm_chmap_elem *elem;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003812
3813 if (codec->pcm_info[i].own_chmap)
3814 continue;
3815 if (!pcm || !hinfo->substreams)
3816 continue;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003817 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
3818 err = snd_pcm_add_chmap_ctls(pcm, str, elem,
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003819 hinfo->channels_max,
3820 0, &chmap);
3821 if (err < 0)
3822 return err;
3823 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
3824 }
3825 }
3826 return 0;
3827}
3828
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003829/* default channel maps for 2.1 speakers;
3830 * since HD-audio supports only stereo, odd number channels are omitted
3831 */
3832const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
3833 { .channels = 2,
3834 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
3835 { .channels = 4,
3836 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
3837 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
3838 { }
3839};
3840EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
3841
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003842int snd_hda_codec_build_controls(struct hda_codec *codec)
3843{
3844 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02003845 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003846 /* continue to initialize... */
3847 if (codec->patch_ops.init)
3848 err = codec->patch_ops.init(codec);
3849 if (!err && codec->patch_ops.build_controls)
3850 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003851 if (err < 0)
3852 return err;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003853
3854 /* we create chmaps here instead of build_pcms */
3855 err = add_std_chmaps(codec);
3856 if (err < 0)
3857 return err;
3858
David Henningsson26a6cb62012-10-09 15:04:21 +02003859 if (codec->jackpoll_interval)
3860 hda_jackpoll_work(&codec->jackpoll_work.work);
3861 else
3862 snd_hda_jack_report_sync(codec); /* call at the last init point */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863 return 0;
3864}
3865
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866/*
3867 * stream formats
3868 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02003869struct hda_rate_tbl {
3870 unsigned int hz;
3871 unsigned int alsa_bits;
3872 unsigned int hda_fmt;
3873};
3874
Takashi Iwai92f10b32010-08-03 14:21:00 +02003875/* rate = base * mult / div */
3876#define HDA_RATE(base, mult, div) \
3877 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
3878 (((div) - 1) << AC_FMT_DIV_SHIFT))
3879
Takashi Iwaibefdf312005-08-22 13:57:55 +02003880static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003882
3883 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003884 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
3885 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
3886 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
3887 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
3888 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
3889 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
3890 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
3891 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
3892 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
3893 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
3894 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003895#define AC_PAR_PCM_RATE_BITS 11
3896 /* up to bits 10, 384kHZ isn't supported properly */
3897
3898 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003899 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003900
Takashi Iwaibefdf312005-08-22 13:57:55 +02003901 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902};
3903
3904/**
3905 * snd_hda_calc_stream_format - calculate format bitset
3906 * @rate: the sample rate
3907 * @channels: the number of channels
3908 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3909 * @maxbps: the max. bps
3910 *
3911 * Calculate the format bitset from the given rate, channels and th PCM format.
3912 *
3913 * Return zero if invalid.
3914 */
3915unsigned int snd_hda_calc_stream_format(unsigned int rate,
3916 unsigned int channels,
3917 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03003918 unsigned int maxbps,
3919 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920{
3921 int i;
3922 unsigned int val = 0;
3923
Takashi Iwaibefdf312005-08-22 13:57:55 +02003924 for (i = 0; rate_bits[i].hz; i++)
3925 if (rate_bits[i].hz == rate) {
3926 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927 break;
3928 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003929 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930 snd_printdd("invalid rate %d\n", rate);
3931 return 0;
3932 }
3933
3934 if (channels == 0 || channels > 8) {
3935 snd_printdd("invalid channels %d\n", channels);
3936 return 0;
3937 }
3938 val |= channels - 1;
3939
3940 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003941 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003942 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003943 break;
3944 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003945 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003946 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947 case 20:
3948 case 24:
3949 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003950 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003951 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003953 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02003955 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956 break;
3957 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003958 snd_printdd("invalid format width %d\n",
3959 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960 return 0;
3961 }
3962
Anssi Hannula32c168c2010-08-03 13:28:57 +03003963 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003964 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03003965
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966 return val;
3967}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003968EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003970static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid,
3971 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003972{
3973 unsigned int val = 0;
3974 if (nid != codec->afg &&
3975 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
3976 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
3977 if (!val || val == -1)
3978 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
3979 if (!val || val == -1)
3980 return 0;
3981 return val;
3982}
3983
3984static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3985{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003986 return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003987 get_pcm_param);
3988}
3989
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003990static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid,
3991 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003992{
3993 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
3994 if (!streams || streams == -1)
3995 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
3996 if (!streams || streams == -1)
3997 return 0;
3998 return streams;
3999}
4000
4001static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
4002{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004003 return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004004 get_stream_param);
4005}
4006
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007/**
4008 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
4009 * @codec: the HDA codec
4010 * @nid: NID to query
4011 * @ratesp: the pointer to store the detected rate bitflags
4012 * @formatsp: the pointer to store the detected formats
4013 * @bpsp: the pointer to store the detected format widths
4014 *
4015 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
4016 * or @bsps argument is ignored.
4017 *
4018 * Returns 0 if successful, otherwise a negative error code.
4019 */
Stephen Warren384a48d2011-06-01 11:14:21 -06004020int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
4022{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004023 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004025 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004026 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027
4028 if (ratesp) {
4029 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004030 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02004032 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004034 if (rates == 0) {
4035 snd_printk(KERN_ERR "hda_codec: rates == 0 "
4036 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
4037 nid, val,
4038 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
4039 return -EIO;
4040 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041 *ratesp = rates;
4042 }
4043
4044 if (formatsp || bpsp) {
4045 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004046 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004048 streams = query_stream_param(codec, nid);
4049 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051
4052 bps = 0;
4053 if (streams & AC_SUPFMT_PCM) {
4054 if (val & AC_SUPPCM_BITS_8) {
4055 formats |= SNDRV_PCM_FMTBIT_U8;
4056 bps = 8;
4057 }
4058 if (val & AC_SUPPCM_BITS_16) {
4059 formats |= SNDRV_PCM_FMTBIT_S16_LE;
4060 bps = 16;
4061 }
4062 if (wcaps & AC_WCAP_DIGITAL) {
4063 if (val & AC_SUPPCM_BITS_32)
4064 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
4065 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
4066 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4067 if (val & AC_SUPPCM_BITS_24)
4068 bps = 24;
4069 else if (val & AC_SUPPCM_BITS_20)
4070 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004071 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
4072 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4074 if (val & AC_SUPPCM_BITS_32)
4075 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076 else if (val & AC_SUPPCM_BITS_24)
4077 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02004078 else if (val & AC_SUPPCM_BITS_20)
4079 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080 }
4081 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004082#if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
Takashi Iwaib5025c52009-07-01 18:05:27 +02004083 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004085 if (!bps)
4086 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02004087 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004088#endif
Takashi Iwaib5025c52009-07-01 18:05:27 +02004089 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02004090 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091 /* temporary hack: we have still no proper support
4092 * for the direct AC3 stream...
4093 */
4094 formats |= SNDRV_PCM_FMTBIT_U8;
4095 bps = 8;
4096 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004097 if (formats == 0) {
4098 snd_printk(KERN_ERR "hda_codec: formats == 0 "
4099 "(nid=0x%x, val=0x%x, ovrd=%i, "
4100 "streams=0x%x)\n",
4101 nid, val,
4102 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
4103 streams);
4104 return -EIO;
4105 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106 if (formatsp)
4107 *formatsp = formats;
4108 if (bpsp)
4109 *bpsp = bps;
4110 }
4111
4112 return 0;
4113}
Stephen Warren384a48d2011-06-01 11:14:21 -06004114EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115
4116/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01004117 * snd_hda_is_supported_format - Check the validity of the format
4118 * @codec: HD-audio codec
4119 * @nid: NID to check
4120 * @format: the HD-audio format value to check
4121 *
4122 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123 *
4124 * Returns 1 if supported, 0 if not.
4125 */
4126int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
4127 unsigned int format)
4128{
4129 int i;
4130 unsigned int val = 0, rate, stream;
4131
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004132 val = query_pcm_param(codec, nid);
4133 if (!val)
4134 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135
4136 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004137 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02004138 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139 if (val & (1 << i))
4140 break;
4141 return 0;
4142 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004143 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144 return 0;
4145
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004146 stream = query_stream_param(codec, nid);
4147 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148 return 0;
4149
4150 if (stream & AC_SUPFMT_PCM) {
4151 switch (format & 0xf0) {
4152 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004153 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154 return 0;
4155 break;
4156 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004157 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 return 0;
4159 break;
4160 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004161 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162 return 0;
4163 break;
4164 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004165 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166 return 0;
4167 break;
4168 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004169 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 return 0;
4171 break;
4172 default:
4173 return 0;
4174 }
4175 } else {
4176 /* FIXME: check for float32 and AC3? */
4177 }
4178
4179 return 1;
4180}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004181EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182
4183/*
4184 * PCM stuff
4185 */
4186static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
4187 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004188 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189{
4190 return 0;
4191}
4192
4193static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
4194 struct hda_codec *codec,
4195 unsigned int stream_tag,
4196 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004197 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198{
4199 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4200 return 0;
4201}
4202
4203static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
4204 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004205 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206{
Takashi Iwai888afa12008-03-18 09:57:50 +01004207 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208 return 0;
4209}
4210
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004211static int set_pcm_default_values(struct hda_codec *codec,
4212 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004214 int err;
4215
Takashi Iwai0ba21762007-04-16 11:29:14 +02004216 /* query support PCM information from the given NID */
4217 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004218 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02004219 info->rates ? NULL : &info->rates,
4220 info->formats ? NULL : &info->formats,
4221 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004222 if (err < 0)
4223 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224 }
4225 if (info->ops.open == NULL)
4226 info->ops.open = hda_pcm_default_open_close;
4227 if (info->ops.close == NULL)
4228 info->ops.close = hda_pcm_default_open_close;
4229 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004230 if (snd_BUG_ON(!info->nid))
4231 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232 info->ops.prepare = hda_pcm_default_prepare;
4233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004235 if (snd_BUG_ON(!info->nid))
4236 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237 info->ops.cleanup = hda_pcm_default_cleanup;
4238 }
4239 return 0;
4240}
4241
Takashi Iwaieb541332010-08-06 13:48:11 +02004242/*
4243 * codec prepare/cleanup entries
4244 */
4245int snd_hda_codec_prepare(struct hda_codec *codec,
4246 struct hda_pcm_stream *hinfo,
4247 unsigned int stream,
4248 unsigned int format,
4249 struct snd_pcm_substream *substream)
4250{
4251 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004252 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004253 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
4254 if (ret >= 0)
4255 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004256 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004257 return ret;
4258}
4259EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
4260
4261void snd_hda_codec_cleanup(struct hda_codec *codec,
4262 struct hda_pcm_stream *hinfo,
4263 struct snd_pcm_substream *substream)
4264{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004265 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004266 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004267 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004268}
4269EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
4270
Takashi Iwaid5191e52009-11-16 14:58:17 +01004271/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01004272const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
4273 "Audio", "SPDIF", "HDMI", "Modem"
4274};
4275
Takashi Iwai176d5332008-07-30 15:01:44 +02004276/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004277 * get the empty PCM device number to assign
Takashi Iwaic8936222010-01-28 17:08:53 +01004278 *
4279 * note the max device number is limited by HDA_MAX_PCMS, currently 10
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004280 */
4281static int get_empty_pcm_device(struct hda_bus *bus, int type)
4282{
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004283 /* audio device indices; not linear to keep compatibility */
4284 static int audio_idx[HDA_PCM_NTYPES][5] = {
4285 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
4286 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01004287 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004288 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004289 };
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004290 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004291
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004292 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004293 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
4294 return -EINVAL;
4295 }
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004296
4297 for (i = 0; audio_idx[type][i] >= 0 ; i++)
4298 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
4299 return audio_idx[type][i];
4300
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004301 /* non-fixed slots starting from 10 */
4302 for (i = 10; i < 32; i++) {
4303 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4304 return i;
4305 }
4306
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004307 snd_printk(KERN_WARNING "Too many %s devices\n",
4308 snd_hda_pcm_type_name[type]);
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004309 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004310}
4311
4312/*
Takashi Iwai176d5332008-07-30 15:01:44 +02004313 * attach a new PCM stream
4314 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004315static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02004316{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004317 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02004318 struct hda_pcm_stream *info;
4319 int stream, err;
4320
Takashi Iwaib91f0802008-11-04 08:43:08 +01004321 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02004322 return -EINVAL;
4323 for (stream = 0; stream < 2; stream++) {
4324 info = &pcm->stream[stream];
4325 if (info->substreams) {
4326 err = set_pcm_default_values(codec, info);
4327 if (err < 0)
4328 return err;
4329 }
4330 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004331 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02004332}
4333
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004334/* assign all PCMs of the given codec */
4335int snd_hda_codec_build_pcms(struct hda_codec *codec)
4336{
4337 unsigned int pcm;
4338 int err;
4339
4340 if (!codec->num_pcms) {
4341 if (!codec->patch_ops.build_pcms)
4342 return 0;
4343 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004344 if (err < 0) {
4345 printk(KERN_ERR "hda_codec: cannot build PCMs"
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004346 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004347 err = snd_hda_codec_reset(codec);
4348 if (err < 0) {
4349 printk(KERN_ERR
4350 "hda_codec: cannot revert codec\n");
4351 return err;
4352 }
4353 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004354 }
4355 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
4356 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
4357 int dev;
4358
4359 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01004360 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004361
4362 if (!cpcm->pcm) {
4363 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
4364 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004365 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004366 cpcm->device = dev;
4367 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004368 if (err < 0) {
4369 printk(KERN_ERR "hda_codec: cannot attach "
4370 "PCM stream %d for codec #%d\n",
4371 dev, codec->addr);
4372 continue; /* no fatal error */
4373 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004374 }
4375 }
4376 return 0;
4377}
4378
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379/**
4380 * snd_hda_build_pcms - build PCM information
4381 * @bus: the BUS
4382 *
4383 * Create PCM information for each codec included in the bus.
4384 *
4385 * The build_pcms codec patch is requested to set up codec->num_pcms and
4386 * codec->pcm_info properly. The array is referred by the top-level driver
4387 * to create its PCM instances.
4388 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4389 * callback.
4390 *
4391 * At least, substreams, channels_min and channels_max must be filled for
4392 * each stream. substreams = 0 indicates that the stream doesn't exist.
4393 * When rates and/or formats are zero, the supported values are queried
4394 * from the given nid. The nid is used also by the default ops.prepare
4395 * and ops.cleanup callbacks.
4396 *
4397 * The driver needs to call ops.open in its open callback. Similarly,
4398 * ops.close is supposed to be called in the close callback.
4399 * ops.prepare should be called in the prepare or hw_params callback
4400 * with the proper parameters for set up.
4401 * ops.cleanup should be called in hw_free for clean up of streams.
4402 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004403 * This function returns 0 if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404 */
Takashi Iwai5cb543d2012-08-09 13:49:23 +02004405int snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004407 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408
Takashi Iwai0ba21762007-04-16 11:29:14 +02004409 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004410 int err = snd_hda_codec_build_pcms(codec);
4411 if (err < 0)
4412 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413 }
4414 return 0;
4415}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004416EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418/**
4419 * snd_hda_check_board_config - compare the current codec with the config table
4420 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004421 * @num_configs: number of config enums
4422 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423 * @tbl: configuration table, terminated by null entries
4424 *
4425 * Compares the modelname or PCI subsystem id of the current codec with the
4426 * given configuration table. If a matching entry is found, returns its
4427 * config value (supposed to be 0 or positive).
4428 *
4429 * If no entries are matching, the function returns a negative value.
4430 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02004431int snd_hda_check_board_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004432 int num_configs, const char * const *models,
Takashi Iwai12f288b2007-08-02 15:51:59 +02004433 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434{
Takashi Iwaif44ac832008-07-30 15:01:45 +02004435 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004436 int i;
4437 for (i = 0; i < num_configs; i++) {
4438 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02004439 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004440 snd_printd(KERN_INFO "hda_codec: model '%s' is "
4441 "selected\n", models[i]);
4442 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443 }
4444 }
4445 }
4446
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004447 if (!codec->bus->pci || !tbl)
4448 return -1;
4449
4450 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
4451 if (!tbl)
4452 return -1;
4453 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02004454#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004455 char tmp[10];
4456 const char *model = NULL;
4457 if (models)
4458 model = models[tbl->value];
4459 if (!model) {
4460 sprintf(tmp, "#%d", tbl->value);
4461 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004463 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4464 "for config %x:%x (%s)\n",
4465 model, tbl->subvendor, tbl->subdevice,
4466 (tbl->name ? tbl->name : "Unknown device"));
4467#endif
4468 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469 }
4470 return -1;
4471}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004472EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473
4474/**
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004475 * snd_hda_check_board_codec_sid_config - compare the current codec
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004476 subsystem ID with the
4477 config table
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004478
4479 This is important for Gateway notebooks with SB450 HDA Audio
4480 where the vendor ID of the PCI device is:
4481 ATI Technologies Inc SB450 HDA Audio [1002:437b]
4482 and the vendor/subvendor are found only at the codec.
4483
4484 * @codec: the HDA codec
4485 * @num_configs: number of config enums
4486 * @models: array of model name strings
4487 * @tbl: configuration table, terminated by null entries
4488 *
4489 * Compares the modelname or PCI subsystem id of the current codec with the
4490 * given configuration table. If a matching entry is found, returns its
4491 * config value (supposed to be 0 or positive).
4492 *
4493 * If no entries are matching, the function returns a negative value.
4494 */
4495int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004496 int num_configs, const char * const *models,
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004497 const struct snd_pci_quirk *tbl)
4498{
4499 const struct snd_pci_quirk *q;
4500
4501 /* Search for codec ID */
4502 for (q = tbl; q->subvendor; q++) {
Takashi Iwaie2301a42011-11-22 19:58:56 +01004503 unsigned int mask = 0xffff0000 | q->subdevice_mask;
4504 unsigned int id = (q->subdevice | (q->subvendor << 16)) & mask;
4505 if ((codec->subsystem_id & mask) == id)
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004506 break;
4507 }
4508
4509 if (!q->subvendor)
4510 return -1;
4511
4512 tbl = q;
4513
4514 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwaid94ff6b2009-09-02 00:20:21 +02004515#ifdef CONFIG_SND_DEBUG_VERBOSE
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004516 char tmp[10];
4517 const char *model = NULL;
4518 if (models)
4519 model = models[tbl->value];
4520 if (!model) {
4521 sprintf(tmp, "#%d", tbl->value);
4522 model = tmp;
4523 }
4524 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4525 "for config %x:%x (%s)\n",
4526 model, tbl->subvendor, tbl->subdevice,
4527 (tbl->name ? tbl->name : "Unknown device"));
4528#endif
4529 return tbl->value;
4530 }
4531 return -1;
4532}
4533EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
4534
4535/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536 * snd_hda_add_new_ctls - create controls from the array
4537 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004538 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539 *
4540 * This helper function creates and add new controls in the given array.
4541 * The array must be terminated with an empty entry as terminator.
4542 *
4543 * Returns 0 if successful, or a negative error code.
4544 */
Takashi Iwai031024e2011-05-02 11:29:30 +02004545int snd_hda_add_new_ctls(struct hda_codec *codec,
4546 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01004548 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549
4550 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004551 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004552 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01004553 if (knew->iface == -1) /* skip this codec private value */
4554 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004555 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004556 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02004557 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01004558 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004559 if (addr > 0)
4560 kctl->id.device = addr;
4561 if (idx > 0)
4562 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01004563 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004564 if (!err)
4565 break;
4566 /* try first with another device index corresponding to
4567 * the codec addr; if it still fails (or it's the
4568 * primary codec), then try another control index
4569 */
4570 if (!addr && codec->addr)
4571 addr = codec->addr;
4572 else if (!idx && !knew->index) {
4573 idx = find_empty_mixer_ctl_idx(codec,
Takashi Iwaidcda5802012-10-12 17:24:51 +02004574 knew->name, 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004575 if (idx <= 0)
4576 return err;
4577 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01004578 return err;
4579 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580 }
4581 return 0;
4582}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004583EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584
Takashi Iwai83012a72012-08-24 18:38:08 +02004585#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02004586static void hda_power_work(struct work_struct *work)
4587{
4588 struct hda_codec *codec =
4589 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004590 struct hda_bus *bus = codec->bus;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004591 unsigned int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004592
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004593 spin_lock(&codec->power_lock);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004594 if (codec->power_transition > 0) { /* during power-up sequence? */
4595 spin_unlock(&codec->power_lock);
4596 return;
4597 }
Maxim Levitsky2e492462007-09-03 15:26:57 +02004598 if (!codec->power_on || codec->power_count) {
4599 codec->power_transition = 0;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004600 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004601 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02004602 }
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004603 spin_unlock(&codec->power_lock);
4604
Dylan Reidd17344b2012-09-28 15:57:01 -07004605 state = hda_call_codec_suspend(codec, true);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004606 codec->pm_down_notified = 0;
4607 if (!bus->power_keep_link_on && (state & AC_PWRST_CLK_STOP_OK)) {
4608 codec->pm_down_notified = 1;
Takashi Iwai68467f52012-08-28 09:14:29 -07004609 hda_call_pm_notify(bus, false);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004610 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02004611}
4612
4613static void hda_keep_power_on(struct hda_codec *codec)
4614{
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004615 spin_lock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004616 codec->power_count++;
4617 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004618 codec->power_jiffies = jiffies;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004619 spin_unlock(&codec->power_lock);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004620}
4621
Takashi Iwaid5191e52009-11-16 14:58:17 +01004622/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01004623void snd_hda_update_power_acct(struct hda_codec *codec)
4624{
4625 unsigned long delta = jiffies - codec->power_jiffies;
4626 if (codec->power_on)
4627 codec->power_on_acct += delta;
4628 else
4629 codec->power_off_acct += delta;
4630 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004631}
4632
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004633/* Transition to powered up, if wait_power_down then wait for a pending
4634 * transition to D3 to complete. A pending D3 transition is indicated
4635 * with power_transition == -1. */
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004636/* call this with codec->power_lock held! */
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004637static void __snd_hda_power_up(struct hda_codec *codec, bool wait_power_down)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004638{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004639 struct hda_bus *bus = codec->bus;
4640
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004641 /* Return if power_on or transitioning to power_on, unless currently
4642 * powering down. */
4643 if ((codec->power_on || codec->power_transition > 0) &&
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004644 !(wait_power_down && codec->power_transition < 0))
Takashi Iwaicb53c622007-08-10 17:21:45 +02004645 return;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004646 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004647
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004648 cancel_delayed_work_sync(&codec->power_work);
4649
4650 spin_lock(&codec->power_lock);
Dylan Reidb43d2242012-06-21 21:51:22 -07004651 /* If the power down delayed work was cancelled above before starting,
4652 * then there is no need to go through power up here.
4653 */
4654 if (codec->power_on) {
Takashi Iwai535b6c52012-08-20 21:25:22 +02004655 if (codec->power_transition < 0)
4656 codec->power_transition = 0;
Dylan Reidb43d2242012-06-21 21:51:22 -07004657 return;
4658 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004659
Takashi Iwaid66fee52011-08-02 15:39:31 +02004660 trace_hda_power_up(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004661 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004662 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004663 codec->power_jiffies = jiffies;
Takashi Iwai7f308302012-05-08 16:52:23 +02004664 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004665 spin_unlock(&codec->power_lock);
4666
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004667 if (codec->pm_down_notified) {
4668 codec->pm_down_notified = 0;
Takashi Iwai68467f52012-08-28 09:14:29 -07004669 hda_call_pm_notify(bus, true);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004670 }
4671
Takashi Iwaicb53c622007-08-10 17:21:45 +02004672 hda_call_codec_resume(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004673
4674 spin_lock(&codec->power_lock);
Takashi Iwaia221e282007-08-16 16:35:33 +02004675 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004676}
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004677
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004678#define power_save(codec) \
4679 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004680
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004681/* Transition to powered down */
4682static void __snd_hda_power_down(struct hda_codec *codec)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004683{
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004684 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004685 return;
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004686
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004687 if (power_save(codec)) {
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004688 codec->power_transition = -1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01004689 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004690 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02004691 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004692}
4693
4694/**
4695 * snd_hda_power_save - Power-up/down/sync the codec
4696 * @codec: HD-audio codec
4697 * @delta: the counter delta to change
4698 *
4699 * Change the power-up counter via @delta, and power up or down the hardware
4700 * appropriately. For the power-down, queue to the delayed action.
4701 * Passing zero to @delta means to synchronize the power state.
4702 */
4703void snd_hda_power_save(struct hda_codec *codec, int delta, bool d3wait)
4704{
4705 spin_lock(&codec->power_lock);
4706 codec->power_count += delta;
4707 trace_hda_power_count(codec);
4708 if (delta > 0)
4709 __snd_hda_power_up(codec, d3wait);
4710 else
4711 __snd_hda_power_down(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004712 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004713}
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004714EXPORT_SYMBOL_HDA(snd_hda_power_save);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004715
Takashi Iwaid5191e52009-11-16 14:58:17 +01004716/**
4717 * snd_hda_check_amp_list_power - Check the amp list and update the power
4718 * @codec: HD-audio codec
4719 * @check: the object containing an AMP list and the status
4720 * @nid: NID to check / update
4721 *
4722 * Check whether the given NID is in the amp list. If it's in the list,
4723 * check the current AMP status, and update the the power-status according
4724 * to the mute status.
4725 *
4726 * This function is supposed to be set or called from the check_power_status
4727 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004728 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004729int snd_hda_check_amp_list_power(struct hda_codec *codec,
4730 struct hda_loopback_check *check,
4731 hda_nid_t nid)
4732{
Takashi Iwai031024e2011-05-02 11:29:30 +02004733 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004734 int ch, v;
4735
4736 if (!check->amplist)
4737 return 0;
4738 for (p = check->amplist; p->nid; p++) {
4739 if (p->nid == nid)
4740 break;
4741 }
4742 if (!p->nid)
4743 return 0; /* nothing changed */
4744
4745 for (p = check->amplist; p->nid; p++) {
4746 for (ch = 0; ch < 2; ch++) {
4747 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4748 p->idx);
4749 if (!(v & HDA_AMP_MUTE) && v > 0) {
4750 if (!check->power_on) {
4751 check->power_on = 1;
4752 snd_hda_power_up(codec);
4753 }
4754 return 1;
4755 }
4756 }
4757 }
4758 if (check->power_on) {
4759 check->power_on = 0;
4760 snd_hda_power_down(codec);
4761 }
4762 return 0;
4763}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004764EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004765#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004767/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004768 * Channel mode helper
4769 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004770
4771/**
4772 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
4773 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004774int snd_hda_ch_mode_info(struct hda_codec *codec,
4775 struct snd_ctl_elem_info *uinfo,
4776 const struct hda_channel_mode *chmode,
4777 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004778{
4779 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4780 uinfo->count = 1;
4781 uinfo->value.enumerated.items = num_chmodes;
4782 if (uinfo->value.enumerated.item >= num_chmodes)
4783 uinfo->value.enumerated.item = num_chmodes - 1;
4784 sprintf(uinfo->value.enumerated.name, "%dch",
4785 chmode[uinfo->value.enumerated.item].channels);
4786 return 0;
4787}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004788EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004789
Takashi Iwaid5191e52009-11-16 14:58:17 +01004790/**
4791 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
4792 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004793int snd_hda_ch_mode_get(struct hda_codec *codec,
4794 struct snd_ctl_elem_value *ucontrol,
4795 const struct hda_channel_mode *chmode,
4796 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004797 int max_channels)
4798{
4799 int i;
4800
4801 for (i = 0; i < num_chmodes; i++) {
4802 if (max_channels == chmode[i].channels) {
4803 ucontrol->value.enumerated.item[0] = i;
4804 break;
4805 }
4806 }
4807 return 0;
4808}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004809EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004810
Takashi Iwaid5191e52009-11-16 14:58:17 +01004811/**
4812 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
4813 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004814int snd_hda_ch_mode_put(struct hda_codec *codec,
4815 struct snd_ctl_elem_value *ucontrol,
4816 const struct hda_channel_mode *chmode,
4817 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004818 int *max_channelsp)
4819{
4820 unsigned int mode;
4821
4822 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01004823 if (mode >= num_chmodes)
4824 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004825 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004826 return 0;
4827 /* change the current channel setting */
4828 *max_channelsp = chmode[mode].channels;
4829 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004830 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004831 return 1;
4832}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004833EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004834
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835/*
4836 * input MUX helper
4837 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004838
4839/**
4840 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
4841 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004842int snd_hda_input_mux_info(const struct hda_input_mux *imux,
4843 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844{
4845 unsigned int index;
4846
4847 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4848 uinfo->count = 1;
4849 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004850 if (!imux->num_items)
4851 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852 index = uinfo->value.enumerated.item;
4853 if (index >= imux->num_items)
4854 index = imux->num_items - 1;
4855 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
4856 return 0;
4857}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004858EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859
Takashi Iwaid5191e52009-11-16 14:58:17 +01004860/**
4861 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
4862 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004863int snd_hda_input_mux_put(struct hda_codec *codec,
4864 const struct hda_input_mux *imux,
4865 struct snd_ctl_elem_value *ucontrol,
4866 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867 unsigned int *cur_val)
4868{
4869 unsigned int idx;
4870
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004871 if (!imux->num_items)
4872 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873 idx = ucontrol->value.enumerated.item[0];
4874 if (idx >= imux->num_items)
4875 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004876 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004878 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
4879 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880 *cur_val = idx;
4881 return 1;
4882}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004883EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884
4885
4886/*
Takashi Iwaidda415d2012-11-30 18:34:38 +01004887 * process kcontrol info callback of a simple string enum array
4888 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
4889 */
4890int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
4891 struct snd_ctl_elem_info *uinfo,
4892 int num_items, const char * const *texts)
4893{
4894 static const char * const texts_default[] = {
4895 "Disabled", "Enabled"
4896 };
4897
4898 if (!texts || !num_items) {
4899 num_items = 2;
4900 texts = texts_default;
4901 }
4902
4903 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4904 uinfo->count = 1;
4905 uinfo->value.enumerated.items = num_items;
4906 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
4907 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
4908 strcpy(uinfo->value.enumerated.name,
4909 texts[uinfo->value.enumerated.item]);
4910 return 0;
4911}
4912EXPORT_SYMBOL_HDA(snd_hda_enum_helper_info);
4913
4914/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915 * Multi-channel / digital-out PCM helper functions
4916 */
4917
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004918/* setup SPDIF output stream */
4919static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
4920 unsigned int stream_tag, unsigned int format)
4921{
Laurence Darby3bef1c32012-11-03 17:00:06 +00004922 struct hda_spdif_out *spdif;
4923 unsigned int curr_fmt;
4924 bool reset;
Stephen Warren7c935972011-06-01 11:14:17 -06004925
Laurence Darby3bef1c32012-11-03 17:00:06 +00004926 spdif = snd_hda_spdif_out_of_nid(codec, nid);
4927 curr_fmt = snd_hda_codec_read(codec, nid, 0,
4928 AC_VERB_GET_STREAM_FORMAT, 0);
4929 reset = codec->spdif_status_reset &&
4930 (spdif->ctls & AC_DIG1_ENABLE) &&
4931 curr_fmt != format;
4932
4933 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
4934 updated */
4935 if (reset)
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004936 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06004937 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02004938 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004939 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02004940 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004941 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004942 for (d = codec->slave_dig_outs; *d; d++)
4943 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
4944 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004945 }
Takashi Iwai2f728532008-09-25 16:32:41 +02004946 /* turn on again (if needed) */
Laurence Darby3bef1c32012-11-03 17:00:06 +00004947 if (reset)
Takashi Iwai2f728532008-09-25 16:32:41 +02004948 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06004949 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02004950}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004951
Takashi Iwai2f728532008-09-25 16:32:41 +02004952static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
4953{
4954 snd_hda_codec_cleanup_stream(codec, nid);
4955 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004956 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004957 for (d = codec->slave_dig_outs; *d; d++)
4958 snd_hda_codec_cleanup_stream(codec, *d);
4959 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004960}
4961
Takashi Iwaid5191e52009-11-16 14:58:17 +01004962/**
4963 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
4964 * @bus: HD-audio bus
4965 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004966void snd_hda_bus_reboot_notify(struct hda_bus *bus)
4967{
4968 struct hda_codec *codec;
4969
4970 if (!bus)
4971 return;
4972 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02004973 if (hda_codec_is_power_on(codec) &&
4974 codec->patch_ops.reboot_notify)
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004975 codec->patch_ops.reboot_notify(codec);
4976 }
4977}
Takashi Iwai8f217a22009-11-10 18:26:12 +01004978EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004979
Takashi Iwaid5191e52009-11-16 14:58:17 +01004980/**
4981 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004983int snd_hda_multi_out_dig_open(struct hda_codec *codec,
4984 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985{
Ingo Molnar62932df2006-01-16 16:34:20 +01004986 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02004987 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
4988 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02004989 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01004991 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992 return 0;
4993}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004994EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995
Takashi Iwaid5191e52009-11-16 14:58:17 +01004996/**
4997 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
4998 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004999int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
5000 struct hda_multi_out *mout,
5001 unsigned int stream_tag,
5002 unsigned int format,
5003 struct snd_pcm_substream *substream)
5004{
5005 mutex_lock(&codec->spdif_mutex);
5006 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
5007 mutex_unlock(&codec->spdif_mutex);
5008 return 0;
5009}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005010EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005011
Takashi Iwaid5191e52009-11-16 14:58:17 +01005012/**
5013 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
5014 */
Takashi Iwai9411e212009-02-13 11:32:28 +01005015int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
5016 struct hda_multi_out *mout)
5017{
5018 mutex_lock(&codec->spdif_mutex);
5019 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5020 mutex_unlock(&codec->spdif_mutex);
5021 return 0;
5022}
5023EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
5024
Takashi Iwaid5191e52009-11-16 14:58:17 +01005025/**
5026 * snd_hda_multi_out_dig_close - release the digital out stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005028int snd_hda_multi_out_dig_close(struct hda_codec *codec,
5029 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030{
Ingo Molnar62932df2006-01-16 16:34:20 +01005031 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01005033 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034 return 0;
5035}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005036EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037
Takashi Iwaid5191e52009-11-16 14:58:17 +01005038/**
5039 * snd_hda_multi_out_analog_open - open analog outputs
5040 *
5041 * Open analog outputs and set up the hw-constraints.
5042 * If the digital outputs can be opened as slave, open the digital
5043 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005045int snd_hda_multi_out_analog_open(struct hda_codec *codec,
5046 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01005047 struct snd_pcm_substream *substream,
5048 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005049{
Takashi Iwai9a081602008-02-12 18:37:26 +01005050 struct snd_pcm_runtime *runtime = substream->runtime;
5051 runtime->hw.channels_max = mout->max_channels;
5052 if (mout->dig_out_nid) {
5053 if (!mout->analog_rates) {
5054 mout->analog_rates = hinfo->rates;
5055 mout->analog_formats = hinfo->formats;
5056 mout->analog_maxbps = hinfo->maxbps;
5057 } else {
5058 runtime->hw.rates = mout->analog_rates;
5059 runtime->hw.formats = mout->analog_formats;
5060 hinfo->maxbps = mout->analog_maxbps;
5061 }
5062 if (!mout->spdif_rates) {
5063 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
5064 &mout->spdif_rates,
5065 &mout->spdif_formats,
5066 &mout->spdif_maxbps);
5067 }
5068 mutex_lock(&codec->spdif_mutex);
5069 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02005070 if ((runtime->hw.rates & mout->spdif_rates) &&
5071 (runtime->hw.formats & mout->spdif_formats)) {
5072 runtime->hw.rates &= mout->spdif_rates;
5073 runtime->hw.formats &= mout->spdif_formats;
5074 if (mout->spdif_maxbps < hinfo->maxbps)
5075 hinfo->maxbps = mout->spdif_maxbps;
5076 } else {
5077 mout->share_spdif = 0;
5078 /* FIXME: need notify? */
5079 }
Takashi Iwai9a081602008-02-12 18:37:26 +01005080 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02005081 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01005082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083 return snd_pcm_hw_constraint_step(substream->runtime, 0,
5084 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
5085}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005086EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087
Takashi Iwaid5191e52009-11-16 14:58:17 +01005088/**
5089 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
5090 *
5091 * Set up the i/o for analog out.
5092 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005094int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
5095 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096 unsigned int stream_tag,
5097 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01005098 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005099{
Takashi Iwaidda14412011-05-02 11:29:30 +02005100 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101 int chs = substream->runtime->channels;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005102 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103 int i;
5104
Ingo Molnar62932df2006-01-16 16:34:20 +01005105 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005106 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Takashi Iwai9a081602008-02-12 18:37:26 +01005107 if (mout->dig_out_nid && mout->share_spdif &&
5108 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02005110 snd_hda_is_supported_format(codec, mout->dig_out_nid,
5111 format) &&
Stephen Warren7c935972011-06-01 11:14:17 -06005112 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005114 setup_dig_out_stream(codec, mout->dig_out_nid,
5115 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116 } else {
5117 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02005118 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119 }
5120 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005121 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005122
5123 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005124 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
5125 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005126 if (!mout->no_share_stream &&
5127 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005128 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005129 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
5130 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005131 /* extra outputs copied from front */
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005132 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5133 if (!mout->no_share_stream && mout->hp_out_nid[i])
5134 snd_hda_codec_setup_stream(codec,
5135 mout->hp_out_nid[i],
5136 stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005137 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02005138 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01005139 snd_hda_codec_setup_stream(codec,
5140 mout->extra_out_nid[i],
5141 stream_tag, 0, format);
5142
Linus Torvalds1da177e2005-04-16 15:20:36 -07005143 /* surrounds */
5144 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02005145 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005146 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5147 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005148 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005149 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5150 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005151 }
5152 return 0;
5153}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005154EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155
Takashi Iwaid5191e52009-11-16 14:58:17 +01005156/**
5157 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005159int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
5160 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161{
Takashi Iwaidda14412011-05-02 11:29:30 +02005162 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163 int i;
5164
5165 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01005166 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005167 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01005168 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005169 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5170 if (mout->hp_out_nid[i])
5171 snd_hda_codec_cleanup_stream(codec,
5172 mout->hp_out_nid[i]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005173 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
5174 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01005175 snd_hda_codec_cleanup_stream(codec,
5176 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01005177 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02005179 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180 mout->dig_out_used = 0;
5181 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005182 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005183 return 0;
5184}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005185EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186
Takashi Iwai47408602012-04-20 13:06:53 +02005187/**
5188 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
5189 *
5190 * Guess the suitable VREF pin bits to be set as the pin-control value.
5191 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
5192 */
5193unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
5194{
5195 unsigned int pincap;
5196 unsigned int oldval;
5197 oldval = snd_hda_codec_read(codec, pin, 0,
5198 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5199 pincap = snd_hda_query_pin_caps(codec, pin);
5200 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5201 /* Exception: if the default pin setup is vref50, we give it priority */
5202 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
5203 return AC_PINCTL_VREF_80;
5204 else if (pincap & AC_PINCAP_VREF_50)
5205 return AC_PINCTL_VREF_50;
5206 else if (pincap & AC_PINCAP_VREF_100)
5207 return AC_PINCTL_VREF_100;
5208 else if (pincap & AC_PINCAP_VREF_GRD)
5209 return AC_PINCTL_VREF_GRD;
5210 return AC_PINCTL_VREF_HIZ;
5211}
5212EXPORT_SYMBOL_HDA(snd_hda_get_default_vref);
5213
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005214int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
5215 unsigned int val, bool cached)
5216{
5217 if (val) {
5218 unsigned int cap = snd_hda_query_pin_caps(codec, pin);
Takashi Iwai6942c102012-04-20 13:08:40 +02005219 if (cap && (val & AC_PINCTL_OUT_EN)) {
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005220 if (!(cap & AC_PINCAP_OUT))
5221 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
5222 else if ((val & AC_PINCTL_HP_EN) &&
5223 !(cap & AC_PINCAP_HP_DRV))
5224 val &= ~AC_PINCTL_HP_EN;
5225 }
Takashi Iwai6942c102012-04-20 13:08:40 +02005226 if (cap && (val & AC_PINCTL_IN_EN)) {
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005227 if (!(cap & AC_PINCAP_IN))
5228 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
5229 }
5230 }
5231 if (cached)
5232 return snd_hda_codec_update_cache(codec, pin, 0,
5233 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5234 else
5235 return snd_hda_codec_write(codec, pin, 0,
5236 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5237}
5238EXPORT_SYMBOL_HDA(_snd_hda_set_pin_ctl);
5239
Takashi Iwai990061c2010-09-09 22:08:44 +02005240/**
5241 * snd_hda_add_imux_item - Add an item to input_mux
5242 *
5243 * When the same label is used already in the existing items, the number
5244 * suffix is appended to the label. This label index number is stored
5245 * to type_idx when non-NULL pointer is given.
5246 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02005247int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
5248 int index, int *type_idx)
5249{
5250 int i, label_idx = 0;
5251 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
5252 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
5253 return -EINVAL;
5254 }
5255 for (i = 0; i < imux->num_items; i++) {
5256 if (!strncmp(label, imux->items[i].label, strlen(label)))
5257 label_idx++;
5258 }
5259 if (type_idx)
5260 *type_idx = label_idx;
5261 if (label_idx > 0)
5262 snprintf(imux->items[imux->num_items].label,
5263 sizeof(imux->items[imux->num_items].label),
5264 "%s %d", label, label_idx);
5265 else
5266 strlcpy(imux->items[imux->num_items].label, label,
5267 sizeof(imux->items[imux->num_items].label));
5268 imux->items[imux->num_items].index = index;
5269 imux->num_items++;
5270 return 0;
5271}
5272EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005273
Takashi Iwai4a471b72005-12-07 13:56:29 +01005274
Linus Torvalds1da177e2005-04-16 15:20:36 -07005275#ifdef CONFIG_PM
5276/*
5277 * power management
5278 */
5279
5280/**
5281 * snd_hda_suspend - suspend the codecs
5282 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005283 *
5284 * Returns 0 if successful.
5285 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02005286int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005288 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005289
Takashi Iwai0ba21762007-04-16 11:29:14 +02005290 list_for_each_entry(codec, &bus->codec_list, list) {
David Henningsson26a6cb62012-10-09 15:04:21 +02005291 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005292 if (hda_codec_is_power_on(codec))
Dylan Reidd17344b2012-09-28 15:57:01 -07005293 hda_call_codec_suspend(codec, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005294 }
5295 return 0;
5296}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005297EXPORT_SYMBOL_HDA(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298
5299/**
5300 * snd_hda_resume - resume the codecs
5301 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005302 *
5303 * Returns 0 if successful.
5304 */
5305int snd_hda_resume(struct hda_bus *bus)
5306{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005307 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005308
Takashi Iwai0ba21762007-04-16 11:29:14 +02005309 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai7f308302012-05-08 16:52:23 +02005310 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005312 return 0;
5313}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005314EXPORT_SYMBOL_HDA(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005315#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005316
5317/*
5318 * generic arrays
5319 */
5320
Takashi Iwaid5191e52009-11-16 14:58:17 +01005321/**
5322 * snd_array_new - get a new element from the given array
5323 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005324 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01005325 * Get a new element from the given array. If it exceeds the
5326 * pre-allocated array size, re-allocate the array.
5327 *
5328 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02005329 */
5330void *snd_array_new(struct snd_array *array)
5331{
Takashi Iwai12f17712012-10-10 08:59:14 +02005332 if (snd_BUG_ON(!array->elem_size))
5333 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005334 if (array->used >= array->alloced) {
5335 int num = array->alloced + array->alloc_align;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005336 int size = (num + 1) * array->elem_size;
Takashi Iwai00ef9612011-07-14 15:57:27 +02005337 int oldsize = array->alloced * array->elem_size;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01005338 void *nlist;
5339 if (snd_BUG_ON(num >= 4096))
5340 return NULL;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005341 nlist = krealloc(array->list, size, GFP_KERNEL);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005342 if (!nlist)
5343 return NULL;
Takashi Iwai00ef9612011-07-14 15:57:27 +02005344 memset(nlist + oldsize, 0, size - oldsize);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005345 array->list = nlist;
5346 array->alloced = num;
5347 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01005348 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005349}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005350EXPORT_SYMBOL_HDA(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005351
Takashi Iwaid5191e52009-11-16 14:58:17 +01005352/**
5353 * snd_array_free - free the given array elements
5354 * @array: the array object
5355 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005356void snd_array_free(struct snd_array *array)
5357{
5358 kfree(array->list);
5359 array->used = 0;
5360 array->alloced = 0;
5361 array->list = NULL;
5362}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005363EXPORT_SYMBOL_HDA(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01005364
Takashi Iwaid5191e52009-11-16 14:58:17 +01005365/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01005366 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5367 * @pcm: PCM caps bits
5368 * @buf: the string buffer to write
5369 * @buflen: the max buffer length
5370 *
5371 * used by hda_proc.c and hda_eld.c
5372 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005373void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5374{
5375 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5376 int i, j;
5377
5378 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5379 if (pcm & (AC_SUPPCM_BITS_8 << i))
5380 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5381
5382 buf[j] = '\0'; /* necessary when j == 0 */
5383}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005384EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005385
5386MODULE_DESCRIPTION("HDA codec core");
5387MODULE_LICENSE("GPL");