blob: a8e7b00fe90d87aece5e63f8a35c36dcee4855b1 [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 Iwaicb53c622007-08-10 17:21:45 +020098static void hda_power_work(struct work_struct *work);
99static void hda_keep_power_on(struct hda_codec *codec);
Takashi Iwaie581f3d2011-07-26 10:19:20 +0200100#define hda_codec_is_power_on(codec) ((codec)->power_on)
Takashi Iwai68467f52012-08-28 09:14:29 -0700101static inline void hda_call_pm_notify(struct hda_bus *bus, bool power_up)
102{
103 if (bus->ops.pm_notify)
104 bus->ops.pm_notify(bus, power_up);
105}
Takashi Iwaicb53c622007-08-10 17:21:45 +0200106#else
107static inline void hda_keep_power_on(struct hda_codec *codec) {}
Takashi Iwaie581f3d2011-07-26 10:19:20 +0200108#define hda_codec_is_power_on(codec) 1
Takashi Iwai68467f52012-08-28 09:14:29 -0700109#define hda_call_pm_notify(bus, state) {}
Takashi Iwaicb53c622007-08-10 17:21:45 +0200110#endif
111
Takashi Iwaid5191e52009-11-16 14:58:17 +0100112/**
113 * snd_hda_get_jack_location - Give a location string of the jack
114 * @cfg: pin default config value
115 *
116 * Parse the pin default config value and returns the string of the
117 * jack location, e.g. "Rear", "Front", etc.
118 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400119const char *snd_hda_get_jack_location(u32 cfg)
120{
121 static char *bases[7] = {
122 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
123 };
124 static unsigned char specials_idx[] = {
125 0x07, 0x08,
126 0x17, 0x18, 0x19,
127 0x37, 0x38
128 };
129 static char *specials[] = {
130 "Rear Panel", "Drive Bar",
131 "Riser", "HDMI", "ATAPI",
132 "Mobile-In", "Mobile-Out"
133 };
134 int i;
135 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
136 if ((cfg & 0x0f) < 7)
137 return bases[cfg & 0x0f];
138 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
139 if (cfg == specials_idx[i])
140 return specials[i];
141 }
142 return "UNKNOWN";
143}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100144EXPORT_SYMBOL_HDA(snd_hda_get_jack_location);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400145
Takashi Iwaid5191e52009-11-16 14:58:17 +0100146/**
147 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
148 * @cfg: pin default config value
149 *
150 * Parse the pin default config value and returns the string of the
151 * jack connectivity, i.e. external or internal connection.
152 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400153const char *snd_hda_get_jack_connectivity(u32 cfg)
154{
155 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
156
157 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
158}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100159EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400160
Takashi Iwaid5191e52009-11-16 14:58:17 +0100161/**
162 * snd_hda_get_jack_type - Give a type string of the jack
163 * @cfg: pin default config value
164 *
165 * Parse the pin default config value and returns the string of the
166 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
167 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400168const char *snd_hda_get_jack_type(u32 cfg)
169{
170 static char *jack_types[16] = {
171 "Line Out", "Speaker", "HP Out", "CD",
172 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
173 "Line In", "Aux", "Mic", "Telephony",
174 "SPDIF In", "Digitial In", "Reserved", "Other"
175 };
176
177 return jack_types[(cfg & AC_DEFCFG_DEVICE)
178 >> AC_DEFCFG_DEVICE_SHIFT];
179}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100180EXPORT_SYMBOL_HDA(snd_hda_get_jack_type);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400181
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100182/*
183 * Compose a 32bit command word to be sent to the HD-audio controller
184 */
185static inline unsigned int
186make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int direct,
187 unsigned int verb, unsigned int parm)
188{
189 u32 val;
190
Takashi Iwai82e1b802009-07-17 12:47:34 +0200191 if ((codec->addr & ~0xf) || (direct & ~1) || (nid & ~0x7f) ||
192 (verb & ~0xfff) || (parm & ~0xffff)) {
Wu Fengguang6430aee2009-07-17 16:49:19 +0800193 printk(KERN_ERR "hda-codec: out of range cmd %x:%x:%x:%x:%x\n",
194 codec->addr, direct, nid, verb, parm);
195 return ~0;
196 }
197
198 val = (u32)codec->addr << 28;
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100199 val |= (u32)direct << 27;
200 val |= (u32)nid << 20;
201 val |= verb << 8;
202 val |= parm;
203 return val;
204}
205
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200206/*
207 * Send and receive a verb
208 */
209static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
210 unsigned int *res)
211{
212 struct hda_bus *bus = codec->bus;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200213 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200214
Wu Fengguang6430aee2009-07-17 16:49:19 +0800215 if (cmd == ~0)
216 return -1;
217
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200218 if (res)
219 *res = -1;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200220 again:
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200221 snd_hda_power_up(codec);
222 mutex_lock(&bus->cmd_mutex);
Takashi Iwaid66fee52011-08-02 15:39:31 +0200223 trace_hda_send_cmd(codec, cmd);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200224 err = bus->ops.command(bus, cmd);
Takashi Iwaid66fee52011-08-02 15:39:31 +0200225 if (!err && res) {
Wu Fengguangdeadff12009-08-01 18:45:16 +0800226 *res = bus->ops.get_response(bus, codec->addr);
Takashi Iwaid66fee52011-08-02 15:39:31 +0200227 trace_hda_get_response(codec, *res);
228 }
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200229 mutex_unlock(&bus->cmd_mutex);
230 snd_hda_power_down(codec);
Takashi Iwai8dd78332009-06-02 01:16:07 +0200231 if (res && *res == -1 && bus->rirb_error) {
232 if (bus->response_reset) {
233 snd_printd("hda_codec: resetting BUS due to "
234 "fatal communication error\n");
Takashi Iwaid66fee52011-08-02 15:39:31 +0200235 trace_hda_bus_reset(bus);
Takashi Iwai8dd78332009-06-02 01:16:07 +0200236 bus->ops.bus_reset(bus);
237 }
238 goto again;
239 }
240 /* clear reset-flag when the communication gets recovered */
241 if (!err)
242 bus->response_reset = 0;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200243 return err;
244}
245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246/**
247 * snd_hda_codec_read - send a command and get the response
248 * @codec: the HDA codec
249 * @nid: NID to send the command
250 * @direct: direct flag
251 * @verb: the verb to send
252 * @parm: the parameter for the verb
253 *
254 * Send a single command and read the corresponding response.
255 *
256 * Returns the obtained response value, or -1 for an error.
257 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200258unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
259 int direct,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 unsigned int verb, unsigned int parm)
261{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200262 unsigned cmd = make_codec_cmd(codec, nid, direct, verb, parm);
263 unsigned int res;
Greg Thelen9857edf2011-06-13 07:45:45 -0700264 if (codec_exec_verb(codec, cmd, &res))
265 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 return res;
267}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100268EXPORT_SYMBOL_HDA(snd_hda_codec_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270/**
271 * snd_hda_codec_write - send a single command without waiting for response
272 * @codec: the HDA codec
273 * @nid: NID to send the command
274 * @direct: direct flag
275 * @verb: the verb to send
276 * @parm: the parameter for the verb
277 *
278 * Send a single command without waiting for response.
279 *
280 * Returns 0 if successful, or a negative error code.
281 */
282int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
283 unsigned int verb, unsigned int parm)
284{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200285 unsigned int cmd = make_codec_cmd(codec, nid, direct, verb, parm);
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100286 unsigned int res;
Takashi Iwaib20f3b82009-06-02 01:20:22 +0200287 return codec_exec_verb(codec, cmd,
288 codec->bus->sync_write ? &res : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100290EXPORT_SYMBOL_HDA(snd_hda_codec_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292/**
293 * snd_hda_sequence_write - sequence writes
294 * @codec: the HDA codec
295 * @seq: VERB array to send
296 *
297 * Send the commands sequentially from the given array.
298 * The array must be terminated with NID=0.
299 */
300void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
301{
302 for (; seq->nid; seq++)
303 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
304}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100305EXPORT_SYMBOL_HDA(snd_hda_sequence_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
307/**
308 * snd_hda_get_sub_nodes - get the range of sub nodes
309 * @codec: the HDA codec
310 * @nid: NID to parse
311 * @start_id: the pointer to store the start NID
312 *
313 * Parse the NID and store the start NID of its sub-nodes.
314 * Returns the number of sub-nodes.
315 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200316int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
317 hda_nid_t *start_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
319 unsigned int parm;
320
321 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
Danny Tholene8a7f132007-09-11 21:41:56 +0200322 if (parm == -1)
323 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 *start_id = (parm >> 16) & 0x7fff;
325 return (int)(parm & 0x7fff);
326}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100327EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200329/* look up the cached results */
330static hda_nid_t *lookup_conn_list(struct snd_array *array, hda_nid_t nid)
331{
332 int i, len;
333 for (i = 0; i < array->used; ) {
334 hda_nid_t *p = snd_array_elem(array, i);
335 if (nid == *p)
336 return p;
337 len = p[1];
338 i += len + 2;
339 }
340 return NULL;
341}
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200342
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200343/* read the connection and add to the cache */
344static int read_and_add_raw_conns(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200345{
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200346 hda_nid_t list[HDA_MAX_CONNECTIONS];
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200347 int len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200348
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200349 len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list));
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200350 if (len < 0)
351 return len;
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200352 return snd_hda_override_conn_list(codec, nid, len, list);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200353}
Takashi Iwaidce20792011-06-24 14:10:28 +0200354
355/**
356 * snd_hda_get_connections - copy connection list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 * @codec: the HDA codec
358 * @nid: NID to parse
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200359 * @conn_list: connection list array; when NULL, checks only the size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 * @max_conns: max. number of connections to store
361 *
362 * Parses the connection list of the given widget and stores the list
363 * of NIDs.
364 *
365 * Returns the number of connections, or a negative error code.
366 */
367int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200368 hda_nid_t *conn_list, int max_conns)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200369{
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200370 struct snd_array *array = &codec->conn_lists;
371 int len;
372 hda_nid_t *p;
373 bool added = false;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200374
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200375 again:
376 mutex_lock(&codec->hash_mutex);
377 len = -1;
378 /* if the connection-list is already cached, read it */
379 p = lookup_conn_list(array, nid);
380 if (p) {
381 len = p[1];
382 if (conn_list && len > max_conns) {
383 snd_printk(KERN_ERR "hda_codec: "
384 "Too many connections %d for NID 0x%x\n",
385 len, nid);
386 mutex_unlock(&codec->hash_mutex);
387 return -EINVAL;
388 }
389 if (conn_list && len)
390 memcpy(conn_list, p + 2, len * sizeof(hda_nid_t));
Takashi Iwaidce20792011-06-24 14:10:28 +0200391 }
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200392 mutex_unlock(&codec->hash_mutex);
393 if (len >= 0)
394 return len;
395 if (snd_BUG_ON(added))
396 return -EINVAL;
397
398 len = read_and_add_raw_conns(codec, nid);
399 if (len < 0)
400 return len;
401 added = true;
402 goto again;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200403}
404EXPORT_SYMBOL_HDA(snd_hda_get_connections);
405
Takashi Iwai9e7717c2011-07-11 15:42:52 +0200406/**
407 * snd_hda_get_raw_connections - copy connection list without cache
408 * @codec: the HDA codec
409 * @nid: NID to parse
410 * @conn_list: connection list array
411 * @max_conns: max. number of connections to store
412 *
413 * Like snd_hda_get_connections(), copy the connection list but without
414 * checking through the connection-list cache.
415 * Currently called only from hda_proc.c, so not exported.
416 */
417int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid,
418 hda_nid_t *conn_list, int max_conns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
420 unsigned int parm;
Takashi Iwai54d17402005-11-21 16:33:22 +0100421 int i, conn_len, conns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 unsigned int shift, num_elems, mask;
Takashi Iwai1ba7a7c2009-07-27 12:56:26 +0200423 unsigned int wcaps;
Takashi Iwai54d17402005-11-21 16:33:22 +0100424 hda_nid_t prev_nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Takashi Iwaida3cec32008-08-08 17:12:14 +0200426 if (snd_BUG_ON(!conn_list || max_conns <= 0))
427 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Takashi Iwai1ba7a7c2009-07-27 12:56:26 +0200429 wcaps = get_wcaps(codec, nid);
430 if (!(wcaps & AC_WCAP_CONN_LIST) &&
Takashi Iwai8d087c72011-06-28 12:45:47 +0200431 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
432 return 0;
Jaroslav Kysela16a433d2009-07-22 16:20:40 +0200433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
435 if (parm & AC_CLIST_LONG) {
436 /* long form */
437 shift = 16;
438 num_elems = 2;
439 } else {
440 /* short form */
441 shift = 8;
442 num_elems = 4;
443 }
444 conn_len = parm & AC_CLIST_LENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 mask = (1 << (shift-1)) - 1;
446
Takashi Iwai0ba21762007-04-16 11:29:14 +0200447 if (!conn_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 return 0; /* no connection */
449
450 if (conn_len == 1) {
451 /* single connection */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200452 parm = snd_hda_codec_read(codec, nid, 0,
453 AC_VERB_GET_CONNECT_LIST, 0);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200454 if (parm == -1 && codec->bus->rirb_error)
455 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 conn_list[0] = parm & mask;
457 return 1;
458 }
459
460 /* multi connection */
461 conns = 0;
Takashi Iwai54d17402005-11-21 16:33:22 +0100462 prev_nid = 0;
463 for (i = 0; i < conn_len; i++) {
464 int range_val;
465 hda_nid_t val, n;
466
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200467 if (i % num_elems == 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +0100468 parm = snd_hda_codec_read(codec, nid, 0,
469 AC_VERB_GET_CONNECT_LIST, i);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200470 if (parm == -1 && codec->bus->rirb_error)
471 return -EIO;
472 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200473 range_val = !!(parm & (1 << (shift-1))); /* ranges */
Takashi Iwai54d17402005-11-21 16:33:22 +0100474 val = parm & mask;
Jaroslav Kysela2e9bf242009-07-18 11:48:19 +0200475 if (val == 0) {
476 snd_printk(KERN_WARNING "hda_codec: "
477 "invalid CONNECT_LIST verb %x[%i]:%x\n",
478 nid, i, parm);
479 return 0;
480 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100481 parm >>= shift;
482 if (range_val) {
483 /* ranges between the previous and this one */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200484 if (!prev_nid || prev_nid >= val) {
485 snd_printk(KERN_WARNING "hda_codec: "
486 "invalid dep_range_val %x:%x\n",
487 prev_nid, val);
Takashi Iwai54d17402005-11-21 16:33:22 +0100488 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100490 for (n = prev_nid + 1; n <= val; n++) {
491 if (conns >= max_conns) {
Takashi Iwai5aacc212010-07-30 10:36:29 +0200492 snd_printk(KERN_ERR "hda_codec: "
493 "Too many connections %d for NID 0x%x\n",
494 conns, nid);
Takashi Iwai54d17402005-11-21 16:33:22 +0100495 return -EINVAL;
496 }
497 conn_list[conns++] = n;
498 }
499 } else {
500 if (conns >= max_conns) {
Takashi Iwai5aacc212010-07-30 10:36:29 +0200501 snd_printk(KERN_ERR "hda_codec: "
502 "Too many connections %d for NID 0x%x\n",
503 conns, nid);
Takashi Iwai54d17402005-11-21 16:33:22 +0100504 return -EINVAL;
505 }
506 conn_list[conns++] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100508 prev_nid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 }
510 return conns;
511}
512
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200513static bool add_conn_list(struct snd_array *array, hda_nid_t nid)
514{
515 hda_nid_t *p = snd_array_new(array);
516 if (!p)
517 return false;
518 *p = nid;
519 return true;
520}
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522/**
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200523 * snd_hda_override_conn_list - add/modify the connection-list to cache
524 * @codec: the HDA codec
525 * @nid: NID to parse
526 * @len: number of connection list entries
527 * @list: the list of connection entries
528 *
529 * Add or modify the given connection-list to the cache. If the corresponding
530 * cache already exists, invalidate it and append a new one.
531 *
532 * Returns zero or a negative error code.
533 */
534int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
535 const hda_nid_t *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536{
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200537 struct snd_array *array = &codec->conn_lists;
538 hda_nid_t *p;
539 int i, old_used;
540
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200541 mutex_lock(&codec->hash_mutex);
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200542 p = lookup_conn_list(array, nid);
543 if (p)
544 *p = -1; /* invalidate the old entry */
545
546 old_used = array->used;
547 if (!add_conn_list(array, nid) || !add_conn_list(array, len))
548 goto error_add;
549 for (i = 0; i < len; i++)
550 if (!add_conn_list(array, list[i]))
551 goto error_add;
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200552 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200553 return 0;
554
555 error_add:
556 array->used = old_used;
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200557 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200558 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559}
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200560EXPORT_SYMBOL_HDA(snd_hda_override_conn_list);
561
562/**
Takashi Iwai8d087c72011-06-28 12:45:47 +0200563 * snd_hda_get_conn_index - get the connection index of the given NID
564 * @codec: the HDA codec
565 * @mux: NID containing the list
566 * @nid: NID to select
567 * @recursive: 1 when searching NID recursively, otherwise 0
568 *
569 * Parses the connection list of the widget @mux and checks whether the
570 * widget @nid is present. If it is, return the connection index.
571 * Otherwise it returns -1.
572 */
573int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
574 hda_nid_t nid, int recursive)
575{
576 hda_nid_t conn[HDA_MAX_NUM_INPUTS];
577 int i, nums;
578
579 nums = snd_hda_get_connections(codec, mux, conn, ARRAY_SIZE(conn));
580 for (i = 0; i < nums; i++)
581 if (conn[i] == nid)
582 return i;
583 if (!recursive)
584 return -1;
585 if (recursive > 5) {
586 snd_printd("hda_codec: too deep connection for 0x%x\n", nid);
587 return -1;
588 }
589 recursive++;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200590 for (i = 0; i < nums; i++) {
591 unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
592 if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
593 continue;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200594 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
595 return i;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200596 }
Takashi Iwai8d087c72011-06-28 12:45:47 +0200597 return -1;
598}
599EXPORT_SYMBOL_HDA(snd_hda_get_conn_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601/**
602 * snd_hda_queue_unsol_event - add an unsolicited event to queue
603 * @bus: the BUS
604 * @res: unsolicited event (lower 32bit of RIRB entry)
605 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
606 *
607 * Adds the given event to the queue. The events are processed in
608 * the workqueue asynchronously. Call this function in the interrupt
609 * hanlder when RIRB receives an unsolicited event.
610 *
611 * Returns 0 if successful, or a negative error code.
612 */
613int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
614{
615 struct hda_bus_unsolicited *unsol;
616 unsigned int wp;
617
Takashi Iwaiecf726f2011-08-09 14:22:44 +0200618 trace_hda_unsol_event(bus, res, res_ex);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200619 unsol = bus->unsol;
620 if (!unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 return 0;
622
623 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
624 unsol->wp = wp;
625
626 wp <<= 1;
627 unsol->queue[wp] = res;
628 unsol->queue[wp + 1] = res_ex;
629
Takashi Iwai6acaed32009-01-12 10:09:24 +0100630 queue_work(bus->workq, &unsol->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
632 return 0;
633}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100634EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
636/*
Wu Fengguang5c1d1a92008-10-07 14:17:53 +0800637 * process queued unsolicited events
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 */
David Howellsc4028952006-11-22 14:57:56 +0000639static void process_unsol_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
David Howellsc4028952006-11-22 14:57:56 +0000641 struct hda_bus_unsolicited *unsol =
642 container_of(work, struct hda_bus_unsolicited, work);
643 struct hda_bus *bus = unsol->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 struct hda_codec *codec;
645 unsigned int rp, caddr, res;
646
647 while (unsol->rp != unsol->wp) {
648 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
649 unsol->rp = rp;
650 rp <<= 1;
651 res = unsol->queue[rp];
652 caddr = unsol->queue[rp + 1];
Takashi Iwai0ba21762007-04-16 11:29:14 +0200653 if (!(caddr & (1 << 4))) /* no unsolicited event? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 continue;
655 codec = bus->caddr_tbl[caddr & 0x0f];
656 if (codec && codec->patch_ops.unsol_event)
657 codec->patch_ops.unsol_event(codec, res);
658 }
659}
660
661/*
662 * initialize unsolicited queue
663 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200664static int init_unsol_queue(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
666 struct hda_bus_unsolicited *unsol;
667
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100668 if (bus->unsol) /* already initialized */
669 return 0;
670
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200671 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200672 if (!unsol) {
673 snd_printk(KERN_ERR "hda_codec: "
674 "can't allocate unsolicited queue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 return -ENOMEM;
676 }
David Howellsc4028952006-11-22 14:57:56 +0000677 INIT_WORK(&unsol->work, process_unsol_events);
678 unsol->bus = bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 bus->unsol = unsol;
680 return 0;
681}
682
683/*
684 * destructor
685 */
686static void snd_hda_codec_free(struct hda_codec *codec);
687
688static int snd_hda_bus_free(struct hda_bus *bus)
689{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200690 struct hda_codec *codec, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Takashi Iwai0ba21762007-04-16 11:29:14 +0200692 if (!bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 return 0;
Takashi Iwai6acaed32009-01-12 10:09:24 +0100694 if (bus->workq)
695 flush_workqueue(bus->workq);
696 if (bus->unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 kfree(bus->unsol);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200698 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 snd_hda_codec_free(codec);
700 }
701 if (bus->ops.private_free)
702 bus->ops.private_free(bus);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100703 if (bus->workq)
704 destroy_workqueue(bus->workq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 kfree(bus);
706 return 0;
707}
708
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100709static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
711 struct hda_bus *bus = device->device_data;
Takashi Iwaib94d35392008-11-21 09:08:06 +0100712 bus->shutdown = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 return snd_hda_bus_free(bus);
714}
715
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200716#ifdef CONFIG_SND_HDA_HWDEP
717static int snd_hda_bus_dev_register(struct snd_device *device)
718{
719 struct hda_bus *bus = device->device_data;
720 struct hda_codec *codec;
721 list_for_each_entry(codec, &bus->codec_list, list) {
722 snd_hda_hwdep_add_sysfs(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +0100723 snd_hda_hwdep_add_power_sysfs(codec);
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200724 }
725 return 0;
726}
727#else
728#define snd_hda_bus_dev_register NULL
729#endif
730
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731/**
732 * snd_hda_bus_new - create a HDA bus
733 * @card: the card entry
734 * @temp: the template for hda_bus information
735 * @busp: the pointer to store the created bus instance
736 *
737 * Returns 0 if successful, or a negative error code.
738 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100739int /*__devinit*/ snd_hda_bus_new(struct snd_card *card,
Takashi Iwai756e2b02007-04-16 11:27:07 +0200740 const struct hda_bus_template *temp,
741 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742{
743 struct hda_bus *bus;
744 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100745 static struct snd_device_ops dev_ops = {
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200746 .dev_register = snd_hda_bus_dev_register,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 .dev_free = snd_hda_bus_dev_free,
748 };
749
Takashi Iwaida3cec32008-08-08 17:12:14 +0200750 if (snd_BUG_ON(!temp))
751 return -EINVAL;
752 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
753 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
755 if (busp)
756 *busp = NULL;
757
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200758 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 if (bus == NULL) {
760 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
761 return -ENOMEM;
762 }
763
764 bus->card = card;
765 bus->private_data = temp->private_data;
766 bus->pci = temp->pci;
767 bus->modelname = temp->modelname;
Takashi Iwaifee2fba2008-11-27 12:43:28 +0100768 bus->power_save = temp->power_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 bus->ops = temp->ops;
770
Ingo Molnar62932df2006-01-16 16:34:20 +0100771 mutex_init(&bus->cmd_mutex);
Takashi Iwai3f50ac62010-08-20 09:44:36 +0200772 mutex_init(&bus->prepare_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 INIT_LIST_HEAD(&bus->codec_list);
774
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100775 snprintf(bus->workq_name, sizeof(bus->workq_name),
776 "hd-audio%d", card->number);
777 bus->workq = create_singlethread_workqueue(bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100778 if (!bus->workq) {
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100779 snd_printk(KERN_ERR "cannot create workqueue %s\n",
780 bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100781 kfree(bus);
782 return -ENOMEM;
783 }
784
Takashi Iwai0ba21762007-04-16 11:29:14 +0200785 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
786 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 snd_hda_bus_free(bus);
788 return err;
789 }
790 if (busp)
791 *busp = bus;
792 return 0;
793}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100794EXPORT_SYMBOL_HDA(snd_hda_bus_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Takashi Iwai82467612007-07-27 19:15:54 +0200796#ifdef CONFIG_SND_HDA_GENERIC
797#define is_generic_config(codec) \
Takashi Iwaif44ac832008-07-30 15:01:45 +0200798 (codec->modelname && !strcmp(codec->modelname, "generic"))
Takashi Iwai82467612007-07-27 19:15:54 +0200799#else
800#define is_generic_config(codec) 0
801#endif
802
Takashi Iwai645f10c2008-11-28 15:07:37 +0100803#ifdef MODULE
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100804#define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
805#else
Takashi Iwai645f10c2008-11-28 15:07:37 +0100806#define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100807#endif
808
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809/*
810 * find a matching codec preset
811 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200812static const struct hda_codec_preset *
Takashi Iwai756e2b02007-04-16 11:27:07 +0200813find_codec_preset(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814{
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100815 struct hda_codec_preset_list *tbl;
816 const struct hda_codec_preset *preset;
Takashi Iwai5d908ab2012-08-24 18:40:10 +0200817 unsigned int mod_requested = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Takashi Iwai82467612007-07-27 19:15:54 +0200819 if (is_generic_config(codec))
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100820 return NULL; /* use the generic parser */
821
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100822 again:
823 mutex_lock(&preset_mutex);
824 list_for_each_entry(tbl, &hda_preset_tables, list) {
825 if (!try_module_get(tbl->owner)) {
826 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
827 continue;
828 }
829 for (preset = tbl->preset; preset->id; preset++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 u32 mask = preset->mask;
Marc Boucherca7cfae2008-01-22 15:32:25 +0100831 if (preset->afg && preset->afg != codec->afg)
832 continue;
833 if (preset->mfg && preset->mfg != codec->mfg)
834 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200835 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 mask = ~0;
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200837 if (preset->id == (codec->vendor_id & mask) &&
Takashi Iwai0ba21762007-04-16 11:29:14 +0200838 (!preset->rev ||
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100839 preset->rev == codec->revision_id)) {
840 mutex_unlock(&preset_mutex);
841 codec->owner = tbl->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 return preset;
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100843 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 }
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100845 module_put(tbl->owner);
846 }
847 mutex_unlock(&preset_mutex);
848
849 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
850 char name[32];
851 if (!mod_requested)
852 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
853 codec->vendor_id);
854 else
855 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
856 (codec->vendor_id >> 16) & 0xffff);
857 request_module(name);
858 mod_requested++;
859 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 }
861 return NULL;
862}
863
864/*
Takashi Iwaif44ac832008-07-30 15:01:45 +0200865 * get_codec_name - store the codec name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 */
Takashi Iwaif44ac832008-07-30 15:01:45 +0200867static int get_codec_name(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868{
869 const struct hda_vendor_id *c;
870 const char *vendor = NULL;
871 u16 vendor_id = codec->vendor_id >> 16;
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200872 char tmp[16];
873
874 if (codec->vendor_name)
875 goto get_chip_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
877 for (c = hda_vendor_ids; c->id; c++) {
878 if (c->id == vendor_id) {
879 vendor = c->name;
880 break;
881 }
882 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200883 if (!vendor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 sprintf(tmp, "Generic %04x", vendor_id);
885 vendor = tmp;
886 }
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200887 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
888 if (!codec->vendor_name)
889 return -ENOMEM;
890
891 get_chip_name:
892 if (codec->chip_name)
893 return 0;
894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 if (codec->preset && codec->preset->name)
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200896 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
897 else {
898 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
899 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
900 }
901 if (!codec->chip_name)
Takashi Iwaif44ac832008-07-30 15:01:45 +0200902 return -ENOMEM;
903 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904}
905
906/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200907 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100909static void /*__devinit*/ setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910{
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200911 int i, total_nodes, function_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 hda_nid_t nid;
913
914 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
915 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200916 function_id = snd_hda_param_read(codec, nid,
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200917 AC_PAR_FUNCTION_TYPE);
Jaroslav Kyselacd7643b2010-07-20 12:11:25 +0200918 switch (function_id & 0xff) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200919 case AC_GRP_AUDIO_FUNCTION:
920 codec->afg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200921 codec->afg_function_id = function_id & 0xff;
922 codec->afg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200923 break;
924 case AC_GRP_MODEM_FUNCTION:
925 codec->mfg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200926 codec->mfg_function_id = function_id & 0xff;
927 codec->mfg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200928 break;
929 default:
930 break;
931 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933}
934
935/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100936 * read widget caps for each widget and store in cache
937 */
938static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
939{
940 int i;
941 hda_nid_t nid;
942
943 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
944 &codec->start_nid);
945 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200946 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +0100947 return -ENOMEM;
948 nid = codec->start_nid;
949 for (i = 0; i < codec->num_nodes; i++, nid++)
950 codec->wcaps[i] = snd_hda_param_read(codec, nid,
951 AC_PAR_AUDIO_WIDGET_CAP);
952 return 0;
953}
954
Takashi Iwai3be14142009-02-20 14:11:16 +0100955/* read all pin default configurations and save codec->init_pins */
956static int read_pin_defaults(struct hda_codec *codec)
957{
958 int i;
959 hda_nid_t nid = codec->start_nid;
960
961 for (i = 0; i < codec->num_nodes; i++, nid++) {
962 struct hda_pincfg *pin;
963 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +0200964 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwai3be14142009-02-20 14:11:16 +0100965 if (wid_type != AC_WID_PIN)
966 continue;
967 pin = snd_array_new(&codec->init_pins);
968 if (!pin)
969 return -ENOMEM;
970 pin->nid = nid;
971 pin->cfg = snd_hda_codec_read(codec, nid, 0,
972 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200973 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
974 AC_VERB_GET_PIN_WIDGET_CONTROL,
975 0);
Takashi Iwai3be14142009-02-20 14:11:16 +0100976 }
977 return 0;
978}
979
980/* look up the given pin config list and return the item matching with NID */
981static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
982 struct snd_array *array,
983 hda_nid_t nid)
984{
985 int i;
986 for (i = 0; i < array->used; i++) {
987 struct hda_pincfg *pin = snd_array_elem(array, i);
988 if (pin->nid == nid)
989 return pin;
990 }
991 return NULL;
992}
993
994/* write a config value for the given NID */
995static void set_pincfg(struct hda_codec *codec, hda_nid_t nid,
996 unsigned int cfg)
997{
998 int i;
999 for (i = 0; i < 4; i++) {
1000 snd_hda_codec_write(codec, nid, 0,
1001 AC_VERB_SET_CONFIG_DEFAULT_BYTES_0 + i,
1002 cfg & 0xff);
1003 cfg >>= 8;
1004 }
1005}
1006
1007/* set the current pin config value for the given NID.
1008 * the value is cached, and read via snd_hda_codec_get_pincfg()
1009 */
1010int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
1011 hda_nid_t nid, unsigned int cfg)
1012{
1013 struct hda_pincfg *pin;
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001014 unsigned int oldcfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001015
Takashi Iwaib82855a2009-12-27 11:24:56 +01001016 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
1017 return -EINVAL;
1018
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001019 oldcfg = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +01001020 pin = look_up_pincfg(codec, list, nid);
1021 if (!pin) {
1022 pin = snd_array_new(list);
1023 if (!pin)
1024 return -ENOMEM;
1025 pin->nid = nid;
1026 }
1027 pin->cfg = cfg;
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001028
1029 /* change only when needed; e.g. if the pincfg is already present
1030 * in user_pins[], don't write it
1031 */
1032 cfg = snd_hda_codec_get_pincfg(codec, nid);
1033 if (oldcfg != cfg)
1034 set_pincfg(codec, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001035 return 0;
1036}
1037
Takashi Iwaid5191e52009-11-16 14:58:17 +01001038/**
1039 * snd_hda_codec_set_pincfg - Override a pin default configuration
1040 * @codec: the HDA codec
1041 * @nid: NID to set the pin config
1042 * @cfg: the pin default config value
1043 *
1044 * Override a pin default configuration value in the cache.
1045 * This value can be read by snd_hda_codec_get_pincfg() in a higher
1046 * priority than the real hardware value.
1047 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001048int snd_hda_codec_set_pincfg(struct hda_codec *codec,
1049 hda_nid_t nid, unsigned int cfg)
1050{
Takashi Iwai346ff702009-02-23 09:42:57 +01001051 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001052}
1053EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
1054
Takashi Iwaid5191e52009-11-16 14:58:17 +01001055/**
1056 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
1057 * @codec: the HDA codec
1058 * @nid: NID to get the pin config
1059 *
1060 * Get the current pin config value of the given pin NID.
1061 * If the pincfg value is cached or overridden via sysfs or driver,
1062 * returns the cached value.
1063 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001064unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
1065{
1066 struct hda_pincfg *pin;
1067
Takashi Iwai3be14142009-02-20 14:11:16 +01001068#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +01001069 pin = look_up_pincfg(codec, &codec->user_pins, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +01001070 if (pin)
1071 return pin->cfg;
1072#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001073 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
1074 if (pin)
1075 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001076 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1077 if (pin)
1078 return pin->cfg;
1079 return 0;
1080}
1081EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
1082
1083/* restore all current pin configs */
1084static void restore_pincfgs(struct hda_codec *codec)
1085{
1086 int i;
1087 for (i = 0; i < codec->init_pins.used; i++) {
1088 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1089 set_pincfg(codec, pin->nid,
1090 snd_hda_codec_get_pincfg(codec, pin->nid));
1091 }
1092}
Takashi Iwai54d17402005-11-21 16:33:22 +01001093
Takashi Iwai92ee6162009-12-27 11:18:59 +01001094/**
1095 * snd_hda_shutup_pins - Shut up all pins
1096 * @codec: the HDA codec
1097 *
1098 * Clear all pin controls to shup up before suspend for avoiding click noise.
1099 * The controls aren't cached so that they can be resumed properly.
1100 */
1101void snd_hda_shutup_pins(struct hda_codec *codec)
1102{
1103 int i;
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001104 /* don't shut up pins when unloading the driver; otherwise it breaks
1105 * the default pin setup at the next load of the driver
1106 */
1107 if (codec->bus->shutdown)
1108 return;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001109 for (i = 0; i < codec->init_pins.used; i++) {
1110 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1111 /* use read here for syncing after issuing each verb */
1112 snd_hda_codec_read(codec, pin->nid, 0,
1113 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
1114 }
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001115 codec->pins_shutup = 1;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001116}
1117EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);
1118
Takashi Iwai2a439522011-07-26 09:52:50 +02001119#ifdef CONFIG_PM
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001120/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1121static void restore_shutup_pins(struct hda_codec *codec)
1122{
1123 int i;
1124 if (!codec->pins_shutup)
1125 return;
1126 if (codec->bus->shutdown)
1127 return;
1128 for (i = 0; i < codec->init_pins.used; i++) {
1129 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1130 snd_hda_codec_write(codec, pin->nid, 0,
1131 AC_VERB_SET_PIN_WIDGET_CONTROL,
1132 pin->ctrl);
1133 }
1134 codec->pins_shutup = 0;
1135}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001136#endif
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001137
David Henningsson26a6cb62012-10-09 15:04:21 +02001138static void hda_jackpoll_work(struct work_struct *work)
1139{
1140 struct hda_codec *codec =
1141 container_of(work, struct hda_codec, jackpoll_work.work);
1142 if (!codec->jackpoll_interval)
1143 return;
1144
1145 snd_hda_jack_set_dirty_all(codec);
1146 snd_hda_jack_poll_all(codec);
1147 queue_delayed_work(codec->bus->workq, &codec->jackpoll_work,
1148 codec->jackpoll_interval);
1149}
1150
Takashi Iwai01751f52007-08-10 16:59:39 +02001151static void init_hda_cache(struct hda_cache_rec *cache,
1152 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001153static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +02001154
Takashi Iwai3be14142009-02-20 14:11:16 +01001155/* restore the initial pin cfgs and release all pincfg lists */
1156static void restore_init_pincfgs(struct hda_codec *codec)
1157{
Takashi Iwai346ff702009-02-23 09:42:57 +01001158 /* first free driver_pins and user_pins, then call restore_pincfg
Takashi Iwai3be14142009-02-20 14:11:16 +01001159 * so that only the values in init_pins are restored
1160 */
Takashi Iwai346ff702009-02-23 09:42:57 +01001161 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001162#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +01001163 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001164#endif
1165 restore_pincfgs(codec);
1166 snd_array_free(&codec->init_pins);
1167}
1168
Takashi Iwai54d17402005-11-21 16:33:22 +01001169/*
Takashi Iwaieb541332010-08-06 13:48:11 +02001170 * audio-converter setup caches
1171 */
1172struct hda_cvt_setup {
1173 hda_nid_t nid;
1174 u8 stream_tag;
1175 u8 channel_id;
1176 u16 format_id;
1177 unsigned char active; /* cvt is currently used */
1178 unsigned char dirty; /* setups should be cleared */
1179};
1180
1181/* get or create a cache entry for the given audio converter NID */
1182static struct hda_cvt_setup *
1183get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1184{
1185 struct hda_cvt_setup *p;
1186 int i;
1187
1188 for (i = 0; i < codec->cvt_setups.used; i++) {
1189 p = snd_array_elem(&codec->cvt_setups, i);
1190 if (p->nid == nid)
1191 return p;
1192 }
1193 p = snd_array_new(&codec->cvt_setups);
1194 if (p)
1195 p->nid = nid;
1196 return p;
1197}
1198
1199/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 * codec destructor
1201 */
1202static void snd_hda_codec_free(struct hda_codec *codec)
1203{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001204 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 return;
David Henningsson26a6cb62012-10-09 15:04:21 +02001206 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai59cad162012-06-26 15:00:20 +02001207 snd_hda_jack_tbl_clear(codec);
Takashi Iwai3be14142009-02-20 14:11:16 +01001208 restore_init_pincfgs(codec);
Takashi Iwai83012a72012-08-24 18:38:08 +02001209#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02001210 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01001211 flush_workqueue(codec->bus->workq);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001212#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001214 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001215 snd_array_free(&codec->nids);
Takashi Iwai59cad162012-06-26 15:00:20 +02001216 snd_array_free(&codec->cvt_setups);
Takashi Iwaia12d3e12011-04-07 15:55:15 +02001217 snd_array_free(&codec->conn_lists);
Stephen Warren7c935972011-06-01 11:14:17 -06001218 snd_array_free(&codec->spdif_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 codec->bus->caddr_tbl[codec->addr] = NULL;
1220 if (codec->patch_ops.free)
1221 codec->patch_ops.free(codec);
Takashi Iwai83012a72012-08-24 18:38:08 +02001222#ifdef CONFIG_PM
Takashi Iwai08fa20a2012-08-31 07:46:56 -07001223 if (!codec->pm_down_notified) /* cancel leftover refcounts */
Takashi Iwai68467f52012-08-28 09:14:29 -07001224 hda_call_pm_notify(codec->bus, false);
1225#endif
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001226 module_put(codec->owner);
Takashi Iwai01751f52007-08-10 16:59:39 +02001227 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001228 free_hda_cache(&codec->cmd_cache);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001229 kfree(codec->vendor_name);
1230 kfree(codec->chip_name);
Takashi Iwaif44ac832008-07-30 15:01:45 +02001231 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +01001232 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 kfree(codec);
1234}
1235
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001236static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec,
1237 hda_nid_t fg, unsigned int power_state);
1238
Takashi Iwaid8193872012-08-31 07:54:38 -07001239static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001240 unsigned int power_state);
1241
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242/**
1243 * snd_hda_codec_new - create a HDA codec
1244 * @bus: the bus to assign
1245 * @codec_addr: the codec address
1246 * @codecp: the pointer to store the generated codec
1247 *
1248 * Returns 0 if successful, or a negative error code.
1249 */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001250int /*__devinit*/ snd_hda_codec_new(struct hda_bus *bus,
1251 unsigned int codec_addr,
1252 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253{
1254 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +02001255 char component[31];
Takashi Iwaid8193872012-08-31 07:54:38 -07001256 hda_nid_t fg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 int err;
1258
Takashi Iwaida3cec32008-08-08 17:12:14 +02001259 if (snd_BUG_ON(!bus))
1260 return -EINVAL;
1261 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1262 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
1264 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001265 snd_printk(KERN_ERR "hda_codec: "
1266 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 return -EBUSY;
1268 }
1269
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001270 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 if (codec == NULL) {
1272 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1273 return -ENOMEM;
1274 }
1275
1276 codec->bus = bus;
1277 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001278 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001279 mutex_init(&codec->control_mutex);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001280 mutex_init(&codec->hash_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +02001281 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001282 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001283 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1284 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001285 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001286 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +02001287 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Takashi Iwaia12d3e12011-04-07 15:55:15 +02001288 snd_array_init(&codec->conn_lists, sizeof(hda_nid_t), 64);
Stephen Warren7c935972011-06-01 11:14:17 -06001289 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
David Henningsson26a6cb62012-10-09 15:04:21 +02001290 INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
Takashi Iwai83012a72012-08-24 18:38:08 +02001292#ifdef CONFIG_PM
Takashi Iwai5536c6d2012-05-08 17:08:10 +02001293 spin_lock_init(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001294 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1295 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1296 * the caller has to power down appropriatley after initialization
1297 * phase.
1298 */
1299 hda_keep_power_on(codec);
Takashi Iwai68467f52012-08-28 09:14:29 -07001300 hda_call_pm_notify(bus, true);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001301#endif
1302
Takashi Iwaic382a9f2012-05-07 15:01:02 +02001303 if (codec->bus->modelname) {
1304 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1305 if (!codec->modelname) {
1306 snd_hda_codec_free(codec);
1307 return -ENODEV;
1308 }
1309 }
1310
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 list_add_tail(&codec->list, &bus->codec_list);
1312 bus->caddr_tbl[codec_addr] = codec;
1313
Takashi Iwai0ba21762007-04-16 11:29:14 +02001314 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1315 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001316 if (codec->vendor_id == -1)
1317 /* read again, hopefully the access method was corrected
1318 * in the last read...
1319 */
1320 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1321 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001322 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1323 AC_PAR_SUBSYSTEM_ID);
1324 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1325 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001327 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001328 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001329 snd_printdd("hda_codec: no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001330 err = -ENODEV;
1331 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 }
1333
Takashi Iwaid8193872012-08-31 07:54:38 -07001334 fg = codec->afg ? codec->afg : codec->mfg;
1335 err = read_widget_caps(codec, fg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001336 if (err < 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +01001337 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001338 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001339 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001340 err = read_pin_defaults(codec);
1341 if (err < 0)
1342 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001343
Takashi Iwai0ba21762007-04-16 11:29:14 +02001344 if (!codec->subsystem_id) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001345 codec->subsystem_id =
Takashi Iwaid8193872012-08-31 07:54:38 -07001346 snd_hda_codec_read(codec, fg, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001347 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001348 }
1349
Takashi Iwai83012a72012-08-24 18:38:08 +02001350#ifdef CONFIG_PM
Takashi Iwaid8193872012-08-31 07:54:38 -07001351 codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec, fg,
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001352 AC_PWRST_CLKSTOP);
1353 if (!codec->d3_stop_clk)
1354 bus->power_keep_link_on = 1;
Mengdong Lin5d6147f2012-08-24 12:06:30 +08001355#endif
Takashi Iwaid8193872012-08-31 07:54:38 -07001356 codec->epss = snd_hda_codec_get_supported_ps(codec, fg,
Takashi Iwai983f6b92012-08-28 09:18:01 -07001357 AC_PWRST_EPSS);
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001358
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001359 /* power-up all before initialization */
Takashi Iwaid8193872012-08-31 07:54:38 -07001360 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001361
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001362 snd_hda_codec_proc_new(codec);
1363
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001364 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001365
1366 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1367 codec->subsystem_id, codec->revision_id);
1368 snd_component_add(codec->bus->card, component);
1369
1370 if (codecp)
1371 *codecp = codec;
1372 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001373
1374 error:
1375 snd_hda_codec_free(codec);
1376 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001377}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001378EXPORT_SYMBOL_HDA(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001379
Takashi Iwaid5191e52009-11-16 14:58:17 +01001380/**
1381 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1382 * @codec: the HDA codec
1383 *
1384 * Start parsing of the given codec tree and (re-)initialize the whole
1385 * patch instance.
1386 *
1387 * Returns 0 if successful or a negative error code.
1388 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001389int snd_hda_codec_configure(struct hda_codec *codec)
1390{
1391 int err;
1392
Takashi Iwaid5ad6302007-03-07 15:55:59 +01001393 codec->preset = find_codec_preset(codec);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001394 if (!codec->vendor_name || !codec->chip_name) {
Takashi Iwaif44ac832008-07-30 15:01:45 +02001395 err = get_codec_name(codec);
1396 if (err < 0)
1397 return err;
1398 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
Takashi Iwai82467612007-07-27 19:15:54 +02001400 if (is_generic_config(codec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai82467612007-07-27 19:15:54 +02001402 goto patched;
1403 }
Takashi Iwai82467612007-07-27 19:15:54 +02001404 if (codec->preset && codec->preset->patch) {
1405 err = codec->preset->patch(codec);
1406 goto patched;
1407 }
1408
1409 /* call the default parser */
Takashi Iwai82467612007-07-27 19:15:54 +02001410 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai35a1e0c2007-10-19 08:13:40 +02001411 if (err < 0)
1412 printk(KERN_ERR "hda-codec: No codec parser is available\n");
Takashi Iwai82467612007-07-27 19:15:54 +02001413
1414 patched:
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001415 if (!err && codec->patch_ops.unsol_event)
1416 err = init_unsol_queue(codec->bus);
Takashi Iwaif62faed2009-12-23 09:27:51 +01001417 /* audio codec should override the mixer name */
1418 if (!err && (codec->afg || !*codec->bus->card->mixername))
1419 snprintf(codec->bus->card->mixername,
1420 sizeof(codec->bus->card->mixername),
1421 "%s %s", codec->vendor_name, codec->chip_name);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001422 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423}
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001424EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
Takashi Iwaied360812012-08-08 17:12:52 +02001426/* update the stream-id if changed */
1427static void update_pcm_stream_id(struct hda_codec *codec,
1428 struct hda_cvt_setup *p, hda_nid_t nid,
1429 u32 stream_tag, int channel_id)
1430{
1431 unsigned int oldval, newval;
1432
1433 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1434 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1435 newval = (stream_tag << 4) | channel_id;
1436 if (oldval != newval)
1437 snd_hda_codec_write(codec, nid, 0,
1438 AC_VERB_SET_CHANNEL_STREAMID,
1439 newval);
1440 p->stream_tag = stream_tag;
1441 p->channel_id = channel_id;
1442 }
1443}
1444
1445/* update the format-id if changed */
1446static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1447 hda_nid_t nid, int format)
1448{
1449 unsigned int oldval;
1450
1451 if (p->format_id != format) {
1452 oldval = snd_hda_codec_read(codec, nid, 0,
1453 AC_VERB_GET_STREAM_FORMAT, 0);
1454 if (oldval != format) {
1455 msleep(1);
1456 snd_hda_codec_write(codec, nid, 0,
1457 AC_VERB_SET_STREAM_FORMAT,
1458 format);
1459 }
1460 p->format_id = format;
1461 }
1462}
1463
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464/**
1465 * snd_hda_codec_setup_stream - set up the codec for streaming
1466 * @codec: the CODEC to set up
1467 * @nid: the NID to set up
1468 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1469 * @channel_id: channel id to pass, zero based.
1470 * @format: stream format.
1471 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001472void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1473 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 int channel_id, int format)
1475{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001476 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001477 struct hda_cvt_setup *p;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001478 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001479 int i;
1480
Takashi Iwai0ba21762007-04-16 11:29:14 +02001481 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001482 return;
1483
Takashi Iwai0ba21762007-04-16 11:29:14 +02001484 snd_printdd("hda_codec_setup_stream: "
1485 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001487 p = get_hda_cvt_setup(codec, nid);
1488 if (!p)
1489 return;
Takashi Iwaied360812012-08-08 17:12:52 +02001490
1491 if (codec->pcm_format_first)
1492 update_pcm_format(codec, p, nid, format);
1493 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1494 if (!codec->pcm_format_first)
1495 update_pcm_format(codec, p, nid, format);
1496
Takashi Iwaieb541332010-08-06 13:48:11 +02001497 p->active = 1;
1498 p->dirty = 0;
1499
1500 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001501 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001502 list_for_each_entry(c, &codec->bus->codec_list, list) {
1503 for (i = 0; i < c->cvt_setups.used; i++) {
1504 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001505 if (!p->active && p->stream_tag == stream_tag &&
David Henningsson54c2a892012-02-01 12:05:41 +01001506 get_wcaps_type(get_wcaps(c, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001507 p->dirty = 1;
1508 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001509 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001511EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
Takashi Iwaif0cea792010-08-13 11:56:53 +02001513static void really_cleanup_stream(struct hda_codec *codec,
1514 struct hda_cvt_setup *q);
1515
Takashi Iwaid5191e52009-11-16 14:58:17 +01001516/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001517 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001518 * @codec: the CODEC to clean up
1519 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001520 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001521 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001522void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1523 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001524{
Takashi Iwaieb541332010-08-06 13:48:11 +02001525 struct hda_cvt_setup *p;
1526
Takashi Iwai888afa12008-03-18 09:57:50 +01001527 if (!nid)
1528 return;
1529
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001530 if (codec->no_sticky_stream)
1531 do_now = 1;
1532
Takashi Iwai888afa12008-03-18 09:57:50 +01001533 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001534 p = get_hda_cvt_setup(codec, nid);
Takashi Iwaif0cea792010-08-13 11:56:53 +02001535 if (p) {
1536 /* here we just clear the active flag when do_now isn't set;
1537 * actual clean-ups will be done later in
1538 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1539 */
1540 if (do_now)
1541 really_cleanup_stream(codec, p);
1542 else
1543 p->active = 0;
1544 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001545}
Takashi Iwaif0cea792010-08-13 11:56:53 +02001546EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001547
Takashi Iwaieb541332010-08-06 13:48:11 +02001548static void really_cleanup_stream(struct hda_codec *codec,
1549 struct hda_cvt_setup *q)
1550{
1551 hda_nid_t nid = q->nid;
Takashi Iwai218264a2011-09-27 17:33:45 +02001552 if (q->stream_tag || q->channel_id)
1553 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1554 if (q->format_id)
1555 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1556);
Takashi Iwaieb541332010-08-06 13:48:11 +02001557 memset(q, 0, sizeof(*q));
1558 q->nid = nid;
1559}
1560
1561/* clean up the all conflicting obsolete streams */
1562static void purify_inactive_streams(struct hda_codec *codec)
1563{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001564 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001565 int i;
1566
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001567 list_for_each_entry(c, &codec->bus->codec_list, list) {
1568 for (i = 0; i < c->cvt_setups.used; i++) {
1569 struct hda_cvt_setup *p;
1570 p = snd_array_elem(&c->cvt_setups, i);
1571 if (p->dirty)
1572 really_cleanup_stream(c, p);
1573 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001574 }
1575}
1576
Takashi Iwai2a439522011-07-26 09:52:50 +02001577#ifdef CONFIG_PM
Takashi Iwaieb541332010-08-06 13:48:11 +02001578/* clean up all streams; called from suspend */
1579static void hda_cleanup_all_streams(struct hda_codec *codec)
1580{
1581 int i;
1582
1583 for (i = 0; i < codec->cvt_setups.used; i++) {
1584 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1585 if (p->stream_tag)
1586 really_cleanup_stream(codec, p);
1587 }
1588}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001589#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001590
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591/*
1592 * amp access functions
1593 */
1594
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001595/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001596#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001597#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001598#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1599#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001601#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
1603/* initialize the hash table */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001604static void /*__devinit*/ init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001605 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606{
Takashi Iwai01751f52007-08-10 16:59:39 +02001607 memset(cache, 0, sizeof(*cache));
1608 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001609 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001610}
1611
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001612static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001613{
Takashi Iwai603c4012008-07-30 15:01:44 +02001614 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615}
1616
1617/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a542010-03-30 18:03:44 +02001618static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619{
Takashi Iwai01751f52007-08-10 16:59:39 +02001620 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1621 u16 cur = cache->hash[idx];
1622 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
1624 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001625 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 if (info->key == key)
1627 return info;
1628 cur = info->next;
1629 }
Takashi Iwaia68d5a542010-03-30 18:03:44 +02001630 return NULL;
1631}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
Takashi Iwaia68d5a542010-03-30 18:03:44 +02001633/* query the hash. allocate an entry if not found. */
1634static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1635 u32 key)
1636{
1637 struct hda_cache_head *info = get_hash(cache, key);
1638 if (!info) {
1639 u16 idx, cur;
1640 /* add a new hash entry */
1641 info = snd_array_new(&cache->buf);
1642 if (!info)
1643 return NULL;
1644 cur = snd_array_index(&cache->buf, info);
1645 info->key = key;
1646 info->val = 0;
1647 idx = key % (u16)ARRAY_SIZE(cache->hash);
1648 info->next = cache->hash[idx];
1649 cache->hash[idx] = cur;
1650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 return info;
1652}
1653
Takashi Iwai01751f52007-08-10 16:59:39 +02001654/* query and allocate an amp hash entry */
1655static inline struct hda_amp_info *
1656get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1657{
1658 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1659}
1660
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001661/* overwrite the value with the key in the caps hash */
1662static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val)
1663{
1664 struct hda_amp_info *info;
1665
1666 mutex_lock(&codec->hash_mutex);
1667 info = get_alloc_amp_hash(codec, key);
1668 if (!info) {
1669 mutex_unlock(&codec->hash_mutex);
1670 return -EINVAL;
1671 }
1672 info->amp_caps = val;
1673 info->head.val |= INFO_AMP_CAPS;
1674 mutex_unlock(&codec->hash_mutex);
1675 return 0;
1676}
1677
1678/* query the value from the caps hash; if not found, fetch the current
1679 * value from the given function and store in the hash
1680 */
1681static unsigned int
1682query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key,
1683 unsigned int (*func)(struct hda_codec *, hda_nid_t, int))
1684{
1685 struct hda_amp_info *info;
1686 unsigned int val;
1687
1688 mutex_lock(&codec->hash_mutex);
1689 info = get_alloc_amp_hash(codec, key);
1690 if (!info) {
1691 mutex_unlock(&codec->hash_mutex);
1692 return 0;
1693 }
1694 if (!(info->head.val & INFO_AMP_CAPS)) {
1695 mutex_unlock(&codec->hash_mutex); /* for reentrance */
1696 val = func(codec, nid, dir);
1697 write_caps_hash(codec, key, val);
1698 } else {
1699 val = info->amp_caps;
1700 mutex_unlock(&codec->hash_mutex);
1701 }
1702 return val;
1703}
1704
1705static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid,
1706 int direction)
1707{
1708 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1709 nid = codec->afg;
1710 return snd_hda_param_read(codec, nid,
1711 direction == HDA_OUTPUT ?
1712 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
1713}
1714
Takashi Iwaid5191e52009-11-16 14:58:17 +01001715/**
1716 * query_amp_caps - query AMP capabilities
1717 * @codec: the HD-auio codec
1718 * @nid: the NID to query
1719 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1720 *
1721 * Query AMP capabilities for the given widget and direction.
1722 * Returns the obtained capability bits.
1723 *
1724 * When cap bits have been already read, this doesn't read again but
1725 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001727u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001729 return query_caps_hash(codec, nid, direction,
1730 HDA_HASH_KEY(nid, direction, 0),
1731 read_amp_cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001733EXPORT_SYMBOL_HDA(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
Takashi Iwaid5191e52009-11-16 14:58:17 +01001735/**
1736 * snd_hda_override_amp_caps - Override the AMP capabilities
1737 * @codec: the CODEC to clean up
1738 * @nid: the NID to clean up
1739 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1740 * @caps: the capability bits to set
1741 *
1742 * Override the cached AMP caps bits value by the given one.
1743 * This function is useful if the driver needs to adjust the AMP ranges,
1744 * e.g. limit to 0dB, etc.
1745 *
1746 * Returns zero if successful or a negative error code.
1747 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001748int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1749 unsigned int caps)
1750{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001751 return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001752}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001753EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001754
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001755static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid,
1756 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001757{
1758 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1759}
1760
Takashi Iwaid5191e52009-11-16 14:58:17 +01001761/**
1762 * snd_hda_query_pin_caps - Query PIN capabilities
1763 * @codec: the HD-auio codec
1764 * @nid: the NID to query
1765 *
1766 * Query PIN capabilities for the given widget.
1767 * Returns the obtained capability bits.
1768 *
1769 * When cap bits have been already read, this doesn't read again but
1770 * returns the cached value.
1771 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001772u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1773{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001774 return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001775 read_pin_cap);
1776}
Takashi Iwai1327a322009-03-23 13:07:47 +01001777EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1778
Wu Fengguang864f92b2009-11-18 12:38:02 +08001779/**
Takashi Iwaif57c2562011-08-15 12:49:07 +02001780 * snd_hda_override_pin_caps - Override the pin capabilities
1781 * @codec: the CODEC
1782 * @nid: the NID to override
1783 * @caps: the capability bits to set
1784 *
1785 * Override the cached PIN capabilitiy bits value by the given one.
1786 *
1787 * Returns zero if successful or a negative error code.
1788 */
1789int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
1790 unsigned int caps)
1791{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001792 return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps);
Takashi Iwaif57c2562011-08-15 12:49:07 +02001793}
1794EXPORT_SYMBOL_HDA(snd_hda_override_pin_caps);
1795
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001796/* read or sync the hash value with the current value;
1797 * call within hash_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 */
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001799static struct hda_amp_info *
1800update_amp_hash(struct hda_codec *codec, hda_nid_t nid, int ch,
1801 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001803 struct hda_amp_info *info;
1804 unsigned int parm, val = 0;
1805 bool val_read = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001807 retry:
1808 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1809 if (!info)
1810 return NULL;
1811 if (!(info->head.val & INFO_AMP_VOL(ch))) {
1812 if (!val_read) {
1813 mutex_unlock(&codec->hash_mutex);
1814 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1815 parm |= direction == HDA_OUTPUT ?
1816 AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1817 parm |= index;
1818 val = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001819 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001820 val &= 0xff;
1821 val_read = true;
1822 mutex_lock(&codec->hash_mutex);
1823 goto retry;
1824 }
1825 info->vol[ch] = val;
1826 info->head.val |= INFO_AMP_VOL(ch);
1827 }
1828 return info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829}
1830
1831/*
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001832 * write the current volume in info to the h/w
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001834static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001835 hda_nid_t nid, int ch, int direction, int index,
1836 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837{
1838 u32 parm;
1839
1840 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1841 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1842 parm |= index << AC_AMP_SET_INDEX_SHIFT;
Takashi Iwai38681372012-02-27 15:00:58 +01001843 if ((val & HDA_AMP_MUTE) && !(info->amp_caps & AC_AMPCAP_MUTE) &&
1844 (info->amp_caps & AC_AMPCAP_MIN_MUTE))
1845 ; /* set the zero value as a fake mute */
1846 else
1847 parm |= val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
1849}
1850
Takashi Iwaid5191e52009-11-16 14:58:17 +01001851/**
1852 * snd_hda_codec_amp_read - Read AMP value
1853 * @codec: HD-audio codec
1854 * @nid: NID to read the AMP value
1855 * @ch: channel (left=0 or right=1)
1856 * @direction: #HDA_INPUT or #HDA_OUTPUT
1857 * @index: the index value (only for input direction)
1858 *
1859 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 */
Takashi Iwai834be882006-03-01 14:16:17 +01001861int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1862 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001864 struct hda_amp_info *info;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001865 unsigned int val = 0;
1866
1867 mutex_lock(&codec->hash_mutex);
1868 info = update_amp_hash(codec, nid, ch, direction, index);
1869 if (info)
1870 val = info->vol[ch];
1871 mutex_unlock(&codec->hash_mutex);
1872 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001874EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
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 Iwai0ba21762007-04-16 11:29:14 +02001892 struct hda_amp_info *info;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001893
Takashi Iwai467126462010-03-29 09:19:38 +02001894 if (snd_BUG_ON(mask & ~0xff))
1895 mask &= 0xff;
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001896 val &= mask;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001897
1898 mutex_lock(&codec->hash_mutex);
1899 info = update_amp_hash(codec, nid, ch, direction, idx);
1900 if (!info) {
1901 mutex_unlock(&codec->hash_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 return 0;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001903 }
1904 val |= info->vol[ch] & ~mask;
1905 if (info->vol[ch] == val) {
1906 mutex_unlock(&codec->hash_mutex);
1907 return 0;
1908 }
1909 info->vol[ch] = val;
1910 mutex_unlock(&codec->hash_mutex);
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001911 put_vol_mute(codec, info, nid, ch, direction, idx, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 return 1;
1913}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001914EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
Takashi Iwaid5191e52009-11-16 14:58:17 +01001916/**
1917 * snd_hda_codec_amp_stereo - update the AMP stereo values
1918 * @codec: HD-audio codec
1919 * @nid: NID to read the AMP value
1920 * @direction: #HDA_INPUT or #HDA_OUTPUT
1921 * @idx: the index value (only for input direction)
1922 * @mask: bit mask to set
1923 * @val: the bits value to set
1924 *
1925 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1926 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02001927 */
1928int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1929 int direction, int idx, int mask, int val)
1930{
1931 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001932
1933 if (snd_BUG_ON(mask & ~0xff))
1934 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02001935 for (ch = 0; ch < 2; ch++)
1936 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1937 idx, mask, val);
1938 return ret;
1939}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001940EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001941
Takashi Iwai2a439522011-07-26 09:52:50 +02001942#ifdef CONFIG_PM
Takashi Iwaid5191e52009-11-16 14:58:17 +01001943/**
1944 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
1945 * @codec: HD-audio codec
1946 *
1947 * Resume the all amp commands from the cache.
1948 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001949void snd_hda_codec_resume_amp(struct hda_codec *codec)
1950{
Takashi Iwai603c4012008-07-30 15:01:44 +02001951 struct hda_amp_info *buffer = codec->amp_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001952 int i;
1953
Takashi Iwai603c4012008-07-30 15:01:44 +02001954 for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001955 u32 key = buffer->head.key;
1956 hda_nid_t nid;
1957 unsigned int idx, dir, ch;
1958 if (!key)
1959 continue;
1960 nid = key & 0xff;
1961 idx = (key >> 16) & 0xff;
1962 dir = (key >> 24) & 0xff;
1963 for (ch = 0; ch < 2; ch++) {
1964 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
1965 continue;
1966 put_vol_mute(codec, buffer, nid, ch, dir, idx,
1967 buffer->vol[ch]);
1968 }
1969 }
1970}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001971EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
Takashi Iwai2a439522011-07-26 09:52:50 +02001972#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001974static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1975 unsigned int ofs)
1976{
1977 u32 caps = query_amp_caps(codec, nid, dir);
1978 /* get num steps */
1979 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1980 if (ofs < caps)
1981 caps -= ofs;
1982 return caps;
1983}
1984
Takashi Iwaid5191e52009-11-16 14:58:17 +01001985/**
1986 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
1987 *
1988 * The control element is supposed to have the private_value field
1989 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1990 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001991int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1992 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993{
1994 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1995 u16 nid = get_amp_nid(kcontrol);
1996 u8 chs = get_amp_channels(kcontrol);
1997 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001998 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002000 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2001 uinfo->count = chs == 3 ? 2 : 1;
2002 uinfo->value.integer.min = 0;
2003 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
2004 if (!uinfo->value.integer.max) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002005 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01002006 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
2007 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 return -EINVAL;
2009 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 return 0;
2011}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002012EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002014
2015static inline unsigned int
2016read_amp_value(struct hda_codec *codec, hda_nid_t nid,
2017 int ch, int dir, int idx, unsigned int ofs)
2018{
2019 unsigned int val;
2020 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
2021 val &= HDA_AMP_VOLMASK;
2022 if (val >= ofs)
2023 val -= ofs;
2024 else
2025 val = 0;
2026 return val;
2027}
2028
2029static inline int
2030update_amp_value(struct hda_codec *codec, hda_nid_t nid,
2031 int ch, int dir, int idx, unsigned int ofs,
2032 unsigned int val)
2033{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002034 unsigned int maxval;
2035
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002036 if (val > 0)
2037 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02002038 /* ofs = 0: raw max value */
2039 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002040 if (val > maxval)
2041 val = maxval;
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002042 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
2043 HDA_AMP_VOLMASK, val);
2044}
2045
Takashi Iwaid5191e52009-11-16 14:58:17 +01002046/**
2047 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
2048 *
2049 * The control element is supposed to have the private_value field
2050 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2051 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002052int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
2053 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054{
2055 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2056 hda_nid_t nid = get_amp_nid(kcontrol);
2057 int chs = get_amp_channels(kcontrol);
2058 int dir = get_amp_direction(kcontrol);
2059 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002060 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 long *valp = ucontrol->value.integer.value;
2062
2063 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002064 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002066 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 return 0;
2068}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002069EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070
Takashi Iwaid5191e52009-11-16 14:58:17 +01002071/**
2072 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
2073 *
2074 * The control element is supposed to have the private_value field
2075 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2076 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002077int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
2078 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079{
2080 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2081 hda_nid_t nid = get_amp_nid(kcontrol);
2082 int chs = get_amp_channels(kcontrol);
2083 int dir = get_amp_direction(kcontrol);
2084 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002085 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 long *valp = ucontrol->value.integer.value;
2087 int change = 0;
2088
Takashi Iwaicb53c622007-08-10 17:21:45 +02002089 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002090 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002091 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002092 valp++;
2093 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002094 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002095 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002096 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 return change;
2098}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002099EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100
Takashi Iwaid5191e52009-11-16 14:58:17 +01002101/**
2102 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2103 *
2104 * The control element is supposed to have the private_value field
2105 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2106 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002107int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2108 unsigned int size, unsigned int __user *_tlv)
2109{
2110 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2111 hda_nid_t nid = get_amp_nid(kcontrol);
2112 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002113 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02002114 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002115 u32 caps, val1, val2;
2116
2117 if (size < 4 * sizeof(unsigned int))
2118 return -ENOMEM;
2119 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002120 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2121 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002122 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002123 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002124 val1 = ((int)val1) * ((int)val2);
Takashi Iwai38681372012-02-27 15:00:58 +01002125 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwaic08d9162010-10-17 10:40:53 +02002126 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002127 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2128 return -EFAULT;
2129 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2130 return -EFAULT;
2131 if (put_user(val1, _tlv + 2))
2132 return -EFAULT;
2133 if (put_user(val2, _tlv + 3))
2134 return -EFAULT;
2135 return 0;
2136}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002137EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002138
Takashi Iwaid5191e52009-11-16 14:58:17 +01002139/**
2140 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2141 * @codec: HD-audio codec
2142 * @nid: NID of a reference widget
2143 * @dir: #HDA_INPUT or #HDA_OUTPUT
2144 * @tlv: TLV data to be stored, at least 4 elements
2145 *
2146 * Set (static) TLV data for a virtual master volume using the AMP caps
2147 * obtained from the reference NID.
2148 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01002149 */
2150void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2151 unsigned int *tlv)
2152{
2153 u32 caps;
2154 int nums, step;
2155
2156 caps = query_amp_caps(codec, nid, dir);
2157 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2158 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2159 step = (step + 1) * 25;
2160 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2161 tlv[1] = 2 * sizeof(unsigned int);
2162 tlv[2] = -nums * step;
2163 tlv[3] = step;
2164}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002165EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002166
2167/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01002168static struct snd_kcontrol *
Takashi Iwaidcda5802012-10-12 17:24:51 +02002169find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002170{
2171 struct snd_ctl_elem_id id;
2172 memset(&id, 0, sizeof(id));
2173 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwaidcda5802012-10-12 17:24:51 +02002174 id.device = dev;
Takashi Iwai09f99702008-02-04 12:31:13 +01002175 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02002176 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2177 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002178 strcpy(id.name, name);
2179 return snd_ctl_find_id(codec->bus->card, &id);
2180}
2181
Takashi Iwaid5191e52009-11-16 14:58:17 +01002182/**
2183 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2184 * @codec: HD-audio codec
2185 * @name: ctl id name string
2186 *
2187 * Get the control element with the given id string and IFACE_MIXER.
2188 */
Takashi Iwai09f99702008-02-04 12:31:13 +01002189struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2190 const char *name)
2191{
Takashi Iwaidcda5802012-10-12 17:24:51 +02002192 return find_mixer_ctl(codec, name, 0, 0);
Takashi Iwai09f99702008-02-04 12:31:13 +01002193}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002194EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01002195
Takashi Iwaidcda5802012-10-12 17:24:51 +02002196static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
2197 int dev)
Takashi Iwai1afe2062010-12-23 10:17:52 +01002198{
2199 int idx;
2200 for (idx = 0; idx < 16; idx++) { /* 16 ctlrs should be large enough */
Takashi Iwaidcda5802012-10-12 17:24:51 +02002201 if (!find_mixer_ctl(codec, name, dev, idx))
Takashi Iwai1afe2062010-12-23 10:17:52 +01002202 return idx;
2203 }
2204 return -EBUSY;
2205}
2206
Takashi Iwaid5191e52009-11-16 14:58:17 +01002207/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002208 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01002209 * @codec: HD-audio codec
2210 * @nid: corresponding NID (optional)
2211 * @kctl: the control element to assign
2212 *
2213 * Add the given control element to an array inside the codec instance.
2214 * All control elements belonging to a codec are supposed to be added
2215 * by this function so that a proper clean-up works at the free or
2216 * reconfiguration time.
2217 *
2218 * If non-zero @nid is passed, the NID is assigned to the control element.
2219 * The assignment is shown in the codec proc file.
2220 *
2221 * snd_hda_ctl_add() checks the control subdev id field whether
2222 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002223 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2224 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01002225 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002226int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2227 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002228{
2229 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002230 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002231 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002232
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002233 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002234 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002235 if (nid == 0)
2236 nid = get_amp_nid_(kctl->private_value);
2237 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002238 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2239 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002240 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01002241 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002242 err = snd_ctl_add(codec->bus->card, kctl);
2243 if (err < 0)
2244 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002245 item = snd_array_new(&codec->mixers);
2246 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002247 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002248 item->kctl = kctl;
2249 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002250 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002251 return 0;
2252}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002253EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002254
Takashi Iwaid5191e52009-11-16 14:58:17 +01002255/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002256 * snd_hda_add_nid - Assign a NID to a control element
2257 * @codec: HD-audio codec
2258 * @nid: corresponding NID (optional)
2259 * @kctl: the control element to assign
2260 * @index: index to kctl
2261 *
2262 * Add the given control element to an array inside the codec instance.
2263 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2264 * NID:KCTL mapping - for example "Capture Source" selector.
2265 */
2266int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2267 unsigned int index, hda_nid_t nid)
2268{
2269 struct hda_nid_item *item;
2270
2271 if (nid > 0) {
2272 item = snd_array_new(&codec->nids);
2273 if (!item)
2274 return -ENOMEM;
2275 item->kctl = kctl;
2276 item->index = index;
2277 item->nid = nid;
2278 return 0;
2279 }
Takashi Iwai28d1a852010-03-15 09:05:46 +01002280 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
2281 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002282 return -EINVAL;
2283}
2284EXPORT_SYMBOL_HDA(snd_hda_add_nid);
2285
2286/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002287 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2288 * @codec: HD-audio codec
2289 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002290void snd_hda_ctls_clear(struct hda_codec *codec)
2291{
2292 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002293 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002294 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002295 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002296 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002297 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002298}
2299
Takashi Iwaia65d6292009-02-23 16:57:04 +01002300/* pseudo device locking
2301 * toggle card->shutdown to allow/disallow the device access (as a hack)
2302 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002303int snd_hda_lock_devices(struct hda_bus *bus)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002304{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002305 struct snd_card *card = bus->card;
2306 struct hda_codec *codec;
2307
Takashi Iwaia65d6292009-02-23 16:57:04 +01002308 spin_lock(&card->files_lock);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002309 if (card->shutdown)
2310 goto err_unlock;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002311 card->shutdown = 1;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002312 if (!list_empty(&card->ctl_files))
2313 goto err_clear;
2314
2315 list_for_each_entry(codec, &bus->codec_list, list) {
2316 int pcm;
2317 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2318 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2319 if (!cpcm->pcm)
2320 continue;
2321 if (cpcm->pcm->streams[0].substream_opened ||
2322 cpcm->pcm->streams[1].substream_opened)
2323 goto err_clear;
2324 }
2325 }
Takashi Iwaia65d6292009-02-23 16:57:04 +01002326 spin_unlock(&card->files_lock);
2327 return 0;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002328
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002329 err_clear:
2330 card->shutdown = 0;
2331 err_unlock:
2332 spin_unlock(&card->files_lock);
2333 return -EINVAL;
2334}
2335EXPORT_SYMBOL_HDA(snd_hda_lock_devices);
2336
2337void snd_hda_unlock_devices(struct hda_bus *bus)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002338{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002339 struct snd_card *card = bus->card;
2340
2341 card = bus->card;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002342 spin_lock(&card->files_lock);
2343 card->shutdown = 0;
2344 spin_unlock(&card->files_lock);
2345}
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002346EXPORT_SYMBOL_HDA(snd_hda_unlock_devices);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002347
Takashi Iwaid5191e52009-11-16 14:58:17 +01002348/**
2349 * snd_hda_codec_reset - Clear all objects assigned to the codec
2350 * @codec: HD-audio codec
2351 *
2352 * This frees the all PCM and control elements assigned to the codec, and
2353 * clears the caches and restores the pin default configurations.
2354 *
2355 * When a device is being used, it returns -EBSY. If successfully freed,
2356 * returns zero.
2357 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002358int snd_hda_codec_reset(struct hda_codec *codec)
2359{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002360 struct hda_bus *bus = codec->bus;
2361 struct snd_card *card = bus->card;
2362 int i;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002363
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002364 if (snd_hda_lock_devices(bus) < 0)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002365 return -EBUSY;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002366
2367 /* OK, let it free */
David Henningsson26a6cb62012-10-09 15:04:21 +02002368 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai83012a72012-08-24 18:38:08 +02002369#ifdef CONFIG_PM
Takashi Iwaia2d96e72012-05-09 12:36:22 +02002370 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwai339876d2012-05-08 16:57:12 +02002371 codec->power_on = 0;
2372 codec->power_transition = 0;
2373 codec->power_jiffies = jiffies;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002374 flush_workqueue(bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002375#endif
2376 snd_hda_ctls_clear(codec);
2377 /* relase PCMs */
2378 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002379 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01002380 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002381 clear_bit(codec->pcm_info[i].device,
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002382 bus->pcm_dev_bits);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002383 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002384 }
2385 if (codec->patch_ops.free)
2386 codec->patch_ops.free(codec);
Takashi Iwai07dc59f2012-09-10 09:39:31 +02002387 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
Takashi Iwai1835a0f2011-10-27 22:12:46 +02002388 snd_hda_jack_tbl_clear(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01002389 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002390 codec->spec = NULL;
2391 free_hda_cache(&codec->amp_cache);
2392 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01002393 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2394 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01002395 /* free only driver_pins so that init_pins + user_pins are restored */
2396 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01002397 restore_pincfgs(codec);
Takashi Iwai09a60712012-06-26 15:01:33 +02002398 snd_array_free(&codec->cvt_setups);
2399 snd_array_free(&codec->spdif_out);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002400 codec->num_pcms = 0;
2401 codec->pcm_info = NULL;
2402 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01002403 codec->slave_dig_outs = NULL;
2404 codec->spdif_status_reset = 0;
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002405 module_put(codec->owner);
2406 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002407
2408 /* allow device access again */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002409 snd_hda_unlock_devices(bus);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002410 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002411}
2412
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002413typedef int (*map_slave_func_t)(void *, struct snd_kcontrol *);
2414
2415/* apply the function to all matching slave ctls in the mixer list */
2416static int map_slaves(struct hda_codec *codec, const char * const *slaves,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002417 const char *suffix, map_slave_func_t func, void *data)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002418{
2419 struct hda_nid_item *items;
2420 const char * const *s;
2421 int i, err;
2422
2423 items = codec->mixers.list;
2424 for (i = 0; i < codec->mixers.used; i++) {
2425 struct snd_kcontrol *sctl = items[i].kctl;
2426 if (!sctl || !sctl->id.name ||
2427 sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
2428 continue;
2429 for (s = slaves; *s; s++) {
Takashi Iwai9322ca52012-02-03 14:28:01 +01002430 char tmpname[sizeof(sctl->id.name)];
2431 const char *name = *s;
2432 if (suffix) {
2433 snprintf(tmpname, sizeof(tmpname), "%s %s",
2434 name, suffix);
2435 name = tmpname;
2436 }
Takashi Iwai9322ca52012-02-03 14:28:01 +01002437 if (!strcmp(sctl->id.name, name)) {
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002438 err = func(data, sctl);
2439 if (err)
2440 return err;
2441 break;
2442 }
2443 }
2444 }
2445 return 0;
2446}
2447
2448static int check_slave_present(void *data, struct snd_kcontrol *sctl)
2449{
2450 return 1;
2451}
2452
Takashi Iwai18478e82012-03-09 17:51:10 +01002453/* guess the value corresponding to 0dB */
2454static int get_kctl_0dB_offset(struct snd_kcontrol *kctl)
2455{
2456 int _tlv[4];
2457 const int *tlv = NULL;
2458 int val = -1;
2459
2460 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2461 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2462 mm_segment_t fs = get_fs();
2463 set_fs(get_ds());
2464 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2465 tlv = _tlv;
2466 set_fs(fs);
2467 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2468 tlv = kctl->tlv.p;
2469 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE)
2470 val = -tlv[2] / tlv[3];
2471 return val;
2472}
2473
2474/* call kctl->put with the given value(s) */
2475static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2476{
2477 struct snd_ctl_elem_value *ucontrol;
2478 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2479 if (!ucontrol)
2480 return -ENOMEM;
2481 ucontrol->value.integer.value[0] = val;
2482 ucontrol->value.integer.value[1] = val;
2483 kctl->put(kctl, ucontrol);
2484 kfree(ucontrol);
2485 return 0;
2486}
2487
2488/* initialize the slave volume with 0dB */
2489static int init_slave_0dB(void *data, struct snd_kcontrol *slave)
2490{
2491 int offset = get_kctl_0dB_offset(slave);
2492 if (offset > 0)
2493 put_kctl_with_value(slave, offset);
2494 return 0;
2495}
2496
2497/* unmute the slave */
2498static int init_slave_unmute(void *data, struct snd_kcontrol *slave)
2499{
2500 return put_kctl_with_value(slave, 1);
2501}
2502
Takashi Iwaid5191e52009-11-16 14:58:17 +01002503/**
2504 * snd_hda_add_vmaster - create a virtual master control and add slaves
2505 * @codec: HD-audio codec
2506 * @name: vmaster control name
2507 * @tlv: TLV data (optional)
2508 * @slaves: slave control names (optional)
Takashi Iwai9322ca52012-02-03 14:28:01 +01002509 * @suffix: suffix string to each slave name (optional)
Takashi Iwai18478e82012-03-09 17:51:10 +01002510 * @init_slave_vol: initialize slaves to unmute/0dB
Takashi Iwai29e58532012-03-12 12:25:03 +01002511 * @ctl_ret: store the vmaster kcontrol in return
Takashi Iwaid5191e52009-11-16 14:58:17 +01002512 *
2513 * Create a virtual master control with the given name. The TLV data
2514 * must be either NULL or a valid data.
2515 *
2516 * @slaves is a NULL-terminated array of strings, each of which is a
2517 * slave control name. All controls with these names are assigned to
2518 * the new virtual master control.
2519 *
2520 * This function returns zero if successful or a negative error code.
2521 */
Takashi Iwai18478e82012-03-09 17:51:10 +01002522int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002523 unsigned int *tlv, const char * const *slaves,
Takashi Iwai29e58532012-03-12 12:25:03 +01002524 const char *suffix, bool init_slave_vol,
2525 struct snd_kcontrol **ctl_ret)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002526{
2527 struct snd_kcontrol *kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002528 int err;
2529
Takashi Iwai29e58532012-03-12 12:25:03 +01002530 if (ctl_ret)
2531 *ctl_ret = NULL;
2532
Takashi Iwai9322ca52012-02-03 14:28:01 +01002533 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002534 if (err != 1) {
Takashi Iwai2f085542008-02-22 18:43:50 +01002535 snd_printdd("No slave found for %s\n", name);
2536 return 0;
2537 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002538 kctl = snd_ctl_make_virtual_master(name, tlv);
2539 if (!kctl)
2540 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002541 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002542 if (err < 0)
2543 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002544
Takashi Iwai9322ca52012-02-03 14:28:01 +01002545 err = map_slaves(codec, slaves, suffix,
2546 (map_slave_func_t)snd_ctl_add_slave, kctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002547 if (err < 0)
2548 return err;
Takashi Iwai18478e82012-03-09 17:51:10 +01002549
2550 /* init with master mute & zero volume */
2551 put_kctl_with_value(kctl, 0);
2552 if (init_slave_vol)
2553 map_slaves(codec, slaves, suffix,
2554 tlv ? init_slave_0dB : init_slave_unmute, kctl);
2555
Takashi Iwai29e58532012-03-12 12:25:03 +01002556 if (ctl_ret)
2557 *ctl_ret = kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002558 return 0;
2559}
Takashi Iwai18478e82012-03-09 17:51:10 +01002560EXPORT_SYMBOL_HDA(__snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002561
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002562/*
2563 * mute-LED control using vmaster
2564 */
2565static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2566 struct snd_ctl_elem_info *uinfo)
2567{
2568 static const char * const texts[] = {
2569 "Off", "On", "Follow Master"
2570 };
2571 unsigned int index;
2572
2573 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2574 uinfo->count = 1;
2575 uinfo->value.enumerated.items = 3;
2576 index = uinfo->value.enumerated.item;
2577 if (index >= 3)
2578 index = 2;
2579 strcpy(uinfo->value.enumerated.name, texts[index]);
2580 return 0;
2581}
2582
2583static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2584 struct snd_ctl_elem_value *ucontrol)
2585{
2586 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2587 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2588 return 0;
2589}
2590
2591static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2592 struct snd_ctl_elem_value *ucontrol)
2593{
2594 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2595 unsigned int old_mode = hook->mute_mode;
2596
2597 hook->mute_mode = ucontrol->value.enumerated.item[0];
2598 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2599 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2600 if (old_mode == hook->mute_mode)
2601 return 0;
2602 snd_hda_sync_vmaster_hook(hook);
2603 return 1;
2604}
2605
2606static struct snd_kcontrol_new vmaster_mute_mode = {
2607 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2608 .name = "Mute-LED Mode",
2609 .info = vmaster_mute_mode_info,
2610 .get = vmaster_mute_mode_get,
2611 .put = vmaster_mute_mode_put,
2612};
2613
2614/*
2615 * Add a mute-LED hook with the given vmaster switch kctl
2616 * "Mute-LED Mode" control is automatically created and associated with
2617 * the given hook.
2618 */
2619int snd_hda_add_vmaster_hook(struct hda_codec *codec,
Takashi Iwaif29735cb2012-03-13 07:55:10 +01002620 struct hda_vmaster_mute_hook *hook,
2621 bool expose_enum_ctl)
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002622{
2623 struct snd_kcontrol *kctl;
2624
2625 if (!hook->hook || !hook->sw_kctl)
2626 return 0;
2627 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
2628 hook->codec = codec;
2629 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
Takashi Iwaif29735cb2012-03-13 07:55:10 +01002630 if (!expose_enum_ctl)
2631 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002632 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2633 if (!kctl)
2634 return -ENOMEM;
2635 return snd_hda_ctl_add(codec, 0, kctl);
2636}
2637EXPORT_SYMBOL_HDA(snd_hda_add_vmaster_hook);
2638
2639/*
2640 * Call the hook with the current value for synchronization
2641 * Should be called in init callback
2642 */
2643void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2644{
2645 if (!hook->hook || !hook->codec)
2646 return;
2647 switch (hook->mute_mode) {
2648 case HDA_VMUTE_FOLLOW_MASTER:
2649 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2650 break;
2651 default:
2652 hook->hook(hook->codec, hook->mute_mode);
2653 break;
2654 }
2655}
2656EXPORT_SYMBOL_HDA(snd_hda_sync_vmaster_hook);
2657
2658
Takashi Iwaid5191e52009-11-16 14:58:17 +01002659/**
2660 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2661 *
2662 * The control element is supposed to have the private_value field
2663 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2664 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002665int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2666 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667{
2668 int chs = get_amp_channels(kcontrol);
2669
2670 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2671 uinfo->count = chs == 3 ? 2 : 1;
2672 uinfo->value.integer.min = 0;
2673 uinfo->value.integer.max = 1;
2674 return 0;
2675}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002676EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677
Takashi Iwaid5191e52009-11-16 14:58:17 +01002678/**
2679 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2680 *
2681 * The control element is supposed to have the private_value field
2682 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2683 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002684int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2685 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686{
2687 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2688 hda_nid_t nid = get_amp_nid(kcontrol);
2689 int chs = get_amp_channels(kcontrol);
2690 int dir = get_amp_direction(kcontrol);
2691 int idx = get_amp_index(kcontrol);
2692 long *valp = ucontrol->value.integer.value;
2693
2694 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002695 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002696 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002698 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002699 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 return 0;
2701}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002702EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703
Takashi Iwaid5191e52009-11-16 14:58:17 +01002704/**
2705 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2706 *
2707 * The control element is supposed to have the private_value field
2708 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2709 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002710int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2711 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712{
2713 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2714 hda_nid_t nid = get_amp_nid(kcontrol);
2715 int chs = get_amp_channels(kcontrol);
2716 int dir = get_amp_direction(kcontrol);
2717 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 long *valp = ucontrol->value.integer.value;
2719 int change = 0;
2720
Takashi Iwaicb53c622007-08-10 17:21:45 +02002721 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002722 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002723 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002724 HDA_AMP_MUTE,
2725 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002726 valp++;
2727 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002728 if (chs & 2)
2729 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002730 HDA_AMP_MUTE,
2731 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002732 hda_call_check_power_status(codec, nid);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002733 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 return change;
2735}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002736EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737
2738/*
Takashi Iwai985be542005-11-02 18:26:49 +01002739 * bound volume controls
2740 *
2741 * bind multiple volumes (# indices, from 0)
2742 */
2743
2744#define AMP_VAL_IDX_SHIFT 19
2745#define AMP_VAL_IDX_MASK (0x0f<<19)
2746
Takashi Iwaid5191e52009-11-16 14:58:17 +01002747/**
2748 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2749 *
2750 * The control element is supposed to have the private_value field
2751 * set up via HDA_BIND_MUTE*() macros.
2752 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002753int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2754 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002755{
2756 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2757 unsigned long pval;
2758 int err;
2759
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002760 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002761 pval = kcontrol->private_value;
2762 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2763 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2764 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002765 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002766 return err;
2767}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002768EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002769
Takashi Iwaid5191e52009-11-16 14:58:17 +01002770/**
2771 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2772 *
2773 * The control element is supposed to have the private_value field
2774 * set up via HDA_BIND_MUTE*() macros.
2775 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002776int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2777 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002778{
2779 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2780 unsigned long pval;
2781 int i, indices, err = 0, change = 0;
2782
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002783 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002784 pval = kcontrol->private_value;
2785 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2786 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002787 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2788 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002789 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2790 if (err < 0)
2791 break;
2792 change |= err;
2793 }
2794 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002795 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002796 return err < 0 ? err : change;
2797}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002798EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002799
Takashi Iwaid5191e52009-11-16 14:58:17 +01002800/**
2801 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2802 *
2803 * The control element is supposed to have the private_value field
2804 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002805 */
2806int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2807 struct snd_ctl_elem_info *uinfo)
2808{
2809 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2810 struct hda_bind_ctls *c;
2811 int err;
2812
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002813 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002814 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002815 kcontrol->private_value = *c->values;
2816 err = c->ops->info(kcontrol, uinfo);
2817 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002818 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002819 return err;
2820}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002821EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002822
Takashi Iwaid5191e52009-11-16 14:58:17 +01002823/**
2824 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2825 *
2826 * The control element is supposed to have the private_value field
2827 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2828 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002829int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2830 struct snd_ctl_elem_value *ucontrol)
2831{
2832 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2833 struct hda_bind_ctls *c;
2834 int err;
2835
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002836 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002837 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002838 kcontrol->private_value = *c->values;
2839 err = c->ops->get(kcontrol, ucontrol);
2840 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002841 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002842 return err;
2843}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002844EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002845
Takashi Iwaid5191e52009-11-16 14:58:17 +01002846/**
2847 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2848 *
2849 * The control element is supposed to have the private_value field
2850 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2851 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002852int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2853 struct snd_ctl_elem_value *ucontrol)
2854{
2855 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2856 struct hda_bind_ctls *c;
2857 unsigned long *vals;
2858 int err = 0, change = 0;
2859
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002860 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002861 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002862 for (vals = c->values; *vals; vals++) {
2863 kcontrol->private_value = *vals;
2864 err = c->ops->put(kcontrol, ucontrol);
2865 if (err < 0)
2866 break;
2867 change |= err;
2868 }
2869 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002870 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002871 return err < 0 ? err : change;
2872}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002873EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02002874
Takashi Iwaid5191e52009-11-16 14:58:17 +01002875/**
2876 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2877 *
2878 * The control element is supposed to have the private_value field
2879 * set up via HDA_BIND_VOL() macro.
2880 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002881int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2882 unsigned int size, unsigned int __user *tlv)
2883{
2884 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2885 struct hda_bind_ctls *c;
2886 int err;
2887
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002888 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002889 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002890 kcontrol->private_value = *c->values;
2891 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2892 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002893 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002894 return err;
2895}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002896EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02002897
2898struct hda_ctl_ops snd_hda_bind_vol = {
2899 .info = snd_hda_mixer_amp_volume_info,
2900 .get = snd_hda_mixer_amp_volume_get,
2901 .put = snd_hda_mixer_amp_volume_put,
2902 .tlv = snd_hda_mixer_amp_tlv
2903};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002904EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02002905
2906struct hda_ctl_ops snd_hda_bind_sw = {
2907 .info = snd_hda_mixer_amp_switch_info,
2908 .get = snd_hda_mixer_amp_switch_get,
2909 .put = snd_hda_mixer_amp_switch_put,
2910 .tlv = snd_hda_mixer_amp_tlv
2911};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002912EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02002913
2914/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 * SPDIF out controls
2916 */
2917
Takashi Iwai0ba21762007-04-16 11:29:14 +02002918static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2919 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920{
2921 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2922 uinfo->count = 1;
2923 return 0;
2924}
2925
Takashi Iwai0ba21762007-04-16 11:29:14 +02002926static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2927 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928{
2929 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2930 IEC958_AES0_NONAUDIO |
2931 IEC958_AES0_CON_EMPHASIS_5015 |
2932 IEC958_AES0_CON_NOT_COPYRIGHT;
2933 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2934 IEC958_AES1_CON_ORIGINAL;
2935 return 0;
2936}
2937
Takashi Iwai0ba21762007-04-16 11:29:14 +02002938static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2939 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940{
2941 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2942 IEC958_AES0_NONAUDIO |
2943 IEC958_AES0_PRO_EMPHASIS_5015;
2944 return 0;
2945}
2946
Takashi Iwai0ba21762007-04-16 11:29:14 +02002947static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2948 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949{
2950 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06002951 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002952 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002954 mutex_lock(&codec->spdif_mutex);
2955 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06002956 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
2957 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
2958 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
2959 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002960 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961
2962 return 0;
2963}
2964
2965/* convert from SPDIF status bits to HDA SPDIF bits
2966 * bit 0 (DigEn) is always set zero (to be filled later)
2967 */
2968static unsigned short convert_from_spdif_status(unsigned int sbits)
2969{
2970 unsigned short val = 0;
2971
2972 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002973 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002975 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002977 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2978 IEC958_AES0_PRO_EMPHASIS_5015)
2979 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002981 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2982 IEC958_AES0_CON_EMPHASIS_5015)
2983 val |= AC_DIG1_EMPHASIS;
2984 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2985 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02002987 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2989 }
2990 return val;
2991}
2992
2993/* convert to SPDIF status bits from HDA SPDIF bits
2994 */
2995static unsigned int convert_to_spdif_status(unsigned short val)
2996{
2997 unsigned int sbits = 0;
2998
Takashi Iwai0ba21762007-04-16 11:29:14 +02002999 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003001 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 sbits |= IEC958_AES0_PROFESSIONAL;
3003 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003004 if (sbits & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
3006 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003007 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003009 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003011 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
3013 sbits |= val & (0x7f << 8);
3014 }
3015 return sbits;
3016}
3017
Takashi Iwai2f728532008-09-25 16:32:41 +02003018/* set digital convert verbs both for the given NID and its slaves */
3019static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
3020 int verb, int val)
3021{
Takashi Iwaidda14412011-05-02 11:29:30 +02003022 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02003023
Takashi Iwai9e976972008-11-25 08:17:20 +01003024 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003025 d = codec->slave_dig_outs;
3026 if (!d)
3027 return;
3028 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01003029 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003030}
3031
3032static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
3033 int dig1, int dig2)
3034{
3035 if (dig1 != -1)
3036 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
3037 if (dig2 != -1)
3038 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
3039}
3040
Takashi Iwai0ba21762007-04-16 11:29:14 +02003041static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
3042 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043{
3044 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003045 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003046 struct hda_spdif_out *spdif;
3047 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 unsigned short val;
3049 int change;
3050
Ingo Molnar62932df2006-01-16 16:34:20 +01003051 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003052 spdif = snd_array_elem(&codec->spdif_out, idx);
3053 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06003054 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
3056 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
3057 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c935972011-06-01 11:14:17 -06003058 val = convert_from_spdif_status(spdif->status);
3059 val |= spdif->ctls & 1;
3060 change = spdif->ctls != val;
3061 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003062 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02003063 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01003064 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 return change;
3066}
3067
Takashi Iwaia5ce8892007-07-23 15:42:26 +02003068#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069
Takashi Iwai0ba21762007-04-16 11:29:14 +02003070static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
3071 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072{
3073 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003074 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003075 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003077 mutex_lock(&codec->spdif_mutex);
3078 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06003079 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003080 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 return 0;
3082}
3083
Stephen Warren74b654c2011-06-01 11:14:18 -06003084static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
3085 int dig1, int dig2)
3086{
3087 set_dig_out_convert(codec, nid, dig1, dig2);
3088 /* unmute amp switch (if any) */
3089 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
3090 (dig1 & AC_DIG1_ENABLE))
3091 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
3092 HDA_AMP_MUTE, 0);
3093}
3094
Takashi Iwai0ba21762007-04-16 11:29:14 +02003095static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
3096 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097{
3098 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003099 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003100 struct hda_spdif_out *spdif;
3101 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 unsigned short val;
3103 int change;
3104
Ingo Molnar62932df2006-01-16 16:34:20 +01003105 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003106 spdif = snd_array_elem(&codec->spdif_out, idx);
3107 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06003108 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02003110 val |= AC_DIG1_ENABLE;
Stephen Warren7c935972011-06-01 11:14:17 -06003111 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003112 spdif->ctls = val;
3113 if (change && nid != (u16)-1)
3114 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01003115 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 return change;
3117}
3118
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003119static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 {
3121 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3122 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003123 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 .info = snd_hda_spdif_mask_info,
3125 .get = snd_hda_spdif_cmask_get,
3126 },
3127 {
3128 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3129 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003130 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 .info = snd_hda_spdif_mask_info,
3132 .get = snd_hda_spdif_pmask_get,
3133 },
3134 {
3135 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003136 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 .info = snd_hda_spdif_mask_info,
3138 .get = snd_hda_spdif_default_get,
3139 .put = snd_hda_spdif_default_put,
3140 },
3141 {
3142 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003143 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 .info = snd_hda_spdif_out_switch_info,
3145 .get = snd_hda_spdif_out_switch_get,
3146 .put = snd_hda_spdif_out_switch_put,
3147 },
3148 { } /* end */
3149};
3150
3151/**
Takashi Iwaidcda5802012-10-12 17:24:51 +02003152 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 * @codec: the HDA codec
Takashi Iwaidcda5802012-10-12 17:24:51 +02003154 * @associated_nid: NID that new ctls associated with
3155 * @cvt_nid: converter NID
3156 * @type: HDA_PCM_TYPE_*
3157 * Creates controls related with the digital output.
3158 * Called from each patch supporting the digital out.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 *
3160 * Returns 0 if successful, or a negative error code.
3161 */
Takashi Iwaidcda5802012-10-12 17:24:51 +02003162int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
3163 hda_nid_t associated_nid,
3164 hda_nid_t cvt_nid,
3165 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166{
3167 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003168 struct snd_kcontrol *kctl;
3169 struct snd_kcontrol_new *dig_mix;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003170 int idx, dev = 0;
3171 const int spdif_pcm_dev = 1;
Stephen Warren7c935972011-06-01 11:14:17 -06003172 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173
Takashi Iwaidcda5802012-10-12 17:24:51 +02003174 if (codec->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
3175 type == HDA_PCM_TYPE_SPDIF) {
3176 dev = spdif_pcm_dev;
3177 } else if (codec->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
3178 type == HDA_PCM_TYPE_HDMI) {
3179 for (idx = 0; idx < codec->spdif_out.used; idx++) {
3180 spdif = snd_array_elem(&codec->spdif_out, idx);
3181 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3182 kctl = find_mixer_ctl(codec, dig_mix->name, 0, idx);
3183 if (!kctl)
3184 break;
3185 kctl->id.device = spdif_pcm_dev;
3186 }
3187 }
3188 codec->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
3189 }
3190 if (!codec->primary_dig_out_type)
3191 codec->primary_dig_out_type = type;
3192
3193 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", dev);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003194 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003195 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
3196 return -EBUSY;
3197 }
Stephen Warren7c935972011-06-01 11:14:17 -06003198 spdif = snd_array_new(&codec->spdif_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3200 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01003201 if (!kctl)
3202 return -ENOMEM;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003203 kctl->id.device = dev;
Takashi Iwai09f99702008-02-04 12:31:13 +01003204 kctl->id.index = idx;
Stephen Warren7c935972011-06-01 11:14:17 -06003205 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06003206 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003207 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 return err;
3209 }
Stephen Warren74b654c2011-06-01 11:14:18 -06003210 spdif->nid = cvt_nid;
3211 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
Stephen Warren7c935972011-06-01 11:14:17 -06003212 AC_VERB_GET_DIGI_CONVERT_1, 0);
3213 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 return 0;
3215}
Takashi Iwaidcda5802012-10-12 17:24:51 +02003216EXPORT_SYMBOL_HDA(snd_hda_create_dig_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003218/* get the hda_spdif_out entry from the given NID
3219 * call within spdif_mutex lock
3220 */
Stephen Warren7c935972011-06-01 11:14:17 -06003221struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3222 hda_nid_t nid)
3223{
3224 int i;
3225 for (i = 0; i < codec->spdif_out.used; i++) {
3226 struct hda_spdif_out *spdif =
3227 snd_array_elem(&codec->spdif_out, i);
3228 if (spdif->nid == nid)
3229 return spdif;
3230 }
3231 return NULL;
3232}
3233EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid);
3234
Stephen Warren74b654c2011-06-01 11:14:18 -06003235void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3236{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003237 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003238
3239 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003240 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003241 spdif->nid = (u16)-1;
3242 mutex_unlock(&codec->spdif_mutex);
3243}
3244EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign);
3245
3246void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3247{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003248 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003249 unsigned short val;
3250
3251 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003252 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003253 if (spdif->nid != nid) {
3254 spdif->nid = nid;
3255 val = spdif->ctls;
3256 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3257 }
3258 mutex_unlock(&codec->spdif_mutex);
3259}
3260EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign);
3261
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262/*
Takashi Iwai9a081602008-02-12 18:37:26 +01003263 * SPDIF sharing with analog output
3264 */
3265static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3266 struct snd_ctl_elem_value *ucontrol)
3267{
3268 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3269 ucontrol->value.integer.value[0] = mout->share_spdif;
3270 return 0;
3271}
3272
3273static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3274 struct snd_ctl_elem_value *ucontrol)
3275{
3276 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3277 mout->share_spdif = !!ucontrol->value.integer.value[0];
3278 return 0;
3279}
3280
3281static struct snd_kcontrol_new spdif_share_sw = {
3282 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3283 .name = "IEC958 Default PCM Playback Switch",
3284 .info = snd_ctl_boolean_mono_info,
3285 .get = spdif_share_sw_get,
3286 .put = spdif_share_sw_put,
3287};
3288
Takashi Iwaid5191e52009-11-16 14:58:17 +01003289/**
3290 * snd_hda_create_spdif_share_sw - create Default PCM switch
3291 * @codec: the HDA codec
3292 * @mout: multi-out instance
3293 */
Takashi Iwai9a081602008-02-12 18:37:26 +01003294int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3295 struct hda_multi_out *mout)
3296{
3297 if (!mout->dig_out_nid)
3298 return 0;
3299 /* ATTENTION: here mout is passed as private_data, instead of codec */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003300 return snd_hda_ctl_add(codec, mout->dig_out_nid,
3301 snd_ctl_new1(&spdif_share_sw, mout));
Takashi Iwai9a081602008-02-12 18:37:26 +01003302}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003303EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01003304
3305/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 * SPDIF input
3307 */
3308
3309#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3310
Takashi Iwai0ba21762007-04-16 11:29:14 +02003311static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3312 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313{
3314 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3315
3316 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3317 return 0;
3318}
3319
Takashi Iwai0ba21762007-04-16 11:29:14 +02003320static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3321 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322{
3323 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3324 hda_nid_t nid = kcontrol->private_value;
3325 unsigned int val = !!ucontrol->value.integer.value[0];
3326 int change;
3327
Ingo Molnar62932df2006-01-16 16:34:20 +01003328 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003330 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003332 snd_hda_codec_write_cache(codec, nid, 0,
3333 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003335 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 return change;
3337}
3338
Takashi Iwai0ba21762007-04-16 11:29:14 +02003339static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3340 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341{
3342 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3343 hda_nid_t nid = kcontrol->private_value;
3344 unsigned short val;
3345 unsigned int sbits;
3346
Andrew Paprocki3982d172007-12-19 12:13:44 +01003347 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 sbits = convert_to_spdif_status(val);
3349 ucontrol->value.iec958.status[0] = sbits;
3350 ucontrol->value.iec958.status[1] = sbits >> 8;
3351 ucontrol->value.iec958.status[2] = sbits >> 16;
3352 ucontrol->value.iec958.status[3] = sbits >> 24;
3353 return 0;
3354}
3355
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003356static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 {
3358 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003359 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 .info = snd_hda_spdif_in_switch_info,
3361 .get = snd_hda_spdif_in_switch_get,
3362 .put = snd_hda_spdif_in_switch_put,
3363 },
3364 {
3365 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3366 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003367 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 .info = snd_hda_spdif_mask_info,
3369 .get = snd_hda_spdif_in_status_get,
3370 },
3371 { } /* end */
3372};
3373
3374/**
3375 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3376 * @codec: the HDA codec
3377 * @nid: audio in widget NID
3378 *
3379 * Creates controls related with the SPDIF input.
3380 * Called from each patch supporting the SPDIF in.
3381 *
3382 * Returns 0 if successful, or a negative error code.
3383 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003384int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385{
3386 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003387 struct snd_kcontrol *kctl;
3388 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003389 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390
Takashi Iwaidcda5802012-10-12 17:24:51 +02003391 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003392 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003393 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
3394 return -EBUSY;
3395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3397 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01003398 if (!kctl)
3399 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003401 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003402 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 return err;
3404 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003405 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01003406 snd_hda_codec_read(codec, nid, 0,
3407 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02003408 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 return 0;
3410}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003411EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412
Takashi Iwai2a439522011-07-26 09:52:50 +02003413#ifdef CONFIG_PM
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003414/*
3415 * command cache
3416 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003418/* build a 32bit cache key with the widget id and the command parameter */
3419#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3420#define get_cmd_cache_nid(key) ((key) & 0xff)
3421#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3422
3423/**
3424 * snd_hda_codec_write_cache - send a single command with caching
3425 * @codec: the HDA codec
3426 * @nid: NID to send the command
3427 * @direct: direct flag
3428 * @verb: the verb to send
3429 * @parm: the parameter for the verb
3430 *
3431 * Send a single command without waiting for response.
3432 *
3433 * Returns 0 if successful, or a negative error code.
3434 */
3435int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
3436 int direct, unsigned int verb, unsigned int parm)
3437{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003438 int err = snd_hda_codec_write(codec, nid, direct, verb, parm);
3439 struct hda_cache_head *c;
3440 u32 key;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003441
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003442 if (err < 0)
3443 return err;
3444 /* parm may contain the verb stuff for get/set amp */
3445 verb = verb | (parm >> 8);
3446 parm &= 0xff;
3447 key = build_cmd_cache_key(nid, verb);
3448 mutex_lock(&codec->bus->cmd_mutex);
3449 c = get_alloc_hash(&codec->cmd_cache, key);
3450 if (c)
3451 c->val = parm;
3452 mutex_unlock(&codec->bus->cmd_mutex);
3453 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003454}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003455EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003456
Takashi Iwaid5191e52009-11-16 14:58:17 +01003457/**
Takashi Iwaia68d5a542010-03-30 18:03:44 +02003458 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3459 * @codec: the HDA codec
3460 * @nid: NID to send the command
3461 * @direct: direct flag
3462 * @verb: the verb to send
3463 * @parm: the parameter for the verb
3464 *
3465 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3466 * command if the parameter is already identical with the cached value.
3467 * If not, it sends the command and refreshes the cache.
3468 *
3469 * Returns 0 if successful, or a negative error code.
3470 */
3471int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
3472 int direct, unsigned int verb, unsigned int parm)
3473{
3474 struct hda_cache_head *c;
3475 u32 key;
3476
3477 /* parm may contain the verb stuff for get/set amp */
3478 verb = verb | (parm >> 8);
3479 parm &= 0xff;
3480 key = build_cmd_cache_key(nid, verb);
3481 mutex_lock(&codec->bus->cmd_mutex);
3482 c = get_hash(&codec->cmd_cache, key);
3483 if (c && c->val == parm) {
3484 mutex_unlock(&codec->bus->cmd_mutex);
3485 return 0;
3486 }
3487 mutex_unlock(&codec->bus->cmd_mutex);
3488 return snd_hda_codec_write_cache(codec, nid, direct, verb, parm);
3489}
3490EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
3491
3492/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003493 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3494 * @codec: HD-audio codec
3495 *
3496 * Execute all verbs recorded in the command caches to resume.
3497 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003498void snd_hda_codec_resume_cache(struct hda_codec *codec)
3499{
Takashi Iwai603c4012008-07-30 15:01:44 +02003500 struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003501 int i;
3502
Takashi Iwai603c4012008-07-30 15:01:44 +02003503 for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003504 u32 key = buffer->key;
3505 if (!key)
3506 continue;
3507 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3508 get_cmd_cache_cmd(key), buffer->val);
3509 }
3510}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003511EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003512
3513/**
3514 * snd_hda_sequence_write_cache - sequence writes with caching
3515 * @codec: the HDA codec
3516 * @seq: VERB array to send
3517 *
3518 * Send the commands sequentially from the given array.
3519 * Thte commands are recorded on cache for power-save and resume.
3520 * The array must be terminated with NID=0.
3521 */
3522void snd_hda_sequence_write_cache(struct hda_codec *codec,
3523 const struct hda_verb *seq)
3524{
3525 for (; seq->nid; seq++)
3526 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3527 seq->param);
3528}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003529EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
Takashi Iwai2a439522011-07-26 09:52:50 +02003530#endif /* CONFIG_PM */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003531
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003532void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
3533 unsigned int power_state,
3534 bool eapd_workaround)
Takashi Iwai54d17402005-11-21 16:33:22 +01003535{
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003536 hda_nid_t nid = codec->start_nid;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003537 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01003538
Takashi Iwaicb53c622007-08-10 17:21:45 +02003539 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003540 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003541 if (!(wcaps & AC_WCAP_POWER))
3542 continue;
3543 /* don't power down the widget if it controls eapd and
3544 * EAPD_BTLENABLE is set.
3545 */
3546 if (eapd_workaround && power_state == AC_PWRST_D3 &&
3547 get_wcaps_type(wcaps) == AC_WID_PIN &&
3548 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3549 int eapd = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003550 AC_VERB_GET_EAPD_BTLENABLE, 0);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003551 if (eapd & 0x02)
3552 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003553 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003554 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
3555 power_state);
Takashi Iwai54d17402005-11-21 16:33:22 +01003556 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003557}
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003558EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all);
3559
3560/*
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003561 * supported power states check
3562 */
3563static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg,
3564 unsigned int power_state)
3565{
3566 int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE);
3567
Mengdong Line037cb42012-08-10 14:11:58 +02003568 if (sup == -1)
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003569 return false;
3570 if (sup & power_state)
3571 return true;
3572 else
3573 return false;
3574}
3575
3576/*
Takashi Iwai432c6412012-08-28 09:59:20 -07003577 * wait until the state is reached, returns the current state
3578 */
3579static unsigned int hda_sync_power_state(struct hda_codec *codec,
3580 hda_nid_t fg,
3581 unsigned int power_state)
3582{
3583 unsigned long end_time = jiffies + msecs_to_jiffies(500);
3584 unsigned int state, actual_state;
3585
3586 for (;;) {
3587 state = snd_hda_codec_read(codec, fg, 0,
3588 AC_VERB_GET_POWER_STATE, 0);
3589 if (state & AC_PWRST_ERROR)
3590 break;
3591 actual_state = (state >> 4) & 0x0f;
3592 if (actual_state == power_state)
3593 break;
3594 if (time_after_eq(jiffies, end_time))
3595 break;
3596 /* wait until the codec reachs to the target state */
3597 msleep(1);
3598 }
3599 return state;
3600}
3601
3602/*
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003603 * set power state of the codec, and return the power state
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003604 */
Takashi Iwaid8193872012-08-31 07:54:38 -07003605static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003606 unsigned int power_state)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003607{
Takashi Iwaid8193872012-08-31 07:54:38 -07003608 hda_nid_t fg = codec->afg ? codec->afg : codec->mfg;
Wang Xingchao09617ce2012-06-08 10:26:08 +08003609 int count;
3610 unsigned int state;
3611
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003612 /* this delay seems necessary to avoid click noise at power-down */
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003613 if (power_state == AC_PWRST_D3) {
3614 /* transition time less than 10ms for power down */
Takashi Iwai983f6b92012-08-28 09:18:01 -07003615 msleep(codec->epss ? 10 : 100);
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003616 }
Wang Xingchao09617ce2012-06-08 10:26:08 +08003617
3618 /* repeat power states setting at most 10 times*/
3619 for (count = 0; count < 10; count++) {
Takashi Iwai432c6412012-08-28 09:59:20 -07003620 if (codec->patch_ops.set_power_state)
3621 codec->patch_ops.set_power_state(codec, fg,
3622 power_state);
3623 else {
3624 snd_hda_codec_read(codec, fg, 0,
3625 AC_VERB_SET_POWER_STATE,
3626 power_state);
3627 snd_hda_codec_set_power_to_all(codec, fg, power_state,
3628 true);
3629 }
3630 state = hda_sync_power_state(codec, fg, power_state);
Wang Xingchao09617ce2012-06-08 10:26:08 +08003631 if (!(state & AC_PWRST_ERROR))
3632 break;
3633 }
Mengdong Linb8dfc4622012-08-23 17:32:30 +08003634
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003635 return state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003636}
Takashi Iwai54d17402005-11-21 16:33:22 +01003637
Takashi Iwai11aeff02008-07-30 15:01:46 +02003638#ifdef CONFIG_SND_HDA_HWDEP
3639/* execute additional init verbs */
3640static void hda_exec_init_verbs(struct hda_codec *codec)
3641{
3642 if (codec->init_verbs.list)
3643 snd_hda_sequence_write(codec, codec->init_verbs.list);
3644}
3645#else
3646static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3647#endif
3648
Takashi Iwai2a439522011-07-26 09:52:50 +02003649#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003650/*
3651 * call suspend and power-down; used both from PM and power-save
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003652 * this function returns the power state in the end
Takashi Iwaicb53c622007-08-10 17:21:45 +02003653 */
Dylan Reidd17344b2012-09-28 15:57:01 -07003654static unsigned int hda_call_codec_suspend(struct hda_codec *codec, bool in_wq)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003655{
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003656 unsigned int state;
3657
Takashi Iwaicb53c622007-08-10 17:21:45 +02003658 if (codec->patch_ops.suspend)
Takashi Iwai68cb2b52012-07-02 15:20:37 +02003659 codec->patch_ops.suspend(codec);
Takashi Iwaieb541332010-08-06 13:48:11 +02003660 hda_cleanup_all_streams(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07003661 state = hda_set_power_state(codec, AC_PWRST_D3);
Dylan Reidd17344b2012-09-28 15:57:01 -07003662 /* Cancel delayed work if we aren't currently running from it. */
3663 if (!in_wq)
3664 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02003665 spin_lock(&codec->power_lock);
3666 snd_hda_update_power_acct(codec);
3667 trace_hda_power_down(codec);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02003668 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02003669 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003670 codec->power_jiffies = jiffies;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02003671 spin_unlock(&codec->power_lock);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003672 return state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003673}
3674
3675/*
3676 * kick up codec; used both from PM and power-save
3677 */
3678static void hda_call_codec_resume(struct hda_codec *codec)
3679{
Takashi Iwai7f308302012-05-08 16:52:23 +02003680 /* set as if powered on for avoiding re-entering the resume
3681 * in the resume / power-save sequence
3682 */
3683 hda_keep_power_on(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07003684 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwai3be14142009-02-20 14:11:16 +01003685 restore_pincfgs(codec); /* restore all current pin configs */
Takashi Iwaiac0547d2010-07-05 16:50:13 +02003686 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02003687 hda_exec_init_verbs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003688 if (codec->patch_ops.resume)
3689 codec->patch_ops.resume(codec);
3690 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02003691 if (codec->patch_ops.init)
3692 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003693 snd_hda_codec_resume_amp(codec);
3694 snd_hda_codec_resume_cache(codec);
3695 }
David Henningsson26a6cb62012-10-09 15:04:21 +02003696
3697 if (codec->jackpoll_interval)
3698 hda_jackpoll_work(&codec->jackpoll_work.work);
3699 else {
3700 snd_hda_jack_set_dirty_all(codec);
3701 snd_hda_jack_report_sync(codec);
3702 }
Takashi Iwai7f308302012-05-08 16:52:23 +02003703 snd_hda_power_down(codec); /* flag down before returning */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003704}
Takashi Iwai2a439522011-07-26 09:52:50 +02003705#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003706
Takashi Iwai54d17402005-11-21 16:33:22 +01003707
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708/**
3709 * snd_hda_build_controls - build mixer controls
3710 * @bus: the BUS
3711 *
3712 * Creates mixer controls for each codec included in the bus.
3713 *
3714 * Returns 0 if successful, otherwise a negative error code.
3715 */
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003716int /*__devinit*/ snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003718 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719
Takashi Iwai0ba21762007-04-16 11:29:14 +02003720 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003721 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003722 if (err < 0) {
Takashi Iwai28d1a852010-03-15 09:05:46 +01003723 printk(KERN_ERR "hda_codec: cannot build controls "
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003724 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003725 err = snd_hda_codec_reset(codec);
3726 if (err < 0) {
3727 printk(KERN_ERR
3728 "hda_codec: cannot revert codec\n");
3729 return err;
3730 }
3731 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003733 return 0;
3734}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003735EXPORT_SYMBOL_HDA(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003737/*
3738 * add standard channel maps if not specified
3739 */
3740static int add_std_chmaps(struct hda_codec *codec)
3741{
3742 int i, str, err;
3743
3744 for (i = 0; i < codec->num_pcms; i++) {
3745 for (str = 0; str < 2; str++) {
3746 struct snd_pcm *pcm = codec->pcm_info[i].pcm;
3747 struct hda_pcm_stream *hinfo =
3748 &codec->pcm_info[i].stream[str];
3749 struct snd_pcm_chmap *chmap;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003750 const struct snd_pcm_chmap_elem *elem;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003751
3752 if (codec->pcm_info[i].own_chmap)
3753 continue;
3754 if (!pcm || !hinfo->substreams)
3755 continue;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003756 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
3757 err = snd_pcm_add_chmap_ctls(pcm, str, elem,
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003758 hinfo->channels_max,
3759 0, &chmap);
3760 if (err < 0)
3761 return err;
3762 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
3763 }
3764 }
3765 return 0;
3766}
3767
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003768/* default channel maps for 2.1 speakers;
3769 * since HD-audio supports only stereo, odd number channels are omitted
3770 */
3771const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
3772 { .channels = 2,
3773 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
3774 { .channels = 4,
3775 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
3776 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
3777 { }
3778};
3779EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
3780
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003781int snd_hda_codec_build_controls(struct hda_codec *codec)
3782{
3783 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02003784 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003785 /* continue to initialize... */
3786 if (codec->patch_ops.init)
3787 err = codec->patch_ops.init(codec);
3788 if (!err && codec->patch_ops.build_controls)
3789 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003790 if (err < 0)
3791 return err;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003792
3793 /* we create chmaps here instead of build_pcms */
3794 err = add_std_chmaps(codec);
3795 if (err < 0)
3796 return err;
3797
David Henningsson26a6cb62012-10-09 15:04:21 +02003798 if (codec->jackpoll_interval)
3799 hda_jackpoll_work(&codec->jackpoll_work.work);
3800 else
3801 snd_hda_jack_report_sync(codec); /* call at the last init point */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802 return 0;
3803}
3804
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805/*
3806 * stream formats
3807 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02003808struct hda_rate_tbl {
3809 unsigned int hz;
3810 unsigned int alsa_bits;
3811 unsigned int hda_fmt;
3812};
3813
Takashi Iwai92f10b32010-08-03 14:21:00 +02003814/* rate = base * mult / div */
3815#define HDA_RATE(base, mult, div) \
3816 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
3817 (((div) - 1) << AC_FMT_DIV_SHIFT))
3818
Takashi Iwaibefdf312005-08-22 13:57:55 +02003819static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003821
3822 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003823 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
3824 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
3825 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
3826 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
3827 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
3828 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
3829 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
3830 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
3831 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
3832 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
3833 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003834#define AC_PAR_PCM_RATE_BITS 11
3835 /* up to bits 10, 384kHZ isn't supported properly */
3836
3837 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003838 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003839
Takashi Iwaibefdf312005-08-22 13:57:55 +02003840 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841};
3842
3843/**
3844 * snd_hda_calc_stream_format - calculate format bitset
3845 * @rate: the sample rate
3846 * @channels: the number of channels
3847 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3848 * @maxbps: the max. bps
3849 *
3850 * Calculate the format bitset from the given rate, channels and th PCM format.
3851 *
3852 * Return zero if invalid.
3853 */
3854unsigned int snd_hda_calc_stream_format(unsigned int rate,
3855 unsigned int channels,
3856 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03003857 unsigned int maxbps,
3858 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859{
3860 int i;
3861 unsigned int val = 0;
3862
Takashi Iwaibefdf312005-08-22 13:57:55 +02003863 for (i = 0; rate_bits[i].hz; i++)
3864 if (rate_bits[i].hz == rate) {
3865 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866 break;
3867 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003868 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869 snd_printdd("invalid rate %d\n", rate);
3870 return 0;
3871 }
3872
3873 if (channels == 0 || channels > 8) {
3874 snd_printdd("invalid channels %d\n", channels);
3875 return 0;
3876 }
3877 val |= channels - 1;
3878
3879 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003880 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003881 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003882 break;
3883 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003884 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003885 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886 case 20:
3887 case 24:
3888 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003889 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003890 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003892 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02003894 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895 break;
3896 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003897 snd_printdd("invalid format width %d\n",
3898 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899 return 0;
3900 }
3901
Anssi Hannula32c168c2010-08-03 13:28:57 +03003902 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003903 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03003904
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905 return val;
3906}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003907EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003909static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid,
3910 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003911{
3912 unsigned int val = 0;
3913 if (nid != codec->afg &&
3914 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
3915 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
3916 if (!val || val == -1)
3917 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
3918 if (!val || val == -1)
3919 return 0;
3920 return val;
3921}
3922
3923static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3924{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003925 return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003926 get_pcm_param);
3927}
3928
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003929static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid,
3930 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003931{
3932 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
3933 if (!streams || streams == -1)
3934 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
3935 if (!streams || streams == -1)
3936 return 0;
3937 return streams;
3938}
3939
3940static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
3941{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02003942 return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003943 get_stream_param);
3944}
3945
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946/**
3947 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
3948 * @codec: the HDA codec
3949 * @nid: NID to query
3950 * @ratesp: the pointer to store the detected rate bitflags
3951 * @formatsp: the pointer to store the detected formats
3952 * @bpsp: the pointer to store the detected format widths
3953 *
3954 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
3955 * or @bsps argument is ignored.
3956 *
3957 * Returns 0 if successful, otherwise a negative error code.
3958 */
Stephen Warren384a48d2011-06-01 11:14:21 -06003959int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
3961{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003962 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003964 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003965 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966
3967 if (ratesp) {
3968 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003969 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02003971 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003973 if (rates == 0) {
3974 snd_printk(KERN_ERR "hda_codec: rates == 0 "
3975 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
3976 nid, val,
3977 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
3978 return -EIO;
3979 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980 *ratesp = rates;
3981 }
3982
3983 if (formatsp || bpsp) {
3984 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003985 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01003987 streams = query_stream_param(codec, nid);
3988 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990
3991 bps = 0;
3992 if (streams & AC_SUPFMT_PCM) {
3993 if (val & AC_SUPPCM_BITS_8) {
3994 formats |= SNDRV_PCM_FMTBIT_U8;
3995 bps = 8;
3996 }
3997 if (val & AC_SUPPCM_BITS_16) {
3998 formats |= SNDRV_PCM_FMTBIT_S16_LE;
3999 bps = 16;
4000 }
4001 if (wcaps & AC_WCAP_DIGITAL) {
4002 if (val & AC_SUPPCM_BITS_32)
4003 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
4004 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
4005 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4006 if (val & AC_SUPPCM_BITS_24)
4007 bps = 24;
4008 else if (val & AC_SUPPCM_BITS_20)
4009 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004010 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
4011 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4013 if (val & AC_SUPPCM_BITS_32)
4014 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015 else if (val & AC_SUPPCM_BITS_24)
4016 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02004017 else if (val & AC_SUPPCM_BITS_20)
4018 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019 }
4020 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004021#if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
Takashi Iwaib5025c52009-07-01 18:05:27 +02004022 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004024 if (!bps)
4025 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02004026 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004027#endif
Takashi Iwaib5025c52009-07-01 18:05:27 +02004028 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02004029 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030 /* temporary hack: we have still no proper support
4031 * for the direct AC3 stream...
4032 */
4033 formats |= SNDRV_PCM_FMTBIT_U8;
4034 bps = 8;
4035 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004036 if (formats == 0) {
4037 snd_printk(KERN_ERR "hda_codec: formats == 0 "
4038 "(nid=0x%x, val=0x%x, ovrd=%i, "
4039 "streams=0x%x)\n",
4040 nid, val,
4041 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
4042 streams);
4043 return -EIO;
4044 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045 if (formatsp)
4046 *formatsp = formats;
4047 if (bpsp)
4048 *bpsp = bps;
4049 }
4050
4051 return 0;
4052}
Stephen Warren384a48d2011-06-01 11:14:21 -06004053EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054
4055/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01004056 * snd_hda_is_supported_format - Check the validity of the format
4057 * @codec: HD-audio codec
4058 * @nid: NID to check
4059 * @format: the HD-audio format value to check
4060 *
4061 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062 *
4063 * Returns 1 if supported, 0 if not.
4064 */
4065int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
4066 unsigned int format)
4067{
4068 int i;
4069 unsigned int val = 0, rate, stream;
4070
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004071 val = query_pcm_param(codec, nid);
4072 if (!val)
4073 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074
4075 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004076 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02004077 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078 if (val & (1 << i))
4079 break;
4080 return 0;
4081 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004082 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083 return 0;
4084
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004085 stream = query_stream_param(codec, nid);
4086 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087 return 0;
4088
4089 if (stream & AC_SUPFMT_PCM) {
4090 switch (format & 0xf0) {
4091 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004092 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093 return 0;
4094 break;
4095 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004096 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097 return 0;
4098 break;
4099 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004100 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101 return 0;
4102 break;
4103 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004104 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105 return 0;
4106 break;
4107 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004108 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109 return 0;
4110 break;
4111 default:
4112 return 0;
4113 }
4114 } else {
4115 /* FIXME: check for float32 and AC3? */
4116 }
4117
4118 return 1;
4119}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004120EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121
4122/*
4123 * PCM stuff
4124 */
4125static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
4126 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004127 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128{
4129 return 0;
4130}
4131
4132static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
4133 struct hda_codec *codec,
4134 unsigned int stream_tag,
4135 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004136 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137{
4138 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4139 return 0;
4140}
4141
4142static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
4143 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004144 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145{
Takashi Iwai888afa12008-03-18 09:57:50 +01004146 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147 return 0;
4148}
4149
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004150static int set_pcm_default_values(struct hda_codec *codec,
4151 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004153 int err;
4154
Takashi Iwai0ba21762007-04-16 11:29:14 +02004155 /* query support PCM information from the given NID */
4156 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004157 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02004158 info->rates ? NULL : &info->rates,
4159 info->formats ? NULL : &info->formats,
4160 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004161 if (err < 0)
4162 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 }
4164 if (info->ops.open == NULL)
4165 info->ops.open = hda_pcm_default_open_close;
4166 if (info->ops.close == NULL)
4167 info->ops.close = hda_pcm_default_open_close;
4168 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004169 if (snd_BUG_ON(!info->nid))
4170 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171 info->ops.prepare = hda_pcm_default_prepare;
4172 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004174 if (snd_BUG_ON(!info->nid))
4175 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176 info->ops.cleanup = hda_pcm_default_cleanup;
4177 }
4178 return 0;
4179}
4180
Takashi Iwaieb541332010-08-06 13:48:11 +02004181/*
4182 * codec prepare/cleanup entries
4183 */
4184int snd_hda_codec_prepare(struct hda_codec *codec,
4185 struct hda_pcm_stream *hinfo,
4186 unsigned int stream,
4187 unsigned int format,
4188 struct snd_pcm_substream *substream)
4189{
4190 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004191 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004192 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
4193 if (ret >= 0)
4194 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004195 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004196 return ret;
4197}
4198EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
4199
4200void snd_hda_codec_cleanup(struct hda_codec *codec,
4201 struct hda_pcm_stream *hinfo,
4202 struct snd_pcm_substream *substream)
4203{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004204 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004205 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004206 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004207}
4208EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
4209
Takashi Iwaid5191e52009-11-16 14:58:17 +01004210/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01004211const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
4212 "Audio", "SPDIF", "HDMI", "Modem"
4213};
4214
Takashi Iwai176d5332008-07-30 15:01:44 +02004215/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004216 * get the empty PCM device number to assign
Takashi Iwaic8936222010-01-28 17:08:53 +01004217 *
4218 * note the max device number is limited by HDA_MAX_PCMS, currently 10
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004219 */
4220static int get_empty_pcm_device(struct hda_bus *bus, int type)
4221{
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004222 /* audio device indices; not linear to keep compatibility */
4223 static int audio_idx[HDA_PCM_NTYPES][5] = {
4224 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
4225 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01004226 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004227 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004228 };
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004229 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004230
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004231 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004232 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
4233 return -EINVAL;
4234 }
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004235
4236 for (i = 0; audio_idx[type][i] >= 0 ; i++)
4237 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
4238 return audio_idx[type][i];
4239
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004240 /* non-fixed slots starting from 10 */
4241 for (i = 10; i < 32; i++) {
4242 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4243 return i;
4244 }
4245
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004246 snd_printk(KERN_WARNING "Too many %s devices\n",
4247 snd_hda_pcm_type_name[type]);
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004248 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004249}
4250
4251/*
Takashi Iwai176d5332008-07-30 15:01:44 +02004252 * attach a new PCM stream
4253 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004254static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02004255{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004256 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02004257 struct hda_pcm_stream *info;
4258 int stream, err;
4259
Takashi Iwaib91f0802008-11-04 08:43:08 +01004260 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02004261 return -EINVAL;
4262 for (stream = 0; stream < 2; stream++) {
4263 info = &pcm->stream[stream];
4264 if (info->substreams) {
4265 err = set_pcm_default_values(codec, info);
4266 if (err < 0)
4267 return err;
4268 }
4269 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004270 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02004271}
4272
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004273/* assign all PCMs of the given codec */
4274int snd_hda_codec_build_pcms(struct hda_codec *codec)
4275{
4276 unsigned int pcm;
4277 int err;
4278
4279 if (!codec->num_pcms) {
4280 if (!codec->patch_ops.build_pcms)
4281 return 0;
4282 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004283 if (err < 0) {
4284 printk(KERN_ERR "hda_codec: cannot build PCMs"
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004285 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004286 err = snd_hda_codec_reset(codec);
4287 if (err < 0) {
4288 printk(KERN_ERR
4289 "hda_codec: cannot revert codec\n");
4290 return err;
4291 }
4292 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004293 }
4294 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
4295 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
4296 int dev;
4297
4298 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01004299 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004300
4301 if (!cpcm->pcm) {
4302 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
4303 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004304 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004305 cpcm->device = dev;
4306 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004307 if (err < 0) {
4308 printk(KERN_ERR "hda_codec: cannot attach "
4309 "PCM stream %d for codec #%d\n",
4310 dev, codec->addr);
4311 continue; /* no fatal error */
4312 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004313 }
4314 }
4315 return 0;
4316}
4317
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318/**
4319 * snd_hda_build_pcms - build PCM information
4320 * @bus: the BUS
4321 *
4322 * Create PCM information for each codec included in the bus.
4323 *
4324 * The build_pcms codec patch is requested to set up codec->num_pcms and
4325 * codec->pcm_info properly. The array is referred by the top-level driver
4326 * to create its PCM instances.
4327 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4328 * callback.
4329 *
4330 * At least, substreams, channels_min and channels_max must be filled for
4331 * each stream. substreams = 0 indicates that the stream doesn't exist.
4332 * When rates and/or formats are zero, the supported values are queried
4333 * from the given nid. The nid is used also by the default ops.prepare
4334 * and ops.cleanup callbacks.
4335 *
4336 * The driver needs to call ops.open in its open callback. Similarly,
4337 * ops.close is supposed to be called in the close callback.
4338 * ops.prepare should be called in the prepare or hw_params callback
4339 * with the proper parameters for set up.
4340 * ops.cleanup should be called in hw_free for clean up of streams.
4341 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004342 * This function returns 0 if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343 */
Takashi Iwai5cb543d2012-08-09 13:49:23 +02004344int snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004346 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347
Takashi Iwai0ba21762007-04-16 11:29:14 +02004348 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004349 int err = snd_hda_codec_build_pcms(codec);
4350 if (err < 0)
4351 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352 }
4353 return 0;
4354}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004355EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357/**
4358 * snd_hda_check_board_config - compare the current codec with the config table
4359 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004360 * @num_configs: number of config enums
4361 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362 * @tbl: configuration table, terminated by null entries
4363 *
4364 * Compares the modelname or PCI subsystem id of the current codec with the
4365 * given configuration table. If a matching entry is found, returns its
4366 * config value (supposed to be 0 or positive).
4367 *
4368 * If no entries are matching, the function returns a negative value.
4369 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02004370int snd_hda_check_board_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004371 int num_configs, const char * const *models,
Takashi Iwai12f288b2007-08-02 15:51:59 +02004372 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373{
Takashi Iwaif44ac832008-07-30 15:01:45 +02004374 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004375 int i;
4376 for (i = 0; i < num_configs; i++) {
4377 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02004378 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004379 snd_printd(KERN_INFO "hda_codec: model '%s' is "
4380 "selected\n", models[i]);
4381 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382 }
4383 }
4384 }
4385
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004386 if (!codec->bus->pci || !tbl)
4387 return -1;
4388
4389 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
4390 if (!tbl)
4391 return -1;
4392 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02004393#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004394 char tmp[10];
4395 const char *model = NULL;
4396 if (models)
4397 model = models[tbl->value];
4398 if (!model) {
4399 sprintf(tmp, "#%d", tbl->value);
4400 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004402 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4403 "for config %x:%x (%s)\n",
4404 model, tbl->subvendor, tbl->subdevice,
4405 (tbl->name ? tbl->name : "Unknown device"));
4406#endif
4407 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408 }
4409 return -1;
4410}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004411EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412
4413/**
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004414 * snd_hda_check_board_codec_sid_config - compare the current codec
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004415 subsystem ID with the
4416 config table
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004417
4418 This is important for Gateway notebooks with SB450 HDA Audio
4419 where the vendor ID of the PCI device is:
4420 ATI Technologies Inc SB450 HDA Audio [1002:437b]
4421 and the vendor/subvendor are found only at the codec.
4422
4423 * @codec: the HDA codec
4424 * @num_configs: number of config enums
4425 * @models: array of model name strings
4426 * @tbl: configuration table, terminated by null entries
4427 *
4428 * Compares the modelname or PCI subsystem id of the current codec with the
4429 * given configuration table. If a matching entry is found, returns its
4430 * config value (supposed to be 0 or positive).
4431 *
4432 * If no entries are matching, the function returns a negative value.
4433 */
4434int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004435 int num_configs, const char * const *models,
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004436 const struct snd_pci_quirk *tbl)
4437{
4438 const struct snd_pci_quirk *q;
4439
4440 /* Search for codec ID */
4441 for (q = tbl; q->subvendor; q++) {
Takashi Iwaie2301a42011-11-22 19:58:56 +01004442 unsigned int mask = 0xffff0000 | q->subdevice_mask;
4443 unsigned int id = (q->subdevice | (q->subvendor << 16)) & mask;
4444 if ((codec->subsystem_id & mask) == id)
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004445 break;
4446 }
4447
4448 if (!q->subvendor)
4449 return -1;
4450
4451 tbl = q;
4452
4453 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwaid94ff6b2009-09-02 00:20:21 +02004454#ifdef CONFIG_SND_DEBUG_VERBOSE
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004455 char tmp[10];
4456 const char *model = NULL;
4457 if (models)
4458 model = models[tbl->value];
4459 if (!model) {
4460 sprintf(tmp, "#%d", tbl->value);
4461 model = tmp;
4462 }
4463 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4464 "for config %x:%x (%s)\n",
4465 model, tbl->subvendor, tbl->subdevice,
4466 (tbl->name ? tbl->name : "Unknown device"));
4467#endif
4468 return tbl->value;
4469 }
4470 return -1;
4471}
4472EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
4473
4474/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475 * snd_hda_add_new_ctls - create controls from the array
4476 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004477 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478 *
4479 * This helper function creates and add new controls in the given array.
4480 * The array must be terminated with an empty entry as terminator.
4481 *
4482 * Returns 0 if successful, or a negative error code.
4483 */
Takashi Iwai031024e2011-05-02 11:29:30 +02004484int snd_hda_add_new_ctls(struct hda_codec *codec,
4485 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01004487 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488
4489 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004490 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004491 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01004492 if (knew->iface == -1) /* skip this codec private value */
4493 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004494 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004495 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02004496 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01004497 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004498 if (addr > 0)
4499 kctl->id.device = addr;
4500 if (idx > 0)
4501 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01004502 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004503 if (!err)
4504 break;
4505 /* try first with another device index corresponding to
4506 * the codec addr; if it still fails (or it's the
4507 * primary codec), then try another control index
4508 */
4509 if (!addr && codec->addr)
4510 addr = codec->addr;
4511 else if (!idx && !knew->index) {
4512 idx = find_empty_mixer_ctl_idx(codec,
Takashi Iwaidcda5802012-10-12 17:24:51 +02004513 knew->name, 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004514 if (idx <= 0)
4515 return err;
4516 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01004517 return err;
4518 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519 }
4520 return 0;
4521}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004522EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523
Takashi Iwai83012a72012-08-24 18:38:08 +02004524#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02004525static void hda_power_work(struct work_struct *work)
4526{
4527 struct hda_codec *codec =
4528 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004529 struct hda_bus *bus = codec->bus;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004530 unsigned int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004531
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004532 spin_lock(&codec->power_lock);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004533 if (codec->power_transition > 0) { /* during power-up sequence? */
4534 spin_unlock(&codec->power_lock);
4535 return;
4536 }
Maxim Levitsky2e492462007-09-03 15:26:57 +02004537 if (!codec->power_on || codec->power_count) {
4538 codec->power_transition = 0;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004539 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004540 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02004541 }
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004542 spin_unlock(&codec->power_lock);
4543
Dylan Reidd17344b2012-09-28 15:57:01 -07004544 state = hda_call_codec_suspend(codec, true);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004545 codec->pm_down_notified = 0;
4546 if (!bus->power_keep_link_on && (state & AC_PWRST_CLK_STOP_OK)) {
4547 codec->pm_down_notified = 1;
Takashi Iwai68467f52012-08-28 09:14:29 -07004548 hda_call_pm_notify(bus, false);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004549 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02004550}
4551
4552static void hda_keep_power_on(struct hda_codec *codec)
4553{
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004554 spin_lock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004555 codec->power_count++;
4556 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004557 codec->power_jiffies = jiffies;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004558 spin_unlock(&codec->power_lock);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004559}
4560
Takashi Iwaid5191e52009-11-16 14:58:17 +01004561/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01004562void snd_hda_update_power_acct(struct hda_codec *codec)
4563{
4564 unsigned long delta = jiffies - codec->power_jiffies;
4565 if (codec->power_on)
4566 codec->power_on_acct += delta;
4567 else
4568 codec->power_off_acct += delta;
4569 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004570}
4571
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004572/* Transition to powered up, if wait_power_down then wait for a pending
4573 * transition to D3 to complete. A pending D3 transition is indicated
4574 * with power_transition == -1. */
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004575/* call this with codec->power_lock held! */
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004576static void __snd_hda_power_up(struct hda_codec *codec, bool wait_power_down)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004577{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004578 struct hda_bus *bus = codec->bus;
4579
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004580 /* Return if power_on or transitioning to power_on, unless currently
4581 * powering down. */
4582 if ((codec->power_on || codec->power_transition > 0) &&
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004583 !(wait_power_down && codec->power_transition < 0))
Takashi Iwaicb53c622007-08-10 17:21:45 +02004584 return;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004585 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004586
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004587 cancel_delayed_work_sync(&codec->power_work);
4588
4589 spin_lock(&codec->power_lock);
Dylan Reidb43d2242012-06-21 21:51:22 -07004590 /* If the power down delayed work was cancelled above before starting,
4591 * then there is no need to go through power up here.
4592 */
4593 if (codec->power_on) {
Takashi Iwai535b6c52012-08-20 21:25:22 +02004594 if (codec->power_transition < 0)
4595 codec->power_transition = 0;
Dylan Reidb43d2242012-06-21 21:51:22 -07004596 return;
4597 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004598
Takashi Iwaid66fee52011-08-02 15:39:31 +02004599 trace_hda_power_up(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004600 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004601 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004602 codec->power_jiffies = jiffies;
Takashi Iwai7f308302012-05-08 16:52:23 +02004603 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004604 spin_unlock(&codec->power_lock);
4605
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004606 if (codec->pm_down_notified) {
4607 codec->pm_down_notified = 0;
Takashi Iwai68467f52012-08-28 09:14:29 -07004608 hda_call_pm_notify(bus, true);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004609 }
4610
Takashi Iwaicb53c622007-08-10 17:21:45 +02004611 hda_call_codec_resume(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004612
4613 spin_lock(&codec->power_lock);
Takashi Iwaia221e282007-08-16 16:35:33 +02004614 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004615}
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004616
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004617#define power_save(codec) \
4618 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004619
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004620/* Transition to powered down */
4621static void __snd_hda_power_down(struct hda_codec *codec)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004622{
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004623 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004624 return;
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004625
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004626 if (power_save(codec)) {
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004627 codec->power_transition = -1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01004628 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004629 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02004630 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004631}
4632
4633/**
4634 * snd_hda_power_save - Power-up/down/sync the codec
4635 * @codec: HD-audio codec
4636 * @delta: the counter delta to change
4637 *
4638 * Change the power-up counter via @delta, and power up or down the hardware
4639 * appropriately. For the power-down, queue to the delayed action.
4640 * Passing zero to @delta means to synchronize the power state.
4641 */
4642void snd_hda_power_save(struct hda_codec *codec, int delta, bool d3wait)
4643{
4644 spin_lock(&codec->power_lock);
4645 codec->power_count += delta;
4646 trace_hda_power_count(codec);
4647 if (delta > 0)
4648 __snd_hda_power_up(codec, d3wait);
4649 else
4650 __snd_hda_power_down(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004651 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004652}
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004653EXPORT_SYMBOL_HDA(snd_hda_power_save);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004654
Takashi Iwaid5191e52009-11-16 14:58:17 +01004655/**
4656 * snd_hda_check_amp_list_power - Check the amp list and update the power
4657 * @codec: HD-audio codec
4658 * @check: the object containing an AMP list and the status
4659 * @nid: NID to check / update
4660 *
4661 * Check whether the given NID is in the amp list. If it's in the list,
4662 * check the current AMP status, and update the the power-status according
4663 * to the mute status.
4664 *
4665 * This function is supposed to be set or called from the check_power_status
4666 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004667 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004668int snd_hda_check_amp_list_power(struct hda_codec *codec,
4669 struct hda_loopback_check *check,
4670 hda_nid_t nid)
4671{
Takashi Iwai031024e2011-05-02 11:29:30 +02004672 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004673 int ch, v;
4674
4675 if (!check->amplist)
4676 return 0;
4677 for (p = check->amplist; p->nid; p++) {
4678 if (p->nid == nid)
4679 break;
4680 }
4681 if (!p->nid)
4682 return 0; /* nothing changed */
4683
4684 for (p = check->amplist; p->nid; p++) {
4685 for (ch = 0; ch < 2; ch++) {
4686 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4687 p->idx);
4688 if (!(v & HDA_AMP_MUTE) && v > 0) {
4689 if (!check->power_on) {
4690 check->power_on = 1;
4691 snd_hda_power_up(codec);
4692 }
4693 return 1;
4694 }
4695 }
4696 }
4697 if (check->power_on) {
4698 check->power_on = 0;
4699 snd_hda_power_down(codec);
4700 }
4701 return 0;
4702}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004703EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004704#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004706/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004707 * Channel mode helper
4708 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004709
4710/**
4711 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
4712 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004713int snd_hda_ch_mode_info(struct hda_codec *codec,
4714 struct snd_ctl_elem_info *uinfo,
4715 const struct hda_channel_mode *chmode,
4716 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004717{
4718 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4719 uinfo->count = 1;
4720 uinfo->value.enumerated.items = num_chmodes;
4721 if (uinfo->value.enumerated.item >= num_chmodes)
4722 uinfo->value.enumerated.item = num_chmodes - 1;
4723 sprintf(uinfo->value.enumerated.name, "%dch",
4724 chmode[uinfo->value.enumerated.item].channels);
4725 return 0;
4726}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004727EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004728
Takashi Iwaid5191e52009-11-16 14:58:17 +01004729/**
4730 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
4731 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004732int snd_hda_ch_mode_get(struct hda_codec *codec,
4733 struct snd_ctl_elem_value *ucontrol,
4734 const struct hda_channel_mode *chmode,
4735 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004736 int max_channels)
4737{
4738 int i;
4739
4740 for (i = 0; i < num_chmodes; i++) {
4741 if (max_channels == chmode[i].channels) {
4742 ucontrol->value.enumerated.item[0] = i;
4743 break;
4744 }
4745 }
4746 return 0;
4747}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004748EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004749
Takashi Iwaid5191e52009-11-16 14:58:17 +01004750/**
4751 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
4752 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004753int snd_hda_ch_mode_put(struct hda_codec *codec,
4754 struct snd_ctl_elem_value *ucontrol,
4755 const struct hda_channel_mode *chmode,
4756 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004757 int *max_channelsp)
4758{
4759 unsigned int mode;
4760
4761 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01004762 if (mode >= num_chmodes)
4763 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004764 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004765 return 0;
4766 /* change the current channel setting */
4767 *max_channelsp = chmode[mode].channels;
4768 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004769 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004770 return 1;
4771}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004772EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004773
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774/*
4775 * input MUX helper
4776 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004777
4778/**
4779 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
4780 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004781int snd_hda_input_mux_info(const struct hda_input_mux *imux,
4782 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783{
4784 unsigned int index;
4785
4786 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4787 uinfo->count = 1;
4788 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004789 if (!imux->num_items)
4790 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791 index = uinfo->value.enumerated.item;
4792 if (index >= imux->num_items)
4793 index = imux->num_items - 1;
4794 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
4795 return 0;
4796}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004797EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798
Takashi Iwaid5191e52009-11-16 14:58:17 +01004799/**
4800 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
4801 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004802int snd_hda_input_mux_put(struct hda_codec *codec,
4803 const struct hda_input_mux *imux,
4804 struct snd_ctl_elem_value *ucontrol,
4805 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806 unsigned int *cur_val)
4807{
4808 unsigned int idx;
4809
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004810 if (!imux->num_items)
4811 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812 idx = ucontrol->value.enumerated.item[0];
4813 if (idx >= imux->num_items)
4814 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004815 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004817 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
4818 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819 *cur_val = idx;
4820 return 1;
4821}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004822EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823
4824
4825/*
4826 * Multi-channel / digital-out PCM helper functions
4827 */
4828
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004829/* setup SPDIF output stream */
4830static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
4831 unsigned int stream_tag, unsigned int format)
4832{
Laurence Darby3bef1c32012-11-03 17:00:06 +00004833 struct hda_spdif_out *spdif;
4834 unsigned int curr_fmt;
4835 bool reset;
Stephen Warren7c935972011-06-01 11:14:17 -06004836
Laurence Darby3bef1c32012-11-03 17:00:06 +00004837 spdif = snd_hda_spdif_out_of_nid(codec, nid);
4838 curr_fmt = snd_hda_codec_read(codec, nid, 0,
4839 AC_VERB_GET_STREAM_FORMAT, 0);
4840 reset = codec->spdif_status_reset &&
4841 (spdif->ctls & AC_DIG1_ENABLE) &&
4842 curr_fmt != format;
4843
4844 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
4845 updated */
4846 if (reset)
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004847 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06004848 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02004849 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004850 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02004851 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004852 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004853 for (d = codec->slave_dig_outs; *d; d++)
4854 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
4855 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004856 }
Takashi Iwai2f728532008-09-25 16:32:41 +02004857 /* turn on again (if needed) */
Laurence Darby3bef1c32012-11-03 17:00:06 +00004858 if (reset)
Takashi Iwai2f728532008-09-25 16:32:41 +02004859 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06004860 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02004861}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004862
Takashi Iwai2f728532008-09-25 16:32:41 +02004863static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
4864{
4865 snd_hda_codec_cleanup_stream(codec, nid);
4866 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004867 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004868 for (d = codec->slave_dig_outs; *d; d++)
4869 snd_hda_codec_cleanup_stream(codec, *d);
4870 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004871}
4872
Takashi Iwaid5191e52009-11-16 14:58:17 +01004873/**
4874 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
4875 * @bus: HD-audio bus
4876 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004877void snd_hda_bus_reboot_notify(struct hda_bus *bus)
4878{
4879 struct hda_codec *codec;
4880
4881 if (!bus)
4882 return;
4883 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02004884 if (hda_codec_is_power_on(codec) &&
4885 codec->patch_ops.reboot_notify)
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004886 codec->patch_ops.reboot_notify(codec);
4887 }
4888}
Takashi Iwai8f217a22009-11-10 18:26:12 +01004889EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01004890
Takashi Iwaid5191e52009-11-16 14:58:17 +01004891/**
4892 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004894int snd_hda_multi_out_dig_open(struct hda_codec *codec,
4895 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896{
Ingo Molnar62932df2006-01-16 16:34:20 +01004897 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02004898 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
4899 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02004900 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01004902 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903 return 0;
4904}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004905EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906
Takashi Iwaid5191e52009-11-16 14:58:17 +01004907/**
4908 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
4909 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004910int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
4911 struct hda_multi_out *mout,
4912 unsigned int stream_tag,
4913 unsigned int format,
4914 struct snd_pcm_substream *substream)
4915{
4916 mutex_lock(&codec->spdif_mutex);
4917 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
4918 mutex_unlock(&codec->spdif_mutex);
4919 return 0;
4920}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004921EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004922
Takashi Iwaid5191e52009-11-16 14:58:17 +01004923/**
4924 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
4925 */
Takashi Iwai9411e212009-02-13 11:32:28 +01004926int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
4927 struct hda_multi_out *mout)
4928{
4929 mutex_lock(&codec->spdif_mutex);
4930 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4931 mutex_unlock(&codec->spdif_mutex);
4932 return 0;
4933}
4934EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
4935
Takashi Iwaid5191e52009-11-16 14:58:17 +01004936/**
4937 * snd_hda_multi_out_dig_close - release the digital out stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004939int snd_hda_multi_out_dig_close(struct hda_codec *codec,
4940 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941{
Ingo Molnar62932df2006-01-16 16:34:20 +01004942 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01004944 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945 return 0;
4946}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004947EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948
Takashi Iwaid5191e52009-11-16 14:58:17 +01004949/**
4950 * snd_hda_multi_out_analog_open - open analog outputs
4951 *
4952 * Open analog outputs and set up the hw-constraints.
4953 * If the digital outputs can be opened as slave, open the digital
4954 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004956int snd_hda_multi_out_analog_open(struct hda_codec *codec,
4957 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01004958 struct snd_pcm_substream *substream,
4959 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960{
Takashi Iwai9a081602008-02-12 18:37:26 +01004961 struct snd_pcm_runtime *runtime = substream->runtime;
4962 runtime->hw.channels_max = mout->max_channels;
4963 if (mout->dig_out_nid) {
4964 if (!mout->analog_rates) {
4965 mout->analog_rates = hinfo->rates;
4966 mout->analog_formats = hinfo->formats;
4967 mout->analog_maxbps = hinfo->maxbps;
4968 } else {
4969 runtime->hw.rates = mout->analog_rates;
4970 runtime->hw.formats = mout->analog_formats;
4971 hinfo->maxbps = mout->analog_maxbps;
4972 }
4973 if (!mout->spdif_rates) {
4974 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
4975 &mout->spdif_rates,
4976 &mout->spdif_formats,
4977 &mout->spdif_maxbps);
4978 }
4979 mutex_lock(&codec->spdif_mutex);
4980 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02004981 if ((runtime->hw.rates & mout->spdif_rates) &&
4982 (runtime->hw.formats & mout->spdif_formats)) {
4983 runtime->hw.rates &= mout->spdif_rates;
4984 runtime->hw.formats &= mout->spdif_formats;
4985 if (mout->spdif_maxbps < hinfo->maxbps)
4986 hinfo->maxbps = mout->spdif_maxbps;
4987 } else {
4988 mout->share_spdif = 0;
4989 /* FIXME: need notify? */
4990 }
Takashi Iwai9a081602008-02-12 18:37:26 +01004991 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02004992 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01004993 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994 return snd_pcm_hw_constraint_step(substream->runtime, 0,
4995 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
4996}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004997EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998
Takashi Iwaid5191e52009-11-16 14:58:17 +01004999/**
5000 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
5001 *
5002 * Set up the i/o for analog out.
5003 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005005int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
5006 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007 unsigned int stream_tag,
5008 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01005009 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010{
Takashi Iwaidda14412011-05-02 11:29:30 +02005011 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012 int chs = substream->runtime->channels;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005013 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014 int i;
5015
Ingo Molnar62932df2006-01-16 16:34:20 +01005016 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005017 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Takashi Iwai9a081602008-02-12 18:37:26 +01005018 if (mout->dig_out_nid && mout->share_spdif &&
5019 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005020 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02005021 snd_hda_is_supported_format(codec, mout->dig_out_nid,
5022 format) &&
Stephen Warren7c935972011-06-01 11:14:17 -06005023 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005024 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005025 setup_dig_out_stream(codec, mout->dig_out_nid,
5026 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027 } else {
5028 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02005029 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030 }
5031 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005032 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033
5034 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005035 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
5036 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005037 if (!mout->no_share_stream &&
5038 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005039 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005040 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
5041 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005042 /* extra outputs copied from front */
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005043 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5044 if (!mout->no_share_stream && mout->hp_out_nid[i])
5045 snd_hda_codec_setup_stream(codec,
5046 mout->hp_out_nid[i],
5047 stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005048 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02005049 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01005050 snd_hda_codec_setup_stream(codec,
5051 mout->extra_out_nid[i],
5052 stream_tag, 0, format);
5053
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054 /* surrounds */
5055 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02005056 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005057 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5058 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005059 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005060 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5061 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005062 }
5063 return 0;
5064}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005065EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066
Takashi Iwaid5191e52009-11-16 14:58:17 +01005067/**
5068 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005070int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
5071 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072{
Takashi Iwaidda14412011-05-02 11:29:30 +02005073 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074 int i;
5075
5076 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01005077 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01005079 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005080 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5081 if (mout->hp_out_nid[i])
5082 snd_hda_codec_cleanup_stream(codec,
5083 mout->hp_out_nid[i]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005084 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
5085 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01005086 snd_hda_codec_cleanup_stream(codec,
5087 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01005088 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02005090 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005091 mout->dig_out_used = 0;
5092 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005093 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005094 return 0;
5095}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005096EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097
Takashi Iwai47408602012-04-20 13:06:53 +02005098/**
5099 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
5100 *
5101 * Guess the suitable VREF pin bits to be set as the pin-control value.
5102 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
5103 */
5104unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
5105{
5106 unsigned int pincap;
5107 unsigned int oldval;
5108 oldval = snd_hda_codec_read(codec, pin, 0,
5109 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5110 pincap = snd_hda_query_pin_caps(codec, pin);
5111 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5112 /* Exception: if the default pin setup is vref50, we give it priority */
5113 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
5114 return AC_PINCTL_VREF_80;
5115 else if (pincap & AC_PINCAP_VREF_50)
5116 return AC_PINCTL_VREF_50;
5117 else if (pincap & AC_PINCAP_VREF_100)
5118 return AC_PINCTL_VREF_100;
5119 else if (pincap & AC_PINCAP_VREF_GRD)
5120 return AC_PINCTL_VREF_GRD;
5121 return AC_PINCTL_VREF_HIZ;
5122}
5123EXPORT_SYMBOL_HDA(snd_hda_get_default_vref);
5124
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005125int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
5126 unsigned int val, bool cached)
5127{
5128 if (val) {
5129 unsigned int cap = snd_hda_query_pin_caps(codec, pin);
Takashi Iwai6942c102012-04-20 13:08:40 +02005130 if (cap && (val & AC_PINCTL_OUT_EN)) {
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005131 if (!(cap & AC_PINCAP_OUT))
5132 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
5133 else if ((val & AC_PINCTL_HP_EN) &&
5134 !(cap & AC_PINCAP_HP_DRV))
5135 val &= ~AC_PINCTL_HP_EN;
5136 }
Takashi Iwai6942c102012-04-20 13:08:40 +02005137 if (cap && (val & AC_PINCTL_IN_EN)) {
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005138 if (!(cap & AC_PINCAP_IN))
5139 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
5140 }
5141 }
5142 if (cached)
5143 return snd_hda_codec_update_cache(codec, pin, 0,
5144 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5145 else
5146 return snd_hda_codec_write(codec, pin, 0,
5147 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5148}
5149EXPORT_SYMBOL_HDA(_snd_hda_set_pin_ctl);
5150
Takashi Iwai990061c2010-09-09 22:08:44 +02005151/**
5152 * snd_hda_add_imux_item - Add an item to input_mux
5153 *
5154 * When the same label is used already in the existing items, the number
5155 * suffix is appended to the label. This label index number is stored
5156 * to type_idx when non-NULL pointer is given.
5157 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02005158int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
5159 int index, int *type_idx)
5160{
5161 int i, label_idx = 0;
5162 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
5163 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
5164 return -EINVAL;
5165 }
5166 for (i = 0; i < imux->num_items; i++) {
5167 if (!strncmp(label, imux->items[i].label, strlen(label)))
5168 label_idx++;
5169 }
5170 if (type_idx)
5171 *type_idx = label_idx;
5172 if (label_idx > 0)
5173 snprintf(imux->items[imux->num_items].label,
5174 sizeof(imux->items[imux->num_items].label),
5175 "%s %d", label, label_idx);
5176 else
5177 strlcpy(imux->items[imux->num_items].label, label,
5178 sizeof(imux->items[imux->num_items].label));
5179 imux->items[imux->num_items].index = index;
5180 imux->num_items++;
5181 return 0;
5182}
5183EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005184
Takashi Iwai4a471b72005-12-07 13:56:29 +01005185
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186#ifdef CONFIG_PM
5187/*
5188 * power management
5189 */
5190
5191/**
5192 * snd_hda_suspend - suspend the codecs
5193 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194 *
5195 * Returns 0 if successful.
5196 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02005197int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005198{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005199 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005200
Takashi Iwai0ba21762007-04-16 11:29:14 +02005201 list_for_each_entry(codec, &bus->codec_list, list) {
David Henningsson26a6cb62012-10-09 15:04:21 +02005202 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005203 if (hda_codec_is_power_on(codec))
Dylan Reidd17344b2012-09-28 15:57:01 -07005204 hda_call_codec_suspend(codec, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205 }
5206 return 0;
5207}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005208EXPORT_SYMBOL_HDA(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209
5210/**
5211 * snd_hda_resume - resume the codecs
5212 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005213 *
5214 * Returns 0 if successful.
5215 */
5216int snd_hda_resume(struct hda_bus *bus)
5217{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005218 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005219
Takashi Iwai0ba21762007-04-16 11:29:14 +02005220 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai7f308302012-05-08 16:52:23 +02005221 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005223 return 0;
5224}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005225EXPORT_SYMBOL_HDA(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005226#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005227
5228/*
5229 * generic arrays
5230 */
5231
Takashi Iwaid5191e52009-11-16 14:58:17 +01005232/**
5233 * snd_array_new - get a new element from the given array
5234 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005235 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01005236 * Get a new element from the given array. If it exceeds the
5237 * pre-allocated array size, re-allocate the array.
5238 *
5239 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02005240 */
5241void *snd_array_new(struct snd_array *array)
5242{
Takashi Iwai12f17712012-10-10 08:59:14 +02005243 if (snd_BUG_ON(!array->elem_size))
5244 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005245 if (array->used >= array->alloced) {
5246 int num = array->alloced + array->alloc_align;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005247 int size = (num + 1) * array->elem_size;
Takashi Iwai00ef9612011-07-14 15:57:27 +02005248 int oldsize = array->alloced * array->elem_size;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01005249 void *nlist;
5250 if (snd_BUG_ON(num >= 4096))
5251 return NULL;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005252 nlist = krealloc(array->list, size, GFP_KERNEL);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005253 if (!nlist)
5254 return NULL;
Takashi Iwai00ef9612011-07-14 15:57:27 +02005255 memset(nlist + oldsize, 0, size - oldsize);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005256 array->list = nlist;
5257 array->alloced = num;
5258 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01005259 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005260}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005261EXPORT_SYMBOL_HDA(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005262
Takashi Iwaid5191e52009-11-16 14:58:17 +01005263/**
5264 * snd_array_free - free the given array elements
5265 * @array: the array object
5266 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005267void snd_array_free(struct snd_array *array)
5268{
5269 kfree(array->list);
5270 array->used = 0;
5271 array->alloced = 0;
5272 array->list = NULL;
5273}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005274EXPORT_SYMBOL_HDA(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01005275
Takashi Iwaid5191e52009-11-16 14:58:17 +01005276/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01005277 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5278 * @pcm: PCM caps bits
5279 * @buf: the string buffer to write
5280 * @buflen: the max buffer length
5281 *
5282 * used by hda_proc.c and hda_eld.c
5283 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005284void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5285{
5286 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5287 int i, j;
5288
5289 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5290 if (pcm & (AC_SUPPCM_BITS_8 << i))
5291 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5292
5293 buf[j] = '\0'; /* necessary when j == 0 */
5294}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005295EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005296
5297MODULE_DESCRIPTION("HDA codec core");
5298MODULE_LICENSE("GPL");