blob: 23c821428a43ba583b0ea37aefc1eb29875de27b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Universal Interface for Intel High Definition Audio Codec
3 *
4 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
5 *
6 *
7 * This driver is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This driver is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
Takashi Iwai18478e82012-03-09 17:51:10 +010022#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/init.h>
24#include <linux/delay.h>
25#include <linux/slab.h>
26#include <linux/pci.h>
Ingo Molnar62932df2006-01-16 16:34:20 +010027#include <linux/mutex.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040028#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <sound/core.h>
30#include "hda_codec.h"
31#include <sound/asoundef.h>
Jaroslav Kysela302e9c52006-07-05 17:39:49 +020032#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <sound/initval.h>
Takashi Iwaicd372fb2011-03-03 14:40:14 +010034#include <sound/jack.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "hda_local.h"
Jaroslav Kysela123c07a2009-10-21 14:48:23 +020036#include "hda_beep.h"
Takashi Iwai1835a0f2011-10-27 22:12:46 +020037#include "hda_jack.h"
Takashi Iwai28073142007-07-27 18:58:06 +020038#include <sound/hda_hwdep.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Takashi Iwaid66fee52011-08-02 15:39:31 +020040#define CREATE_TRACE_POINTS
41#include "hda_trace.h"
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043/*
44 * vendor / preset table
45 */
46
47struct hda_vendor_id {
48 unsigned int id;
49 const char *name;
50};
51
52/* codec vendor labels */
53static struct hda_vendor_id hda_vendor_ids[] = {
Takashi Iwaic8cd1282008-02-13 16:59:29 +010054 { 0x1002, "ATI" },
Takashi Iwaie5f14242009-07-01 18:11:44 +020055 { 0x1013, "Cirrus Logic" },
Takashi Iwaia9226252006-09-17 22:05:54 +020056 { 0x1057, "Motorola" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010057 { 0x1095, "Silicon Image" },
Takashi Iwai31117b72008-12-16 14:43:21 +010058 { 0x10de, "Nvidia" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010059 { 0x10ec, "Realtek" },
Takashi Iwai4e01f542009-04-16 08:53:34 +020060 { 0x1102, "Creative" },
Joseph Chanc577b8a2006-11-29 15:29:40 +010061 { 0x1106, "VIA" },
Matthew Ranostay7f168592007-10-18 17:38:17 +020062 { 0x111d, "IDT" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010063 { 0x11c1, "LSI" },
Takashi Iwai54b903e2005-05-15 14:30:10 +020064 { 0x11d4, "Analog Devices" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 { 0x13f6, "C-Media" },
Takashi Iwaia9226252006-09-17 22:05:54 +020066 { 0x14f1, "Conexant" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010067 { 0x17e8, "Chrontel" },
68 { 0x1854, "LG" },
Mark Brown8199de32008-10-28 14:50:13 +000069 { 0x1aec, "Wolfson Microelectronics" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 { 0x434d, "C-Media" },
Takashi Iwai74c61132008-12-18 09:11:33 +010071 { 0x8086, "Intel" },
Matt2f2f4252005-04-13 14:45:30 +020072 { 0x8384, "SigmaTel" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 {} /* terminator */
74};
75
Takashi Iwai1289e9e2008-11-27 15:47:11 +010076static DEFINE_MUTEX(preset_mutex);
77static LIST_HEAD(hda_preset_tables);
78
79int snd_hda_add_codec_preset(struct hda_codec_preset_list *preset)
80{
81 mutex_lock(&preset_mutex);
82 list_add_tail(&preset->list, &hda_preset_tables);
83 mutex_unlock(&preset_mutex);
84 return 0;
85}
Takashi Iwaiff7a3262008-11-28 15:17:06 +010086EXPORT_SYMBOL_HDA(snd_hda_add_codec_preset);
Takashi Iwai1289e9e2008-11-27 15:47:11 +010087
88int snd_hda_delete_codec_preset(struct hda_codec_preset_list *preset)
89{
90 mutex_lock(&preset_mutex);
91 list_del(&preset->list);
92 mutex_unlock(&preset_mutex);
93 return 0;
94}
Takashi Iwaiff7a3262008-11-28 15:17:06 +010095EXPORT_SYMBOL_HDA(snd_hda_delete_codec_preset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Takashi Iwai83012a72012-08-24 18:38:08 +020097#ifdef CONFIG_PM
Takashi Iwaid846b172012-11-24 11:58:24 +010098#define codec_in_pm(codec) ((codec)->in_pm)
Takashi Iwaicb53c622007-08-10 17:21:45 +020099static void hda_power_work(struct work_struct *work);
100static void hda_keep_power_on(struct hda_codec *codec);
Takashi Iwaie581f3d2011-07-26 10:19:20 +0200101#define hda_codec_is_power_on(codec) ((codec)->power_on)
Takashi Iwai68467f52012-08-28 09:14:29 -0700102static inline void hda_call_pm_notify(struct hda_bus *bus, bool power_up)
103{
104 if (bus->ops.pm_notify)
105 bus->ops.pm_notify(bus, power_up);
106}
Takashi Iwaicb53c622007-08-10 17:21:45 +0200107#else
Takashi Iwaid846b172012-11-24 11:58:24 +0100108#define codec_in_pm(codec) 0
Takashi Iwaicb53c622007-08-10 17:21:45 +0200109static inline void hda_keep_power_on(struct hda_codec *codec) {}
Takashi Iwaie581f3d2011-07-26 10:19:20 +0200110#define hda_codec_is_power_on(codec) 1
Takashi Iwai68467f52012-08-28 09:14:29 -0700111#define hda_call_pm_notify(bus, state) {}
Takashi Iwaicb53c622007-08-10 17:21:45 +0200112#endif
113
Takashi Iwaid5191e52009-11-16 14:58:17 +0100114/**
115 * snd_hda_get_jack_location - Give a location string of the jack
116 * @cfg: pin default config value
117 *
118 * Parse the pin default config value and returns the string of the
119 * jack location, e.g. "Rear", "Front", etc.
120 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400121const char *snd_hda_get_jack_location(u32 cfg)
122{
123 static char *bases[7] = {
124 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
125 };
126 static unsigned char specials_idx[] = {
127 0x07, 0x08,
128 0x17, 0x18, 0x19,
129 0x37, 0x38
130 };
131 static char *specials[] = {
132 "Rear Panel", "Drive Bar",
133 "Riser", "HDMI", "ATAPI",
134 "Mobile-In", "Mobile-Out"
135 };
136 int i;
137 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
138 if ((cfg & 0x0f) < 7)
139 return bases[cfg & 0x0f];
140 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
141 if (cfg == specials_idx[i])
142 return specials[i];
143 }
144 return "UNKNOWN";
145}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100146EXPORT_SYMBOL_HDA(snd_hda_get_jack_location);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400147
Takashi Iwaid5191e52009-11-16 14:58:17 +0100148/**
149 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
150 * @cfg: pin default config value
151 *
152 * Parse the pin default config value and returns the string of the
153 * jack connectivity, i.e. external or internal connection.
154 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400155const char *snd_hda_get_jack_connectivity(u32 cfg)
156{
157 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
158
159 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
160}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100161EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400162
Takashi Iwaid5191e52009-11-16 14:58:17 +0100163/**
164 * snd_hda_get_jack_type - Give a type string of the jack
165 * @cfg: pin default config value
166 *
167 * Parse the pin default config value and returns the string of the
168 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
169 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400170const char *snd_hda_get_jack_type(u32 cfg)
171{
172 static char *jack_types[16] = {
173 "Line Out", "Speaker", "HP Out", "CD",
174 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
175 "Line In", "Aux", "Mic", "Telephony",
176 "SPDIF In", "Digitial In", "Reserved", "Other"
177 };
178
179 return jack_types[(cfg & AC_DEFCFG_DEVICE)
180 >> AC_DEFCFG_DEVICE_SHIFT];
181}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100182EXPORT_SYMBOL_HDA(snd_hda_get_jack_type);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400183
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100184/*
185 * Compose a 32bit command word to be sent to the HD-audio controller
186 */
187static inline unsigned int
188make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int direct,
189 unsigned int verb, unsigned int parm)
190{
191 u32 val;
192
Takashi Iwai82e1b802009-07-17 12:47:34 +0200193 if ((codec->addr & ~0xf) || (direct & ~1) || (nid & ~0x7f) ||
194 (verb & ~0xfff) || (parm & ~0xffff)) {
Wu Fengguang6430aee2009-07-17 16:49:19 +0800195 printk(KERN_ERR "hda-codec: out of range cmd %x:%x:%x:%x:%x\n",
196 codec->addr, direct, nid, verb, parm);
197 return ~0;
198 }
199
200 val = (u32)codec->addr << 28;
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100201 val |= (u32)direct << 27;
202 val |= (u32)nid << 20;
203 val |= verb << 8;
204 val |= parm;
205 return val;
206}
207
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200208/*
209 * Send and receive a verb
210 */
211static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
212 unsigned int *res)
213{
214 struct hda_bus *bus = codec->bus;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200215 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200216
Wu Fengguang6430aee2009-07-17 16:49:19 +0800217 if (cmd == ~0)
218 return -1;
219
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200220 if (res)
221 *res = -1;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200222 again:
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200223 snd_hda_power_up(codec);
224 mutex_lock(&bus->cmd_mutex);
Takashi Iwaid66fee52011-08-02 15:39:31 +0200225 trace_hda_send_cmd(codec, cmd);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200226 err = bus->ops.command(bus, cmd);
Takashi Iwaid66fee52011-08-02 15:39:31 +0200227 if (!err && res) {
Wu Fengguangdeadff12009-08-01 18:45:16 +0800228 *res = bus->ops.get_response(bus, codec->addr);
Takashi Iwaid66fee52011-08-02 15:39:31 +0200229 trace_hda_get_response(codec, *res);
230 }
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200231 mutex_unlock(&bus->cmd_mutex);
232 snd_hda_power_down(codec);
Takashi Iwaid846b172012-11-24 11:58:24 +0100233 if (!codec_in_pm(codec) && res && *res == -1 && bus->rirb_error) {
Takashi Iwai8dd78332009-06-02 01:16:07 +0200234 if (bus->response_reset) {
235 snd_printd("hda_codec: resetting BUS due to "
236 "fatal communication error\n");
Takashi Iwaid66fee52011-08-02 15:39:31 +0200237 trace_hda_bus_reset(bus);
Takashi Iwai8dd78332009-06-02 01:16:07 +0200238 bus->ops.bus_reset(bus);
239 }
240 goto again;
241 }
242 /* clear reset-flag when the communication gets recovered */
Takashi Iwaid846b172012-11-24 11:58:24 +0100243 if (!err || codec_in_pm(codec))
Takashi Iwai8dd78332009-06-02 01:16:07 +0200244 bus->response_reset = 0;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200245 return err;
246}
247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248/**
249 * snd_hda_codec_read - send a command and get the response
250 * @codec: the HDA codec
251 * @nid: NID to send the command
252 * @direct: direct flag
253 * @verb: the verb to send
254 * @parm: the parameter for the verb
255 *
256 * Send a single command and read the corresponding response.
257 *
258 * Returns the obtained response value, or -1 for an error.
259 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200260unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
261 int direct,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 unsigned int verb, unsigned int parm)
263{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200264 unsigned cmd = make_codec_cmd(codec, nid, direct, verb, parm);
265 unsigned int res;
Greg Thelen9857edf2011-06-13 07:45:45 -0700266 if (codec_exec_verb(codec, cmd, &res))
267 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 return res;
269}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100270EXPORT_SYMBOL_HDA(snd_hda_codec_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272/**
273 * snd_hda_codec_write - send a single command without waiting for response
274 * @codec: the HDA codec
275 * @nid: NID to send the command
276 * @direct: direct flag
277 * @verb: the verb to send
278 * @parm: the parameter for the verb
279 *
280 * Send a single command without waiting for response.
281 *
282 * Returns 0 if successful, or a negative error code.
283 */
284int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
285 unsigned int verb, unsigned int parm)
286{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200287 unsigned int cmd = make_codec_cmd(codec, nid, direct, verb, parm);
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100288 unsigned int res;
Takashi Iwaib20f3b82009-06-02 01:20:22 +0200289 return codec_exec_verb(codec, cmd,
290 codec->bus->sync_write ? &res : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100292EXPORT_SYMBOL_HDA(snd_hda_codec_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
294/**
295 * snd_hda_sequence_write - sequence writes
296 * @codec: the HDA codec
297 * @seq: VERB array to send
298 *
299 * Send the commands sequentially from the given array.
300 * The array must be terminated with NID=0.
301 */
302void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
303{
304 for (; seq->nid; seq++)
305 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
306}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100307EXPORT_SYMBOL_HDA(snd_hda_sequence_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309/**
310 * snd_hda_get_sub_nodes - get the range of sub nodes
311 * @codec: the HDA codec
312 * @nid: NID to parse
313 * @start_id: the pointer to store the start NID
314 *
315 * Parse the NID and store the start NID of its sub-nodes.
316 * Returns the number of sub-nodes.
317 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200318int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
319 hda_nid_t *start_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
321 unsigned int parm;
322
323 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
Danny Tholene8a7f132007-09-11 21:41:56 +0200324 if (parm == -1)
325 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 *start_id = (parm >> 16) & 0x7fff;
327 return (int)(parm & 0x7fff);
328}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100329EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200331/* look up the cached results */
332static hda_nid_t *lookup_conn_list(struct snd_array *array, hda_nid_t nid)
333{
334 int i, len;
335 for (i = 0; i < array->used; ) {
336 hda_nid_t *p = snd_array_elem(array, i);
337 if (nid == *p)
338 return p;
339 len = p[1];
340 i += len + 2;
341 }
342 return NULL;
343}
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200344
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200345/* read the connection and add to the cache */
346static int read_and_add_raw_conns(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200347{
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200348 hda_nid_t list[HDA_MAX_CONNECTIONS];
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200349 int len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200350
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200351 len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list));
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200352 if (len < 0)
353 return len;
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200354 return snd_hda_override_conn_list(codec, nid, len, list);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200355}
Takashi Iwaidce20792011-06-24 14:10:28 +0200356
357/**
358 * snd_hda_get_connections - copy connection list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 * @codec: the HDA codec
360 * @nid: NID to parse
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200361 * @conn_list: connection list array; when NULL, checks only the size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 * @max_conns: max. number of connections to store
363 *
364 * Parses the connection list of the given widget and stores the list
365 * of NIDs.
366 *
367 * Returns the number of connections, or a negative error code.
368 */
369int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200370 hda_nid_t *conn_list, int max_conns)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200371{
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200372 struct snd_array *array = &codec->conn_lists;
373 int len;
374 hda_nid_t *p;
375 bool added = false;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200376
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200377 again:
378 mutex_lock(&codec->hash_mutex);
379 len = -1;
380 /* if the connection-list is already cached, read it */
381 p = lookup_conn_list(array, nid);
382 if (p) {
383 len = p[1];
384 if (conn_list && len > max_conns) {
385 snd_printk(KERN_ERR "hda_codec: "
386 "Too many connections %d for NID 0x%x\n",
387 len, nid);
388 mutex_unlock(&codec->hash_mutex);
389 return -EINVAL;
390 }
391 if (conn_list && len)
392 memcpy(conn_list, p + 2, len * sizeof(hda_nid_t));
Takashi Iwaidce20792011-06-24 14:10:28 +0200393 }
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200394 mutex_unlock(&codec->hash_mutex);
395 if (len >= 0)
396 return len;
397 if (snd_BUG_ON(added))
398 return -EINVAL;
399
400 len = read_and_add_raw_conns(codec, nid);
401 if (len < 0)
402 return len;
403 added = true;
404 goto again;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200405}
406EXPORT_SYMBOL_HDA(snd_hda_get_connections);
407
Takashi Iwai9e7717c2011-07-11 15:42:52 +0200408/**
409 * snd_hda_get_raw_connections - copy connection list without cache
410 * @codec: the HDA codec
411 * @nid: NID to parse
412 * @conn_list: connection list array
413 * @max_conns: max. number of connections to store
414 *
415 * Like snd_hda_get_connections(), copy the connection list but without
416 * checking through the connection-list cache.
417 * Currently called only from hda_proc.c, so not exported.
418 */
419int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid,
420 hda_nid_t *conn_list, int max_conns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
422 unsigned int parm;
Takashi Iwai54d17402005-11-21 16:33:22 +0100423 int i, conn_len, conns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 unsigned int shift, num_elems, mask;
Takashi Iwai1ba7a7c2009-07-27 12:56:26 +0200425 unsigned int wcaps;
Takashi Iwai54d17402005-11-21 16:33:22 +0100426 hda_nid_t prev_nid;
Takashi Iwai5fdaecd2012-12-20 10:45:55 +0100427 int null_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Takashi Iwaida3cec32008-08-08 17:12:14 +0200429 if (snd_BUG_ON(!conn_list || max_conns <= 0))
430 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Takashi Iwai1ba7a7c2009-07-27 12:56:26 +0200432 wcaps = get_wcaps(codec, nid);
433 if (!(wcaps & AC_WCAP_CONN_LIST) &&
Takashi Iwai8d087c72011-06-28 12:45:47 +0200434 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
435 return 0;
Jaroslav Kysela16a433d2009-07-22 16:20:40 +0200436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
438 if (parm & AC_CLIST_LONG) {
439 /* long form */
440 shift = 16;
441 num_elems = 2;
442 } else {
443 /* short form */
444 shift = 8;
445 num_elems = 4;
446 }
447 conn_len = parm & AC_CLIST_LENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 mask = (1 << (shift-1)) - 1;
449
Takashi Iwai0ba21762007-04-16 11:29:14 +0200450 if (!conn_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 return 0; /* no connection */
452
453 if (conn_len == 1) {
454 /* single connection */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200455 parm = snd_hda_codec_read(codec, nid, 0,
456 AC_VERB_GET_CONNECT_LIST, 0);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200457 if (parm == -1 && codec->bus->rirb_error)
458 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 conn_list[0] = parm & mask;
460 return 1;
461 }
462
463 /* multi connection */
464 conns = 0;
Takashi Iwai54d17402005-11-21 16:33:22 +0100465 prev_nid = 0;
466 for (i = 0; i < conn_len; i++) {
467 int range_val;
468 hda_nid_t val, n;
469
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200470 if (i % num_elems == 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +0100471 parm = snd_hda_codec_read(codec, nid, 0,
472 AC_VERB_GET_CONNECT_LIST, i);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200473 if (parm == -1 && codec->bus->rirb_error)
474 return -EIO;
475 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200476 range_val = !!(parm & (1 << (shift-1))); /* ranges */
Takashi Iwai54d17402005-11-21 16:33:22 +0100477 val = parm & mask;
Takashi Iwai5fdaecd2012-12-20 10:45:55 +0100478 if (val == 0 && null_count++) { /* no second chance */
Jaroslav Kysela2e9bf242009-07-18 11:48:19 +0200479 snd_printk(KERN_WARNING "hda_codec: "
480 "invalid CONNECT_LIST verb %x[%i]:%x\n",
481 nid, i, parm);
482 return 0;
483 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100484 parm >>= shift;
485 if (range_val) {
486 /* ranges between the previous and this one */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200487 if (!prev_nid || prev_nid >= val) {
488 snd_printk(KERN_WARNING "hda_codec: "
489 "invalid dep_range_val %x:%x\n",
490 prev_nid, val);
Takashi Iwai54d17402005-11-21 16:33:22 +0100491 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100493 for (n = prev_nid + 1; n <= val; n++) {
494 if (conns >= max_conns) {
Takashi Iwai5aacc212010-07-30 10:36:29 +0200495 snd_printk(KERN_ERR "hda_codec: "
496 "Too many connections %d for NID 0x%x\n",
497 conns, nid);
Takashi Iwai54d17402005-11-21 16:33:22 +0100498 return -EINVAL;
499 }
500 conn_list[conns++] = n;
501 }
502 } else {
503 if (conns >= max_conns) {
Takashi Iwai5aacc212010-07-30 10:36:29 +0200504 snd_printk(KERN_ERR "hda_codec: "
505 "Too many connections %d for NID 0x%x\n",
506 conns, nid);
Takashi Iwai54d17402005-11-21 16:33:22 +0100507 return -EINVAL;
508 }
509 conn_list[conns++] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100511 prev_nid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 }
513 return conns;
514}
515
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200516static bool add_conn_list(struct snd_array *array, hda_nid_t nid)
517{
518 hda_nid_t *p = snd_array_new(array);
519 if (!p)
520 return false;
521 *p = nid;
522 return true;
523}
524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525/**
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200526 * snd_hda_override_conn_list - add/modify the connection-list to cache
527 * @codec: the HDA codec
528 * @nid: NID to parse
529 * @len: number of connection list entries
530 * @list: the list of connection entries
531 *
532 * Add or modify the given connection-list to the cache. If the corresponding
533 * cache already exists, invalidate it and append a new one.
534 *
535 * Returns zero or a negative error code.
536 */
537int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
538 const hda_nid_t *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539{
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200540 struct snd_array *array = &codec->conn_lists;
541 hda_nid_t *p;
542 int i, old_used;
543
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200544 mutex_lock(&codec->hash_mutex);
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200545 p = lookup_conn_list(array, nid);
546 if (p)
547 *p = -1; /* invalidate the old entry */
548
549 old_used = array->used;
550 if (!add_conn_list(array, nid) || !add_conn_list(array, len))
551 goto error_add;
552 for (i = 0; i < len; i++)
553 if (!add_conn_list(array, list[i]))
554 goto error_add;
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200555 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200556 return 0;
557
558 error_add:
559 array->used = old_used;
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200560 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200561 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562}
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200563EXPORT_SYMBOL_HDA(snd_hda_override_conn_list);
564
565/**
Takashi Iwai8d087c72011-06-28 12:45:47 +0200566 * snd_hda_get_conn_index - get the connection index of the given NID
567 * @codec: the HDA codec
568 * @mux: NID containing the list
569 * @nid: NID to select
570 * @recursive: 1 when searching NID recursively, otherwise 0
571 *
572 * Parses the connection list of the widget @mux and checks whether the
573 * widget @nid is present. If it is, return the connection index.
574 * Otherwise it returns -1.
575 */
576int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
577 hda_nid_t nid, int recursive)
578{
579 hda_nid_t conn[HDA_MAX_NUM_INPUTS];
580 int i, nums;
581
582 nums = snd_hda_get_connections(codec, mux, conn, ARRAY_SIZE(conn));
583 for (i = 0; i < nums; i++)
584 if (conn[i] == nid)
585 return i;
586 if (!recursive)
587 return -1;
588 if (recursive > 5) {
589 snd_printd("hda_codec: too deep connection for 0x%x\n", nid);
590 return -1;
591 }
592 recursive++;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200593 for (i = 0; i < nums; i++) {
594 unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
595 if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
596 continue;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200597 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
598 return i;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200599 }
Takashi Iwai8d087c72011-06-28 12:45:47 +0200600 return -1;
601}
602EXPORT_SYMBOL_HDA(snd_hda_get_conn_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
604/**
605 * snd_hda_queue_unsol_event - add an unsolicited event to queue
606 * @bus: the BUS
607 * @res: unsolicited event (lower 32bit of RIRB entry)
608 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
609 *
610 * Adds the given event to the queue. The events are processed in
611 * the workqueue asynchronously. Call this function in the interrupt
612 * hanlder when RIRB receives an unsolicited event.
613 *
614 * Returns 0 if successful, or a negative error code.
615 */
616int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
617{
618 struct hda_bus_unsolicited *unsol;
619 unsigned int wp;
620
Takashi Iwaiecf726f2011-08-09 14:22:44 +0200621 trace_hda_unsol_event(bus, res, res_ex);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200622 unsol = bus->unsol;
623 if (!unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 return 0;
625
626 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
627 unsol->wp = wp;
628
629 wp <<= 1;
630 unsol->queue[wp] = res;
631 unsol->queue[wp + 1] = res_ex;
632
Takashi Iwai6acaed32009-01-12 10:09:24 +0100633 queue_work(bus->workq, &unsol->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
635 return 0;
636}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100637EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
639/*
Wu Fengguang5c1d1a92008-10-07 14:17:53 +0800640 * process queued unsolicited events
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 */
David Howellsc4028952006-11-22 14:57:56 +0000642static void process_unsol_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643{
David Howellsc4028952006-11-22 14:57:56 +0000644 struct hda_bus_unsolicited *unsol =
645 container_of(work, struct hda_bus_unsolicited, work);
646 struct hda_bus *bus = unsol->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 struct hda_codec *codec;
648 unsigned int rp, caddr, res;
649
650 while (unsol->rp != unsol->wp) {
651 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
652 unsol->rp = rp;
653 rp <<= 1;
654 res = unsol->queue[rp];
655 caddr = unsol->queue[rp + 1];
Takashi Iwai0ba21762007-04-16 11:29:14 +0200656 if (!(caddr & (1 << 4))) /* no unsolicited event? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 continue;
658 codec = bus->caddr_tbl[caddr & 0x0f];
659 if (codec && codec->patch_ops.unsol_event)
660 codec->patch_ops.unsol_event(codec, res);
661 }
662}
663
664/*
665 * initialize unsolicited queue
666 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200667static int init_unsol_queue(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668{
669 struct hda_bus_unsolicited *unsol;
670
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100671 if (bus->unsol) /* already initialized */
672 return 0;
673
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200674 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200675 if (!unsol) {
676 snd_printk(KERN_ERR "hda_codec: "
677 "can't allocate unsolicited queue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 return -ENOMEM;
679 }
David Howellsc4028952006-11-22 14:57:56 +0000680 INIT_WORK(&unsol->work, process_unsol_events);
681 unsol->bus = bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 bus->unsol = unsol;
683 return 0;
684}
685
686/*
687 * destructor
688 */
689static void snd_hda_codec_free(struct hda_codec *codec);
690
691static int snd_hda_bus_free(struct hda_bus *bus)
692{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200693 struct hda_codec *codec, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Takashi Iwai0ba21762007-04-16 11:29:14 +0200695 if (!bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 return 0;
Takashi Iwai6acaed32009-01-12 10:09:24 +0100697 if (bus->workq)
698 flush_workqueue(bus->workq);
699 if (bus->unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 kfree(bus->unsol);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200701 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 snd_hda_codec_free(codec);
703 }
704 if (bus->ops.private_free)
705 bus->ops.private_free(bus);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100706 if (bus->workq)
707 destroy_workqueue(bus->workq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 kfree(bus);
709 return 0;
710}
711
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100712static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
714 struct hda_bus *bus = device->device_data;
Takashi Iwaib94d35392008-11-21 09:08:06 +0100715 bus->shutdown = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 return snd_hda_bus_free(bus);
717}
718
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200719#ifdef CONFIG_SND_HDA_HWDEP
720static int snd_hda_bus_dev_register(struct snd_device *device)
721{
722 struct hda_bus *bus = device->device_data;
723 struct hda_codec *codec;
724 list_for_each_entry(codec, &bus->codec_list, list) {
725 snd_hda_hwdep_add_sysfs(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +0100726 snd_hda_hwdep_add_power_sysfs(codec);
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200727 }
728 return 0;
729}
730#else
731#define snd_hda_bus_dev_register NULL
732#endif
733
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734/**
735 * snd_hda_bus_new - create a HDA bus
736 * @card: the card entry
737 * @temp: the template for hda_bus information
738 * @busp: the pointer to store the created bus instance
739 *
740 * Returns 0 if successful, or a negative error code.
741 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +0100742int snd_hda_bus_new(struct snd_card *card,
Takashi Iwai756e2b02007-04-16 11:27:07 +0200743 const struct hda_bus_template *temp,
744 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745{
746 struct hda_bus *bus;
747 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100748 static struct snd_device_ops dev_ops = {
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200749 .dev_register = snd_hda_bus_dev_register,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 .dev_free = snd_hda_bus_dev_free,
751 };
752
Takashi Iwaida3cec32008-08-08 17:12:14 +0200753 if (snd_BUG_ON(!temp))
754 return -EINVAL;
755 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
756 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
758 if (busp)
759 *busp = NULL;
760
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200761 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 if (bus == NULL) {
763 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
764 return -ENOMEM;
765 }
766
767 bus->card = card;
768 bus->private_data = temp->private_data;
769 bus->pci = temp->pci;
770 bus->modelname = temp->modelname;
Takashi Iwaifee2fba2008-11-27 12:43:28 +0100771 bus->power_save = temp->power_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 bus->ops = temp->ops;
773
Ingo Molnar62932df2006-01-16 16:34:20 +0100774 mutex_init(&bus->cmd_mutex);
Takashi Iwai3f50ac62010-08-20 09:44:36 +0200775 mutex_init(&bus->prepare_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 INIT_LIST_HEAD(&bus->codec_list);
777
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100778 snprintf(bus->workq_name, sizeof(bus->workq_name),
779 "hd-audio%d", card->number);
780 bus->workq = create_singlethread_workqueue(bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100781 if (!bus->workq) {
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100782 snd_printk(KERN_ERR "cannot create workqueue %s\n",
783 bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100784 kfree(bus);
785 return -ENOMEM;
786 }
787
Takashi Iwai0ba21762007-04-16 11:29:14 +0200788 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
789 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 snd_hda_bus_free(bus);
791 return err;
792 }
793 if (busp)
794 *busp = bus;
795 return 0;
796}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100797EXPORT_SYMBOL_HDA(snd_hda_bus_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Takashi Iwai82467612007-07-27 19:15:54 +0200799#ifdef CONFIG_SND_HDA_GENERIC
800#define is_generic_config(codec) \
Takashi Iwaif44ac832008-07-30 15:01:45 +0200801 (codec->modelname && !strcmp(codec->modelname, "generic"))
Takashi Iwai82467612007-07-27 19:15:54 +0200802#else
803#define is_generic_config(codec) 0
804#endif
805
Takashi Iwai645f10c2008-11-28 15:07:37 +0100806#ifdef MODULE
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100807#define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
808#else
Takashi Iwai645f10c2008-11-28 15:07:37 +0100809#define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100810#endif
811
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812/*
813 * find a matching codec preset
814 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200815static const struct hda_codec_preset *
Takashi Iwai756e2b02007-04-16 11:27:07 +0200816find_codec_preset(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817{
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100818 struct hda_codec_preset_list *tbl;
819 const struct hda_codec_preset *preset;
Takashi Iwai5d908ab2012-08-24 18:40:10 +0200820 unsigned int mod_requested = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Takashi Iwai82467612007-07-27 19:15:54 +0200822 if (is_generic_config(codec))
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100823 return NULL; /* use the generic parser */
824
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100825 again:
826 mutex_lock(&preset_mutex);
827 list_for_each_entry(tbl, &hda_preset_tables, list) {
828 if (!try_module_get(tbl->owner)) {
829 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
830 continue;
831 }
832 for (preset = tbl->preset; preset->id; preset++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 u32 mask = preset->mask;
Marc Boucherca7cfae2008-01-22 15:32:25 +0100834 if (preset->afg && preset->afg != codec->afg)
835 continue;
836 if (preset->mfg && preset->mfg != codec->mfg)
837 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200838 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 mask = ~0;
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200840 if (preset->id == (codec->vendor_id & mask) &&
Takashi Iwai0ba21762007-04-16 11:29:14 +0200841 (!preset->rev ||
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100842 preset->rev == codec->revision_id)) {
843 mutex_unlock(&preset_mutex);
844 codec->owner = tbl->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 return preset;
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 }
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100848 module_put(tbl->owner);
849 }
850 mutex_unlock(&preset_mutex);
851
852 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
853 char name[32];
854 if (!mod_requested)
855 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
856 codec->vendor_id);
857 else
858 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
859 (codec->vendor_id >> 16) & 0xffff);
860 request_module(name);
861 mod_requested++;
862 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 }
864 return NULL;
865}
866
867/*
Takashi Iwaif44ac832008-07-30 15:01:45 +0200868 * get_codec_name - store the codec name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 */
Takashi Iwaif44ac832008-07-30 15:01:45 +0200870static int get_codec_name(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
872 const struct hda_vendor_id *c;
873 const char *vendor = NULL;
874 u16 vendor_id = codec->vendor_id >> 16;
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200875 char tmp[16];
876
877 if (codec->vendor_name)
878 goto get_chip_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
880 for (c = hda_vendor_ids; c->id; c++) {
881 if (c->id == vendor_id) {
882 vendor = c->name;
883 break;
884 }
885 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200886 if (!vendor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 sprintf(tmp, "Generic %04x", vendor_id);
888 vendor = tmp;
889 }
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200890 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
891 if (!codec->vendor_name)
892 return -ENOMEM;
893
894 get_chip_name:
895 if (codec->chip_name)
896 return 0;
897
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 if (codec->preset && codec->preset->name)
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200899 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
900 else {
901 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
902 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
903 }
904 if (!codec->chip_name)
Takashi Iwaif44ac832008-07-30 15:01:45 +0200905 return -ENOMEM;
906 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907}
908
909/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200910 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +0100912static void setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913{
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200914 int i, total_nodes, function_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 hda_nid_t nid;
916
917 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
918 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200919 function_id = snd_hda_param_read(codec, nid,
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200920 AC_PAR_FUNCTION_TYPE);
Jaroslav Kyselacd7643b2010-07-20 12:11:25 +0200921 switch (function_id & 0xff) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200922 case AC_GRP_AUDIO_FUNCTION:
923 codec->afg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200924 codec->afg_function_id = function_id & 0xff;
925 codec->afg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200926 break;
927 case AC_GRP_MODEM_FUNCTION:
928 codec->mfg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200929 codec->mfg_function_id = function_id & 0xff;
930 codec->mfg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200931 break;
932 default:
933 break;
934 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936}
937
938/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100939 * read widget caps for each widget and store in cache
940 */
941static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
942{
943 int i;
944 hda_nid_t nid;
945
946 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
947 &codec->start_nid);
948 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200949 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +0100950 return -ENOMEM;
951 nid = codec->start_nid;
952 for (i = 0; i < codec->num_nodes; i++, nid++)
953 codec->wcaps[i] = snd_hda_param_read(codec, nid,
954 AC_PAR_AUDIO_WIDGET_CAP);
955 return 0;
956}
957
Takashi Iwai3be14142009-02-20 14:11:16 +0100958/* read all pin default configurations and save codec->init_pins */
959static int read_pin_defaults(struct hda_codec *codec)
960{
961 int i;
962 hda_nid_t nid = codec->start_nid;
963
964 for (i = 0; i < codec->num_nodes; i++, nid++) {
965 struct hda_pincfg *pin;
966 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +0200967 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwai3be14142009-02-20 14:11:16 +0100968 if (wid_type != AC_WID_PIN)
969 continue;
970 pin = snd_array_new(&codec->init_pins);
971 if (!pin)
972 return -ENOMEM;
973 pin->nid = nid;
974 pin->cfg = snd_hda_codec_read(codec, nid, 0,
975 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200976 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
977 AC_VERB_GET_PIN_WIDGET_CONTROL,
978 0);
Takashi Iwai3be14142009-02-20 14:11:16 +0100979 }
980 return 0;
981}
982
983/* look up the given pin config list and return the item matching with NID */
984static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
985 struct snd_array *array,
986 hda_nid_t nid)
987{
988 int i;
989 for (i = 0; i < array->used; i++) {
990 struct hda_pincfg *pin = snd_array_elem(array, i);
991 if (pin->nid == nid)
992 return pin;
993 }
994 return NULL;
995}
996
Takashi Iwai3be14142009-02-20 14:11:16 +0100997/* set the current pin config value for the given NID.
998 * the value is cached, and read via snd_hda_codec_get_pincfg()
999 */
1000int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
1001 hda_nid_t nid, unsigned int cfg)
1002{
1003 struct hda_pincfg *pin;
1004
Takashi Iwaib82855a2009-12-27 11:24:56 +01001005 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
1006 return -EINVAL;
1007
Takashi Iwai3be14142009-02-20 14:11:16 +01001008 pin = look_up_pincfg(codec, list, nid);
1009 if (!pin) {
1010 pin = snd_array_new(list);
1011 if (!pin)
1012 return -ENOMEM;
1013 pin->nid = nid;
1014 }
1015 pin->cfg = cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001016 return 0;
1017}
1018
Takashi Iwaid5191e52009-11-16 14:58:17 +01001019/**
1020 * snd_hda_codec_set_pincfg - Override a pin default configuration
1021 * @codec: the HDA codec
1022 * @nid: NID to set the pin config
1023 * @cfg: the pin default config value
1024 *
1025 * Override a pin default configuration value in the cache.
1026 * This value can be read by snd_hda_codec_get_pincfg() in a higher
1027 * priority than the real hardware value.
1028 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001029int snd_hda_codec_set_pincfg(struct hda_codec *codec,
1030 hda_nid_t nid, unsigned int cfg)
1031{
Takashi Iwai346ff702009-02-23 09:42:57 +01001032 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001033}
1034EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
1035
Takashi Iwaid5191e52009-11-16 14:58:17 +01001036/**
1037 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
1038 * @codec: the HDA codec
1039 * @nid: NID to get the pin config
1040 *
1041 * Get the current pin config value of the given pin NID.
1042 * If the pincfg value is cached or overridden via sysfs or driver,
1043 * returns the cached value.
1044 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001045unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
1046{
1047 struct hda_pincfg *pin;
1048
Takashi Iwai3be14142009-02-20 14:11:16 +01001049#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +01001050 pin = look_up_pincfg(codec, &codec->user_pins, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +01001051 if (pin)
1052 return pin->cfg;
1053#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001054 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
1055 if (pin)
1056 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001057 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1058 if (pin)
1059 return pin->cfg;
1060 return 0;
1061}
1062EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
1063
Takashi Iwai92ee6162009-12-27 11:18:59 +01001064/**
1065 * snd_hda_shutup_pins - Shut up all pins
1066 * @codec: the HDA codec
1067 *
1068 * Clear all pin controls to shup up before suspend for avoiding click noise.
1069 * The controls aren't cached so that they can be resumed properly.
1070 */
1071void snd_hda_shutup_pins(struct hda_codec *codec)
1072{
1073 int i;
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001074 /* don't shut up pins when unloading the driver; otherwise it breaks
1075 * the default pin setup at the next load of the driver
1076 */
1077 if (codec->bus->shutdown)
1078 return;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001079 for (i = 0; i < codec->init_pins.used; i++) {
1080 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1081 /* use read here for syncing after issuing each verb */
1082 snd_hda_codec_read(codec, pin->nid, 0,
1083 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
1084 }
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001085 codec->pins_shutup = 1;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001086}
1087EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);
1088
Takashi Iwai2a439522011-07-26 09:52:50 +02001089#ifdef CONFIG_PM
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001090/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1091static void restore_shutup_pins(struct hda_codec *codec)
1092{
1093 int i;
1094 if (!codec->pins_shutup)
1095 return;
1096 if (codec->bus->shutdown)
1097 return;
1098 for (i = 0; i < codec->init_pins.used; i++) {
1099 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1100 snd_hda_codec_write(codec, pin->nid, 0,
1101 AC_VERB_SET_PIN_WIDGET_CONTROL,
1102 pin->ctrl);
1103 }
1104 codec->pins_shutup = 0;
1105}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001106#endif
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001107
David Henningsson26a6cb62012-10-09 15:04:21 +02001108static void hda_jackpoll_work(struct work_struct *work)
1109{
1110 struct hda_codec *codec =
1111 container_of(work, struct hda_codec, jackpoll_work.work);
1112 if (!codec->jackpoll_interval)
1113 return;
1114
1115 snd_hda_jack_set_dirty_all(codec);
1116 snd_hda_jack_poll_all(codec);
1117 queue_delayed_work(codec->bus->workq, &codec->jackpoll_work,
1118 codec->jackpoll_interval);
1119}
1120
Takashi Iwai01751f52007-08-10 16:59:39 +02001121static void init_hda_cache(struct hda_cache_rec *cache,
1122 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001123static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +02001124
Takashi Iwai3fdf1462012-11-22 08:16:31 +01001125/* release all pincfg lists */
1126static void free_init_pincfgs(struct hda_codec *codec)
Takashi Iwai3be14142009-02-20 14:11:16 +01001127{
Takashi Iwai346ff702009-02-23 09:42:57 +01001128 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001129#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +01001130 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001131#endif
Takashi Iwai3be14142009-02-20 14:11:16 +01001132 snd_array_free(&codec->init_pins);
1133}
1134
Takashi Iwai54d17402005-11-21 16:33:22 +01001135/*
Takashi Iwaieb541332010-08-06 13:48:11 +02001136 * audio-converter setup caches
1137 */
1138struct hda_cvt_setup {
1139 hda_nid_t nid;
1140 u8 stream_tag;
1141 u8 channel_id;
1142 u16 format_id;
1143 unsigned char active; /* cvt is currently used */
1144 unsigned char dirty; /* setups should be cleared */
1145};
1146
1147/* get or create a cache entry for the given audio converter NID */
1148static struct hda_cvt_setup *
1149get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1150{
1151 struct hda_cvt_setup *p;
1152 int i;
1153
1154 for (i = 0; i < codec->cvt_setups.used; i++) {
1155 p = snd_array_elem(&codec->cvt_setups, i);
1156 if (p->nid == nid)
1157 return p;
1158 }
1159 p = snd_array_new(&codec->cvt_setups);
1160 if (p)
1161 p->nid = nid;
1162 return p;
1163}
1164
1165/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 * codec destructor
1167 */
1168static void snd_hda_codec_free(struct hda_codec *codec)
1169{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001170 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 return;
David Henningsson26a6cb62012-10-09 15:04:21 +02001172 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai59cad162012-06-26 15:00:20 +02001173 snd_hda_jack_tbl_clear(codec);
Takashi Iwai3fdf1462012-11-22 08:16:31 +01001174 free_init_pincfgs(codec);
Takashi Iwai83012a72012-08-24 18:38:08 +02001175#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02001176 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01001177 flush_workqueue(codec->bus->workq);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001178#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001180 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001181 snd_array_free(&codec->nids);
Takashi Iwai59cad162012-06-26 15:00:20 +02001182 snd_array_free(&codec->cvt_setups);
Takashi Iwaia12d3e12011-04-07 15:55:15 +02001183 snd_array_free(&codec->conn_lists);
Stephen Warren7c9359762011-06-01 11:14:17 -06001184 snd_array_free(&codec->spdif_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 codec->bus->caddr_tbl[codec->addr] = NULL;
1186 if (codec->patch_ops.free)
1187 codec->patch_ops.free(codec);
Takashi Iwai83012a72012-08-24 18:38:08 +02001188#ifdef CONFIG_PM
Takashi Iwai08fa20a2012-08-31 07:46:56 -07001189 if (!codec->pm_down_notified) /* cancel leftover refcounts */
Takashi Iwai68467f52012-08-28 09:14:29 -07001190 hda_call_pm_notify(codec->bus, false);
1191#endif
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001192 module_put(codec->owner);
Takashi Iwai01751f52007-08-10 16:59:39 +02001193 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001194 free_hda_cache(&codec->cmd_cache);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001195 kfree(codec->vendor_name);
1196 kfree(codec->chip_name);
Takashi Iwaif44ac832008-07-30 15:01:45 +02001197 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +01001198 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 kfree(codec);
1200}
1201
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001202static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec,
1203 hda_nid_t fg, unsigned int power_state);
1204
Takashi Iwaid8193872012-08-31 07:54:38 -07001205static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001206 unsigned int power_state);
1207
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208/**
1209 * snd_hda_codec_new - create a HDA codec
1210 * @bus: the bus to assign
1211 * @codec_addr: the codec address
1212 * @codecp: the pointer to store the generated codec
1213 *
1214 * Returns 0 if successful, or a negative error code.
1215 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001216int snd_hda_codec_new(struct hda_bus *bus,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001217 unsigned int codec_addr,
1218 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219{
1220 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +02001221 char component[31];
Takashi Iwaid8193872012-08-31 07:54:38 -07001222 hda_nid_t fg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 int err;
1224
Takashi Iwaida3cec32008-08-08 17:12:14 +02001225 if (snd_BUG_ON(!bus))
1226 return -EINVAL;
1227 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1228 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
1230 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001231 snd_printk(KERN_ERR "hda_codec: "
1232 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 return -EBUSY;
1234 }
1235
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001236 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 if (codec == NULL) {
1238 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1239 return -ENOMEM;
1240 }
1241
1242 codec->bus = bus;
1243 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001244 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001245 mutex_init(&codec->control_mutex);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001246 mutex_init(&codec->hash_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +02001247 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001248 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001249 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1250 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001251 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001252 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +02001253 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Takashi Iwaia12d3e12011-04-07 15:55:15 +02001254 snd_array_init(&codec->conn_lists, sizeof(hda_nid_t), 64);
Stephen Warren7c9359762011-06-01 11:14:17 -06001255 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
Takashi Iwai361dab32012-05-09 14:35:27 +02001256 snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01001257 snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8);
David Henningsson26a6cb62012-10-09 15:04:21 +02001258 INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
Takashi Iwai83012a72012-08-24 18:38:08 +02001260#ifdef CONFIG_PM
Takashi Iwai5536c6d2012-05-08 17:08:10 +02001261 spin_lock_init(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001262 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1263 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1264 * the caller has to power down appropriatley after initialization
1265 * phase.
1266 */
1267 hda_keep_power_on(codec);
Takashi Iwai68467f52012-08-28 09:14:29 -07001268 hda_call_pm_notify(bus, true);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001269#endif
1270
Takashi Iwaic382a9f2012-05-07 15:01:02 +02001271 if (codec->bus->modelname) {
1272 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1273 if (!codec->modelname) {
1274 snd_hda_codec_free(codec);
1275 return -ENODEV;
1276 }
1277 }
1278
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 list_add_tail(&codec->list, &bus->codec_list);
1280 bus->caddr_tbl[codec_addr] = codec;
1281
Takashi Iwai0ba21762007-04-16 11:29:14 +02001282 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1283 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001284 if (codec->vendor_id == -1)
1285 /* read again, hopefully the access method was corrected
1286 * in the last read...
1287 */
1288 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1289 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001290 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1291 AC_PAR_SUBSYSTEM_ID);
1292 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1293 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001295 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001296 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001297 snd_printdd("hda_codec: no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001298 err = -ENODEV;
1299 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 }
1301
Takashi Iwaid8193872012-08-31 07:54:38 -07001302 fg = codec->afg ? codec->afg : codec->mfg;
1303 err = read_widget_caps(codec, fg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001304 if (err < 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +01001305 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001306 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001307 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001308 err = read_pin_defaults(codec);
1309 if (err < 0)
1310 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001311
Takashi Iwai0ba21762007-04-16 11:29:14 +02001312 if (!codec->subsystem_id) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001313 codec->subsystem_id =
Takashi Iwaid8193872012-08-31 07:54:38 -07001314 snd_hda_codec_read(codec, fg, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001315 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001316 }
1317
Takashi Iwai83012a72012-08-24 18:38:08 +02001318#ifdef CONFIG_PM
Takashi Iwaid8193872012-08-31 07:54:38 -07001319 codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec, fg,
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001320 AC_PWRST_CLKSTOP);
1321 if (!codec->d3_stop_clk)
1322 bus->power_keep_link_on = 1;
Mengdong Lin5d6147f2012-08-24 12:06:30 +08001323#endif
Takashi Iwaid8193872012-08-31 07:54:38 -07001324 codec->epss = snd_hda_codec_get_supported_ps(codec, fg,
Takashi Iwai983f6b92012-08-28 09:18:01 -07001325 AC_PWRST_EPSS);
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001326
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001327 /* power-up all before initialization */
Takashi Iwaid8193872012-08-31 07:54:38 -07001328 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001329
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001330 snd_hda_codec_proc_new(codec);
1331
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001332 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001333
1334 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1335 codec->subsystem_id, codec->revision_id);
1336 snd_component_add(codec->bus->card, component);
1337
1338 if (codecp)
1339 *codecp = codec;
1340 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001341
1342 error:
1343 snd_hda_codec_free(codec);
1344 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001345}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001346EXPORT_SYMBOL_HDA(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001347
Takashi Iwaid5191e52009-11-16 14:58:17 +01001348/**
1349 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1350 * @codec: the HDA codec
1351 *
1352 * Start parsing of the given codec tree and (re-)initialize the whole
1353 * patch instance.
1354 *
1355 * Returns 0 if successful or a negative error code.
1356 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001357int snd_hda_codec_configure(struct hda_codec *codec)
1358{
1359 int err;
1360
Takashi Iwaid5ad6302007-03-07 15:55:59 +01001361 codec->preset = find_codec_preset(codec);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001362 if (!codec->vendor_name || !codec->chip_name) {
Takashi Iwaif44ac832008-07-30 15:01:45 +02001363 err = get_codec_name(codec);
1364 if (err < 0)
1365 return err;
1366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
Takashi Iwai82467612007-07-27 19:15:54 +02001368 if (is_generic_config(codec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai82467612007-07-27 19:15:54 +02001370 goto patched;
1371 }
Takashi Iwai82467612007-07-27 19:15:54 +02001372 if (codec->preset && codec->preset->patch) {
1373 err = codec->preset->patch(codec);
1374 goto patched;
1375 }
1376
1377 /* call the default parser */
Takashi Iwai82467612007-07-27 19:15:54 +02001378 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai35a1e0c2007-10-19 08:13:40 +02001379 if (err < 0)
1380 printk(KERN_ERR "hda-codec: No codec parser is available\n");
Takashi Iwai82467612007-07-27 19:15:54 +02001381
1382 patched:
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001383 if (!err && codec->patch_ops.unsol_event)
1384 err = init_unsol_queue(codec->bus);
Takashi Iwaif62faed2009-12-23 09:27:51 +01001385 /* audio codec should override the mixer name */
1386 if (!err && (codec->afg || !*codec->bus->card->mixername))
1387 snprintf(codec->bus->card->mixername,
1388 sizeof(codec->bus->card->mixername),
1389 "%s %s", codec->vendor_name, codec->chip_name);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001390 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391}
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001392EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
Takashi Iwaied360812012-08-08 17:12:52 +02001394/* update the stream-id if changed */
1395static void update_pcm_stream_id(struct hda_codec *codec,
1396 struct hda_cvt_setup *p, hda_nid_t nid,
1397 u32 stream_tag, int channel_id)
1398{
1399 unsigned int oldval, newval;
1400
1401 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1402 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1403 newval = (stream_tag << 4) | channel_id;
1404 if (oldval != newval)
1405 snd_hda_codec_write(codec, nid, 0,
1406 AC_VERB_SET_CHANNEL_STREAMID,
1407 newval);
1408 p->stream_tag = stream_tag;
1409 p->channel_id = channel_id;
1410 }
1411}
1412
1413/* update the format-id if changed */
1414static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1415 hda_nid_t nid, int format)
1416{
1417 unsigned int oldval;
1418
1419 if (p->format_id != format) {
1420 oldval = snd_hda_codec_read(codec, nid, 0,
1421 AC_VERB_GET_STREAM_FORMAT, 0);
1422 if (oldval != format) {
1423 msleep(1);
1424 snd_hda_codec_write(codec, nid, 0,
1425 AC_VERB_SET_STREAM_FORMAT,
1426 format);
1427 }
1428 p->format_id = format;
1429 }
1430}
1431
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432/**
1433 * snd_hda_codec_setup_stream - set up the codec for streaming
1434 * @codec: the CODEC to set up
1435 * @nid: the NID to set up
1436 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1437 * @channel_id: channel id to pass, zero based.
1438 * @format: stream format.
1439 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001440void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1441 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 int channel_id, int format)
1443{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001444 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001445 struct hda_cvt_setup *p;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001446 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001447 int i;
1448
Takashi Iwai0ba21762007-04-16 11:29:14 +02001449 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001450 return;
1451
Takashi Iwai0ba21762007-04-16 11:29:14 +02001452 snd_printdd("hda_codec_setup_stream: "
1453 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001455 p = get_hda_cvt_setup(codec, nid);
1456 if (!p)
1457 return;
Takashi Iwaied360812012-08-08 17:12:52 +02001458
1459 if (codec->pcm_format_first)
1460 update_pcm_format(codec, p, nid, format);
1461 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1462 if (!codec->pcm_format_first)
1463 update_pcm_format(codec, p, nid, format);
1464
Takashi Iwaieb541332010-08-06 13:48:11 +02001465 p->active = 1;
1466 p->dirty = 0;
1467
1468 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001469 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001470 list_for_each_entry(c, &codec->bus->codec_list, list) {
1471 for (i = 0; i < c->cvt_setups.used; i++) {
1472 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001473 if (!p->active && p->stream_tag == stream_tag &&
David Henningsson54c2a892012-02-01 12:05:41 +01001474 get_wcaps_type(get_wcaps(c, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001475 p->dirty = 1;
1476 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001479EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
Takashi Iwaif0cea792010-08-13 11:56:53 +02001481static void really_cleanup_stream(struct hda_codec *codec,
1482 struct hda_cvt_setup *q);
1483
Takashi Iwaid5191e52009-11-16 14:58:17 +01001484/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001485 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001486 * @codec: the CODEC to clean up
1487 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001488 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001489 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001490void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1491 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001492{
Takashi Iwaieb541332010-08-06 13:48:11 +02001493 struct hda_cvt_setup *p;
1494
Takashi Iwai888afa12008-03-18 09:57:50 +01001495 if (!nid)
1496 return;
1497
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001498 if (codec->no_sticky_stream)
1499 do_now = 1;
1500
Takashi Iwai888afa12008-03-18 09:57:50 +01001501 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001502 p = get_hda_cvt_setup(codec, nid);
Takashi Iwaif0cea792010-08-13 11:56:53 +02001503 if (p) {
1504 /* here we just clear the active flag when do_now isn't set;
1505 * actual clean-ups will be done later in
1506 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1507 */
1508 if (do_now)
1509 really_cleanup_stream(codec, p);
1510 else
1511 p->active = 0;
1512 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001513}
Takashi Iwaif0cea792010-08-13 11:56:53 +02001514EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001515
Takashi Iwaieb541332010-08-06 13:48:11 +02001516static void really_cleanup_stream(struct hda_codec *codec,
1517 struct hda_cvt_setup *q)
1518{
1519 hda_nid_t nid = q->nid;
Takashi Iwai218264a2011-09-27 17:33:45 +02001520 if (q->stream_tag || q->channel_id)
1521 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1522 if (q->format_id)
1523 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1524);
Takashi Iwaieb541332010-08-06 13:48:11 +02001525 memset(q, 0, sizeof(*q));
1526 q->nid = nid;
1527}
1528
1529/* clean up the all conflicting obsolete streams */
1530static void purify_inactive_streams(struct hda_codec *codec)
1531{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001532 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001533 int i;
1534
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001535 list_for_each_entry(c, &codec->bus->codec_list, list) {
1536 for (i = 0; i < c->cvt_setups.used; i++) {
1537 struct hda_cvt_setup *p;
1538 p = snd_array_elem(&c->cvt_setups, i);
1539 if (p->dirty)
1540 really_cleanup_stream(c, p);
1541 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001542 }
1543}
1544
Takashi Iwai2a439522011-07-26 09:52:50 +02001545#ifdef CONFIG_PM
Takashi Iwaieb541332010-08-06 13:48:11 +02001546/* clean up all streams; called from suspend */
1547static void hda_cleanup_all_streams(struct hda_codec *codec)
1548{
1549 int i;
1550
1551 for (i = 0; i < codec->cvt_setups.used; i++) {
1552 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1553 if (p->stream_tag)
1554 really_cleanup_stream(codec, p);
1555 }
1556}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001557#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001558
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559/*
1560 * amp access functions
1561 */
1562
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001563/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001564#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001565#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001566#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1567#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001569#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
1571/* initialize the hash table */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001572static void init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001573 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574{
Takashi Iwai01751f52007-08-10 16:59:39 +02001575 memset(cache, 0, sizeof(*cache));
1576 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001577 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001578}
1579
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001580static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001581{
Takashi Iwai603c4012008-07-30 15:01:44 +02001582 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583}
1584
1585/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001586static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587{
Takashi Iwai01751f52007-08-10 16:59:39 +02001588 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1589 u16 cur = cache->hash[idx];
1590 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
1592 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001593 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 if (info->key == key)
1595 return info;
1596 cur = info->next;
1597 }
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001598 return NULL;
1599}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001601/* query the hash. allocate an entry if not found. */
1602static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1603 u32 key)
1604{
1605 struct hda_cache_head *info = get_hash(cache, key);
1606 if (!info) {
1607 u16 idx, cur;
1608 /* add a new hash entry */
1609 info = snd_array_new(&cache->buf);
1610 if (!info)
1611 return NULL;
1612 cur = snd_array_index(&cache->buf, info);
1613 info->key = key;
1614 info->val = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01001615 info->dirty = 0;
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001616 idx = key % (u16)ARRAY_SIZE(cache->hash);
1617 info->next = cache->hash[idx];
1618 cache->hash[idx] = cur;
1619 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 return info;
1621}
1622
Takashi Iwai01751f52007-08-10 16:59:39 +02001623/* query and allocate an amp hash entry */
1624static inline struct hda_amp_info *
1625get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1626{
1627 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1628}
1629
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001630/* overwrite the value with the key in the caps hash */
1631static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val)
1632{
1633 struct hda_amp_info *info;
1634
1635 mutex_lock(&codec->hash_mutex);
1636 info = get_alloc_amp_hash(codec, key);
1637 if (!info) {
1638 mutex_unlock(&codec->hash_mutex);
1639 return -EINVAL;
1640 }
1641 info->amp_caps = val;
1642 info->head.val |= INFO_AMP_CAPS;
1643 mutex_unlock(&codec->hash_mutex);
1644 return 0;
1645}
1646
1647/* query the value from the caps hash; if not found, fetch the current
1648 * value from the given function and store in the hash
1649 */
1650static unsigned int
1651query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key,
1652 unsigned int (*func)(struct hda_codec *, hda_nid_t, int))
1653{
1654 struct hda_amp_info *info;
1655 unsigned int val;
1656
1657 mutex_lock(&codec->hash_mutex);
1658 info = get_alloc_amp_hash(codec, key);
1659 if (!info) {
1660 mutex_unlock(&codec->hash_mutex);
1661 return 0;
1662 }
1663 if (!(info->head.val & INFO_AMP_CAPS)) {
1664 mutex_unlock(&codec->hash_mutex); /* for reentrance */
1665 val = func(codec, nid, dir);
1666 write_caps_hash(codec, key, val);
1667 } else {
1668 val = info->amp_caps;
1669 mutex_unlock(&codec->hash_mutex);
1670 }
1671 return val;
1672}
1673
1674static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid,
1675 int direction)
1676{
1677 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1678 nid = codec->afg;
1679 return snd_hda_param_read(codec, nid,
1680 direction == HDA_OUTPUT ?
1681 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
1682}
1683
Takashi Iwaid5191e52009-11-16 14:58:17 +01001684/**
1685 * query_amp_caps - query AMP capabilities
1686 * @codec: the HD-auio codec
1687 * @nid: the NID to query
1688 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1689 *
1690 * Query AMP capabilities for the given widget and direction.
1691 * Returns the obtained capability bits.
1692 *
1693 * When cap bits have been already read, this doesn't read again but
1694 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001696u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001698 return query_caps_hash(codec, nid, direction,
1699 HDA_HASH_KEY(nid, direction, 0),
1700 read_amp_cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001702EXPORT_SYMBOL_HDA(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
Takashi Iwaid5191e52009-11-16 14:58:17 +01001704/**
1705 * snd_hda_override_amp_caps - Override the AMP capabilities
1706 * @codec: the CODEC to clean up
1707 * @nid: the NID to clean up
1708 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1709 * @caps: the capability bits to set
1710 *
1711 * Override the cached AMP caps bits value by the given one.
1712 * This function is useful if the driver needs to adjust the AMP ranges,
1713 * e.g. limit to 0dB, etc.
1714 *
1715 * Returns zero if successful or a negative error code.
1716 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001717int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1718 unsigned int caps)
1719{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001720 return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001721}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001722EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001723
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001724static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid,
1725 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001726{
1727 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1728}
1729
Takashi Iwaid5191e52009-11-16 14:58:17 +01001730/**
1731 * snd_hda_query_pin_caps - Query PIN capabilities
1732 * @codec: the HD-auio codec
1733 * @nid: the NID to query
1734 *
1735 * Query PIN capabilities for the given widget.
1736 * Returns the obtained capability bits.
1737 *
1738 * When cap bits have been already read, this doesn't read again but
1739 * returns the cached value.
1740 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001741u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1742{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001743 return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001744 read_pin_cap);
1745}
Takashi Iwai1327a322009-03-23 13:07:47 +01001746EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1747
Wu Fengguang864f92b2009-11-18 12:38:02 +08001748/**
Takashi Iwaif57c2562011-08-15 12:49:07 +02001749 * snd_hda_override_pin_caps - Override the pin capabilities
1750 * @codec: the CODEC
1751 * @nid: the NID to override
1752 * @caps: the capability bits to set
1753 *
1754 * Override the cached PIN capabilitiy bits value by the given one.
1755 *
1756 * Returns zero if successful or a negative error code.
1757 */
1758int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
1759 unsigned int caps)
1760{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001761 return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps);
Takashi Iwaif57c2562011-08-15 12:49:07 +02001762}
1763EXPORT_SYMBOL_HDA(snd_hda_override_pin_caps);
1764
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001765/* read or sync the hash value with the current value;
1766 * call within hash_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 */
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001768static struct hda_amp_info *
1769update_amp_hash(struct hda_codec *codec, hda_nid_t nid, int ch,
Takashi Iwai280e57d2012-12-14 10:32:21 +01001770 int direction, int index, bool init_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001772 struct hda_amp_info *info;
1773 unsigned int parm, val = 0;
1774 bool val_read = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001776 retry:
1777 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1778 if (!info)
1779 return NULL;
1780 if (!(info->head.val & INFO_AMP_VOL(ch))) {
1781 if (!val_read) {
1782 mutex_unlock(&codec->hash_mutex);
1783 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1784 parm |= direction == HDA_OUTPUT ?
1785 AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1786 parm |= index;
1787 val = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001788 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001789 val &= 0xff;
1790 val_read = true;
1791 mutex_lock(&codec->hash_mutex);
1792 goto retry;
1793 }
1794 info->vol[ch] = val;
1795 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001796 } else if (init_only)
1797 return NULL;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001798 return info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799}
1800
1801/*
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001802 * write the current volume in info to the h/w
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001804static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001805 hda_nid_t nid, int ch, int direction, int index,
1806 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807{
1808 u32 parm;
1809
1810 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1811 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1812 parm |= index << AC_AMP_SET_INDEX_SHIFT;
Takashi Iwai38681372012-02-27 15:00:58 +01001813 if ((val & HDA_AMP_MUTE) && !(info->amp_caps & AC_AMPCAP_MUTE) &&
1814 (info->amp_caps & AC_AMPCAP_MIN_MUTE))
1815 ; /* set the zero value as a fake mute */
1816 else
1817 parm |= val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
1819}
1820
Takashi Iwaid5191e52009-11-16 14:58:17 +01001821/**
1822 * snd_hda_codec_amp_read - Read AMP value
1823 * @codec: HD-audio codec
1824 * @nid: NID to read the AMP value
1825 * @ch: channel (left=0 or right=1)
1826 * @direction: #HDA_INPUT or #HDA_OUTPUT
1827 * @index: the index value (only for input direction)
1828 *
1829 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 */
Takashi Iwai834be882006-03-01 14:16:17 +01001831int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1832 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001834 struct hda_amp_info *info;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001835 unsigned int val = 0;
1836
1837 mutex_lock(&codec->hash_mutex);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001838 info = update_amp_hash(codec, nid, ch, direction, index, false);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001839 if (info)
1840 val = info->vol[ch];
1841 mutex_unlock(&codec->hash_mutex);
1842 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001844EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845
Takashi Iwai280e57d2012-12-14 10:32:21 +01001846static int codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1847 int direction, int idx, int mask, int val,
1848 bool init_only)
1849{
1850 struct hda_amp_info *info;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01001851 unsigned int cache_only;
Takashi Iwai280e57d2012-12-14 10:32:21 +01001852
1853 if (snd_BUG_ON(mask & ~0xff))
1854 mask &= 0xff;
1855 val &= mask;
1856
1857 mutex_lock(&codec->hash_mutex);
1858 info = update_amp_hash(codec, nid, ch, direction, idx, init_only);
1859 if (!info) {
1860 mutex_unlock(&codec->hash_mutex);
1861 return 0;
1862 }
1863 val |= info->vol[ch] & ~mask;
1864 if (info->vol[ch] == val) {
1865 mutex_unlock(&codec->hash_mutex);
1866 return 0;
1867 }
1868 info->vol[ch] = val;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01001869 cache_only = info->head.dirty = codec->cached_write;
Takashi Iwai280e57d2012-12-14 10:32:21 +01001870 mutex_unlock(&codec->hash_mutex);
Takashi Iwaide1e37b2012-12-20 11:00:21 +01001871 if (!cache_only)
Takashi Iwai280e57d2012-12-14 10:32:21 +01001872 put_vol_mute(codec, info, nid, ch, direction, idx, val);
1873 return 1;
1874}
1875
Takashi Iwaid5191e52009-11-16 14:58:17 +01001876/**
1877 * snd_hda_codec_amp_update - update the AMP value
1878 * @codec: HD-audio codec
1879 * @nid: NID to read the AMP value
1880 * @ch: channel (left=0 or right=1)
1881 * @direction: #HDA_INPUT or #HDA_OUTPUT
1882 * @idx: the index value (only for input direction)
1883 * @mask: bit mask to set
1884 * @val: the bits value to set
1885 *
1886 * Update the AMP value with a bit mask.
1887 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001888 */
Takashi Iwai834be882006-03-01 14:16:17 +01001889int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1890 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891{
Takashi Iwai280e57d2012-12-14 10:32:21 +01001892 return codec_amp_update(codec, nid, ch, direction, idx, mask, val, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001894EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895
Takashi Iwaid5191e52009-11-16 14:58:17 +01001896/**
1897 * snd_hda_codec_amp_stereo - update the AMP stereo values
1898 * @codec: HD-audio codec
1899 * @nid: NID to read the AMP value
1900 * @direction: #HDA_INPUT or #HDA_OUTPUT
1901 * @idx: the index value (only for input direction)
1902 * @mask: bit mask to set
1903 * @val: the bits value to set
1904 *
1905 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1906 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02001907 */
1908int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1909 int direction, int idx, int mask, int val)
1910{
1911 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001912
1913 if (snd_BUG_ON(mask & ~0xff))
1914 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02001915 for (ch = 0; ch < 2; ch++)
1916 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1917 idx, mask, val);
1918 return ret;
1919}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001920EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001921
Takashi Iwai280e57d2012-12-14 10:32:21 +01001922/* Works like snd_hda_codec_amp_update() but it writes the value only at
1923 * the first access. If the amp was already initialized / updated beforehand,
1924 * this does nothing.
1925 */
1926int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
1927 int dir, int idx, int mask, int val)
1928{
1929 return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true);
1930}
1931EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init);
1932
1933int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
1934 int dir, int idx, int mask, int val)
1935{
1936 int ch, ret = 0;
1937
1938 if (snd_BUG_ON(mask & ~0xff))
1939 mask &= 0xff;
1940 for (ch = 0; ch < 2; ch++)
1941 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
1942 idx, mask, val);
1943 return ret;
1944}
1945EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init_stereo);
1946
Takashi Iwaid5191e52009-11-16 14:58:17 +01001947/**
1948 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
1949 * @codec: HD-audio codec
1950 *
1951 * Resume the all amp commands from the cache.
1952 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001953void snd_hda_codec_resume_amp(struct hda_codec *codec)
1954{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001955 int i;
1956
Takashi Iwaic370dd62012-12-13 18:30:04 +01001957 mutex_lock(&codec->hash_mutex);
1958 for (i = 0; i < codec->amp_cache.buf.used; i++) {
1959 struct hda_amp_info *buffer;
1960 u32 key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001961 hda_nid_t nid;
1962 unsigned int idx, dir, ch;
Takashi Iwaic370dd62012-12-13 18:30:04 +01001963
1964 buffer = snd_array_elem(&codec->amp_cache.buf, i);
1965 key = buffer->head.key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001966 if (!key)
1967 continue;
1968 nid = key & 0xff;
1969 idx = (key >> 16) & 0xff;
1970 dir = (key >> 24) & 0xff;
1971 for (ch = 0; ch < 2; ch++) {
1972 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
1973 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01001974 if (!buffer->head.dirty)
1975 continue;
1976 buffer->head.dirty = 0;
1977 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001978 put_vol_mute(codec, buffer, nid, ch, dir, idx,
1979 buffer->vol[ch]);
Takashi Iwaic370dd62012-12-13 18:30:04 +01001980 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001981 }
1982 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01001983 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001984}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001985EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001987static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1988 unsigned int ofs)
1989{
1990 u32 caps = query_amp_caps(codec, nid, dir);
1991 /* get num steps */
1992 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1993 if (ofs < caps)
1994 caps -= ofs;
1995 return caps;
1996}
1997
Takashi Iwaid5191e52009-11-16 14:58:17 +01001998/**
1999 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
2000 *
2001 * The control element is supposed to have the private_value field
2002 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2003 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002004int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
2005 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006{
2007 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2008 u16 nid = get_amp_nid(kcontrol);
2009 u8 chs = get_amp_channels(kcontrol);
2010 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002011 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002013 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2014 uinfo->count = chs == 3 ? 2 : 1;
2015 uinfo->value.integer.min = 0;
2016 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
2017 if (!uinfo->value.integer.max) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002018 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01002019 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
2020 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 return -EINVAL;
2022 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 return 0;
2024}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002025EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002027
2028static inline unsigned int
2029read_amp_value(struct hda_codec *codec, hda_nid_t nid,
2030 int ch, int dir, int idx, unsigned int ofs)
2031{
2032 unsigned int val;
2033 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
2034 val &= HDA_AMP_VOLMASK;
2035 if (val >= ofs)
2036 val -= ofs;
2037 else
2038 val = 0;
2039 return val;
2040}
2041
2042static inline int
2043update_amp_value(struct hda_codec *codec, hda_nid_t nid,
2044 int ch, int dir, int idx, unsigned int ofs,
2045 unsigned int val)
2046{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002047 unsigned int maxval;
2048
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002049 if (val > 0)
2050 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02002051 /* ofs = 0: raw max value */
2052 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002053 if (val > maxval)
2054 val = maxval;
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002055 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
2056 HDA_AMP_VOLMASK, val);
2057}
2058
Takashi Iwaid5191e52009-11-16 14:58:17 +01002059/**
2060 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
2061 *
2062 * The control element is supposed to have the private_value field
2063 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2064 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002065int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
2066 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067{
2068 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2069 hda_nid_t nid = get_amp_nid(kcontrol);
2070 int chs = get_amp_channels(kcontrol);
2071 int dir = get_amp_direction(kcontrol);
2072 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002073 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 long *valp = ucontrol->value.integer.value;
2075
2076 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002077 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002079 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 return 0;
2081}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002082EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
Takashi Iwaid5191e52009-11-16 14:58:17 +01002084/**
2085 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
2086 *
2087 * The control element is supposed to have the private_value field
2088 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2089 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002090int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
2091 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092{
2093 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2094 hda_nid_t nid = get_amp_nid(kcontrol);
2095 int chs = get_amp_channels(kcontrol);
2096 int dir = get_amp_direction(kcontrol);
2097 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002098 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 long *valp = ucontrol->value.integer.value;
2100 int change = 0;
2101
Takashi Iwaicb53c622007-08-10 17:21:45 +02002102 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002103 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002104 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002105 valp++;
2106 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002107 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002108 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002109 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 return change;
2111}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002112EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113
Takashi Iwaid5191e52009-11-16 14:58:17 +01002114/**
2115 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2116 *
2117 * The control element is supposed to have the private_value field
2118 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2119 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002120int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2121 unsigned int size, unsigned int __user *_tlv)
2122{
2123 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2124 hda_nid_t nid = get_amp_nid(kcontrol);
2125 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002126 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02002127 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002128 u32 caps, val1, val2;
2129
2130 if (size < 4 * sizeof(unsigned int))
2131 return -ENOMEM;
2132 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002133 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2134 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002135 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002136 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002137 val1 = ((int)val1) * ((int)val2);
Takashi Iwai38681372012-02-27 15:00:58 +01002138 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwaic08d9162010-10-17 10:40:53 +02002139 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002140 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2141 return -EFAULT;
2142 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2143 return -EFAULT;
2144 if (put_user(val1, _tlv + 2))
2145 return -EFAULT;
2146 if (put_user(val2, _tlv + 3))
2147 return -EFAULT;
2148 return 0;
2149}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002150EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002151
Takashi Iwaid5191e52009-11-16 14:58:17 +01002152/**
2153 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2154 * @codec: HD-audio codec
2155 * @nid: NID of a reference widget
2156 * @dir: #HDA_INPUT or #HDA_OUTPUT
2157 * @tlv: TLV data to be stored, at least 4 elements
2158 *
2159 * Set (static) TLV data for a virtual master volume using the AMP caps
2160 * obtained from the reference NID.
2161 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01002162 */
2163void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2164 unsigned int *tlv)
2165{
2166 u32 caps;
2167 int nums, step;
2168
2169 caps = query_amp_caps(codec, nid, dir);
2170 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2171 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2172 step = (step + 1) * 25;
2173 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2174 tlv[1] = 2 * sizeof(unsigned int);
2175 tlv[2] = -nums * step;
2176 tlv[3] = step;
2177}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002178EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002179
2180/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01002181static struct snd_kcontrol *
Takashi Iwaidcda5802012-10-12 17:24:51 +02002182find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002183{
2184 struct snd_ctl_elem_id id;
2185 memset(&id, 0, sizeof(id));
2186 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwaidcda5802012-10-12 17:24:51 +02002187 id.device = dev;
Takashi Iwai09f99702008-02-04 12:31:13 +01002188 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02002189 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2190 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002191 strcpy(id.name, name);
2192 return snd_ctl_find_id(codec->bus->card, &id);
2193}
2194
Takashi Iwaid5191e52009-11-16 14:58:17 +01002195/**
2196 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2197 * @codec: HD-audio codec
2198 * @name: ctl id name string
2199 *
2200 * Get the control element with the given id string and IFACE_MIXER.
2201 */
Takashi Iwai09f99702008-02-04 12:31:13 +01002202struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2203 const char *name)
2204{
Takashi Iwaidcda5802012-10-12 17:24:51 +02002205 return find_mixer_ctl(codec, name, 0, 0);
Takashi Iwai09f99702008-02-04 12:31:13 +01002206}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002207EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01002208
Takashi Iwaidcda5802012-10-12 17:24:51 +02002209static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
2210 int dev)
Takashi Iwai1afe2062010-12-23 10:17:52 +01002211{
2212 int idx;
2213 for (idx = 0; idx < 16; idx++) { /* 16 ctlrs should be large enough */
Takashi Iwaidcda5802012-10-12 17:24:51 +02002214 if (!find_mixer_ctl(codec, name, dev, idx))
Takashi Iwai1afe2062010-12-23 10:17:52 +01002215 return idx;
2216 }
2217 return -EBUSY;
2218}
2219
Takashi Iwaid5191e52009-11-16 14:58:17 +01002220/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002221 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01002222 * @codec: HD-audio codec
2223 * @nid: corresponding NID (optional)
2224 * @kctl: the control element to assign
2225 *
2226 * Add the given control element to an array inside the codec instance.
2227 * All control elements belonging to a codec are supposed to be added
2228 * by this function so that a proper clean-up works at the free or
2229 * reconfiguration time.
2230 *
2231 * If non-zero @nid is passed, the NID is assigned to the control element.
2232 * The assignment is shown in the codec proc file.
2233 *
2234 * snd_hda_ctl_add() checks the control subdev id field whether
2235 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002236 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2237 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01002238 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002239int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2240 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002241{
2242 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002243 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002244 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002245
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002246 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002247 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002248 if (nid == 0)
2249 nid = get_amp_nid_(kctl->private_value);
2250 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002251 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2252 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002253 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01002254 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002255 err = snd_ctl_add(codec->bus->card, kctl);
2256 if (err < 0)
2257 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002258 item = snd_array_new(&codec->mixers);
2259 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002260 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002261 item->kctl = kctl;
2262 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002263 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002264 return 0;
2265}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002266EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002267
Takashi Iwaid5191e52009-11-16 14:58:17 +01002268/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002269 * snd_hda_add_nid - Assign a NID to a control element
2270 * @codec: HD-audio codec
2271 * @nid: corresponding NID (optional)
2272 * @kctl: the control element to assign
2273 * @index: index to kctl
2274 *
2275 * Add the given control element to an array inside the codec instance.
2276 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2277 * NID:KCTL mapping - for example "Capture Source" selector.
2278 */
2279int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2280 unsigned int index, hda_nid_t nid)
2281{
2282 struct hda_nid_item *item;
2283
2284 if (nid > 0) {
2285 item = snd_array_new(&codec->nids);
2286 if (!item)
2287 return -ENOMEM;
2288 item->kctl = kctl;
2289 item->index = index;
2290 item->nid = nid;
2291 return 0;
2292 }
Takashi Iwai28d1a852010-03-15 09:05:46 +01002293 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
2294 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002295 return -EINVAL;
2296}
2297EXPORT_SYMBOL_HDA(snd_hda_add_nid);
2298
2299/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002300 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2301 * @codec: HD-audio codec
2302 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002303void snd_hda_ctls_clear(struct hda_codec *codec)
2304{
2305 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002306 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002307 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002308 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002309 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002310 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002311}
2312
Takashi Iwaia65d6292009-02-23 16:57:04 +01002313/* pseudo device locking
2314 * toggle card->shutdown to allow/disallow the device access (as a hack)
2315 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002316int snd_hda_lock_devices(struct hda_bus *bus)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002317{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002318 struct snd_card *card = bus->card;
2319 struct hda_codec *codec;
2320
Takashi Iwaia65d6292009-02-23 16:57:04 +01002321 spin_lock(&card->files_lock);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002322 if (card->shutdown)
2323 goto err_unlock;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002324 card->shutdown = 1;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002325 if (!list_empty(&card->ctl_files))
2326 goto err_clear;
2327
2328 list_for_each_entry(codec, &bus->codec_list, list) {
2329 int pcm;
2330 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2331 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2332 if (!cpcm->pcm)
2333 continue;
2334 if (cpcm->pcm->streams[0].substream_opened ||
2335 cpcm->pcm->streams[1].substream_opened)
2336 goto err_clear;
2337 }
2338 }
Takashi Iwaia65d6292009-02-23 16:57:04 +01002339 spin_unlock(&card->files_lock);
2340 return 0;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002341
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002342 err_clear:
2343 card->shutdown = 0;
2344 err_unlock:
2345 spin_unlock(&card->files_lock);
2346 return -EINVAL;
2347}
2348EXPORT_SYMBOL_HDA(snd_hda_lock_devices);
2349
2350void snd_hda_unlock_devices(struct hda_bus *bus)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002351{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002352 struct snd_card *card = bus->card;
2353
2354 card = bus->card;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002355 spin_lock(&card->files_lock);
2356 card->shutdown = 0;
2357 spin_unlock(&card->files_lock);
2358}
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002359EXPORT_SYMBOL_HDA(snd_hda_unlock_devices);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002360
Takashi Iwaid5191e52009-11-16 14:58:17 +01002361/**
2362 * snd_hda_codec_reset - Clear all objects assigned to the codec
2363 * @codec: HD-audio codec
2364 *
2365 * This frees the all PCM and control elements assigned to the codec, and
2366 * clears the caches and restores the pin default configurations.
2367 *
2368 * When a device is being used, it returns -EBSY. If successfully freed,
2369 * returns zero.
2370 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002371int snd_hda_codec_reset(struct hda_codec *codec)
2372{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002373 struct hda_bus *bus = codec->bus;
2374 struct snd_card *card = bus->card;
2375 int i;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002376
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002377 if (snd_hda_lock_devices(bus) < 0)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002378 return -EBUSY;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002379
2380 /* OK, let it free */
David Henningsson26a6cb62012-10-09 15:04:21 +02002381 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai83012a72012-08-24 18:38:08 +02002382#ifdef CONFIG_PM
Takashi Iwaia2d96e72012-05-09 12:36:22 +02002383 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwai339876d2012-05-08 16:57:12 +02002384 codec->power_on = 0;
2385 codec->power_transition = 0;
2386 codec->power_jiffies = jiffies;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002387 flush_workqueue(bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002388#endif
2389 snd_hda_ctls_clear(codec);
2390 /* relase PCMs */
2391 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002392 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01002393 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002394 clear_bit(codec->pcm_info[i].device,
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002395 bus->pcm_dev_bits);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002396 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002397 }
2398 if (codec->patch_ops.free)
2399 codec->patch_ops.free(codec);
Takashi Iwai07dc59f2012-09-10 09:39:31 +02002400 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
Takashi Iwai1835a0f2011-10-27 22:12:46 +02002401 snd_hda_jack_tbl_clear(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01002402 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002403 codec->spec = NULL;
2404 free_hda_cache(&codec->amp_cache);
2405 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01002406 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2407 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01002408 /* free only driver_pins so that init_pins + user_pins are restored */
2409 snd_array_free(&codec->driver_pins);
Takashi Iwai09a60712012-06-26 15:01:33 +02002410 snd_array_free(&codec->cvt_setups);
2411 snd_array_free(&codec->spdif_out);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01002412 snd_array_free(&codec->verbs);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002413 codec->num_pcms = 0;
2414 codec->pcm_info = NULL;
2415 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01002416 codec->slave_dig_outs = NULL;
2417 codec->spdif_status_reset = 0;
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002418 module_put(codec->owner);
2419 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002420
2421 /* allow device access again */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002422 snd_hda_unlock_devices(bus);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002423 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002424}
2425
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002426typedef int (*map_slave_func_t)(void *, struct snd_kcontrol *);
2427
2428/* apply the function to all matching slave ctls in the mixer list */
2429static int map_slaves(struct hda_codec *codec, const char * const *slaves,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002430 const char *suffix, map_slave_func_t func, void *data)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002431{
2432 struct hda_nid_item *items;
2433 const char * const *s;
2434 int i, err;
2435
2436 items = codec->mixers.list;
2437 for (i = 0; i < codec->mixers.used; i++) {
2438 struct snd_kcontrol *sctl = items[i].kctl;
2439 if (!sctl || !sctl->id.name ||
2440 sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
2441 continue;
2442 for (s = slaves; *s; s++) {
Takashi Iwai9322ca52012-02-03 14:28:01 +01002443 char tmpname[sizeof(sctl->id.name)];
2444 const char *name = *s;
2445 if (suffix) {
2446 snprintf(tmpname, sizeof(tmpname), "%s %s",
2447 name, suffix);
2448 name = tmpname;
2449 }
Takashi Iwai9322ca52012-02-03 14:28:01 +01002450 if (!strcmp(sctl->id.name, name)) {
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002451 err = func(data, sctl);
2452 if (err)
2453 return err;
2454 break;
2455 }
2456 }
2457 }
2458 return 0;
2459}
2460
2461static int check_slave_present(void *data, struct snd_kcontrol *sctl)
2462{
2463 return 1;
2464}
2465
Takashi Iwai18478e82012-03-09 17:51:10 +01002466/* guess the value corresponding to 0dB */
2467static int get_kctl_0dB_offset(struct snd_kcontrol *kctl)
2468{
2469 int _tlv[4];
2470 const int *tlv = NULL;
2471 int val = -1;
2472
2473 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2474 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2475 mm_segment_t fs = get_fs();
2476 set_fs(get_ds());
2477 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2478 tlv = _tlv;
2479 set_fs(fs);
2480 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2481 tlv = kctl->tlv.p;
2482 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE)
2483 val = -tlv[2] / tlv[3];
2484 return val;
2485}
2486
2487/* call kctl->put with the given value(s) */
2488static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2489{
2490 struct snd_ctl_elem_value *ucontrol;
2491 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2492 if (!ucontrol)
2493 return -ENOMEM;
2494 ucontrol->value.integer.value[0] = val;
2495 ucontrol->value.integer.value[1] = val;
2496 kctl->put(kctl, ucontrol);
2497 kfree(ucontrol);
2498 return 0;
2499}
2500
2501/* initialize the slave volume with 0dB */
2502static int init_slave_0dB(void *data, struct snd_kcontrol *slave)
2503{
2504 int offset = get_kctl_0dB_offset(slave);
2505 if (offset > 0)
2506 put_kctl_with_value(slave, offset);
2507 return 0;
2508}
2509
2510/* unmute the slave */
2511static int init_slave_unmute(void *data, struct snd_kcontrol *slave)
2512{
2513 return put_kctl_with_value(slave, 1);
2514}
2515
Takashi Iwaid5191e52009-11-16 14:58:17 +01002516/**
2517 * snd_hda_add_vmaster - create a virtual master control and add slaves
2518 * @codec: HD-audio codec
2519 * @name: vmaster control name
2520 * @tlv: TLV data (optional)
2521 * @slaves: slave control names (optional)
Takashi Iwai9322ca52012-02-03 14:28:01 +01002522 * @suffix: suffix string to each slave name (optional)
Takashi Iwai18478e82012-03-09 17:51:10 +01002523 * @init_slave_vol: initialize slaves to unmute/0dB
Takashi Iwai29e58532012-03-12 12:25:03 +01002524 * @ctl_ret: store the vmaster kcontrol in return
Takashi Iwaid5191e52009-11-16 14:58:17 +01002525 *
2526 * Create a virtual master control with the given name. The TLV data
2527 * must be either NULL or a valid data.
2528 *
2529 * @slaves is a NULL-terminated array of strings, each of which is a
2530 * slave control name. All controls with these names are assigned to
2531 * the new virtual master control.
2532 *
2533 * This function returns zero if successful or a negative error code.
2534 */
Takashi Iwai18478e82012-03-09 17:51:10 +01002535int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002536 unsigned int *tlv, const char * const *slaves,
Takashi Iwai29e58532012-03-12 12:25:03 +01002537 const char *suffix, bool init_slave_vol,
2538 struct snd_kcontrol **ctl_ret)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002539{
2540 struct snd_kcontrol *kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002541 int err;
2542
Takashi Iwai29e58532012-03-12 12:25:03 +01002543 if (ctl_ret)
2544 *ctl_ret = NULL;
2545
Takashi Iwai9322ca52012-02-03 14:28:01 +01002546 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002547 if (err != 1) {
Takashi Iwai2f085542008-02-22 18:43:50 +01002548 snd_printdd("No slave found for %s\n", name);
2549 return 0;
2550 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002551 kctl = snd_ctl_make_virtual_master(name, tlv);
2552 if (!kctl)
2553 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002554 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002555 if (err < 0)
2556 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002557
Takashi Iwai9322ca52012-02-03 14:28:01 +01002558 err = map_slaves(codec, slaves, suffix,
2559 (map_slave_func_t)snd_ctl_add_slave, kctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002560 if (err < 0)
2561 return err;
Takashi Iwai18478e82012-03-09 17:51:10 +01002562
2563 /* init with master mute & zero volume */
2564 put_kctl_with_value(kctl, 0);
2565 if (init_slave_vol)
2566 map_slaves(codec, slaves, suffix,
2567 tlv ? init_slave_0dB : init_slave_unmute, kctl);
2568
Takashi Iwai29e58532012-03-12 12:25:03 +01002569 if (ctl_ret)
2570 *ctl_ret = kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002571 return 0;
2572}
Takashi Iwai18478e82012-03-09 17:51:10 +01002573EXPORT_SYMBOL_HDA(__snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002574
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002575/*
2576 * mute-LED control using vmaster
2577 */
2578static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2579 struct snd_ctl_elem_info *uinfo)
2580{
2581 static const char * const texts[] = {
David Henningssonc86c2d42013-01-03 14:12:29 +01002582 "On", "Off", "Follow Master"
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002583 };
2584 unsigned int index;
2585
2586 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2587 uinfo->count = 1;
2588 uinfo->value.enumerated.items = 3;
2589 index = uinfo->value.enumerated.item;
2590 if (index >= 3)
2591 index = 2;
2592 strcpy(uinfo->value.enumerated.name, texts[index]);
2593 return 0;
2594}
2595
2596static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2597 struct snd_ctl_elem_value *ucontrol)
2598{
2599 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2600 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2601 return 0;
2602}
2603
2604static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2605 struct snd_ctl_elem_value *ucontrol)
2606{
2607 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2608 unsigned int old_mode = hook->mute_mode;
2609
2610 hook->mute_mode = ucontrol->value.enumerated.item[0];
2611 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2612 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2613 if (old_mode == hook->mute_mode)
2614 return 0;
2615 snd_hda_sync_vmaster_hook(hook);
2616 return 1;
2617}
2618
2619static struct snd_kcontrol_new vmaster_mute_mode = {
2620 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2621 .name = "Mute-LED Mode",
2622 .info = vmaster_mute_mode_info,
2623 .get = vmaster_mute_mode_get,
2624 .put = vmaster_mute_mode_put,
2625};
2626
2627/*
2628 * Add a mute-LED hook with the given vmaster switch kctl
2629 * "Mute-LED Mode" control is automatically created and associated with
2630 * the given hook.
2631 */
2632int snd_hda_add_vmaster_hook(struct hda_codec *codec,
Takashi Iwaif29735c2012-03-13 07:55:10 +01002633 struct hda_vmaster_mute_hook *hook,
2634 bool expose_enum_ctl)
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002635{
2636 struct snd_kcontrol *kctl;
2637
2638 if (!hook->hook || !hook->sw_kctl)
2639 return 0;
2640 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
2641 hook->codec = codec;
2642 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
Takashi Iwaif29735c2012-03-13 07:55:10 +01002643 if (!expose_enum_ctl)
2644 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002645 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2646 if (!kctl)
2647 return -ENOMEM;
2648 return snd_hda_ctl_add(codec, 0, kctl);
2649}
2650EXPORT_SYMBOL_HDA(snd_hda_add_vmaster_hook);
2651
2652/*
2653 * Call the hook with the current value for synchronization
2654 * Should be called in init callback
2655 */
2656void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2657{
2658 if (!hook->hook || !hook->codec)
2659 return;
2660 switch (hook->mute_mode) {
2661 case HDA_VMUTE_FOLLOW_MASTER:
2662 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2663 break;
2664 default:
2665 hook->hook(hook->codec, hook->mute_mode);
2666 break;
2667 }
2668}
2669EXPORT_SYMBOL_HDA(snd_hda_sync_vmaster_hook);
2670
2671
Takashi Iwaid5191e52009-11-16 14:58:17 +01002672/**
2673 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2674 *
2675 * The control element is supposed to have the private_value field
2676 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2677 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002678int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2679 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680{
2681 int chs = get_amp_channels(kcontrol);
2682
2683 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2684 uinfo->count = chs == 3 ? 2 : 1;
2685 uinfo->value.integer.min = 0;
2686 uinfo->value.integer.max = 1;
2687 return 0;
2688}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002689EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690
Takashi Iwaid5191e52009-11-16 14:58:17 +01002691/**
2692 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2693 *
2694 * The control element is supposed to have the private_value field
2695 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2696 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002697int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2698 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699{
2700 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2701 hda_nid_t nid = get_amp_nid(kcontrol);
2702 int chs = get_amp_channels(kcontrol);
2703 int dir = get_amp_direction(kcontrol);
2704 int idx = get_amp_index(kcontrol);
2705 long *valp = ucontrol->value.integer.value;
2706
2707 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002708 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002709 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002711 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002712 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 return 0;
2714}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002715EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716
Takashi Iwaid5191e52009-11-16 14:58:17 +01002717/**
2718 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2719 *
2720 * The control element is supposed to have the private_value field
2721 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2722 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002723int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2724 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725{
2726 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2727 hda_nid_t nid = get_amp_nid(kcontrol);
2728 int chs = get_amp_channels(kcontrol);
2729 int dir = get_amp_direction(kcontrol);
2730 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 long *valp = ucontrol->value.integer.value;
2732 int change = 0;
2733
Takashi Iwaicb53c622007-08-10 17:21:45 +02002734 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002735 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002736 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002737 HDA_AMP_MUTE,
2738 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002739 valp++;
2740 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002741 if (chs & 2)
2742 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002743 HDA_AMP_MUTE,
2744 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002745 hda_call_check_power_status(codec, nid);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002746 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 return change;
2748}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002749EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750
2751/*
Takashi Iwai985be542005-11-02 18:26:49 +01002752 * bound volume controls
2753 *
2754 * bind multiple volumes (# indices, from 0)
2755 */
2756
2757#define AMP_VAL_IDX_SHIFT 19
2758#define AMP_VAL_IDX_MASK (0x0f<<19)
2759
Takashi Iwaid5191e52009-11-16 14:58:17 +01002760/**
2761 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2762 *
2763 * The control element is supposed to have the private_value field
2764 * set up via HDA_BIND_MUTE*() macros.
2765 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002766int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2767 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002768{
2769 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2770 unsigned long pval;
2771 int err;
2772
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002773 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002774 pval = kcontrol->private_value;
2775 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2776 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2777 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002778 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002779 return err;
2780}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002781EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002782
Takashi Iwaid5191e52009-11-16 14:58:17 +01002783/**
2784 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2785 *
2786 * The control element is supposed to have the private_value field
2787 * set up via HDA_BIND_MUTE*() macros.
2788 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002789int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2790 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002791{
2792 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2793 unsigned long pval;
2794 int i, indices, err = 0, change = 0;
2795
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002796 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002797 pval = kcontrol->private_value;
2798 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2799 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002800 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2801 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002802 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2803 if (err < 0)
2804 break;
2805 change |= err;
2806 }
2807 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002808 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002809 return err < 0 ? err : change;
2810}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002811EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002812
Takashi Iwaid5191e52009-11-16 14:58:17 +01002813/**
2814 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2815 *
2816 * The control element is supposed to have the private_value field
2817 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002818 */
2819int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2820 struct snd_ctl_elem_info *uinfo)
2821{
2822 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2823 struct hda_bind_ctls *c;
2824 int err;
2825
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002826 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002827 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002828 kcontrol->private_value = *c->values;
2829 err = c->ops->info(kcontrol, uinfo);
2830 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002831 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002832 return err;
2833}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002834EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002835
Takashi Iwaid5191e52009-11-16 14:58:17 +01002836/**
2837 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2838 *
2839 * The control element is supposed to have the private_value field
2840 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2841 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002842int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2843 struct snd_ctl_elem_value *ucontrol)
2844{
2845 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2846 struct hda_bind_ctls *c;
2847 int err;
2848
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002849 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002850 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002851 kcontrol->private_value = *c->values;
2852 err = c->ops->get(kcontrol, ucontrol);
2853 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002854 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002855 return err;
2856}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002857EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002858
Takashi Iwaid5191e52009-11-16 14:58:17 +01002859/**
2860 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2861 *
2862 * The control element is supposed to have the private_value field
2863 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2864 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002865int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2866 struct snd_ctl_elem_value *ucontrol)
2867{
2868 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2869 struct hda_bind_ctls *c;
2870 unsigned long *vals;
2871 int err = 0, change = 0;
2872
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002873 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002874 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002875 for (vals = c->values; *vals; vals++) {
2876 kcontrol->private_value = *vals;
2877 err = c->ops->put(kcontrol, ucontrol);
2878 if (err < 0)
2879 break;
2880 change |= err;
2881 }
2882 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002883 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002884 return err < 0 ? err : change;
2885}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002886EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02002887
Takashi Iwaid5191e52009-11-16 14:58:17 +01002888/**
2889 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2890 *
2891 * The control element is supposed to have the private_value field
2892 * set up via HDA_BIND_VOL() macro.
2893 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002894int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2895 unsigned int size, unsigned int __user *tlv)
2896{
2897 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2898 struct hda_bind_ctls *c;
2899 int err;
2900
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002901 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002902 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002903 kcontrol->private_value = *c->values;
2904 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2905 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002906 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002907 return err;
2908}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002909EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02002910
2911struct hda_ctl_ops snd_hda_bind_vol = {
2912 .info = snd_hda_mixer_amp_volume_info,
2913 .get = snd_hda_mixer_amp_volume_get,
2914 .put = snd_hda_mixer_amp_volume_put,
2915 .tlv = snd_hda_mixer_amp_tlv
2916};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002917EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02002918
2919struct hda_ctl_ops snd_hda_bind_sw = {
2920 .info = snd_hda_mixer_amp_switch_info,
2921 .get = snd_hda_mixer_amp_switch_get,
2922 .put = snd_hda_mixer_amp_switch_put,
2923 .tlv = snd_hda_mixer_amp_tlv
2924};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002925EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02002926
2927/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 * SPDIF out controls
2929 */
2930
Takashi Iwai0ba21762007-04-16 11:29:14 +02002931static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2932 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933{
2934 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2935 uinfo->count = 1;
2936 return 0;
2937}
2938
Takashi Iwai0ba21762007-04-16 11:29:14 +02002939static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2940 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941{
2942 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2943 IEC958_AES0_NONAUDIO |
2944 IEC958_AES0_CON_EMPHASIS_5015 |
2945 IEC958_AES0_CON_NOT_COPYRIGHT;
2946 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2947 IEC958_AES1_CON_ORIGINAL;
2948 return 0;
2949}
2950
Takashi Iwai0ba21762007-04-16 11:29:14 +02002951static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2952 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953{
2954 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2955 IEC958_AES0_NONAUDIO |
2956 IEC958_AES0_PRO_EMPHASIS_5015;
2957 return 0;
2958}
2959
Takashi Iwai0ba21762007-04-16 11:29:14 +02002960static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2961 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962{
2963 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002964 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002965 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002967 mutex_lock(&codec->spdif_mutex);
2968 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c9359762011-06-01 11:14:17 -06002969 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
2970 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
2971 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
2972 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002973 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974
2975 return 0;
2976}
2977
2978/* convert from SPDIF status bits to HDA SPDIF bits
2979 * bit 0 (DigEn) is always set zero (to be filled later)
2980 */
2981static unsigned short convert_from_spdif_status(unsigned int sbits)
2982{
2983 unsigned short val = 0;
2984
2985 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002986 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002988 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002990 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2991 IEC958_AES0_PRO_EMPHASIS_5015)
2992 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002994 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2995 IEC958_AES0_CON_EMPHASIS_5015)
2996 val |= AC_DIG1_EMPHASIS;
2997 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2998 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02003000 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
3002 }
3003 return val;
3004}
3005
3006/* convert to SPDIF status bits from HDA SPDIF bits
3007 */
3008static unsigned int convert_to_spdif_status(unsigned short val)
3009{
3010 unsigned int sbits = 0;
3011
Takashi Iwai0ba21762007-04-16 11:29:14 +02003012 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003014 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 sbits |= IEC958_AES0_PROFESSIONAL;
3016 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003017 if (sbits & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
3019 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003020 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003022 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003024 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
3026 sbits |= val & (0x7f << 8);
3027 }
3028 return sbits;
3029}
3030
Takashi Iwai2f728532008-09-25 16:32:41 +02003031/* set digital convert verbs both for the given NID and its slaves */
3032static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
3033 int verb, int val)
3034{
Takashi Iwaidda14412011-05-02 11:29:30 +02003035 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02003036
Takashi Iwai9e976972008-11-25 08:17:20 +01003037 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003038 d = codec->slave_dig_outs;
3039 if (!d)
3040 return;
3041 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01003042 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003043}
3044
3045static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
3046 int dig1, int dig2)
3047{
3048 if (dig1 != -1)
3049 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
3050 if (dig2 != -1)
3051 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
3052}
3053
Takashi Iwai0ba21762007-04-16 11:29:14 +02003054static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
3055 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056{
3057 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06003058 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003059 struct hda_spdif_out *spdif;
3060 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 unsigned short val;
3062 int change;
3063
Ingo Molnar62932df2006-01-16 16:34:20 +01003064 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003065 spdif = snd_array_elem(&codec->spdif_out, idx);
3066 nid = spdif->nid;
Stephen Warren7c9359762011-06-01 11:14:17 -06003067 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
3069 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
3070 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c9359762011-06-01 11:14:17 -06003071 val = convert_from_spdif_status(spdif->status);
3072 val |= spdif->ctls & 1;
3073 change = spdif->ctls != val;
3074 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003075 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02003076 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01003077 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 return change;
3079}
3080
Takashi Iwaia5ce8892007-07-23 15:42:26 +02003081#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082
Takashi Iwai0ba21762007-04-16 11:29:14 +02003083static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
3084 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085{
3086 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06003087 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003088 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003090 mutex_lock(&codec->spdif_mutex);
3091 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c9359762011-06-01 11:14:17 -06003092 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003093 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 return 0;
3095}
3096
Stephen Warren74b654c2011-06-01 11:14:18 -06003097static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
3098 int dig1, int dig2)
3099{
3100 set_dig_out_convert(codec, nid, dig1, dig2);
3101 /* unmute amp switch (if any) */
3102 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
3103 (dig1 & AC_DIG1_ENABLE))
3104 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
3105 HDA_AMP_MUTE, 0);
3106}
3107
Takashi Iwai0ba21762007-04-16 11:29:14 +02003108static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
3109 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110{
3111 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06003112 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003113 struct hda_spdif_out *spdif;
3114 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 unsigned short val;
3116 int change;
3117
Ingo Molnar62932df2006-01-16 16:34:20 +01003118 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003119 spdif = snd_array_elem(&codec->spdif_out, idx);
3120 nid = spdif->nid;
Stephen Warren7c9359762011-06-01 11:14:17 -06003121 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02003123 val |= AC_DIG1_ENABLE;
Stephen Warren7c9359762011-06-01 11:14:17 -06003124 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003125 spdif->ctls = val;
3126 if (change && nid != (u16)-1)
3127 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01003128 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 return change;
3130}
3131
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003132static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 {
3134 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3135 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003136 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 .info = snd_hda_spdif_mask_info,
3138 .get = snd_hda_spdif_cmask_get,
3139 },
3140 {
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, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 .info = snd_hda_spdif_mask_info,
3145 .get = snd_hda_spdif_pmask_get,
3146 },
3147 {
3148 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003149 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 .info = snd_hda_spdif_mask_info,
3151 .get = snd_hda_spdif_default_get,
3152 .put = snd_hda_spdif_default_put,
3153 },
3154 {
3155 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003156 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 .info = snd_hda_spdif_out_switch_info,
3158 .get = snd_hda_spdif_out_switch_get,
3159 .put = snd_hda_spdif_out_switch_put,
3160 },
3161 { } /* end */
3162};
3163
3164/**
Takashi Iwaidcda5802012-10-12 17:24:51 +02003165 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 * @codec: the HDA codec
Takashi Iwaidcda5802012-10-12 17:24:51 +02003167 * @associated_nid: NID that new ctls associated with
3168 * @cvt_nid: converter NID
3169 * @type: HDA_PCM_TYPE_*
3170 * Creates controls related with the digital output.
3171 * Called from each patch supporting the digital out.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 *
3173 * Returns 0 if successful, or a negative error code.
3174 */
Takashi Iwaidcda5802012-10-12 17:24:51 +02003175int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
3176 hda_nid_t associated_nid,
3177 hda_nid_t cvt_nid,
3178 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179{
3180 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003181 struct snd_kcontrol *kctl;
3182 struct snd_kcontrol_new *dig_mix;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003183 int idx, dev = 0;
3184 const int spdif_pcm_dev = 1;
Stephen Warren7c9359762011-06-01 11:14:17 -06003185 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186
Takashi Iwaidcda5802012-10-12 17:24:51 +02003187 if (codec->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
3188 type == HDA_PCM_TYPE_SPDIF) {
3189 dev = spdif_pcm_dev;
3190 } else if (codec->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
3191 type == HDA_PCM_TYPE_HDMI) {
3192 for (idx = 0; idx < codec->spdif_out.used; idx++) {
3193 spdif = snd_array_elem(&codec->spdif_out, idx);
3194 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3195 kctl = find_mixer_ctl(codec, dig_mix->name, 0, idx);
3196 if (!kctl)
3197 break;
3198 kctl->id.device = spdif_pcm_dev;
3199 }
3200 }
3201 codec->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
3202 }
3203 if (!codec->primary_dig_out_type)
3204 codec->primary_dig_out_type = type;
3205
3206 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", dev);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003207 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003208 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
3209 return -EBUSY;
3210 }
Stephen Warren7c9359762011-06-01 11:14:17 -06003211 spdif = snd_array_new(&codec->spdif_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3213 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01003214 if (!kctl)
3215 return -ENOMEM;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003216 kctl->id.device = dev;
Takashi Iwai09f99702008-02-04 12:31:13 +01003217 kctl->id.index = idx;
Stephen Warren7c9359762011-06-01 11:14:17 -06003218 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06003219 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003220 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 return err;
3222 }
Stephen Warren74b654c2011-06-01 11:14:18 -06003223 spdif->nid = cvt_nid;
3224 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
Stephen Warren7c9359762011-06-01 11:14:17 -06003225 AC_VERB_GET_DIGI_CONVERT_1, 0);
3226 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 return 0;
3228}
Takashi Iwaidcda5802012-10-12 17:24:51 +02003229EXPORT_SYMBOL_HDA(snd_hda_create_dig_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003231/* get the hda_spdif_out entry from the given NID
3232 * call within spdif_mutex lock
3233 */
Stephen Warren7c9359762011-06-01 11:14:17 -06003234struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3235 hda_nid_t nid)
3236{
3237 int i;
3238 for (i = 0; i < codec->spdif_out.used; i++) {
3239 struct hda_spdif_out *spdif =
3240 snd_array_elem(&codec->spdif_out, i);
3241 if (spdif->nid == nid)
3242 return spdif;
3243 }
3244 return NULL;
3245}
3246EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid);
3247
Stephen Warren74b654c2011-06-01 11:14:18 -06003248void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3249{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003250 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003251
3252 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003253 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003254 spdif->nid = (u16)-1;
3255 mutex_unlock(&codec->spdif_mutex);
3256}
3257EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign);
3258
3259void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3260{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003261 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003262 unsigned short val;
3263
3264 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003265 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003266 if (spdif->nid != nid) {
3267 spdif->nid = nid;
3268 val = spdif->ctls;
3269 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3270 }
3271 mutex_unlock(&codec->spdif_mutex);
3272}
3273EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign);
3274
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275/*
Takashi Iwai9a081602008-02-12 18:37:26 +01003276 * SPDIF sharing with analog output
3277 */
3278static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3279 struct snd_ctl_elem_value *ucontrol)
3280{
3281 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3282 ucontrol->value.integer.value[0] = mout->share_spdif;
3283 return 0;
3284}
3285
3286static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3287 struct snd_ctl_elem_value *ucontrol)
3288{
3289 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3290 mout->share_spdif = !!ucontrol->value.integer.value[0];
3291 return 0;
3292}
3293
3294static struct snd_kcontrol_new spdif_share_sw = {
3295 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3296 .name = "IEC958 Default PCM Playback Switch",
3297 .info = snd_ctl_boolean_mono_info,
3298 .get = spdif_share_sw_get,
3299 .put = spdif_share_sw_put,
3300};
3301
Takashi Iwaid5191e52009-11-16 14:58:17 +01003302/**
3303 * snd_hda_create_spdif_share_sw - create Default PCM switch
3304 * @codec: the HDA codec
3305 * @mout: multi-out instance
3306 */
Takashi Iwai9a081602008-02-12 18:37:26 +01003307int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3308 struct hda_multi_out *mout)
3309{
3310 if (!mout->dig_out_nid)
3311 return 0;
3312 /* ATTENTION: here mout is passed as private_data, instead of codec */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003313 return snd_hda_ctl_add(codec, mout->dig_out_nid,
3314 snd_ctl_new1(&spdif_share_sw, mout));
Takashi Iwai9a081602008-02-12 18:37:26 +01003315}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003316EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01003317
3318/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 * SPDIF input
3320 */
3321
3322#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3323
Takashi Iwai0ba21762007-04-16 11:29:14 +02003324static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3325 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326{
3327 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3328
3329 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3330 return 0;
3331}
3332
Takashi Iwai0ba21762007-04-16 11:29:14 +02003333static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3334 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335{
3336 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3337 hda_nid_t nid = kcontrol->private_value;
3338 unsigned int val = !!ucontrol->value.integer.value[0];
3339 int change;
3340
Ingo Molnar62932df2006-01-16 16:34:20 +01003341 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003343 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003345 snd_hda_codec_write_cache(codec, nid, 0,
3346 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003348 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 return change;
3350}
3351
Takashi Iwai0ba21762007-04-16 11:29:14 +02003352static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3353 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354{
3355 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3356 hda_nid_t nid = kcontrol->private_value;
3357 unsigned short val;
3358 unsigned int sbits;
3359
Andrew Paprocki3982d172007-12-19 12:13:44 +01003360 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 sbits = convert_to_spdif_status(val);
3362 ucontrol->value.iec958.status[0] = sbits;
3363 ucontrol->value.iec958.status[1] = sbits >> 8;
3364 ucontrol->value.iec958.status[2] = sbits >> 16;
3365 ucontrol->value.iec958.status[3] = sbits >> 24;
3366 return 0;
3367}
3368
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003369static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 {
3371 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003372 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 .info = snd_hda_spdif_in_switch_info,
3374 .get = snd_hda_spdif_in_switch_get,
3375 .put = snd_hda_spdif_in_switch_put,
3376 },
3377 {
3378 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3379 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003380 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 .info = snd_hda_spdif_mask_info,
3382 .get = snd_hda_spdif_in_status_get,
3383 },
3384 { } /* end */
3385};
3386
3387/**
3388 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3389 * @codec: the HDA codec
3390 * @nid: audio in widget NID
3391 *
3392 * Creates controls related with the SPDIF input.
3393 * Called from each patch supporting the SPDIF in.
3394 *
3395 * Returns 0 if successful, or a negative error code.
3396 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003397int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398{
3399 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003400 struct snd_kcontrol *kctl;
3401 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003402 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403
Takashi Iwaidcda5802012-10-12 17:24:51 +02003404 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003405 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003406 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
3407 return -EBUSY;
3408 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3410 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01003411 if (!kctl)
3412 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003414 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003415 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 return err;
3417 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003418 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01003419 snd_hda_codec_read(codec, nid, 0,
3420 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02003421 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 return 0;
3423}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003424EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003426/*
3427 * command cache
3428 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429
Takashi Iwaic370dd62012-12-13 18:30:04 +01003430/* build a 31bit cache key with the widget id and the command parameter */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003431#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3432#define get_cmd_cache_nid(key) ((key) & 0xff)
3433#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3434
3435/**
3436 * snd_hda_codec_write_cache - send a single command with caching
3437 * @codec: the HDA codec
3438 * @nid: NID to send the command
3439 * @direct: direct flag
3440 * @verb: the verb to send
3441 * @parm: the parameter for the verb
3442 *
3443 * Send a single command without waiting for response.
3444 *
3445 * Returns 0 if successful, or a negative error code.
3446 */
3447int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
3448 int direct, unsigned int verb, unsigned int parm)
3449{
Takashi Iwaic370dd62012-12-13 18:30:04 +01003450 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003451 struct hda_cache_head *c;
3452 u32 key;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003453 unsigned int cache_only;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003454
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003455 cache_only = codec->cached_write;
3456 if (!cache_only) {
Takashi Iwaic370dd62012-12-13 18:30:04 +01003457 err = snd_hda_codec_write(codec, nid, direct, verb, parm);
3458 if (err < 0)
3459 return err;
3460 }
3461
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003462 /* parm may contain the verb stuff for get/set amp */
3463 verb = verb | (parm >> 8);
3464 parm &= 0xff;
3465 key = build_cmd_cache_key(nid, verb);
3466 mutex_lock(&codec->bus->cmd_mutex);
3467 c = get_alloc_hash(&codec->cmd_cache, key);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003468 if (c) {
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003469 c->val = parm;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003470 c->dirty = cache_only;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003471 }
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003472 mutex_unlock(&codec->bus->cmd_mutex);
3473 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003474}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003475EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003476
Takashi Iwaid5191e52009-11-16 14:58:17 +01003477/**
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003478 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3479 * @codec: the HDA codec
3480 * @nid: NID to send the command
3481 * @direct: direct flag
3482 * @verb: the verb to send
3483 * @parm: the parameter for the verb
3484 *
3485 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3486 * command if the parameter is already identical with the cached value.
3487 * If not, it sends the command and refreshes the cache.
3488 *
3489 * Returns 0 if successful, or a negative error code.
3490 */
3491int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
3492 int direct, unsigned int verb, unsigned int parm)
3493{
3494 struct hda_cache_head *c;
3495 u32 key;
3496
3497 /* parm may contain the verb stuff for get/set amp */
3498 verb = verb | (parm >> 8);
3499 parm &= 0xff;
3500 key = build_cmd_cache_key(nid, verb);
3501 mutex_lock(&codec->bus->cmd_mutex);
3502 c = get_hash(&codec->cmd_cache, key);
3503 if (c && c->val == parm) {
3504 mutex_unlock(&codec->bus->cmd_mutex);
3505 return 0;
3506 }
3507 mutex_unlock(&codec->bus->cmd_mutex);
3508 return snd_hda_codec_write_cache(codec, nid, direct, verb, parm);
3509}
3510EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
3511
3512/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003513 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3514 * @codec: HD-audio codec
3515 *
3516 * Execute all verbs recorded in the command caches to resume.
3517 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003518void snd_hda_codec_resume_cache(struct hda_codec *codec)
3519{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003520 int i;
3521
Takashi Iwaic370dd62012-12-13 18:30:04 +01003522 mutex_lock(&codec->hash_mutex);
3523 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3524 struct hda_cache_head *buffer;
3525 u32 key;
3526
3527 buffer = snd_array_elem(&codec->cmd_cache.buf, i);
3528 key = buffer->key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003529 if (!key)
3530 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003531 if (!buffer->dirty)
3532 continue;
3533 buffer->dirty = 0;
3534 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003535 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3536 get_cmd_cache_cmd(key), buffer->val);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003537 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003538 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01003539 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003540}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003541EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003542
3543/**
3544 * snd_hda_sequence_write_cache - sequence writes with caching
3545 * @codec: the HDA codec
3546 * @seq: VERB array to send
3547 *
3548 * Send the commands sequentially from the given array.
3549 * Thte commands are recorded on cache for power-save and resume.
3550 * The array must be terminated with NID=0.
3551 */
3552void snd_hda_sequence_write_cache(struct hda_codec *codec,
3553 const struct hda_verb *seq)
3554{
3555 for (; seq->nid; seq++)
3556 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3557 seq->param);
3558}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003559EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003560
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003561void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
3562 unsigned int power_state,
3563 bool eapd_workaround)
Takashi Iwai54d17402005-11-21 16:33:22 +01003564{
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003565 hda_nid_t nid = codec->start_nid;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003566 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01003567
Takashi Iwaicb53c622007-08-10 17:21:45 +02003568 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003569 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003570 if (!(wcaps & AC_WCAP_POWER))
3571 continue;
3572 /* don't power down the widget if it controls eapd and
3573 * EAPD_BTLENABLE is set.
3574 */
3575 if (eapd_workaround && power_state == AC_PWRST_D3 &&
3576 get_wcaps_type(wcaps) == AC_WID_PIN &&
3577 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3578 int eapd = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003579 AC_VERB_GET_EAPD_BTLENABLE, 0);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003580 if (eapd & 0x02)
3581 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003582 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003583 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
3584 power_state);
Takashi Iwai54d17402005-11-21 16:33:22 +01003585 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003586}
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003587EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all);
3588
3589/*
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003590 * supported power states check
3591 */
3592static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg,
3593 unsigned int power_state)
3594{
3595 int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE);
3596
Mengdong Line037cb42012-08-10 14:11:58 +02003597 if (sup == -1)
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003598 return false;
3599 if (sup & power_state)
3600 return true;
3601 else
3602 return false;
3603}
3604
3605/*
Takashi Iwai432c6412012-08-28 09:59:20 -07003606 * wait until the state is reached, returns the current state
3607 */
3608static unsigned int hda_sync_power_state(struct hda_codec *codec,
3609 hda_nid_t fg,
3610 unsigned int power_state)
3611{
3612 unsigned long end_time = jiffies + msecs_to_jiffies(500);
3613 unsigned int state, actual_state;
3614
3615 for (;;) {
3616 state = snd_hda_codec_read(codec, fg, 0,
3617 AC_VERB_GET_POWER_STATE, 0);
3618 if (state & AC_PWRST_ERROR)
3619 break;
3620 actual_state = (state >> 4) & 0x0f;
3621 if (actual_state == power_state)
3622 break;
3623 if (time_after_eq(jiffies, end_time))
3624 break;
3625 /* wait until the codec reachs to the target state */
3626 msleep(1);
3627 }
3628 return state;
3629}
3630
3631/*
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003632 * set power state of the codec, and return the power state
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003633 */
Takashi Iwaid8193872012-08-31 07:54:38 -07003634static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003635 unsigned int power_state)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003636{
Takashi Iwaid8193872012-08-31 07:54:38 -07003637 hda_nid_t fg = codec->afg ? codec->afg : codec->mfg;
Wang Xingchao09617ce2012-06-08 10:26:08 +08003638 int count;
3639 unsigned int state;
3640
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003641 /* this delay seems necessary to avoid click noise at power-down */
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003642 if (power_state == AC_PWRST_D3) {
3643 /* transition time less than 10ms for power down */
Takashi Iwai983f6b92012-08-28 09:18:01 -07003644 msleep(codec->epss ? 10 : 100);
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003645 }
Wang Xingchao09617ce2012-06-08 10:26:08 +08003646
3647 /* repeat power states setting at most 10 times*/
3648 for (count = 0; count < 10; count++) {
Takashi Iwai432c6412012-08-28 09:59:20 -07003649 if (codec->patch_ops.set_power_state)
3650 codec->patch_ops.set_power_state(codec, fg,
3651 power_state);
3652 else {
3653 snd_hda_codec_read(codec, fg, 0,
3654 AC_VERB_SET_POWER_STATE,
3655 power_state);
3656 snd_hda_codec_set_power_to_all(codec, fg, power_state,
3657 true);
3658 }
3659 state = hda_sync_power_state(codec, fg, power_state);
Wang Xingchao09617ce2012-06-08 10:26:08 +08003660 if (!(state & AC_PWRST_ERROR))
3661 break;
3662 }
Mengdong Linb8dfc4622012-08-23 17:32:30 +08003663
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003664 return state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003665}
Takashi Iwai54d17402005-11-21 16:33:22 +01003666
Takashi Iwai11aeff02008-07-30 15:01:46 +02003667#ifdef CONFIG_SND_HDA_HWDEP
3668/* execute additional init verbs */
3669static void hda_exec_init_verbs(struct hda_codec *codec)
3670{
3671 if (codec->init_verbs.list)
3672 snd_hda_sequence_write(codec, codec->init_verbs.list);
3673}
3674#else
3675static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3676#endif
3677
Takashi Iwai2a439522011-07-26 09:52:50 +02003678#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003679/*
3680 * call suspend and power-down; used both from PM and power-save
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003681 * this function returns the power state in the end
Takashi Iwaicb53c622007-08-10 17:21:45 +02003682 */
Dylan Reidd17344b2012-09-28 15:57:01 -07003683static unsigned int hda_call_codec_suspend(struct hda_codec *codec, bool in_wq)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003684{
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003685 unsigned int state;
3686
Takashi Iwai989c3182012-11-19 14:14:58 +01003687 codec->in_pm = 1;
3688
Takashi Iwaicb53c622007-08-10 17:21:45 +02003689 if (codec->patch_ops.suspend)
Takashi Iwai68cb2b52012-07-02 15:20:37 +02003690 codec->patch_ops.suspend(codec);
Takashi Iwaieb541332010-08-06 13:48:11 +02003691 hda_cleanup_all_streams(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07003692 state = hda_set_power_state(codec, AC_PWRST_D3);
Dylan Reidd17344b2012-09-28 15:57:01 -07003693 /* Cancel delayed work if we aren't currently running from it. */
3694 if (!in_wq)
3695 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02003696 spin_lock(&codec->power_lock);
3697 snd_hda_update_power_acct(codec);
3698 trace_hda_power_down(codec);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02003699 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02003700 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003701 codec->power_jiffies = jiffies;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02003702 spin_unlock(&codec->power_lock);
Takashi Iwai989c3182012-11-19 14:14:58 +01003703 codec->in_pm = 0;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003704 return state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003705}
3706
Takashi Iwaic370dd62012-12-13 18:30:04 +01003707/* mark all entries of cmd and amp caches dirty */
3708static void hda_mark_cmd_cache_dirty(struct hda_codec *codec)
3709{
3710 int i;
3711 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3712 struct hda_cache_head *cmd;
3713 cmd = snd_array_elem(&codec->cmd_cache.buf, i);
3714 cmd->dirty = 1;
3715 }
3716 for (i = 0; i < codec->amp_cache.buf.used; i++) {
3717 struct hda_amp_info *amp;
3718 amp = snd_array_elem(&codec->cmd_cache.buf, i);
3719 amp->head.dirty = 1;
3720 }
3721}
3722
Takashi Iwaicb53c622007-08-10 17:21:45 +02003723/*
3724 * kick up codec; used both from PM and power-save
3725 */
3726static void hda_call_codec_resume(struct hda_codec *codec)
3727{
Takashi Iwai989c3182012-11-19 14:14:58 +01003728 codec->in_pm = 1;
3729
Takashi Iwaic370dd62012-12-13 18:30:04 +01003730 hda_mark_cmd_cache_dirty(codec);
3731
Takashi Iwai7f308302012-05-08 16:52:23 +02003732 /* set as if powered on for avoiding re-entering the resume
3733 * in the resume / power-save sequence
3734 */
3735 hda_keep_power_on(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07003736 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02003737 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02003738 hda_exec_init_verbs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003739 if (codec->patch_ops.resume)
3740 codec->patch_ops.resume(codec);
3741 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02003742 if (codec->patch_ops.init)
3743 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003744 snd_hda_codec_resume_amp(codec);
3745 snd_hda_codec_resume_cache(codec);
3746 }
David Henningsson26a6cb62012-10-09 15:04:21 +02003747
3748 if (codec->jackpoll_interval)
3749 hda_jackpoll_work(&codec->jackpoll_work.work);
3750 else {
3751 snd_hda_jack_set_dirty_all(codec);
3752 snd_hda_jack_report_sync(codec);
3753 }
Takashi Iwai989c3182012-11-19 14:14:58 +01003754
3755 codec->in_pm = 0;
Takashi Iwai7f308302012-05-08 16:52:23 +02003756 snd_hda_power_down(codec); /* flag down before returning */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003757}
Takashi Iwai2a439522011-07-26 09:52:50 +02003758#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003759
Takashi Iwai54d17402005-11-21 16:33:22 +01003760
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761/**
3762 * snd_hda_build_controls - build mixer controls
3763 * @bus: the BUS
3764 *
3765 * Creates mixer controls for each codec included in the bus.
3766 *
3767 * Returns 0 if successful, otherwise a negative error code.
3768 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01003769int snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003771 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772
Takashi Iwai0ba21762007-04-16 11:29:14 +02003773 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003774 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003775 if (err < 0) {
Takashi Iwai28d1a852010-03-15 09:05:46 +01003776 printk(KERN_ERR "hda_codec: cannot build controls "
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003777 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003778 err = snd_hda_codec_reset(codec);
3779 if (err < 0) {
3780 printk(KERN_ERR
3781 "hda_codec: cannot revert codec\n");
3782 return err;
3783 }
3784 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003786 return 0;
3787}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003788EXPORT_SYMBOL_HDA(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003790/*
3791 * add standard channel maps if not specified
3792 */
3793static int add_std_chmaps(struct hda_codec *codec)
3794{
3795 int i, str, err;
3796
3797 for (i = 0; i < codec->num_pcms; i++) {
3798 for (str = 0; str < 2; str++) {
3799 struct snd_pcm *pcm = codec->pcm_info[i].pcm;
3800 struct hda_pcm_stream *hinfo =
3801 &codec->pcm_info[i].stream[str];
3802 struct snd_pcm_chmap *chmap;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003803 const struct snd_pcm_chmap_elem *elem;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003804
3805 if (codec->pcm_info[i].own_chmap)
3806 continue;
3807 if (!pcm || !hinfo->substreams)
3808 continue;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003809 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
3810 err = snd_pcm_add_chmap_ctls(pcm, str, elem,
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003811 hinfo->channels_max,
3812 0, &chmap);
3813 if (err < 0)
3814 return err;
3815 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
3816 }
3817 }
3818 return 0;
3819}
3820
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003821/* default channel maps for 2.1 speakers;
3822 * since HD-audio supports only stereo, odd number channels are omitted
3823 */
3824const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
3825 { .channels = 2,
3826 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
3827 { .channels = 4,
3828 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
3829 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
3830 { }
3831};
3832EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
3833
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003834int snd_hda_codec_build_controls(struct hda_codec *codec)
3835{
3836 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02003837 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003838 /* continue to initialize... */
3839 if (codec->patch_ops.init)
3840 err = codec->patch_ops.init(codec);
3841 if (!err && codec->patch_ops.build_controls)
3842 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003843 if (err < 0)
3844 return err;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003845
3846 /* we create chmaps here instead of build_pcms */
3847 err = add_std_chmaps(codec);
3848 if (err < 0)
3849 return err;
3850
David Henningsson26a6cb62012-10-09 15:04:21 +02003851 if (codec->jackpoll_interval)
3852 hda_jackpoll_work(&codec->jackpoll_work.work);
3853 else
3854 snd_hda_jack_report_sync(codec); /* call at the last init point */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855 return 0;
3856}
3857
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858/*
3859 * stream formats
3860 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02003861struct hda_rate_tbl {
3862 unsigned int hz;
3863 unsigned int alsa_bits;
3864 unsigned int hda_fmt;
3865};
3866
Takashi Iwai92f10b32010-08-03 14:21:00 +02003867/* rate = base * mult / div */
3868#define HDA_RATE(base, mult, div) \
3869 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
3870 (((div) - 1) << AC_FMT_DIV_SHIFT))
3871
Takashi Iwaibefdf312005-08-22 13:57:55 +02003872static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003874
3875 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003876 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
3877 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
3878 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
3879 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
3880 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
3881 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
3882 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
3883 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
3884 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
3885 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
3886 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003887#define AC_PAR_PCM_RATE_BITS 11
3888 /* up to bits 10, 384kHZ isn't supported properly */
3889
3890 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003891 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003892
Takashi Iwaibefdf312005-08-22 13:57:55 +02003893 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894};
3895
3896/**
3897 * snd_hda_calc_stream_format - calculate format bitset
3898 * @rate: the sample rate
3899 * @channels: the number of channels
3900 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3901 * @maxbps: the max. bps
3902 *
3903 * Calculate the format bitset from the given rate, channels and th PCM format.
3904 *
3905 * Return zero if invalid.
3906 */
3907unsigned int snd_hda_calc_stream_format(unsigned int rate,
3908 unsigned int channels,
3909 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03003910 unsigned int maxbps,
3911 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912{
3913 int i;
3914 unsigned int val = 0;
3915
Takashi Iwaibefdf312005-08-22 13:57:55 +02003916 for (i = 0; rate_bits[i].hz; i++)
3917 if (rate_bits[i].hz == rate) {
3918 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 break;
3920 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003921 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922 snd_printdd("invalid rate %d\n", rate);
3923 return 0;
3924 }
3925
3926 if (channels == 0 || channels > 8) {
3927 snd_printdd("invalid channels %d\n", channels);
3928 return 0;
3929 }
3930 val |= channels - 1;
3931
3932 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003933 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003934 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003935 break;
3936 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003937 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003938 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939 case 20:
3940 case 24:
3941 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003942 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003943 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003945 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02003947 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948 break;
3949 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003950 snd_printdd("invalid format width %d\n",
3951 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952 return 0;
3953 }
3954
Anssi Hannula32c168c2010-08-03 13:28:57 +03003955 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003956 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03003957
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958 return val;
3959}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003960EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003962static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid,
3963 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003964{
3965 unsigned int val = 0;
3966 if (nid != codec->afg &&
3967 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
3968 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
3969 if (!val || val == -1)
3970 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
3971 if (!val || val == -1)
3972 return 0;
3973 return val;
3974}
3975
3976static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3977{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003978 return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003979 get_pcm_param);
3980}
3981
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003982static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid,
3983 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003984{
3985 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
3986 if (!streams || streams == -1)
3987 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
3988 if (!streams || streams == -1)
3989 return 0;
3990 return streams;
3991}
3992
3993static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
3994{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003995 return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003996 get_stream_param);
3997}
3998
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999/**
4000 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
4001 * @codec: the HDA codec
4002 * @nid: NID to query
4003 * @ratesp: the pointer to store the detected rate bitflags
4004 * @formatsp: the pointer to store the detected formats
4005 * @bpsp: the pointer to store the detected format widths
4006 *
4007 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
4008 * or @bsps argument is ignored.
4009 *
4010 * Returns 0 if successful, otherwise a negative error code.
4011 */
Stephen Warren384a48d2011-06-01 11:14:21 -06004012int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
4014{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004015 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004017 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004018 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019
4020 if (ratesp) {
4021 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004022 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02004024 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004026 if (rates == 0) {
4027 snd_printk(KERN_ERR "hda_codec: rates == 0 "
4028 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
4029 nid, val,
4030 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
4031 return -EIO;
4032 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033 *ratesp = rates;
4034 }
4035
4036 if (formatsp || bpsp) {
4037 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004038 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004040 streams = query_stream_param(codec, nid);
4041 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043
4044 bps = 0;
4045 if (streams & AC_SUPFMT_PCM) {
4046 if (val & AC_SUPPCM_BITS_8) {
4047 formats |= SNDRV_PCM_FMTBIT_U8;
4048 bps = 8;
4049 }
4050 if (val & AC_SUPPCM_BITS_16) {
4051 formats |= SNDRV_PCM_FMTBIT_S16_LE;
4052 bps = 16;
4053 }
4054 if (wcaps & AC_WCAP_DIGITAL) {
4055 if (val & AC_SUPPCM_BITS_32)
4056 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
4057 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
4058 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4059 if (val & AC_SUPPCM_BITS_24)
4060 bps = 24;
4061 else if (val & AC_SUPPCM_BITS_20)
4062 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004063 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
4064 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4066 if (val & AC_SUPPCM_BITS_32)
4067 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068 else if (val & AC_SUPPCM_BITS_24)
4069 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02004070 else if (val & AC_SUPPCM_BITS_20)
4071 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 }
4073 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004074#if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
Takashi Iwaib5025c52009-07-01 18:05:27 +02004075 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004077 if (!bps)
4078 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02004079 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004080#endif
Takashi Iwaib5025c52009-07-01 18:05:27 +02004081 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02004082 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083 /* temporary hack: we have still no proper support
4084 * for the direct AC3 stream...
4085 */
4086 formats |= SNDRV_PCM_FMTBIT_U8;
4087 bps = 8;
4088 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004089 if (formats == 0) {
4090 snd_printk(KERN_ERR "hda_codec: formats == 0 "
4091 "(nid=0x%x, val=0x%x, ovrd=%i, "
4092 "streams=0x%x)\n",
4093 nid, val,
4094 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
4095 streams);
4096 return -EIO;
4097 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098 if (formatsp)
4099 *formatsp = formats;
4100 if (bpsp)
4101 *bpsp = bps;
4102 }
4103
4104 return 0;
4105}
Stephen Warren384a48d2011-06-01 11:14:21 -06004106EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107
4108/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01004109 * snd_hda_is_supported_format - Check the validity of the format
4110 * @codec: HD-audio codec
4111 * @nid: NID to check
4112 * @format: the HD-audio format value to check
4113 *
4114 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115 *
4116 * Returns 1 if supported, 0 if not.
4117 */
4118int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
4119 unsigned int format)
4120{
4121 int i;
4122 unsigned int val = 0, rate, stream;
4123
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004124 val = query_pcm_param(codec, nid);
4125 if (!val)
4126 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127
4128 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004129 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02004130 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131 if (val & (1 << i))
4132 break;
4133 return 0;
4134 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004135 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136 return 0;
4137
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004138 stream = query_stream_param(codec, nid);
4139 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140 return 0;
4141
4142 if (stream & AC_SUPFMT_PCM) {
4143 switch (format & 0xf0) {
4144 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004145 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146 return 0;
4147 break;
4148 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004149 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150 return 0;
4151 break;
4152 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004153 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154 return 0;
4155 break;
4156 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004157 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 return 0;
4159 break;
4160 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004161 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162 return 0;
4163 break;
4164 default:
4165 return 0;
4166 }
4167 } else {
4168 /* FIXME: check for float32 and AC3? */
4169 }
4170
4171 return 1;
4172}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004173EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174
4175/*
4176 * PCM stuff
4177 */
4178static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
4179 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004180 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181{
4182 return 0;
4183}
4184
4185static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
4186 struct hda_codec *codec,
4187 unsigned int stream_tag,
4188 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004189 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190{
4191 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4192 return 0;
4193}
4194
4195static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
4196 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004197 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198{
Takashi Iwai888afa12008-03-18 09:57:50 +01004199 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200 return 0;
4201}
4202
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004203static int set_pcm_default_values(struct hda_codec *codec,
4204 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004206 int err;
4207
Takashi Iwai0ba21762007-04-16 11:29:14 +02004208 /* query support PCM information from the given NID */
4209 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004210 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02004211 info->rates ? NULL : &info->rates,
4212 info->formats ? NULL : &info->formats,
4213 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004214 if (err < 0)
4215 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216 }
4217 if (info->ops.open == NULL)
4218 info->ops.open = hda_pcm_default_open_close;
4219 if (info->ops.close == NULL)
4220 info->ops.close = hda_pcm_default_open_close;
4221 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004222 if (snd_BUG_ON(!info->nid))
4223 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224 info->ops.prepare = hda_pcm_default_prepare;
4225 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004227 if (snd_BUG_ON(!info->nid))
4228 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229 info->ops.cleanup = hda_pcm_default_cleanup;
4230 }
4231 return 0;
4232}
4233
Takashi Iwaieb541332010-08-06 13:48:11 +02004234/*
4235 * codec prepare/cleanup entries
4236 */
4237int snd_hda_codec_prepare(struct hda_codec *codec,
4238 struct hda_pcm_stream *hinfo,
4239 unsigned int stream,
4240 unsigned int format,
4241 struct snd_pcm_substream *substream)
4242{
4243 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004244 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004245 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
4246 if (ret >= 0)
4247 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004248 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004249 return ret;
4250}
4251EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
4252
4253void snd_hda_codec_cleanup(struct hda_codec *codec,
4254 struct hda_pcm_stream *hinfo,
4255 struct snd_pcm_substream *substream)
4256{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004257 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004258 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004259 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004260}
4261EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
4262
Takashi Iwaid5191e52009-11-16 14:58:17 +01004263/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01004264const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
4265 "Audio", "SPDIF", "HDMI", "Modem"
4266};
4267
Takashi Iwai176d5332008-07-30 15:01:44 +02004268/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004269 * get the empty PCM device number to assign
Takashi Iwaic8936222010-01-28 17:08:53 +01004270 *
4271 * note the max device number is limited by HDA_MAX_PCMS, currently 10
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004272 */
4273static int get_empty_pcm_device(struct hda_bus *bus, int type)
4274{
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004275 /* audio device indices; not linear to keep compatibility */
4276 static int audio_idx[HDA_PCM_NTYPES][5] = {
4277 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
4278 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01004279 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004280 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004281 };
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004282 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004283
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004284 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004285 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
4286 return -EINVAL;
4287 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004288
4289 for (i = 0; audio_idx[type][i] >= 0 ; i++)
4290 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
4291 return audio_idx[type][i];
4292
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004293 /* non-fixed slots starting from 10 */
4294 for (i = 10; i < 32; i++) {
4295 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4296 return i;
4297 }
4298
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004299 snd_printk(KERN_WARNING "Too many %s devices\n",
4300 snd_hda_pcm_type_name[type]);
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004301 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004302}
4303
4304/*
Takashi Iwai176d5332008-07-30 15:01:44 +02004305 * attach a new PCM stream
4306 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004307static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02004308{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004309 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02004310 struct hda_pcm_stream *info;
4311 int stream, err;
4312
Takashi Iwaib91f0802008-11-04 08:43:08 +01004313 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02004314 return -EINVAL;
4315 for (stream = 0; stream < 2; stream++) {
4316 info = &pcm->stream[stream];
4317 if (info->substreams) {
4318 err = set_pcm_default_values(codec, info);
4319 if (err < 0)
4320 return err;
4321 }
4322 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004323 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02004324}
4325
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004326/* assign all PCMs of the given codec */
4327int snd_hda_codec_build_pcms(struct hda_codec *codec)
4328{
4329 unsigned int pcm;
4330 int err;
4331
4332 if (!codec->num_pcms) {
4333 if (!codec->patch_ops.build_pcms)
4334 return 0;
4335 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004336 if (err < 0) {
4337 printk(KERN_ERR "hda_codec: cannot build PCMs"
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004338 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004339 err = snd_hda_codec_reset(codec);
4340 if (err < 0) {
4341 printk(KERN_ERR
4342 "hda_codec: cannot revert codec\n");
4343 return err;
4344 }
4345 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004346 }
4347 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
4348 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
4349 int dev;
4350
4351 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01004352 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004353
4354 if (!cpcm->pcm) {
4355 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
4356 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004357 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004358 cpcm->device = dev;
4359 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004360 if (err < 0) {
4361 printk(KERN_ERR "hda_codec: cannot attach "
4362 "PCM stream %d for codec #%d\n",
4363 dev, codec->addr);
4364 continue; /* no fatal error */
4365 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004366 }
4367 }
4368 return 0;
4369}
4370
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371/**
4372 * snd_hda_build_pcms - build PCM information
4373 * @bus: the BUS
4374 *
4375 * Create PCM information for each codec included in the bus.
4376 *
4377 * The build_pcms codec patch is requested to set up codec->num_pcms and
4378 * codec->pcm_info properly. The array is referred by the top-level driver
4379 * to create its PCM instances.
4380 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4381 * callback.
4382 *
4383 * At least, substreams, channels_min and channels_max must be filled for
4384 * each stream. substreams = 0 indicates that the stream doesn't exist.
4385 * When rates and/or formats are zero, the supported values are queried
4386 * from the given nid. The nid is used also by the default ops.prepare
4387 * and ops.cleanup callbacks.
4388 *
4389 * The driver needs to call ops.open in its open callback. Similarly,
4390 * ops.close is supposed to be called in the close callback.
4391 * ops.prepare should be called in the prepare or hw_params callback
4392 * with the proper parameters for set up.
4393 * ops.cleanup should be called in hw_free for clean up of streams.
4394 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004395 * This function returns 0 if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396 */
Takashi Iwai5cb543d2012-08-09 13:49:23 +02004397int snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004399 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400
Takashi Iwai0ba21762007-04-16 11:29:14 +02004401 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004402 int err = snd_hda_codec_build_pcms(codec);
4403 if (err < 0)
4404 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405 }
4406 return 0;
4407}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004408EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410/**
4411 * snd_hda_check_board_config - compare the current codec with the config table
4412 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004413 * @num_configs: number of config enums
4414 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415 * @tbl: configuration table, terminated by null entries
4416 *
4417 * Compares the modelname or PCI subsystem id of the current codec with the
4418 * given configuration table. If a matching entry is found, returns its
4419 * config value (supposed to be 0 or positive).
4420 *
4421 * If no entries are matching, the function returns a negative value.
4422 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02004423int snd_hda_check_board_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004424 int num_configs, const char * const *models,
Takashi Iwai12f288b2007-08-02 15:51:59 +02004425 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426{
Takashi Iwaif44ac832008-07-30 15:01:45 +02004427 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004428 int i;
4429 for (i = 0; i < num_configs; i++) {
4430 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02004431 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004432 snd_printd(KERN_INFO "hda_codec: model '%s' is "
4433 "selected\n", models[i]);
4434 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435 }
4436 }
4437 }
4438
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004439 if (!codec->bus->pci || !tbl)
4440 return -1;
4441
4442 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
4443 if (!tbl)
4444 return -1;
4445 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02004446#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004447 char tmp[10];
4448 const char *model = NULL;
4449 if (models)
4450 model = models[tbl->value];
4451 if (!model) {
4452 sprintf(tmp, "#%d", tbl->value);
4453 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004455 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4456 "for config %x:%x (%s)\n",
4457 model, tbl->subvendor, tbl->subdevice,
4458 (tbl->name ? tbl->name : "Unknown device"));
4459#endif
4460 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461 }
4462 return -1;
4463}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004464EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465
4466/**
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004467 * snd_hda_check_board_codec_sid_config - compare the current codec
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004468 subsystem ID with the
4469 config table
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004470
4471 This is important for Gateway notebooks with SB450 HDA Audio
4472 where the vendor ID of the PCI device is:
4473 ATI Technologies Inc SB450 HDA Audio [1002:437b]
4474 and the vendor/subvendor are found only at the codec.
4475
4476 * @codec: the HDA codec
4477 * @num_configs: number of config enums
4478 * @models: array of model name strings
4479 * @tbl: configuration table, terminated by null entries
4480 *
4481 * Compares the modelname or PCI subsystem id of the current codec with the
4482 * given configuration table. If a matching entry is found, returns its
4483 * config value (supposed to be 0 or positive).
4484 *
4485 * If no entries are matching, the function returns a negative value.
4486 */
4487int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004488 int num_configs, const char * const *models,
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004489 const struct snd_pci_quirk *tbl)
4490{
4491 const struct snd_pci_quirk *q;
4492
4493 /* Search for codec ID */
4494 for (q = tbl; q->subvendor; q++) {
Takashi Iwaie2301a42011-11-22 19:58:56 +01004495 unsigned int mask = 0xffff0000 | q->subdevice_mask;
4496 unsigned int id = (q->subdevice | (q->subvendor << 16)) & mask;
4497 if ((codec->subsystem_id & mask) == id)
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004498 break;
4499 }
4500
4501 if (!q->subvendor)
4502 return -1;
4503
4504 tbl = q;
4505
4506 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwaid94ff6b2009-09-02 00:20:21 +02004507#ifdef CONFIG_SND_DEBUG_VERBOSE
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004508 char tmp[10];
4509 const char *model = NULL;
4510 if (models)
4511 model = models[tbl->value];
4512 if (!model) {
4513 sprintf(tmp, "#%d", tbl->value);
4514 model = tmp;
4515 }
4516 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4517 "for config %x:%x (%s)\n",
4518 model, tbl->subvendor, tbl->subdevice,
4519 (tbl->name ? tbl->name : "Unknown device"));
4520#endif
4521 return tbl->value;
4522 }
4523 return -1;
4524}
4525EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
4526
4527/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528 * snd_hda_add_new_ctls - create controls from the array
4529 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004530 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531 *
4532 * This helper function creates and add new controls in the given array.
4533 * The array must be terminated with an empty entry as terminator.
4534 *
4535 * Returns 0 if successful, or a negative error code.
4536 */
Takashi Iwai031024e2011-05-02 11:29:30 +02004537int snd_hda_add_new_ctls(struct hda_codec *codec,
4538 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01004540 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541
4542 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004543 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004544 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01004545 if (knew->iface == -1) /* skip this codec private value */
4546 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004547 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004548 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02004549 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01004550 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004551 if (addr > 0)
4552 kctl->id.device = addr;
4553 if (idx > 0)
4554 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01004555 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004556 if (!err)
4557 break;
4558 /* try first with another device index corresponding to
4559 * the codec addr; if it still fails (or it's the
4560 * primary codec), then try another control index
4561 */
4562 if (!addr && codec->addr)
4563 addr = codec->addr;
4564 else if (!idx && !knew->index) {
4565 idx = find_empty_mixer_ctl_idx(codec,
Takashi Iwaidcda5802012-10-12 17:24:51 +02004566 knew->name, 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004567 if (idx <= 0)
4568 return err;
4569 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01004570 return err;
4571 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572 }
4573 return 0;
4574}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004575EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576
Takashi Iwai83012a72012-08-24 18:38:08 +02004577#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02004578static void hda_power_work(struct work_struct *work)
4579{
4580 struct hda_codec *codec =
4581 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004582 struct hda_bus *bus = codec->bus;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004583 unsigned int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004584
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004585 spin_lock(&codec->power_lock);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004586 if (codec->power_transition > 0) { /* during power-up sequence? */
4587 spin_unlock(&codec->power_lock);
4588 return;
4589 }
Maxim Levitsky2e492462007-09-03 15:26:57 +02004590 if (!codec->power_on || codec->power_count) {
4591 codec->power_transition = 0;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004592 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004593 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02004594 }
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004595 spin_unlock(&codec->power_lock);
4596
Dylan Reidd17344b2012-09-28 15:57:01 -07004597 state = hda_call_codec_suspend(codec, true);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004598 codec->pm_down_notified = 0;
4599 if (!bus->power_keep_link_on && (state & AC_PWRST_CLK_STOP_OK)) {
4600 codec->pm_down_notified = 1;
Takashi Iwai68467f52012-08-28 09:14:29 -07004601 hda_call_pm_notify(bus, false);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004602 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02004603}
4604
4605static void hda_keep_power_on(struct hda_codec *codec)
4606{
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004607 spin_lock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004608 codec->power_count++;
4609 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004610 codec->power_jiffies = jiffies;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004611 spin_unlock(&codec->power_lock);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004612}
4613
Takashi Iwaid5191e52009-11-16 14:58:17 +01004614/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01004615void snd_hda_update_power_acct(struct hda_codec *codec)
4616{
4617 unsigned long delta = jiffies - codec->power_jiffies;
4618 if (codec->power_on)
4619 codec->power_on_acct += delta;
4620 else
4621 codec->power_off_acct += delta;
4622 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004623}
4624
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004625/* Transition to powered up, if wait_power_down then wait for a pending
4626 * transition to D3 to complete. A pending D3 transition is indicated
4627 * with power_transition == -1. */
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004628/* call this with codec->power_lock held! */
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004629static void __snd_hda_power_up(struct hda_codec *codec, bool wait_power_down)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004630{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004631 struct hda_bus *bus = codec->bus;
4632
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004633 /* Return if power_on or transitioning to power_on, unless currently
4634 * powering down. */
4635 if ((codec->power_on || codec->power_transition > 0) &&
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004636 !(wait_power_down && codec->power_transition < 0))
Takashi Iwaicb53c622007-08-10 17:21:45 +02004637 return;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004638 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004639
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004640 cancel_delayed_work_sync(&codec->power_work);
4641
4642 spin_lock(&codec->power_lock);
Dylan Reidb43d2242012-06-21 21:51:22 -07004643 /* If the power down delayed work was cancelled above before starting,
4644 * then there is no need to go through power up here.
4645 */
4646 if (codec->power_on) {
Takashi Iwai535b6c52012-08-20 21:25:22 +02004647 if (codec->power_transition < 0)
4648 codec->power_transition = 0;
Dylan Reidb43d2242012-06-21 21:51:22 -07004649 return;
4650 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004651
Takashi Iwaid66fee52011-08-02 15:39:31 +02004652 trace_hda_power_up(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004653 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004654 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004655 codec->power_jiffies = jiffies;
Takashi Iwai7f308302012-05-08 16:52:23 +02004656 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004657 spin_unlock(&codec->power_lock);
4658
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004659 if (codec->pm_down_notified) {
4660 codec->pm_down_notified = 0;
Takashi Iwai68467f52012-08-28 09:14:29 -07004661 hda_call_pm_notify(bus, true);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004662 }
4663
Takashi Iwaicb53c622007-08-10 17:21:45 +02004664 hda_call_codec_resume(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004665
4666 spin_lock(&codec->power_lock);
Takashi Iwaia221e282007-08-16 16:35:33 +02004667 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004668}
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004669
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004670#define power_save(codec) \
4671 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004672
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004673/* Transition to powered down */
4674static void __snd_hda_power_down(struct hda_codec *codec)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004675{
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004676 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004677 return;
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004678
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004679 if (power_save(codec)) {
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004680 codec->power_transition = -1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01004681 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004682 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02004683 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004684}
4685
4686/**
4687 * snd_hda_power_save - Power-up/down/sync the codec
4688 * @codec: HD-audio codec
4689 * @delta: the counter delta to change
4690 *
4691 * Change the power-up counter via @delta, and power up or down the hardware
4692 * appropriately. For the power-down, queue to the delayed action.
4693 * Passing zero to @delta means to synchronize the power state.
4694 */
4695void snd_hda_power_save(struct hda_codec *codec, int delta, bool d3wait)
4696{
4697 spin_lock(&codec->power_lock);
4698 codec->power_count += delta;
4699 trace_hda_power_count(codec);
4700 if (delta > 0)
4701 __snd_hda_power_up(codec, d3wait);
4702 else
4703 __snd_hda_power_down(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004704 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004705}
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004706EXPORT_SYMBOL_HDA(snd_hda_power_save);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004707
Takashi Iwaid5191e52009-11-16 14:58:17 +01004708/**
4709 * snd_hda_check_amp_list_power - Check the amp list and update the power
4710 * @codec: HD-audio codec
4711 * @check: the object containing an AMP list and the status
4712 * @nid: NID to check / update
4713 *
4714 * Check whether the given NID is in the amp list. If it's in the list,
4715 * check the current AMP status, and update the the power-status according
4716 * to the mute status.
4717 *
4718 * This function is supposed to be set or called from the check_power_status
4719 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004720 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004721int snd_hda_check_amp_list_power(struct hda_codec *codec,
4722 struct hda_loopback_check *check,
4723 hda_nid_t nid)
4724{
Takashi Iwai031024e2011-05-02 11:29:30 +02004725 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004726 int ch, v;
4727
4728 if (!check->amplist)
4729 return 0;
4730 for (p = check->amplist; p->nid; p++) {
4731 if (p->nid == nid)
4732 break;
4733 }
4734 if (!p->nid)
4735 return 0; /* nothing changed */
4736
4737 for (p = check->amplist; p->nid; p++) {
4738 for (ch = 0; ch < 2; ch++) {
4739 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4740 p->idx);
4741 if (!(v & HDA_AMP_MUTE) && v > 0) {
4742 if (!check->power_on) {
4743 check->power_on = 1;
4744 snd_hda_power_up(codec);
4745 }
4746 return 1;
4747 }
4748 }
4749 }
4750 if (check->power_on) {
4751 check->power_on = 0;
4752 snd_hda_power_down(codec);
4753 }
4754 return 0;
4755}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004756EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004757#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004759/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004760 * Channel mode helper
4761 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004762
4763/**
4764 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
4765 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004766int snd_hda_ch_mode_info(struct hda_codec *codec,
4767 struct snd_ctl_elem_info *uinfo,
4768 const struct hda_channel_mode *chmode,
4769 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004770{
4771 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4772 uinfo->count = 1;
4773 uinfo->value.enumerated.items = num_chmodes;
4774 if (uinfo->value.enumerated.item >= num_chmodes)
4775 uinfo->value.enumerated.item = num_chmodes - 1;
4776 sprintf(uinfo->value.enumerated.name, "%dch",
4777 chmode[uinfo->value.enumerated.item].channels);
4778 return 0;
4779}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004780EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004781
Takashi Iwaid5191e52009-11-16 14:58:17 +01004782/**
4783 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
4784 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004785int snd_hda_ch_mode_get(struct hda_codec *codec,
4786 struct snd_ctl_elem_value *ucontrol,
4787 const struct hda_channel_mode *chmode,
4788 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004789 int max_channels)
4790{
4791 int i;
4792
4793 for (i = 0; i < num_chmodes; i++) {
4794 if (max_channels == chmode[i].channels) {
4795 ucontrol->value.enumerated.item[0] = i;
4796 break;
4797 }
4798 }
4799 return 0;
4800}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004801EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004802
Takashi Iwaid5191e52009-11-16 14:58:17 +01004803/**
4804 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
4805 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004806int snd_hda_ch_mode_put(struct hda_codec *codec,
4807 struct snd_ctl_elem_value *ucontrol,
4808 const struct hda_channel_mode *chmode,
4809 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004810 int *max_channelsp)
4811{
4812 unsigned int mode;
4813
4814 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01004815 if (mode >= num_chmodes)
4816 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004817 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004818 return 0;
4819 /* change the current channel setting */
4820 *max_channelsp = chmode[mode].channels;
4821 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004822 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004823 return 1;
4824}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004825EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004826
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827/*
4828 * input MUX helper
4829 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004830
4831/**
4832 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
4833 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004834int snd_hda_input_mux_info(const struct hda_input_mux *imux,
4835 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836{
4837 unsigned int index;
4838
4839 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4840 uinfo->count = 1;
4841 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004842 if (!imux->num_items)
4843 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844 index = uinfo->value.enumerated.item;
4845 if (index >= imux->num_items)
4846 index = imux->num_items - 1;
4847 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
4848 return 0;
4849}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004850EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851
Takashi Iwaid5191e52009-11-16 14:58:17 +01004852/**
4853 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
4854 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004855int snd_hda_input_mux_put(struct hda_codec *codec,
4856 const struct hda_input_mux *imux,
4857 struct snd_ctl_elem_value *ucontrol,
4858 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859 unsigned int *cur_val)
4860{
4861 unsigned int idx;
4862
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004863 if (!imux->num_items)
4864 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865 idx = ucontrol->value.enumerated.item[0];
4866 if (idx >= imux->num_items)
4867 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004868 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004870 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
4871 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872 *cur_val = idx;
4873 return 1;
4874}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004875EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876
4877
4878/*
Takashi Iwaidda415d2012-11-30 18:34:38 +01004879 * process kcontrol info callback of a simple string enum array
4880 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
4881 */
4882int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
4883 struct snd_ctl_elem_info *uinfo,
4884 int num_items, const char * const *texts)
4885{
4886 static const char * const texts_default[] = {
4887 "Disabled", "Enabled"
4888 };
4889
4890 if (!texts || !num_items) {
4891 num_items = 2;
4892 texts = texts_default;
4893 }
4894
4895 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4896 uinfo->count = 1;
4897 uinfo->value.enumerated.items = num_items;
4898 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
4899 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
4900 strcpy(uinfo->value.enumerated.name,
4901 texts[uinfo->value.enumerated.item]);
4902 return 0;
4903}
4904EXPORT_SYMBOL_HDA(snd_hda_enum_helper_info);
4905
4906/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907 * Multi-channel / digital-out PCM helper functions
4908 */
4909
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004910/* setup SPDIF output stream */
4911static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
4912 unsigned int stream_tag, unsigned int format)
4913{
Laurence Darby3bef1c32012-11-03 17:00:06 +00004914 struct hda_spdif_out *spdif;
4915 unsigned int curr_fmt;
4916 bool reset;
Stephen Warren7c9359762011-06-01 11:14:17 -06004917
Laurence Darby3bef1c32012-11-03 17:00:06 +00004918 spdif = snd_hda_spdif_out_of_nid(codec, nid);
4919 curr_fmt = snd_hda_codec_read(codec, nid, 0,
4920 AC_VERB_GET_STREAM_FORMAT, 0);
4921 reset = codec->spdif_status_reset &&
4922 (spdif->ctls & AC_DIG1_ENABLE) &&
4923 curr_fmt != format;
4924
4925 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
4926 updated */
4927 if (reset)
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004928 set_dig_out_convert(codec, nid,
Stephen Warren7c9359762011-06-01 11:14:17 -06004929 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02004930 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004931 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02004932 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004933 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004934 for (d = codec->slave_dig_outs; *d; d++)
4935 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
4936 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004937 }
Takashi Iwai2f728532008-09-25 16:32:41 +02004938 /* turn on again (if needed) */
Laurence Darby3bef1c32012-11-03 17:00:06 +00004939 if (reset)
Takashi Iwai2f728532008-09-25 16:32:41 +02004940 set_dig_out_convert(codec, nid,
Stephen Warren7c9359762011-06-01 11:14:17 -06004941 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02004942}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004943
Takashi Iwai2f728532008-09-25 16:32:41 +02004944static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
4945{
4946 snd_hda_codec_cleanup_stream(codec, nid);
4947 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004948 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004949 for (d = codec->slave_dig_outs; *d; d++)
4950 snd_hda_codec_cleanup_stream(codec, *d);
4951 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004952}
4953
Takashi Iwaid5191e52009-11-16 14:58:17 +01004954/**
4955 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
4956 * @bus: HD-audio bus
4957 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004958void snd_hda_bus_reboot_notify(struct hda_bus *bus)
4959{
4960 struct hda_codec *codec;
4961
4962 if (!bus)
4963 return;
4964 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02004965 if (hda_codec_is_power_on(codec) &&
4966 codec->patch_ops.reboot_notify)
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004967 codec->patch_ops.reboot_notify(codec);
4968 }
4969}
Takashi Iwai8f217a22009-11-10 18:26:12 +01004970EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004971
Takashi Iwaid5191e52009-11-16 14:58:17 +01004972/**
4973 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004975int snd_hda_multi_out_dig_open(struct hda_codec *codec,
4976 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977{
Ingo Molnar62932df2006-01-16 16:34:20 +01004978 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02004979 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
4980 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02004981 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01004983 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004984 return 0;
4985}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004986EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987
Takashi Iwaid5191e52009-11-16 14:58:17 +01004988/**
4989 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
4990 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004991int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
4992 struct hda_multi_out *mout,
4993 unsigned int stream_tag,
4994 unsigned int format,
4995 struct snd_pcm_substream *substream)
4996{
4997 mutex_lock(&codec->spdif_mutex);
4998 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
4999 mutex_unlock(&codec->spdif_mutex);
5000 return 0;
5001}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005002EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005003
Takashi Iwaid5191e52009-11-16 14:58:17 +01005004/**
5005 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
5006 */
Takashi Iwai9411e212009-02-13 11:32:28 +01005007int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
5008 struct hda_multi_out *mout)
5009{
5010 mutex_lock(&codec->spdif_mutex);
5011 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5012 mutex_unlock(&codec->spdif_mutex);
5013 return 0;
5014}
5015EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
5016
Takashi Iwaid5191e52009-11-16 14:58:17 +01005017/**
5018 * snd_hda_multi_out_dig_close - release the digital out stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005020int snd_hda_multi_out_dig_close(struct hda_codec *codec,
5021 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022{
Ingo Molnar62932df2006-01-16 16:34:20 +01005023 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005024 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01005025 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026 return 0;
5027}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005028EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029
Takashi Iwaid5191e52009-11-16 14:58:17 +01005030/**
5031 * snd_hda_multi_out_analog_open - open analog outputs
5032 *
5033 * Open analog outputs and set up the hw-constraints.
5034 * If the digital outputs can be opened as slave, open the digital
5035 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005037int snd_hda_multi_out_analog_open(struct hda_codec *codec,
5038 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01005039 struct snd_pcm_substream *substream,
5040 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041{
Takashi Iwai9a081602008-02-12 18:37:26 +01005042 struct snd_pcm_runtime *runtime = substream->runtime;
5043 runtime->hw.channels_max = mout->max_channels;
5044 if (mout->dig_out_nid) {
5045 if (!mout->analog_rates) {
5046 mout->analog_rates = hinfo->rates;
5047 mout->analog_formats = hinfo->formats;
5048 mout->analog_maxbps = hinfo->maxbps;
5049 } else {
5050 runtime->hw.rates = mout->analog_rates;
5051 runtime->hw.formats = mout->analog_formats;
5052 hinfo->maxbps = mout->analog_maxbps;
5053 }
5054 if (!mout->spdif_rates) {
5055 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
5056 &mout->spdif_rates,
5057 &mout->spdif_formats,
5058 &mout->spdif_maxbps);
5059 }
5060 mutex_lock(&codec->spdif_mutex);
5061 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02005062 if ((runtime->hw.rates & mout->spdif_rates) &&
5063 (runtime->hw.formats & mout->spdif_formats)) {
5064 runtime->hw.rates &= mout->spdif_rates;
5065 runtime->hw.formats &= mout->spdif_formats;
5066 if (mout->spdif_maxbps < hinfo->maxbps)
5067 hinfo->maxbps = mout->spdif_maxbps;
5068 } else {
5069 mout->share_spdif = 0;
5070 /* FIXME: need notify? */
5071 }
Takashi Iwai9a081602008-02-12 18:37:26 +01005072 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02005073 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01005074 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005075 return snd_pcm_hw_constraint_step(substream->runtime, 0,
5076 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
5077}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005078EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005079
Takashi Iwaid5191e52009-11-16 14:58:17 +01005080/**
5081 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
5082 *
5083 * Set up the i/o for analog out.
5084 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005086int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
5087 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088 unsigned int stream_tag,
5089 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01005090 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005091{
Takashi Iwaidda14412011-05-02 11:29:30 +02005092 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093 int chs = substream->runtime->channels;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005094 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095 int i;
5096
Ingo Molnar62932df2006-01-16 16:34:20 +01005097 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005098 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Takashi Iwai9a081602008-02-12 18:37:26 +01005099 if (mout->dig_out_nid && mout->share_spdif &&
5100 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02005102 snd_hda_is_supported_format(codec, mout->dig_out_nid,
5103 format) &&
Stephen Warren7c9359762011-06-01 11:14:17 -06005104 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005106 setup_dig_out_stream(codec, mout->dig_out_nid,
5107 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005108 } else {
5109 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02005110 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111 }
5112 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005113 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005114
5115 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005116 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
5117 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005118 if (!mout->no_share_stream &&
5119 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005120 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005121 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
5122 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005123 /* extra outputs copied from front */
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005124 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5125 if (!mout->no_share_stream && mout->hp_out_nid[i])
5126 snd_hda_codec_setup_stream(codec,
5127 mout->hp_out_nid[i],
5128 stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005129 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02005130 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01005131 snd_hda_codec_setup_stream(codec,
5132 mout->extra_out_nid[i],
5133 stream_tag, 0, format);
5134
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135 /* surrounds */
5136 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02005137 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005138 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5139 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005140 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005141 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5142 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005143 }
5144 return 0;
5145}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005146EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147
Takashi Iwaid5191e52009-11-16 14:58:17 +01005148/**
5149 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Linus Torvalds1da177e2005-04-16 15:20:36 -07005150 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005151int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
5152 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153{
Takashi Iwaidda14412011-05-02 11:29:30 +02005154 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155 int i;
5156
5157 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01005158 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005159 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01005160 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005161 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5162 if (mout->hp_out_nid[i])
5163 snd_hda_codec_cleanup_stream(codec,
5164 mout->hp_out_nid[i]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005165 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
5166 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01005167 snd_hda_codec_cleanup_stream(codec,
5168 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01005169 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02005171 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172 mout->dig_out_used = 0;
5173 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005174 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005175 return 0;
5176}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005177EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178
Takashi Iwai47408602012-04-20 13:06:53 +02005179/**
5180 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
5181 *
5182 * Guess the suitable VREF pin bits to be set as the pin-control value.
5183 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
5184 */
5185unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
5186{
5187 unsigned int pincap;
5188 unsigned int oldval;
5189 oldval = snd_hda_codec_read(codec, pin, 0,
5190 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5191 pincap = snd_hda_query_pin_caps(codec, pin);
5192 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5193 /* Exception: if the default pin setup is vref50, we give it priority */
5194 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
5195 return AC_PINCTL_VREF_80;
5196 else if (pincap & AC_PINCAP_VREF_50)
5197 return AC_PINCTL_VREF_50;
5198 else if (pincap & AC_PINCAP_VREF_100)
5199 return AC_PINCTL_VREF_100;
5200 else if (pincap & AC_PINCAP_VREF_GRD)
5201 return AC_PINCTL_VREF_GRD;
5202 return AC_PINCTL_VREF_HIZ;
5203}
5204EXPORT_SYMBOL_HDA(snd_hda_get_default_vref);
5205
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005206int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
5207 unsigned int val, bool cached)
5208{
5209 if (val) {
5210 unsigned int cap = snd_hda_query_pin_caps(codec, pin);
Takashi Iwai6942c102012-04-20 13:08:40 +02005211 if (cap && (val & AC_PINCTL_OUT_EN)) {
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005212 if (!(cap & AC_PINCAP_OUT))
5213 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
5214 else if ((val & AC_PINCTL_HP_EN) &&
5215 !(cap & AC_PINCAP_HP_DRV))
5216 val &= ~AC_PINCTL_HP_EN;
5217 }
Takashi Iwai6942c102012-04-20 13:08:40 +02005218 if (cap && (val & AC_PINCTL_IN_EN)) {
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005219 if (!(cap & AC_PINCAP_IN))
5220 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
5221 }
5222 }
5223 if (cached)
5224 return snd_hda_codec_update_cache(codec, pin, 0,
5225 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5226 else
5227 return snd_hda_codec_write(codec, pin, 0,
5228 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5229}
5230EXPORT_SYMBOL_HDA(_snd_hda_set_pin_ctl);
5231
Takashi Iwai990061c2010-09-09 22:08:44 +02005232/**
5233 * snd_hda_add_imux_item - Add an item to input_mux
5234 *
5235 * When the same label is used already in the existing items, the number
5236 * suffix is appended to the label. This label index number is stored
5237 * to type_idx when non-NULL pointer is given.
5238 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02005239int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
5240 int index, int *type_idx)
5241{
5242 int i, label_idx = 0;
5243 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
5244 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
5245 return -EINVAL;
5246 }
5247 for (i = 0; i < imux->num_items; i++) {
5248 if (!strncmp(label, imux->items[i].label, strlen(label)))
5249 label_idx++;
5250 }
5251 if (type_idx)
5252 *type_idx = label_idx;
5253 if (label_idx > 0)
5254 snprintf(imux->items[imux->num_items].label,
5255 sizeof(imux->items[imux->num_items].label),
5256 "%s %d", label, label_idx);
5257 else
5258 strlcpy(imux->items[imux->num_items].label, label,
5259 sizeof(imux->items[imux->num_items].label));
5260 imux->items[imux->num_items].index = index;
5261 imux->num_items++;
5262 return 0;
5263}
5264EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005265
Takashi Iwai4a471b72005-12-07 13:56:29 +01005266
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267#ifdef CONFIG_PM
5268/*
5269 * power management
5270 */
5271
5272/**
5273 * snd_hda_suspend - suspend the codecs
5274 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005275 *
5276 * Returns 0 if successful.
5277 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02005278int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005279{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005280 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005281
Takashi Iwai0ba21762007-04-16 11:29:14 +02005282 list_for_each_entry(codec, &bus->codec_list, list) {
David Henningsson26a6cb62012-10-09 15:04:21 +02005283 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005284 if (hda_codec_is_power_on(codec))
Dylan Reidd17344b2012-09-28 15:57:01 -07005285 hda_call_codec_suspend(codec, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005286 }
5287 return 0;
5288}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005289EXPORT_SYMBOL_HDA(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005290
5291/**
5292 * snd_hda_resume - resume the codecs
5293 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005294 *
5295 * Returns 0 if successful.
5296 */
5297int snd_hda_resume(struct hda_bus *bus)
5298{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005299 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005300
Takashi Iwai0ba21762007-04-16 11:29:14 +02005301 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai7f308302012-05-08 16:52:23 +02005302 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005303 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304 return 0;
5305}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005306EXPORT_SYMBOL_HDA(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005307#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005308
5309/*
5310 * generic arrays
5311 */
5312
Takashi Iwaid5191e52009-11-16 14:58:17 +01005313/**
5314 * snd_array_new - get a new element from the given array
5315 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005316 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01005317 * Get a new element from the given array. If it exceeds the
5318 * pre-allocated array size, re-allocate the array.
5319 *
5320 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02005321 */
5322void *snd_array_new(struct snd_array *array)
5323{
Takashi Iwai12f17712012-10-10 08:59:14 +02005324 if (snd_BUG_ON(!array->elem_size))
5325 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005326 if (array->used >= array->alloced) {
5327 int num = array->alloced + array->alloc_align;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005328 int size = (num + 1) * array->elem_size;
Takashi Iwai00ef9612011-07-14 15:57:27 +02005329 int oldsize = array->alloced * array->elem_size;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01005330 void *nlist;
5331 if (snd_BUG_ON(num >= 4096))
5332 return NULL;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005333 nlist = krealloc(array->list, size, GFP_KERNEL);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005334 if (!nlist)
5335 return NULL;
Takashi Iwai00ef9612011-07-14 15:57:27 +02005336 memset(nlist + oldsize, 0, size - oldsize);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005337 array->list = nlist;
5338 array->alloced = num;
5339 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01005340 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005341}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005342EXPORT_SYMBOL_HDA(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005343
Takashi Iwaid5191e52009-11-16 14:58:17 +01005344/**
5345 * snd_array_free - free the given array elements
5346 * @array: the array object
5347 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005348void snd_array_free(struct snd_array *array)
5349{
5350 kfree(array->list);
5351 array->used = 0;
5352 array->alloced = 0;
5353 array->list = NULL;
5354}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005355EXPORT_SYMBOL_HDA(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01005356
Takashi Iwaid5191e52009-11-16 14:58:17 +01005357/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01005358 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5359 * @pcm: PCM caps bits
5360 * @buf: the string buffer to write
5361 * @buflen: the max buffer length
5362 *
5363 * used by hda_proc.c and hda_eld.c
5364 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005365void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5366{
5367 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5368 int i, j;
5369
5370 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5371 if (pcm & (AC_SUPPCM_BITS_8 << i))
5372 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5373
5374 buf[j] = '\0'; /* necessary when j == 0 */
5375}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005376EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005377
5378MODULE_DESCRIPTION("HDA codec core");
5379MODULE_LICENSE("GPL");